的笔记

随时随地编辑

RakNet笔记

I.SOURCE

http://www.ogre3d.org/tikiwiki/-RakNet
http://www.jenkinssoftware.com/

II.USE(VER3.6.11)

新版4.0里这些地方有细微差别,见官方文档和源码。
1.引入
#include "RakPeerInterface.h"
#include "MessageIdentifiers.h"
#include "RakNetworkFactory.h"

2.创建与销毁

RakPeerInterface *peer = RakNetworkFactory::GetRakPeerInterface();
//! As client
peer->Startup(1, 30 , &SocketDescriptor(), 1);
peer->Startup(MAX_CLIENTS, 30 , &SocketDescriptor(SERVER_PORT,0), 1);

//! As server
peer->Startup(MAX_CLIENTS, 30 , &SocketDescriptor(SERVER_PORT,0), 1);
peer->SetMaximumIncomingConnections(MAX_CLIENTS);


do something...

//! destroy
client->Shutdown(300);
RakNetworkFactory::DestroyRakPeerInterface(peer);

3.working
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;
        }

    }

}



II.CHEDAN

just do it
http://www.jenkinssoftware.com/raknet/manual/tutorialsample2.html



posted on 2011-07-20 17:11 的笔记 阅读(723) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理