Esempio n. 1
0
        /// <summary>
        /// Connect to the specified endpoint and return a stream that can be used to communicate with it. (for initiator)
        /// </summary>
        /// <param name="endpoint">The endpoint.</param>
        /// <param name="settings">The socket settings.</param>
        /// <param name="logger">Logger to use.</param>
        /// <returns>an opened and initiated stream which can be read and written to</returns>
        public static Stream CreateClientStream(IPEndPoint endpoint, SocketSettings settings, ILog logger)
        {
            // If system has configured a proxy for this config, use it.
            Socket socket = CreateTunnelThruProxy(endpoint.Address.ToString(), endpoint.Port);

            // No proxy.  Set up a regular socket.
            if (socket == null)
            {
                socket         = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.NoDelay = settings.SocketNodelay;
                if (settings.SocketReceiveBufferSize.HasValue)
                {
                    socket.ReceiveBufferSize = settings.SocketReceiveBufferSize.Value;
                }
                if (settings.SocketSendBufferSize.HasValue)
                {
                    socket.SendBufferSize = settings.SocketSendBufferSize.Value;
                }
                socket.Connect(endpoint);
            }

            Stream stream = new NetworkStream(socket, true);

            if (settings.UseSSL)
            {
                stream = new SSLStreamFactory(logger, settings)
                         .CreateClientStreamAndAuthenticate(stream);
            }

            return(stream);
        }
Esempio n. 2
0
        /// <summary>
        /// Connect to the specified endpoint and return a stream that can be used to communicate with it. (for initiator)
        /// </summary>
        /// <param name="endpoint">The endpoint.</param>
        /// <param name="settings">The socket settings.</param>
        /// <param name="logger">Logger to use.</param>
        /// <returns>an opened and initiated stream which can be read and written to</returns>
        public static Stream CreateClientStream(IPEndPoint endpoint, SocketSettings settings, ILog logger)
        {
            // var socket = GetProxySocket();
            // socket.Disconnect(true);

            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.NoDelay = settings.SocketNodelay;
            if (settings.SocketReceiveBufferSize.HasValue)
            {
                socket.ReceiveBufferSize = settings.SocketReceiveBufferSize.Value;
            }
            if (settings.SocketSendBufferSize.HasValue)
            {
                socket.SendBufferSize = settings.SocketSendBufferSize.Value;
            }
            socket.Connect(new IPEndPoint(new IPAddress(new byte[4] {
                172, 16, 13, 11
            }), 3128));
            //socket.Connect(endpoint);
            Stream stream = new NetworkStream(socket, true);

            if (settings.UseSSL)
            {
                stream = new SSLStreamFactory(logger, settings)
                         .CreateClientStreamAndAuthenticate(stream);
            }

            return(stream);
        }
Esempio n. 3
0
        /// <summary>
        /// Initiate communication to the remote client and return a stream that can be used to communicate with it. (for acceptor)
        /// </summary>
        /// <param name="tcpClient">The TCP client.</param>
        /// <param name="settings">The socket settings.</param>
        /// <param name="logger">Logger to use.</param>
        /// <returns>an opened and initiated stream which can be read and written to</returns>
        /// <exception cref="System.ArgumentException">tcp client must be connected in order to get stream;tcpClient</exception>
        public static Stream CreateServerStream(TcpClient tcpClient, SocketSettings settings, ILog logger)
        {
            if (tcpClient.Connected == false)
                throw new ArgumentException("tcp client must be connected in order to get stream", "tcpClient");

            Stream stream = tcpClient.GetStream();
            if (settings.UseSSL)
            {
                stream = new SSLStreamFactory(logger, settings)
                                .CreateServerStreamAndAuthenticate(stream);
            }

            return stream;
        }
Esempio n. 4
0
        /// <summary>
        /// Connect to the specified endpoint and return a stream that can be used to communicate with it. (for initiator)
        /// </summary>
        /// <param name="endpoint">The endpoint.</param>
        /// <param name="settings">The socket settings.</param>
        /// <param name="logger">Logger to use.</param>
        /// <returns>an opened and initiated stream which can be read and written to</returns>
        public static Stream CreateClientStream(IPEndPoint endpoint, SocketSettings settings, ILog logger)
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.NoDelay = settings.SocketNodelay;
            socket.Connect(endpoint);
            Stream stream = new NetworkStream(socket, true);

            if (settings.UseSSL)
            {
                stream = new SSLStreamFactory(logger, settings)
                                .CreateClientStreamAndAuthenticate(stream);
            }

            return stream;
        }
Esempio n. 5
0
        /// <summary>
        /// Connect to the specified endpoint and return a stream that can be used to communicate with it. (for initiator)
        /// </summary>
        /// <param name="endpoint">The endpoint.</param>
        /// <param name="settings">The socket settings.</param>
        /// <param name="logger">Logger to use.</param>
        /// <returns>an opened and initiated stream which can be read and written to</returns>
        public static Stream CreateClientStream(IPEndPoint endpoint, SocketSettings settings, ILog logger)
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.NoDelay = settings.SocketNodelay;
            socket.Connect(endpoint);
            Stream stream = new NetworkStream(socket, ownsSocket: true);

            if (settings.UseSSL)
            {
                stream = new SSLStreamFactory(logger, settings)
                         .CreateClientStreamAndAuthenticate(stream);
            }

            return(stream);
        }
Esempio n. 6
0
        /// <summary>
        /// Initiate communication to the remote client and return a stream that can be used to communicate with it. (for acceptor)
        /// </summary>
        /// <param name="tcpClient">The TCP client.</param>
        /// <param name="settings">The socket settings.</param>
        /// <param name="logger">Logger to use.</param>
        /// <returns>an opened and initiated stream which can be read and written to</returns>
        /// <exception cref="System.ArgumentException">tcp client must be connected in order to get stream;tcpClient</exception>
        public static Stream CreateServerStream(TcpClient tcpClient, SocketSettings settings, ILog logger)
        {
            if (tcpClient.Connected == false)
            {
                throw new ArgumentException("tcp client must be connected in order to get stream", "tcpClient");
            }

            Stream stream = tcpClient.GetStream();

            if (settings.UseSSL)
            {
                stream = new SSLStreamFactory(logger, settings)
                         .CreateServerStreamAndAuthenticate(stream);
            }

            return(stream);
        }