Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,11 @@ private void writeBuffer() throws IOException {
}
}

private class WriteCheck implements WorkerTask {
private class WriteCheck extends WorkerTask {
@Override
public void run(long now) {
protected void run(long now) {
if (!key.isValid())
return;
if (hasPendingWrites())
key.interestOpsOr(SelectionKey.OP_WRITE);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public final void start() {
t.start();
}

public final boolean isAlive() {
return t.isAlive();
}

/**
* stop the loop
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private boolean checkHandshake(HandshakeStatus hs, long now) throws IOException
}
}

private final class RunTask implements Runnable, WorkerTask {
private final class RunTask extends WorkerTask implements Runnable {
@Override
public void run() {
logger.trace("start task");
Expand All @@ -175,7 +175,7 @@ public void run() {
}

@Override
public void run(long now) {
protected void run(long now) {
logger.trace("resume handshake");
try {
checkHandshake(sslEngine.getHandshakeStatus(), now);
Expand Down
36 changes: 29 additions & 7 deletions unknow-server-nio/src/main/java/unknow/server/nio/NIOWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
}

protected final void execute(WorkerTask task) {
listener.accepted(name, task);
tasks.add(task);
selector.wakeup();
}
Expand Down Expand Up @@ -99,8 +100,10 @@
@Override
protected void onSelect(long now, boolean close) {
WorkerTask r;
while ((r = tasks.poll()) != null)
r.run(now);
while ((r = tasks.poll()) != null) {
r.work(now);
listener.done(name, r);
}

checkPending(now, close);
finishClosing(now);
Expand Down Expand Up @@ -227,7 +230,7 @@
private void startClose(NIOConnection co, long now) {
if (!remove(co))
return;
logger.debug("{} start closing", co);
listener.closing(name, co);
closing.add(co);
co.startClose(now);
}
Expand All @@ -250,8 +253,21 @@
return tasks.size();
}

public static interface WorkerTask {
void run(long now);
public static class WorkerTask {
final void work(long now) {
try {
run(now);
} catch (Exception e) {
error(e, now);
}
}

protected void run(@SuppressWarnings("unused") long now) { // ok
}

protected void error(Exception e, @SuppressWarnings("unused") long now) { // ok

Check warning on line 268 in unknow-server-nio/src/main/java/unknow/server/nio/NIOWorker.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused method parameter "now".

See more on https://sonarcloud.io/project/issues?id=Unknow0_unknow-server&issues=AZ22CXM7kXxxl2V_kV0P&open=AZ22CXM7kXxxl2V_kV0P&pullRequest=91
logger.warn("Error on task {}", this, e);
}
}

private final void unlink(NIOConnection co) {
Expand Down Expand Up @@ -291,7 +307,7 @@
head = co;
}

private final class RegisterTask implements WorkerTask {
private final class RegisterTask extends WorkerTask {
private final SelectionKey key;
private final ConnectionFactory factory;

Expand All @@ -301,7 +317,7 @@
}

@Override
public void run(long now) {
protected void run(long now) {
NIOConnection co = new NIOConnection(NIOWorker.this, key, factory.build());
key.attach(co);
listener.accepted(name, co);
Expand All @@ -318,6 +334,12 @@
submit(new AsyncInit(co));
toTail(co, now);
}

@Override
protected void error(Exception e, long now) {
logger.warn("Failed to register key {}", key, e);
key.cancel();
}
}

private static final class AsyncInit implements Runnable {
Expand Down
Loading