Example #1
0
        private static async Task MainLoop(string[] args, CancellationTokenSource tokenSource, int port)
        {
            System.Net.Sockets.TcpClient   socket   = null;
            System.Net.Sockets.TcpListener listener = null;

            listener = new System.Net.Sockets.TcpListener(IPAddress.Any, port);
            listener.Start();

            // *** starting to listen to socket

            while (!tokenSource.Token.WaitHandle.WaitOne(1))
            {
                Console.WriteLine("listening on port " + port);
                try
                {
                    socket = await listener.AcceptTcpClientAsync();

                    Console.WriteLine("Connected!");

                    var ai = new Ai();

                    using (var stream = socket.GetStream())
                        using (var sr = new StreamReader(stream))
                            using (var sw = new StreamWriter(stream))
                            {
                                while (!tokenSource.Token.WaitHandle.WaitOne(1))
                                {
                                    string line = await sr.ReadLineAsync();

                                    Console.WriteLine("received " + line);

                                    var commands = ai.GoJson(line);

                                    await sw.WriteAsync(commands);

                                    await sw.FlushAsync();
                                }
                            }
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("\n" + e.Message);
                    socket?.Dispose();
                }
            }
            socket?.Dispose();
        }
Example #2
0
        public void Disconnect()
        {
            if (_tcpClient == null)
            {
                return;
            }

            Stop(); // Stop first

            _tcpClient.Dispose();
            _tcpClient = null;

            clearScheduledTransfers();

            _receiveThread.Join();
            _receiveThread = null;
        }
Example #3
0
        protected async override Task AcceptConnection(CancellationToken token)
        {
            System.Net.Sockets.TcpClient tcpClient = null;
            try
            {
                using var closeToken = token.Register(Dispose);
                tcpClient            = await _tcpServer.AcceptTcpClientAsync();

                // pass the ownership of the connection
                HandleConnection(tcpClient.GetStream(), callbackFactory => new Client(action => action(), callbackFactory), token);
            }
            catch (Exception ex)
            {
                tcpClient?.Dispose();
                if (!token.IsCancellationRequested)
                {
                    Logger.LogException(ex, Settings.Name);
                }
            }
        }
Example #4
0
        public void Connect(int fisPort)
        {
            if (_tcpClient != null)
            {
                return;
            }

            _fisHostName = "live.fisski.com";
            _fisPort     = fisPort;

            _sequence = 0;
            try
            {
                clearScheduledTransfers();
                _tcpClient = new System.Net.Sockets.TcpClient();
                _tcpClient.Connect(_fisHostName, _fisPort);

                // Spawn receive thread
                _receiveThread = new System.Threading.Thread(receiveMethod);
                _receiveThread.Start();

                _keepAliveTimer          = new System.Timers.Timer();
                _keepAliveTimer.Elapsed += keepAliveTimer_Elapsed;
                _keepAliveTimer.Interval = 5 * 60 * 1000; // 5 minutes * 60s * 1000ms
                //_keepAliveTimer.Interval = 1000; // for testing
                _keepAliveTimer.AutoReset = true;
                _keepAliveTimer.Enabled   = true;
            }
            catch (Exception e)
            {
                Logger.Warn(e, "Connect to {0} on port {1} failed", _fisHostName, _fisPort);
                _tcpClient.Dispose();
                _tcpClient = null;
                throw; // re-throw
            }
        }
Example #5
0
 public void Dispose()
 {
     _sslStream.Dispose();
     _client.Dispose();
 }
Example #6
0
 public void Dispose()
 {
     _stream.Dispose();
     _client.Dispose();
     IsConnected = false;
 }
 public void Dispose()
 {
     Close();
     _client.Dispose();
 }
Example #8
0
 public void Dispose()
 {
     client.Dispose();
 }