示例#1
0
        internal static TransportChannel connectTcps(string target,
                                                     Options options, IOWorker ioWorker)
        {
            IpComponents tcpComponents = parseTcps(target);

            TcpClient tcpClient = new System.Net.Sockets.TcpClient();

            Int32 timeout;

            if (options.tcpConnectTimeout > 0)
            {
                timeout = options.tcpConnectTimeout;
            }
            else
            {
                timeout = Timeout.Infinite;
            }

            IAsyncResult result = tcpClient.BeginConnect(tcpComponents.hostName, tcpComponents.port, null, null);
            bool         connectionAttempted = result.AsyncWaitHandle.WaitOne(timeout);

            if (!connectionAttempted || !tcpClient.Connected)
            {
                throw new YAMIIOException("Cannot create SSL connection");
            }

            SslStream ssl = new SslStream(tcpClient.GetStream(), false,
                                          new RemoteCertificateValidationCallback(
                                              (object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors errors) =>
            {
                return(true);
            })
                                          );

            try
            {
                ssl.AuthenticateAsClient(tcpComponents.hostName, new X509CertificateCollection(),
                                         System.Security.Authentication.SslProtocols.Default, false);
            }
            catch (AuthenticationException)
            {
                throw new YAMIIOException("SSL authentication error");
            }

            tcpClient.NoDelay = options.tcpNoDelay;
            tcpClient.Client.SetSocketOption(
                SocketOptionLevel.Socket,
                SocketOptionName.KeepAlive,
                options.tcpKeepAlive ? 1 : 0);

            return(new TransportChannel(ssl, options.tcpFrameSize, ioWorker));
        }
示例#2
0
        internal static TransportChannel connectTcp(string target,
                                                    Options options)
        {
            IpComponents tcpComponents = parseTcp(target);

            Socket connection = CreateTCPSocket();

            connection.Blocking = false;
            try
            {
                connection.Connect(
                    tcpComponents.hostName, tcpComponents.port);
            }
            catch (SocketException ex)
            {
                // ignore if operaion in progress
                if (ex.SocketErrorCode != SocketError.WouldBlock &&
                    ex.SocketErrorCode != SocketError.AlreadyInProgress &&
                    ex.SocketErrorCode != SocketError.IsConnected)
                {
                    throw ex;
                }
            }
            // convert milliseconds to microseconds expected by
            // Socket.Select()
            int timeout = options.tcpConnectTimeout * 1000;

            if (timeout == 0) // Sockets.Select needs -1 for infinite wait
            {
                timeout = -1;
            }

            if (!connection.Connected)
            {
                connection.Blocking = true;
                List <Socket> writeable = new List <Socket>();
                writeable.Add(connection);
                List <Socket> error = new List <Socket>();
                error.Add(connection);
                Socket.Select(null, writeable, error, timeout);
                if (writeable.Count == 0)
                {
                    throw new SocketException(WSAETIMEDOUT);
                }
                connection.Blocking = false;
            }

            configureTcpChannel(connection, options);

            return(new TransportChannel(connection));
        }
示例#3
0
        private static TcpListener prepareTcpServer(string target,
                                                    IncomingMessageDispatchCallback incomingMessageDispatchCallback,
                                                    Options options,
                                                    LogCallback logCallback, LogEventArgs.LogLevel logLevel)
        {
            IpComponents tcpComponents = parseTcp(target);

            string hostName = tcpComponents.hostName;
            int    port     = tcpComponents.port;

            Socket s = CreateTCPSocket();

            IPEndPoint address;
            string     boundHostName;

            if (hostName == "*")
            {
                // bind to the wildcard local address
                // and resolve to the local hostname
                address       = new IPEndPoint(IPAddress.Any, port);
                boundHostName = Dns.GetHostName();
            }
            else
            {
                //TODO: translation for empty result (if possible)
                address = new IPEndPoint(
                    Dns.GetHostAddresses(hostName)[0], port);
                boundHostName = hostName;
            }

            s.SetSocketOption(
                SocketOptionLevel.Socket,
                SocketOptionName.ReuseAddress,
                options.tcpReuseAddress ? 1 : 0);
            s.Blocking = false;

            s.Bind(address); // , options.tcpListenBacklog);
            s.Listen(options.tcpListenBacklog);

            // get the actual address of this server socket

            int boundPort = ((IPEndPoint)s.LocalEndPoint).Port;

            string resolvedTarget =
                formatTcpTarget(boundHostName, boundPort);

            return(new TcpListener(s, resolvedTarget,
                                   incomingMessageDispatchCallback, options,
                                   logCallback, logLevel));
        }
示例#4
0
        internal static TransportChannel createUdp(
            string target, Options options)
        {
            IpComponents udpComponents = parseUdp(target);

            Socket ch = CreateUDPSocket();

            configureUdpChannel(ch, options);

            // remember the target address, so that it can be used
            // for each sent message
            IPEndPoint address = new IPEndPoint(
                Dns.GetHostAddresses(udpComponents.hostName)[0],
                udpComponents.port);

            return(new TransportChannel(ch, address));
        }