RakNetLua (updated)

Plugins y todo lo relacionado para Autoplay Media Studio.
Gracias!
Gracias!
Gracias
Pabloko escribió:
09 Jul 2016 23:30
RakNetLua
Documentation

THE CLIENT/SERVER LUA MODULE

Código: Seleccionar todo

RakNetLua is a raknet module for lua binding, that has enough api to open client/server communications at network level.
RakNet is a cross platform, open source, C++ networking engine for game programmers
INITIALIZATION
Just use require to load the module. no return values will be sent. Automaticly after this, both namespaces RakNet and BitStream will be available.
require("RakNetLua")

Working with the client

RakNet.OpenClient(string_ip_addr, number_port, string_password, boolean_treadkind)
This will request a connection opening to the server. If you didnt suscribed for packets, this is the moment to do it.
string_ip_addr: any ip or hostname string
number_port: port to server
string_password: string password used on the server, may use blank with empty string ""
boolean_treadkind: use MULTITHREADED or SINGLETHREADED, that got defined on pligin load.
No return will be sent
RakNet.OpenClient("127.0.0.1", 87, "", MULTITHREADED)
Remark on MULTITHREADED/SINGLETHREADED: multithread will spawn a process to read and send data trought network, if you use singlethreaded model you have to make calls to RakNet.Update() each a short value of milliseconds.
[hr][/hr]RakNet.Disconnect()
Closes the previously opened instance of raknet. Call this at least on you app closing event.
No arguments needed
No return will be sent

RakNet.Suscribe(number_packet_id, function_callback)
Closes the previously opened instance of raknet. Call this at least on you app closing event.
number_packet_id: The number of packet to suscribe
function_callback(BitStream): send a callback function where first argument is a BitStream instance. you can use anonymous function or named, just make sure it can handle the bitstream.
No return will be sent
PACKET_ID_CHAT=200
RakNet.Suscribe(PACKET_ID_CHAT, function(bsOut)
      --....do whatever with bsOut....
      print("Recieved chat data");
end)

RakNet.Send(bitstream_data)
Send a previously created (and filled) BitStream data to server.
bitstream_data Created with BitStream.Create(packet_id) or recieved on a suscription callback.
No return will be sent
local bsData = BitStream.Create(PACKET_ID_CHAT)
BitStream.Write(bsData, "test chat line", TSTRING)
RakNet.Send(bsData)

bitstream_data = BitStream.Create(number_packet_id)
Create a bitstream data object
number_packet_id The packet id to craft.
bitstream_data will be sent

data_value = BitStream.Read(bitstream_data, bitstream_type)
Read (next) chunk of data from a BitStram recieved on the suscription callback. it will read from the beggining of the packet and it will pop the data of bitstream for future readings.
bitstream_data the BitStream to read from.
bitstream_type the type of data
return data_value actual value of data
PACKET_ID_CHAT=200
RakNet.Suscribe(PACKET_ID_CHAT, function(bsOut)
      local chat_user = BitStream.Read(bsOut, TUINT8)
      local chat_text = BitStream.Read(bsOut, TSTRING)
      print("User id "..chat_user.." sent chat: "..chat_text);
end)

BitStream.Write(bitstream_data, data_value, bitstream_type)
Write (next) chunk of data on a BitStram. It will append the data on the packet.
bitstream_data the BitStream to write in.
bitstream_type the type of data
data_value the data to append.
no return will be sent
--see RakNet.Send example

Remark on BitSteam Types: The type of data determines how many data will consume the value on the packet, so always use lowest possible size for you data. All possible types are:
TBOOL, TUINT8, TINT8, TUINT16, TINT16, TUINT32, TINT32, TFLOAT, TDOUBLE, TLONG, TSTRING

Working with the server

RakNet.OpenServer(number_port, number_max_peers, string_password, boolean_treadkind)
This will open a server. If you didnt suscribed for packets, this is the moment to do it.
number_port: port to server
number_max_peers: maximum clients that can connect to the server
string_password: password of the servers that clients has to use, may be blank with ""
boolean_treadkind: use MULTITHREADED or SINGLETHREADED, that got defined on pligin load.
No return will be sent
RakNet.OpenServer(87, 500, "", MULTITHREADED)
Remark on MULTITHREADED/SINGLETHREADED: multithread will spawn a process to read and send data trought network, if you use singlethreaded model you have to make calls to RakNet.Update() each a short value of milliseconds.

All the client api are also available on server with few changes:
RakNet.Send(bitstream_data, number_peer_id)
RakNetSuscribe(number_packet_id, function_callback(bitstream_data, number_peer_id))
Both functions will recive extra parameters for user id number, in order to send data to correct client. on client version this is not used since you can only send data to 1 peer, the server.

string_ip, number_port, number_ping_avg = RakNet.GetUserNetwork(number_peer_id)
Get peer data from peer id number. returns ip, port and ping average.
RakNet.Kick(number_peer_id)
Kick some peer, dropping the connection.
RakNet.Ban(number_peer_id, number_seconds)
Kick a peer and disallow it from reconnecting in the given time.
[align=center]RakNetLua_Server.exe[/align]
its an easy to use executable that runs a lua script called server.lua and loads raknetlua on it. Its a general propourse server for windows that also run on linux with wine.
It adds to server an HTTP interface, and command line input. see server.lua in examples.
[align=center]DOWNLOAD & GIT[/align]
Git repo: http://git.euwebhost.com/Pabloko/lua-raknet/
Windows build (msvc aware) - dynamic lua vm 5.1.4 (luajit compatible) + demo
{{This message has been hidden}}
Imagen