Ejemplo n.º 1
0
        public void Connect2GlobalServer(string ip, ushort port)
        {
            if (string.IsNullOrEmpty(ip))
            {
                return;
            }

            //只有连接上了世界服,才连接全局服
            if (!IsConnectedWorldServer())
            {
                return;
            }

            if (m_global_conn_idx != 0)
            {
                Log.Warning("全局服已经连接");
                return;
            }

            Log.Info("开始连接全局服 ip:" + ip + " port:" + port);
            long        conn_idx   = NetConnectManager.Instance.ConnectTo(ip, port, OnConnected, OnMessageReveived, OnConnectClose);
            ConnAppProc app_server = CreateConnApp(eServerType.GLOBAL);

            app_server.conn_idx          = conn_idx;
            app_server.srv_info.srv_type = eServerType.GLOBAL;
            app_server.srv_info.srv_endpoint.Set(SocketUtils.IpToInt(ip), port);
            app_server.srv_info.srv_status = eConnAppStatus.CONNECTING;
            m_app_servers.Add(conn_idx, app_server);
            m_global_conn_idx = conn_idx;
        }
Ejemplo n.º 2
0
        private ConnAppProc CreateConnApp(eServerType server_type)
        {
            ConnAppProc app_server = null;

            switch (server_type)
            {
            case eServerType.WORLD:
                app_server = new WorldMsgProc();
                break;

            case eServerType.GATE:
                app_server = new GateMsgProc();
                break;

            case eServerType.FIGHT:
                app_server = new FightMsgProc();
                break;

            case eServerType.GLOBAL:
                app_server = new GlobalMsgProc();
                break;

            default:
                Log.Warning("错误的服务器类型:" + server_type);
                return(null);
            }
            app_server.Setup();
            return(app_server);
        }
Ejemplo n.º 3
0
 private void OnConnAppLeave(ConnAppProc app_server)
 {
     Log.Info("服务器退出 type:" + app_server.srv_info.srv_type.ToString() + " sid:" + app_server.srv_info.srv_uid);
     app_server.srv_info.srv_status = eConnAppStatus.CLOSED;
     m_srv_servers.Remove(app_server.srv_info.srv_uid);
     ServerInfoManager.Instance.RemoveRemoteServer(app_server.srv_info.srv_uid);
 }
Ejemplo n.º 4
0
        public ConnAppProc GetConnApp(ushort srv_uid)
        {
            ConnAppProc app_server = null;

            if (m_srv_servers.TryGetValue(srv_uid, out app_server))
            {
                return(app_server);
            }
            return(null);
        }
 private void OnConnAppEnter(ConnAppProc app_server)
 {
     Log.Info("连接上服务器 type:" + app_server.srv_info.srv_type.ToString() + " sid:" + app_server.srv_info.srv_uid);
     switch (app_server.srv_info.srv_type)
     {
     case eServerType.WORLD:
         EventController.TriggerEvent(EventID.NET_CONNECTED_WORLD_SRV);    //成功连接到世界服
         break;
     }
 }
        private void OnConnAppLeave(ConnAppProc app_server)
        {
            Log.Info("断开与服务器的连接 type:" + app_server.srv_info.srv_type.ToString() + " sid:" + app_server.srv_info.srv_uid);

            if (app_server.srv_info.srv_type == eServerType.WORLD)
            {
                m_world_conn_idx = 0;
                EventController.TriggerEvent(EventID.NET_DISCONNECT_WORLD_SRV);
            }
        }
 private void OnConnAppEnter(ConnAppProc app_server)
 {
     if (m_srv_servers.ContainsKey(app_server.srv_info.srv_uid))
     {
         Log.Warning("已经存在服务器 sid:" + app_server.srv_info.srv_uid);
         return;
     }
     Log.Info("服务器加入 type:" + app_server.srv_info.srv_type.ToString() + " sid:" + app_server.srv_info.srv_uid);
     m_srv_servers.Add(app_server.srv_info.srv_uid, app_server);
 }
 private void OnConnAppLeave(ConnAppProc app_server)
 {
     Log.Info("断开与服务器的连接 type:" + app_server.srv_info.srv_type.ToString() + " sid:" + app_server.srv_info.srv_uid);
     app_server.srv_info.srv_status = eConnAppStatus.CLOSED;
     switch (app_server.srv_info.srv_type)
     {
     case eServerType.WORLD:
         m_world_conn_idx = 0;
         EventController.TriggerEvent(EventID.NET_DISCONNECT_WORLD_SRV);
         break;
     }
 }
        private void OnConnectClose(long conn_idx)
        {
            Log.Info("for server close session:" + conn_idx);

            ConnAppProc app_server = null;

            if (m_app_servers.TryGetValue(conn_idx, out app_server))
            {
                OnConnAppLeave(app_server);
            }
            m_app_servers.Remove(conn_idx);
        }
        private void OnConnAppEnter(ConnAppProc app_server)
        {
            Log.Info("连接上服务器 type:" + app_server.srv_info.srv_type.ToString() + " sid:" + app_server.srv_info.srv_uid);
            if (app_server.srv_info.srv_type == eServerType.WORLD)
            {
                EventController.TriggerEvent(EventID.NET_CONNECTED_WORLD_SRV);//成功连接到世界服

                //启动DB
                DBManager.Instance.Start(ServerConfig.net_info.db_list);
                InitNextCharIdx();
            }
        }
        private void OnConnAppEnter(ConnAppProc app_server)
        {
            if (m_srv_servers.ContainsKey(app_server.srv_info.srv_uid))
            {
                Log.Warning("已经存在服务器 sid:" + app_server.srv_info.srv_uid);
                return;
            }
            string ip = m_net_socket.GetConnIP(app_server.conn_idx);

            Log.Info("服务器加入 type:" + app_server.srv_info.srv_type.ToString() + " wid:" + app_server.srv_info.srv_realm_idx + " sid:" + app_server.srv_info.srv_uid + " ip:" + ip);
            m_srv_servers.Add(app_server.srv_info.srv_uid, app_server);
        }
        private void OnMessageReveived(long conn_idx, ushort header, ByteArray data)
        {
            PacketBase packet = PacketPools.Get(header);

            packet.Read(data);
            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;
                        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 = msg.srv_info.srv_realm_idx;
                        app_server.srv_info.srv_uid       = msg.srv_info.srv_uid;
                        app_server.srv_info.srv_endpoint  = msg.srv_info.srv_endpoint;
                        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.FIGHT;
                        rep_msg.srv_info.srv_realm_idx = app_server.srv_info.srv_realm_idx;
                        rep_msg.srv_info.srv_uid       = app_server.srv_info.srv_uid;
                        this.Send(conn_idx, rep_msg);
                    }
                }
            }
            if (app_server != null)
            {
                if (!app_server.HandleMsg(conn_idx, packet))
                {
                    //switch (packet.header)
                    //{
                    //    case inner.msg.APPSERVER_SHUTDOWN:
                    //        app_server.is_res_loaded = true;
                    //        break;

                    //}
                }
            }
            PacketPools.Recover(packet);
        }
Ejemplo n.º 13
0
        public int Send2WS(PacketBase packet)
        {
            if (m_world_conn_idx == 0)
            {
                return(0);
            }
            ConnAppProc app_server = null;

            if (m_app_servers.TryGetValue(m_world_conn_idx, out app_server))
            {
                return(app_server.Send(packet));
            }
            return(0);
        }
        public void Connect2Server(string ip, ushort port, eServerType type, ushort srv_uid)
        {
            Log.Info("连接服务器 ip:" + ip + " port:" + port + " type:" + type + " uid:" + srv_uid);
            long conn_idx = NetConnectManager.Instance.ConnectTo(ip, port, OnConnected, OnMessageReveived, OnConnectClose);

            ConnAppProc app_server = CreateConnApp(type);

            app_server.conn_idx               = conn_idx;
            app_server.srv_info.srv_type      = type;
            app_server.srv_info.srv_uid       = srv_uid;
            app_server.srv_info.srv_realm_idx = m_srv_realm_idx;
            app_server.srv_info.srv_status    = eConnAppStatus.CONNECTING;
            m_app_servers.Add(conn_idx, app_server);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// 发给fs
        /// </summary>
        public int Send2FS(ushort fs_uid, PacketBase packet)
        {
            if (fs_uid == 0)
            {
                return(0);
            }

            ConnAppProc app_server = null;

            if (m_srv_servers.TryGetValue(fs_uid, out app_server))
            {
                return(app_server.Send(packet));
            }
            return(0);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// 通过sid发送
        /// </summary>
        public int Send(ushort srv_uid, PacketBase packet)
        {
            int         size       = 0;
            ConnAppProc app_server = null;

            if (m_srv_servers.TryGetValue(srv_uid, out app_server))
            {
                size = app_server.Send(packet);
            }
            else
            {
                PacketPools.Recover(packet);
            }
            return(size);
        }
Ejemplo n.º 17
0
        private void OnConnectClose(long conn_idx)
        {
            Log.Info("Close session:" + conn_idx);

            ConnAppProc app_server = null;

            if (m_app_servers.TryGetValue(conn_idx, out app_server))
            {
                //广播消息
                inner.AppServerRemove msg = PacketPools.Get(inner.msg.APPSERVER_REMOVE) as inner.AppServerRemove;
                msg.srv_uid = app_server.srv_info.srv_uid;
                this.BroadcastMsgWithout(msg, app_server.conn_idx);

                OnConnAppLeave(app_server);
            }
            m_app_servers.Remove(conn_idx);
        }
        /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~连接列表~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        private ConnAppProc CreateConnApp(eServerType server_type)
        {
            ConnAppProc app_server = null;

            switch (server_type)
            {
            case eServerType.WORLD:
                app_server = new WorldMsgProc();
                break;

            default:
                Log.Warning("错误的服务器类型:" + server_type);
                return(null);
            }
            app_server.Setup();
            return(app_server);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// 分配战斗服id
        /// </summary>
        public ushort AllocFightServer()
        {
            List <ushort> list = new List <ushort>();

            foreach (var obj in m_app_servers)
            {
                ConnAppProc app_server = obj.Value;
                if (app_server.srv_info.srv_status == eConnAppStatus.CONNECTED && app_server.srv_info.srv_type == eServerType.FIGHT)
                {
                    list.Add(app_server.srv_info.srv_uid);
                }
            }
            if (list.Count == 0)
            {
                return(0);
            }
            return(MathUtils.RandRange_List <ushort>(list));
        }
Ejemplo n.º 20
0
 private void OnConnAppEnter(ConnAppProc app_server)
 {
     if (m_srv_servers.ContainsKey(app_server.srv_info.srv_uid))
     {
         Log.Warning("已经存在服务器 sid:" + app_server.srv_info.srv_uid);
         return;
     }
     Log.Info("连接上服务器 type:" + app_server.srv_info.srv_type.ToString() + " sid:" + app_server.srv_info.srv_uid);
     m_srv_servers.Add(app_server.srv_info.srv_uid, app_server);
     switch (app_server.srv_info.srv_type)
     {
     case eServerType.WORLD:
         //之所以放在连接世界服后再连接全局服,是可以告诉全局服ss所在大区id
         Connect2GlobalServer(ServerConfig.net_info.gl_ip, ServerConfig.net_info.gl_port);
         EventController.TriggerEvent(EventID.NET_CONNECTED_WORLD_SRV);    //成功连接到世界服
         break;
     }
 }
Ejemplo n.º 21
0
        private void OnConnAppEnter(ConnAppProc app_server)
        {
            if (m_srv_servers.ContainsKey(app_server.srv_info.srv_uid))
            {
                Log.Warning("已经存在服务器 sid:" + app_server.srv_info.srv_uid);
                return;
            }
            Log.Info("连接上服务器 type:" + app_server.srv_info.srv_type.ToString() + " sid:" + app_server.srv_info.srv_uid);
            m_srv_servers.Add(app_server.srv_info.srv_uid, app_server);
            switch (app_server.srv_info.srv_type)
            {
            case eServerType.WORLD:
                EventController.TriggerEvent(EventID.NET_CONNECTED_WORLD_SRV);    //成功连接到世界服
                break;

            case eServerType.GATE:
                break;
            }
        }
Ejemplo n.º 22
0
        public void Connect2WorldServer(string ip, ushort port)
        {
            if (m_world_conn_idx != 0)
            {
                Log.Warning("世界服已经连接");
                return;
            }

            Log.Info("开始连接世界服 ip:" + ip + " port:" + port);
            long        conn_idx   = NetConnectManager.Instance.ConnectTo(ip, port, OnConnected, OnMessageReveived, OnConnectClose);
            ConnAppProc app_server = CreateConnApp(eServerType.WORLD);

            app_server.conn_idx          = conn_idx;
            app_server.srv_info.srv_type = eServerType.WORLD;
            app_server.srv_info.srv_endpoint.Set(SocketUtils.IpToInt(ip), port);
            app_server.srv_info.srv_status = eConnAppStatus.CONNECTING;
            m_app_servers.Add(conn_idx, app_server);
            m_world_conn_idx = conn_idx;
        }
Ejemplo n.º 23
0
        /// <summary>
        /// 连接战斗服:战斗服只在需要的时候连接,如匹配,战斗中等
        /// </summary>
        public void ConnectFightServer()
        {
            //已经连接了战斗服,直接返回
            if (m_connect_state == eFSConnectState.Connected)
            {
                return;
            }

            Player player = UnitManager.Instance.GetPlayerByIdx(m_char_idx);

            if (player == null || player.fs_uid == 0)
            {
                return;
            }

            //判断是否已经有战斗服
            ConnAppProc app_server = ServerNetManager.Instance.GetConnApp(player.fs_uid);

            if (app_server == null || app_server.srv_info.srv_status != eConnAppStatus.CONNECTED)
            {
                return;
            }

            //如果存在,就当已经连上,因为本身ss已经连上fs
            {
                //告诉fs
                ss2fs.LoginClient msg = PacketPools.Get(ss2fs.msg.LOGIN_CLIENT) as ss2fs.LoginClient;
                msg.client_uid = player.client_uid;
                msg.data.Copy(player.unit_attr.player_info);
                ServerNetManager.Instance.Send2FS(player.fs_uid, msg);

                //告诉gs分配给玩家的fs
                ss2gs.NotifyServer gs_msg = PacketPools.Get(ss2gs.msg.NOTIFY_SERVER) as ss2gs.NotifyServer;
                gs_msg.account_idx = player.account_idx;
                gs_msg.s_type      = eServerType.FIGHT;
                gs_msg.fs_uid      = player.fs_uid;
                ServerNetManager.Instance.Send(player.client_uid.srv_uid, gs_msg);

                m_connect_state = eFSConnectState.Connected;
            }
        }
Ejemplo n.º 24
0
        private void OnConnAppLeave(ConnAppProc app_server)
        {
            Log.Info("断开与服务器的连接 type:" + app_server.srv_info.srv_type.ToString() + " sid:" + app_server.srv_info.srv_uid);
            app_server.srv_info.srv_status = eConnAppStatus.CLOSED;
            switch (app_server.srv_info.srv_type)
            {
            case eServerType.WORLD:
                m_world_conn_idx = 0;
                EventController.TriggerEvent(EventID.NET_DISCONNECT_WORLD_SRV);
                break;

            case eServerType.GLOBAL:
                m_global_conn_idx = 0;
                break;

            case eServerType.FIGHT:    //如果战斗服退出,当前战斗服的玩家自动连接其他战斗服
                EventController.TriggerEvent(EventID.NET_DISCONNECT_FIGHT_SRV, app_server.srv_info.srv_uid);
                break;
            }
            m_srv_servers.Remove(app_server.srv_info.srv_uid);
        }
Ejemplo n.º 25
0
        private void OnConnAppEnter(ConnAppProc app_server)
        {
            if (m_srv_servers.ContainsKey(app_server.srv_info.srv_uid))
            {
                Log.Warning("已经存在服务器 sid:" + app_server.srv_info.srv_uid);
                return;
            }
            string ip = m_net_socket.GetConnIP(app_server.conn_idx);

            Log.Info("服务器加入 type:" + app_server.srv_info.srv_type.ToString() + " sid:" + app_server.srv_info.srv_uid + " ip:" + ip);
            m_srv_servers.Add(app_server.srv_info.srv_uid, app_server);

            //写入服务器信息
            RemoteServerInfo server_info = new RemoteServerInfo();

            server_info.srv_uid    = app_server.srv_info.srv_uid;
            server_info.type       = app_server.srv_info.srv_type;
            server_info.ip         = ip;
            server_info.start_time = Time.time;
            ServerInfoManager.Instance.AddRemoteServer(server_info);
        }
        /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~连接列表~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        private ConnAppProc CreateConnApp(eServerType server_type)
        {
            ConnAppProc app_server = null;

            switch (server_type)
            {
            case eServerType.SERVER:
                app_server = new ServerMsgProc();
                break;

            case eServerType.GATE:
                app_server = new GateMsgProc();
                break;

            default:
                Log.Warning("错误的服务器类型:" + server_type);
                return(null);
            }
            app_server.Setup();
            return(app_server);
        }
 private void OnConnAppLeave(ConnAppProc app_server)
 {
     Log.Info("服务器退出 type:" + app_server.srv_info.srv_type.ToString() + " sid:" + app_server.srv_info.srv_uid);
     m_srv_servers.Remove(app_server.srv_info.srv_uid);
 }
 private void OnConnAppLeave(ConnAppProc app_server)
 {
     Log.Info("服务器退出 type:" + app_server.srv_info.srv_type.ToString() + " wid:" + app_server.srv_info.srv_realm_idx + " sid:" + app_server.srv_info.srv_uid);
     app_server.srv_info.srv_status = eConnAppStatus.CLOSED;
     m_srv_servers.Remove(app_server.srv_info.srv_uid);
 }
Ejemplo n.º 29
0
        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);
        }