private void NewSingleChat(string remoteIP, string NickName, string msg)
        {
            SingleChatWindow window = new SingleChatWindow
            {
                RemoteIP = remoteIP
            };

            window.SetNickName(NickName);
            instance.singleWindows.Add(window);
            window.Show();
            if (!msg.Equals(string.Empty))
            {
                window.NewMessage(msg);
            }
        }
Пример #2
0
        private void Receive(object obj)
        {
            Socket           recv         = obj as Socket;
            string           filename     = "";
            long             filelength   = 0;
            long             recvlength   = 0;
            SingleChatWindow window       = null;
            MemoryStream     memoryStream = new MemoryStream();

            try
            {
                while (true)
                {
                    byte[] buffer = new byte[1024 * 1024 + 2048];
                    int    count  = recv.Receive(buffer);

                    memoryStream.Write(buffer, 0, count);
                    buffer = memoryStream.ToArray();

                    if (count == 0)
                    {
                        break;
                    }

                    int startIndex = 0;
                    while (true)
                    {
                        int totalLength = 0, headerLength = 0;
                        if (buffer.Length - startIndex < 4)
                        {
                            totalLength = -1;
                        }
                        else
                        {
                            totalLength = BitConverter.ToInt32(buffer, startIndex);
                        }

                        if ((buffer.Length - startIndex < (totalLength + 8)) || totalLength == -1)
                        {
                            memoryStream.Close();
                            memoryStream.Dispose();
                            memoryStream = new MemoryStream();
                            memoryStream.Write(buffer, startIndex, buffer.Length - startIndex);
                            break;
                        }

                        headerLength = BitConverter.ToInt32(buffer, startIndex + 4);

                        string  header = Encoding.Default.GetString(buffer, startIndex + 8, headerLength);
                        JObject jobj   = (JObject)JsonConvert.DeserializeObject(header);

                        switch (jobj["action"].ToObject <int>())
                        {
                        case GROUPCHAT:
                        {
                            string remoteIP = jobj["IP"].ToString();
                            string nickname = Users.Find(x => x.IP.Equals(remoteIP)).NickName;
                            string msg      = GetFormattedMessage(nickname, jobj["time"].ToString(), jobj["content"].ToString());
                            groupChatWindow.NewMessage(msg);
                        }
                        break;

                        case USERLISTSYNC:
                        {
                            Users.Clear();
                            Users = jobj["Users"].ToObject <List <User> >();
                            groupChatWindow.Sync();
                        }
                        break;

                        case SINGLECHAT:
                        {
                            string remoteIP = jobj["IP"].ToString();
                            string nickname = Users.Find(x => x.IP.Equals(remoteIP)).NickName;
                            string content  = jobj["content"].ToString();
                            string msg      = GetFormattedMessage(nickname, jobj["time"].ToString(), jobj["content"].ToString());
                            window = singleWindows.Find(win => win.RemoteIP.Equals(remoteIP));
                            if (window == null)
                            {
                                groupChatWindow.NewSingleChatWindow(remoteIP, NickName, msg);
                            }
                            else
                            {
                                window.NewMessage(msg);
                            }
                        }
                        break;

                        case SENDFILE:
                        {
                            string remoteIP = jobj["IP"].ToString();
                            filename   = jobj["filename"].ToString();
                            filelength = jobj["filelength"].ToObject <long>();
                            if (recvlength == 0)
                            {
                                if (File.Exists(filename))
                                {
                                    File.Delete(filename);
                                }
                            }
                            int size = totalLength - headerLength;         //BitConverter.ToInt32(buffer, 4 + headerLength);
                            using (FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write))
                            {
                                fs.Write(buffer, startIndex + 8 + headerLength, size);
                                recvlength += size;
                            }
                            window = singleWindows.Find(win => win.RemoteIP.Equals(remoteIP));
                            if (window == null)
                            {
                                groupChatWindow.NewSingleChatWindow(remoteIP, NickName, string.Empty);
                            }
                            else
                            {
                                window.RefreshProgress((double)recvlength / filelength);
                            }
                            if (recvlength == filelength)
                            {
                                recvlength = 0;
                            }
                        }

                        break;

                        default:
                            break;
                        }
                        startIndex += totalLength + 8;
                    }
                }
            }
            catch (SocketException)
            {
                MessageBox.Show("服务器终止连接!");
                groupChatWindow.CloseAllWindow();
            }
        }