diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-11-05 00:18:36 +0200 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-11-05 00:18:36 +0200 |
commit | 12cbf4d451d1002b8872b0028acbe6bd886ca9bd (patch) | |
tree | c8d8ed236cc7ae907f52209cb0dd1f7d40ebf346 /src/main/scala/Server/ConnectionGetter.scala | |
parent | ae82027a9bd4e75582f9499d4006b18c29a4129c (diff) | |
download | scalevalapokalypsi-12cbf4d451d1002b8872b0028acbe6bd886ca9bd.tar.gz scalevalapokalypsi-12cbf4d451d1002b8872b0028acbe6bd886ca9bd.zip |
Small refactoring
* Moved `ConnectionGetter` to its own file
* Added method `mapAndRemove` to Server/Client.scala
* Replaced usages of `removeNonSatisfying` with the above
* Added some documentation
Diffstat (limited to 'src/main/scala/Server/ConnectionGetter.scala')
-rw-r--r-- | src/main/scala/Server/ConnectionGetter.scala | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/main/scala/Server/ConnectionGetter.scala b/src/main/scala/Server/ConnectionGetter.scala new file mode 100644 index 0000000..b3246a7 --- /dev/null +++ b/src/main/scala/Server/ConnectionGetter.scala @@ -0,0 +1,25 @@ +package o1game.Server + +import java.io.IOException +import java.net.{ServerSocket, Socket} +import scala.concurrent.Future +import scala.util.{Failure, Success} +import scala.concurrent.ExecutionContext.Implicits.global + +/** Small helper class for getting new connections using futures */ +class ConnectionGetter(val socket: ServerSocket): + + private var nextClient: Future[Socket] = Future.failed(IOException()) + + /** Returns a new socket to a client if there is any new connections. */ + def newClient(): Option[Socket] = + this.nextClient.value match + case Some(Success(s)) => + nextClient = Future(socket.accept()) + Some(s) + case Some(Failure(e)) => + nextClient = Future(socket.accept()) + None + case None => None + +end ConnectionGetter |