/* TODO stream is very inefficient of transmiting when receive - * receiver forces transmiter to accasionaly flush out-buffer * */ static unsigned short tcp_stream_getchar (tcp_stream_t *u) { unsigned short c; tcp_socket_t* s = u->socket; if (!s) return -1; mutex_lock (&s->lock); /* Flush output buffer. */ if (u->outptr > u->outdata) { stream_flush (u); mutex_unlock (&s->lock); socket_flush (s); mutex_lock (&s->lock); } if (u->inbuf) mutex_unlock (&s->lock); else { /* Wait for data. */ while (tcp_queue_is_empty (s)) { if (!tcp_socket_is_state(s, TCP_STATES_TRANSFER)) { mutex_unlock (&s->lock); return -1; } mutex_wait (&s->lock); } u->inbuf = tcp_queue_get (s); u->inptr = u->inbuf->payload; mutex_unlock (&s->lock); /*debug_printf ("tstream input"); buf_print (u->inbuf);*/ mutex_lock (&s->ip->lock); if (! (s->flags & TF_ACK_DELAY) && ! (s->flags & TF_ACK_NOW)) { tcp_ack (s); } mutex_unlock (&s->ip->lock); } /* Get byte from buffer. */ c = *u->inptr++; if (u->inptr >= u->inbuf->payload + u->inbuf->len) { buf_t *old = u->inbuf; u->inbuf = old->next; if (u->inbuf) { u->inptr = u->inbuf->payload; old->next = 0; } buf_free (old); } return c; }
static void tcp_stream_flush (tcp_stream_t *u) { tcp_socket_t* s = u->socket; if (! s) return; mutex_lock (&s->lock); /* Flush output buffer. */ if (u->outptr <= u->outdata) { mutex_unlock (&s->lock); return; } stream_flush (u); mutex_unlock (&s->lock); /* Force IP level to send a packet. */ socket_flush (s); }
void controller_user_login(bmessage_t* pMsg) { bresponse_t response = {0}; trans_str_size_t len = TRANS_STR_LEN; char pwd[TRANS_STR_LEN] = {0}; int uid = 0; char sql[200] = {0}; char* username = NULL; BMYSQL_RES* bresult; int ret = 0; uid = socket_read_int(pMsg); socket_read_string(pMsg, pwd, &len); snprintf(sql, 199, "select `username` from `qgame_users` where `uid`=%d and `password`=\"%s\"", uid, pwd); printf("before bmysql_query_scalar\n"); int ret = bmysql_query_scalar(sql, &username, bresult); if(ret == 0) { printf("user login bresult->id:%d\tpMsg:%p\n", bresult->id, pMsg); if(username == NULL) { response.code = ERROR_USER_LOGIN_FAIL; } else { socket_write_string(&response, username, strlen(username)); socket_write_int(&response, uid); socket_write_string(&response, pwd, len); } socket_flush(pMsg, &response); bmysql_free_result(bresult); } else { printf("mysql query error:%d\n", ret); } }
static int tcp_stream_peekchar (tcp_stream_t *u) { tcp_socket_t* s = u->socket; if (! s) return -1; mutex_lock (&s->lock); /* Flush output buffer. */ if (u->outptr > u->outdata) { stream_flush (u); mutex_unlock (&s->lock); socket_flush (s); mutex_lock (&s->lock); } /* Any data available? */ if (u->inbuf) mutex_unlock (&s->lock); else { if (tcp_queue_is_empty (s)) { mutex_unlock (&s->lock); return -1; } u->inbuf = tcp_queue_get (s); u->inptr = u->inbuf->payload; mutex_unlock (&s->lock); mutex_lock (&s->ip->lock); if (! (s->flags & TF_ACK_DELAY) && ! (s->flags & TF_ACK_NOW)) { tcp_ack (s); } mutex_unlock (&s->ip->lock); } return *u->inptr; }
static void tcp_stream_putchar (tcp_stream_t *u, short c) { tcp_socket_t* s = u->socket; if (! s) return; mutex_lock (&s->lock); if (!tcp_socket_is_state(s, TCP_STATES_TRANSFER | tcpfCLOSE_WAIT)) { /* Connection was closed. */ mutex_unlock (&s->lock); return; } /* Put byte into output buffer. */ *u->outptr++ = c; if (u->outptr < (u->outdata + u->outdata_size) ) { mutex_unlock (&s->lock); return; } stream_flush (u); mutex_unlock (&s->lock); /* Force IP level to send a packet. */ socket_flush (s); }