Game Development Reference
In-Depth Information
listening. This is how the game attaches new players. It relates the socket ID to an
object or actor in the game and a new kind of game view that fools the server into
thinking that the client is actually a human player playing on the same system.
You are now ready to see the final piece of this puzzle
—
how the sockets system ties
into the event system and the game views.
'
s take inventory. What have you learned so far in this chapter?
n
NetSocket()
and
ClientSocketManager()
work together to create the
generic client side of the network communications.
n
NetListenSocket()
and
BaseSocketManager()
work together to create
the generic server side of the network communications.
n
GameServerListenSocket()
is a custom server-side class that creates special
sockets that can take network data and translate them into events that game
systems can listen to, just like you saw in Chapter 10,
“
User Interface
Programming.
”
So what
s left? A few things, actually. You need a socket that can translate network
data into events, and you also need a class that can take events and create network
packets to be sent along to remote computers
'
client or server. Both the client and
the server will do this because they both generate and listen for events coming from
the other side.
Translating C++ objects of any kind requires streaming. There are tons of useful
implementations of streams out there, and in my great practice of doing something
rather stupid to make a point, I
—
'
m going to show you how to use STL
istrstream
and
ostrstream
templates.
Even though I
'
m an old-school C hound and still use
printf()
everywhere, I
'
m
sure many of you have seen streams like this:
char nameBuffer[64];
cout <<
“
Hello World! What is your name?
”
;
cin >> nameBuffer;
The
istrstream
and
ostrstream
work very similarly. Think of them as a string-
based memory stream that you can read from and write to very easily. At some point
in this topic, I mentioned how useful it was to use streams to initialize C++ objects
and use them to save them out to disk for saved games. Well, here
'
s an example of
what this looks like with a simple C++ object: