Repository: jerryscript-project/jerryscript
File: jerry-ext/debugger/debugger-tcp.c (C)

Code before:
static bool
jerryx_debugger_tcp_send (jerry_debugger_transport_header_t *header_p, /**< tcp implementation */
                          uint8_t *message_p, /**< message to be sent */
                          size_t message_length) /**< message length in bytes */
{
  JERRYX_ASSERT (jerry_debugger_transport_is_connected ());

  jerryx_debugger_transport_tcp_t *tcp_p = (jerryx_debugger_transport_tcp_t *) header_p;
  jerryx_socket_size_t message_to_send_length = (jerryx_socket_size_t) message_length;

  do
  {
#ifdef __linux__
    ssize_t is_err = recv (tcp_p->tcp_socket, NULL, 0, MSG_PEEK);

    if (is_err == 0 && errno != JERRYX_EWOULDBLOCK)
    {
      int err_val = errno;
      jerry_debugger_transport_close ();
      jerryx_debugger_tcp_log_error (err_val);
      return false;
    }
#endif /* __linux__ */

    const jerryx_socket_void_t *message_to_send_p = (const jerryx_socket_void_t *) message_p;
    jerryx_socket_ssize_t sent_bytes = send (tcp_p->tcp_socket, message_to_send_p, message_to_send_length, 0);

    if (sent_bytes < 0)
    {
      int err_val = jerryx_debugger_tcp_get_errno ();

      if (err_val == JERRYX_EWOULDBLOCK)
      {
        continue;
      }

      jerry_debugger_transport_close ();
      jerryx_debugger_tcp_log_error (err_val);
      return false;
    }

    message_p += sent_bytes;
    message_to_send_length -= (jerryx_socket_size_t) sent_bytes;
  }
  while (message_to_send_length > 0);

  return true;
} /* jerryx_debugger_tcp_send */

/**
 * Receive data from a tcp connection.

Review comment (bug):
This was not what the previous review was about. I've suggested to change the signature of `jerryx_debugger_tcp_send` to have its parameter as `uint8_t *message_p`. Then, `send` can be called as `send (tcp_p->tcp_socket, (const jerry_socket_void_t *) message_p, message_to_send_length, 0);`



Code after:
static bool
jerryx_debugger_tcp_send (jerry_debugger_transport_header_t *header_p, /**< tcp implementation */
                          uint8_t *message_p, /**< message to be sent */
                          size_t message_length) /**< message length in bytes */
{
  JERRYX_ASSERT (jerry_debugger_transport_is_connected ());

  jerryx_debugger_transport_tcp_t *tcp_p = (jerryx_debugger_transport_tcp_t *) header_p;
  jerryx_socket_size_t remaining_bytes = (jerryx_socket_size_t) message_length;

  do
  {
#ifdef __linux__
    ssize_t is_err = recv (tcp_p->tcp_socket, NULL, 0, MSG_PEEK);

    if (is_err == 0 && errno != JERRYX_EWOULDBLOCK)
    {
      int err_val = errno;
      jerry_debugger_transport_close ();
      jerryx_debugger_tcp_log_error (err_val);
      return false;
    }
#endif /* __linux__ */

    jerryx_socket_ssize_t sent_bytes = send (tcp_p->tcp_socket,
                                             (jerryx_socket_void_t *) message_p,
                                             remaining_bytes,
                                             0);

    if (sent_bytes < 0)
    {
      int err_val = jerryx_debugger_tcp_get_errno ();

      if (err_val == JERRYX_EWOULDBLOCK)
      {
        continue;
      }

      jerry_debugger_transport_close ();
      jerryx_debugger_tcp_log_error (err_val);
      return false;
    }

    message_p += sent_bytes;
    remaining_bytes -= (jerryx_socket_size_t) sent_bytes;
  }
  while (remaining_bytes > 0);

  return true;
} /* jerryx_debugger_tcp_send */