Ejemplo n.º 1
0
        public bool RecvFromSocketBuf(LogicRecvHandler recvHandler)
        {
            if (recvHandler == null)
            {
                return(false);
            }

            if (!IsConnected())
            {
                return(true);
            }

            SocketRecvBuf recvBuff = m_SocketRecvBufQueue.GetRecvBuf();

            //-- 说明已经创建了DEFAULT_RECV_QUEUE_SIZE SocketRecvBuf等待接收数据,所以返回。
            if (recvBuff == null)
            {
                return(true);
            }

            MemoryStream recvMemStream = recvBuff.GetMemoryStream();

            if (recvMemStream == null)
            {
                return(false);
            }

            //if ( recvMemStream.Length == 0 )
            //{
            //    // 如果长度为0,则认为服务器主动踢掉客户端;
            //    NetClient.GetInstance().Disconnect();
            //    return false;
            //}

            SocketAsyncEventArgs receiveCompleteArgs = new SocketAsyncEventArgs();

            if (recvMemStream.Length < TNetConfig.DEFAULT_SOCKET_RECV_BUFFERSIZE ||
                recvMemStream.Capacity < TNetConfig.DEFAULT_SOCKET_RECV_BUFFERSIZE
                )
            {
                recvMemStream.SetLength(TNetConfig.DEFAULT_SOCKET_RECV_BUFFERSIZE);
                Array.Clear(recvMemStream.GetBuffer(), 0, (Int32)recvMemStream.Length);
            }
            receiveCompleteArgs.SetBuffer(recvMemStream.GetBuffer(), 0, (Int32)recvMemStream.Length);

            //-- test
            //Console.WriteLine("recvMemStream.Length:=[" + recvMemStream.Length + "]");

            recvBuff.SetUsedFlag(true);
            m_RecvData = new OnRecvObj();
            m_RecvData.m_RecvBuffer       = recvBuff;
            m_RecvData.m_LogicRecvHandler = recvHandler;
            m_RecvData.m_TSocket          = this;

            receiveCompleteArgs.UserToken      = m_RecvData;
            receiveCompleteArgs.RemoteEndPoint = m_IPAndPoint;
            receiveCompleteArgs.Completed     += new EventHandler <SocketAsyncEventArgs>(OnReceive);

            if (!m_Socket.ReceiveAsync(receiveCompleteArgs))
            {
                OnReceive(null, receiveCompleteArgs);
            }

            return(true);
            ////-TODO: encode type
            //Array.Clear(m_CurrRecvBuff, 0, TNetConfig.DEFAULT_SOCKET_INPUT_BUFFERSIZE);
            //if (    m_ReceiveCompleteArgs.UserToken == null ||
            //        m_ReceiveCompleteArgs.UserToken.Equals(iStream) == false
            //)
            //{
            //    m_ReceiveCompleteArgs.UserToken = iStream;
            //}

            //if (    m_ReceiveCompleteArgs.RemoteEndPoint == null    ||
            //        m_ReceiveCompleteArgs.RemoteEndPoint.Equals(m_IPAndPoint) == false
            //)
            //{
            //    m_ReceiveCompleteArgs.RemoteEndPoint = m_IPAndPoint;
            //}

            //m_ReceiveCompleteArgs.SetBuffer(m_CurrRecvBuff, 0, TNetConfig.DEFAULT_SOCKET_INPUT_BUFFERSIZE);

            ////-: true,io pend. waiting callback
            //try
            //{
            //    if (m_Socket.ReceiveAsync(m_ReceiveCompleteArgs))
            //    {
            //        if (s_ReceiveAutoEvent.WaitOne(TNetConfig.DEFAULT_SOCKET_RECEIVE_ASYNC_WAITING_TIME))
            //        {
            //            RecvFromSocketBuf(iStream);
            //            int iTest01 = m_ReceiveCompleteArgs.BytesTransferred;
            //        }
            //        else
            //        {
            //            int iTest02 = m_ReceiveCompleteArgs.BytesTransferred;
            //            return true;
            //        }
            //    }
            //    else
            //    {
            //        return true;
            //    }
            //}
            //catch (System.Exception ex)
            //{
            //    Console.WriteLine(ex.ToString());
            //    return false;
            //}

            //return true;
        }
Ejemplo n.º 2
0
        //-- 将从socket接收到的数据,拷贝到LogicRecvBuf中。
        private static void OnReceive(object sender, SocketAsyncEventArgs e)
        {
            if (e == null)
            {
                return;
            }

            if (e.LastOperation != SocketAsyncOperation.Receive)
            {
                return;
            }

            if (e.SocketError != SocketError.Success)
            {
                return;
            }

            OnRecvObj recvData = e.UserToken as OnRecvObj;

            if (recvData == null)
            {
                return;
            }

            if (recvData.m_RecvBuffer == null)
            {
                return;
            }

            if (recvData.m_LogicRecvHandler == null)
            {
                return;
            }

            if (recvData.m_TSocket == null)
            {
                return;
            }

            LogicRecvBufQueue logicRecvBufQueue = recvData.m_LogicRecvHandler.GetLogicRecvBufQueue();

            if (logicRecvBufQueue == null)
            {
                //-- log
                return;
            }

            LogicRecvBuffer logicRecvBuf = logicRecvBufQueue.GetEmptyBuf();

            if (logicRecvBuf == null)
            {
                //-- log
                return;
            }

            MemoryStream memStream = logicRecvBuf.GetMemoryStream();

            if (memStream == null)
            {
                //-- log
                return;
            }

            if (e.BytesTransferred > 0)
            {
                //-- test
                //Console.WriteLine("e.BytesTransferred:=[" + e.BytesTransferred + "]");

                //-- 拷贝数据要从memStream的头写入。
                memStream.Seek(0, SeekOrigin.Begin);
                memStream.Write(e.Buffer, 0, e.BytesTransferred);
                memStream.Seek(0, SeekOrigin.Begin);
                logicRecvBuf.SetUsedFlag(true);
                //-- 释放recvData.m_RecvBuffer
                recvData.m_RecvBuffer.SetUsedFlag(false);
                //-- 继续接收
                recvData.m_TSocket.RecvFromSocketBuf(recvData.m_LogicRecvHandler);
            }
            else
            {
                recvData.m_RecvBuffer.SetUsedFlag(false);
                //-- log查看是否接收缓冲区满了
                CommonDebugLog.Log("Server DisConnect!!!");
                NetClient.GetInstance().Disconnect();
            }
            return;
        }