1 /******************************************************************************
4 Copyright 1993, 1998 The Open Group
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 Except as contained in this notice, the name of The Open Group shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from The Open Group.
26 Author: Ralph Mor, X Consortium
27 ******************************************************************************/
32 #include <X11/ICE/ICElib.h>
33 #include "ICElibint.h"
34 #include <X11/Xtrans/Xtrans.h>
43 if (iceConn->proto_ref_count == 0 || iceConn->process_msg_info == NULL ||
44 majorOpcode < 1 || majorOpcode > _IceLastMajorOpcode)
51 * Make sure this majorOpcode is really being used.
56 for (i = iceConn->his_min_opcode; i <= iceConn->his_max_opcode; i++)
58 if (iceConn->process_msg_info[
59 i - iceConn->his_min_opcode].in_use &&
60 iceConn->process_msg_info[
61 i - iceConn->his_min_opcode].my_opcode == majorOpcode)
65 if (i > iceConn->his_max_opcode)
72 * OK, we can shut down the protocol.
75 iceConn->process_msg_info[
76 i - iceConn->his_min_opcode].in_use = False;
77 iceConn->proto_ref_count--;
87 IceSetShutdownNegotiation (
92 iceConn->skip_want_to_close = negotiate ? False : True;
98 IceCheckShutdownNegotiation (
102 return (iceConn->skip_want_to_close ? False : True);
112 int refCountReachedZero;
113 IceCloseStatus status;
116 * If this connection object was never valid, we can close
117 * it right now. This happens if IceAcceptConnection was
118 * called, but after calling IceProcessMessages several times
119 * the connection was rejected (because of authentication or
120 * some other reason).
123 if (iceConn->listen_obj &&
124 iceConn->connection_status != IceConnectAccepted)
126 _IceConnectionClosed (iceConn); /* invoke watch procs */
127 _IceFreeConnection (iceConn);
128 return (IceClosedNow);
132 /*---------------------------------------------------------------
136 A = Invoke Watch Procedures
137 B = Set free-asap bit
139 D = Initialize shutdown negotiation
145 IO free- dispatch protocol shutdown
146 error asap bit level refcount negotiation ACTION
147 occured set reached 0 reached 0
182 ---------------------------------------------------------------*/
184 if (iceConn->open_ref_count > 0)
185 iceConn->open_ref_count--;
187 refCountReachedZero = iceConn->open_ref_count == 0 &&
188 iceConn->proto_ref_count == 0;
190 status = IceConnectionInUse;
192 if (!iceConn->free_asap && (!iceConn->io_ok ||
193 (iceConn->io_ok && refCountReachedZero &&
194 iceConn->skip_want_to_close)))
197 * Invoke the watch procedures now.
200 _IceConnectionClosed (iceConn);
201 status = IceClosedNow; /* may be overwritten by IceClosedASAP */
204 if (!iceConn->free_asap && iceConn->dispatch_level != 0 &&
206 (iceConn->io_ok && refCountReachedZero &&
207 iceConn->skip_want_to_close)))
210 * Set flag so we free the connection as soon as possible.
213 iceConn->free_asap = True;
214 status = IceClosedASAP;
217 if (iceConn->io_ok && iceConn->dispatch_level == 0 &&
218 !iceConn->skip_want_to_close && refCountReachedZero)
221 * Initiate shutdown negotiation.
224 IceSimpleMessage (iceConn, 0, ICE_WantToClose);
227 iceConn->want_to_close = 1;
229 status = IceStartedShutdownNegotiation;
231 else if (iceConn->dispatch_level == 0 &&
232 (!iceConn->io_ok || (iceConn->io_ok && iceConn->skip_want_to_close &&
233 (iceConn->free_asap || (!iceConn->free_asap && refCountReachedZero)))))
236 * Free the connection.
239 _IceFreeConnection (iceConn);
241 status = IceClosedNow;
254 if (iceConn->listen_obj == NULL)
257 * This iceConn was created with IceOpenConnection.
258 * We keep track of all open IceConn's, so we need
259 * to remove it from the list.
264 for (i = 0; i < _IceConnectionCount; i++)
265 if (_IceConnectionObjs[i] == iceConn)
268 if (i < _IceConnectionCount)
270 if (i < _IceConnectionCount - 1)
272 _IceConnectionObjs[i] =
273 _IceConnectionObjs[_IceConnectionCount - 1];
274 _IceConnectionStrings[i] =
275 _IceConnectionStrings[_IceConnectionCount - 1];
278 _IceConnectionCount--;
282 if (iceConn->trans_conn)
283 _IceTransClose (iceConn->trans_conn);
285 if (iceConn->connection_string)
286 free (iceConn->connection_string);
289 free (iceConn->vendor);
291 if (iceConn->release)
292 free (iceConn->release);
295 free (iceConn->inbuf);
298 free (iceConn->outbuf);
300 if (iceConn->scratch)
301 free (iceConn->scratch);
303 if (iceConn->process_msg_info)
304 free ((char *) iceConn->process_msg_info);
306 if (iceConn->connect_to_you)
307 free ((char *) iceConn->connect_to_you);
309 if (iceConn->protosetup_to_you)
310 free ((char *) iceConn->protosetup_to_you);
312 if (iceConn->connect_to_me)
313 free ((char *) iceConn->connect_to_me);
315 if (iceConn->protosetup_to_me)
316 free ((char *) iceConn->protosetup_to_me);
318 free ((char *) iceConn);