use of com.biglybt.pif.messaging.MessageException in project BiglyBT by BiglySoftware.
the class GenericMessageConnectionImpl method send.
@Override
public void send(PooledByteBuffer message) throws MessageException {
int size = ((PooledByteBufferImpl) message).getBuffer().remaining(DirectByteBuffer.SS_EXTERNAL);
if (size > getMaximumMessageSize()) {
throw (new MessageException("Message is too large: supplied is " + size + ", maximum is " + getMaximumMessageSize()));
}
delegate.send(message);
}
use of com.biglybt.pif.messaging.MessageException in project BiglyBT by BiglySoftware.
the class GenericMessageConnectionImpl method connectUDP.
protected void connectUDP(final ByteBuffer initial_data, final InetSocketAddress udp_ep, boolean nat_traversal) {
if (TRACE) {
System.out.println("UDP connection attempt to " + udp_ep + " (nat=" + nat_traversal + ")");
}
final GenericMessageEndpointImpl gen_udp = new GenericMessageEndpointImpl(endpoint.getNotionalAddress());
gen_udp.addUDP(udp_ep);
final GenericMessageConnectionAdapter udp_delegate = new GenericMessageConnectionDirect(msg_id, msg_desc, gen_udp, stream_crypto, shared_secrets);
udp_delegate.setOwner(this);
if (nat_traversal || TEST_TUNNEL) {
final NATTraverser nat_traverser = message_manager.getNATTraverser();
Map request = new HashMap();
nat_traverser.attemptTraversal(message_manager, udp_ep, request, false, new NATTraversalObserver() {
@Override
public void succeeded(final InetSocketAddress rendezvous, final InetSocketAddress target, Map reply) {
if (closed) {
reportFailed(new MessageException("Connection has been closed"));
} else {
connect_method_count++;
if (TEST_TUNNEL) {
initial_data.rewind();
connectTunnel(initial_data, gen_udp, rendezvous, target);
} else {
udp_delegate.connect(initial_data, new GenericMessageConnectionAdapter.ConnectionListener() {
private boolean connected;
@Override
public void connectSuccess() {
connected = true;
setDelegate(udp_delegate);
if (closed) {
try {
delegate.close();
} catch (Throwable e) {
}
reportFailed(new MessageException("Connection has been closed"));
} else {
reportConnected();
}
}
@Override
public void connectFailure(Throwable failure_msg) {
if (connected) {
reportFailed(failure_msg);
} else {
initial_data.rewind();
connectTunnel(initial_data, gen_udp, rendezvous, target);
}
}
});
}
}
}
@Override
public void failed(int failure_type) {
reportFailed(new MessageException("UDP connection attempt failed - NAT traversal failed (" + NATTraversalObserver.FT_STRINGS[failure_type] + ")"));
}
@Override
public void failed(Throwable cause) {
reportFailed(cause);
}
@Override
public void disabled() {
reportFailed(new MessageException("UDP connection attempt failed as DDB is disabled"));
}
});
} else {
udp_delegate.connect(initial_data, new GenericMessageConnectionAdapter.ConnectionListener() {
private boolean connected;
@Override
public void connectSuccess() {
connected = true;
setDelegate(udp_delegate);
if (closed) {
try {
delegate.close();
} catch (Throwable e) {
}
reportFailed(new MessageException("Connection has been closed"));
} else {
reportConnected();
}
}
@Override
public void connectFailure(Throwable failure_msg) {
if (connected) {
reportFailed(failure_msg);
} else {
initial_data.rewind();
connectUDP(initial_data, udp_ep, true);
}
}
});
}
}
use of com.biglybt.pif.messaging.MessageException in project BiglyBT by BiglySoftware.
the class GenericMessageConnectionIndirect method send.
@Override
public void send(PooledByteBuffer pbb) throws MessageException {
byte[] bytes = pbb.toByteArray();
if (TRACE) {
trace("send " + bytes.length);
}
if (incoming) {
synchronized (send_queue) {
if (send_queue.size() > 64) {
throw (new MessageException("Send queue limit exceeded"));
}
send_queue.add(bytes);
}
send_queue_sem.release();
} else {
List messages = new ArrayList();
messages.add(bytes);
send(messages);
}
}
use of com.biglybt.pif.messaging.MessageException in project BiglyBT by BiglySoftware.
the class GenericMessageConnectionIndirect method receive.
protected static Map receive(MessageManagerImpl message_manager, InetSocketAddress originator, Map message) {
if (TRACE) {
System.out.println("receive:" + originator + "/" + message);
}
if (!message.containsKey("type")) {
return (null);
}
int type = ((Long) message.get("type")).intValue();
if (type == MESSAGE_TYPE_CONNECT) {
String msg_id = new String((byte[]) message.get("msg_id"));
String msg_desc = new String((byte[]) message.get("msg_desc"));
GenericMessageEndpointImpl endpoint = new GenericMessageEndpointImpl(originator);
endpoint.addUDP(originator);
GenericMessageHandler handler = message_manager.getHandler(msg_id);
if (handler == null) {
Debug.out("No message handler registered for '" + msg_id + "'");
return (null);
}
try {
Long con_id;
synchronized (remote_connections) {
if (remote_connections.size() >= MAX_REMOTE_CONNECTIONS) {
Debug.out("Maximum remote connections exceeded - request from " + originator + " denied [" + getRemoteConnectionStatus() + "]");
return (null);
}
int num_from_this_ip = 0;
Iterator it = remote_connections.values().iterator();
while (it.hasNext()) {
GenericMessageConnectionIndirect con = (GenericMessageConnectionIndirect) it.next();
if (con.getEndpoint().getNotionalAddress().getAddress().equals(originator.getAddress())) {
num_from_this_ip++;
}
}
if (num_from_this_ip >= MAX_REMOTE_CONNECTIONS_PER_IP) {
Debug.out("Maximum remote connections per-ip exceeded - request from " + originator + " denied [" + getRemoteConnectionStatus() + "]");
return (null);
}
con_id = new Long(connection_id_next++);
}
GenericMessageConnectionIndirect indirect_connection = new GenericMessageConnectionIndirect(message_manager, msg_id, msg_desc, endpoint, con_id.longValue());
GenericMessageConnectionImpl new_connection = new GenericMessageConnectionImpl(message_manager, indirect_connection);
if (handler.accept(new_connection)) {
new_connection.accepted();
synchronized (remote_connections) {
remote_connections.put(con_id, indirect_connection);
}
List replies = indirect_connection.receive((List) message.get("data"));
Map reply = new HashMap();
reply.put("type", new Long(MESSAGE_TYPE_CONNECT));
reply.put("con_id", con_id);
reply.put("data", replies);
return (reply);
} else {
return (null);
}
} catch (MessageException e) {
Debug.out("Error accepting message", e);
return (null);
}
} else if (type == MESSAGE_TYPE_DATA) {
Long con_id = (Long) message.get("con_id");
GenericMessageConnectionIndirect indirect_connection;
synchronized (remote_connections) {
indirect_connection = (GenericMessageConnectionIndirect) remote_connections.get(con_id);
}
if (indirect_connection == null) {
return (null);
}
Map reply = new HashMap();
if (indirect_connection.isClosed()) {
reply.put("type", new Long(MESSAGE_TYPE_DISCONNECT));
} else {
List replies = indirect_connection.receive((List) message.get("data"));
reply.put("type", new Long(MESSAGE_TYPE_DATA));
reply.put("data", replies);
if (indirect_connection.receiveIncomplete()) {
reply.put("more_data", new Long(1));
}
}
return (reply);
} else {
// error or disconnect
Long con_id = (Long) message.get("con_id");
GenericMessageConnectionIndirect indirect_connection;
synchronized (remote_connections) {
indirect_connection = (GenericMessageConnectionIndirect) remote_connections.get(con_id);
}
if (indirect_connection != null) {
try {
indirect_connection.close(new Throwable("Remote closed connection"));
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
return (null);
}
}
Aggregations