📄 rom400_sock.h
字号:
/**
* \brief Create a network socket for TCP or UDP communication.
*
* Creates a socket for network communication. This function returns
* a socket handle, but has not specific local address assigned to it.
* Note that this function calls <i>#task_gettaskid</i> through the
* function redirect table.
*
* \param domain ignored
* \param type #SOCKET_TYPE_DATAGRAM or #SOCK_DGRAM for UDP,
* #SOCKET_TYPE_STREAM or #SOCK_STREAM for TCP
* \param protocol ignored
*
* \return 0x0FFFF for failure, or the socket handle (socket number)
*
* \sa #bind
* \sa #connect
* \sa #closesocket
*/
//---------------------------------------------------------------------------
#define socket(domain,type,protocol) syn_socket((type))
/**
* \brief Closes a specific socket.
*
* Closes the specified socket that was created using the <i>socket</i>
* function.
*
* \param socket_num the socket handle to close
*
* \return 0 for success, non-zero for failure.
*
* \sa #socket
*/
//---------------------------------------------------------------------------
#define closesocket(socket_num) syn_closesocket((socket_num))
/**
* \brief Sends a UDP datagram to a specified address.
*
* Sends a UDP datagram to a specified address. The success/failure code
* this function returns says nothing of if the packet was recieved by the
* target, only that the socket layer was able to push the data out. The
* socket <i>socket_num</i> must have been created with a type
* <i>#SOCKET_TYPE_DATAGRAM</i>.
*
* \param socket_num the socket handle to use to send a UDP packet
* \param buffer the data to send in the datagram packet
* \param length the number of bytes to send in the datagram packet
* \param flags ignored
* \param address the destination address and port
* \param address_length the length of the address structure (ignored)
*
* \return 0 for success, non-zero for failure.
*
* \sa #recvfrom
* \sa #socket
*/
//---------------------------------------------------------------------------
#define sendto(socket_num,buffer,length,flags,address,address_length) syn_sendto(syn_setDatagramAddress((socket_num),1,(address)),(length),(buffer))
/**
* \brief Receive a UDP datagram.
*
* Receives a message on the specified socket, and stores the address that
* sent it. If no data is available,
* <i>#recvfrom</i> blocks subject to the <i>#SO_TIMEOUT</i> value. The socket
* <i>socket_num</i> must have been created with a type
* <i>#SOCKET_TYPE_DATAGRAM</i>. It is required to use <i>#bind</i> to assign a
* local port to the socket, before receiving data.
* <b>NOTE:</b> This function reads <b>up to</b> <i>length</i> bytes of a
* datagram. Any data not read in the datagram will be discarded.
*
* \param socket_num the socket handle to use to wait for and read a UDP packet
* \param buffer the location to write any data read from the datagram socket
* \param length the maximum number of bytes to read from a datagram socket
* \param flags ignored
* \param address location to fill in the address and port of the sender
* \param address_length the length of the address structure (ignored)
*
* \return The number of bytes read. If the operation times out according
* to the <i>SO_TIMEOUT</i>, a value of 0 is returned. If another
* error occurs, 0x0FFFF is returned.
*
* \sa #sendto
* \sa #socket
* \sa #bind
*/
//---------------------------------------------------------------------------
#define recvfrom(socket_num,buffer,length,flags,address,address_length) syn_recvfrom(syn_setDatagramAddress((socket_num),0,(address)),(length),(buffer))
/**
* \brief Connects a TCP socket to a specified address.
*
* Connects to a specified address with a streaming socket. This function can
* only be used once with each socket. The socket <i>socket_num</i> must have
* been created with type <i>#SOCKET_TYPE_STREAM</i>.
*
* \param socket_num the socket handle to use to wait for and read a UDP packet
* \param address IP address and port number to create a streaming connection to
* \param address_length the length of the address structure (ignored)
*
* \return 0 for success, non-zero for failure.
*
* \sa #socket
*/
//---------------------------------------------------------------------------
#define connect(socket_num,address,address_length) syn_connect((socket_num),(address))
/**
* \brief Binds a socket to a specified address.
*
* Assigns a local address and port (stored in the <i>address</i> parameter)
* to a socket. Binding a socket is necessary for server sockets. For
* client sockets, use <i>#bind</i> if a specific source port is desirable.
*
* Fill <i>address</I> with 0's (for sin_addr and sin_port) to bind to
* any available local port. Use <i>#getsockname</i> to discover
* which port the socket was bound to.
*
* <b>NOTE:</b> When binding a UDP socket, matching inbound UDP packets will
* be queued up for the socket. Call <i>#recvfrom</i> periodically to avoid
* the risk of running out of kernel memory.
*
* \param socket_num socket handle to bind to a local port number
* \param address contains the local address (including port number)
* \param address_length the length of the address structure (ignored)
*
* \return 0 for success, non-zero for failure.
*
* \sa #listen
* \sa #getsockname
* \sa #recvfrom
*/
//---------------------------------------------------------------------------
#define bind(socket_num,address,address_length) syn_bind((socket_num),(address))
/**
* \brief Tells a socket to listen for incoming connections.
*
* Tells the socket to listen for connections. A queue of length <i>backlog</i>
* is created for pending (un-<i>#accept</i>ed connections). It is required to
* use <i>#bind</i> to assign a local port before calling <i>#listen</i>. Use
* <i>#accept</i> to move an incoming request to an established state, or wait
* for incoming connections.
*
* \param socket_num socket handle that will listen for connections
* \param backlog the maximum number of pending connections (max 16
* for the DS80C400)
*
* \return 0 for success, non-zero for failure.
*
* \sa #bind
* \sa #accept
*/
//---------------------------------------------------------------------------
#define listen(socket_num,backlog) syn_listen((socket_num),(backlog))
/**
* \brief Accepts TCP connections on the specified socket.
*
* Accepts a TCP conection on the specified socket. This function moves
* the first pending connection request from the listen queue into the
* established state, assigning a new local socket to the connection
* for communication. <i>#accept</i> blocks if there are no pending
* incoming requests. The socket <i>socket_num</i> must have been created
* with type <i>#SOCKET_TYPE_STREAM</i>, bound to an address using <i>#bind</i>,
* and given a listen queue by calling <i>#listen</i>.
*
* \param socket_num the handle of the socket that will wait for connections
* \param address location to write remote address
* \param address_length the length of the address structure (ignored)
*
* \return New socket handle for communicating with remote
* socket, or 0x0FFFF for failure
*
* \sa #socket
* \sa #bind
* \sa #listen
*/
//---------------------------------------------------------------------------
#define accept(socket_num,address,address_length) syn_accept((socket_num),(address))
/**
* \brief Reads data from a TCP socket.
*
* Reads data from a TCP socket. If there is no data available, <i>#recv</i>
* blocks until there is data, subject to the value of <i>#SO_TIMEOUT</i>.
* <b>NOTE:</b> This function reads <b>up to</b> <i>length</i> bytes. Call this function
* repeatedly if you need to read a minimum number of bytes.
*
* \param socket_num handle of the streaming socket that will read data
* \param buffer location to write any data read
* \param length maximum amount of data to read
* \param flags ignored
*
* \return The number of bytes read. If the operation times out according
* to the <i>SO_TIMEOUT</i>, a value of 0 is returned. If another
* error occurs, 0x0FFFF is returned.
*
* \sa #connect
* \sa #send
*/
//---------------------------------------------------------------------------
#define recv(socket_num,buffer,length,flags) syn_recv((socket_num),(length),(buffer))
/**
* \brief Sends data to a TCP socket.
*
* Writes data to a TCP socket. The return value of this function is only a
* local success/failure code, and may not necessarily detect transmission
* errors.
*
* \param socket_num handle of the streaming socket that will write data
* \param buffer location of data to write
* \param length number of bytes to write
* \param flags ignored
*
* \return 0 for success, non-zero for failure.
*
* \sa #connect
* \sa #recv
*/
//---------------------------------------------------------------------------
#define send(socket_num,buffer,length,flags) syn_send((socket_num),(length),(buffer))
/**
* \brief Get various socket options.
*
* Reads a number of supported socket options. Data written into the
* buffer depends on the requested socket option.
*
* <table>
* <tr> <td> <b>Name</b> </td> <td> <b>Description</b> </td> <td> <b>Data in buffer</b> </td> </tr>
* <tr> <td> TCP_NODELAY </td> <td> TCP Nagle Enable </td> <td> 1 byte </td> </tr>
* <tr> <td> SO_LINGER </td> <td> Ignored </td> <td> N/A </td> </tr>
* <tr> <td> SO_TIMEOUT </td> <td> Inactivity timeout </td> <td> 4 bytes (milliseconds, MSB first) </td> </tr>
* <tr> <td> SO_BINDADDR </td> <td> Local socket IP </td> <td> 16 bytes </td> </tr>
* </table>
*
* \param socket_num socket to get option information for
* \param level ignored
* \param name option to get
* \param buffer location where option data will be written
* \param length length of the buffer
*
* \return 0 for success, non-zero for failure
*
* \sa #setsockopt
*/
//---------------------------------------------------------------------------
#define getsockopt(socket_num,level,name,buffer,length) syn_getsockopt((socket_num),(name),(buffer))
/**
* \brief Set various socket options.
*
* Sets a number of supported socket options. Input data in the
* buffer depends on the desired socket option.
*
* <table>
* <tr> <td> <b>Name</b> </td> <td> <b>Description</b> </td> <td> <b>Data in buffer</b> </td> </tr>
* <tr> <td> TCP_NODELAY </td> <td> TCP Nagle Enable </td> <td> 1 byte </td> </tr>
* <tr> <td> SO_LINGER </td> <td> Ignored </td> <td> N/A </td> </tr>
* <tr> <td> SO_TIMEOUT </td> <td> Inactivity timeout </td> <td> 4 bytes (milliseconds, MSB first) </td> </tr>
* <tr> <td> SO_BINDADDR </td> <td> Read only </td> <td> N/A </td> </tr>
* </table>
*
* \param socket_num socket to set option information for
* \param level ignored
* \param name option to set
* \param buffer location of option data that will be written
* \param length length of the buffer
*
* \return 0 for success, non-zero for failure
*
* \sa #getsockopt
*/
//---------------------------------------------------------------------------
#define setsockopt(socket_num,level,name,buffer,length) syn_setsockopt((socket_num),(name),(buffer))
/**
* \brief Gets the local IP and port of a socket.
*
* Stores the local IP and port number of the specified socket in the
* the <i>address</i> parameter. Use <i>#getpeername</i> to get
* the remote port's information for a connection-based (TCP)
* socket.
*
* \param socket_num handle of the socket to get local IP and port for
* \param address structure where IP and port will be stored
* \param address_length the length of the address structure (ignored)
*
* \return 0 for success, non-zero for failure
*
* \sa #getpeername
*/
//---------------------------------------------------------------------------
#define getsockname(socket_num,address,address_length) syn_getsockname((socket_num),(address))
/**
* \brief Gets the remote address of a connection-based (TCP socket).
*
* Stores the IP address of the remote socket communicating with the socket
* specified by <i>socket_num</i>. Use <i>#getsockname</i> to get the local
* port's information.
*
* \param socket_num handle of the socket to get remote IP and port for
* \param address structure where IP and port will be stored
* \param address_length the length of the address structure (ignored)
*
* \return 0 for success, non-zero for failure
*
* \sa #getsockname
*/
//---------------------------------------------------------------------------
#define getpeername(socket_num,address,address_length) syn_getpeername((socket_num),(address))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -