Exemplo n.º 1
0
        public void EncodePacket(TcpServerCommand cmd, object response)
        {
            MemoryStream outStream = new MemoryStream(Constants.MAX_PACKET_SIZE);

            outStream.Seek(Constants.PACKET_LENGTH_SIZE, SeekOrigin.Begin);
            outStream.Write(BitConverter.GetBytes((short)cmd), 0, sizeof(short));

            outStream.Seek(Constants.HEADER_SIZE, SeekOrigin.Begin);

            CodedOutputStream output = new CodedOutputStream(outStream);

            global::Google.Protobuf.IMessage msg = response as IMessage;
            if (msg != null)
            {
                msg.WriteTo(output);
                //output.WriteMessage(msg);
                output.Flush();
            }

            short length = (short)outStream.Length;

            outStream.Seek(0, SeekOrigin.Begin);
            outStream.Write(BitConverter.GetBytes(length), 0, sizeof(short));

            outStream.Seek(0, SeekOrigin.Begin);
            outStream.Read(_buffer, 0, length);

            _packetSize = length;
        }
Exemplo n.º 2
0
        protected override void ProcessPacket(byte[] buffer, int offset, int length)
        {
            Packet           packet  = new Packet(buffer, offset, length);
            TcpServerCommand command = (TcpServerCommand)packet.GetCommand();

            if (false == s_PacketHandlers.ContainsKey(command))
            {
                Log.AddLog($"packet handler not fouind : cmd:{command.ToString()}");
                return;
            }

            int count = length - Constants.HEADER_SIZE;

            byte[] copiedBuffer = new byte[count];
            Buffer.BlockCopy(buffer, Constants.HEADER_SIZE, copiedBuffer, 0, count);
            CodedInputStream protoStream = new CodedInputStream(copiedBuffer);

            s_PacketHandlers[command](this, protoStream);
        }