コード例 #1
0
ファイル: TcpObject.cs プロジェクト: phuhao00/antnet-unity
        protected void OnRecvHead()
        {
            int read = tcpClient.GetStream().EndRead(asyncReader);

            byte[] data = (byte[])asyncReader.AsyncState;
            asyncReader.AsyncWaitHandle.Close();
            asyncReader = null;
            if (read > 0)
            {
                totalRead += read;
                if (totalRead == MessageHead.Length)
                {
                    totalRead = 0;
                    head      = MessageHead.Parse(data);
                    if (head.error > 0)
                    {
                        if (head.error != Error.ErrOk)
                        {
                            if (onRecvError != null)
                            {
                                onRecvError(head.error);
                            }

                            Log.Warn("服务器返回错误码 : {0}", head.error);
                        }
                    }
                    if (head.len > MessageHead.MaxRecvLength)
                    {
                        Stop(false);
                        disconnCallback(this);
                        return;
                    }

                    if (head.len == 0)
                    {
                        CallBack(head, new RecvData(head));
                        head = null;
                    }

                    ProcessRecv();
                }
                else
                {
                    asyncReader = tcpClient.GetStream().BeginRead(data, totalRead, MessageHead.Length - totalRead, null, data);
                }
            }
            else if (read == 0)
            {
                Stop(false);
                disconnCallback(this);
            }
        }
コード例 #2
0
ファイル: UdpObject.cs プロジェクト: phuhao00/antnet-unity
        protected void ProcessRecv()
        {
            while (udpClient.Available > 0)
            {
                byte[]      data = udpClient.Receive(ref serverAddr);
                MessageHead head = MessageHead.Parse(data);

                Log.Info("recv index:{0} needack:{1}", head.index, head.needAck);
                if (head.len == 0)
                {
                    if (head.needAck)
                    {
                        SendMsg(new SendData(head.cmd, head.act, (ushort)(head.flags | (ushort)MessageHead.Flags.FlagAck), head.index));
                    }
                    CallBack(head, new RecvData(head));
                }
                else if (head.len <= MessageHead.MaxRecvLength)
                {
                    uint   oldlen  = (head.flags & (ushort)MessageHead.Flags.FlagCompress) > 0 ? BitConverter.ToUInt32(data, MessageHead.Length) : 0;
                    uint   datalen = oldlen > 0 ? oldlen : head.len;
                    byte[] xdata   = new byte[datalen];
                    if (oldlen > 0)
                    {
                        GZipInputStream gzi = new GZipInputStream(new MemoryStream(data, MessageHead.Length + 4, (int)head.len - 4));
                        gzi.Read(xdata, 0, xdata.Length);
                    }
                    else
                    {
                        Array.Copy(data, MessageHead.Length, xdata, 0, datalen);
                    }

                    if (head.needAck)
                    {
                        SendMsg(new SendData(head.cmd, head.act, (ushort)(head.flags | (ushort)MessageHead.Flags.FlagAck), head.index));
                    }
                    CallBack(head, new RecvData(head, xdata));
                }
            }
        }