Exemplo n.º 1
0
        private static IProxyChannel <TContractType> CreateProxyChannel <TContractType>(CustomizedMessageHeaderData pHeaderData, string hostName) where TContractType : class
        {
            ServiceEndpoint endpoint = EndpointProvider.GetEndpoint <TContractType>(hostName);
            IProxyChannel <TContractType> channel = new ProxyChannel <TContractType>(endpoint, pHeaderData);

            return(channel);
        }
Exemplo n.º 2
0
        static async Task Main(string[] args)
        {
            var encoder = new ProxyPackageEncoder();
            var decoder = new ProxyPackageDecoder();
            var socket  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            var endpoint = new IPEndPoint(IPAddress.Any, 9091);

            socket.Bind(endpoint);

            socket.Listen(10);
            Console.WriteLine($"开始监听:{endpoint}");
            var factory = new LoggerFactory();
            var logger  = factory.CreateLogger("");

            while (true)
            {
                var nscoket = await socket.AcceptAsync();

                Console.WriteLine($"连接成功:{nscoket.RemoteEndPoint}");
                var channel = new ProxyChannel <ProxyPackage>(nscoket, encoder, decoder, logger, new ChannelOptions());
                channel.PackageReceived += async(channl, package) =>
                {
                    Console.WriteLine($"接收到一个数据包:{package},内容:{Encoding.UTF8.GetString(package.Data)}");
                    var reply = new ProxyPackage
                    {
                        Data = Encoding.UTF8.GetBytes("你好呀")
                    };
                    await channel.SendAsync(package);
                };
                await channel.StartAsync();
            }
        }
Exemplo n.º 3
0
        public ProxyChannel ToModel()
        {
            ProxyChannel model = new ProxyChannel();

            model.Name       = this.Name;
            model.ListenPort = this.ListenPort;
            model.RemoteIP   = this.RemoteIP;
            model.RemotePort = this.RemotePort;
            return(model);
        }
Exemplo n.º 4
0
        public static ProxyChannelItem FromModel(ProxyChannel model)
        {
            ProxyChannelItem tempVM = new ProxyChannelItem();

            tempVM.Name       = model.Name;
            tempVM.ListenPort = model.ListenPort;
            tempVM.RemoteIP   = model.RemoteIP;
            tempVM.RemotePort = model.RemotePort;
            return(tempVM);
        }
Exemplo n.º 5
0
        public void Proxy_WhenCallingMethodThatReturnsNull_DoesNotThrowException()
        {
            WcfChannelProxyTypeBuilder builder = new WcfChannelProxyTypeBuilder(typeof(ProxyChannel <IService>));

            Type            t = builder.BuildProxyType();
            ConstructorInfo constructorInfo = t.GetConstructors()[0];

            ProxyChannel <IService> channel = (ProxyChannel <IService>)constructorInfo.Invoke(null);

            channel.ChannelManager = new StubChannelManager();
            ((IService)channel).ReturnComplexObject();
        }
Exemplo n.º 6
0
        public async Task StartAsync()
        {
            var endpoint     = new IPEndPoint(serverAddress, serverPort);
            var serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            await serverSocket.ConnectAsync(endpoint);

            proxyChannel = new ProxyChannel <ProxyPackage>(serverSocket, encoder, decoder, logger, channelOptions);
            proxyChannel.PackageReceived += OnPackageReceived;
            proxyChannel.Closed          += OnProxyChannelClosed;
            var task = proxyChannel.StartAsync();
            await proxyChannel.SendAsync(new ProxyPackage
            {
                ChannelId = channelConfig.ChannelId,
                Type      = PackageType.Connect
            });

            await task;
        }
Exemplo n.º 7
0
        private async Task StartProxyServer()
        {
            var socket   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            var endpoint = new IPEndPoint(IPAddress.Any, options.ServerPort);

            socket.Bind(endpoint);
            socket.Listen(options.MaxConnection);
            await Task.Factory.StartNew(async() =>
            {
                logger.LogInformation($"ProxyServer listen on : {endpoint.Port}");
                while (true)
                {
                    var clientSocket              = await socket.AcceptAsync();
                    var proxyChannel              = new ProxyChannel <ProxyPackage>(clientSocket, encoder, decoder, logger, new ChannelOptions());
                    proxyChannel.PackageReceived += OnPackageReceived;
                    proxyChannel.Closed          += OnProxyClosedAsync;
                    _ = proxyChannel.StartAsync();
                }
            });
        }
Exemplo n.º 8
0
        static async Task Main(string[] args)
        {
            var encoder = new ProxyPackageEncoder();
            var decoder = new ProxyPackageDecoder();
            var socket  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            var endpoint = new IPEndPoint(IPAddress.Loopback, 9091);

            await socket.ConnectAsync(endpoint);

            var factory = new LoggerFactory();
            var logger  = factory.CreateLogger("");
            var options = new ChannelOptions();
            var channel = new ProxyChannel <ProxyPackage>(socket, encoder, decoder, logger, options);

            channel.PackageReceived += async(channl, package) =>
            {
                Console.WriteLine($"接收到一个数据包:{package},内容:{Encoding.UTF8.GetString(package.Data)}");
                await Task.CompletedTask;
            };

            _ = channel.StartAsync();
            var c = 0;

            while (c < 100000)
            {
                var package = new ProxyPackage
                {
                    ChannelId    = 1,
                    ConnectionId = 100,
                    Type         = PackageType.Transfer,
                    Data         = Encoding.UTF8.GetBytes(c.ToString())
                };

                await channel.SendAsync(package);

                c++;
            }

            Console.ReadKey();
        }
Exemplo n.º 9
0
        /// <summary>
        /// 启动一个通道,用于客户端和服务端做认证用【TODO:可以保持连接,使得不用重启客户端配置修改能够生效】
        /// </summary>
        /// <returns></returns>
        private async Task StartAuthenticationAsync()
        {
            var endpoint   = new IPEndPoint(IPAddress.Parse(options.ServerAddress), options.ServerPort);
            var authSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            await authSocket.ConnectAsync(endpoint);

            var authChannel = new ProxyChannel <ProxyPackage>(authSocket, encoder, decoder, logger, channelOptions);

            authChannel.PackageReceived += OnAuthPackageReceived;

            AuthChannelTask = authChannel.StartAsync();

            await authChannel.SendAsync(new ProxyPackage
            {
                Type = PackageType.Authentication,
                Data = new AuthenticationModel
                {
                    ClientId  = options.ClientId,
                    SecretKey = options.SecretKey
                }.ObjectToBytes()
            });
        }