예제 #1
0
        public void Connect()
        {
            client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                client_socket.Connect(new IPEndPoint(IPAddress.Parse(IP), port));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not connect to server!");

                lobby.Invoke(() =>
                {
                    lobby.Close();
                });
                return;
            }

            if (client_socket.Connected)
            {
                Packet pack = new Packet();
                pack.Type         = JoinMessage;
                pack.PlayerStatus = new player();

                pack.PlayerStatus.Name = lobby.player_name;

                SendPacket(pack);
            }

            while (client_socket.IsConnected())
            {
                process_packet.ReadData(client_socket);
            }

            lobby.Invoke(() =>
            {
                lobby.Close();
            });
        }
예제 #2
0
        public void ReadData(Socket client_sock)
        {
            Packet        pack;
            NetworkStream clientStream = null;

            try
            {
                clientStream = new NetworkStream(client_sock);
                pack         = Packet.Parser.ParseDelimitedFrom(clientStream);
            }
            catch (Exception)
            {
                return;
            }

            string PlayerName;

            switch (pack.Type)
            {
            case QuitMessage:
                lobby.Invoke(() =>
                {
                    lobby.RemovePlayer(pack.PlayerStatus.Name, pack.PlayerStatus);
                });
                break;

            case HostSnapshot:
                lobby.Invoke(() =>
                {
                    lobby.UpdateLobby(pack);
                });
                break;

            case JoinMessage:

                PlayerName = pack.PlayerStatus.Name;

                lobby.Invoke(() =>
                {
                    lobby.AddPlayer(PlayerName, clientStream);
                });

                if (ishost)
                {
                    //Create socket->player map to remove them and track them by name for chat and such.
                    lobby_server.PlayerSocketMap.Add(client_sock, PlayerName);

                    lobby_server.SendPacketToAll(pack);
                }
                break;


            case ChatMessage:
                PlayerName = pack.Chat.Name;
                if (ishost)
                {
                    PlayerName = lobby_server.PlayerSocketMap[client_sock];

                    //Send packet to all other players.
                    pack.Chat.Name = PlayerName;
                    lobby_server.SendPacketToAll(pack);
                }

                lobby.Invoke(() =>
                {
                    lobby.AddChatText(PlayerName, pack.Chat.Message);
                });

                break;

            case StartGame:
                lobby.Invoke(() =>
                {
                    lobby.GameStart();
                });
                break;
            }
        }
예제 #3
0
        public void Listen()
        {
            this.sock_listen = true;

            listener_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            listener_sock.Bind(new IPEndPoint(IPAddress.Any, host_port));
            listener_sock.Listen(16);


            while (lobby_form.Visible)
            {
                List <Socket> ReadList  = new List <Socket>(ConnectedSockets);
                List <Socket> ErrorList = new List <Socket>(ConnectedSockets);
                ReadList.Add(listener_sock);


                Socket.Select(ReadList, null, ErrorList, 1000);



                foreach (Socket client_sock in ReadList)
                {
                    if (client_sock == listener_sock)
                    {
                        ConnectedSockets.Add(client_sock.Accept());
                    }
                    else
                    {
                        if (client_sock.Available > 0)
                        {
                            process_packet.ReadData(client_sock);
                        }
                        else
                        {
                            ErrorList.Add(client_sock);
                        }
                    }
                }

                foreach (Socket client_sock in ErrorList)
                {
                    ConnectedSockets.Remove(client_sock);
                    string PlayerName  = PlayerSocketMap[client_sock];
                    player player_pack = PlayerToPack[PlayerName];

                    PlayerSocketMap.Remove(client_sock);

                    lobby_form.Invoke(() =>
                    {
                        lobby_form.RemovePlayer(PlayerName, player_pack);
                    });
                }
            }

            foreach (Socket client in ConnectedSockets)
            {
                if (client.Connected)
                {
                    client.Close();
                }

                client.Dispose();
            }

            listener_sock.Close();
            listener_sock.Dispose();
        }