Exemplo n.º 1
0
        private void WaitForData(Socket socket, int clientNumber)
        {
            try{
                if (WorkerCallBack == null)
                {
                    WorkerCallBack = new AsyncCallback(OnDataReceived);
                }

                SocketPacket _SocketPacket =
                    new SocketPacket(socket, clientNumber);

                socket.BeginReceive(_SocketPacket.dataBuffer, 0,
                                    _SocketPacket.dataBuffer.Length,
                                    SocketFlags.None,
                                    WorkerCallBack,
                                    _SocketPacket
                                    );
            }
            catch (SocketException se) {
                throw se;
            }
        }
Exemplo n.º 2
0
        private void OnDataReceived(IAsyncResult asyn)
        {
            SocketPacket socketData = (SocketPacket)asyn.AsyncState;

            try
            {
                lock (this)
                {
                    int ResultBytesLength = socketData.CurrentSocket.EndReceive(asyn);

                    logger.Debug("ResultBytesLength=" + ResultBytesLength);
                    if (ResultBytesLength == 3)
                    {
                        return;
                    }

                    if (ResultBytesLength <= 0)
                    {
                        this.CloseSocket(socketData.CurrentSocket);
                        return;
                    }

                    /*
                     *
                     * char[] charsLength =
                     * new char[ResultBytesLength];
                     *
                     * System.Text.Decoder _Decoder = System.Text.Encoding.UTF8.GetDecoder();
                     *
                     * _Decoder.GetChars(
                     * socketData.dataBuffer,
                     * 0,
                     * ResultBytesLength,
                     * charsLength,
                     * 0);
                     *
                     * string recByteLength = new string(charsLength);
                     * int BufferSize = int.Parse(recByteLength);
                     *
                     * byte[] dataBuffer = new byte[BufferSize];
                     *
                     * int BytesReceive = socketData.CurrentSocket.Receive(dataBuffer,
                     *                                           dataBuffer.Length,
                     *                                           SocketFlags.None
                     *                                           );
                     *
                     * char[] charLenght = new char[BytesReceive];
                     *
                     * string Message = string.Empty;
                     *
                     * System.Text.Decoder decoder = System.Text.Encoding.UTF8.GetDecoder();
                     *
                     * decoder.GetChars(
                     * dataBuffer, 0, BytesReceive, charLenght, 0);
                     *
                     * Message += new string(charLenght);
                     *
                     * if (BytesReceive < BufferSize)
                     * {
                     * Message +=
                     *     ReceiveData(
                     *     BytesReceive,
                     *     BufferSize,
                     *     socketData.CurrentSocket
                     *     );
                     * } */

                    int BufferSize = 0;
                    int pos        = 0;

                    BufferSize += (((int)socketData.dataBuffer[pos++]) & 0xFF) << 24;
                    BufferSize += (((int)socketData.dataBuffer[pos++]) & 0xFF) << 16;
                    BufferSize += (((int)socketData.dataBuffer[pos++]) & 0xFF) << 8;
                    BufferSize += (((int)socketData.dataBuffer[pos++]) & 0xFF);

                    logger.Debug("BufferSize = " + BufferSize);

                    byte[] dataBuffer = new byte[BufferSize];

                    int BytesReceive = socketData.CurrentSocket.Receive(dataBuffer,
                                                                        dataBuffer.Length,
                                                                        SocketFlags.None
                                                                        );

                    char[] charLenght = new char[BytesReceive];

                    string Message = string.Empty;

                    System.Text.Decoder decoder = System.Text.Encoding.UTF8.GetDecoder();

                    decoder.GetChars(
                        dataBuffer, 0, BytesReceive, charLenght, 0);

                    Message += new string(charLenght);

                    if (BytesReceive < BufferSize)
                    {
                        Message +=
                            ReceiveData(
                                BytesReceive,
                                BufferSize,
                                socketData.CurrentSocket
                                );
                    }

                    logger.Debug("Message [" + Message + "]");

                    List <object> lstObject = new List <object>();

                    lstObject.Add(Message);
                    lstObject.Add(socketData.CurrentSocket);

                    ThreadPool.QueueUserWorkItem(
                        new WaitCallback(MessageBroker), lstObject
                        );


                    this.WaitForData(
                        socketData.CurrentSocket,
                        socketData.ClientNumber
                        );
                }
            }
            catch (SocketException se) {
                if (se.ErrorCode == (int)(SocketError.ConnectionReset))
                {
                    this.CloseSocket(socketData.CurrentSocket);
                }
            }
            catch (Exception ex) {
                logger.Error("OnDataReceived(): " + ex.Message, ex);
                this.CloseSocket(socketData.CurrentSocket);
            }
        }
Exemplo n.º 3
0
        private void OnDataReceived(IAsyncResult asyn)
        {
            SocketPacket socketData = (SocketPacket)asyn.AsyncState;

            try
            {
                lock (this)
                {
                    int ResultBytesLength = socketData.CurrentSocket.EndReceive(asyn);

                    if (ResultBytesLength <= 0)
                    {
                        this.CloseSocket(socketData.CurrentSocket);
                        return;
                    }

                    char[] charsLength =
                        new char[ResultBytesLength];

                    System.Text.Decoder _Decoder = System.Text.Encoding.UTF8.GetDecoder();

                    _Decoder.GetChars(
                        socketData.dataBuffer,
                        0,
                        ResultBytesLength,
                        charsLength,
                        0);

                    string recByteLength = new string(charsLength);
                    int    BufferSize    = int.Parse(recByteLength);

                    byte[] dataBuffer = new byte[BufferSize];

                    int BytesReceive = socketData.CurrentSocket.Receive(dataBuffer,
                                                                        dataBuffer.Length,
                                                                        SocketFlags.None
                                                                        );

                    char[] charLenght = new char[BytesReceive];

                    string Message = string.Empty;

                    System.Text.Decoder decoder = System.Text.Encoding.UTF8.GetDecoder();

                    decoder.GetChars(
                        dataBuffer, 0, BytesReceive, charLenght, 0);

                    Message += new string(charLenght);

                    if (BytesReceive < BufferSize)
                    {
                        Message +=
                            ReceiveData(
                                BytesReceive,
                                BufferSize,
                                socketData.CurrentSocket
                                );
                    }

                    List <object> lstObject = new List <object>();

                    lstObject.Add(Message);
                    lstObject.Add(socketData.CurrentSocket);

                    ThreadPool.QueueUserWorkItem(
                        new WaitCallback(MessageBroker), lstObject
                        );


                    this.WaitForData(
                        socketData.CurrentSocket,
                        socketData.ClientNumber
                        );
                }
            }
            catch (SocketException se) {
                if (se.ErrorCode == (int)(SocketError.ConnectionReset))
                {
                    this.CloseSocket(socketData.CurrentSocket);
                }
            }
            catch (Exception ex) {
                throw ex;
            }
        }