コード例 #1
0
ファイル: TestSocketServer.cs プロジェクト: xxjeng/nuxleus
        public void TestECHO()
        {
            var bootstrap = new DefaultBootstrap();

            IServerConfig config = new ServerConfig
            {
                Name = "My Custom Server",
                Ip = "Any",
                Port = 100,
                Mode = SocketMode.Tcp,
                MaxConnectionNumber = 1
            };

            var rootConfig = new RootConfig();

            YourServer server = new YourServer();

            bootstrap.Initialize(rootConfig, new IAppServer[] { server }, new IServerConfig[] { config }, new ConsoleLogFactory());

            bootstrap.Start();

            EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), config.Port);

            using (Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.Connect(serverAddress);
                Stream socketStream = new NetworkStream(socket);
                using (StreamReader reader = new StreamReader(socketStream, Encoding.Default, true))
                using (StreamWriter writer = new StreamWriter(socketStream, Encoding.Default, 1024 * 8))
                {
                    //ignore welcome message
                    reader.ReadLine();

                    string command = "CMD:ECHO ";
                    string[] parameters = new string[] { "Kerry", "Jiang", "China", "Shanghai" };
                    string parameter = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Join(" ", parameters)));
                    writer.WriteLine(command + parameter);
                    writer.Flush();

                    foreach (var p in parameters)
                    {
                        string param = reader.ReadLine();
                        Console.WriteLine(param);
                        Assert.AreEqual(p, param);
                    }
                }
            }

            bootstrap.Stop();
        }
コード例 #2
0
        public override void Setup()
        {
            m_Bootstrap = new DefaultBootstrap();

            m_Encoding = new UTF8Encoding();

            m_WebSocketServer = new WebSocketServer(new BasicSubProtocol("Basic", new List<Assembly>{ this.GetType().Assembly } ));

            m_Bootstrap.Initialize(new RootConfig { DisablePerformanceDataCollector = true }, new IAppServer[] { m_WebSocketServer }, new IServerConfig[] { new ServerConfig
                {
                    Port = 2012,
                    Ip = "Any",
                    MaxConnectionNumber = 100,
                    Mode = SocketMode.Tcp,
                    Name = "SuperWebSocket Server"
                }}, new ConsoleLogFactory());
        }
コード例 #3
0
ファイル: SocketServerTest.cs プロジェクト: xxjeng/nuxleus
        public void Setup()
        {
            if (m_Servers.ContainsKey(m_Config))
                return;

            var serverX = new TestServer();
            var serverY = new TestServer(new TestCommandParser());

            IBootstrap bootstrap = new DefaultBootstrap();
            bootstrap.Initialize(m_RootConfig, new IAppServer[] { serverX, serverY }, new IServerConfig[] { m_Config, m_Config });

            m_Servers[m_Config] = new TestServer[]
            {
                serverX,
                serverY
            };
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: xxjeng/nuxleus
        static void RunAsConsole()
        {
            IBootstrap bootstrap = new DefaultBootstrap();

            SocketServiceConfig serverConfig = ConfigurationManager.GetSection("socketServer") as SocketServiceConfig;
            if (!bootstrap.Initialize(serverConfig))
            {
                Console.WriteLine("Failed to initialize SuperSocket server! Please check error log for more information!");
                return;
            }

            var result = bootstrap.Start();

            switch(result)
            {
                case(StartResult.None):
                    Console.WriteLine("No server is configured, please check you configuration!");
                    break;

                case(StartResult.Success):
                    Console.WriteLine("The server has been started!");
                    break;

                case (StartResult.Failed):
                    Console.WriteLine("Failed to start SuperSocket server! Please check error log for more information!");
                    break;

                case (StartResult.PartialSuccess):
                    Console.WriteLine("Some server instances were started successfully, but the others failed to start! Please check error log for more information!");
                    break;
            }

            Console.WriteLine("Press key 'q' to stop the server.");

            while (Console.ReadKey().Key != ConsoleKey.Q)
            {
                Console.WriteLine();
                continue;
            }

            bootstrap.Stop();

            Console.WriteLine();
            Console.WriteLine("The server has been stopped!");
        }
コード例 #5
0
        /// <summary>
        /// Creates the bootstrap.
        /// </summary>
        /// <param name="config">The config.</param>
        /// <returns></returns>
        public static IBootstrap CreateBootstrap(IConfigurationSource config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            IBootstrap bootstrap;

            bootstrap = new DefaultBootstrap(config);

            var section = config as ConfigurationSection;

            if (section != null)
            {
                ConfigurationWatcher.Watch(section, bootstrap);
            }

            return(bootstrap);
        }
コード例 #6
0
        /// <summary>
        /// Creates the bootstrap.
        /// </summary>
        /// <param name="config">The config.</param>
        /// <returns></returns>
        public static IBootstrap CreateBootstrap(IConfigurationSource config)
        {
            if (config == null)
                throw new ArgumentNullException("config");

            IBootstrap bootstrap;

            if (config.Isolation == IsolationMode.AppDomain)
                bootstrap = new AppDomainBootstrap(config);
            else if (config.Isolation == IsolationMode.Process)
                bootstrap = new ProcessBootstrap(config);
            else
                bootstrap = new DefaultBootstrap(config);

            var section = config as ConfigurationSection;

            if (section != null)
                ConfigurationWatcher.Watch(section, bootstrap);

            return bootstrap;
        }
コード例 #7
0
ファイル: SocketServerTest.cs プロジェクト: xxjeng/nuxleus
        private bool TestMaxConnectionNumber(int maxConnectionNumber)
        {
            var server = new TestServer();
            var defaultConfig = DefaultServerConfig;

            var config = new ServerConfig
            {
                Ip = defaultConfig.Ip,
                LogCommand = defaultConfig.LogCommand,
                MaxConnectionNumber = maxConnectionNumber,
                Mode = defaultConfig.Mode,
                Name = defaultConfig.Name,
                Port = defaultConfig.Port,
                Security = defaultConfig.Security,
                Certificate = defaultConfig.Certificate
            };

            var bootstrap = new DefaultBootstrap();

            bootstrap.Initialize(m_RootConfig, new IAppServer[] { server }, new IServerConfig[] { config });

            List<Socket> sockets = new List<Socket>();

            try
            {
                bootstrap.Start();

                EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), m_Config.Port);

                for (int i = 0; i < maxConnectionNumber; i++)
                {
                    Socket socket = CreateClientSocket();
                    socket.Connect(serverAddress);
                    Stream socketStream = GetSocketStream(socket);
                    StreamReader reader = new StreamReader(socketStream, m_Encoding, true);
                    reader.ReadLine();
                    sockets.Add(socket);
                }

                try
                {
                    using (Socket trySocket = CreateClientSocket())
                    {
                        trySocket.Connect(serverAddress);
                        var innerSocketStream = new NetworkStream(trySocket);
                        innerSocketStream.ReadTimeout = 500;

                        using (StreamReader tryReader = new StreamReader(innerSocketStream, m_Encoding, true))
                        {
                            string welcome = tryReader.ReadLine();
                            Console.WriteLine(welcome);
                            return true;
                        }
                    }
                }
                catch (Exception)
                {
                    return true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + " " + e.StackTrace);
                return false;
            }
            finally
            {
                bootstrap.Stop();
            }
        }