本帖最后由 匿名 于 2011-8-10 11:29 编辑
- #include <windows.h>
- #include <string.h>
- #include "RakPeerInterface.h"
- #include "RakNetworkFactory.h"
- #include "MessageIdentifiers.h"
- #include <fstream>
- using namespace std;
- SocketDescriptor g_sdp(5417,0);
- SocketDescriptor g_sdp2(5418,0);
- RakPeerInterface *rakPeer1;
- RakPeerInterface *rakPeer2;
- struct SATalkingMessage
- {
- unsigned char messageID;
- //char* str;
- char str[512];
- };
- ///////////////////////////////////////////////////////////////////////////////////////////
- int InitialServer()
- {
- rakPeer1 = RakNetworkFactory::GetRakPeerInterface();
- rakPeer1->Startup(8,30,&g_sdp,1);
- rakPeer1->SetMaximumIncomingConnections(8);
- return 1;
- }
- void ServerGetPacket(char iner[])
- {
- Packet *packet = rakPeer1->Receive();
- if(packet)
- {
- if(packet->data[0] == ID_USER_PACKET_ENUM)
- {
- sprintf(iner,"%s::%s",packet->systemAddress.ToString(),packet->data+1);
- }
- else if(packet->data[0] == ID_NEW_INCOMING_CONNECTION)
- {
- //Welcome~~~~~~~~
- sprintf(iner,"%s::RN_newing",packet->systemAddress.ToString());
- }
- else if(packet->data[0] == ID_CONNECTION_LOST || packet->data[0] == ID_DISCONNECTION_NOTIFICATION)
- {
- sprintf(iner,"%s::RN_disconnect",packet->systemAddress.ToString());
- }
- rakPeer1->DeallocatePacket(packet);
- }
- return;
- }
- int ServerSend(char* messagetest,int type)
- {
- SATalkingMessage message;
- message.messageID = ID_USER_PACKET_ENUM;
- strcpy(message.str,messagetest);
- rakPeer1->Send((char*)&message,sizeof(message),HIGH_PRIORITY,(PacketReliability)type,0,UNASSIGNED_SYSTEM_ADDRESS,true);
- return 1;
- }
- void CloseServer()
- {
- RakNetworkFactory::DestroyRakPeerInterface(rakPeer1);
- }
- int ServerConnectionCount(void)
- {
- int i,count;
- for (i=0,count=0; i < 8;i++)
- {
- if (rakPeer1->GetSystemAddressFromIndex(i) != UNASSIGNED_SYSTEM_ADDRESS)
- {
- count++;
- }
- }
- return count;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////
- int InitialClient()
- {
- rakPeer2 = RakNetworkFactory::GetRakPeerInterface();
- rakPeer2->Startup(8,30,&g_sdp2,1);
- return 1;
- }
- int ClientConnect(char* host)
- {
- rakPeer2->Connect(host,5417,0,0,0);
- while(1)
- {
- Packet *packet = rakPeer2->Receive();
- if(packet)
- {
- if(packet->data[0] == ID_CONNECTION_REQUEST_ACCEPTED)
- {
- //We connect target yada!
- rakPeer2->DeallocatePacket(packet);
- return 1;
- }
- else if(packet->data[0] == ID_CONNECTION_ATTEMPT_FAILED)
- {
- //We failed to connect...
- rakPeer2->DeallocatePacket(packet);
- return 0;
- }
- else if(packet->data[0] == ID_ALREADY_CONNECTED)
- {
- // We already connected!!!
- rakPeer2->DeallocatePacket(packet);
- return 2;
- }
- else if(packet->data[0] == ID_NO_FREE_INCOMING_CONNECTIONS)
- {
- // the server is full!
- rakPeer2->DeallocatePacket(packet);
- return 4;
- }
- rakPeer2->DeallocatePacket(packet);
- }
- }
- return 0;
- }
- int ClientSend(char* messagetest,int type)
- {
- SATalkingMessage message;
- message.messageID = ID_USER_PACKET_ENUM;
- strcpy(message.str,messagetest);
- rakPeer2->Send((char*)&message,sizeof(message),HIGH_PRIORITY,(PacketReliability)type,0,UNASSIGNED_SYSTEM_ADDRESS,true);
- return 1;
- }
- void SearchforServer()
- {
- rakPeer2->Ping("255.255.255.255",5417,true);
- }
- void ClientGetPacket(char iner[])
- {
- Packet *packet = rakPeer2->Receive();
- if(packet)
- {
- if(packet->data[0] == ID_USER_PACKET_ENUM)
- {
- sprintf(iner,"%s::%s",packet->systemAddress.ToString(),packet->data+1);
- }
- else if(packet->data[0] == ID_CONNECTION_LOST || packet->data[0] == ID_DISCONNECTION_NOTIFICATION)
- {
- sprintf(iner,"%s::RN_disconnect",packet->systemAddress.ToString());
- }
- else if(packet->data[0] == ID_PONG)
- {
- sprintf(iner,"%s::RN_PONG",packet->systemAddress.ToString());
- }
- rakPeer2->DeallocatePacket(packet);
- }
- return;
- }
- int ClientDisConnect()
- {
- rakPeer2->CloseConnection(rakPeer2->GetSystemAddressFromIndex(0),true,0);
- return 1;
- }
- int CloseClient()
- {
- RakNetworkFactory::DestroyRakPeerInterface(rakPeer2);
- return 1;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////
- void DonotGCme()
- {
- }
-
- void ClientGetMyIP(char iner[])
- {
- sprintf(iner,"%s",rakPeer2->GetLocalIP(0));
- }
- void SeverGetMyIP(char iner[])
- {
- sprintf(iner,"%s",rakPeer1->GetLocalIP(0));
- }
- ///////////////////////////////////////////////////////////////////////////////////////////
- BOOL APIENTRY DllMain( HANDLE hModule,
- DWORD ul_reason_for_call,
- LPVOID lpReserved
- )
- {
- return TRUE;
- }
复制代码 于是偶果断把去年寒假的蛋痛物挖出来了。
RM的部分在
http://www.diyrpg.net/forum.php? ... st%3D1%26digest%3D1
后来更新过一次补上了自动搜索局域网主机神马的。
没把战斗同步和某些安全问题填完就去轰杀GRE和托福了,于是那些一直坑着 囧
那个……那个……明明那个时候思思就在那个帖子里的啊 囧 |