Network

TCP networking

  • TcpClient — TCP client for connecting to a server
  • TcpServer — TCP server for accepting and managing client connections
#include <uhcnet.uhh>
namespace TcpClient {
  #if defined(_WIN32) || defined(_WIN64)
    struct It {
      SOCKET sockfd = INVALID_SOCKET
      HANDLE thread = NULL
    }
  #else
    struct It {
      I32 sockfd = -1
      pthread_t thread = NULL
    }
  #endif
  TcpClient create(I8* ip, I16 port)
  U0 destroy(TcpClient client)
  U8 write(TcpClient client, I8* buffer, U32 size)
  U8 read(TcpClient client, I8* buffer, U32 size)
  U8 readAll(TcpClient client, I8* buffer, U32 size)
}

namespace TcpServer {
  #if defined(_WIN32) || defined(_WIN64)
    struct It {
      SOCKET sockfd = INVALID_SOCKET
      U8 running = 0
      U16 slots = 0
    }
  #else
    struct It {
      I32 sockfd = -1
      U8 running = 0
      U16 slots = 0
    }
  #endif
  TcpServer create(I16 port, U16 slots)
  U0 destroy(TcpServer server)
  U0 listen(TcpServer server, U0 (*callback)(TcpClient*))
  U8 write(TcpClient client, I8* buffer, U32 size)
  U8 read(TcpClient client, I8* buffer, U32 size)
  U8 readAll(TcpClient client, I8* buffer, U32 size)
  U0 broadcast(List<TcpClient> clients, I8* buffer, U32 size)
}