conserves memory and conforms more with general coding style.
plus few cleanups
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-bool client_proto(SBuf *sbuf, SBufEvent evtype, MBuf *pkt, void *arg) _MUSTCHECK;
+bool client_proto(SBuf *sbuf, SBufEvent evtype, MBuf *pkt) _MUSTCHECK;
bool set_pool(PgSocket *client, const char *dbname, const char *username) _MUSTCHECK;
next event loop (eg. too few data available). */
typedef bool (*sbuf_cb_t)(SBuf *sbuf,
SBufEvent evtype,
- MBuf *mbuf,
- void *arg);
+ MBuf *mbuf);
/* for some reason, libevent has no typedef for callback */
typedef void (*sbuf_libevent_cb)(int, short, void *);
int sock; /* fd for this socket */
- unsigned pkt_remain; /* total packet length remaining */
+ unsigned pkt_remain; /* total packet length remaining */
sbuf_cb_t proto_cb; /* protocol callback */
- void *proto_cb_arg; /* extra arg to callback */
SBuf *dst; /* target SBuf for current packet */
- IOBuf *io;
+ IOBuf *io; /* data buffer, lazily allocated */
};
#define sbuf_socket(sbuf) ((sbuf)->sock)
-void sbuf_init(SBuf *sbuf, sbuf_cb_t proto_fn, void *arg);
+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;
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-bool server_proto(SBuf *sbuf, SBufEvent evtype, MBuf *pkt, void *arg) _MUSTCHECK;
+bool server_proto(SBuf *sbuf, SBufEvent evtype, MBuf *pkt) _MUSTCHECK;
}
/* callback from SBuf */
-bool client_proto(SBuf *sbuf, SBufEvent evtype, MBuf *data, void *arg)
+bool client_proto(SBuf *sbuf, SBufEvent evtype, MBuf *data)
{
bool res = false;
- PgSocket *client = arg;
+ PgSocket *client = container_of(sbuf, PgSocket, sbuf);
PktHdr pkt;
memset(client, 0, sizeof(PgSocket));
list_init(&client->head);
- sbuf_init(&client->sbuf, client_proto, client);
+ sbuf_init(&client->sbuf, client_proto);
client->state = CL_FREE;
}
memset(server, 0, sizeof(PgSocket));
list_init(&server->head);
- sbuf_init(&server->sbuf, server_proto, server);
+ sbuf_init(&server->sbuf, server_proto);
server->state = SV_FREE;
}
*********************************/
/* initialize SBuf with proto handler */
-void sbuf_init(SBuf *sbuf, sbuf_cb_t proto_fn, void *arg)
+void sbuf_init(SBuf *sbuf, sbuf_cb_t proto_fn)
{
memset(sbuf, 0, sizeof(SBuf));
- sbuf->proto_cb_arg = arg;
sbuf->proto_cb = proto_fn;
}
AssertActive(sbuf);
event_set(&sbuf->ev, sbuf->sock, EV_READ | EV_PERSIST,
- user_cb, sbuf->proto_cb_arg);
+ user_cb, sbuf);
err = event_add(&sbuf->ev, NULL);
if (err < 0) {
else
memset(&mbuf, 0, sizeof(mbuf));
- res = sbuf->proto_cb(sbuf, event, &mbuf, sbuf->proto_cb_arg);
+ res = sbuf->proto_cb(sbuf, event, &mbuf);
AssertSanity(sbuf);
Assert(event != SBUF_EV_READ || !res || sbuf->sock > 0);
}
/* callback from SBuf */
-bool server_proto(SBuf *sbuf, SBufEvent evtype, MBuf *data, void *arg)
+bool server_proto(SBuf *sbuf, SBufEvent evtype, MBuf *data)
{
bool res = false;
- PgSocket *server = arg;
+ PgSocket *server = container_of(sbuf, PgSocket, sbuf);
PgPool *pool = server->pool;
PktHdr pkt;
*/
static void takeover_recv_cb(int sock, short flags, void *arg)
{
- PgSocket *bouncer = arg;
+ PgSocket *bouncer = container_of(arg, PgSocket, sbuf);
uint8_t data_buf[2048];
uint8_t cnt_buf[128];
struct msghdr msg;