コード例 #1
0
        /// <summary>
        /// 发包
        /// </summary>
        /// <param name="conn_idx"></param>
        /// <param name="packet"></param>
        public void Send(long conn_idx, PacketBase packet)
        {
            if (m_sockets.Contains(conn_idx))
            {
                //包索引
                uint packet_idx = 0;
                if (packet is PackBaseC2S)
                {
                    packet_idx = PacketEncrypt.GetPacketIndex();
                    (packet as PackBaseC2S).packet_idx = PacketEncrypt.EncrpytPacketIndex(packet_idx, GlobalID.ENCRYPT_KEY);
                }

                m_send_by.Clear();
                m_send_by.WriteUShort(0);//先写入长度占位
                packet.Write(m_send_by);
                int data_len = m_send_by.Available - NetID.PacketHeadSize;
                m_send_by.ModifyUShort((ushort)data_len, 0);

                //数据有效性
                if (packet is PackBaseC2S)
                {
                    //包长2 + 协议头2 + 包索引id4 + 有效性校验2 = 10
                    ushort data_verify = PacketEncrypt.CalcPacketDataVerify(m_send_by.Buffer, 10, m_send_by.Available - 10, packet_idx, GlobalID.ENCRYPT_KEY);
                    m_send_by.ModifyUShort((ushort)data_verify, 8);
                }

                int size = NetConnectManager.Instance.Send(conn_idx, m_send_by);
                m_send_msg_count++;
                m_send_msg_size += size;
            }

            PacketPools.Recover(packet);
        }
コード例 #2
0
        /// <summary>
        /// 发包
        /// </summary>
        /// <param name="conn_idx"></param>
        /// <param name="packet"></param>
        public void Send(PacketBase packet)
        {
            if (m_cur_conn_idx > 0)
            {
                //包索引
                uint packet_idx = 0;
                if (packet is PackBaseC2S)
                {
                    packet_idx = PacketEncrypt.GetPacketIndex();
                    (packet as PackBaseC2S).packet_idx = PacketEncrypt.EncrpytPacketIndex(packet_idx, GlobalID.ENCRYPT_KEY);
                }

                //数据写入stream
                m_send_by.Clear();
                m_send_by.WriteUShort(0);                                  //先写入长度占位
                packet.Write(m_send_by);
                int data_len = m_send_by.Available - NetID.PacketHeadSize; //总长度
                m_send_by.ModifyUShort((ushort)data_len, 0);

                //数据有效性
                if (packet is PackBaseC2S)
                {
                    //包长2 + 协议头2 + 包索引id4 + 有效性校验2 = 10
                    ushort data_verify = PacketEncrypt.CalcPacketDataVerify(m_send_by.Buffer, 10, m_send_by.Available - 10, packet_idx, GlobalID.ENCRYPT_KEY);
                    m_send_by.ModifyUShort((ushort)data_verify, 8);
                }

                NetConnectManager.Instance.Send(m_cur_conn_idx, m_send_by);
            }

            PacketPools.Recover(packet);
        }
コード例 #3
0
        /// <summary>
        /// 处理由gate转发的client消息
        /// </summary>
        /// <param name="packet"></param>
        private void HandleProxyClientMsg(PacketBase packet)
        {
            ProxyC2SMsg proxy_msg = packet as ProxyC2SMsg;

            //转发的消息id
            ushort header = proxy_msg.data.ReadUShort();

            if (header < ProtocolID.MSG_BASE_C2SS || header >= ProtocolID.MSG_BASE_C2SS + ProtocolID.MSG_APPLAYER_PER_INTERVAL)
            {
                Log.Debug("收到错误的消息ID:" + header);
                return;
            }

            PacketBase msg = PacketPools.Get(header);

            msg.Read(proxy_msg.data);

            //处理
            ushort          msg_id = (ushort)(header - ProtocolID.MSG_BASE_C2SS);
            MsgProcFunction fun    = m_client_msg_proc[msg_id];

            if (fun == null)
            {
                Log.Debug("未注册消息处理函数ID:" + header);
            }
            else
            {
                fun(msg);
            }
            PacketPools.Recover(msg);
        }
コード例 #4
0
        /// <summary>
        /// 网络事件处理
        /// </summary>
        public void OnNetworkServer(long conn_idx, 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);
                }
            }
            else
            {
                Log.Warning("未注册消息处理函数id:" + header);
            }
            PacketPools.Recover(packet);
        }
コード例 #5
0
        public int Send(long conn_idx, PacketBase packet)
        {
            m_send_by.Clear();
            m_send_by.WriteUShort(0);//先写入长度占位
            packet.Write(m_send_by);
            int len = NetConnectManager.Instance.Send(conn_idx, m_send_by);

            PacketPools.Recover(packet);
            return(len);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
 /// <summary>
 /// 广播消息:通过gate转发给client
 /// </summary>
 public void BroadcastProxyMsg(PackBaseS2C packet)
 {
     foreach (var obj in m_app_servers)
     {
         if (obj.Value.srv_info.srv_type == eServerType.GATE)
         {
             ProxyS2CMsg msg = PacketPools.Get((ushort)ss2gs.msg.PROXY_SS_MSG) as ProxyS2CMsg;
             msg.is_broadcast = true;
             msg.Set(packet.client_uid, packet);
             obj.Value.Send(msg);
         }
     }
     PacketPools.Recover(packet);//回收消息本身
 }
コード例 #8
0
        public int Send2WS(PacketBase packet)
        {
            if (m_world_conn_idx == 0)
            {
                return(0);
            }
            m_send_by.Clear();
            m_send_by.WriteUShort(0);//先写入长度占位
            packet.Write(m_send_by);
            int len = NetConnectManager.Instance.Send(m_world_conn_idx, m_send_by);

            PacketPools.Recover(packet);
            return(len);
        }
コード例 #9
0
        /// <summary>
        /// 发消息给client
        /// </summary>
        /// <param name="conn_idx"></param>
        /// <param name="packet"></param>
        /// <returns></returns>
        public int Send(long conn_idx, PacketBase packet)
        {
            int size = 0;

            if (m_net_socket != null)
            {
                m_send_by.Clear();
                m_send_by.WriteUShort(0);//先写入长度占位
                packet.Write(m_send_by);
                size = m_net_socket.Send(conn_idx, m_send_by);
            }
            PacketPools.Recover(packet);
            return(size);
        }
コード例 #10
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);
        }
コード例 #11
0
        /// <summary>
        /// 根据服务器id发
        /// </summary>
        public int Send(ushort sid, PacketBase packet)
        {
            ConnAppProc app_server;

            if (m_srv_servers.TryGetValue(sid, out app_server))
            {
                return(Send(app_server.conn_idx, packet));
            }
            else
            {
                PacketPools.Recover(packet);
                Log.Warning("未找到服务器id:" + sid);
                return(0);
            }
        }
コード例 #12
0
        /// <summary>
        /// 通过gate转发给client
        /// </summary>
        public int SendProxy(ClientUID client_uid, PackBaseS2C packet, bool is_broadcast = false)
        {
            int         size = 0;
            ConnAppProc app_server;

            if (m_srv_servers.TryGetValue(client_uid.srv_uid, out app_server))
            {
                ProxyS2CMsg msg = PacketPools.Get((ushort)ss2gs.msg.PROXY_SS_MSG) as ProxyS2CMsg;
                msg.is_broadcast = is_broadcast;
                msg.Set(client_uid, packet);
                size = Send(app_server.conn_idx, msg);
            }
            PacketPools.Recover(packet);//回收消息本身
            return(size);
        }
コード例 #13
0
        /// <summary>
        /// 发给客户端
        /// </summary>
        public int SendProxy(ClientUID client_uid, PackBaseS2C packet)
        {
            int         size = 0;
            ConnAppProc app_server;

            if (m_srv_servers.TryGetValue(client_uid.srv_uid, out app_server))
            {
                ProxyS2CMsg msg = PacketPools.Get((ushort)ws2gs.msg.PROXY_WS_MSG) as ProxyS2CMsg;
                msg.Set(client_uid, packet);
                size = Send(app_server.conn_idx, msg);
            }
            else
            {
                Log.Debug("未找到服务器id:" + client_uid.srv_uid);
            }

            PacketPools.Recover(packet);//回收消息本身
            return(size);
        }
コード例 #14
0
        /// <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);
        }
コード例 #15
0
        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);
        }
コード例 #16
0
        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;
            }
        }
コード例 #17
0
        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);
        }
コード例 #18
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);
        }
コード例 #19
0
        /// <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);
            }
        }