Exemplo n.º 1
0
        /// <summary>
        /// Try to read a message from the buffer.
        /// </summary>
        /// <param name="message">The destination message</param>
        /// <param name="state">The state object</param>
        /// <param name="byteRead">The umber of bytes in the input buffer</param>
        /// <returns>The message read, otherwise false.</returns>
        internal static AbstractMessage TryReadMessage(AbstractMessage message, SocketStateObject state, int byteRead)
        {
            AbstractMessage messageRead = null;
            int             moreMessage = 0;

            byte[] buffer = state.buffer;    // Get buffer

            if (state.pendingBuffer != null) //Check for pending data and merge it
            {
                buffer = new byte[byteRead + state.pendingBuffer.Length];
                Array.Copy(state.pendingBuffer, 0, buffer, 0, state.pendingBuffer.Length);
                Array.Copy(state.buffer, 0, buffer, state.pendingBuffer.Length, byteRead);
                byteRead = buffer.Length;
            }
            state.pendingBuffer = null;
            if (state.message == null)
            {
                state.message = message;
                moreMessage   = state.message.ReadFirstMessage(buffer, byteRead);
                Trace.WriteLine(string.Format("Receive 1st package MessageUID {0} ClientUID {1}", state.message.MessageUID, state.message.ClientUID));
            }
            else
            {
                moreMessage = state.message.AppendBuffer(buffer, byteRead);
                Trace.WriteLine(string.Format("Receive more package MessageUID {0} ClientUID {1}", state.message.MessageUID, state.message.ClientUID));
            }
            if (state.message.IsComplete())
            {
                Trace.WriteLine(string.Format("Receive complete message {0} len {1}", state.message.MessageUID, state.message.MessageLength));
                messageRead = state.message;
                Trace.WriteLine(string.Format("Prepare to receive a new message. moreMessage = {0}", moreMessage));
                state.message = null;
                if (moreMessage > 0)
                {
                    state.pendingBuffer = new byte[byteRead - moreMessage];
                    Array.Copy(buffer, moreMessage, state.pendingBuffer, 0, state.pendingBuffer.Length);
                    Trace.WriteLine(string.Format("Copy {0} bytes to pending buffer", state.pendingBuffer.Length));
                }
            }
            return(messageRead);
        }
 public virtual void SendAsync(AbstractMessage message)
 {
     this.sendDone.WaitOne();
     byte[] buffer = message.GetEnvelope();
     Trace.WriteLine(string.Format("Sending asynch message {0} len {1}", message.MessageUID, message.MessageLength));
     this.socket.BeginSend(buffer, this, SendCallback);
 }
 /// <summary>
 /// Raise the receive message event.
 /// </summary>
 /// <param name="handler">The socket client handler for the received message</param>
 /// <param name="abstractMessage">The message received</param>
 protected virtual void OnReceiveMessage(AbstractTcpSocketClientHandler handler, AbstractMessage abstractMessage)
 {
     if (receiveMessageEvent != null)
     {
         receiveMessageEvent(handler, abstractMessage);
     }
 }
 /// <summary>
 /// Send a buffer. It's a synchronous operation. The previous send has to be completed.
 /// </summary>
 /// <param name="message">The message to send</param>
 /// <returns>True if the message has been sent, otherwise false</returns>
 public virtual bool Send(AbstractMessage message)
 {
     return Send(message, 0);
 }
 /// <summary>
 /// Send a buffer. It's a synchronous operation. The previous send has to be completed.
 /// You can define a timeout on the previous send and skip this one in case of time out.
 /// </summary>
 /// <param name="message">The message to send</param>
 /// <param name="previousSendTimeout">Timeout on the previous send in ms</param>
 /// <returns>True if the message has been sent, otherwise false</returns>
 internal bool SendMessage(AbstractMessage message, int previousSendTimeout)
 {
     byte[] buffer = message.GetEnvelope();
     Trace.WriteLine(string.Format("Sending message {0} len {1}", message.MessageUID, message.MessageLength));
     bool r = SendBuffer(this, buffer, previousSendTimeout);
     return r;
 }
 /// <summary>
 /// Send a buffer. It's a synchronous operation. The previous send has to be completed.
 /// You can define a timeout on the previous send and skip this one in case of time out.
 /// </summary>
 /// <param name="message">The message to send</param>
 /// <param name="previousSendTimeout">Timeout on the previous send in ms</param>
 /// <returns>True if the message has been sent, otherwise false</returns>
 public virtual bool Send(AbstractMessage message, int previousSendTimeout)
 {
     return SendMessage(message, previousSendTimeout);
 }
 /// <summary>
 /// Send asynchronous message
 /// </summary>
 /// <param name="message">The message to send</param>
 public void SendAsync(AbstractMessage message)
 {
     if (handler == null)
     {
         return;
     }
     handler.SendAsync(message);
 }
 /// <summary>
 /// Send a message
 /// </summary>
 /// <param name="message">The message to send</param>
 /// <returns>True if the message has been sent, otherwise false</returns>
 public bool Send(AbstractMessage message)
 {
     if (handler == null)
     {
         return false;
     }
     return handler.Send(message);
 }
 /// <summary>
 /// Raise a received message event.
 /// </summary>
 /// <param name="handler">The socket client handler of the close connection</param>
 /// <param name="message">The message received</param>
 void handler_ReceiveMessageEvent(AbstractTcpSocketClientHandler handler, AbstractMessage message)
 {
     OnReceiveMessage(handler, message);
 }
Exemplo n.º 10
0
        /// <summary>
        /// Try to read a message from the buffer.
        /// </summary>
        /// <param name="message">The destination message</param>
        /// <param name="state">The state object</param>
        /// <param name="byteRead">The umber of bytes in the input buffer</param>
        /// <returns>The message read, otherwise false.</returns>
        internal static AbstractMessage TryReadMessage(AbstractMessage message, SocketStateObject state, int byteRead)
        {
            AbstractMessage messageRead = null;
            int moreMessage = 0;
            byte[] buffer = state.buffer;  // Get buffer

            if (state.pendingBuffer != null) //Check for pending data and merge it
            {
                buffer = new byte[byteRead + state.pendingBuffer.Length];
                Array.Copy(state.pendingBuffer, 0, buffer, 0, state.pendingBuffer.Length);
                Array.Copy(state.buffer, 0, buffer, state.pendingBuffer.Length, byteRead);
                byteRead = buffer.Length;
            }
            state.pendingBuffer = null;
            if (state.message == null)
            {
                state.message = message;
                moreMessage = state.message.ReadFirstMessage(buffer, byteRead);
                Trace.WriteLine(string.Format("Receive 1st package MessageUID {0} ClientUID {1}", state.message.MessageUID, state.message.ClientUID));
            }
            else
            {
                moreMessage = state.message.AppendBuffer(buffer, byteRead);
                Trace.WriteLine(string.Format("Receive more package MessageUID {0} ClientUID {1}", state.message.MessageUID, state.message.ClientUID));
            }
            if (state.message.IsComplete())
            {
                Trace.WriteLine(string.Format("Receive complete message {0} len {1}", state.message.MessageUID, state.message.MessageLength));
                messageRead = state.message;
                Trace.WriteLine(string.Format("Prepare to receive a new message. moreMessage = {0}", moreMessage));
                state.message = null;
                if (moreMessage > 0)
                {
                    state.pendingBuffer = new byte[byteRead - moreMessage];
                    Array.Copy(buffer, moreMessage, state.pendingBuffer, 0, state.pendingBuffer.Length);
                    Trace.WriteLine(string.Format("Copy {0} bytes to pending buffer", state.pendingBuffer.Length));
                }
            }
            return messageRead;
        }