added Socket>>writeBytes:offset:length:

This commit is contained in:
hyunghwan.chung
2018-01-24 13:29:36 +00:00
parent 625e2fbcdb
commit 7cba31e8f9
3 changed files with 44 additions and 6 deletions

View File

@ -413,6 +413,7 @@ static moo_pfrc_t pf_write_socket (moo_t* moo, moo_ooi_t nargs)
{
oop_sck_t sck;
moo_oop_byte_t buf;
moo_oow_t offset, length, maxlen;
int fd;
ssize_t n;
@ -437,7 +438,37 @@ static moo_pfrc_t pf_write_socket (moo_t* moo, moo_ooi_t nargs)
return MOO_PF_FAILURE;
}
n = send (fd, MOO_OBJ_GET_BYTE_SLOT(buf), MOO_OBJ_GET_SIZE(buf), 0);
offset = 0;
maxlen = MOO_OBJ_GET_SIZE(buf);
length = maxlen;
if (nargs >= 2)
{
moo_oop_t tmp;
tmp = MOO_STACK_GETARG(moo, nargs, 1);
if (moo_inttooow (moo, tmp, &offset) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid offset - %O", tmp);
return MOO_PF_FAILURE;
}
if (nargs >= 3)
{
tmp = MOO_STACK_GETARG(moo, nargs, 2);
if (moo_inttooow (moo, tmp, &length) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid length - %O", tmp);
return MOO_PF_FAILURE;
}
}
if (offset >= maxlen) offset = maxlen - 1;
if (length > maxlen - offset) length = maxlen - offset;
}
n = send (fd, &MOO_OBJ_GET_BYTE_SLOT(buf)[offset], length, 0);
if (n <= -1 && errno != EWOULDBLOCK)
{
moo_seterrwithsyserr (moo, errno);
@ -476,7 +507,8 @@ static moo_pfinfo_t pfinfos[] =
{ I, { 'o','p','e','n','\0' }, 0, { pf_open_socket, 3, 3 } },
{ I, { 'r','e','a','d','B','y','t','e','s',':','\0' }, 0, { pf_read_socket, 1, 1 } },
{ I, { 's','o','c','k','e','t','E','r','r','o','r','\0' }, 0, { pf_get_socket_error, 0, 0 } },
{ I, { 'w','r','i','t','e','B','y','t','e','s',':','\0' }, 0, { pf_write_socket, 1, 1 } }
{ I, { 'w','r','i','t','e','B','y','t','e','s',':','\0' }, 0, { pf_write_socket, 1, 1 } },
{ I, { 'w','r','i','t','e','B','y','t','e','s',':','o','f','f','s','e','t',':','l','e','n','g','t','h',':','\0' }, 0, { pf_write_socket, 3, 3 } }
};
/* ------------------------------------------------------------------------ */