internal static int SendDisconnected(NetBase netBase, string reason, NetConnection connection) { NetMessage bye = new NetMessage(NetMessageType.Handshake, reason.Length + 3); bye.Write((byte)NetHandshakeType.Disconnected); bye.Write(reason); return(netBase.SendSingleMessageAtOnce(bye, connection, connection.RemoteEndpoint)); }
internal static int SendConnectResponse(NetBase netBase, NetConnection clientConnection, IPEndPoint remoteEndpoint) { double now = NetTime.Now; ushort nowEnc = NetTime.Encoded(now); NetMessage response = new NetMessage(NetMessageType.Handshake, 3); response.Write((byte)NetHandshakeType.ConnectResponse); response.Write(nowEnc); clientConnection.m_firstSentHandshake = now; clientConnection.m_lastSentHandshake = now; return(netBase.SendSingleMessageAtOnce(response, clientConnection, remoteEndpoint)); }
public void SendDelayedPackets(NetBase netBase, double now) { foreach (DelayedPacket pk in m_delayed) { if (now >= pk.DelayedUntil) { // send it! m_delaySent.Add(pk); try { //m_log.Debug("(sending delayed packet after " + (int)(pk.DelayAmount * 1000.0f) + " ms)"); netBase.m_socket.SendTo(pk.Data, 0, pk.Data.Length, SocketFlags.None, pk.RemoteEP); } catch (SocketException sex) { if (sex.SocketErrorCode == SocketError.ConnectionReset || sex.SocketErrorCode == SocketError.ConnectionRefused || sex.SocketErrorCode == SocketError.ConnectionAborted) { m_log.Warning("Remote socket forcefully closed: " + sex.SocketErrorCode); //if (connection != null) // connection.Disconnect("Socket forcefully closed: " + sex.SocketErrorCode); continue; } m_log.Warning("Execute SocketException: " + sex.SocketErrorCode); continue; } } } if (m_delaySent.Count > 0) { foreach (DelayedPacket pk in m_delaySent) { m_delayed.Remove(pk); } m_delaySent.Clear(); } }
public void SendDelayedPackets(NetBase netBase, double now) { foreach(DelayedPacket pk in m_delayed) { if (now >= pk.DelayedUntil) { // send it! m_delaySent.Add(pk); try { //m_log.Debug("(sending delayed packet after " + (int)(pk.DelayAmount * 1000.0f) + " ms)"); netBase.m_socket.SendTo(pk.Data, 0, pk.Data.Length, SocketFlags.None, pk.RemoteEP); } catch (SocketException sex) { if (sex.SocketErrorCode == SocketError.ConnectionReset || sex.SocketErrorCode == SocketError.ConnectionRefused || sex.SocketErrorCode == SocketError.ConnectionAborted) { m_log.Warning("Remote socket forcefully closed: " + sex.SocketErrorCode); //if (connection != null) // connection.Disconnect("Socket forcefully closed: " + sex.SocketErrorCode); continue; } m_log.Warning("Execute SocketException: " + sex.SocketErrorCode); continue; } } } if (m_delaySent.Count > 0) { foreach (DelayedPacket pk in m_delaySent) m_delayed.Remove(pk); m_delaySent.Clear(); } }
internal NetConnection(NetBase parent, IPEndPoint remote) { m_parent = parent; m_remoteEndpoint = remote; m_nextSequenceNumber = new ushort[NetConstants.NumSequenceChannels]; m_sequenceHandler = new NetSequenceHandler(); m_unsentMessages = new Queue <NetMessage>(10); m_receivedMessages = new Queue <NetMessage>(10); m_withheldMessages = new List <NetMessage>(); m_unsentAcknowledges = new Queue <NetMessage>(10); m_lastHeardFromRemote = (float)NetTime.Now; m_reusedAckMessage = new NetMessage(NetMessageType.Acknowledge, null); m_savedReliableMessages = new List <NetMessage> [NetConstants.NumSequenceChannels]; Configuration = new NetConnectionConfiguration(this); m_ping = new NetPing(this); Statistics = new NetStatistics(this); m_stringTable = new NetStringTable(); m_encryption = new NetEncryption(); if (parent.Configuration.UsesEncryption) { m_encryption.SetRSAKey(parent.Configuration.ServerPublicKeyBytes, parent.Configuration.ServerPrivateKeyBytes); } }
internal NetConnection(NetBase parent, IPEndPoint remote) { m_parent = parent; m_remoteEndpoint = remote; m_nextSequenceNumber = new ushort[NetConstants.NumSequenceChannels]; m_sequenceHandler = new NetSequenceHandler(); m_unsentMessages = new Queue<NetMessage>(10); m_receivedMessages = new Queue<NetMessage>(10); m_withheldMessages = new List<NetMessage>(); m_unsentAcknowledges = new Queue<NetMessage>(10); m_lastHeardFromRemote = (float)NetTime.Now; m_reusedAckMessage = new NetMessage(NetMessageType.Acknowledge, null); m_savedReliableMessages = new List<NetMessage>[NetConstants.NumSequenceChannels]; Configuration = new NetConnectionConfiguration(this); m_ping = new NetPing(this); Statistics = new NetStatistics(this); m_stringTable = new NetStringTable(); m_encryption = new NetEncryption(); if (parent.Configuration.UsesEncryption) m_encryption.SetRSAKey(parent.Configuration.ServerPublicKeyBytes, parent.Configuration.ServerPrivateKeyBytes); }
public int ExecuteSend(NetBase netBase, NetBuffer buffer, NetConnection connection, IPEndPoint remoteEP) { int len = buffer.LengthBytes; #if DEBUG if (connection != null) { NetConnectionConfiguration config = connection.Configuration; if (config.LossChance > 0.0f && NetRandom.Default.Chance(config.LossChance)) { //m_log.Debug("(simulating loss of sent packet)"); return(len); } if (config.LagDelayChance > 0.0f && NetRandom.Default.Chance(config.LagDelayChance)) { float delayAmount = config.LagDelayMinimum + (NetRandom.Default.NextFloat() * config.LagDelayVariance); DelayedPacket pk = new DelayedPacket(); pk.Data = new byte[len]; Array.Copy(buffer.Data, pk.Data, buffer.LengthBytes); pk.DelayAmount = delayAmount; pk.DelayedUntil = NetTime.Now + delayAmount; pk.RemoteEP = remoteEP; m_delayed.Add(pk); //m_log.Debug("(queueing packet for " + (int)(pk.DelayAmount * 1000.0f) + " ms)"); return(len); } } #endif try { int bytesSent = netBase.m_socket.SendTo(buffer.Data, 0, len, SocketFlags.None, remoteEP); m_log.Verbose(string.Format(CultureInfo.InvariantCulture, "Sent {0} bytes to {1}", bytesSent, remoteEP)); #if DEBUG if (connection != null) { NetConnectionConfiguration config = connection.Configuration; if (NetRandom.Default.Chance(config.DuplicatedPacketChance)) { m_log.Debug("(simulating send packet duplication)"); netBase.m_socket.SendTo(buffer.Data, 0, buffer.LengthBytes, SocketFlags.None, remoteEP); } } #endif return(bytesSent); } catch (SocketException sex) { if (sex.SocketErrorCode == SocketError.ConnectionReset || sex.SocketErrorCode == SocketError.ConnectionRefused || sex.SocketErrorCode == SocketError.ConnectionAborted) { m_log.Warning("Remote socket forcefully closed: " + sex.SocketErrorCode); if (connection != null) { connection.Disconnect("Socket forcefully closed: " + sex.SocketErrorCode); } return(0); } m_log.Warning("Execute SocketException: " + sex.SocketErrorCode); return(0); } }
internal static int SendDisconnected(NetBase netBase, string reason, NetConnection connection) { NetMessage bye = new NetMessage(NetMessageType.Handshake, reason.Length + 3); bye.Write((byte)NetHandshakeType.Disconnected); bye.Write(reason); return netBase.SendSingleMessageAtOnce(bye, connection, connection.RemoteEndpoint); }
internal static int SendConnectResponse(NetBase netBase, NetConnection clientConnection, IPEndPoint remoteEndpoint) { double now = NetTime.Now; ushort nowEnc = NetTime.Encoded(now); NetMessage response = new NetMessage(NetMessageType.Handshake, 3); response.Write((byte)NetHandshakeType.ConnectResponse); response.Write(nowEnc); clientConnection.m_firstSentHandshake = now; clientConnection.m_lastSentHandshake = now; return netBase.SendSingleMessageAtOnce(response, clientConnection, remoteEndpoint); }
public int ExecuteSend(NetBase netBase, NetBuffer buffer, NetConnection connection, IPEndPoint remoteEP) { int len = buffer.LengthBytes; #if DEBUG if (connection != null) { NetConnectionConfiguration config = connection.Configuration; if (config.LossChance > 0.0f && NetRandom.Default.Chance(config.LossChance)) { //m_log.Debug("(simulating loss of sent packet)"); return len; } if (config.LagDelayChance > 0.0f && NetRandom.Default.Chance(config.LagDelayChance)) { float delayAmount = config.LagDelayMinimum + (NetRandom.Default.NextFloat() * config.LagDelayVariance); DelayedPacket pk = new DelayedPacket(); pk.Data = new byte[len]; Array.Copy(buffer.Data, pk.Data, buffer.LengthBytes); pk.DelayAmount = delayAmount; pk.DelayedUntil = NetTime.Now + delayAmount; pk.RemoteEP = remoteEP; m_delayed.Add(pk); //m_log.Debug("(queueing packet for " + (int)(pk.DelayAmount * 1000.0f) + " ms)"); return len; } } #endif try { int bytesSent = netBase.m_socket.SendTo(buffer.Data, 0, len, SocketFlags.None, remoteEP); m_log.Verbose(string.Format(CultureInfo.InvariantCulture, "Sent {0} bytes to {1}", bytesSent, remoteEP)); #if DEBUG if (connection != null) { NetConnectionConfiguration config = connection.Configuration; if (NetRandom.Default.Chance(config.DuplicatedPacketChance)) { m_log.Debug("(simulating send packet duplication)"); netBase.m_socket.SendTo(buffer.Data, 0, buffer.LengthBytes, SocketFlags.None, remoteEP); } } #endif return bytesSent; } catch (SocketException sex) { if (sex.SocketErrorCode == SocketError.ConnectionReset || sex.SocketErrorCode == SocketError.ConnectionRefused || sex.SocketErrorCode == SocketError.ConnectionAborted) { m_log.Warning("Remote socket forcefully closed: " + sex.SocketErrorCode); if (connection != null) connection.Disconnect("Socket forcefully closed: " + sex.SocketErrorCode); return 0; } m_log.Warning("Execute SocketException: " + sex.SocketErrorCode); return 0; } }