Skip to content

Commit 44b336a

Browse files
committed
Net-server: properly synchronize clients array
1 parent 38a91f1 commit 44b336a

File tree

1 file changed

+57
-42
lines changed

1 file changed

+57
-42
lines changed

java/libraries/net/src/processing/net/Server.java

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ public class Server implements Runnable {
5252
PApplet parent;
5353
Method serverEventMethod;
5454

55-
Thread thread;
55+
volatile Thread thread;
5656
ServerSocket server;
5757
int port;
58-
58+
59+
protected final Object clientsLock = new Object[0];
5960
/** Number of clients currently connected. */
6061
public int clientCount;
6162
/** Array of client objects, useful length is determined by clientCount. */
@@ -127,26 +128,30 @@ public Server(PApplet parent, int port, String host) {
127128
*/
128129
public void disconnect(Client client) {
129130
client.stop();
130-
int index = clientIndex(client);
131-
if (index != -1) {
132-
removeIndex(index);
131+
synchronized (clientsLock) {
132+
int index = clientIndex(client);
133+
if (index != -1) {
134+
removeIndex(index);
135+
}
133136
}
134137
}
135138

136139

137140
protected void removeIndex(int index) {
138-
clientCount--;
139-
// shift down the remaining clients
140-
for (int i = index; i < clientCount; i++) {
141-
clients[i] = clients[i+1];
141+
synchronized (clientsLock) {
142+
clientCount--;
143+
// shift down the remaining clients
144+
for (int i = index; i < clientCount; i++) {
145+
clients[i] = clients[i + 1];
146+
}
147+
// mark last empty var for garbage collection
148+
clients[clientCount] = null;
142149
}
143-
// mark last empty var for garbage collection
144-
clients[clientCount] = null;
145150
}
146151

147152

148153
protected void disconnectAll() {
149-
synchronized (clients) {
154+
synchronized (clientsLock) {
150155
for (int i = 0; i < clientCount; i++) {
151156
try {
152157
clients[i].stop();
@@ -161,20 +166,24 @@ protected void disconnectAll() {
161166

162167

163168
protected void addClient(Client client) {
164-
if (clientCount == clients.length) {
165-
clients = (Client[]) PApplet.expand(clients);
169+
synchronized (clientsLock) {
170+
if (clientCount == clients.length) {
171+
clients = (Client[]) PApplet.expand(clients);
172+
}
173+
clients[clientCount++] = client;
166174
}
167-
clients[clientCount++] = client;
168175
}
169176

170177

171178
protected int clientIndex(Client client) {
172-
for (int i = 0; i < clientCount; i++) {
173-
if (clients[i] == client) {
174-
return i;
179+
synchronized (clientsLock) {
180+
for (int i = 0; i < clientCount; i++) {
181+
if (clients[i] == client) {
182+
return i;
183+
}
175184
}
185+
return -1;
176186
}
177-
return -1;
178187
}
179188

180189

@@ -219,7 +228,7 @@ static public String ip() {
219228
* @usage application
220229
*/
221230
public Client available() {
222-
synchronized (clients) {
231+
synchronized (clientsLock) {
223232
int index = lastAvailable + 1;
224233
if (index >= clientCount) index = 0;
225234

@@ -293,7 +302,7 @@ public void run() {
293302
try {
294303
Socket socket = server.accept();
295304
Client client = new Client(parent, socket);
296-
synchronized (clients) {
305+
synchronized (clientsLock) {
297306
addClient(client);
298307
if (serverEventMethod != null) {
299308
try {
@@ -333,39 +342,45 @@ public void run() {
333342
* @param data data to write
334343
*/
335344
public void write(int data) { // will also cover char
336-
int index = 0;
337-
while (index < clientCount) {
338-
if (clients[index].active()) {
339-
clients[index].write(data);
340-
index++;
341-
} else {
342-
removeIndex(index);
345+
synchronized (clientsLock) {
346+
int index = 0;
347+
while (index < clientCount) {
348+
if (clients[index].active()) {
349+
clients[index].write(data);
350+
index++;
351+
} else {
352+
removeIndex(index);
353+
}
343354
}
344355
}
345356
}
346357

347358

348359
public void write(byte data[]) {
349-
int index = 0;
350-
while (index < clientCount) {
351-
if (clients[index].active()) {
352-
clients[index].write(data);
353-
index++;
354-
} else {
355-
removeIndex(index);
360+
synchronized (clientsLock) {
361+
int index = 0;
362+
while (index < clientCount) {
363+
if (clients[index].active()) {
364+
clients[index].write(data);
365+
index++;
366+
} else {
367+
removeIndex(index);
368+
}
356369
}
357370
}
358371
}
359372

360373

361374
public void write(String data) {
362-
int index = 0;
363-
while (index < clientCount) {
364-
if (clients[index].active()) {
365-
clients[index].write(data);
366-
index++;
367-
} else {
368-
removeIndex(index);
375+
synchronized (clientsLock) {
376+
int index = 0;
377+
while (index < clientCount) {
378+
if (clients[index].active()) {
379+
clients[index].write(data);
380+
index++;
381+
} else {
382+
removeIndex(index);
383+
}
369384
}
370385
}
371386
}

0 commit comments

Comments
 (0)