use of com.biglybt.core.peermanager.messaging.Message in project BiglyBT by BiglySoftware.
the class HTTPNetworkConnection method sendAndClose.
protected void sendAndClose(String data) {
final Message http_message = new HTTPMessage(data);
getConnection().getOutgoingMessageQueue().registerQueueListener(new OutgoingMessageQueue.MessageQueueListener() {
@Override
public boolean messageAdded(Message message) {
return (true);
}
@Override
public void messageQueued(Message message) {
}
@Override
public void messageRemoved(Message message) {
}
@Override
public void messageSent(Message message) {
if (message == http_message) {
close("Close after message send complete");
}
}
@Override
public void protocolBytesSent(int byte_count) {
}
@Override
public void dataBytesSent(int byte_count) {
}
@Override
public void flush() {
}
});
getConnection().getOutgoingMessageQueue().addMessage(http_message, false);
}
use of com.biglybt.core.peermanager.messaging.Message in project BiglyBT by BiglySoftware.
the class AZMessageDecoder method postReadProcess.
private int postReadProcess() throws IOException {
int prot_bytes_read = 0;
int data_bytes_read = 0;
if (!reading_length_mode && !destroyed) {
// reading payload data mode
// ensure-restore proper buffer limits
payload_buffer.limit(SS, message_length);
length_buffer.limit(SS, 4);
int curr_position = payload_buffer.position(SS);
int read = curr_position - pre_read_start_position;
if (msg_id_bytes == null && curr_position >= 4) {
// need to have read the message id length first 4 bytes
payload_buffer.position(SS, 0);
int id_size = payload_buffer.getInt(SS);
// restore
payload_buffer.position(SS, curr_position);
if (id_size < 1 || id_size > 1024)
throw new IOException("invalid id_size [" + id_size + "]");
msg_id_bytes = new byte[id_size];
}
if (msg_id_bytes != null && curr_position >= msg_id_bytes.length + 4) {
// need to have also read the message id bytes
if (!msg_id_read_complete) {
payload_buffer.position(SS, 4);
payload_buffer.get(SS, msg_id_bytes);
// restore
payload_buffer.position(SS, curr_position);
msg_id_read_complete = true;
}
Message message = MessageManager.getSingleton().lookupMessage(msg_id_bytes);
if (message == null) {
Debug.out("Unknown message type '" + new String(msg_id_bytes) + "'");
throw (new IOException("Unknown message type"));
}
if (message.getType() == Message.TYPE_DATA_PAYLOAD) {
data_bytes_read += read;
} else {
prot_bytes_read += read;
}
} else {
prot_bytes_read += read;
}
if (!payload_buffer.hasRemaining(SS) && !is_paused) {
// full message received!
// prepare for use
payload_buffer.position(SS, 0);
DirectByteBuffer ref_buff = payload_buffer;
payload_buffer = null;
try {
Message msg = AZMessageFactory.createAZMessage(ref_buff);
messages_last_read.add(msg);
} catch (Throwable e) {
ref_buff.returnToPoolIfNotFree();
if (e instanceof RuntimeException) {
throw ((RuntimeException) e);
}
throw new IOException("AZ message decode failed: " + e.getMessage());
}
// see if we've already read the next message's length
reading_length_mode = true;
// reset receive percentage
percent_complete = -1;
msg_id_bytes = null;
msg_id_read_complete = false;
} else {
// only partial received so far
// compute receive percentage
percent_complete = (payload_buffer.position(SS) * 100) / message_length;
}
}
if (reading_length_mode && !destroyed) {
// ensure proper buffer limit
length_buffer.limit(SS, 4);
prot_bytes_read += (pre_read_start_buffer == 1) ? length_buffer.position(SS) - pre_read_start_position : length_buffer.position(SS);
if (!length_buffer.hasRemaining(SS)) {
// done reading the length
reading_length_mode = false;
length_buffer.position(SS, 0);
message_length = length_buffer.getInt(SS);
// reset it for next length read
length_buffer.position(SS, 0);
if (message_length < MIN_MESSAGE_LENGTH || message_length > maximum_message_size) {
throw new IOException("Invalid message length given for AZ message decode: " + message_length + " (max=" + maximum_message_size + ")");
}
payload_buffer = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_MSG_AZ_PAYLOAD, message_length);
}
}
protocol_bytes_last_read += prot_bytes_read;
data_bytes_last_read += data_bytes_read;
return prot_bytes_read + data_bytes_read;
}
use of com.biglybt.core.peermanager.messaging.Message in project BiglyBT by BiglySoftware.
the class IncomingMessageQueueImpl method receiveFromTransport.
/**
* Receive (read) message(s) data from the underlying transport.
* @param max_bytes to read
* @return number of bytes received
* @throws IOException on receive error
*/
@Override
public int[] receiveFromTransport(int max_bytes, boolean protocol_is_free) throws IOException {
if (max_bytes < 1) {
if (!protocol_is_free) {
Debug.out("max_bytes < 1: " + max_bytes);
}
return new int[2];
}
if (listeners.isEmpty()) {
Debug.out("no queue listeners registered!");
throw new IOException("no queue listeners registered!");
}
int bytes_read;
try {
// perform decode op
bytes_read = stream_decoder.performStreamDecode(connection.getTransport(), max_bytes);
} catch (RuntimeException e) {
Debug.out("Stream decode for " + connection.getString() + " failed: " + Debug.getNestedExceptionMessageAndStack(e));
throw (e);
}
// check if anything was decoded and notify listeners if so
Message[] messages = stream_decoder.removeDecodedMessages();
if (messages != null) {
for (int i = 0; i < messages.length; i++) {
Message msg = messages[i];
if (msg == null) {
System.out.println("received msg == null [messages.length=" + messages.length + ", #" + i + "]: " + connection.getTransport().getDescription());
continue;
}
// copy-on-write
ArrayList listeners_ref = listeners;
boolean handled = false;
for (int x = 0; x < listeners_ref.size(); x++) {
MessageQueueListener mql = (MessageQueueListener) listeners_ref.get(x);
if (mql.messageReceived(msg)) {
handled = true;
}
}
if (!handled) {
if (listeners_ref.size() > 0) {
System.out.println("no registered listeners [out of " + listeners_ref.size() + "] handled decoded message [" + msg.getDescription() + "]");
}
DirectByteBuffer[] buffs = msg.getData();
for (int x = 0; x < buffs.length; x++) {
buffs[x].returnToPool();
}
}
}
}
int protocol_read = stream_decoder.getProtocolBytesDecoded();
if (protocol_read > 0) {
// copy-on-write
ArrayList listeners_ref = listeners;
for (int i = 0; i < listeners_ref.size(); i++) {
MessageQueueListener mql = (MessageQueueListener) listeners_ref.get(i);
mql.protocolBytesReceived(protocol_read);
}
}
int data_read = stream_decoder.getDataBytesDecoded();
if (data_read > 0) {
// copy-on-write
ArrayList listeners_ref = listeners;
for (int i = 0; i < listeners_ref.size(); i++) {
MessageQueueListener mql = (MessageQueueListener) listeners_ref.get(i);
mql.dataBytesReceived(data_read);
}
}
// ideally bytes_read = data_read + protocol_read. in case it isn't then we want to
// return bytes_read = d + p with bias to p
data_read = bytes_read - protocol_read;
if (data_read < 0) {
protocol_read = bytes_read;
data_read = 0;
}
return (new int[] { data_read, protocol_read });
}
use of com.biglybt.core.peermanager.messaging.Message in project BiglyBT by BiglySoftware.
the class HTTPNetworkConnection method encodePiece.
protected RawMessage[] encodePiece(Message message) {
last_http_activity_time = SystemTime.getCurrentTime();
BTPiece piece = (BTPiece) message;
List<pendingRequest> ready_requests = new ArrayList<>();
boolean found = false;
synchronized (outstanding_requests) {
if (destroyed) {
return (new RawMessage[] { getEmptyRawMessage(message) });
}
for (int i = 0; i < outstanding_requests.size(); i++) {
pendingRequest req = outstanding_requests.get(i);
if (req.getPieceNumber() == piece.getPieceNumber() && req.getStart() == piece.getPieceOffset() && req.getLength() == piece.getPieceData().remaining(DirectByteBuffer.SS_NET)) {
if (req.getBTPiece() == null) {
req.setBTPiece(piece);
found = true;
if (i == 0) {
Iterator<pendingRequest> it = outstanding_requests.iterator();
while (it.hasNext()) {
pendingRequest r = it.next();
BTPiece btp = r.getBTPiece();
if (btp == null) {
break;
}
it.remove();
ready_requests.add(r);
}
}
break;
}
}
}
}
if (!found) {
Debug.out("request not matched");
return (new RawMessage[] { getEmptyRawMessage(message) });
}
if (ready_requests.size() == 0) {
return (new RawMessage[] { getEmptyRawMessage(message) });
}
try {
submitBTRequests();
} catch (IOException e) {
}
pendingRequest req = (pendingRequest) ready_requests.get(0);
DirectByteBuffer[] buffers;
httpRequest http_request = req.getHTTPRequest();
RawMessage[] raw_messages = new RawMessage[ready_requests.size()];
for (int i = 0; i < raw_messages.length; i++) {
buffers = new DirectByteBuffer[2];
if (!http_request.hasSentFirstReply()) {
http_request.setSentFirstReply();
String header = encodeHeader(http_request);
buffers[0] = new DirectByteBuffer(ByteBuffer.wrap(header.getBytes()));
} else {
// we have to do this as core code assumes buffer entry 0 is protocol
buffers[0] = new DirectByteBuffer(ByteBuffer.allocate(0));
}
req = (pendingRequest) ready_requests.get(i);
BTPiece this_piece = req.getBTPiece();
int piece_number = this_piece.getPieceNumber();
if (!piece_map.get(piece_number)) {
// kinda crappy as it triggers on first block of piece, however better
// than nothing
piece_map.set(piece_number);
decoder.addMessage(new BTHave(piece_number, (byte) 1));
}
buffers[1] = this_piece.getPieceData();
req.logQueued();
if (request_listeners != null) {
Iterator<requestListener> it = request_listeners.iterator();
while (it.hasNext()) {
((requestListener) it.next()).requestComplete(req);
}
}
raw_messages[i] = new RawMessageImpl(this_piece, buffers, RawMessage.PRIORITY_HIGH, true, new Message[0]);
}
return (raw_messages);
}
use of com.biglybt.core.peermanager.messaging.Message in project BiglyBT by BiglySoftware.
the class HTTPNetworkConnection method flushRequestsSupport.
protected void flushRequestsSupport(final flushListener l) {
OutgoingMessageQueue omq = getConnection().getOutgoingMessageQueue();
final Message http_message = new HTTPMessage(new byte[0]);
omq.registerQueueListener(new OutgoingMessageQueue.MessageQueueListener() {
@Override
public boolean messageAdded(Message message) {
return (true);
}
@Override
public void messageQueued(Message message) {
}
@Override
public void messageRemoved(Message message) {
}
@Override
public void messageSent(Message message) {
if (message == http_message) {
l.flushed();
}
}
@Override
public void protocolBytesSent(int byte_count) {
}
@Override
public void dataBytesSent(int byte_count) {
}
@Override
public void flush() {
}
});
omq.addMessage(http_message, false);
if (omq.getTotalSize() == 0) {
l.flushed();
}
}
Aggregations