Ejemplo n.º 1
0
        public byte[] Serialize(GamePacket packet)
        {
            using (var ms = new MemoryStream())
                using (var bw = new BinaryWriter(ms))
                {
                    bw.Write(packet.Id);
                    bw.Write(packet.Type);

                    var correlationBytes = packet.CorrelationId.ToByteArray();
                    bw.Write(correlationBytes.Length);
                    bw.Write(correlationBytes);

                    var body = binarySerializer.Serialize(packet.Data);
                    bw.Write(body.Length);
                    bw.Write(body);

                    return(ms.ToArray());
                }
        }
Ejemplo n.º 2
0
        public GamePacket Deserialize(byte[] data, int length)
        {
            var packet = new GamePacket();

            using (var ms = new MemoryStream(data, 0, length))
                using (var br = new BinaryReader(ms))
                {
                    packet.Id            = br.ReadString();
                    packet.Type          = br.ReadString();
                    packet.CorrelationId = new Guid(br.ReadBytes(br.ReadInt32()));

                    var dataSize = br.ReadInt32();
                    var payload  = br.ReadBytes(dataSize);

                    packet.Data = loadedTypes.TryGetValue(packet.Type, out var targetType)
                    ? binarySerializer.Deserialize(payload, targetType)
                    : payload;
                }
            return(packet);
        }
Ejemplo n.º 3
0
 public abstract Task HandleAsync(GamePacket packet);
Ejemplo n.º 4
0
        private async Task <bool> ReceiveDataAsync()
        {
            if (!IsReady)
            {
                //if (!await CreateAsync())
                return(false);
            }

            try
            {
                var buffer  = new byte[4096];
                var segment = new ArraySegment <byte>(buffer);
                var result  = await webSocket.ReceiveAsync(segment, CancellationToken.None);

                if (result.CloseStatus != null || !string.IsNullOrEmpty(result.CloseStatusDescription))
                {
                    Disconnect();
                    return(false);
                }

                if (!result.EndOfMessage)
                {
                    if (unfinishedPacket == null)
                    {
                        unfinishedPacket =
                            new PartialGamePacket(
                                packetSerializer,
                                segment.Array,
                                result.Count);
                    }
                    else
                    {
                        unfinishedPacket.Append(segment.Array, result.Count);
                    }
                }
                else
                {
                    try
                    {
                        GamePacket packet = null;
                        if (unfinishedPacket != null)
                        {
                            packet           = unfinishedPacket.Build();
                            unfinishedPacket = null;
                        }
                        else
                        {
                            packet = packetSerializer.Deserialize(segment.Array, result.Count);
                        }

                        if (awaitedReplies.TryGetValue(packet.CorrelationId, out var task))
                        {
                            if (task.TrySetResult(packet))
                            {
                                return(true);
                            }
                        }

                        await HandlePacketAsync(packet);
                    }
                    catch (Exception exc)
                    {
                        Debug.LogError("Error deserializing packet: " + exc);
                    }
                }
                return(true);
            }
            //catch (System.IO.IOException exc)
            //{
            //    logger.Error(exc.ToString());
            //    this.Disconnect();
            //    return false;
            //}
            catch (WebSocketException socketExc)
            {
                if (socketExc.WebSocketErrorCode != WebSocketError.ConnectionClosedPrematurely)
                {
                    logger.Error(socketExc.ToString());
                }

                gameManager.ForceGameSessionUpdate();

                Disconnect();
                return(false);
            }
            catch (Exception exc)
            {
                logger.Error(exc.ToString());
                Disconnect();
                return(false);
            }
        }
Ejemplo n.º 5
0
 public void SendNoAwait(GamePacket packet)
 {
     sendQueue.Enqueue(packet);
 }