예제 #1
0
 public static void OnSendHead(NetBuffer buf, int cmd, int datalen)
 {
     if (_isXorEncrypt)
     {
         NetBuffer tmp = new NetBuffer();
         tmp.WriteUInt((uint)cmd);
         tmp.WriteUInt((uint)(datalen));
         buf.WriteBytes(NetEncoder.XOR(tmp.ToBytes(), HEAD_LEN, _xorKey, _xorKey.Length));
     }
     else
     {
         buf.WriteUInt((uint)cmd);
         buf.WriteUInt((uint)(datalen));
     }
 }
예제 #2
0
        void Update()
        {
            if (IsConnect())
            {
                byte[] bts = _socket.Recv();

                if (bts != null && bts.Length > 0)
                {
                    _dataBuffer.Seek(0, SeekOrigin.End);
                    _dataBuffer.WriteBytes(bts, 0, bts.Length);
                    _dataBuffer.Seek(0, SeekOrigin.Begin);
                }

                if (_dataBuffer.RemainLen() > NetEncoder.HEAD_LEN)
                {
                    int cmd, datalen;
                    NetEncoder.OnRecvHead(_dataBuffer, out cmd, out datalen);

                    if (_dataBuffer.RemainLen() >= datalen)
                    {
                        NetBuffer msgbuf = new NetBuffer();
                        msgbuf.WriteBytes(_dataBuffer.ReadBytes(datalen));
                        msgbuf.Seek(0, SeekOrigin.Begin);
                        _dataBuffer.ResetRemain();
                        int len = msgbuf.GetLength();

                        byte[] data;
                        data = NetEncoder.OnRecvData(msgbuf.ReadBytes(len));

                        if (cmd == 0)
                        {
                            onConnect?.Invoke(true, "");
                        }
                        onReceive?.Invoke(cmd, data);
                    }
                    else
                    {
                        _dataBuffer.Seek(0, SeekOrigin.Begin);
                        // _dataBuffer.ResetRemain();
                    }
                }
            }
        }
예제 #3
0
        protected void DoRecv()
        {
            if (_tcpClient != null && _tcpClient.Connected && _socketStream.DataAvailable)
            { //&& _socketStream.DataAvailable
                try
                {
                    int len = _socketStream.Read(_recvBuffer, 0, BUFFER_LEN);
                    if (len < 1)
                    {
                        //Close();
                        if (onReconnect != null)
                        {
                            onReconnect();
                        }
                        AddEvent(new NetEvent(false, -1, null, "connect disconnect!"));
                        return;
                    }
                    _dataBuffer.Seek(0, SeekOrigin.End);
                    _dataBuffer.WriteBytes(_recvBuffer, 0, len);
                    _dataBuffer.Seek(0, SeekOrigin.Begin);
                }
                catch (Exception e)
                {
                    // Close();
                    if (onReconnect != null)
                    {
                        onReconnect();
                    }
                    AddEvent(new NetEvent(false, -1, null, "connect err!"));
                    Log.Debug("Read data from socket err :" + e.ToString());
                }
            }

            if (_dataBuffer.RemainLen() > NetEncoder.HEAD_LEN)
            {
                int cmd, datalen;
                NetEncoder.OnRecvHead(_dataBuffer, out cmd, out datalen);
                if (_dataBuffer.RemainLen() >= datalen)
                {
                    NetBuffer msgbuf = new NetBuffer();
                    msgbuf.WriteBytes(_dataBuffer.ReadBytes(datalen));
                    msgbuf.Seek(0, SeekOrigin.Begin);
                    _dataBuffer.ResetRemain();
                    int len = msgbuf.GetLength();

                    byte[] data;
                    data = NetEncoder.OnRecvData(msgbuf.ReadBytes(len));

                    if (_recvThread != null)
                    {
                        AddEvent(new NetEvent(true, cmd, data)); //Encoding.UTF8.GetString( ) msgbuf.ReadBytes(len)
                    }
                    else
                    {
                        onRecv(new NetEvent(true, cmd, data));
                    }
                }
                else
                {
                    _dataBuffer.Seek(0, SeekOrigin.Begin);
                    // _dataBuffer.ResetRemain();
                }
            }
        }