dol: initial dol commit
[jump.git] / dol / src / dol / visitor / hdsd / scd / scd_command_writer.h
diff --git a/dol/src/dol/visitor/hdsd/scd/scd_command_writer.h b/dol/src/dol/visitor/hdsd/scd/scd_command_writer.h
new file mode 100644 (file)
index 0000000..a8bb427
--- /dev/null
@@ -0,0 +1,61 @@
+#ifndef SCD_COMMAND_WRITER_H
+#define SCD_COMMAND_WRITER_H
+
+#include <list>
+
+#include "scd_command.h"
+#include "scd_socket.h"
+
+
+/**
+ * Queues multiple commands and writes then to a socket continuously.
+ */
+class scd_command_writer
+{
+public:
+    scd_command_writer();
+
+    virtual ~scd_command_writer();
+
+    /**
+     * Sets the socket to write to. Do not try to write before
+     * setting the socket.
+     */
+    void set_socket(scd_socket& sock);
+
+    /**
+     * Indicates if the writer is currently sending a command.
+     * \return true if the writer is busy sending commands
+     */
+    bool is_writing() const;
+
+    /**
+     * Queues a command to be sent. Commands will be sent out in FIFO
+     * manner. Sending is not initiated by this call.
+     * \param message to send
+     * \return false if the message is longet than SCD_CM_MAXLEN. In this
+     * case the message is destroyed.
+     */
+    bool queue_command(scd_command* cmd);
+
+    /**
+     * Write commands or part of commands to the socket. As many queued
+     * commands as possible are sent but none has to be finished.
+     * \exception scd_exception if unexpected errors occured
+     */
+    void write();
+
+private:
+    scd_socket* _socket;
+    bool _is_writing;
+
+    std::list<scd_command*>_commands;
+    char _buf[SCD_CM_HEADER + SCD_CM_MAXLEN];
+    size_t _remaining;
+    size_t _off;
+
+    inline bool _send_cmd();
+
+};
+
+#endif