Beispiel #1
0
        private void Resize()
        {
            SocketRecvBuf buffer = null;

            for (UInt32 index = 0; index < TNetConfig.DEFAULT_SOCKET_RECV_QUEUE_SIZE; ++index)
            {
                buffer = new SocketRecvBuf();
                if (buffer == null)
                {
                    //-- 抛出异常
                }
                m_RecvList.Add(buffer);
            }
        }
Beispiel #2
0
        public SocketRecvBuf GetRecvBuf()
        {
            lock (m_LockerForRecvList)
            {
                SocketRecvBuf buffer = null;

                //-- 01. 在当前list中查找free的。
                for (Int32 index = 0; index < m_RecvList.Count; ++index, m_iPos++)
                {
                    if (m_iPos >= m_RecvList.Count)
                    {
                        m_iPos = 0;
                    }
                    try
                    {
                        buffer = m_RecvList[m_iPos];
                    }
                    catch (System.Exception ex)
                    {
                        //-- log记录异常
                        //-- 从开始找到一个可以free的。
                        CommonDebugLog.LogWarning("ex:=[" + ex.ToString() + "]");
                        m_iPos = 0;
                        return(null);
                    }

                    if (buffer.GetUsedFlag() == true)
                    {
                        continue;
                    }

                    buffer.Clear();

                    //-- test
                    buffer.m_indexInPool = m_iPos;

                    m_iPos++;
                    if (m_iPos >= m_RecvList.Count)
                    {
                        m_iPos = 0;
                    }

                    return(buffer);
                }

                //-- 02. 没有找到,则返回null。等待数据接收
                return(null);
            }
        }
Beispiel #3
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;
        }