int cs_routine(int fd, sqlite3 *db) { sockfd_buf_t *rwbuf = sockfd_list[fd]; if (rwbuf == NULL) return -1; if (rwbuf->rbuf.data == NULL) return -1; if (rwbuf->wbuf.data == NULL) return -1; int n = read(fd, rwbuf->rbuf.data, rwbuf->rbuf.max); if (n < 0) { E("%s", strerror(errno)); return -1; } else if (n == 0) { sockfd_buf_free(rwbuf); sockfd_list[fd] = NULL; unregister_readfd(fd); unregister_writefd(fd); unregister_exceptfd(fd); D(GREEN"client %d is closed."NO, fd); close(fd); return 0; } D(GREEN"receive %s %d bytes from %d."NO, rwbuf->rbuf.data, n, fd); rwbuf->rbuf.len = n; n = sql_routine(fd, db, rwbuf); if (n == -1) { E("sql_routine() failed."); return -1; } D(GREEN"send %s %d bytes to %d."NO, rwbuf->wbuf.data, rwbuf->wbuf.len, fd); n = write(fd, rwbuf->wbuf.data, rwbuf->wbuf.len); if (n == -1) { E("%s", strerror(errno)); return -1; } memset(rwbuf->wbuf.data, 0, rwbuf->wbuf.max); rwbuf->wbuf.len = 0; memset(rwbuf->rbuf.data, 0, rwbuf->rbuf.max); rwbuf->rbuf.len = 0; return 0; }
int cs_routine(int fd, sqlite3 *db) { sockfd_buf_t *rwbuf = sockfd_list[fd]; if (rwbuf == NULL) return -1; if (rwbuf->rbuf.data == NULL) return -1; if (rwbuf->wbuf.data == NULL) return -1; int n = read(fd, rwbuf->rbuf.data, rwbuf->rbuf.max); if (n < 0) { E("%s", strerror(errno)); return -1; } else if (n == 0) { sockfd_buf_free(rwbuf); sockfd_list[fd] = NULL; unregister_readfd(fd); unregister_writefd(fd); unregister_exceptfd(fd); close(fd); D(GREEN"close %d client success."NO, fd); return 0; } D(GREEN"receive %s %d bytes from %d."NO, rwbuf->rbuf.data, n, fd); rwbuf->rbuf.len = n; DDBUF(rwbuf->rbuf); rwbuf->rbuf.len -= 4; strncpy(rwbuf->rbuf.data, rwbuf->rbuf.data + 4, rwbuf->rbuf.len); DDSTR(rwbuf->rbuf); n = sql_routine(fd, db, rwbuf); if (n == -1) { E("sql_routine() failed."); if (rwbuf->wbuf.len == 0) { strncpy(rwbuf->wbuf.data, "err", 3); rwbuf->wbuf.len = 3; } //return -1; } D(GREEN"send %s %d bytes to %d."NO, rwbuf->wbuf.data, rwbuf->wbuf.len, fd); //rwbuf->wbuf.data[0] = '\0'; //rwbuf->wbuf.data[1] = '\0'; //rwbuf->wbuf.data[2] = '\0'; //rwbuf->wbuf.data[3] = 8; //DDBUF(rwbuf->wbuf); n = write(fd, rwbuf->wbuf.data, rwbuf->wbuf.len); if (n == -1) { E("%s", strerror(errno)); return -1; } memset(rwbuf->wbuf.data, 0, rwbuf->wbuf.max); rwbuf->wbuf.len = 0; memset(rwbuf->rbuf.data, 0, rwbuf->rbuf.max); rwbuf->rbuf.len = 0; return 0; }