Exemplo n.º 1
0
        /// <summary>
        /// 回调函数:接受消息
        /// </summary>
        /// <param name="ar">the ar</param>
        public static void recCallBack(IAsyncResult ar)
        {
            try
            {
                // 接受消息的缓冲槽
                byte[] recvBuffer = (byte[])ar.AsyncState;

                // 当前用户的实例
                User currentUser = User.GetInstance();

                // 接受到的每一条消息
                RecvdMsg recvMsg = new RecvdMsg();

                // 消息列表实例
                MsgList msgList = MsgList.GetInstance();

                try
                {
                    int    i        = currentUser.socket.EndReceive(ar);
                    string str_data = Encoding.UTF8.GetString(recvBuffer, 0, i);

                    // 解析数据
                    C2CAnayMsg c2cAnayMsg = new C2CAnayMsg(str_data);

                    // 初始化收到信息类
                    recvMsg.type     = c2cAnayMsg.type;
                    recvMsg.userName = c2cAnayMsg.srcUser;
                    recvMsg.srcIP    = c2cAnayMsg.srcIP;
                    recvMsg.destName = c2cAnayMsg.destName;
                    recvMsg.content  = c2cAnayMsg.content;

                    if (recvMsg.type != null)
                    {
                        msgList.recvMsgList.Add(recvMsg);
                    } // if

                    recvBuffer = new byte[currentUser.socket.SendBufferSize];

                    //循环接收
                    AsyncCallback callBack = new AsyncCallback(recCallBack);

                    //异步调用
                    currentUser.socket.BeginReceive(recvBuffer, 0, recvBuffer.Length, SocketFlags.None, callBack, recvBuffer);//开始从连接的Socket中接收数据
                } // try

                catch (System.Exception ex)
                {
                    if (currentUser.socket != null)
                    {
                        SocketHandler.ShutDownConn(currentUser.socket);
                    } // if
                }     /// catch
            }         // try

            catch (System.Exception ex)
            {
                // 封装客户端停止连接时的异常
            } // catch
        }     // recCallBack
Exemplo n.º 2
0
        }     // acceptCallBack

        /// <summary>
        /// 接受消息
        /// </summary>
        /// <param name="ar"></param>
        public static void recvCallBack(IAsyncResult ar)
        {
            try
            {
                // 初始化关键参数
                RecvCallBackParam          recvCallBackParam = (RecvCallBackParam)ar.AsyncState;
                ListBox                    lstBox_statu      = recvCallBackParam.lstBox_statu;
                MyFriend                   frd          = recvCallBackParam.frd;
                DelegateMem.AppendDelegate appendString = recvCallBackParam.appendString;
                bool    IsStart = recvCallBackParam.IsStart;
                Friends friends = Friends.GetInstance();

                try
                {
                    int i = frd.socket.EndReceive(ar);
                    if (i == 0)
                    {
                        lock (friends)
                        {
                            friends.friendList.Remove(frd);
                        }
                        frd.Dispose();
                        return;
                    } // if

                    else
                    {
                        string strData = Encoding.UTF8.GetString(frd.recvBuffer, 0, i);

                        // 解析收到的数据
                        C2CAnayMsg c2cAnayMsg = new C2CAnayMsg(strData);
                        #region 登录
                        if (c2cAnayMsg.type == "login")
                        {
                            frd.userName = c2cAnayMsg.srcUser;
                        } // if-login
                        #endregion
                        #region 群聊
                        if (c2cAnayMsg.type == "group")
                        {
                            string finalData = string.Format("收到来自:{0},发送给:{1} 的消息: {2}", c2cAnayMsg.srcUser, c2cAnayMsg.destName, c2cAnayMsg.content);
                            lstBox_statu.Invoke(appendString, finalData, lstBox_statu);
                            foreach (MyFriend destFrd in friends.friendList)
                            {
                                if (destFrd.userName != c2cAnayMsg.srcUser)
                                {
                                    string groupMsg = BuildMsg.buildSendMsg("group", c2cAnayMsg.srcUser, c2cAnayMsg.srcIP, c2cAnayMsg.destName, c2cAnayMsg.content);
                                    sendData(destFrd, groupMsg);
                                }
                            }
                        }
                        #endregion
                        #region 点对点聊天
                        if (c2cAnayMsg.type == "client2client")
                        {
                            string finalData = string.Format("收到来自:{0},发送给:{1} 的消息: {2}", c2cAnayMsg.srcUser, c2cAnayMsg.destName, c2cAnayMsg.content);

                            lstBox_statu.Invoke(appendString, finalData, lstBox_statu);

                            // 寻找目的frd
                            int destUserIndex = 0;
                            for (destUserIndex = 0; destUserIndex < friends.friendList.Count; destUserIndex++)
                            {
                                MyFriend destFrd = (MyFriend)friends.friendList[destUserIndex];
                                if (destFrd.userName == c2cAnayMsg.destName)
                                {
                                    break;
                                }
                            } // for

                            if (destUserIndex == friends.friendList.Count)
                            {
                                // TODO 通知发送方,对方不在线
                                int srcUserIndex = 0;
                                for (srcUserIndex = 0; srcUserIndex < friends.friendList.Count; srcUserIndex++)
                                {
                                    MyFriend destFrd = (MyFriend)friends.friendList[srcUserIndex];
                                    if (destFrd.userName == c2cAnayMsg.srcUser)
                                    {
                                        break;
                                    }
                                } // for

                                // 这种情况,客户端收到后,srcUser是自己的名字,而destName不是自己的名字
                                string offlineMsg = BuildMsg.buildSendMsg("offline", c2cAnayMsg.srcUser, c2cAnayMsg.srcIP, c2cAnayMsg.destName, "对方未在线");
                                sendData((MyFriend)friends.friendList[srcUserIndex], offlineMsg);
                            } // if

                            else
                            {
                                // 转发出去 这种情况,转发给另外一个用户,src是发送者的名字,destName是自己的名字
                                string onlineMsg = BuildMsg.buildSendMsg("client2client", c2cAnayMsg.srcUser, c2cAnayMsg.srcIP, c2cAnayMsg.destName, c2cAnayMsg.content);
                                sendData((MyFriend)friends.friendList[destUserIndex], onlineMsg);
                            } // else
                        }     // if-lient2client
                        #endregion
                        #region 退出
                        if (c2cAnayMsg.type == "logout")
                        {
                            //  TDO 已处理
                            string quitMsg = frd.userName + "(" + frd.socket.LocalEndPoint + ")断开了连接";
                            lstBox_statu.Invoke(appendString, quitMsg, lstBox_statu);
                            lock (friends)
                            {
                                friends.friendList.Remove(frd);
                            }
                            frd.Dispose();
                        } // if
                        #endregion
                        #region 添加好友
                        if (c2cAnayMsg.type == "addfrd")
                        {
                            int destUserIndex = 0;
                            for (destUserIndex = 0; destUserIndex < friends.friendList.Count; destUserIndex++)
                            {
                                MyFriend destFrd = (MyFriend)friends.friendList[destUserIndex];
                                if (destFrd.userName == c2cAnayMsg.destName)
                                {
                                    break;
                                } // if
                            }     // for

                            // 被添加的用户肯定存在
                            string onlineMsg = BuildMsg.buildSendMsg("addfrd", c2cAnayMsg.srcUser, c2cAnayMsg.srcIP, c2cAnayMsg.destName, c2cAnayMsg.content);
                            sendData((MyFriend)friends.friendList[destUserIndex], onlineMsg);
                        }
                        #endregion
                        frd.clearBuffer();
                        AsyncCallback callBack = new AsyncCallback(recvCallBack);
                        frd.socket.BeginReceive(frd.recvBuffer, 0, frd.recvBuffer.Length, SocketFlags.None, callBack, recvCallBackParam);
                    } // else
                }     // try

                catch (System.Exception ex)
                {
                    lock (friends)
                    {
                        friends.friendList.Remove(frd);
                    }
                    frd.Dispose();
                } // catch
            }     // try

            catch (System.Exception ex)
            {
            } // cath
        }     // recvCallBack