da_conn.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef DA_CONN_H_
  17. #define DA_CONN_H_
  18. #include "compiler.h"
  19. #include "da_errno.h"
  20. #include "da_msg.h"
  21. #include "da_queue.h"
  22. #include "da_rbtree.h"
  23. #include "da_util.h"
  24. #include <stdbool.h>
  25. #include <stdint.h>
  26. #include <sys/socket.h>
  27. struct conn;
  28. struct context;
  29. struct msg;
  30. // connection type
  31. #define FRONTWORK 0x0001
  32. #define BACKWORK 0x0002
  33. #define LISTENER 0x0004
  34. // connection epoll flag
  35. #define RECV_ACTIVE 0x0001
  36. #define SEND_ACTIVE 0x0002
  37. #define RECV_READY 0x0004
  38. #define SEND_READY 0x0008
  39. typedef int (*conn_recv_t)(struct context *, struct conn *);
  40. typedef struct msg *(*conn_recv_next_t)(struct context *, struct conn *, bool);
  41. typedef void (*conn_recv_done_t)(struct context *, struct conn *, struct msg *,
  42. struct msg *);
  43. typedef int (*conn_send_t)(struct context *, struct conn *);
  44. typedef struct msg *(*conn_send_next_t)(struct context *, struct conn *);
  45. typedef void (*conn_send_done_t)(struct context *, struct conn *, struct msg *);
  46. typedef void (*conn_close_t)(struct context *, struct conn *);
  47. typedef int (*conn_active_t)(struct conn *);
  48. typedef void (*conn_ref_t)(struct conn *, void *);
  49. typedef void (*conn_unref_t)(struct conn *);
  50. typedef void (*conn_msgq_t)(struct context *, struct conn *, struct msg *);
  51. typedef void (*conn_msgtree_t)(struct context *, struct conn *, struct msg *);
  52. typedef enum conn_stage{
  53. CONN_STAGE_UNLOGIN = 0,
  54. CONN_STAGE_LOGGING_IN,
  55. CONN_STAGE_SWITCH_NATIVE_PASSWD,
  56. CONN_STAGE_LOGGED_IN,
  57. CONN_STAGE_DEFAULT
  58. }conn_stage_t;
  59. struct conn {
  60. TAILQ_ENTRY(conn) conn_tqe; /*list linked in server or server pool*/
  61. void *owner; /*owner server server_pool*/
  62. int fd; /*socket fd*/
  63. int family; /*socket family*/
  64. socklen_t addrlen; /*socket addr len*/
  65. struct sockaddr *addr; /*socket address (ref in server)*/
  66. uint32_t type; /*front conn,back conn,or listener*/
  67. uint32_t events; /*the event need process*/
  68. uint32_t flag; /*epool flag*/
  69. conn_stage_t stage; /* authorization stage */
  70. char dbname[250]; /* use db info */
  71. struct rbtree msg_tree; /*tree for message search*/
  72. struct rbnode msg_rbs; /*sentinel for msg_tree */
  73. struct msg_tqh imsg_q; /*request msgq */
  74. struct msg_tqh omsg_q; /*outstanding request Q */
  75. struct msg *rmsg; /* current message being rcvd */
  76. struct msg *smsg; /* current message being sent */
  77. conn_recv_t recv; /* recv (read) handler */
  78. conn_recv_next_t recv_next; /* recv next message handler */
  79. conn_recv_done_t recv_done; /* read done handler */
  80. conn_send_t send; /* send (write) handler */
  81. conn_send_next_t send_next; /* write next message handler */
  82. conn_send_done_t send_done; /* write done handler */
  83. conn_close_t close; /* close handler */
  84. conn_active_t active; /* active? handler */
  85. conn_ref_t ref; /* connection reference handler */
  86. conn_unref_t unref; /* connection unreference handler */
  87. conn_msgq_t enqueue_outq; /* connection outq msg enqueue handler */
  88. conn_msgq_t dequeue_outq; /* connection outq msg dequeue handler */
  89. conn_msgq_t enqueue_inq; /* connection outq msg enqueue handler */
  90. conn_msgq_t dequeue_inq; /* connection outq msg dequeue handler */
  91. conn_msgtree_t en_msgtree; /* connection msg tree entree handler*/
  92. conn_msgtree_t de_msgtree; /* connection msg tree detree handler*/
  93. size_t recv_bytes; /* received (read) bytes */
  94. size_t send_bytes; /* sent (written) bytes */
  95. int err; /*connection error no*/
  96. unsigned error : 1; /*connetion err sig*/
  97. unsigned connecting : 1; /*connecting is on*/
  98. unsigned connected : 1; /*connected*/
  99. unsigned eof : 1; /*connection is end,half connection*/
  100. unsigned done : 1; /*connection is done*/
  101. unsigned writecached : 1;
  102. unsigned isvalid : 1;
  103. };
  104. TAILQ_HEAD(conn_tqh, conn);
  105. /*
  106. * init the pool2_conn
  107. */
  108. int conn_init();
  109. /*
  110. * deinit conn pool
  111. */
  112. int conn_deinit();
  113. /*
  114. * put conn back to pool
  115. */
  116. void conn_put(struct conn *c);
  117. /*
  118. *get listener from conn pool
  119. */
  120. struct conn *get_listener(void *owner);
  121. /*
  122. *get client conn
  123. */
  124. struct conn *get_client_conn(void *owner);
  125. /*
  126. *get server conn
  127. */
  128. struct conn *get_server_conn(void *owner);
  129. /*
  130. *get instance conn
  131. */
  132. struct conn *get_instance_conn(void *owner);
  133. /*
  134. * get number of total conn
  135. */
  136. uint64_t get_ntotal_conn();
  137. /*
  138. * get number of current conn
  139. */
  140. uint32_t get_ncurr_conn();
  141. /*
  142. *get number of current client conn
  143. */
  144. uint32_t get_ncurr_cconn();
  145. /*recv data from fd*/
  146. ssize_t conn_recv(struct conn *conn, void *buf, size_t size);
  147. /* send data to fd*/
  148. ssize_t conn_sendv(struct conn *conn, struct array *sendv, size_t nsend);
  149. struct context *conn_to_ctx(struct conn *conn);
  150. #endif /* DA_CONN_H_ */