forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameClient.cs
More file actions
197 lines (166 loc) · 5.51 KB
/
GameClient.cs
File metadata and controls
197 lines (166 loc) · 5.51 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using log4net;
using L2dotNET.DataContracts;
using L2dotNET.Encryption;
using L2dotNET.model.player;
using L2dotNET.Models;
using L2dotNET.Network;
using L2dotNET.Network.serverpackets;
using L2dotNET.Services.Contracts;
using L2dotNET.Utility;
using L2dotNET.world;
using Ninject;
namespace L2dotNET
{
public class GameClient
{
[Inject]
public IPlayerService PlayerService => GameServer.Kernel.Get<IPlayerService>();
private static readonly ILog Log = LogManager.GetLogger(typeof(GameClient));
public EndPoint Address;
public TcpClient Client;
public NetworkStream Stream;
private byte[] _buffer;
private readonly GameCrypt _crypt;
public byte[] BlowfishKey;
public ScrambledKeyPair ScrambledPair;
public L2Player CurrentPlayer;
private List<L2Player> _accountChars = new List<L2Player>();
public int Protocol;
public bool IsTerminated;
public long TrafficUp,
TrafficDown;
public GameClient(TcpClient tcpClient)
{
Log.Info($"connection from {tcpClient.Client.RemoteEndPoint}");
Client = tcpClient;
Stream = tcpClient.GetStream();
Address = tcpClient.Client.RemoteEndPoint;
_crypt = new GameCrypt();
new System.Threading.Thread(Read).Start();
}
public byte[] EnableCrypt()
{
byte[] key = BlowFishKeygen.GetRandomKey();
_crypt.SetKey(key);
return key;
}
public void SendPacket(GameserverPacket sbp)
{
if (IsTerminated)
return;
sbp.Write();
byte[] data = sbp.ToByteArray();
_crypt.Encrypt(data);
List<byte> bytes = new List<byte>();
bytes.AddRange(BitConverter.GetBytes((short)(data.Length + 2)));
bytes.AddRange(data);
TrafficDown += bytes.Count;
if (sbp is CharacterSelectionInfo)
{
// byte[] st = ToByteArray();
//foreach (byte s in data)
// log.Info($"{ s:X2 } ");
}
try
{
Stream.Write(bytes.ToArray(), 0, bytes.Count);
Stream.Flush();
}
catch
{
Log.Info($"client {AccountName} terminated.");
Termination();
}
}
public void Termination()
{
Log.Info("termination");
IsTerminated = true;
Stream.Close();
Client.Close();
if(CurrentPlayer?.Online == 1)
CurrentPlayer?.DeleteMe();
//_accountChars.ForEach(p => p?.DeleteMe());
//_accountChars.Clear();
ClientManager.Instance.Terminate(Address.ToString());
}
public void Read()
{
if (IsTerminated)
return;
try
{
_buffer = new byte[2];
Stream.BeginRead(_buffer, 0, 2, OnReceiveCallbackStatic, null);
}
catch
{
Termination();
}
}
public L2Player LoadPlayerInSlot(string accName, int charSlot)
{
L2Player player = PlayerService.GetPlayerBySlotId(accName, charSlot);
return player;
}
public L2Player GetPlayer(string accName, int charSlot)
{
L2Player playerContract = LoadPlayerInSlot(accName, charSlot);
L2Player player = L2World.Instance.GetPlayer(playerContract.ObjId);
return player;
}
private void OnReceiveCallbackStatic(IAsyncResult result)
{
try
{
int rs = Stream.EndRead(result);
if (rs <= 0)
return;
short length = BitConverter.ToInt16(_buffer, 0);
_buffer = new byte[length - 2];
Stream.BeginRead(_buffer, 0, length - 2, OnReceiveCallback, result.AsyncState);
}
catch
{
Termination();
}
}
private void OnReceiveCallback(IAsyncResult result)
{
if (IsTerminated)
return;
Stream.EndRead(result);
byte[] buff = new byte[_buffer.Length];
_buffer.CopyTo(buff, 0);
_crypt.Decrypt(buff);
TrafficUp += _buffer.Length;
GamePacketHandler.HandlePacket(new Packet(1, buff), this);
new System.Threading.Thread(Read).Start();
}
public void RemoveAccountCharAndResetSlotIndex(int charSlot)
{
AccountChars = AccountChars.Where(filter => filter.CharSlot != charSlot).OrderBy(orderby => orderby.CharSlot).ToList();
int slot = 0;
AccountChars.ForEach(p =>
{
p.CharSlot = slot;
slot++;
});
}
public string AccountName { get; set; }
public SessionKey SessionKey { get; set; }
public string AccountType { get; set; }
public string AccountTimeEnd { get; set; }
public DateTime AccountTimeLogIn { get; set; }
public List<L2Player> AccountChars
{
get { return _accountChars ?? new List<L2Player>(); }
set { _accountChars = value; }
}
}
}