static void writer_margin( Writer* w, int margin) { static const char spaces[10] = " "; while (margin >= 10) { writer_write(w,spaces,10); margin -= 10; } if (margin > 0) writer_write(w,spaces,margin); }
void client_dispatch_response(Client *client, PendingRequest *pending_request, Packet *response, bool force, bool ignore_authentication) { Node *pending_request_client_node = NULL; int enqueued = 0; #ifdef BRICKD_WITH_PROFILING uint64_t elapsed; #endif if (!ignore_authentication && client->authentication_state != CLIENT_AUTHENTICATION_STATE_DISABLED && client->authentication_state != CLIENT_AUTHENTICATION_STATE_DONE) { log_packet_debug("Ignoring non-authenticated client ("CLIENT_SIGNATURE_FORMAT")", client_expand_signature(client)); goto cleanup; } // find matching pending request if not forced and no pending request is // already given. do this before the disconnect check to ensure that even // for a disconnected client the pending request list is updated correctly if (!force && pending_request == NULL) { pending_request_client_node = client->pending_request_sentinel.next; while (pending_request_client_node != &client->pending_request_sentinel) { pending_request = containerof(pending_request_client_node, PendingRequest, client_node); if (packet_is_matching_response(response, &pending_request->header)) { break; } pending_request_client_node = pending_request_client_node->next; } if (pending_request_client_node == &client->pending_request_sentinel) { pending_request = NULL; goto cleanup; } } if (client->disconnected) { log_debug("Ignoring disconnected client ("CLIENT_SIGNATURE_FORMAT")", client_expand_signature(client)); goto cleanup; } if (force || pending_request != NULL) { enqueued = writer_write(&client->response_writer, response); if (enqueued < 0) { goto cleanup; } if (force) { log_packet_debug("Forced to %s response to client ("CLIENT_SIGNATURE_FORMAT")", enqueued ? "enqueue" : "send", client_expand_signature(client)); } else { #ifdef BRICKD_WITH_PROFILING elapsed = microseconds() - pending_request->arrival_time; log_packet_debug("%s response to client ("CLIENT_SIGNATURE_FORMAT"), was requested %u.%03u msec ago, %d request(s) still pending", enqueued ? "Enqueued" : "Sent", client_expand_signature(client), (unsigned int)(elapsed / 1000), (unsigned int)(elapsed % 1000), client->pending_request_count - 1); #else log_packet_debug("%s response to client ("CLIENT_SIGNATURE_FORMAT"), %d request(s) still pending", enqueued ? "Enqueued" : "Sent", client_expand_signature(client), client->pending_request_count - 1); #endif } } cleanup: if (pending_request != NULL) { pending_request_remove_and_free(pending_request); } }
static void writer_str(Writer* w, const char* str) { writer_write(w, str, strlen(str)); }
static void writer_c(Writer* w, char c) { writer_write(w, &c, 1); }
int process_source(RemoteSource *source, bool compress, bool seal) { size_t remain, target; int r; assert(source); assert(source->writer); r = process_data(source); if (r <= 0) return r; /* We have a full event */ log_trace("Received a full event from source@%p fd:%d (%s)", source, source->fd, source->name); if (!source->iovw.count) { log_warning("Entry with no payload, skipping"); goto freeing; } assert(source->iovw.iovec); assert(source->iovw.count); r = writer_write(source->writer, &source->iovw, &source->ts, compress, seal); if (r < 0) log_error_errno(r, "Failed to write entry of %zu bytes: %m", iovw_size(&source->iovw)); else r = 1; freeing: iovw_free_contents(&source->iovw); /* possibly reset buffer position */ remain = source->filled - source->offset; if (remain == 0) /* no brainer */ source->offset = source->scanned = source->filled = 0; else if (source->offset > source->size - source->filled && source->offset > remain) { memcpy(source->buf, source->buf + source->offset, remain); source->offset = source->scanned = 0; source->filled = remain; } target = source->size; while (target > 16 * LINE_CHUNK && remain < target / 2) target /= 2; if (target < source->size) { char *tmp; tmp = realloc(source->buf, target); if (!tmp) log_warning("Failed to reallocate buffer to (smaller) size %zu", target); else { log_debug("Reallocated buffer from %zu to %zu bytes", source->size, target); source->buf = tmp; source->size = target; } } return r; }