touched up hcl-x code
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-04-25 21:38:20 +09:00
parent d2a70a2292
commit 93200c9936
4 changed files with 216 additions and 413 deletions

View File

@ -388,169 +388,7 @@ struct proto_xtn_t
};
typedef struct proto_xtn_t proto_xtn_t;
static int handle_packet (hcl_xproto_t* proto, hcl_xpkt_type_t type, const void* data, hcl_oow_t len)
{
if (type == HCL_XPKT_STDOUT)
{
/*if (len > 0) fwrite (data, 1, len, stdout); */
if (len > 0) fprintf (stdout, "%.*s", (int)len, data);
}
return 1;
}
#if 0
int hcl_client_process (hcl_client_t* client, const char* ipaddr, const char* script, int reuse_addr, int shut_wr_after_req)
{
hcl_oow_t used, avail;
int x;
hcl_bch_t buf[256];
ssize_t n;
const char* scptr;
const char* sccur;
hcl_xproto_t* proto = HCL_NULL;
proto_xtn_t* proto_xtn;
hcl_xproto_cb_t proto_cb;
scptr = sccur = script;
while (1)
{
struct pollfd pfd;
pfd.fd = sck;
pfd.events = POLLIN;
if (*sccur != '\0') pfd.events |= POLLOUT;
pfd.revents = 0;
n = poll(&pfd, 1, 1000);
if (n <= -1)
{
fprintf (stderr, "poll error on %d - %s\n", sck, strerror(n));
goto oops;
}
if (n == 0)
{
/* TODO: proper timeout handling */
continue;
}
if (pfd.revents & POLLERR)
{
fprintf (stderr, "error condition detected on %d\n", sck);
goto oops;
}
if (pfd.revents & POLLOUT)
{
hcl_xpkt_hdr_t hdr;
struct iovec iov[2];
hcl_uint16_t seglen;
while (*sccur != '\0' && sccur - scptr < HCL_XPKT_MAX_PLD_LEN) sccur++;
seglen = sccur - scptr;
hdr.id = 1; /* TODO: */
hdr.type = HCL_XPKT_CODE | (((seglen >> 8) & 0x0F) << 4);
hdr.len = seglen & 0xFF;
iov[0].iov_base = &hdr;
iov[0].iov_len = HCL_SIZEOF(hdr);
iov[1].iov_base = scptr;
iov[1].iov_len = seglen;
hcl_sys_send_iov (sck, iov, 2); /* TODO: error check */
scptr = sccur;
if (*sccur == '\0')
{
hdr.id = 1; /* TODO: */
hdr.type = HCL_XPKT_EXECUTE;
hdr.len = 0;
iov[0].iov_base = &hdr;
iov[0].iov_len = HCL_SIZEOF(hdr);
hcl_sys_send_iov (sck, iov, 1);
if (shut_wr_after_req)
{
shutdown (sck, SHUT_WR);
}
else
{
hdr.type = HCL_XPKT_DISCONNECT;
hdr.id = 1; /* TODO: */
hdr.len = 0;
iov[0].iov_base = &hdr;
iov[0].iov_len = HCL_SIZEOF(hdr);
hcl_sys_send_iov (sck, iov, 1);
}
}
}
if (pfd.revents & POLLIN)
{
hcl_oow_t bcap;
hcl_uint8_t* bptr;
bptr = hcl_xproto_getbuf(proto, &bcap);;
x = recv(sck, bptr, bcap, 0);
if (x <= -1)
{
if (errno == EINTR) goto carry_on; /* didn't read read */
/*hcl_seterrwithsyserr (hcl, 0, errno); */
/* TODO: error info set... */
return -1;
}
if (x == 0) hcl_xproto_seteof(proto, 1);
hcl_xproto_advbuf (proto, x);
}
carry_on:
while (hcl_xproto_ready(proto))
{
if ((n = hcl_xproto_process(proto)) <= -1)
{
/* TODO: proper error message */
return -1;
}
if (n == 0)
{
/* TODO: chceck if there is remaining data in the buffer...?? */
printf ("NO MORE DATA. EXITING...\n");
goto done;
}
}
if (hcl_xproto_geteof(proto)) break;
}
done:
/* TODO: we can check if the buffer has all been consumed. if not, there is trailing garbage.. */
/*{
struct linger linger;
linger.l_onoff = 1;
linger.l_linger = 0;
setsockopt (sck, SOL_SOCKET, SO_LINGER, (char *) &linger, sizeof(linger));
}*/
hcl_xproto_close (proto);
close (sck);
return 0;
oops:
if (proto) hcl_xproto_close (proto);
if (sck >= 0) close (sck);
return -1;
}
#endif
int hcl_client_connect (hcl_client_t* client, const char* ipaddr, int reuse_addr)
static int client_connect (hcl_client_t* client, const char* ipaddr, int reuse_addr)
{
hcl_sckaddr_t sckaddr;
hcl_scklen_t scklen;
@ -607,6 +445,8 @@ int hcl_client_connect (hcl_client_t* client, const char* ipaddr, int reuse_addr
}
}
/* TODO: async connect? */
/* TODO: connect timeout */
if (connect(sck, (struct sockaddr*)&sckaddr, scklen) <= -1)
{
hcl_client_seterrbfmt (client, HCL_ESYSERR,
@ -616,12 +456,12 @@ int hcl_client_connect (hcl_client_t* client, const char* ipaddr, int reuse_addr
memset (&proto, 0, HCL_SIZEOF(proto_cb));
proto_cb.on_packet = handle_packet;
proto_cb.on_packet = client->prim.on_packet;
proto = hcl_xproto_open(hcl_client_getmmgr(client), &proto_cb, HCL_SIZEOF(*proto_xtn));
if (HCL_UNLIKELY(!proto))
{
fprintf (stderr, "cannot open protocol to %s\n", ipaddr);
hcl_client_seterrbfmt (client, HCL_ESYSERR, "cannot open protocol to %s", ipaddr);
goto oops;
}
proto_xtn = hcl_xproto_getxtn(proto);
@ -636,3 +476,163 @@ oops:
if (sck >= 0) close (sck);
return -1;
}
static void client_close (hcl_client_t* client)
{
if (client->proto)
{
hcl_xproto_close (client->proto);
client->proto = HCL_NULL;
}
if (client->sck >= 0)
{
close (client->sck);
client->sck = -1;
}
}
int hcl_client_start (hcl_client_t* client, const char* ipaddr, const char* script, int reuse_addr, int shut_wr_after_req)
{
int x;
ssize_t n;
const char* scptr, * sccur;
if (client_connect(client, ipaddr, reuse_addr) <= -1) return -1;
scptr = sccur = script;
while (1)
{
struct pollfd pfd;
pfd.fd = client->sck;
pfd.events = POLLIN;
if (*sccur != '\0') pfd.events |= POLLOUT;
pfd.revents = 0;
n = poll(&pfd, 1, 1000);
if (n <= -1)
{
hcl_client_seterrbfmt (client, HCL_ESYSERR, "poll error on %d - %hs", client->sck, strerror(n));
goto oops;
}
if (n == 0)
{
/* TODO: proper timeout handling */
continue;
}
if (pfd.revents & POLLERR)
{
hcl_client_seterrbfmt (client, HCL_ESYSERR, "error condition detected on %d", client->sck);
goto oops;
}
if (pfd.revents & POLLOUT)
{
hcl_xpkt_hdr_t hdr;
struct iovec iov[2];
hcl_uint16_t seglen;
while (*sccur != '\0' && sccur - scptr < HCL_XPKT_MAX_PLD_LEN) sccur++;
seglen = sccur - scptr;
hdr.id = 1; /* TODO: */
hdr.type = HCL_XPKT_CODE | (((seglen >> 8) & 0x0F) << 4);
hdr.len = seglen & 0xFF;
iov[0].iov_base = &hdr;
iov[0].iov_len = HCL_SIZEOF(hdr);
iov[1].iov_base = scptr;
iov[1].iov_len = seglen;
hcl_sys_send_iov (client->sck, iov, 2); /* TODO: error check */
scptr = sccur;
if (*sccur == '\0')
{
hdr.id = 1; /* TODO: */
hdr.type = HCL_XPKT_EXECUTE;
hdr.len = 0;
iov[0].iov_base = &hdr;
iov[0].iov_len = HCL_SIZEOF(hdr);
hcl_sys_send_iov (client->sck, iov, 1);
if (shut_wr_after_req)
{
shutdown (client->sck, SHUT_WR);
}
else
{
hdr.type = HCL_XPKT_DISCONNECT;
hdr.id = 1; /* TODO: */
hdr.len = 0;
iov[0].iov_base = &hdr;
iov[0].iov_len = HCL_SIZEOF(hdr);
hcl_sys_send_iov (client->sck, iov, 1);
}
}
}
if (pfd.revents & POLLIN)
{
hcl_oow_t bcap;
hcl_uint8_t* bptr;
bptr = hcl_xproto_getbuf(client->proto, &bcap);;
x = recv(client->sck, bptr, bcap, 0);
if (x <= -1)
{
if (errno == EINTR) goto carry_on; /* didn't read read */
/*hcl_seterrwithsyserr (hcl, 0, errno); */
/* TODO: error info set... */
return -1;
}
if (x == 0) hcl_xproto_seteof(client->proto, 1);
hcl_xproto_advbuf (client->proto, x);
}
carry_on:
while (hcl_xproto_ready(client->proto))
{
if ((n = hcl_xproto_process(client->proto)) <= -1)
{
/* TODO: proper error message */
return -1;
}
if (n == 0)
{
/* TODO: chceck if there is remaining data in the buffer...?? */
printf ("NO MORE DATA. EXITING...\n");
goto done;
}
}
if (hcl_xproto_geteof(client->proto)) break;
}
done:
/* TODO: we can check if the buffer has all been consumed. if not, there is trailing garbage.. */
/*{
struct linger linger;
linger.l_onoff = 1;
linger.l_linger = 0;
setsockopt (client->sck, SOL_SOCKET, SO_LINGER, (char *) &linger, sizeof(linger));
}*/
client_close (client);
return 0;
oops:
client_close (client);
return -1;
}