public SocketServerBase(IAppServer <TAppSession> appServer, IPEndPoint localEndPoint, ICustomProtocol <TCommandInfo> protocol)
 {
     AppServer = appServer;
     EndPoint  = localEndPoint;
     Protocol  = protocol;
     IsRunning = false;
 }
Beispiel #2
0
        private bool SetupProtocol(IServerConfig config, ICustomProtocol <TCommandInfo> protocol)
        {
            //The protocol passed by programming has higher priority, then by config
            if (protocol != null)
            {
                Protocol = protocol;
            }
            else
            {
                //There is a protocol configuration existing
                if (!string.IsNullOrEmpty(config.Protocol))
                {
                    ICustomProtocol <TCommandInfo> configuredProtocol;
                    Exception exception;
                    if (!AssemblyUtil.TryCreateInstance <ICustomProtocol <TCommandInfo> >(config.Protocol, out configuredProtocol, out exception))
                    {
                        Logger.LogError("Invalid configured protocol " + config.Protocol + ".", exception);
                        return(false);
                    }
                    Protocol = configuredProtocol;
                }
            }

            //If there is no defined protocol, use CommandLineProtocol as default
            if (Protocol == null)
            {
                Logger.LogError("Protocol hasn't been set!");
                return(false);
            }

            return(true);
        }
        public UdpSocketServer(IAppServer <TAppSession> appServer, IPEndPoint localEndPoint, ICustomProtocol <TCommandInfo> protocol)
            : base(appServer, localEndPoint)
        {
            m_Protocol = protocol;

            if (typeof(TCommandInfo).IsSubclassOf(typeof(UdpCommandInfo)))
            {
                m_SessionKeyFromCommandInfo = true;
                m_UdpCommandInfoReader      = m_Protocol.CreateCommandReader(this.AppServer);
            }
        }
Beispiel #4
0
 public AsyncStreamSocketServer(IAppServer <TAppSession> appServer, IPEndPoint localEndPoint, ICustomProtocol <TCommandInfo> protocol)
     : base(appServer, localEndPoint, protocol)
 {
 }
Beispiel #5
0
 public AppServer(ICustomProtocol <StringCommandInfo> protocol)
     : base(protocol)
 {
 }
Beispiel #6
0
        public ISocketServer CreateSocketServer <TAppSession, TCommandInfo>(IAppServer <TAppSession> appServer, System.Net.IPEndPoint localEndPoint, SocketBase.Config.IServerConfig config, ICustomProtocol <TCommandInfo> protocol)
            where TAppSession : IAppSession, IAppSession <TAppSession, TCommandInfo>, new()
            where TCommandInfo : SocketBase.Command.ICommandInfo
        {
            if (protocol == null)
            {
                throw new ArgumentNullException("protocol");
            }

            switch (config.Mode)
            {
            case (SocketMode.Udp):
                return(new UdpSocketServer <TAppSession, TCommandInfo>(appServer, localEndPoint, protocol));

            case (SocketMode.Sync):
                return(new SyncSocketServer <TAppSession, TCommandInfo>(appServer, localEndPoint, protocol));

            case (SocketMode.Async):
                if (string.IsNullOrEmpty(config.Security) || config.Security.Equals(m_SecurityNone, StringComparison.OrdinalIgnoreCase))
                {
                    return(new AsyncSocketServer <TAppSession, TCommandInfo>(appServer, localEndPoint, protocol));
                }
                else
                {
                    return(new AsyncStreamSocketServer <TAppSession, TCommandInfo>(appServer, localEndPoint, protocol));
                }

            default:
                throw new NotSupportedException("Unsupported SocketMode:" + config.Mode);
            }
        }
        public ISocketServer CreateSocketServer <TAppSession, TCommandInfo>(IAppServer <TAppSession> appServer, System.Net.IPEndPoint localEndPoint, SocketBase.Config.IServerConfig config, ICustomProtocol <TCommandInfo> protocol)
            where TAppSession : IAppSession, IAppSession <TAppSession, TCommandInfo>, new()
            where TCommandInfo : SocketBase.Command.ICommandInfo
        {
            if (protocol == null)
            {
                throw new ArgumentNullException("protocol");
            }

            switch (config.Mode)
            {
            case (SocketMode.Udp):
                return(new UdpSocketServer <TAppSession, TCommandInfo>(appServer, localEndPoint, protocol));

            case (SocketMode.Sync):
                return(new SyncSocketServer <TAppSession, TCommandInfo>(appServer, localEndPoint, protocol));

            case (SocketMode.Async):
                return(new AsyncSocketServer <TAppSession, TCommandInfo>(appServer, localEndPoint, protocol));

            default:
                throw new NotSupportedException("Unsupported SocketMode:" + config.Mode);
            }
        }
Beispiel #8
0
 public AppServerBase(ICustomProtocol <TCommandInfo> protocol)
 {
     this.Protocol = protocol;
 }
Beispiel #9
0
        public virtual bool Setup(IRootConfig rootConfig, IServerConfig config, ISocketServerFactory socketServerFactory, ICustomProtocol <TCommandInfo> protocol)
        {
            if (rootConfig == null)
            {
                throw new ArgumentNullException("rootConfig");
            }

            RootConfig = rootConfig;

            if (!m_ThreadPoolConfigured)
            {
                if (!TheadPoolEx.ResetThreadPool(rootConfig.MaxWorkingThreads >= 0 ? rootConfig.MaxWorkingThreads : new Nullable <int>(),
                                                 rootConfig.MaxCompletionPortThreads >= 0 ? rootConfig.MaxCompletionPortThreads : new Nullable <int>(),
                                                 rootConfig.MinWorkingThreads >= 0 ? rootConfig.MinWorkingThreads : new Nullable <int>(),
                                                 rootConfig.MinCompletionPortThreads >= 0 ? rootConfig.MinCompletionPortThreads : new Nullable <int>()))
                {
                    return(false);
                }

                m_ThreadPoolConfigured = true;
            }

            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            Config       = config;
            Name         = config.Name;
            m_LogCommand = config.LogCommand;

            m_SocketServerFactory = socketServerFactory;

            SetupLogger();

            if (!SetupLocalEndpoint(config))
            {
                Logger.LogError("Invalid config ip/port");
                return(false);
            }

            if (!SetupProtocol(config, protocol))
            {
                return(false);
            }

            if (!SetupCommands(m_CommandDict))
            {
                return(false);
            }

            if (!SetupSecurity(config))
            {
                return(false);
            }

            return(SetupSocketServer());
        }
Beispiel #10
0
        public override bool Setup(IRootConfig rootConfig, IServerConfig config, ISocketServerFactory socketServerFactory, ICustomProtocol <BinaryCommandInfo> protocol)
        {
            var policyRequest = config.Options.GetValue("policyRequest");

            if (!string.IsNullOrEmpty(policyRequest))
            {
                m_PolicyRequest = policyRequest;
            }

            m_ExpectedReceivedLength = Encoding.UTF8.GetByteCount(m_PolicyRequest);

            this.Protocol = new FixSizeCommandProtocol(m_ExpectedReceivedLength);

            if (!base.Setup(rootConfig, config, socketServerFactory, protocol))
            {
                return(false);
            }

            m_PolicyFile = config.Options.GetValue("policyFile");

            if (string.IsNullOrEmpty(m_PolicyFile))
            {
                Logger.LogError("Configuration option policyFile is required!");
                return(false);
            }

            if (!Path.IsPathRooted(m_PolicyFile))
            {
                m_PolicyFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, m_PolicyFile);
            }

            if (!File.Exists(m_PolicyFile))
            {
                Logger.LogError("The specified policyFile doesn't exist! " + m_PolicyFile);
                return(false);
            }

            PolicyResponse = SetupPolicyResponse(File.ReadAllBytes(m_PolicyFile));

            this.CommandHandler += new CommandHandler <PolicySession, BinaryCommandInfo>(PolicyServer_CommandHandler);

            return(true);
        }
Beispiel #11
0
        public TcpSocketServerBase(IAppServer <TAppSession> appServer, IPEndPoint localEndPoint, ICustomProtocol <TCommandInfo> protocol)
            : base(appServer, localEndPoint, protocol)
        {
            uint dummy = 0;

            m_KeepAliveOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
            //whether enable KeepAlive
            BitConverter.GetBytes((uint)1).CopyTo(m_KeepAliveOptionValues, 0);
            //how long will start first keep alive
            BitConverter.GetBytes((uint)(appServer.Config.KeepAliveTime * 1000)).CopyTo(m_KeepAliveOptionValues, Marshal.SizeOf(dummy));
            //keep alive interval
            BitConverter.GetBytes((uint)(appServer.Config.KeepAliveInterval * 1000)).CopyTo(m_KeepAliveOptionValues, Marshal.SizeOf(dummy) * 2);
        }
        public override bool Setup(IRootConfig rootConfig, IServerConfig config, ISocketServerFactory socketServerFactory, ICustomProtocol <IWebSocketFragment> protocol)
        {
            if (!base.Setup(rootConfig, config, socketServerFactory, protocol))
            {
                return(false);
            }

            var users = config.GetChildConfig <UserConfigCollection>("users");

            if (users == null || users.Count <= 0)
            {
                Logger.LogError("No user defined");
                return(false);
            }

            m_UsersDict = new Dictionary <string, UserConfig>(StringComparer.OrdinalIgnoreCase);

            foreach (var u in users)
            {
                m_UsersDict.Add(u.Name, u);
            }

            return(true);
        }
Beispiel #13
0
        public override bool Setup(IRootConfig rootConfig, IServerConfig config, ISocketServerFactory socketServerFactory, ICustomProtocol <StringCommandInfo> protocol)
        {
            if (!base.Setup(rootConfig, config, socketServerFactory, protocol))
            {
                return(false);
            }

            int      interval         = config.Options.GetValue("MonitorInterval", "1").ToInt32();
            TimeSpan intervalTimeSpan = new TimeSpan(0, interval, 0);

            m_MonitorTimer = new Timer(new TimerCallback(OnMonitorTimerCallback), new object(), intervalTimeSpan, intervalTimeSpan);

            return(true);
        }
 public SyncSocketServer(IAppServer <TAppSession> appServer, IPEndPoint localEndPoint, ICustomProtocol <TCommandInfo> protocol)
     : base(appServer, localEndPoint)
 {
     m_Protocol = protocol;
 }