public void HandleRequest(TcpClient client, string values) {
            var data = values.GetFirst();

            switch (data.Item1) {
                case "PlayerList":
                    PlayerList(client);
                    break;
            }
        }
    public CommunicationHandler(TcpClient serverCon, IErrorHandler error, IInvokable invoke, INotifiable notify, ILobby lobby) {
        _errorHandler = error;
        _tcpClient = serverCon;
        _tcpClient.DataReceived += TcpClient_DataReceived;
        _tcpClient.Disconnected += TcpClient_Disconnnected; 
        _tcpClient.Start();

        _processor = new DataProcessor(invoke, notify, lobby);
    }
        private void SocketAccepted(SocketAcceptedEventArgs e) {
            IPEndPoint sender = (IPEndPoint)e.Socket.RemoteEndPoint;
            Console.WriteLine("Client connected at address : {0}:{1}", sender.Address, sender.Port);
            TcpClient tcpClient = new TcpClient(e.Socket);

            tcpClient.DataReceived += TcpClient_DataReceived;
            tcpClient.Disconnected += Client_Disconnected;
            tcpClient.Start();

        }
        private void Client_Disconnected(TcpClient sender) {
            // TODO: Server message : Player disconnected

            IPEndPoint senderPoint = (IPEndPoint) sender.Socket.RemoteEndPoint;
            Console.WriteLine("TcpClient at address \"{0}:{1}\" has disconnected.", senderPoint.Address,
                senderPoint.Port);
            Player player = _playerContainer.GetPlayer(sender);
            if (player == null)
                return;
            _playerContainer.RemovePlayer(player);
            _playerContainer.AddCorner(player.CornerId);
            player.Dispose();

        }
Ejemplo n.º 5
0
        public void HandleNotify(TcpClient client, string values) {
            var data = values.GetFirst();

            switch (data.Item1) {
                case "SetName":
                    SetName(client, data.Item2);
                    break;
                case "PlayerReady":
                    PlayerReady(data.Item2);
                    break;
                case "EndTurn":
                    _notify.EndTurn(data.Item2);
                    break;
                case "MoveToEmpty":
                case "MoveToMerge":
                case "MoveToAttack":
                    MoveUnit(data.Item1, data.Item2);
                    break;
                case "Attack":
                    Attack(data.Item2);
                    break;
                case "UnitCreated":
                    CreateUnit(data.Item2);
                    break;
                case "SplitUnit":
                    SplitUnit(data.Item2);
                    break;
                case "CashChanged":
                    CashChanged(data.Item2);
                    break;
                case "GameStart":
                    GameStart(data.Item2);
                    break;
                case "GameWon":
                    GameWon(data.Item2);
                    break;
                case "GameLost":
                    GameLost(data.Item2);
                    break;
                case "GameLoaded":
                    GameLoaded(data.Item2);
                    break;
            }
        }
        internal void ProcessMessage(TcpClient sender, string message) {
            string[] separated =
            message.Split(new[] { "]" }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.TrimStart('[')).ToArray();

            foreach (string command in separated) {
                var data = command.GetFirst();

                switch (data.Item1) {
                    case "Notify":
                        _notify.HandleNotify(sender, data.Item2);
                        break;
                    case "Request":
                        _request.HandleRequest(sender, data.Item2);
                        break;
                    default:
                        Console.WriteLine($"UDP Client received a message that could not be handled : {message}");
                        return;

                }
            }
        }
 internal TcpDataReceivedEventArgs(TcpClient sender, byte[] receivedData, Encoding encoding) {
     ReceivedData = receivedData;
     Sender = sender;
     Encoding = encoding;
 }
 private void PlayerList(TcpClient sender) {
     string players = _request.PlayerList();
     sender.Send($"[Lobby:SetPlayers:{players}]");
 }
Ejemplo n.º 9
0
 private void SetName(TcpClient sender, string values) {
     _notify.SetName(sender, values);
 }
Ejemplo n.º 10
0
    public void SetNameHandshake(string response) {
        LoginStatus login = GetLoginStatus();
        SessionData session = GetSession();

        IPEndPoint lobbyIp = GetLobbyIp(response);
        if (lobbyIp == null) {
            EndSession();
            return;
        }
        // TODO: Method v
        Socket tcp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        tcp.Connect(lobbyIp);
        TcpClient tcpClient = new TcpClient(tcp);


        string auth = GetResponse(tcpClient.Socket, string.Format("[Notify:SetName:{0}]", login.Name));
        if (auth == null) {
            EndSession();
            return;
        }

        string[] messages =
            auth.Split(new [] { "]" }, StringSplitOptions.RemoveEmptyEntries)
                .Select(x => x.TrimStart('['))
                .ToArray();

        SplitData first = messages[0].GetFirst();
        if (first.CommandType == "Error") {
            ShowError(first.Values.Split(':').Last());
            return;
        }

        string[] authData = messages[0].GetFirst().Values.GetFirst().Values.Split(new [] {"|",}, StringSplitOptions.RemoveEmptyEntries);
        session.Guid = authData[0];
        session.OwnId = Int32.Parse(authData[1]);
        session.OwnName = authData[2];
        session.LobbyId = authData[3];
        session.LobbyConnection = tcpClient;

        string[] playerData = messages[1].GetFirst().Values.GetFirst().Values.Split(new[] { "|", }, StringSplitOptions.RemoveEmptyEntries).ToArray();
        TempPlayer[] players = new TempPlayer[playerData.Length];
        for (int i = 0; i < players.Length; i++) {
            string info = playerData[i].Trim('(', ')');
            string[] splt = info.Split(':');
            players[i] = new TempPlayer() {
                Name = splt[0],
                Id = int.Parse(splt[1]),
                Ready = bool.Parse(splt[2]),
                IsHost = bool.Parse(splt[3])
            };
        }

        session.Players = players;
        SceneManager.LoadScene("Lobby");
    }
 public Player GetPlayer(TcpClient client) {
     lock (_players)
         return _players.FirstOrDefault(player => player.TcpClient == client);
 }
 public void SetName(TcpClient sender, string name) {
     int corner= GetRandomCorner();
     if (corner < 0) {
         sender.Send("[Error:ERR4:The lobby you tried to join is full.]");
         sender.Dispose();
         return;
     }
     if (_gameStarted) {
         sender.Send("[Error:ERR5:The game you tried to join is already in progress.]");
         sender.Dispose();
         return;
     }
     Player player = new Player(Guid.NewGuid(), sender) {
         Name = name,
         CornerId = corner
     };
     AddPlayer(player);
     player.TcpClient.Send($"[Response:Authenticated:{player.Guid}|{player.CornerId}|{player.Name}|{_lobbyId}]" +
                           $"[Response:SetPlayers:{PlayerList()}]");
 }
 private void TcpClient_Disconnnected(TcpClient sender) {
     _errorHandler.ServerDisconnected();
 }
Ejemplo n.º 14
0
        static void SendPersonList(object ob)
        {
            IPAddress     Addr   = IPAddress.Parse((string)((object[])ob)[0]);
            List <Person> ls     = (List <Person>)((object[])ob)[1];
            TcpClient     Sender = new TcpClient();

            for (int i = 0; i < 128; i++)
            {
                try
                {
                    Sender.Connect(Addr, 7778);
                    break;
                }
                catch { }
            }
            if (!Sender.Connected)
            {
                return;
            }

            Sender.GetStream().Write(BitConverter.GetBytes(ls.Count - 1), 0, 4);

            byte[] MainPackage = new byte[37], buf;

            string[] Strings = new string[4];
            foreach (var i in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (i.AddressFamily == AddressFamily.InterNetwork)
                {
                    Strings = i.ToString().Split('.');
                }
            }

            FileTools.Log($"Sended ip:{Strings[0]}.{Strings[1]}.{Strings[2]}.{Strings[3]}");

            for (int i = 0; i < ls.Count - 1; i++)
            {
                MainPackage[0] = byte.Parse(Strings[0]);
                MainPackage[1] = byte.Parse(Strings[1]);
                MainPackage[2] = byte.Parse(Strings[2]);
                MainPackage[3] = byte.Parse(Strings[3]);

                MainPackage[4] = 0;

                buf = BitConverter.GetBytes((int)ls[i].ID);
                ToolsClass.CopyFromArrayToArry(ref MainPackage, ref buf, 5, 0, 4);

                buf = BitConverter.GetBytes(ls[i].X);
                ToolsClass.CopyFromArrayToArry(ref MainPackage, ref buf, 9, 0, 4);

                buf = BitConverter.GetBytes(ls[i].Y);
                ToolsClass.CopyFromArrayToArry(ref MainPackage, ref buf, 13, 0, 4);

                buf = BitConverter.GetBytes(ls[i].XSpeed);
                ToolsClass.CopyFromArrayToArry(ref MainPackage, ref buf, 17, 0, 4);

                buf = BitConverter.GetBytes(ls[i].YSpeed);
                ToolsClass.CopyFromArrayToArry(ref MainPackage, ref buf, 21, 0, 4);

                buf = BitConverter.GetBytes(8);
                ToolsClass.CopyFromArrayToArry(ref MainPackage, ref buf, 25, 0, 4);

                buf = BitConverter.GetBytes(8);
                ToolsClass.CopyFromArrayToArry(ref MainPackage, ref buf, 29, 0, 4);

                buf = BitConverter.GetBytes(-1);
                ToolsClass.CopyFromArrayToArry(ref MainPackage, ref buf, 33, 0, 4);

                Sender.GetStream().Write(MainPackage, 0, MainPackage.Length);
            }
            FileTools.Log("Person list sended");
            Sender.Close();
        }