socket_server.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef skynet_socket_server_h
  2. #define skynet_socket_server_h
  3. #include <stdint.h>
  4. #include "socket_info.h"
  5. #include "socket_buffer.h"
  6. #define SOCKET_DATA 0
  7. #define SOCKET_CLOSE 1
  8. #define SOCKET_OPEN 2
  9. #define SOCKET_ACCEPT 3
  10. #define SOCKET_ERR 4
  11. #define SOCKET_EXIT 5
  12. #define SOCKET_UDP 6
  13. #define SOCKET_WARNING 7
  14. // Only for internal use
  15. #define SOCKET_RST 8
  16. #define SOCKET_MORE 9
  17. struct socket_server;
  18. struct socket_message {
  19. int id;
  20. uintptr_t opaque;
  21. int ud; // for accept, ud is new connection id ; for data, ud is size of data
  22. char * data;
  23. };
  24. struct socket_server * socket_server_create(uint64_t time);
  25. void socket_server_release(struct socket_server *);
  26. void socket_server_updatetime(struct socket_server *, uint64_t time);
  27. int socket_server_poll(struct socket_server *, struct socket_message *result, int *more);
  28. void socket_server_exit(struct socket_server *);
  29. void socket_server_close(struct socket_server *, uintptr_t opaque, int id);
  30. void socket_server_shutdown(struct socket_server *, uintptr_t opaque, int id);
  31. void socket_server_start(struct socket_server *, uintptr_t opaque, int id);
  32. void socket_server_pause(struct socket_server *, uintptr_t opaque, int id);
  33. // return -1 when error
  34. int socket_server_send(struct socket_server *, struct socket_sendbuffer *buffer);
  35. int socket_server_send_lowpriority(struct socket_server *, struct socket_sendbuffer *buffer);
  36. // ctrl command below returns id
  37. int socket_server_listen(struct socket_server *, uintptr_t opaque, const char * addr, int port, int backlog);
  38. int socket_server_connect(struct socket_server *, uintptr_t opaque, const char * addr, int port);
  39. int socket_server_bind(struct socket_server *, uintptr_t opaque, int fd);
  40. // for tcp
  41. void socket_server_nodelay(struct socket_server *, int id);
  42. struct socket_udp_address;
  43. // create an udp socket handle, attach opaque with it . udp socket don't need call socket_server_start to recv message
  44. // if port != 0, bind the socket . if addr == NULL, bind ipv4 0.0.0.0 . If you want to use ipv6, addr can be "::" and port 0.
  45. int socket_server_udp(struct socket_server *, uintptr_t opaque, const char * addr, int port);
  46. // set default dest address, return 0 when success
  47. int socket_server_udp_connect(struct socket_server *, int id, const char * addr, int port);
  48. // If the socket_udp_address is NULL, use last call socket_server_udp_connect address instead
  49. // You can also use socket_server_send
  50. int socket_server_udp_send(struct socket_server *, const struct socket_udp_address *, struct socket_sendbuffer *buffer);
  51. // extract the address of the message, struct socket_message * should be SOCKET_UDP
  52. const struct socket_udp_address * socket_server_udp_address(struct socket_server *, struct socket_message *, int *addrsz);
  53. struct socket_object_interface {
  54. const void * (*buffer)(const void *);
  55. size_t (*size)(const void *);
  56. void (*free)(void *);
  57. };
  58. // if you send package with type SOCKET_BUFFER_OBJECT, use soi.
  59. void socket_server_userobject(struct socket_server *, struct socket_object_interface *soi);
  60. struct socket_info * socket_server_info(struct socket_server *);
  61. #endif