]> git.lizzy.rs Git - rudp.git/blob - rudp.h
Switch to CMake
[rudp.git] / rudp.h
1 #ifndef __rudp_h__
2 #define __rudp_h__
3
4 #ifdef _WIN32
5 #include <winsock2.h>
6 #else
7 #include <netinet/in.h>
8 #endif
9 #include "platform_adpt.h"
10
11 typedef void* RUDPSOCKET;
12 #define INVALID_RUDPSOCKET      NULL
13 /*
14 typedef struct _tagIOVEC {
15         void *iov_base;
16         unsigned int iov_len;
17 } IOVEC;
18 */
19 /*
20  * Errors
21  */
22 #define ERUDP_FIRST         -10000
23 #define ERUDP_NOT_SOCKET    (ERUDP_FIRST-1)
24 #define ERUDP_NOT_ALLOWED   (ERUDP_FIRST-2)
25 #define ERUDP_CONN_FAILED   (ERUDP_FIRST-3)
26 #define ERUDP_CONNECTED     (ERUDP_FIRST-4)
27 #define ERUDP_IN_PROGRESS   (ERUDP_FIRST-5)
28 #define ERUDP_NO_CONN       (ERUDP_FIRST-6)
29 #define ERUDP_BIND          (ERUDP_FIRST-7)
30 #define ERUDP_RESETED       (ERUDP_FIRST-8)
31 #define ERUDP_TIMEOUTED     (ERUDP_FIRST-9)
32 #define ERUDP_INVALID       (ERUDP_FIRST-10)
33 #define ERUDP_PEER_CLOSED   (ERUDP_FIRST-11) //normal close
34 #define ERUDP_AGAIN         (ERUDP_FIRST-12)
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 typedef void (*NONRUDPPACKETCB)(const uint8_t *buff, int len, void *p_user);
41
42 int RUDPStart();
43 int RUDPCleanup();
44
45 RUDPSOCKET RUDPSocket();
46 RUDPSOCKET RUDPSocketFromUdp(int udpsock);
47 int RUDPSetInvalidPacketCB(RUDPSOCKET, NONRUDPPACKETCB pkt_cb, void *p_user);
48 int RUDPClose(RUDPSOCKET sock);
49
50 int RUDPListen(RUDPSOCKET sock, int n);
51 int RUDPAccept(RUDPSOCKET sock, /*OUT*/RUDPSOCKET *accepted, struct sockaddr *addr, int *addrlen);
52 int RUDPBind(RUDPSOCKET sock, const struct sockaddr *addr, int addrlen);
53 int RUDPConnect(RUDPSOCKET sock, const struct sockaddr* addr, int addr_len);
54
55 // Set rudp socket to connected state(RS_ESTABLSHED) with default setting(receiver's buffer, ...) 
56 // peer_rbuf_sz: size of peer's rbuff
57 int RUDPConnected(RUDPSOCKET sock, const struct sockaddr* addr, int peer_rbuf_sz);
58
59 #define RUDPMSG_DONTWAIT        0x0001
60 //flags: 0 or RUDPMSG_xxx
61 int RUDPRecv(RUDPSOCKET sock, /*OUT*/int *chno, void *ptr, int len, int flags);
62 int RUDPRecvChn(RUDPSOCKET sock, int *chno, void *ptr, int len, int flags);
63 int RUDPSend(RUDPSOCKET sock, int chno, const void *ptr, int len, int flags);
64 int RUDPSendEx(RUDPSOCKET sock, int chno, int priority, const void *ptr, int len, int flags);
65 int RUDPSendV(RUDPSOCKET sock, int chno, const PA_IOVEC *v, unsigned int size, int flags);
66 int RUDPSendVEx(RUDPSOCKET sock, int chno, int priority, const PA_IOVEC *v, unsigned int size, int flags);
67
68
69 #define OPT_UDP_SNDBUF    1
70 #define OPT_UDP_RCVBUF    2
71 #define OPT_RUDP_SNDBUF   3 //called after connection
72 #define OPT_RUDP_RCVBUF   4 //called before connection
73 #define OPT_LINGER        5
74 #define OPT_FC            6
75 #define OPT_MSS           7
76 #define OPT_SNDTIMEO      11
77 #define OPT_RCVTIMEO      12
78 #define OPT_REUSEADDR     13 //default: 1
79 #define OPT_ADHOC         14 //simultaneously connect ?
80 #define OPT_NBLK          15
81 #define OPT_ERR           16 //get error
82 int RUDPGetSockOpt(RUDPSOCKET sock, int opt, void *optval, int *optlen);
83 int RUDPSetSockOpt(RUDPSOCKET sock, int opt, const void *optval, int optlen);
84
85 #define RUDPSELECT_READABLE     0x01
86 #define RUDPSELECT_WRITABLE     0x02
87 //#define RUDPSELECT_ERROR      0x04
88 int RUDPSelectSock(RUDPSOCKET sock, int chno, int flag/*one of RUDPSELECT_xxx*/, const struct timeval *timeout);
89 //return: >0 - condition is yes
90 //        =0 - negative
91 //        <0 - error
92
93 typedef
94 struct _tagSelectChn {
95         RUDPSOCKET sock;        //NULL for system socket
96         int     chno;           //system socket, or chno for rudp socket
97 } RUDPSOCKCHNO;
98 //All parameters except timeout are for INOUT
99 int RUDPSelect(RUDPSOCKCHNO *r_rcs, int *n_rrc, RUDPSOCKCHNO *w_rcs, int *n_wrc, RUDPSOCKCHNO *e_rcs, int *n_erc, const struct timeval *timeout);
100
101 #define RUDP_FD_SET(fd, prc, size) \
102         do { prc[size].sock = NULL; prc[size++].chno = fd; }while(0)
103 #define RUDP_SET(s, chn, prc, size) \
104         do { prc[size].sock = s; prc[size++].chno = chn; }while(0)
105 #define RUDP_CLR(s, chn, prc, size) \
106         do { int i; for(i=0; i<size; i++) \
107                 if(prc[i].sock == s && prc[i].chno == chn) break; \
108                 if(i < size) { while(i<size-1) prc[i] = prc[i+1]; size--; } \
109         } while(0)
110
111 int RUDP_FD_ISSET(int fd, const RUDPSOCKCHNO *prc, int size);
112 int RUDP_ISSET(RUDPSOCKET s, const RUDPSOCKCHNO *prc, int size);
113
114 int RUDPGetSockName(RUDPSOCKET sock, struct sockaddr *name);
115 int RUDPGetPeerName(RUDPSOCKET sock, struct sockaddr *name);
116
117 #ifdef __cplusplus
118 }
119 #endif
120
121 #endif
122