Example #1
0
msgpack_unpacked *filter_receive_message(Filter * const filter)
{
    msgpack_unpacker * const msgpack_unpacker = &filter->msgpack_unpacker;    
    msgpack_unpacker_destroy(msgpack_unpacker);
    msgpack_unpacker_init(msgpack_unpacker, FILTER_UNPACK_BUFFER_SIZE);
    msgpack_unpacker_reserve_buffer(msgpack_unpacker,
                                    FILTER_READ_BUFFER_SIZE);
    msgpack_unpacked * const message = &filter->message;
    
    ssize_t readnb;    
    while (msgpack_unpacker_next(msgpack_unpacker, message) == false) {
        assert(msgpack_unpacker_buffer_capacity(msgpack_unpacker) > 0U);
        readnb = safe_read_partial
            (filter->upipe_stdout.fd_read,
                msgpack_unpacker_buffer(msgpack_unpacker),
                msgpack_unpacker_buffer_capacity(msgpack_unpacker));
        if (readnb <= (ssize_t) 0) {
            assert(0);
            return NULL;
        }
        msgpack_unpacker_buffer_consumed(msgpack_unpacker, readnb);
    }
    assert(message->data.type == MSGPACK_OBJECT_MAP);
    
    return message;
}
Example #2
0
void tmate_decoder_get_buffer(struct tmate_decoder *decoder,
			      char **buf, size_t *len)
{
	/* rewind the buffer if possible */
	if (msgpack_unpacker_buffer_capacity(&decoder->unpacker) <
						TMATE_MAX_MESSAGE_SIZE) {
		msgpack_unpacker_expand_buffer(&decoder->unpacker, 0);
	}

	*buf = msgpack_unpacker_buffer(&decoder->unpacker);
	*len = msgpack_unpacker_buffer_capacity(&decoder->unpacker);
}
Example #3
0
size_t receiver_to_unpacker(receiver* r, size_t request_size,
        msgpack_unpacker *unpacker)
{
    size_t recv_len;
    // make sure there's enough room, or expand the unpacker accordingly
    if (msgpack_unpacker_buffer_capacity(unpacker) < request_size) {
        msgpack_unpacker_reserve_buffer(unpacker, request_size);
        assert(msgpack_unpacker_buffer_capacity(unpacker) >= request_size);
    }
    recv_len = receiver_recv(r, msgpack_unpacker_buffer(unpacker),
                             request_size);
    msgpack_unpacker_buffer_consumed(unpacker, recv_len);
    return recv_len;
}
Example #4
0
void tmate_decoder_get_buffer(struct tmate_decoder *decoder,
			      char **buf, size_t *len)
{
	if (!msgpack_unpacker_reserve_buffer(&decoder->unpacker, UNPACKER_RESERVE_SIZE))
		tmate_fatal("cannot expand decoder buffer");

	*buf = msgpack_unpacker_buffer(&decoder->unpacker);
	*len = msgpack_unpacker_buffer_capacity(&decoder->unpacker);
}
Example #5
0
/**
 * Process incoming data from the underlying device
 *
 * \see QIODevice()
 */
void MsgpackIODevice::dataAvailable()
{
	qint64 read = 1;
	while (read > 0) {
		if ( msgpack_unpacker_buffer_capacity(&m_uk) == 0 ) {
			if ( !msgpack_unpacker_reserve_buffer(&m_uk, 8192 ) ) {
				// FIXME: error out
				qWarning() << "Could not allocate memory in unpack buffer";
				return;
			}
		}

		read = m_dev->read(msgpack_unpacker_buffer(&m_uk), msgpack_unpacker_buffer_capacity(&m_uk));
		if ( read > 0 ) {
			msgpack_unpacker_buffer_consumed(&m_uk, read);
			msgpack_unpacked result;
			msgpack_unpacked_init(&result);
			while(msgpack_unpacker_next(&m_uk, &result)) {
				dispatch(result.data);
			}
		}
	}
}
Example #6
0
/**
 * Process incoming data from the given fd.
 *
 * \see fromStdinOut() and QSocketNotifier()
 */
void MsgpackIODevice::dataAvailableFd(int fd)
{
	qint64 bytes = read(fd, msgpack_unpacker_buffer(&m_uk),
			msgpack_unpacker_buffer_capacity(&m_uk));
	if (bytes > 0) {
		msgpack_unpacker_buffer_consumed(&m_uk, bytes);
		msgpack_unpacked result;
		msgpack_unpacked_init(&result);
		while(msgpack_unpacker_next(&m_uk, &result)) {
			dispatch(result.data);
		}
	} else if (bytes == -1) {
		setError(InvalidDevice, tr("Error when reading from device"));
	}
}
Example #7
0
/**
 * Process incoming data.
 *
 * \see fromStdinOut() and StdinReader()
 */
void MsgpackIODevice::dataAvailableStdin(const QByteArray& data)
{
	if ( (quint64)data.length() > msgpack_unpacker_buffer_capacity(&m_uk)) {
		setError(InvalidDevice, tr("Error when reading from stdin, BUG(buffered data exceeds capaciy)"));
		return;
	} else if ( data.length() > 0 ) {
		memcpy(msgpack_unpacker_buffer(&m_uk), data.constData(), data.length());
		msgpack_unpacker_buffer_consumed(&m_uk, data.length());
		msgpack_unpacked result;
		msgpack_unpacked_init(&result);
		while(msgpack_unpacker_next(&m_uk, &result)) {
			dispatch(result.data);
		}
	}
}
Example #8
0
/**
 * Build a MsgpackIODevice that reads from stdin and writes to
 * stdout
 */
MsgpackIODevice* MsgpackIODevice::fromStdinOut(QObject *parent)
{
	MsgpackIODevice *rpc = new MsgpackIODevice(NULL, parent);
	msgpack_packer_init(&rpc->m_pk, rpc, (msgpack_packer_write)MsgpackIODevice::msgpack_write_to_stdout);
#ifdef _WIN32
	StdinReader *rsn = new StdinReader(msgpack_unpacker_buffer_capacity(&rpc->m_uk), rpc);
	connect(rsn, &StdinReader::dataAvailable,
			rpc, &MsgpackIODevice::dataAvailableStdin);
	rsn->start();
#else
	QSocketNotifier *rsn = new QSocketNotifier(0, QSocketNotifier::Read, rpc);
	connect(rsn, &QSocketNotifier::activated,
			rpc, &MsgpackIODevice::dataAvailableFd);
#endif
	return rpc;
}
Example #9
0
size_t msgpack_unpacker_buffer_capacity_wrap(const msgpack_unpacker *mpac)
{
  return msgpack_unpacker_buffer_capacity(mpac);
}
Example #10
0
void monitor_method_recv(void)
{
  fd_set rfds;
  int retval;
  static const struct timespec tv = {0, 0};

  ssize_t size;
  char strname[100];

  msgpack_unpacked result;
  msgpack_object *name, *data;

  FD_ZERO(&rfds);
  FD_SET(sock, &rfds);
  retval = pselect(sock + 1, &rfds, NULL, NULL, &tv, NULL);

  if(retval == 0) {
    /* no data */
    return;
  }
  else if(retval == -1) {
    if(errno != EINTR) {
      err(EXIT_FAILURE, "method_receive pselect");
    }
    return;
  }

  /* receive */
  size = read(sock, msgpack_unpacker_buffer(up), msgpack_unpacker_buffer_capacity(up));

  msgpack_unpacker_buffer_consumed(up, size);
  msgpack_unpacked_init(&result);

  /* stream unpacker */
  while(msgpack_unpacker_next(up, &result)) {
    if(DEBUG_MON) {
      printf("[Monitor] ");
      msgpack_object_print(stdout, result.data);
      puts("");
    }

    /* method: ["METHODNAME", [some, method, args]] */
    if(result.data.type != MSGPACK_OBJECT_ARRAY || result.data.via.array.size <= 0) {
      errx(EXIT_FAILURE, "invalid method");
    }

    /* method name */
    name = result.data.via.array.ptr;

    /* method data */
    if(result.data.via.array.size == 2) {
      data = result.data.via.array.ptr + 1;
    }
    else {
      data = NULL;
    }

    if(name->type != MSGPACK_OBJECT_RAW) {
      errx(EXIT_FAILURE, "invalid method");      
    }

    /* convert method name */
    memcpy(strname, name->via.raw.ptr, name->via.raw.size);
    strname[name->via.raw.size] = '\0';

    /* call method */
    if(!strcmp(strname, "CONNECT")) {
    }
    else if(!strcmp(strname, "DISCONNECT")) {
      exec_finish = true;
    }
    else if(data == NULL) {
      errx(EXIT_FAILURE, "invalid method (no data?)");
    }
    else if(!strcmp(strname, "KEYBOARD_SCANCODE")) {
      msgpack_object *obj;
      unsigned int i, n;

      unsigned char scancode;

      if(data->type == MSGPACK_OBJECT_ARRAY) {
	obj = data->via.array.ptr;
	n = data->via.array.size;
      }
      else if(data->type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
	obj = data;
	n = 1;
      }
      else {
	n = 0;
      }

      for(i = 0; i < n; i++, obj++) {
	/* push FIFO */
	scancode = obj->via.i64 & 0xff;
	fifo_scancode[fifo_scancode_end++] = scancode;

	if(fifo_scancode_end >= KMC_FIFO_SCANCODE_SIZE) {
	  fifo_scancode_end = 0;
	}
	if(fifo_scancode_start == fifo_scancode_end) {
	  fifo_scancode_start++;
	  if(fifo_scancode_start >= KMC_FIFO_SCANCODE_SIZE) {
	    fifo_scancode_start = 0;
	  }
	}

	gci_nodes[GCI_KMC_NUM].int_dispatch = true;

	DEBUGMON("[Monitor] KEYBOARD_SCANCODE %x\n", scancode);
      }
    }
    else {
      errx(EXIT_FAILURE, "unknown method '%s'", strname);
    }
  }
}