-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleClient.cs
153 lines (129 loc) · 4.61 KB
/
ConsoleClient.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Net;
using System.Text;
namespace Terralite
{
public class ConsoleClient
{
/// <summary>
/// Whether to send using <c>Send</c> versus <c>SendReliable</c>
/// </summary>
public bool ReliableMode { get; set; }
private const int DEFAULT_PORT = 10346;
private ReliableConnection rc;
private bool run;
private int connection;
public ConsoleClient()
{
ReliableMode = false;
rc = new ReliableConnection();
run = true;
string line;
Console.WriteLine("Enter commands below. Type /help for a list of commands");
do
{
Console.Write("> ");
line = Console.ReadLine();
if (line[0] == '/')
ParseCommand(line);
else if (rc.HasConnections)
{
if (ReliableMode)
rc.SendReliable(connection, line);
else
rc.Send(connection, line);
}
else
Console.WriteLine("You must be connected to send text!");
}
while (run);
rc.Disconnect(connection);
}
private void OnReceive(int connId, byte[] data)
{
Console.WriteLine(Encoding.UTF8.GetString(data));
}
/// <summary>
/// Parses a line of input for specific commands
/// </summary>
/// <param name="line">Line to parse</param>
private void ParseCommand(string line)
{
string[] parts = line.Substring(1).Split(new string[] { " " }, StringSplitOptions.None);
switch (parts[0])
{
case "h":
case "help":
ShowHelp();
break;
case "c":
case "connect":
string ip;
int port = DEFAULT_PORT;
bool success;
if (parts[1].Contains(":"))
{
ip = parts[1].Split(':')[0];
string portString = parts[1].Split(':')[1];
success = int.TryParse(portString, out port);
if (!success)
{
Console.WriteLine("Invalid port '" + portString + "'.");
return;
}
}
else
ip = parts[1];
if (ip == "")
ip += "127.0.0.1";
IPAddress address;
success = IPAddress.TryParse(ip, out address);
if (!success)
{
Console.WriteLine("Invalid IP address '" + parts[1] + "'.");
return;
}
connection = rc.Connect(ip, port);
rc.AddReceiveEvent(connection, OnReceive);
break;
case "dc":
case "disconnect":
rc.Disconnect(connection);
break;
case "r":
case "reliable":
ReliableMode = true;
break;
case "nr":
case "nonreliable":
ReliableMode = false;
break;
case "e":
case "exit":
run = false;
break;
default:
Console.WriteLine("Unknown command '" + parts[0] + "'. Type /help for a list of commands");
break;
}
}
/// <summary>
/// Displays the help dialog
/// </summary>
private void ShowHelp()
{
Console.WriteLine("-----===== Console Client Help =====-----");
Console.WriteLine("/help - Show this help dialog");
Console.WriteLine("/connect <IP[:port]> - Connect to an IP address. Default port is 10346");
Console.WriteLine("/disconnect - Disconnect from the remote end point");
Console.WriteLine("/reliable - Turn ReliableMode on");
Console.WriteLine("/nonreliable - Turn ReliableMode off");
Console.WriteLine("/exit - Exit the program");
Console.WriteLine("-----===== =====-----");
}
static void Main(string[] args)
{
new ConsoleClient();
}
}
}