aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/Client/ReceivedLineParser.scala
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-17 13:45:44 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-17 13:45:44 +0200
commit4de67b497e0e229fe4a42f66f833640b6e50fd5a (patch)
tree34fb5b0e776f7cd3adcb4556f4d6a7c8ad66de39 /src/scalevalapokalypsi/Client/ReceivedLineParser.scala
parent8595e892abc0e0554f589ed2eb88c351a347fbd4 (diff)
downloadscalevalapokalypsi-4de67b497e0e229fe4a42f66f833640b6e50fd5a.tar.gz
scalevalapokalypsi-4de67b497e0e229fe4a42f66f833640b6e50fd5a.zip
Moved the project to an IDEA project & wrote part of README.txt
Diffstat (limited to 'src/scalevalapokalypsi/Client/ReceivedLineParser.scala')
-rw-r--r--src/scalevalapokalypsi/Client/ReceivedLineParser.scala27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/scalevalapokalypsi/Client/ReceivedLineParser.scala b/src/scalevalapokalypsi/Client/ReceivedLineParser.scala
new file mode 100644
index 0000000..dfcc2d2
--- /dev/null
+++ b/src/scalevalapokalypsi/Client/ReceivedLineParser.scala
@@ -0,0 +1,27 @@
+package scalevalapokalypsi.Client
+
+import scala.collection.mutable.Buffer
+import scalevalapokalypsi.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