source: trunk/third-party/libutp/utp.h @ 13317

Last change on this file since 13317 was 13317, checked in by livings124, 9 years ago

update libutp, fixing #4915

File size: 5.8 KB
Line 
1#ifndef __UTP_H__
2#define __UTP_H__
3
4#include "utypes.h"
5
6#ifdef WIN32
7#define _CRT_SECURE_NO_DEPRECATE
8#define WIN32_LEAN_AND_MEAN
9#include <windows.h>
10#include <winsock2.h>
11#include <ws2tcpip.h>
12#pragma comment(lib,"ws2_32.lib")
13#else
14#include <stdlib.h>
15#include <sys/types.h>
16#include <sys/socket.h>
17#include <netinet/in.h>
18#include <arpa/inet.h>
19#endif
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25struct UTPSocket;
26
27// Used to set sockopt on a uTP socket to set the version of uTP
28// to use for outgoing connections. This can only be called before
29// the uTP socket is connected
30#define SO_UTPVERSION 99
31
32enum {
33        // socket has reveived syn-ack (notification only for outgoing connection completion)
34        // this implies writability
35        UTP_STATE_CONNECT = 1,
36
37        // socket is able to send more data
38        UTP_STATE_WRITABLE = 2,
39
40        // connection closed
41        UTP_STATE_EOF = 3,
42
43        // socket is being destroyed, meaning all data has been sent if possible.
44        // it is not valid to refer to the socket after this state change occurs
45        UTP_STATE_DESTROYING = 4,
46};
47
48// Callbacks called by a uTP socket (register with UTP_SetCallbacks)
49
50// The uTP socket layer calls this when bytes have been received from the network.
51typedef void UTPOnReadProc(void *userdata, const byte *bytes, size_t count);
52
53// The uTP socket layer calls this to fill the outgoing buffer with bytes.
54// The uTP layer takes responsibility that those bytes will be delivered.
55typedef void UTPOnWriteProc(void *userdata, byte *bytes, size_t count);
56
57// The uTP socket layer calls this to retrieve number of bytes currently in read buffer
58typedef size_t UTPGetRBSize(void *userdata);
59
60// The uTP socket layer calls this whenever the socket becomes writable.
61typedef void UTPOnStateChangeProc(void *userdata, int state);
62
63// The uTP socket layer calls this when an error occurs on the socket.
64// These errors currently include ECONNREFUSED, ECONNRESET and ETIMEDOUT, but
65// could eventually include any BSD socket error.
66typedef void UTPOnErrorProc(void *userdata, int errcode);
67
68// The uTP socket layer calls this to report overhead statistics
69typedef void UTPOnOverheadProc(void *userdata, bool send, size_t count, int type);
70
71struct UTPFunctionTable {
72        UTPOnReadProc *on_read;
73        UTPOnWriteProc *on_write;
74        UTPGetRBSize *get_rb_size;
75        UTPOnStateChangeProc *on_state;
76        UTPOnErrorProc *on_error;
77        UTPOnOverheadProc *on_overhead;
78};
79
80
81// The uTP socket layer calls this when a new incoming uTP connection is established
82// this implies writability
83typedef void UTPGotIncomingConnection(void *userdata, struct UTPSocket* s);
84
85// The uTP socket layer calls this to send UDP packets
86typedef void SendToProc(void *userdata, const byte *p, size_t len, const struct sockaddr *to, socklen_t tolen);
87
88
89// Functions which can be called with a uTP socket
90
91// Create a uTP socket
92struct UTPSocket *UTP_Create(SendToProc *send_to_proc, void *send_to_userdata,
93                                          const struct sockaddr *addr, socklen_t addrlen);
94
95// Setup the callbacks - must be done before connect or on incoming connection
96void UTP_SetCallbacks(struct UTPSocket *socket, struct UTPFunctionTable *func, void *userdata);
97
98// Valid options include SO_SNDBUF, SO_RCVBUF and SO_UTPVERSION
99bool UTP_SetSockopt(struct UTPSocket *socket, int opt, int val);
100
101// Try to connect to a specified host.
102void UTP_Connect(struct UTPSocket *socket);
103
104// Process a UDP packet from the network. This will process a packet for an existing connection,
105// or create a new connection and call incoming_proc. Returns true if the packet was processed
106// in some way, false if the packet did not appear to be uTP.
107bool UTP_IsIncomingUTP(UTPGotIncomingConnection *incoming_proc,
108                                           SendToProc *send_to_proc, void *send_to_userdata,
109                                           const byte *buffer, size_t len, const struct sockaddr *to, socklen_t tolen);
110
111// Process an ICMP received UDP packet.
112bool UTP_HandleICMP(const byte* buffer, size_t len, const struct sockaddr *to, socklen_t tolen);
113
114// Write bytes to the uTP socket.
115// Returns true if the socket is still writable.
116bool UTP_Write(struct UTPSocket *socket, size_t count);
117
118// Notify the uTP socket of buffer drain
119void UTP_RBDrained(struct UTPSocket *socket);
120
121// Call periodically to process timeouts and other periodic events
122void UTP_CheckTimeouts(void);
123
124// Retrieves the peer address of the specified socket, stores this address in the
125// sockaddr structure pointed to by the addr argument, and stores the length of this
126// address in the object pointed to by the addrlen argument.
127void UTP_GetPeerName(struct UTPSocket *socket, struct sockaddr *addr, socklen_t *addrlen);
128
129void UTP_GetDelays(struct UTPSocket *socket, int32 *ours, int32 *theirs, uint32 *age);
130
131size_t UTP_GetPacketSize(struct UTPSocket *socket);
132
133#ifdef _DEBUG
134struct UTPStats {
135        uint64 _nbytes_recv;    // total bytes received
136        uint64 _nbytes_xmit;    // total bytes transmitted
137        uint32 _rexmit;         // retransmit counter
138        uint32 _fastrexmit;     // fast retransmit counter
139        uint32 _nxmit;          // transmit counter
140        uint32 _nrecv;          // receive counter (total)
141        uint32 _nduprecv;       // duplicate receive counter
142};
143
144// Get stats for UTP socket
145void UTP_GetStats(struct UTPSocket *socket, UTPStats *stats);
146#endif
147
148// Close the UTP socket.
149// It is not valid to issue commands for this socket after it is closed.
150// This does not actually destroy the socket until outstanding data is sent, at which
151// point the socket will change to the UTP_STATE_DESTROYING state.
152void UTP_Close(struct UTPSocket *socket);
153
154struct UTPGlobalStats {
155        uint32 _nraw_recv[5];   // total packets recieved less than 300/600/1200/MTU bytes fpr all connections (global)
156        uint32 _nraw_send[5];   // total packets sent less than 300/600/1200/MTU bytes for all connections (global)
157};
158
159void UTP_GetGlobalStats(struct UTPGlobalStats *stats);
160
161#ifdef __cplusplus
162}
163#endif
164
165#endif //__UTP_H__
Note: See TracBrowser for help on using the repository browser.