Changeset 589
- Timestamp:
- Aug 20, 2011, 11:47:49 AM (13 years ago)
- Location:
- Daodan/MSVC
- Files:
-
- 10 added
- 17 edited
Legend:
- Unmodified
- Added
- Removed
-
Daodan/MSVC/Daodan.c
r586 r589 9 9 #include "Daodan_BSL.h" 10 10 #include "Daodan_Console.h" 11 11 #include "Daodan_Update.h" 12 12 #include "Flatline_BSL.h" 13 13 #include "Flatline_Hooks.h" … … 61 61 bool opt_topmost = false; 62 62 63 typedef int (__cdecl *CHINESEPROC)(DWORD WINAPI); 64 bool patch_chinese = false; 63 65 bool DDrPatch_Init() 64 66 { … … 236 238 } 237 239 240 if(patch_chinese) 241 { 242 HMODULE dll = LoadLibrary("xfhsm_oni.dll"); 243 if( dll ) 244 { 245 void* proc = GetProcAddress( dll, "InstallHook" ); 246 if(proc) 247 { 248 ((CHINESEPROC)proc)(GetCurrentThreadId()); 249 } 250 } 251 } 252 238 253 //Fix crappy ai2_shownames 239 254 if(1) … … 384 399 break; 385 400 case s_language: 386 if (!_stricmp(name, "savepoint")) 401 if (!_stricmp(name, "chinese")) 402 patch_chinese = true; 403 else if (!_stricmp(name, "savepoint")) 387 404 { 388 405 char* str = _strdup(value); … … 574 591 opt_sound = true; 575 592 593 DDrStartupMessage("checking for updates..."); 594 595 DDr_CheckForUpdates(argc, argv); 596 576 597 DDrConfig(); 577 598 DDrStartupMessage("parsing command line..."); -
Daodan/MSVC/Daodan_Console.h
r567 r589 30 30 extern TStColorFormattingCharacter* TStColorFormattingCharacters; 31 31 extern TStColorFormattingCharacter DDrDSayColors[]; 32 33 #ifdef __cplusplus 34 extern "C" 35 #endif 32 36 void DDrConsole_Print(const char* text); 37 38 #ifdef __cplusplus 39 extern "C" 40 #endif 33 41 void DDrConsole_PrintColored(const char* text, int priority, RGBA color, RGBA shade); 42 43 #ifdef __cplusplus 44 extern "C" 45 #endif 34 46 void DDrConsole_PrintF(const char* fmt, ...); 35 47 -
Daodan/MSVC/Daodan_Utility.c
r586 r589 8 8 #include "BFW_Utility.h" 9 9 #include "Oni.h" 10 #include "Flatline_Public.h" 11 #include "Mariusnet_Public.h" 10 12 11 13 const double fps = 60.0; … … 119 121 //this probably could be reorganized 120 122 if(weapon_msg) { //SSrMsgblah returns NULL if it can't find the message key 121 memcpy(weapon_name, weapon_msg, (strstr(weapon_msg," received.") - weapon_msg)); //strips uneeded characters123 memcpy(weapon_name, weapon_msg, (strstr(weapon_msg,"] ") - weapon_msg + 1)); //strips uneeded characters 122 124 sprintf(output_ptr, "Press [c.Q] to pick up %s", weapon_name); 123 125 } … … 184 186 185 187 } 186 FLrRun_Scores(); 188 if(server_started || client_connected) 189 { 190 FLrRun_Scores(); 191 } 192 else if( MsNet_Running ) 193 { 194 MSNet_DrawGames(); 195 } 196 197 187 198 DDrPasteHack(); 188 199 -
Daodan/MSVC/Daodan_Utility.h
r572 r589 6 6 #include "Daodan.h" 7 7 8 #ifdef __cplusplus 9 extern "C" 10 #endif 8 11 void __cdecl DDrStartupMessage(const char* fmt, ...); 9 12 int64_t ONICALL DDrMachineTime_High(); -
Daodan/MSVC/Flatline.c
r588 r589 87 87 uint16_t x; 88 88 uint16_t y; 89 90 89 } IMtPoint2D; 91 90 static flatline_packet cache_input = {0}; -
Daodan/MSVC/Flatline.h
r587 r589 13 13 14 14 #define thread __thread 15 16 #include "Flatline_Public.h" 15 17 16 18 #ifdef WIN32 … … 292 294 int UDPServer_SendToAll(void* packet, int size); 293 295 294 extern bool client_connected;295 extern bool server_started;296 extern char player_name[];297 296 298 297 void FLrRun_Scores(); -
Daodan/MSVC/Flatline_BSL.c
r588 r589 8 8 #include "Flatline_Server.h" 9 9 #include "Mariusnet_Public.h" 10 #include "Flatline_Client.h" 10 11 bool server_started = 0; 11 12 bool client_connected = 0; … … 77 78 { 78 79 //TODO: Move this into the client initialization. Doing it like this is silly. 79 if( NetPlatform_Initalize()) { 80 static flatline_packet packet; 81 memset(&client_address, 0, sizeof(sockaddr_in)); 82 sock = NetUDPSocket_Create(27777, &client_address); 83 address.sin_family = AF_INET; address.sin_port = htons(27777); address.sin_addr.S_un.S_addr = inet_addr(args[0].value_str32 ); 84 //address.sin_family = AF_INET; address.sin_port = htons(27777); address.sin_addr.S_un.S_addr = inet_addr("192.168.0.1"); 85 86 packet.id = CONNECT_SEND; 87 memcpy(((connect_send*)(packet.data))->country , player_country, 2); 88 memcpy(((connect_send*)(packet.data))->name, player_name, 256); 89 DDrConsole_PrintF("%s", ((connect_send*)(packet.data))->name); 90 CreateThread(NULL, 0, StartClient, &packet, 0, 0); 91 92 } 93 80 FLcConnect(inet_addr(args[0].value_str32), htons(27777)); 94 81 return 0; 95 82 } … … 237 224 } 238 225 226 extern uint16_t ONICALL mnet_joingame(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], int* dontuse1, int* dontuse2, sl_arg* ret); 227 239 228 void SLrFlatline_Initialize() 240 229 { … … 249 238 SLrScript_Command_Register_ReturnType("name","changes your name", "name:string", sl_void, change_name); 250 239 SLrScript_Command_Register_Void("status","shows the connection status", "", status); 251 SLrGlobalVariable_Register_String("country", "Your Multiplayer country", player_name);240 //SLrGlobalVariable_Register_String("country", "Your Multiplayer country", player_name); 252 241 SLrScript_Command_Register_ReturnType("addbot","adds a fake client", "", sl_void, addfake); 253 242 SLrScript_Command_Register_Void("kick", "Kicks a client from the server", "clientnum:int", kick); … … 256 245 257 246 SLrScript_Command_Register_Void("login", "logs into mariusnet", "username:string password:string", mnet_login); 258 } 247 SLrScript_Command_Register_Void("join", "joins a mariusnet game", "index:int", mnet_joingame); 248 } -
Daodan/MSVC/Flatline_Client.c
r586 r589 1 1 #include "Flatline.h" 2 2 #include "Flatline_Client.h" 3 #include "Mariusnet_Public.h" 4 #include "PortForwardWrapper.h" 3 5 4 6 int client_slot = 0; … … 32 34 } 33 35 36 void FLcConnect( int ip, short port ) 37 { 38 if( NetPlatform_Initalize()) { 39 static flatline_packet packet; 40 memset(&client_address, 0, sizeof(sockaddr_in)); 41 sock = NetUDPSocket_Create(ntohs(port), &client_address); 42 address.sin_family = AF_INET; address.sin_port = port; address.sin_addr.S_un.S_addr = ip; 43 //address.sin_family = AF_INET; address.sin_port = htons(27777); address.sin_addr.S_un.S_addr = inet_addr("192.168.0.1"); 44 45 packet.id = CONNECT_SEND; 46 memcpy(((connect_send*)(packet.data))->country , "XD", 2); 47 memcpy(((connect_send*)(packet.data))->name, player_name, 256); 48 DDrConsole_PrintF("%s", ((connect_send*)(packet.data))->name); 49 CreateThread(NULL, 0, StartClient, &packet, 0, 0); 50 51 } 52 } 34 53 35 54 bool FLrClient_Run(flatline_packet* packet) … … 40 59 int j; 41 60 int sent_bytes; 61 62 PortMappingContainer_C PMC = 63 { 64 "", 65 "27777", 66 "27777", 67 "UDP", 68 "", 69 "", 70 "Flatline!" 71 }; 72 73 uPnP_Remove( &PMC ); 42 74 client_connected = 0; 43 75 44 76 45 77 //starts the connection 46 78 DDrConsole_PrintF("Connecting to server %s on socket %i", inet_ntoa(address.sin_addr), client_sock); … … 82 114 //DDrPatch_NOOP(0x4EC248,(0x5A-0x48)); 83 115 //DDrPatch_NOOP(0x4EC861, 6); 116 117 //Close Mariusnet collection because it hurts latency 118 MSNet_RoomExit(); 84 119 break; 85 120 } -
Daodan/MSVC/Flatline_Client.h
r584 r589 6 6 bool FLrClient_Run(flatline_packet* packet); 7 7 void FLrClient_GetPackets(); 8 8 void FLcConnect( int ip, short port ); 9 9 extern int client_slot; 10 10 -
Daodan/MSVC/Flatline_Net.h
r588 r589 6 6 7 7 8 8 typedef struct hostent hostent ; 9 typedef struct in_addr in_addr; 9 10 10 11 typedef struct { -
Daodan/MSVC/Flatline_Server.c
r588 r589 3 3 #include <Windows.h> 4 4 #include "PortForwardWrapper.h" 5 #include "Mariusnet_Public.h" 5 6 //I hereby apologize for the uglyness of the below code. 6 7 //It was never intended to be "final" code, much less shared with anyone … … 190 191 sprintf(Char->OSD.Class, "%s", TMrInstance_GetInstanceName(PlayerList[j]->Chr->ONCC)); 191 192 192 if(AC )193 if(AC && AC->PhyContext) 193 194 { 194 195 Char->Header.Position = AC->PhyContext->Position; … … 381 382 bool FLrServer_Run() 382 383 { 384 m_announcegame Game; 383 385 HRESULT ret; 384 386 PortMappingContainer_C PMC = … … 413 415 414 416 DDrConsole_PrintF("Server started at %s...", inet_ntoa (*(struct in_addr *)*host_entry->h_addr_list)); 417 418 Game.port = htons(27777); 419 sprintf_s(Game.g.Buffer, 128, "%s's Game", MariusLogin); 420 421 MSNet_CreateGame( &Game ); 422 415 423 return NetUDPServer_Listen(27777, FLrServer_PacketCallback); 416 424 } -
Daodan/MSVC/Mariusnet.c
r588 r589 4 4 #include "Flatline_Net.h" 5 5 #include "Flatline_Win32.h" 6 #include "Flatline_Client.h" 6 7 #include "Mariusnet_Defs.h" 7 8 #include "Daodan_Console.h" 9 #include "Oni_Symbols.h" 8 10 #include <stdio.h> 9 11 #include <inaddr.h> 10 12 #pragma comment(lib, "wininet.lib") 11 13 12 13 typedef struct hostent hostent ;14 typedef struct in_addr in_addr;15 14 16 15 char MariusLogin[32]={0}; … … 27 26 sockaddr_in RoomAddr = {0}; 28 27 int PlayerID = 0; 28 29 m_gameinfo StaticGameList[64] = {0}; 30 enum 31 { 32 glAddGame, 33 glRemoveGame, 34 glUpdateGame, 35 }; 36 29 37 /* 30 38 … … 41 49 extern RGBA green; 42 50 extern RGBA grey; 51 52 typedef struct 53 { 54 uint16_t x; 55 uint16_t y; 56 57 } IMtPoint2D; 58 void* DrawInstance = 0; 59 void MSNet_DrawGames() 60 { 61 62 if(!DrawInstance){ 63 void* TSFFTahoma; 64 TMrInstance_GetDataPtr( 'TSFF', "Tahoma", &TSFFTahoma); 65 TSrContext_New( TSFFTahoma, 7, 1, 1, 0, &DrawInstance); 66 } 67 if(DrawInstance){ 68 const int white = 0x00FFFFFF; 69 const int green = 0x0000FF00; 70 const int red = 0x00FF0000; 71 const int blue = 0x000000FF; 72 int i; 73 char DrawString[255]; 74 const int LineHeight = 15; 75 IMtPoint2D StartLocation = {500, 20}; 76 IMtPoint2D DrawLocation = StartLocation; 77 TSrContext_SetShade(DrawInstance, white); 78 TSrContext_DrawText(DrawInstance, "Mariusnet Games:", 255, 0, &DrawLocation); 79 TSrContext_SetShade(DrawInstance, white); 80 DrawLocation.y += LineHeight; 81 82 for(i = 0; i < 64; i++) 83 { 84 85 if(StaticGameList[i].gameID) 86 { 87 DrawLocation.x = StartLocation.x - 10; 88 sprintf(DrawString, "%i.", i ); 89 TSrContext_DrawText(DrawInstance, DrawString, 255, 0, &DrawLocation); 90 DrawLocation.x += 20; 91 TSrContext_DrawText(DrawInstance, StaticGameList[i].g.Buffer, 255, 0, &DrawLocation); 92 DrawLocation.y += LineHeight; 93 } 94 } 95 96 } 97 } 98 99 43 100 int MariusNet_OutgoingPacketWrapper(int socket, const sockaddr* address, marius_packet* data, short datalen) 44 101 { … … 312 369 } 313 370 371 void MSNet_RoomExit() 372 { 373 if(MariusNet_LoggedIn) 374 { 375 if( Room_Socket != -1) closesocket(Room_Socket); 376 memset(StaticGameList, 0, sizeof(m_gameinfo) * 64); 377 MsNet_Running = 0; 378 MariusNet_LoggedIn = 0; 379 380 } 381 return; 382 } 383 384 314 385 int MSNet_Cleanup() 315 386 { … … 319 390 Room_Socket = -1; 320 391 memset(RoomList, 0, sizeof(m_room) * 64 ); 392 memset(StaticGameList, 0, sizeof(m_gameinfo) * 64); 321 393 DDrConsole_PrintF("The metaserver connection will now close." ); 322 394 MsNet_Running = 0; … … 325 397 } 326 398 399 400 327 401 void MSNet_HandleGameList( m_gameinfo* PacketGames, int PacketSize ) 328 402 { 329 403 m_gameinfo TempGameList[64] = {0}; 330 int i 404 int i, k; 331 405 for( i = 0; PacketSize > 0; i++ ) 332 406 { … … 340 414 341 415 } 416 for( i = 0; TempGameList[i].gameID; i++) 417 { 418 int DesiredID = 0; 419 switch(TempGameList[i].verb) 420 { 421 case(glRemoveGame): 422 //Remove game from list 423 DDrConsole_PrintF("%s has ended", TempGameList[i].g.Buffer); 424 for(k = 0; k < 64; k++) 425 { 426 if(TempGameList[i].gameID == StaticGameList[k].gameID) 427 { 428 memset(StaticGameList + k, 0, sizeof(m_gameinfo) ); 429 break; 430 } 431 432 } 433 break; 434 435 case(glAddGame): 436 //Add game to list 437 DDrConsole_PrintF("%s has started", TempGameList[i].g.Buffer); 438 case(glUpdateGame): 439 //Update list 440 441 //If updating, look for the game to update, otherwise look for empty 442 if(TempGameList[i].verb) DesiredID = TempGameList[i].gameID; 443 for(k = 0; k < 64; k++) 444 { 445 if(DesiredID == StaticGameList[k].gameID) 446 { 447 memcpy(StaticGameList + k, TempGameList + i, sizeof(m_gameinfo) ); 448 break; 449 } 450 451 } 452 break; 453 break; 454 } 455 //TempGame 456 } 342 457 //Do stuff with the games. 343 458 } 344 459 345 int MSNet_Room_Join( short Room, char* Key) 346 { 347 marius_packet mPacket = {0}; 348 349 char incomingData[1400] = {0}; 350 marius_packet* incomingPacket = (marius_packet*)incomingData; 351 int sent_bytes = 0; 352 353 sockaddr_in SockAddr; 354 RoomAddr.sin_port = RoomList[Room].Port; 355 RoomAddr.sin_family = AF_INET; 356 RoomAddr.sin_addr.S_un.S_addr = RoomList[Room].IPAddress; 357 358 Room_Socket = NetTCPSocket_Create(RoomList[Room].Port, &SockAddr); 359 360 DDrConsole_PrintF("Joining room %hi", Room); 361 362 if(Room_Socket == -1) 363 { 364 DDrConsole_PrintF("Failed to initialize room socket!"); 365 return MSNet_Cleanup(); 366 } 367 368 if(connect( Room_Socket, (sockaddr *)&RoomAddr, sizeof(sockaddr_in))) 369 { 370 NetCatchError(); 371 MariusNet_LoggedIn = 0; 372 return MSNet_Cleanup(); 373 } 374 375 Initialize_MPacket(&mPacket, pt_ChatRoomLogin); 376 memcpy(mPacket.chatroom_join.RoomKey, Key, 32); 377 strcpy(mPacket.chatroom_join.Name, MariusLogin); 378 sent_bytes = MariusNet_OutgoingPacketWrapper(Room_Socket, (sockaddr*)&RoomAddr, (char*)&mPacket, 379 sizeof(marius_header) + 33 + strlen(MariusLogin)); 380 381 382 383 Initialize_MPacket(&mPacket, pt_RoomPlayerInfo); 384 mPacket.player_info.clientVersion = htons(5000); 385 sprintf(mPacket.player_info.Name, MariusLogin); 386 sent_bytes = MariusNet_OutgoingPacketWrapper(Room_Socket, (sockaddr*)&RoomAddr, (char*)&mPacket, 387 sizeof(marius_header) + 40 + strlen(MariusLogin)); 388 389 if(MariusNet_IncomingPacketWrapper(Room_Socket, incomingData, 1400, 0) == SOCKET_ERROR) 390 { 391 return MSNet_Cleanup(); 392 } 393 394 if(ntohs(incomingPacket->header.PacketId)== pt_LoginInfo) 395 { 396 //DDrConsole_PrintF("Login failed: %s", incomingPacket->login_denied.DenialMessage ); 397 MSNet_LoginError_Output(ntohl(incomingPacket->login_denied.code)); 398 return MSNet_Cleanup(); 399 } 400 401 MariusNet_LoggedIn = 1; 460 int MSNet_CreateGame( m_announcegame* Game ) 461 { 462 marius_packet mPacket; 463 Initialize_MPacket(&mPacket, 104); 464 mPacket.newgame = *Game; 465 MariusNet_OutgoingPacketWrapper(Room_Socket, (sockaddr*)&RoomAddr, (char*)&mPacket, 466 sizeof(marius_header) + sizeof(m_announcegame)); 467 } 402 468 403 469 /* … … 411 477 sizeof(marius_header) + sizeof(m_announcegame) + 50); 412 478 */ 479 480 int MSNet_Room_Join( short Room, char* Key) 481 { 482 marius_packet mPacket = {0}; 483 484 char incomingData[1400] = {0}; 485 marius_packet* incomingPacket = (marius_packet*)incomingData; 486 int sent_bytes = 0; 487 488 sockaddr_in SockAddr; 489 RoomAddr.sin_port = RoomList[Room].Port; 490 RoomAddr.sin_family = AF_INET; 491 RoomAddr.sin_addr.S_un.S_addr = RoomList[Room].IPAddress; 492 493 Room_Socket = NetTCPSocket_Create(RoomList[Room].Port, &SockAddr); 494 495 DDrConsole_PrintF("Joining room %hi", Room); 496 497 if(Room_Socket == -1) 498 { 499 DDrConsole_PrintF("Failed to initialize room socket!"); 500 return MSNet_Cleanup(); 501 } 502 503 if(connect( Room_Socket, (sockaddr *)&RoomAddr, sizeof(sockaddr_in))) 504 { 505 NetCatchError(); 506 MariusNet_LoggedIn = 0; 507 return MSNet_Cleanup(); 508 } 509 510 Initialize_MPacket(&mPacket, pt_ChatRoomLogin); 511 memcpy(mPacket.chatroom_join.RoomKey, Key, 32); 512 strcpy(mPacket.chatroom_join.Name, MariusLogin); 513 sent_bytes = MariusNet_OutgoingPacketWrapper(Room_Socket, (sockaddr*)&RoomAddr, (char*)&mPacket, 514 sizeof(marius_header) + 33 + strlen(MariusLogin)); 515 516 517 518 Initialize_MPacket(&mPacket, pt_RoomPlayerInfo); 519 mPacket.player_info.clientVersion = htons(5000); 520 sprintf(mPacket.player_info.Name, MariusLogin); 521 sent_bytes = MariusNet_OutgoingPacketWrapper(Room_Socket, (sockaddr*)&RoomAddr, (char*)&mPacket, 522 sizeof(marius_header) + 40 + strlen(MariusLogin)); 523 524 if(MariusNet_IncomingPacketWrapper(Room_Socket, incomingData, 1400, 0) == SOCKET_ERROR) 525 { 526 return MSNet_Cleanup(); 527 } 528 529 if(ntohs(incomingPacket->header.PacketId)== pt_LoginInfo) 530 { 531 //DDrConsole_PrintF("Login failed: %s", incomingPacket->login_denied.DenialMessage ); 532 MSNet_LoginError_Output(ntohl(incomingPacket->login_denied.code)); 533 return MSNet_Cleanup(); 534 } 535 536 MariusNet_LoggedIn = 1; 537 538 413 539 while(1) 414 540 { … … 581 707 memcpy(RoomToken, incomingPacket->login_success.Token, 32); 582 708 DDrConsole_PrintF("Logged into Mariusnet!"); 709 strcpy_s( player_name, 32, MariusLogin ); 583 710 } 584 711 else if(ntohs(incomingPacket->header.PacketId)== pt_LoginInfo) … … 634 761 closesocket(Marius_Socket); 635 762 636 MSNet_Room_Join( 1, RoomToken ); 637 638 763 MSNet_Room_Join( 0, RoomToken ); 764 765 766 } 767 return 0; 768 } 769 770 uint16_t ONICALL mnet_joingame(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], int* dontuse1, int* dontuse2, sl_arg* ret) 771 { 772 int index = args[0].value_int32; 773 if(index >= 0) 774 { 775 FLcConnect(StaticGameList[index].ipAddress, StaticGameList[index].port); 639 776 } 640 777 return 0; -
Daodan/MSVC/Mariusnet_Defs.h
r588 r589 1 1 //to client 2 #include "Mariusnet_Public.h" 2 3 enum 3 4 { … … 176 177 typedef struct 177 178 { 178 short unknown16;179 short type;180 int options;181 int timelimit;182 int checksum;183 short difficulty;184 short maxplayers;185 short teamrandomseed;186 short maxteams; //-1187 int planningtime;188 int unusedint;189 int unusedint_2;190 short unknown16_2;191 short pluginflag;192 char pluginlist[512];193 int clientversion;//0xc136e436194 int unknownint;195 short status;196 short numplayers;197 char action;198 char pad[3];199 char Buffer[128];200 } m_gamedescription;201 202 typedef struct203 {204 short port;205 short zero;206 m_gamedescription g;207 } m_announcegame;208 209 typedef struct210 {211 179 unsigned int gameID; 212 180 unsigned int ipAddress; -
Daodan/MSVC/Mariusnet_Public.h
r588 r589 3 3 bool MSNet_Login(char* username, char* password); 4 4 void MSNet_SendChat( char* msg ); 5 void MSNet_RoomExit(); 6 void MSNet_DrawGames(); 7 extern char MariusLogin[32]; 8 9 typedef struct 10 { 11 short unknown16; 12 short type; 13 int options; 14 int timelimit; 15 int checksum; 16 short difficulty; 17 short maxplayers; 18 short teamrandomseed; 19 short maxteams; //-1 20 int planningtime; 21 int unusedint; 22 int unusedint_2; 23 short unknown16_2; 24 short pluginflag; 25 char pluginlist[512]; 26 int clientversion;//0xc136e436 27 int unknownint; 28 short status; 29 short numplayers; 30 char action; 31 char pad[3]; 32 char Buffer[128]; 33 } m_gamedescription; 34 35 typedef struct 36 { 37 short port; 38 short zero; 39 m_gamedescription g; 40 } m_announcegame; 41 -
Daodan/MSVC/PortForwardWrapper.cpp
r588 r589 1 #include <exception> 1 2 #include <atlbase.h> 2 3 #include "PortForwardWrapper.h" … … 5 6 #include <stdlib.h> 6 7 #include "Daodan_Console.h" 8 #include "Daodan_Utility.h" 9 10 11 extern "C" int uPnP_Remove( PortMappingContainer_C* ptr ) 12 { 13 try 14 { 15 bool bContinue = true; 16 IUPnPNAT* piNAT = NULL; 17 IStaticPortMappingCollection* piPortMappingCollection = NULL; 18 19 return 0; 20 CoInitialize(NULL); 21 HRESULT CoResult; 22 23 if ( !bContinue || !SUCCEEDED( CoResult = CoCreateInstance( 24 __uuidof(UPnPNAT), 25 NULL, 26 CLSCTX_ALL, 27 __uuidof(IUPnPNAT), 28 (void **)&piNAT) 29 ) ) 30 bContinue = FALSE; 31 32 // Get the collection of forwarded ports 33 34 if ( !bContinue || !SUCCEEDED( CoResult = piNAT->get_StaticPortMappingCollection(&piPortMappingCollection)) || (piPortMappingCollection==NULL ) ) 35 bContinue = FALSE; 36 37 38 39 // add the new mapping 40 41 IStaticPortMapping* piStaticPortMapping = NULL; 42 USES_CONVERSION; // for conversion from CString's 43 44 //VARIANT_BOOL vb = ( ( newMapping.Enabled == _T("Yes") ) ? VARIANT_TRUE : VARIANT_FALSE ); 45 BSTR Protocol = SysAllocString( CT2W(ptr->Protocol) ); 46 BSTR InternalClient = SysAllocString( CT2W(ptr->InternalClient) ); 47 BSTR Description = SysAllocString( CT2W(ptr->Description) ); 48 49 //Remove the old binding, just in case. Probably not the best option, but it shall do for now. 50 if ( !bContinue ) piPortMappingCollection->Remove( _ttol( ptr->ExternalPort), Protocol ); 51 52 // clean up and de-initialize COM 53 54 if ( piStaticPortMapping != NULL ) 55 { 56 piStaticPortMapping->Release(); 57 piStaticPortMapping = NULL; 58 } 59 60 61 if ( piPortMappingCollection != NULL ) 62 { 63 piPortMappingCollection->Release(); 64 piPortMappingCollection = NULL; 65 } 66 67 if ( piNAT != NULL ) 68 { 69 piNAT->Release(); 70 piNAT = NULL; 71 } 72 73 74 CoUninitialize(); 75 if(bContinue) return 0; 76 else return CoResult; 77 } 78 catch(std::exception& ex) 79 { 80 DDrConsole_Print(ex.what()); 81 return 0; 82 } 83 } 84 85 7 86 extern "C" int uPnP_Forward( PortMappingContainer_C* ptr ) 8 87 { 9 bool bContinue = true;10 IUPnPNAT* piNAT = NULL;11 IStaticPortMappingCollection* piPortMappingCollection = NULL;12 CoInitialize(NULL);13 HRESULT CoResult;88 try 89 { 90 bool bContinue = true; 91 IUPnPNAT* piNAT = NULL; 92 IStaticPortMappingCollection* piPortMappingCollection = NULL; 14 93 15 if ( !bContinue || !SUCCEEDED( CoResult = CoCreateInstance( 16 __uuidof(UPnPNAT), 17 NULL, 18 CLSCTX_ALL, 19 __uuidof(IUPnPNAT), 20 (void **)&piNAT) 21 ) ) 22 bContinue = FALSE; 94 return 0; 95 CoInitialize(NULL); 96 HRESULT CoResult; 23 97 24 // Get the collection of forwarded ports 98 if ( !bContinue || !SUCCEEDED( CoResult = CoCreateInstance( 99 __uuidof(UPnPNAT), 100 NULL, 101 CLSCTX_ALL, 102 __uuidof(IUPnPNAT), 103 (void **)&piNAT) 104 ) ) 105 bContinue = FALSE; 25 106 26 if ( !bContinue || !SUCCEEDED( CoResult = piNAT->get_StaticPortMappingCollection(&piPortMappingCollection)) || (piPortMappingCollection==NULL ) ) 27 bContinue = FALSE; 107 // Get the collection of forwarded ports 28 108 29 30 31 // add the new mapping 109 if ( !bContinue || !SUCCEEDED( CoResult = piNAT->get_StaticPortMappingCollection(&piPortMappingCollection)) || (piPortMappingCollection==NULL ) ) 110 bContinue = FALSE; 32 111 33 IStaticPortMapping* piStaticPortMapping = NULL;34 USES_CONVERSION; // for conversion from CString's35 112 36 //VARIANT_BOOL vb = ( ( newMapping.Enabled == _T("Yes") ) ? VARIANT_TRUE : VARIANT_FALSE ); 37 BSTR Protocol = SysAllocString( CT2W(ptr->Protocol) ); 38 BSTR InternalClient = SysAllocString( CT2W(ptr->InternalClient) ); 39 BSTR Description = SysAllocString( CT2W(ptr->Description) ); 40 41 //Remove the old binding, just in case. Probably not the best option, but it shall do for now. 42 if ( !bContinue ) piPortMappingCollection->Remove( _ttol( ptr->ExternalPort), Protocol ); 43 44 if ( !bContinue || 45 !SUCCEEDED( CoResult = 46 piPortMappingCollection->Add( 113 114 // add the new mapping 115 116 IStaticPortMapping* piStaticPortMapping = NULL; 117 USES_CONVERSION; // for conversion from CString's 118 119 //VARIANT_BOOL vb = ( ( newMapping.Enabled == _T("Yes") ) ? VARIANT_TRUE : VARIANT_FALSE ); 120 BSTR Protocol = SysAllocString( CT2W(ptr->Protocol) ); 121 BSTR InternalClient = SysAllocString( CT2W(ptr->InternalClient) ); 122 BSTR Description = SysAllocString( CT2W(ptr->Description) ); 123 124 //Remove the old binding, just in case. Probably not the best option, but it shall do for now. 125 if ( !bContinue ) piPortMappingCollection->Remove( _ttol( ptr->ExternalPort), Protocol ); 126 127 if ( !bContinue || 128 !SUCCEEDED( CoResult = 129 piPortMappingCollection->Add( 47 130 _ttol( ptr->ExternalPort), 48 131 Protocol, … … 51 134 -1, 52 135 Description, 53 &piStaticPortMapping ) ) || (piStaticPortMapping==NULL) ) 54 55 bContinue = FALSE; 56 57 SysFreeString(Protocol); 58 SysFreeString(InternalClient); 59 SysFreeString(Description); 60 61 62 // clean up and de-initialize COM 63 64 if ( piStaticPortMapping != NULL ) 136 &piStaticPortMapping ) ) || (piStaticPortMapping==NULL) ) 137 138 bContinue = FALSE; 139 140 SysFreeString(Protocol); 141 SysFreeString(InternalClient); 142 SysFreeString(Description); 143 144 145 // clean up and de-initialize COM 146 147 if ( piStaticPortMapping != NULL ) 148 { 149 piStaticPortMapping->Release(); 150 piStaticPortMapping = NULL; 151 } 152 153 154 if ( piPortMappingCollection != NULL ) 155 { 156 piPortMappingCollection->Release(); 157 piPortMappingCollection = NULL; 158 } 159 160 if ( piNAT != NULL ) 161 { 162 piNAT->Release(); 163 piNAT = NULL; 164 } 165 166 167 CoUninitialize(); 168 if(bContinue) return 0; 169 else return CoResult; 170 } 171 catch(std::exception& ex) 65 172 { 66 piStaticPortMapping->Release();67 piStaticPortMapping = NULL;173 DDrConsole_Print(ex.what()); 174 return 0; 68 175 } 69 70 71 if ( piPortMappingCollection != NULL )72 {73 piPortMappingCollection->Release();74 piPortMappingCollection = NULL;75 }76 77 if ( piNAT != NULL )78 {79 piNAT->Release();80 piNAT = NULL;81 }82 83 84 CoUninitialize();85 if(bContinue) return 0;86 else return CoResult;87 176 } -
Daodan/MSVC/PortForwardWrapper.h
r588 r589 14 14 #endif 15 15 int uPnP_Forward( PortMappingContainer_C* ptr ); 16 #ifdef __cplusplus 17 extern "C" 18 #endif 19 int uPnP_Remove( PortMappingContainer_C* ptr );
Note:
See TracChangeset
for help on using the changeset viewer.