Fixed for new multi (2.0.1)

Added Fix for multi 1.7.x
Requires luasocket 3.0+
Added Examples
This commit is contained in:
Ryan 2017-06-27 11:45:34 -04:00
parent 480088970c
commit 4f40bc3dfc
6 changed files with 173 additions and 14 deletions

121
README.md
View File

@ -1,4 +1,10 @@
# net # net (2.0.1)
Updated from 2.0.0 to 2.0.1
Added:
- Examples
- Support for latest multi version
- Thats about it
# Discord # Discord
For real-time assistance with my libraries! A place where you can ask questions and get help with any of my libraries</br> For real-time assistance with my libraries! A place where you can ask questions and get help with any of my libraries</br>
@ -9,12 +15,25 @@ The net library was created to make servers and clients interact easily. This is
This library depends on luasocket and you should use luajit! It will work on a standard lua interperter, but wont be as fast. This library depends on luasocket and you should use luajit! It will work on a standard lua interperter, but wont be as fast.
# Goal # Goal
A simple and powerful way to make servers and clients A simple and powerful way to make servers and clients
# Todo
- [ ] Write the wiki
- [x] Make Example folder
- [ ] Document stable modules
- [ ] IPV6 Support
- [ ] Multicast support
- [ ] Clean up modules
- [ ] Improve 'stable' modules
- [ ] 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
- [ ] Priority handling
# Usage # Usage
server.lua server.lua
```lua ```lua
require("bin") -- this library needs a lot of work it has a bunch of old useless code, but also has many nice things as well that are really useful require("bin") -- this library needs a lot of work it has a bunch of old useless code, but also has many nice things as well that are really useful
require("multi.all") -- you need this to handle multiple connections and such require("multi") -- you need this to handle multiple connections and such
require("net") -- That requires the main library require("net") -- That requires the main library
server=net:newTCPServer(12345) -- create a server that listens on port 12345 server=net:newTCPServer(12345) -- create a server that listens on port 12345
server.OnDataRecieved(function(self,data,CID_OR_HANDLE,IP_OR_HANDLE,PORT_OR_IP,UPDATER_OR_NIL) -- a bit confusing, but dont worry you will hardly ever need more then the first 5 arguments, unless you are writing modules! server.OnDataRecieved(function(self,data,CID_OR_HANDLE,IP_OR_HANDLE,PORT_OR_IP,UPDATER_OR_NIL) -- a bit confusing, but dont worry you will hardly ever need more then the first 5 arguments, unless you are writing modules!
@ -28,7 +47,7 @@ multi:mainloop()
client.lua client.lua
```lua ```lua
require("bin") -- this library needs a lot of work it has a bunch of old useless code, but also has many nice things as well that are really useful require("bin") -- this library needs a lot of work it has a bunch of old useless code, but also has many nice things as well that are really useful
require("multi.all") -- you need this to handle multiple connections and such require("multi") -- you need this to handle multiple connections and such
require("net") -- That requires the main library require("net") -- That requires the main library
client=net:newTCPClient("localhost",12345) -- connect to the server client=net:newTCPClient("localhost",12345) -- connect to the server
client.OnClientReady(function(self) client.OnClientReady(function(self)
@ -41,8 +60,94 @@ client.OnDataRecieved(function(self,data) -- thats it clients only have to worry
end) end)
multi:mainloop() multi:mainloop()
``` ```
The bin and multi library can be found on my github page. They are all 100% pure lua so it should be easy to add to your project.
# Todo There is support for broadcasting, multicasting will be added soon requires luasocker 3.0+.</br>
- Write the wiki Here is a broadcasting example:</br>
- put examples on main page broadcastingExampleServer.lua (included in example folders)
- ```lua
package.path="?/init.lua;"..package.path
-- Note: you need 2 computers to test this! Broadcast does not on local host!
-- I have tested this code and it works, but only on seperate PC's within the same LAN network
require("bin")
require("multi")
require("net")
server=net:newServer(12345)
server.OnDataRecieved(function(self,data,CID_OR_HANDLE,IP_OR_HANDLE,PORT_OR_IP,UPDATER_OR_NIL)
if data=="Hello!" then -- copy from other example
print("Got response from client sending back data!")
self:send(IP_OR_HANDLE,"Hello Client!",PORT_OR_IP) -- doing it like this makes this code work for both udp and tcp
end
end)
server:broadcast("Lua_Server")
multi:mainloop()
```
broadcastingExampleClient.lua (included in example folders)
```lua
package.path="?/init.lua;"..package.path
require("bin")
require("multi")
require("net.aft")
client=net:newCastedClient("Lua_Server") -- searches the lan for this server name
-- Both udp and tcp clients can be broadcasted
client.OnClientReady(function(self)
self:send("Hello!")
end) -- For a tcp client the client is already ready, with udp a small handshake is done and the client is not instantly ready
client.OnDataRecieved(function(self,data) -- thats it clients only have to worry about itself and the server
if data=="Hello Client!" then
print("Server Responded Back!")
end
end)
multi:mainloop()
```
The net library also provides a powerful module creation interface. You have all of the modules in the net folder as examples, however I will show you how you could go about creating your own!</br>
**All functions include:</br>**
- net.OnServerCreated() -- Refer to the multi library example on connections: https://github.com/rayaman/multi#connections
- net.OnClientCreated() -- ^^
- net.normalize(input) -- formats data in base 64
- net.denormalize(input) -- takes base 64 data and turns it back into what it was
- net.getLocalIP() -- returns your loacl ip
- net.getExternalIP() -- If this ever stops working let me know... Ill have to update the service I am using
- net:registerModule(mod,version) -- registers a module. Checks out the example below
- net.getModuleVersion(ext) -- returns the version of a module
- net.resolveID(obj) -- an internal method for generating unique IDs obj is a table with an ID key
- net.inList(list,dat) -- checks if dat is a value in list
- net.setTrigger(funcW,funcE) -- Currently does nothing... I forgot where I was going with this
- net:newCastedClient(name) -- connects to a server that is being broadcasted see above example
- net:newServer(port,servercode) -- creates a UDP Server
- net:newClient(host,port,servercode,nonluaServer) -- creates a UDP Client
- net:newTCPServer(port) -- creates a TCP Server
- net:newTCPClient(host,port) -- creates a TCP Client
When using the module creation support here is the shell that you can use:
```lua
require("net") -- what do you need? other modules or the core? always require the core so users can require your module without having to require the core themself
local MODULENAME="EXAMPLE"
net:registerModule(MODULENAME,{1,0,0})
if not io.dirExists(string.upper(MODULENAME)) then -- do you need a directory to store stuff for your module?
io.mkDir(string.upper(MODULENAME))
end
net.OnServerCreated:connect(function(s)
s.OnDataRecieved(function(self,data,CID_OR_HANDLE,IP_OR_HANDLE,PORT_OR_IP)
local cmd,arg1,arg2=data:match("!"..MODULENAME.."! (%S+) (%S+) (%S+)") -- change to fit your needs
if cmd=="SEND" then
--
elseif cmd=="GET" then
--
end
end,MODULENAME)
end)
net.OnClientCreated:connect(function(c)
c.OnDataRecieved(function(self,data)
local cmd,arg1,arg2=data:match("!"..MODULENAME.."! (%S+) (%S+) (%S+)") -- change to fit your needs
if cmd=="SEND" then
--
elseif cmd=="GET" then
--
end
end,MODULENAME)
end)
```
The bin and multi library can be found on my github page. They are all '100%'(If you ingore intergrations) pure lua so it should be easy to add to your project.
https://github.com/rayaman/multi</br>https://github.com/rayaman/bin

13
examples/basicClient.lua Normal file
View File

@ -0,0 +1,13 @@
require("bin") -- this library needs a lot of work it has a bunch of old useless code, but also has many nice things as well that are really useful
require("multi") -- you need this to handle multiple connections and such
require("net") -- That requires the main library
client=net:newTCPClient("localhost",12345) -- connect to the server
client.OnClientReady(function(self)
self:send("Hello!")
end) -- For a tcp client the client is already ready, with udp a small handshake is done and the client is not instantly ready
client.OnDataRecieved(function(self,data) -- thats it clients only have to worry about itself and the server
if data=="Hello Client!" then
print("Server Responded Back!")
end
end)
multi:mainloop()

11
examples/basicServer.lua Normal file
View File

@ -0,0 +1,11 @@
require("bin") -- this library needs a lot of work it has a bunch of old useless code, but also has many nice things as well that are really useful
require("multi") -- you need this to handle multiple connections and such
require("net") -- That requires the main library
server=net:newTCPServer(12345) -- create a server that listens on port 12345
server.OnDataRecieved(function(self,data,CID_OR_HANDLE,IP_OR_HANDLE,PORT_OR_IP,UPDATER_OR_NIL) -- a bit confusing, but dont worry you will hardly ever need more then the first 5 arguments, unless you are writing modules!
if data=="Hello!" then
print("Got response from client sending back data!")
self:send(IP_OR_HANDLE,"Hello Client!",PORT_OR_IP) -- doing it like this makes this code work for both udp and tcp
end
end)
multi:mainloop()

View File

@ -0,0 +1,15 @@
package.path="?/init.lua;"..package.path
require("bin")
require("multi")
require("net.aft")
client=net:newCastedClient("Lua_Server") -- searches the lan for this server name
-- Both udp and tcp clients can be broadcasted
client.OnClientReady(function(self)
self:send("Hello!")
end) -- For a tcp client the client is already ready, with udp a small handshake is done and the client is not instantly ready
client.OnDataRecieved(function(self,data) -- thats it clients only have to worry about itself and the server
if data=="Hello Client!" then
print("Server Responded Back!")
end
end)
multi:mainloop()

View File

@ -0,0 +1,15 @@
package.path="?/init.lua;"..package.path
-- Note: you need 2 computers to test this! Broadcast does not on local host!
-- I have tested this code and it works, but only on seperate PC's within the same LAN network
require("bin")
require("multi")
require("net")
server=net:newServer(12345)
server.OnDataRecieved(function(self,data,CID_OR_HANDLE,IP_OR_HANDLE,PORT_OR_IP,UPDATER_OR_NIL)
if data=="Hello!" then -- copy from other example
print("Got response from client sending back data!")
self:send(IP_OR_HANDLE,"Hello Client!",PORT_OR_IP) -- doing it like this makes this code work for both udp and tcp
end
end)
server:broadcast("Lua_Server")
multi:mainloop()

View File

@ -1,9 +1,9 @@
--[[ --[[
UPCOMMING ADDITIONS UPCOMMING ADDITIONS
AUDP - advance udp/ Ensures packets arrive and handles late packets. AUDP - advance udp. Ensures packets arrive and handles late packets.
P2P - peer to peer (Server to set up initial connection) P2P - peer to peer (Server to set up initial connection)
Relay - offput server load (locally) Relay - offput server load (locally)
Threading - Simple threading (UDP/AUDP Only) Threading - Simple threading ~~(UDP/AUDP Only)~~ Thanks to an updated multi library we can thread with ease
Priority handling Priority handling
]] ]]
--[[ --[[
@ -31,8 +31,8 @@ socket=require("socket")
http=require("socket.http") http=require("socket.http")
mime=require("mime") mime=require("mime")
net={} net={}
net.Version={2,0,0} -- This will probably stay this version for quite a while... The modules on the otherhand will be more inconsistant 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.0" net._VERSION="2.0.1"
net.OnServerCreated=multi:newConnection() net.OnServerCreated=multi:newConnection()
net.OnClientCreated=multi:newConnection() net.OnClientCreated=multi:newConnection()
net.loadedModules={} net.loadedModules={}
@ -149,7 +149,7 @@ function net:newServer(port,servercode)
c.broad=socket.udp() c.broad=socket.udp()
c.hostip=net.getLocalIP() c.hostip=net.getLocalIP()
function c:broadcast(name) function c:broadcast(name)
local loop=multi:newTLoop(function(dt,loop) local loop=multi:newTLoop(function(loop,dt)
self.broad:setoption('broadcast',true) self.broad:setoption('broadcast',true)
self.broad:sendto(name.."|"..self.Type.."|"..self.hostip..":"..self.port, "255.255.255.255", 11111) self.broad:sendto(name.."|"..self.Type.."|"..self.hostip..":"..self.port, "255.255.255.255", 11111)
self.broad:setoption('broadcast',false) self.broad:setoption('broadcast',false)
@ -428,7 +428,7 @@ function net:newTCPServer(port)
c.broad=socket.udp() c.broad=socket.udp()
c.hostip=net.getLocalIP() c.hostip=net.getLocalIP()
function c:broadcast(name) function c:broadcast(name)
local loop=multi:newTLoop(function(dt,loop) local loop=multi:newTLoop(function(loop,dt)
self.broad:setoption('broadcast',true) self.broad:setoption('broadcast',true)
self.broad:sendto(name.."|"..self.Type.."|"..self.hostip..":"..self.port, "255.255.255.255", 11111) self.broad:sendto(name.."|"..self.Type.."|"..self.hostip..":"..self.port, "255.255.255.255", 11111)
self.broad:setoption('broadcast',false) self.broad:setoption('broadcast',false)