For my final in my Networking course, my group and I joked about making a “Pong MMO”. We ended up getting inspired however and ended up making a server application using Raknet, and then making a Raknet DLL for Unity to send and receive messages from that server and then transmit data to Unity.
The server is a C++ application using Raknet that keeps track of all current players, and acts as a middle man for sending messages and notifications to other players. On top of this, the server can also keep track of several Pong games (256) running at once. I realize that a real commercial server would have to be more robust but we were making this for educational purposes.
Our FrameworkState class, that acts as a client.
DLL code, basically acts as a wrapper for functions in the class above.
The plugin mostly consists of wrapper functions that we call from Unity that wrap around our RakNet Peer class’ functionalities. Our plugin also contains an instance of our FrameworkState class, this class acts as a client to the server, and is in charge of sending messages to the server, and receiving and decoding messages. By doing this, we can completely manage our own Networking within the DLL, and from there, all we have to worry about is getting the relevant data to Unity. We accomplish this by having a dynamic byte array that we write data to. Unity can then access this byte array and parse the data as needed depending on the message.
An example of the execute function for one of our message types.
In order to prep for this, when receiving a message from the server, we add it to our message event queue. When the message is executed, we use polymorphism in order to get different results depending on the message type. What matters is that we set the byte array to have the data we want it to have, and then we return an int. This int is basically an ID that Unity uses in order to know how to parse the byte array correctly.
Interfacing with Unity
Where most of the magic happens…
On the Unity end, we import the functions from our DLL, in this case we’re using the updateEventManager() function above. This is where the ID is important, and where we access the dataBuffer from the plugin in order to affect the game objects and game state in Unity. It isn’t shown here, but we also call the various sendMessage() functions from Unity, which is how the plugin interacts with our server.
Our Networking Professor is a cool guy.
After all is said and done, the result is a Unity executable where you can connect to an active server, big chill in a chat room, and then play some pong with your friends. I understand that the game itself is simple, but the group’s main focus was setting up the architecture for our custom built networking. If we were to do more with this project, I’d definitely dive into latency issues and server correction techniques in order to make the smoothest pong experience possible.