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

Last change on this file since 11919 was 11919, checked in by jordan, 10 years ago

import libutp into third-party/ and plug it in to autoconf, automake

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