static void force_close(struct socket_server *ss, struct socket *s, struct socket_message *result) { result->id = s->id; result->ud = 0; result->data = NULL; result->opaque = s->opaque; if (s->type == SOCKET_TYPE_INVALID) { return; } assert(s->type != SOCKET_TYPE_RESERVE); struct write_buffer *wb = s->head; while (wb) { struct write_buffer *tmp = wb; wb = wb->next; FREE(tmp->buffer); FREE(tmp); } s->head = s->tail = NULL; if (s->type != SOCKET_TYPE_PACCEPT && s->type != SOCKET_TYPE_PLISTEN) { sp_del(ss->event_fd, s->fd); } if (s->type != SOCKET_TYPE_BIND) { close(s->fd); } s->type = SOCKET_TYPE_INVALID; }
// 强制关闭 static void force_close(struct socket_server *ss, struct socket *s, struct socket_message *result) { result->id = s->id; result->ud = 0; result->data = NULL; result->opaque = s->opaque; if (s->type == SOCKET_TYPE_INVALID) { // 如果是初始类型则直接返回 return; } // 保留套接字不可被强制关闭 assert(s->type != SOCKET_TYPE_RESERVE); // 清空写缓冲链表 free_wb_list(ss,&s->high); free_wb_list(ss,&s->low); if (s->type != SOCKET_TYPE_PACCEPT && s->type != SOCKET_TYPE_PLISTEN) { // 已加入事件循环管理的套接字,从事件循环管理中移除 sp_del(ss->event_fd, s->fd); } if (s->type != SOCKET_TYPE_BIND) { // 如果是正常socket套接字,执行close关闭套接字 if (close(s->fd) < 0) { perror("close socket:"); } } // 修改套接字为初始类型 s->type = SOCKET_TYPE_INVALID; }
//强制关闭套接字 void socket_proxy::force_close() { if (this->type == SOCKET_TYPE_INVALID) { return; } assert(this->type != SOCKET_TYPE_RESERVE); //销毁发送缓冲区 struct write_buffer *wb = this->head; while (wb) { struct write_buffer *tmp = wb; wb = wb->next; FREE(tmp->buffer); FREE(tmp); } this->head = this->tail = NULL; if (this->type != SOCKET_TYPE_PACCEPT && this->type != SOCKET_TYPE_PLISTEN) { sp_del(this->ss->event_fd, this->fd); //epoll取消关注该套接字 } //SOCKET_TYPE_BIN类型不需要close(比如stdin,stdout等),其它socket类型需要close if (this->type != SOCKET_TYPE_BIND) { close(this->fd); } this->type = SOCKET_TYPE_INVALID; }
static void force_close(struct socket_server *ss, struct socket *s, struct socket_message *result) { result->id = s->id; result->ud = 0; result->data = NULL; result->opaque = s->opaque; if (s->type == SOCKET_TYPE_INVALID) { return; } assert(s->type != SOCKET_TYPE_RESERVE); struct write_buffer *wb = s->head; while (wb) { struct write_buffer *tmp = wb; wb = wb->next; FREE(tmp->buffer); // 释放节点指向的内存 FREE(tmp); // 释放节点本身 } s->head = s->tail = NULL; // 强制关闭的时候 如果type不为SOCKET_TYPE_PACCEPT SOCKET_TYPE_PACCEPT这2个是没有加入epoll管理的 if (s->type != SOCKET_TYPE_PACCEPT && s->type != SOCKET_TYPE_PACCEPT) { sp_del(ss->event_fd, s->fd); // 从epoll中取消关注fd } if (s->type != SOCKET_TYPE_BIND) { close(s->fd); } s->type = SOCKET_TYPE_INVALID; }
static void delsocket(struct silly_socket *ss, struct socket *s) { if (s->type == STYPE_RESERVE) { const char *fmt = "[socket] delsocket sid:%d error type:%d\n"; fprintf(stderr, fmt, s->sid, s->type); return ; } sp_del(ss->spfd, s->fd); close(s->fd); freesocket(ss, s); return ; }
static void force_close(struct socket_server *ss, struct socket *s) { if (s->type == SOCKET_TYPE_INVALID) { return; } struct write_buffer *wb = s->head; while (wb) { struct write_buffer *tmp = wb; wb = wb->next; FREE(tmp->buffer); FREE(tmp); } s->head = s->tail = NULL; sp_del(ss->event_fd, s->fd); if (s->type != SOCKET_TYPE_BIND) { close(s->fd); } s->type = SOCKET_TYPE_INVALID; }
static void force_close(struct socket_server *ss, struct socket *s, struct socket_message *result) { result->id = s->id; result->ud = 0; result->data = NULL; result->opaque = s->opaque; if (s->type == SOCKET_TYPE_INVALID) { return; } assert(s->type != SOCKET_TYPE_RESERVE); free_wb_list(&s->high); free_wb_list(&s->low); if (s->type != SOCKET_TYPE_PACCEPT && s->type != SOCKET_TYPE_PLISTEN) { sp_del(ss->event_fd, s->fd); } if (s->type != SOCKET_TYPE_BIND) { close(s->fd); } s->type = SOCKET_TYPE_INVALID; }