コード例 #1
0
        public MessengerForm(string ip)
        {
            if (String.IsNullOrWhiteSpace(ip) || String.IsNullOrEmpty(ip))
            {
                MessageBox.Show("Please specify the server IP in the launch arguments like so:\n\nC:/.../MESENGER.exe -[ip:port]\n\nThe program will now close.");
                Process.GetCurrentProcess().Kill();
                return;
            }

            var loginForm = new LoginForm(ip);

            loginForm.ShowDialog();

            _server  = loginForm._client;
            _stream  = loginForm._stream;
            _account = loginForm._account;

            if (_account == null)
            {
                Process.GetCurrentProcess().Kill();
            }

            InitializeComponent();

            lblProfile.Text = _account.Nickname;

            pbProfile.Image = _account.GetProfileImage();

            pbProfile.AllowDrop = true;

            //listen for incomming data
            new Thread(() =>
            {
                while (true)
                {
                    try
                    {
                        if (_server.Connected && _stream.CanRead && _stream.DataAvailable)
                        {
                            BinaryFormatter bf = new BinaryFormatter();
                            onDataReceived(bf.Deserialize(_stream));
                        }
                    }
                    catch
                    {
                    }

                    Thread.Sleep(1);
                }
            })
            {
                IsBackground = true
            }.Start();
        }
コード例 #2
0
        private void lbOnline_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (lbOnline.Items.Count > 0 && e.Index != -1)
            {
                ClientUserAccount account = lbOnline.Items[e.Index] as ClientUserAccount;

                DrawingHelper.enableSmooth(e.Graphics, false);

                e.DrawBackground();

                if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                {
                    e = new DrawItemEventArgs(e.Graphics,
                                              e.Font,
                                              e.Bounds,
                                              e.Index,
                                              e.State ^ DrawItemState.Selected,
                                              e.ForeColor,
                                              Color.FromArgb(0, 180, 255));

                    e.DrawBackground();
                    e.DrawFocusRectangle();
                }

                Brush brush = account.banned ? new SolidBrush(Color.FromArgb(255, 50, 100)) : new SolidBrush(account.Online ? Color.FromArgb(150, 255, 50) : Color.FromArgb(50, 50, 50));

                e.Graphics.FillRectangle(brush, new Rectangle(e.Bounds.Width - 5, e.Bounds.Y, 5, 50));
                DrawingHelper.enableSmooth(e.Graphics, true);

                Image img = ImageUtils.ResizeAndCrop(account.GetProfileImage(), 50, 50);

                e.Graphics.DrawImage(img, e.Bounds.X, e.Bounds.Y);

                if (account.isTyping)
                {
                    Font f = new Font(lbOnline.Font.FontFamily, 21, FontStyle.Bold);

                    string text = "...";

                    SizeF s = e.Graphics.MeasureString(text, f, new Size(50, 50));

                    e.Graphics.FillRectangle(new SolidBrush(Color.Black), 10, e.Bounds.Y + 50 - 10, 50 - 20, 10);

                    e.Graphics.DrawString(text, f, Brushes.Turquoise, 25 - s.Width / 2f + 1, e.Bounds.Y + 25 - s.Height / 2f + 14);
                }

                int i = (int)e.Graphics.MeasureString(account.ToString(), lbOnline.Font, lbOnline.Width - 60).Height;
                e.Graphics.DrawString(account.ToString(), e.Font, new SolidBrush(Color.Black),
                                      new Rectangle(new Point(55, e.Bounds.Y + 25 - i / 2), new Size(e.Bounds.Width - 60, 50)));
            }
        }
コード例 #3
0
ファイル: LoginForm.cs プロジェクト: TominoCZ/MESSENGER.NET
        private void receivedData()
        {
            BinaryFormatter bf       = new BinaryFormatter();
            object          received = bf.Deserialize(_stream);

            if (received is ClientUserAccount acc)
            {
                _account = acc;
                OK       = true;
                BeginInvoke(new MethodInvoker(Close));
            }
            else if (received is LoginFailedPacket lfp)
            {
                MessageBox.Show("Failed to log in:\n" + lfp.reason);
            }
            else if (received is RegisterFailedPacket)
            {
                MessageBox.Show("This username is not available.");
            }
        }
コード例 #4
0
        private void updateChatBox(ClientUserAccount account)
        {
            if (account == lbOnline.SelectedItem)
            {
                account.viewedChat();
            }

            if (account == null || account != lbOnline.SelectedItem)
            {
                return;
            }

            lbChat.Items.Clear();
            List <ChatNode> chat = account.getChat();

            foreach (var n in chat)
            {
                lbChat.Items.Add(n);
            }

            lbChat.TopIndex      = lbChat.Items.Count - 1;
            lbChat.SelectedIndex = lbChat.Items.Count - 1;
            lbChat.SelectedIndex = -1;
        }
コード例 #5
0
        private void onDataReceived(object packet)
        {
            if (packet is List <ClientUserAccount> users)
            {
                //PRESERVE PREVIOUS DATA
                int selected = -1;
                Dictionary <int, List <ChatNode> > chats       = new Dictionary <int, List <ChatNode> >();
                Dictionary <int, string>           textsToSend = new Dictionary <int, string>();
                Dictionary <int, int>  unread = new Dictionary <int, int>();
                Dictionary <int, bool> typing = new Dictionary <int, bool>();

                for (int i = 0; i < lbOnline.Items.Count; i++)
                {
                    var node = lbOnline.Items[i] as ClientUserAccount;

                    unread.Add(node.UUID, node.unread);
                    chats.Add(node.UUID, node.getChat());
                    typing.Add(node.UUID, node.isTyping);

                    if (node.Online)
                    {
                        textsToSend.Add(node.UUID, node.textToSend);
                    }
                }

                //LOAD HISTORY
                try
                {
                    if (!loadedHistory)
                    {
                        if (File.Exists($"MSGR_chat_history\\{_account.UUID}.dat"))
                        {
                            using (var fs = File.OpenRead($"MSGR_chat_history\\{_account.UUID}.dat"))
                            {
                                var             bf  = new BinaryFormatter();
                                UserChatHistory uch = bf.Deserialize(fs) as UserChatHistory;

                                if (uch.UUID == _account.UUID)
                                {
                                    chats  = uch.chats;
                                    unread = uch.unread;

                                    selected = uch.lastSelected;
                                }
                            }
                        }

                        loadedHistory = true;
                    }
                }
                catch
                {
                }

                BeginInvoke(new MethodInvoker(() =>
                {
                    lbOnline.BeginUpdate();

                    if (lbOnline.SelectedItem != null)
                    {
                        selected = (lbOnline.SelectedItem as ClientUserAccount).UUID;
                    }

                    lbOnline.Items.Clear();

                    foreach (var client in users)
                    {
                        if (client.UUID == _account.UUID)
                        {
                            continue;
                        }

                        if (chats.TryGetValue(client.UUID, out var chat))
                        {
                            client.setChat(chat);
                        }

                        lbOnline.Items.Add(client);
                    }

                    if (lbOnline.Items.Count > 0)
                    {
                        try
                        {
                            if (selected != -1)
                            {
                                lbOnline.SelectedItem = getNodeWithID(selected);
                            }
                            else
                            {
                                lbOnline.SelectedIndex = 0;
                            }
                        }
                        catch
                        {
                        }
                        for (int i = 0; i < lbOnline.Items.Count; i++)
                        {
                            var node = lbOnline.Items[i] as ClientUserAccount;

                            unread.TryGetValue(node.UUID, out node.unread);
                            typing.TryGetValue(node.UUID, out node.isTyping);
                            textsToSend.TryGetValue(node.UUID, out node.textToSend);

                            if (lbOnline.SelectedItem is ClientUserAccount cua && cua.UUID == node.UUID)
                            {
                                tbMessage.Enabled = node.Online;
                            }
                        }
                    }
                    else
                    {
                        lbChat.Items.Clear();

                        tbMessage.Size   = new Size(355, tbMessage.Size.Height);
                        pbToSend.Visible = false;
                        pbToSend.Image   = null;
                        image            = null;
                        tbMessage.Text   = "";
                        lblChat.Text     = "Chat:";
                    }

                    lbOnline.EndUpdate();
                }));
            }
            else if (packet is MessagePacket msgp)
            {
                BeginInvoke(new MethodInvoker(() =>
                {
                    ClientUserAccount from = getNodeWithID(msgp.from);

                    if (from != null)
                    {
                        from.textToSend = msgp.message;
                        from.appendMessage(/*msgp.message, */ from.Nickname, from.UUID, msgp.GetImage(), false);
                        from.textToSend = "";

                        if (from != lbOnline.SelectedItem)
                        {
                            from.unread++;

                            lbOnline.Invalidate();
                        }

                        updateChatBox(from);
                    }
                }));

                saveChatHistory();
            }
            else if (packet is LogoutAckPacket lop)
            {
                onDisconnected(lop.message);
            }
            else if (packet is TypingPacket tp)
            {
                var node = getNodeWithID(tp.from);

                if (node != null)
                {
                    node.isTyping = tp.typing;
                    lbOnline.Invalidate();
                }
            }
        }