⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 article841.asp.htm

📁 一个网络游戏程序编写的例子
💻 HTM
字号:


<!--<TABLE>
	<TR>
		<TD>
			Why Pluggable Factories Rock My Multiplayer World
		</TD>
		<TD>
			See Also:
		</TD>
	</TR>
	<TR>
		<TD COLSPAN=2>-->

<FONT FACE="Verdana, Tahoma, Arial" SIZE="2">

<CENTER><FONT COLOR="#003E98" SIZE="5"><B>Why Pluggable Factories Rock My Multiplayer World</B></FONT>
<BR><B>by <A HREF="javascript:if(confirm('http://www.spin-studios.com/  \n\n这个文件不能通过 Teleport Pro 取回, 因为 它被访问于一个域或在它的起始地址边界外部的路径上.  \n\n你想从服务器打开它吗?'))window.location='http://www.spin-studios.com/'" tppabs="http://www.spin-studios.com/">Mason McCuskey</A></B></CENTER>

<P><FONT COLOR="#00983E" SIZE="4"><B>Introduction</B></FONT>
<P>I've developed a nasty habit over the years.  Whenever I come across a business programming article, I instinctively assume that it won't be relevant to anything cool.  My initial reaction is usually "OK, wow, this is great for writing middleware, but probably useless in game programming."
<P>Most of the time this turns out to be true (when was the last time you used a SQL database to store saved games?), however, there are always a few articles that describe something that can be useful for game programming.  One of those articles recently appeared in the magazine "C++ Report." (<A HREF="javascript:if(confirm('http://www.creport.com/  \n\n这个文件不能通过 Teleport Pro 取回, 因为 它被访问于一个域或在它的起始地址边界外部的路径上.  \n\n你想从服务器打开它吗?'))window.location='http://www.creport.com/'" tppabs="http://www.creport.com/">http://www.creport.com</A>).  Timothy R. Culp wrote an article entitled <A HREF="javascript:if(confirm('http://archive.creport.com/9910/html/from_pages/feature.shtml  \n\n这个文件不能通过 Teleport Pro 取回, 因为 它被访问于一个域或在它的起始地址边界外部的路径上.  \n\n你想从服务器打开它吗?'))window.location='http://archive.creport.com/9910/html/from_pages/feature.shtml'" tppabs="http://archive.creport.com/9910/html/from_pages/feature.shtml">"Industrial Strength Pluggable Factories."</A>  In it, he describes a very valuable trick, not only in the business world, but in game programming as well.
<P>This article is an attempt to take Mr. Culp's work and bring it down into the scary mosh pit of game development.  Before continuing, head over to the C++ Report website and read the pluggable factories article.  I'm not going to duplicate what's already been said; I'm going to assume you've read the article and know the basics, and I'm going to dive straight into showing how Pluggable Factories can be used to simplify DirectPlay communications.

<P><FONT COLOR="#00983E" SIZE="4"><B>The Problem</B></FONT>
<P>Networked multiplayer apps today must deal with a wide variety of messages.   There's the standard set of DirectPlay messages (Create Player, Delete Player, Chat, etc.), as well as the army of messages your game needs to communicate.  All of these messages have their own data items, and they all must be able to send themselves through a DirectPlay connection and reassemble themselves on the other side. It's your job as a network game programmer to sort everything out so that your game has an elegant way to send and receive its information.
<P>The obvious way to do it in C++ is to use classes to represent the different messages.  These classes contain all of the data for a particular message, as well as methods that serialize and deserialize the data into a byte stream (suitable for sending over a DirectPlay connection).  Also, since all of the messages have certain data elements in common (like, who the message was from, and who it's going to), it makes sense to implement an abstract base class and then derive each different message type from it, like so:

<BLOCKQUOTE><PRE><FONT FACE="Courier New, fixedsys" SIZE="2" COLOR="#000088">
// the net_message base class
class net_message
{
public:
  net_message() { }
  ~net_message() { clear(); }
  
  void clear(void) { }

  virtual int serializeto(byte *output) { return(0); } 
  virtual void serializefrom(byte *fromdata, int datasize) { }

  DPID getfrom(void) { return(m_from); }
  DPID getto(void)   { return(m_to);   }

protected:
  
  void setfrom(DPID id) { m_from = id; }
  void setto(DPID id) { m_to = id; }

  DPID m_from;
  DPID m_to;
};

// a specific message derived from the base class 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -