} // recvCallBack /// <summary> /// 发送数据 /// </summary> /// <param name="frd"></param> /// <param name="strData"></param> private static void sendData(MyFriend frd, string strData) { // 获取在线用户列表的实例 Friends friends = Friends.GetInstance(); try { byte[] msg = Encoding.UTF8.GetBytes(strData); //初始化相关参数 SendCallBackParam sendCallBackParam = new SendCallBackParam(frd); //异步调用 AsyncCallback callBack = new AsyncCallback(CallBackFunc.sendCallBack); frd.socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, callBack, sendCallBackParam); } // try catch (System.Exception ex) { lock (friends) { friends.friendList.Remove(frd); } frd.Dispose(); } // catch } // sendData
} // sendData /// <summary> /// 发送消息 /// </summary> /// <param name="ar"></param> public static void sendCallBack(IAsyncResult ar) { // 初始化关键参数 Friends friends = Friends.GetInstance(); SendCallBackParam sendCallBackParam = (SendCallBackParam)ar.AsyncState; MyFriend frd = sendCallBackParam.frd; try { frd.socket.EndSend(ar); } // try catch (System.Exception ex) { lock (friends) { friends.friendList.Remove(frd); } frd.Dispose(); } // catch } // sendCallBac
} // 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