Exemplo n.º 1
0
 public int Add(Client client)
 {
     Console.WriteLine("Adding client with ID " + (_nextClientId++).ToString());
     client.ID = _nextClientId - 1;
     _tcpClients.Add(client);
     return _tcpClients.Last().ID;
 }
Exemplo n.º 2
0
 /*
  * Packet ID: 8
  * Sends new client information to all connected clients
  */
 public void AddPlayer(Client client)
 {
     string name = client.Name;
     byte[] buffer = new byte[9 + name.Length];
     buffer[0] = 8;
     Array.Copy(BitConverter.GetBytes(client.ID), 0, buffer, 1, 4);
     Array.Copy(BitConverter.GetBytes(name.Length), 0, buffer, 5, 4);
     Array.Copy(ASCIIEncoding.ASCII.GetBytes(name), 0, buffer, 9, name.Length);
     SendAll(buffer);
 }
Exemplo n.º 3
0
 public void AddPlayer(Client addClient, Client toClient)
 {
     Console.WriteLine("Sending player {0} to player {1}", addClient.ID, toClient.ID);
     string name = addClient.Name;
     byte[] buffer = new byte[9 + name.Length];
     buffer[0] = 8;
     Array.Copy(BitConverter.GetBytes(addClient.ID), 0, buffer, 1, 4);
     Array.Copy(BitConverter.GetBytes(name.Length), 0, buffer, 5, 4);
     Array.Copy(ASCIIEncoding.ASCII.GetBytes(name), 0, buffer, 9, name.Length);
     SendData(toClient, buffer);
 }
Exemplo n.º 4
0
 public void Remove(Client client)
 {
     _tcpClients.Remove(client);
     _server.RemovePlayerAll(client);
     Console.WriteLine("Removing client " + client.ID);
 }
Exemplo n.º 5
0
        /*
         * Packet ID: 16
         * InitiateGame
         */
        public void InitiateGame(Client playerOne, Client playerTwo)
        {
            if (playerOne.TcpSocket.Connected && playerTwo.TcpSocket.Connected)
            {
                byte[] buffer = new byte[5];
                buffer[0] = 16;
                // Pack player two's ID and send it to player 1 with an InitiateGame packet
                Array.Copy(BitConverter.GetBytes(playerTwo.ID), 0, buffer, 1, 4);
                SendData(playerOne, buffer);
                // Pack player one's ID and send it to player 2
                Array.Copy(BitConverter.GetBytes(playerOne.ID), 0, buffer, 1, 4);
                SendData(playerTwo, buffer);

                Game newGame = new Game(playerOne, playerTwo);
                playerOne.CurrentGame = newGame;
                playerTwo.CurrentGame = newGame;
            }
        }
Exemplo n.º 6
0
 private void SendData(Client client, byte[] buffer)
 {
     if (buffer.Length <= 1024)
     {
         try
         {
             client.TcpSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallback), client);
         }
         catch (SocketException)
         {
             tcpClients._tcpClients.Remove(client);
         }
     }
 }
Exemplo n.º 7
0
        /*
         * Packet ID: 3
         * SyncPlayerList
         * Sends all client information to one client
         */
        public void SyncPlayers(Client client)
        {
            Console.WriteLine("Sending names");
            int packetLength = 1;
            foreach (Client otherClient in tcpClients._tcpClients)
            {
                packetLength += 8;
                packetLength += otherClient.Name.Length;
            }

            byte[] buffer = new byte[packetLength];
            buffer[0] = 3;
            int packetCounter = 1;
            for (int i = 0; i < tcpClients._tcpClients.Count; i++)
            {
                Array.Copy(BitConverter.GetBytes(tcpClients._tcpClients[i].ID), 0, buffer, packetCounter, 4);
                Array.Copy(BitConverter.GetBytes(tcpClients._tcpClients[i].Name.Length), 0, buffer, packetCounter + 4, 4);
                Array.Copy(ASCIIEncoding.ASCII.GetBytes(tcpClients._tcpClients[i].Name), 0, buffer, packetCounter + 8, tcpClients._tcpClients[i].Name.Length);
                packetCounter += 8 + tcpClients._tcpClients[i].Name.Length;
            }
            Console.WriteLine("Buffer length: " + buffer.Length);

            SendData(client, buffer);
        }
Exemplo n.º 8
0
        private void ListenForClients()
        {
            Console.WriteLine("Listening socket opened");

            try
            {
                this.tcpListener.Start();
            }
            catch (SocketException)
            {

            }

            while (running)
            {
                Client newClient = new Client(-1, "name", tcpListener.AcceptSocket());

                tcpClients.Add(newClient);

                newClient.TcpSocket.BeginReceive(newClient.buffer, 0, newClient.buffer.Length, 0, new AsyncCallback(ReadData), newClient);
                new Thread(new ParameterizedThreadStart(CheckPing)).Start(newClient);

                for (int i = 0; i < tcpClients._tcpClients.Count; i++)
                {
                    if (tcpClients._tcpClients[i] != null && tcpClients._tcpClients[i] != newClient)
                        AddPlayer(newClient, tcpClients._tcpClients[i]);
                }

                SendData(newClient.ID, Encoding.ASCII.GetBytes("Welcome to battleships"));

                //if (_clientThread == null)
                //{
                //    _clientThread = new Thread(ClientLoop);
                //    _clientThread.Start();
                //}
                //SendData(lastClientId, new ASCIIEncoding().GetBytes("Connected to server\n"));
            }
        }
Exemplo n.º 9
0
 /*
  * Packet ID: 15
  * SendPlayerId
  * Sends player their player ID
  */
 public void SendPlayerId(Client client)
 {
     byte[] buffer = new byte[9 + client.Name.Length];
     buffer[0] = 15;
     Array.Copy(BitConverter.GetBytes(client.ID), 0, buffer, 1, 4);
     Array.Copy(BitConverter.GetBytes(client.Name.Length), 0, buffer, 5, 4);
     Array.Copy(ASCIIEncoding.ASCII.GetBytes(client.Name), 0, buffer, 9, client.Name.Length);
     SendData(client, buffer);
 }
Exemplo n.º 10
0
        public void SendHit(Client client)
        {
            Client otherClient = (client == client.CurrentGame._playerOne ? client.CurrentGame._playerTwo : client.CurrentGame._playerOne);
            List<Point> otherClientPoints = (client == client.CurrentGame._playerOne ? client.CurrentGame._playerTwoAllSquares : client.CurrentGame._playerOneAllSquares);
            // Build thingy to send back
            List<Point> hits = new List<Point>();
            foreach (Point point in client.targets)
            {
                foreach (Point otherPoint in otherClientPoints)
                {
                    if (point.X == otherPoint.X && point.Y == otherPoint.Y)
                    {
                        hits.Add(point);
                        //otherClientPoints.Remove(otherPoint);
                    }
                }
            }

            byte[] temp = new byte[5 + (8 * hits.Count)];
            temp[0] = 23;
            Array.Copy(BitConverter.GetBytes(hits.Count), 0, temp, 1, 4);

            for (int i = 0; i < hits.Count; i++)
            {
                Array.Copy(BitConverter.GetBytes(hits[i].X), 0, temp, (8 * i) + 5, 4);
                Array.Copy(BitConverter.GetBytes(hits[i].Y), 0, temp, (8 * i) + 9, 4);
            }

            SendData(client, temp);

            // Check if the player has won
        }
Exemplo n.º 11
0
 public void SendChatMessage(Client client, string message)
 {
     byte[] temp = new byte[5 + message.Length];
     temp[0] = 7;
     Array.Copy(BitConverter.GetBytes(message.Length), 0, temp, 1, 4);
     Array.Copy(ASCIIEncoding.ASCII.GetBytes(message), 0, temp, 5, message.Length);
     SendData(client, temp);
 }
Exemplo n.º 12
0
 /*
  * Packet ID: 10
  * Sends new client removed information to a client
  */
 public void RemovePlayerAll(Client client)
 {
     Console.WriteLine("Syncing removed player to all players");
     byte[] buffer = new byte[5];
     buffer[0] = 10;
     Array.Copy(BitConverter.GetBytes(client.ID), 0, buffer, 1, 4);
     SendAll(buffer);
 }
Exemplo n.º 13
0
 /*
  * Packet ID: 23
  * SendHit
  */
 public void RemoveHit(Client client)
 {
     Client otherClient = (client == client.CurrentGame._playerOne ? client.CurrentGame._playerTwo : client.CurrentGame._playerOne);
     List<Point> otherClientPoints = (client == client.CurrentGame._playerOne ? client.CurrentGame._playerTwoAllSquares : client.CurrentGame._playerOneAllSquares);
     // Build thingy to send back
     List<Point> hits = new List<Point>();
     foreach (Point point in client.targets)
     {
         for (int i = 0; i < otherClientPoints.Count; i++)
         {
             if (point.X == otherClientPoints[i].X && point.Y == otherClientPoints[i].Y)
             {
                 otherClientPoints.Remove(otherClientPoints[i]);
                 i--;
                 continue;
             }
         }
     }
 }