MCPcopy Create free account
hub / github.com/defold/defold / tcp_create

Function tcp_create

engine/script/src/luasocket/tcp.c:359–391  ·  view source on GitHub ↗

=========================================================================*\ * Library functions \*=========================================================================*/ -------------------------------------------------------------------------*\ * Creates a master tcp object \*-------------------------------------------------------------------------*/

Source from the content-addressed store, hash-verified

357* Creates a master tcp object
358\*-------------------------------------------------------------------------*/
359static int tcp_create(lua_State *L, int family) {
360 t_socket sock;
361 const char *err = inet_trycreate(&sock, family, SOCK_STREAM);
362 /* try to allocate a system socket */
363 if (!err) {
364 /* allocate tcp object */
365 p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
366 memset(tcp, 0, sizeof(t_tcp));
367 /* set its type as master object */
368 auxiliar_setclass(L, "tcp{master}", -1);
369 /* initialize remaining structure fields */
370 socket_setnonblocking(&sock);
371
372#if !defined(DM_IPV6_UNSUPPORTED) && !defined(__EMSCRIPTEN__)
373 if (family == PF_INET6) {
374 int yes = 1;
375 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
376 (void *)&yes, sizeof(yes));
377 }
378#endif
379 tcp->sock = sock;
380 io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv,
381 (p_error) socket_ioerror, &tcp->sock);
382 timeout_init(&tcp->tm, -1, -1);
383 buffer_init(&tcp->buf, &tcp->io, &tcp->tm);
384 tcp->family = family;
385 return 1;
386 } else {
387 lua_pushnil(L);
388 lua_pushstring(L, err);
389 return 2;
390 }
391}
392
393static int global_create(lua_State *L) {
394 return tcp_create(L, AF_INET);

Callers 2

global_createFunction · 0.85
global_create6Function · 0.85

Calls 9

inet_trycreateFunction · 0.85
lua_newuserdataFunction · 0.85
auxiliar_setclassFunction · 0.85
io_initFunction · 0.85
timeout_initFunction · 0.85
buffer_initFunction · 0.85
lua_pushnilFunction · 0.85
lua_pushstringFunction · 0.85
socket_setnonblockingFunction · 0.70

Tested by

no test coverage detected