Example #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Server");
            TcpServer server = new TcpServer(8000, new ClientHandler());

            server.Start();
            while (true)
            {
                string s = "7";
            }
            //server.Stop();
        }
Example #2
0
        static void Main(string[] args)
        {
            serverHandler = new TcpServer(300, 1024);

            serverHandler.Init();
            serverHandler.ServerStopedEvent += m_socket_ServerStopedEvent;


            int  port    = 50000;
            bool isStart = serverHandler.Start(new IPEndPoint(IPAddress.Any, port));

            if (isStart)
            {
                Console.WriteLine("服务器已启动 \r\n");
                serverHandler.ClientNumberChange += m_socket_ClientNumberChange;
                serverHandler.ReceiveClientData  += m_socket_ReceiveClientData;
            }


            Log.Info("Hello World!");
        }
 private void FileReceiverThread()
 {
     FRProvider = new FileReceiver();
     FRServidor = new TcpServer(FRProvider, 7004);
     FRServidor.Start();
 }
 private void FileServerThread()
 {
     FSProvider = new FileServer();
     FSServidor = new TcpServer(FSProvider, 7005);
     FSServidor.Start();
 }
Example #5
0
        private static IServer StartProtocol(Configuration c, ChatProtocolConfiguration pc, ServerContext ServerContext, TaskFactory Factory, TaskFactory PurifierFactory)
        {
            if (pc.OnTcp)
            {
                var s = pc.Tcp;

                if (!(s.SerializationProtocolType == SerializationProtocolType.Binary || s.SerializationProtocolType == SerializationProtocolType.Json))
                {
                    throw new InvalidOperationException("未知协议类型: " + s.SerializationProtocolType.ToString());
                }

                Func <ISessionContext, IBinaryTransformer, KeyValuePair <IServerImplementation, IStreamedVirtualTransportServer> > VirtualTransportServerFactory;
                if (s.SerializationProtocolType == SerializationProtocolType.Binary)
                {
                    VirtualTransportServerFactory = (Context, t) =>
                    {
                        var p    = ServerContext.CreateServerImplementationWithBinaryAdapter(Factory, Context);
                        var si   = p.Key;
                        var a    = p.Value;
                        var bcps = new BinaryCountPacketServer(a, CommandName => true, t);
                        return(new KeyValuePair <IServerImplementation, IStreamedVirtualTransportServer>(si, bcps));
                    };
                }
                else if (s.SerializationProtocolType == SerializationProtocolType.Json)
                {
                    VirtualTransportServerFactory = (Context, t) =>
                    {
                        var p    = ServerContext.CreateServerImplementationWithJsonAdapter(Factory, Context);
                        var si   = p.Key;
                        var a    = p.Value;
                        var bcps = new JsonLinePacketServer(a, CommandName => true, t);
                        return(new KeyValuePair <IServerImplementation, IStreamedVirtualTransportServer>(si, bcps));
                    };
                }
                else
                {
                    throw new InvalidOperationException();
                }

                var Server  = new TcpServer(ServerContext, VirtualTransportServerFactory, a => Factory.StartNew(a), a => PurifierFactory.StartNew(a));
                var Success = false;

                try
                {
                    Server.Bindings           = s.Bindings.Select(b => new IPEndPoint(IPAddress.Parse(b.IpAddress), b.Port)).ToArray();
                    Server.SessionIdleTimeout = s.SessionIdleTimeout;
                    Server.UnauthenticatedSessionIdleTimeout = s.UnauthenticatedSessionIdleTimeout;
                    Server.MaxConnections          = s.MaxConnections;
                    Server.MaxConnectionsPerIP     = s.MaxConnectionsPerIP;
                    Server.MaxUnauthenticatedPerIP = s.MaxUnauthenticatedPerIP;
                    Server.MaxBadCommands          = s.MaxBadCommands;

                    Server.Start();

                    Console.WriteLine(@"TCP/{0}服务器已启动。结点: {1}".Formats(s.SerializationProtocolType.ToString(), String.Join(", ", Server.Bindings.Select(b => b.ToString() + "(TCP)"))));

                    Success = true;
                }
                finally
                {
                    if (!Success)
                    {
                        Server.Dispose();
                    }
                }

                return(Server);
            }
            else if (pc.OnUdp)
            {
                var s = pc.Udp;

                if (!(s.SerializationProtocolType == SerializationProtocolType.Binary || s.SerializationProtocolType == SerializationProtocolType.Json))
                {
                    throw new InvalidOperationException("未知协议类型: " + s.SerializationProtocolType.ToString());
                }

                Func <ISessionContext, IBinaryTransformer, KeyValuePair <IServerImplementation, IStreamedVirtualTransportServer> > VirtualTransportServerFactory;
                if (s.SerializationProtocolType == SerializationProtocolType.Binary)
                {
                    VirtualTransportServerFactory = (Context, t) =>
                    {
                        var p    = ServerContext.CreateServerImplementationWithBinaryAdapter(Factory, Context);
                        var si   = p.Key;
                        var a    = p.Value;
                        var bcps = new BinaryCountPacketServer(a, CommandName => true, t);
                        return(new KeyValuePair <IServerImplementation, IStreamedVirtualTransportServer>(si, bcps));
                    };
                }
                else if (s.SerializationProtocolType == SerializationProtocolType.Json)
                {
                    VirtualTransportServerFactory = (Context, t) =>
                    {
                        var p    = ServerContext.CreateServerImplementationWithJsonAdapter(Factory, Context);
                        var si   = p.Key;
                        var a    = p.Value;
                        var bcps = new JsonLinePacketServer(a, CommandName => true, t);
                        return(new KeyValuePair <IServerImplementation, IStreamedVirtualTransportServer>(si, bcps));
                    };
                }
                else
                {
                    throw new InvalidOperationException();
                }

                var Server  = new UdpServer(ServerContext, VirtualTransportServerFactory, a => Factory.StartNew(a), a => PurifierFactory.StartNew(a));
                var Success = false;

                try
                {
                    Server.Bindings           = s.Bindings.Select(b => new IPEndPoint(IPAddress.Parse(b.IpAddress), b.Port)).ToArray();
                    Server.SessionIdleTimeout = s.SessionIdleTimeout;
                    Server.UnauthenticatedSessionIdleTimeout = s.UnauthenticatedSessionIdleTimeout;
                    Server.MaxConnections          = s.MaxConnections;
                    Server.MaxConnectionsPerIP     = s.MaxConnectionsPerIP;
                    Server.MaxUnauthenticatedPerIP = s.MaxUnauthenticatedPerIP;
                    Server.MaxBadCommands          = s.MaxBadCommands;

                    Server.TimeoutCheckPeriod = s.TimeoutCheckPeriod;

                    Server.Start();

                    Console.WriteLine(@"UDP/{0}服务器已启动。结点: {1}".Formats(s.SerializationProtocolType.ToString(), String.Join(", ", Server.Bindings.Select(b => b.ToString() + "(UDP)"))));

                    Success = true;
                }
                finally
                {
                    if (!Success)
                    {
                        Server.Dispose();
                    }
                }

                return(Server);
            }
            else if (pc.OnHttp)
            {
                var s = pc.Http;

                Func <ISessionContext, KeyValuePair <IServerImplementation, IHttpVirtualTransportServer> > VirtualTransportServerFactory = Context =>
                {
                    var p    = ServerContext.CreateServerImplementationWithJsonAdapter(Factory, Context);
                    var si   = p.Key;
                    var a    = p.Value;
                    var jhps = new JsonHttpPacketServer(a, CommandName => true);
                    return(new KeyValuePair <IServerImplementation, IHttpVirtualTransportServer>(si, jhps));
                };

                var Server  = new HttpServer(ServerContext, VirtualTransportServerFactory, a => Factory.StartNew(a), a => PurifierFactory.StartNew(a));
                var Success = false;

                try
                {
                    Server.Bindings           = s.Bindings.Select(b => b.Prefix).ToArray();
                    Server.SessionIdleTimeout = s.SessionIdleTimeout;
                    Server.UnauthenticatedSessionIdleTimeout = s.UnauthenticatedSessionIdleTimeout;
                    Server.MaxConnections          = s.MaxConnections;
                    Server.MaxConnectionsPerIP     = s.MaxConnectionsPerIP;
                    Server.MaxUnauthenticatedPerIP = s.MaxUnauthenticatedPerIP;
                    Server.MaxBadCommands          = s.MaxBadCommands;

                    Server.TimeoutCheckPeriod = s.TimeoutCheckPeriod;
                    Server.ServiceVirtualPath = s.ServiceVirtualPath;

                    Server.Start();

                    Console.WriteLine(@"HTTP/{0}服务器已启动。结点: {1}".Formats("Json", String.Join(", ", Server.Bindings.Select(b => b.ToString()))));

                    Success = true;
                }
                finally
                {
                    if (!Success)
                    {
                        Server.Dispose();
                    }
                }

                return(Server);
            }
            else if (pc.OnHttpStatic)
            {
                var s = pc.HttpStatic;

                var Server  = new StaticHttpServer(ServerContext, a => Factory.StartNew(a), 8 * 1024);
                var Success = false;

                try
                {
                    Server.Bindings                = s.Bindings.Select(b => b.Prefix).ToArray();
                    Server.SessionIdleTimeout      = Math.Min(s.SessionIdleTimeout, s.UnauthenticatedSessionIdleTimeout);
                    Server.MaxConnections          = s.MaxConnections;
                    Server.MaxConnectionsPerIP     = s.MaxConnectionsPerIP;
                    Server.MaxBadCommands          = s.MaxBadCommands;
                    Server.MaxUnauthenticatedPerIP = s.MaxUnauthenticatedPerIP;

                    Server.ServiceVirtualPath = s.ServiceVirtualPath;
                    Server.PhysicalPath       = s.PhysicalPath;
                    Server.Indices            = s.Indices.Split(',').Select(Index => Index.Trim(' ')).Where(Index => Index != "").ToArray();

                    Server.Start();

                    Console.WriteLine(@"HTTP静态服务器已启动。结点: {0}".Formats(String.Join(", ", Server.Bindings.Select(b => b.ToString()))));

                    Success = true;
                }
                finally
                {
                    if (!Success)
                    {
                        Server.Dispose();
                    }
                }

                return(Server);
            }
            else
            {
                throw new InvalidOperationException("未知服务器类型: " + pc._Tag.ToString());
            }
        }