-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
SocketServer.java
177 lines (155 loc) · 5.88 KB
/
SocketServer.java
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
package com.morphanone.depenizenbungee.sockets;
import com.morphanone.depenizenbungee.Depenizen;
import com.morphanone.depenizenbungee.dB;
import com.morphanone.depenizenbungee.packets.Packet;
import com.morphanone.depenizenbungee.packets.ServerPacketOutAcceptRegister;
import com.morphanone.depenizenbungee.packets.ServerPacketOutServer;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.scheduler.ScheduledTask;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
public class SocketServer implements Runnable {
private String password;
private ServerSocket socket;
private ScheduledTask task;
private boolean isRunning;
private ClientConnection[] clients;
private Map<String, ClientConnection> registeredClients;
public SocketServer(int maxClients, int port, String password) {
this.clients = new ClientConnection[maxClients];
this.registeredClients = new HashMap<String, ClientConnection>();
this.password = md5(password);
try {
this.socket = new ServerSocket(port);
this.socket.setSoTimeout(0);
this.start();
dB.log("SocketServer started on port " + port + ".");
} catch (Exception e) {
dB.echoError(e);
}
}
public String getPassword() {
return password;
}
public boolean isRegistered(String name) {
return registeredClients.containsKey(name.toLowerCase());
}
public ClientConnection getClient(String name) {
return registeredClients.get(name.toLowerCase());
}
public void sendToAll(Packet packet) {
for (ClientConnection clientConnection : registeredClients.values()) {
clientConnection.send(packet);
}
}
public void sendToAllExcept(int exceptId, Packet packet) {
for (ClientConnection clientConnection : registeredClients.values()) {
if (clientConnection.getClientId() == exceptId)
continue;
clientConnection.send(packet);
}
}
public void start() {
if (task == null) {
this.isRunning = true;
this.task = ProxyServer.getInstance().getScheduler().runAsync(Depenizen.getCurrentInstance(), this);
}
}
public void stop() {
this.close();
if (task != null) {
this.isRunning = false;
this.task.cancel();
task = null;
}
}
public void close() {
for (ClientConnection client : this.clients) {
if (client != null)
client.stop();
}
try {
this.socket.close();
dB.log("SocketServer stopped.");
} catch (Exception e) {
dB.echoError(e);
}
}
private int getNewClientId() {
for (int id = 0; id < clients.length; id++) {
if (clients[id] == null)
return id;
}
return -1;
}
private void addClient(Socket client) {
int clientId = getNewClientId();
if (clientId == -1) {
dB.log("Refused client '" + client.getInetAddress() + "': Maximum clients reached.");
return;
}
ClientConnection clientConnection = new ClientConnection(clientId, this, client);
this.clients[clientId] = clientConnection;
dB.log("New client connected: " + clientId + " (" + client.getInetAddress() + ")");
}
public synchronized boolean registerClient(int clientId, String name) {
String nameLwr = name.toLowerCase();
if (registeredClients.containsKey(nameLwr))
return false;
ServerPacketOutServer packet = new ServerPacketOutServer(ServerPacketOutServer.Action.REGISTERED, name);
for (ClientConnection clientConnection : registeredClients.values()) {
clientConnection.send(packet);
}
ClientConnection client = clients[clientId];
client.send(new ServerPacketOutAcceptRegister(ServerPacketOutAcceptRegister.Action.ACCEPTED, registeredClients.keySet()));
registeredClients.put(nameLwr, clients[clientId]);
client.setClientName(name);
dB.log("Client " + clientId + " has registered as '" + name + "'.");
return true;
}
public synchronized void removeClient(int clientId, String reason) {
ClientConnection client = this.clients[clientId];
client.stop();
dB.log("Client " + (client.isRegistered() ? client.getClientName() : clientId)
+ " has disconnected from SocketServer: " + reason);
this.clients[clientId] = null;
if (client.isRegistered()) {
String name = client.getClientName();
registeredClients.remove(name.toLowerCase());
ServerPacketOutServer packet = new ServerPacketOutServer(ServerPacketOutServer.Action.DISCONNECTED, name);
for (ClientConnection clientConnection : registeredClients.values()) {
clientConnection.send(packet);
}
}
}
private static String md5(String string) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(string.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte b : bytes)
sb.append(Integer.toHexString(b & 0xFF | 0x100)).substring(1, 3);
return sb.toString();
}
catch (Exception e) {
dB.echoError(e);
return null;
}
}
@Override
public void run() {
while (task != null && this.isRunning) {
try {
Socket socket = this.socket.accept();
socket.setSoTimeout(0);
socket.setKeepAlive(true);
this.addClient(socket);
} catch (Exception e) {
dB.echoError(e);
}
}
}
}