1 /* 2 * twemproxy - A fast and lightweight proxy for memcached protocol. 3 * Copyright (C) 2011 Twitter, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #ifndef _NC_CONNECTION_H_ 19 #define _NC_CONNECTION_H_ 20 21 #include <nc_core.h> 22 23 typedef rstatus_t (*conn_recv_t)(struct context *, struct conn*); 24 typedef struct msg* (*conn_recv_next_t)(struct context *, struct conn *, bool); 25 typedef void (*conn_recv_done_t)(struct context *, struct conn *, struct msg *, struct msg *); 26 27 typedef rstatus_t (*conn_send_t)(struct context *, struct conn*); 28 typedef struct msg* (*conn_send_next_t)(struct context *, struct conn *); 29 typedef void (*conn_send_done_t)(struct context *, struct conn *, struct msg *); 30 31 typedef void (*conn_close_t)(struct context *, struct conn *); 32 typedef bool (*conn_active_t)(const struct conn *); 33 34 typedef void (*conn_ref_t)(struct conn *, void *); 35 typedef void (*conn_unref_t)(struct conn *); 36 37 typedef void (*conn_msgq_t)(struct context *, struct conn *, struct msg *); 38 typedef void (*conn_post_connect_t)(struct context *ctx, struct conn *, struct server *server); 39 typedef void (*conn_swallow_msg_t)(struct conn *, struct msg *, struct msg *); 40 41 struct conn { 42 TAILQ_ENTRY(conn) conn_tqe; /* link in server_pool / server / free q */ 43 void *owner; /* connection owner - server_pool / server */ 44 45 int sd; /* socket descriptor */ 46 int family; /* socket address family */ 47 socklen_t addrlen; /* socket length */ 48 struct sockaddr *addr; /* socket address (ref in server or server_pool) */ 49 50 struct msg_tqh imsg_q; /* incoming request Q */ 51 struct msg_tqh omsg_q; /* outstanding request Q */ 52 struct msg *rmsg; /* current message being rcvd */ 53 struct msg *smsg; /* current message being sent */ 54 55 conn_recv_t recv; /* recv (read) handler */ 56 conn_recv_next_t recv_next; /* recv next message handler */ 57 conn_recv_done_t recv_done; /* read done handler */ 58 conn_send_t send; /* send (write) handler */ 59 conn_send_next_t send_next; /* write next message handler */ 60 conn_send_done_t send_done; /* write done handler */ 61 conn_close_t close; /* close handler */ 62 conn_active_t active; /* active? handler */ 63 conn_post_connect_t post_connect; /* post connect handler */ 64 conn_swallow_msg_t swallow_msg; /* react on messages to be swallowed */ 65 66 conn_ref_t ref; /* connection reference handler */ 67 conn_unref_t unref; /* connection unreference handler */ 68 69 conn_msgq_t enqueue_inq; /* connection inq msg enqueue handler */ 70 conn_msgq_t dequeue_inq; /* connection inq msg dequeue handler */ 71 conn_msgq_t enqueue_outq; /* connection outq msg enqueue handler */ 72 conn_msgq_t dequeue_outq; /* connection outq msg dequeue handler */ 73 74 size_t recv_bytes; /* received (read) bytes */ 75 size_t send_bytes; /* sent (written) bytes */ 76 77 uint32_t events; /* connection io events */ 78 err_t err; /* connection errno */ 79 unsigned recv_active:1; /* recv active? */ 80 unsigned recv_ready:1; /* recv ready? */ 81 unsigned send_active:1; /* send active? */ 82 unsigned send_ready:1; /* send ready? */ 83 84 unsigned client:1; /* client? or server? */ 85 unsigned proxy:1; /* proxy? */ 86 unsigned connecting:1; /* connecting? */ 87 unsigned connected:1; /* connected? */ 88 unsigned eof:1; /* eof? aka passive close? */ 89 unsigned done:1; /* done? aka close? */ 90 unsigned redis:1; /* redis? */ 91 unsigned authenticated:1; /* authenticated? */ 92 }; 93 94 TAILQ_HEAD(conn_tqh, conn); 95 96 struct context *conn_to_ctx(const struct conn *conn); 97 struct conn *conn_get(void *owner, bool client, bool redis); 98 struct conn *conn_get_proxy(struct server_pool *pool); 99 void conn_put(struct conn *conn); 100 ssize_t conn_recv(struct conn *conn, void *buf, size_t size); 101 ssize_t conn_sendv(struct conn *conn, const struct array *sendv, size_t nsend); 102 void conn_init(void); 103 void conn_deinit(void); 104 uint32_t conn_ncurr_conn(void); 105 uint64_t conn_ntotal_conn(void); 106 uint32_t conn_ncurr_cconn(void); 107 bool conn_authenticated(const struct conn *conn); 108 109 #endif 110