internal static void Shutdown(RUDPSocket rudp) { if (rudp._status == RUDPSocketStatus.Accepting) { rudp.Reset(RUDPSocketStatus.Closed); return; } if (rudp._status == RUDPSocketStatus.Closed || rudp._isShutingDown) return; if (rudp._status == RUDPSocketStatus.Closing || rudp._status == RUDPSocketStatus.ClosingACKed) return; if (rudp._status == RUDPSocketStatus.Connecting) { rudp.Reset(RUDPSocketStatus.Closed); return; } //---- Send the tear down message //-- Update the status rudp._isShutingDown = true; rudp._status = RUDPSocketStatus.Closing; //-- Wait for sending while (!rudp._controlWindow.CanSend(0)) { if (rudp._status != RUDPSocketStatus.Closing) return; Thread.Sleep(100); } //-- Send the tear down message PushPacketToSend(rudp, true, RUDPPacketChannel.TearDown, null, 0, 0); //---- Currently closing the connection, wait for the end of the operation long startTime = HiResTimer.MicroSeconds; //-- Wait until closed // Wait until "ClosingACKed" // Wait until we have receive the "TearDown" message too and send the ACK // Wait until "Time out" while (rudp._status == RUDPSocketStatus.Closing && rudp._outgoingPackets.Count > 0 && (HiResTimer.MicroSeconds - startTime) < rudp._sto) { Thread.Sleep(100); } //---- Set the status as closed rudp.Reset(RUDPSocketStatus.Closed); //---- Notify rudp._physical.OnDisconnected(rudp, RUDPSocketError.Shutdown); }
internal static RUDPSocketError BeginConnect(RUDPSocket rudp, int timeOut) { Trace("Connecting to :" + rudp._remoteEndPoint); if (rudp._status == RUDPSocketStatus.Connected) return RUDPSocketError.IsConnected; if (rudp._status == RUDPSocketStatus.Connecting) return RUDPSocketError.AlreadyInProgress; //---- Set the status rudp.Reset(RUDPSocketStatus.Connecting); //---- Register for the stack RUDPStack.RegisterRUDPSocket(rudp); //---- Send a ping if (rudp.IsRendezVousMode) PushPacketToSend(rudp, true, RUDPPacketChannel.PingRendezVous, null, 0, 0); else PushPacketToSend(rudp, true, RUDPPacketChannel.Ping, null, 0, 0); return RUDPSocketError.Success; }
/// <summary> /// Close the socket. Send the tear down message. /// </summary> internal static void Close(RUDPSocket rudp) { if (rudp._status == RUDPSocketStatus.Closed) return; if (rudp._status == RUDPSocketStatus.Accepting) { rudp.Reset(RUDPSocketStatus.Closed); return; } AsyncShutdown(rudp); }
internal static void OnDisconnected(RUDPSocket rudp, DisconnectionReason reason) { if (rudp._status == RUDPSocketStatus.Closed) return; //---- Reset rudp._outgoingPacketsLock.EnterWriteLock(); rudp._outgoingPackets.Clear(); rudp._outgoingPacketsLock.ExitWriteLock(); rudp.Reset(RUDPSocketStatus.Closed); //---- Notify if (reason != DisconnectionReason.ConnectionClosed) { RUDPSocketError error = RUDPSocketError.ConnectionReset; if (reason == DisconnectionReason.SocketError) error = RUDPSocketError.SocketError; if (reason == DisconnectionReason.TimeOut) error = RUDPSocketError.ConnectionReset; rudp._physical.OnDisconnected(rudp, error); } }