Ejemplo n.º 1
0
void *recv_msg(int s, void *data, int64_t *length, int64_t offset) {
  int64_t incoming;
  if (recv_from_socket(s, &incoming, sizeof(int64_t))<0) return data;
  if (*length < (offset + incoming)) {
    data = socket_check_realloc(data, offset + incoming, "receive buffer");
    assert(data != NULL);
    *length = offset+incoming;
  }
  recv_from_socket(s, data+offset, incoming);
  return data;
}
Ejemplo n.º 2
0
void *
tcsd_thread_run(void *v)
{
	struct tcsd_thread_data *data = (struct tcsd_thread_data *)v;
	BYTE *buffer;
	int recv_size, send_size;
	TSS_RESULT result;
	UINT64 offset;
#ifndef TCSD_SINGLE_THREAD_DEBUG
	int rc;

	thread_signal_init();
#endif

	data->comm.buf_size = TCSD_INIT_TXBUF_SIZE;
	data->comm.buf = calloc(1, data->comm.buf_size);
	while (data->comm.buf) {
		/* get the packet header to get the size of the incoming packet */
		buffer = data->comm.buf;
		recv_size = sizeof(struct tcsd_packet_hdr);
		if ((recv_size = recv_from_socket(data->sock, buffer, recv_size)) < 0)
			break;
		buffer += sizeof(struct tcsd_packet_hdr);       /* increment the buffer pointer */

		/* check the packet size */
		recv_size = Decode_UINT32(data->comm.buf);
		if (recv_size < (int)sizeof(struct tcsd_packet_hdr)) {
			LogError("Packet to receive from socket %d is too small (%d bytes)",
					data->sock, recv_size);
			break;
		}

		if (recv_size > data->comm.buf_size ) {
			BYTE *new_buffer;

			LogDebug("Increasing communication buffer to %d bytes.", recv_size);
			new_buffer = realloc(data->comm.buf, recv_size);
			if (new_buffer == NULL) {
				LogError("realloc of %d bytes failed.", recv_size);
				break;
			}
			buffer = new_buffer + sizeof(struct tcsd_packet_hdr);
			data->comm.buf_size = recv_size;
			data->comm.buf = new_buffer;
		}

		/* get the rest of the packet */
		recv_size -= sizeof(struct tcsd_packet_hdr);    /* already received the header */
		if ((recv_size = recv_from_socket(data->sock, buffer, recv_size)) < 0)
			break;
		LogDebug("Rx'd packet");
		printf("recv data:\n");
		player_print_hex(data->comm.buf, data->comm.hdr.packet_size);
		/* create a platform version of the tcsd header */
		offset = 0;
		UnloadBlob_UINT32(&offset, &data->comm.hdr.packet_size, data->comm.buf);
		UnloadBlob_UINT32(&offset, &data->comm.hdr.u.result, data->comm.buf);
		UnloadBlob_UINT32(&offset, &data->comm.hdr.num_parms, data->comm.buf);
		UnloadBlob_UINT32(&offset, &data->comm.hdr.type_size, data->comm.buf);
		UnloadBlob_UINT32(&offset, &data->comm.hdr.type_offset, data->comm.buf);
		UnloadBlob_UINT32(&offset, &data->comm.hdr.parm_size, data->comm.buf);
		UnloadBlob_UINT32(&offset, &data->comm.hdr.parm_offset, data->comm.buf);

		if ((result = getTCSDPacket(data)) != TSS_SUCCESS) {
			/* something internal to the TCSD went wrong in preparing the packet
			 * to return to the TSP.  Use our already allocated buffer to return a
			 * TSS_E_INTERNAL_ERROR return code to the TSP. In the non-error path,
			 * these LoadBlob's are done in getTCSDPacket().
			 */
			/* set everything to zero, fill in what is non-zero */
			memset(data->comm.buf, 0, data->comm.buf_size);
			offset = 0;
			/* load packet size */
			LoadBlob_UINT32(&offset, sizeof(struct tcsd_packet_hdr), data->comm.buf);
			/* load result */
			LoadBlob_UINT32(&offset, result, data->comm.buf);
		}
		send_size = Decode_UINT32(data->comm.buf);
		LogDebug("Sending 0x%X bytes back", send_size);
		printf("send data:\n");
		player_print_hex(data->comm.buf, send_size);
		send_size = send_to_socket(data->sock, data->comm.buf, send_size);
		if (send_size < 0)
			break;

		/* check for shutdown */
		if (tm->shutdown) {
			LogDebug("Thread %ld exiting via shutdown signal!", THREAD_ID);
			break;
		}
	}

	LogDebug("Thread exiting.");

	/* Closing connection to TSP */
	close(data->sock);
	data->sock = -1;
	free(data->comm.buf);
	data->comm.buf = NULL;
	data->comm.buf_size = -1;
	/* If the connection was not shut down cleanly, free TCS resources here */
	if (data->context != NULL_TCS_HANDLE) {
		TCS_CloseContext_Internal(data->context);
		data->context = NULL_TCS_HANDLE;
	}
	if(data->hostname != NULL) {
		free(data->hostname);
		data->hostname = NULL;
	}

#ifndef TCSD_SINGLE_THREAD_DEBUG
	pthread_mutex_lock(&(tm->lock));
	tm->num_active_threads--;
	/* if we're not in shutdown mode, then nobody is waiting to join this thread, so
	 * detach it so that its resources are free at pthread_exit() time. */
	if (!tm->shutdown) {
		if ((rc = pthread_detach(*(data->thread_id)))) {
			LogError("pthread_detach failed (errno %d)."
				 " Resources may not be properly released.", rc);
		}
	}
	free(data->thread_id);
	data->thread_id = THREAD_NULL;
	pthread_mutex_unlock(&(tm->lock));
	pthread_exit(NULL);
	return NULL; /*never go into here,just for compiling ,add by xlyu*/
#else
	return NULL;
#endif
}
Ejemplo n.º 3
0
void *recv_and_alloc(int s, void *data, int64_t length) {
  data = socket_check_realloc(data, length, "receive buffer");
  assert(data != NULL || length <= 0);
  recv_from_socket(s, data, length);
  return data;
}