|
|
|
@ -112,6 +112,8 @@ void handle_error(CSimpleSocket::CSocketError err,bool skip_timeout=true)
|
|
|
|
|
return;
|
|
|
|
|
if (err == CSimpleSocket::SocketTimedout && skip_timeout)
|
|
|
|
|
return;
|
|
|
|
|
if (err == CSimpleSocket::SocketEwouldblock && skip_timeout)
|
|
|
|
|
return;
|
|
|
|
|
throw std::runtime_error(translate_socket_error(err));
|
|
|
|
|
}
|
|
|
|
|
static int lua_socket_bind(std::string ip,int port)
|
|
|
|
@ -218,6 +220,7 @@ static std::string lua_client_receive(int server_id,int client_id,int bytes,std:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
ret+=(char)*sock->GetData();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
@ -285,11 +288,12 @@ static int lua_socket_connect(std::string ip,int port)
|
|
|
|
|
delete sock;
|
|
|
|
|
throw std::runtime_error(translate_socket_error(err));
|
|
|
|
|
}
|
|
|
|
|
sock->SetNonblocking();
|
|
|
|
|
last_client_id++;
|
|
|
|
|
clients[last_client_id]=sock;
|
|
|
|
|
return last_client_id;
|
|
|
|
|
}
|
|
|
|
|
static void lua_socket_set_timeout(int server_id,int client_id,int32_t sec,int32_t msec)
|
|
|
|
|
CSimpleSocket* get_socket(int server_id, int client_id)
|
|
|
|
|
{
|
|
|
|
|
std::map<int, CActiveSocket*>* target = &clients;
|
|
|
|
|
if (server_id>0)
|
|
|
|
@ -301,10 +305,7 @@ static void lua_socket_set_timeout(int server_id,int client_id,int32_t sec,int32
|
|
|
|
|
server &cur_server = servers[server_id];
|
|
|
|
|
if (client_id == -1)
|
|
|
|
|
{
|
|
|
|
|
cur_server.socket->SetConnectTimeout(sec,msec);
|
|
|
|
|
cur_server.socket->SetReceiveTimeout(sec,msec);
|
|
|
|
|
cur_server.socket->SetSendTimeout(sec,msec);
|
|
|
|
|
return;
|
|
|
|
|
return cur_server.socket;
|
|
|
|
|
}
|
|
|
|
|
target = &cur_server.clients;
|
|
|
|
|
}
|
|
|
|
@ -314,13 +315,53 @@ static void lua_socket_set_timeout(int server_id,int client_id,int32_t sec,int32
|
|
|
|
|
throw std::runtime_error("Client does with this id not exist");
|
|
|
|
|
}
|
|
|
|
|
CActiveSocket *sock = (*target)[client_id];
|
|
|
|
|
return sock;
|
|
|
|
|
}
|
|
|
|
|
static void lua_socket_set_timeout(int server_id,int client_id,int32_t sec,int32_t msec)
|
|
|
|
|
{
|
|
|
|
|
CSimpleSocket *sock = get_socket(server_id, client_id);
|
|
|
|
|
sock->SetConnectTimeout(sec,msec);
|
|
|
|
|
sock->SetReceiveTimeout(sec,msec);
|
|
|
|
|
sock->SetSendTimeout(sec,msec);
|
|
|
|
|
if (!sock->SetReceiveTimeout(sec, msec) ||
|
|
|
|
|
!sock->SetSendTimeout(sec, msec))
|
|
|
|
|
{
|
|
|
|
|
CSimpleSocket::CSocketError err = sock->GetSocketError();
|
|
|
|
|
throw std::runtime_error(translate_socket_error(err));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
static bool lua_socket_select(int server_id, int client_id, int32_t sec, int32_t msec)
|
|
|
|
|
{
|
|
|
|
|
CSimpleSocket *sock = get_socket(server_id, client_id);
|
|
|
|
|
return sock->Select(sec, msec);
|
|
|
|
|
}
|
|
|
|
|
static void lua_socket_set_blocking(int server_id, int client_id, bool value)
|
|
|
|
|
{
|
|
|
|
|
CSimpleSocket *sock = get_socket(server_id, client_id);
|
|
|
|
|
bool ok;
|
|
|
|
|
if (value)
|
|
|
|
|
{
|
|
|
|
|
ok = sock->SetBlocking();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ok = sock->SetNonblocking();
|
|
|
|
|
}
|
|
|
|
|
if (!ok)
|
|
|
|
|
{
|
|
|
|
|
CSimpleSocket::CSocketError err = sock->GetSocketError();
|
|
|
|
|
throw std::runtime_error(translate_socket_error(err));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
static bool lua_socket_is_blocking(int server_id, int client_id)
|
|
|
|
|
{
|
|
|
|
|
CSimpleSocket *sock = get_socket(server_id, client_id);
|
|
|
|
|
return !sock->IsNonblocking();
|
|
|
|
|
}
|
|
|
|
|
DFHACK_PLUGIN_LUA_FUNCTIONS {
|
|
|
|
|
DFHACK_LUA_FUNCTION(lua_socket_bind), //spawn a server
|
|
|
|
|
DFHACK_LUA_FUNCTION(lua_socket_connect),//spawn a client (i.e. connection)
|
|
|
|
|
DFHACK_LUA_FUNCTION(lua_socket_select),
|
|
|
|
|
DFHACK_LUA_FUNCTION(lua_socket_set_blocking),
|
|
|
|
|
DFHACK_LUA_FUNCTION(lua_socket_is_blocking),
|
|
|
|
|
DFHACK_LUA_FUNCTION(lua_socket_set_timeout),
|
|
|
|
|
DFHACK_LUA_FUNCTION(lua_server_accept),
|
|
|
|
|
DFHACK_LUA_FUNCTION(lua_server_close),
|
|
|
|
|