Beispiel #1
0
  /**
   * Sends the object over the network using UDP.
   *
   * @return The number of bytes sent.
   * @see Kryo#register(Class, com.esotericsoftware.kryo.Serializer)
   * @throws IllegalStateException if this connection was not opened with both TCP and UDP.
   */
  public int sendUDP(Object object) {
    if (object == null) throw new IllegalArgumentException("object cannot be null.");
    SocketAddress address = udpRemoteAddress;
    if (address == null && udp != null) address = udp.connectedAddress;
    if (address == null && isConnected)
      throw new IllegalStateException("Connection is not connected via UDP.");

    try {
      if (address == null) throw new SocketException("Connection is closed.");

      int length = udp.send(this, object, address);
      if (length == 0) {
        if (TRACE) trace("kryonet", this + " UDP had nothing to send.");
      } else if (DEBUG) {
        if (length != -1) {
          String objectString = object == null ? "null" : object.getClass().getSimpleName();
          if (!(object instanceof FrameworkMessage)) {
            debug("kryonet", this + " sent UDP: " + objectString + " (" + length + ")");
          } else if (TRACE) {
            trace("kryonet", this + " sent UDP: " + objectString + " (" + length + ")");
          }
        } else debug("kryonet", this + " was unable to send, UDP socket buffer full.");
      }
      return length;
    } catch (IOException ex) {
      if (DEBUG) debug("kryonet", "Unable to send UDP with connection: " + this, ex);
      close();
      return 0;
    } catch (KryoNetException ex) {
      if (ERROR) error("kryonet", "Unable to send UDP with connection: " + this, ex);
      close();
      return 0;
    }
  }