示例#1
0
        private void ReadFromServer()
        {
            try
            {
                var rdr = new DReader(dest.GetStream());

                while (true)
                {
                    int    len;
                    byte   id;
                    byte[] content;
                    try
                    {
                        len     = rdr.ReadInt32();
                        id      = rdr.ReadByte();
                        content = rdr.ReadBytes(len - PACKET_HEADER_SIZE);
                    }
                    catch (IOException)
                    {
                        break;
                    }

                    content = ServerReceiveKey.Crypt(content);

                    if (OnServerPacketReceive != null)
                    {
                        Packet pkt = Packet.Packets[(PacketID)id].CreateInstance();
                        pkt.Read(content, len - PACKET_HEADER_SIZE);

                        if (this.OnServerPacketReceive(ref pkt))
                        {
                            SendToClient(pkt);
                        }
                        else
                        {
                            log.InfoFormat("Skip sending packet, abort by user: {0}", pkt.ID);
                        }
                    }
                    else
                    {
                        m_sendToClient(new RawPacket {
                            id = id, content = content
                        });
                    }
                }
            }
            catch (ObjectDisposedException) { }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            finally
            {
                Disconnect();
            }
        }
示例#2
0
        private void ReadFromClient()
        {
            try
            {
                var rdr = new DReader(new NetworkStream(clientSocket));
                while (true)
                {
                    int    len;
                    byte   id;
                    byte[] content;
                    try
                    {
                        len     = rdr.ReadInt32();
                        id      = rdr.ReadByte();
                        content = rdr.ReadBytes(len - PACKET_HEADER_SIZE);
                    }
                    catch (IOException)
                    {
                        break;
                    }

                    content = ClientReceiveKey.Crypt(content);

                    if (OnClientPacketReceive != null)
                    {
                        Packet pkt = Packet.Packets[(PacketID)id].CreateInstance();
                        pkt.Read(content, len - PACKET_HEADER_SIZE);

                        //if (pkt.ID == PacketID.HELLO)
                        //{
                        //    log.Info("Hello from client.");
                        //    this.OnClientPacketReceive(ref pkt);
                        //    m_sendToServer(new RawPacket { id = id, content = content });
                        //}
                        //else
                        //{
                        if (this.OnClientPacketReceive(ref pkt))
                        {
                            SendToServer(pkt);
                        }
                        else
                        {
                            log.InfoFormat("Skip sending packet, abort by user:\n{0}", pkt.ToString());
                        }
                        //}
                    }
                    else
                    {
                        m_sendToServer(new RawPacket {
                            id = id, content = content
                        });
                    }
                }
            }
            catch (ObjectDisposedException) { }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            finally
            {
                Disconnect();
            }
        }