vectorize socket_write()

the objective is making the buffer code aware of the total size of a
write in advance. this commit doesn't take advantage of that yet.
This commit is contained in:
Oswald Buddenhagen 2014-10-26 21:10:25 +01:00
parent 3f629af07e
commit efb23ab96a
3 changed files with 61 additions and 28 deletions

View File

@ -260,8 +260,9 @@ done_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd, int response )
static int static int
send_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd ) send_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd )
{ {
int bufl, litplus; int bufl, litplus, iovcnt = 1;
const char *buffmt; const char *buffmt;
conn_iovec_t iov[3];
char buf[1024]; char buf[1024];
cmd->tag = ++ctx->nexttag; cmd->tag = ++ctx->nexttag;
@ -286,15 +287,21 @@ send_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd )
printf( "%s>>> %d LOGIN <user> <pass>\n", ctx->label, cmd->tag ); printf( "%s>>> %d LOGIN <user> <pass>\n", ctx->label, cmd->tag );
fflush( stdout ); fflush( stdout );
} }
if (socket_write( &ctx->conn, buf, bufl, KeepOwn ) < 0) iov[0].buf = buf;
goto bail; iov[0].len = bufl;
iov[0].takeOwn = KeepOwn;
if (litplus) { if (litplus) {
char *p = cmd->param.data; iov[1].buf = cmd->param.data;
iov[1].len = cmd->param.data_len;
iov[1].takeOwn = GiveOwn;
cmd->param.data = 0; cmd->param.data = 0;
if (socket_write( &ctx->conn, p, cmd->param.data_len, GiveOwn ) < 0 || iov[2].buf = "\r\n";
socket_write( &ctx->conn, "\r\n", 2, KeepOwn ) < 0) iov[2].len = 2;
goto bail; iov[2].takeOwn = KeepOwn;
iovcnt = 3;
} }
if (socket_write( &ctx->conn, iov, iovcnt ) < 0)
goto bail;
if (cmd->param.to_trash && ctx->trashnc == TrashUnknown) if (cmd->param.to_trash && ctx->trashnc == TrashUnknown)
ctx->trashnc = TrashChecking; ctx->trashnc = TrashChecking;
cmd->next = 0; cmd->next = 0;
@ -1195,6 +1202,7 @@ imap_socket_read( void *aux )
struct imap_cmd *cmdp, **pcmdp; struct imap_cmd *cmdp, **pcmdp;
char *cmd, *arg, *arg1, *p; char *cmd, *arg, *arg1, *p;
int resp, resp2, tag, greeted; int resp, resp2, tag, greeted;
conn_iovec_t iov[2];
greeted = ctx->greeting; greeted = ctx->greeting;
for (;;) { for (;;) {
@ -1273,10 +1281,14 @@ imap_socket_read( void *aux )
if (cmdp->param.data) { if (cmdp->param.data) {
if (cmdp->param.to_trash) if (cmdp->param.to_trash)
ctx->trashnc = TrashKnown; /* Can't get NO [TRYCREATE] any more. */ ctx->trashnc = TrashKnown; /* Can't get NO [TRYCREATE] any more. */
p = cmdp->param.data; iov[0].buf = cmdp->param.data;
iov[0].len = cmdp->param.data_len;
iov[0].takeOwn = GiveOwn;
cmdp->param.data = 0; cmdp->param.data = 0;
if (socket_write( &ctx->conn, p, cmdp->param.data_len, GiveOwn ) < 0 || iov[1].buf = "\r\n";
socket_write( &ctx->conn, "\r\n", 2, KeepOwn ) < 0) iov[1].len = 2;
iov[1].takeOwn = KeepOwn;
if (socket_write( &ctx->conn, iov, 2 ) < 0)
return; return;
} else if (cmdp->param.cont) { } else if (cmdp->param.cont) {
if (cmdp->param.cont( ctx, cmdp, cmd )) if (cmdp->param.cont( ctx, cmdp, cmd ))
@ -1803,11 +1815,12 @@ encode_sasl_data( const char *out, uint out_len, char **enc, uint *enc_len )
static int static int
do_sasl_auth( imap_store_t *ctx, struct imap_cmd *cmdp ATTR_UNUSED, const char *prompt ) do_sasl_auth( imap_store_t *ctx, struct imap_cmd *cmdp ATTR_UNUSED, const char *prompt )
{ {
int rc, ret; int rc, ret, iovcnt = 0;
uint in_len, out_len, enc_len; uint in_len, out_len, enc_len;
const char *out; const char *out;
char *in, *enc; char *in, *enc;
sasl_interact_t *interact = NULL; sasl_interact_t *interact = NULL;
conn_iovec_t iov[2];
if (!ctx->sasl_cont) { if (!ctx->sasl_cont) {
error( "Error: IMAP wants more steps despite successful SASL authentication.\n" ); error( "Error: IMAP wants more steps despite successful SASL authentication.\n" );
@ -1825,20 +1838,26 @@ do_sasl_auth( imap_store_t *ctx, struct imap_cmd *cmdp ATTR_UNUSED, const char *
if (encode_sasl_data( out, out_len, &enc, &enc_len ) < 0) if (encode_sasl_data( out, out_len, &enc, &enc_len ) < 0)
goto bail; goto bail;
iov[0].buf = enc;
iov[0].len = enc_len;
iov[0].takeOwn = GiveOwn;
iovcnt = 1;
if (DFlags & VERBOSE) { if (DFlags & VERBOSE) {
printf( "%s>+> %s\n", ctx->label, enc ); printf( "%s>+> %s\n", ctx->label, enc );
fflush( stdout ); fflush( stdout );
} }
if (socket_write( &ctx->conn, enc, enc_len, GiveOwn ) < 0)
return -1;
} else { } else {
if (DFlags & VERBOSE) { if (DFlags & VERBOSE) {
printf( "%s>+>\n", ctx->label ); printf( "%s>+>\n", ctx->label );
fflush( stdout ); fflush( stdout );
} }
} }
return socket_write( &ctx->conn, "\r\n", 2, KeepOwn ); iov[iovcnt].buf = "\r\n";
iov[iovcnt].len = 2;
iov[iovcnt].takeOwn = KeepOwn;
iovcnt++;
return socket_write( &ctx->conn, iov, iovcnt );
bail: bail:
imap_open_store_bail( ctx ); imap_open_store_bail( ctx );

View File

@ -660,22 +660,31 @@ do_append( conn_t *conn, char *buf, int len, ownership_t takeOwn )
} }
int int
socket_write( conn_t *conn, char *buf, int len, ownership_t takeOwn ) socket_write( conn_t *conn, conn_iovec_t *iov, int iovcnt )
{ {
for (; iovcnt; iovcnt--, iov++) {
if (conn->write_buf) { if (conn->write_buf) {
do_append( conn, buf, len, takeOwn ); do_append( conn, iov->buf, iov->len, iov->takeOwn );
return len;
} else { } else {
int n = do_write( conn, buf, len ); int n = do_write( conn, iov->buf, iov->len );
if (n != len && n >= 0) { if (n < 0) {
do {
if (iov->takeOwn == GiveOwn)
free( iov->buf );
iovcnt--, iov++;
} while (iovcnt);
return -1;
}
if (n != iov->len) {
conn->write_offset = n; conn->write_offset = n;
do_append( conn, buf, len, takeOwn ); do_append( conn, iov->buf, iov->len, iov->takeOwn );
} else if (takeOwn) { } else if (iov->takeOwn == GiveOwn) {
free( buf ); free( iov->buf );
} }
return n;
} }
} }
return 0;
}
static void static void
socket_fd_cb( int events, void *aux ) socket_fd_cb( int events, void *aux )

View File

@ -123,6 +123,11 @@ void socket_close( conn_t *sock );
int socket_read( conn_t *sock, char *buf, int len ); /* never waits */ int socket_read( conn_t *sock, char *buf, int len ); /* never waits */
char *socket_read_line( conn_t *sock ); /* don't free return value; never waits */ char *socket_read_line( conn_t *sock ); /* don't free return value; never waits */
typedef enum { KeepOwn = 0, GiveOwn } ownership_t; typedef enum { KeepOwn = 0, GiveOwn } ownership_t;
int socket_write( conn_t *sock, char *buf, int len, ownership_t takeOwn ); typedef struct conn_iovec {
char *buf;
int len;
ownership_t takeOwn;
} conn_iovec_t;
int socket_write( conn_t *sock, conn_iovec_t *iov, int iovcnt );
#endif #endif