-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgameServer.ts
More file actions
158 lines (137 loc) · 5.1 KB
/
gameServer.ts
File metadata and controls
158 lines (137 loc) · 5.1 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
import { AccountManager } from "./manager/accountManager";
import { ClientManager } from "./manager/clientManager";
import { Convert } from "./util/convert";
import { Database } from "./util/database";
import { DatabaseSetting } from "./config/databaseSetting";
import { GameClient } from "./game/gameClient";
import { GameSettingServer } from "./config/gameSettingServer";
import { Log } from "./util/log";
import { MapModeIni } from "./data/loader/mapModeIni";
import { MapsXML } from "./data/loader/mapsXML";
import { MissionPb } from "./data/loader/missionPb";
import { Server } from "./network/server";
import { decrypt } from "./util/decrypt";
import net from "net";
export class GameServer extends Server{
constructor(port: number, ip: string) {
super("game");
this.load();
// this.port = GameSettingServer.serverPort;
// this.ip = GameSettingServer.serverIp;
this.port = port;
this.ip = ip;
this.createServer();
}
createServer() {
this.server = net.createServer((socket) => {
socket.on("data", (data) => {
if (data.length < 4)
return Log.getLogger(this.type).error(
"Data received was under 4 byte length"
);
let len = data.readUInt16LE();
if (len > 8908) {
len &= 32767;
}
//decrypt
let _client = ClientManager.getClient(
"client_ip",
socket.remoteAddress
);
let client = ClientManager.getClient("approve", _client.connection.sessionId);
let decryptedData = decrypt(data, client.CRYPT_KEY);
Log.getLogger(this.type).info(
"Data Decrypted \n" + Convert.bufferToString(decryptedData)
);
client.receivePacket(decryptedData);
});
socket.on("connect", (connect) => {
console.log("connect");
});
socket.on("close", (close) => {
const connection = ClientManager.getClient(
"client_ip",
socket.remoteAddress
).connection;
const id = connection.sessionId;
//if the player switch the server, then dont delete the socket
if(connection.player.changeServer){
connection.player.changeServer = false;
} else {
if (
!ClientManager.deleteClient(
"client_ip",
socket.remoteAddress
) ||
!ClientManager.deleteClient("approve", id)
) {
Log.getLogger(this.type).error(
`The Connection closed but the instance can't be deleted or not exist (session Id: ${id} ) `
);
}
}
if (connection.account) {
connection.account.offline().then(
(success) => {
Log.getLogger(this.type).info(
"The Connection sucessfuly closed"
);
},
(err) => {
Log.getLogger(this.type).error(
"The Connection closed but the Database can't be updated"
);
}
);
}
});
socket.on("connectionAttempt", (stream) => {
console.log("trying to connect!");
});
socket.on("error", (err) => {
console.log(err);
});
});
this.server.on("connection", (socket) => {
this.addSocket(socket);
});
}
load() {
/**
* load game.ini
*/
GameSettingServer.load();
/**
* load maps
*/
MapsXML.load();
/**
* load default maps
*/
MapModeIni.load();
/**
* load missions
*/
MissionPb.load();
/**
* load database setting
*/
DatabaseSetting.load();
Database.getInstance();
}
addSocket(socket: net.Socket) {
let authClient = ClientManager.getClient("client_ip", socket.remoteAddress);
if(authClient){
let sessionId = authClient.connection.sessionId;
let gameClient = new GameClient(socket, sessionId, authClient.connection.player, authClient.connection.account);
Log.getLogger(this.type).info(
`The Connection is succesfully established! (session Id: ${sessionId} )`
);
ClientManager.approveClient(authClient.connection.sessionId, gameClient);
} else {
Log.getLogger(this.type).error(
`authClient not found in ip: ${socket.remoteAddress}`
);
}
}
}