コード例 #1
0
        //Broadcast the message to the recipients

        public void Broadcast(Chat.Packet packet, Chat.Users recipients)
        {
            //If the dataset is Nothing, broadcast to everyone
            if (recipients == null)
            {
                foreach (DictionaryEntry c in AllClients)
                {
                    //Broadcast the message to all users
                    ((Server)c.Value).SendMessage(packet);
                }
            }
            else
            {
                //Broadcast to selected recipients
                foreach (DictionaryEntry c in AllClients)
                {
                    foreach (Chat.User usr in recipients.List)
                    {
                        if (((Server)c.Value)._clientName == usr.Name)
                        {
                            //Send message to the recipient
                            ((Server)c.Value).SendMessage(packet);

                            //---log it locally
                            //Console.WriteLine("sending -----> " & message)
                            break; // TODO: might not be correct. Was : Exit For
                        }
                    }
                }
            }
        }
コード例 #2
0
        internal void receivedData(Chat.Packet packet)
        {
            Chat.Clients recipients = new Chat.Clients();

            if (packet != null)
            {
                switch (packet.ID)
                {
                case Chat.Action.Join:

                    // Deserialize the content
                    Chat.Client usr = Chat.Client.Deserialize(packet.Content);
                    ClientName = usr.Name;

                    // Add the user set to receive a full list of user
                    recipients.List.Add(usr);

                    Chat.Clients usrs = new Chat.Clients();
                    foreach (DictionaryEntry c in IPList)
                    {
                        Chat.Client u = new Chat.Client();
                        u.Name = ((Server)c.Value).ClientName;
                        usrs.List.Add(u);
                    }

                    byte[] msg = Chat.Serialize(Chat.Action.ClientList, usrs);

                    //Send the full user list to the user that just signed on
                    Broadcast(msg, recipients);

                    //Rebroadcast the original message to everyone that this user is online
                    Broadcast(Data, null);

                    break;

                case Chat.Action.Message:
                    Chat.Message message = Chat.Message.Deserialize(packet.Content);

                    foreach (Chat.Client recipient in message.Reciptients)
                    {
                        recipients.List.Add(recipient);
                    }

                    //Send the message to the recipients
                    Broadcast(Data, recipients);
                    break;

                default:

                    break;
                }
            }
        }
コード例 #3
0
        public void sessionEnded(string clientIP)
        {
            AllClients.Remove(clientIP);

            Chat.User usr = new Chat.User();
            usr.Name = _clientName;

            Chat.Packet packet = new Chat.Packet();
            packet.ID      = Chat.MSGType.Disconnect;
            packet.Content = usr;

            Broadcast(packet, null);
        }
コード例 #4
0
        public async void ReceiveMessage(IAsyncResult ar)
        {
            //Read from client
            int bytesRead = 0;

            try
            {
                if (Client.Connected == true)
                {
                    lock (Client.GetStream())
                    {
                        bytesRead = Client.GetStream().EndRead(ar);
                    }

                    //Client disconnected
                    if (bytesRead < 1)
                    {
                        sessionEnded(ClientIP);
                        return;
                    }
                    else
                    {
                        // string deserializer
                        //string messageReceived = System.Text.Encoding.ASCII.GetString(Data, 0, bytesRead);
                        //Chat.Packet packet = Chat.Packet.Deserialize(messageReceived);

                        // you have to cast the deserialized object
                        Chat.Packet packet = Chat.Packet.Deserialize(Data);
                        if (packet != null)
                        {
                            await Task.Run(() => receivedData(packet));
                        }
                    }

                    //Continue reading from the client
                    lock (Client.GetStream()) {
                        Client.GetStream().BeginRead(Data, 0, Convert.ToInt32(Client.ReceiveBufferSize), ReceiveMessage, null);
                    }
                }
            } catch (Exception ex) {
                sessionEnded(ClientIP);
            }
        }
コード例 #5
0
        public async void ReceiveMessage(IAsyncResult ar)
        {
            try
            {
                //Check if the client is connected before you try to get the stream
                if (Session.TCP.Connected == true)
                {
                    int bytesRead = 0;
                    bytesRead = Session.TCP.GetStream().EndRead(ar);
                    if (bytesRead < 1)
                    {
                        return;
                    }
                    else
                    {
                        // string deserializer
                        //string messageReceived = System.Text.Encoding.ASCII.GetString(Session.Data, 0, bytesRead);
                        //Chat.Packet packet = Chat.Packet.Deserialize(messageReceived);

                        // you have to cast the deserialized object
                        Chat.Packet packet = Chat.Packet.Deserialize(Session.Data);
                        if (packet != null)
                        {
                            await Task.Run(() => receivedData(packet));
                        }


                        //receivedDataSets(packet);
                    }

                    //Continue reading for more data
                    Session.TCP.GetStream().BeginRead(Session.Data, 0, Convert.ToInt32(Session.TCP.ReceiveBufferSize), new AsyncCallback(ReceiveMessage), null);
                }
            }
            catch (Exception ex)
            {
                //As soon as the client is disconnected from the IM server,
                //it will trigger this exception. This would be a good place
                //to close the client and begin attempting to reconnect.
                Disconnect();
                MessageBox.Show("Your connection to the server was lost.123");
            }
        }
コード例 #6
0
        public void SendMessage(Chat.Packet msg)
        {
            try {
                //Send the text
                System.Net.Sockets.NetworkStream ns = default(System.Net.Sockets.NetworkStream);
                lock (_client.GetStream()) {
                    ns = _client.GetStream();

                    // Serialize the message
                    string message = "";
                    message = Chat.Serialize(msg);

                    byte[] bytesToSend = System.Text.Encoding.ASCII.GetBytes(message);
                    ns.Write(bytesToSend, 0, bytesToSend.Length);
                    ns.Flush();
                }
            } catch (Exception ex) {
                //MessageBox.Show(ex.ToString);
            }
        }
コード例 #7
0
        public void ReceiveMessage(IAsyncResult ar)
        {
            //Read from client
            int bytesRead = 0;

            try
            {
                if (_client.Connected == true)
                {
                    lock (_client.GetStream())
                    {
                        bytesRead = _client.GetStream().EndRead(ar);
                    }

                    //Client disconnected
                    if (bytesRead < 1)
                    {
                        sessionEnded(_clientIP);
                        return;
                    }
                    else
                    {
                        //The message received from the remote client
                        string messageReceived = System.Text.Encoding.ASCII.GetString(data, 0, bytesRead);

                        // Deserialize the packet
                        Chat.Packet packet = new Chat.Packet();
                        packet = (Chat.Packet)Chat.Deserialize(messageReceived, packet.GetType());

                        Chat.Users recipients = new Chat.Users();
                        switch (packet.ID)
                        {
                        case Chat.MSGType.Join:


                            Chat.User usr = (Chat.User)packet.Content;
                            _clientName = usr.Name;

                            // Add the user set to receive a full list of user
                            recipients.List.Add(usr);

                            Chat.Users usrs = new Chat.Users();
                            foreach (DictionaryEntry c in AllClients)
                            {
                                Chat.User u = new Chat.User();
                                u.Name = ((Server)c.Value)._clientName;
                                usrs.List.Add(u);
                            }

                            Chat.Packet usersListPacket = new Chat.Packet();
                            usersListPacket.ID      = Chat.MSGType.ClientList;
                            usersListPacket.Content = usrs;


                            //Send the full user list to the user that just signed on
                            Broadcast(usersListPacket, recipients);

                            //Rebroadcast the original message to everyone that this user is online
                            Broadcast(packet, null);

                            break;

                        case Chat.MSGType.Message:
                            Chat.Message message = (Chat.Message)packet.Content;

                            foreach (string recipient in message.Reciptients)
                            {
                                Chat.User u = new Chat.User();
                                u.Name = recipient;
                                recipients.List.Add(u);
                            }

                            //Send the message to the recipients
                            Broadcast(packet, recipients);
                            break;

                        default:

                            break;
                        }
                    }

                    //Continue reading from the client
                    lock (_client.GetStream()) {
                        _client.GetStream().BeginRead(data, 0, Convert.ToInt32(_client.ReceiveBufferSize), ReceiveMessage, null);
                    }
                }
            } catch (Exception ex) {
                sessionEnded(_clientIP);
            }
        }
コード例 #8
0
        //Delegate and subroutine to update the textBox control
        //Friend Delegate Sub delUpdateHistory(ByVal str As String)
        internal void receivedData(Chat.Packet packet)
        {
            switch (packet.ID)
            {
            case Chat.Action.Join:

                Chat.Client usr = Chat.Client.Deserialize(packet.Content);
                lstUsers.Items.Add(usr.Name);

                break;

            case Chat.Action.ClientList:
                lstUsers.Items.Clear();

                Chat.Clients usrs = Chat.Clients.Deserialize(packet.Content);
                foreach (Chat.Client u in usrs.List)
                {
                    if (Client.Name != u.Name)
                    {
                        lstUsers.Items.Add(u.Name);
                    }
                }

                break;

            case Chat.Action.Message:
                //Read the message
                Chat.Message msg = Chat.Message.Deserialize(packet.Content);

                switch (msg.Type)
                {
                case Chat.MGSType.Chat:

                    break;

                case Chat.MGSType.PowerShell:

                    break;

                case Chat.MGSType.ComputerInfo:

                    break;
                }

                //Read the recipients...

                //Update the recipient's message history
                txtMessageHistory.Text += msg.Sender.Name + " - " + msg.MSG + Environment.NewLine;
                break;

            case Chat.Action.Disconnect:
                Chat.Client user = Chat.Client.Deserialize(packet.Content);

                //Remove the user from the listbox
                lstUsers.Items.Remove(user.Name);

                break;

            default:
                break;
            }
        }