/** Returns the first object registered with the specified ID in any of the ObjectSpaces the specified connection belongs * to. */ static Object getRegisteredObject (Connection connection, int objectID) { ObjectSpace[] instances = ObjectSpace.instances; for (int i = 0, n = instances.length; i < n; i++) { ObjectSpace objectSpace = instances[i]; // Check if the connection is in this ObjectSpace. Connection[] connections = objectSpace.connections; for (int j = 0; j < connections.length; j++) { if (connections[j] != connection) continue; // Find an object with the objectID. Object object = objectSpace.idToObject.get(objectID); if (object != null) return object; } } return null; }
/** 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); }
void removeConnection (Connection connection) { ArrayList<Connection> temp = new ArrayList(Arrays.asList(connections)); temp.remove(connection); connections = temp.toArray(new Connection[temp.size()]); pendingConnections.remove(connection.id); }
@SuppressWarnings({"unchecked", "rawtypes"}) @Override public void write(Kryo kryo, Output output, T o) { try { ObjectMap graphContext = kryo.getGraphContext(); ObjectOutputStream objectStream = (ObjectOutputStream)graphContext.get(this); if (objectStream == null) { objectStream = new ObjectOutputStream(output); graphContext.put(this, objectStream); } objectStream.writeObject(o); objectStream.flush(); } catch (Exception ex) { throw new KryoException("Error during Java serialization.", ex); } }
public Object read (Kryo kryo, Input input, Class type) { int objectID = input.readInt(true); Connection connection = (Connection)kryo.getContext().get("connection"); return ObjectSpace.getRemoteObject(connection, objectID, type); } }
}, new MapReferenceResolver(), new DefaultStreamFactory());
public synchronized Object read (Connection connection, ByteBuffer buffer) { input.setBuffer(buffer); kryo.getContext().put("connection", connection); return kryo.readClassAndObject(input); }
/** 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); }
/** Increases the size of the backing array to acommodate the specified number of additional items. Useful before adding many * items to avoid multiple backing array resizes. */ public void ensureCapacity (int additionalCapacity) { int sizeNeeded = size + additionalCapacity; if (sizeNeeded >= threshold) resize(ObjectMap.nextPowerOfTwo((int)(sizeNeeded / loadFactor))); }
public Registration getRegistration(Class type) { instanceCounts.adjustOrPutValue(type, 1, 1); return super.getRegistration(type); }
@SuppressWarnings({"unchecked", "rawtypes"}) @Override public T read(Kryo kryo, Input input, Class aClass) { try { ObjectMap graphContext = kryo.getGraphContext(); ObjectInputStream objectStream = (ObjectInputStream)graphContext.get(this); if (objectStream == null) { // make sure we use Kryo's classloader objectStream = new InstantiationUtil.ClassLoaderObjectInputStream(input, kryo.getClassLoader()); graphContext.put(this, objectStream); } return (T) objectStream.readObject(); } catch (Exception ex) { throw new KryoException("Error during Java deserialization.", ex); } }
public void write (Kryo kryo, Output output, Object object) { Connection connection = (Connection)kryo.getContext().get("connection"); int id = getRegisteredID(connection, object); if (id == Integer.MAX_VALUE) throw new KryoNetException("Object not found in an ObjectSpace: " + object); output.writeInt(id, true); }
public synchronized void write (Connection connection, ByteBuffer buffer, Object object) { output.setBuffer(buffer); kryo.getContext().put("connection", connection); kryo.writeClassAndObject(output, object); output.flush(); }
public void received (final Connection connection, Object object) { if (!(object instanceof InvokeMethod)) return; if (connections != null) { int i = 0, n = connections.length; for (; i < n; i++) if (connection == connections[i]) break; if (i == n) return; // The InvokeMethod message is not for a connection in this ObjectSpace. } final InvokeMethod invokeMethod = (InvokeMethod)object; final Object target = idToObject.get(invokeMethod.objectID); if (target == null) { if (WARN) warn("kryonet", "Ignoring remote invocation request for unknown object ID: " + invokeMethod.objectID); return; } if (executor == null) invoke(connection, target, invokeMethod); else { executor.execute(new Runnable() { public void run () { invoke(connection, target, invokeMethod); } }); } }
private void acceptOperation (SocketChannel socketChannel) { Connection connection = newConnection(); connection.initialize(serialization, writeBufferSize, objectBufferSize); connection.endPoint = this; UdpConnection udp = this.udp; if (udp != null) connection.udp = udp; try { SelectionKey selectionKey = connection.tcp.accept(selector, socketChannel); selectionKey.attach(connection); int id = nextConnectionID++; if (nextConnectionID == -1) nextConnectionID = 1; connection.id = id; connection.setConnected(true); connection.addListener(dispatchListener); if (udp == null) addConnection(connection); else pendingConnections.put(id, connection); RegisterTCP registerConnection = new RegisterTCP(); registerConnection.connectionID = id; connection.sendTCP(registerConnection); if (udp == null) connection.notifyConnected(); } catch (IOException ex) { connection.close(); if (DEBUG) debug("kryonet", "Unable to accept TCP connection.", ex); } }
/** Reduces the size of the backing arrays to be the specified capacity or less. If the capacity is already less, nothing is * done. If the map contains more items than the specified capacity, the next highest power of two capacity is used instead. */ public void shrink (int maximumCapacity) { if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity); if (size > maximumCapacity) maximumCapacity = size; if (capacity <= maximumCapacity) return; maximumCapacity = ObjectMap.nextPowerOfTwo(maximumCapacity); resize(maximumCapacity); }
/** 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); }
public Object read (Kryo kryo, Input input, Class type) { int objectID = input.readInt(true); Connection connection = (Connection)kryo.getContext().get("connection"); Object object = getRegisteredObject(connection, objectID); if (WARN && object == null) warn("kryonet", "Unknown object ID " + objectID + " for connection: " + connection); return object; } });
/** Creates a new map with the specified initial capacity and load factor. This map will hold initialCapacity * loadFactor items * before growing the backing table. */ public ObjectIntMap (int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity); if (initialCapacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity); capacity = ObjectMap.nextPowerOfTwo(initialCapacity); if (loadFactor <= 0) throw new IllegalArgumentException("loadFactor must be > 0: " + loadFactor); this.loadFactor = loadFactor; threshold = (int)(capacity * loadFactor); mask = capacity - 1; hashShift = 31 - Integer.numberOfTrailingZeros(capacity); stashCapacity = Math.max(3, (int)Math.ceil(Math.log(capacity)) * 2); pushIterations = Math.max(Math.min(capacity, 8), (int)Math.sqrt(capacity) / 8); keyTable = (K[])new Object[capacity + stashCapacity]; valueTable = new int[keyTable.length]; }
Connection connection = pendingConnections.remove(fromConnectionID); if (connection != null) { if (connection.udpRemoteAddress != null) continue outer;