public override ProxyNetworkService Create(Logger logger)
        {
            ProxyNetworkService ret = null;

            if (logger == null)
            {
                logger = Logger.GetSystemLogger();
            }

            try
            {
                ProxyServer server = new PassThroughProxyServer(logger, _layers);

                ret = new ProxyNetworkService(_packets, new SerialPortProxyListener(ServerPort),
                                              _netGraph == null ? BuildDefaultProxyFactory() : _netGraph.Factory, logger, _globalMeta, _history,
                                              _credentials, server, new SerialPortProxyClient(ClientPort), null, Timeout.Infinite, false);

                ret.DefaultBinding = NetworkLayerBinding.ClientAndServer;
            }
            catch (Exception ex)
            {
                if (ex is NetServiceException)
                {
                    throw;
                }

                throw new NetServiceException("Error creating service", ex);
            }

            return(ret);
        }
Exemple #2
0
        /// <summary>
        /// Method to create the network service
        /// </summary>
        /// <param name="logger">The logger to use</param>
        /// <returns>The network service</returns>
        /// <exception cref="NetServiceException">Thrown if invalid configuration</exception>
        public override ProxyNetworkService Create(Logger logger)
        {
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            if (string.IsNullOrEmpty(ListenPath))
            {
                throw new NetServiceException(Properties.Resources.UnixSocketProxyTemplate_InvalidListenPath);
            }

            if (string.IsNullOrEmpty(ConnectPath))
            {
                throw new NetServiceException(Properties.Resources.UnixSocketProxyTemplate_InvalidConnectPath);
            }

            try
            {
                var server = new PassThroughProxyServer(logger, Layers);
                var client = new FixedStreamSocketProxyClient(new UnixEndPoint(ConnectPath), 0);
                if (File.Exists(ListenPath))
                {
                    File.Delete(ListenPath);
                }
                var listener = new StreamSocketNetworkListener(new UnixEndPoint(ListenPath), 0, logger);

                return(new ProxyNetworkService(listener,
                                               Graph ?? BuildDefaultProxyFactory(), logger, _globalMeta,
                                               server, client, null, Timeout.Infinite, false)
                {
                    DefaultBinding = NetworkLayerBinding.ClientAndServer
                });
            }
            catch (SocketException ex)
            {
                throw new NetServiceException(Properties.Resources.UnixSocketProxyTemplate_ErrorCreatingProxy, ex);
            }
            catch (IOException ex)
            {
                throw new NetServiceException(Properties.Resources.UnixSocketProxyTemplate_ErrorCreatingProxy, ex);
            }
        }
        /// <summary>
        /// Create the base service
        /// </summary>
        /// <param name="logger">The logger to use</param>
        /// <returns>The new network service</returns>
        public override ProxyNetworkService Create(Logger logger)
        {
            ProxyNetworkService ret = null;

            if (logger == null)
            {
                logger = Logger.SystemLogger;
            }

            if ((LocalPort < 0) || (LocalPort > 65535))
            {
                throw new NetServiceException(Properties.Resources.NetServerDocument_ValidPort);
            }
            else if (ServerFactory == null)
            {
                throw new NetServiceException(Properties.Resources.NetServerDocument_MustSpecifyServer);
            }
            else
            {
                try
                {
                    ProxyServer server = new PassThroughProxyServer(logger, Layers);

                    ProxyClient client = new NetServerProxyClient(ServerFactory);

                    INetworkListener listener = null;

                    if (!UdpEnable)
                    {
                        if (NetUtils.OSSupportsIPv4)
                        {
                            listener = new TcpNetworkListener(AnyBind, false, LocalPort, logger, false);
                        }

                        if (Ipv6Bind && NetUtils.OSSupportsIPv6)
                        {
                            INetworkListener ipv6Listener = new TcpNetworkListener(AnyBind, true, LocalPort, logger, false);

                            if (listener != null)
                            {
                                listener = new AggregateNetworkListener(listener, ipv6Listener);
                            }
                            else
                            {
                                listener = ipv6Listener;
                            }
                        }
                    }
                    else
                    {
                        if (NetUtils.OSSupportsIPv4)
                        {
                            listener = new UdpNetworkListener(AnyBind, false, LocalPort, EnableBroadcast, logger);
                        }

                        if (Ipv6Bind && NetUtils.OSSupportsIPv6)
                        {
                            INetworkListener ipv6Listener = new UdpNetworkListener(AnyBind, true, LocalPort, EnableBroadcast, logger);

                            if (listener != null)
                            {
                                listener = new AggregateNetworkListener(listener, ipv6Listener);
                            }
                            else
                            {
                                listener = ipv6Listener;
                            }
                        }
                    }

                    if (listener == null)
                    {
                        throw new NetServiceException(Properties.Resources.NetServiceDocument_CannotSetupListener);
                    }

                    ret = new ProxyNetworkService(listener,
                                                  Graph ?? BuildDefaultProxyFactory(), logger, _globalMeta,
                                                  server, client, null, Timeout.Infinite, false);

                    ret.DefaultBinding = NetworkLayerBinding.Server;
                }
                catch (SocketException ex)
                {
                    throw new NetServiceException(Properties.Resources.NetServerDocument_ErrorCreatingServer, ex);
                }
                catch (IOException ex)
                {
                    throw new NetServiceException(Properties.Resources.NetServerDocument_ErrorCreatingServer, ex);
                }

                return(ret);
            }
        }
Exemple #4
0
        /// <summary>
        /// Create the base service
        /// </summary>
        /// <param name="logger">The logger to use</param>
        /// <returns>The new network service</returns>
        public override ProxyNetworkService Create(Logger logger)
        {
            ProxyNetworkService ret = null;

            if (logger == null)
            {
                logger = Logger.GetSystemLogger();
            }

            if ((_port < 0) || (_port > 65535))
            {
                throw new NetServiceException(Properties.Resources.NetServerDocument_ValidPort);
            }
            else if (_factory == null)
            {
                throw new NetServiceException(Properties.Resources.NetServerDocument_MustSpecifyServer);
            }
            else
            {
                try
                {
                    ProxyServer server = new PassThroughProxyServer(logger, _layers);

                    ProxyClient client = new NetServerProxyClient(_factory);

                    INetworkListener listener = null;

                    if (!_udp)
                    {
                        if (NetUtils.OSSupportsIPv4)
                        {
                            listener = new TcpNetworkListener(_anyBind, false, _port, logger, false);
                        }

                        if (_ipv6Bind && NetUtils.OSSupportsIPv6)
                        {
                            INetworkListener ipv6Listener = new TcpNetworkListener(_anyBind, true, _port, logger, false);

                            if (listener != null)
                            {
                                listener = new AggregateNetworkListener(listener, ipv6Listener);
                            }
                            else
                            {
                                listener = ipv6Listener;
                            }
                        }
                    }
                    else
                    {
                        if (NetUtils.OSSupportsIPv4)
                        {
                            listener = new UdpNetworkListener(_anyBind, false, _port, _enableBroadcast, logger);
                        }

                        if (_ipv6Bind && NetUtils.OSSupportsIPv6)
                        {
                            INetworkListener ipv6Listener = new UdpNetworkListener(_anyBind, true, _port, _enableBroadcast, logger);

                            if (listener != null)
                            {
                                listener = new AggregateNetworkListener(listener, ipv6Listener);
                            }
                            else
                            {
                                listener = ipv6Listener;
                            }
                        }
                    }

                    if (listener == null)
                    {
                        throw new NetServiceException(CANAPE.Documents.Properties.Resources.NetServiceDocument_CannotSetupListener);
                    }

                    ret = new ProxyNetworkService(
                        _packets,
                        listener,
                        _netGraph == null ? BuildDefaultProxyFactory() : _netGraph.Factory, logger, _globalMeta, _history,
                        _credentials, server, client, null, Timeout.Infinite, false);

                    ret.DefaultBinding = NetworkLayerBinding.Server;
                }
                catch (SocketException ex)
                {
                    throw new NetServiceException(Properties.Resources.NetServerDocument_ErrorCreatingServer, ex);
                }
                catch (IOException ex)
                {
                    throw new NetServiceException(Properties.Resources.NetServerDocument_ErrorCreatingServer, ex);
                }

                return(ret);
            }
        }