Пример #1
0
        private void Transmission_Load(object sender, EventArgs e)
        {
            receive_progressBar.Minimum = 0;
            receive_progressBar.Maximum = (int)Math.Ceiling(Double.Parse(fileSize) / ChatRoom.TCP_DATA_MAX_SIZE);
            receive_progressBar.Value   = 0;

            ReceiveFileClass receiveFileThread = new ReceiveFileClass(Win32API.FindWindow(null, this.Text), ipEnd, filePath);

            receiveFileThread.Start();
        }
Пример #2
0
        public static void IntervalBroadcast()
        {
            IntPtr charRoom = Win32API.FindWindow(null, "Чат групи LAN");
            string info     = string.Join(ChatRoom.SEPARATOR + "", new string[] { MessageType.Broadcast + "", ChatRoom.COMPUTER_NAME, ChatRoom.IP_ADDRESS });

            while (true)
            {
                Win32API.SendMessage(charRoom, (int)MessageType.ClearUsers, 0, 0);
                UdpSendMessage.SendToAll(info);
                Thread.Sleep(BROADCAST_INTERVAL);
            }
        }
Пример #3
0
 public static Form GetRoom()
 {
     if (chatRoom == null)
     {
         lock (locker)
         {
             IntPtr handle = Win32API.FindWindow(null, WINDOWS_NAME);
             if (handle != IntPtr.Zero)
             {
                 Win32API.SendMessage(handle, (int)MessageType.ShowWindow, 0, 0);
                 Process.GetCurrentProcess().Kill();
             }
             if (chatRoom == null)
             {
                 chatRoom = new ChatRoom();
             }
         }
     }
     return(chatRoom);
 }
Пример #4
0
 private static void FileAccept(object obj)
 {
     while (true)
     {
         Socket sendFileAccept = ((Socket)obj).Accept();
         byte[] tmp1           = new byte[ChatRoom.TCP_DATA_MAX_SIZE];
         sendFileAccept.Receive(tmp1);
         string filePath = Encoding.Default.GetString(tmp1);
         try
         {
             Win32API.My_lParam lp = new Win32API.My_lParam();
             lp.t = sendFileAccept;
             lp.s = filePath;
             Win32API.SendMessage(Win32API.FindWindow(null, ChatRoom.WINDOWS_NAME), (int)MessageType.FileRequest, 0, ref lp);
         }
         catch (Exception e)
         {
             sendFileAccept.Send(Encoding.Default.GetBytes(e.Message), SocketFlags.None);
             sendFileAccept.Close();
         }
     }
 }
Пример #5
0
 public static Form GetRoom()
 {
     //多线程同时运行到这里,会同时通过这个判断条件执行条件内的代码
     if (chatRoom == null)
     {
         //多线程同时运行到这里后,只能有一个线程通过lock锁,其他线程会被挂起
         lock (locker)
         {
             // 判断是否已存在群聊进程,是则激活并结束当前进程,否则启动群聊窗口
             IntPtr handle = Win32API.FindWindow(null, WINDOWS_NAME);
             if (handle != IntPtr.Zero)
             {
                 Win32API.SendMessage(handle, (int)MessageType.ShowWindow, 0, 0);
                 Process.GetCurrentProcess().Kill();
             }
             //保证群聊窗口唯一
             if (chatRoom == null)
             {
                 chatRoom = new ChatRoom();
             }
         }
     }
     return(chatRoom);
 }
Пример #6
0
        public static void StartListen()
        {
            UdpClient  udpClient  = new UdpClient(ChatRoom.LISTEN_PORT);
            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 0);
            IntPtr     charRoom   = Win32API.FindWindow(null, ChatRoom.WINDOWS_NAME);

            while (true)
            {
                byte[] buff    = udpClient.Receive(ref ipEndPoint);
                string tmpInfo = Encoding.Default.GetString(buff);

                string[]    infos       = tmpInfo.Split(ChatRoom.SEPARATOR);
                MessageType messageType = (MessageType)Enum.Parse(typeof(MessageType), infos[0]);

                switch (messageType)
                {
                case MessageType.Broadcast:
                {
                    string             computerName = infos[1];
                    string             ipAddress    = infos[2];
                    Win32API.My_lParam lp           = new Win32API.My_lParam();
                    lp.s = string.Join(ChatRoom.SEPARATOR + "", new string[] { computerName, ipAddress });
                    Win32API.SendMessage(charRoom, (int)MessageType.Broadcast, 0, ref lp);
                    string myInfo = string.Join(ChatRoom.SEPARATOR + "", new string[] { MessageType.BroadcastReply + "", ChatRoom.COMPUTER_NAME, ChatRoom.IP_ADDRESS });
                    UdpSendMessage.SendToOne(ipAddress, myInfo);
                }
                break;

                case MessageType.BroadcastReply:
                {
                    string             computerName = infos[1];
                    string             ipAddress    = infos[2];
                    Win32API.My_lParam lp           = new Win32API.My_lParam();
                    lp.s = string.Join(ChatRoom.SEPARATOR + "", new string[] { computerName, ipAddress });
                    Win32API.SendMessage(charRoom, (int)MessageType.BroadcastReply, 0, ref lp);
                }
                break;

                case MessageType.ChatMessage:
                {
                    string             computerName = infos[1];
                    string             ipAddress    = infos[2];
                    string             chatData     = infos[3];
                    Win32API.My_lParam lp           = new Win32API.My_lParam();
                    lp.s = string.Join(ChatRoom.SEPARATOR + "", new string[] { computerName, ipAddress, chatData });
                    Win32API.SendMessage(charRoom, (int)MessageType.ChatMessage, 0, ref lp);
                }
                break;

                case MessageType.FileMessage:
                {
                    string             computerName = infos[1];
                    string             ipAddress    = infos[2];
                    string             filePath     = infos[3];
                    string             fileSize     = infos[4];
                    Win32API.My_lParam lp           = new Win32API.My_lParam();
                    lp.s = string.Join(ChatRoom.SEPARATOR + "", new string[] { computerName, ipAddress, filePath, fileSize });
                    Win32API.SendMessage(charRoom, (int)MessageType.FileMessage, 0, ref lp);
                }
                break;


                default:

                    break;
                }
            }
        }