Skip to content
This repository has been archived by the owner on Aug 15, 2022. It is now read-only.

Message Pools

Brent Farris edited this page Apr 11, 2020 · 10 revisions
Directory Previous Next
Directory Network Message Interpreters Serialization Strategy

It is very likely that you will be creating messages that are not intended to be sent only once, but instead be sent many times over the runtime of your program, for this, message pooling comes in handy.

Creating a message pool

Forge comes with a generic class built in named MessagePool which will take care of all the backend pooling of messages so that you don't need to. A pool allows you to re-use the memory allocated to a message that you previously used of the same type. Below is an example of how a message pool for the message ChatMessage is created.

private MessagePool<ChatMessage> _chatMessagePool = new MessagePool<ChatMessage>();

To get a message from the pool you just need to call the Get function _chatMessagePool.Get(). This will take care of creating a new message if the pool is empty, otherwise it will return the first free message that can be used. Below is an example of how to use the message pool and sending off the message (modified from Network Messages documentation).

ChatMessage m = _chatMessagePool.Get();
m.Name = "Forge";
m.Text = "Hello World!";
IEngineFacade engine = GameObject.FindObjectOfType<ForgeEngineFacade>();
ISocket mySocket = engine.NetworkMediator.SocketFacade.ManagedSocket;
engine.NetworkMediator.SendReliableMessage(m, mySocket.EndPoint);

Conclusion

I would highly recommend using message pools whenever possible to help combat a large amount of GC and have more performant code.


Directory Previous Next
Directory Network Message Interpreters Serialization Strategy

Home

Getting Started
Network Contract Wizard (NCW)
Network Object
Remote Procedure Calls (RPCs)
Unity Integration
Basic Network Samples
Scene Navigation
NetWorker
Master Server
Web Server
Netcoding Design Patterns
Troubleshooting
Miscellaneous
Forge Networking Alloy
Steamworks
Clone this wiki locally