-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
69 lines (62 loc) · 1.46 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using IPK_Project1.Enums;
using IPK_Project1.Transport;
namespace IPK_Project1;
static class Program {
static void Main(string[] args) {
Cli cli = new();
try {
cli.Parse(args);
// Run the client based on the transport protocol
if (cli.TransportProtocol == TransportProtocol.Tcp) {
Tcp tcp = new Tcp();
RunClient(tcp, cli);
} else {
Error.Print("UDP is currently not supported.");
// Udp udp = new Udp();
// RunClient(udp, cli);
}
} catch (Exception e) {
Error.Print(e.Message);
Environment.Exit(1);
}
}
static void RunClient(Client client, Cli cli) {
if (string.IsNullOrEmpty(cli.ServerAddress)) {
Error.Print("Missing required arguments: Server Address.");
Environment.Exit(1);
}
// Run the client
try {
client.Run(cli);
} catch (Exception e) {
Error.Print(e.Message);
Environment.Exit(1);
}
// Handle Ctrl+C
Console.CancelKeyPress += (sender, eventArgs) => {
eventArgs.Cancel = true;
client.Close();
Environment.Exit(0);
};
// Read data from the console and send it to the server
try {
while (true) {
string? message = Console.ReadLine();
if (!string.IsNullOrEmpty(message)) {
client.SendData(message);
} else {
if (message == null) {
client.SendBye();
Environment.Exit(0);
}
Error.Print("Input cannot be empty.");
}
}
} catch (Exception e) {
Error.Print(e.Message);
} finally {
client.Close();
}
}
}