Пример #1
1
        /// <summary>
        /// Sends a MDSMessage to the MDS server
        /// </summary>
        /// <param name="message">Message to send</param>
        public void SendMessage(MDSMessage message)
        {
            lock (_sendLock)
            {
                if (State != CommunicationStates.Connected || !_socket.Connected)
                {
                    throw new MDSException("Client's state is not connected. It can not send message.");
                }

                SendMessageToSocket(message);
            }
        }
Пример #2
0
 /// <summary>
 /// Serializes and writes a MDSMessage according to the protocol rules.
 /// </summary>
 /// <param name="serializer">Serializer to serialize message</param>
 /// <param name="message">Message to be serialized</param>
 public void WriteMessage(IMDSSerializer serializer, MDSMessage message)
 {
     //Write protocol type
     serializer.WriteUInt32(MDSDefaultProtocolType);
     
     //Write the message type
     serializer.WriteInt32(message.MessageTypeId);
     
     //Write message
     serializer.WriteObject(message);
 }
Пример #3
0
 /// <summary>
 /// Sends a MDSMessage object to MDS server.
 /// </summary>
 /// <param name="message"></param>
 private void SendMessageInternal(MDSMessage message)
 {
     try
     {
         _communicationChannel.SendMessage(message);
         LastOutgoingMessageTime = DateTime.Now;
     }
     catch (Exception)
     {
         CloseCommunicationChannel();
         throw;
     }
 }
Пример #4
0
        /// <summary>
        /// Sends MDSMessage object to the socket.
        /// </summary>
        /// <param name="message">Message to be sent</param>
        private void SendMessageToSocket(MDSMessage message)
        {
            Logger.Debug("Message is being sent to communicator " + ComminicatorId + ": " + message.GetType().Name);

            //Create MemoryStream to write message to a byte array
            var memoryStream = new MemoryStream();

            //Write message
            _wireProtocol.WriteMessage(new MDSDefaultSerializer(memoryStream), message);

            //Check the length of message data
            if (memoryStream.Length > CommunicationConsts.MaxMessageSize)
            {
                throw new Exception("Message is too big to send.");
            }

            //SendMessage message (contents of created memory stream)
            var sendBuffer = memoryStream.ToArray();
            var length = sendBuffer.Length;
            var totalSent = 0;
            while (totalSent < length)
            {
                var sent = _socket.Send(sendBuffer, totalSent, length - totalSent, SocketFlags.None);
                if (sent <= 0)
                {
                    throw new Exception("Message can not be sent via TCP socket. Only " + totalSent + " bytes of " + length + " bytes are sent.");
                }

                totalSent += sent;
            }

            Logger.Debug("Message is sent to communicator " + ComminicatorId + ": " + message.GetType().Name);
        }
Пример #5
0
        /// <summary>
        /// Sends a message to the TCP communicator according Communication type
        /// </summary>
        /// <param name="message">Message to send</param>
        protected override void SendMessageInternal(MDSMessage message)
        {
            if(State != CommunicationStates.Connected)
            {
                throw new MDSException("Communicator's state is not connected. It can not send message.");
            }

            SendMessageToSocket(message);
        }
Пример #6
0
 /// <summary>
 /// Sends a MDSMessage to a spesific communicator of this application.
 /// This method does not block calling thread to wait an ACK for sending message.
 /// If communicator is null, then it sends message first communicator of receiver communicators.
 /// </summary>
 /// <param name="message">outgoing message</param>
 /// <param name="communicator">Communicator to send message (may be null)</param>
 public void SendMessage(MDSMessage message, ICommunicator communicator)
 {
     _messageDeliverer.SendMessage(message, communicator);
 }
Пример #7
0
 /// <summary>
 /// Sends a MDSMessage to this application.
 /// This method does not block calling thread to wait an ACK for sending message.
 /// This method is just an overload for SendMessage(MDSMessage, ICommunicator) method as communicator is null.
 /// </summary>
 /// <param name="message">outgoing message</param>
 public void SendMessage(MDSMessage message)
 {
     SendMessage(message, null);
 }
Пример #8
0
        /// <summary>
        /// When a MDSMessage received from MDS server, this method is called to raise MessageReceived event.
        /// </summary>
        /// <param name="message">Incoming message from server</param>
        private void OnMessageReceived(MDSMessage message)
        {
            if (MessageReceived == null)
            {
                return;
            }

            try
            {
                MessageReceived(this,
                                new MessageReceivedEventArgs
                                {
                                    Message = message
                                });
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
            }
        }
Пример #9
0
        /// <summary>
        /// This method is used to add a message to outgoing messages queue.
        /// It is called by CommunicatorBase.
        /// </summary>
        /// <param name="message">Message to send</param>
        protected override void SendMessageInternal(MDSMessage message)
        {
            if (message.MessageTypeId != MDSMessageFactory.MessageTypeIdMDSDataTransferMessage)
            {
                return;
            }

            _outgoingMessageQueue.Add(message as MDSDataTransferMessage);
        }
Пример #10
0
 /// <summary>
 /// Derived class must override this method to send a message.
 /// </summary>
 protected abstract void SendMessageInternal(MDSMessage message);
Пример #11
0
 /// <summary>
 /// When a MDSMessage received, this method is called from derived class.
 /// </summary>
 /// <param name="message">incoming message from communicator</param>
 protected void OnMessageReceived(MDSMessage message)
 {
     if (MessageReceived != null)
     {
         MessageReceived(this, new MessageReceivedFromCommunicatorEventArgs {Communicator = this, Message = message});
     }
 }
Пример #12
0
 /// <summary>
 /// Sends a message to the communicator.
 /// </summary>
 /// <param name="message"></param>
 public void SendMessage(MDSMessage message)
 {
     lock (_sendLock)
     {
         SendMessageInternal(message);                
     }
 }
Пример #13
0
        /// <summary>
        /// Sends a mssage to MDS server and waits a response for timeoutMilliseconds milliseconds.
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <param name="waitingResponseType">What type of response is being waited</param>
        /// <param name="timeoutMilliseconds">Maximum waiting time for response</param>
        /// <returns>Received message from server</returns>
        private MDSMessage SendAndWaitForReply(MDSMessage message, int waitingResponseType, int timeoutMilliseconds)
        {
            //Create a WaitingMessage to wait and get response message and add it to waiting messages
            var waitingMessage = new WaitingMessage(waitingResponseType);
            lock (_waitingMessages)
            {
                _waitingMessages[message.MessageId] = waitingMessage;
            }

            try
            {
                //Create a WaitingMessage to wait and get response message and add it to waiting messages
                SendMessageInternal(message);

                //Send message to the server
                waitingMessage.WaitEvent.WaitOne(timeoutMilliseconds);

                //Check for errors
                switch (waitingMessage.State)
                {
                    case WaitingMessageStates.WaitingForResponse:
                        throw new MDSTimeoutException("Timeout occured. Can not received response to message.");
                    case WaitingMessageStates.ClientDisconnected:
                        throw new MDSException("Client disconnected from MDS server before response received to message.");
                    case WaitingMessageStates.MessageRejected:
                        throw new MDSException("Message is rejected by MDS server or destination application.");
                }

                if (waitingMessage.ResponseMessage == null)
                {
                    throw new MDSException("An unexpected error occured, message can not send.");
                }

                return waitingMessage.ResponseMessage;
            }
            finally
            {
                //Remove message from waiting messages
                lock (_waitingMessages)
                {
                    if (_waitingMessages.ContainsKey(message.MessageId))
                    {
                        _waitingMessages.Remove(message.MessageId);
                    }
                }
            }
        }
            /// <summary>
            /// Sends a MDSMessage to a spesific communicator of this application.
            /// This method does not block calling thread to wait an ACK for sending message.
            /// If communicator is null, then it sends message first communicator of receiver communicators.
            /// </summary>
            /// <param name="message">outgoing message</param>
            /// <param name="communicator">Communicator to send message (may be null)</param>
            public void SendMessage(MDSMessage message, ICommunicator communicator)
            {
                if(communicator == null)
                {
                    lock (_remoteApplication._communicators)
                    {
                        var receiverCommunicator = GetAnyReceiverCommunicator();
                        //If no receiver is connected to server, throw exception
                        if (receiverCommunicator == null)
                        {
                            throw new MDSNoCommunicatorException("There is no communicator for remote application '" + _remoteApplication.Name + "' to send message.");
                        }

                        communicator = receiverCommunicator.Communicator;
                    }
                }

                communicator.SendMessage(message);
                _remoteApplication.LastOutgoingMessageTime = DateTime.Now;
            }