zoukankan      html  css  js  c++  java
  • RakNet基本教程

    官方网址:http://www.jenkinssoftware.com/raknet/manual/tutorialsample3.html

    Tutorial code sample 3
    Remote procedure call setup. 

    The target of this exercise was to add the following features to sample 2:
    1. When the client successfully connects, send a custom user message.
    2. The first byte of custom messages should always start after the enumeration ID_USER_PACKET_ENUM, defined in MessageIdentifiers.h. Otherwise, RakNet would confuse its own identifiers with your game identifiers.
    3. Read the custom message in our packet processing loop. The native RakString type can read and write to the BitStream class. You would not be able to do this using std::string.
    New code over sample 2 is in bold.
    
    #include <stdio.h>
    #include <string.h>
    #include "RakPeerInterface.h"
    #include "MessageIdentifiers.h"
    #include "BitStream.h"
    #include "RakNetTypes.h"  // MessageID
    
    #define MAX_CLIENTS 10
    #define SERVER_PORT 60000
    
    enum GameMessages
    {
    	ID_GAME_MESSAGE_1=ID_USER_PACKET_ENUM+1
    };
    
    int main(void)
    {
    	char str[512];
    
    	RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
    	bool isServer;
    	RakNet::Packet *packet;
    
    	printf("(C) or (S)erver?
    ");
    	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;
    	}
    
    	if (isServer)
    	{
    		printf("Starting the server.
    ");
    		// 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
    ");
    		gets(str);
    		if (str[0]==0){
    			strcpy(str, "127.0.0.1");
    		}
    		printf("Starting the client.
    ");
    		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.
    ");
    				break;
    			case ID_REMOTE_CONNECTION_LOST:
    				printf("Another client has lost the connection.
    ");
    				break;
    			case ID_REMOTE_NEW_INCOMING_CONNECTION:
    				printf("Another client has connected.
    ");
    				break;
    			case ID_CONNECTION_REQUEST_ACCEPTED:
    				{
    					printf("Our connection request has been accepted.
    ");
    
    					// Use a BitStream to write a custom user message
    					// Bitstreams are easier to use than sending casted structures, and handle endian swapping automatically
    					RakNet::BitStream bsOut;
    					bsOut.Write((RakNet::MessageID)ID_GAME_MESSAGE_1);
    					bsOut.Write("Hello world");
    					peer->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,packet->systemAddress,false);
    				}
    				break;
    			case ID_NEW_INCOMING_CONNECTION:
    				printf("A connection is incoming.
    ");
    				break;
    			case ID_NO_FREE_INCOMING_CONNECTIONS:
    				printf("The server is full.
    ");
    				break;
    			case ID_DISCONNECTION_NOTIFICATION:
    				if (isServer){
    					printf("A client has disconnected.
    ");
    				} else {
    					printf("We have been disconnected.
    ");
    				}
    				break;
    			case ID_CONNECTION_LOST:
    				if (isServer){
    					printf("A client lost the connection.
    ");
    				} else {
    					printf("Connection lost.
    ");
    				}
    				break;
    				
    			case ID_GAME_MESSAGE_1:
    				{
    					RakNet::RakString rs;
    					RakNet::BitStream bsIn(packet->data,packet->length,false);
    					bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
    					bsIn.Read(rs);
    					printf("%s
    ", rs.C_String());
    				}
    				break;
    			
    			default:
    				printf("Message with identifier %i has arrived.
    ", packet->data[0]);
    				break;
    			}
    		}
    	}
    
    
    	RakNet::RakPeerInterface::DestroyInstance(peer);
    
    	return 0;
    }
  • 相关阅读:
    自定义ASP.NET MVC Html辅助方法
    逻辑回归代价函数的详细推导
    cv::Mat与IplImage 的相互转换
    [转]GDAL1.9.0版本编译后,打不开含中文路径文件的解决办法
    恢复matlab文件关联方法
    error LNK2001: unresolved external symbol "*******__cdecl****"
    如何Latex中把下标放置到正下方
    Office2010每次启动都要配置的解决办法
    TC中列出所有文件的快捷键:ctrl+b
    [转]STL中vector转数组(实际是数组的指针)
  • 原文地址:https://www.cnblogs.com/coolbear/p/6214258.html
Copyright © 2011-2022 走看看