Пример #1
0
 private void ReceiveCallback(IAsyncResult ar)
 {
     try
     {
         ar.AsyncWaitHandle.Close();
         this.m_ar_Recv = null;
         StateObjectForRecvData stateObjectForRecvData = (StateObjectForRecvData)ar.AsyncState;
         Socket workSocket = stateObjectForRecvData.workSocket;
         int    num        = workSocket.EndReceive(ar);
         if (num > 0)
         {
             MemoryStreamEx comunicationMem = this.m_ComunicationMem;
             Monitor.Enter(comunicationMem);
             try
             {
                 this.m_ComunicationMem.Write(stateObjectForRecvData.buffer, 0, num);
             }
             finally
             {
                 Monitor.Exit(comunicationMem);
             }
         }
         this.m_ar_Recv = workSocket.BeginReceive(stateObjectForRecvData.buffer, 0, 4096, SocketFlags.None, new AsyncCallback(this.ReceiveCallback), stateObjectForRecvData);
     }
     catch (Exception e)
     {
         this.DidDisconnect(e);
     }
 }
Пример #2
0
        /// <summary>
        ///  序列化proto
        /// </summary>
        /// <typeparam name="TT"></typeparam>
        /// <param name="_tt"> S2C proto</param>
        /// <returns></returns>
        public MemoryStreamEx ProtobufSerialize <TT>(TT _tt)
        {
            MemoryStreamEx ms = new MemoryStreamEx();

            Serializer.Serialize <TT>(ms, _tt);

            //byte[] _bytes = new byte[2];
            MemoryStream mss = new MemoryStream();

            return(ms);
        }
Пример #3
0
        public void Update()
        {
            MemoryStreamEx comunicationMem = this.m_ComunicationMem;

            Monitor.Enter(comunicationMem);
            try
            {
                if (this.m_ComunicationMem.Length > 0L)
                {
                    if (this.m_Reader != null)
                    {
                        this.m_Reader.DidReadData(this.m_ComunicationMem.GetBuffer(), (int)this.m_ComunicationMem.Length);
                    }
                    this.m_ComunicationMem.Clear();
                }
            }
            finally
            {
                Monitor.Exit(comunicationMem);
            }
            object eNetWorkState = this.m_eNetWorkState;

            Monitor.Enter(eNetWorkState);
            try
            {
                EClientNetWorkState eClientNetWorkState = (EClientNetWorkState)((int)this.m_eNetWorkState);
                if (eClientNetWorkState == EClientNetWorkState.E_CNWS_CONNECTED)
                {
                    this.CallBackNetState(eClientNetWorkState);
                    m_eNetWorkState = EClientNetWorkState.E_CNWS_NORMAL;
                }
                if (eClientNetWorkState > EClientNetWorkState.E_CNWS_NORMAL && this.m_ClientSocket != null)
                {
                    this.ReleaseSocket();
                    this.CallBackNetState(eClientNetWorkState);
                }
            }
            finally
            {
                Monitor.Exit(eNetWorkState);
            }
        }
Пример #4
0
        public MemoryStreamEx MoveStream(int index)
        {
            //1. 得到正在使用的MemoryStreamEx
            MemoryStreamEx memoryStreamEx = (MemoryStreamEx)this.m_streamList[this.m_nActivedStreamPosition];

            if (index > 0)
            {
                //2. index为将要移动的stream的开始位置,也就是网络接收到的stream解析完一个完整消息后的位置,把剩余的stream移动出来
                if ((long)index < memoryStreamEx.Length)
                {
                    //3. 把当前使用的MemoryStreamEx位置索引更新
                    this.m_nActivedStreamPosition = (this.m_nActivedStreamPosition + 1) % 2;
                    MemoryStreamEx memoryStreamEx2 = (MemoryStreamEx)this.m_streamList[this.m_nActivedStreamPosition];
                    memoryStreamEx2.Clear();
                    //4. 把剩余未解析的stream拷贝到memoryStreamEx2里去。
                    memoryStreamEx2.Write(memoryStreamEx.GetBuffer(), index, (int)(memoryStreamEx.Length - (long)index));
                    memoryStreamEx.Clear();
                    return(memoryStreamEx2);
                }
                memoryStreamEx.Clear();
            }
            return(memoryStreamEx);
        }