Esempio n. 1
0
        public AppSocketServer(IAppSocketConfig rootConfig, IAppSocketServerConfig serverConfig)
        {
            AppSocketContainer.Ins.SetContainer(new UnityObjectContainer());
            try
            {
                _rootConfig = rootConfig;
                _serverConfig = serverConfig;

                _logger = new Log4NetLogger();
                _logger.Init(_serverConfig.Logger);
                _autoReset = new AutoResetEvent(false);

                AppSocketContainer.Ins.RegisterInstance<ILogger>(_logger, IOCLife.Singleton);

                AppSocketContainer.Ins.RegisterInstance<IAppSocketConfig>(_rootConfig, IOCLife.Singleton);

                AppSocketContainer.Ins.RegisterInstance<IAppSocketServerConfig>(_serverConfig, IOCLife.Singleton);

                AppSocketContainer.Ins.RegisterAppSocketServerType<IAppSocketServerEvent>(_serverConfig, (p, l) =>
                {
                    p.RegisterType<IAppSocketServerEvent, AppSocketServerEvent>(l);
                });

                _socketServerEvent = AppSocketContainer.Ins.Resolve<IAppSocketServerEvent>();

                _socketServerEvent.OnPreInit(AppSocketContainer.Ins);

                AppSocketContainer.Ins.RegisterAppSocketServerType<IAppSocketAsyncEventArgsPool>(_serverConfig, (p, l) =>
                {
                    p.RegisterType<IAppSocketAsyncEventArgsPool, DefaultAppSocketAsyncEventArgsPool>(l);
                });

                AppSocketContainer.Ins.RegisterAppSocketServerType<IAppSocketSessionManager>(_serverConfig, (p, l) =>
                {
                    p.RegisterType<IAppSocketSessionManager, DefaultAppSocketSessionManager>(l);
                });

                AppSocketContainer.Ins.RegisterAppSocketServerType<IAppSocketBufferManager>(_serverConfig, (p, l) =>
                {
                    p.RegisterType<IAppSocketBufferManager, DefaultAppSocketBufferManager>(l);
                });

                AppSocketContainer.Ins.RegisterAppSocketServerType<IAppSocketProtocol>(_serverConfig, (p, l) =>
                {
                    p.RegisterType<IAppSocketProtocol, DefaultAppSocketSplitProtocol>(l);
                });

                AppSocketContainer.Ins.RegisterAppSocketServerType<IAppSocketClient>(_serverConfig, (p, l) =>
                {
                    p.RegisterType<IAppSocketClient, DefaultAppSocketClient>(l);
                }, IOCLife.Transient);

                AppSocketContainer.Ins.RegisterAppSocketServerType<IAppSocketDataProcessor>(_serverConfig, (p, l) =>
                {
                    p.RegisterType<IAppSocketDataProcessor, DefaultAppSocketDataProcessor>(l);
                }, IOCLife.Transient);

                _socketServerEvent.OnInit(AppSocketContainer.Ins);

                _logger = AppSocketContainer.Ins.Resolve<ILogger>();

                _logger.Init(_serverConfig.Logger);

                _socketBufferManager = AppSocketContainer.Ins.Resolve<IAppSocketBufferManager>();
                _socketAsyncEventArgsPool = AppSocketContainer.Ins.Resolve<IAppSocketAsyncEventArgsPool>();
                _socketDataProcessor = AppSocketContainer.Ins.Resolve<IAppSocketDataProcessor>();
                _socketSessionManager = AppSocketContainer.Ins.Resolve<IAppSocketSessionManager>();
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
            }
        }
Esempio n. 2
0
        public DefaultAppSocketTcpServer(IAppSocketConfig rootConfig, IAppSocketServerConfig serverConfig)
            : base(rootConfig, serverConfig)
        {
            try
            {
                //处理监听地址
                IPEndPoint myEnd = new IPEndPoint(IPAddress.Any, _serverConfig.Port);

                if (string.IsNullOrEmpty(_serverConfig.Host) || "Any".Equals(_serverConfig.Host, StringComparison.CurrentCultureIgnoreCase))
                {
                    IPHostEntry p = Dns.GetHostEntry(Dns.GetHostName());

                    foreach (IPAddress s in p.AddressList)
                    {
                        if (!s.IsIPv6LinkLocal && s.AddressFamily != AddressFamily.InterNetworkV6)
                        {
                            myEnd = new IPEndPoint(s, _serverConfig.Port);
                            break;
                        }
                    }
                }
                else
                {
                    try
                    {
                        myEnd = new IPEndPoint(IPAddress.Parse(_serverConfig.Host), _serverConfig.Port);
                    }
                    catch (FormatException)
                    {
                        IPHostEntry p = Dns.GetHostEntry(Dns.GetHostName());

                        foreach (IPAddress s in p.AddressList)
                        {
                            if (!s.IsIPv6LinkLocal)
                            {
                                myEnd = new IPEndPoint(s, _serverConfig.Port);
                            }
                        }
                    }
                }

                //更新监听地址
                _serverConfig.Host = myEnd.Address.ToString();
                //用于检测用户断开 比如异常断电 拔网线等。
                uint dummy = 0;
                byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
                BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);
                BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy));
                BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);
                _sokect = new Socket(myEnd.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                _sokect.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
                _sokect.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                _sokect.Bind(myEnd);
                _sokect.Listen(20);

                for (int i = 0; i < _serverConfig.MaxConnect; i++)
                {
                    SocketAsyncEventArgs socketAsyncEventArgs = new SocketAsyncEventArgs();
                    socketAsyncEventArgs.Completed += SocketAsyncEventArgs_Completed;
                    _socketAsyncEventArgsPool.Push(socketAsyncEventArgs);
                }
                _socketServerEvent.OnInitCompleted(AppSocketContainer.Ins);
                AcceptAsync();
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
            }
        }