示例#1
0
        public NetServer(
            ServerOption serverOption,
            NetStatistic statistics        = null,
            ILoggerFactory loggerFactory   = null,
            ISessionFactory sessionFactory = null)
        {
            Name = serverOption.Name;

            _serverOption = serverOption;
            //_serverOptions.PacketFilter = _serverOptions.PacketFilter ?? new XorPacketFilter();

            _loggerFactory = loggerFactory ?? DefaultLoggerFactory.Create(builder => { builder.AddConsoleLogger(); });
            _logger        = _loggerFactory.CreateLogger("Server");

            _statistic = statistics ?? new NetStatistic();

            _sessionFactory = sessionFactory ?? new DefaultSessionFactory(_serverOption, _loggerFactory, _statistic);

            _sessionManager = new SessionManager(_serverOption.MaxSession);

            if (_serverOption.IsServiceUdp)
            {
                _p2pManager      = new P2pManager();
                OnSessionClosed += _p2pManager.OnSessionClose;
            }

            _rpcHandlers = new List <IRpcInvokable>();
        }
示例#2
0
        public DefaultSessionFactory(
            ServerOption serverOption,
            ILoggerFactory loggerFactory,
            NetStatistic statistic,
            Func <SessionCreateInfo, ServerSession> createSessionFunc = null)
        {
            createSessionFunc = createSessionFunc ?? CreateSession;

            _sessionQueue = new ConcurrentQueue <ISession>();
            _maxSession   = serverOption.MaxSession;

            var createInfo = new SessionCreateInfo();

            for (int i = 0; i < _maxSession; ++i)
            {
                TcpChannel tcpChannel = new TcpChannel(
                    serverOption,
                    loggerFactory.CreateLogger(nameof(TcpChannel)),
                    statistic);

                UdpChannel udpChannel = null;

                if (serverOption.IsServiceUdp)
                {
                    udpChannel = new UdpChannel(
                        serverOption,
                        loggerFactory.CreateLogger(nameof(UdpChannel)),
                        statistic,
                        0);
                }

                createInfo.SessionId  = (ushort)(i + 1);
                createInfo.TcpChannel = tcpChannel;
                createInfo.UdpChannel = udpChannel;
                createInfo.Statistic  = statistic;

                ServerSession session = createSessionFunc(createInfo);

                _sessionQueue.Enqueue(session);
            }
        }
示例#3
0
 public TcpListener(ServerOption options, ILogger logger)
 {
     Options = options;
     _logger = logger;
 }