dol: initial dol commit
[jump.git] / dol / src / dol / visitor / hdsd / scd / scd_command_writer.h
1 #ifndef SCD_COMMAND_WRITER_H
2 #define SCD_COMMAND_WRITER_H
3
4 #include <list>
5
6 #include "scd_command.h"
7 #include "scd_socket.h"
8
9
10 /**
11  * Queues multiple commands and writes then to a socket continuously.
12  */
13 class scd_command_writer
14 {
15 public:
16     scd_command_writer();
17
18     virtual ~scd_command_writer();
19
20     /**
21      * Sets the socket to write to. Do not try to write before
22      * setting the socket.
23      */
24     void set_socket(scd_socket& sock);
25
26     /**
27      * Indicates if the writer is currently sending a command.
28      * \return true if the writer is busy sending commands
29      */
30     bool is_writing() const;
31
32     /**
33      * Queues a command to be sent. Commands will be sent out in FIFO
34      * manner. Sending is not initiated by this call.
35      * \param message to send
36      * \return false if the message is longet than SCD_CM_MAXLEN. In this
37      * case the message is destroyed.
38      */
39     bool queue_command(scd_command* cmd);
40
41     /**
42      * Write commands or part of commands to the socket. As many queued
43      * commands as possible are sent but none has to be finished.
44      * \exception scd_exception if unexpected errors occured
45      */
46     void write();
47
48 private:
49     scd_socket* _socket;
50     bool _is_writing;
51
52     std::list<scd_command*>_commands;
53     char _buf[SCD_CM_HEADER + SCD_CM_MAXLEN];
54     size_t _remaining;
55     size_t _off;
56
57     inline bool _send_cmd();
58
59 };
60
61 #endif