Beispiel #1
0
 public void Ok_button_clicked(object sender, FormEventData e)
 {
     if (Host_name_textbox.Text.Length > 12)
     {
         Game1.MessageBox(new IntPtr(0), "Player Name cannot more than 12 characters", "Player Name Invalid", 0);
         return;
     }
     if (Room_name_textbox.Text.Length > 12)
     {
         Game1.MessageBox(new IntPtr(0), "Room Name cannot more than 12 characters", "Room Name Invalid", 0);
         return;
     }
     try
     {
         main_game.mRoomScreen.IPAddress = ipTextBox.Text;
         Player player = new Player(Host_name_textbox.Text, Game1.getLocalIP());
         player.Status = true;
         Room r = new Room(player, Room_name_textbox.Text,
                           int.Parse(Number_of_player_textbox.Text));
         ScreenEvent.Invoke(this, new SivEventArgs(4, r));
     }
     catch (Exception ex)
     {
         main_game.mRoomScreen.End(new Command());
         Game1.MessageBox(new IntPtr(0), ex.Message, "Exception", 0);
     }
 }
Beispiel #2
0
        private void RequestJoin(Room room)
        {
            Command c = new Command(CommandCode.Can_I_Join);

            byte[] data = c.Serialize();

            try
            {
                tcpClient     = new TcpClient(room.Player_List[room.owner_index].Address, 51002);
                networkStream = tcpClient.GetStream();
                networkStream.Write(data, 0, data.Length);

                Byte[]   bytes = new Byte[1024 * 16];
                DateTime dt    = DateTime.Now;
                while (true)
                {
                    int i;
                    while ((i = networkStream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        Command c2 = new Command(bytes);
                        if (c2.Command_Code == CommandCode.OK_to_Join)
                        {
                            tcpClient.Close();
                            Player player = new Player(playerName.Text, Game1.getLocalIP());
                            room.Player_List.Add(player);
                            ScreenEvent.Invoke(this, new SivEventArgs(4, room));
                            return;
                        }
                        else if (c2.Command_Code == CommandCode.Cant_Join_Room_Full)
                        {
                            tcpClient.Close();
                            Game1.MessageBox(new IntPtr(0), "Can't join, the room is full.", "Can not Join", 0);
                            return;
                        }
                    }
                    if ((DateTime.Now - dt) > new TimeSpan(0, 0, 3))
                    {
                        tcpClient.Close();
                        Game1.MessageBox(new IntPtr(0), "Host is not responding", "Can not Join", 0);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Game1.MessageBox(new IntPtr(0), ex.Message, "Can not Join", 0);
            }
        }