1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
/*
* PgBouncer - Lightweight connection pooler for PostgreSQL.
*
* Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* event types for protocol handler
*/
typedef enum {
SBUF_EV_READ, /* got new packet */
SBUF_EV_RECV_FAILED, /* error */
SBUF_EV_SEND_FAILED, /* error */
SBUF_EV_CONNECT_FAILED, /* error */
SBUF_EV_CONNECT_OK, /* got connection */
SBUF_EV_FLUSH, /* data is sent, buffer empty */
SBUF_EV_PKT_CALLBACK, /* next part of pkt data */
} SBufEvent;
/*
* If less that this amount of data is pending, then
* prefer to merge it with next recv().
*
* It needs to be larger than data handler wants
* to see completely. Generally just header,
* but currently also ServerParam pkt.
*/
#define SBUF_SMALL_PKT 64
/* fwd def */
typedef struct SBuf SBuf;
/* callback should return true if it used one of sbuf_prepare_* on sbuf,
false if it used sbuf_pause(), sbuf_close() or simply wants to wait for
next event loop (eg. too few data available). */
typedef bool (*sbuf_cb_t)(SBuf *sbuf,
SBufEvent evtype,
MBuf *mbuf);
/* for some reason, libevent has no typedef for callback */
typedef void (*sbuf_libevent_cb)(int, short, void *);
/*
* Stream Buffer.
*
* Stream is divided to packets. On each packet start
* protocol handler is called that decides what to do.
*/
struct SBuf {
struct event ev; /* libevent handle */
bool is_unix; /* is it unix socket */
uint8_t wait_type; /* track wait state */
uint8_t pkt_action; /* method for handling current pkt */
int sock; /* fd for this socket */
unsigned pkt_remain; /* total packet length remaining */
sbuf_cb_t proto_cb; /* protocol callback */
SBuf *dst; /* target SBuf for current packet */
IOBuf *io; /* data buffer, lazily allocated */
};
#define sbuf_socket(sbuf) ((sbuf)->sock)
void sbuf_init(SBuf *sbuf, sbuf_cb_t proto_fn);
bool sbuf_accept(SBuf *sbuf, int read_sock, bool is_unix) _MUSTCHECK;
bool sbuf_connect(SBuf *sbuf, const PgAddr *addr, const char *unix_dir, int timeout_sec) _MUSTCHECK;
bool sbuf_pause(SBuf *sbuf) _MUSTCHECK;
void sbuf_continue(SBuf *sbuf);
bool sbuf_close(SBuf *sbuf) _MUSTCHECK;
/* proto_fn can use those functions to order behaviour */
void sbuf_prepare_send(SBuf *sbuf, SBuf *dst, unsigned amount);
void sbuf_prepare_skip(SBuf *sbuf, unsigned amount);
void sbuf_prepare_fetch(SBuf *sbuf, unsigned amount);
bool sbuf_answer(SBuf *sbuf, const void *buf, unsigned len) _MUSTCHECK;
bool sbuf_continue_with_callback(SBuf *sbuf, sbuf_libevent_cb cb) _MUSTCHECK;
/*
* Returns true if SBuf is has no data buffered
* and is not in a middle of a packet.
*/
static inline bool sbuf_is_empty(SBuf *sbuf)
{
return iobuf_empty(sbuf->io) && sbuf->pkt_remain == 0;
}
static inline bool sbuf_is_closed(SBuf *sbuf)
{
return sbuf->sock == 0;
}
|