/// <summary> /// Use underline socket to send protocol message async. /// </summary> /// <param name="vMessage">The protocol message to be sended.</param> public void SendMessage(System.Object vMsg) { Log.Info("一个消息需要发送"); if (State != ConnectionState.Established) { Log.Info("一个消息需要发送 1"); Debug.WriteLine(String.Format("SendMessage Error:in State {0}", State)); return; } if (vMsg == null) { Log.Info("一个消息需要发送 2"); Debug.WriteLine(String.Format("SendMessage Error:vMsg is null")); return; } ArraySegment <Byte> sndBuf = ProtoHelper.EncodeMessage(vMsg, vMsg is IResponse); SocketAsyncEventArgs sendEventArgs = new SocketAsyncEventArgs(); sendEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(OnCompletedForSend); sendEventArgs.SetBuffer(sndBuf.Array, sndBuf.Offset, sndBuf.Count); Debug.WriteLine(string.Format("SendMessage Send {0}", vMsg.GetType().Name)); Log.Info(string.Format("SendMessage Send {0}", vMsg.GetType().Name)); if (!ConnSocket.SendAsync(sendEventArgs)) { OnCompletedForSendImpl(sendEventArgs); sendEventArgs.Dispose(); } }
private void OnCompletedForReceiveImpl(SocketAsyncEventArgs e) { //Log.Info("接收到消息 Count = "+ e.BytesTransferred); // Log.Info("sadsadsadsa " + e.BytesTransferred); try { //Log.Info($"SocketError :{ e.SocketError}"); if (e.SocketError == SocketError.Success && e.BytesTransferred != 0) { // Log.Info("开始处理消息"); // at first, write state.buffer to recvcache RecvCache.Write(e.Buffer, e.BytesTransferred); //Debug.WriteLine(string.Format("OnCompletedForReceiveImpl Receive {0} RecvCache.Length={1}", e.BytesTransferred, RecvCache.Length)); // second, push the msg to recvqueue and decode message while (true) { ushort msgId = 0; bool flag; object msg = ProtoHelper.DecodeMessage(RecvCache, out msgId, out flag); //Log.Msg(msg.GetType()); if (msg == null) { break; } lock (RecvQueue) { var eMsg = new CCMSGConnectionRevMsg(); eMsg.MessageInfo.Message = msg as IMessage; eMsg.MessageInfo.Opcode = (ushort)msgId; eMsg.MessageInfo.flag = flag; // Log.Info("接收到消息 并且丢进了队列"); RecvQueue.Enqueue(new KeyValuePair <int, object>(eMsg.MessageId, eMsg)); } // all cache is handled if (RecvCache.Length == 0) { break; } } //Debug.WriteLine(string.Format("OnCompletedForReceiveImpl Receive End, RecvCache.Length={0}", RecvCache.Length)); RecvCache.Crunch(); // third, restart the async receive process m_receiveEventArg.SetBuffer(0, ProtoConst.MAX_PACKAGE_LENGTH); if (!ConnSocket.ReceiveAsync(m_receiveEventArg)) { OnCompletedForReceiveImpl(m_receiveEventArg); } return; } else { throw new Exception(string.Format("The result of ReceiveAsync is not correct, SocketError={0} BytesTransferred={1}", e.SocketError, e.BytesTransferred)); } } catch (Exception ex) { // Client exception means connection is disconnected // Or, there is an bad message format in byte stream CCMSGConnectionRecvFailure eMsg = new CCMSGConnectionRecvFailure(); eMsg.ExceptionInfo = ex.ToString(); lock (RecvQueue) { RecvQueue.Enqueue(new KeyValuePair <int, object>(eMsg.MessageId, eMsg)); } goto BREAK_CONNECT; } BREAK_CONNECT: // when receive action is over, which represents that socket can't receive any data lock (RecvQueue) { if (State == ConnectionState.Established || State == ConnectionState.Disconnecting) // 这个状态是为了让客户端主动断开服务器的时候 { State = ConnectionState.Closed; var vMsg = new CCMSGConnectionBreak(); RecvQueue.Enqueue(new KeyValuePair <int, object>(vMsg.MessageId, vMsg)); } } }