Esempio n. 1
0
        private void buttonSend_Click(object sender, EventArgs e)
        {
            if ((textBox_Input.Text.Length > 0) && (client != null))
            {
                try
                {
                    if (listBox_ClientList.SelectedIndex == 0)
                    {
                        PublicChatMsg publicMsg = new PublicChatMsg();

                        publicMsg.msg = textBox_Input.Text;
                        MemoryStream outStream = publicMsg.WriteData();
                        client.Send(outStream.GetBuffer());
                    }
                    else
                    {
                        PrivateChatMsg privateMsg = new PrivateChatMsg();

                        privateMsg.msg         = textBox_Input.Text;
                        privateMsg.destination = currentClientList[listBox_ClientList.SelectedIndex];
                        MemoryStream outStream = privateMsg.WriteData();
                        client.Send(outStream.GetBuffer());
                    }
                }
                catch (System.Exception)
                {
                }

                textBox_Input.Text = "";
            }
        }
Esempio n. 2
0
        static void SendPrivateMessage(Socket s, String from, String msg)
        {
            PrivateChatMsg chatMsg = new PrivateChatMsg();

            chatMsg.msg         = msg;
            chatMsg.destination = from;
            MemoryStream outStream = chatMsg.WriteData();

            try
            {
                s.Send(outStream.GetBuffer());
            }
            catch (System.Exception)
            {
            }
        }
Esempio n. 3
0
        private void buttonSend_Click(object sender, EventArgs e)
        {
            if (client != null)
            {
                try
                {
                    if (listBox_ClientList.SelectedIndex == 0)
                    {
                        PublicChatMsg publicMsg = new PublicChatMsg();

                        publicMsg.msg = textBox_Input.Text;
                        MemoryStream outStream = publicMsg.WriteData();
                        client.Send(outStream.GetBuffer());
                    }
                    // If == last item == "Game" Msg
                    else if (listBox_ClientList.SelectedIndex == listBox_ClientList.Items.Count - 1)
                    {
                        GameMsg GameMessage = new GameMsg();
                        GameMessage.msg         = textBox_Input.Text;
                        GameMessage.destination = currentClientList[listBox_ClientList.SelectedIndex];
                        MemoryStream outStream = GameMessage.WriteData();
                        client.Send(outStream.GetBuffer());
                    }
                    else
                    {
                        PrivateChatMsg privateMsg = new PrivateChatMsg();

                        privateMsg.msg         = textBox_Input.Text;
                        privateMsg.destination = currentClientList[listBox_ClientList.SelectedIndex];
                        MemoryStream outStream = privateMsg.WriteData();
                        client.Send(outStream.GetBuffer());
                    }
                }
                catch (System.Exception)
                {
                }

                textBox_Input.Text = "";
            }
        }
Esempio n. 4
0
        static void receiveClientProcess(Object o)
        {
            bool bQuit = false;

            Socket chatClient = (Socket)o;

            Console.WriteLine("client receive thread for " + GetNameFromSocket(chatClient));

            SendClientList();



            while (bQuit == false)
            {
                try
                {
                    byte[] buffer = new byte[4096];
                    int    result;

                    result = chatClient.Receive(buffer);

                    if (result > 0)
                    {
                        MemoryStream stream = new MemoryStream(buffer);
                        BinaryReader read   = new BinaryReader(stream);

                        Msg m = Msg.DecodeStream(read);



                        if (m != null)
                        {
                            Console.Write("Got a message: ");
                            switch (m.mID)
                            {
                            case PublicChatMsg.ID:
                            {
                                PublicChatMsg publicMsg = (PublicChatMsg)m;

                                String formattedMsg = "<" + GetNameFromSocket(chatClient) + "> " + publicMsg.msg;

                                Console.WriteLine("public chat - " + formattedMsg);

                                SendChatMessage(formattedMsg);
                            }
                            break;

                            case PrivateChatMsg.ID:
                            {
                                PrivateChatMsg privateMsg = (PrivateChatMsg)m;

                                String formattedMsg = "PRIVATE <" + GetNameFromSocket(chatClient) + "> " + privateMsg.msg;

                                Console.WriteLine("private chat - " + formattedMsg + "to " + privateMsg.destination);

                                SendPrivateMessage(GetSocketFromName(privateMsg.destination), GetNameFromSocket(chatClient), formattedMsg);

                                formattedMsg = "<" + GetNameFromSocket(chatClient) + "> --> <" + privateMsg.destination + "> " + privateMsg.msg;
                                SendPrivateMessage(chatClient, "", formattedMsg);
                            }
                            break;

                            default:
                                break;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    bQuit = true;

                    String output = "Lost client: " + GetNameFromSocket(chatClient);
                    Console.WriteLine(output);
                    SendChatMessage(output);

                    RemoveClientBySocket(chatClient);

                    SendClientList();
                }
            }
        }
Esempio n. 5
0
        static void clientReceive(Object o)
        {
            Form1 form = (Form1)o;

            while (form.bConnected == true)
            {
                try
                {
                    byte[] buffer = new byte[4096];
                    int    result;

                    result = form.client.Receive(buffer);

                    if (result > 0)
                    {
                        MemoryStream stream = new MemoryStream(buffer);
                        BinaryReader read   = new BinaryReader(stream);

                        Msg m = Msg.DecodeStream(read);

                        if (m != null)
                        {
                            Console.Write("Got a message: ");
                            switch (m.mID)
                            {
                            case PublicChatMsg.ID:
                            {
                                PublicChatMsg publicMsg = (PublicChatMsg)m;

                                form.AddMessageText(publicMsg.msg);
                            }
                            break;

                            case PrivateChatMsg.ID:
                            {
                                PrivateChatMsg privateMsg = (PrivateChatMsg)m;
                                form.AddMessageText(privateMsg.msg);
                            }
                            break;

                            case ClientListMsg.ID:
                            {
                                ClientListMsg clientList = (ClientListMsg)m;
                                form.SetClientList(clientList);
                            }
                            break;

                            case ClientNameMsg.ID:
                            {
                                ClientNameMsg clientName = (ClientNameMsg)m;
                                form.SetClientName(clientName.name);
                            }
                            break;

                            case GameMsg.ID:
                            {
                                GameMsg gameMessage = (GameMsg)m;
                                form.AddGameText(gameMessage.msg);
                            }
                            break;



                            default:
                                break;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    form.bConnected = false;
                    Console.WriteLine("Lost server!");
                }
            }
        }
Esempio n. 6
0
        static void ReceiveClientProcess(Object socket)
        {
            bool bQuit = false;

            // Cast object argument back into a socket
            Socket chatClient = (Socket)socket;

            // Server console debug message
            Console.WriteLine("client receive thread for " + GetNameFromSocket(chatClient));

            // Create a new Player class instance
            Player newPlayer = new Player(GetNameFromSocket(chatClient));

            // Initialise the start location
            newPlayer.CurrentRoom = myDungeon.StartRoom;

            // Locking on heavens door
            lock (playerDictionary)
            {
                // Sanity check, then add new Player and appropriate socket as a PlayerInfo struct to the playerDictionary with the clientName as the dictionary's key
                if (playerDictionary.ContainsKey(GetNameFromSocket(chatClient)))
                {
                    playerDictionary[GetNameFromSocket(chatClient)] = new PlayerInfo(newPlayer, chatClient);
                }
                else
                {
                    playerDictionary.Add(GetNameFromSocket(chatClient), new PlayerInfo(newPlayer, chatClient));
                }
            }

            // On new player instantiation, update all players with the new client list
            SendClientList();

            // Initialise string that will recieve the message from the server
            String sendMsg = "";

            while (bQuit == false)
            {
                try
                {
                    byte[] buffer = new byte[4096];
                    int    result;

                    result = chatClient.Receive(buffer);

                    if (result > 0)
                    {
                        MemoryStream stream = new MemoryStream(buffer);
                        BinaryReader read   = new BinaryReader(stream);

                        Msg message = Msg.DecodeStream(read);

                        if (message != null)
                        {
                            Console.Write("Got a message: ");
                            switch (message.mID)
                            {
                            case PublicChatMsg.ID:
                            {
                                PublicChatMsg publicMsg = (PublicChatMsg)message;

                                String formattedMsg = "<" + GetNameFromSocket(chatClient) + "> " + publicMsg.msg;

                                Console.WriteLine("public chat - " + formattedMsg);

                                SendChatMessage(formattedMsg);
                            }
                            break;

                            case PrivateChatMsg.ID:
                            {
                                PrivateChatMsg privateMsg = (PrivateChatMsg)message;

                                String formattedMsg = "PRIVATE <" + GetNameFromSocket(chatClient) + "> " + privateMsg.msg;

                                Console.WriteLine("private chat - " + formattedMsg + "to " + privateMsg.destination);

                                SendPrivateMessage(GetSocketFromName(privateMsg.destination), GetNameFromSocket(chatClient), formattedMsg);

                                formattedMsg = "<" + GetNameFromSocket(chatClient) + "> --> <" + privateMsg.destination + "> " + privateMsg.msg;
                                SendPrivateMessage(chatClient, "", formattedMsg);
                            }
                            break;

                            case GameMsg.ID:
                            {
                                // Cast back to GameMsg class
                                GameMsg gameMessage = (GameMsg)message;

                                // Get the string from the GameMsg class
                                String formattedMsg = gameMessage.msg;

                                // Filter the incoming message for a reference to another player and store
                                // Empty player with otherwise unassignable name, this can be picked up in the controls checks
                                Player   targetedOtherPlayer  = null;     // new Player("unassigned", new List<Item>());
                                String[] FormattedStringWords = formattedMsg.Split(' ');
                                foreach (String possibleName in FormattedStringWords)
                                {
                                    if (playerDictionary.ContainsKey(possibleName))
                                    {
                                        targetedOtherPlayer = playerDictionary[possibleName].player;
                                        break;
                                    }
                                }

                                // Get the sending client name, to be used as a dictionary key next, and to pass the String value
                                String clientName = GetNameFromSocket(chatClient);

                                // Lock it before changing it
                                lock (playerDictionary)
                                {
                                    // Temporary variable to alter outside of dictionary
                                    Player tempPlayer = playerDictionary[clientName].player;

                                    // Update function of the DungeonClass instance. If moved, Updates the current room of the player instance, and the currentRoom occupency of the dungeon room
                                    sendMsg = controls.Update(ref tempPlayer, ref targetedOtherPlayer, formattedMsg);

                                    // Console debug message
                                    Console.WriteLine(sendMsg);

                                    // Update dictionary variable
                                    playerDictionary[clientName] = new PlayerInfo(tempPlayer, playerDictionary[clientName].socket);

                                    // Parse the returned String from the controls for any server specific commands. Program.cs is the glue which holds the playerDictionary together.
                                    // OUTBOUND STRING PARSING!!! Is this a good idea?

                                    // Try/catch is needed to avoid crashing due to mismatched string sizes. i.e. comparing the first 14 characters of a 10 length string.

                                    // "Say" command will always return a sendMsg beginning with "Room chat:". If true then send private message to all players in room
                                    try
                                    {
                                        if (sendMsg.Substring(0, 10) == "Room chat:")
                                        {
                                            foreach (String playerName in tempPlayer.GetCurrentRoomRef.PlayersInRoom)
                                            {
                                                SendPrivateMessage(playerDictionary[playerName].socket, "", sendMsg);
                                            }
                                            break;
                                        }
                                    }
                                    catch
                                    {
                                    }



                                    // If sendMsg conditions have not returned true for any of the ablove specific cases, then send the message to the player socket
                                    SendGameMessage(chatClient, "", sendMsg);
                                }
                            }
                            break;

                            case PlayerInitMsg.ID:
                            {
                                // Cast back to GameMsg class
                                PlayerInitMsg playerInitMessage = (PlayerInitMsg)message;

                                // Get the string from the PlayerInitMsg class
                                // String characterSheetString = playerInitMessage.msg;

                                // String[] processedCharacterSheet = characterSheetString.Split(' ');

                                // Get the sending client name, to be used as a dictionary key next, and to pass the String value
                                String clientName = GetNameFromSocket(chatClient);

                                // Lock
                                lock (playerDictionary)
                                {
                                    // Temporary variable to assign value outside of dictionary
                                    Player tempPlayer = playerDictionary[clientName].player;


                                    // Update dictionary variable
                                    playerDictionary[clientName] = new PlayerInfo(tempPlayer, playerDictionary[clientName].socket);
                                }
                            }
                            break;

                            default:
                                break;
                            }
                        }
                    }
                }

                // Remove player from game
                catch (Exception)
                {
                    bQuit = true;

                    // Lost the player message
                    String output = "Lost client: " + GetNameFromSocket(chatClient);

                    // Debug message to server console
                    Console.WriteLine(output);

                    // Inform the other players
                    SendChatMessage(output);

                    // Always lock the list!!!
                    lock (playerDictionary)
                    {
                        try
                        {
                            // Sanity check, then remove player name from the current rooms list of currently occupying players names
                            if (playerDictionary[GetNameFromSocket(chatClient)].player.GetCurrentRoomRef.PlayersInRoom.Contains(GetNameFromSocket(chatClient)))
                            {
                                playerDictionary[GetNameFromSocket(chatClient)].player.GetCurrentRoomRef.RemovePlayer(GetNameFromSocket(chatClient));
                            }

                            // Sanity check, then remove player information from playerDictionary
                            if (playerDictionary.ContainsKey(GetNameFromSocket(chatClient)))
                            {
                                playerDictionary.Remove(GetNameFromSocket(chatClient));
                            }
                        }
                        catch (Exception)
                        { }
                    }

                    // Update all other players current client list
                    SendClientList();
                }
            }
        }