/// <summary> /// Gets called when a receive operation resolves. If we were listening for data and the connection /// closed, that also counts as a receive operation resolving. /// </summary> /// <param name="args"></param> private void OnReceiveResolved(int numGot, byte[] data, EndPoint ep) { //// Log.LogMsg("==>++++ Async RECEIVE Op Completed - #" + ((SockState)args.UserToken).ID.ToString() + "#"); INetworkConnection con = null; try { if (!Listening) { return; } con = GetConnnection(ep as IPEndPoint); // If no data was received, close the connection. This is a NORMAL // situation that shows when the client has finished sending data. if (numGot == 0) { con.KillConnection("Connection closed by remote host."); return; } // restart listening process con.AssembleInboundPacket(data, numGot, m_State); } catch (Exception ex) { Log.LogMsg("UDPListenerDuplexBlocking Error ProcessReceive. " + ex.Message); if (con != null) { con.KillConnection("UDPListenerDuplexBlocking Error receive. " + ex.Message); } } }
/// <summary> /// Gets called when a receive operation resolves. If we were listening for data and the connection /// closed, that also counts as a receive operation resolving. /// </summary> /// <param name="args"></param> private void OnReceiveResolved(SocketAsyncEventArgs args) { //// Log.LogMsg("Testy 50"); //// Log.LogMsg("==>++++ Async RECEIVE Op Completed - #" + ((SockState)args.UserToken).ID.ToString() + "#"); INetworkConnection con = null; try { if (m_ShuttingDown || !Listening) { return; } //// Log.LogMsg("Testy 51"); con = GetConnnection(args.RemoteEndPoint as IPEndPoint); if (args.SocketError == SocketError.Success && con == null || !con.IsAlive) { //// Log.LogMsg("Testy 52"); throw new ArgumentException("UDPListener - received packet from unknown connection " + args.RemoteEndPoint.ToString()); } SockState state = args.UserToken as SockState; if (args.SocketError != SocketError.Success) { //// Log.LogMsg("Testy 53"); con.KillConnection("UDP Connection lost! Network receive error: " + args.SocketError.ToString()); //Jump out of the ProcessReceive method. return; } // If no data was received, close the connection. This is a NORMAL // situation that shows when the client has finished sending data. if (args.BytesTransferred == 0) { //// Log.LogMsg("Testy 54"); con.KillConnection("Connection closed by remote host."); return; } // restart listening process //// Log.LogMsg("Testy 55"); con.AssembleInboundPacket(args, state); } catch (Exception ex) { Log.LogMsg("UDPListener Error ProcessReceive. " + ex.Message); if (con != null) { con.KillConnection("UDPListener Error receive. " + ex.Message); } } finally { if (!m_ShuttingDown) { bool willRaiseEvent = false; // Post async receive operation on the socket. //Log.LogMsg("==><<<< TCP Async RECEIVE op started - #" + ((SockState)m_RecArgs.UserToken).ID.ToString() + "#"); willRaiseEvent = Socket.ReceiveFromAsync(args); if (!willRaiseEvent) { OnReceiveResolved(args); } } } }