Tcp Doc Polishing

Also remove some `this.` method calls.
This commit is contained in:
Gary Russell
2019-02-04 12:32:56 -05:00
committed by Artem Bilan
parent f82c7164f7
commit 6391e4bebf
3 changed files with 70 additions and 52 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2001-2018 the original author or authors.
* Copyright 2001-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -81,7 +81,7 @@ public class TcpNetConnection extends TcpConnectionSupport implements Scheduling
*/
@Override
public void close() {
this.setNoReadErrorOnClose(true);
setNoReadErrorOnClose(true);
try {
this.socket.close();
}
@@ -111,8 +111,8 @@ public class TcpNetConnection extends TcpConnectionSupport implements Scheduling
this.socketOutputStream.flush();
}
catch (Exception e) {
this.publishConnectionExceptionEvent(new MessagingException(message, "Failed TCP serialization", e));
this.closeConnection(true);
publishConnectionExceptionEvent(new MessagingException(message, "Failed TCP serialization", e));
closeConnection(true);
throw e;
}
if (logger.isDebugEnabled()) {
@@ -122,7 +122,8 @@ public class TcpNetConnection extends TcpConnectionSupport implements Scheduling
@Override
public Object getPayload() throws Exception {
return this.getDeserializer().deserialize(inputStream());
return getDeserializer()
.deserialize(inputStream());
}
@Override
@@ -172,16 +173,16 @@ public class TcpNetConnection extends TcpConnectionSupport implements Scheduling
public void run() {
boolean okToRun = true;
if (logger.isDebugEnabled()) {
logger.debug(this.getConnectionId() + " Reading...");
logger.debug(getConnectionId() + " Reading...");
}
while (okToRun) {
Message<?> message = null;
try {
message = this.getMapper().toMessage(this);
message = getMapper().toMessage(this);
this.lastRead = System.currentTimeMillis();
}
catch (Exception e) {
this.publishConnectionExceptionEvent(e);
publishConnectionExceptionEvent(e);
if (handleReadException(e)) {
okToRun = false;
}
@@ -218,7 +219,7 @@ public class TcpNetConnection extends TcpConnectionSupport implements Scheduling
* For client connections, we have to wait for 2 timeouts if the last
* send was within the current timeout.
*/
if (!this.isServer() && e instanceof SocketTimeoutException) {
if (!isServer() && e instanceof SocketTimeoutException) {
long now = System.currentTimeMillis();
try {
int soTimeout = this.socket.getSoTimeout();
@@ -226,7 +227,7 @@ public class TcpNetConnection extends TcpConnectionSupport implements Scheduling
doClose = false;
}
if (!doClose && logger.isDebugEnabled()) {
logger.debug("Skipping a socket timeout because we have a recent send " + this.getConnectionId());
logger.debug("Skipping a socket timeout because we have a recent send " + getConnectionId());
}
}
catch (SocketException e1) {
@@ -234,39 +235,39 @@ public class TcpNetConnection extends TcpConnectionSupport implements Scheduling
}
}
if (doClose) {
boolean noReadErrorOnClose = this.isNoReadErrorOnClose();
boolean noReadErrorOnClose = isNoReadErrorOnClose();
closeConnection(true);
if (!(e instanceof SoftEndOfStreamException)) {
if (e instanceof SocketTimeoutException) {
if (logger.isDebugEnabled()) {
logger.debug("Closed socket after timeout:" + this.getConnectionId());
logger.debug("Closed socket after timeout:" + getConnectionId());
}
}
else {
if (noReadErrorOnClose) {
if (logger.isTraceEnabled()) {
logger.trace("Read exception " +
this.getConnectionId(), e);
getConnectionId(), e);
}
else if (logger.isDebugEnabled()) {
logger.debug("Read exception " +
this.getConnectionId() + " " +
getConnectionId() + " " +
e.getClass().getSimpleName() +
":" + (e.getCause() != null ? e.getCause() + ":" : "") + e.getMessage());
}
}
else if (logger.isTraceEnabled()) {
logger.error("Read exception " +
this.getConnectionId(), e);
getConnectionId(), e);
}
else {
logger.error("Read exception " +
this.getConnectionId() + " " +
getConnectionId() + " " +
e.getClass().getSimpleName() +
":" + (e.getCause() != null ? e.getCause() + ":" : "") + e.getMessage());
}
}
this.sendExceptionToListener(e);
sendExceptionToListener(e);
}
}
return doClose;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -115,7 +115,7 @@ public class TcpNioConnection extends TcpConnectionSupport {
@Override
public void close() {
this.setNoReadErrorOnClose(true);
setNoReadErrorOnClose(true);
doClose();
}
@@ -144,19 +144,19 @@ public class TcpNioConnection extends TcpConnectionSupport {
synchronized (this.socketChannel) {
if (this.bufferedOutputStream == null) {
int writeBufferSize = this.socketChannel.socket().getSendBufferSize();
this.bufferedOutputStream = new BufferedOutputStream(this.getChannelOutputStream(),
this.bufferedOutputStream = new BufferedOutputStream(getChannelOutputStream(),
writeBufferSize > 0 ? writeBufferSize : 8192);
}
Object object = this.getMapper().fromMessage(message);
Object object = getMapper().fromMessage(message);
Assert.state(object != null, "Mapper mapped the message to 'null'.");
this.lastSend = System.currentTimeMillis();
try {
((Serializer<Object>) this.getSerializer()).serialize(object, this.bufferedOutputStream);
((Serializer<Object>) getSerializer()).serialize(object, this.bufferedOutputStream);
this.bufferedOutputStream.flush();
}
catch (Exception e) {
this.publishConnectionExceptionEvent(new MessagingException(message, "Failed TCP serialization", e));
this.closeConnection(true);
publishConnectionExceptionEvent(new MessagingException(message, "Failed TCP serialization", e));
closeConnection(true);
throw e;
}
if (logger.isDebugEnabled()) {
@@ -167,7 +167,8 @@ public class TcpNioConnection extends TcpConnectionSupport {
@Override
public Object getPayload() throws Exception {
return this.getDeserializer().deserialize(inputStream());
return getDeserializer()
.deserialize(inputStream());
}
@Override
@@ -224,7 +225,7 @@ public class TcpNioConnection extends TcpConnectionSupport {
@Override
public void run() {
if (logger.isTraceEnabled()) {
logger.trace(this.getConnectionId() + " Nio message assembler running...");
logger.trace(getConnectionId() + " Nio message assembler running...");
}
boolean moreDataAvailable = true;
while (moreDataAvailable) {
@@ -242,8 +243,10 @@ public class TcpNioConnection extends TcpConnectionSupport {
catch (RejectedExecutionException e) {
this.executionControl.decrementAndGet();
if (logger.isInfoEnabled()) {
logger.info(getConnectionId() + " Insufficient threads in the assembler fixed thread pool; consider " +
"increasing this task executor pool size; data avail: " + this.channelInputStream.available());
logger.info(getConnectionId()
+ " Insufficient threads in the assembler fixed thread pool; consider "
+ "increasing this task executor pool size; data avail: "
+ this.channelInputStream.available());
}
}
}
@@ -259,24 +262,24 @@ public class TcpNioConnection extends TcpConnectionSupport {
catch (Exception e) {
if (logger.isTraceEnabled()) {
logger.error("Read exception " +
this.getConnectionId(), e);
getConnectionId(), e);
}
else if (!this.isNoReadErrorOnClose()) {
else if (!isNoReadErrorOnClose()) {
logger.error("Read exception " +
this.getConnectionId() + " " +
getConnectionId() + " " +
e.getClass().getSimpleName() +
":" + e.getCause() + ":" + e.getMessage());
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Read exception " +
this.getConnectionId() + " " +
getConnectionId() + " " +
e.getClass().getSimpleName() +
":" + e.getCause() + ":" + e.getMessage());
}
}
this.closeConnection(true);
this.sendExceptionToListener(e);
closeConnection(true);
sendExceptionToListener(e);
return;
}
}
@@ -301,12 +304,13 @@ public class TcpNioConnection extends TcpConnectionSupport {
}
if (moreDataAvailable) {
if (logger.isTraceEnabled()) {
logger.trace(this.getConnectionId() + " Nio message assembler continuing...");
logger.trace(getConnectionId() + " Nio message assembler continuing...");
}
}
else {
if (logger.isTraceEnabled()) {
logger.trace(this.getConnectionId() + " Nio message assembler exiting... avail: " + this.channelInputStream.available());
logger.trace(getConnectionId() + " Nio message assembler exiting... avail: "
+ this.channelInputStream.available());
}
}
}
@@ -355,13 +359,13 @@ public class TcpNioConnection extends TcpConnectionSupport {
}
Message<?> message = null;
try {
message = this.getMapper().toMessage(this);
message = getMapper().toMessage(this);
}
catch (Exception e) {
this.closeConnection(true);
closeConnection(true);
if (e instanceof SocketTimeoutException) {
if (logger.isDebugEnabled()) {
logger.debug("Closing socket after timeout " + this.getConnectionId());
logger.debug("Closing socket after timeout " + getConnectionId());
}
}
else {
@@ -420,7 +424,7 @@ public class TcpNioConnection extends TcpConnectionSupport {
int len = this.socketChannel.read(this.rawBuffer);
if (len < 0) {
this.writingToPipe = false;
this.closeConnection(true);
closeConnection(true);
}
if (logger.isTraceEnabled()) {
logger.trace("After read:" + this.rawBuffer.position() + "/" + this.rawBuffer.limit());
@@ -432,13 +436,13 @@ public class TcpNioConnection extends TcpConnectionSupport {
if (logger.isDebugEnabled()) {
logger.debug("Read " + this.rawBuffer.limit() + " into raw buffer");
}
this.sendToPipe(this.rawBuffer);
sendToPipe(this.rawBuffer);
}
catch (RejectedExecutionException e) {
throw e;
}
catch (Exception e) {
this.publishConnectionExceptionEvent(e);
publishConnectionExceptionEvent(e);
throw e;
}
finally {
@@ -450,7 +454,7 @@ public class TcpNioConnection extends TcpConnectionSupport {
protected void sendToPipe(ByteBuffer rawBuffer) throws IOException {
Assert.notNull(rawBuffer, "rawBuffer cannot be null");
if (logger.isTraceEnabled()) {
logger.trace(this.getConnectionId() + " Sending " + rawBuffer.limit() + " to pipe");
logger.trace(getConnectionId() + " Sending " + rawBuffer.limit() + " to pipe");
}
this.channelInputStream.write(rawBuffer);
rawBuffer.clear();
@@ -462,7 +466,7 @@ public class TcpNioConnection extends TcpConnectionSupport {
// only execute run() if we don't already have one running
this.executionControl.set(1);
if (logger.isDebugEnabled()) {
logger.debug(this.getConnectionId() + " Running an assembler");
logger.debug(getConnectionId() + " Running an assembler");
}
try {
this.taskExecutor.execute2(this);
@@ -487,25 +491,25 @@ public class TcpNioConnection extends TcpConnectionSupport {
*/
public void readPacket() {
if (logger.isDebugEnabled()) {
logger.debug(this.getConnectionId() + " Reading...");
logger.debug(getConnectionId() + " Reading...");
}
try {
doRead();
}
catch (ClosedChannelException cce) {
if (logger.isDebugEnabled()) {
logger.debug(this.getConnectionId() + " Channel is closed");
logger.debug(getConnectionId() + " Channel is closed");
}
this.closeConnection(true);
closeConnection(true);
}
catch (RejectedExecutionException e) {
throw e;
}
catch (Exception e) {
logger.error("Exception on Read " +
this.getConnectionId() + " " +
getConnectionId() + " " +
e.getMessage(), e);
this.closeConnection(true);
closeConnection(true);
}
}
@@ -514,7 +518,7 @@ public class TcpNioConnection extends TcpConnectionSupport {
*/
void timeout() {
this.timedOut = true;
this.closeConnection(true);
closeConnection(true);
}
/**