![]() |
|
Making games with netplay |
Felix-The-Ghost
Member #9,729
April 2008
![]() |
Hey guys, what library or sources do you recommend for learning how to implement netplay into your Allegro games? Something simple like an artillery game I mean, probably just over LAN. Anything for Windows and Mac? |
kazzmir
Member #1,786
December 2001
![]() |
I was going to look into http://www.zeromq.org/, it looks pretty neat. Otherwise maybe you want to try http://www.zoidcom.com/ |
Jimage
Member #4,205
January 2004
|
I used to use ZoidCom, but have since migrated to eNet http://enet.bespin.org/ as it provides a lot more control and doesn't try to do everything for you (that and it can be statically linked unlike ZoidCom). Never heard of zeromq, may be worth a look. |
Felix-The-Ghost
Member #9,729
April 2008
![]() |
GullRaDriel
Member #3,861
September 2003
![]() |
For something simple as an artillery game he does not need those hi-perf library. A simple wrapper for sockets can do the trick. Here is how I use mine, it's C and I can give you the sources and a working client server example. 1/* Basic structure to handle a network connection, client or server */
2NETWORK *n_client = NULL;
3
4int main( int argc , char *argv[] )
5{
6 /* Usefull in windows only, does nothing under linux, unix, mac. */
7 Init_All_Network( 2,2);
8
9 /* That's how you connect a client to a server */
10 Connect_Network ( &n_client , ip , PORT ) ;
11
12 /* Once connected, let's start some handy data collecting threads.
13 That part is a little overdone but it was for tests and it's working ! ;-p */
14 n_client -> state = NETW_RUN ;
15 Create_Send_Thread( n_client ) ;
16 Create_Recv_Thread( n_client ) ;
17
18 /*
19 * Main loop
20 */
21 do{
22 /* let's say it's a simple chat management
23 key enter will add a message to send to the given n_client,
24 which will be done as fast as possible. That's the sending thread jorb,
25 sending queued message as they're there */
26 if( key[ KEY_ENTER ] )
27 Add_Msg( n_client, buf, ustrsize(buf)+1 );
28
29
30 /* Let's check we don't have any message collected by the reception thread */
31 Get_Msg( n_client , &in_buf , &nboct);
32 if( in_buf )
33 {
34 /* Get_Msg didn't set in_buf to Null ? we HAD a message ! let's process
35 what's inside in_buf */
36 }
37 }while( !key[ KEY_ESC ] );
38 /* This example does not take care of the return value of the network functions,
39 so even if the connection is closed the main loop will run.
40 Can be corrected easily thought ;-) */
41
42 /* These are pretty self eplanative, but heh :
43 The stop function just stop the send/recv threads. You can still manually
44 send or receive */
45 Stop_Network( &n_client );
46 /* Effectively end up the connection and free the n_client structure */
47 Close_Network( &n_client );
48}END_OF_MAIN()
Anyway, it's just so you get the idea. It's compiling under windows and linux. Compiling under mac should be trivial. "Code is like shit - it only smells if it is not yours" |
kazzmir
Member #1,786
December 2001
![]() |
A simple wrapper around socket is reasonable, in my game I currently use hawknl which is basically a glorified wrapper around winsock and posix sockets. I did have to implement a lot of network logic that some of the mentioned libraries probably already do. Its partly a waste of time to implement such logic and its extremely difficult as more features are required. |
Felix-The-Ghost
Member #9,729
April 2008
![]() |
Those sock-it rappers are a lot easier looking, but I can't tell what's plain C or what's custom functions. Would I be able to use a LAN with that? Like I guess one computer would function as the host? I guess I'm kind of unfamiliar with the whole process. |
Sirocco
Member #88
April 2000
![]() |
Another vote for enet. I've been messing with it for a bit, and it seems like a good fit for lightweight apps that just need to swap data every now and then (for example, most non-MMO games). --> |
GullRaDriel
Member #3,861
September 2003
![]() |
Felix, take a look at the basics in the beejs guide. If it looks too much complicated then go for enet or whatever. "Code is like shit - it only smells if it is not yours" |
Don Freeman
Member #5,110
October 2004
![]() |
The actual networking code is pretty simple and can be picked up in about 10 minutes. The hard part is creating a protocol and serializing and de-serializing your data to be sent and received. -- |
GullRaDriel
Member #3,861
September 2003
![]() |
Writing a wrapper like mine is a 3 days works with the help of the beej's guide. Go Felix, go ! "Code is like shit - it only smells if it is not yours" |
bamccaig
Member #7,536
July 2006
![]() |
Felix-The-Ghost said: Those sock-it...
Socket, as in light socket, or socket wrench. Felix-The-Ghost said: ...rappers... Wrappers, as in Christmas, not Jay-Z. Felix-The-Ghost said: Would I be able to use a LAN with that? Like I guess one computer would function as the host? I guess I'm kind of unfamiliar with the whole process. The most common network protocol is IP, which is based on IP addresses. There's really no difference between your local area network (LAN) and the Internet as a whole. In fact, the Internet is basically just the result of taking thousands upon thousands of local networks and networking those together. That's what the Internet is: a network of networks. If there's a difference it's that your local area network is more feasible to ask questions at random. For example, you can broadcast that you're a server and expect clients on your network to get your messages. Essentially, broadcasting is just a regular message, but sent to a special address that is interpreted as a "send to all" address. If you've played games in the past with a LAN option that could detect servers on the network, this is pretty much the way they'd do it. In any case, for a simple game, you shouldn't need to worry about LAN vs. WAN or broadcasting or anything. All you need to worry about is IP addresses, and you can let the users worry about the exact addresses. The Beej's guide should tell you basically what you need to know. It's certainly recommended. Don't start with a game though. Start by just sending a single message from a "client" to a "server". Then you can worry about handling unlimited messages from the client to the server, and then worry about handling unlimited messages from the client to the server from multiple clients. At that point, you should be ready to figure out how it will apply to your game. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Felix-The-Ghost
Member #9,729
April 2008
![]() |
Yeah, I know what I typed when I said sock-it rappers |
GullRaDriel
Member #3,861
September 2003
![]() |
"Code is like shit - it only smells if it is not yours" |
Felix-The-Ghost
Member #9,729
April 2008
![]() |
GullRaDriel
Member #3,861
September 2003
![]() |
You're gessing it wrong. winsock is the header for windows, that's all. A couple of #define and you're done. Evil copy paste example: 1#ifdef LINUX
2 #include <sys/types.h>
3 #include <netinet/in.h>
4 #include <netinet/tcp.h>
5 #include <sys/socket.h>
6 #include <arpa/inet.h>
7
8 #ifndef SOCKET
9 #define SOCKET int
10 #endif
11
12 #define closesocket close
13
14 /*! Netflag for sigpipe */
15 #define NETFLAGS MSG_NOSIGNAL /* for program to not quit under linux when a connection is crashing 2 time*/
16 #define INVALID_SOCKET -1
17#endif
18
19#ifdef WIN32
20 #include <winsock.h>
21 #include <winsock2.h>
22
23 /*! Netflag for sigpipe */
24 #define NETFLAGS 0 /* no flags needed for microsoft */
25#endif
"Code is like shit - it only smells if it is not yours" |
Karadoc ~~
Member #2,749
September 2002
![]() |
Does anyone here have anything to say about boost::asio? I know next to nothing about sockets and networking etc. but that's what I've been experimenting with recently. How does ::asio compare to something like hawknl or enet? ----------- |
GullRaDriel
Member #3,861
September 2003
![]() |
Like comparing a 2cv and a Ferrari. "Code is like shit - it only smells if it is not yours" |
Karadoc ~~
Member #2,749
September 2002
![]() |
By that, do you mean it is like comparing two network libraries that are designed with different target audiences in mind — and that there is one that most people would prefer to have, but that one is much more expensive? I wonder which one is which!? ... Part of the reason I started looking at ::asio is that I heard somewhere that it might become part of the standard C++0x library. I don't know if that's true or not, but if is true then I figure that it's worth knowing about. [edit] But... suppose I'm making a turn-based game in which latency isn't particularly important, and suppose I put a high value on using 'standard' protocols and libraries for support and security reasons (or whatever other reasons). In that case, how does Enet compare to boost::asio::tcp? It seems to me that if the latency advantages of Enet are not important to me, then maybe the boost::asio would be the better choice just because it is the better known 'brand'. I'm interested to hear more thoughts and comments about this. ----------- |
GullRaDriel
Member #3,861
September 2003
![]() |
As said before, for something as simple as a turn based thing, I would go by building myself a little wrapper around the socket library, using tcp. IMHO, ENET is overwhelmed for turn based thing. Like using boost, it's just too much. Perhaps it's me, but just reading the beej's guide and you know how to send and receive data, synchronously, asynchronously, ect. "Code is like shit - it only smells if it is not yours" |
BAF
Member #2,981
December 2002
![]() |
The nice thing about a premade library is that you don't have to deal with the mundane boilerplate code, writing the ifdefs for winsock, etc. |
Karadoc ~~
Member #2,749
September 2002
![]() |
GullRaDriel said: IMHO, ENET is overwhelmed for turn based thing. Like using boost, it's just too much. I really don't understand what you mean. Saying that Enet is overwhelmed by turn-based games suggests it just can't handle turn-based games. Presumably you didn't mean that. And saying that 'boost is too much'... too much what? Too much power and flexibility for such a simple task? Or is it the opposite: too much time and effort to make it actually do such a simple task? My guess is that you mean that both libraries are 'overkill' for turn-based games; ie. that they have many more features than turn-based games would require. But wouldn't that still be easier than writing your own sock-it rapper? ENet in particular seems pretty light-weight and straight forward. I think if I was to write my own wrapper then I'd probably just end up with a worse version of ENet. Meanwhile, asio has a heap more features because it isn't only a network library. It seems to me that asio could function as a viable replacement to Allegro5's event system (which I'm a great fan of). But the asio network stuff is actually pretty low level. There are sockets and accessors and resolvers and io_services... the whole thing is pretty heavy stuff for a newbie like me, but I think that if I was going to make a wrapper around anything, it would be a wrapper around the asio stuff — because like BAF said, that would allow me to avoid ifdefs and such. ... I feel like this post goes a long way towards answering my own questions about boost::asio vs ENet. I guess I knew more about it than I thought. ----------- |
GullRaDriel
Member #3,861
September 2003
![]() |
BAF said: writing the ifdefs for winsock, etc.
3 copy paste. Perhaps 4 or 5 and I do not touch write something else than google search key. Even if you are a lazy guys, that's quicker than downloading and installing a library. Copy paste from beej guides and you'll have a basic client server. Hell I didn't ever wrote a line of code ! Well you have idea. Karadoc ~~ said: because like BAF said, that would allow me to avoid ifdefs and such. Yeah. Let's embed a 1000000K lines of code instead of 100k lines just before we can't afford to include a #ifdef. Plus don't dare to say all all your project are already coded to be cross platform. In most way, especially for a game, you'll target win32 computers. As said before, there are only the headers and a few variables to take in account (like 3) and you have your cross platform sock library. Boost:asio server example (from the docs) #include <cstdlib> #include <iostream> #include <boost/aligned_storage.hpp> #include <boost/array.hpp> #include <boost/bind.hpp> #include <boost/enable_shared_from_this.hpp> #include <boost/noncopyable.hpp> #include <boost/shared_ptr.hpp> #include <boost/asio.hpp>
that's what I call a bunch for doing a simple thing as turn based data. I would use Boost:asio for an html viewer, some kind of webservice, ect. But not for a game. I said ENET is overwhelmed because it's more aimed to fast paced network than to turn based. If I was to choose between ENET and Boost:asio I would go for Enet which is a lot simpler to implement. And if you leave the choice to me, I would use a simple wrapper, as simple as possible, around the socket library (and use TCP BTW). "Code is like shit - it only smells if it is not yours" |
|