int length = socketChannel.write(byteBufferRequest);
if (length > 0) {
if (byteBufferRequest.hasRemaining()) {
if ((System.currentTimeMillis() - beginTime) > timeoutMillis) {
// 发送请求超时
throw new RemotingSendRequestException(addr);
}
}
}
else {
throw new RemotingSendRequestException(addr);
}
// 比较土
Thread.sleep(1);
}
sendRequestOK = true;
// 接收应答 SIZE
ByteBuffer byteBufferSize = ByteBuffer.allocate(4);
while (byteBufferSize.hasRemaining()) {
int length = socketChannel.read(byteBufferSize);
if (length > 0) {
if (byteBufferSize.hasRemaining()) {
if ((System.currentTimeMillis() - beginTime) > timeoutMillis) {
// 接收应答超时
throw new RemotingTimeoutException(addr, timeoutMillis);
}
}
}
else {
throw new RemotingTimeoutException(addr, timeoutMillis);
}
// 比较土
Thread.sleep(1);
}
// 接收应答 BODY
int size = byteBufferSize.getInt(0);
ByteBuffer byteBufferBody = ByteBuffer.allocate(size);
while (byteBufferBody.hasRemaining()) {
int length = socketChannel.read(byteBufferBody);
if (length > 0) {
if (byteBufferBody.hasRemaining()) {
if ((System.currentTimeMillis() - beginTime) > timeoutMillis) {
// 接收应答超时
throw new RemotingTimeoutException(addr, timeoutMillis);
}
}
}
else {
throw new RemotingTimeoutException(addr, timeoutMillis);
}
// 比较土
Thread.sleep(1);
}
// 对应答数据解码
byteBufferBody.flip();
return RemotingCommand.decode(byteBufferBody);
}
catch (IOException e) {
e.printStackTrace();
if (sendRequestOK) {
throw new RemotingTimeoutException(addr, timeoutMillis);
}
else {
throw new RemotingSendRequestException(addr);
}
}
finally {
try {
socketChannel.close();