本文整理汇总了C++中poll函数的典型用法代码示例。如果您正苦于以下问题:C++ poll函数的具体用法?C++ poll怎么用?C++ poll使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了poll函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: console_thread_func
void* console_thread_func(void*)
{
/*Start logs*/
while(bConsoleActive)
{
while(!qConsoleMessages.empty())
{
console_msg front_msg = qConsoleMessages.front();
gdk_threads_enter();
GtkTextIter console_end_iter;
gtk_text_buffer_get_end_iter(console_buffer, &console_end_iter); /*Get end of console*/
if(front_msg.type != important_online && front_msg.type != important_offline && front_msg.type != motd){
gtk_text_buffer_insert_with_tags(console_buffer, &console_end_iter, front_msg.from_who.c_str(), strlen(front_msg.from_who.c_str()), console_format_normal, NULL);
gtk_text_buffer_get_end_iter(console_buffer, &console_end_iter); /*Get end again*/
gtk_text_buffer_insert_with_tags(console_buffer, &console_end_iter, ": ", strlen(": "), console_format_normal, NULL);/*Formating*/
gtk_text_buffer_get_end_iter(console_buffer, &console_end_iter);
}
switch(front_msg.type){ //from_who uses console_format_normal
case notification:
gtk_text_buffer_insert_with_tags(console_buffer, &console_end_iter, front_msg.strMessage.c_str(), strlen(front_msg.strMessage.c_str()), console_format_notification, NULL);
break;
case error:
gtk_text_buffer_insert_with_tags(console_buffer, &console_end_iter, front_msg.strMessage.c_str(), strlen(front_msg.strMessage.c_str()), console_format_error, NULL);
break;
case warning:
gtk_text_buffer_insert_with_tags(console_buffer, &console_end_iter, front_msg.strMessage.c_str(), strlen(front_msg.strMessage.c_str()), console_format_warning, NULL);
break;
case important_online:
gtk_text_buffer_insert_with_tags(console_buffer, &console_end_iter, front_msg.strMessage.c_str(), strlen(front_msg.strMessage.c_str()), console_format_important_online, NULL);
break;
case important_offline:
gtk_text_buffer_insert_with_tags(console_buffer, &console_end_iter, front_msg.strMessage.c_str(), strlen(front_msg.strMessage.c_str()), console_format_important_offline, NULL);
break;
case motd:
gtk_text_buffer_insert_with_tags(console_buffer, &console_end_iter, front_msg.strMessage.c_str(), strlen(front_msg.strMessage.c_str()), console_format_motd, NULL);
gtk_text_buffer_get_end_iter(console_buffer, &console_end_iter);
gtk_text_buffer_insert(console_buffer, &console_end_iter, "\n", strlen("\n"));
break;
}
gtk_text_buffer_get_end_iter(console_buffer, &console_end_iter);
gtk_text_buffer_insert(console_buffer, &console_end_iter, "\n", strlen("\n"));
gtk_text_buffer_get_end_iter(console_buffer, &console_end_iter);
gtk_text_view_scroll_to_iter((GtkTextView*)console_text, &console_end_iter, 0, 0, 0, 0);
gdk_threads_leave();
qConsoleMessages.pop(); //pop 'em
poll(0,0, CONSOLE_DELAY); /*Don't want it to spout all out and shit*/
}
}
/*Clean up and save logs here*/
return NULL;
}
开发者ID:kevintrinh,项目名称:softserver,代码行数:73,代码来源:main.cpp
示例2: manager_output_loop
static void *
manager_output_loop( DirectThread *thread, void *arg )
{
int len;
struct pollfd pf;
VoodooManager *manager = arg;
while (!manager->quit) {
D_MAGIC_ASSERT( manager, VoodooManager );
pf.fd = manager->fd;
pf.events = POLLOUT;
switch (poll( &pf, 1, 100 )) {
case -1:
if (errno != EINTR) {
D_PERROR( "Voodoo/Output: Could not poll() the socket!\n" );
usleep( 200000 );
}
/* fall through */
case 0:
continue;
}
pthread_mutex_lock( &manager->output.lock );
while (manager->output.start == manager->output.end) {
struct timeval now;
struct timespec timeout;
D_ASSUME( manager->output.start == 0 );
D_ASSUME( manager->output.end == 0 );
gettimeofday( &now, NULL );
timeout.tv_sec = now.tv_sec;
timeout.tv_nsec = (now.tv_usec + 50000) * 1000;
timeout.tv_sec += timeout.tv_nsec / 1000000000;
timeout.tv_nsec %= 1000000000;
pthread_cond_timedwait( &manager->output.wait, &manager->output.lock, &timeout );
if (manager->quit)
break;
}
if (!manager->quit) {
len = send( manager->fd, manager->output.buffer + manager->output.start,
manager->output.end - manager->output.start, MSG_DONTWAIT );
if (len < 0) {
switch (errno) {
case EINTR:
case EAGAIN:
break;
default:
D_PERROR( "Voodoo/Output: Could not send() data!\n" );
usleep( 200000 );
}
}
else {
D_DEBUG( "Voodoo/Output: Sent %d/%d bytes...\n", len, manager->output.end - manager->output.start );
manager->output.start += len;
if (manager->output.start == manager->output.end) {
manager->output.start = manager->output.end = 0;
pthread_cond_broadcast( &manager->output.wait );
}
}
}
pthread_mutex_unlock( &manager->output.lock );
}
return NULL;
}
开发者ID:kizukukoto,项目名称:WDN900_GPL,代码行数:79,代码来源:manager.c
示例3: smcp_plat_process
smcp_status_t
smcp_plat_process(
smcp_t self
) {
SMCP_EMBEDDED_SELF_HOOK;
smcp_status_t ret = 0;
int tmp;
struct pollfd polls[4];
int poll_count;
poll_count = smcp_plat_update_pollfds(self, polls, sizeof(polls)/sizeof(polls[0]));
if (poll_count > (int)(sizeof(polls)/sizeof(*polls))) {
poll_count = sizeof(polls)/sizeof(*polls);
}
errno = 0;
tmp = poll(polls, poll_count, 0);
// Ensure that poll did not fail with an error.
require_action_string(
errno == 0,
bail,
ret = SMCP_STATUS_ERRNO,
strerror(errno)
);
if(tmp > 0) {
for (tmp = 0; tmp < poll_count; tmp++) {
if (!polls[tmp].revents) {
continue;
} else {
char packet[SMCP_MAX_PACKET_LENGTH+1];
smcp_sockaddr_t remote_saddr = {};
smcp_sockaddr_t local_saddr = {};
ssize_t packet_len = 0;
char cmbuf[0x100];
struct iovec iov = { packet, SMCP_MAX_PACKET_LENGTH };
struct msghdr msg = {
.msg_name = &remote_saddr,
.msg_namelen = sizeof(remote_saddr),
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cmbuf,
.msg_controllen = sizeof(cmbuf),
};
struct cmsghdr *cmsg;
packet_len = recvmsg(polls[tmp].fd, &msg, 0);
require_action(packet_len > 0, bail, ret = SMCP_STATUS_ERRNO);
packet[packet_len] = 0;
for (
cmsg = CMSG_FIRSTHDR(&msg);
cmsg != NULL;
cmsg = CMSG_NXTHDR(&msg, cmsg)
) {
if (cmsg->cmsg_level != SMCP_IPPROTO
|| cmsg->cmsg_type != SMCP_PKTINFO
) {
continue;
}
// Preinitialize some of the fields.
local_saddr = remote_saddr;
#if SMCP_BSD_SOCKETS_NET_FAMILY==AF_INET6
struct in6_pktinfo *pi = (struct in6_pktinfo *)CMSG_DATA(cmsg);
local_saddr.smcp_addr = pi->ipi6_addr;
local_saddr.sin6_scope_id = pi->ipi6_ifindex;
#elif SMCP_BSD_SOCKETS_NET_FAMILY==AF_INET
struct in_pktinfo *pi = (struct in_pktinfo *)CMSG_DATA(cmsg);
local_saddr.smcp_addr = pi->ipi_addr;
#endif
local_saddr.smcp_port = htons(get_port_for_fd(polls[tmp].fd));
self->plat.pktinfo = *pi;
}
smcp_set_current_instance(self);
smcp_plat_set_remote_sockaddr(&remote_saddr);
smcp_plat_set_local_sockaddr(&local_saddr);
if (self->plat.fd_udp == polls[tmp].fd) {
smcp_plat_set_session_type(SMCP_SESSION_TYPE_UDP);
ret = smcp_inbound_packet_process(self, packet, (coap_size_t)packet_len, 0);
require_noerr(ret, bail);
#if SMCP_DTLS
} else if (self->plat.fd_dtls == polls[tmp].fd) {
smcp_plat_set_session_type(SMCP_SESSION_TYPE_DTLS);
smcp_plat_ssl_inbound_packet_process(
self,
//.........这里部分代码省略.........
开发者ID:paulobrizolara,项目名称:smcp,代码行数:101,代码来源:smcp-plat-bsd.c
示例4: add_console_msg
void *listen_thread_func(void* args) //TCP's thread
{
bThreadActive[0] = true;
thread_args* my_args = (thread_args*) args;
add_console_msg("[Listen]",notification, "Thread has started");
add_console_msg("[Listen]",notification, "Doing safe cleanup of Sockets and Clients");
if(rgSockets.size() > 0)
{
rgSockets.clear();
}
if(rgClients.size() > 0 )
{
rgClients.clear();
}
if(rgAccounts.size() > 0)
{
rgAccounts.clear();
}
pollfd MasterSocket;
MasterSocket.fd = uiMasterSocketTCP;
MasterSocket.events = POLLIN;
rgSockets.push_back(MasterSocket);
soft_client MasterClient(server_fill, uiMasterSocketTCP, uiMasterSocketUDP);
rgClients.push_back(MasterClient);
soft_account MasterAccount;
MasterClient.set_account(MasterAccount);
rgAccounts.push_back(MasterAccount);
int PollActivity;
pollfd* ptrPollSockets;
add_console_msg("[Listen]",notification, "Poll is ready to read incoming connections");
while(bServerOnline)
{
ptrPollSockets = &rgSockets[0];
while(bServerOnline)
{
PollActivity = poll(ptrPollSockets, rgSockets.size(), POLL_DELAY);
if(PollActivity !=0) break;
}
if(PollActivity < 0)
{
perror("tcp-poll");
add_console_msg("[Listen]",warning, "Could not poll sockets");
}
if(rgSockets[0].revents & POLLIN) //Server received a connection
{
unsigned int uiNewSocket;
struct sockaddr_storage SNewClientAddr;
socklen_t iNewClientAddr_Size = sizeof SNewClientAddr;
if((uiNewSocket = accept(uiMasterSocketTCP, (struct sockaddr*)&SNewClientAddr, &iNewClientAddr_Size)) < 0)
{
perror("tcp-accept");
add_console_msg("[Listen]", warning, "Failed to accept client");
}
else
{
add_console_msg("[Listen]", notification, "A client has successfully connected"); //Remove this later
/*Create a pollfd for new socket*/
pollfd NewSocket;
NewSocket.fd = uiNewSocket;
NewSocket.events = POLLIN;
rgSockets.push_back(NewSocket);
/*Create a new client for socket*/
soft_client NewClient(uiNewSocket, SNewClientAddr); //takes TCP socket and udp address and port
soft_account InactiveAccount; //Creates a blank account with inactivityand databaseID equals to 0
NewClient.set_account(InactiveAccount); //This does not increment accounts logged in. You must LOG in to do so.
rgClients.push_back(NewClient);
rgAccounts.push_back(InactiveAccount);
change_status_msg("<span foreground='green'><b>Online</b></span> <b>"+convert_to_str(rgSockets.size()-1)+"</b> user(s) connected");
/*Add to Address Watchlist*/
rgAddressWatch.push_back(SNewClientAddr);
}
}
for(unsigned int i=1; i<rgSockets.size(); i++)
{
if(i != rgClients[i].uiPosition) rgClients[i].uiPosition = i; //Take uiPosition
if(rgSockets[i].revents & POLLIN)
{
int read_val;
char chBuffer[50];
if((read_val = (recv(rgSockets[i].fd, chBuffer, 50, 0))) !=0)
{
/*Players get linked to an account here*/
//.........这里部分代码省略.........
开发者ID:kevintrinh,项目名称:softserver,代码行数:101,代码来源:main.cpp
示例5: read_gps_mtk
int read_gps_mtk(int *fd, char *gps_rx_buffer, int buffer_size) // returns 1 if the thread should terminate
{
// printf("in read_gps_mtk\n");
uint8_t ret = 0;
uint8_t c;
int rx_count = 0;
int gpsRxOverflow = 0;
struct pollfd fds;
fds.fd = *fd;
fds.events = POLLIN;
// This blocks the task until there is something on the buffer
while (1) {
//check if the thread should terminate
if (terminate_gps_thread == true) {
// printf("terminate_gps_thread=%u ", terminate_gps_thread);
// printf("exiting mtk thread\n");
// fflush(stdout);
ret = 1;
break;
}
if (poll(&fds, 1, 1000) > 0) {
if (read(*fd, &c, 1) > 0) {
// printf("Read %x\n",c);
if (rx_count >= buffer_size) {
// The buffer is already full and we haven't found a valid NMEA sentence.
// Flush the buffer and note the overflow event.
gpsRxOverflow++;
rx_count = 0;
mtk_decode_init();
if (gps_verbose) printf("[gps] Buffer full\r\n");
} else {
//gps_rx_buffer[rx_count] = c;
rx_count++;
}
int msg_read = mtk_parse(c, gps_rx_buffer);
if (msg_read > 0) {
// printf("Found sequence\n");
break;
}
} else {
break;
}
} else {
break;
}
}
return ret;
}
开发者ID:aqakulov,项目名称:Firmware,代码行数:62,代码来源:mtk.c
示例6: zmq_poll
int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
{
if (!items_) {
errno = EFAULT;
return -1;
}
#if defined ZMQ_POLL_BASED_ON_POLL
if (unlikely (nitems_ < 0)) {
errno = EINVAL;
return -1;
}
if (unlikely (nitems_ == 0)) {
if (timeout_ == 0)
return 0;
#if defined ZMQ_HAVE_WINDOWS
Sleep (timeout_ > 0 ? timeout_ : INFINITE);
return 0;
#elif defined ZMQ_HAVE_ANDROID
usleep (timeout_ * 1000);
return 0;
#else
return usleep (timeout_ * 1000);
#endif
}
zmq::clock_t clock;
uint64_t now = 0;
uint64_t end = 0;
pollfd *pollfds = (pollfd*) malloc (nitems_ * sizeof (pollfd));
alloc_assert (pollfds);
// Build pollset for poll () system call.
for (int i = 0; i != nitems_; i++) {
// If the poll item is a 0MQ socket, we poll on the file descriptor
// retrieved by the ZMQ_FD socket option.
if (items_ [i].socket) {
size_t zmq_fd_size = sizeof (zmq::fd_t);
if (zmq_getsockopt (items_ [i].socket, ZMQ_FD, &pollfds [i].fd,
&zmq_fd_size) == -1) {
free (pollfds);
return -1;
}
pollfds [i].events = items_ [i].events ? POLLIN : 0;
}
// Else, the poll item is a raw file descriptor. Just convert the
// events to normal POLLIN/POLLOUT for poll ().
else {
pollfds [i].fd = items_ [i].fd;
pollfds [i].events =
(items_ [i].events & ZMQ_POLLIN ? POLLIN : 0) |
(items_ [i].events & ZMQ_POLLOUT ? POLLOUT : 0);
}
}
bool first_pass = true;
int nevents = 0;
while (true) {
// Compute the timeout for the subsequent poll.
int timeout;
if (first_pass)
timeout = 0;
else if (timeout_ < 0)
timeout = -1;
else
timeout = end - now;
// Wait for events.
while (true) {
int rc = poll (pollfds, nitems_, timeout);
if (rc == -1 && errno == EINTR) {
free (pollfds);
return -1;
}
errno_assert (rc >= 0);
break;
}
// Check for the events.
for (int i = 0; i != nitems_; i++) {
items_ [i].revents = 0;
// The poll item is a 0MQ socket. Retrieve pending events
// using the ZMQ_EVENTS socket option.
if (items_ [i].socket) {
size_t zmq_events_size = sizeof (uint32_t);
uint32_t zmq_events;
if (zmq_getsockopt (items_ [i].socket, ZMQ_EVENTS, &zmq_events,
&zmq_events_size) == -1) {
free (pollfds);
return -1;
}
if ((items_ [i].events & ZMQ_POLLOUT) &&
(zmq_events & ZMQ_POLLOUT))
items_ [i].revents |= ZMQ_POLLOUT;
if ((items_ [i].events & ZMQ_POLLIN) &&
(zmq_events & ZMQ_POLLIN))
//.........这里部分代码省略.........
开发者ID:DeadZen,项目名称:CloudI,代码行数:101,代码来源:zmq.cpp
示例7: tls_do_handshake
int tls_do_handshake(rdpTls* tls, BOOL clientMode)
{
CryptoCert cert;
int verify_status, status;
do
{
#ifdef HAVE_POLL_H
struct pollfd pollfds;
#else
struct timeval tv;
fd_set rset;
#endif
int fd;
status = BIO_do_handshake(tls->bio);
if (status == 1)
break;
if (!BIO_should_retry(tls->bio))
return -1;
/* we select() only for read even if we should test both read and write
* depending of what have blocked */
fd = BIO_get_fd(tls->bio, NULL);
if (fd < 0)
{
WLog_ERR(TAG, "unable to retrieve BIO fd");
return -1;
}
#ifdef HAVE_POLL_H
pollfds.fd = fd;
pollfds.events = POLLIN;
pollfds.revents = 0;
do
{
status = poll(&pollfds, 1, 10 * 1000);
}
while ((status < 0) && (errno == EINTR));
#else
FD_ZERO(&rset);
FD_SET(fd, &rset);
tv.tv_sec = 0;
tv.tv_usec = 10 * 1000; /* 10ms */
status = _select(fd + 1, &rset, NULL, NULL, &tv);
#endif
if (status < 0)
{
WLog_ERR(TAG, "error during select()");
return -1;
}
}
while (TRUE);
cert = tls_get_certificate(tls, clientMode);
if (!cert)
{
WLog_ERR(TAG, "tls_get_certificate failed to return the server certificate.");
return -1;
}
tls->Bindings = tls_get_channel_bindings(cert->px509);
if (!tls->Bindings)
{
WLog_ERR(TAG, "unable to retrieve bindings");
verify_status = -1;
goto out;
}
if (!crypto_cert_get_public_key(cert, &tls->PublicKey, &tls->PublicKeyLength))
{
WLog_ERR(TAG, "crypto_cert_get_public_key failed to return the server public key.");
verify_status = -1;
goto out;
}
/* Note: server-side NLA needs public keys (keys from us, the server) but no
* certificate verify
*/
verify_status = 1;
if (clientMode)
{
verify_status = tls_verify_certificate(tls, cert, tls->hostname, tls->port);
if (verify_status < 1)
{
WLog_ERR(TAG, "certificate not trusted, aborting.");
tls_disconnect(tls);
verify_status = 0;
}
}
out:
tls_free_certificate(cert);
//.........这里部分代码省略.........
开发者ID:MrRecovery,项目名称:FreeRDP,代码行数:101,代码来源:tls.c
示例8: lgssd_run
void
lgssd_run()
{
int ret;
struct sigaction dn_act;
int fd;
time_t child_check = 0;
pid_t child_pid;
/* Taken from linux/Documentation/dnotify.txt: */
dn_act.sa_sigaction = dir_notify_handler;
sigemptyset(&dn_act.sa_mask);
dn_act.sa_flags = SA_SIGINFO;
sigaction(DNOTIFY_SIGNAL, &dn_act, NULL);
if ((fd = open(pipefs_dir, O_RDONLY)) == -1) {
printerr(0, "ERROR: failed to open %s: %s\n",
pipefs_dir, strerror(errno));
return;
}
fcntl(fd, F_SETSIG, DNOTIFY_SIGNAL);
fcntl(fd, F_NOTIFY, DN_CREATE|DN_DELETE|DN_MODIFY|DN_MULTISHOT);
init_client_list();
while (1) {
while (dir_changed) {
dir_changed = 0;
printerr(2, "pipefs root dir changed\n");
if (update_client_list()) {
printerr(0, "ERROR: couldn't update "
"client list\n");
goto out;
}
}
/* every 5s cleanup possible zombies of child processes */
if (time(NULL) - child_check >= 5) {
printerr(3, "check zombie children...\n");
while (1) {
child_pid = waitpid(-1, NULL, WNOHANG);
if (child_pid <= 0)
break;
printerr(2, "terminate zombie child: %d\n",
child_pid);
}
child_check = time(NULL);
}
/* race condition here: dir_changed could be set before we
* enter the poll, and we'd never notice if it weren't for the
* timeout. */
ret = poll(pollarray, pollsize, POLL_MILLISECS);
if (ret < 0) {
if (errno != EINTR)
printerr(0,
"WARNING: error return from poll\n");
} else if (ret == 0) {
/* timeout */
} else { /* ret > 0 */
scan_poll_results(ret);
}
}
out:
close(fd);
return;
}
开发者ID:Xyratex,项目名称:lustre-stable,代码行数:70,代码来源:gssd_main_loop.c
示例9: ev_get
int ev_get(int timeout_ms)
{
int r, i;
r = poll(ev_fds, ev_count, timeout_ms);
if (r <= 0)
return -1;
for (i=0;i<ev_count;i++) {
if ((ev_fds[i].revents & POLLIN) == 0)
continue;
if (ev_type[i] == EV_TYPE_KEYBOARD) {
struct input_event ev;
r = read(ev_fds[i].fd, &ev, sizeof(ev));
fprintf(stderr, "keyboard event: (%x,%x,%x)\n", ev.type, ev.code, ev.value);
if(r == sizeof(ev)) {
/* POWER key */
if ((ev.type == EV_KEY) && (ev.code == EV_POWER_KEY_CODE) && (ev.value == EV_KEY_VALUE_DOWN))
return EVENT_POWER_KEY_DOWN;
if ((ev.type == EV_KEY) && (ev.code == EV_POWER_KEY_CODE) && (ev.value == EV_KEY_VALUE_UP))
return EVENT_POWER_KEY_UP;
/* VOLUMEDOWN key */
if ((ev.type == EV_KEY) && (ev.code == EV_VOLUMEDOWN_KEY_CODE) && (ev.value == EV_KEY_VALUE_DOWN))
return EVENT_VOLUMEDOWN_KEY_DOWN;
if ((ev.type == EV_KEY) && (ev.code == EV_VOLUMEDOWN_KEY_CODE) && (ev.value == EV_KEY_VALUE_UP))
return EVENT_VOLUMEDOWN_KEY_UP;
/* VOLUMEUP key */
if ((ev.type == EV_KEY) && (ev.code == EV_VOLUMEUP_KEY_CODE) && (ev.value == EV_KEY_VALUE_DOWN))
return EVENT_VOLUMEUP_KEY_DOWN;
if ((ev.type == EV_KEY) && (ev.code == EV_VOLUMEUP_KEY_CODE) && (ev.value == EV_KEY_VALUE_UP))
return EVENT_VOLUMEUP_KEY_UP;
/* CAMERA key */
if ((ev.type == EV_KEY) && (ev.code == EV_CAMERA_KEY_CODE) && (ev.value == EV_KEY_VALUE_DOWN))
return EVENT_CAMERA_KEY_DOWN;
if ((ev.type == EV_KEY) && (ev.code == EV_CAMERA_KEY_CODE) && (ev.value == EV_KEY_VALUE_UP))
return EVENT_CAMERA_KEY_UP;
return -1;
}
} else if (ev_type[i] == EV_TYPE_UEVENT) {
char msg[1024];
while ((r = recv(ev_fds[i].fd, msg, sizeof(msg), 0)) > 0)
;
if(strstr(msg, CHARGER_DRIVER))
{
ALOGD("pm8921_battery UEVENT msg : %s\n", msg);
return EVENT_BATTERY;
}
}
}
return -1;
}
开发者ID:5idaidai,项目名称:android_device_xiaomi_qcom-common,代码行数:61,代码来源:events.c
示例10: Curl_socket_ready
/*
* This is an internal function used for waiting for read or write
* events on a pair of file descriptors. It uses poll() when a fine
* poll() is available, in order to avoid limits with FD_SETSIZE,
* otherwise select() is used. An error is returned if select() is
* being used and a file descriptor is too large for FD_SETSIZE.
* A negative timeout value makes this function wait indefinitely,
* unles no valid file descriptor is given, when this happens the
* negative timeout is ignored and the function times out immediately.
* When compiled with CURL_ACKNOWLEDGE_EINTR defined, EINTR condition
* is honored and function might exit early without awaiting timeout,
* otherwise EINTR will be ignored.
*
* Return values:
* -1 = system call error or fd >= FD_SETSIZE
* 0 = timeout
* CURL_CSELECT_IN | CURL_CSELECT_OUT | CURL_CSELECT_ERR
*/
int Curl_socket_ready(curl_socket_t readfd, curl_socket_t writefd,
int timeout_ms)
{
#ifdef HAVE_POLL_FINE
struct pollfd pfd[2];
int num;
#else
struct timeval pending_tv;
struct timeval *ptimeout;
fd_set fds_read;
fd_set fds_write;
fd_set fds_err;
curl_socket_t maxfd;
#endif
struct timeval initial_tv = {0,0};
int pending_ms = 0;
int error;
int r;
int ret;
if((readfd == CURL_SOCKET_BAD) && (writefd == CURL_SOCKET_BAD)) {
r = wait_ms(timeout_ms);
return r;
}
/* Avoid initial timestamp, avoid gettimeofday() call, when elapsed
time in this function does not need to be measured. This happens
when function is called with a zero timeout or a negative timeout
value indicating a blocking call should be performed. */
if(timeout_ms > 0) {
pending_ms = timeout_ms;
initial_tv = curlx_tvnow();
}
#ifdef HAVE_POLL_FINE
num = 0;
if(readfd != CURL_SOCKET_BAD) {
pfd[num].fd = readfd;
pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
pfd[num].revents = 0;
num++;
}
if(writefd != CURL_SOCKET_BAD) {
pfd[num].fd = writefd;
pfd[num].events = POLLWRNORM|POLLOUT;
pfd[num].revents = 0;
num++;
}
do {
if(timeout_ms < 0)
pending_ms = -1;
else if(!timeout_ms)
pending_ms = 0;
r = poll(pfd, num, pending_ms);
if(r != -1)
break;
error = SOCKERRNO;
if(error && error_not_EINTR)
break;
if(timeout_ms > 0) {
pending_ms = timeout_ms - elapsed_ms;
if(pending_ms <= 0)
break;
}
} while(r == -1);
if(r < 0)
return -1;
if(r == 0)
return 0;
ret = 0;
num = 0;
if(readfd != CURL_SOCKET_BAD) {
if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
ret |= CURL_CSELECT_IN;
if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
ret |= CURL_CSELECT_ERR;
num++;
//.........这里部分代码省略.........
开发者ID:kevinw,项目名称:curl,代码行数:101,代码来源:select.c
示例11: SE
SE(WindowHandle _handle)
: m_ev(poll(_handle) )
{
}
开发者ID:JimmyBenKlieve,项目名称:Torque6,代码行数:4,代码来源:entry.cpp
示例12: kdbus_test_chat
int kdbus_test_chat(struct kdbus_test_env *env)
{
int ret, cookie;
struct kdbus_conn *conn_a, *conn_b;
struct pollfd fds[2];
uint64_t flags;
int count;
conn_a = kdbus_hello(env->buspath, 0, NULL, 0);
conn_b = kdbus_hello(env->buspath, 0, NULL, 0);
ASSERT_RETURN(conn_a && conn_b);
flags = KDBUS_NAME_ALLOW_REPLACEMENT;
ret = kdbus_name_acquire(conn_a, "foo.bar.test", &flags);
ASSERT_RETURN(ret == 0);
ret = kdbus_name_acquire(conn_a, "foo.bar.baz", NULL);
ASSERT_RETURN(ret == 0);
flags = KDBUS_NAME_QUEUE;
ret = kdbus_name_acquire(conn_b, "foo.bar.baz", &flags);
ASSERT_RETURN(ret == 0);
ret = kdbus_name_acquire(conn_a, "foo.bar.double", NULL);
ASSERT_RETURN(ret == 0);
ret = kdbus_name_acquire(conn_a, "foo.bar.double", NULL);
ASSERT_RETURN(ret == -EALREADY);
ret = kdbus_name_release(conn_a, "foo.bar.double");
ASSERT_RETURN(ret == 0);
ret = kdbus_name_release(conn_a, "foo.bar.double");
ASSERT_RETURN(ret == -ESRCH);
ret = kdbus_list(conn_b, KDBUS_LIST_UNIQUE |
KDBUS_LIST_NAMES |
KDBUS_LIST_QUEUED |
KDBUS_LIST_ACTIVATORS);
ASSERT_RETURN(ret == 0);
ret = kdbus_add_match_empty(conn_a);
ASSERT_RETURN(ret == 0);
ret = kdbus_add_match_empty(conn_b);
ASSERT_RETURN(ret == 0);
cookie = 0;
ret = kdbus_msg_send(conn_b, NULL, 0xc0000000 | cookie, 0, 0, 0,
KDBUS_DST_ID_BROADCAST);
ASSERT_RETURN(ret == 0);
fds[0].fd = conn_a->fd;
fds[1].fd = conn_b->fd;
kdbus_printf("-- entering poll loop ...\n");
for (count = 0;; count++) {
int i, nfds = sizeof(fds) / sizeof(fds[0]);
for (i = 0; i < nfds; i++) {
fds[i].events = POLLIN | POLLPRI | POLLHUP;
fds[i].revents = 0;
}
ret = poll(fds, nfds, 3000);
ASSERT_RETURN(ret >= 0);
if (fds[0].revents & POLLIN) {
if (count > 2)
kdbus_name_release(conn_a, "foo.bar.baz");
ret = kdbus_msg_recv(conn_a, NULL, NULL);
ASSERT_RETURN(ret == 0);
ret = kdbus_msg_send(conn_a, NULL,
0xc0000000 | cookie++,
0, 0, 0, conn_b->id);
ASSERT_RETURN(ret == 0);
}
if (fds[1].revents & POLLIN) {
ret = kdbus_msg_recv(conn_b, NULL, NULL);
ASSERT_RETURN(ret == 0);
ret = kdbus_msg_send(conn_b, NULL,
0xc0000000 | cookie++,
0, 0, 0, conn_a->id);
ASSERT_RETURN(ret == 0);
}
ret = kdbus_list(conn_b, KDBUS_LIST_UNIQUE |
KDBUS_LIST_NAMES |
KDBUS_LIST_QUEUED |
KDBUS_LIST_ACTIVATORS);
ASSERT_RETURN(ret == 0);
if (count > 10)
break;
}
kdbus_printf("-- closing bus connections\n");
//.........这里部分代码省略.........
开发者ID:adhideguchi,项目名称:kdbus,代码行数:101,代码来源:test-chat.c
示例13: test
/**
* Perform some basic functional tests on the driver;
* make sure we can collect data from the sensor in polled
* and automatic modes.
*/
void
test()
{
struct range_finder_report report;
ssize_t sz;
int ret;
int fd = open(MB12XX_DEVICE_PATH, O_RDONLY);
if (fd < 0) {
err(1, "%s open failed (try 'mb12xx start' if the driver is not running", MB12XX_DEVICE_PATH);
}
/* do a simple demand read */
sz = read(fd, &report, sizeof(report));
if (sz != sizeof(report)) {
err(1, "immediate read failed");
}
warnx("single read");
warnx("measurement: %0.2f m", (double)report.distance);
warnx("time: %lld", report.timestamp);
/* start the sensor polling at 2Hz */
if (OK != ioctl(fd, SENSORIOCSPOLLRATE, 2)) {
errx(1, "failed to set 2Hz poll rate");
}
/* read the sensor 5x and report each value */
for (unsigned i = 0; i < 5; i++) {
struct pollfd fds;
/* wait for data to be ready */
fds.fd = fd;
fds.events = POLLIN;
ret = poll(&fds, 1, 2000);
if (ret != 1) {
errx(1, "timed out waiting for sensor data");
}
/* now go get it */
sz = read(fd, &report, sizeof(report));
if (sz != sizeof(report)) {
err(1, "periodic read failed");
}
warnx("periodic read %u", i);
warnx("measurement: %0.3f", (double)report.distance);
warnx("time: %lld", report.timestamp);
}
/* reset the sensor polling to default rate */
if (OK != ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT)) {
errx(1, "failed to set default poll rate");
}
errx(0, "PASS");
}
开发者ID:30rasheed,项目名称:x-VTOLdrone,代码行数:66,代码来源:mb12xx.cpp
示例14: wait_for_mysql
static int
wait_for_mysql(MYSQL *mysql, int status)
{
#ifdef __WIN__
fd_set rs, ws, es;
int res;
struct timeval tv, *timeout;
my_socket s= mysql_get_socket(mysql);
FD_ZERO(&rs);
FD_ZERO(&ws);
FD_ZERO(&es);
if (status & MYSQL_WAIT_READ)
FD_SET(s, &rs);
if (status & MYSQL_WAIT_WRITE)
FD_SET(s, &ws);
if (status & MYSQL_WAIT_EXCEPT)
FD_SET(s, &es);
if (status & MYSQL_WAIT_TIMEOUT)
{
tv.tv_sec= mysql_get_timeout_value(mysql);
tv.tv_usec= 0;
timeout= &tv;
}
else
timeout= NULL;
res= select(1, &rs, &ws, &es, timeout);
if (res == 0)
return MYSQL_WAIT_TIMEOUT;
else if (res == SOCKET_ERROR)
{
/*
In a real event framework, we should handle errors and re-try the select.
*/
return MYSQL_WAIT_TIMEOUT;
}
else
{
int status= 0;
if (FD_ISSET(s, &rs))
status|= MYSQL_WAIT_READ;
if (FD_ISSET(s, &ws))
status|= MYSQL_WAIT_WRITE;
if (FD_ISSET(s, &es))
status|= MYSQL_WAIT_EXCEPT;
return status;
}
#else
struct pollfd pfd;
int timeout;
int res;
pfd.fd= mysql_get_socket(mysql);
pfd.events=
(status & MYSQL_WAIT_READ ? POLLIN : 0) |
(status & MYSQL_WAIT_WRITE ? POLLOUT : 0) |
(status & MYSQL_WAIT_EXCEPT ? POLLPRI : 0);
if (status & MYSQL_WAIT_TIMEOUT)
timeout= 1000*mysql_get_timeout_value(mysql);
else
timeout= -1;
res= poll(&pfd, 1, timeout);
if (res == 0)
return MYSQL_WAIT_TIMEOUT;
else if (res < 0)
{
/*
In a real event framework, we should handle EINTR and re-try the poll.
*/
return MYSQL_WAIT_TIMEOUT;
}
else
{
int status= 0;
if (pfd.revents & POLLIN)
status|= MYSQL_WAIT_READ;
if (pfd.revents & POLLOUT)
status|= MYSQL_WAIT_WRITE;
if (pfd.revents & POLLPRI)
status|= MYSQL_WAIT_EXCEPT;
return status;
}
#endif
}
开发者ID:hans511002,项目名称:erydb,代码行数:83,代码来源:async_example.c
示例15: http1_wait
static enum req_fsm_nxt
http1_wait(struct sess *sp, struct worker *wrk, struct req *req)
{
int j, tmo;
struct pollfd pfd[1];
double now, when;
enum sess_close why = SC_NULL;
enum http1_status_e hs;
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
assert(req->sp == sp);
AZ(req->vcl);
AZ(req->esi_level);
AZ(isnan(sp->t_idle));
assert(isnan(req->t_first));
assert(isnan(req->t_prev));
assert(isnan(req->t_req));
tmo = (int)(1e3 * cache_param->timeout_linger);
while (1) {
pfd[0].fd = sp->fd;
pfd[0].events = POLLIN;
pfd[0].revents = 0;
j = poll(pfd, 1, tmo);
assert(j >= 0);
now = VTIM_real();
if (j != 0)
hs = HTTP1_Rx(req->htc);
else
hs = HTTP1_Complete(req->htc);
if (hs == HTTP1_COMPLETE) {
/* Got it, run with it */
if (isnan(req->t_first))
req->t_first = now;
if (isnan(req->t_req))
req->t_req = now;
req->acct.req_hdrbytes += Tlen(req->htc->rxbuf);
return (REQ_FSM_MORE);
} else if (hs == HTTP1_ERROR_EOF) {
why = SC_REM_CLOSE;
break;
} else if (hs == HTTP1_OVERFLOW) {
why = SC_RX_OVERFLOW;
break;
} else if (hs == HTTP1_ALL_WHITESPACE) {
/* Nothing but whitespace */
when = sp->t_idle + cache_param->timeout_idle;
if (when < now) {
why = SC_RX_TIMEOUT;
break;
}
when = sp->t_idle + cache_param->timeout_linger;
tmo = (int)(1e3 * (when - now));
if (when < now || tmo == 0) {
wrk->stats->sess_herd++;
SES_ReleaseReq(req);
WAIT_Enter(sp);
return (REQ_FSM_DONE);
}
} else {
/* Working on it */
if (isnan(req->t_first))
/* Record first byte received time stamp */
req->t_first = now;
when = sp->t_idle + cache_param->timeout_req;
tmo = (int)(1e3 * (when - now));
if (when < now || tmo == 0) {
why = SC_RX_TIMEOUT;
break;
}
}
}
req->acct.req_hdrbytes += Tlen(req->htc->rxbuf);
CNT_AcctLogCharge(wrk->stats, req);
SES_ReleaseReq(req);
assert(why != SC_NULL);
SES_Delete(sp, why, now);
return (REQ_FSM_DONE);
}
开发者ID:Open-Party,项目名称:Varnish-Cache,代码行数:83,代码来源:cache_http1_fsm.c
示例16: Curl_poll
/*
* This is a wrapper around poll(). If poll() does not exist, then
* select() is used instead. An error is returned if select() is
* being used and a file descriptor is too large for FD_SETSIZE.
* A negative timeout value makes this function wait indefinitely,
* unles no valid file descriptor is given, when this happens the
* negative timeout is ignored and the function times out immediately.
* When compiled with CURL_ACKNOWLEDGE_EINTR defined, EINTR condition
* is honored and function might exit early without awaiting timeout,
* otherwise EINTR will be ignored.
*
* Return values:
* -1 = system call error or fd >= FD_SETSIZE
* 0 = timeout
* N = number of structures with non zero revent fields
*/
int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms)
{
#ifndef HAVE_POLL_FINE
struct timeval pending_tv;
struct timeval *ptimeout;
fd_set fds_read;
fd_set fds_write;
fd_set fds_err;
curl_socket_t maxfd;
#endif
struct timeval initial_tv = {0,0};
bool fds_none = TRUE;
unsigned int i;
int pending_ms = 0;
int error;
int r;
if(ufds) {
for (i = 0; i < nfds; i++) {
if(ufds[i].fd != CURL_SOCKET_BAD) {
fds_none = FALSE;
break;
}
}
}
if(fds_none) {
r = wait_ms(timeout_ms);
return r;
}
/* Avoid initial timestamp, avoid gettimeofday() call, when elapsed
time in this function does not need to be measured. This happens
when function is called with a zero timeout or a negative timeout
value indicating a blocking call should be performed. */
if(timeout_ms > 0) {
pending_ms = timeout_ms;
initial_tv = curlx_tvnow();
}
#ifdef HAVE_POLL_FINE
do {
if(timeout_ms < 0)
pending_ms = -1;
else if(!timeout_ms)
pending_ms = 0;
r = poll(ufds, nfds, pending_ms);
if(r != -1)
break;
error = SOCKERRNO;
if(error && error_not_EINTR)
break;
if(timeout_ms > 0) {
pending_ms = timeout_ms - elapsed_ms;
if(pending_ms <= 0)
break;
}
} while(r == -1);
#else /* HAVE_POLL_FINE */
FD_ZERO(&fds_read);
FD_ZERO(&fds_write);
FD_ZERO(&fds_err);
maxfd = (curl_socket_t)-1;
for (i = 0; i < nfds; i++) {
ufds[i].revents = 0;
if(ufds[i].fd == CURL_SOCKET_BAD)
continue;
VERIFY_SOCK(ufds[i].fd);
if(ufds[i].events & (POLLIN|POLLOUT|POLLPRI|
POLLRDNORM|POLLWRNORM|POLLRDBAND)) {
if(ufds[i].fd > maxfd)
maxfd = ufds[i].fd;
if(ufds[i].events & (POLLRDNORM|POLLIN))
FD_SET(ufds[i].fd, &fds_read);
if(ufds[i].events & (POLLWRNORM|POLLOUT))
FD_SET(ufds[i].fd, &fds_write);
if(ufds[i].events & (POLLRDBAND|POLLPRI))
FD_SET(ufds[i].fd, &fds_err);
}
}
//.........这里部分代码省略.........
开发者ID:kevinw,项目名称:curl,代码行数:101,代码来源:select.c
示例17: loop
static void loop (int master_fd, int ignore_eof)
{
ssize_t n;
char buf [BUFSIZ];
struct pollfd fds [2];
char control_buf [BUFSIZ];
char data_buf [BUFSIZ];
int flags;
struct strbuf control;
struct strbuf data;
struct iocblk *ioc;
struct termios *term;
unsigned char msg_type;
int i;
fds [0].fd = STDIN_FILENO;
fds [0].events = POLLIN;
fds [0].revents = 0;
fds [1].fd = master_fd;
fds [1].events = POLLIN;
fds [1].revents = 0;
control.buf = control_buf;
control.maxlen = BUFSIZ;
data.buf = data_buf;
data.maxlen = BUFSIZ;
for (;;) {
if (poll ((struct pollfd *) &fds, 2, INFTIM) == -1)
err_msg ("poll failed");
if (fds [0].revents & POLLIN) {
if ((n = read (STDIN_FILENO, buf, BUFSIZ)) == -1)
err_msg ("read from stdin failed");
if (n == 0) {
if (ignore_eof) {
fds [0].events = 0;
continue;
}
else
break;
}
if (writen (master_fd, buf, n) == -1)
err_msg ("writen to pty master failed");
}
if (fds [1].revents & POLLIN) {
flags = 0;
if ((n = getmsg (master_fd, &control, &data, &flags)) == -1)
err_msg ("getmsg from pty master failed");
msg_type = control.buf [0];
switch (msg_type) {
case M_DATA:
if (writen (STDOUT_FILENO, data.buf, data.len) == -1)
err_msg ("writen to stdout failed");
break;
case M_FLUSH:
fprintf (stderr, "pckt: pty slave flushed its queues\n");
break;
case M_STOPI:
fprintf (stderr, "pckt: pty slave suspended output\n");
break;
case M_STARTI:
fprintf (stderr, "pckt: pty slave resumed output\n");
break;
case M_STOP:
fprintf (stderr, "pckt: pty slave disabled XON/XOFF "
"flow control\n");
break;
case M_START:
fprintf (stderr, "pckt: pty slave enabled XON/XOFF "
"flow control\n");
break;
case M_IOCTL:
ioc = (struct iocblk *) &data.buf [0];
switch (ioc -> ioc_cmd) {
case TCSBRK:
fprintf (stderr, "pckt: pty slave sent BREAK\n");
goto out;
case TCSETS:
case TCSETSW:
case TCSETSF:
fprintf (stderr, "pckt: pty slave changed "
"terminal attributes\n");
term = (struct termios *)
&data.buf [sizeof (struct iocblk)];
fprintf (stderr, " term.c_iflag = %04x\n",
term -> c_iflag);
fprintf (stderr, " term.c_oflag = %04x\n",
//.........这里部分代码省略.........
开发者ID:kavinsivak,项目名称:sandbox,代码行数:101,< |
请发表评论