-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClient.cs
150 lines (132 loc) · 4.78 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
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Sockets;
namespace cSharpServer
{
/// <summary>
/// This handles everything a user needs.
/// </summary>
public class Client : IDisposable
{
public TcpClient TcpConnection;
public Client(TcpClient _TcpConnection, Server _CurrentServer)
{
this.CurrentServer = _CurrentServer;
this.TcpConnection = _TcpConnection;
}
public long SessionId;
public string Username;
public string Password;
public Server CurrentServer;
/// <summary>
/// Store custom data for the user.
/// </summary>
public List<ClientArgument> Arguments = new List<ClientArgument>();
public void GetArgument(string Name, out string Value)
{
for(int i = 0, y = Arguments.Count; i < y; i++)
if(string.Compare(Name, Arguments[i].Name) == 0)
{
Value = Arguments[i].GetString();
return;
}
Value = string.Empty;
}
public void GetArgument(string Name, out int Value)
{
for (int i = 0, y = Arguments.Count; i < y; i++)
if (string.Compare(Name, Arguments[i].Name) == 0)
{
Value = Arguments[i].GetInt();
return;
}
Value = 0;
}
/// <summary>
/// This is the MYSQL connection used for Database Class.
/// </summary>
public MySql.Data.MySqlClient.MySqlConnection Connection = null;
/// <summary>
/// This will be called before a client Interface gets called.
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public bool ProessRequest(NetworkStream stream)
{
try
{
BinaryBuffer buff = new BinaryBuffer(Database.ReadBuffer(stream));
buff.BeginRead();
int count = buff.ReadInt();
BinaryBuffer writeBuff = new BinaryBuffer();
writeBuff.BeginWrite();
for (int i = 0; i < count;i++)
{
IClientRequest Request = CurrentServer.GetRequest(buff.ReadByte());
// if a IClientRequest to close. we need to handle by not reading and writing back!
if (!this.TcpConnection.Connected)
return false;
byte[] data = buff.ReadByteArray(buff.ReadInt());
if(Request != null)
{
data = Request.Process(new BinaryBuffer(data), this);
writeBuff.Write(data.Length);
writeBuff.Write(data);
}
else
{
writeBuff.Write(0); // length;
}
}
writeBuff.EndWrite();
Database.WriteBuffer(writeBuff.ByteBuffer, stream);
return true;
}
catch (Exception)
{ }
return false;
}
/// <summary>
/// Connection time.
/// </summary>
public Stopwatch ClientStopwatch = Stopwatch.StartNew();
/// <summary>
/// Launch the process/login of the client.
/// </summary>
public void Launch()
{
string localEndPoint = TcpConnection.Client.LocalEndPoint.ToString();
try
{
NetworkStream stream;
if (TcpConnection.Connected)
{
if (CurrentServer.ClientLogin.Login(Database.ReadBuffer((stream = TcpConnection.GetStream())), this))
while (TcpConnection.Connected)
if(!ProessRequest(stream))
break;
TcpConnection.Close();
}
}
catch(Exception e)
{
// log!
Console.WriteLine(e.ToString());
}
finally
{
ClientStopwatch.Stop();
Console.WriteLine("Client {1}: {0} has disconnected, Connected for: {2} ms", localEndPoint, SessionId, ClientStopwatch.ElapsedMilliseconds);
}
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
}
}