//Constructor public GMTCPClient(Socket sock, AbstractServer parent) : base(sock, parent) { PacketBuffer handshakePacket = new PacketBuffer(); Logger.Trace("Connection Accepted. Beginning GM Handshake..."); handshakePacket.WriteString("GM:Studio-Connect", true); base.SendPacket(handshakePacket); handshakeStatus = GMTCPHandshakeStage.AwaitingConnectAck; }
//Data has been received override protected void OnDataReceived(byte[] data, int len) { if (handshakeStatus == GMTCPHandshakeStage.Complete) { int bytesReceived = 0; //Receive packets until all the data on the wire has been processed while (bytesReceived < len) { UInt32 packetIdentifier = BitConverter.ToUInt32(data, bytesReceived); if (packetIdentifier != 0xdeadc0de) { Logger.Trace("Packet dropped. Invalid identifier/magic number."); break; } else { //Length of the packet data UInt32 payloadLength = BitConverter.ToUInt32(data, bytesReceived + 8); PacketBuffer p = new PacketBuffer(data, bytesReceived + 12, (int)payloadLength); OnPacketReceived(p); //Process the packet bytesReceived += (int)payloadLength + 12; } } } else if (handshakeStatus == GMTCPHandshakeStage.AwaitingConnectAck) { uint magicNumber = BitConverter.ToUInt32(data, 0); if (magicNumber == 0xcafebabe) { //If the magic number matches Logger.Trace("Connect Acknowledged Successfully, Finalizing Handshake..."); PacketBuffer handshakePacket = new PacketBuffer(); handshakePacket.WriteUInt32(0xdeafbead); handshakePacket.WriteUInt32(0xf00dbeeb); handshakePacket.WriteUInt32(0x0000000c); base.SendPacket(handshakePacket); Logger.Trace("Handshake Finalized."); handshakeStatus = GMTCPHandshakeStage.Complete; } } }