GameLoop.Networking
1.0.0
¡Una biblioteca de redes UDP para juegos creados en GameLoop! Ofrece múltiples niveles de abstracción, desde un contenedor UDP simple de bajo nivel hasta niveles superiores.
// Spin up a socket listener. It listens for datagrams from any network address, on the specified port.
var peer = new NetworkSocket ( memoryPool ) ;
peer . Bind ( new IPEndPoint ( IPAddress . Any , port ) ) ;
// Send data. It sends data to the specified network address and port.
peer . SendTo ( new IPEndPoint ( 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 ( out NetworkArrivedData message ) )
{
var addressFrom = message . EndPoint ;
var receivedData = message . Data
// Execute your logic on received data.
}
La biblioteca no asigna memoria directamente. En cambio, delega esta responsabilidad a dos interfaces: IMemoryPool e IMemoryAllocator. Al implementar estas interfaces, puede manipular cómo se asigna y administra la memoria en su aplicación. La biblioteca contiene un asignador trivial y un grupo trivial que puede utilizar para crear prototipos rápidamente.
var memoryAllocator = new SimpleManagedAllocator ( ) ;
var memoryPool = new SimpleMemoryPool ( memoryAllocator ) ;
La biblioteca incluye algunas estructuras convenientes para trabajar con buffers, para ayudar al usuario a escribir/leer en/desde ellos.
// Reading from a buffer.
byte [ ] buffer = message . Data ;
var reader = default ( NetworkReader ) ;
reader . Initialize ( ref buffer ) ;
int myInt = reader . ReadInt ( ) ;
long myLong = reader . ReadLong ( ) ;
float myFloat = reader . ReadFloat ( ) ;
string myString = reader . ReadString ( ) ;
// etc
// Writing to a buffer.
var writer = default ( NetworkWriter ) ;
// 1) You can manually manage the buffer:
/* 1) */ byte [ ] buffer = memoryPool . Rent ( size ) ;
/* 1) */ writer . Initialize ( ref buffer ) ;
// 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 , ref initialState ) ;
// NOTE: this will automatically expand the buffer if a Write call requires more space.
writer . Write ( 12 ) ;
writer . Write ( 24f ) ;
writer . Write ( "36" ) ;
// etc