Skip to content

Commit

Permalink
MINOR : vzsock_send() & vzsock_recv_str() functions were added
Browse files Browse the repository at this point in the history
  • Loading branch information
krasnov committed Aug 26, 2008
1 parent b4731bb commit 2fe3993
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 8 deletions.
15 changes: 13 additions & 2 deletions libvzsock.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct vzsock_ctx {
#define VZS_ERR_BAD_PARAM 3
#define VZS_ERR_TIMEOUT 4
#define VZS_ERR_CONN_BROKEN 5

#define VZS_ERR_TOOLONG 6
#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -72,7 +72,18 @@ int vzsock_create_conn(struct vzsock_ctx *ctx,
int vzsock_close_conn(struct vzsock_ctx *ctx, void *conn);
int vzsock_set_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t size);

int vzsock_send(
struct vzsock_ctx *ctx,
void *conn,
const char * data,
size_t size);
/* read string, separated by <separator>. Will write '\0' on end of string */
int vzsock_recv_str(
struct vzsock_ctx *ctx,
void *conn,
char separator,
char *data,
size_t size);
#ifdef __cplusplus
}
#endif
Expand Down
24 changes: 24 additions & 0 deletions sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ static int _send(
void *conn,
const char * data,
size_t size);
static int recv_str(
struct vzsock_ctx *ctx,
void *conn,
char separator,
char *data,
size_t size);


int _vzs_sock_init(struct vzsock_ctx *ctx, struct vzs_handlers *handlers)
Expand All @@ -55,6 +61,7 @@ int _vzs_sock_init(struct vzsock_ctx *ctx, struct vzs_handlers *handlers)
handlers->close_conn = close_conn;
handlers->set_conn = set_conn;
handlers->send = _send;
handlers->recv_str = recv_str;

return 0;
}
Expand Down Expand Up @@ -171,3 +178,20 @@ static int _send(

return _vzs_writefd(ctx, cn->sock, data, size);
}

/*
read from nonblocking descriptor <fd> string, separated by <separator>.
will write '\0' on the end of string
*/
static int recv_str(
struct vzsock_ctx *ctx,
void *conn,
char separator,
char *data,
size_t size)
{
struct sock_conn *cn = (struct sock_conn *)conn;

return _vzs_recv_str(ctx, cn->sock, separator, data, size);
}

24 changes: 24 additions & 0 deletions ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ static int send(
void *conn,
const char * data,
size_t size);
static int recv_str(
struct vzsock_ctx *ctx,
void *conn,
char separator,
char *data,
size_t size);


int _vzs_ssh_init(struct vzsock_ctx *ctx, struct vzs_handlers *handlers)
Expand All @@ -58,6 +64,7 @@ int _vzs_ssh_init(struct vzsock_ctx *ctx, struct vzs_handlers *handlers)
handlers->close_conn = close_conn;
handlers->set_conn = set_conn;
handlers->send = send;
handlers->recv_str = recv_str;

return 0;
}
Expand Down Expand Up @@ -488,3 +495,20 @@ static int send(

return _vzs_writefd(ctx, cn->out, data, size);
}

/*
read from nonblocking descriptor <fd> string, separated by <separator>.
will write '\0' on the end of string
*/
static int recv_str(
struct vzsock_ctx *ctx,
void *conn,
char separator,
char *data,
size_t size)
{
struct ssh_conn *cn = (struct ssh_conn *)conn;

return _vzs_recv_str(ctx, cn->in, separator, data, size);
}

62 changes: 62 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,66 @@ int _vzs_writefd(struct vzsock_ctx *ctx, int fd, const char * data, size_t size)
return VZS_ERR_CONN_BROKEN;
}

/*
read from nonblocking descriptor <fd> string, separated by <separator>.
will write '\0' on the end of string
*/
int _vzs_recv_str(
struct vzsock_ctx *ctx,
int fd,
char separator,
char *data,
size_t size)
{
int rc;
char * p;
fd_set fds;
struct timeval tv;

p = data;
*p = '\0';
while (1) {
/* read data */
while (1) {
errno = 0;
rc = read(fd, p, 1);
if (rc > 0) {
if (*p == separator) {
*p = '\0';
return 0;
}
p++;
if (p >= data + size)
return VZS_ERR_TOOLONG;
continue;
} else if (rc == 0) {
/* end of file */
*p = '\0';
return 0;
}
if (errno == EAGAIN)
/* wait next data */
break;
else
return VZS_ERR_CONN_BROKEN;
}

/* wait next data in socket */
do {
FD_ZERO(&fds);
FD_SET(fd, &fds);
tv.tv_sec = ctx->tmo;
tv.tv_usec = 0;
rc = select(fd + 1, &fds, NULL, NULL, &tv);
if (rc == 0)
return VZS_ERR_TIMEOUT;
else if (rc <= 0)
return VZS_ERR_CONN_BROKEN;
} while (!FD_ISSET(fd, &fds));
}

/* but we never should be here */
return VZS_ERR_CONN_BROKEN;
}


10 changes: 10 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ int _vzs_rmdir(struct vzsock_ctx *ctx, const char *dirname);

/* Write <size> bytes of <data> in non-blocking descriptor <fd>. */
int _vzs_writefd(struct vzsock_ctx *ctx, int fd, const char * data, size_t size);
/*
read from nonblocking descriptor <fd> string, separated by <separator>.
will write '\0' on the end of string
*/
int _vzs_recv_str(
struct vzsock_ctx *ctx,
int fd,
char separator,
char *data,
size_t size);

#ifdef __cplusplus
}
Expand Down
23 changes: 23 additions & 0 deletions vzsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,27 @@ int vzsock_set_conn(struct vzsock_ctx *ctx, void *conn,
return handlers->set_conn(ctx, conn, type, data, size);
}

int vzsock_send(
struct vzsock_ctx *ctx,
void *conn,
const char * data,
size_t size)
{
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;

return handlers->send(ctx, conn, data, size);
}

int vzsock_recv_str(
struct vzsock_ctx *ctx,
void *conn,
char separator,
char *data,
size_t size)
{
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;

return handlers->recv_str(ctx, conn, separator, data, size);
}


17 changes: 11 additions & 6 deletions vzsock.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ struct vzs_handlers {
int (*set_conn)(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t size);
int (*send)(
struct vzsock_ctx *ctx,
void *conn,
const char * data,
size_t size);
struct vzsock_ctx *ctx,
void *conn,
const char * data,
size_t size);
int (*recv_str)(
struct vzsock_ctx *ctx,
void *conn,
char separator,
char *data,
size_t size);

/*
int (*recv_str)(void *conn,
char separator, char *data, size_t size);
int (*is_connected)(void *conn);
*/
};
Expand Down

0 comments on commit 2fe3993

Please sign in to comment.