/// <summary> /// Inicializate Client /// max_channel default value is 0 and mean all channel, required /// timeout default value is 15, required /// </summary> /// <param name="ip_address">IP used for server </param> /// <param name="port">Port used for server</param> /// <param name="channel">Max channel used in server - Opcional</param> /// <param name="timeout">Max time to client's response time</param> public Client(string ip_address, ushort port, int channel, int timeout) { AllocCallback OnMemoryAllocate = (size) => { return(Marshal.AllocHGlobal(size)); }; FreeCallback OnMemoryFree = (memory) => { Marshal.FreeHGlobal(memory); }; NoMemoryCallback OnNoMemory = () => { throw new OutOfMemoryException(); }; Callbacks callbacks = new Callbacks(OnMemoryAllocate, OnMemoryFree, OnNoMemory); if (ENet.Library.Initialize(callbacks)) { Debug.LogWarning("ENet successfully initialized using a custom memory allocator"); } client = new Host(); Address address = new Address(); address.Port = port; address.SetHost(ip_address); client.Create(); client.EnableCompression(); peer = client.Connect(address); }
/// <summary> /// Inicializate server /// max_channel default value is 0 and mean all channel, required /// timeout default value is 15, required /// </summary> /// <param name="ip_address">IP used for server </param> /// <param name="port">Port used for server</param> /// <param name="max_clients">Max clients in server</param> /// <param name="max_channel">Max channel used in server - Opcional</param> /// <param name="timeout">Max time to client's response time</param> public Server(string ip_address, ushort port, int max_clients, int max_channel, int timeout) { AllocCallback OnMemoryAllocate = (size) => { return(Marshal.AllocHGlobal(size)); }; FreeCallback OnMemoryFree = (memory) => { Marshal.FreeHGlobal(memory); }; NoMemoryCallback OnNoMemory = () => { throw new OutOfMemoryException(); }; Callbacks callbacks = new Callbacks(OnMemoryAllocate, OnMemoryFree, OnNoMemory); if (ENet.Library.Initialize(callbacks)) { //Debug.LogWarning("ENet successfully initialized using a custom memory allocator"); clients = new List <Peer>(); } //init server Address address = new Address(); address.SetHost(ip_address); address.Port = port; server = new Host(); server.Create(address, max_clients, max_channel); server.EnableCompression(); this.timeout = timeout; // Debug.Log("Create server IP : " + ip_address); }
public Callbacks(AllocCallback allocCallback, FreeCallback freeCallback, NoMemoryCallback noMemoryCallback) { nativeCallbacks.malloc = allocCallback; nativeCallbacks.free = freeCallback; nativeCallbacks.noMemory = noMemoryCallback; }
public Callbacks(AllocCallback allocCallback, FreeCallback freeCallback, NoMemoryCallback noMemoryCallback) { nativeCallbacks.malloc = Marshal.GetFunctionPointerForDelegate(allocCallback); nativeCallbacks.free = Marshal.GetFunctionPointerForDelegate(freeCallback); nativeCallbacks.no_memory = Marshal.GetFunctionPointerForDelegate(noMemoryCallback); }