/// <summary> /// 处理用户登录的一些列操作,登陆不成功返回null,成功则返回一个LoginReturn /// </summary> /// <param name="msg"></param> /// <returns></returns> private static LoginReturn UserLogin(LoginData msg, byte[] login_ip) { //业务逻辑: //第一步,检测是否满足登陆条件,如果已经上线呵呵NULL,满足则进入第二步,否则返回NULL //第二步,将自己加入在线列表,并将自己在线的消息通知给在线好友 //第三步获取对登陆者来说要进行更新的好友列表并返回LoginReturn if (isdebug) { Console.WriteLine("{0}--{1}", msg.username, msg.password); } LoginReturn info_return = new LoginReturn(); User temp_user = new User(); int login_id = SqlManager.checkUser(msg.username, msg.password, ref temp_user); if (login_id != -1)//是否满足登陆条件 { if (isOnline(login_id)) { if (isdebug) { Console.WriteLine("已登录{0}", login_id); } return(null); } if (isdebug) { Console.WriteLine("登录验证成功,用户id是{0}", login_id); } try { //登记在线 //Idonline_Dic.Add(login_id,init_Vitality); OnlineUserManager.addOnlineUser(new OnlineUser(login_id, login_ip, temp_user.name, temp_user.nickname)); //记录ip // Ip_Dic.Add(login_id,login_ip); //获取在线好友列表(用于返回给该用户),通知在线好友该用户在线了 info_return.OnlineFriends = getOnlineFriends(login_id); //获取该用户的消息,一个用户刚刚上线,应该从数据库获取才对 info_return.message = OnlineUserManager.user_list[login_id].getUserMessageList(); info_return.FriendsTimeStamp = SqlManager.getFriendsTimestamp(login_id); info_return.GroupTimeStamp = SqlManager.getGroupTimeStamp(login_id); info_return.myTimeStamp = SqlManager.getUserTimestamp(login_id); } catch (Exception e) { Console.WriteLine("构建返回值时出现问题,{0}", e.Message); } } else { if (isdebug) { Console.WriteLine("if({0}<0) then 密码搞错了 else 已经登录过了", login_id); } info_return = null; } return(info_return); }
/// <summary> /// 服务器监听函数 /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private static void server_accept(object sender, ServerAcceptEventArgs args) { NetworkStream Ns = args.client.GetStream(); var Formatter = new BinaryFormatter(); Object obj = Formatter.Deserialize(Ns); var Message = (UserMessage)obj; byte[] current_client_ip = UserExt.IPStringToByte(((System.Net.IPEndPoint)(args.client).Client.RemoteEndPoint).Address.ToString()); if (isdebug) { Console.WriteLine("Client accept!"); Console.WriteLine("client ip:" + UserExt.IPByteToString(current_client_ip)); } //获得数据,不同请求类型对应不同的数据 Object Data = Message.data; //定义返回数据 Object ReturnObject = null; switch (Message.type) { case 1: //注册功能实现 //添加个人信息至数据库 //返回id给客户端done ReturnObject = UserRegist((RegisterData)Data); break; case 2: //登陆功能实现,done ReturnObject = UserLogin((LoginData)Data, current_client_ip); break; case 3: //确认在线,//更新活力值接收在线消息,done ReturnObject = HoldLineAndGetMessage((int)Data); break; case 4: //邀请好友使用某插件 ReturnObject = InviteFriendToUsePlugin((List <int>)Data); break; case 5: //注册用户名校验,done ReturnObject = CheckUsernameRegistable((string)Data); break; case 6: //搜索符合条件的好友,目前只是按全名字匹配搜索 = = List <User> list_u = SearchUserByName(Data as string); foreach (var item in list_u) { if (isOnline(item.user_id)) { item.ip = OnlineUserManager.user_list[item.user_id].IP; } } ReturnObject = list_u; break; case 7: //添加好友 ReturnObject = AskToBeFriend((AddFriendData)Data); break; case 8: //用户更新资料 ReturnObject = UserInfoSlefUpdate((User)Data); break; case 9: //修改密码 ,如果原密码正确才进行修改密码操作,否则返回-1 ReturnObject = UserPasswordModify((UpdatePasswordData)Data); break; case 10: //获取指定id用户的所有信息 User ur = SqlManager.getUserById((int)Data, true); if (isOnline(ur.user_id)) { ur.ip = OnlineUserManager.user_list[ur.user_id].IP; } ReturnObject = ur; break; case 11: //根据id获取群组的的数据信息 ReturnObject = SqlManager.getGroupById((int)Data); break; case 12: //批量获取用户和群组的信息 ReturnObject = "尼玛什么都没有行了吧"; break; case 13: //处理同意加某人为好友 myid friendid ReturnObject = OnlineUserManager.dealToMakeFriend((int[])Data); break; case 14: //确认已经读过某消息 ReturnObject = ConfirnUserMessage((int[])Data); break; case 15: //创建新群组 List <object> temp_pram = (List <object>)Data; ReturnObject = SqlManager.createAGroup((int)(temp_pram[0]), (String)(temp_pram[1]), (String)(temp_pram[2])); break; } try { Formatter.Serialize(Ns, ReturnObject);//将要返回的对象串行化并放入数据流 } catch (Exception e) { Console.WriteLine("{0}-唉,{1}", Message.type, e.Message); } }