private void RecvThread()
            {
                while (true)
                {
                    if (!this.clientSocket.Connected)
                    {
                        Debug.Log("没有链接,退出接收");
                        break;
                    }

                    try
                    {
                        var recvLen = 0;
                        if (this.recved < RecvLen)
                        {
                            recvLen = this.clientSocket.Receive(this._recvBuf, this.recved, RecvLen - this.recved, SocketFlags.None);
                        }
                        else
                        {     // 大包
                            if (this.longPkg == null)
                            { // 尚未分配内存
                                int pkgSize;
                                int headSize;
                                TcpProtocol.ReadHeader(this._recvBuf, this.recved, out pkgSize, out headSize);
                                this.longPkgSize = pkgSize;
                                this.longPkg     = new byte[longPkgSize];
                                Array.Copy(this._recvBuf, 0, this.longPkg, 0, this.recved);
                            }

                            recvLen = this.clientSocket.Receive(this.longPkg, this.recved, this.longPkgSize - this.recved,
                                                                SocketFlags.None);
                        }

                        if (recvLen > 0)
                        {// 收到数据长度
                            this.recved += recvLen;
                            OnRecvTcpData();
                        }
                    }
                    catch (Exception e)
                    {
                        if (this.onException != null)
                        {
                            this.onException(e);
                        }
                        else
                        {
                            throw e;
                        }
                        break;
                    }
                }

                Debug.LogWarning("退出接收线程");
            }
コード例 #2
0
        private void OnRecvTcpData()
        {
            var dataBuffer = this.longPkg ?? this.receiveBuffer;

            while (this.usedBufferLength > 0)
            {
                if (!TcpProtocol.ReadHeader(dataBuffer, dataBuffer.Length,
                                            out var pkgSize,
                                            out var headerSize))
                {
                    break;
                }

                if (this.usedBufferLength < pkgSize)
                {
                    break;
                }

                var rawDataStart = headerSize;
                var rawDataLen   = pkgSize - headerSize;
#if DebugMode
                if (ifEnableSocketLog())
                {
                    Debug.Log($"Tcp[{Ip}:{Port}] 接收到数据包,[{pkgSize}]");
                }
#endif
                this.onRecvCmd(dataBuffer, rawDataStart, rawDataLen);

                if (this.usedBufferLength > pkgSize)
                {    //粘包
                    Array.Copy(dataBuffer, pkgSize, dataBuffer, 0, this.usedBufferLength - pkgSize);
                }

                this.usedBufferLength -= pkgSize;

                if (this.usedBufferLength == 0 && this.longPkg != null)
                {
                    this.longPkg     = null;
                    this.longPkgSize = 0;
                }
            }
        }
コード例 #3
0
        private void RecvThread()
        {
            while (true)
            {
                if (shouldKill)
                {
                    try
                    {
                        Thread.CurrentThread.Abort();
                    }
                    catch (Exception e)
                    {
#if DebugMode
                        if (GameSettings.Instance.EnableSocketClosingException)
                        {
                            Debug.Log($"[Tcp.shouldKill.Abort]{e}");
                        }
#endif
                    }
                    break;
                }
                if (!this.clientSocket.Connected)
                {
#if DebugMode
                    if (ifEnableSocketLog())
                    {
                        Debug.Log($"Tcp[{Ip}:{Port}] 连接断开,退出接收");
                    }
#endif
                    break;
                }

                try
                {
                    var recvLen = 0;
                    if (this.usedBufferLength < MaxBufferSize)
                    {
                        recvLen = this.clientSocket.Receive(this.receiveBuffer, this.usedBufferLength, MaxBufferSize - this.usedBufferLength, SocketFlags.None);
                    }
                    else
                    {    // 大包
                        Debug.LogWarning($"出现大包!");
                        if (this.longPkg == null)
                        {    // 尚未分配内存
                            TcpProtocol.ReadHeader(this.receiveBuffer, this.usedBufferLength, out var pkgSize, out var headSize);
                            this.longPkgSize = pkgSize;
                            this.longPkg     = new byte[longPkgSize];
                            Array.Copy(this.receiveBuffer, 0, this.longPkg, 0, this.usedBufferLength);
                        }

                        recvLen = this.clientSocket.Receive(this.longPkg, this.usedBufferLength, this.longPkgSize - this.usedBufferLength,
                                                            SocketFlags.None);
                    }

                    if (recvLen > 0)
                    {    // 收到数据长度
                        this.usedBufferLength += recvLen;
                        OnRecvTcpData();
                    }
                }
                catch (Exception e)
                {
                    if (this.onException != null)
                    {
                        this.onException(e);
                    }
                    break;
                }
            }
        }