Пример #1
0
 void irc_OnJoinRecvd(JoinMsg join)
 {
     if (oplist.ContainsKey(join.who))
     {
         irc.SendMessage(join.who, "to get ops, respond with \"op (channel) (password)\"");
     }
 }
Пример #2
0
 void irc_OnJoinRecvd(JoinMsg join)
 {
     if (oplist.ContainsKey(join.who))
     {
         irc.SendMessage(join.who, "to get ops, respond with \"op (channel) (password)\"");
     }
 }
Пример #3
0
        private void RecieveData(IAsyncResult result)
        {
            int amountOfdata = Player.PlayerSocket.EndReceive(result);
            //byte[] temp = new byte[amountOfdata];
            //File.WriteAllBytes("fle.txt", recBuffer);
            //temp = ArrayCopy(recBuffer, temp);
            //Array.Copy(recBuffer, 0, temp, 0, temp.Length);
            string dataInString = Encoding.Default.GetString(recBuffer);

            //MessageBox.Show(dataInString);

            // checking what type of response has the server sent
            if (dataInString.Contains("Create Room"))
            {
                List <Room> rooms = JsonConvert.DeserializeObject <List <Room> >(dataInString);
                Rooms = rooms;
                lsRooms.Items.Clear();
                foreach (Room room in Rooms)
                {
                    lsRooms.Items.Add(room.RoomName + " " + room.RoomId);
                }
            }
            else if (dataInString.Contains("yes join"))
            {
                JoinMsg joinMsg = JsonConvert.DeserializeObject <JoinMsg>(dataInString);
                Room    room    = null;
                foreach (Room r in Rooms)
                {
                    if (r.RoomId == joinMsg.RoomId)
                    {
                        room = r;
                        break;
                    }
                }
                this.Hide();
                GameFrm gameFrm = new GameFrm(joinMsg.SenderPlayer, room, this);
                gameFrm.Show();
            }
            else
            {
                string recData = Encoding.Default.GetString(recBuffer);

                if (recData.Contains("room"))
                {
                    // deserialzing the data as a room list object
                    // recieving rooms
                    Rooms = JsonConvert.DeserializeObject <List <Room> >(recData);

                    foreach (Room room in Rooms)
                    {
                        lsRooms.Items.Add(room.RoomName + " " + room.RoomId);
                    }
                }
            }
            recBuffer = new byte[4048];
            Player.PlayerSocket.BeginReceive(recBuffer, 0, recBuffer.Length, SocketFlags.None, RecieveData, null);
        }
Пример #4
0
        private void btnJoin_Click(object sender, EventArgs e)
        {
            // sending a special message to the server
            // to indicate that the player wants to join a room
            // we need 2 things (room id, all player info)
            Player.msgType = "Join";

            // creating a helper object to send a special kind of data to the server
            JoinMsg helper = new JoinMsg
            {
                SenderPlayer = Player,
                RoomId       = lsRooms.SelectedItem.ToString().Split(' ')[1]
            };

            string sentData = JsonConvert.SerializeObject(helper);

            recBuffer = Encoding.Default.GetBytes(sentData);

            /// =?>
            var res = Player.PlayerSocket.BeginSend(recBuffer, 0, recBuffer.Length, SocketFlags.None, SendJoinData, null);
        }
Пример #5
0
        static void ReceivingData_callback(IAsyncResult result)
        {
            Socket socket       = (Socket)result.AsyncState;
            int    amountOfData = 0;

            // we are handling wether there is user that is disconnected
            try
            {
                amountOfData = socket.EndReceive(result);
            }
            catch (SocketException)
            {
                Console.WriteLine("===============================================");
                Console.WriteLine(socket.RemoteEndPoint + " Disconnected");
                Console.WriteLine("===============================================");
            }
            // creating a new player
            byte[] temp = new byte[amountOfData];
            Array.Copy(receivingBuffer, 0, temp, 0, temp.Length);

            // checking the type of the incoming message
            string data = Encoding.UTF8.GetString(temp);

            if (data.Contains("Send Name"))
            {
                // then the player is sending an object along with his name
                // so we will cast the data to Player object
                Player player = JsonConvert.DeserializeObject <Player>(data);
                // adding the captured socket to the player socket
                player.PlayerSocket = socket;
                // adding the player to the connected players list
                ConnectedPlayers.Add(player);

                // sending rooms data to the client
                string roomsData = JsonConvert.SerializeObject(Rooms);
                sendingBuffer = Encoding.Default.GetBytes(roomsData);
                player.PlayerSocket.BeginSend(sendingBuffer, 0, sendingBuffer.Length, SocketFlags.None, SendingData_callback, player.PlayerSocket);
            }
            else if (data.Contains("Create Room"))
            {
                // getting data from a client that wants to create a room
                Room room = JsonConvert.DeserializeObject <Room>(data);
                foreach (Player p in ConnectedPlayers)
                {
                    if (p.PlayerSocket == socket)
                    {
                        room.OwnerPlayer = p;
                    }
                }
                Rooms.Add(room);

                // serializing the room list to be send
                string roomsListStr = JsonConvert.SerializeObject(Rooms);

                // sending created rooms info to all players
                foreach (Player player in ConnectedPlayers)
                {
                    byte[] tmp = Encoding.Default.GetBytes(roomsListStr);
                    player.PlayerSocket.BeginSend(tmp, 0, tmp.Length, SocketFlags.None, SendingData_callback, player.PlayerSocket);
                }
                Console.WriteLine($"Room {room.RoomName} created by");
            }
            else if (data.Contains("Join"))
            {
                // receveing the data from a client that wants to join a room
                recMsg = JsonConvert.DeserializeObject <JoinMsg>(data);
                Console.WriteLine(recMsg);
                // checking the room status
                foreach (Room room in Rooms)
                {
                    if (room.RoomId == recMsg.RoomId)
                    {
                        if (room.Players.Count == 1)
                        {
                            string       msg = $"{recMsg.SenderPlayer.PlayerName} wants to join?";
                            byte[]       tmp = Encoding.Default.GetBytes(msg);
                            IAsyncResult ar  = room.OwnerPlayer.PlayerSocket.BeginSend(tmp, 0, tmp.Length, SocketFlags.None, SendingJoinReq_callback, room.OwnerPlayer.PlayerSocket);
                            //Console.WriteLine(ar.IsCompleted);
                        }
                    }
                }

                // if is full true we will allow him to enter the room as a player
                // otherwise we will show a message
                Console.WriteLine(recMsg.SenderPlayer.PlayerName + " Wants to join room " + recMsg.RoomId);

                //TODO: check if the room is full with 2 players or not
                // if so => send a request msg to the room owner
                // otherwise => allow him to enter the room as a watcher
            }
            else if (data.Contains("yes join"))
            {
                // sending data to owner and sending data to joiner
                //Player owner = JsonConvert.DeserializeObject<Player>(data);
                recMsg.SenderPlayer.msgType = "yes join";
                string sentData = JsonConvert.SerializeObject(recMsg);
                sendingBuffer = Encoding.Default.GetBytes(sentData);
                Socket senderSocket = null;
                foreach (Player p in ConnectedPlayers)
                {
                    if (p.Id == recMsg.SenderPlayer.Id)
                    {
                        senderSocket = p.PlayerSocket;
                    }
                }
                senderSocket.BeginSend(sendingBuffer, 0, sendingBuffer.Length, SocketFlags.None, SendingData_callback, senderSocket);
            }

            // showing the data on the console screen
            foreach (Player p in ConnectedPlayers)
            {
                Console.WriteLine($"playerId: {p.Id}\n playerName: {p.PlayerName} \n playerStatus:  {p.Status} \n PlayerSocket: {p.PlayerSocket.ToString()}");
            }
            // begin receiving data from the client
            try
            {
                socket.BeginReceive(receivingBuffer, 0, receivingBuffer.Length, SocketFlags.None, ReceivingData_callback, socket);
            }
            catch (SocketException)
            {
                Console.WriteLine("===============================================");
                Console.WriteLine(socket.RemoteEndPoint + " Disconnected");
                Console.WriteLine("===============================================");
                // removing the disconnected client from the players list
                foreach (Player player in ConnectedPlayers)
                {
                    if (player.PlayerSocket == socket)
                    {
                        ConnectedPlayers.Remove(player);
                        break;
                    }
                }
            }
        }
 void irc_OnQuitRecvd(JoinMsg join)
 {
     LoggedIn.Remove(join.who + "!" + join.whoUser + "@" + join.whoHost);
 }
 void irc_OnNickRecvd(JoinMsg join)
 {
     if (LoggedIn.ContainsKey(join.who + "!" + join.whoUser + "@" + join.whoHost))
     {
         string nick = LoggedIn[join.who + "!" + join.whoUser + "@" + join.whoHost];
         LoggedIn.Remove(join.who + "!" + join.whoUser + "@" + join.whoHost);
         LoggedIn.Add(join.channel + "!" + join.whoUser + "@" + join.whoHost, nick);
     }
 }