private void m_ConnectCallBack(IAsyncResult asyncResult) { FIRING_D_C_F _firEventType = FIRING_D_C_F.DEFAULT; string errorMessage = string.Empty; SocketAnsynParam _ansynParam = asyncResult.AsyncState as SocketAnsynParam; ISocketConEventArgs _conEventArgs = _ansynParam.transEventArgs as ISocketConEventArgs; try { _ansynParam.workSocket.EndConnect(asyncResult); errorMessage = "√√√ no mistake √√√"; _firEventType = FIRING_D_C_F.FIR_COMPLETE; } catch (ArgumentNullException ee) { errorMessage = "××× ArgumentNullException ×××:" + " asyncResult 为 null。" + ee.Message; _firEventType = FIRING_D_C_F.FIR_FAILED; } catch (ArgumentException ee) { errorMessage = "××× ArgumentException ×××:" + " BeginConnect 方法调用未返回 asyncResult。" + 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 (InvalidOperationException ee) { errorMessage = "××× InvalidOperationException ×××:" + "先前曾为异步连接调用过 EndConnect。" + 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("<<<<<<< CON ENP IN CLIENT >>>>>>>", "*-*-*-*-*[ out ]*-*-*-*-*"); _conEventArgs.SetErrMessage(errorMessage); m_ConnectEventFiring(_firEventType, _conEventArgs, "m_ConnectCallBack"); }
public void TryAnsyncReceiveMessage(int bufferSize, string tag) { DebugQy.LogTag("<<<<<<< REV MSG IN CLIENT <<<<<<<", "↓↓↓↓↓↓ [ in ][" + tag + ":" + bufferSize + "]↓↓↓↓↓↓ ", "#3C9553FF"); DebugQy.LogTag("m_ReceiveMessage", " ---- in ---- ", "#3C9553FF"); ISocketRecEventArgs recEventArgs = new RecEventArgs(); recEventArgs.NeedRecvBufferSize = bufferSize; recEventArgs.Tag = tag; SocketAnsynParam stateParam = new SocketAnsynParam(); stateParam.workSocket = m_clientSocket; stateParam.transEventArgs = recEventArgs; m_ReceiveMessage(stateParam); }
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"); }
private void m_SendMsgCallBack(IAsyncResult asyncResult) { DebugQy.LogTag("m_SendMsgCallBack", " ---- in ---- ", "#3487E8FF"); string errorMessage = string.Empty; SocketAnsynParam ansynParam = asyncResult.AsyncState as SocketAnsynParam; ISocketSndEventArgs sndEventArgs = ansynParam.transEventArgs as ISocketSndEventArgs; FIRING_D_C_F firEventType = FIRING_D_C_F.DEFAULT; try { SocketError socketErrorCode; int sndBufferSize = ansynParam.workSocket.EndSend(asyncResult, out socketErrorCode); if (socketErrorCode != SocketError.Success) { DebugQy.LogWarning("[SEND MESAGE CALLBACK] Socket error : [" + socketErrorCode.ToString() + "]"); } if (sndBufferSize == sndEventArgs.BufferSize) { errorMessage = "√√√ no mistake √√√ SendComplete buffer lenth is :[" + sndBufferSize + "]"; firEventType = FIRING_D_C_F.FIR_COMPLETE; } else { DebugQy.LogWarning("Real 'SEND' buffer != needSize NeedSndLengrh :[" + sndEventArgs.BufferSize + "]*-*-*-*-*"); DebugQy.LogWarning("Real 'SEND' buffer != needSize RealSndLengrh :[" + sndBufferSize + "]*-*-*-*-*"); DebugQy.LogWarning("源:." + Encoding.UTF8.GetString(sndEventArgs.SendBuffer, 0, sndEventArgs.BufferSize) + "."); errorMessage = "××× receive bytes size is wrong. ××× :" + "需接收数据与接收数据字节不匹配!"; DebugQy.LogWarning(errorMessage); firEventType = FIRING_D_C_F.FIR_FAILED; //TODO:返回值校验的逻辑处理:查看Socket底层后再做补充 !!! } } catch (ArgumentNullException ee) { errorMessage = "××× ArgumentNullException ×××:" + "asyncResult 为 null。" + ee.Message; firEventType = FIRING_D_C_F.FIR_FAILED; } catch (ArgumentException ee) { errorMessage = "××× ArgumentException ×××:" + "BeginSend 方法调用后未返回 asyncResult。" + 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 (InvalidOperationException ee) { errorMessage = "××× InvalidOperationException ×××" + "先前为异步发送已调用过 EndSend。" + 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; } sndEventArgs.SetErrMessage(errorMessage); m_SendMessageEventFiring(firEventType, sndEventArgs, "m_SendMsgCallBack"); DebugQy.LogTag(">>>>>>> SEN MSG IN CLIENT >>>>>>>", "↑↑↑↑↑↑↑ [ out ][" + sndEventArgs.Tag + ":" + sndEventArgs.BufferSize + "]↑↑↑↑↑↑↑ ", "#3487E8FF"); }
private void m_SendMessage(byte[] buffer, string tag, SocketFlags flags = SocketFlags.None) { DebugQy.LogTag("m_SendMessage", " ---- in ---- ", "#3487E8FF"); FIRING_D_C_F firEventType = FIRING_D_C_F.DEFAULT; string errorMessage = string.Empty; ISocketSndEventArgs sndEventArgs = new SndEventArgs(); sndEventArgs.SetBuffer(buffer); sndEventArgs.SetTag(tag); SocketAnsynParam stateParam = new SocketAnsynParam(); stateParam.workSocket = m_clientSocket; stateParam.transEventArgs = sndEventArgs; try { SocketError socketErrorCode; m_clientSocket.BeginSend(sndEventArgs.SendBuffer, 0, sndEventArgs.BufferSize, flags, out socketErrorCode, new AsyncCallback(m_SendMsgCallBack), stateParam ); if (socketErrorCode != SocketError.Success) { DebugQy.LogWarning("[SEND MESAGE] Socket error : [" + socketErrorCode.ToString() + "] ,Check it !!!!!!!"); } } catch (ArgumentNullException ee) { errorMessage = "××× ArgumentNullException ×××:" + "buffers 为 null。" + ee.Message; firEventType = FIRING_D_C_F.FIR_FAILED; } catch (ArgumentException ee) { errorMessage = "××× ArgumentException ×××:" + "buffers 为空。" + 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 (Exception ee) { errorMessage = "××× Unknown exception ××× :" + "Firing other unknown exception !!!!!!" + ee.Message; firEventType = FIRING_D_C_F.FIR_FAILED; } sndEventArgs.SetErrMessage(errorMessage); m_SendMessageEventFiring(firEventType, sndEventArgs, "m_SendMessage:"); }
private void m_BeginConnect(EndPoint endPoint) { DebugQy.LogTag("<<<<<<< CON ENP IN CLIENT >>>>>>>", "*-*-*-*-*[ in ]*-*-*-*-*"); FIRING_D_C_F _firEventType = FIRING_D_C_F.DEFAULT; string errorMessage = string.Empty; ISocketConEventArgs _conEventArgs = new ConEventArgs(); SocketAnsynParam stateParam = new SocketAnsynParam(); stateParam.workSocket = m_clientSocket; stateParam.transEventArgs = _conEventArgs; try { m_clientSocket.BeginConnect(endPoint, new AsyncCallback(m_ConnectCallBack), stateParam ); } catch (ArgumentNullException ee) { errorMessage = "××× ArgumentNullException ××× :" + "remoteEP is null. Error code :" + 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 (SecurityException ee) { errorMessage = "××× SecurityException ××× :" + "调用堆栈上部的调用方无权执行所请求的操作。" + ee.Message; _firEventType = FIRING_D_C_F.FIR_FAILED; } catch (InvalidOperationException ee) { errorMessage = "××× InvalidOperationException ××× :" + "Socket 为 Listen。" + 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; } _conEventArgs.SetErrMessage(errorMessage); m_ConnectEventFiring(_firEventType, _conEventArgs, "m_BeginConnect"); }