/// <summary> /// 返回战斗信息 /// </summary> /// <param name="result">接受消息</param> /// <returns>返回战斗信息</returns> public UserAndReviewInfo returnUserAndReviewInfo(byte[] result) { UserAndReviewInfo reviewInfo = new UserAndReviewInfo(); reviewInfo.ReadByBytes(result); return(reviewInfo); }
static void Main(string[] args) { Client c = new Client(IpStr, port); c.SendMsgAskUserInfo("zbm"); c.SendMsgAskReviewInfo("zbm"); c.SendMsgAskMatchInfo(); c.Receive(); UserInfo userInfo = new UserInfo(); userInfo = c.getReceiveUserInfo(); Console.WriteLine("userInfo: username:{0},password:{1}", userInfo.username, userInfo.password); UserAndReviewInfo reviewInfo = new UserAndReviewInfo(); reviewInfo = c.getReceiveReviewInfo(); Console.WriteLine("battleNum:{0},victoryNum:{1},escapeNum:{2},singlehighestScore:{3},totalScore:{4}", reviewInfo.battleNum, reviewInfo.victoryNum, reviewInfo.escapeNum, reviewInfo.singlehighestScore, reviewInfo.totalScore); bool isMatch = false; while (!isMatch) { Console.WriteLine("匹配失败"); c.SendMsgAskMatchInfo(); c.Receive(); isMatch = c.getReceiveIsMatch(); Console.WriteLine("IsMatch:{0}", isMatch); } Console.ReadKey(); }
/// <summary> /// 发送更新战斗信息的请求 /// </summary> /// <param name="reviewInfo">战斗信息</param> public void SendMsgAskUpdateReviewInfo(UserAndReviewInfo reviewInfo) { int request = CommandParam.UPDATE_REQUEST; try { byte[] sender = reviewInfo.WriteAsBytes(CommandParam.UPDATE_REQUEST); streamToServer.Write(sender, 0, sender.Length); Console.WriteLine("Send:{0} is success", request); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } }
/// <summary> /// 处理接受的数据 /// </summary> /// <param name="buffer">接受的数据</param> public void doDealMsg(byte[] buffer) { IAsyncResult ar; ByteBuffer temp = new ByteBuffer(buffer); int reply = temp.ReadInt(); switch (reply) { case CommandParam.LOGIN_SUCCESS_REPLY: //登陆成功 del_UserInfo = new ReturnUserInfoDelegate(returnInfos.returnUserInfo); ar = del_UserInfo.BeginInvoke(buffer, null, null); userInfo = del_UserInfo.EndInvoke(ar); if (LoginEvent != null) { LoginEvent(userInfo); } break; case CommandParam.LOGIN_FAIL_REPLY: //登陆失败 userInfo = null; if (LoginEvent != null) { LoginEvent(userInfo); } Console.WriteLine("用户不存在"); break; case CommandParam.LOADING_SUCCESS_REPLY: //载入成功 del_UserAndReviewInfo = new ReturnUserAndReviewInfoDelegate(returnInfos.returnUserAndReviewInfo); ar = del_UserAndReviewInfo.BeginInvoke(buffer, null, null); userAndReviewInfo = del_UserAndReviewInfo.EndInvoke(ar); if (LoadEvent != null) { LoadEvent(userAndReviewInfo); } break; case CommandParam.UPDATE_FAIL_REPLY: //更新失败 del_IsUpdate = new ReturnIsUpdate(returnInfos.returnIsUpdate); ar = del_IsUpdate.BeginInvoke(buffer, null, null); IsUpdate = del_IsUpdate.EndInvoke(ar); break; case CommandParam.MATCH_SUCCESS_REPLY: //匹配成功 del_IsMatch = new ReturnIsMatch(returnInfos.returnIsMatchInfo); ar = del_IsMatch.BeginInvoke(buffer, null, null); IsMatch = del_IsMatch.EndInvoke(ar); if (MatchEvent != null) { MatchEvent(IsMatch); } break; case CommandParam.MATCH_FAIL_REPLY: //匹配失败 del_IsMatch = new ReturnIsMatch(returnInfos.returnIsMatchInfo); ar = del_IsMatch.BeginInvoke(buffer, null, null); IsMatch = del_IsMatch.EndInvoke(ar); if (MatchEvent != null) { MatchEvent(IsMatch); } break; case CommandParam.BATTLE_REPLY: //战斗回应 del_BattleInfo = new ReturnBattleInfo(returnInfos.returnBatteInfo); ar = del_BattleInfo.BeginInvoke(buffer, null, null); battleInfo = del_BattleInfo.EndInvoke(ar); if (BattleEvent != null) { BattleEvent(battleInfo); } break; default: del_IsOnline = new ReturnIsOnline(returnInfos.returnIsOnline); ar = del_IsOnline.BeginInvoke(buffer, null, null); IsOnline = del_IsOnline.EndInvoke(ar); break; } }