/** ------------------------------------------------------------------------- * Recieve Datagram from a UDP socket. * \param L The lua state. * \lparam socket socket userdata. * \lreturn rcvd number of bytes recieved. * \lreturn ip ip endpoint userdata. * \return int # of values pushed onto the stack. *-------------------------------------------------------------------------*/ static int lt_net_udp_recvfrom( lua_State *L ) { struct t_net *s; struct t_buf *buf; struct sockaddr_in *si_cli; int rcvd; char buffer[ BUFSIZ ]; char *rcv = &(buffer[ 0 ]); int len = sizeof( buffer )-1; unsigned int slen = sizeof( si_cli ); s = t_net_udp_check_ud( L, 1, 1 ); if (lua_isuserdata( L, 2 )) { buf = t_buf_check_ud ( L, 2, 1 ); rcv = (char *) &(buf->b[ 0 ]); len = buf->len; } si_cli = t_net_ip4_create_ud( L ); if ((rcvd = recvfrom( s->fd, rcv, len, 0, (struct sockaddr *) &(*si_cli), &slen ) ) == -1) return t_push_error( L, "Failed to recieve UDP packet"); // return buffer, length, IpEndpoint lua_pushlstring( L, rcv, rcvd ); lua_pushinteger( L, rcvd ); lua_pushvalue( L, -3 ); return 3; }
/**-------------------------------------------------------------------------- * update NOT ALLOWED. * \param L The lua state. * \lparam Http.Connection instance. * \lparam key string/integer * \lparam value LuaType * \return The # of items pushed to the stack. * --------------------------------------------------------------------------*/ static int lt_htp_con__newindex( lua_State *L ) { t_htp_con_check_ud( L, -3, 1 ); return t_push_error( L, "Can't change values in `"T_HTP_CON_TYPE"`" ); }
/**-------------------------------------------------------------------------- * update NOT ALLOWED. * \param L The lua state. * \lparam Http.Connection instance. * \lparam key string/integer * \lparam value LuaType * \return The # of items pushed to the stack. * --------------------------------------------------------------------------*/ static int lt_htp_con__newindex( lua_State *L ) { t_htp_con_check_ud( L, -3, 1 ); return t_push_error( L, "Can't change values in `T.Http.Connection`" ); }
/**-------------------------------------------------------------------------- * update NOT ALLOWED. * \param L The lua state. * \lparam Http.Message instance. * \lparam key string/integer * \lparam value LuaType * \return int # of values pushed onto the stack. * --------------------------------------------------------------------------*/ static int lt_htp_str__newindex( lua_State *L ) { t_htp_str_check_ud( L, -3, 1 ); return t_push_error( L, "Can't change values in `T.Http.Stream`" ); }
/** ------------------------------------------------------------------------- * Send Datagram over a UDP socket to an IP endpoint. * \param L The lua state. * \lparam socket socket userdata. * \lparam ip sockaddr userdata. * \lparam msg luastring. * or * \lparam T.Buffer lua-t BUffer type. * \lreturn sent number of bytes sent. * \return int # of values pushed onto the stack. *-------------------------------------------------------------------------*/ static int lt_net_udp_sendto( lua_State *L ) { struct t_net *s; struct sockaddr_in *ip; struct t_buf *buf; int sent; int len; const char *msg; s = t_net_udp_check_ud( L, 1, 1 ); ip = t_net_ip4_check_ud( L, 2, 1 ); if (lua_isstring( L, 3 )) { msg = lua_tostring( L, 3 ); len = strlen( msg ); } else if (lua_isuserdata( L, 3 )) { buf = t_buf_check_ud( L, 3, 1 ); msg = (char *) &(buf->b[ 0 ]); len = buf->len; } else return t_push_error( L, "ERROR sendTo(socket,ip,msg) takes msg argument" ); if ((sent = sendto( s->fd, msg, len, 0, (struct sockaddr *) &(*ip), sizeof( struct sockaddr )) ) == -1) return t_push_error( L, "Failed to send UDP packet to %s:%d", inet_ntoa( ip->sin_addr ), ntohs( ip->sin_port ) ); lua_pushinteger( L, sent ); return 1; }
/**-------------------------------------------------------------------------- * Sets the onData method in T.Http.Message. * \param L The lua state. * \lparam Http.Message instance. * \lparam function to be executed when body data arrives on connection. * \return int # of values pushed onto the stack. * --------------------------------------------------------------------------*/ static int lt_htp_str_onbody( lua_State *L ) { struct t_htp_str *m = t_htp_str_check_ud( L, 1, 1 ); if (lua_isfunction( L, 2 )) { m->bR = luaL_ref( L, LUA_REGISTRYINDEX ); return 0; } if (lua_isnoneornil( L, 2 )) { m->bR = LUA_NOREF; return 0; } else return t_push_error( L, "Argument must be function or nil" ); }