Exemple #1
0
        public void ListenLoop()
        {
            if (true == mServerSocket.Connected)
            {
                try
                {
                    if (null == mReadCallback)
                    {
                        mReadCallback = new AsyncCallback(ReadCallback);
                    }

                    SocketPacket packet = new SocketPacket(mServerSocket);

                    mServerSocket.BeginReceive(packet.buffer, 0, SocketPacket.BufferSize, SocketFlags.None, mReadCallback, packet);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(" >> " + ex.ToString());
                }
            }
            else
            {
                EndServerConnection();
            }
        }
Exemple #2
0
        public void ReadCallback(IAsyncResult ar)
        {
            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            SocketPacket packet  = (SocketPacket)ar.AsyncState;
            Socket       handler = packet.workSocket;

            // Read data from the client socket.
            try
            {
                if (true == handler.Connected)
                {
                    SocketError errorCode;
                    int         bytesRead = handler.EndReceive(ar, out errorCode);
                    if (SocketError.Success != errorCode)
                    {
                        bytesRead = 0;
                    }

                    if (bytesRead > 0)
                    {
                        // All the data has been read from the
                        // client. Display it on the console.
                        Console.WriteLine("Read {0} bytes from socket.",
                                          bytesRead);

                        // Check for end-of-file tag. If it is not there, read
                        // more data.
                        int msgEnd = MessageHelper.FindEOM(packet.buffer);

                        if (msgEnd > -1)
                        {
                            // At least one full message read
                            while (msgEnd > -1)
                            {
                                byte[] dataMsg;
                                byte[] newMsg;

                                // Sorts out the message data into at least one full message, saves any spare bytes for next message
                                MessageHelper.ClearMessageFromStream(msgEnd, packet.buffer, out dataMsg, out newMsg);

                                // Do something with client message
                                MessageHandler.HandleMessage(dataMsg);

                                packet.buffer = newMsg;

                                msgEnd = MessageHelper.FindEOM(packet.buffer);
                            }
                        }
                        else
                        {
                            // Not all data received. Get more.
                            handler.BeginReceive(packet.buffer, bytesRead, SocketPacket.BufferSize, 0,
                                                 mReadCallback, packet);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            ListenLoop();
        }