| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 38a91f1 commit 44b336a
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -52,10 +52,11 @@ public class Server implements Runnable { | |||
| 52 | 52 | PApplet parent; | |
| 53 | 53 | Method serverEventMethod; | |
| 54 | 54 | ||
| 55 | - Thread thread; | ||
| 55 | + volatile Thread thread; | ||
| 56 | 56 | ServerSocket server; | |
| 57 | 57 | int port; | |
| 58 | - | ||
| 58 | + | ||
| 59 | + protected final Object clientsLock = new Object[0]; | ||
| 59 | 60 | /** Number of clients currently connected. */ | |
| 60 | 61 | public int clientCount; | |
| 61 | 62 | /** Array of client objects, useful length is determined by clientCount. */ | |
@@ -127,26 +128,30 @@ public Server(PApplet parent, int port, String host) { | |||
| 127 | 128 | */ | |
| 128 | 129 | public void disconnect(Client client) { | |
| 129 | 130 | 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 | + } | ||
| 133 | 136 | } | |
| 134 | 137 | } | |
| 135 | 138 | ||
| 136 | 139 | ||
| 137 | 140 | 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; | ||
| 142 | 149 | } | |
| 143 | - // mark last empty var for garbage collection | ||
| 144 | - clients[clientCount] = null; | ||
| 145 | 150 | } | |
| 146 | 151 | ||
| 147 | 152 | ||
| 148 | 153 | protected void disconnectAll() { | |
| 149 | - synchronized (clients) { | ||
| 154 | + synchronized (clientsLock) { | ||
| 150 | 155 | for (int i = 0; i < clientCount; i++) { | |
| 151 | 156 | try { | |
| 152 | 157 | clients[i].stop(); | |
@@ -161,20 +166,24 @@ protected void disconnectAll() { | |||
| 161 | 166 | ||
| 162 | 167 | ||
| 163 | 168 | 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; | ||
| 166 | 174 | } | |
| 167 | - clients[clientCount++] = client; | ||
| 168 | 175 | } | |
| 169 | 176 | ||
| 170 | 177 | ||
| 171 | 178 | 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 | + } | ||
| 175 | 184 | } | |
| 185 | + return -1; | ||
| 176 | 186 | } | |
| 177 | - return -1; | ||
| 178 | 187 | } | |
| 179 | 188 | ||
| 180 | 189 | ||
@@ -219,7 +228,7 @@ static public String ip() { | |||
| 219 | 228 | * @usage application | |
| 220 | 229 | */ | |
| 221 | 230 | public Client available() { | |
| 222 | - synchronized (clients) { | ||
| 231 | + synchronized (clientsLock) { | ||
| 223 | 232 | int index = lastAvailable + 1; | |
| 224 | 233 | if (index >= clientCount) index = 0; | |
| 225 | 234 | ||
@@ -293,7 +302,7 @@ public void run() { | |||
| 293 | 302 | try { | |
| 294 | 303 | Socket socket = server.accept(); | |
| 295 | 304 | Client client = new Client(parent, socket); | |
| 296 | - synchronized (clients) { | ||
| 305 | + synchronized (clientsLock) { | ||
| 297 | 306 | addClient(client); | |
| 298 | 307 | if (serverEventMethod != null) { | |
| 299 | 308 | try { | |
@@ -333,39 +342,45 @@ public void run() { | |||
| 333 | 342 | * @param data data to write | |
| 334 | 343 | */ | |
| 335 | 344 | 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 | + } | ||
| 343 | 354 | } | |
| 344 | 355 | } | |
| 345 | 356 | } | |
| 346 | 357 | ||
| 347 | 358 | ||
| 348 | 359 | 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 | + } | ||
| 356 | 369 | } | |
| 357 | 370 | } | |
| 358 | 371 | } | |
| 359 | 372 | ||
| 360 | 373 | ||
| 361 | 374 | 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 | + } | ||
| 369 | 384 | } | |
| 370 | 385 | } | |
| 371 | 386 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments