dol: initial dol commit
[jump.git] / dol / src / dol / visitor / hdsd / scd / scd_command_reader.h
diff --git a/dol/src/dol/visitor/hdsd/scd/scd_command_reader.h b/dol/src/dol/visitor/hdsd/scd/scd_command_reader.h
new file mode 100644 (file)
index 0000000..db06830
--- /dev/null
@@ -0,0 +1,64 @@
+#ifndef SCD_COMMAND_READER_H
+#define SCD_COMMAND_READER_H
+
+#include "scd_command.h"
+#include "scd_socket.h"
+
+/**
+ * Reads commands from a socket. The receiption does not have to complete
+ * withing one call. As long as it is ongoing the socket shall not be read
+ * by the application.
+ */
+class scd_command_reader
+{
+public:
+    scd_command_reader();
+
+    virtual ~scd_command_reader();
+
+    /**
+     * Sets the socket to read from. Do not try to read before setting the
+     * socket.
+     */
+    void set_socket(scd_socket &sock);
+
+    /**
+     * Initiates reading a command if it is not reading yet or continues
+     * to read a previously unfinished command.
+     */
+    void read();
+
+    /**
+     * Indicates if a command is being read. This is the case between calling
+     * read() and the completion of the command (or failure if an illegal
+     * command was received).
+     * \return true if a command is currently being received
+     */
+    bool is_reading();
+
+    /**
+     * Indicates if a command has been received successfully and is ready
+     * to be picked up.
+     * \return true if a command can be collected
+     */
+    bool has_command();
+
+    /**
+     * Returns a successfully received command. The application has to free
+     * it by itself. The reader does not keep a reference.
+     * \return the command or NULL if has_command() is false
+     */
+    scd_command* get_command();
+
+private:
+    scd_socket* _socket;
+    scd_command* _command;
+    bool _is_reading;
+    bool _header_read;
+    bool _success;
+    size_t _remaining;
+    size_t _off;
+    char _header_buf[SCD_CM_HEADER];
+};
+
+#endif