public void listenUDP(EndPoint localEp) { UdpConnection connection = new UdpConnection(); logger_.debug("Starting to listen on " + localEp); try { connection.init(localEp.getPort()); endPoints_.add(localEp); } catch (IOException e) { logger_.warn(LogUtil.throwableToString(e)); } }
/** * 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; } }
public void sendUdpOneWay(Message message, EndPoint to) { EndPoint from = message.getFrom(); if (message.getFrom().equals(to)) { MessagingService.receive(message); return; } UdpConnection connection = null; try { connection = new UdpConnection(); connection.init(); connection.write(message, to); } catch (IOException e) { logger_.warn(LogUtil.throwableToString(e)); } finally { if (connection != null) connection.close(); } }
@Override public void run() { if (count / 4 > 0) { udp.mDeviceInfo.setPoint(new Point("0", "0", String.valueOf(++count))); } udp.sendBroadcast(); mHandler.postDelayed(mRunnable, 300); count++; }
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); }
public static UdpResponse communicate(final UdpConnection connection, final UdpRequest request) throws UdpConnectionException { return connection.communicate(request); }