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;
                    }
                }
            }
        }
예제 #2
0
        private void HandleRequestMessage(Stream message)
        {
            using (EneterTrace.Entering())
            {
                ProtocolMessage aProtocolMessage = myProtocolFormatter.DecodeMessage(message);

                if (aProtocolMessage != null)
                {
                    // If it is open connection then add the new connected client.
                    if (aProtocolMessage.MessageType == EProtocolMessageType.OpenConnectionRequest)
                    {
                        if (!string.IsNullOrEmpty(aProtocolMessage.ResponseReceiverId))
                        {
                            using (ThreadLock.Lock(myConnectedClients))
                            {
                                if (!myConnectedClients.ContainsKey(aProtocolMessage.ResponseReceiverId))
                                {
                                    TimeSpan           aDummyOpenTimeout = TimeSpan.Zero;
                                    SharedMemorySender aClientSender     = new SharedMemorySender(aProtocolMessage.ResponseReceiverId, false, aDummyOpenTimeout, mySendResponseTimeout, myMaxMessageSize, mySecurity);

                                    myConnectedClients[aProtocolMessage.ResponseReceiverId] = aClientSender;
                                }
                                else
                                {
                                    EneterTrace.Warning(TracedObject + "could not open connection for client '" + aProtocolMessage.ResponseReceiverId + "' because the client with same id is already connected.");
                                }
                            }
                        }
                        else
                        {
                            EneterTrace.Warning(TracedObject + "could not connect a client because response recevier id was not available in open connection message.");
                        }
                    }
                    // When a client closes connection with the service.
                    else if (aProtocolMessage.MessageType == EProtocolMessageType.CloseConnectionRequest)
                    {
                        if (!string.IsNullOrEmpty(aProtocolMessage.ResponseReceiverId))
                        {
                            using (ThreadLock.Lock(myConnectedClients))
                            {
                                SharedMemorySender aClientSender;
                                myConnectedClients.TryGetValue(aProtocolMessage.ResponseReceiverId, out aClientSender);

                                if (aClientSender != null)
                                {
                                    myConnectedClients.Remove(aProtocolMessage.ResponseReceiverId);

                                    aClientSender.Dispose();
                                }
                            }
                        }
                    }

                    MessageContext aMessageContext = new MessageContext(aProtocolMessage, "");
                    NotifyMessageContext(aMessageContext);
                }
            }
        }
예제 #3
0
        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();
            }
        }