From 4de67b497e0e229fe4a42f66f833640b6e50fd5a Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Sun, 17 Nov 2024 13:45:44 +0200 Subject: Moved the project to an IDEA project & wrote part of README.txt --- .../Client/ReceivedLineParser.scala | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/scalevalapokalypsi/Client/ReceivedLineParser.scala (limited to 'src/scalevalapokalypsi/Client/ReceivedLineParser.scala') 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 -- cgit v1.2.3