// The thread which sends data to the server public void sendOutgoing() { List<byte[]> m = null; while (isConnected()) { try { try { Thread.sleep(100); } catch (Exception e) { } if (toSend.size() == 0) continue; m = toSend.getQueue(); for (int i = 0; i < m.size(); i++) outStream.write(m.get(i)); } catch (Exception e) { e.printStackTrace(); close(); // stream error // break; } } }
// handle all events in this threads event queue protected void handleEvents() { PluginMethod[] evts = events.getQueue().toArray(new PluginMethod[0]); for (int i = 0; i < evts.length; i++) { try { Method m = getClass().getMethod(evts[i].getName(), evts[i].getArgClasses()); try { m.invoke(this, evts[i].getArgs()); } catch (Exception e) { log("Unhandled Event Error (" + evts[i].getName() + ") " + e.getMessage()); } } catch (Exception e) { // The plugin doesnt handle the event } } }
// The thread which listens for server responses public void listenForIncoming() { while (isConnected()) { try { String n = bReader.readLine(); if (n == null) { close(); // the server disconnected us System.out.println("N WAS NULL"); continue; } recieved.add(n); try { Thread.sleep(10); } catch (Exception e) { } } catch (Exception e) { e.printStackTrace(); close(); // stream error // break; } } }
// sends a line to the output buffer to be sent to the server. public void sendLine(String n) { toSend.add((n + "\r\n").getBytes()); }
// Returns the current contents of the incoming queue, then empty's the queue as well. public List<String> getIncoming() { return recieved.getQueue(); }
// add event to be procesed by this thread public void addEvent(PluginMethod e) { events.add(e); }