Exemplo n.º 1
0
        public static byte[] SerializeRequest(NetworkMessage request)
        {
            if (request.GetType() == typeof(ConnectServerRequest))
            {
                var    connectServerRequest = (ConnectServerRequest)request;
                int    messageSize          = 2 + 4 + 1 + 256 + 256;
                byte[] buffer = new byte[messageSize];
                int    offset = 0;
                offset = BytesUtils.WritePrimitiveBytes(buffer, offset, connectServerRequest.EventType());
                offset = BytesUtils.WritePrimitiveBytes(buffer, offset, connectServerRequest.MagicNumber);
                offset = BytesUtils.WritePrimitiveBytes(buffer, offset, connectServerRequest.AuthType);

                offset = BytesUtils.WriteArrayBytes(buffer, offset, connectServerRequest.Username);
                offset = BytesUtils.WriteBytes(buffer, offset, BytesUtils.Zeros(256 - connectServerRequest.Username.Length));

                offset = BytesUtils.WriteArrayBytes(buffer, offset, connectServerRequest.Password);
                offset = BytesUtils.WriteBytes(buffer, offset, BytesUtils.Zeros(256 - connectServerRequest.Password.Length));
                if (offset != messageSize)
                {
                    throw new Exception("Message size is not matched");
                }
                return(buffer);
            }
            else
            {
                throw new Exception("Unsupported type: " + request.GetType());
            }
        }
Exemplo n.º 2
0
        private void Receiving()
        {
            try {
                // Waiting for sendingThread to establish the connnection.
                while (running && error == "" && networkStream == null)
                {
                    Thread.Sleep(0);
                }

                IMessageBytesFiller filler = null;
                while (running && error == "")
                {
                    if (filler == null)
                    {
                        ushort eventType = BytesUtils.ReadUInt16(networkStream);
                        // Find filler.
                        foreach (var eachFiller in messageBytesFillers)
                        {
                            if (eachFiller.EventType() == eventType)
                            {
                                filler = eachFiller;
                            }
                        }
                        if (filler == null)
                        {
                            throw new Exception("Could not find a filler for " + eventType.ToString());
                        }

                        // Add the event type into the filler.
                        var raw = BitConverter.GetBytes(eventType);
                        filler.Fill(raw[0]);
                        filler.Fill(raw[1]);
                    }
                    else
                    {
                        byte   b      = BytesUtils.ReadByte(networkStream);
                        byte[] buffer = filler.Fill(b);
                        if (buffer != null)
                        {
                            receivingQueue.Enqueue(SerializationUtils.DeserializeResponse(buffer));
                            // A new filler will be picked for the next message.
                            filler = null;
                        }
                    }
                }
            } catch (Exception e) {
                this.error = e.ToString();
                throw e;
            }
        }
Exemplo n.º 3
0
        public static NetworkMessage DeserializeResponse(byte[] data)
        {
            ushort eventType = BitConverter.ToUInt16(data, 0);

            if (eventType == NetworkMessage.EventType_ConnectServer)
            {
                byte[] sessionId = BytesUtils.Slice(data, 2, 20);
                ulong  userId    = BitConverter.ToUInt64(data, 22);
                byte   errorCode = data[30];
                return(new ConnectServerResponse(eventType, sessionId, userId, errorCode));
            }
            else
            {
                throw new Exception("Unknown NetworkMessage");
            }
        }