/// <summary> /// 协议发送事件失败回调 /// </summary> /// <param name="sender">触发该事件的SocketQY</param> /// <param name="eventArgs">参数</param> private void m_ProtocolRecvCompleteCallback(ISocketQy sender, ISocketRecEventArgs eventArgs) { string recStr = Encoding.ASCII.GetString(eventArgs.ReceiveBuffer); DebugQy.LogTag("ClientController", "Recv Complete CallBack:" + recStr); m_FirSomeEvent(EVENT_TYPE.RECEIVE, COMPLETE_OR_FAILED.COMPLETE, recStr); }
private void m_ReceiveBodyDataCallBack(ISocketQy sender, ISocketRecEventArgs args) { int currentBufferSize = args.NeedRecvBufferSize; if (currentBufferSize != m_nextPackageSize) { DebugQy.LogWarning("m_ReceiveBodyDataCallBack(): rec buffer size != 'BODY_BUFFER_SIZE' . Please check your operate in receive."); DebugQy.LogWarning("'I Has stop your receive function !' ------- from writer. "); if (RecMsgFailedEvent != null) { RecMsgFailedEvent(sender, args); } StopReceiveMessage(); return; } DebugQy.Log("'BODY_PACKAGE' has received."); m_nextHeadOrBody = HEAD_BODY_PACKET.HEAD; m_nextPackageSize = HEAD_BUFFER_SIZE; DebugQy.Log("start receive [BODY] :-------- thread.set ↑↑↑↑↑\n "); m_recEventWaitHandle.Set(); if (RecMsgCompleteEvent != null) { RecMsgCompleteEvent(sender, args); } }
private void m_ReceiveMsgFailedCallBack(ISocketQy sender, ISocketRecEventArgs eventArgs) { if (RecMsgFailedEvent != null) { RecMsgFailedEvent(sender, eventArgs); } }
private void m_ReceiveMessageEventFiring(FIRING_D_C_F firType, ISocketRecEventArgs recArgs, string funcionTag) { switch (firType) { case FIRING_D_C_F.DEFAULT: DebugQy.LogTag(funcionTag, "begin receive . please wait callback.", "#3C9553FF"); break; case FIRING_D_C_F.FIR_COMPLETE: DebugQy.LogTag(funcionTag, firType.ToString(), "#3C9553FF"); if (AnsyncRecMsg_Complete_Event != null) { ISocketRecEventArgs _args = recArgs; AnsyncRecMsg_Complete_Event(this, _args); } break; case FIRING_D_C_F.FIR_FAILED: DebugQy.LogTag(funcionTag, firType.ToString() + recArgs.ErrMessage, "#BB7979FF"); if (AnsyncRecMsg_Failed_Event != null) { ISocketRecEventArgs _args = recArgs; AnsyncRecMsg_Failed_Event(this, _args); } break; default: break; } }
private void m_ReceiveMsgCompleteCallBack(ISocketQy sender, ISocketRecEventArgs eventArgs) { if (eventArgs.Tag == "HEAD") { m_ReceiveHeadDataCallBack(sender, eventArgs); } else if (eventArgs.Tag == "BODY") { m_ReceiveBodyDataCallBack(sender, eventArgs); } }
private void m_ReceiveHeadDataCallBack(ISocketQy sender, ISocketRecEventArgs args) { int currentBufferSize = args.NeedRecvBufferSize; if (currentBufferSize != HEAD_BUFFER_SIZE) { DebugQy.LogWarning("m_ReceiveHeadDataCallBack(): rec buffer size != 'HEAD_BUFFER_SIZE' . Please check your operate in receive."); if (RecMsgFailedEvent != null) { RecMsgFailedEvent(sender, args); } DebugQy.LogWarning("'I Has stop your receive function !' ------- from writer. "); StopReceiveMessage(); return; } string responseStr = Encoding.UTF8.GetString(args.ReceiveBuffer, 0, args.NeedRecvBufferSize); DebugQy.LogTag("HeadBodyTcpProtocol", "'HEAD_PACKAGE' has received. value of the package is :[" + responseStr + "]"); int nextBufferSize; bool isSuccess = m_GetIntFromStrWith_SCALE_OF_BYTE_SYSTEM(responseStr, out nextBufferSize); if (!isSuccess) { DebugQy.LogWarning("m_ReceiveHeadDataCallBack():the 'HeadBuffer' from sender is not a appoint value.\n" + "HeadBuffer to string :---." + responseStr + ".---"); if (RecMsgFailedEvent != null) { RecMsgFailedEvent(sender, args); } StopReceiveMessage(); DebugQy.LogWarning("'I Has stop your receive function !' ------- from writer."); } else { DebugQy.LogTag("HeadBodyTcpProtocol", "'HEAD_PACKAGE' say next body size is:[" + nextBufferSize + "]"); m_nextHeadOrBody = HEAD_BODY_PACKET.BODY; m_nextPackageSize = nextBufferSize; m_recEventWaitHandle.Set(); DebugQy.LogTag("HeadBodyTcpProtocol", "start receive [HEAD] :-------- thread.set ↑↑↑↑↑\n "); } }
/// <summary> /// 协议发送事件失败回调 /// </summary> /// <param name="sender">触发该事件的SocketQY</param> /// <param name="eventArgs">参数</param> private void m_ProtocolRecvFailedCallback(ISocketQy sender, ISocketRecEventArgs eventArgs) { DebugQy.LogTag("ClientController", "Recv Failed CallBack:" + eventArgs.ErrMessage); m_FirSomeEvent(EVENT_TYPE.RECEIVE, COMPLETE_OR_FAILED.FAILED, eventArgs.ErrMessage); }
private void m_ReceiveMsgCallBack(IAsyncResult asyncResult) { DebugQy.LogTag("m_ReceiveMsgCallBack", " ---- in ---- ", "#3C9553FF"); FIRING_D_C_F firEventType = FIRING_D_C_F.DEFAULT; string errorMessage = string.Empty; SocketAnsynParam ansynParam = asyncResult.AsyncState as SocketAnsynParam; ISocketRecEventArgs recEventArgs = ansynParam.transEventArgs as ISocketRecEventArgs; try { SocketError socketErrorCode; int realRevbufSize = ansynParam.workSocket.EndReceive(asyncResult, out socketErrorCode); if (socketErrorCode != SocketError.Success) { DebugQy.LogWarning("[RECV MESAGE] Socket error : [" + socketErrorCode.ToString() + "] ,Check it !!!!!!!"); } if (realRevbufSize == 0) { errorMessage = "××× receive bytes size is '0' ××× :" + "远程链接已调用shutDown\\close."; firEventType = FIRING_D_C_F.FIR_FAILED; } else { DebugQy.LogTag("m_ReceiveMsgCallBack", "Current receive buffer length :[" + realRevbufSize + "]", "#3C9553FF"); byte[] bytes = new byte[realRevbufSize]; Buffer.BlockCopy(m_tempRecvBuffer, 0, bytes, 0, realRevbufSize); m_lastBuffer.AddRange(bytes); if (m_lastBuffer.Count >= recEventArgs.NeedRecvBufferSize) { DebugQy.LogTag("m_ReceiveMsgCallBack", "Real 'RECV' buffer > needSize NeedRecLength :[" + recEventArgs.NeedRecvBufferSize + "]", "#3C9553FF"); DebugQy.LogTag("m_ReceiveMsgCallBack", "Real 'RECV' buffer > needSize RealRecLengrh :[" + m_lastBuffer.Count + "]", "#3C9553FF"); byte[] buffer = new byte[recEventArgs.NeedRecvBufferSize]; m_lastBuffer.CopyTo(0, buffer, 0, recEventArgs.NeedRecvBufferSize); m_lastBuffer.RemoveRange(0, recEventArgs.NeedRecvBufferSize); recEventArgs.SetBuffer(buffer); errorMessage = "√√√ no mistake √√√ "; firEventType = FIRING_D_C_F.FIR_COMPLETE; } else if (m_lastBuffer.Count < recEventArgs.NeedRecvBufferSize) { DebugQy.LogTag("m_ReceiveMsgCallBack", "Real 'RECV' buffer < needSize NeedRecLength :[" + recEventArgs.NeedRecvBufferSize + "]", "#3C9553FF"); DebugQy.LogTag("m_ReceiveMsgCallBack", "Real 'RECV' buffer < needSize RealRecLengrh :[" + m_lastBuffer.Count + "]", "#3C9553FF"); m_ReceiveMessage(ansynParam); return; } } } catch (ArgumentNullException ee) { errorMessage = "××× ArgumentNullException ×××:" + "asyncResult 为 null。" + ee.Message; firEventType = FIRING_D_C_F.FIR_FAILED; } catch (ArgumentException ee) { errorMessage = "××× ArgumentException ×××:" + "方法调用后未返回 asyncResult。" + ee.Message; firEventType = FIRING_D_C_F.FIR_FAILED; } catch (ObjectDisposedException ee) { errorMessage = "××× ObjectDisposedException ×××:" + "Socket 已关闭。" + ee.Message; firEventType = FIRING_D_C_F.FIR_FAILED; } catch (InvalidOperationException ee) { errorMessage = "××× InvalidOperationException ×××:" + "先前曾为异步读取调用过 EndReceive。" + ee.Message; firEventType = FIRING_D_C_F.FIR_FAILED; } catch (SocketException ee) { errorMessage = "××× SocketException ×××:" + " 试图访问套接字时发生错误。" + ee.Message; firEventType = FIRING_D_C_F.FIR_FAILED; } catch (Exception ee) { errorMessage = "××× Unknown exception ××× :" + "Firing other unknown exception !!!!!!" + ee.Message; firEventType = FIRING_D_C_F.FIR_FAILED; } DebugQy.LogTag("<<<<<<< REV MSG IN CLIENT <<<<<<<", "↑↑↑↑↑↑↑ [ out ][" + recEventArgs.Tag + ":" + recEventArgs.NeedRecvBufferSize + "]↑↑↑↑↑↑↑ ", "#3C9553FF"); recEventArgs.SetErrMessage(errorMessage); m_ReceiveMessageEventFiring(firEventType, recEventArgs, "m_ReceiveMsgCallBack"); }
private void m_ReceiveMessage(SocketAnsynParam stateParam, SocketFlags flags = SocketFlags.None) { ISocketRecEventArgs recvEventArgs = stateParam.transEventArgs as ISocketRecEventArgs; string errorMessage = string.Empty; FIRING_D_C_F firEventType = FIRING_D_C_F.DEFAULT; if (m_lastBuffer.Count >= recvEventArgs.NeedRecvBufferSize) //当上一次剩余的缓存数据足够本次使用时 { DebugQy.LogTag("m_ReceiveMessage", "need buffer size :[" + recvEventArgs.NeedRecvBufferSize + "]", "#3C9553FF"); DebugQy.LogTag("m_ReceiveMessage", "last buffer length :[" + m_lastBuffer.Count + "]", "#3C9553FF"); DebugQy.LogTag("m_ReceiveMessage", "so dont need begin receive .", "#3C9553FF"); byte[] buffer = new byte[recvEventArgs.NeedRecvBufferSize]; m_lastBuffer.CopyTo(0, buffer, 0, recvEventArgs.NeedRecvBufferSize); m_lastBuffer.RemoveRange(0, recvEventArgs.NeedRecvBufferSize); recvEventArgs.SetBuffer(buffer); errorMessage = "√√√ no mistake √√√ "; firEventType = FIRING_D_C_F.FIR_COMPLETE; } else { try { SocketError socketErrorCode; m_clientSocket.BeginReceive(m_tempRecvBuffer, 0, m_tempRecvBuffer.Length, flags, out socketErrorCode, new AsyncCallback(m_ReceiveMsgCallBack), stateParam ); if (socketErrorCode != SocketError.Success) { DebugQy.LogWarning("[RECV MESAGE] Socket error : [" + socketErrorCode.ToString() + "] ,Check it !!!!!!!"); } } catch (ArgumentNullException ee) { errorMessage = "××× ArgumentNullException ×××:" + "buffers 为 null。" + ee.Message; firEventType = FIRING_D_C_F.FIR_FAILED; } catch (SocketException ee) { errorMessage = "××× SocketException ×××:" + "试图访问套接字时发生错误。" + ee.Message; firEventType = FIRING_D_C_F.FIR_FAILED; } catch (ObjectDisposedException ee) { errorMessage = "××× ObjectDisposedException ×××:" + "Socket 已关闭。" + ee.Message; firEventType = FIRING_D_C_F.FIR_FAILED; } catch (ArgumentOutOfRangeException ee) { errorMessage = "××× ArgumentOutOfRangeException ×××:" + "offset 小于 0。-或 -offset 大于 buffer 的长度。-或 -size 小于 0。-或 -size 大于 buffer 的长度减去 offset 参数的值。" + ee.Message; firEventType = FIRING_D_C_F.FIR_FAILED; } catch (Exception ee) { errorMessage = "××× Unknown exception ××× :" + "Firing other unknown exception !!!!!!" + ee.Message; firEventType = FIRING_D_C_F.FIR_FAILED; } } recvEventArgs.SetErrMessage(errorMessage); m_ReceiveMessageEventFiring(firEventType, recvEventArgs, "m_ReceiveMessage"); }