ffrostflame / bytenet Goto Github PK
View Code? Open in Web Editor NEWAn advanced, modern networking library for Luau/Roblox
Home Page: https://ffrostflame.github.io/ByteNet/
License: MIT License
An advanced, modern networking library for Luau/Roblox
Home Page: https://ffrostflame.github.io/ByteNet/
License: MIT License
In cases where you are firing events on PlayerAdded it is highly likely that the following error will be produced:
This seems to occur because perPlayerReliable[player]/perPlayerReliable[player] are nil.
I was able to fix this issue by making the following modications to the server
module:
server.start
:
[...]
local function playerAdded(player: Player)
if not perPlayerReliable[player] then
perPlayerReliable[player] = create()
end
if not perPlayerUnreliable[player] then
perPlayerUnreliable[player] = create()
end
end
for _, player in Players:GetPlayers() do
playerAdded(player)
end
Players.PlayerAdded:Connect(playerAdded)
[...]
server.sendPlayerReliable
:
function serverProcess.sendPlayerReliable(
player: Player,
id: number,
writer: (value: any) -> (),
data: { [string]: any }
)
if not perPlayerReliable[player] then
perPlayerReliable[player] = create()
end
load(perPlayerReliable[player])
alloc(1)
u8(id)
writer(data)
perPlayerReliable[player] = bufferWriter.export()
end
server.sendPlayerUnreliable
:
function serverProcess.sendPlayerUnreliable(
player: Player,
id: number,
writer: (value: any) -> (),
data: { [string]: any }
)
if not perPlayerUnreliable[player] then
perPlayerUnreliable[player] = create()
end
load(perPlayerUnreliable[player])
alloc(1)
u8(id)
writer(data)
perPlayerUnreliable[player] = bufferWriter.export()
end
Packet type has method sendToList
defined, but this method does not exist (should be sendToPlayers). It is also missing sendToAllExcept
.
Corrected type:
type Packet<T> = {
sendToAll: (data: T) -> (),
sendTo: (data: T, target: Player) -> (),
sendToPlayers: (data: T, targets: { Player }) -> (),
sendToAllExcept: (data: T, except: Player) -> (),
wait: () -> T,
send: (data: T, target: Player?) -> (),
listen: (callback: (data: T, player: Player?) -> ()) -> (),
}
For some reason, using a struct inside of an array causes the struct to need the value key when being declared.
ex:
export const reflex = defineNamespace("reflex", () => {
const broadcastAction = struct({
name: string,
arguments: array(unknown),
});
return {
dispatch: definePacket({
value: array(broadcastAction),
reliabilityType: "reliable",
}),
};
});
reflex.dispatch.sendToAll([{ name: "a", arguments: [] }]); // does not work
reflex.dispatch.sendToAll([{ value: { name: "a", arguments: [] } }]); // works
reflex.dispatch.sendToAll([{ name: "a", arguments: [] }]);
-----------------------------^^^^
Object literal may only specify known properties, and 'name' does not exist in type 'struct<{ name: ByteNetType<string>; arguments: array<ByteNetType<unknown>>; }>'.
Convenient methods to avoid writing for loops
Checking out your current encode/decode logic the system seems neat and could be relatively easily adjusted to drop the need for dynamically growing buffer object by allocating just the right size buffer.
I think this could be achived by adding new method to the dataTypeInterface<T>
called size
, which would then compute and return size of the value it represents.
Not only this would greatly speed up your encoding/decoding process, but also simplify the code by removing explicit memory management via bufferWriter.alloc
which scatters dynamic allocations across the codebase.
This same idea could be also applied to the server and client queueing logic. Instead of appending the new packet's buffer to the pending buffer, keep all the pending buffers in a table, which will be your queue and then right before sending compute the total packet size, allocate one large buffer and copy each buffers content into the final buffer.
I use similar method in my Luau msgpack implementation and given that it can outperform native JSON encode/decode methods shows that now one can actually reason about memory allocation patterns in Luau and do a better job than native code that most likely neglected these concerns.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.