Exemplo n.º 1
0
static struct getdns_dict_item *
getdns_dict_find_and_add(struct getdns_dict *dict, const char *key)
{
	struct getdns_dict_item *item;

	item = (struct getdns_dict_item *)
		   _getdns_rbtree_search(&(dict->root), key);

	if (!item) {
		/* add a node */
		item = GETDNS_MALLOC(dict->mf, struct getdns_dict_item);
		item->node.key = _getdns_strdup(&dict->mf, key);
		item->data.n = 0;
		_getdns_rbtree_insert(&(dict->root), (_getdns_rbnode_t *) item);
	}
Exemplo n.º 2
0
int _getdns_event_add(struct _getdns_event *ev, struct timeval *tv)
{
	printf( "event_add %p added=%d fd=%d tv=" ARG_LL "d %s%s%s", 
		ev, ev->added, ev->ev_fd, 
		(tv?(long long)tv->tv_sec*1000+(long long)tv->tv_usec/1000:-1),
		(ev->ev_events&EV_READ)?" EV_READ":"",
		(ev->ev_events&EV_WRITE)?" EV_WRITE":"",
		(ev->ev_events&EV_TIMEOUT)?" EV_TIMEOUT":"");
		
   if(ev->added)
       event_del(ev);
 /* gowri no works
 if (ev->ev_fd == -1 || find_fd(ev->ev_base, ev->ev_fd) == -1) {
	   printf("event_add failed, bad fd\n");
	   return 0;
   }
   */

	ev->is_tcp = 0;
	ev->is_signal = 0;
	ev->just_checked = 0;

	if ((ev->ev_events&(EV_READ | EV_WRITE)) && ev->ev_fd != -1) {
		BOOL b = 0;
		int t, l;
		long events = 0;
		//gprintf("\getdns_event_add %d %d\n", ev->ev_fd, events);

		if (ev->ev_base->max == ev->ev_base->cap)
			return -1;
		ev->idx = ev->ev_base->max++;
		ev->ev_base->items[ev->idx] = ev;

		if ((ev->ev_events&EV_READ))
			events |= FD_READ;
		if ((ev->ev_events&EV_WRITE))
		{
			events |= FD_CONNECT;
		    events |= FD_WRITE;
	    }

		//printf("\getdns_event_add %d read = %d write = %d %d\n", ev->ev_fd, ev->ev_events&EV_READ, ev->ev_events&EV_WRITE, events);

		l = sizeof(t);
		if(getsockopt(ev->ev_fd, SOL_SOCKET, SO_TYPE,
			(void*)&t, &l) != 0)
			log_err("getdns: getsockopt(SO_TYPE) failed: %s",
				wsa_strerror(WSAGetLastError()));
		if(t == SOCK_STREAM) {
			/* TCP socket */
			ev->is_tcp = 1;
			events |= FD_CLOSE;
			if( (ev->ev_events&EV_WRITE) )
				events |= FD_CONNECT;
			l = sizeof(b);
			if(getsockopt(ev->ev_fd, SOL_SOCKET, SO_ACCEPTCONN,
				(void*)&b, &l) != 0)
				log_err("getdns: getsockopt(SO_ACCEPTCONN) failed: %s",
					wsa_strerror(WSAGetLastError()));
			if(b) /* TCP accept socket */
				events |= FD_ACCEPT;
		}
		ev->hEvent = WSACreateEvent();
		if(ev->hEvent == WSA_INVALID_EVENT)
			log_err("getdns: WSACreateEvent failed: %s",
				wsa_strerror(WSAGetLastError()));
		/* automatically sets fd to nonblocking mode.
		 * nonblocking cannot be disabled, until wsaES(fd, NULL, 0) */
		//g printf("\nWSAEventSelect %d events %d hEvent %d\n", ev->ev_fd, events, ev->hEvent); 
		//gg if (WSAEventSelect(ev->ev_fd, ev->hEvent, FD_ACCEPT | FD_CONNECT | FD_READ | FD_CLOSE | FD_WRITE) != 0) {
		//if (WSAEventSelect(ev->ev_fd, ev->hEvent,FD_READ | FD_WRITE) != 0) {
		if (WSAEventSelect(ev->ev_fd, ev->hEvent, events) != 0) {
			log_err("getdns: WSAEventSelect in getdns failed: %s",
				wsa_strerror(WSAGetLastError()));
		}
		if(ev->is_tcp && ev->stick_events && 
			(ev->ev_events & ev->old_events)) {
			/* go to processing the sticky event right away */
			ev->ev_base->tcp_reinvigorated = 1;
		}
	}

	if(tv && (ev->ev_events&EV_TIMEOUT)) {
#ifndef S_SPLINT_S
                struct timeval *now = ev->ev_base->time_tv;
                ev->ev_timeout.tv_sec = tv->tv_sec + now->tv_sec;
                ev->ev_timeout.tv_usec = tv->tv_usec + now->tv_usec;
                while(ev->ev_timeout.tv_usec > 1000000) {
                        ev->ev_timeout.tv_usec -= 1000000;
                        ev->ev_timeout.tv_sec++;
                }
#endif
                (void)_getdns_rbtree_insert(ev->ev_base->times, &ev->node);
        }
        ev->added = 1;
	return 0;
}