private void CleanConnection(bool sendMessageFlag) { using (EneterTrace.Entering()) { using (ThreadLock.Lock(myConnectionManipulatorLock)) { myResponseMessageHandler = null; if (myResponseReceiver != null) { if (sendMessageFlag) { try { byte[] anEncodedMessage = (byte[])myProtocolFormatter.EncodeCloseConnectionMessage(myOutputConnectorAddress); if (anEncodedMessage != null) { myResponseReceiver.SendTo(anEncodedMessage, myServiceEndpoint); } } catch (Exception err) { EneterTrace.Warning(TracedObject + "failed to send close connection message.", err); } } myResponseReceiver.StopListening(); myResponseReceiver = null; } } } }
public override void DetachDuplexInputChannel() { using (EneterTrace.Entering()) { base.DetachDuplexInputChannel(); // If this is singleton service mode. if (mySingletonService != null) { mySingletonService.DetachInputChannel(); } else { // If per client mode then detach all service stubs. using (ThreadLock.Lock(myPerConnectionServices)) { foreach (KeyValuePair <string, ServiceStub <TServiceInterface> > aServiceStub in myPerConnectionServices) { aServiceStub.Value.UnsubscribeClientFromEvents(aServiceStub.Key); aServiceStub.Value.DetachInputChannel(); } } } } }
public void DetachDuplexInputChannel() { using (EneterTrace.Entering()) { myReceiver.DetachDuplexInputChannel(); } }
public RoundRobinBalancer(IMessagingSystemFactory outputMessagingFactory) { using (EneterTrace.Entering()) { myOutputMessagingFactory = outputMessagingFactory; } }
public IInputConnector CreateInputConnector(string inputConnectorAddress) { using (EneterTrace.Entering()) { return(new TcpInputConnector(inputConnectorAddress, myProtocolFormatter, mySecurityFactory, mySendTimeout, myReceiveTimeout, mySendBuffer, myReceiveBuffer, myReuseAddressFlag, myMaxAmountOfConnections)); } }
public void CloseConnection() { using (EneterTrace.Entering()) { CleanConnection(true); } }
// A client was connected the load balancer. protected override void OnResponseReceiverConnected(object sender, ResponseReceiverEventArgs e) { using (EneterTrace.Entering()) { Notify(ResponseReceiverConnected, e); } }
/// <summary> /// Deserializes data into the specified type. /// </summary> /// <typeparam name="_T">Type of serialized data.</typeparam> /// <param name="serializedData">Encrypted data to be deserialized.</param> /// <param name="algorithm">algorithm used to decrypt data before deserialization</param> /// <returns>Deserialized object.</returns> public _T Deserialize <_T>(object serializedData, SymmetricAlgorithm algorithm) { using (EneterTrace.Entering()) { byte[] anEncryptedData = (byte[])serializedData; // Put encrypted data to the stream. using (MemoryStream anEncryptedDataStream = new MemoryStream(anEncryptedData)) { // Algorythm decrypting data. SymmetricAlgorithm aDecryptor = algorithm; aDecryptor.Key = mySecretKey; aDecryptor.IV = myInitializeVector; // Create a decrytor to perform the stream transform. ICryptoTransform aTransfromer = aDecryptor.CreateDecryptor(aDecryptor.Key, aDecryptor.IV); using (CryptoStream aCryptoStream = new CryptoStream(anEncryptedDataStream, aTransfromer, CryptoStreamMode.Read)) { object aDecodedData = myEncoderDecoder.Decode(aCryptoStream); _T aDeserializedData = myUnderlyingSerializer.Deserialize <_T>(aDecodedData); return(aDeserializedData); } } } }
public void SendBroadcast(object message) { using (EneterTrace.Entering()) { List <string> aDisconnectedClients = new List <string>(); using (ThreadLock.Lock(myConnectedClients)) { foreach (string aClientId in myConnectedClients.Keys) { try { MessageBusMessage aMessage = new MessageBusMessage(EMessageBusRequest.SendResponseMessage, aClientId, message); object aSerializedMessage = mySerializer.Serialize <MessageBusMessage>(aMessage); myMessageBusOutputChannel.SendMessage(aSerializedMessage); } catch (Exception err) { EneterTrace.Error(TracedObject + ErrorHandler.FailedToSendResponseMessage, err); aDisconnectedClients.Add(aClientId); // Note: Exception is not rethrown because if sending to one client fails it should not // affect sending to other clients. } } } // Disconnect failed clients. foreach (String anOutputConnectorAddress in aDisconnectedClients) { CloseConnection(anOutputConnectorAddress, true); } } }
/// <summary> /// Creates the duplex input channel receiving messages from the duplex output channel and sending back response messages by using the thread pool. /// </summary> /// <param name="channelId">Identifies this duplex input channel.</param> /// <returns>duplex input channel</returns> public IDuplexInputChannel CreateDuplexInputChannel(string channelId) { using (EneterTrace.Entering()) { return(myDefaultMessagingFactory.CreateDuplexInputChannel(channelId)); } }
/// <summary> /// Serializes data to the object. /// The returned object is type of byte[]. Output bytes are encrypted. /// </summary> /// <typeparam name="_T">Type of serialized data.</typeparam> /// <param name="dataToSerialize">Data to be serialized.</param> /// <param name="algorithm">algorithm used to encrypt the serialized data</param> /// <returns>Data serialized in byte[].</returns> public object Serialize <_T>(_T dataToSerialize, SymmetricAlgorithm algorithm) { using (EneterTrace.Entering()) { // Use memory stream to store the compressed data. using (MemoryStream anEncryptedData = new MemoryStream()) { // Algorythm encrypting data. SymmetricAlgorithm anAlgorithm = algorithm; anAlgorithm.Key = mySecretKey; anAlgorithm.IV = myInitializeVector; // Object performing the encryption. ICryptoTransform aTransformer = anAlgorithm.CreateEncryptor(anAlgorithm.Key, anAlgorithm.IV); // Create stream encrypting data. using (CryptoStream anEncryptor = new CryptoStream(anEncryptedData, aTransformer, CryptoStreamMode.Write)) { // Use underlying serializer to serialize data. object aSerializedData = myUnderlyingSerializer.Serialize <_T>(dataToSerialize); myEncoderDecoder.Encode(anEncryptor, aSerializedData); } byte[] aCompressedDataArray = anEncryptedData.ToArray(); return(aCompressedDataArray); } } }
/// <summary> /// Creates the duplex output channel sending messages to the duplex input channel and receiving response messages by using the thread pool. /// </summary> /// <param name="channelId">Identifies the receiving duplex input channel.</param> /// <param name="responseReceiverId">Identifies the response receiver of this duplex output channel.</param> /// <returns>duplex output channel</returns> public IDuplexOutputChannel CreateDuplexOutputChannel(string channelId, string responseReceiverId) { using (EneterTrace.Entering()) { return(myDefaultMessagingFactory.CreateDuplexOutputChannel(channelId, responseReceiverId)); } }
/// <summary> /// Constructs the factory. /// </summary> /// <param name="protocolFormatter">formatter used to encode low-level messages between channels</param> public ThreadPoolMessagingSystemFactory(IProtocolFormatter protocolFormatter) { using (EneterTrace.Entering()) { myDefaultMessagingFactory = new DefaultMessagingSystemFactory(new ThreadPoolMessagingProvider(), protocolFormatter); } }
public void OpenConnection(Action <MessageContext> responseMessageHandler) { using (EneterTrace.Entering()) { if (responseMessageHandler == null) { throw new ArgumentNullException("responseMessageHandler is null."); } using (ThreadLock.Lock(myConnectionManipulatorLock)) { try { myResponseMessageHandler = responseMessageHandler; myResponseReceiver = UdpReceiver.CreateConnectedReceiver(myServiceEndpoint, myReuseAddressFlag, myResponseReceivingPort, myTtl); myResponseReceiver.StartListening(OnResponseMessageReceived); byte[] anEncodedMessage = (byte[])myProtocolFormatter.EncodeOpenConnectionMessage(myOutputConnectorAddress); if (anEncodedMessage != null) { myResponseReceiver.SendTo(anEncodedMessage, myServiceEndpoint); } } catch { CloseConnection(); throw; } } } }
private void CleanConnection(bool sendMessageFlag) { using (EneterTrace.Entering()) { using (ThreadLock.Lock(myConnectionManipulatorLock)) { if (myResponseReceiver != null) { myResponseReceiver.StopListening(); myResponseReceiver = null; } if (mySender != null) { if (sendMessageFlag) { // Send close connection message. try { object anEncodedMessage = myProtocolFormatter.EncodeCloseConnectionMessage(myOutputConnectorAddress); mySender.SendMessage(anEncodedMessage); } catch (Exception err) { EneterTrace.Warning(TracedObject + "failed to send close connection message.", err); } } mySender.Dispose(); mySender = null; } } } }
public void CloseConnection(string clientId) { using (EneterTrace.Entering()) { CloseConnection(clientId, false); } }
public void OpenConnection(Action <MessageContext> responseMessageHandler) { using (EneterTrace.Entering()) { if (responseMessageHandler == null) { throw new ArgumentNullException("responseMessageHandler is null."); } using (ThreadLock.Lock(myConnectionManipulatorLock)) { try { mySender = new NamedPipeSender(myInputConnectorAddress, myTimeout); myResponseMessageHandler = responseMessageHandler; myResponseReceiver = new NamedPipeReceiver(myOutputConnectorAddress, 1, myTimeout, mySecurity); myResponseReceiver.StartListening(HandleResponseMessages); // Send the open connection request. object anEncodedMessage = myProtocolFormatter.EncodeOpenConnectionMessage(myOutputConnectorAddress); mySender.SendMessage(anEncodedMessage); } catch { CloseConnection(); throw; } } } }
private void CloseConnection(string clientId, bool notifyFlag) { using (EneterTrace.Entering()) { try { MessageBusMessage aMessage = new MessageBusMessage(EMessageBusRequest.DisconnectClient, clientId, null); object aSerializedMessage = mySerializer.Serialize <MessageBusMessage>(aMessage); myMessageBusOutputChannel.SendMessage(aSerializedMessage); } catch (Exception err) { EneterTrace.Warning(TracedObject + ErrorHandler.FailedToCloseConnection, err); } if (notifyFlag) { ProtocolMessage aProtocolMessage = new ProtocolMessage(EProtocolMessageType.CloseConnectionRequest, clientId, null); MessageContext aMessageContext = new MessageContext(aProtocolMessage, ""); NotifyMessageContext(aMessageContext); } } }
// Removes all request receivers. public void RemoveAllDuplexOutputChannels() { using (EneterTrace.Entering()) { using (ThreadLock.Lock(myReceiverManipulatorLock)) { myAvailableReceivers.Clear(); // Close all open connections with request receivers. foreach (TConnection aConnection in myOpenConnections) { try { aConnection.DuplexOutputChannel.CloseConnection(); } catch (Exception err) { EneterTrace.Warning(TracedObject + "failed to close connection to " + aConnection.DuplexOutputChannel.ChannelId, err); } aConnection.DuplexOutputChannel.ResponseMessageReceived -= OnResponseMessageReceived; aConnection.DuplexOutputChannel.ConnectionClosed -= OnRequestReceiverClosedConnection; } // Clear all open connections. myOpenConnections.Clear(); // Note: Clients (response receivers) stay connected becaue it is still possible to add // new request receivers to the pool. } } }
public void StartListening(Action <MessageContext> messageHandler) { using (EneterTrace.Entering()) { if (messageHandler == null) { throw new ArgumentNullException("messageHandler is null."); } using (ThreadLock.Lock(myListeningManipulatorLock)) { try { myMessageHandler = messageHandler; myMessageBusOutputChannel.ResponseMessageReceived += OnMessageFromMessageBusReceived; // Open connection with the message bus. myMessageBusOutputChannel.OpenConnection(); // Register service in the message bus. MessageBusMessage aMessage = new MessageBusMessage(EMessageBusRequest.RegisterService, myServiceId, null); object aSerializedMessage = mySerializer.Serialize <MessageBusMessage>(aMessage); myMessageBusOutputChannel.SendMessage(aSerializedMessage); } catch { StopListening(); throw; } } } }
// A client was disconnected from the load balancer. protected override void OnResponseReceiverDisconnected(object sender, ResponseReceiverEventArgs e) { using (EneterTrace.Entering()) { using (ThreadLock.Lock(myReceiverManipulatorLock)) { myOpenConnections.RemoveWhere(x => { if (x.ResponseReceiverId == e.ResponseReceiverId) { try { x.DuplexOutputChannel.CloseConnection(); } catch (Exception err) { EneterTrace.Warning(TracedObject + ErrorHandler.FailedToCloseConnection, err); } x.DuplexOutputChannel.ResponseMessageReceived -= OnResponseMessageReceived; x.DuplexOutputChannel.ConnectionClosed -= OnRequestReceiverClosedConnection; return(true); } return(false); }); } Notify(ResponseReceiverDisconnected, e); } }
/// <summary> /// Method is called when a response is received from the duplex output channel. /// It wrapps the response and sends the wrapped response to the correct response receiver as the response. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnResponseMessageReceived(object sender, DuplexChannelMessageEventArgs e) { using (EneterTrace.Entering()) { try { // try to find the response receiver id where the wrapped message should be responded. TDuplexConnection aConnction = null; using (ThreadLock.Lock(myConnections)) { aConnction = myConnections.FirstOrDefault(x => x.DuplexOutputChannel == (IDuplexOutputChannel)sender); } if (aConnction != null) { ISerializer aSerializer = mySerializer.ForResponseReceiver(aConnction.ResponseReceiverId); object aMessage = DataWrapper.Wrap(e.ChannelId, e.Message, aSerializer); AttachedDuplexInputChannel.SendResponseMessage(aConnction.ResponseReceiverId, aMessage); } else { EneterTrace.Warning(TracedObject + "failed to send the response message because the response receiver id does not exist. It is possible the response receiver has already been disconnected."); } } catch (Exception err) { EneterTrace.Error(TracedObject + ErrorHandler.FailedToSendResponseMessage, err); } } }
// Removes the request receiver to the pool. public void RemoveDuplexOutputChannel(string channelId) { using (EneterTrace.Entering()) { using (ThreadLock.Lock(myReceiverManipulatorLock)) { myAvailableReceivers.Remove(channelId); // Close all open connection with this request receiver. myOpenConnections.RemoveWhere(x => { if (x.DuplexOutputChannel.ChannelId == channelId) { try { // Close connection with the request receiver. x.DuplexOutputChannel.CloseConnection(); } catch (Exception err) { EneterTrace.Warning(TracedObject + "failed to close connection to " + channelId, err); } x.DuplexOutputChannel.ConnectionClosed -= OnRequestReceiverClosedConnection; x.DuplexOutputChannel.ResponseMessageReceived -= OnResponseMessageReceived; return(true); } return(false); }); } } }
/// <summary> /// Creates duplex output channel which can send and receive messages from the duplex input channel using Android USB cable. /// </summary> /// <param name="channelId">Port number where the Android application is listening.</param> /// <param name="responseReceiverId">Identifies the response receiver of this duplex output channel.</param> /// <returns></returns> public IDuplexOutputChannel CreateDuplexOutputChannel(string channelId, string responseReceiverId) { using (EneterTrace.Entering()) { return(new AndroidUsbDuplexOutputChannel(int.Parse(channelId), responseReceiverId, myAdbHostPort, myUnderlyingTcpMessaging)); } }
protected override void OnResponseReceiverDisconnected(object sender, ResponseReceiverEventArgs e) { using (EneterTrace.Entering()) { if (mySingletonService != null) { mySingletonService.UnsubscribeClientFromEvents(e.ResponseReceiverId); } else { // If per client mode then remove service stub for the disconnected client. using (ThreadLock.Lock(myPerConnectionServices)) { // Unsubscribe disconnected client from all events. ServiceStub <TServiceInterface> aServiceStub; myPerConnectionServices.TryGetValue(e.ResponseReceiverId, out aServiceStub); if (aServiceStub != null) { aServiceStub.UnsubscribeClientFromEvents(e.ResponseReceiverId); aServiceStub.DetachInputChannel(); myPerConnectionServices.Remove(e.ResponseReceiverId); } } } if (ResponseReceiverDisconnected != null) { ResponseReceiverDisconnected(this, e); } } }
/// <summary> /// Creates the serializer that allows user to specify his own method instantiating XmlSerializer /// with desired settings. /// </summary> /// <param name="xmlSerializerFactoryMethod"> /// The factory method responsible for creation of XmlSerializer. /// The factory method is called during the serialization or deserialization.<br/> /// The factory method can be called from more threads at the same time, so be sure, your factory method /// is thread safe. /// </param> public XmlStringSerializer(Func <Type, XmlSerializer> xmlSerializerFactoryMethod) { using (EneterTrace.Entering()) { myXmlSerializerFactoryMethod = xmlSerializerFactoryMethod; } }
public void AttachDuplexInputChannel(IDuplexInputChannel duplexInputChannel) { using (EneterTrace.Entering()) { myReceiver.AttachDuplexInputChannel(duplexInputChannel); } }
/// <summary> /// Serializes data to the xml string. /// </summary> /// <remarks> /// It internally XmlSerializer provided by .Net. /// </remarks> /// <typeparam name="_T">Type of serialized data.</typeparam> /// <param name="dataToSerialize">Data to be serialized.</param> /// <returns>xml string</returns> public object Serialize <_T>(_T dataToSerialize) { using (EneterTrace.Entering()) { string aSerializedObjectStr = ""; try { using (StringWriter aStringWriter = new StringWriter()) { XmlWriterSettings anXmlSettings = new XmlWriterSettings(); anXmlSettings.OmitXmlDeclaration = true; using (XmlWriter anXmlWriter = XmlWriter.Create(aStringWriter, anXmlSettings)) { XmlSerializer xmlSerializer = myXmlSerializerFactoryMethod(typeof(_T)); xmlSerializer.Serialize(anXmlWriter, dataToSerialize); } aSerializedObjectStr = aStringWriter.ToString(); } } catch (Exception err) { EneterTrace.Error("The serialization to XML failed.", err); throw; } return(aSerializedObjectStr); } }
/// <summary> /// Returns IP addresses assigned to the device which can be used for listening. /// </summary> /// <returns>array of available addresses</returns> public static string[] GetAvailableIpAddresses() { using (EneterTrace.Entering()) { return(TcpMessagingSystemFactory.GetAvailableIpAddresses()); } }
/// <summary> /// Creates the duplex router. /// </summary> /// <returns>duplex router</returns> public IDuplexRouter CreateDuplexRouter() { using (EneterTrace.Entering()) { return(new DuplexRouter(myDuplexOutputChannelMessaging)); } }