public string Read(NetMessage msg) { bool encoded = msg.ReadBoolean(); int idx = (int)msg.Read7BitEncodedUInt(); string retval; if (!encoded) { // insert into table retval = msg.ReadString(); while (m_data.Count <= idx) { m_data.Add(null); // dummy values } m_data[idx] = retval; return(retval); } // look in table if (m_data.Count <= idx) { return(null); } return(m_data[idx]); }
public string Read(NetMessage msg) { bool encoded = msg.ReadBoolean(); int idx = (int)msg.Read7BitEncodedUInt(); string retval; if (!encoded) { // insert into table retval = msg.ReadString(); while(m_data.Count <= idx) m_data.Add(null); // dummy values m_data[idx] = retval; return retval; } // look in table if (m_data.Count <= idx) return null; return m_data[idx]; }
internal void HandleOptimizeInfo(double now, NetMessage pongMessage) { float optimizeMillis = (float)pongMessage.Read7BitEncodedUInt(); m_connection.Configuration.OptimizeSettings(optimizeMillis / 1000.0f); }
internal static void HandleConnect(NetMessage connectMsg, NetServer server, IPEndPoint senderEndpoint) { NetMessage response; if (server.NumConnected >= server.Configuration.MaximumConnections) { // server full response = new NetMessage(NetMessageType.Handshake, "Server full".Length + 1); response.Write((byte)NetHandshakeType.Disconnected); response.Write("Server full"); server.SendSingleMessageAtOnce(response, null, senderEndpoint); return; } ushort remoteNow = connectMsg.ReadUInt16(); //server.Log.Debug("Setting remote clock based on guess of 50 ms lag..."); int remoteClockOffset = NetTime.CalculateOffset(NetTime.Now, remoteNow, 0.05); // assume 50ms... // read symmetric key int encSymKeyLen = connectMsg.ReadUInt16(); byte[] encSymKey = null; if (encSymKeyLen > 0) { encSymKey = connectMsg.ReadBytes(encSymKeyLen); } // read custom data int cdLen = (int)connectMsg.Read7BitEncodedUInt(); byte[] customData = null; if (cdLen > 0) { customData = connectMsg.ReadBytes(cdLen); } string failReason = null; bool ok = server.ApproveConnection(senderEndpoint, customData, out failReason); if (!ok) { if (!string.IsNullOrEmpty(failReason)) { // send disconnect reason; unencrypted, client can handle it because status is connecting response = new NetMessage(NetMessageType.Handshake, failReason.Length + 3); response.Write((byte)NetHandshakeType.Disconnected); response.Write(failReason); server.SendSingleMessageAtOnce(response, null, senderEndpoint); } // connection not approved return; } NetConnection connection = server.AddConnection(senderEndpoint, remoteClockOffset); if (connection == null) { return; // uh oh } if (encSymKeyLen > 0) { byte[] symKey = connection.m_encryption.DecryptRSA(encSymKey); if (symKey == null) { // send disconnect unencrypted, client can handle it because status is connecting string bye = "RSA failed; are you using correct public key?"; response = new NetMessage(NetMessageType.Handshake, bye.Length + 3); response.Write((byte)NetHandshakeType.Disconnected); response.Write(bye); server.SendSingleMessageAtOnce(response, null, senderEndpoint); server.Log.Warning("Failed to decrypt RSA encrypted symmetric key from " + senderEndpoint); return; } connection.m_encryption.SetSymmetricKey(symKey); server.Log.Debug("Received Connect containing key: " + Convert.ToBase64String(symKey)); } else { if (server.Configuration.UsesEncryption) { server.Log.Warning("Client tried to connect without encryption from " + senderEndpoint); // denied response = new NetMessage(NetMessageType.Handshake, "Encryption required".Length + 1); response.Write((byte)NetHandshakeType.Disconnected); response.Write("Encryption required"); server.SendSingleMessageAtOnce(response, null, senderEndpoint); return; } server.Log.Debug("Received Connect - using unencrypted connection!"); } // send connect response int bytesSent = SendConnectResponse(server, connection, senderEndpoint); if (connection != null) { // account for connectresponse connection.Statistics.PacketsSent++; connection.Statistics.MessagesSent++; connection.Statistics.BytesSent += bytesSent; } }
internal static void HandleConnect(NetMessage connectMsg, NetServer server, IPEndPoint senderEndpoint) { NetMessage response; if (server.NumConnected >= server.Configuration.MaximumConnections) { // server full response = new NetMessage(NetMessageType.Handshake, "Server full".Length + 1); response.Write((byte)NetHandshakeType.Disconnected); response.Write("Server full"); server.SendSingleMessageAtOnce(response, null, senderEndpoint); return; } ushort remoteNow = connectMsg.ReadUInt16(); //server.Log.Debug("Setting remote clock based on guess of 50 ms lag..."); int remoteClockOffset = NetTime.CalculateOffset(NetTime.Now, remoteNow, 0.05); // assume 50ms... // read symmetric key int encSymKeyLen = connectMsg.ReadUInt16(); byte[] encSymKey = null; if (encSymKeyLen > 0) encSymKey = connectMsg.ReadBytes(encSymKeyLen); // read custom data int cdLen = (int)connectMsg.Read7BitEncodedUInt(); byte[] customData = null; if (cdLen > 0) customData = connectMsg.ReadBytes(cdLen); string failReason = null; bool ok = server.ApproveConnection(senderEndpoint, customData, out failReason); if (!ok) { if (!string.IsNullOrEmpty(failReason)) { // send disconnect reason; unencrypted, client can handle it because status is connecting response = new NetMessage(NetMessageType.Handshake, failReason.Length + 3); response.Write((byte)NetHandshakeType.Disconnected); response.Write(failReason); server.SendSingleMessageAtOnce(response, null, senderEndpoint); } // connection not approved return; } NetConnection connection = server.AddConnection(senderEndpoint, remoteClockOffset); if (connection == null) return; // uh oh if (encSymKeyLen > 0) { byte[] symKey = connection.m_encryption.DecryptRSA(encSymKey); if (symKey == null) { // send disconnect unencrypted, client can handle it because status is connecting string bye = "RSA failed; are you using correct public key?"; response = new NetMessage(NetMessageType.Handshake, bye.Length + 3); response.Write((byte)NetHandshakeType.Disconnected); response.Write(bye); server.SendSingleMessageAtOnce(response, null, senderEndpoint); server.Log.Warning("Failed to decrypt RSA encrypted symmetric key from " + senderEndpoint); return; } connection.m_encryption.SetSymmetricKey(symKey); server.Log.Debug("Received Connect containing key: " + Convert.ToBase64String(symKey)); } else { if (server.Configuration.UsesEncryption) { server.Log.Warning("Client tried to connect without encryption from " + senderEndpoint); // denied response = new NetMessage(NetMessageType.Handshake, "Encryption required".Length + 1); response.Write((byte)NetHandshakeType.Disconnected); response.Write("Encryption required"); server.SendSingleMessageAtOnce(response, null, senderEndpoint); return; } server.Log.Debug("Received Connect - using unencrypted connection!"); } // send connect response int bytesSent = SendConnectResponse(server, connection, senderEndpoint); if (connection != null) { // account for connectresponse connection.Statistics.PacketsSent++; connection.Statistics.MessagesSent++; connection.Statistics.BytesSent += bytesSent; } }