-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit da67fbd Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Tue Nov 5 23:01:24 2024 -0500 - Ping/pong handling fixes. commit ae312e7 Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Tue Nov 5 21:49:22 2024 -0500 - Ping/pong handling fixes. commit fdb4ca8 Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Tue Nov 5 21:18:22 2024 -0500 - Ping/pong handling fixes. commit d49aff4 Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Sun Oct 13 16:22:16 2024 -0400 Squashed commit of the following: - Connection handling fixes. - Minor version bump. commit bad35f6 Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Thu Oct 10 10:43:39 2024 -0400 - Dependency update. commit 85bbd1b Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Thu Oct 10 10:37:39 2024 -0400 - Client connection handler fixes. - Minor version bump. commit 2acdd05 Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Wed Oct 9 01:22:48 2024 -0400 - Server connection handler fixes. - Logging tweaks. - Test case updates. - Minor version bump. commit 44e6ec3 Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Sat Sep 28 16:53:40 2024 -0400 - Logging tweaks. - Patch version bump. commit 0f765dd Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Sat Sep 28 16:17:20 2024 -0400 Squashed commit of the following: - Preliminary WS server implementation. - Minor version bump. commit 7f26b8d Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Tue Sep 24 01:15:22 2024 -0400 - Logging tweaks. - Patch version bump. commit cfe5ae6 Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Mon Sep 16 04:03:05 2024 -0400 - Connection error handling fixes. - Minor version bump. commit f5b9498 Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Sun Sep 15 17:21:56 2024 -0400 - Host header fix. - Minor version bump. commit 0c448b8 Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Sat Sep 14 13:45:43 2024 -0400 - Logging tweaks. - Minor version bump. commit 889a4e9 Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Sat Sep 14 03:36:03 2024 -0400 - Missing close code handler notification. - Logging tweaks. - Minor version bump. commit cde16da Author: Jesus Zazueta <jjzazuet@gmail.com> Date: Thu Sep 12 19:34:27 2024 -0400 - Socket connection error notifications.
- Loading branch information
Showing
28 changed files
with
882 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package io.vacco.tokoeka; | ||
|
||
import io.vacco.tokoeka.spi.*; | ||
import io.vacco.tokoeka.util.*; | ||
import org.slf4j.*; | ||
import java.io.*; | ||
import java.net.*; | ||
import java.util.concurrent.*; | ||
import java.util.function.*; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static io.vacco.tokoeka.util.TkSockets.*; | ||
|
||
public class TkSocketServer implements Closeable { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(TkSocketServer.class); | ||
|
||
private final int port; | ||
private final TkSocketHdl socketHdl; | ||
private final Supplier<TkSocketState> stateFn; | ||
private final ExecutorService clientPool; | ||
|
||
private ServerSocket serverSocket; | ||
|
||
public TkSocketServer(int port, TkSocketHdl socketHdl, Supplier<TkSocketState> stateFn, ExecutorService clientPool) { | ||
this.port = port; | ||
this.stateFn = requireNonNull(stateFn); | ||
this.socketHdl = requireNonNull(socketHdl); | ||
this.clientPool = requireNonNull(clientPool); | ||
} | ||
|
||
public TkSocketServer(int port, TkSocketHdl socketHdl, Supplier<TkSocketState> stateFn) { | ||
this(port, socketHdl, stateFn, Executors.newCachedThreadPool()); | ||
} | ||
|
||
public void start() { | ||
try { | ||
serverSocket = new ServerSocket(port); | ||
log.info("WebSocket server started on port {}", port); | ||
while (!serverSocket.isClosed()) { | ||
var clientSocket = serverSocket.accept(); | ||
clientPool.submit(() -> handleClient(clientSocket)); | ||
} | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Unable to start websocket server", e); | ||
} | ||
} | ||
|
||
private void handleClient(Socket clientSocket) { | ||
log.debug("Client connection: {}", clientSocket); | ||
TkConn conn = null; | ||
try { | ||
var inputStream = clientSocket.getInputStream(); | ||
var outputStream = clientSocket.getOutputStream(); | ||
var socketState = this.stateFn.get(); | ||
var handShake = wsServerHandShakeOf(inputStream); | ||
var handshakeResponse = performHandshake(handShake, outputStream); | ||
conn = new TkConnAdapter(clientSocket, socketState, msg -> send(msg, outputStream)); | ||
this.socketHdl.onOpen(conn, handshakeResponse); | ||
while (!clientSocket.isClosed()) { | ||
var stop = handleMessage(this.socketHdl, conn, inputStream, outputStream); | ||
if (stop) { | ||
break; | ||
} | ||
} | ||
} catch (Exception e) { | ||
if (log.isDebugEnabled()) { | ||
log.debug("Client connection error - {}", clientSocket.getRemoteSocketAddress(), e); | ||
} | ||
} finally { | ||
tearDown(clientSocket, conn, socketHdl); | ||
} | ||
} | ||
|
||
@Override public void close() { | ||
clientPool.shutdown(); | ||
if (serverSocket != null) { | ||
doClose(serverSocket); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,4 +70,5 @@ public void close() { | |
line.close(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.