Exemplo n.º 1
0
        public void ReadCallback(IAsyncResult ar)
        {
            String      content = String.Empty;
            StateObject state   = (StateObject)ar.AsyncState;
            Socket      handler = state.workSocket;

            if (!reserved_connections.ContainsKey(handler))
            {
                // close connection?
                return;
            }
            int bytesRead = 0;

            try
            {
                bytesRead = handler.EndReceive(ar);
            }
            catch (SocketException exception)
            {
                if (reserved_connections[handler] != null)
                {
                    GameConnectionEventArgs pa = new GameConnectionEventArgs(reserved_connections[handler]);
                    reserved_connections.Remove(handler);
                    PlayerDropped(this, pa);
                }
                return;
            }

            if (bytesRead > 0)
            {
                MemoryStream current_stream = reserved_connections[handler].CurrentStream;
                current_stream.Write(state.buffer, 0, bytesRead);
                //Console.WriteLine("Read {0} bytes from socket.", bytesRead);
                object command = Slicer.Slice(current_stream, state);
                while (command != null)
                {
                    HandleCommand(handler, command);
                    command = Slicer.Slice(current_stream, state);
                }
                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                     new AsyncCallback(ReadCallback), state);
            }
        }
Exemplo n.º 2
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the state object and the client socket
                // from the asynchronous state object.
                StateObject state  = (StateObject)ar.AsyncState;
                Socket      client = state.workSocket;

                // Read data from the remote device.
                int bytesRead = client.EndReceive(ar);
                buffer.Write(state.buffer, 0, bytesRead);
                if (bytesRead > 0)
                {
                    Console.WriteLine("Read {0} bytes from socket.", bytesRead);
                    object command = Slicer.Slice(buffer, state);
                    while (command != null)
                    {
                        HandleCommand(command);
                        command = Slicer.Slice(buffer, state);
                    }
                    // Get the rest of the data.
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                        new AsyncCallback(ReceiveCallback), state);
                }
                else
                {
                    //receiveDone.Set();
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                        new AsyncCallback(ReceiveCallback), state);
                }
            }
            catch (Exception e)
            {
                client_interface.Output(e.Message);
                Console.WriteLine(e.ToString());
            }
        }