Skip to content

Repository files navigation

GL-RTS_banner

GameLoop.Networking

A UDP networking library for games made in GameLoop! It offers multiple abstraction levels, starting from low-level simple UDP wrapper up to higher-level ones.

Usage of low-level UDP wrapper

// Spin up a socket listener. It listens for datagrams from any network address, on the specified port.varpeer=newNetworkSocket(memoryPool);peer.Bind(newIPEndPoint(IPAddress.Any,port));
// Send data. It sends data to the specified network address and port.peer.SendTo(newIPEndPoint(IPAddress.Loopback,port));
// Polling for data. It fetches a message from the receiving queue, if any. If nothing has been received, it just returns "false".while(peer.Poll(outNetworkArrivedDatamessage)){varaddressFrom=message.EndPoint;varreceivedData=message.Data// Execute your logic on received data.}

IMemoryPool and IMemoryAllocator

The library does not allocates memory directly. Instead it delegates this responsability to two interfaces: IMemoryPool and IMemoryAllocator. By implementing these interfaces, you can manipulate how memory is allocated and managed in your application. The library contains a trivial allocator and a trivial pool you can use to prototype quickly.

varmemoryAllocator=newSimpleManagedAllocator();varmemoryPool=newSimpleMemoryPool(memoryAllocator);

Working with buffers

The library includes some convenient structures to work with buffers, to help the user to write/read to/from them.

// Reading from a buffer.byte[]buffer=message.Data;varreader=default(NetworkReader);reader.Initialize(refbuffer);intmyInt=reader.ReadInt();longmyLong=reader.ReadLong();floatmyFloat=reader.ReadFloat();stringmyString=reader.ReadString();// etc
// Writing to a buffer.varwriter=default(NetworkWriter);// 1) You can manually manage the buffer:/* 1) */byte[]buffer=memoryPool.Rent(size);/* 1) */writer.Initialize(refbuffer);// NOTE: in this way the writer will NOT expand the buffer if a Write call requires more space.// OR// 2) You can let the writer to manage internally the buffer:/* 2) */writer.Initialize(memoryPool,initialSize);// NOTE: this will automatically expand the buffer if a Write call requires more space.// OR// 3) You can pass a buffer to copy its initial state, but let the writer to manage its own buffer internally:/* 3) */byte[]initialState=memoryPool.Rent(size);/* 3) */writer.Initialize(memoryPool,refinitialState);// NOTE: this will automatically expand the buffer if a Write call requires more space.writer.Write(12);writer.Write(24f);writer.Write("36");// etc

About

A UDP networking library for games made in GameLoop!

Topics

Resources

Stars

17 stars

Watchers

5 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages