Compare commits
7 Commits
v2.0.0-Tes
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 0a4daa3dd8 | |||
| 950b7e3fa3 | |||
| a7189fab8b | |||
| 56981f5b83 | |||
| d5de3a2667 | |||
| 923227885e | |||
| 14dec82611 |
@ -27,7 +27,7 @@ A simple and powerful way to make servers and clients
|
||||
- [ ] AUDP - advance udp. Ensures packets arrive and handles late packets.
|
||||
- [ ] P2P - peer to peer (Server to set up initial connection)
|
||||
- [ ] Relay - offput server load (locally)
|
||||
- [ ] Threading - Simple threading ~~(UDP/AUDP Only)~~ Thanks to an updated multi library we can thread with ease
|
||||
- [ ] Threading - Simple threading Should be simple using multi@15.3.0
|
||||
- [ ] Priority handling
|
||||
|
||||
# Note
|
||||
|
||||
40
bmptester.lua
Normal file
40
bmptester.lua
Normal file
@ -0,0 +1,40 @@
|
||||
local bin = require("bin")
|
||||
bmp = {}
|
||||
bmp.__index = bin
|
||||
bmp.width = 0
|
||||
bmp.height = 0
|
||||
bmp.Type = "BMP"
|
||||
local File_Header_Size = 14
|
||||
local Image_Header_Size = 40
|
||||
--[[
|
||||
File Header:
|
||||
bfType 2 The characters "BM"
|
||||
bfSize 4 The size of the file in bytes
|
||||
bfReserved1 2 Unused - must be zero
|
||||
bfReserved2 2 Unused - must be zero
|
||||
bfOffBits 4 Offset to start of Pixel Data
|
||||
|
||||
The Image Header:
|
||||
biSize 4 Header Size - Must be at least 40
|
||||
biWidth 4 Image width in pixels
|
||||
biHeight 4 Image height in pixels
|
||||
biPlanes 2 Must be 1
|
||||
biBitCount 2 Bits per pixel - 1, 4, 8, 16, 24, or 32
|
||||
biCompression 4 Compression type (0 = uncompressed)
|
||||
biSizeImage 4 Image Size - may be zero for uncompressed images
|
||||
biXPelsPerMeter 4 Preferred resolution in pixels per meter
|
||||
biYPelsPerMeter 4 Preferred resolution in pixels per meter
|
||||
biClrUsed 4 Number Color Map entries that are actually used
|
||||
biClrImportant 4 Number of significant colors
|
||||
|
||||
|
||||
]]
|
||||
function bmp:new(w,h)
|
||||
local c = {}
|
||||
setmetatable(c,self)
|
||||
c.header = bin.newDataBuffer(File_Header_Size,"\0")
|
||||
c.fileheader = bin.newDataBuffer(Image_Header_Size,"\0")
|
||||
end
|
||||
function bmp:tofile(name)
|
||||
--
|
||||
end
|
||||
17
client.lua
17
client.lua
@ -1,10 +1,17 @@
|
||||
package.path="?/init.lua;"..package.path
|
||||
require("multi")
|
||||
require("net")
|
||||
client = net:newUDPClient("localhost",12345)
|
||||
local multi = require("multi")
|
||||
local net = require("net")
|
||||
client = net:newTCPClient("localhost",12345)
|
||||
client:enableBinaryMode()
|
||||
local file = bin.new()
|
||||
client.OnDataRecieved(function(self,data)
|
||||
print(data)
|
||||
if data == "END" then
|
||||
file:tofile("test2.mp3")
|
||||
print("File transfered!")
|
||||
else
|
||||
file:tackE(data)
|
||||
end
|
||||
end)
|
||||
client.OnClientReady:holdUT() -- waots until the client is ready... You can also connect to this event as well and have code do stuff too
|
||||
client.OnClientReady:holdUT() -- waits until the client is ready... You can also connect to this event as well and have code do stuff too
|
||||
client:send("Hello Server!")
|
||||
multi:mainloop()
|
||||
|
||||
93
net/audp.lua
93
net/audp.lua
@ -1,93 +0,0 @@
|
||||
require("net")
|
||||
function net:newUDPServer(port,servercode)
|
||||
local c={}
|
||||
c.hostip=net.getLocalIP()
|
||||
c.port=port
|
||||
c.bannedCIDs={}
|
||||
c.bannedIPs={}
|
||||
function c:setUpdateRate(n)
|
||||
self.updater:setSkip(n or 0)
|
||||
end
|
||||
function c:banCID(cid)
|
||||
--
|
||||
end
|
||||
function c:banIP(ip)
|
||||
--
|
||||
end
|
||||
function c:broadcast(name)
|
||||
--
|
||||
end
|
||||
function c:send(ip,data,port,cid)
|
||||
--
|
||||
end
|
||||
function c:pollClientModules(ip,port)
|
||||
--
|
||||
end
|
||||
function c:CIDFrom(ip,port)
|
||||
--
|
||||
end
|
||||
function c:sendAll(data)
|
||||
--
|
||||
end
|
||||
function c:sendAllBut(data,cid)
|
||||
--
|
||||
end
|
||||
function c:clientRegistered(cid)
|
||||
--
|
||||
end
|
||||
function c:clientLoggedIn(cid)
|
||||
--
|
||||
end
|
||||
function c:update()
|
||||
--
|
||||
end
|
||||
c.Updater=multi:newUpdater(0)
|
||||
c.Updater.link=c
|
||||
c.updater:OnUpdate(function(self)
|
||||
self.link:update()
|
||||
end)
|
||||
c.OnClientsModulesList=multi:newConnection(false)
|
||||
c.OnDataRecieved=multi:newConnection(false)
|
||||
c.OnClientClosed=multi:newConnection(false)
|
||||
c.OnClientConnected=multi:newConnection(false)
|
||||
end
|
||||
function net:newUDPClient(host,port,servercode)
|
||||
local c={}
|
||||
c.host=host
|
||||
c.port=port
|
||||
function c:setUpdateRate(n)
|
||||
self.updater:setSkip(n or 0)
|
||||
end
|
||||
function c:send(data)
|
||||
--
|
||||
end
|
||||
function c:sendRaw(data)
|
||||
--
|
||||
end
|
||||
function c:close()
|
||||
--
|
||||
end
|
||||
function c:getCID()
|
||||
--
|
||||
end
|
||||
function c:update()
|
||||
--
|
||||
end
|
||||
function c:reconnect()
|
||||
--
|
||||
end
|
||||
function c:IDAssigned()
|
||||
--
|
||||
end
|
||||
c.Updater=multi:newUpdater(0)
|
||||
c.Updater.link=c
|
||||
c.updater:OnUpdate(function(self)
|
||||
self.link:update()
|
||||
end)
|
||||
c.OnDataRecieved=multi:newConnection(false)
|
||||
c.OnClientReady=multi:newConnection(false)
|
||||
c.OnClientDisconnected=multi:newConnection(false)
|
||||
c.OnConnectionRegained=multi:newConnection(false)
|
||||
c.OnPingRecieved=multi:newConnection(false)
|
||||
c.OnServerNotAvailable=multi:newConnection(false)
|
||||
end
|
||||
342
net/init.lua
342
net/init.lua
@ -40,18 +40,23 @@ for i = 97,122 do
|
||||
end
|
||||
local isHyphen = {[9]=1,[14]=1,[19]=1,[24]=1}
|
||||
math.randomseed(os.time())
|
||||
local multi = require("multi")
|
||||
local socket=require("socket")
|
||||
local http=require("socket.http")
|
||||
local mime=require("mime")
|
||||
--ssl=require("ssl")
|
||||
--https=require("ssl.https")
|
||||
local net={}
|
||||
net.Version={2,0,1} -- This will probably stay this version for quite a while... The modules on the otherhand will be more inconsistant
|
||||
net._VERSION="2.0.1"
|
||||
net.Version={3,0,0} -- This will probably stay this version for quite a while... The modules on the otherhand will be more inconsistant
|
||||
net._VERSION="3.0.0"
|
||||
net.ClientCache = {}
|
||||
net.OnServerCreated=multi:newConnection()
|
||||
net.OnClientCreated=multi:newConnection()
|
||||
net.loadedModules={}
|
||||
net.OnCastedClientInfo=multi:newConnection()
|
||||
net.autoInit=true
|
||||
net.ConnectionDriver = {}
|
||||
net.BroadcastDriver = {}
|
||||
net.generateGUID = function(t)
|
||||
local pass = {}
|
||||
local a=0
|
||||
@ -143,6 +148,7 @@ function net:newCastedClient(name) -- connects to the broadcasted server
|
||||
error("Timeout! Server by the name: "..name.." has not been found!")
|
||||
end
|
||||
if data then
|
||||
print("found!",data)
|
||||
local n,tp,ip,port=data:match("(%S-)|(%S-)|(%S-):(%d+)")
|
||||
if n:match(name) then
|
||||
print("Found Server!",n,tp,ip,port)
|
||||
@ -155,6 +161,28 @@ function net:newCastedClient(name) -- connects to the broadcasted server
|
||||
end
|
||||
end
|
||||
end
|
||||
function net:newCastedClients(name) -- connects to the broadcasted server
|
||||
local listen = socket.udp() -- make a new socket
|
||||
listen:setsockname(net.getLocalIP(), 11111)
|
||||
listen:settimeout(0)
|
||||
multi:newTLoop(function(self)
|
||||
local data, ip, port = listen:receivefrom()
|
||||
if data then
|
||||
local n,tp,ip,port=data:match("(%S-)|(%S-)|(%S-):(%d+)")
|
||||
if n:match(name) and not net.ClientCache[n] then
|
||||
local capture = n:match(name)
|
||||
local client = {}
|
||||
if tp=="tcp" then
|
||||
client=net:newTCPClient(ip,tonumber(port))
|
||||
else
|
||||
client=net:newUDPClient(ip,tonumber(port))
|
||||
end
|
||||
net.ClientCache[n]=client
|
||||
net.OnCastedClientInfo:Fire(client,n,ip,port)
|
||||
end
|
||||
end
|
||||
end,.1):setName("net.castedTask")
|
||||
end
|
||||
-- UDP Stuff
|
||||
function net:newUDPServer(port,servercode,nonluaServer)
|
||||
local c={}
|
||||
@ -163,7 +191,9 @@ function net:newUDPServer(port,servercode,nonluaServer)
|
||||
c.udp:setsockname("*", port)
|
||||
c.ips={}
|
||||
c.Type="udp"
|
||||
c.port=port
|
||||
if port == 0 then
|
||||
_, c.port = c.udp:getsockname()
|
||||
end
|
||||
c.ids={}
|
||||
c.servercode=servercode
|
||||
c.bannedIPs={}
|
||||
@ -181,11 +211,11 @@ function net:newUDPServer(port,servercode,nonluaServer)
|
||||
c.broad=socket.udp()
|
||||
c.hostip=net.getLocalIP()
|
||||
function c:broadcast(name)
|
||||
local loop=multi:newTLoop(function(loop,dt)
|
||||
table.insert(net.BroadcastDriver,function(loop,dt)
|
||||
self.broad:setoption('broadcast',true)
|
||||
self.broad:sendto(name.."|"..self.Type.."|"..self.hostip..":"..self.port, "255.255.255.255", 11111)
|
||||
self.broad:setoption('broadcast',false)
|
||||
end,1)
|
||||
end)
|
||||
end
|
||||
function c:send(ip,data,port,cid)
|
||||
if self.autoNormalization then
|
||||
@ -250,7 +280,7 @@ function net:newUDPServer(port,servercode,nonluaServer)
|
||||
function c:update()
|
||||
local data,ip,port=self.udp:receivefrom()
|
||||
if net.inList(self.bannedIPs,ip) or net.inList(self.bannedCIDs,cid) then
|
||||
print("We will ingore data from a banned client!")
|
||||
print("We will ignore data from a banned client!")
|
||||
return
|
||||
end
|
||||
if data then
|
||||
@ -317,20 +347,17 @@ function net:newUDPServer(port,servercode,nonluaServer)
|
||||
c.OnDataRecieved=multi:newConnection()
|
||||
c.OnClientClosed=multi:newConnection()
|
||||
c.OnClientConnected=multi:newConnection()
|
||||
c.connectiontest=multi:newAlarm(30)
|
||||
c.connectiontest=multi:newAlarm(30):setName("net.pingOutTask")
|
||||
c.connectiontest.link=c
|
||||
c.connectiontest:OnRing(function(alarm)
|
||||
--print("pinging clients!")
|
||||
alarm.link:sendAll("ping")
|
||||
alarm:Reset()
|
||||
end)
|
||||
multi:newLoop(function()
|
||||
c:update()
|
||||
end)
|
||||
table.insert(net.ConnectionDriver,c)
|
||||
net.OnServerCreated:Fire(c)
|
||||
return c
|
||||
end
|
||||
|
||||
local pingManager = {}
|
||||
function net:newUDPClient(host,port,servercode,nonluaServer)
|
||||
local c={}
|
||||
c.ip=assert(socket.dns.toip(host))
|
||||
@ -403,6 +430,7 @@ function net:newUDPClient(host,port,servercode,nonluaServer)
|
||||
self.cid="NIL"
|
||||
c.udp:send("I!")
|
||||
end
|
||||
self.pingEvent:Resume()
|
||||
self.OnConnectionRegained:Fire(self)
|
||||
end
|
||||
c.pingEvent=multi:newEvent(function(self) return self.link:pollPing() end)
|
||||
@ -415,7 +443,8 @@ function net:newUDPClient(host,port,servercode,nonluaServer)
|
||||
self.link.OnServerNotAvailable:Fire("Connection to server lost: ping timeout!")
|
||||
self.link.OnClientDisconnected:Fire(self,"closed")
|
||||
end
|
||||
end)
|
||||
self:Pause()
|
||||
end):setName("net.pingInTask")
|
||||
c.pingEvent.link=c
|
||||
c.OnPingRecieved=multi:newConnection()
|
||||
c.OnDataRecieved=multi:newConnection()
|
||||
@ -424,30 +453,132 @@ function net:newUDPClient(host,port,servercode,nonluaServer)
|
||||
c.OnClientDisconnected=multi:newConnection()
|
||||
c.OnConnectionRegained=multi:newConnection()
|
||||
c.notConnected=multi:newFunction(function(self)
|
||||
self:hold(3)
|
||||
if self.link:IDAssigned()==false then
|
||||
self.link.OnServerNotAvailable:Fire("Can't connect to the server: no response from server")
|
||||
end
|
||||
multi:newAlarm(3):OnRing(function(alarm)
|
||||
if self.link:IDAssigned()==false then
|
||||
self.link.OnServerNotAvailable:Fire("Can't connect to the server: no response from server")
|
||||
end
|
||||
alarm:Destroy()
|
||||
end):setName("net.clientTimeout")
|
||||
end)
|
||||
c.notConnected.link=c
|
||||
if not nonluaServer then
|
||||
c.udp:send("I!")
|
||||
end
|
||||
multi:newLoop(function()
|
||||
c:update()
|
||||
end)
|
||||
multi:newJob(function() c.notConnected() end)
|
||||
table.insert(net.ConnectionDriver,c)
|
||||
multi.nextStep(function() c.notConnected() end)
|
||||
net.OnClientCreated:Fire(c)
|
||||
return c
|
||||
end
|
||||
multi:newThread(function()
|
||||
|
||||
end)
|
||||
--TCP Stuff
|
||||
function net:newTCPClientObject(fd)
|
||||
local c = {}
|
||||
local client
|
||||
c.Type="tcp-ClientObj"
|
||||
c.rMode="*l"
|
||||
c.sMode="*l"
|
||||
function c:packMsg(data)
|
||||
local temp = bin.new()
|
||||
temp:addBlock(#data,self.numspace,"n")
|
||||
temp:addBlock(data)
|
||||
return temp:getData()
|
||||
end
|
||||
function c:enableBinaryMode()
|
||||
self.rMode = "b"
|
||||
self.sMode = "b"
|
||||
end
|
||||
if fd then
|
||||
client=socket.tcp()
|
||||
client:setfd(fd)
|
||||
_,port = client:getsockname()
|
||||
c.handle = client
|
||||
else
|
||||
error("You need to enter a fd in order to be able to create a tcp client object like this!")
|
||||
end
|
||||
function c:setUpdateRate(n)
|
||||
self.updaterRate=n
|
||||
end
|
||||
function c:setReceiveMode(mode)
|
||||
self.rMode=mode
|
||||
end
|
||||
function c:setSendMode(mode)
|
||||
self.rMode=mode
|
||||
end
|
||||
function c:send(data)
|
||||
if self.autoNormalization then
|
||||
data=net.normalize(data)
|
||||
end
|
||||
if self.sMode=="*l" then
|
||||
self.handle:send(data.."\n")
|
||||
elseif self.sMode=="b" then
|
||||
self.handle:send(self:packMsg(data))
|
||||
else
|
||||
self.handle:send(data)
|
||||
end
|
||||
end
|
||||
multi:newThread("ServerClientHandler",function()
|
||||
while true do
|
||||
thread.skip(1)
|
||||
local data, err, dat, len
|
||||
if self.rMode == "b" then
|
||||
thread.hold(function()
|
||||
dat = client:receive(self.numspace)
|
||||
return dat
|
||||
end)
|
||||
len = bin.new(dat):getBlock("n",self.numspace)
|
||||
data, err = client:receive(len)
|
||||
else
|
||||
data, err = client:receive(self.rMode)
|
||||
end
|
||||
if err=="closed" then
|
||||
for i=1,#self.ips do
|
||||
if self.ips[i]==client then
|
||||
table.remove(self.ips,i)
|
||||
end
|
||||
end
|
||||
self.OnClientClosed:Fire(self,"Client Closed Connection!",client,client,ip)
|
||||
self.links[client]=nil -- lets clean up
|
||||
self:Destroy()
|
||||
end
|
||||
if data then
|
||||
if self.autoNormalization then
|
||||
data=net.denormalize(data)
|
||||
end
|
||||
if net.inList(self.bannedIPs,ip) then
|
||||
print("We will ingore data from a banned client!")
|
||||
return
|
||||
end
|
||||
local hook=data:match("!(.-)!")
|
||||
self.OnDataRecieved:getConnection(hook):Fire(self,data,client,client,ip,self)
|
||||
if data:sub(1,2)=="L!" then
|
||||
cList=data
|
||||
local list={}
|
||||
for m,v in cList:gmatch("(%S-):(%S-)|") do
|
||||
list[m]=v
|
||||
end
|
||||
self.OnClientsModulesList:Fire(list,client,client,ip)
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
c.OnClientsModulesList=multi:newConnection()
|
||||
c.OnDataRecieved=multi:newConnection()
|
||||
c.OnClientClosed=multi:newConnection()
|
||||
c.OnClientConnected=multi:newConnection()
|
||||
return c
|
||||
end
|
||||
function net:newTCPServer(port)
|
||||
local c={}
|
||||
local port = port or 0
|
||||
c.tcp=assert(socket.bind("*", port))
|
||||
c.tcp:settimeout(0)
|
||||
c.ip,c.port=c.tcp:getsockname()
|
||||
c.ips={}
|
||||
c.port=port
|
||||
if port == 0 then
|
||||
_, c.port = c.tcp:getsockname()
|
||||
end
|
||||
c.ids={}
|
||||
c.bannedIPs={}
|
||||
c.Type="tcp"
|
||||
@ -457,14 +588,25 @@ function net:newTCPServer(port)
|
||||
c.autoNormalization=false
|
||||
c.updates={}
|
||||
c.links={}
|
||||
c.numspace = 4
|
||||
c.broad=socket.udp()
|
||||
c.hostip=net.getLocalIP()
|
||||
function c:packMsg(data)
|
||||
local temp = bin.new()
|
||||
temp:addBlock(#data,self.numspace,"n")
|
||||
temp:addBlock(data)
|
||||
return temp:getData()
|
||||
end
|
||||
function c:enableBinaryMode()
|
||||
self.rMode = "b"
|
||||
self.sMode = "b"
|
||||
end
|
||||
function c:broadcast(name)
|
||||
local loop=multi:newTLoop(function(loop,dt)
|
||||
table.insert(net.BroadcastDriver,function(loop,dt)
|
||||
self.broad:setoption('broadcast',true)
|
||||
self.broad:sendto(name.."|"..self.Type.."|"..self.hostip..":"..self.port, "255.255.255.255", 11111)
|
||||
self.broad:setoption('broadcast',false)
|
||||
end,1)
|
||||
end)
|
||||
end
|
||||
function c:setUpdateRate(n)
|
||||
self.updaterRate=n
|
||||
@ -487,6 +629,8 @@ function net:newTCPServer(port)
|
||||
end
|
||||
if self.sMode=="*l" then
|
||||
handle:send(data.."\n")
|
||||
elseif self.sMode=="b" then
|
||||
handle:send(self:packMsg(data))
|
||||
else
|
||||
handle:send(data)
|
||||
end
|
||||
@ -535,64 +679,76 @@ function net:newTCPServer(port)
|
||||
ip,port=client:getpeername()
|
||||
if ip and port then
|
||||
print("Got connection from: ",ip,port)
|
||||
local updater=multi:newUpdater(skip)
|
||||
self.updates[client]=updater
|
||||
self.OnClientConnected:Fire(self,self.client,self.client,ip)
|
||||
updater:OnUpdate(function(self)
|
||||
local data, err = self.client:receive(self.rMode or self.Link.rMode)
|
||||
if err=="closed" then
|
||||
for i=1,#self.Link.ips do
|
||||
if self.Link.ips[i]==self.client then
|
||||
table.remove(self.Link.ips,i)
|
||||
-- local updater=multi:newUpdater(skip):setName("net.tcpClientObj")
|
||||
-- self.updates[client]=updater
|
||||
self.OnClientConnected:Fire(self,client,client,ip)
|
||||
--updater:OnUpdate(function(self)
|
||||
multi:newThread("ServerClientHandler",function()
|
||||
while true do
|
||||
thread.skip(1)
|
||||
local data, err, dat, len
|
||||
if self.rMode == "b" then
|
||||
thread.hold(function()
|
||||
dat = client:receive(self.numspace)
|
||||
return dat
|
||||
end)
|
||||
len = bin.new(dat):getBlock("n",self.numspace)
|
||||
data, err = client:receive(len)
|
||||
else
|
||||
data, err = client:receive(self.rMode)
|
||||
end
|
||||
if err=="closed" then
|
||||
for i=1,#self.ips do
|
||||
if self.ips[i]==client then
|
||||
table.remove(self.ips,i)
|
||||
end
|
||||
end
|
||||
self.OnClientClosed:Fire(self,"Client Closed Connection!",client,client,ip)
|
||||
self.links[client]=nil -- lets clean up
|
||||
self:Destroy()
|
||||
thread.kill()
|
||||
end
|
||||
self.Link.OnClientClosed:Fire(self.Link,"Client Closed Connection!",self.client,self.client,ip)
|
||||
self.Link.links[self.client]=nil -- lets clean up
|
||||
self:Destroy()
|
||||
end
|
||||
if data then
|
||||
if self.autoNormalization then
|
||||
data=net.denormalize(data)
|
||||
end
|
||||
if net.inList(self.Link.bannedIPs,ip) then
|
||||
print("We will ingore data from a banned client!")
|
||||
return
|
||||
end
|
||||
local hook=data:match("!(.-)!")
|
||||
self.Link.OnDataRecieved:getConnection(hook):Fire(self.Link,data,self.client,self.client,ip,self)
|
||||
if data:sub(1,2)=="L!" then
|
||||
cList=data
|
||||
local list={}
|
||||
for m,v in cList:gmatch("(%S-):(%S-)|") do
|
||||
list[m]=v
|
||||
if data then
|
||||
if self.autoNormalization then
|
||||
data=net.denormalize(data)
|
||||
end
|
||||
if net.inList(self.bannedIPs,ip) then
|
||||
print("We will ingore data from a banned client!")
|
||||
return
|
||||
end
|
||||
local hook=data:match("!(.-)!")
|
||||
self.OnDataRecieved:getConnection(hook):Fire(self,data,client,client,ip,self)
|
||||
if data:sub(1,2)=="L!" then
|
||||
cList=data
|
||||
local list={}
|
||||
for m,v in cList:gmatch("(%S-):(%S-)|") do
|
||||
list[m]=v
|
||||
end
|
||||
self.OnClientsModulesList:Fire(list,client,client,ip)
|
||||
end
|
||||
self.Link.OnClientsModulesList:Fire(list,self.client,self.client,ip)
|
||||
end
|
||||
end
|
||||
end)
|
||||
updater:SetSkip(self.updaterRate)
|
||||
updater.client=client
|
||||
updater.Link=self
|
||||
function updater:setReceiveMode(mode)
|
||||
self.rMode=mode
|
||||
end
|
||||
self.links[client]=updater
|
||||
-- updater:SetSkip(self.updaterRate)
|
||||
-- updater.client=client
|
||||
-- updater.Link=self
|
||||
-- function updater:setReceiveMode(mode)
|
||||
-- self.rMode=mode
|
||||
-- end
|
||||
-- self.links[client]=updater
|
||||
end
|
||||
end
|
||||
c.OnClientsModulesList=multi:newConnection()
|
||||
c.OnDataRecieved=multi:newConnection()
|
||||
c.OnClientClosed=multi:newConnection()
|
||||
c.OnClientConnected=multi:newConnection()
|
||||
multi:newLoop(function()
|
||||
c:update()
|
||||
end)
|
||||
table.insert(net.ConnectionDriver,c)
|
||||
net.OnServerCreated:Fire(c)
|
||||
return c
|
||||
end
|
||||
function net:newTCPClient(host,port)
|
||||
local c={}
|
||||
c.ip=assert(socket.dns.toip(host))
|
||||
c.port=port
|
||||
c.tcp=socket.connect(c.ip,port)
|
||||
if not c.tcp then
|
||||
print("Can't connect to the server: no response from server")
|
||||
@ -606,18 +762,31 @@ function net:newTCPClient(host,port)
|
||||
c.rMode="*l"
|
||||
c.sMode="*l"
|
||||
c.autoNormalization=false
|
||||
c.numspace = 4
|
||||
function c:enableBinaryMode()
|
||||
self.rMode = "b"
|
||||
self.sMode = "b"
|
||||
end
|
||||
function c:setReceiveMode(mode)
|
||||
self.rMode=mode
|
||||
end
|
||||
function c:setSendMode(mode)
|
||||
self.sMode=mode
|
||||
end
|
||||
function c:packMsg(data)
|
||||
local temp = bin.new()
|
||||
temp:addBlock(#data,self.numspace)
|
||||
temp:addBlock(data)
|
||||
return temp:getData()
|
||||
end
|
||||
function c:send(data)
|
||||
if self.autoNormalization then
|
||||
data=net.normalize(data)
|
||||
end
|
||||
if self.sMode=="*l" then
|
||||
ind,err=self.tcp:send(data.."\n")
|
||||
elseif self.sMode=="b" then
|
||||
ind,err=self.tcp:send(self:packMsg(data))
|
||||
else
|
||||
ind,err=self.tcp:send(data)
|
||||
end
|
||||
@ -646,7 +815,17 @@ function net:newTCPClient(host,port)
|
||||
end
|
||||
function c:update()
|
||||
if not self.tcp then return end
|
||||
local data,err=self.tcp:receive()
|
||||
local data,err,dat
|
||||
if self.rMode == "b" then
|
||||
thread.hold(function()
|
||||
dat = self.tcp:receive(self.numspace)
|
||||
return dat
|
||||
end)
|
||||
len = bin.new(dat):getBlock("n",self.numspace)
|
||||
data, err = self.tcp:receive(len)
|
||||
else
|
||||
data,err = self.tcp:receive()
|
||||
end
|
||||
if err=="closed" then
|
||||
self.OnClientDisconnected:Fire(self,err)
|
||||
elseif err=="timeout" then
|
||||
@ -667,9 +846,11 @@ function net:newTCPClient(host,port)
|
||||
self.tcp=socket.connect(self.ip,self.port)
|
||||
if self.tcp==nil then
|
||||
print("Can't connect to the server: No response from server!")
|
||||
func:hold(3)
|
||||
self:reconnect()
|
||||
return
|
||||
multi:newAlarm(3):OnRing(function(alarm)
|
||||
self:reconnect()
|
||||
alarm:Destroy()
|
||||
return
|
||||
end):setName("net.timeoutTask")
|
||||
end
|
||||
self.OnConnectionRegained:Fire(self)
|
||||
self.tcp:settimeout(0)
|
||||
@ -679,19 +860,34 @@ function net:newTCPClient(host,port)
|
||||
end
|
||||
c.event=multi:newEvent(function(event)
|
||||
return event.link:IDAssigned()
|
||||
end)
|
||||
c.event:OnEvent(function(event)
|
||||
end):OnEvent(function(event)
|
||||
event.link.OnClientReady:Fire(event.link)
|
||||
event:Destroy()
|
||||
end)
|
||||
c.event:setName("net.handshakeTask")
|
||||
c.event.link=c
|
||||
c.OnClientReady=multi:newConnection()
|
||||
c.OnClientDisconnected=multi:newConnection()
|
||||
c.OnDataRecieved=multi:newConnection()
|
||||
c.OnConnectionRegained=multi:newConnection()
|
||||
multi:newLoop(function()
|
||||
c:update()
|
||||
end)
|
||||
table.insert(net.ConnectionDriver,c)
|
||||
net.OnClientCreated:Fire(c)
|
||||
return c
|
||||
end
|
||||
net.timer = multi:newTimer():Start()
|
||||
multi:newThread("ClientServerHandler",function()
|
||||
while true do
|
||||
thread.skip()
|
||||
for i=1,#net.ConnectionDriver do
|
||||
thread.skip()
|
||||
net.ConnectionDriver[i]:update()
|
||||
end
|
||||
if net.timer:Get()>=1 then
|
||||
for i=1,#net.BroadcastDriver do
|
||||
net.BroadcastDriver[i]()
|
||||
end
|
||||
net.timer:Reset()
|
||||
end
|
||||
end
|
||||
end)
|
||||
return net
|
||||
|
||||
24
rockspec/lnet-4.0-0.rockspec
Normal file
24
rockspec/lnet-4.0-0.rockspec
Normal file
@ -0,0 +1,24 @@
|
||||
package = "lnet"
|
||||
version = "4.0-0"
|
||||
source = {
|
||||
url = "git://github.com/rayaman/net.git",
|
||||
tag = "v4.0.0",
|
||||
}
|
||||
description = {
|
||||
summary = "Lua networking library that wraps around lua-socket to make networking easy.",
|
||||
detailed = [[
|
||||
This library uses the multi library. The new multitasking library and this one are now co-Dependant if using the networkManager integration for network parallelism. This has an event driven approach for networking which allows one to easily work async with the data.
|
||||
]],
|
||||
homepage = "https://github.com/rayaman/net",
|
||||
license = "MIT"
|
||||
}
|
||||
dependencies = {
|
||||
"lua >= 5.1",
|
||||
"multi"
|
||||
}
|
||||
build = {
|
||||
type = "builtin",
|
||||
modules = {
|
||||
["net.init"] = "net/init.lua",
|
||||
}
|
||||
}
|
||||
18
server.lua
18
server.lua
@ -1,10 +1,20 @@
|
||||
package.path="?/init.lua;"..package.path
|
||||
require("multi")
|
||||
require("net")
|
||||
server = net:newUDPServer(12345)
|
||||
local multi = require("multi")
|
||||
local net = require("net")
|
||||
local GLOBAL, THREAD = require("multi.integration.lanesManager").init()
|
||||
server = net:newTCPServer(12345)
|
||||
server:enableBinaryMode()
|
||||
print("Server hosted on "..net.getExternalIP().." listening on port: 12345")
|
||||
server.OnDataRecieved(function(self,data,cid,ip,port)
|
||||
print(data)
|
||||
self:send(ip,"Hello Client! "..net.generateGUID(),port,cid)
|
||||
local file = bin.load("test.mp3")
|
||||
local dat = file:read(1024)
|
||||
while dat do
|
||||
thread.sleep(.002)
|
||||
self:send(ip,dat,port,cid)
|
||||
dat = file:read(1024)
|
||||
end
|
||||
self:send(ip,dat or "",port,cid)
|
||||
self:send(ip,"END",port,cid)
|
||||
end)
|
||||
multi:mainloop()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user