use of org.nustaq.kontraktor.remoting.base.ObjectSocket in project kontraktor by RuedigerMoeller.
the class TCPServerConnector method acceptLoop.
protected Promise acceptLoop(Actor facade, int port, Function<ObjectSocket, ObjectSink> factory, Promise p) {
try {
numberOfThreads.incrementAndGet();
acceptSocket = new ServerSocket(port);
p.resolve();
while (!acceptSocket.isClosed()) {
Socket clientSocket = acceptSocket.accept();
clientSockets.add(clientSocket);
MyTCPSocket objectSocket = new MyTCPSocket(clientSocket);
facade.execute(() -> {
ObjectSink sink = factory.apply(objectSocket);
new Thread(() -> {
try {
numberOfThreads.incrementAndGet();
while (!clientSocket.isClosed()) {
try {
Object o = objectSocket.readObject();
sink.receiveObject(o, null, null);
} catch (Exception e) {
if (e instanceof EOFException == false && e instanceof SocketException == false)
Log.Warn(this, e);
try {
clientSocket.close();
} catch (IOException e1) {
Log.Warn(this, e1);
}
}
}
sink.sinkClosed();
} finally {
clientSockets.remove(clientSocket);
numberOfThreads.decrementAndGet();
}
}, "tcp receiver").start();
});
}
} catch (Exception e) {
Log.Info(this, e.getMessage());
if (!p.isSettled())
p.reject(e);
} finally {
if (!p.isSettled())
p.reject("conneciton failed");
try {
acceptSocket.close();
} catch (IOException e) {
Log.Warn(this, e);
}
numberOfThreads.decrementAndGet();
}
return p;
}
Aggregations