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/Server.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/Server.scala')
-rw-r--r-- | src/main/scala/Server/Server.scala | 49 |
1 files changed, 11 insertions, 38 deletions
diff --git a/src/main/scala/Server/Server.scala b/src/main/scala/Server/Server.scala index 829010c..1f0cbfc 100644 --- a/src/main/scala/Server/Server.scala +++ b/src/main/scala/Server/Server.scala @@ -6,9 +6,6 @@ package o1game.Server import java.io.IOException import java.lang.Thread.sleep import java.net.{ServerSocket, Socket} -import scala.concurrent.ExecutionContext.Implicits.global -import scala.concurrent.Future -import scala.util.{Failure, Success, Try} import o1game.constants.* /** `Server` exists to initialize a server for the game @@ -47,44 +44,20 @@ class Server(port: Int, maxClients: Int): * @param message the message to send */ private def writeToAll(message: String): Unit = - clients.removeNonSatisfying((c: Client) => - try - val output = c.socket.getOutputStream - output.write(message.toVector.map(_.toByte).toArray) - output.flush() - true - catch - case e: IOException => false + clients.mapAndRemove(c => + val output = c.socket.getOutputStream + output.write(message.toVector.map(_.toByte).toArray) + output.flush() ) /** Reads data sent by clients and stores it in the `Client`s of `clients` */ private def readFromAll(): Unit = - clients.removeNonSatisfying((c: Client) => - try - val input = c.socket.getInputStream - while input.available() != 0 do - val bytesRead = input.read(buffer) - if bytesRead != -1 then - c.receiveData(buffer.take(bytesRead).toVector) - true - catch - case e: IOException => false + clients.mapAndRemove(c => + val input = c.socket.getInputStream + while input.available() != 0 do + val bytesRead = input.read(buffer) + if bytesRead != -1 then + c.receiveData(buffer.take(bytesRead).toVector) ) -end Server - -class ConnectionGetter(val socket: ServerSocket): - - private var nextClient: Future[Socket] = Future.failed(IOException()) - - 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 +end Server
\ No newline at end of file |