/// <summary>
        /// Start asynchronous receiving.
        /// </summary>
        internal virtual void StartReceive()
        {
            SocketStateObject state = new SocketStateObject();

            state.workHandler = this;
            socket.BeginReceive(state, new AsyncCallback(ReadCallback));
        }
 /// <summary>
 /// Constructor for a socket client handler on SSL
 /// </summary>
 /// <param name="handler">The socket handler</param>
 /// <param name="stream">The ssl stream</param>
 /// <param name="useReceiveQueue">If true the message receiving is throw a queue, otherwise eash message is handle by a different thread</useReceiveQueue>
 public AbstractAsyncTcpSocketClientHandler(Socket handler, SslStream stream, bool useReceiveQueue)
     : base(handler, stream, useReceiveQueue)
 {
     // Change the TcpSocket for the non SSL
     if (stream == null)
     {
         this.socket                  = new TcpSocketAsync(handler);
         this.stateObject             = new SocketStateObject();
         this.stateObject.workHandler = this;
         this.readAsyncEventArgs      = new SocketAsyncEventArgs();
         this.readAsyncEventArgs.SetBuffer(this.stateObject.buffer, 0, this.stateObject.buffer.Length);
         this.readAsyncEventArgs.UserToken  = this;
         this.readAsyncEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(socketIOAsyncEvent_Completed);
     }
 }
 /// <summary>
 /// Constructor for a socket client handler on SSL
 /// </summary>
 /// <param name="handler">The socket handler</param>
 /// <param name="stream">The ssl stream</param>
 /// <param name="useReceiveQueue">If true the message receiving is throw a queue, otherwise eash message is handle by a different thread</useReceiveQueue>
 public AbstractAsyncTcpSocketClientHandler(Socket handler, SslStream stream, bool useReceiveQueue)
     : base(handler, stream, useReceiveQueue)
 {
     // Change the TcpSocket for the non SSL
     if (stream == null)
     {
         this.socket = new TcpSocketAsync(handler);
         this.stateObject = new SocketStateObject();
         this.stateObject.workHandler = this;
         this.readAsyncEventArgs = new SocketAsyncEventArgs();
         this.readAsyncEventArgs.SetBuffer(this.stateObject.buffer, 0, this.stateObject.buffer.Length);
         this.readAsyncEventArgs.UserToken = this;
         this.readAsyncEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(socketIOAsyncEvent_Completed);
     }
 }
 /// <summary>
 /// Start the asynchronous receiving data.
 /// </summary>
 /// <param name="state">The socket state object to receive data</param>
 /// <param name="callback">The callback for the asynchronous receiving</param>
 internal override void BeginReceive(SocketStateObject state, AsyncCallback callback)
 {
     throw new Exception("Method not valid for this class.");
 }
예제 #5
0
 /// <summary>
 /// Start the asynchronous receiving data.
 /// </summary>
 /// <param name="state">The socket state object to receive data</param>
 /// <param name="callback">The callback for the asynchronous receiving</param>
 internal virtual void BeginReceive(SocketStateObject state, AsyncCallback callback)
 {
     this.socket.BeginReceive(state.buffer, 0, SocketStateObject.BufferSize, 0, callback, state);
 }
 /// <summary>
 /// Start asynchronous receiving.
 /// </summary>
 internal virtual void StartReceive()
 {
     SocketStateObject state = new SocketStateObject();
     state.workHandler = this;
     socket.BeginReceive(state, new AsyncCallback(ReadCallback));
 }
        /// <summary>
        /// Callback for asynchronous receiving.
        /// </summary>
        /// <param name="ar">The socket state object for receiving data</param>
        protected static void ReadCallback(IAsyncResult ar)
        {
            SocketStateObject state = (SocketStateObject)ar.AsyncState;
            AbstractTcpSocketClientHandler handler = state.workHandler;

            try
            {
                // Read data from the client socket.
                int read = handler.EndReceive(ar);
                Trace.WriteLine(string.Format("Receive {0} bytes", read));

                // Data was read from the client socket.
                if (read > 0)
                {
                    // Fire event for incoming message
                    handler.OnReceivingMessage(handler);
                    while (true)
                    {
                        AbstractMessage message = AbstractMessage.TryReadMessage(handler.GetMessageInstance(), state, read);
                        // Fire event for received message
                        if (message != null)
                        {
                            ReceiveMessageStateObject rcvObj = new ReceiveMessageStateObject()
                            {
                                Handler = handler, Message = message
                            };
                            handler.OnReceiveMessage(rcvObj);
                        }
                        if (state.pendingBuffer == null)
                        {
                            break;
                        }
                        read = 0;
                    }
                    handler.socket.BeginReceive(state, new AsyncCallback(ReadCallback));
                }
                else
                {
                    handler.Close();
                }
            }
            catch (MessageException mex)
            {
                // Invalid message
                Trace.WriteLine(string.Format("Exception {0}. Connection will be closed", mex));
                state.message       = null;
                state.pendingBuffer = null;
                handler.Close();
            }
            catch (SocketException sex)
            {
                if (sex.SocketErrorCode == SocketError.ConnectionReset || sex.SocketErrorCode == SocketError.ConnectionAborted)
                {
                    Trace.WriteLine(string.Format("Socket error for disconnection {0} : {1} : {2}. Client will be disconnected", sex.ErrorCode, sex.SocketErrorCode, sex.Message));
                    state.message       = null;
                    state.pendingBuffer = null;
                    handler.Close();
                }
                else
                {
                    Trace.WriteLine(string.Format("Exception {0}. Connection will be closed", sex));
                    state.message       = null;
                    state.pendingBuffer = null;
                    handler.Close();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("Exception {0}. Connection will be closed", ex));
                state.message       = null;
                state.pendingBuffer = null;
                handler.Close();
            }
        }
예제 #8
0
 /// <summary>
 /// Start the asynchronous receiving data.
 /// </summary>
 /// <param name="state">The socket state object to receive data</param>
 /// <param name="callback">The callback for the asynchronous receiving</param>
 internal override void BeginReceive(SocketStateObject state, AsyncCallback callback)
 {
     this.sslStream.BeginRead(state.buffer, 0, SocketStateObject.BufferSize, callback, state);
 }
예제 #9
0
 /// <summary>
 /// Start the asynchronous receiving data. 
 /// </summary>
 /// <param name="state">The socket state object to receive data</param>
 /// <param name="callback">The callback for the asynchronous receiving</param>
 internal override void BeginReceive(SocketStateObject state, AsyncCallback callback)
 {
     this.sslStream.BeginRead(state.buffer, 0, SocketStateObject.BufferSize, callback, state);
 }
예제 #10
0
 /// <summary>
 /// Start the asynchronous receiving data. 
 /// </summary>
 /// <param name="state">The socket state object to receive data</param>
 /// <param name="callback">The callback for the asynchronous receiving</param>
 internal override void BeginReceive(SocketStateObject state, AsyncCallback callback)
 {
     throw new Exception("Method not valid for this class.");
 }
예제 #11
0
 /// <summary>
 /// Start the asynchronous receiving data. 
 /// </summary>
 /// <param name="state">The socket state object to receive data</param>
 /// <param name="callback">The callback for the asynchronous receiving</param>
 internal virtual void BeginReceive(SocketStateObject state, AsyncCallback callback)
 {
     this.socket.BeginReceive(state.buffer, 0, SocketStateObject.BufferSize, 0, callback, state);
 }
예제 #12
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;
        }