private void OnConnected(long conn_idx) { Log.Info("open conn session:" + conn_idx); ConnAppProc app_server; if (m_app_servers.TryGetValue(conn_idx, out app_server)) { Log.Info("发送内部服务器连接请求:" + app_server.srv_info.srv_type); inner.ReqLogin msg = PacketPools.Get(inner.msg.REQ_LOGIN) as inner.ReqLogin; msg.srv_info.srv_type = eServerType.FIGHT; msg.srv_info.srv_uid = m_srv_uid; msg.srv_info.srv_endpoint.ip = SocketUtils.IpToInt(ServerConfig.net_info.ip_for_server);//告诉其他服,战斗服的ip和端口 msg.srv_info.srv_endpoint.port = ServerConfig.net_info.port_for_server; this.Send(conn_idx, msg); } else { Log.Warning("未找到连接:" + conn_idx); } }
/// <summary> /// 网络事件处理 /// </summary> public void OnNetworkServer(ushort header, ByteArray data) { PacketBase packet = PacketPools.Get(header); packet.Read(data); MsgProcFunction fun; if (m_msg_proc.TryGetValue(packet.header, out fun)) { try { fun(packet); } catch (Exception e) { Log.Exception(e); } } PacketPools.Recover(packet); }
/// <summary> /// 加密 /// </summary> private void OnClientEncrypt(long conn_idx, PacketBase packet) { if (!ClientSessionManager.Instance.HasAcceptSession(conn_idx)) { return; } c2gs.EncryptInfo msg = packet as c2gs.EncryptInfo; //版本验证 byte main_version; byte sub_version; ushort revision_version; GlobalID.SplitVersion(msg.version, out main_version, out sub_version, out revision_version); if (main_version != GlobalID.VERSION_MAIN) { ClientSessionManager.Instance.KickoutSession(conn_idx); return; } GlobalID.SetClientVersion(msg.version); //收到正常的握手协议后,才加入正式session列表 ClientSession session = ClientSessionManager.Instance.AddSession(conn_idx); if (session != null) { ClientSessionManager.Instance.CleanupAcceptSession(conn_idx); session.session_status = eSessionStatus.CREATED; gs2c.EncryptInfo rep_msg = PacketPools.Get(gs2c.msg.ENCRYPT) as gs2c.EncryptInfo; rep_msg.key = PacketEncrypt.Encrypt_Key; rep_msg.flags = 0; session.Send(rep_msg); } else { ClientSessionManager.Instance.KickoutSession(conn_idx); } }
/// <summary> /// 读邮件 /// </summary> public void HandleReadMail(long mail_idx) { Player player = UnitManager.Instance.GetUnitByIdx(m_char_idx) as Player; if (player == null) { return; } MailInfo mail_info; if (m_all_mails.TryGetValue(mail_idx, out mail_info)) { if (m_del_mails.Contains(mail_idx)) { return; } //是否全服邮件 if (mail_info.receiver_idx == 0) { if (!m_had_recv_mails.Contains(mail_idx)) {//同步到db,已经领取过全局邮件 m_had_recv_mails.Add(mail_idx); SQLMailHandle.UpdateCharRecvs(m_char_idx, m_had_recv_mails, player.db_id); } } else if (!Utils.HasFlag(mail_info.flags, (uint)eMailFlags.READED)) {//修改标记 mail_info.flags = Utils.InsertFlag(mail_info.flags, (uint)eMailFlags.READED); m_save_mails.Add(mail_idx); m_is_dirty = true; } //通知客户端 ss2c.MailRead msg_client = PacketPools.Get(ss2c.msg.MAIL_READ) as ss2c.MailRead; msg_client.mail_info.Copy(mail_info); ServerNetManager.Instance.SendProxy(player.client_uid, msg_client); } }
private void ProcessWrite(MailWriteInfo info) { PlayerCache player = PlayerCacheManager.Instance.GetMember(m_char_idx); if (player == null) { return; } if (info.receiver_idx <= 0) { Log.Warning("错误的收件人id:" + info.receiver_idx); return; } //存db MailInfo mail_info = CommonObjectPools.Spawn <MailInfo>(); mail_info.mail_type = eMailType.NORMAL; mail_info.spid = 0;//普通邮件,不区分渠道id mail_info.receiver_idx = info.receiver_idx; mail_info.sender_idx = m_char_idx; mail_info.sender_name = player.ss_data.char_name; mail_info.send_time = Time.second_time; mail_info.expire_time = 0; mail_info.delivery_time = 0; mail_info.flags = (uint)eMailFlags.NONE; mail_info.subject = info.subject; mail_info.bin_mail_content.content_type = eMailContentType.NORMAL; mail_info.bin_mail_content.bin_normal_content.content = info.content; SQLMailHandle.CreateMail(mail_info); CommonObjectPools.Despawn(mail_info); //返回结果给ss db2ss.MailCommand rep_msg = PacketPools.Get(db2ss.msg.MAIL_COMMAND) as db2ss.MailCommand; rep_msg.mail_idx = 0; rep_msg.command_type = eMailCommandType.WRITE_MAIL; rep_msg.error_type = eMailCommandError.NONE; ForServerNetManager.Instance.Send(player.ss_uid, rep_msg); }
private void SendAOIMove(long char_idx, long move_unit_idx, int row, int col) { Player player = UnitManager.Instance.GetUnitByIdx(char_idx) as Player; if (player == null) { return; } Unit move_unit = UnitManager.Instance.GetUnitByIdx(move_unit_idx); if (move_unit == null) { return; } ss2c.UnitMove rep_msg = PacketPools.Get(ss2c.msg.UNIT_MOVE) as ss2c.UnitMove; rep_msg.unit_idx.Set(move_unit.unit_type, 0, move_unit.obj_idx); rep_msg.pos.Set(col, row); rep_msg.flags = 0; ServerNetManager.Instance.SendProxy(player.client_uid, rep_msg, false); PacketPools.Recover(rep_msg); }
/// <summary> /// 登录 /// </summary> private void OnClientLogin(PacketBase packet) { ws2gs.ClientLogin msg = packet as ws2gs.ClientLogin; ///1.查询完成,确认这个过程中是否已经退出 ClientSession session = ClientSessionManager.Instance.GetSession(msg.client_uid.conn_idx); if (session == null) { return; } ///2.修改状态 if (msg.login_result == eLoginResult.E_SUCCESS) { //更新数据 session.account_idx = msg.account_idx; session.spid = msg.spid; session.ss_uid = msg.ss_uid; session.session_status = eSessionStatus.ALREADY_LOGIN; ClientSessionManager.Instance.AddSessionByAccount(session.account_idx, session.conn_idx); } else { session.session_status = eSessionStatus.LOGIN_FAILED; //是否超过验证次数:是的话直接踢号 session.login_error_count += 1; if (session.login_error_count >= 5) { ClientSessionManager.Instance.KickoutSession(session.conn_idx); return; } } ///3.告诉客户端 gs2c.ClientLogin rep_msg = PacketPools.Get(gs2c.msg.CLIENT_LOGIN) as gs2c.ClientLogin; rep_msg.login_result = msg.login_result; session.Send(rep_msg); }
private void OnConnected(long conn_idx) { Log.Info("Open session:" + conn_idx); ConnAppProc app_server; if (m_app_servers.TryGetValue(conn_idx, out app_server)) { Log.Info("发送内部服务器连接请求:" + app_server.srv_info.srv_type); inner.ReqLogin msg = PacketPools.Get(inner.msg.REQ_LOGIN) as inner.ReqLogin; msg.srv_info.srv_type = eServerType.SERVER; msg.srv_info.srv_realm_idx = m_srv_realm_idx; msg.srv_info.srv_uid = m_srv_uid; msg.srv_info.srv_endpoint.ip = 0;//逻辑服只连接其他服,不需要设置 msg.srv_info.srv_endpoint.port = 0; this.Send(conn_idx, msg); } else { Log.Warning("未找到连接:" + conn_idx); } }
/// <summary> /// 移除关系 /// </summary> public void RemoveRelationCommand(long target_char_idx) { //不在线了 Unit player = UnitManager.Instance.GetUnitByIdx(m_char_idx); if (player == null) { return; } //是否已经存在关系 RelationInfo relation_info = null; if (m_relations.TryGetValue(target_char_idx, out relation_info)) { this.RemoveRelation(target_char_idx); //通知逻辑服,由逻辑服转发给客户端 gl2ss.RelationRemove msg_remove = PacketPools.Get(gl2ss.msg.RELATION_REMOVE) as gl2ss.RelationRemove; msg_remove.char_idx = m_char_idx; msg_remove.target_idx = target_char_idx; ForServerNetManager.Instance.Send(player.ss_srv_uid, msg_remove); //添加到db,需要先判断数据库是否已经有写入过,防止重复写入 SQLRelationHandle.QueryExistsRelationEvent(m_char_idx, target_char_idx, relation_info.flags, (event_idx) => { if (event_idx == 0 && m_char_idx > 0) {//保存事件 RelationEventInfo e_info = new RelationEventInfo(); e_info.target_char_idx = target_char_idx; e_info.source_char_idx = m_char_idx; e_info.event_type = eRelationEvent.Delete; SQLRelationHandle.InsertRelationEvent(e_info); //立刻通知接受者 this.BroadcastEvent(e_info.target_char_idx, e_info.event_type); } }); } }
/// <summary> /// 处理登出逻辑 /// </summary> private void HandleLogoutAccount(ClientUID client_uid) { Player player = UnitManager.Instance.GetPlayerByClientUID(client_uid) as Player; if (player == null) {//如果在加载完角色信息前退出,则不会有unit return; } long char_idx = player.char_idx; ushort fs_uid = player.fs_uid; //从场景移除 BaseScene scene = SceneManager.Instance.GetScene(player.scene_obj_idx); if (scene != null) { scene.RemoveUnit(player); } //从管理器移除 UnitManager.Instance.RemoveUnit(player); //告诉fs ss2fs.LogoutClient fs_msg = PacketPools.Get(ss2fs.msg.LOGOUT_CLIENT) as ss2fs.LogoutClient; fs_msg.char_idx = char_idx; ServerNetManager.Instance.Send2FS(fs_uid, fs_msg); //告诉ws ss2ws.LogoutClient ws_msg = PacketPools.Get(ss2ws.msg.LOGOUT_CLIENT) as ss2ws.LogoutClient; ws_msg.char_idx = char_idx; ServerNetManager.Instance.Send2WS(ws_msg); //告诉gl ss2gl.LogoutClient gl_msg = PacketPools.Get(ss2gl.msg.LOGOUT_CLIENT) as ss2gl.LogoutClient; gl_msg.char_idx = char_idx; ServerNetManager.Instance.Send2GL(gl_msg); }
/// <summary> /// 压力测试 /// </summary> private void OnClientRobotTest(ClientSession session, PacketBase packet) { c2gs.RobotTest msg = packet as c2gs.RobotTest; if (Utils.HasFlag(msg.flags, (uint)eServerType.WORLD)) { gs2ws.RobotTest rep_msg = PacketPools.Get(gs2ws.msg.ROBOT_TEST) as gs2ws.RobotTest; rep_msg.client_uid = session.client_uid; rep_msg.length = msg.length; ServerNetManager.Instance.Send2WS(rep_msg); } if (Utils.HasFlag(msg.flags, (uint)eServerType.SERVER)) { gs2ss.RobotTest rep_msg = PacketPools.Get(gs2ss.msg.ROBOT_TEST) as gs2ss.RobotTest; rep_msg.client_uid = session.client_uid; rep_msg.length = msg.length; session.Send2SS(rep_msg); } if (msg.flags == 0 || Utils.HasFlag(msg.flags, (uint)eServerType.GATE)) { gs2c.RobotTest rep_msg = PacketPools.Get(gs2c.msg.ROBOT_TEST) as gs2c.RobotTest; rep_msg.length = msg.length; session.Send(rep_msg); } }
private void OnCharacterInfoLoaded(ClientUID client_uid, InterServerID server_uid, long char_idx) { PlayerCache member = PlayerCacheManager.Instance.GetMember(char_idx); if (member == null) { Log.Warning("OnPlayerInfoLoaded - 未找到玩家缓存信息:" + char_idx); return; } //设置服务器信息 member.client_uid = client_uid; member.ss_uid = server_uid.ss_uid; member.UpdateAttribute(eUnitModType.UMT_time_last_login, Time.second_time); //返回数据给ss db2ss.CharacterInfo rep_msg = PacketPools.Get(db2ss.msg.CHARACTER_INFO) as db2ss.CharacterInfo; rep_msg.server_uid = server_uid; rep_msg.client_uid = client_uid; rep_msg.data.Copy(member.ss_data); this.Send(rep_msg); //读取邮件 MailCacheManager.Instance.LoadMailbox(member.char_idx); }
private void SendAOIEnter(long char_idx, long enter_unit_idx, int row, int col) { Player player = UnitManager.Instance.GetUnitByIdx(char_idx) as Player; if (player == null) { return; } Unit enter_unit = UnitManager.Instance.GetUnitByIdx(enter_unit_idx); if (enter_unit == null) { return; } //广播aoi消息 ss2c.UnitEnterAOI rep_msg = PacketPools.Get(ss2c.msg.ENTER_AOI) as ss2c.UnitEnterAOI; rep_msg.unit_idx.Set(enter_unit.unit_type, 0, enter_unit.obj_idx); rep_msg.pos = new Position2D(col, row); rep_msg.dir = eDirection.TOP; rep_msg.flags = 0; rep_msg.unit_info = enter_unit.GetUnitAOIInfo(); ServerNetManager.Instance.SendProxy(player.client_uid, rep_msg, false); }
/// <summary> /// 网络事件处理 /// </summary> public void OnNetworkClient(long conn_idx, ushort header, ByteArray data) { ClientSession session = ClientSessionManager.Instance.GetSession(conn_idx); if (session != null) { ///1.判断cd if (!session.CheckCooldown(header)) { //Log.Debug("协议cd不够:" + header); return; } ///2.消息解密 if (GlobalID.ENABLE_PACKET_ENCRYPT) { //id有效性校验 uint packet_idx = data.ReadUInt(); packet_idx = PacketEncrypt.DecrpytPacketIndex(packet_idx, PacketEncrypt.Encrypt_Key); if (packet_idx < session.last_packet_idx) { Log.Warning("协议索引校验失败conn_idx:" + conn_idx + " header:" + header); ClientSessionManager.Instance.KickoutSession(conn_idx); return; } session.last_packet_idx = packet_idx; //验证数据的有效性:防止被修改 ushort send_verify_data = data.ReadUShort(); ushort verify_data = PacketEncrypt.CalcPacketDataVerify(data.Buffer, 6, data.Available, packet_idx, PacketEncrypt.Encrypt_Key); if (send_verify_data != verify_data) { Log.Warning("数据有效性校验失败conn_idx:" + conn_idx + " header:" + header); ClientSessionManager.Instance.KickoutSession(conn_idx); return; } } ///3.是否转发消息 if (header >= ProtocolID.MSG_BASE_C2SS && header < ProtocolID.MSG_BASE_C2SS + ProtocolID.MSG_APPLAYER_PER_INTERVAL) { HandleProxyMsgToSS(session, header, data); } else if (header >= ProtocolID.MSG_BASE_C2WS && header < ProtocolID.MSG_BASE_C2WS + ProtocolID.MSG_APPLAYER_PER_INTERVAL) { HandleProxyMsgToWS(session, header, data); } else if (header >= ProtocolID.MSG_BASE_C2FS && header < ProtocolID.MSG_BASE_C2FS + ProtocolID.MSG_APPLAYER_PER_INTERVAL) { HandleProxyMsgToFS(session, header, data); } else { PacketBase packet = PacketPools.Get(header); packet.Read(data); MsgProcFunction fun; if (m_msg_proc.TryGetValue(packet.header, out fun)) { try { fun(session, packet); } catch (Exception e) { Log.Exception(e); } } else { Log.Warning("未注册消息处理函数id:" + header); } PacketPools.Recover(packet); } } else { PacketBase packet = PacketPools.Get(header); packet.Read(data); switch (packet.header) { case c2gs.msg.ENCRYPT: OnClientEncrypt(conn_idx, packet); break; default: //没有session,又不是握手协议,说明发错协议了,直接踢掉 ClientSessionManager.Instance.KickoutSession(conn_idx); break; } PacketPools.Recover(packet); } }
/// <summary> /// 收取邮件附件 /// </summary> public static void SendTakeMail(long mail_idx) { c2ss.MailTake msg = PacketPools.Get(c2ss.msg.MAIL_TAKE) as c2ss.MailTake; msg.mail_idx = mail_idx; ClientNetManager.Instance.Send(msg); }
/// <summary> /// 移除关系 /// </summary> public static void SendRelationRemove(long char_idx) { c2ss.RelationRemove msg = PacketPools.Get(c2ss.msg.RELATION_REMOVE) as c2ss.RelationRemove; msg.target_char_idx = char_idx; ClientNetManager.Instance.Send(msg); }
/// <summary> /// 添加关系 /// </summary> /// <param name="target">目标</param> /// <param name="flag">关系标记</param> public void AddRelationClient(RelationAddTarget target, eRelationFlag flag, string message) { Player player = UnitManager.Instance.GetUnitByIdx(m_char_idx) as Player; if (player == null) { return; } //超过上限 if (IsRelationFull(flag)) { return; } //判断是否已经存在关系 if (target.type == eRelationAddType.Idx) { RelationInfo relation_info; if (m_relations.TryGetValue(target.char_idx, out relation_info)) { //已经存在相同关系,或黑名单状态加好友 if (relation_info.flags == flag || (relation_info.flags == eRelationFlag.Block && flag == eRelationFlag.Friend)) { return; } } } else { foreach (var relation_info in m_relations) { if (relation_info.Value.char_name == target.char_name) { //已经存在相同关系,或黑名单状态加好友 if (relation_info.Value.flags == flag || (relation_info.Value.flags == eRelationFlag.Block && flag == eRelationFlag.Friend)) { return; } } } } //是否已经请求过 long last_add_time = 0; if (m_add_relations.TryGetValue(target, out last_add_time)) { if (Time.timeSinceStartup - last_add_time < relation.ADD_RELATION_TIME_OFFSET * 1000) { return; } m_add_relations.Remove(target); } //发送到gl ss2gl.RelationAdd msg = PacketPools.Get(ss2gl.msg.RELATION_ADD) as ss2gl.RelationAdd; msg.char_idx = m_char_idx; msg.target_id = target; msg.flag = flag; msg.message = message; ServerNetManager.Instance.Send2GL(msg); //添加到请求队列 m_add_relations.Add(target, Time.timeSinceStartup); }
/// <summary> /// 请求服务器时间 /// </summary> public static void SendServerTime() { c2ws.ServerTime msg = PacketPools.Get(c2ws.msg.SERVER_TIME) as c2ws.ServerTime; ClientNetManager.Instance.Send(msg); }
/// <summary> /// 删除邮件 /// </summary> public static void SendDeleteMail(long mail_idx) { c2ss.MailDelete msg = PacketPools.Get(c2ss.msg.MAIL_DELETE) as c2ss.MailDelete; msg.mail_list.Add(mail_idx); ClientNetManager.Instance.Send(msg); }
/// <summary> /// 处理关系事件 /// </summary> private void HandleRelationEvent(List <RelationEventInfo> list_event) { //查询过程中如果下线 if (m_char_idx == 0) { return; } Unit player = UnitManager.Instance.GetUnitByIdx(m_char_idx); if (player == null) { return; } foreach (var relation_evt in list_event) { switch (relation_evt.event_type) { case eRelationEvent.Add: //对方申请加你好友 { //超过上限 if (IsRelationFull(relation_evt.bin_content.bin_add_content.flag)) { //清除处理过的事件 SQLRelationHandle.RemoveRelationEvent(relation_evt.event_idx); continue; } //是否已经存在相同关系 RelationInfo relation_info = null; if (m_relations.TryGetValue(relation_evt.source_char_idx, out relation_info) && relation_info.flags == relation_evt.bin_content.bin_add_content.flag) { //清除处理过的事件 SQLRelationHandle.RemoveRelationEvent(relation_evt.event_idx); continue; } //拉黑不需要同意,所以不添加事件 if (relation_evt.bin_content.bin_add_content.flag == eRelationFlag.Block) { if (m_relations.TryGetValue(relation_evt.source_char_idx, out relation_info)) { //已经存在其他关系,则用新关系覆盖旧关系 relation_info.flags = eRelationFlag.Block; this.SyncRelation2SS(relation_evt.source_char_idx); } else { Unit target_player = UnitManager.Instance.GetUnitByIdx(relation_evt.source_char_idx); if (target_player == null) { PlayerInfoForGL data = CommonObjectPools.Spawn <PlayerInfoForGL>(); SQLCharHandle.QueryCharacterInfo(relation_evt.source_char_idx, data, (ret) => { if (ret && m_char_idx > 0) { relation_info = CommonObjectPools.Spawn <RelationInfo>(); relation_info.Copy(data); relation_info.flags = eRelationFlag.Block; this.AddRelation(relation_info); this.SyncRelation2SS(relation_evt.source_char_idx); } CommonObjectPools.Despawn(data); }); } else { relation_info = CommonObjectPools.Spawn <RelationInfo>(); relation_info.Copy(target_player.player_data); relation_info.flags = eRelationFlag.Block; this.AddRelation(relation_info); this.SyncRelation2SS(relation_evt.source_char_idx); } } //清除处理过的事件 SQLRelationHandle.RemoveRelationEvent(relation_evt.event_idx); m_relation_events.Remove(relation_evt.event_idx); } else { //添加关系事件 if (m_relation_events.ContainsKey(relation_evt.event_idx)) { continue; } else { m_relation_events.Add(relation_evt.event_idx, relation_evt); } //通知逻辑服,由逻辑服转发给客户端 gl2ss.RelationAdd msg = PacketPools.Get(gl2ss.msg.RELATION_ADD) as gl2ss.RelationAdd; msg.event_idx = relation_evt.event_idx; msg.char_idx = m_char_idx; msg.player_id.Set(relation_evt.source_char_idx, relation_evt.bin_content.bin_add_content.char_name); msg.message = relation_evt.bin_content.bin_add_content.message; msg.flag = relation_evt.bin_content.bin_add_content.flag; ForServerNetManager.Instance.Send(player.ss_srv_uid, msg); } break; } case eRelationEvent.Delete: //对方删除了你 { if (m_relations.ContainsKey(relation_evt.source_char_idx)) { this.RemoveRelation(relation_evt.source_char_idx); //通知逻辑服,由逻辑服转发给客户端 gl2ss.RelationRemove msg_remove = PacketPools.Get(gl2ss.msg.RELATION_REMOVE) as gl2ss.RelationRemove; msg_remove.char_idx = m_char_idx; msg_remove.target_idx = relation_evt.source_char_idx; ForServerNetManager.Instance.Send(player.ss_srv_uid, msg_remove); } //清除处理过的事件 SQLRelationHandle.RemoveRelationEvent(relation_evt.event_idx); m_relation_events.Remove(relation_evt.event_idx); break; } case eRelationEvent.Agree: //对方同意了你的申请 { if (relation_evt.bin_content.bin_agree_content.cmd == eRelationApplyCmd.Agree) { //如果对方在线,则取对方身上的数据;否则取数据库的数据 Unit target_player = UnitManager.Instance.GetUnitByIdx(relation_evt.source_char_idx); if (target_player != null) { this.RemoveRelation(relation_evt.source_char_idx); RelationInfo relation_info = CommonObjectPools.Spawn <RelationInfo>(); relation_info.Copy(target_player.player_data); relation_info.flags = relation_evt.bin_content.bin_agree_content.flag; this.AddRelation(relation_info); this.SyncRelation2SS(relation_evt.source_char_idx); //清除处理过的事件 SQLRelationHandle.RemoveRelationEvent(relation_evt.event_idx); m_relation_events.Remove(relation_evt.event_idx); } else { PlayerInfoForGL data = CommonObjectPools.Spawn <PlayerInfoForGL>(); SQLCharHandle.QueryCharacterInfo(relation_evt.source_char_idx, data, (ret) => { if (ret && m_char_idx > 0) { this.RemoveRelation(relation_evt.source_char_idx); RelationInfo relation_info = CommonObjectPools.Spawn <RelationInfo>(); relation_info.Copy(data); relation_info.flags = relation_evt.bin_content.bin_agree_content.flag; this.AddRelation(relation_info); this.SyncRelation2SS(relation_evt.source_char_idx); } CommonObjectPools.Despawn(data); //清除处理过的事件 SQLRelationHandle.RemoveRelationEvent(relation_evt.event_idx); m_relation_events.Remove(relation_evt.event_idx); }); } } else { //清除处理过的事件 SQLRelationHandle.RemoveRelationEvent(relation_evt.event_idx); m_relation_events.Remove(relation_evt.event_idx); } break; } case eRelationEvent.Give: { Log.Info("赠送 src:" + relation_evt.source_char_idx + " dst:" + relation_evt.target_char_idx); RelationEventContent.GiveContent give_info = relation_evt.bin_content.bin_give_content; //发给ss处理 if (item.IsValidItem(give_info.item_id)) { gl2ss.RelationGive msg_give = PacketPools.Get(gl2ss.msg.RELATION_GIVE) as gl2ss.RelationGive; msg_give.char_idx = m_char_idx; msg_give.src_player_id.Set(relation_evt.source_char_idx, give_info.char_name); msg_give.item_id = give_info.item_id; ForServerNetManager.Instance.Send(player.ss_srv_uid, msg_give); } //清除处理过的事件 SQLRelationHandle.RemoveRelationEvent(relation_evt.event_idx); m_relation_events.Remove(relation_evt.event_idx); break; } } } }
/// <summary> /// 加速检测 /// </summary> public static void SendSpeedCheck(int check_sn) { c2gs.SpeedCheck msg = PacketPools.Get(c2gs.msg.SPEED_CHECK) as c2gs.SpeedCheck; msg.check_sn = check_sn; ClientNetManager.Instance.Send(msg); }
/// <summary> /// 读邮件 /// </summary> public static void SendReadMail(long mail_idx) { c2ss.MailRead msg = PacketPools.Get(c2ss.msg.MAIL_READ) as c2ss.MailRead; msg.mail_idx = mail_idx; ClientNetManager.Instance.Send(msg); }
/// <summary> /// 处理客户端发送的聊天信息 /// </summary> public void HandleSendChat(Player player, eChatType type, CharacterIdxOrName receiver, string content) { //TODO:是否禁言 switch (type) { case eChatType.PRIVATE: //私聊 Player target = null; if (receiver.IsIdxValid()) { // 不能对自己发送 if (receiver.char_idx == player.char_idx) { return; } target = UnitManager.Instance.GetPlayerByIdx(receiver.char_idx); } else { if (receiver.char_name == player.char_name) { return; } target = UnitManager.Instance.GetPlayerByName(receiver.char_name); } if (target != null) { //如果本服玩家在线,直接发送 //检测是否在对方黑名单 if (RelationManager.Instance.HaveRelationFlag(player.char_idx, target.char_idx, eRelationFlag.Block)) { ss2c.ChatResult msg = PacketPools.Get(ss2c.msg.CHAT_RESULT) as ss2c.ChatResult; msg.error = eChatError.ERROR_DST_REFUSE; ServerNetManager.Instance.SendProxy(target.client_uid, msg); } else { //直接发给对方 ss2c.ChatRecv msg = PacketPools.Get(ss2c.msg.CHAT_RECV) as ss2c.ChatRecv; msg.type = type; msg.sender.Set(player.char_idx, player.char_name); msg.chat_content = content; ServerNetManager.Instance.SendProxy(player.client_uid, msg); } } else { //通过ws转发 ss2ws.ChatSend msg = PacketPools.Get(ss2ws.msg.CHAT_SEND) as ss2ws.ChatSend; msg.type = type; msg.sender.Set(player.char_idx, player.char_name); msg.receiver = receiver; msg.chat_content = content; ServerNetManager.Instance.Send2WS(msg); } break; case eChatType.WORLD: //世界聊天 { ss2c.ChatRecv msg = PacketPools.Get(ss2c.msg.CHAT_RECV) as ss2c.ChatRecv; msg.type = type; msg.sender.Set(player.char_idx, player.char_name); msg.chat_content = content; ServerNetManager.Instance.BroadcastProxyMsg(msg); } break; case eChatType.CURRENT: //当前聊天 break; case eChatType.GROUP: //组队聊天 break; case eChatType.GUILD: //军团聊天 break; case eChatType.INSTANCE: //战场聊天 break; } }
/// <summary> /// 请求列表 /// </summary> public static void SendRelationList() { c2ss.RelationList msg = PacketPools.Get(c2ss.msg.RELATION_LIST) as c2ss.RelationList; ClientNetManager.Instance.Send(msg); }
/// <summary> /// 请求邮件列表 /// </summary> public static void SendMailList() { c2ss.MailList msg = PacketPools.Get(c2ss.msg.MAIL_LIST) as c2ss.MailList; ClientNetManager.Instance.Send(msg); }
/// <summary> /// 投递邮件到信箱 /// </summary> private void OnLoadMailEnd(List <MailInfo> list) { if (list.Count == 0) { return; } //可能读取db时,玩家下线了 Player player = UnitManager.Instance.GetUnitByIdx(m_char_idx) as Player; if (player == null) { list.ForEach(info => CommonObjectPools.Despawn(info)); return; } //过滤掉已经领取过附件的全局邮件 for (int i = list.Count - 1; i >= 0; --i) { MailInfo mail_info = list[i]; if (mail_info.receiver_idx == 0 && m_had_recv_mails.Contains(mail_info.mail_idx)) { CommonObjectPools.Despawn(mail_info); list.RemoveAt(i); } } //添加到邮件集合 list.ForEach(info => { if (!m_all_mails.ContainsKey(info.mail_idx)) { m_all_mails.Add(info.mail_idx, info); } }); //加入到新邮件列表 list.ForEach(info => m_new_mails.Add(info.mail_idx)); //过滤掉系统内部邮件 List <long> del_list = new List <long>(); for (int i = list.Count - 1; i >= 0; i--) { MailInfo mail_info = list[i]; if (mail_info.mail_type == eMailType.SYSTEM_INTERNAL) { if (mail_info.bin_mail_content.content_type == eMailContentType.MOD) {//修改玩家属性 MailModContent content = mail_info.bin_mail_content.bin_mod_content; foreach (var obj in content.mods) { player.player_attr.SetAttrib(obj.type, obj.value_i, obj.value_str); } list.RemoveAt(i); del_list.Add(mail_info.mail_idx); } } } if (del_list.Count > 0) { this.HandleDeleteMail(del_list); } //通知客户端有新邮件到达 if (list.Count > 0) { //获取未读邮件数量 int unread_count = 0; list.ForEach(info => { if (!Utils.HasFlag(info.flags, (uint)eMailFlags.READED)) { unread_count++; } }); ss2c.MailCount msg = PacketPools.Get(ss2c.msg.MAIL_COUNT) as ss2c.MailCount; msg.total_mail_count = (ushort)list.Count; msg.unread_mail_count = (ushort)unread_count; ServerNetManager.Instance.SendProxy(player.client_uid, msg); } }
/// <summary> /// 进入场景 /// </summary> private void OnEnterScene(PacketBase packet) { c2ss.EnterScene msg = packet as c2ss.EnterScene; Player player = UnitManager.Instance.GetPlayerByClientUID(msg.client_uid); if (player == null) { Log.Debug("OnEnterScene 未找到unit:" + msg.client_uid.srv_uid + ", " + msg.client_uid.conn_idx); return; } uint scene_idx = 0; if (msg.scene_type == 0) { //进入游戏后的第一个消息:告诉client需要进入的场景id scene_idx = player.scene_type_idx; //上次所在场景 } else { //相同场景跳转,直接返回 if (msg.scene_type == player.scene_type_idx) { return; } scene_idx = msg.scene_type; } //判断场景是否有效 if (!SceneID.IsValidScene(scene_idx)) { return; } //加入场景 BaseScene scene = SceneManager.Instance.CreateScene(scene_idx); if (scene != null) { //先从旧的场景移除 BaseScene old_scene = SceneManager.Instance.GetScene(player.scene_obj_idx); if (old_scene != null) { old_scene.RemoveUnit(player); } //再加入新的场景 scene.AddUnit(player); player.unit_attr.SetAttribInteger(eUnitModType.UMT_scene_type, scene_idx); } else { Log.Warning("加入场景失败:" + scene_idx); return; } //告诉client ss2c.EnterScene rep_msg = PacketPools.Get(ss2c.msg.ENTER_SCENE) as ss2c.EnterScene; rep_msg.scene_type = scene_idx; rep_msg.scene_instance_id = 1; rep_msg.pos.Set(player.pos_x, player.pos_y);//TODO:获取场景出生点 rep_msg.dir = eDirection.NONE; ServerNetManager.Instance.SendProxy(msg.client_uid, rep_msg, false); }
private void OnMessageReveived(long conn_idx, ushort header, ByteArray data) { PacketBase packet = PacketPools.Get(header); packet.Read(data); do { ConnAppProc app_server = null; if (!m_app_servers.TryGetValue(conn_idx, out app_server)) { if (packet.header == inner.msg.REQ_LOGIN) { if (!m_app_servers.ContainsKey(conn_idx)) { inner.ReqLogin msg = packet as inner.ReqLogin; //检测是否相同id的服务器以及连接 if (m_srv_servers.ContainsKey(msg.srv_info.srv_uid)) { Log.Warning("相同服务器以及连接 sid:" + msg.srv_info.srv_uid); m_net_socket.CloseConn(conn_idx); break; } Log.Info("收到内部服务器连接请求:" + msg.srv_info.srv_type); //创建新连接 app_server = CreateConnApp(msg.srv_info.srv_type); app_server.conn_idx = conn_idx; app_server.srv_info.srv_type = msg.srv_info.srv_type; app_server.srv_info.srv_status = eConnAppStatus.CONNECTED; app_server.srv_info.srv_realm_idx = ServerConfig.net_info.server_realm; app_server.srv_info.srv_uid = msg.srv_info.srv_uid; app_server.srv_info.srv_endpoint.ip = msg.srv_info.srv_endpoint.ip; app_server.srv_info.srv_endpoint.port = msg.srv_info.srv_endpoint.port; m_app_servers.Add(conn_idx, app_server); OnConnAppEnter(app_server); //响应 inner.RepLogin rep_msg = PacketPools.Get(inner.msg.REP_LOGIN) as inner.RepLogin; rep_msg.result = inner.RepLogin.eResult.SUCCESS; rep_msg.srv_info.srv_type = eServerType.WORLD; rep_msg.srv_info.srv_realm_idx = ServerConfig.net_info.server_realm; rep_msg.srv_info.srv_uid = ServerConfig.net_info.server_uid; rep_msg.ws_time = GameTimeManager.Instance.server_time; this.Send(conn_idx, rep_msg); //告诉启动次数 IdSharedManager.Instance.SendStartCount(conn_idx); //告诉当前存在的服务器 List <ConnAppProc> list_app = new List <ConnAppProc>(); GetConnAppList(list_app, eServerType.GATE); GetConnAppList(list_app, eServerType.SERVER); GetConnAppList(list_app, eServerType.FIGHT); //发送当前存在的服务器列表 while (list_app.Count > 0) { inner.AppServerList list_msg = PacketPools.Get(inner.msg.APPSERVER_LIST) as inner.AppServerList; for (int i = 0; i < 10 && list_app.Count > 0; ++i) { ConnAppProc tmp_app = list_app[list_app.Count - 1]; if (tmp_app.srv_info.srv_uid == app_server.srv_info.srv_uid) {//不发送自己 list_app.RemoveAt(list_app.Count - 1); continue; } AppServerItem item = new AppServerItem(); item.srv_uid = tmp_app.srv_info.srv_uid; item.srv_type = tmp_app.srv_info.srv_type; item.srv_ip = tmp_app.srv_info.srv_endpoint.ip; item.srv_port = tmp_app.srv_info.srv_endpoint.port; list_msg.list.Add(item); list_app.RemoveAt(list_app.Count - 1); } if (list_msg.list.Count > 0) { app_server.Send(list_msg); } else { PacketPools.Recover(list_msg); } } //广播新服务器加入 inner.AppServerAdd add_msg = PacketPools.Get(inner.msg.APPSERVER_ADD) as inner.AppServerAdd; add_msg.app_info.srv_uid = app_server.srv_info.srv_uid; add_msg.app_info.srv_type = app_server.srv_info.srv_type; add_msg.app_info.srv_ip = app_server.srv_info.srv_endpoint.ip; add_msg.app_info.srv_port = app_server.srv_info.srv_endpoint.port; this.BroadcastMsgWithout(add_msg, app_server.conn_idx); } } } if (app_server != null) { if (!app_server.HandleMsg(conn_idx, packet)) { switch (packet.header) { case inner.msg.APPSERVER_SHUTDOWN: break; } } } } while (false); PacketPools.Recover(packet); }
private void OnMessageReveived(long conn_idx, ushort header, ByteArray data) { PacketBase packet = PacketPools.Get(header); packet.Read(data); ConnAppProc app_server; if (m_app_servers.TryGetValue(conn_idx, out app_server)) { if (app_server.srv_info.srv_status == eConnAppStatus.CONNECTED) { if (!app_server.HandleMsg(conn_idx, packet)) { if (app_server.srv_info.srv_type == eServerType.WORLD) { switch (packet.header) { case inner.msg.APPSERVER_LIST: (app_server as WorldMsgProc).HandleAppServerList(packet); break; case inner.msg.APPSERVER_ADD: (app_server as WorldMsgProc).HandleAppServerAdd(packet); break; case inner.msg.APPSERVER_REMOVE: (app_server as WorldMsgProc).HandleAppServerRemove(packet); break; case inner.msg.APPSERVER_SHUTDOWN: Master.Instance.Stop(); break; } } } } else if (app_server.srv_info.srv_status == eConnAppStatus.CONNECTING) { if (packet.header == inner.msg.REP_LOGIN) { inner.RepLogin msg = packet as inner.RepLogin; if (msg.result == inner.RepLogin.eResult.SUCCESS) { app_server.srv_info.srv_status = eConnAppStatus.CONNECTED; if (msg.srv_info.srv_type == eServerType.WORLD) { m_srv_realm_idx = msg.srv_info.srv_realm_idx;//由世界服分配的id app_server.srv_info.srv_uid = msg.srv_info.srv_uid; app_server.srv_info.srv_realm_idx = msg.srv_info.srv_realm_idx; GameTimeManager.Instance.SetAdjustTime(msg.ws_time - Time.time);//修正服务器时间 Log.Info("当前时间:" + Time.time + " 当前ws时间:" + msg.ws_time); Log.Info("当前服所属区服id:" + m_srv_realm_idx + ", sid:" + m_srv_uid); } OnConnAppEnter(app_server); } else { Log.Warning("连接服务器出错 type:" + msg.srv_info.srv_type + " result:" + msg.result); } } else { Log.Warning("收到无效协议:" + packet.header); } } } PacketPools.Recover(packet); }
/// <summary> /// 写邮件 /// </summary> public static void SendWriteMail(MailWriteInfo info) { c2ss.MailWrite msg = PacketPools.Get(c2ss.msg.MAIL_WRITE) as c2ss.MailWrite; msg.info = info; ClientNetManager.Instance.Send(msg); }