dol: initial dol commit
[jump.git] / dol / src / dol / visitor / hdsd / scd / scd_command_reader.h
1 #ifndef SCD_COMMAND_READER_H
2 #define SCD_COMMAND_READER_H
3
4 #include "scd_command.h"
5 #include "scd_socket.h"
6
7 /**
8  * Reads commands from a socket. The receiption does not have to complete
9  * withing one call. As long as it is ongoing the socket shall not be read
10  * by the application.
11  */
12 class scd_command_reader
13 {
14 public:
15     scd_command_reader();
16
17     virtual ~scd_command_reader();
18
19     /**
20      * Sets the socket to read from. Do not try to read before setting the
21      * socket.
22      */
23     void set_socket(scd_socket &sock);
24
25     /**
26      * Initiates reading a command if it is not reading yet or continues
27      * to read a previously unfinished command.
28      */
29     void read();
30
31     /**
32      * Indicates if a command is being read. This is the case between calling
33      * read() and the completion of the command (or failure if an illegal
34      * command was received).
35      * \return true if a command is currently being received
36      */
37     bool is_reading();
38
39     /**
40      * Indicates if a command has been received successfully and is ready
41      * to be picked up.
42      * \return true if a command can be collected
43      */
44     bool has_command();
45
46     /**
47      * Returns a successfully received command. The application has to free
48      * it by itself. The reader does not keep a reference.
49      * \return the command or NULL if has_command() is false
50      */
51     scd_command* get_command();
52
53 private:
54     scd_socket* _socket;
55     scd_command* _command;
56     bool _is_reading;
57     bool _header_read;
58     bool _success;
59     size_t _remaining;
60     size_t _off;
61     char _header_buf[SCD_CM_HEADER];
62 };
63
64 #endif