Example #1
0
        private void sndMsgBtn_Click(object sender, EventArgs e)
        {
            string msg = inputBox.Text;

            Update(client.FormatMsg(userName, msg));
            MessageFrame mf = MsgParser(msg);

            client.SendObjStream(mf);
        }
Example #2
0
 private void inputBox_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.KeyCode == Keys.Enter)
     {
         string msg = inputBox.Text;
         Update(client.FormatMsg(userName, msg));
         MessageFrame mf = MsgParser(msg);
         client.SendObjStream(mf);
         inputBox.Text      = "";
         e.SuppressKeyPress = true;
     }
 }
Example #3
0
        //odczytuje strumień jako string
        private async Task <string> ReadStreamAsync(NetworkStream stream)
        {
            byte[] result;
            result = new byte[1024];
            int bytesRead = await stream.ReadAsync(result, 0, result.Length);

            MessageFrame messageFrame = new MessageFrame();

            messageFrame = (MessageFrame)Deserialize(result);

            //string response = System.Text.Encoding.UTF8.GetString(result, 0, bytesRead);
            return(messageFrame.Data);
        }
Example #4
0
 public void StopClient()
 {
     if (!clientIsDisconnected)
     {
         MessageFrame mf = new MessageFrame();
         mf.Data = string.Format(nickname + " disconected");
         SendObjStream(mf);
         usr.clientStream.Close();
         clientsTaskList.Remove(usr);
         nickname             = string.Empty;
         clientIsDisconnected = true;
     }
 }
Example #5
0
            private async void ReadStreamAsync() //odczytuje strumień i przekazuje do innych podłączonych klientów
            {
                while (!isDisconected)
                {
                    try
                    {
                        object obj = await service.ReadObjAsync(clientStream);

                        MessageFrame mf = new MessageFrame();
                        mf = (MessageFrame)obj;
                        if (mf.Destination == null && mf.Command == null)
                        {
                            service.BroadCast(obj);
                            service.UpdateUI(service.FormatMsg(mf.Sender, mf.Data));
                        }
                        if (mf.Destination != null)
                        {
                            service.BroadCast(obj, mf.Destination);
                        }
                        if (mf.Command == "list")
                        {
                            MessageFrame listmf = new MessageFrame();
                            listmf.ConnectedUsers = service.GetUsersList();
                            service.BroadCast(listmf, mf.Sender);
                        }
                        if (mf.Command == "join")
                        {
                            ChatRoom romm = new ChatRoom(mf.Param);
                            romm.AddClient(clientStream);
                        }
                    }

                    catch (System.IO.IOException) // przerwij kiedy połączenie z klientem zerwane
                    {
                        service.clientsTaskList.Remove(this);
                        break;
                    }

                    catch (ObjectDisposedException) when(service.serverIsClosed)  //przerwij kiedy serwer rozłączony
                    {
                        break;
                    }
                    catch (SerializationException) when(service.serverIsClosed)
                    {
                        break;
                    }
                }
                service.clientsTaskList.Remove(this);
                clientStream.Close();
            }
Example #6
0
        private void BroadCast(object obj)
        {
            MessageFrame mf = (MessageFrame)obj;

            foreach (var client in clientsTaskList.Where(u => u.username != mf.Sender))
            {
                try
                {
                    SendObjStream(obj, client.clientStream);
                }
                catch (Exception ex)
                {
                    UpdateUI(ex.Message);
                }
            }
        }
Example #7
0
        public async void ClientReadStreamAsync(NetworkStream stream)
        {
            while (!clientIsDisconnected)
            {
                try
                {
                    MessageFrame mf  = new MessageFrame();
                    object       obj = await ReadObjAsync(stream);

                    mf = (MessageFrame)obj;
                    if (mf.Data != null)
                    {
                        //string response = await ReadStreamAsync(stream);
                        UpdateUI(FormatMsg(mf.Sender, mf.Data));
                        if (mf.Data == string.Empty)
                        {
                            break;
                        }
                    }
                    if (mf.ConnectedUsers != null)
                    {
                        foreach (var user in mf.ConnectedUsers)
                        {
                            UpdateUI(FormatMsg(usr.username, user));
                        }
                    }
                }

                catch (System.IO.IOException)
                {
                    break;
                }

                catch (ObjectDisposedException) when(serverIsClosed)  //przerwij kiedy serwer rozłączony
                {
                    break;
                }

                catch (SerializationException) when(serverIsClosed)
                {
                    break;
                }
            }
            stream.Close();
        }
Example #8
0
        private MessageFrame MsgParser(string msg)
        {
            MessageFrame mf = new MessageFrame();

            string[] param = msg.Split(new char[] { ' ' }, 3);
            mf.Sender = userName;

            switch (param[0])
            {
            case ":list":
                mf.Command = "list";
                break;

            case ":quit":

                Close();
                break;

            case ":join":

                mf.Command = "join";
                mf.Param   = param[1];
                break;

            case ":pv":
                mf.Data        = msg;
                mf.Destination = param[1];


                break;

            default:
                mf.Data = msg;
                break;
            }
            return(mf);
        }
Example #9
0
        //oczekuje na połączenia dodaje klientów do listy
        private async void ListenForClients()
        {
            while (!serverIsClosed)
            {
                try
                {
                    MessageFrame mf = new MessageFrame();
                    client = await listener.AcceptTcpClientAsync();

                    string username = await ReadStreamAsync(client.GetStream());

                    clientsTaskList.Add(new User(client.GetStream(), username, this));
                    string time = timer.TimeOfDay.ToString();
                    string msg  = string.Format(TimeStamp() + " " + "{0}, joined {1}\n", username, GetServerIpAddress());
                    UpdateUI(msg);
                }
                catch (ObjectDisposedException) when(serverIsClosed)
                {
                    break;
                }

                catch (SerializationException) when(serverIsClosed)
                {
                    break;
                }
            }

            //jeśli sa podłączeni klienci rozłącz i wyczysć liste klientów
            if (clientsTaskList != null)
            {
                foreach (var client in clientsTaskList)
                {
                    client.Close();
                }
                clientsTaskList.Clear();
            }
        }
Example #10
0
        public void StartClient()
        {
            if (serverIsClosed)
            {
                try
                {
                    client = new TcpClient(iPAddress.ToString(), port);
                    usr    = new User(client.GetStream(), nickname);
                    clientIsDisconnected = false;
                    MessageFrame mf = new MessageFrame();
                    mf.Data = usr.username;
                    SendObjStream(mf);
                    ClientReadStreamAsync(usr.clientStream);
                }

                catch (System.Net.Sockets.SocketException ex)
                {
                    UpdateUI(ex.Message + "\n");
                }
            }
            else
            {
            }
        }