HL_PRIM bool hl_socket_select( varray *ra, varray *wa, varray *ea, char *tmp, int tmp_size, double timeout ) { struct timeval tval, *tt; fd_set *rs, *ws, *es; unsigned int max = 0; rs = make_socket_set(ra,&tmp,&tmp_size,&max); ws = make_socket_set(wa,&tmp,&tmp_size,&max); es = make_socket_set(ea,&tmp,&tmp_size,&max); if( rs == NULL || ws == NULL || es == NULL ) return false; if( timeout < 0 ) tt = NULL; else { tt = &tval; init_timeval(timeout,tt); } hl_blocking(true); if( select((int)(max+1),ra?rs:NULL,wa?ws:NULL,ea?es:NULL,tt) == SOCKET_ERROR ) { hl_blocking(false); return false; } hl_blocking(false); make_array_result(rs,ra); make_array_result(ws,wa); make_array_result(es,ea); return true; }
/** socket_select : read : 'socket array -> write : 'socket array -> others : 'socket array -> timeout:number? -> 'socket array array <doc>Perform the [select] operation. Timeout is in seconds or [null] if infinite</doc> **/ static value socket_select( value rs, value ws, value es, value timeout ) { struct timeval tval; struct timeval *tt; SOCKET n = 0; fd_set rx, wx, ex; fd_set *ra, *wa, *ea; value r; POSIX_LABEL(select_again); ra = make_socket_array(rs,val_array_size(rs),&rx,&n); wa = make_socket_array(ws,val_array_size(ws),&wx,&n); ea = make_socket_array(es,val_array_size(es),&ex,&n); if( ra == &INVALID || wa == &INVALID || ea == &INVALID ) neko_error(); if( val_is_null(timeout) ) tt = NULL; else { val_check(timeout,number); tt = &tval; init_timeval(val_number(timeout),tt); } if( select((int)(n+1),ra,wa,ea,tt) == SOCKET_ERROR ) { HANDLE_EINTR(select_again); neko_error(); } r = alloc_array(3); val_array_ptr(r)[0] = make_array_result(rs,ra); val_array_ptr(r)[1] = make_array_result(ws,wa); val_array_ptr(r)[2] = make_array_result(es,ea); return r; }
/** socket_select : read : 'socket array -> write : 'socket array -> others : 'socket array -> timeout:number? -> 'socket array array <doc>Perform the [select] operation. Timeout is in seconds or [null] if infinite</doc> **/ static value socket_select( value rs, value ws, value es, value timeout ) { struct timeval tval; struct timeval *tt; SOCKET n = 0; fd_set rx, wx, ex; fd_set *ra, *wa, *ea; value r; POSIX_LABEL(select_again); ra = make_socket_array(rs,&rx,&n); wa = make_socket_array(ws,&wx,&n); ea = make_socket_array(es,&ex,&n); if( ra == &INVALID || wa == &INVALID || ea == &INVALID ) { val_throw( alloc_string("No valid sockets") ); return alloc_null(); } if( val_is_null(timeout) ) tt = NULL; else { val_check(timeout,number); tt = &tval; init_timeval(val_number(timeout),tt); } gc_enter_blocking(); if( select((int)(n+1),ra,wa,ea,tt) == SOCKET_ERROR ) { HANDLE_EINTR(select_again); gc_exit_blocking(); char buf[100]; sprintf(buf,"Select error %d", errno ); val_throw( alloc_string(buf) ); } gc_exit_blocking(); r = alloc_array(3); val_array_set_i(r,0,make_array_result(rs,ra)); val_array_set_i(r,1,make_array_result(ws,wa)); val_array_set_i(r,2,make_array_result(es,ea)); return r; }
/* * regexp_split_to_array() * Split the string at matches of the pattern, returning the * split-out substrings as an array. */ datum_t regexp_split_to_array(PG_FUNC_ARGS) { array_build_s *astate = NULL; regexp_matches_ctx *splitctx; splitctx = setup_regexp_matches(ARG_TEXT_PP(0), ARG_TEXT_PP(1), ARG_TEXT_PP_IF_EXISTS(2), PG_COLLATION(), true, false, true); while (splitctx->next_match <= splitctx->nmatches) { astate = accum_array_result(astate, build_regexp_split_result(splitctx), false, TEXTOID, current_mctx); splitctx->next_match++; } /* * We don't call cleanup_regexp_matches here; it would try to pfree the * input string, which we didn't copy. The space is not in a long-lived * memory context anyway. */ RET_ARRAY_P(make_array_result(astate, current_mctx)); }