-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIMClient.cs
More file actions
237 lines (217 loc) · 7.79 KB
/
IMClient.cs
File metadata and controls
237 lines (217 loc) · 7.79 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Security.Cryptography;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
namespace InstantMessenger
{
public class IMClient
{
Thread tcpThread; // Receiver
bool _conn = false; // Is connected/connecting?
bool _logged = false; // Is logged in?
string _user; // Username
string _pass; // Password
bool reg; // Register mode
public string Server { get { return "localhost"; } } // Address of server. In this case - local IP address.
public int Port { get { return 2000; } }
public bool IsLoggedIn { get { return _logged; } }
public string UserName { get { return _user; } }
public string Password { get { return _pass; } }
// Start connection thread and login or register.
void connect(string user, string password, bool register)
{
if (!_conn)
{
_conn = true;
_user = user;
_pass = password;
reg = register;
tcpThread = new Thread(new ThreadStart(SetupConn));
tcpThread.Start();
}
}
public void Login(string user, string password)
{
connect(user, password, false);
}
public void Register(string user, string password)
{
connect(user, password, true);
}
public void Disconnect()
{
if (_conn)
CloseConn();
}
public void IsAvailable(string user)
{
if (_conn)
{
bw.Write(IM_IsAvailable);
bw.Write(user);
bw.Flush();
}
}
public void SendMessage(string to, string msg)
{
if (_conn)
{
bw.Write(IM_Send);
bw.Write(to);
bw.Write(msg);
bw.Flush();
}
}
// Events
public event EventHandler LoginOK;
public event EventHandler RegisterOK;
public event IMErrorEventHandler LoginFailed;
public event IMErrorEventHandler RegisterFailed;
public event EventHandler Disconnected;
public event IMAvailEventHandler UserAvailable;
public event IMReceivedEventHandler MessageReceived;
virtual protected void OnLoginOK()
{
if (LoginOK != null)
LoginOK(this, EventArgs.Empty);
}
virtual protected void OnRegisterOK()
{
if (RegisterOK != null)
RegisterOK(this, EventArgs.Empty);
}
virtual protected void OnLoginFailed(IMErrorEventArgs e)
{
if (LoginFailed != null)
LoginFailed(this, e);
}
virtual protected void OnRegisterFailed(IMErrorEventArgs e)
{
if (RegisterFailed != null)
RegisterFailed(this, e);
}
virtual protected void OnDisconnected()
{
if (Disconnected != null)
Disconnected(this, EventArgs.Empty);
}
virtual protected void OnUserAvail(IMAvailEventArgs e)
{
if (UserAvailable != null)
UserAvailable(this, e);
}
virtual protected void OnMessageReceived(IMReceivedEventArgs e)
{
if (MessageReceived != null)
MessageReceived(this, e);
}
TcpClient client;
NetworkStream netStream;
SslStream ssl;
BinaryReader br;
BinaryWriter bw;
void SetupConn() // Setup connection and login
{
client = new TcpClient(Server, Port); // Connect to the server.
netStream = client.GetStream();
ssl = new SslStream(netStream, false, new RemoteCertificateValidationCallback(ValidateCert));
ssl.AuthenticateAsClient("InstantMessengerServer");
// Now we have encrypted connection.
br = new BinaryReader(ssl, Encoding.UTF8);
bw = new BinaryWriter(ssl, Encoding.UTF8);
// Receive "hello"
int hello = br.ReadInt32();
if (hello == IM_Hello)
{
// Hello OK, so answer.
bw.Write(IM_Hello);
bw.Write(reg ? IM_Register : IM_Login); // Login or register
bw.Write(UserName);
bw.Write(Password);
bw.Flush();
byte ans = br.ReadByte(); // Read answer.
if (ans == IM_OK) // Login/register OK
{
if (reg)
OnRegisterOK(); // Register is OK.
OnLoginOK(); // Login is OK (when registered, automatically logged in)
Receiver(); // Time for listening for incoming messages.
}
else
{
IMErrorEventArgs err = new IMErrorEventArgs((IMError)ans);
if (reg)
OnRegisterFailed(err);
else
OnLoginFailed(err);
}
}
if (_conn)
CloseConn();
}
void CloseConn() // Close connection.
{
br.Close();
bw.Close();
ssl.Close();
netStream.Close();
client.Close();
OnDisconnected();
_conn = false;
}
void Receiver() // Receive all incoming packets.
{
_logged = true;
try
{
while (client.Connected) // While we are connected.
{
byte type = br.ReadByte(); // Get incoming packet type.
if (type == IM_IsAvailable)
{
string user = br.ReadString();
bool isAvail = br.ReadBoolean();
OnUserAvail(new IMAvailEventArgs(user, isAvail));
}
else if (type == IM_Received)
{
string from = br.ReadString();
string msg = br.ReadString();
OnMessageReceived(new IMReceivedEventArgs(from, msg));
}
}
}
catch (IOException) { }
_logged = false;
}
// Packet types
public const int IM_Hello = 2012; // Hello
public const byte IM_OK = 0; // OK
public const byte IM_Login = 1; // Login
public const byte IM_Register = 2; // Register
public const byte IM_TooUsername = 3; // Too long username
public const byte IM_TooPassword = 4; // Too long password
public const byte IM_Exists = 5; // Already exists
public const byte IM_NoExists = 6; // Doesn't exist
public const byte IM_WrongPass = 7; // Wrong password
public const byte IM_IsAvailable = 8; // Is user available?
public const byte IM_Send = 9; // Send message
public const byte IM_Received = 10; // Message received
public static bool ValidateCert(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
// Uncomment this lines to disallow untrusted certificates.
//if (sslPolicyErrors == SslPolicyErrors.None)
// return true;
//else
// return false;
return true; // Allow untrusted certificates.
}
}
}