-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.cs
192 lines (157 loc) · 7.96 KB
/
Server.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
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
using Packets;
using System;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Collections.Generic;
using MiscUtil.Conversion;
namespace LegendSharp
{
public class StateObject {
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
public const int PacketBufferSize = 1048576;
public byte[] packetBuffer = new byte[PacketBufferSize];
public uint packetLength = 0;
public uint packetPosition = 0;
public ClientHandler clientHandler;
}
public class AsynchronousSocketListener {
// Thread signal.
public static ManualResetEvent allDone = new ManualResetEvent(false);
public static BigEndianBitConverter converter = new BigEndianBitConverter();
public static HttpClient httpClient = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate });
public static Legend legend = new Legend();
public AsynchronousSocketListener() {
}
public static void StartListening() {
httpClient.DefaultRequestHeaders.Add("User-Agent", "Legend Plus login bot v0.1");
// Establish the local endpoint for the socket.
// The DNS name of the computer
// running the listener is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 21321);
// Create a TCP/IP socket.
Socket listener = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp );
// Bind the socket to the local endpoint and listen for incoming connections.
try {
listener.Bind(localEndPoint);
listener.Listen(100);
while (true) {
// Set the event to nonsignaled state.
allDone.Reset();
// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener );
// Wait until a connection is made before continuing.
allDone.WaitOne();
}
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
public static void AcceptCallback(IAsyncResult ar) {
// Signal the main thread to continue.
allDone.Set();
// Get the socket that handles the client request.
Socket listener = (Socket) ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
state.clientHandler = new ClientHandler(handler, legend);
var readyPacket = new Packets.ReadyPacket(0);
AsynchronousSocketListener.Send(state.workSocket, readyPacket);
handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
public static void ReadCallback(IAsyncResult ar) {
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject) ar.AsyncState;
Socket handler = state.workSocket;
// Read data from the client socket.
try {
int bytesRead = handler.EndReceive(ar);
//System.Console.WriteLine(BitConverter.ToString( state.buffer ));
uint offset = 0;
if (state.packetLength == state.packetPosition) {
offset = 4;
state.packetLength = converter.ToUInt32(state.buffer, 0);
state.packetPosition = 0;
}
if (bytesRead > 0) {
// There might be more data, so store the data received so far.
Buffer.BlockCopy(state.buffer, (int) offset, state.packetBuffer, (int) state.packetPosition, Math.Min((int) bytesRead - (int) offset, (int) (state.packetLength - state.packetPosition)));
state.packetPosition += (uint) Math.Min((int) bytesRead - (int) offset, (int) (state.packetLength - state.packetPosition));
/*for (var i = offset; i < bytesRead && state.packetPosition < state.packetLength; i++)
{
state.packetBuffer[state.packetPosition] = state.buffer[i];
state.packetPosition++;
}*/
}
if (state.packetLength == state.packetPosition) {
short packetId = converter.ToInt16(state.packetBuffer, 0);
byte[] packetData = new byte[state.packetLength - 2];
Buffer.BlockCopy(state.packetBuffer, 2, packetData, 0, (int)state.packetLength-2);
//System.Console.WriteLine(BitConverter.ToString( packetData ));
var packet = Packets.Packets.decode(packetId, packetData);
state.clientHandler.OnPacket(packet);
}
state.buffer = new byte[StateObject.BufferSize];
handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
catch (SocketException)
{
state.clientHandler.OnDisconnect();
Console.WriteLine("Client disconnected.");
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
public static void Send(Socket handler, String data) {
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}
public static void Send(Socket handler, byte[] byteData) {
// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}
public static void Send(Socket handler, Packet packet) {
byte[] byteData = Packets.Packets.encode(packet);
Console.WriteLine("Sending packet #{0} {1}", packet.id, packet.name);
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}
private static void SendCallback(IAsyncResult ar) {
try {
// Retrieve the socket from the state object.
Socket handler = (Socket) ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
}
}