-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClient.cs
159 lines (143 loc) · 4.39 KB
/
Client.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
154
155
156
157
158
159
using System;
using System.Net;
namespace TelnetServer
{
public enum EClientStatus
{
/// <summary>
/// The client has not been
/// authenticated yet.
/// </summary>
Guest = 0,
/// <summary>
/// The client is authenticating.
/// </summary>
Authenticating = 1,
/// <summary>
/// The client is logged in.
/// </summary>
LoggedIn = 2
}
public class Client
{
/// <summary>
/// The client's identifier.
/// </summary>
private uint id;
/// <summary>
/// The client's remote address.
/// </summary>
private IPEndPoint remoteAddr;
/// <summary>
/// The connection datetime.
/// </summary>
private DateTime connectedAt;
/// <summary>
/// The client's current status.
/// </summary>
private EClientStatus status;
/// <summary>
/// The last received data from the client.
/// </summary>
private string receivedData;
/// <summary>
/// Initializes a new instance of the <see cref="Client"/> class.
/// </summary>
/// <param name="clientId">The client's identifier.</param>
/// <param name="remoteAddress">The remote address.</param>
public Client(uint clientId, IPEndPoint remoteAddress)
{
this.id = clientId;
this.remoteAddr = remoteAddress;
this.connectedAt = DateTime.Now;
this.status = EClientStatus.Guest;
this.receivedData = string.Empty;
}
/// <summary>
/// Gets the client identifier.
/// </summary>
/// <returns>Client's identifier.</returns>
public uint getClientID()
{
return id;
}
/// <summary>
/// Gets the remote address.
/// </summary>
/// <returns>Client's remote address.</returns>
public IPEndPoint getRemoteAddress()
{
return remoteAddr;
}
/// <summary>
/// Gets the connection time.
/// </summary>
/// <returns>The connection time.</returns>
public DateTime getConnectionTime()
{
return connectedAt;
}
/// <summary>
/// Gets the client's current status.
/// </summary>
/// <returns>The client's status.</returns>
public EClientStatus getCurrentStatus()
{
return status;
}
/// <summary>
/// Gets the client's last received data.
/// </summary>
/// <returns>Client's last received data.</returns>
public string getReceivedData()
{
return receivedData;
}
/// <summary>
/// Sets the client's current status.
/// </summary>
/// <param name="newStatus">The new status.</param>
public void setStatus(EClientStatus newStatus)
{
this.status = newStatus;
}
/// <summary>
/// Sets the client's last received data.
/// </summary>
/// <param name="newReceivedData">The new received data.</param>
public void setReceivedData(string newReceivedData)
{
this.receivedData = newReceivedData;
}
/// <summary>
/// Appends a string to the client's last
/// received data.
/// </summary>
/// <param name="dataToAppend">The data to append.</param>
public void appendReceivedData(string dataToAppend)
{
this.receivedData += dataToAppend;
}
/// <summary>
/// Removes the last character from the
/// client's last received data.
/// </summary>
public void removeLastCharacterReceived()
{
receivedData = receivedData.Substring(0, receivedData.Length - 1);
}
/// <summary>
/// Resets the last received data.
/// </summary>
public void resetReceivedData()
{
receivedData = string.Empty;
}
public override string ToString()
{
string ip = string.Format("{0}:{1}", remoteAddr.Address.ToString(), remoteAddr.Port);
string res = string.Format("Client #{0} (From: {1}, Status: {2}, Connection time: {3})", id, ip, status, connectedAt);
return res;
}
}
}