+ | listenNet = _listen('ip或域名:port 或 @name', onListen, onClose) |
启动监听网络连接,地址为ip或域名,端口为port。 |
+ | onListen(net, listenNet, ip, port, myip, myport, share) |
监听到连接时回调,net为连接的网络对象,listenNet为正在监听连接的网络对象。 |
+ | net = _connect('ip或域名:port', onConnect, onClose, timeout) |
连接网络,地址为ip或域名,端口为port,timeout为超时秒数1~86400,注意域名解析会阻塞程序。 |
+ | onConnect(net, ip, port, myip, myport) |
连接成功时回调,net为连接的网络对象。 |
+ | data = net:receiving(length, timeoutSeconds) |
阻塞接收数据。 |
+ | data, separator = net:receiving(separator1, ..., length, timeoutSeconds) |
阻塞接收数据。 |
+ | net:receive(length, onReceive, timeoutSeconds) |
接收数据,length为需要接收的字节数,onReceive为接收到数据的回调。 |
+ | net:receive(separator1, separator2, ..., length, onReceive, timeoutSeconds) |
接收数据,separator1等为分割数据的字符串,length为最多等待分割的数据字节数,onReceive为接收到数据的回调。 |
+ | onReceive(net, data, separator) |
接收到数据时回调,net为网络对象,data为接收到的数据,#data为字节数。 |
+ | sent = net:send(data, from, to, partial) |
发送数据,data为字符串或编码函数_encode返回的数据。 |
+ | ipnumber... = _hostips('ip或域名'[, timeout]) |
解析域名,依据网络情况可能阻塞较长时间,可设置timeout时间,单位秒。
|
+ | net:close() |
关闭连接,立即回调{onClose},对于监听端口,不回调{onClose}。 |
+ | net:closed( ) |
返回net连接是否处于关闭状态。 |
+ | net:connecting( ) |
返回当前net连接是否处于正在连接的状态。 |
+ | net:nagle( bool on ) |
打开或关闭net连接的延迟发送。 延迟发送会减少流量,合并内容减少包头,立即发送对于小数据包头明显增加。系统默认打开。 |
+ | net:share(name, data, from, to) |
共享网络对象,只支持linux |
+ | net:state( ) |
返回当前net连接的状态。 |
+ | onClose(net, timeout, notconn, err) |
连接关闭回调,net为被关闭的网络连接对象,关闭监听时不回调。 |
function netSend(net, data) local send = net and net.send if send then send(net, string.from32b(#data)) send(net, data) endendlocal onHead, onBody, onIndexfunction onHead(net, data) local len = string.to32b(data, 1, true) if len <= 0 or len > 4096 then net:close() error'invalid net data' end net:receive(len, onBody, 10)endfunction onBody(net, data) net:receive(4, onHead, 300) print(data:tostr()) netSend(net, data)endlocal nets = {}local function onListen(net, lisn, ip, port, myip, myport) print('accept', net) net:receive(4, onHead, 60) net.ip = ip net.port = port nets[net] = trueendlocal function onClose(net, ...) print('close', net, ...) nets[net] = nilend_G.listening = _listen('*:1234', onListen, onClose)
function netSend(net, data) local send = net and net.send if send then send(net, string.from32b(#data)) send(net, data) endendlocal function connectServer(to, onInit, onConnect, reconnect) local onHead, onBody function onHead(net, data) local len = string.to32b(data, 1, true) if len <= 0 or len > 1024*1024 then net:close() error'invalid net data' end net:receive(len, onBody, 86400) end function onBody(net, data) net:receive(4, onHead, 86400) print(data:tostr()) end print('connecting', to) onInit(_connect(to, function (net) print('connect', to) net:receive(4, onHead, 86400) onConnect(net) end, function (net, timeout, notconn, ...) if notconn then print('connect failed', to, timeout, notconn, ...) if reconnect then _enqueue(os.now(0)+1000000, nil, connectServer, to, onInit, onConnect, reconnect) end else print('close', to, timeout, notconn, ...) end end, 10))endconnectServer('0.0.0.0:1234', function (net) _G.server = netend, function (net) print'connected' netSend(net, 'hello')end, true)