139 lines
4.3 KiB
HTML
139 lines
4.3 KiB
HTML
<HTML>
|
|
<HEAD>
|
|
|
|
<TITLE>Tutorial code sample 2</TITLE>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></HEAD>
|
|
<meta name="title" content="RakNet - Advanced multiplayer game networking API">
|
|
</HEAD>
|
|
<BODY BGCOLOR="#ffffff" LINK="#003399" vlink="#003399" alink="#003399" LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0"">
|
|
<img src="RakNetLogo.jpg" alt="Oculus VR, Inc."><BR><BR>
|
|
|
|
<table width="100%" border="0"><tr><td bgcolor="#6699CC"><font color="#FFFFFF" size="3" face="Arial, Helvetica, sans-serif"><strong>
|
|
<img src="spacer.gif" width="8" height="1">Tutorial code sample 2</strong></font></td></tr></table>
|
|
<TABLE BORDER="0" CELLPADDING="10" CELLSPACING="0" WIDTH="100%"><TR><TD>
|
|
<FONT FACE="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2" CLASS="G10" COLOR="#3366CC"><strong>Connecting, reading, and parsing network messages.
|
|
</strong></FONT>
|
|
<FONT FACE="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2" CLASS="G10" COLOR="#666666">
|
|
<BR><BR>
|
|
The target of this exercise was to add the following features to sample 1:
|
|
<OL>
|
|
<LI>Create a main loop for the program.
|
|
<LI>Call Receive and store the pointer returned in a pointer variable of type Packet. Note that we continually call Receive() until no further packets are returned. A common mistake is to only call it once.
|
|
<LI>Include the header file to use struct Packet
|
|
<LI>If a packet arrived, check the first byte of Packet::data, and process accordingly. The list of enumerations is in MessageIdentifiers.h.
|
|
<LI>Print out the comment that goes along with the enumeration.
|
|
<LI>Deallocate the packet pointer when done with it.
|
|
</OL>
|
|
New code over sample 1 is in bold.
|
|
</FONT>
|
|
<PRE><FONT FACE="Geneva, Verdana, Arial, Helvetica, sans-serif" size="1" CLASS="G10" COLOR="#111122">
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
<strong>#include <string.h></strong>
|
|
#include "RakPeerInterface.h"
|
|
<strong>#include "MessageIdentifiers.h"</strong>
|
|
|
|
#define MAX_CLIENTS 10
|
|
#define SERVER_PORT 60000
|
|
|
|
int main(void)
|
|
{
|
|
char str[512];
|
|
|
|
RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
|
|
bool isServer;
|
|
<strong>RakNet::Packet *packet;</strong>
|
|
|
|
printf("(C) or (S)erver?\n");
|
|
gets(str);
|
|
|
|
if ((str[0]=='c')||(str[0]=='C'))
|
|
{
|
|
RakNet::SocketDescriptor sd;
|
|
peer->Startup(1,&sd, 1);
|
|
isServer = false;
|
|
} else {
|
|
RakNet::SocketDescriptor sd(SERVER_PORT,0);
|
|
peer->Startup(MAX_CLIENTS, &sd, 1);
|
|
isServer = true;
|
|
}
|
|
|
|
<strong>
|
|
if (isServer)
|
|
{
|
|
printf("Starting the server.\n");
|
|
// We need to let the server accept incoming connections from the clients
|
|
peer->SetMaximumIncomingConnections(MAX_CLIENTS);
|
|
} else {
|
|
printf("Enter server IP or hit enter for 127.0.0.1\n");
|
|
gets(str);
|
|
if (str[0]==0){
|
|
strcpy(str, "127.0.0.1");
|
|
}
|
|
printf("Starting the client.\n");
|
|
peer->Connect(str, SERVER_PORT, 0,0);
|
|
|
|
}
|
|
|
|
while (1)
|
|
{
|
|
for (packet=peer->Receive(); packet; peer->DeallocatePacket(packet), packet=peer->Receive())
|
|
{
|
|
switch (packet->data[0])
|
|
{
|
|
case ID_REMOTE_DISCONNECTION_NOTIFICATION:
|
|
printf("Another client has disconnected.\n");
|
|
break;
|
|
case ID_REMOTE_CONNECTION_LOST:
|
|
printf("Another client has lost the connection.\n");
|
|
break;
|
|
case ID_REMOTE_NEW_INCOMING_CONNECTION:
|
|
printf("Another client has connected.\n");
|
|
break;
|
|
case ID_CONNECTION_REQUEST_ACCEPTED:
|
|
printf("Our connection request has been accepted.\n");
|
|
break;
|
|
case ID_NEW_INCOMING_CONNECTION:
|
|
printf("A connection is incoming.\n");
|
|
break;
|
|
case ID_NO_FREE_INCOMING_CONNECTIONS:
|
|
printf("The server is full.\n");
|
|
break;
|
|
case ID_DISCONNECTION_NOTIFICATION:
|
|
if (isServer){
|
|
printf("A client has disconnected.\n");
|
|
} else {
|
|
printf("We have been disconnected.\n");
|
|
}
|
|
break;
|
|
case ID_CONNECTION_LOST:
|
|
if (isServer){
|
|
printf("A client lost the connection.\n");
|
|
} else {
|
|
printf("Connection lost.\n");
|
|
}
|
|
break;
|
|
default:
|
|
printf("Message with identifier %i has arrived.\n", packet->data[0]);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
</strong>
|
|
|
|
RakNet::RakPeerInterface::DestroyInstance(peer);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
</FONT></PRE>
|
|
</TD></TR></TABLE>
|
|
</BODY>
|
|
</HTML>
|