Create repository
[fwd.git] / prod / qdsocket.h
1 /*
2  * QuanDocs socket API.
3  * (Convenience wrapper around BSD sockets)
4  *
5  * Copyright (C) 2004, 2015 by Chris Jaekl
6  */
7
8 #ifndef __QD_SOCKET_H__
9 #define __QD_SOCKET_H__
10
11 #include <assert.h>
12 #include <sys/socket.h>
13 #include <sys/types.h>
14
15 #define QD_SOCK_BUF_SIZE        32768
16
17 struct qdSocket {
18     int  sd;                    // socket (file) descriptor
19     int  toWrite;               // number of bytes in buf (starting from [0]) that are waiting to be written
20     char closeRequested;        // boolean:  has qdClose() been called on this socket?
21     char buf[QD_SOCK_BUF_SIZE];
22 };
23
24 void qdSockCheckClose(struct qdSocket *sock);
25
26 void qdSockClose(struct qdSocket *sock);
27
28 // Returns 0 on success, non-zero on failure
29 int qdSockConnect(struct qdSocket *sock, const char * host, int port);
30
31 void qdSockInit(struct qdSocket *sock);
32
33 // Returns # of bytes actually written by this flush operation
34 ssize_t qdSockFlush(struct qdSocket *to);
35
36 ssize_t qdSockWrite(struct qdSocket *to, char * buf, ssize_t n);
37
38 #endif /* __QD_SOCKET_H__ */
39