sfchat - a client/server chat class for SFML

For the last year or so I have been acquainting myself with SFML (specifically the RC for version 2), which is a cross-platform multimedia library aimed at writing 2D OpenGL accelerated games. Recently I have been working on a class with the intent on making it available for other users to enable them to add in-game chat to multiplayer games. To make the class as easy to use as possible I've provided a simple interface to a single 'chat object' which is capable of maintaining the network connection, hooking keyboard input and parsing strings/commands. Using the class essentially boils down to:

Create the object.

Call Chat::Create() or Chat::Connect() to create a server or a client

Hook the SFML window events in the event poll loop with Chat::Update(event)

Set the string of a sf::Text object with the return value of Chat::GetText()

Draw the text.

This is a little bit generalised, but more or less the gist of what's needed for someone to add chat capability to their project. Currently sfchat supports IRC style commands such as /nick to change your name or /team to send messages to only clients who are on the same team. The class also allows logging of conversations to text files on both the client and the server.

Inside the class things work like this:
The Chat class contains members for buffering the input and output strings - that is a string member to hold whatever a user is typing in, and a std::deque to hold the last 20 lines of chat or so (the value can be configured in the source). Once a line is entered it's pushed back onto the deque and, if the deque has reached the predefined size, the front is popped. The chat class is capable as running as either a server or a client depending on whether or not Chat::Create() or Chat::Connect() was called. These functions both set up a few parameters (such as port number or IP address) before creating a new thread dedicated to running either a client or server.

The server thread uses the sf::SocketSelector class to hold a list of references to incoming tcp socket connections from clients. The program basically loops over this list, first listening for new connections, then checking for incoming data from existing connections. Any messages it receives are then pushed onto the back of the output buffer. Once this is done the server then loops over the list of connected clients sending out the current message from the back of the output buffer, as well as any updates such as name changes or client disconnects.

The client thread polls the input buffer for new messages and then sends any it finds out to the server if a connection is available, along with any name change requests. Any messages received from the server are parsed accordingly, usually being pushed onto the back of the local deque. If a connection is lost then the thread exits and the chat object returns to a disconnected state which can then either be used to create a new server or connect to another one.

Currently most of my planned features are implimented bar allowing text input via the console when running a console-only server (removing the dependency on OpenGL). There are also a few bugs of which I'm aware and, I'm sure, plenty of which I'm not. sfchat needs some thorough testing. I've uploaded the source to google code where anyone who wants to try it out can get it and leave feedback. I'm also in the process of trying to document it as accurately as possible although it tends to get outdated rather quickly as I fix / add new features.

Here's a screenshot showing the example applications included with the source code:

Popular Posts