예제 #1
0
        public StreamingAPIConnector ConnectStreaming()
        {
            if (this.streamingConnector != null)
            {
                this.streamingConnector.Disconnect();
            }

            this.streamingConnector = new StreamingAPIConnector(this.server);
            return(this.streamingConnector);
        }
예제 #2
0
        public StreamingAPIConnector ConnectStreaming()
        {
            if (streamingConnector != null)
            {
                streamingConnector.Disconnect();
            }

            streamingConnector = new StreamingAPIConnector(server);
            return(streamingConnector);
        }
예제 #3
0
        /// <summary>
        /// Connects to the remote server.
        /// </summary>
        /// <param name="server">Server data</param>
        /// <param name="lookForBackups">If false, no connection to backup servers will be made</param>
        private void Connect(Server server, bool lookForBackups = true)
        {
            this.server    = server;
            this.apiSocket = new System.Net.Sockets.TcpClient();

            bool connectionAttempted = false;

            while (!connectionAttempted || !apiSocket.Connected)
            {
                // Try to connect asynchronously and wait for the result
                IAsyncResult result = apiSocket.BeginConnect(this.server.Address, this.server.MainPort, null, null);
                connectionAttempted = result.AsyncWaitHandle.WaitOne(TIMEOUT, true);

                // If connection attempt failed (timeout) or not connected
                if (!connectionAttempted || !apiSocket.Connected)
                {
                    apiSocket.Close();
                    if (lookForBackups)
                    {
                        this.server = Servers.GetBackup(this.server);
                        apiSocket   = new System.Net.Sockets.TcpClient();
                    }
                    else
                    {
                        throw new APICommunicationException("Cannot connect to: " + server.Address + ":" + server.MainPort);
                    }
                }
            }

            if (server.Secure)
            {
                SslStream sl = new SslStream(apiSocket.GetStream(), false, new RemoteCertificateValidationCallback(SSLHelper.TrustAllCertificatesCallback));

                //sl.AuthenticateAsClient(server.Address);

                bool authenticated = ExecuteWithTimeLimit.Execute(TimeSpan.FromMilliseconds(5000), () =>
                {
                    sl.AuthenticateAsClient(server.Address, new X509CertificateCollection(), System.Security.Authentication.SslProtocols.Default, false);
                });

                if (!authenticated)
                {
                    throw new APICommunicationException("Error during SSL handshaking (timed out?)");
                }

                apiWriteStream = new StreamWriter(sl);
                apiReadStream  = new StreamReader(sl);
            }
            else
            {
                NetworkStream ns = apiSocket.GetStream();
                apiWriteStream = new StreamWriter(ns);
                apiReadStream  = new StreamReader(ns);
            }

            this.apiConnected = true;

            if (OnConnected != null)
            {
                OnConnected.Invoke(this.server);
            }


            this.streamingConnector = new StreamingAPIConnector(this.server);
        }