diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-11-14 19:25:19 +0200 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-11-14 19:25:19 +0200 |
commit | a43812ed462630850edbf29bda182fbf1e5e1263 (patch) | |
tree | 20e84b60adee6fa31ceb970ffccffa5b6b583d86 /src/main/scala/Client/ReceivedLineParser.scala | |
parent | c87263e9e493fe6c130f5ad6a523871c08987f4c (diff) | |
download | scalevalapokalypsi-a43812ed462630850edbf29bda182fbf1e5e1263.tar.gz scalevalapokalypsi-a43812ed462630850edbf29bda182fbf1e5e1263.zip |
Immediate printing of actions & no prompt on blocking action & refactoring
Diffstat (limited to 'src/main/scala/Client/ReceivedLineParser.scala')
-rw-r--r-- | src/main/scala/Client/ReceivedLineParser.scala | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/main/scala/Client/ReceivedLineParser.scala b/src/main/scala/Client/ReceivedLineParser.scala new file mode 100644 index 0000000..7cbf935 --- /dev/null +++ b/src/main/scala/Client/ReceivedLineParser.scala @@ -0,0 +1,27 @@ +package o1game.Client + +import scala.collection.mutable.Buffer +import o1game.constants.* + +/** A class for checking asynchronously for received lines */ +class ReceivedLineParser: + + private var serverLineState = ServerLineState.ActionDescription + + private var bufferedData: Buffer[Byte] = Buffer.empty // TODO: suboptimal DS + + /** Add received data */ + def in(data: Array[Byte]): Unit = + this.bufferedData ++= data + + /** Read a line from the received data */ + def nextLine(): Option[String] = + val indexOfCRLF = this.bufferedData.indexOfSlice(CRLF) + if indexOfCRLF == -1 then + None + else + val splitData = this.bufferedData.splitAt(indexOfCRLF) + this.bufferedData = Buffer.from(splitData(1).drop(CRLF.length)) + Some(String(splitData(0).toArray)) + +end ReceivedLineParser |