public void addListener (Listener listener) { if (listener == null) throw new IllegalArgumentException("listener cannot be null."); synchronized (listenerLock) { Listener[] listeners = this.listeners; int n = listeners.length; for (int i = 0; i < n; i++) if (listener == listeners[i]) return; Listener[] newListeners = new Listener[n + 1]; newListeners[0] = listener; System.arraycopy(listeners, 0, newListeners, 1, n); this.listeners = newListeners; } if (TRACE) trace("kryonet", "Server listener added: " + listener.getClass().getName()); }
/** If the listener already exists, it is not added again. */ public void addListener (Listener listener) { if (listener == null) throw new IllegalArgumentException("listener cannot be null."); synchronized (listenerLock) { Listener[] listeners = this.listeners; int n = listeners.length; for (int i = 0; i < n; i++) if (listener == listeners[i]) return; Listener[] newListeners = new Listener[n + 1]; newListeners[0] = listener; System.arraycopy(listeners, 0, newListeners, 1, n); this.listeners = newListeners; } if (TRACE) trace("kryonet", "Connection listener added: " + listener.getClass().getName()); }
public void removeListener (Listener listener) { if (listener == null) throw new IllegalArgumentException("listener cannot be null."); synchronized (listenerLock) { Listener[] listeners = this.listeners; int n = listeners.length; Listener[] newListeners = new Listener[n - 1]; for (int i = 0, ii = 0; i < n; i++) { Listener copyListener = listeners[i]; if (listener == copyListener) continue; if (ii == n - 1) return; newListeners[ii++] = copyListener; } this.listeners = newListeners; } if (TRACE) trace("kryonet", "Server listener removed: " + listener.getClass().getName()); }
public void removeListener (Listener listener) { if (listener == null) throw new IllegalArgumentException("listener cannot be null."); synchronized (listenerLock) { Listener[] listeners = this.listeners; int n = listeners.length; if (n == 0) return; Listener[] newListeners = new Listener[n - 1]; for (int i = 0, ii = 0; i < n; i++) { Listener copyListener = listeners[i]; if (listener == copyListener) continue; if (ii == n - 1) return; newListeners[ii++] = copyListener; } this.listeners = newListeners; } if (TRACE) trace("kryonet", "Connection listener removed: " + listener.getClass().getName()); }
/** Allows the remote end of the specified connection to access objects registered in this ObjectSpace. */ public void addConnection (Connection connection) { if (connection == null) throw new IllegalArgumentException("connection cannot be null."); synchronized (connectionsLock) { Connection[] newConnections = new Connection[connections.length + 1]; newConnections[0] = connection; System.arraycopy(connections, 0, newConnections, 1, connections.length); connections = newConnections; } connection.addListener(invokeListener); if (TRACE) trace("kryonet", "Added connection to ObjectSpace: " + connection); }
public void stop () { if (shutdown) return; close(); if (TRACE) trace("kryonet", "Server thread stopping."); shutdown = true; }
/** Causes this ObjectSpace to stop listening to the connections for method invocation messages. */ public void close () { Connection[] connections = this.connections; for (int i = 0; i < connections.length; i++) connections[i].removeListener(invokeListener); synchronized (instancesLock) { ArrayList<ObjectSpace> temp = new ArrayList(Arrays.asList(instances)); temp.remove(this); instances = temp.toArray(new ObjectSpace[temp.size()]); } if (TRACE) trace("kryonet", "Closed ObjectSpace."); }
public void removeListener (Listener listener) { super.removeListener(listener); if (TRACE) trace("kryonet", "Client listener removed."); }
public void stop () { if (shutdown) return; close(); if (TRACE) trace("kryonet", "Client thread stopping."); shutdown = true; selector.wakeup(); }
/** Removes the specified connection, it will no longer be able to access objects registered in this ObjectSpace. */ public void removeConnection (Connection connection) { if (connection == null) throw new IllegalArgumentException("connection cannot be null."); connection.removeListener(invokeListener); synchronized (connectionsLock) { ArrayList<Connection> temp = new ArrayList(Arrays.asList(connections)); temp.remove(connection); connections = temp.toArray(new Connection[temp.size()]); } if (TRACE) trace("kryonet", "Removed connection from ObjectSpace: " + connection); }
public void addListener (Listener listener) { super.addListener(listener); if (TRACE) trace("kryonet", "Client listener added."); }
/** Registers an object to allow the remote end of the ObjectSpace's connections to access it using the specified ID. * <p> * If a connection is added to multiple ObjectSpaces, the same object ID should not be registered in more than one of those * ObjectSpaces. * @param objectID Must not be Integer.MAX_VALUE. * @see #getRemoteObject(Connection, int, Class...) */ public void register (int objectID, Object object) { if (objectID == Integer.MAX_VALUE) throw new IllegalArgumentException("objectID cannot be Integer.MAX_VALUE."); if (object == null) throw new IllegalArgumentException("object cannot be null."); idToObject.put(objectID, object); objectToID.put(object, objectID); if (TRACE) trace("kryonet", "Object registered with ObjectSpace as " + objectID + ": " + object); }
public void run () { if (TRACE) trace("kryonet", "Server thread started."); shutdown = false; while (!shutdown) { try { update(250); } catch (IOException ex) { if (ERROR) error("kryonet", "Error updating server connections.", ex); close(); } } if (TRACE) trace("kryonet", "Server thread stopped."); }
/** Removes an object. The remote end of the ObjectSpace's connections will no longer be able to access it. */ public void remove (int objectID) { Object object = idToObject.remove(objectID); if (object != null) objectToID.remove(object, 0); if (TRACE) trace("kryonet", "Object " + objectID + " removed from ObjectSpace: " + object); }
void notifyReceived (Object object) { if (object instanceof Ping) { Ping ping = (Ping)object; if (ping.isReply) { if (ping.id == lastPingID - 1) { returnTripTime = (int)(System.currentTimeMillis() - lastPingSendTime); if (TRACE) trace("kryonet", this + " return trip time: " + returnTripTime); } } else { ping.isReply = true; sendTCP(ping); } } Listener[] listeners = this.listeners; for (int i = 0, n = listeners.length; i < n; i++) listeners[i].received(this, object); }
public void run () { if (TRACE) trace("kryonet", "Client thread started."); shutdown = false; while (!shutdown) { if (TRACE) { if (isConnected) trace("kryonet", "Unable to update connection: " + this, ex); else trace("kryonet", "Unable to update connection.", ex); } else if (DEBUG) { if (isConnected) if (TRACE) trace("kryonet", "Client thread stopped.");
debug("kryonet", connection + " TCP write buffer is approaching capacity: " + percentage + "%"); else if (TRACE && percentage > 0.25f) trace("kryonet", connection + " TCP write buffer utilization: " + percentage + "%");
/** Sends the object over the network using TCP. * @return The number of bytes sent. * @see Kryo#register(Class, com.esotericsoftware.kryo.Serializer) */ public int sendTCP (Object object) { if (object == null) throw new IllegalArgumentException("object cannot be null."); try { int length = tcp.send(this, object); if (length == 0) { if (TRACE) trace("kryonet", this + " TCP had nothing to send."); } else if (DEBUG) { String objectString = object == null ? "null" : object.getClass().getSimpleName(); if (!(object instanceof FrameworkMessage)) { debug("kryonet", this + " sent TCP: " + objectString + " (" + length + ")"); } else if (TRACE) { trace("kryonet", this + " sent TCP: " + objectString + " (" + length + ")"); } } return length; } catch (IOException ex) { if (DEBUG) debug("kryonet", "Unable to send TCP with connection: " + this, ex); close(); return 0; } catch (KryoNetException ex) { if (ERROR) error("kryonet", "Unable to send TCP with connection: " + this, ex); close(); return 0; } }
/** Removes an object. The remote end of the ObjectSpace's connections will no longer be able to access it. */ public void remove (Object object) { if (!idToObject.containsValue(object, true)) return; int objectID = idToObject.findKey(object, true, -1); idToObject.remove(objectID); objectToID.remove(object, 0); if (TRACE) trace("kryonet", "Object " + objectID + " removed from ObjectSpace: " + object); }