Пример #1
0
        private static byte[] SendBuffer(Packet packet)
        {
            var encoded = packet.Encode();
            var header  = IPAddress.HostToNetworkOrder(encoded.Length);

            return(BinarySerde.Encode(writer =>
            {
                writer.Write(header);
                writer.Write(encoded);
            }));
        }
Пример #2
0
        public byte[] Encode()
        {
            return(BinarySerde.Encode(writer =>
            {
                writer.Write(PacketType);
                writer.Write(_name);

                // ignored if Messages is empty
                foreach (var message in Messages)
                {
                    writer.Write(message.Encode());
                }
            }));
        }
Пример #3
0
        private static Tuple <byte, string, Message[]> ReadPacket(byte[] data)
        {
            return(BinarySerde.Parse(data, reader =>
            {
                var packetType = reader.ReadByte();
                var(name, nameLen) = IsNamedList[packetType]
                    ? ReadName(reader)
                    : new Tuple <string, int>(null, 0);

                var remain = data.Length - (1 + nameLen);
                var messages = remain == 0
                    ? new Message[] { }
                    : ReadMessages(reader, remain);

                return new Tuple <byte, string, Message[]>(packetType, name, messages);
            }));
        }
Пример #4
0
        public static Message[] Parse(byte[] data)
        {
            return(BinarySerde.Parse(data, reader =>
            {
                var count = 0;
                var messages = new List <Message>();
                while (count < data.Length)
                {
                    var type = reader.ReadByte();
                    count += 1;

                    var(message, length) = reader.ReadMessage(type);
                    messages.Add(message);
                    count += length;
                }

                return messages.ToArray();
            }));
        }
Пример #5
0
        private int ReceiveHeader()
        {
            const int HEADER_SIZE  = 4;
            var       headerBuffer = new byte[HEADER_SIZE];
            var       length       = HEADER_SIZE;
            var       sum          = 0;

            while (sum < length)
            {
                var received = _socket.Receive(headerBuffer, sum, length - sum, SocketFlags.None);
                sum += received;
            }

            return(BinarySerde.Parse(headerBuffer, reader =>
            {
                var data = reader.ReadInt32();
                return IPAddress.NetworkToHostOrder(data);
            }));
        }
Пример #6
0
        private byte[] ReceiveData(int length)
        {
            var buffer = new byte[MaxSegment];
            var sum    = 0;

            while (sum < length)
            {
                var received = _socket.Receive(buffer, sum, length - sum, SocketFlags.None);
                sum += received;
            }

            if (sum > length)
            {
                throw new InvalidDataException($"invalid size: {sum} (expected:{length})");
            }

            return(BinarySerde.Parse(buffer, reader =>
            {
                return reader.ReadBytes(length);
            }));
        }
Пример #7
0
 public override byte[] Encode()
 {
     return(BinarySerde.Encode(Key, Values));
 }