Exemple #1
0
        /// <summary>
        /// Method that is called when a message needs to be sent threw the socket to the SCM
        /// </summary>
        /// <param name="message">The message object</param>
        public bool Send(string message)
        {
            try
            {
                //Add the C3 message termination
                message = message + C3MessageTerminations.DLE + C3MessageTerminations.ETX;

                // Convert the string data to byte data using ASCII encoding.
                byte[] byteData = Encoding.ASCII.GetBytes(message);

                //Create the socket packet and assign a socket to it and ad the message type so when the callback is called we know what message was send
                SentSocketPacket sentSocketPacket = new SentSocketPacket();
                sentSocketPacket.Message    = message;
                sentSocketPacket.workSocket = connectedSocket;

                // Begin sending the data to the remote device.
                connectedSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallback), sentSocketPacket);

                Log.Info(C3NET_COMMUNICATOR_LOG, string.Format("OUT : {0}", message));

                return(true);
            }
            catch (Exception ex)
            {
                Log.Error(C3NET_COMMUNICATOR_LOG, string.Format("Send : {0}", ex.ToString()));
                return(false);
            }
        }
Exemple #2
0
        private void SendCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.
                SentSocketPacket sentSocketPacket = (SentSocketPacket)ar.AsyncState;

                // Complete sending the data to the remote device.
                int bytesSent = sentSocketPacket.workSocket.EndSend(ar);

                // Signal that all bytes have been sent.
                RaiseSendedMessageEventHandler(true, sentSocketPacket.MessageType, sentSocketPacket.Message);
            }
            catch (ObjectDisposedException)
            {
                //This is good error (when the socket closes an error that states that the deposed object can't be accesed is triggered)
                //Never foud a clean close solution
            }
            catch (Exception ex)
            {
                Log.Error(C3NET_COMMUNICATOR_LOG, string.Format("SendCallback : {0}", ex.ToString()));
            }
        }