Beispiel #1
0
        /// <summary>
        /// Sends a message to the remote application.
        /// </summary>
        /// <param name="message">Message to be sent</param>
        protected override void SendMessageInternal(IScsMessage message)
        {
            //Send message
            var totalSent = 0;

            lock (_syncLock)
            {
                //Create a byte array from message according to current protocol
                var messageBytes = WireProtocol.GetBytes(message);
                //Send all bytes to the remote application
                while (totalSent < messageBytes.Length)
                {
                    var sent = _clientSocket.Send(messageBytes, totalSent, messageBytes.Length - totalSent, SocketFlags.None);
                    if (sent <= 0)
                    {
                        throw new CommunicationException("Message could not be sent via TCP socket. Only " + totalSent + " bytes of " + messageBytes.Length + " bytes are sent.");
                    }

                    totalSent += sent;
                }

                LastSentMessageTime = DateTime.Now;
                OnMessageSent(message);
            }
        }
        /// <summary>
        /// Sends a message to the remote application.
        /// </summary>
        /// <param name="message">Message to be sent</param>
        protected override void SendMessageInternal(IScsMessage message)
        {
            //Send message
            var totalSent = 0;

            lock (_syncLock)
            {
                //Create a byte array from message according to current protocol
                var messageBytes = WireProtocol.GetBytes(message);
                //Send all bytes to the remote application

                try
                {
                    _sslStream.Write(messageBytes, totalSent, messageBytes.Length);
                }
                catch
                {
                    throw new CommunicationException("Cannot send data on the SSL stream");
                }


                LastSentMessageTime = DateTime.Now;
                OnMessageSent(message);
            }
        }
 /// <summary>
 /// Sends a message to the remote application.
 /// </summary>
 /// <param name="message">Message to be sent</param>
 /// <param name="priority">Priority of message to send</param>
 protected override void SendMessagepublic(IScsMessage message, byte priority)
 {
     if (priority > 5)
     {
         _highPriorityBuffer.Enqueue(WireProtocol.GetBytes(message));
     }
     else
     {
         _lowPriorityBuffer.Enqueue(WireProtocol.GetBytes(message));
     }
 }
 /// <summary>
 /// Sends a message to the remote application.
 /// </summary>
 /// <param name="message">Message to be sent</param>
 protected override void SendMessagepublic(IScsMessage message)
 {
     try
     {
         if (_clientSocket.Connected)
         {
             // Create a byte array from message according to current protocol
             var messageBytes = WireProtocol.GetBytes(message);
             // Begin sending the data to the remote device
             _clientSocket.BeginSend(messageBytes, 0, messageBytes.Length, SocketFlags.None,
                                     new AsyncCallback(SendCallback), _clientSocket);
         }
     }
     catch (Exception e)
     {
         // disconnect
     }
 }
Beispiel #5
0
        /// <summary>
        /// Sends a message to the remote application.
        /// </summary>
        /// <param name="message">Message to be sent</param>
        protected override void SendMessageInternal(IScsMessage message)
        {
            //Send message
            var totalSent = 0;

            lock (_syncLock)
            {
                //Create a byte array from message according to current protocol
                var messageBytes = WireProtocol.GetBytes(message);
                //Send all bytes to the remote application
                while (totalSent < messageBytes.Length)
                {
                    try
                    {
                        var sent = _clientSocket.Send(messageBytes, totalSent, messageBytes.Length - totalSent, SocketFlags.None);
                        if (sent <= 0)
                        {
                            throw new CommunicationException("Message could not be sent via TCP socket. Only " + totalSent + " bytes of " + messageBytes.Length + " bytes are sent.");
                        }

                        totalSent += sent;
                    }
                    catch (SocketException ex)
                    {
                        if (ex.SocketErrorCode == SocketError.WouldBlock ||
                            ex.SocketErrorCode == SocketError.IOPending ||
                            ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
                        {
                            // socket buffer is probably full, wait and try again
                            Thread.Sleep(30);
                        }
                    }
                }

                LastSentMessageTime = DateTime.Now;

                OnMessageSent(message);
            }
        }
        /// <summary>
        /// Sends a message to the remote application.
        /// </summary>
        /// <param name="message">Message to be sent</param>
        protected override void SendMessagepublic(IScsMessage message)
        {
            //Send message
            var totalSent = 0;

            lock (_syncLock)
            {
                //Create a byte array from message according to current protocol
                var messageBytes = WireProtocol.GetBytes(message);
                //Send all bytes to the remote application
                while (totalSent < messageBytes.Length)
                {
                    if (_clientSocket.Connected)
                    {
                        var sent = 0;
                        try
                        {
                            sent = _clientSocket.Send(messageBytes, totalSent, messageBytes.Length - totalSent, SocketFlags.None);
                        }
                        catch (Exception e)
                        {
                            //annoying bug
                            Logger.Log.Error("A packet would have been sent to a disconnected client. IGNORE THIS.", e);
                        }

                        if (sent <= 0)
                        {
                            throw new CommunicationException("Message could not be sent via TCP socket. Only " + totalSent + " bytes of " + messageBytes.Length + " bytes are sent.");
                        }

                        totalSent += sent;
                    }
                }

                LastSentMessageTime = DateTime.Now;
                OnMessageSent(message);
            }
        }