void resume_in_waiting_get_connection(int loop_fd) { int k = 0; for(k = 0; k < 64; k++) { if(waiting_get_connections[k]) { cosocket_t *c*k = ((cosocket_waiting_get_connection_t *)waiting_get_connections[k])->c*k; c*k->ptr = get_connection_in_pool(loop_fd, c*k->pool_key, c*k); if(c*k->ptr) { c*k->pool_wait = NULL; delete_in_waiting_get_connection(waiting_get_connections[k]); ((se_ptr_t *) c*k->ptr)->data = c*k; c*k->status = 2; c*k->inuse = 0; c*k->reusedtimes = 1; c*k->in_read_action = 0; c*k->fd = ((se_ptr_t *) c*k->ptr)->fd; //printf("reuse %d\n", c*k->fd); delete_timeout(c*k->timeout_ptr); c*k->timeout_ptr = NULL; se_be_pri(c*k->ptr, NULL); lua_pushboolean(c*k->L, 1); lua_f_lua_uthread_resume_in_c(c*k->L, 1); return; } } } }
static void close_client(epdata_t *epd) { se_delete(epd->se_ptr); delete_timeout(epd->timeout_ptr); close(epd->fd); smp_free(epd); }
int main(void) { uint32_t n; uint32_t time; init_timeout(); time = 20; for (n=5; n<40; n++) { test_timeout(counter, n, time); } delete_timeout(); return 0; }
int cosocket_be_ssl_connected(se_ptr_t *ptr) { cosocket_t *c*k = ptr->data; if(SSL_connect(c*k->ssl)) { delete_timeout(c*k->timeout_ptr); c*k->timeout_ptr = NULL; se_be_pri(c*k->ptr, NULL); lua_pushboolean(c*k->L, 1); c*k->inuse = 0; lua_f_lua_uthread_resume_in_c(c*k->L, 1); return 1; } return 0; }
void close_client(epdata_t *epd) { if(!epd) { return; } if(epd->ssl && epd->fd > -1) { if(!SSL_shutdown(epd->ssl)) { shutdown(epd->fd, 1); SSL_shutdown(epd->ssl); } SSL_free(epd->ssl); epd->ssl = NULL; } if(epd->L) { if(epd->status == STEP_PROCESS) { LOGF(ERR, "at working!!!"); } release_lua_thread(epd->L); epd->L = NULL; } if(epd->status == STEP_READ) { serv_status.reading_counts--; } else if(epd->status == STEP_SEND) { serv_status.sending_counts--; } se_delete(epd->se_ptr); delete_timeout(epd->timeout_ptr); epd->timeout_ptr = NULL; if(epd->fd > -1) { serv_status.active_counts--; close(epd->fd); epd->fd = -1; } free_epd(epd); }
void close_client ( epdata_t *epd ) { if ( !epd ) { return; } if ( epd->status == STEP_READ ) { serv_status.reading_counts--; } else if ( epd->status == STEP_SEND ) { serv_status.sending_counts--; } se_delete ( epd->se_ptr ); delete_timeout ( epd->timeout_ptr ); epd->timeout_ptr = NULL; if ( epd->fd > -1 ) { serv_status.active_counts--; close ( epd->fd ); epd->fd = -1; } if ( epd->websocket ) { if ( epd->websocket->websocket_handles > 0 ) { luaL_unref ( epd->websocket->ML, LUA_REGISTRYINDEX, epd->websocket->websocket_handles ); } lua_resume ( epd->websocket->ML, 0 ); free ( epd->websocket ); epd->websocket = NULL; } free_epd ( epd ); }
static int _lua_co_close(lua_State *L, cosocket_t *c*k) { if(c*k->read_buf) { cosocket_link_buf_t *fr = c*k->read_buf; cosocket_link_buf_t *nb = NULL; while(fr) { nb = fr->next; free(fr->buf); free(fr); fr = nb; } c*k->read_buf = NULL; } if(c*k->send_buf_need_free) { free(c*k->send_buf_need_free); c*k->send_buf_need_free = NULL; } if(c*k->pool_wait) { delete_in_waiting_get_connection(c*k->pool_wait); c*k->pool_wait = NULL; } delete_timeout(c*k->timeout_ptr); c*k->timeout_ptr = NULL; c*k->status = 0; if(c*k->fd > -1) { ((se_ptr_t *) c*k->ptr)->fd = c*k->fd; if(c*k->pool_size < 1 || add_connection_to_pool(_loop_fd, c*k->pool_key, c*k->pool_size, c*k->ptr, c*k->ssl, c*k->ctx, c*k->ssl_pw) == 0) { se_delete(c*k->ptr); c*k->ptr = NULL; connection_pool_counter_operate(c*k->pool_key, -1); close(c*k->fd); if(c*k->ssl) { SSL_free(c*k->ssl); c*k->ssl = NULL; } if(c*k->ctx) { SSL_CTX_free(c*k->ctx); c*k->ctx = NULL; } if(c*k->ssl_pw) { free(c*k->ssl_pw); c*k->ssl_pw = NULL; } } c*k->ssl = NULL; c*k->ctx = NULL; c*k->ssl_pw = NULL; c*k->ptr = NULL; c*k->fd = -1; } }
static void timeout_handle(void *ptr) { cosocket_t *c*k = ptr; delete_timeout(c*k->timeout_ptr); c*k->timeout_ptr = NULL; if(c*k->pool_wait) { delete_in_waiting_get_connection(c*k->pool_wait); c*k->pool_wait = NULL; } lua_pushnil(c*k->L); if(c*k->ptr) { if(c*k->status == 3) { lua_pushstring(c*k->L, "Connect error!(wait pool timeout)"); } else if(((se_ptr_t *) c*k->ptr)->wfunc == cosocket_be_ssl_connected) { lua_pushstring(c*k->L, "SSL Connect timeout!"); } else if(((se_ptr_t *) c*k->ptr)->wfunc == cosocket_be_write) { lua_pushstring(c*k->L, "Send timeout!"); } else if(((se_ptr_t *) c*k->ptr)->rfunc == cosocket_be_read) { lua_pushstring(c*k->L, "Read timeout!"); } else { lua_pushstring(c*k->L, "Timeout!"); } } else { if(c*k->status == 3) { lua_pushstring(c*k->L, "Connect error!(wait pool timeout)"); } else { lua_pushstring(c*k->L, "Timeout!"); } } { se_delete(c*k->ptr); c*k->ptr = NULL; if(c*k->fd > -1) { connection_pool_counter_operate(c*k->pool_key, -1); close(c*k->fd); c*k->fd = -1; } c*k->status = 0; } if(c*k->ssl) { SSL_shutdown(c*k->ssl); SSL_free(c*k->ssl); c*k->ssl = NULL; SSL_CTX_free(c*k->ctx); c*k->ctx = NULL; } if(c*k->read_buf) { cosocket_link_buf_t *fr = c*k->read_buf; cosocket_link_buf_t *nb = NULL; while(fr) { nb = fr->next; free(fr->buf); free(fr); fr = nb; } c*k->read_buf = NULL; } if(c*k->send_buf_need_free) { free(c*k->send_buf_need_free); c*k->send_buf_need_free = NULL; } c*k->inuse = 0; lua_f_lua_uthread_resume_in_c(c*k->L, 2); }
int cosocket_be_read(se_ptr_t *ptr) { cosocket_t *c*k = ptr->data; int n = 0, ret = 0; init_read_buf: if(!c*k->read_buf || (c*k->last_buf->buf_len >= c*k->last_buf->buf_size)) { /// init read buf cosocket_link_buf_t *nbuf = NULL; nbuf = malloc(sizeof(cosocket_link_buf_t)); if(nbuf == NULL) { LOGF(ERR, "malloc error @%s:%d\n", __FILE__, __LINE__); exit(1); } nbuf->buf = large_malloc(4096); if(!nbuf->buf) { LOGF(ERR, "malloc error @%s:%d\n", __FILE__, __LINE__); exit(1); } nbuf->buf_size = 4096; nbuf->buf_len = 0; nbuf->next = NULL; if(c*k->read_buf) { c*k->last_buf->next = nbuf; } else { c*k->read_buf = nbuf; } c*k->last_buf = nbuf; } if(!c*k->ssl) { while((n = recv(c*k->fd, c*k->last_buf->buf + c*k->last_buf->buf_len, c*k->last_buf->buf_size - c*k->last_buf->buf_len, 0)) > 0) { c*k->last_buf->buf_len += n; c*k->total_buf_len += n; if(c*k->last_buf->buf_len >= c*k->last_buf->buf_size) { goto init_read_buf; } } } else { while((n = SSL_read(c*k->ssl, c*k->last_buf->buf + c*k->last_buf->buf_len, c*k->last_buf->buf_size - c*k->last_buf->buf_len)) > 0) { c*k->last_buf->buf_len += n; c*k->total_buf_len += n; if(c*k->last_buf->buf_len >= c*k->last_buf->buf_size) { goto init_read_buf; } } } if(n == 0 || (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) { int rfd = c*k->fd; /// socket closed delete_timeout(c*k->timeout_ptr); c*k->timeout_ptr = NULL; { c*k->status = 0; se_delete(c*k->ptr); c*k->ptr = NULL; connection_pool_counter_operate(c*k->pool_key, -1); close(c*k->fd); c*k->fd = -1; c*k->status = 0; } if(c*k->in_read_action == 1) { c*k->in_read_action = 0; int rt = lua_co_read_(c*k); c*k->inuse = 0; if(rt > 0) { ret = lua_f_lua_uthread_resume_in_c(c*k->L, rt); } else if(n == 0) { lua_pushnil(c*k->L); ret = lua_f_lua_uthread_resume_in_c(c*k->L, 1); } if(ret == LUA_ERRRUN) { se_delete(c*k->ptr); c*k->ptr = NULL; connection_pool_counter_operate(c*k->pool_key, -1); close(c*k->fd); c*k->fd = -1; c*k->status = 0; } } } else { if(c*k->in_read_action == 1) { int rt = lua_co_read_(c*k); if(rt > 0) { c*k->in_read_action = 0; delete_timeout(c*k->timeout_ptr); c*k->timeout_ptr = NULL; c*k->inuse = 0; ret = lua_f_lua_uthread_resume_in_c(c*k->L, rt); if(ret == LUA_ERRRUN) { se_delete(c*k->ptr); c*k->ptr = NULL; connection_pool_counter_operate(c*k->pool_key, -1); close(c*k->fd); c*k->fd = -1; c*k->status = 0; } } } } return 0; }
int cosocket_be_write(se_ptr_t *ptr) { cosocket_t *c*k = ptr->data; int n = 0, ret = 0; c*k->in_read_action = 0; if(!c*k->ssl) { while((n = send(c*k->fd, c*k->send_buf + c*k->send_buf_ed, c*k->send_buf_len - c*k->send_buf_ed, MSG_DONTWAIT)) > 0) { c*k->send_buf_ed += n; } } else { while((n = SSL_write(c*k->ssl, c*k->send_buf + c*k->send_buf_ed, c*k->send_buf_len - c*k->send_buf_ed)) > 0) { c*k->send_buf_ed += n; } } if(c*k->send_buf_ed == c*k->send_buf_len || (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) { if(n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { se_delete(c*k->ptr); c*k->ptr = NULL; connection_pool_counter_operate(c*k->pool_key, -1); close(c*k->fd); c*k->fd = -1; c*k->status = 0; c*k->send_buf_ed = 0; } /* else { se_be_pri(c*k->ptr, NULL); }*/ if(c*k->send_buf_need_free) { free(c*k->send_buf_need_free); c*k->send_buf_need_free = NULL; } delete_timeout(c*k->timeout_ptr); c*k->timeout_ptr = NULL; int rc = 1; if(c*k->send_buf_ed >= c*k->send_buf_len) { lua_pushnumber(c*k->L, c*k->send_buf_ed); } else if(c*k->fd == -1) { lua_pushnil(c*k->L); lua_pushstring(c*k->L, "connection closed!"); rc = 2; } else { lua_pushboolean(c*k->L, 0); } if(c*k->inuse == 1) { se_be_pri(c*k->ptr, NULL); c*k->inuse = 0; lua_f_lua_uthread_resume_in_c(c*k->L, rc); return 0; } else { return 0 - rc; } } return c*k->send_buf_len - c*k->send_buf_ed; }