private static void AcceptCallback(IAsyncResult result) { var ima = result.AsyncState as IncomingMessageAcceptor; try { if (ima == null) { var logger = LogManager.GetLogger("IncomingMessageAcceptor", LoggerType.Runtime); if (result.AsyncState == null) { logger.Warn(ErrorCode.Messaging_IMA_AcceptCallbackNullState, "AcceptCallback invoked with a null unexpected async state"); } else { logger.Warn(ErrorCode.Messaging_IMA_AcceptCallbackUnexpectedState, "AcceptCallback invoked with an unexpected async state of type {0}", result.AsyncState.GetType()); } return; } // First check to see if we're shutting down, in which case there's no point in doing anything other // than closing the accepting socket and returning. if (ima.Cts.IsCancellationRequested) { SocketManager.CloseSocket(ima.AcceptingSocket); ima.Log.Info(ErrorCode.Messaging_IMA_ClosingSocket, "Closing accepting socket during shutdown"); return; } // Then, start a new Accept try { ima.AcceptingSocket.BeginAccept(AcceptCallback, ima); } catch (Exception ex) { ima.Log.Warn(ErrorCode.MessagingBeginAcceptSocketException, "Exception on accepting socket during BeginAccept", ex); // Open a new one ima.RestartAcceptingSocket(); } Socket sock; // Complete this accept try { sock = ima.AcceptingSocket.EndAccept(result); } catch (ObjectDisposedException) { // Socket was closed, but we're not shutting down; we need to open a new socket and start over... // Close the old socket and open a new one ima.Log.Warn(ErrorCode.MessagingAcceptingSocketClosed, "Accepting socket was closed when not shutting down"); ima.RestartAcceptingSocket(); return; } catch (Exception ex) { // There was a network error. We need to get a new accepting socket and re-issue an accept before we continue. // Close the old socket and open a new one ima.Log.Warn(ErrorCode.MessagingEndAcceptSocketException, "Exception on accepting socket during EndAccept", ex); ima.RestartAcceptingSocket(); return; } if (ima.Log.IsVerbose3) { ima.Log.Verbose3("Received a connection from {0}", sock.RemoteEndPoint); } // Finally, process the incoming request: // Prep the socket so it will reset on close sock.LingerState = new LingerOption(true, 0); // Add the socket to the open socket collection if (ima.RecordOpenedSocket(sock)) { // And set up the asynch receive var rcc = new ReceiveCallbackContext(sock, ima); try { rcc.BeginReceive(ReceiveCallback); } catch (Exception exception) { var socketException = exception as SocketException; ima.Log.Warn(ErrorCode.Messaging_IMA_NewBeginReceiveException, String.Format("Exception on new socket during BeginReceive with RemoteEndPoint {0}: {1}", socketException != null ? socketException.SocketErrorCode.ToString() : "", rcc.RemoteEndPoint), exception); ima.SafeCloseSocket(sock); } } else { ima.SafeCloseSocket(sock); } } catch (Exception ex) { var logger = ima != null ? ima.Log : LogManager.GetLogger("IncomingMessageAcceptor", LoggerType.Runtime); logger.Error(ErrorCode.Messaging_IMA_ExceptionAccepting, "Unexpected exception in IncomingMessageAccepter.AcceptCallback", ex); } }
private static void AcceptCallback(IAsyncResult result) { var ima = result.AsyncState as IncomingMessageAcceptor; try { if (ima == null) { var logger = LogManager.GetLogger("IncomingMessageAcceptor", LoggerType.Runtime); if (result.AsyncState == null) logger.Warn(ErrorCode.Messaging_IMA_AcceptCallbackNullState, "AcceptCallback invoked with a null unexpected async state"); else logger.Warn(ErrorCode.Messaging_IMA_AcceptCallbackUnexpectedState, "AcceptCallback invoked with an unexpected async state of type {0}", result.AsyncState.GetType()); return; } // First check to see if we're shutting down, in which case there's no point in doing anything other // than closing the accepting socket and returning. if (ima.Cts.IsCancellationRequested) { SocketManager.CloseSocket(ima.AcceptingSocket); ima.Log.Info(ErrorCode.Messaging_IMA_ClosingSocket, "Closing accepting socket during shutdown"); return; } // Then, start a new Accept try { ima.AcceptingSocket.BeginAccept(AcceptCallback, ima); } catch (Exception ex) { ima.Log.Warn(ErrorCode.MessagingBeginAcceptSocketException, "Exception on accepting socket during BeginAccept", ex); // Open a new one ima.RestartAcceptingSocket(); } Socket sock; // Complete this accept try { sock = ima.AcceptingSocket.EndAccept(result); } catch (ObjectDisposedException) { // Socket was closed, but we're not shutting down; we need to open a new socket and start over... // Close the old socket and open a new one ima.Log.Warn(ErrorCode.MessagingAcceptingSocketClosed, "Accepting socket was closed when not shutting down"); ima.RestartAcceptingSocket(); return; } catch (Exception ex) { // There was a network error. We need to get a new accepting socket and re-issue an accept before we continue. // Close the old socket and open a new one ima.Log.Warn(ErrorCode.MessagingEndAcceptSocketException, "Exception on accepting socket during EndAccept", ex); ima.RestartAcceptingSocket(); return; } if (ima.Log.IsVerbose3) ima.Log.Verbose3("Received a connection from {0}", sock.RemoteEndPoint); // Finally, process the incoming request: // Prep the socket so it will reset on close sock.LingerState = new LingerOption(true, 0); // Add the socket to the open socket collection if (ima.RecordOpenedSocket(sock)) { // And set up the asynch receive var rcc = new ReceiveCallbackContext(sock, ima); try { rcc.BeginReceive(ReceiveCallback); } catch (Exception exception) { var socketException = exception as SocketException; ima.Log.Warn(ErrorCode.Messaging_IMA_NewBeginReceiveException, String.Format("Exception on new socket during BeginReceive with RemoteEndPoint {0}: {1}", socketException != null ? socketException.SocketErrorCode.ToString() : "", rcc.RemoteEndPoint), exception); ima.SafeCloseSocket(sock); } } else { ima.SafeCloseSocket(sock); } } catch (Exception ex) { var logger = ima != null ? ima.Log : LogManager.GetLogger("IncomingMessageAcceptor", LoggerType.Runtime); logger.Error(ErrorCode.Messaging_IMA_ExceptionAccepting, "Unexpected exception in IncomingMessageAccepter.AcceptCallback", ex); } }