private void OnLogoutClient(PacketBase packet) { }
/// <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> private void OnPlayerCount(PacketBase packet) { ss2ws.OnlineCount msg = packet as ss2ws.OnlineCount; ServerInfoManager.Instance.UpdatePlayerCount(msg.server_uid.ss_uid, msg.count); }
/// <summary> /// 角色基础信息 /// </summary> /// <param name="packet"></param> private void OnCharacterInfo(PacketBase packet) { ss2c.CharacterInfo msg = packet as ss2c.CharacterInfo; m_unit_info = msg.data; }
/// <summary> /// 场景切换 /// </summary> private void OnEnterScene(PacketBase packet) { ss2c.EnterScene msg = packet as ss2c.EnterScene; m_scene_type = msg.scene_type; m_pos = msg.pos; }
/// <summary> /// 移除服务器 /// </summary> /// <param name="packet"></param> public void HandleAppServerRemove(PacketBase packet) { inner.AppServerRemove re_msg = packet as inner.AppServerRemove; Log.Info("收到服务器关闭 sid:" + re_msg.srv_uid); }
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); }
/// <summary> /// 防加速 /// </summary> private void OnSpeedCheck(PacketBase packet) { gs2c.SpeedCheck msg = packet as gs2c.SpeedCheck; SpeedCheckManager.Instance.OnRecvCheckGrap(msg.check_sn, msg.delay_time); }
/// <summary> /// 角色基础信息 /// </summary> /// <param name="packet"></param> private void OnCharacterInfo(PacketBase packet) { ss2c.CharacterInfo msg = packet as ss2c.CharacterInfo; PlayerDataMgr.Instance.main_player_info = msg.data; PlayerDataMgr.Instance.main_player_id = msg.data.char_idx; }
/// <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); }
/// <summary> /// 压力测试 /// </summary> private void OnRobotTest(PacketBase packet) { }
/// <summary> /// 踢号 /// </summary> private void OnKickAccount(PacketBase packet) { gs2ss.KickoutAccount msg = packet as gs2ss.KickoutAccount; HandleLogoutAccount(msg.account_idx); }
/// <summary> /// 发消息 /// </summary> /// <param name="by"></param> /// <returns></returns> public override int Send(PacketBase packet) { return(ForServerNetManager.Instance.Send(m_conn_idx, packet)); }
private void OnGameEvent(GameEvent evt) { switch (evt.type) { case ClientEventID.NET_CONNECTED_OPEN: { if (!m_active) { break; } long conn_idx = evt.Get <long>(0); if (!m_connectes.Contains(conn_idx)) { m_connectes.Add(conn_idx); } } break; case ClientEventID.NET_CONNECTED_CLOSE: { if (!m_active) { break; } long conn_idx = evt.Get <long>(0); m_connectes.Remove(conn_idx); } break; case ClientEventID.SWITCH_PRESSURE: { ePressureType type = evt.Get <ePressureType>(0); bool is_start = evt.Get <bool>(1); if (type == ePressureType.Net && is_start) { m_pressure_info = evt.Get <sPressureNetInfo>(2); this.Start(); } else { this.Stop(); } } break; case ClientEventID.RECV_DATA: { if (!m_active) { break; } long conn_idx = evt.Get <long>(0); ushort header = evt.Get <ushort>(1); ByteArray data = evt.Get <ByteArray>(2); if (header == gs2c.msg.ENCRYPT) { m_had_recv_encrypt = true; PacketBase packet = PacketPools.Get(header); packet.Read(data); GlobalID.ENCRYPT_KEY = (packet as gs2c.EncryptInfo).key; PacketPools.Recover(packet); } } break; } }
/// <summary> /// 服务器即将关闭 /// </summary> private void OnServerShutdown(PacketBase packet) { ws2c.ShutdownServer msg = packet as ws2c.ShutdownServer; Log.Debug("服务器即将关闭:" + msg.leave_time); }
/// <summary> /// 邮件列表 /// </summary> private void OnMailList(PacketBase packet) { ss2c.MailList msg = packet as ss2c.MailList; MailDataManager.Instance.AddNewMails(msg.mail_list); }
/// <summary> /// 新服务器加入 /// </summary> public void HandleAppServerAdd(PacketBase packet) { inner.AppServerAdd add_msg = packet as inner.AppServerAdd; Log.Info("收到新服务器 type:" + add_msg.app_info.srv_type + " sid:" + add_msg.app_info.srv_uid); AppServerItem item = add_msg.app_info; }
/// <summary> /// 邮件操作结果 /// </summary> private void OnMailCommand(PacketBase packet) { ss2c.MailCommand msg = packet as ss2c.MailCommand; }
/// <summary> /// 发给客户端 /// </summary> public int Send(PacketBase packet) { return(ForClientNetManager.Instance.Send(m_conn_idx, packet)); }
/// <summary> /// 移除关系 /// </summary> private void OnRelationRemove(PacketBase packet) { ss2c.RelationRemove msg = packet as ss2c.RelationRemove; RelationDataManager.Instance.RemoveRelation(msg.target_char_idx); }
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; case inner.msg.SERVER_START_COUNT: IdSharedManager.Instance.InitServerStartCount((packet as inner.ServerStartCount).count); 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); } else if (msg.srv_info.srv_type == eServerType.GLOBAL) { app_server.srv_info.srv_uid = msg.srv_info.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> private void OnRelationList(PacketBase packet) { ss2c.RelationList msg = packet as ss2c.RelationList; RelationDataManager.Instance.AddRelation(msg.list); }
/// <summary> /// 进入游戏 /// </summary> private void OnEnterGame(PacketBase packet) { gs2c.EnterGame msg = packet as gs2c.EnterGame; ServerMsgSend.SendEnterScene(m_conn_idx, 0); }
/// <summary> /// 赠送 /// </summary> private void OnRelationGive(PacketBase packet) { ss2c.RelationGive msg = packet as ss2c.RelationGive; System.Windows.Forms.MessageBox.Show("收到(" + msg.player_id.char_name + ")赠送的礼物", "提示", System.Windows.Forms.MessageBoxButtons.OK); }
/// <summary> /// 加速检测 /// </summary> private void OnSpeedCheck(ClientSession session, PacketBase packet) { c2gs.SpeedCheck msg = packet as c2gs.SpeedCheck; session.speed_checker.OnRecvTrapMsg(msg.check_sn); }
/// <summary> /// 添加关系 /// </summary> private void OnChatRecv(PacketBase packet) { ss2c.ChatRecv msg = packet as ss2c.ChatRecv; ChatDataManager.Instance.AddChat(msg.type, msg.sender, msg.chat_content); }
/// <summary> /// 账号登出 /// </summary> private void OnLogoutAccount(PacketBase packet) { ss2ws.LogoutClient msg = packet as ss2ws.LogoutClient; UnitManager.Instance.HandleLogout(msg.char_idx); Log.Debug("玩家离开游戏:" + msg.char_idx); }
/// <summary> /// 聊天错误 /// </summary> private void OnChatError(PacketBase packet) { ss2c.ChatResult msg = packet as ss2c.ChatResult; }
/// <summary> /// 发消息 /// </summary> /// <param name="by"></param> /// <returns></returns> public virtual int Send(PacketBase packet) { return(0); }
private void OnCharacterInfo(PacketBase packet) { }