Example #1
0
        public async Task AppConnect(AppConnection connection)
        {
            TcpClient client = new TcpClient();

            try
            {
                Log.Debug("Attempting connect");
                await client.ConnectAsync(Host, Port);
            }
            catch (Exception e)
            {
                Log.Error(e, "Error while connecting to TCP socket");
                await connection.Refuse();
            }

            await connection.Accept();

            ConnectedPipe pipe = new ConnectedPipe(client, connection);

            _ = Task.Run(async() =>
            {
                await pipe.Run();
                client.Close();
            });
        }
Example #2
0
        public async Task TcpListen()
        {
            Random      rand     = new Random();
            TcpListener listener = new TcpListener(IPAddress.Loopback, Port);

            listener.Start();
            try
            {
                while (true)
                {
                    var apps = await EntryP.DiscoverApps(AppName);

                    if (apps.Length < 1)
                    {
                        Log.Error("No suitable app found on entry point");
                        break;
                    }

                    var app = apps[rand.Next(0, apps.Length - 1)];
                    var s   = await listener.AcceptTcpClientAsync();

                    var appConn = new AppConnection(app, EntryP);
                    _ = Task.Run(async() =>
                    {
                        try
                        {
                            await appConn.Connect();
                            ConnectedPipe pipe = new ConnectedPipe(s, appConn);
                            _ = pipe.Run();
                        }
                        catch (Exception e)
                        {
                            Log.Error(e, "Error while handling");
                            s.Dispose();
                        }
                    });
                }
            }
            catch (Exception e)
            {
                Log.Error(e, "Error while accepting");
            }
            Environment.Exit(-1);
        }