Beispiel #1
0
        private void ReadClient(IAsyncResult ar)
        {
            var state        = (ClientState)ar.AsyncState;
            var clientSocket = state.socket;

            var bytesRead = clientSocket.EndReceive(ar);

            if (bytesRead > 0)
            {
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); // decode bytes to string
                var content = state.sb.ToString();
                if (content.IndexOf("<EOF>") > -1)
                {
                    content = content.Substring(0, content.Length - 5);
                    state.sb.Clear();
                    state.sb.Append(content);

                    Console.WriteLine("Client #" + state.id + " writes...");
                    if (OnResponse != null)
                    {
                        OnResponse.Invoke(this, state.sb);
                        Console.WriteLine("Server response to Client #" + state.id);
                        Send(state, state.sb.ToString());
                    }

                    //if (content.IndexOf("close") > -1)
                    //{
                    //    clientSocket.BeginDisconnect(false, new AsyncCallback(DisconnectClient), state);
                    //}
                }
            }
            else
            {
                clientSocket.BeginReceive(state.buffer, 0, ClientState.BUFFERSIZE, SocketFlags.None, new AsyncCallback(ReadClient), state);
            }
        }
Beispiel #2
0
        public async Task ListenPlayer(Player player, LogicFacade logic)
        {
            try
            {
                progress.Invoke(this, new Progress(player.User));
                var stream = player.User.Client.GetStream();
                while (true)
                {
                    await Task.Delay(5);

                    if (stream.DataAvailable && player.User.Client.Available >= 12)
                    {
                        int    available = player.User.Client.Available;
                        byte[] buffer    = new byte[available];
                        stream.Read(buffer, 0, available);
                        if (buffer.Length > 8)
                        {
                            int     index       = 1;
                            int     commandType = BitConverter.ToInt32(buffer, 0);
                            Command cmd         = null;

                            switch (commandType)
                            {
                            case 1:
                            {
                                cmd = new ArenaCommand {
                                    Author = player, TimeStamp = DateTime.UtcNow
                                };
                                progress.Invoke(this, new Progress(player.User, "Got message: ArenaCommand - " + commandType));
                                break;
                            }

                            case 0:
                            {
                                cmd = new GeneralCommand {
                                    Author = player, TimeStamp = DateTime.UtcNow
                                };
                                progress.Invoke(this, new Progress(player.User, "Got message: GeneralCommand - " + commandType));
                                break;
                            }

                            default:
                            {
                                break;
                            }
                            }

                            while (buffer.Length >= (index + 1) * 4 + 4)
                            {
                                cmd.AddSubCommand(BitConverter.ToInt32(buffer, (index + 1) * 4));
                                index++;
                            }
                            logic.AddCommand(cmd);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #3
0
 public PlayerService()
 {
     init.Invoke(this, "Player service has started");
 }