Ejemplo n.º 1
0
 private void ReceiveUdp(object obj)
 {
     try
     {
         while (!eventExitThread.WaitOne(0))
         {
             try
             {
                 var socketReceiveData = TimeoutRead();
                 if (socketReceiveData.ReceiveData != null)
                 {
                     HandleRecvByte(socketReceiveData);
                 }
             }
             catch (System.Exception ex)
             {
                 LogHelper.LogUtil.WXHOOKUI.Warn("ReceiveUdp Warnning: ", ex);
                 if (ex.Message == "正在中止线程。")
                 {
                     return;
                 }
                 socketClient.Close();
                 socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                 socketClient.DontFragment   = true;
                 socketClient.ReceiveTimeout = 3000;
                 socketClient.Bind(g_serverEndPort);
                 Thread.Sleep(5000);
             }
         }
     }
     catch (Exception e)
     {
         System.Diagnostics.Debug.WriteLine(e.Message);
     }
     finally
     {
         lock (this)
         {
             eventExitThread.Dispose();
             eventExitThread = null;
             threadRecv      = null;
             socketClient.Close();
             socketClient = null;
         }
     }
 }
Ejemplo n.º 2
0
        private void init_socket()
        {
            SafeCloseSocket();

            IPAddress ip = IPAddress.Parse("127.0.0.1");

            g_serverEndPort = new IPEndPoint(ip, 24777);

            socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            //g_client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            socketClient.DontFragment   = true;
            socketClient.ReceiveTimeout = 3000;
            socketClient.Bind(g_serverEndPort);

            /* 开始手发包线程 */
            eventExitThread = new ManualResetEvent(false);
            threadRecv      = new MyThread(new ParameterizedThreadStart(ReceiveUdp));
            threadRecv.Start(0);
        }
Ejemplo n.º 3
0
        private void HandMessagecb(int type, string msg, EndPoint clientOfReceiveFrom)
        {
            LogHelper.LogUtil.WXHOOKUI.InfoFormat("HandMessagecb ClientOfReceiveFrom: [{2}], type: [{0}], msg: [{1}]", type, msg, clientOfReceiveFrom);

            ListViewItem lvi = new ListViewItem();

            lvi.Text = "0";
            lvi.SubItems.Add(type.ToString());
            lvi.SubItems.Add(msg);
            this.lv_recv_msg.Items.Add(lvi);

            if (type == 2)
            {
                string[]       wxinfoDetail = msg.Split('|');
                WX_Info_Entity wxInfoEntity = new WX_Info_Entity()
                {
                    WxID        = wxinfoDetail[wxinfoDetail.Length - 3],
                    WxNickName  = wxinfoDetail[wxinfoDetail.Length - 2],
                    WxProcessID = wxinfoDetail[wxinfoDetail.Length - 1],
                    LoginTime   = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    Addr        = clientOfReceiveFrom
                };
                if (wxLoggedinList.Count == 0 && currentWxInfoEntity == null)
                {
                    currentWxInfoEntity = wxInfoEntity;
                }
                wxLoggedinList.Add(wxInfoEntity);

                ListViewItem lviWxLoggedin = new ListViewItem();
                lviWxLoggedin.Text = wxInfoEntity.WxID;
                lviWxLoggedin.Tag  = wxInfoEntity;
                lviWxLoggedin.SubItems.Add(wxInfoEntity.WxNickName);
                lviWxLoggedin.SubItems.Add(wxInfoEntity.WxProcessID);
                lsvWxLoggedin.BeginUpdate();
                lsvWxLoggedin.Items.Add(lviWxLoggedin);
                lsvWxLoggedin.EndUpdate();

                lb_online.Text = "当前在线微信:" + wxLoggedinList.Count;
                SendWeDllCmd(currentWxInfoEntity, (int)WX_CMD.WX_CMD_TYPE_E_MSG_READ, "");
            }

            if (type == (int)WX_CMD.WX_CMD_TYPE_E_MSG_READ_ACK)
            {
                if (msg == "END")
                {
                    MyThread tmp = new MyThread(new ParameterizedThreadStart(get_msg_rcv));
                    tmp.Start(0);
                }
                else
                {
                    SendWeDllCmd(currentWxInfoEntity, (int)WX_CMD.WX_CMD_TYPE_E_MSG_READ, "");
                }
            }

            //好友列表
            if (type == (int)WX_CMD.WX_CMD_TYPE_E_GET_WX_FRIEND_INFO_ACK)
            {
                if (msg.Equals("END", StringComparison.OrdinalIgnoreCase))
                {
                    LogHelper.LogUtil.WXHOOKUI.InfoFormat("FriendList count: [{0}]", currentWxInfoEntity.FriendList.Count);
                    lsvFriend.Items.Clear();
                    lsvFriend.BeginUpdate();
                    foreach (FriendInfo_Entity friend_Info in currentWxInfoEntity.FriendList)
                    {
                        ListViewItem lviFriend = new ListViewItem();
                        lviFriend.Text = friend_Info.Nick;
                        lviFriend.SubItems.Add(friend_Info.Friend_Orig_ID);
                        lsvFriend.Items.Add(lviFriend);
                    }
                    lsvFriend.EndUpdate();
                }
                else
                {
                    var friendList  = msg.Split('|');
                    int friendIndex = CommonUtil.TranNull <int>(friendList[0]);
                    friendIndex = friendIndex + 1;
                    FriendInfo_Entity friendInfo = new FriendInfo_Entity()
                    {
                        Friend_Orig_ID = friendList[1],
                        Friend_ID      = friendList[2],
                        V_ID           = friendList[3],
                        Nick           = friendList[4],
                        Remark         = friendList[5],
                        Sex            = friendList[6]
                    };
                    currentWxInfoEntity.FriendList.Add(friendInfo);
                    SendWeDllCmd(currentWxInfoEntity, (int)WX_CMD.WX_CMD_TYPE_E_GET_WX_FRIEND_INFO, friendIndex.ToString());
                }
            }

            //群列表
            if (type == (int)WX_CMD.WX_CMD_TYPE_E_GET_WX_GROUP_INFO_ACK)
            {
                if (msg.Equals("END", StringComparison.OrdinalIgnoreCase))
                {
                    LogHelper.LogUtil.WXHOOKUI.InfoFormat("GroupList count: [{0}]", currentWxInfoEntity.GroupList.Count);
                    lsvGroup.Items.Clear();
                    lsvGroup.BeginUpdate();
                    foreach (GroupInfo_Entity groupInfo in currentWxInfoEntity.GroupList)
                    {
                        ListViewItem lviGroup = new ListViewItem();
                        lviGroup.Text = string.Format("{0} ({1})", groupInfo.Nick, groupInfo.MemberNumber);
                        lviGroup.SubItems.Add(groupInfo.Group_Orig_ID);
                        lsvGroup.Items.Add(lviGroup);
                    }
                    lsvGroup.EndUpdate();
                }
                else
                {
                    var groupList  = msg.Split('|');
                    int groupIndex = CommonUtil.TranNull <int>(groupList[0]);
                    groupIndex = groupIndex + 1;
                    GroupInfo_Entity groupInfo = new GroupInfo_Entity()
                    {
                        Group_Orig_ID = groupList[1],
                        Group_ID      = groupList[2],
                        V_ID          = groupList[3],
                        Nick          = groupList[4],
                        MemberNumber  = groupList[5]
                    };
                    currentWxInfoEntity.GroupList.Add(groupInfo);
                    SendWeDllCmd(currentWxInfoEntity, (int)WX_CMD.WX_CMD_TYPE_E_GET_WX_GROUP_INFO, groupIndex.ToString());
                }
            }

            //群好友
            if (type == (int)WX_CMD.WX_CMD_TYPE_E_GET_WX_GROUP_MEMBER_INFO_ACK)
            {
                if (msg.Equals("END", StringComparison.OrdinalIgnoreCase))
                {
                    List <GroupInfo_Entity> selectedGroupList = currentWxInfoEntity.GroupList.FindAll(x => x.Selected == true);
                    LogHelper.LogUtil.WXHOOKUI.InfoFormat("GroupList selected count: [{0}]", selectedGroupList.Count);
                    GroupInfo_Entity currentGroup = selectedGroupList[0];
                    LogHelper.LogUtil.WXHOOKUI.InfoFormat("Group ID: [{0}], member count: [{1}]", currentGroup.Group_Orig_ID, currentGroup.MemberList.Count);
                    lsvGroupMember.Items.Clear();
                    lsvGroupMember.BeginUpdate();
                    foreach (FriendInfo_Entity friend_Info in currentGroup.MemberList)
                    {
                        ListViewItem lviFriend = new ListViewItem();
                        lviFriend.Text = friend_Info.Nick;
                        ListViewItem.ListViewSubItem lsvs = new ListViewItem.ListViewSubItem()
                        {
                            Text = friend_Info.Friend_Orig_ID,
                            Tag  = friend_Info.Group_Orig_ID
                        };
                        LogHelper.LogUtil.WXHOOKUI.InfoFormat("lsvs.Tag of friend_Info.Group_Orig_ID: {0}", friend_Info.Group_Orig_ID);
                        LogHelper.LogUtil.WXHOOKUI.InfoFormat("lsvs.Tag: {0}", lsvs.Tag);
                        lviFriend.SubItems.Add(lsvs);
                        lsvGroupMember.Items.Add(lviFriend);
                    }
                    lsvGroupMember.EndUpdate();
                }
                else
                {
                    var groupmemberList  = msg.Split('|');
                    int groupmemberIndex = CommonUtil.TranNull <int>(groupmemberList[0]);
                    groupmemberIndex = groupmemberIndex + 1;
                    string            groupID        = groupmemberList[1];
                    string            groupMemberMsg = string.Format("{0}|{1}", groupmemberIndex, groupID);
                    FriendInfo_Entity friendInfo     = new FriendInfo_Entity()
                    {
                        Group_Orig_ID  = groupID,
                        Friend_Orig_ID = groupmemberList[2],
                        Friend_ID      = groupmemberList[2],
                        V_ID           = groupmemberList[4],
                        Nick           = groupmemberList[5],
                        Remark         = groupmemberList[6],
                        Sex            = groupmemberList[7]
                    };
                    currentWxInfoEntity.GroupList.ForEach(x => x.Selected = false);
                    GroupInfo_Entity currentGroup = currentWxInfoEntity.GroupList.Find(x => x.Group_Orig_ID.Equals(groupID, StringComparison.OrdinalIgnoreCase));
                    if (currentGroup != null)
                    {
                        currentGroup.Selected = true;
                        currentGroup.MemberList.Add(friendInfo);
                    }
                    SendWeDllCmd(currentWxInfoEntity, (int)WX_CMD.WX_CMD_TYPE_E_GET_WX_GROUP_MEMBER_INFO, groupMemberMsg);
                }
            }
        }
Ejemplo n.º 4
0
        static public void QueueUserWorkItem(WaitCallback callBack, object state)
        {
            MyThread tt = new MyThread(callBack, state);

            tt.ThreadPoolQueueUserWorkItem();
        }