Пример #1
0
 public void ClearUp()
 {
     Disconnect();
     m_Socket           = null;
     m_LogicRecvHandler = null;
     m_LogicSendHandler = null;
     m_serverMsgIndex   = 0;
 }
Пример #2
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;
        }
Пример #3
0
 public NetManager()
 {
     m_Socket           = new TSocket();
     m_LogicRecvHandler = new LogicRecvHandler(m_Socket);
     m_LogicSendHandler = new LogicSendHandler(m_Socket);
 }