// received a library message while Connected internal void ReceivedLibraryMessage(NetMessageType tp, int ptr, int payloadLength) { m_peer.VerifyNetworkThread(); float now = (float)NetTime.Now; switch (tp) { case NetMessageType.Disconnect: NetIncomingMessage msg = m_peer.SetupReadHelperMessage(ptr, payloadLength); ExecuteDisconnect(msg.ReadString(), false); break; case NetMessageType.Acknowledge: for (int i = 0; i < payloadLength; i += 3) { NetMessageType acktp = (NetMessageType)m_peer.m_receiveBuffer[ptr++]; // netmessagetype int seqNr = m_peer.m_receiveBuffer[ptr++]; seqNr |= (m_peer.m_receiveBuffer[ptr++] << 8); NetSenderChannelBase chan = m_sendChannels[(int)acktp - 1]; if (chan == null) { chan = CreateSenderChannel(acktp); } //m_peer.LogVerbose("Received ack for " + acktp + "#" + seqNr); chan.ReceiveAcknowledge(now, seqNr); } break; case NetMessageType.Ping: int pingNr = m_peer.m_receiveBuffer[ptr++]; SendPong(pingNr); break; case NetMessageType.Pong: NetIncomingMessage pmsg = m_peer.SetupReadHelperMessage(ptr, payloadLength); int pongNr = pmsg.ReadByte(); float remoteSendTime = pmsg.ReadSingle(); ReceivedPong(now, pongNr, remoteSendTime); break; case NetMessageType.ExpandMTURequest: SendMTUSuccess(payloadLength); break; case NetMessageType.ExpandMTUSuccess: NetIncomingMessage emsg = m_peer.SetupReadHelperMessage(ptr, payloadLength); int size = emsg.ReadInt32(); HandleExpandMTUSuccess(now, size); break; default: m_peer.LogWarning("Connection received unhandled library message: " + tp); break; } }
public void Decode(NetIncomingMessage im) { Id = im.ReadInt64(); MessageTime = im.ReadDouble(); /*this.Position = im.ReadVector3(); this.Velocity = im.ReadVector3();*/ Rotation = im.ReadSingle(); }
// received a library message while Connected internal void ReceivedLibraryMessage(NetMessageType tp, int ptr, int payloadLength) { m_peer.VerifyNetworkThread(); float now = (float)NetTime.Now; switch (tp) { case NetMessageType.Disconnect: NetIncomingMessage msg = m_peer.SetupReadHelperMessage(ptr, payloadLength); ExecuteDisconnect(msg.ReadString(), false); break; case NetMessageType.Acknowledge: for (int i = 0; i < payloadLength; i += 3) { NetMessageType acktp = (NetMessageType)m_peer.m_receiveBuffer[ptr++]; // netmessagetype int seqNr = m_peer.m_receiveBuffer[ptr++]; seqNr |= (m_peer.m_receiveBuffer[ptr++] << 8); // need to enqueue this and handle it in the netconnection heartbeat; so be able to send resends together with normal sends m_queuedIncomingAcks.Enqueue(new NetTuple <NetMessageType, int>(acktp, seqNr)); } break; case NetMessageType.Ping: int pingNr = m_peer.m_receiveBuffer[ptr++]; SendPong(pingNr); break; case NetMessageType.Pong: NetIncomingMessage pmsg = m_peer.SetupReadHelperMessage(ptr, payloadLength); int pongNr = pmsg.ReadByte(); float remoteSendTime = pmsg.ReadSingle(); ReceivedPong(now, pongNr, remoteSendTime); break; case NetMessageType.ExpandMTURequest: SendMTUSuccess(payloadLength); break; case NetMessageType.ExpandMTUSuccess: NetIncomingMessage emsg = m_peer.SetupReadHelperMessage(ptr, payloadLength); int size = emsg.ReadInt32(); HandleExpandMTUSuccess(now, size); break; case NetMessageType.NatIntroduction: // Unusual situation where server is actually already known, but got a nat introduction - oh well, lets handle it as usual m_peer.HandleNatIntroduction(ptr); break; default: m_peer.LogWarning("Connection received unhandled library message: " + tp); break; } }
private bool ValidateHandshakeData(int ptr, int payloadLength, out byte[] hail) { hail = null; // create temporary incoming message NetIncomingMessage msg = m_peer.SetupReadHelperMessage(ptr, payloadLength); try { string remoteAppIdentifier = msg.ReadString(); long remoteUniqueIdentifier = msg.ReadInt64(); InitializeRemoteTimeOffset(msg.ReadSingle()); int remainingBytes = payloadLength - (msg.PositionInBytes - ptr); if (remainingBytes > 0) { hail = msg.ReadBytes(remainingBytes); } if (remoteAppIdentifier != m_peer.m_configuration.AppIdentifier) { // wrong app identifier ExecuteDisconnect("Wrong application identifier!", true); return(false); } m_remoteUniqueIdentifier = remoteUniqueIdentifier; } catch (Exception ex) { // whatever; we failed ExecuteDisconnect("Handshake data validation failed", true); m_peer.LogWarning("ReadRemoteHandshakeData failed: " + ex.Message); return(false); } return(true); }
internal void ReceivedHandshake(double now, NetMessageType tp, int ptr, int payloadLength) { byte[] hail; switch (tp) { case NetMessageType.Connect: if (m_status == NetConnectionStatus.ReceivedInitiation) { // Whee! Server full has already been checked bool ok = ValidateHandshakeData(ptr, payloadLength, out hail); if (ok) { if (hail != null) { m_remoteHailMessage = m_peer.CreateIncomingMessage(NetIncomingMessageType.Data, hail); m_remoteHailMessage.LengthBits = (hail.Length * 8); } else { m_remoteHailMessage = null; } if (m_peerConfiguration.IsMessageTypeEnabled(NetIncomingMessageType.ConnectionApproval)) { // ok, let's not add connection just yet NetIncomingMessage appMsg = m_peer.CreateIncomingMessage(NetIncomingMessageType.ConnectionApproval, (m_remoteHailMessage == null ? 0 : m_remoteHailMessage.LengthBytes)); appMsg.m_receiveTime = now; appMsg.m_senderConnection = this; appMsg.m_senderEndPoint = this.m_remoteEndPoint; if (m_remoteHailMessage != null) { appMsg.Write(m_remoteHailMessage.m_data, 0, m_remoteHailMessage.LengthBytes); } SetStatus(NetConnectionStatus.RespondedAwaitingApproval, "Awaiting approval"); m_peer.ReleaseMessage(appMsg); return; } SendConnectResponse((float)now, true); } return; } if (m_status == NetConnectionStatus.RespondedAwaitingApproval) { m_peer.LogWarning("Ignoring multiple Connect() most likely due to a delayed Approval"); return; } if (m_status == NetConnectionStatus.RespondedConnect) { // our ConnectResponse must have been lost SendConnectResponse((float)now, true); return; } m_peer.LogDebug("Unhandled Connect: " + tp + ", status is " + m_status + " length: " + payloadLength); break; case NetMessageType.ConnectResponse: HandleConnectResponse(now, tp, ptr, payloadLength); break; case NetMessageType.ConnectionEstablished: switch (m_status) { case NetConnectionStatus.Connected: // ok... break; case NetConnectionStatus.Disconnected: case NetConnectionStatus.Disconnecting: case NetConnectionStatus.None: // too bad, almost made it break; case NetConnectionStatus.ReceivedInitiation: // uh, a little premature... ignore break; case NetConnectionStatus.InitiatedConnect: // weird, should have been RespondedConnect... break; case NetConnectionStatus.RespondedConnect: // awesome NetIncomingMessage msg = m_peer.SetupReadHelperMessage(ptr, payloadLength); InitializeRemoteTimeOffset(msg.ReadSingle()); m_peer.AcceptConnection(this); InitializePing(); SetStatus(NetConnectionStatus.Connected, "Connected to " + NetUtility.ToHexString(m_remoteUniqueIdentifier)); return; } break; case NetMessageType.Disconnect: // ouch string reason = "Ouch"; try { NetIncomingMessage inc = m_peer.SetupReadHelperMessage(ptr, payloadLength); reason = inc.ReadString(); } catch { } ExecuteDisconnect(reason, false); break; case NetMessageType.Discovery: m_peer.HandleIncomingDiscoveryRequest(now, m_remoteEndPoint, ptr, payloadLength); return; case NetMessageType.DiscoveryResponse: m_peer.HandleIncomingDiscoveryResponse(now, m_remoteEndPoint, ptr, payloadLength); return; case NetMessageType.Ping: // silently ignore return; default: m_peer.LogDebug("Unhandled type during handshake: " + tp + " length: " + payloadLength); break; } }
// received a library message while Connected internal void ReceivedLibraryMessage(NetMessageType tp, int ptr, int payloadLength) { m_peer.VerifyNetworkThread(); float now = (float)NetTime.Now; switch (tp) { case NetMessageType.Connect: m_peer.LogDebug("Received handshake message (" + tp + ") despite connection being in place"); break; case NetMessageType.ConnectResponse: // handshake message must have been lost HandleConnectResponse(now, tp, ptr, payloadLength); break; case NetMessageType.ConnectionEstablished: // do nothing, all's well break; case NetMessageType.LibraryError: m_peer.ThrowOrLog("LibraryError received by ReceivedLibraryMessage; this usually indicates a malformed message"); break; case NetMessageType.Disconnect: NetIncomingMessage msg = m_peer.SetupReadHelperMessage(ptr, payloadLength); m_disconnectRequested = true; m_disconnectMessage = msg.ReadString(); m_disconnectReqSendBye = false; //ExecuteDisconnect(msg.ReadString(), false); break; case NetMessageType.Acknowledge: for (int i = 0; i < payloadLength; i += 3) { NetMessageType acktp = (NetMessageType)m_peer.m_receiveBuffer[ptr++]; // netmessagetype int seqNr = m_peer.m_receiveBuffer[ptr++]; seqNr |= (m_peer.m_receiveBuffer[ptr++] << 8); // need to enqueue this and handle it in the netconnection heartbeat; so be able to send resends together with normal sends m_queuedIncomingAcks.Enqueue(new NetTuple <NetMessageType, int>(acktp, seqNr)); } break; case NetMessageType.Ping: int pingNr = m_peer.m_receiveBuffer[ptr++]; SendPong(pingNr); break; case NetMessageType.Pong: NetIncomingMessage pmsg = m_peer.SetupReadHelperMessage(ptr, payloadLength); int pongNr = pmsg.ReadByte(); float remoteSendTime = pmsg.ReadSingle(); ReceivedPong(now, pongNr, remoteSendTime); break; case NetMessageType.ExpandMTURequest: SendMTUSuccess(payloadLength); break; case NetMessageType.ExpandMTUSuccess: if (m_peer.Configuration.AutoExpandMTU == false) { m_peer.LogDebug("Received ExpandMTURequest altho AutoExpandMTU is turned off!"); break; } NetIncomingMessage emsg = m_peer.SetupReadHelperMessage(ptr, payloadLength); int size = emsg.ReadInt32(); HandleExpandMTUSuccess(now, size); break; case NetMessageType.NatIntroduction: // Unusual situation where server is actually already known, but got a nat introduction - oh well, lets handle it as usual m_peer.HandleNatIntroduction(ptr); break; default: m_peer.LogWarning("Connection received unhandled library message: " + tp); break; } }
public static MsgPlayerServerUpdatePacket Read(NetIncomingMessage packet) { Byte slot = packet.ReadByte(); Vector2 position = new Vector2 { X = packet.ReadSingle(), Y = packet.ReadSingle() }; Single rotation = packet.ReadSingle(); return new MsgPlayerServerUpdatePacket(slot, position, rotation); }
public static MsgPlayerClientUpdatePacket Read(NetIncomingMessage packet) { Vector2 position = new Vector2 { X = packet.ReadSingle(), Y = packet.ReadSingle() }; Single rotation = packet.ReadSingle(); return new MsgPlayerClientUpdatePacket(position, rotation); }
public static MsgBeginShotPacket Read(NetIncomingMessage packet) { Byte slot = packet.ReadByte(); Byte shotSlot = packet.ReadByte(); Vector2 position = packet.ReadVector2(); Single rotation = packet.ReadSingle(); Vector2 velocity = packet.ReadVector2(); return new MsgBeginShotPacket(slot, shotSlot, position, rotation, velocity); }
public static MsgSpawnPacket Read(NetIncomingMessage packet) { Byte slot = packet.ReadByte(); Vector2 position = packet.ReadVector2(); Single rotation = packet.ReadSingle(); return new MsgSpawnPacket(slot, position, rotation); }
private static Object ReadValue(NetIncomingMessage packet, TypeCode typeCode) { Object value; switch (typeCode) { case TypeCode.Empty: throw new NotSupportedException("Empty is not a supported type for variables"); case TypeCode.Object: throw new NotSupportedException("Object is not a supported type for variables"); case TypeCode.DBNull: throw new NotSupportedException("DBNull is not a supported type for variables"); case TypeCode.Boolean: value = packet.ReadBoolean(); break; case TypeCode.Char: throw new NotSupportedException("Char is not a supported type for variables"); case TypeCode.SByte: value = packet.ReadSByte(); break; case TypeCode.Byte: value = packet.ReadByte(); break; case TypeCode.Int16: value = packet.ReadInt16(); break; case TypeCode.UInt16: value = packet.ReadUInt16(); break; case TypeCode.Int32: value = packet.ReadInt32(); break; case TypeCode.UInt32: value = packet.ReadUInt32(); break; case TypeCode.Int64: value = packet.ReadInt64(); break; case TypeCode.UInt64: value = packet.ReadUInt64(); break; case TypeCode.Single: value = packet.ReadSingle(); break; case TypeCode.Double: value = packet.ReadDouble(); break; case TypeCode.Decimal: throw new NotSupportedException("Decimal is not a supported type for variables"); case TypeCode.DateTime: throw new NotSupportedException("DateTime is not a supported type for variables"); case TypeCode.String: value = packet.ReadString(); break; default: throw new NotSupportedException(String.Format("Unknown type {0} is not a supported type for variables", typeCode)); } return value; }
/// <summary> /// The decode. /// </summary> /// <param name="im"> /// The im. /// </param> public void Decode(NetIncomingMessage im) { this.Id = im.ReadInt64(); this.MessageTime = im.ReadDouble(); this.Position = im.ReadVector2(); this.Velocity = im.ReadVector2(); this.Rotation = im.ReadSingle(); this.Path = im.ReadInt32(); }
internal void ReceivedHandshake(double now, NetMessageType tp, int ptr, int payloadLength) { m_peer.VerifyNetworkThread(); byte[] hail; switch (tp) { case NetMessageType.Connect: if (m_status == NetConnectionStatus.None) { // Whee! Server full has already been checked bool ok = ValidateHandshakeData(ptr, payloadLength, out hail); if (ok) { if (hail != null) { m_remoteHailMessage = m_peer.CreateIncomingMessage(NetIncomingMessageType.Data, hail); m_remoteHailMessage.LengthBits = (hail.Length * 8); } else { m_remoteHailMessage = null; } if (m_peerConfiguration.IsMessageTypeEnabled(NetIncomingMessageType.ConnectionApproval)) { // ok, let's not add connection just yet NetIncomingMessage appMsg = m_peer.CreateIncomingMessage(NetIncomingMessageType.ConnectionApproval, (m_remoteHailMessage == null ? 0 : m_remoteHailMessage.LengthBytes)); appMsg.m_receiveTime = now; appMsg.m_senderConnection = this; appMsg.m_senderEndpoint = this.m_remoteEndpoint; if (m_remoteHailMessage != null) { appMsg.Write(m_remoteHailMessage.m_data, 0, m_remoteHailMessage.LengthBytes); } m_peer.ReleaseMessage(appMsg); return; } SendConnectResponse((float)now, true); } return; } if (m_status == NetConnectionStatus.RespondedConnect) { // our ConnectResponse must have been lost SendConnectResponse((float)now, true); return; } m_peer.LogDebug("Unhandled Connect: " + tp + ", status is " + m_status + " length: " + payloadLength); break; case NetMessageType.ConnectResponse: switch (m_status) { case NetConnectionStatus.InitiatedConnect: // awesome bool ok = ValidateHandshakeData(ptr, payloadLength, out hail); if (ok) { if (hail != null) { m_remoteHailMessage = m_peer.CreateIncomingMessage(NetIncomingMessageType.Data, hail); m_remoteHailMessage.LengthBits = (hail.Length * 8); } else { m_remoteHailMessage = null; } m_peer.AcceptConnection(this); SendConnectionEstablished(); return; } break; case NetConnectionStatus.RespondedConnect: // hello, wtf? break; case NetConnectionStatus.Disconnecting: case NetConnectionStatus.Disconnected: case NetConnectionStatus.None: // wtf? anyway, bye! break; case NetConnectionStatus.Connected: // my ConnectionEstablished must have been lost, send another one SendConnectionEstablished(); return; } break; case NetMessageType.ConnectionEstablished: switch (m_status) { case NetConnectionStatus.Connected: // ok... break; case NetConnectionStatus.Disconnected: case NetConnectionStatus.Disconnecting: case NetConnectionStatus.None: // too bad, almost made it break; case NetConnectionStatus.InitiatedConnect: // weird, should have been ConnectResponse... break; case NetConnectionStatus.RespondedConnect: // awesome NetIncomingMessage msg = m_peer.SetupReadHelperMessage(ptr, payloadLength); InitializeRemoteTimeOffset(msg.ReadSingle()); m_peer.AcceptConnection(this); InitializePing(); SetStatus(NetConnectionStatus.Connected, "Connected to " + NetUtility.ToHexString(m_remoteUniqueIdentifier)); return; } break; case NetMessageType.Disconnect: // ouch string reason = "Ouch"; try { NetIncomingMessage inc = m_peer.SetupReadHelperMessage(ptr, payloadLength); reason = inc.ReadString(); } catch { } ExecuteDisconnect(reason, false); break; default: m_peer.LogDebug("Unhandled type during handshake: " + tp + " length: " + payloadLength); break; } }
public override void ReadNetworkInstanceInfo(NetIncomingMessage msg) { this.iMovetime = msg.ReadInt32(); this.fHeightDif = msg.ReadSingle(); this.xOwner.xRenderComponent.v2OffsetRenderPos.Y = msg.ReadSingle(); }