mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
writeblock
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include <sys/uio.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
@@ -70,6 +71,45 @@ _write(lua_State *L) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
_writeblock(lua_State *L) {
|
||||||
|
int fd = luaL_checkinteger(L,1);
|
||||||
|
int type = lua_type(L,2);
|
||||||
|
const char * buffer = NULL;
|
||||||
|
size_t sz;
|
||||||
|
if (type == LUA_TSTRING) {
|
||||||
|
buffer = lua_tolstring(L,2,&sz);
|
||||||
|
} else if (type == LUA_TLIGHTUSERDATA) {
|
||||||
|
buffer = lua_touserdata(L,2);
|
||||||
|
sz = luaL_checkinteger(L,3);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sz > 65535) {
|
||||||
|
luaL_error(L, "Too big package %d", (int)sz);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct iovec buf[2];
|
||||||
|
// send big-endian header
|
||||||
|
uint8_t head[2] = { sz >> 8 & 0xff , sz & 0xff };
|
||||||
|
buf[0].iov_base = head;
|
||||||
|
buf[0].iov_len = 2;
|
||||||
|
buf[1].iov_base = (void *)buffer;
|
||||||
|
buf[1].iov_len = sz;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
int err = writev(fd, buf, 2);
|
||||||
|
if (err < 0) {
|
||||||
|
switch (errno) {
|
||||||
|
case EAGAIN:
|
||||||
|
case EINTR:
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert(err == sz +2);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct buffer {
|
struct buffer {
|
||||||
int cap;
|
int cap;
|
||||||
int size;
|
int size;
|
||||||
@@ -221,6 +261,7 @@ luaopen_socket_c(lua_State *L) {
|
|||||||
{ "read", _read },
|
{ "read", _read },
|
||||||
{ "readline", _readline },
|
{ "readline", _readline },
|
||||||
{ "readblock", _readblock },
|
{ "readblock", _readblock },
|
||||||
|
{ "writeblock", _writeblock },
|
||||||
{ NULL, NULL },
|
{ NULL, NULL },
|
||||||
};
|
};
|
||||||
luaL_checkversion(L);
|
luaL_checkversion(L);
|
||||||
|
|||||||
@@ -38,6 +38,10 @@ function socket.write(...)
|
|||||||
c.write(fd, ...)
|
c.write(fd, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function socket.writeblock(...)
|
||||||
|
c.writeblock(fd, ...)
|
||||||
|
end
|
||||||
|
|
||||||
function socket.close()
|
function socket.close()
|
||||||
if fd then
|
if fd then
|
||||||
c.close(fd)
|
c.close(fd)
|
||||||
|
|||||||
Reference in New Issue
Block a user