示例#1
0
 private void GetTcpConnections(clientChat client)
 {
     if (client.client.Client.Poll(0, SelectMode.SelectRead))
     {
         byte[] buff = new byte[1];
         if (client.client.Client.Receive(buff, SocketFlags.Peek) == 0)
         {
             // Client disconnected
             lbMessage.Items.Add("Koniec poł z: " + client.IP.ToString());
             client.activeCall = false;
         }
     }
 }
示例#2
0
        private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            //receive messages
            try
            {
                int l = consL.Count;
                while (true)
                {
                    for (int i = 0; i < consL.Count; i++)
                    {
                        lCountUsers.Invoke(new MethodInvoker(delegate { lCountUsers.Text = consL.Count.ToString(); }));

                        //GetTcpConnections(consL[i]);
                        string message = consL[i].message;
                        if (message == "//command END")
                        {
                            lbMessage.Invoke(new MethodInvoker(delegate { lbMessage.Items.Add("[" + consL[i].IP.ToString() + "] :Koniec połączenia"); lbMessage.TopIndex = Math.Max(lbMessage.Items.Count - visibleItems + 1, 0); }));
                            tvUsers.Invoke(new MethodInvoker(delegate
                            {
                                rooms[consL[i].room].Nodes.Remove(consL[i].treeNode);
                                roomsTab[consL[i].room].Remove(consL[i]);
                            }));
                            List <string> users = new List <string>();
                            for (int u = 0; u < roomsTab[consL[i].room].Count; u++)
                            {
                                users.Add(roomsTab[consL[i].room][u].nick);
                            }
                            clientChat leaveUser = consL[i];
                            consL[i].activeCall = false;
                            consL[i].bw.CancelAsync();
                            consL[i].Close();
                            consL.RemoveAt(i);
                            string messageSent = "Żegnamy " + leaveUser.nick;
                            for (int j = 0; j < consL.Count; j++)
                            {
                                if (consL[j].room == leaveUser.room)
                                {
                                    consL[j].writing.Write(jsonS(messageSent, jsonS(users.ToArray())));
                                }
                            }
                            lCountUsers.Invoke(new MethodInvoker(delegate { lCountUsers.Text = consL.Count.ToString(); }));
                            continue;
                        }
                        else if (message != " ")
                        {
                            CultureInfo ci          = CultureInfo.InvariantCulture;
                            string      time        = DateTime.Now.ToString("HH:mm", ci);
                            string      messageSent = "[" + time + "]{" + consL[i].room + "} <" + consL[i].nick + "> " + message;
                            lbMessage.Invoke(new MethodInvoker(delegate { lbMessage.Items.Add(messageSent); lbMessage.TopIndex = Math.Max(lbMessage.Items.Count - visibleItems + 1, 0); }));
                            List <string> users = new List <string>();
                            for (int u = 0; u < roomsTab[consL[i].room].Count; u++)
                            {
                                users.Add(roomsTab[consL[i].room][u].nick);
                            }
                            for (int j = 0; j < consL.Count; j++)
                            {
                                if (consL[j].room == consL[i].room)
                                {
                                    consL[j].writing.Write(jsonS(messageSent, jsonS(users.ToArray())));
                                }
                            }
                            consL[i].message = " ";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString(), "Błąd BACG 2");
            }
        }
示例#3
0
        private void bwServer_DoWork(object sender, DoWorkEventArgs e)
        {
            IPAddress adresIP = null;

            try
            {
                adresIP = IPAddress.Parse(tbHostAddress.Text);
            }
            catch
            {
                MessageBox.Show("Błędny format adresu IP!", "Błąd");
                tbHostAddress.Invoke(new MethodInvoker(delegate { tbHostAddress.Text = String.Empty; }));
                //tbHostAddress.Text = String.Empty;
                return;
            }

            int port = System.Convert.ToInt16(nUDPort.Value);


            bool activeCall = false;

            //connection:
            server = new TcpListener(adresIP, port);
            server.Start();
            if (!backgroundWorker2.IsBusy)
            {
                backgroundWorker2.RunWorkerAsync();
            }

            lbMessage.Invoke(new MethodInvoker(delegate {
                visibleItems = lbMessage.ClientSize.Height / lbMessage.ItemHeight;
            }));

            try
            {
                while (true)
                {
                    clientChat    client  = new clientChat(server);
                    NetworkStream ns      = client.ns;
                    IPEndPoint    IP      = client.IP;
                    BinaryReader  reading = client.reading;
                    BinaryWriter  writing = client.writing;
                    bool          newNick = true;

                    string[] request = JsonConvert.DeserializeObject <string[]>(reading.ReadString());
                    //foreach (string Bar in request)
                    //{
                    //    lbMessage.Invoke(new MethodInvoker(delegate {
                    //        lbMessage.Items.Add(Bar);
                    //    }));
                    //}
                    client.nick     = request[1];
                    client.room     = System.Convert.ToInt16(request[2]);
                    client.treeNode = new TreeNode(client.nick);
                    client.treeNode.Nodes.Add(new TreeNode(IP.ToString()));
                    for (int i = 0; i < consL.Count; i++)
                    {
                        if (consL[i].nick == client.nick)
                        {
                            newNick = false;
                        }
                    }
                    if (newNick)
                    {
                        if (request[0] == Hash(tbPass.Text) && request.Length == 3)
                        {
                            lbMessage.Invoke(new MethodInvoker(delegate { lbMessage.Items.Add("[" + IP.ToString() + "] :Nawiązano połączenie"); lbMessage.TopIndex = Math.Max(lbMessage.Items.Count - visibleItems + 1, 0); }));

                            client.activeCall = true;
                            string messageSent = "1";
                            writing.Write(jsonS(messageSent));
                            messageSent = "Witaj w SzymCZAT pokój ogólny to 0";
                            writing.Write(jsonS(messageSent));
                            ;
                        }
                        else
                        {
                            lbMessage.Invoke(new MethodInvoker(delegate { lbMessage.Items.Add("[" + IP.ToString() + "] :Odrzucono połączenie"); lbMessage.TopIndex = Math.Max(lbMessage.Items.Count - visibleItems + 1, 0); }));
                            string messageSent = "0";
                            writing.Write(jsonS(messageSent));
                            client.Close();
                            client.activeCall = false;
                        }
                    }
                    else
                    {
                        lbMessage.Invoke(new MethodInvoker(delegate { lbMessage.Items.Add("[" + IP.ToString() + "] :Odrzucono połączenie - zajety nick"); lbMessage.TopIndex = Math.Max(lbMessage.Items.Count - visibleItems + 1, 0); }));
                        string messageSent = "2";
                        writing.Write(jsonS(messageSent));
                        client.Close();
                        client.activeCall = false;
                    }
                    if (client.activeCall)
                    {
                        consL.Add(client);
                        if (!client.bw.IsBusy)
                        {
                            client.bw.RunWorkerAsync();
                        }

                        tvUsers.Invoke(new MethodInvoker(delegate {
                            rooms[client.room].Nodes.Add(client.treeNode);
                            roomsTab[client.room].Add(client);
                        }));

                        string messageSent = "Zalogowałeś do pokoju {" + client.room + "}";
                        writing.Write(jsonS(messageSent));
                        List <string> users = new List <string>();
                        for (int u = 0; u < roomsTab[client.room].Count; u++)
                        {
                            users.Add(roomsTab[client.room][u].nick);
                        }
                        messageSent = "Witamy " + client.nick;
                        for (int j = 0; j < consL.Count; j++)
                        {
                            if (consL[j].room == client.room)
                            {
                                consL[j].writing.Write(jsonS(messageSent, jsonS(users.ToArray())));
                            }
                        }
                        //BackgroundWorker bw = new BackgroundWorker();
                        //bw.DoWork += new System.ComponentModel.DoWorkEventHandler(bwCON_DoWork);
                        //bw.WorkerSupportsCancellation = true;
                    }
                }
            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.ToString(), "Błąd");
            }
        }