net.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef _NET_H
  2. #define _NET_H
  3. #include "common.h"
  4. #include "array.h"
  5. #include "file.h"
  6. #ifdef WIN32
  7. /* the next few lines mean we need winXP or better,
  8. * better being non-windows, really */
  9. #ifdef _WIN32_WINNT
  10. #undef _WIN32_WINNT
  11. #endif
  12. #define _WIN32_WINNT 0x0501
  13. #include <winsock2.h>
  14. #include <ws2tcpip.h>
  15. #else
  16. #include <sys/socket.h>
  17. #include <sys/select.h>
  18. #include <netdb.h>
  19. #include <netinet/in.h>
  20. #endif
  21. #define NETSTATE_UNUSED 0
  22. #define NETSTATE_OPEN 1
  23. #define NETSTATE_CLOSED 2
  24. #define NETTYPE_TCP 1
  25. #define NETTYPE_UDP 2
  26. #define NETTYPE_TCP_HOST 3
  27. #define NETTYPE_UDP_HOST 4
  28. #ifndef _HAVE_NETTCP_BUFFER_TYPE
  29. #define _HAVE_NETTCP_BUFFER_TYPE
  30. typedef struct net_tcp_buffer_s {
  31. unsigned char buff[1024];
  32. int start;
  33. int end;
  34. } net_tcp_buffer_t;
  35. #endif
  36. #ifndef _HAVE_NETUDP_BUFFER_TYPE
  37. #define _HAVE_NETUDP_BUFFER_TYPE
  38. typedef struct net_udp_buffer_s {
  39. file_t *unsorted;
  40. file_t *sorted;
  41. } net_udp_buffer_t;
  42. #endif
  43. #ifndef _HAVE_NET_TYPE
  44. #define _HAVE_NET_TYPE
  45. typedef struct net_connection_s {
  46. struct net_connection_s *prev;
  47. struct net_connection_s *next;
  48. struct addrinfo hints;
  49. struct addrinfo *addr;
  50. struct sockaddr_storage remote_addr;
  51. socklen_t remote_addr_len;
  52. int id;
  53. int type;
  54. int fd;
  55. int state;
  56. int tries;
  57. char* host;
  58. char* port;
  59. union {
  60. net_tcp_buffer_t tcp;
  61. net_udp_buffer_t udp;
  62. } buff;
  63. array_t *peers;
  64. } net_connection_t;
  65. #endif
  66. /* defined in net.c */
  67. int net_init(void);
  68. net_connection_t *net_connection(void);
  69. net_connection_t *net_fetch(int id);
  70. int net_state(net_connection_t *n);
  71. void net_disconnect(net_connection_t *n);
  72. void net_close(net_connection_t *n);
  73. array_t *net_select(unsigned int msec, int reconnect, net_connection_t *n);
  74. array_t *net_select_array(unsigned int msec, int reconnect, array_t *a);
  75. int net_write(net_connection_t *n, void *buff, unsigned int size);
  76. int net_write_string(net_connection_t *n, char* fmt, ...);
  77. int net_read(net_connection_t *n, void *buff, unsigned int size);
  78. int net_readline(net_connection_t *n, void *buff, unsigned int size);
  79. int net_broadcast(net_connection_t *n, void *buff, unsigned int size);
  80. int net_accept(net_connection_t *n);
  81. /* defined in net_udp.c */
  82. file_t *udp_next(net_connection_t *n);
  83. #ifdef _NET_LOCAL
  84. /* defined in net_tcp.c */
  85. net_connection_t *tcp_client_connect(char* host, char* port);
  86. int tcp_client_reconnect(net_connection_t *n);
  87. net_connection_t *tcp_host_connect(char* host, char* port);
  88. int tcp_host_reconnect(net_connection_t *n);
  89. int tcp_write(net_connection_t *n, void *buff, unsigned int size);
  90. int tcp_pending(net_connection_t *n);
  91. int tcp_read(net_connection_t *n, void *buff, unsigned int size);
  92. int tcp_readline(net_connection_t *n, void *buf, unsigned int size);
  93. int tcp_broadcast(net_connection_t *n, void *buff, unsigned int size);
  94. int tcp_accept(net_connection_t *n);
  95. /* defined in net_udp.c */
  96. net_connection_t *udp_client_connect(char* host, char* port);
  97. int udp_client_reconnect(net_connection_t *n);
  98. net_connection_t *udp_host_connect(char* host, char* port);
  99. int udp_host_reconnect(net_connection_t *n);
  100. int udp_write(net_connection_t *n, void *buff, unsigned int size);
  101. int udp_pending(net_connection_t *n);
  102. int udp_read(net_connection_t *n, void *buff, unsigned int size);
  103. int udp_readline(net_connection_t *n, void *buf, unsigned int size);
  104. int udp_broadcast(net_connection_t *n, void *buff, unsigned int size);
  105. int udp_accept(net_connection_t *n);
  106. #endif
  107. #endif