public void OpenConnection(Action <MessageContext> responseMessageHandler) { using (EneterTrace.Entering()) { // If it shall listen to responses then check the responseMessageHandler. if (responseMessageHandler == null) { throw new InvalidOperationException("responseMessageHandler is null."); } using (ThreadLock.Lock(myConnectionManipulatorLock)) { try { // Send open connection message to open the connection. // When DuplexInputChannel receives the open connection message it creates // shared memory for sending response messages. mySender = new SharedMemorySender(myInputConnectorAddress, true, myConnectTimeout, mySendTimeout, myMaxMessageSize, mySecurity); mySender.SendMessage(x => myProtocolFormatter.EncodeOpenConnectionMessage(myOutputConnectorAddress, x)); // The input connector has opened the shared memory for responses // so we can start listening from it. // (It will open existing memory mapped file.) myResponseMessageHandler = responseMessageHandler; myReceiver = new SharedMemoryReceiver(myOutputConnectorAddress, true, myConnectTimeout, myMaxMessageSize, mySecurity); myReceiver.StartListening(HandleResponseMessage); } catch { CloseConnection(); throw; } } } }
private void CloseConnection(string outputConnectorAddress, SharedMemorySender clientSender) { using (EneterTrace.Entering()) { try { clientSender.SendMessage(x => myProtocolFormatter.EncodeCloseConnectionMessage(outputConnectorAddress, x)); } catch (Exception err) { EneterTrace.Warning("failed to send the close message.", err); } clientSender.Dispose(); } }