~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/mysql_protocol/vio.h

  • Committer: Olaf van der Spek
  • Date: 2011-02-12 18:24:24 UTC
  • mto: (2167.1.2 build) (2172.1.4 build)
  • mto: This revision was merged to the branch mainline in revision 2168.
  • Revision ID: olafvdspek@gmail.com-20110212182424-kgnm9osi7qo97at2
casts

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
 
/*
17
 
 * Virtual I/O layer, only used with TCP/IP sockets at the moment.
18
 
 */
19
16
 
20
17
#ifndef PLUGIN_MYSQL_PROTOCOL_VIO_H
21
18
#define PLUGIN_MYSQL_PROTOCOL_VIO_H
22
19
 
23
20
#include <sys/socket.h>
24
 
#include <errno.h>
25
 
 
26
 
typedef struct st_vio Vio;
27
 
 
28
 
struct st_vio
29
 
{
 
21
#include <cerrno>
 
22
 
 
23
namespace drizzle_plugin
 
24
{
 
25
 
 
26
/**
 
27
 *@brief Virtual I/O layer, only used with TCP/IP sockets at the moment.
 
28
 */
 
29
class Vio
 
30
{
 
31
public:
 
32
  /**
 
33
   * Constructor. 
 
34
   * @param[in] sd Descriptor to use.
 
35
   */
 
36
  Vio(int sd);
 
37
  ~Vio();
 
38
 
 
39
  /**
 
40
   *Close the connection.
 
41
   *@returns 0 on success.
 
42
   */
 
43
  int close();
 
44
 
 
45
  /**
 
46
   * Read some data from the remote end.
 
47
   *@param[out] buf A buffer to write the new data to.
 
48
   *@param[in] size The size of the buffer
 
49
   *@returns The number of bytes read.
 
50
   */
 
51
  size_t read(unsigned char* buf, size_t size);
 
52
  
 
53
  /**
 
54
   * Write some data to the remote end.
 
55
   *@param[in] buf A buffer that contains the data to send.
 
56
   *@param[in] size The size of the buffer
 
57
   *@returns The number of bytes written.
 
58
   */
 
59
  size_t write(const unsigned char* buf, size_t size);
 
60
 
 
61
  /**
 
62
   * Set device blocking mode.
 
63
   *@param[in] set_blocking_mode Whether the device should block. true sets blocking mode, false clears it.
 
64
   *@param[out] old_mode This will be set to the previous blocking mode.
 
65
   *@returns 0 on success.
 
66
   */
 
67
  int blocking(bool set_blocking_mode, bool *old_mode);
 
68
 
 
69
  /**
 
70
   * Enables fast sending.
 
71
   * Setting this sets the TCP_NODELAY socket option.
 
72
   *@returns 0 on succcess.
 
73
   */
 
74
  int fastsend();
 
75
 
 
76
  /**
 
77
   * Sets or clears the keepalive option.
 
78
   *@param[in] set_keep_alive Whether to set or clear the flag. True Sets keepalive, false clears it.
 
79
   *@returns 0 on success.
 
80
   */
 
81
  int32_t keepalive(bool set_keep_alive);
 
82
 
 
83
  /**
 
84
   *@returns true if the caller should retry the last operation.
 
85
   */
 
86
  bool should_retry() const;
 
87
 
 
88
  /**
 
89
   *@returns true if the last operation was interrupted.
 
90
   */
 
91
  bool was_interrupted() const;
 
92
 
 
93
  /**
 
94
   *Gets the address details of the peer.
 
95
   @param[out] buf Buffer that will recieve the peer address.
 
96
   @param[out] port Port of remote end.
 
97
   @param[in] buflen Size of buf.
 
98
   @returns True on success, false otherwise.
 
99
   */
 
100
  bool peer_addr(char *buf, uint16_t *port, size_t buflen) const;
 
101
 
 
102
  /**
 
103
   * Sets either the send, or recieve timeouts for the socket.
 
104
   *@param[in] is_sndtimeo Set to true to change the send timeout, false to change the recieve timeout.
 
105
   *@param[in] timeout The new timeout to set, in seconds.
 
106
   */
 
107
  void timeout(bool is_sndtimeo, int32_t timeout);
 
108
 
 
109
  /**
 
110
   * Returns the last error code.
 
111
   *@returns the last error code, as described in errno.h
 
112
   */
 
113
  int get_errno() const;
 
114
 
 
115
  /**
 
116
   * Get the underlying descriptor this class is using.
 
117
   *@returns The descriptor passed in to the constructor of this class.
 
118
   */
 
119
  int get_fd() const;
 
120
 
 
121
  /**
 
122
   * Returns the current read position.
 
123
   */
 
124
  char *get_read_pos() const;
 
125
 
 
126
  /**
 
127
   * Returns the current write position.
 
128
   */
 
129
  char *get_read_end() const;
 
130
 
 
131
private:
30
132
  bool closed;
31
133
  int sd;
32
134
  int fcntl_mode; /* Buffered fcntl(sd,F_GETFL) */
33
135
  struct sockaddr_storage local; /* Local internet address */
34
136
  struct sockaddr_storage remote; /* Remote internet address */
35
 
  int addrLen; /* Length of remote address */
36
137
  char *read_pos; /* start of unfetched data in the read buffer */
37
138
  char *read_end; /* end of unfetched data */
38
139
 
39
 
  /* function pointers. They are similar for socket/SSL/whatever */
40
 
  void (*viodelete)(Vio*);
41
 
  int32_t (*vioerrno)(Vio*);
42
 
  size_t (*read)(Vio*, unsigned char *, size_t);
43
 
  size_t (*write)(Vio*, const unsigned char *, size_t);
44
 
  int32_t (*vioblocking)(Vio*, bool, bool *);
45
 
  int32_t (*viokeepalive)(Vio*, bool);
46
 
  int32_t (*fastsend)(Vio*);
47
 
  bool (*peer_addr)(Vio*, char *, uint16_t *, size_t);
48
 
  void (*in_addr)(Vio*, struct sockaddr_storage*);
49
 
  bool (*should_retry)(Vio*);
50
 
  bool (*was_interrupted)(Vio*);
51
 
  int32_t (*vioclose)(Vio*);
52
 
  void (*timeout)(Vio*, bool is_sndtimeo, int32_t timeout);
53
140
};
54
141
 
55
 
Vio* mysql_protocol_vio_new(int sd);
56
 
 
57
 
#define vio_fd(vio) (vio)->sd
58
 
#define vio_delete(vio) (vio)->viodelete(vio)
59
 
#define vio_errno(vio) (vio)->vioerrno(vio)
60
 
#define vio_read(vio, buf, size) ((vio)->read)(vio,buf,size)
61
 
#define vio_write(vio, buf, size) ((vio)->write)(vio, buf, size)
62
 
#define vio_blocking(vio, set_blocking_mode, old_mode) (vio)->vioblocking(vio, set_blocking_mode, old_mode)
63
 
#define vio_fastsend(vio) (vio)->fastsend(vio)
64
 
#define vio_keepalive(vio, set_keep_alive) (vio)->viokeepalive(vio, set_keep_alive)
65
 
#define vio_should_retry(vio) (vio)->should_retry(vio)
66
 
#define vio_was_interrupted(vio) (vio)->was_interrupted(vio)
67
 
#define vio_close(vio) ((vio)->vioclose)(vio)
68
 
#define vio_peer_addr(vio, buf, prt, buflen) (vio)->peer_addr(vio, buf, prt, buflen)
69
 
#define vio_timeout(vio, which, seconds) (vio)->timeout(vio, which, seconds)
 
142
} /* namespace drizzle_plugin */
70
143
 
71
144
#endif /* PLUGIN_MYSQL_PROTOCOL_VIO_H */