/** * 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; } }
public void close() { boolean wasConnected = isConnected; isConnected = false; tcp.close(); if (udp != null && udp.connectedAddress != null) udp.close(); if (wasConnected) { notifyDisconnected(); if (INFO) info("kryonet", this + " disconnected."); } setConnected(false); }
/** * If the percent of the TCP write buffer that is filled is less than the specified threshold, * {@link Listener#idle(Connection)} will be called for each network thread update. Default is * 0.1. */ public void setIdleThreshold(float idleThreshold) { tcp.idleThreshold = idleThreshold; }
/** * Workaround for broken NIO networking on Android 1.6. If true, the underlying NIO buffer is * always copied to the beginning of the buffer before being given to the SocketChannel for * sending. The Harmony SocketChannel implementation in Android 1.6 ignores the buffer position, * always copying from the beginning of the buffer. This is fixed in Android 2.0+. */ public void setBufferPositionFix(boolean bufferPositionFix) { tcp.bufferPositionFix = bufferPositionFix; }
/** * If the specified amount of time passes without receiving an object over TCP, the connection is * considered closed. When a TCP socket is closed normally, the remote end is notified immediately * and this timeout is not needed. However, if a socket is closed abnormally (eg, power loss), * KryoNet uses this timeout to detect the problem. The timeout should be set higher than the * {@link #setKeepAliveTCP(int) TCP keep alive} for the remote end of the connection. The keep * alive ensures that the remote end of the connection will be constantly sending objects, and * setting the timeout higher than the keep alive allows for network latency. Set to zero to * disable. Defaults to 12000. */ public void setTimeout(int timeoutMillis) { tcp.timeoutMillis = timeoutMillis; }
/** * An empty object will be sent if the TCP connection has not sent an object within the specified * milliseconds. Periodically sending a keep alive ensures that an abnormal close is detected in a * reasonable amount of time (see {@link #setTimeout(int)} ). Also, some network hardware will * close a TCP connection that ceases to transmit for a period of time (typically 1+ minutes). Set * to zero to disable. Defaults to 8000. */ public void setKeepAliveTCP(int keepAliveMillis) { tcp.keepAliveMillis = keepAliveMillis; }