コード例 #1
0
ファイル: echo-server.c プロジェクト: litesky/html-sqlite
void read_done( uv_stream_t *handle, ssize_t nread, uv_buf_t buf )
{
    ctx_t *ctx = ( ctx_t *)handle->data;
    sql_t *sql = ( sql_t *) malloc( sizeof( sql_t ) );
    uv_write_t *wreq = ( uv_write_t *) malloc( sizeof( uv_write_t ) );
    int n;
    if ( nread < 0 ) { /* Error or EOF */
        if ( buf.base ) {
            free( buf.base );
        }
        uv_close( ( uv_handle_t *)&ctx->tcp, close_done );
        free( wreq );
        return;
    }
    if ( nread == 0 ) { /* Everything OK, but nothing read. */
        free( buf.base );
        uv_close( ( uv_handle_t *)&ctx->tcp, close_done );
        free( wreq );
        return;
    }
    sql->db = ctx->db;
    sql->raw = buf.base;
    sql->size = nread;
    get_sql( sql );
    free( buf.base );
    nread = sql->size;
    ctx->tcp_nread = nread;
    uv_read_stop( ( uv_stream_t *)&ctx->tcp );
    wreq->data = ctx;
    buf.len =sql->size;
    buf.base = sql->raw;
    n = uv_write( wreq, ( uv_stream_t *)&ctx->tcp, &buf, 1, write_done );
    free( buf.base );
    free(sql);
}
コード例 #2
0
ファイル: proxyserver.c プロジェクト: companycy/sql-protocol
int main(int argc, char**argv) {
  // config here
  const enum SqlserverType type = MSSQL_SERVER;

  const int idx = (int)type;
  const int fe_port = server_infos[idx].fe_port; // accept connection from driver
  const char* be_ip = server_infos[idx].be_ip;   // sql server ip
  const int be_port = server_infos[idx].be_port;

  const int socketfd = make_socket(fe_port);
  const int maxconn = 1024;
  if (listen(socketfd, maxconn) < 0)
    error("ERROR on listen");

  struct sockaddr_in cliaddr;
  socklen_t clilen = sizeof(cliaddr);
  // for(;;)
  {
    const int connfd = accept(socketfd, (struct sockaddr *)&cliaddr, &clilen); // accept is blocking
    if (connfd < 0) {
      error("ERROR on accept");
    } else {
      // printf("new accepted fd %d\n", connfd);
    }

    struct hostent *hostp = gethostbyaddr((const char *)&cliaddr.sin_addr.s_addr,
                                          sizeof(cliaddr.sin_addr.s_addr), AF_INET);
    if (hostp == NULL)
      error("ERROR on gethostbyaddr");

    const char* hostaddrp = inet_ntoa(cliaddr.sin_addr);
    if (hostaddrp == NULL)
      error("ERROR on inet_ntoa\n");
    printf("server established connection with %s (%s) from port: %d\n", hostp->h_name, hostaddrp, fe_port);

    const int sendfd = get_sendfd(be_ip, be_port); // new socket to connect ms sql server

    char buf[1024];
    bzero(buf, sizeof(buf));

    for (;;) {
      const int n = read(connfd, buf, sizeof(buf));
      if (n > 0) {
        printf("read from frontend:  %d bytes\n", n);

        // for (int i = 0; i < n; ++i) {
        //   printf("%c", *(buf + i));
        // }
        // printf("\n");

        get_sql(type, buf, n);

        const int m = write(sendfd, buf, n);
        if (m < 0)
          error("ERROR writing to socket");
        else
          printf("write to backend %d bytes, total %d bytes\n", m, n);

        bzero(buf, sizeof(buf));
        const int j = read(sendfd, buf, sizeof(buf));
        if (j < 0)
          error("ERROR reading from socket");
        else
          printf("read from backend bytes:  %d\n", j);

        const int k = write(connfd, buf, j);
        if (k < 0)
          error("ERROR writing to socket");
        else
          printf("write to frontend bytes: %d\n", k);

      }

      // close(sendfd);
    }

    close(connfd);
  }

  return 0;
}