Skip to content

Latest commit

 

History

History
339 lines (261 loc) · 9.75 KB

api_doc.md

File metadata and controls

339 lines (261 loc) · 9.75 KB

utp API

rt_init

Description

The rt_init function initialize utp stack. This function should be called before other functions.

Syntax:

int rt_init(const char* str, int len);

Parameter:

str: Reserved for later use, should be NULL.

len: Reserved for later use, should be zero.

Return value:

rt_init returns utp version number.

rt_set_callback

Description

The rt_set_callback set call back functions to utp library.

Syntax:

void rt_set_callback(socket_event_callback *event_callback, send_data_callback *sendproc);

Parameter:

event_callback: A function pointer which is used to notify utp socket event.

void socket_event_callback(RTSOCKET socket, int event)

Event Type Meaning
RTTP_EVENT_CONNECT int32 Socket connected
RTTP_EVENT_READ int32 Data readable on socket
RTTP_EVENT_WRITE int32 Socket is writable now
RTTP_EVENT_ERROR int32 Error happened on socket

sendproc: A function pointer which is used to send packet generated by utp library.

void send_proc(RTSOCKET socket, const char * buffer, int len, const char* addr, int addr_len)

Return value:

This function has no return value.

rt_socket

Description

The rt_socket function create a utp socket

Syntax:

RTSOCKET rt_socket(int mode);

Parameter:

mode: Reserved for later use.

Return value:

If no error occurs, rt_socket returns a descriptor referencing the new utp socket.

rt_connect

Description

The rt_connect function attempts to establish a connection to a specified socket. This function does not wait for the establish of connection.

Syntax:

int rt_connect(RTSOCKET s, const char *to, int tolen);

Parameter:

s: A descriptor identifying an unconnected socket returned by rt_socket function.

to: Remote address to which the connection should be established, utp saves this address and passes it back when call send_proc callback to send data.

tolen: Length of address, in bytes.

Return value:

If no error occurs, rt_connect returns 0, otherwise it returns a error code.

rt_recv

Description

The rt_recv function receives data from a connected utp socket.

Syntax:

int rt_recv(RTSOCKET s, char * buffer, int len, int flag);

Parameter:

s: A descriptor identifying a connected utp socket.

buffer: A pointer to the buffer to receive the incoming data.

len: The length, in bytes, of the buffer pointed to by the buffer parameter.

flag: Reserved for later use.

Return value:

If no error occurs, rt_recv returns the number of bytes received and the buffer pointed to by the buffer parameter will contain this data received. If the connection has been gracefully closed, the return value is zero. If there is no data available, the return value is -1. If error occurs the return value is a negative error code.

rt_send

Description

The rt_send function sends data on a connected socket.

Syntax:

int rt_send(RTSOCKET s, const char * buffer, int len, int flag);

Parameter:

s: A descriptor identifying a connected utp socket.

buffer: A pointer to a buffer containing the data to be transmitted.

len: The length, in bytes, of the buffer pointed to by the buffer parameter.

flag: Reserved for later use.

Return value:

If no error occurs, send returns the total number of bytes sent, which can be less than the number requested to be sent in the len parameter. If no data can be sent the return value is -1 which means the requested operation would block. If error occurs the return value is a negative error code.

Notes:

Avoid to send data one byte by one byte which is very inefficient.

rt_close

Description

The rt_close function closes an existing socket.

Syntax:

void rt_close(RTSOCKET s);

Parameter:

s: A descriptor identifying the utp socket to close.

Return value:

This function has no return value.

rt_incoming_packet

Description

The rt_incoming_packet function input a packet to utp library. Rttp does not send and receive data itself which allows utp to keep out of socket io framework and ease to integrate.

Syntax:

RTSOCKET rt_incoming_packet(const char * buffer, int len, const char from, int fromlen, void userdata);

Parameter:

buffer: A pointer to a buffer containing the data

len: data length in bytes

from: The address which the incoming packet comes from

fromlen: The address length, in bytes.

userdata: The userdata will be set to utp socket when the utp socket is a incoming connection.

Return value:

The return value is the new created utp socket descriptor if there is a incoming utp connection, otherwise the return value is 0.

rt_tick

Description

The rt_tick function inputs a timer event to utp library. This function should be called periodically. The recommended time interval should be less than 20 ms.

Syntax:

void rt_tick();

Parameter:

This function has no parameter.

Return value:

This function has no return value.

rt_getpeername

Description

The rt_getpeername function retrieves the address of the peer to which a socket is connected.

Syntax:

int rt_getpeername(RTSOCKET s, char* name, int len)

Parameter:

s: A descriptor identifying the utp socket

name: A pointer to the buffer to receive the address which is identical with the parameter of rt_connect and rt_incoming_packet

len: The length, in bytes, of the buffer pointed to by the name parameter.

Return value:

If len is smaller than the address size the return value is -1, otherwise the function returns the number of bytes copied.

rt_get_error

Description

The rt_get_error function returns the error status of utp socket.

Syntax:

int rt_get_error(RTSOCKET s);

Parameter:

s: A descriptor identifying the utp socket

Return value:

The return value is the error code.

rt_connected

Description

The rt_connected function determines whether the utp socket connection is established with remote

Syntax:

int rt_connected(RTSOCKET s);

Parameter:

s:

A descriptor identifying the utp socket

Return value:

rt_connected return 1 if connected, otherwise the return value is 0

rt_writable

Description

The rt_writable function determines whether rt_write will block on the utp socket

Syntax:

int rt_writable(RTSOCKET s);

Parameter:

s:

A descriptor identifying the utp socket

Return value:

rt_writable return 1 if block will not happen, otherwise the return value is 0

rt_readable

Description

The rt_readable function determines the incoming data status of utp socket

Syntax:

int rt_readable(RTSOCKET s);

Parameter:

s:

A descriptor identifying the utp socket

Return value:

The rt_readable function returns 1 if there is data waiting for receiving in buffer and recv would not block, otherwise the return value is 0

rt_setsockopt

Description

The rt_setsockopt function sets a socket option.

Syntax:

int rt_setsockopt(RTSOCKET s, int optname, void *optval, int optlen);

Parameter:

s:

A descriptor identifying the utp socket

optname:

The utp socket option for which the value is to be set

Value Type Meaning
RTSO_MTU int32 MTU
RTSO_FEC int32 Enable FEC or not
RTSO_FAST_ACK int32 Enable fast ack or not
RTSO_RCVBUF int32 Max utp socket recv buffer
RTSO_MODE int32 Rttp mode

optval:

A pointer to the buffer in which the value for the requested option is specified.

optlen: The size, in bytes, of the buffer pointed to by the optval parameter.

Return value:

If no error occurs, rt_setsockopt returns zero, otherwise the return value is -1.

rt_getsockopt

Description

The rt_getsockopt function sets a socket option.

Syntax:

int rt_getsockopt(RTSOCKET s, int optname, void *optval, int optlen);

Parameter:

s:

A descriptor identifying the utp socket

optname:

The utp socket option for which the value is to be retrieved

Value Type Meaning
RTSO_MTU int32 MTU
RTSO_FEC int32 FEC enabled or not
RTSO_FAST_ACK int32 Fast ack enabled or not
RTSO_RCVBUF int32 Max utp socket recv buffer
RTSO_RTT int64 Round trip time, in micro seconds
RTSO_LOST_RATE int32 Packet lost percent since connection
RTSO_RECENT_LOST_RATE int32 Packet lost percent in recent 3 seconds

optval:

A pointer to the buffer in which the value for the requested option is to be returned

optlen:

A pointer to the size, in bytes, of the optval buffer

Return value:

If no error occurs, rt_setsockopt returns size of value saved in optval, otherwise the return value is -1.

rt_set_userdata

Description

The rt_set_userdata function sets a data to utp socket which can be retrieved later by rt_get_userdata function

Syntax:

void rt_set_userdata(RTSOCKET s, void *userdata);

Parameter:

s: A descriptor identifying the utp socket

userdata: data set to utp socket.

Return value:

This function has no return value.

rt_get_userdata

Description

The rt_get_userdata function retrieves user data associated with utp socket

Syntax:

void* rt_get_userdata(RTSOCKET s);

Parameter:

s: A descriptor identifying the utp socket

Return value:

rt_get_userdata returns user data set by rt_set_userdata function.

rt_uninit

Description

The rt_uninit function uninit utp stack.

Syntax:

void rt_uninit();

Parameter:

This function has no parameter.

Return value:

This function has no return value.