本文整理汇总了C++中set_nonblocking函数的典型用法代码示例。如果您正苦于以下问题:C++ set_nonblocking函数的具体用法?C++ set_nonblocking怎么用?C++ set_nonblocking使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了set_nonblocking函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: make_connection
int make_connection(uint32 ip, uint16 port)
{
struct sockaddr_in remote_address;
int fd;
int result;
fd = sSocket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
ShowError("make_connection: socket creation failed (code %d)!\n", sErrno);
return -1;
}
if( fd == 0 )
{// reserved
ShowError("make_connection: Socket #0 is reserved - Please report this!!!\n");
sClose(fd);
return -1;
}
if( fd >= FD_SETSIZE )
{// socket number too big
ShowError("make_connection: New socket #%d is greater than can we handle! Increase the value of FD_SETSIZE (currently %d) for your OS to fix this!\n", fd, FD_SETSIZE);
sClose(fd);
return -1;
}
setsocketopts(fd);
remote_address.sin_family = AF_INET;
remote_address.sin_addr.s_addr = htonl(ip);
remote_address.sin_port = htons(port);
ShowStatus("Connecting to %d.%d.%d.%d:%i\n", CONVIP(ip), port);
result = sConnect(fd, (struct sockaddr *)(&remote_address), sizeof(struct sockaddr_in));
if( result == SOCKET_ERROR ) {
ShowError("make_connection: connect failed (socket #%d, code %d)!\n", fd, sErrno);
do_close(fd);
return -1;
}
//Now the socket can be made non-blocking. [Skotlex]
set_nonblocking(fd, 1);
if (fd_max <= fd) fd_max = fd + 1;
sFD_SET(fd,&readfds);
create_session(fd, recv_to_fifo, send_from_fifo, default_func_parse);
session[fd]->client_addr = ntohl(remote_address.sin_addr.s_addr);
return fd;
}
示例2: server_worker
void* server_worker(void* arg)
{
sockfd_t sock = 0;
struct addrinfo *rp;
for (rp = serv.ai; rp != NULL; rp = rp->ai_next) {
sock = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (sock < 0) continue;
int ret = 1;
int err = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
&ret, 4);
if (err != 0) {
sys_error("setsockopt: SO_REUSEADDR");
}
if (bind(sock, rp->ai_addr, rp->ai_addrlen) == 0) {
break;
}
}
if (rp == NULL) {
sys_error("bind");
goto server_worker_exit;
}
if (set_nonblocking(sock) == -1) {
goto server_worker_exit;
}
if (serv.socktype == SOCK_STREAM) {
if (listen(sock, 5) == -1) {
sys_error("listen");
goto server_worker_exit;
}
}
serv.serv_sock = sock;
struct event evt;
event_set(&evt, sock, EV_READ | EV_PERSIST, serv.handler, arg);
event_add(&evt, NULL);
event_dispatch();
server_worker_exit:
evt_close(sock);
#ifdef _WIN32
WSACleanup();
#endif
return NULL;
}
示例3: do_accept_issue
int do_accept_issue(int listenfd){
while(true){
if(!isLoop)
break;
do{
struct sockaddr_in client_addr;
memset(&client_addr,0,sizeof(client_addr));
socklen_t addrlen=sizeof(client_addr);
#if NOT_USE_NONBLOCK
int connfd=accept(m_sockfd,\
(struct sockaddr *)&client_addr,&addrlen);
set_nonblocking(connfd);
#else
int connfd=accept4(m_sockfd,(struct sockaddr*)&client_addr,\
&addrlen,SOCK_NONBLOCK|SOCK_CLOEXEC);
#endif
if (connfd < 0){
//log_error ...
}else if(connfd > 0){
if(EINTR == errno || EAGAIN == errno){
continue;
}
continue;
}else{
//log_error ...
continue;
}
char client[IP_LEN + 1 + 10];
memset(client,'\0',IP_LEN + 1 + 10);
snprintf(client,IP_LEN + 1 + 10,"%s 's port: %d",\
inet_ntoa(client_addr.sin_addr),\
ntohs(client_addr.sin_port));
/*
* needs a struct to manage processes or threads,not finish yet !
* if error return -1
*/
notify_t *p_notify=p_notify_t+connfd%thread_num;
int ret=write(p_notify->pipe_fd[1],&connfd,sizeof(connfd));
if(ret!=sizeof(connfd)){
close(connfd);
return -1;
}
}while(false);
}
return 0;
}
示例4: be_ssl_accept
static void be_ssl_accept(int client_fd, struct in_addr client_addr)
{
if(!set_nonblocking(client_fd, 1)) {
close(client_fd);
return;
}
epdata_t *epd = malloc(sizeof(epdata_t));
if(!epd) {
close(client_fd);
return;
}
bzero(epd, sizeof(epdata_t));
if(NULL == ssl_ctx) {
free(epd);
LOGF(ERR, "ssl ctx not inited");
close(client_fd);
return;
}
if(ssl_epd_idx == -1) {
epd->ssl_verify = 1;
}
epd->L = new_lua_thread(_L);
if(epd->L) {
lua_pushlightuserdata(epd->L, epd);
lua_setglobal(epd->L, "__epd__");
}
epd->fd = client_fd;
epd->client_addr = client_addr;
epd->status = STEP_WAIT;
epd->content_length = -1;
epd->keepalive = -1;
epd->response_sendfile_fd = -1;
//epd->start_time = longtime();
epd->se_ptr = se_add(loop_fd, client_fd, epd);
epd->timeout_ptr = add_timeout(epd, STEP_WAIT_TIMEOUT, timeout_handle);
se_be_read(epd->se_ptr, _be_ssl_accept);
serv_status.active_counts++;
serv_status.connect_counts++;
}
示例5: make_connection
int make_connection(uint32 ip, uint16 port)
{
struct sockaddr_in remote_address;
int fd;
int result;
fd = sSocket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
ShowError("make_connection: criação do socket falhou ("CL_RED"%s"CL_RESET")!\n", error_msg());
return -1;
}
if( fd == 0 )
{// reserved
ShowError("make_connection: Socket #"CL_WHITE"0"CL_RESET" é reservado - Reporte isso!\n");
sClose(fd);
return -1;
}
if( fd >= FD_SETSIZE )
{// socket number too big
ShowError("make_connection: Novo socket #"CL_WHITE"%d"CL_RESET" é maior que o que suporta-se! Aumente o valor do FD_SETSIZE (atualemente "CL_WHITE"%d"CL_RESET") para seu SO consertar isso!\n", fd, FD_SETSIZE);
sClose(fd);
return -1;
}
setsocketopts(fd);
remote_address.sin_family = AF_INET;
remote_address.sin_addr.s_addr = htonl(ip);
remote_address.sin_port = htons(port);
ShowStatus("Conectando-se a "CL_WHITE"%d.%d.%d.%d"CL_RESET":"CL_WHITE"%i"CL_RESET"\n", CONVIP(ip), port);
result = sConnect(fd, (struct sockaddr *)(&remote_address), sizeof(struct sockaddr_in));
if( result == SOCKET_ERROR ) {
ShowError("make_connection: conexão falhou (soquete #"CL_WHITE"%d"CL_RESET", "CL_WHITE"%s"CL_RESET")!\n", fd, error_msg());
do_close(fd);
return -1;
}
//Now the socket can be made non-blocking. [Skotlex]
set_nonblocking(fd, 1);
if (fd_max <= fd) fd_max = fd + 1;
sFD_SET(fd,&readfds);
create_session(fd, recv_to_fifo, send_from_fifo, default_func_parse);
session[fd]->client_addr = ntohl(remote_address.sin_addr.s_addr);
return fd;
}
示例6: qnet_tcp_accept
int qnet_tcp_accept(int listen_fd,
struct sockaddr *addr, socklen_t *addrlen) {
int fd;
fd = accept(listen_fd, addr, addrlen);
if (fd == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return 0;
}
return -1;
}
set_nonblocking(fd);
return fd;
}
示例7: make_listen_bind
int make_listen_bind(uint32 ip, uint16 port)
{
struct sockaddr_in server_address;
int fd;
int result;
fd = sSocket(AF_INET, SOCK_STREAM, 0);
if( fd == -1 ) {
ShowError("make_listen_bind: socket creation failed (%s)!\n", error_msg());
exit(EXIT_FAILURE);
}
if( fd == 0 ) { // reserved
ShowError("make_listen_bind: Socket #0 is reserved - Please report this!!!\n");
sClose(fd);
return -1;
}
if( fd >= FD_SETSIZE ) { // socket number too big
ShowError("make_listen_bind: New socket #%d is greater than can we handle! Increase the value of FD_SETSIZE (currently %d) for your OS to fix this!\n", fd, FD_SETSIZE);
sClose(fd);
return -1;
}
setsocketopts(fd,NULL);
set_nonblocking(fd, 1);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(ip);
server_address.sin_port = htons(port);
result = sBind(fd, (struct sockaddr*)&server_address, sizeof(server_address));
if( result == SOCKET_ERROR ) {
ShowError("make_listen_bind: bind failed (socket #%d, %s)!\n", fd, error_msg());
exit(EXIT_FAILURE);
}
result = sListen(fd,5);
if( result == SOCKET_ERROR ) {
ShowError("make_listen_bind: listen failed (socket #%d, %s)!\n", fd, error_msg());
exit(EXIT_FAILURE);
}
if(fd_max <= fd) fd_max = fd + 1;
sFD_SET(fd, &readfds);
create_session(fd, connect_client, null_send, null_parse);
session[fd]->client_addr = 0; // just listens
session[fd]->rdata_tick = 0; // disable timeouts on this socket
return fd;
}
示例8: start_server
void start_server(int f_in, int f_out, int argc, char *argv[])
{
set_nonblocking(f_in);
set_nonblocking(f_out);
io_set_sock_fds(f_in, f_out);
setup_protocol(f_out, f_in);
if (protocol_version >= 23)
io_start_multiplex_out();
if (am_sender) {
keep_dirlinks = 0; /* Must be disabled on the sender. */
if (need_messages_from_generator)
io_start_multiplex_in();
recv_filter_list(f_in);
do_server_sender(f_in, f_out, argc, argv);
} else {
do_server_recv(f_in, f_out, argc, argv);
}
exit_cleanup(0);
}
示例9: bgp_connect
/* BGP try to connect to the peer. */
int
bgp_connect (struct peer *peer)
{
ifindex_t ifindex = 0;
/* Make socket for the peer. */
peer->fd = sockunion_socket (&peer->su);
if (peer->fd < 0)
return -1;
set_nonblocking (peer->fd);
/* Set socket send buffer size */
bgp_update_sock_send_buffer_size(peer->fd);
bgp_set_socket_ttl (peer, peer->fd);
sockopt_reuseaddr (peer->fd);
sockopt_reuseport (peer->fd);
#ifdef IPTOS_PREC_INTERNETCONTROL
if (bgpd_privs.change (ZPRIVS_RAISE))
zlog_err ("%s: could not raise privs", __func__);
if (sockunion_family (&peer->su) == AF_INET)
setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL);
else if (sockunion_family (&peer->su) == AF_INET6)
setsockopt_ipv6_tclass (peer->fd, IPTOS_PREC_INTERNETCONTROL);
if (bgpd_privs.change (ZPRIVS_LOWER))
zlog_err ("%s: could not lower privs", __func__);
#endif
if (peer->password)
bgp_md5_set_connect (peer->fd, &peer->su, peer->password);
/* Bind socket. */
bgp_bind (peer);
/* Update source bind. */
bgp_update_source (peer);
if (peer->ifname)
ifindex = ifname2ifindex (peer->ifname);
if (BGP_DEBUG (events, EVENTS))
plog_debug (peer->log, "%s [Event] Connect start to %s fd %d",
peer->host, peer->host, peer->fd);
/* Connect to the remote peer. */
return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
}
示例10: make_listen_bind
int make_listen_bind(uint32 ip, uint16 port)
{
struct sockaddr_in server_address = { 0 };
int fd;
int result;
fd = sSocket(AF_INET, SOCK_STREAM, 0);
if( fd == -1 ) {
ShowError("make_listen_bind: Criacao de socket falhou (%s)!\n", error_msg());
exit(EXIT_FAILURE);
}
if( fd == 0 ) { // reserved
ShowError("make_listen_bind: Socket #0 esta reservado - Por favor reporte isso!!!\n");
sClose(fd);
return -1;
}
if( fd >= FD_SETSIZE ) { // socket number too big
ShowError("make_listen_bind: Novo socket #%d e maior do que o limite suportado! Aumentando o valor para FD_SETSIZE (atualmente %d) seu SO deve corrigir isso!\n", fd, FD_SETSIZE);
sClose(fd);
return -1;
}
setsocketopts(fd,NULL);
set_nonblocking(fd, 1);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(ip);
server_address.sin_port = htons(port);
result = sBind(fd, (struct sockaddr*)&server_address, sizeof(server_address));
if( result == SOCKET_ERROR ) {
ShowError("make_listen_bind: socket-bind falhou (socket #%d, %s)!\n", fd, error_msg());
exit(EXIT_FAILURE);
}
result = sListen(fd,5);
if( result == SOCKET_ERROR ) {
ShowError("make_listen_bind: socket-listen falhou (socket #%d, %s)!\n", fd, error_msg());
exit(EXIT_FAILURE);
}
if(sockt->fd_max <= fd) sockt->fd_max = fd + 1;
sFD_SET(fd, &readfds);
create_session(fd, connect_client, null_send, null_parse);
session[fd]->client_addr = 0; // just listens
session[fd]->rdata_tick = 0; // disable timeouts on this socket
return fd;
}
示例11: make_connection
int make_connection(uint32 ip, uint16 port, struct hSockOpt *opt) {
struct sockaddr_in remote_address = { 0 };
int fd;
int result;
fd = sSocket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
ShowError("make_connection: Criacao de socket falhou (%s)!\n", error_msg());
return -1;
}
if( fd == 0 ) {// reserved
ShowError("make_connection: Socket #0 esta reservado - Por favor reporte isso!!!\n");
sClose(fd);
return -1;
}
if( fd >= FD_SETSIZE ) {// socket number too big
ShowError("make_connection: Novo socket #%d e maior do que o limite suportado! Aumentando o valor para FD_SETSIZE (atualmente %d) seu SO deve corrigir isso!\n", fd, FD_SETSIZE);
sClose(fd);
return -1;
}
setsocketopts(fd,opt);
remote_address.sin_family = AF_INET;
remote_address.sin_addr.s_addr = htonl(ip);
remote_address.sin_port = htons(port);
if( !( opt && opt->silent ) )
ShowStatus("Conectando-se a %d.%d.%d.%d:%i\n", CONVIP(ip), port);
result = sConnect(fd, (struct sockaddr *)(&remote_address), sizeof(struct sockaddr_in));
if( result == SOCKET_ERROR ) {
if( !( opt && opt->silent ) )
ShowError("make_connection: Falha na conexao (socket #%d, %s)!\n", fd, error_msg());
sockt->close(fd);
return -1;
}
//Now the socket can be made non-blocking. [Skotlex]
set_nonblocking(fd, 1);
if (sockt->fd_max <= fd) sockt->fd_max = fd + 1;
sFD_SET(fd,&readfds);
create_session(fd, recv_to_fifo, send_from_fifo, default_func_parse);
session[fd]->client_addr = ntohl(remote_address.sin_addr.s_addr);
return fd;
}
示例12: qnet_tcp_connect
int
qnet_tcp_connect(int port, const char *addr, int *error, int *fd) {
struct sockaddr_in dstaddr;
socklen_t len;
if ((*fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
*error = errno;
qerror("create socket error: %s", strerror(errno));
return QERROR;
}
if (set_nonblocking(*fd) < 0) {
*error = errno;
return QERROR;
}
dstaddr.sin_family = AF_INET;
dstaddr.sin_port = htons(port);
if (inet_aton(addr, &dstaddr.sin_addr) == 0) {
qerror("invalid bind address");
*error = errno;
close(*fd);
return QERROR;
}
len = sizeof(dstaddr);
while (1) {
if (connect(*fd, (struct sockaddr*)(&dstaddr), len) == -1) {
if (errno == EINTR) {
continue;
}
if (errno == EINPROGRESS) {
*error = errno;
return QNONBLOCKING;
}
if (errno == EISCONN) {
return QOK;
}
qerror("connect to %s:%d error: %s", addr, port, strerror(errno));
close(*fd);
return QERROR;
}
break;
}
return QOK;
}
示例13: test_connect
static void test_connect(struct network_test *t)
{
int s;
struct sockaddr_in s_in;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
err(1, "socket()");
t->nt_s = s;
prepare_ctl(t);
if (t->nt_proto == TEST_TCP) {
int off = 0;
if (tcpcryptd_setsockopt(&t->nt_ctl, TCP_CRYPT_ENABLE, &off,
sizeof(off)) == -1)
errx(1, "tcpcryptd_setsockopt()");
} else {
int one = 1;
assert(t->nt_proto == TEST_CRYPT);
if (tcpcryptd_setsockopt(&t->nt_ctl, TCP_CRYPT_NOCACHE, &one,
sizeof(one)) == -1)
errx(1, "tcpcryptd_setsockopt()");
}
set_nonblocking(s);
memset(&s_in, 0, sizeof(s_in));
s_in.sin_family = AF_INET;
s_in.sin_port = htons(t->nt_port);
s_in.sin_addr = _state.s_nt_ip;
if (connect(s, (struct sockaddr*) &s_in, sizeof(s_in)) == -1) {
#ifdef __WIN32__
if (WSAGetLastError() != WSAEWOULDBLOCK)
#else
if (errno != EINPROGRESS)
#endif
err(1, "connect()");
}
t->nt_ctl.tcc_dst = s_in.sin_addr;
t->nt_ctl.tcc_dport = s_in.sin_port;
t->nt_state = TEST_STATE_CONNECTING;
t->nt_start = time(NULL);
}
示例14: connection_handler
static void connection_handler(ioloop_t *loop, int listen_fd, unsigned int events, void *args) {
socklen_t addr_len;
int conn_fd;
struct sockaddr_in remo_addr;
iostream_t *stream;
// -------- Accepting connection ----------------------------
debug("Accepting new connection...");
addr_len = sizeof(struct sockaddr_in);
conn_fd = accept(listen_fd, (struct sockaddr*) &remo_addr, &addr_len);
debug("Connection fd: %d...", conn_fd);
if (conn_fd == -1) {
error("Error accepting new connection");
return;
}
if (set_nonblocking(conn_fd)) {
error("Error configuring Non-blocking");
return;
}
stream = iostream_create(loop, conn_fd, 1024, 1024, NULL);
iostream_set_close_handler(stream, connection_close_handler);
switch(mode) {
case 0:
error("Testing read 16 bytes");
iostream_read_bytes(stream, 16, read_bytes, NULL);
break;
case 1:
error("Testing read_until two blank lines(\\n\\n)");
iostream_read_until(stream, "\r\n\r\n", read_headers);
break;
case 2:
error("Testing writing dummy data");
write_texts(stream);
break;
case 3:
error("Testing sending file");
send_file(stream);
break;
default:
error("Unknown mode: read_until two blank lines(\\n)");
iostream_read_until(stream, "\r\n\r\n", read_headers);
break;
}
}
示例15: my_write
static VALUE my_write(VALUE io, VALUE str, int io_wait)
{
struct wr_args a;
long n;
prepare_write(&a, io, str);
set_nonblocking(a.fd);
retry:
n = (long)write(a.fd, a.ptr, a.len);
if (write_check(&a, n, "write", io_wait) != 0)
goto retry;
if (TYPE(a.buf) != T_SYMBOL)
kgio_autopush_write(io);
return a.buf;
}