Пример #1
0
        private void ReceiveThread(object o)
        {
            StreamBuffer receiveStreamBuffer = StreamBufferPool.GetStream(mMaxReceiveBuffer, false, true);
            int          bufferCurLen        = 0;

            while (mReceiveWork)
            {
                try
                {
                    if (!mReceiveWork)
                    {
                        break;
                    }
                    if (mClientSocket != null)
                    {
                        int bufferLeftLen = receiveStreamBuffer.size - bufferCurLen;
                        int readLen       = mClientSocket.Receive(receiveStreamBuffer.GetBuffer(), bufferCurLen, bufferLeftLen, SocketFlags.None);
                        if (readLen == 0)
                        {
                            throw new ObjectDisposedException("DisposeEX", "receive from server 0 bytes,closed it");
                        }
                        if (readLen < 0)
                        {
                            throw new Exception("Unknow exception, readLen < 0" + readLen);
                        }

                        bufferCurLen += readLen;
                        DoReceive(receiveStreamBuffer, ref bufferCurLen);
                        if (bufferCurLen == receiveStreamBuffer.size)
                        {
                            throw new Exception("Receive from sever no enough buff size:" + bufferCurLen);
                        }
                    }
                }
                catch (ObjectDisposedException e)
                {
                    ReportSocketClosed(ESocketError.ERROR_3, e.Message);
                    break;
                }
                catch (Exception e)
                {
                    ReportSocketClosed(ESocketError.ERROR_4, e.Message);
                    break;
                }
            }

            StreamBufferPool.RecycleStream(receiveStreamBuffer);
            if (mStatus == SOCKSTAT.CONNECTED)
            {
                mStatus = SOCKSTAT.CLOSED;
            }
        }
Пример #2
0
 void OnDestory()
 {
     Instance = null;
     StreamBufferPool.RecycleStream(msSend);
     StreamBufferPool.RecycleStream(msRecive);
 }
Пример #3
0
        //protected void ReportSocketClosed(int result, string msg)
        //{
        //    if (OnClosed != null)
        //    {
        //        AddNetworkEvt(new HjNetworkEvt(this, result, msg, OnClosed));
        //    }
        //}


        private void ReceiveThread(object o)
        {
            StreamBuffer receiveStreamBuffer = StreamBufferPool.GetStream(mMaxReceiveBuffer, false, true);
            int          bufferCurLen        = 0;

            try
            {
                while (mReceiveWork)
                {
                    if (!mReceiveWork)
                    {
                        break;
                    }

                    if (mClientSocket == null)
                    {
                        continue;
                    }

                    //if (!mClientSocket.Connected) {
                    //    Logger.LogError("mClientSocket.Connected : ");
                    //    continue;
                    //}

                    //if (mClientSocket.Available <= 0) {//收不到东西
                    //    Logger.LogError("mClientSocket.Available : ");
                    //    continue;
                    //}
                    //if (mClientSocket.Available > 0) {
                    //    if (mClientSocket != null)
                    //{
                    // 总大小  receiveStreamBuffer.size  bufferCurLen当前缓存位置
                    int         bufferLeftLen = receiveStreamBuffer.size - bufferCurLen; // 剩余容量
                    SocketError socketError;
                    int         readLen = mClientSocket.Receive(receiveStreamBuffer.GetBuffer(), bufferCurLen, bufferLeftLen, SocketFlags.None, out socketError);

                    if (readLen == 0)
                    {//什么都没有读到
                        if (socketError == SocketError.ConnectionReset ||
                            socketError == SocketError.ConnectionAborted
                            )
                        {
                            Logger.LogError("socketError : " + socketError);
                            ReportSocketConnected(ESocketError.ERROR_NEW_11, socketError.ToString());
                            throw new Exception(" Receive socketError " + socketError.ToString());// 网络链接已经断开 需要重新连接
                            //continue;
                        }
                        else if (socketError == SocketError.Interrupted) // 主动中断
                        {
                            Logger.LogError("socketError : " + socketError);
                            continue;
                        }
                        else if (socketError == SocketError.Success)
                        {
                            continue;
                        }
                        else
                        {
                            Logger.LogError("socketError : " + socketError);
                            ReportSocketConnected(ESocketError.ERROR_NEW_11, socketError.ToString());
                            throw new Exception(" Receive socketError " + socketError.ToString());
                            //continue;
                            //throw new SocketException((int)socketError);// 网络链接已经断开 需要重新连接

                            //Logger.LogError("socketError : " + socketError);
                            //string disposeEXError = "receive from server 0 bytes,closed it";
                            //ReportSocketConnected(ESocketError.ERROR_NEW_10, disposeEXError);
                            //throw new ObjectDisposedException("DisposeEX", disposeEXError);// 网络链接已经断开 需要重新连接
                        }
                        //    string disposeEXError = "receive from server 0 bytes,closed it";
                        //    ReportSocketConnected(ESocketError.ERROR_NEW_10, disposeEXError);
                        //throw new ObjectDisposedException("DisposeEX", disposeEXError);// 网络链接已经断开 需要重新连接
                        // 主动登出 或者是 网络链接已经断开 需要重新连接
                    }

                    if (readLen < 0)
                    {
                        throw new Exception("Unknow exception, readLen < 0" + readLen);
                    }

                    bufferCurLen += readLen;
                    DoReceive(receiveStreamBuffer, ref bufferCurLen);
                    if (bufferCurLen == receiveStreamBuffer.size)
                    {
                        throw new Exception("Receive from sever no enough buff size:" + bufferCurLen);
                    }

                    //}
                }
            }
            catch (ObjectDisposedException e)
            {
                //ReportSocketConnected(ESocketError.ERROR_NEW_10, e.Message);
                // 正常网络链接断开 不做处理
                UnityEngine.Debug.LogException(e);
            }
            catch (SocketException e)
            {
                UnityEngine.Debug.Log("e.NativeErrorCode : " + e.NativeErrorCode + " e.SocketErrorCode : " + e.SocketErrorCode);
                string msg = null;
                switch (e.NativeErrorCode)
                {
                case 10004:    // 连接断开 登出
                    msg = string.Format("{0} 连接断开: error code {1}!", "", e.NativeErrorCode);
                    break;

                default:
                    //其他错误
                    msg = e.Message;
                    ReportSocketConnected(ESocketError.ERROR_NEW_11, e.Message);
                    break;
                }
                UnityEngine.Debug.LogException(e);
            }
            catch (ThreadAbortException e)
            {
                ReportSocketConnected(ESocketError.ERROR_NEW_12, e.Message);
                UnityEngine.Debug.LogException(e);
                // 按照正常处理
            }
            catch (Exception e)
            {
                //ReportSocketConnected(ESocketError.ERROR_NEW_13, e.Message);
                //UnityEngine.Debug.LogException(e);
            }
            finally {
                StreamBufferPool.RecycleStream(receiveStreamBuffer);
                if (mStatus == SOCKSTAT.CONNECTED)
                {
                    mStatus = SOCKSTAT.CLOSED;
                }
            }
        }