1
0
mirror of https://github.com/elua/elua.git synced 2025-01-25 01:02:54 +08:00
elua/inc/elua_net.h
Thomas Hornschuh 427ea3970f Changed net.accept behaviour
The original implementation of net.accept did not unlisten the port
after returing. So additional clients can connect to the port, but the
connection is not used. The easiest way to solve this, is to make an
unlisten() call before returning from accept. But I think it is better
to separate listen/unlisten from accept and let the system allow
accepting new connections in the background which can be later taken
with a call to accept. This allows for example using a coroutine waiting
for new connections with a non-blocking accept loop and a yield call.
This commit contains everything to implement the new behaviour and also
add the methods net.listen and net.unlisten. To be compatible with the
old semantics net.accept automatically calls listen to the port.

In addtion the error handling of accept was changed in a way that a
timeout returns the new return value net.ERR_WAIT_TIMEDOUT instead of
returning -1. I find this more consistent.
2017-07-30 20:52:27 +02:00

57 lines
1.5 KiB
C

// Network services provided by eLua
#ifndef __ELUA_NET_H__
#define __ELUA_NET_H__
#include "lauxlib.h"
#include "platform.h"
// eLua network typedefs
typedef s16 elua_net_size;
// eLua net error codes
enum
{
ELUA_NET_ERR_OK = 0,
ELUA_NET_ERR_TIMEDOUT,
ELUA_NET_ERR_CLOSED,
ELUA_NET_ERR_ABORTED,
ELUA_NET_ERR_OVERFLOW,
ELUA_NET_ERR_LIMIT_EXCEEDED, // New TH
ELUA_NET_ERR_WAIT_TIMEDOUT // New TH
};
// eLua IP address type
typedef union
{
u32 ipaddr;
u8 ipbytes[ 4 ];
u16 ipwords[ 2 ];
} elua_net_ip;
// eLua services ports
#define ELUA_NET_TELNET_PORT 23
// Different constants
#define ELUA_NET_SOCK_STREAM 0
#define ELUA_NET_SOCK_DGRAM 1
// 'no lastchar' for read to char (recv)
#define ELUA_NET_NO_LASTCHAR ( -1 )
// eLua TCP/IP functions
int elua_net_socket( int type );
int elua_net_close( int s );
elua_net_size elua_net_recvbuf( int s, luaL_Buffer *buf, elua_net_size maxsize, s16 readto, unsigned timer_id, timer_data_type to_us );
elua_net_size elua_net_recv( int s, void *buf, elua_net_size maxsize, s16 readto, unsigned timer_id, timer_data_type to_us );
elua_net_size elua_net_send( int s, const void* buf, elua_net_size len );
int elua_accept( u16 port, unsigned timer_id, timer_data_type to_us, elua_net_ip* pfrom );
int elua_net_connect( int s, elua_net_ip addr, u16 port );
elua_net_ip elua_net_lookup( const char* hostname );
int elua_listen( u16 port,BOOL flisten ); // Added TH
int elua_net_get_last_err( int s );
int elua_net_get_telnet_socket( void );
#endif