mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
return string after socket.write error
This commit is contained in:
@@ -55,7 +55,8 @@ _write(lua_State *L) {
|
|||||||
size_t sz;
|
size_t sz;
|
||||||
if (type == LUA_TSTRING) {
|
if (type == LUA_TSTRING) {
|
||||||
buffer = lua_tolstring(L,2,&sz);
|
buffer = lua_tolstring(L,2,&sz);
|
||||||
} else if (type == LUA_TLIGHTUSERDATA) {
|
} else {
|
||||||
|
luaL_checktype(L,2, LUA_TLIGHTUSERDATA);
|
||||||
buffer = lua_touserdata(L,2);
|
buffer = lua_touserdata(L,2);
|
||||||
sz = luaL_checkinteger(L,3);
|
sz = luaL_checkinteger(L,3);
|
||||||
}
|
}
|
||||||
@@ -67,7 +68,20 @@ _write(lua_State *L) {
|
|||||||
case EINTR:
|
case EINTR:
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
return 0;
|
if (type == LUA_TSTRING) {
|
||||||
|
lua_settop(L,2);
|
||||||
|
} else {
|
||||||
|
lua_pushlstring(L, buffer, sz);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (err == 0) {
|
||||||
|
if (type == LUA_TSTRING) {
|
||||||
|
lua_settop(L,2);
|
||||||
|
} else {
|
||||||
|
lua_pushlstring(L, buffer, sz);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
assert(err == sz);
|
assert(err == sz);
|
||||||
return 0;
|
return 0;
|
||||||
@@ -83,7 +97,8 @@ _writeblock(lua_State *L) {
|
|||||||
size_t sz;
|
size_t sz;
|
||||||
if (type == LUA_TSTRING) {
|
if (type == LUA_TSTRING) {
|
||||||
buffer = lua_tolstring(L,3,&sz);
|
buffer = lua_tolstring(L,3,&sz);
|
||||||
} else if (type == LUA_TLIGHTUSERDATA) {
|
} else {
|
||||||
|
luaL_checktype(L, 3, LUA_TLIGHTUSERDATA);
|
||||||
buffer = lua_touserdata(L,3);
|
buffer = lua_touserdata(L,3);
|
||||||
sz = luaL_checkinteger(L,4);
|
sz = luaL_checkinteger(L,4);
|
||||||
}
|
}
|
||||||
@@ -120,11 +135,20 @@ _writeblock(lua_State *L) {
|
|||||||
case EINTR:
|
case EINTR:
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
return 0;
|
break;
|
||||||
|
}
|
||||||
|
if (err == 0) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
assert(err == sz + header);
|
assert(err == sz + header);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
luaL_Buffer b;
|
||||||
|
luaL_buffinitsize(L,&b, buf[0].iov_len + buf[1].iov_len);
|
||||||
|
luaL_addlstring(&b, buf[0].iov_base, buf[0].iov_len);
|
||||||
|
luaL_addlstring(&b, buf[1].iov_base, buf[1].iov_len);
|
||||||
|
luaL_pushresult(&b);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct buffer {
|
struct buffer {
|
||||||
|
|||||||
@@ -4,6 +4,17 @@ local c = require "socket.c"
|
|||||||
local socket = {}
|
local socket = {}
|
||||||
local fd
|
local fd
|
||||||
local object
|
local object
|
||||||
|
local data = {}
|
||||||
|
|
||||||
|
local function presend()
|
||||||
|
if next(data) then
|
||||||
|
local tmp = data
|
||||||
|
data = {}
|
||||||
|
for _,v in ipairs(tmp) do
|
||||||
|
socket.write(fd, v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function socket.connect(addr)
|
function socket.connect(addr)
|
||||||
local ip, port = string.match(addr,"([^:]+):(.+)")
|
local ip, port = string.match(addr,"([^:]+):(.+)")
|
||||||
@@ -14,6 +25,7 @@ function socket.connect(addr)
|
|||||||
end
|
end
|
||||||
skynet.send(".connection", "text", "ADD", fd , skynet.address(skynet.self()))
|
skynet.send(".connection", "text", "ADD", fd , skynet.address(skynet.self()))
|
||||||
object = c.new()
|
object = c.new()
|
||||||
|
presend()
|
||||||
end
|
end
|
||||||
|
|
||||||
function socket.stdin()
|
function socket.stdin()
|
||||||
@@ -40,11 +52,17 @@ function socket.readblock(...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function socket.write(...)
|
function socket.write(...)
|
||||||
c.write(fd, ...)
|
local str = c.write(fd, ...)
|
||||||
|
if str then
|
||||||
|
table.insert(data, str)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function socket.writeblock(...)
|
function socket.writeblock(...)
|
||||||
c.writeblock(fd, ...)
|
local str = c.write(fd, ...)
|
||||||
|
if str then
|
||||||
|
table.insert(data, str)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function socket.close()
|
function socket.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user