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

Code before:

#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <unistd.h>

/* On *nix the EWOULDBLOCK errno value can be returned for non-blocking operations */
#define JERRYX_EWOULDBLOCK EWOULDBLOCK

/* On *nix the invalid socket has a value of -1 */
#define JERRYX_SOCKET_INVALID (-1)

/* On *nix the sockets are integer identifiers */
typedef int jerryx_socket;
typedef size_t jerryx_socket_size_t;
#endif /* _WIN32 */

/**
 * Implementation of transport over tcp/ip.
 */
typedef struct
{
  jerry_debugger_transport_header_t header; /**< transport header */
  jerryx_socket tcp_socket; /**< tcp socket */
} jerryx_debugger_transport_tcp_t;

/**
 * Get the network error value.
 *
 * On Windows this returns the result of the `WSAGetLastError ()` call and
 * on any other system the `errno` value.
 *
 *
 * @return last error value.
 */
static inline int
jerryx_debugger_tcp_get_errno (void)
{
#ifdef _WIN32
  return WSAGetLastError ();
#else /* !_WIN32 */
  return errno;
#endif /* _WIN32 */
} /* jerryx_debugger_tcp_get_errno */

/**
 * Correctly close a single socket.
 */
static inline void
jerryx_debugger_tcp_close_socket (jerryx_socket socket_id) /**< socket to close */
{

Review comment (suggestion):
On Windows, `send` and `recv` have the following signatures:
`int send(SOCKET s, const char *buf, int len, int flags);`
`int recv(SOCKET s, char *buf, int len, int flags);`

On Linux/POSIX, `send` and `recv` look like:
`ssize_t send(int sockfd, const void *buf, size_t len, int flags);`
`ssize_t recv(int sockfd, void *buf, size_t len, int flags);`

I think it would be useful to have typedefs for the first three arguments, as they are all different on the two platforms. It should look something like the following:

```c
/*
 * On Windows, send has the following signature:
 * int send(SOCKET s, const char *buf, int len, int flags);
 * int recv(SOCKET s, char *buf, int len, int flags);
 */
typedef SOCKET jerryx_socket_t; 
typedef char *jerryx_socket_buffer_t; 
typedef int jerryx_socket_buffer_length_t;
```

```c
/*
 * On *nix, send and recv have the following signatures:
 * ssize_t send(int sockfd, const void *buf, size_t len, int flags);
 * ssize_t recv(int sockfd, void *buf, size_t len, int flags);
 */
typedef int jerryx_socket_t;
typedef void *jerryx_socket_buffer_t;
typedef size_t jerryx_socket_buffer_length_t;
```

Note that the existing `jerryx_socket` type name has also been changed.

Code after:
typedef int jerryx_socket_ssize_t;
typedef SOCKET jerryx_socket_t;
typedef char jerryx_socket_void_t;
typedef int jerryx_socket_size_t;
#else /* !_WIN32 */
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <unistd.h>

/* On *nix the EWOULDBLOCK errno value can be returned for non-blocking operations */
#define JERRYX_EWOULDBLOCK EWOULDBLOCK

/* On *nix the invalid socket has a value of -1 */
#define JERRYX_SOCKET_INVALID (-1)

/*
 * On *nix, socket functions have the following signatures:
 * ssize_t send(int sockfd, const void *buf, size_t len, int flags);
 * ssize_t recv(int sockfd, void *buf, size_t len, int flags);
 * int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
 */
typedef ssize_t jerryx_socket_ssize_t;
typedef int jerryx_socket_t;
typedef void jerryx_socket_void_t;
typedef size_t jerryx_socket_size_t;
#endif /* _WIN32 */

/**
 * Implementation of transport over tcp/ip.
 */
typedef struct
{
  jerry_debugger_transport_header_t header; /**< transport header */
  jerryx_socket_t tcp_socket; /**< tcp socket */
} jerryx_debugger_transport_tcp_t;

/**
 * Get the network error value.
 *
 * On Windows this returns the result of the `WSAGetLastError ()` call and
 * on any other system the `errno` value.
 *
 *
 * @return last error value.
 */
static inline int
jerryx_debugger_tcp_get_errno (void)
{
#ifdef _WIN32
  return WSAGetLastError ();