예제 #1
0
파일: kftserver.c 프로젝트: shroff/kft
void process_read() {
	if(in_buffer[0] == 0) { /* New Connection */
		if(debug)
			if(finished)
				printf("Received a new connection. Never got terminating message from last connection.\n");
			else
				printf("Received a new connection. Terminating last connection\n");
		finished = 0;
		free(out_buffer);
		process_new_connection();
	} else if(in_buffer[0] == 2) { /* Old connection closed */
		if(debug)
			printf("Transaction successfully completed. File sent\n");
		finished = 0;
		serving = 0;
		free(out_buffer);
	} else if(in_buffer[0] == 3) { /* Connection closed after file was not found */
		if(debug)
			printf("Transaction successfully completed. File not foud on local server.\n");
		finished = 0;
		serving = 0;
		free(out_buffer);
	} else if(in_buffer[0] >> 7 != alt) {
		if(debug)
			printf("Resending old packet.\n");
		resend = 1;
	} else {
		if(debug) {
예제 #2
0
파일: kftserver.c 프로젝트: shroff/kft
void accept_connection() {
	if(debug)
		printf("Waiting for connection.\n");

	src_len = sizeof(*src);
	if((in_size = recvfrom(socket_descriptor, in_buffer, max_in_size, 0, (struct sockaddr *) src, &src_len)) <= 0) {
		if(debug)
			printf("Could not read data.\n");
		return;
	}
	process_new_connection();
}
예제 #3
0
static void worker_thread(struct mg_context *ctx) {
  struct mg_connection *conn;
  // This is the specified request size limit for DIAL requests.  Note that
  // this will effectively make the request limit one byte *smaller* than the
  // required in the DIAL specification.
  int buf_size = MAX_REQUEST_SIZE;

  pthread_setname_np( pthread_self(), __func__);
  conn = (struct mg_connection *) calloc(1, sizeof(*conn) + buf_size);
  conn->buf_size = buf_size;
  conn->buf = (char *) (conn + 1);
  assert(conn != NULL);

  while (ctx->stop_flag == 0 && consume_socket(ctx, &conn->client)) {
    conn->birth_time = time(NULL);
    conn->ctx = ctx;

    // Fill in IP, port info early so even if SSL setup below fails,
    // error handler would have the corresponding info.
    // Thanks to Johannes Winkelmann for the patch.
    memcpy(&conn->request_info.remote_addr,
           &conn->client.remote_addr, sizeof(conn->client.remote_addr));

    // Fill in local IP info
    socklen_t addr_len = sizeof(conn->request_info.local_addr);
    getsockname(conn->client.sock,
        (struct sockaddr *) &conn->request_info.local_addr, &addr_len);

    process_new_connection(conn);

    close_connection(conn);
  }
  free(conn);

  // Signal master that we're done with connection and exiting
  (void) pthread_mutex_lock(&ctx->mutex);
  ctx->num_threads--;
  (void) pthread_cond_signal(&ctx->cond);
  assert(ctx->num_threads >= 0);
  (void) pthread_mutex_unlock(&ctx->mutex);

  DEBUG_TRACE(("exiting"));
}