conn *conn_new() { conn *c = conn_from_freelist(); if (NULL == c) { if (!(c = (conn *)calloc(1, sizeof(conn)))) { merror("connection alloc failed!"); return NULL; } } return c; }
conn *conn_new() { conn *c = conn_from_freelist(); if (NULL == c) { if (!(c = (conn *)malloc(sizeof(conn)))) { merror("connection alloc failed!"); return NULL; } } c->data = NULL; c->user = NULL; c->arg = NULL; c->state = STATE_NOT_CONNECTED; c->bev = NULL; c->refcnt = 1; pthread_mutex_init(&c->lock, NULL); c->thread = NULL; return c; }
//handle socket event void handle_connection(const int sfd, struct event_base* base) { conn *c=conn_from_freelist(); if(NULL==c) { if(!(c=(conn*)calloc(1,sizeof(conn)))) { fprintf(stderr,"calloc()\n"); return; } } c->sfd = sfd; event_set(&(c->event),sfd,EV_READ|EV_PERSIST,event_handler,(void*)c); event_base_set(base,&c->event); if(event_add(&c->event,0)==-1) { if( conn_add_to_freelist(c)) conn_free(c); perror("event_add"); return; } }
// Create a new connection value. conn *conn_new(const int sfd, enum conn_states init_state, const int event_flags, const int read_buffer_size, struct event_base *base) { conn *c = conn_from_freelist(); if (NULL == c) { if (!(c = (conn *)calloc(1, sizeof(conn)))) { fprintf(stderr, "calloc()\n"); return NULL; } c->rsize = read_buffer_size; c->wsize = DATA_BUFFER_SIZE; c->rbuf = (char *)malloc((size_t)c->rsize); c->wbuf = (char *)malloc((size_t)c->wsize); c->iovsize = IOV_LIST_INITIAL; c->iov = (struct iovec *)malloc(sizeof(struct iovec) * c->iovsize); c->msgsize = MSG_LIST_INITIAL; c->msglist = (struct msghdr *)malloc(sizeof(struct msghdr) * c->msgsize); c->isize = ITEM_LIST_INITIAL; c->ilist = (item **)malloc(sizeof(item *) * c->isize); if (c->rbuf == NULL || c->wbuf == NULL || c->iov == NULL || c->msglist == NULL || c->ilist == NULL) { conn_free(c); fprintf(stderr, "malloc()\n"); return NULL; } } c->sfd = sfd; c->state = init_state; c->after_write = init_state; c->rbytes = c->wbytes = 0; c->rcurr = c->rbuf; c->wcurr = c->wbuf; c->iovused = 0; c->msgcurr = 0; c->msgused = 0; c->ileft = 0; c->icurr = c->ilist; event_set(&c->event, sfd, event_flags, event_handler, (void *)c); event_base_set(base, &c->event); c->ev_flags = event_flags; if (event_add(&c->event, 0) == -1) { if (!conn_add_to_freelist(c)) { conn_free(c); } perror("event_add"); return NULL; } return c; }
conn *conn_new(const int sfd,enum conn_states init_state,const int event_flags, const int read_buffer_size,enum network_transport transport, struct event_base *base) { conn *c=conn_from_freelist(); if(NULL==c) { if(!(c=(conn*)calloc(1,sizeof(conn)))) { fprintf(stderr,"calloc()\n"); return NULL; } //initiate c /* { MEMCACHED_CONN_CREATE(c); c->rbuf = c->wbuf = 0; c->ilist = 0; c->suffixlist = 0; c->iov = 0; c->msglist = 0; c->hdrbuf = 0; c->rsize = read_buffer_size; c->wsize = DATA_BUFFER_SIZE; c->isize = ITEM_LIST_INITIAL; c->suffixsize = SUFFIX_LIST_INITIAL; c->iovsize = IOV_LIST_INITIAL; c->msgsize = MSG_LIST_INITIAL; c->hdrsize = 0; c->rbuf = (char *)malloc((size_t)c->rsize); c->wbuf = (char *)malloc((size_t)c->wsize); c->ilist = (item **)malloc(sizeof(item *) * c->isize); c->suffixlist = (char **)malloc(sizeof(char *) * c->suffixsize); c->iov = (struct iovec *)malloc(sizeof(struct iovec) * c->iovsize); c->msglist = (struct msghdr *)malloc(sizeof(struct msghdr) * c->msgsize); //根据配置大小来分配,可能会分配失败 if (c->rbuf == 0 || c->wbuf == 0 || c->ilist == 0 || c->iov == 0 || c->msglist == 0 || c->suffixlist == 0) { conn_free(c); fprintf(stderr, "malloc()\n"); return NULL; } //end initiate c } */ } //initiate c /* { c->transport = transport; //传输方式 c->protocol = settings.binding_protocol; //传输协议 // unix socket mode doesn't need this, so zeroed out. but why // is this done for every command? presumably for UDP // mode. if (!settings.socketpath) { c->request_addr_size = sizeof(c->request_addr); } else { c->request_addr_size = 0; } if (settings.verbose > 1) { if (init_state == conn_listening) { fprintf(stderr, "<%d server listening (%s)\n", sfd, prot_text(c->protocol)); } else if (IS_UDP(transport)) { fprintf(stderr, "<%d server listening (udp)\n", sfd); } else if (c->protocol == negotiating_prot) { fprintf(stderr, "<%d new auto-negotiating client connection\n", sfd); } else if (c->protocol == ascii_prot) { fprintf(stderr, "<%d new ascii client connection.\n", sfd); } else if (c->protocol == binary_prot) { fprintf(stderr, "<%d new binary client connection.\n", sfd); } else { fprintf(stderr, "<%d new unknown (%d) client connection\n", sfd, c->protocol); assert(false); } } //信息赋值给conn结构,conn的构造把item全用上了 c->sfd = sfd; c->state = init_state; c->rlbytes = 0; c->cmd = -1; c->rbytes = c->wbytes = 0; c->wcurr = c->wbuf; c->rcurr = c->rbuf; c->ritem = 0; c->icurr = c->ilist; c->suffixcurr = c->suffixlist; c->ileft = 0; c->suffixleft = 0; c->iovused = 0; c->msgcurr = 0; c->msgused = 0; c->write_and_go = init_state; c->write_and_free = 0; c->item = 0; c->noreply = false; } */ //end initiate c c->sfd=sfd; event_set(&c->event,sfd,event_flags,event_handler,(void*)c); event_base_set(base,&c->event); c->ev_flags=event_flags; if(event_add(&c->event,0)==-1) { if( conn_add_to_freelist(c)) conn_free(c); perror("event_add"); return NULL; } //MEMCACHED_CONN_ALLOCATE(c->sfd); return c; }
conn *conn_new(const int sfd, const int init_state, const int event_flags, const int read_buffer_size, const bool is_udp, struct event_base *base) { conn *c = conn_from_freelist(); if (NULL == c) { if (!(c = (conn *) malloc(sizeof(conn)))) { fprintf(stderr, "malloc()\n"); return NULL; } c->rbuf = c->wbuf = 0; c->suffixlist = 0; c->iov = 0; c->msglist = 0; c->hdrbuf = 0; c->rsize = read_buffer_size; c->wsize = DATA_BUFFER_SIZE; c->suffixsize = SUFFIX_LIST_INITIAL; c->iovsize = IOV_LIST_INITIAL; c->msgsize = MSG_LIST_INITIAL; c->hdrsize = 0; c->rbuf = (char *) malloc((size_t) c->rsize); c->wbuf = (char *) malloc((size_t) c->wsize); c->suffixlist = (char **) malloc(sizeof(char *) * c->suffixsize); c->iov = (struct iovec *) malloc(sizeof(struct iovec) * c->iovsize); c->msglist = (struct msghdr *) malloc(sizeof(struct msghdr) * c->msgsize); if (c->rbuf == 0 || c->wbuf == 0 || c->iov == 0 || c->msglist == 0 || c->suffixlist == 0) { if (c->rbuf != 0) free(c->rbuf); if (c->wbuf != 0) free(c->wbuf); if (c->suffixlist != 0) free(c->suffixlist); if (c->iov != 0) free(c->iov); if (c->msglist != 0) free(c->msglist); free(c); fprintf(stderr, "malloc()\n"); return NULL; } } if (settings.verbose > 1) { if (init_state == conn_listening) fprintf(stderr, "<%d server listening\n", sfd); else if (is_udp) fprintf(stderr, "<%d server listening (udp)\n", sfd); else fprintf(stderr, "<%d new client connection\n", sfd); } c->sfd = sfd; c->udp = is_udp; c->state = init_state; c->rlbytes = 0; c->rbytes = c->wbytes = 0; c->wcurr = c->wbuf; c->rcurr = c->rbuf; c->ritem = 0; c->suffixcurr = c->suffixlist; c->suffixleft = 0; c->iovused = 0; c->msgcurr = 0; c->msgused = 0; c->write_and_go = conn_read; c->write_and_free = 0; c->bucket = -1; c->gen = 0; c->noreply = false; event_set(&c->event, sfd, event_flags, event_handler, (void *) c); event_base_set(base, &c->event); c->ev_flags = event_flags; if (event_add(&c->event, 0) == -1) { if (conn_add_to_freelist(c)) { conn_free(c); } perror("event_add"); return NULL; } return c; }