skynet_socket.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef skynet_socket_h
  2. #define skynet_socket_h
  3. #include "socket_info.h"
  4. #include "socket_buffer.h"
  5. struct skynet_context;
  6. #define SKYNET_SOCKET_TYPE_DATA 1
  7. #define SKYNET_SOCKET_TYPE_CONNECT 2
  8. #define SKYNET_SOCKET_TYPE_CLOSE 3
  9. #define SKYNET_SOCKET_TYPE_ACCEPT 4
  10. #define SKYNET_SOCKET_TYPE_ERROR 5
  11. #define SKYNET_SOCKET_TYPE_UDP 6
  12. #define SKYNET_SOCKET_TYPE_WARNING 7
  13. struct skynet_socket_message {
  14. int type;
  15. int id;
  16. int ud;
  17. char * buffer;
  18. };
  19. void skynet_socket_init();
  20. void skynet_socket_exit();
  21. void skynet_socket_free();
  22. int skynet_socket_poll();
  23. void skynet_socket_updatetime();
  24. int skynet_socket_sendbuffer(struct skynet_context *ctx, struct socket_sendbuffer *buffer);
  25. int skynet_socket_sendbuffer_lowpriority(struct skynet_context *ctx, struct socket_sendbuffer *buffer);
  26. int skynet_socket_listen(struct skynet_context *ctx, const char *host, int port, int backlog);
  27. int skynet_socket_connect(struct skynet_context *ctx, const char *host, int port);
  28. int skynet_socket_bind(struct skynet_context *ctx, int fd);
  29. void skynet_socket_close(struct skynet_context *ctx, int id);
  30. void skynet_socket_shutdown(struct skynet_context *ctx, int id);
  31. void skynet_socket_start(struct skynet_context *ctx, int id);
  32. void skynet_socket_pause(struct skynet_context *ctx, int id);
  33. void skynet_socket_nodelay(struct skynet_context *ctx, int id);
  34. int skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port);
  35. int skynet_socket_udp_connect(struct skynet_context *ctx, int id, const char * addr, int port);
  36. int skynet_socket_udp_sendbuffer(struct skynet_context *ctx, const char * address, struct socket_sendbuffer *buffer);
  37. const char * skynet_socket_udp_address(struct skynet_socket_message *, int *addrsz);
  38. struct socket_info * skynet_socket_info();
  39. // legacy APIs
  40. static inline void sendbuffer_init_(struct socket_sendbuffer *buf, int id, const void *buffer, int sz) {
  41. buf->id = id;
  42. buf->buffer = buffer;
  43. if (sz < 0) {
  44. buf->type = SOCKET_BUFFER_OBJECT;
  45. } else {
  46. buf->type = SOCKET_BUFFER_MEMORY;
  47. }
  48. buf->sz = (size_t)sz;
  49. }
  50. static inline int skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz) {
  51. struct socket_sendbuffer tmp;
  52. sendbuffer_init_(&tmp, id, buffer, sz);
  53. return skynet_socket_sendbuffer(ctx, &tmp);
  54. }
  55. static inline int skynet_socket_send_lowpriority(struct skynet_context *ctx, int id, void *buffer, int sz) {
  56. struct socket_sendbuffer tmp;
  57. sendbuffer_init_(&tmp, id, buffer, sz);
  58. return skynet_socket_sendbuffer_lowpriority(ctx, &tmp);
  59. }
  60. static inline int skynet_socket_udp_send(struct skynet_context *ctx, int id, const char * address, const void *buffer, int sz) {
  61. struct socket_sendbuffer tmp;
  62. sendbuffer_init_(&tmp, id, buffer, sz);
  63. return skynet_socket_udp_sendbuffer(ctx, address, &tmp);
  64. }
  65. #endif