/// <summary> /// Denies this connection; disconnecting it /// </summary> /// <param name="reason">The stated reason for the disconnect, readable as a string/message in the StatusChanged message on the remote host</param> public void Deny(NetReason reason) { // send disconnect; remove from handshakes SendDisconnect(reason, false); // remove from handshakes lock (m_peer.m_handshakes) m_peer.m_handshakes.Remove(m_remoteEndPoint); }
/// <summary> /// Disconnects all active connections and closes the socket /// </summary> public void Shutdown(NetReason bye, string debugMessage = default) { // called on user thread if (default == m_socket) { // already shut down return; } LogDebug($"Shutdown requested ({debugMessage ?? "reason"})"); m_shutdownReason = bye; m_status = NetPeerStatus.ShutdownRequested; }
/// <summary> /// Creates a new message for sending and writes the contents of the reason to it /// </summary> /// <param name="reason">the reason (text or message) to copy</param> /// <returns></returns> public NetOutgoingMessage CreateMessage(NetReason reason) { var newMessage = CreateMessage(reason.Length); if (reason.Message != null) { Buffer.BlockCopy(reason.Message.m_data, 0, newMessage.m_data, 0, reason.Message.LengthBytes); newMessage.m_bitLength = reason.Message.m_bitLength; } else { newMessage.Write(reason.Text); } return(newMessage); }
internal void SetStatus(NetConnectionStatus status, NetReason reason) { // user or library thread m_status = status; if (m_status == NetConnectionStatus.Connected) { m_timeoutDeadline = NetTime.Now + m_peerConfiguration.m_connectionTimeout; m_peer.LogVerbose("Timeout deadline initialized to " + m_timeoutDeadline); } if (m_peerConfiguration.IsMessageTypeEnabled(NetIncomingMessageType.StatusChanged)) { if (m_outputtedStatus != status) { var outgoingMessage = reason.Message; var info = m_peer.CreateIncomingMessage(NetIncomingMessageType.StatusChanged, 1 + reason.Length); info.Write((byte)m_status); if (outgoingMessage != null) { Buffer.BlockCopy(outgoingMessage.m_data, 0, info.m_data, 1, outgoingMessage.LengthBytes); info.m_bitLength = outgoingMessage.m_bitLength; } else { info.Write(reason.Text); } info.m_senderConnection = this; info.m_senderEndPoint = m_remoteEndPoint; m_peer.ReleaseMessage(info); m_outputtedStatus = status; } } else { // app dont want those messages, update visible status immediately m_outputtedStatus = m_status; m_visibleStatus = m_status; } }
internal void SendDisconnect(NetReason reason, bool onLibraryThread) { if (onLibraryThread) { m_peer.VerifyNetworkThread(); } var message = m_peer.CreateMessage(reason); message.m_messageType = NetMessageType.Disconnect; Interlocked.Increment(ref message.m_recyclingCount); if (onLibraryThread) { m_peer.SendLibrary(message, m_remoteEndPoint); } else { m_peer.m_unsentUnconnectedMessages.Enqueue(new NetTuple <NetEndPoint, NetOutgoingMessage>(m_remoteEndPoint, message)); } }
internal void ExecuteDisconnect(NetReason reason, bool sendByeMessage) { m_peer.VerifyNetworkThread(); // clear send queues for (int i = 0; i < m_sendChannels.Length; i++) { NetSenderChannelBase channel = m_sendChannels[i]; if (channel != null) { channel.Reset(); } } if (sendByeMessage) { SendDisconnect(reason, true); } if (m_status == NetConnectionStatus.ReceivedInitiation) { // nothing much has happened yet; no need to send disconnected status message m_status = NetConnectionStatus.Disconnected; } else { SetStatus(NetConnectionStatus.Disconnected, reason); } // in case we're still in handshake lock (m_peer.m_handshakes) m_peer.m_handshakes.Remove(m_remoteEndPoint); m_disconnectRequested = false; m_connectRequested = false; m_handshakeAttempts = 0; }
internal void Shutdown(NetReason reason) => ExecuteDisconnect(reason, true);