diff --git a/CMakeLists.txt b/CMakeLists.txt index 0814bb8e0..dc0bc2ad2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,58 +1,58 @@ -cmake_minimum_required(VERSION 2.6) - -project(RakNet) - -if( NOT APPLE ) -# check 64 bit -if( CMAKE_SIZEOF_VOID_P MATCHES "4" ) - set( HAVE_64_BIT 0 ) -else( CMAKE_SIZEOF_VOID_P MATCHES "4") - set( HAVE_64_BIT 1 ) -endif( CMAKE_SIZEOF_VOID_P MATCHES "4") -endif( NOT APPLE ) - -IF (WIN32 AND NOT UNIX) - set (PROGRAMFILESX86 $ENV{PROGRAMFILES}) - string(REPLACE "\\" "/" PROGRAMFILESX86 ${PROGRAMFILESX86}) -ENDIF(WIN32 AND NOT UNIX) - -IF (WIN32 AND NOT UNIX) - set(RAKNET_LIBRARY_LIBS ws2_32.lib) -ELSE(WIN32 AND NOT UNIX) - set(RAKNET_LIBRARY_LIBS pthread) -ENDIF(WIN32 AND NOT UNIX) - - -# Options -option( RAKNET_ENABLE_SAMPLES "Generate RakNet sample projects if true." TRUE ) -option( RAKNET_ENABLE_DLL "Generate the DLL project if true." TRUE ) -option( RAKNET_ENABLE_STATIC "Generate the static library project if true." TRUE ) -option( RAKNET_GENERATE_INCLUDE_ONLY_DIR "Setup a include/RakNet/ directory in which all the headers are copied." FALSE ) - -set( RAKNETHEADERFILES ${RakNet_SOURCE_DIR}/Source ) #This name doesn't follow CMake conventions but for retro compatibility I'll let it there. - -if( RAKNET_GENERATE_INCLUDE_ONLY_DIR ) - set( RAKNET_INCLUDE_ONLY_DIR ${RakNet_SOURCE_DIR}/include ) # this will be visible by client code - set( RAKNET_NAMED_INCLUDE_ONLY_DIR ${RAKNET_INCLUDE_ONLY_DIR}/RakNet ) - message( STATUS "Setting up the ${RAKNET_NAMED_INCLUDE_ONLY_DIR} directory..." ) - # Now setup the include/RakNet/*.h files. - file( MAKE_DIRECTORY ${RAKNET_NAMED_INCLUDE_ONLY_DIR} ) - file( COPY ${RAKNETHEADERFILES}/ DESTINATION ${RAKNET_NAMED_INCLUDE_ONLY_DIR} FILES_MATCHING PATTERN "*.h" ) - message( STATUS "DONE: Setting up the ${RAKNET_NAMED_INCLUDE_ONLY_DIR} directory." ) -endif() - -set( RAKNET_INCLUDE_DIRS ${RAKNETHEADERFILES} ${RAKNET_INCLUDE_ONLY_DIR} PARENT_SCOPE ) # Visible from outside - -include(./CmakeIncludes/CmakeMacros.txt) -FIXLINKOPTIONS() -FIXCOMPILEOPTIONS() - - -add_subdirectory(Lib) - - -set(RAKNET_COMMON_LIBS RakNetLibStatic) - -if( RAKNET_GENERATE_SAMPLES ) - add_subdirectory(Samples) +cmake_minimum_required(VERSION 3.10) + +project(RakNet) + +if( NOT APPLE ) +# check 64 bit +if( CMAKE_SIZEOF_VOID_P MATCHES "4" ) + set( HAVE_64_BIT 0 ) +else() + set( HAVE_64_BIT 1 ) +endif() +endif() + +IF (WIN32 AND NOT UNIX) + set (PROGRAMFILESX86 $ENV{PROGRAMFILES}) + string(REPLACE "\\" "/" PROGRAMFILESX86 ${PROGRAMFILESX86}) +ENDIF(WIN32 AND NOT UNIX) + +IF (WIN32 AND NOT UNIX) + set(RAKNET_LIBRARY_LIBS ws2_32.lib) +ELSE(WIN32 AND NOT UNIX) + set(RAKNET_LIBRARY_LIBS pthread) +ENDIF(WIN32 AND NOT UNIX) + + +# Options +option( RAKNET_ENABLE_SAMPLES "Generate RakNet sample projects if true." TRUE ) +option( RAKNET_ENABLE_DLL "Generate the DLL project if true." TRUE ) +option( RAKNET_ENABLE_STATIC "Generate the static library project if true." TRUE ) +option( RAKNET_GENERATE_INCLUDE_ONLY_DIR "Setup a include/RakNet/ directory in which all the headers are copied." FALSE ) + +set( RAKNETHEADERFILES ${RakNet_SOURCE_DIR}/Source ) #This name doesn't follow CMake conventions but for retro compatibility I'll let it there. + +if( RAKNET_GENERATE_INCLUDE_ONLY_DIR ) + set( RAKNET_INCLUDE_ONLY_DIR ${RakNet_SOURCE_DIR}/include ) # this will be visible by client code + set( RAKNET_NAMED_INCLUDE_ONLY_DIR ${RAKNET_INCLUDE_ONLY_DIR}/RakNet ) + message( STATUS "Setting up the ${RAKNET_NAMED_INCLUDE_ONLY_DIR} directory..." ) + # Now setup the include/RakNet/*.h files. + file( MAKE_DIRECTORY ${RAKNET_NAMED_INCLUDE_ONLY_DIR} ) + file( COPY ${RAKNETHEADERFILES}/ DESTINATION ${RAKNET_NAMED_INCLUDE_ONLY_DIR} FILES_MATCHING PATTERN "*.h" ) + message( STATUS "DONE: Setting up the ${RAKNET_NAMED_INCLUDE_ONLY_DIR} directory." ) +endif() + +set( RAKNET_INCLUDE_DIRS ${RAKNETHEADERFILES} ${RAKNET_INCLUDE_ONLY_DIR} PARENT_SCOPE ) # Visible from outside + +include(./CmakeIncludes/CmakeMacros.txt) +FIXLINKOPTIONS() +FIXCOMPILEOPTIONS() + + +add_subdirectory(Lib) + + +set(RAKNET_COMMON_LIBS RakNetLibStatic) + +if( RAKNET_GENERATE_SAMPLES ) + add_subdirectory(Samples) endif() \ No newline at end of file diff --git a/Lib/CMakeLists.txt b/Lib/CMakeLists.txt index 06459de22..79bc47be7 100644 --- a/Lib/CMakeLists.txt +++ b/Lib/CMakeLists.txt @@ -1,16 +1,16 @@ -cmake_minimum_required(VERSION 2.6) - -set( RAKNET_INTERNAL_INCLUDE_DIRS - ${RakNet_SOURCE_DIR}/Source - ${RakNet_SOURCE_DIR}/DependentExtensions/openssl-1.0.0d/include - ${RakNet_SOURCE_DIR}/DependentExtensions -) - -if( RAKNET_ENABLE_STATIC ) - add_subdirectory(LibStatic) -endif() - - -if( RAKNET_ENABLE_DLL ) - add_subdirectory(DLL) -endif() +cmake_minimum_required(VERSION 3.10) + +set( RAKNET_INTERNAL_INCLUDE_DIRS + ${RakNet_SOURCE_DIR}/Source + ${RakNet_SOURCE_DIR}/DependentExtensions/openssl-1.0.0d/include + ${RakNet_SOURCE_DIR}/DependentExtensions +) + +if( RAKNET_ENABLE_STATIC ) + add_subdirectory(LibStatic) +endif() + + +if( RAKNET_ENABLE_DLL ) + add_subdirectory(DLL) +endif() diff --git a/Lib/DLL/CMakeLists.txt b/Lib/DLL/CMakeLists.txt index 771fdc6b8..a498cf1f7 100644 --- a/Lib/DLL/CMakeLists.txt +++ b/Lib/DLL/CMakeLists.txt @@ -1,21 +1,21 @@ -cmake_minimum_required(VERSION 2.6) -project(RakNetDLL) - -FILE(GLOB ALL_HEADER_SRCS ${RakNet_SOURCE_DIR}/Source/*.h) -FILE(GLOB ALL_CPP_SRCS ${RakNet_SOURCE_DIR}/Source/*.cpp) - - -include_directories( ${RAKNET_INTERNAL_INCLUDE_DIRS} ) - -add_library(RakNetDLL SHARED ${ALL_CPP_SRCS} ${ALL_HEADER_SRCS} readme.txt) - -IF(WIN32 AND NOT UNIX) - SET( CMAKE_CXX_FLAGS "/D WIN32 /D _RAKNET_DLL /D _CRT_NONSTDC_NO_DEPRECATE /D _CRT_SECURE_NO_DEPRECATE /GS- /GR- ") -ENDIF(WIN32 AND NOT UNIX) - -IF(WIN32 AND NOT UNIX) - target_link_libraries (RakNetDLL ${RAKNET_LIBRARY_LIBS}) -ELSE(WIN32 AND NOT UNIX) - target_link_libraries (RakNetDLL ${RAKNET_LIBRARY_LIBS}) - INSTALL(TARGETS RakNetDLL DESTINATION ${RakNet_SOURCE_DIR}/Lib/DLL) -ENDIF(WIN32 AND NOT UNIX) +cmake_minimum_required(VERSION 3.10) +project(RakNetDLL) + +FILE(GLOB ALL_HEADER_SRCS ${RakNet_SOURCE_DIR}/Source/*.h) +FILE(GLOB ALL_CPP_SRCS ${RakNet_SOURCE_DIR}/Source/*.cpp) + + +include_directories( ${RAKNET_INTERNAL_INCLUDE_DIRS} ) + +add_library(RakNetDLL SHARED ${ALL_CPP_SRCS} ${ALL_HEADER_SRCS} readme.txt) + +IF(WIN32 AND NOT UNIX) + SET( CMAKE_CXX_FLAGS "/D WIN32 /D _RAKNET_DLL /D _CRT_NONSTDC_NO_DEPRECATE /D _CRT_SECURE_NO_DEPRECATE /GS- /GR- ") +ENDIF(WIN32 AND NOT UNIX) + +IF(WIN32 AND NOT UNIX) + target_link_libraries (RakNetDLL ${RAKNET_LIBRARY_LIBS}) +ELSE(WIN32 AND NOT UNIX) + target_link_libraries (RakNetDLL ${RAKNET_LIBRARY_LIBS}) + INSTALL(TARGETS RakNetDLL DESTINATION ${RakNet_SOURCE_DIR}/Lib/DLL) +ENDIF(WIN32 AND NOT UNIX) diff --git a/Lib/LibStatic/CMakeLists.txt b/Lib/LibStatic/CMakeLists.txt index c116ee5f0..25f603d49 100644 --- a/Lib/LibStatic/CMakeLists.txt +++ b/Lib/LibStatic/CMakeLists.txt @@ -1,34 +1,34 @@ -cmake_minimum_required(VERSION 2.6) -project(RakNetLibStatic) - -FILE(GLOB ALL_HEADER_SRCS ${RakNet_SOURCE_DIR}/Source/*.h) -FILE(GLOB ALL_CPP_SRCS ${RakNet_SOURCE_DIR}/Source/*.cpp) - -include_directories( ${RAKNET_INTERNAL_INCLUDE_DIRS} ) - -add_library(RakNetLibStatic STATIC ${ALL_CPP_SRCS} ${ALL_HEADER_SRCS} readme.txt) - -IF(WIN32 AND NOT UNIX) - SET( CMAKE_CXX_FLAGS "/D WIN32 /D _RAKNET_LIB /D _CRT_NONSTDC_NO_DEPRECATE /D _CRT_SECURE_NO_DEPRECATE /GS- /GR- ") -ENDIF(WIN32 AND NOT UNIX) - -IF(WIN32 AND NOT UNIX) - target_link_libraries (RakNetLibStatic ${RAKNET_LIBRARY_LIBS}) - - IF(NOT ${CMAKE_GENERATOR} STREQUAL "MSYS Makefiles") - - IF( MSVC10 OR MSVC11 OR MSVC12 ) - set_target_properties(RakNetLibStatic PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB:\"LIBCD.lib LIBCMTD.lib MSVCRT.lib\"" ) - ELSE() - set_target_properties(RakNetLibStatic PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB:"LIBCD.lib LIBCMTD.lib MSVCRT.lib"" ) - ENDIF() - - ENDIF(NOT ${CMAKE_GENERATOR} STREQUAL "MSYS Makefiles") - -ELSE(WIN32 AND NOT UNIX) - target_link_libraries (RakNetLibStatic ${RAKNET_LIBRARY_LIBS}) - INSTALL(TARGETS RakNetLibStatic DESTINATION ${RakNet_SOURCE_DIR}/Lib/RakNetLibStatic) - INSTALL(FILES ${ALL_HEADER_SRCS} DESTINATION ${RakNet_SOURCE_DIR}/include/raknet) -ENDIF(WIN32 AND NOT UNIX) - - +cmake_minimum_required(VERSION 3.10) +project(RakNetLibStatic) + +FILE(GLOB ALL_HEADER_SRCS ${RakNet_SOURCE_DIR}/Source/*.h) +FILE(GLOB ALL_CPP_SRCS ${RakNet_SOURCE_DIR}/Source/*.cpp) + +include_directories( ${RAKNET_INTERNAL_INCLUDE_DIRS} ) + +add_library(RakNetLibStatic STATIC ${ALL_CPP_SRCS} ${ALL_HEADER_SRCS} readme.txt) + +IF(WIN32 AND NOT UNIX) + SET( CMAKE_CXX_FLAGS "/D WIN32 /D _RAKNET_LIB /D _CRT_NONSTDC_NO_DEPRECATE /D _CRT_SECURE_NO_DEPRECATE /GS- /GR- ") +ENDIF(WIN32 AND NOT UNIX) + +IF(WIN32 AND NOT UNIX) + target_link_libraries (RakNetLibStatic ${RAKNET_LIBRARY_LIBS}) + + IF(NOT ${CMAKE_GENERATOR} STREQUAL "MSYS Makefiles") + + IF( MSVC10 OR MSVC11 OR MSVC12 ) + set_target_properties(RakNetLibStatic PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB:\"LIBCD.lib LIBCMTD.lib MSVCRT.lib\"" ) + ELSE() + set_target_properties(RakNetLibStatic PROPERTIES STATIC_LIBRARY_FLAGS "/NODEFAULTLIB:"LIBCD.lib LIBCMTD.lib MSVCRT.lib"" ) + ENDIF() + + ENDIF(NOT ${CMAKE_GENERATOR} STREQUAL "MSYS Makefiles") + +ELSE(WIN32 AND NOT UNIX) + target_link_libraries (RakNetLibStatic ${RAKNET_LIBRARY_LIBS}) + INSTALL(TARGETS RakNetLibStatic DESTINATION ${RakNet_SOURCE_DIR}/Lib/RakNetLibStatic) + INSTALL(FILES ${ALL_HEADER_SRCS} DESTINATION ${RakNet_SOURCE_DIR}/include/raknet) +ENDIF(WIN32 AND NOT UNIX) + + diff --git a/Samples/AutoPatcherServer_MySQL/CMakeLists.txt b/Samples/AutoPatcherServer_MySQL/CMakeLists.txt index 8fead3514..94012e62b 100644 --- a/Samples/AutoPatcherServer_MySQL/CMakeLists.txt +++ b/Samples/AutoPatcherServer_MySQL/CMakeLists.txt @@ -1,24 +1,24 @@ -cmake_minimum_required(VERSION 2.6) -project("AutoPatcherServer_MySQL") -IF(WIN32 AND NOT UNIX) - FILE(GLOB AUTOSRC "${Autopatcher_SOURCE_DIR}/AutopatcherServer.cpp" "${Autopatcher_SOURCE_DIR}/MemoryCompressor.cpp" "${Autopatcher_SOURCE_DIR}/CreatePatch.cpp" "${Autopatcher_SOURCE_DIR}/AutopatcherServer.h") - FILE(GLOB BZSRC "${BZip2_SOURCE_DIR}/*.c" "${BZip2_SOURCE_DIR}/*.h") - LIST(REMOVE_ITEM BZSRC "${BZip2_SOURCE_DIR}/dlltest.c" "${BZip2_SOURCE_DIR}/mk251.c" "${BZip2_SOURCE_DIR}/bzip2recover.c") - SOURCE_GROUP(BZip FILES ${BZSRC}) - SOURCE_GROUP("Source Files" FILES ${AUTOSRC}) - SOURCE_GROUP(Main "AutopatcherServerTest_MySQL.cpp") - include_directories(${RAKNETHEADERFILES} ./ ${AutopatcherMySQLRepository_SOURCE_DIR} ${MySQLInterface_SOURCE_DIR} ${Autopatcher_SOURCE_DIR} ${BZip2_SOURCE_DIR}) - add_executable("AutoPatcherServer_MySQL" "AutopatcherServerTest_MySQL.cpp" readme.txt ${AUTOSRC} ${BZSRC}) - target_link_libraries("AutoPatcherServer_MySQL" ${RAKNET_COMMON_LIBS} AutoPatcherMySQLRepository) - VSUBFOLDER(AutoPatcherServer_MySQL "Samples/AutoPatcher/Server/MySQL") -ELSE(WIN32 AND NOT UNIX) - include_directories(${RAKNETHEADERFILES} ./ ${AutopatcherMySQLRepository_SOURCE_DIR} ${MySQLInterface_SOURCE_DIR} ${Autopatcher_SOURCE_DIR}) - add_executable("AutoPatcherServer_MySQL" "AutopatcherServerTest_MySQL.cpp" readme.txt) - target_link_libraries("AutoPatcherServer_MySQL" ${RAKNET_COMMON_LIBS} LibAutopatcher AutoPatcherMySQLRepository LibMySQLInterface) -ENDIF(WIN32 AND NOT UNIX) - - - - - - +cmake_minimum_required(VERSION 3.10) +project("AutoPatcherServer_MySQL") +IF(WIN32 AND NOT UNIX) + FILE(GLOB AUTOSRC "${Autopatcher_SOURCE_DIR}/AutopatcherServer.cpp" "${Autopatcher_SOURCE_DIR}/MemoryCompressor.cpp" "${Autopatcher_SOURCE_DIR}/CreatePatch.cpp" "${Autopatcher_SOURCE_DIR}/AutopatcherServer.h") + FILE(GLOB BZSRC "${BZip2_SOURCE_DIR}/*.c" "${BZip2_SOURCE_DIR}/*.h") + LIST(REMOVE_ITEM BZSRC "${BZip2_SOURCE_DIR}/dlltest.c" "${BZip2_SOURCE_DIR}/mk251.c" "${BZip2_SOURCE_DIR}/bzip2recover.c") + SOURCE_GROUP(BZip FILES ${BZSRC}) + SOURCE_GROUP("Source Files" FILES ${AUTOSRC}) + SOURCE_GROUP(Main "AutopatcherServerTest_MySQL.cpp") + include_directories(${RAKNETHEADERFILES} ./ ${AutopatcherMySQLRepository_SOURCE_DIR} ${MySQLInterface_SOURCE_DIR} ${Autopatcher_SOURCE_DIR} ${BZip2_SOURCE_DIR}) + add_executable("AutoPatcherServer_MySQL" "AutopatcherServerTest_MySQL.cpp" readme.txt ${AUTOSRC} ${BZSRC}) + target_link_libraries("AutoPatcherServer_MySQL" ${RAKNET_COMMON_LIBS} AutoPatcherMySQLRepository) + VSUBFOLDER(AutoPatcherServer_MySQL "Samples/AutoPatcher/Server/MySQL") +ELSE(WIN32 AND NOT UNIX) + include_directories(${RAKNETHEADERFILES} ./ ${AutopatcherMySQLRepository_SOURCE_DIR} ${MySQLInterface_SOURCE_DIR} ${Autopatcher_SOURCE_DIR}) + add_executable("AutoPatcherServer_MySQL" "AutopatcherServerTest_MySQL.cpp" readme.txt) + target_link_libraries("AutoPatcherServer_MySQL" ${RAKNET_COMMON_LIBS} LibAutopatcher AutoPatcherMySQLRepository LibMySQLInterface) +ENDIF(WIN32 AND NOT UNIX) + + + + + + diff --git a/Samples/AutopatcherClient/CMakeLists.txt b/Samples/AutopatcherClient/CMakeLists.txt index 5592f594a..29b47a07c 100644 --- a/Samples/AutopatcherClient/CMakeLists.txt +++ b/Samples/AutopatcherClient/CMakeLists.txt @@ -1,23 +1,23 @@ -cmake_minimum_required(VERSION 2.6) -project(AutopatcherClient) - -set(Autopatcher_SOURCE_DIR ${RakNet_SOURCE_DIR}/DependentExtensions/Autopatcher) -set(BZip2_SOURCE_DIR ${RakNet_SOURCE_DIR}/DependentExtensions/bzip2-1.0.6) - -include_directories(${RAKNETHEADERFILES} ./ ${Autopatcher_SOURCE_DIR} ${BZip2_SOURCE_DIR} ) -FILE(GLOB AUTOSRC "${Autopatcher_SOURCE_DIR}/*.cpp" "${Autopatcher_SOURCE_DIR}/*.h") -LIST(REMOVE_ITEM AUTOSRC "${Autopatcher_SOURCE_DIR}/AutopatcherServer.cpp" "${Autopatcher_SOURCE_DIR}/AutopatcherServer.h" ) -FILE(GLOB BZSRC "${BZip2_SOURCE_DIR}/*.c" "${BZip2_SOURCE_DIR}/*.h") -LIST(REMOVE_ITEM BZSRC "${BZip2_SOURCE_DIR}/dlltest.c" "${BZip2_SOURCE_DIR}/mk251.c" "${BZip2_SOURCE_DIR}/bzip2recover.c") -SOURCE_GROUP(BZip2 FILES ${BZSRC}) -SET(WRAPFILES "${Autopatcher_SOURCE_DIR}/MemoryCompressor.cpp" "${Autopatcher_SOURCE_DIR}/MemoryCompressor.h") -LIST(REMOVE_ITEM AUTOSRC ${WRAPFILES}) -SOURCE_GROUP(Client_Files FILES ${AUTOSRC}) -SOURCE_GROUP(MAIN FILES "AutopatcherClientTest.cpp") -SOURCE_GROUP(BZip2Wrapper FILES ${WRAPFILES}) -add_executable(AutopatcherClient "AutopatcherClientTest.cpp" ${AUTOSRC} ${BZSRC} ${WRAPFILES} "readme.txt") -target_link_libraries(AutopatcherClient ${RAKNET_COMMON_LIBS}) - -##VSUBFOLDER(AutopatcherClient "Samples/AutoPatcher/Client") - - +cmake_minimum_required(VERSION 3.10) +project(AutopatcherClient) + +set(Autopatcher_SOURCE_DIR ${RakNet_SOURCE_DIR}/DependentExtensions/Autopatcher) +set(BZip2_SOURCE_DIR ${RakNet_SOURCE_DIR}/DependentExtensions/bzip2-1.0.6) + +include_directories(${RAKNETHEADERFILES} ./ ${Autopatcher_SOURCE_DIR} ${BZip2_SOURCE_DIR} ) +FILE(GLOB AUTOSRC "${Autopatcher_SOURCE_DIR}/*.cpp" "${Autopatcher_SOURCE_DIR}/*.h") +LIST(REMOVE_ITEM AUTOSRC "${Autopatcher_SOURCE_DIR}/AutopatcherServer.cpp" "${Autopatcher_SOURCE_DIR}/AutopatcherServer.h" ) +FILE(GLOB BZSRC "${BZip2_SOURCE_DIR}/*.c" "${BZip2_SOURCE_DIR}/*.h") +LIST(REMOVE_ITEM BZSRC "${BZip2_SOURCE_DIR}/dlltest.c" "${BZip2_SOURCE_DIR}/mk251.c" "${BZip2_SOURCE_DIR}/bzip2recover.c") +SOURCE_GROUP(BZip2 FILES ${BZSRC}) +SET(WRAPFILES "${Autopatcher_SOURCE_DIR}/MemoryCompressor.cpp" "${Autopatcher_SOURCE_DIR}/MemoryCompressor.h") +LIST(REMOVE_ITEM AUTOSRC ${WRAPFILES}) +SOURCE_GROUP(Client_Files FILES ${AUTOSRC}) +SOURCE_GROUP(MAIN FILES "AutopatcherClientTest.cpp") +SOURCE_GROUP(BZip2Wrapper FILES ${WRAPFILES}) +add_executable(AutopatcherClient "AutopatcherClientTest.cpp" ${AUTOSRC} ${BZSRC} ${WRAPFILES} "readme.txt") +target_link_libraries(AutopatcherClient ${RAKNET_COMMON_LIBS}) + +##VSUBFOLDER(AutopatcherClient "Samples/AutoPatcher/Client") + + diff --git a/Samples/AutopatcherClientGFx3.0/CMakeLists.txt b/Samples/AutopatcherClientGFx3.0/CMakeLists.txt index cf68affba..159194d21 100644 --- a/Samples/AutopatcherClientGFx3.0/CMakeLists.txt +++ b/Samples/AutopatcherClientGFx3.0/CMakeLists.txt @@ -1,27 +1,27 @@ -cmake_minimum_required(VERSION 2.6) -project(AutopatcherClientGFx3) -IF(WIN32 AND NOT UNIX) - FINDD3D() - FINDSCALEGFX() - include_directories(${RAKNETHEADERFILES} ./ ${Autopatcher_SOURCE_DIR} ${BZip2_SOURCE_DIR} ${D3D_INCLUDE_DIR} ${SCALEGFX_INCLUDE_DIR} "${RakNet_SOURCE_DIR}/DependentExtensions/GFx3") - FILE(GLOB GFXSRC GFxPlayerTinyD3D9.cpp "${RakNet_SOURCE_DIR}/DependentExtensions/GFx3/*.cpp" "${RakNet_SOURCE_DIR}/DependentExtensions/GFx3/*.h") - FILE(GLOB AUTOSRC "${Autopatcher_SOURCE_DIR}/*.cpp" "${Autopatcher_SOURCE_DIR}/*.h") - FILE(GLOB BZSRC "${BZip2_SOURCE_DIR}/*.c" "${BZip2_SOURCE_DIR}/*.h") - LIST(REMOVE_ITEM BZSRC "${BZip2_SOURCE_DIR}/dlltest.c" "${BZip2_SOURCE_DIR}/mk251.c" "${BZip2_SOURCE_DIR}/bzip2recover.c") - SOURCE_GROUP(BZip2 FILES ${BZSRC}) - SET(WRAPFILES "${Autopatcher_SOURCE_DIR}/MemoryCompressor.cpp" "${Autopatcher_SOURCE_DIR}/MemoryCompressor.h") - LIST(REMOVE_ITEM AUTOSRC ${WRAPFILES}) - SOURCE_GROUP(Client_Files FILES ${AUTOSRC}) - SOURCE_GROUP(Main FILES "AutopatcherClientGFx3Impl.cpp" AutopatcherClientGFx3Impl.h) - SOURCE_GROUP(BZip2Wrapper FILES ${WRAPFILES}) - SOURCE_GROUP(GFx3 FILES ${GFXSRC}) - add_executable(AutopatcherClientGFx3 WIN32 "AutopatcherClientGFx3Impl.cpp" AutopatcherClientGFx3Impl.h ${AUTOSRC} ${BZSRC} ${WRAPFILES} ${GFXSRC} "readme.txt") - target_link_libraries(AutopatcherClientGFx3 ${RAKNET_COMMON_LIBS} ${D3D_LIBRARIES} winmm.lib imm32.lib ${SCALEGFX_DEBUG_LIBRARIES} ${SCALEGFX_LIBRARIES}) - VSUBFOLDER(AutopatcherClientGFx3 "Samples/AutoPatcher/Client") -ENDIF(WIN32 AND NOT UNIX) - - - - - - +cmake_minimum_required(VERSION 3.10) +project(AutopatcherClientGFx3) +IF(WIN32 AND NOT UNIX) + FINDD3D() + FINDSCALEGFX() + include_directories(${RAKNETHEADERFILES} ./ ${Autopatcher_SOURCE_DIR} ${BZip2_SOURCE_DIR} ${D3D_INCLUDE_DIR} ${SCALEGFX_INCLUDE_DIR} "${RakNet_SOURCE_DIR}/DependentExtensions/GFx3") + FILE(GLOB GFXSRC GFxPlayerTinyD3D9.cpp "${RakNet_SOURCE_DIR}/DependentExtensions/GFx3/*.cpp" "${RakNet_SOURCE_DIR}/DependentExtensions/GFx3/*.h") + FILE(GLOB AUTOSRC "${Autopatcher_SOURCE_DIR}/*.cpp" "${Autopatcher_SOURCE_DIR}/*.h") + FILE(GLOB BZSRC "${BZip2_SOURCE_DIR}/*.c" "${BZip2_SOURCE_DIR}/*.h") + LIST(REMOVE_ITEM BZSRC "${BZip2_SOURCE_DIR}/dlltest.c" "${BZip2_SOURCE_DIR}/mk251.c" "${BZip2_SOURCE_DIR}/bzip2recover.c") + SOURCE_GROUP(BZip2 FILES ${BZSRC}) + SET(WRAPFILES "${Autopatcher_SOURCE_DIR}/MemoryCompressor.cpp" "${Autopatcher_SOURCE_DIR}/MemoryCompressor.h") + LIST(REMOVE_ITEM AUTOSRC ${WRAPFILES}) + SOURCE_GROUP(Client_Files FILES ${AUTOSRC}) + SOURCE_GROUP(Main FILES "AutopatcherClientGFx3Impl.cpp" AutopatcherClientGFx3Impl.h) + SOURCE_GROUP(BZip2Wrapper FILES ${WRAPFILES}) + SOURCE_GROUP(GFx3 FILES ${GFXSRC}) + add_executable(AutopatcherClientGFx3 WIN32 "AutopatcherClientGFx3Impl.cpp" AutopatcherClientGFx3Impl.h ${AUTOSRC} ${BZSRC} ${WRAPFILES} ${GFXSRC} "readme.txt") + target_link_libraries(AutopatcherClientGFx3 ${RAKNET_COMMON_LIBS} ${D3D_LIBRARIES} winmm.lib imm32.lib ${SCALEGFX_DEBUG_LIBRARIES} ${SCALEGFX_LIBRARIES}) + VSUBFOLDER(AutopatcherClientGFx3 "Samples/AutoPatcher/Client") +ENDIF(WIN32 AND NOT UNIX) + + + + + + diff --git a/Samples/AutopatcherClientRestarter/CMakeLists.txt b/Samples/AutopatcherClientRestarter/CMakeLists.txt index 945a15325..d92a4bac0 100644 --- a/Samples/AutopatcherClientRestarter/CMakeLists.txt +++ b/Samples/AutopatcherClientRestarter/CMakeLists.txt @@ -1,11 +1,11 @@ -cmake_minimum_required(VERSION 2.6) -project(AutopatcherClientRestarter) -include_directories(${RAKNETHEADERFILES} ./) -add_executable(AutopatcherClientRestarter "main.cpp" "readme.txt") -target_link_libraries(AutopatcherClientRestarter ${RAKNET_COMMON_LIBS}) -VSUBFOLDER(AutopatcherClientRestarter "Samples/AutoPatcher/Client") - - - - - +cmake_minimum_required(VERSION 3.10) +project(AutopatcherClientRestarter) +include_directories(${RAKNETHEADERFILES} ./) +add_executable(AutopatcherClientRestarter "main.cpp" "readme.txt") +target_link_libraries(AutopatcherClientRestarter ${RAKNET_COMMON_LIBS}) +VSUBFOLDER(AutopatcherClientRestarter "Samples/AutoPatcher/Client") + + + + + diff --git a/Samples/AutopatcherServer/CMakeLists.txt b/Samples/AutopatcherServer/CMakeLists.txt index 2cf77cc3f..6bde128d2 100644 --- a/Samples/AutopatcherServer/CMakeLists.txt +++ b/Samples/AutopatcherServer/CMakeLists.txt @@ -1,26 +1,26 @@ -cmake_minimum_required(VERSION 2.6) -project(AutopatcherServer) - -IF(WIN32 AND NOT UNIX) - FILE(GLOB AUTOSRC "${Autopatcher_SOURCE_DIR}/AutopatcherServer.cpp" "${Autopatcher_SOURCE_DIR}/MemoryCompressor.cpp" "${Autopatcher_SOURCE_DIR}/CreatePatch.cpp" "${Autopatcher_SOURCE_DIR}/AutopatcherServer.h") - FILE(GLOB BZSRC "${BZip2_SOURCE_DIR}/*.c" "${BZip2_SOURCE_DIR}/*.h") - LIST(REMOVE_ITEM BZSRC "${BZip2_SOURCE_DIR}/dlltest.c" "${BZip2_SOURCE_DIR}/mk251.c" "${BZip2_SOURCE_DIR}/bzip2recover.c") - SOURCE_GROUP(BZip FILES ${BZSRC}) - SOURCE_GROUP("Source Files" FILES ${AUTOSRC}) - SOURCE_GROUP(Main "AutopatcherServerTest.cpp") - include_directories(${RAKNETHEADERFILES} ./ ${AutopatcherPostgreRepository_SOURCE_DIR} ${PostgreSQLInterface_SOURCE_DIR} ${Autopatcher_SOURCE_DIR} ${BZip2_SOURCE_DIR}) - add_executable(AutopatcherServer_PostgreSQL "AutopatcherServerTest.cpp" ${AUTOSRC} ${BZSRC} readme.txt) - target_link_libraries(AutopatcherServer_PostgreSQL ${RAKNET_COMMON_LIBS} AutopatcherPostgreRepository) - VSUBFOLDER(AutopatcherServer_PostgreSQL "Samples/AutoPatcher/Server/PostgreSQL") -ELSE(WIN32 AND NOT UNIX) - include_directories(${RAKNETHEADERFILES} ./ ${AutopatcherPostgreRepository_SOURCE_DIR} ${PostgreSQLInterface_SOURCE_DIR} ${Autopatcher_SOURCE_DIR}) - add_executable(AutopatcherServer_PostgreSQL "AutopatcherServerTest.cpp") - target_link_libraries(AutopatcherServer_PostgreSQL ${RAKNET_COMMON_LIBS} LibAutopatcher AutopatcherPostgreRepository LibPostgreSQLInterface) -ENDIF(WIN32 AND NOT UNIX) - - - - - - - +cmake_minimum_required(VERSION 3.10) +project(AutopatcherServer) + +IF(WIN32 AND NOT UNIX) + FILE(GLOB AUTOSRC "${Autopatcher_SOURCE_DIR}/AutopatcherServer.cpp" "${Autopatcher_SOURCE_DIR}/MemoryCompressor.cpp" "${Autopatcher_SOURCE_DIR}/CreatePatch.cpp" "${Autopatcher_SOURCE_DIR}/AutopatcherServer.h") + FILE(GLOB BZSRC "${BZip2_SOURCE_DIR}/*.c" "${BZip2_SOURCE_DIR}/*.h") + LIST(REMOVE_ITEM BZSRC "${BZip2_SOURCE_DIR}/dlltest.c" "${BZip2_SOURCE_DIR}/mk251.c" "${BZip2_SOURCE_DIR}/bzip2recover.c") + SOURCE_GROUP(BZip FILES ${BZSRC}) + SOURCE_GROUP("Source Files" FILES ${AUTOSRC}) + SOURCE_GROUP(Main "AutopatcherServerTest.cpp") + include_directories(${RAKNETHEADERFILES} ./ ${AutopatcherPostgreRepository_SOURCE_DIR} ${PostgreSQLInterface_SOURCE_DIR} ${Autopatcher_SOURCE_DIR} ${BZip2_SOURCE_DIR}) + add_executable(AutopatcherServer_PostgreSQL "AutopatcherServerTest.cpp" ${AUTOSRC} ${BZSRC} readme.txt) + target_link_libraries(AutopatcherServer_PostgreSQL ${RAKNET_COMMON_LIBS} AutopatcherPostgreRepository) + VSUBFOLDER(AutopatcherServer_PostgreSQL "Samples/AutoPatcher/Server/PostgreSQL") +ELSE(WIN32 AND NOT UNIX) + include_directories(${RAKNETHEADERFILES} ./ ${AutopatcherPostgreRepository_SOURCE_DIR} ${PostgreSQLInterface_SOURCE_DIR} ${Autopatcher_SOURCE_DIR}) + add_executable(AutopatcherServer_PostgreSQL "AutopatcherServerTest.cpp") + target_link_libraries(AutopatcherServer_PostgreSQL ${RAKNET_COMMON_LIBS} LibAutopatcher AutopatcherPostgreRepository LibPostgreSQLInterface) +ENDIF(WIN32 AND NOT UNIX) + + + + + + + diff --git a/Samples/BigPacketTest/CMakeLists.txt b/Samples/BigPacketTest/CMakeLists.txt index 8174b4532..9ac5f32b3 100644 --- a/Samples/BigPacketTest/CMakeLists.txt +++ b/Samples/BigPacketTest/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Samples") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Samples") + + + + + + diff --git a/Samples/BurstTest/CMakeLists.txt b/Samples/BurstTest/CMakeLists.txt index 6349f7e88..1c63ac592 100644 --- a/Samples/BurstTest/CMakeLists.txt +++ b/Samples/BurstTest/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(BurstTest) -VSUBFOLDER(BurstTest "Internal Tests") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(BurstTest) +VSUBFOLDER(BurstTest "Internal Tests") + + + + + + diff --git a/Samples/CMakeLists.txt b/Samples/CMakeLists.txt index b1ce30942..4b36c3dd6 100644 --- a/Samples/CMakeLists.txt +++ b/Samples/CMakeLists.txt @@ -1,307 +1,307 @@ -cmake_minimum_required(VERSION 2.6) - -option( RAKNET_SAMPLE_AutopatcherClient "" True ) -#option( RAKNET_SAMPLE_AutopatcherClientGFx3_0 "" True ) -option( RAKNET_SAMPLE_AutopatcherClientRestarter "" True ) -option( RAKNET_SAMPLE_AutopatcherServer "" True ) -option( RAKNET_SAMPLE_AutoPatcherServer_MySQL "" True ) -option( RAKNET_SAMPLE_BigPacketTest "" True ) -option( RAKNET_SAMPLE_BurstTest "" True ) -option( RAKNET_SAMPLE_Chat_Example "" True ) -option( RAKNET_SAMPLE_CloudClient "" True ) -option( RAKNET_SAMPLE_CloudServer "" True ) -option( RAKNET_SAMPLE_CloudTest "" True ) -option( RAKNET_SAMPLE_CommandConsoleClient "" True ) -option( RAKNET_SAMPLE_CommandConsoleServer "" True ) -option( RAKNET_SAMPLE_ComprehensivePCGame "" True ) -option( RAKNET_SAMPLE_ComprehensiveTest "" True ) -#option( RAKNET_SAMPLE_CrashRelauncher "" True ) -option( RAKNET_SAMPLE_CrashReporter "" True ) -option( RAKNET_SAMPLE_CrossConnectionTest "" True ) -option( RAKNET_SAMPLE_DirectoryDeltaTransfer "" True ) -option( RAKNET_SAMPLE_Dropped_Connection_Test "" True ) -option( RAKNET_SAMPLE_Encryption "" True ) -option( RAKNET_SAMPLE_FCMHost "" True ) -option( RAKNET_SAMPLE_FCMHostSimultaneous "" True ) -option( RAKNET_SAMPLE_FCMVerifiedJoinSimultaneous "" True ) -option( RAKNET_SAMPLE_FileListTransfer "" True ) -option( RAKNET_SAMPLE_Flow_Control_Test "" True ) -option( RAKNET_SAMPLE_Fully_Connected_Mesh "" True ) -#option( RAKNET_SAMPLE_GFWL "" True ) -#option( RAKNET_SAMPLE_iOS "" True ) -option( RAKNET_SAMPLE_LANServerDiscovery "" True ) -option( RAKNET_SAMPLE_Lobby2Client "" True ) -#option( RAKNET_SAMPLE_Lobby2ClientGFx3_0 "" True ) -#option( RAKNET_SAMPLE_Lobby2Client_PS3 "" True ) -#option( RAKNET_SAMPLE_Lobby2Server_PGSQL "" True ) -#option( RAKNET_SAMPLE_LobbyDB_PostgreSQL "" True ) -#option( RAKNET_SAMPLE_LoopbackPerformanceTest "" True ) -#option( RAKNET_SAMPLE_Marmalade "" True ) -option( RAKNET_SAMPLE_MasterServer "" True ) -option( RAKNET_SAMPLE_MessageFilter "" True ) -option( RAKNET_SAMPLE_MessageSizeTest "" True ) -option( RAKNET_SAMPLE_NATCompleteClient "" True ) -option( RAKNET_SAMPLE_NATCompleteServer "" True ) -option( RAKNET_SAMPLE_OfflineMessagesTest "" True ) -option( RAKNET_SAMPLE_PacketLogger "" True ) -option( RAKNET_SAMPLE_PHPDirectoryServer2 "" True ) -option( RAKNET_SAMPLE_Ping "" True ) -#option( RAKNET_SAMPLE_PS3 "" True ) -option( RAKNET_SAMPLE_RackspaceConsole "" True ) -option( RAKNET_SAMPLE_RakVoice "" True ) -option( RAKNET_SAMPLE_RakVoiceDSound "" True ) -option( RAKNET_SAMPLE_RakVoiceFMOD "" True ) -#option( RAKNET_SAMPLE_RakVoiceFMODAsDLL "" True ) -#option( RAKNET_SAMPLE_RankingServerDB "" True ) -#option( RAKNET_SAMPLE_RankingServerDBTest "" True ) -#option( RAKNET_SAMPLE_ReadyEvent "" True ) -option( RAKNET_SAMPLE_Reliable_Ordered_Test "" True ) -option( RAKNET_SAMPLE_ReplicaManager3 "" True ) -#option( RAKNET_SAMPLE_Rooms "" True ) -#option( RAKNET_SAMPLE_RoomsBrowserGFx3 "" True ) -option( RAKNET_SAMPLE_Router2 "" True ) -option( RAKNET_SAMPLE_RPC3 "" True ) -option( RAKNET_SAMPLE_RPC4 "" True ) -option( RAKNET_SAMPLE_SendEmail "" True ) -option( RAKNET_SAMPLE_ServerClientTest2 "" True ) -option( RAKNET_SAMPLE_StatisticsHistoryTest "" True ) -#option( RAKNET_SAMPLE_SteamLobby "" True ) -option( RAKNET_SAMPLE_TeamManager "" True ) -option( RAKNET_SAMPLE_TestDLL "" True ) -option( RAKNET_SAMPLE_Tests "" True ) -option( RAKNET_SAMPLE_ThreadTest "" True ) -option( RAKNET_SAMPLE_Timestamping "" True ) -option( RAKNET_SAMPLE_TitleValidationDB_PostgreSQL "" True ) -option( RAKNET_SAMPLE_TwoWayAuthentication "" True ) -option( RAKNET_SAMPLE_UDPForwarder "" True ) -#option( RAKNET_SAMPLE_Vita "" True ) -#option( RAKNET_SAMPLE_XBOX360 "" True ) - -if(RAKNET_SAMPLE_AutopatcherClient) - add_subdirectory("AutopatcherClient") -endif() -if(RAKNET_SAMPLE_AutopatcherClientGFx3_0) - #add_subdirectory("AutopatcherClientGFx3.0") -endif() -if(RAKNET_SAMPLE_AutopatcherClientRestarter) - add_subdirectory("AutopatcherClientRestarter") -endif() -if(RAKNET_SAMPLE_AutopatcherServer) - add_subdirectory("AutopatcherServer") -endif() -if(RAKNET_SAMPLE_AutoPatcherServer_MySQL) - add_subdirectory("AutoPatcherServer_MySQL") -endif() -if(RAKNET_SAMPLE_BigPacketTest) - add_subdirectory("BigPacketTest") -endif() -if(RAKNET_SAMPLE_BurstTest) - add_subdirectory("BurstTest") -endif() -if(RAKNET_SAMPLE_Chat_Example) - add_subdirectory("Chat Example") -endif() -if(RAKNET_SAMPLE_CloudClient) - add_subdirectory("CloudClient") -endif() -if(RAKNET_SAMPLE_CloudServer) - add_subdirectory("CloudServer") -endif() -if(RAKNET_SAMPLE_CloudTest) - add_subdirectory("CloudTest") -endif() -if(RAKNET_SAMPLE_CommandConsoleClient) - add_subdirectory("CommandConsoleClient") -endif() -if(RAKNET_SAMPLE_CommandConsoleServer) - add_subdirectory("CommandConsoleServer") -endif() -if(RAKNET_SAMPLE_ComprehensivePCGame) - add_subdirectory("ComprehensivePCGame") -endif() -if(RAKNET_SAMPLE_ComprehensiveTest) - add_subdirectory("ComprehensiveTest") -endif() -if(RAKNET_SAMPLE_CrashRelauncher) - #add_subdirectory("CrashRelauncher") -endif() -if(RAKNET_SAMPLE_CrashReporter) - add_subdirectory("CrashReporter") -endif() -if(RAKNET_SAMPLE_CrossConnectionTest) - add_subdirectory("CrossConnectionTest") -endif() -if(RAKNET_SAMPLE_DirectoryDeltaTransfer) - add_subdirectory("DirectoryDeltaTransfer") -endif() -if(RAKNET_SAMPLE_Dropped_Connection_Test) - add_subdirectory("Dropped Connection Test") -endif() -if(RAKNET_SAMPLE_Encryption) - add_subdirectory("Encryption") -endif() -if(RAKNET_SAMPLE_FCMHost) - add_subdirectory("FCMHost") -endif() -if(RAKNET_SAMPLE_FCMHostSimultaneous) - add_subdirectory("FCMHostSimultaneous") -endif() -if(RAKNET_SAMPLE_FCMVerifiedJoinSimultaneous) - add_subdirectory("FCMVerifiedJoinSimultaneous") -endif() -if(RAKNET_SAMPLE_FileListTransfer) - add_subdirectory("FileListTransfer") -endif() -if(RAKNET_SAMPLE_Flow_Control_Test) - add_subdirectory("Flow Control Test") -endif() -if(RAKNET_SAMPLE_Fully_Connected_Mesh) - add_subdirectory("Fully Connected Mesh") -endif() -if(RAKNET_SAMPLE_GFWL) - #add_subdirectory("GFWL") -endif() -if(RAKNET_SAMPLE_iOS) - #add_subdirectory("iOS") -endif() -if(RAKNET_SAMPLE_LANServerDiscovery) - add_subdirectory("LANServerDiscovery") -endif() -if(RAKNET_SAMPLE_Lobby2Client) - add_subdirectory("Lobby2Client") -endif() -if(RAKNET_SAMPLE_Lobby2ClientGFx3_0) - #add_subdirectory("Lobby2ClientGFx3.0") -endif() -if(RAKNET_SAMPLE_Lobby2Client_PS3) - #add_subdirectory("Lobby2Client_PS3") -endif() -if(RAKNET_SAMPLE_Lobby2Server_PGSQL) - #add_subdirectory("Lobby2Server_PGSQL") -endif() -if(RAKNET_SAMPLE_LobbyDB_PostgreSQL) - #add_subdirectory("LobbyDB_PostgreSQL") -endif() -if(RAKNET_SAMPLE_LoopbackPerformanceTest) - #add_subdirectory("LoopbackPerformanceTest") -endif() -if(RAKNET_SAMPLE_Marmalade) - #add_subdirectory("Marmalade") -endif() -if(RAKNET_SAMPLE_MasterServer) - add_subdirectory("MasterServer") -endif() -if(RAKNET_SAMPLE_MessageFilter) - add_subdirectory("MessageFilter") -endif() -if(RAKNET_SAMPLE_MessageSizeTest) - add_subdirectory("MessageSizeTest") -endif() -if(RAKNET_SAMPLE_NATCompleteClient) - add_subdirectory("NATCompleteClient") -endif() -if(RAKNET_SAMPLE_NATCompleteServer) - add_subdirectory("NATCompleteServer") -endif() -if(RAKNET_SAMPLE_OfflineMessagesTest) - add_subdirectory("OfflineMessagesTest") -endif() -if(RAKNET_SAMPLE_PacketLogger) - add_subdirectory("PacketLogger") -endif() -if(RAKNET_SAMPLE_PHPDirectoryServer2) - add_subdirectory("PHPDirectoryServer2") -endif() -if(RAKNET_SAMPLE_Ping) - add_subdirectory("Ping") -endif() -if(RAKNET_SAMPLE_PS3) - #add_subdirectory("PS3") -endif() -if(RAKNET_SAMPLE_RackspaceConsole) - add_subdirectory("RackspaceConsole") -endif() -if(RAKNET_SAMPLE_RakVoice) - add_subdirectory("RakVoice") -endif() -if(RAKNET_SAMPLE_RakVoiceDSound) - add_subdirectory("RakVoiceDSound") -endif() -if(RAKNET_SAMPLE_RakVoiceFMOD) - add_subdirectory("RakVoiceFMOD") -endif() -if(RAKNET_SAMPLE_RakVoiceFMODAsDLL) - #add_subdirectory("RakVoiceFMODAsDLL") -endif() -if(RAKNET_SAMPLE_RankingServerDB) - #add_subdirectory("RankingServerDB") -endif() -if(RAKNET_SAMPLE_RankingServerDBTest) - #add_subdirectory("RankingServerDBTest") -endif() -if(RAKNET_SAMPLE_ReadyEvent) - #add_subdirectory("ReadyEvent") -endif() -if(RAKNET_SAMPLE_Reliable_Ordered_Test) - add_subdirectory("Reliable Ordered Test") -endif() -if(RAKNET_SAMPLE_ReplicaManager3) - add_subdirectory("ReplicaManager3") -endif() -if(RAKNET_SAMPLE_Rooms) - #add_subdirectory("Rooms") -endif() -if(RAKNET_SAMPLE_RoomsBrowserGFx3) - #add_subdirectory("RoomsBrowserGFx3") -endif() -if(RAKNET_SAMPLE_Router2) - add_subdirectory("Router2") -endif() -if(RAKNET_SAMPLE_RPC3) - add_subdirectory("RPC3") -endif() -if(RAKNET_SAMPLE_RPC4) - add_subdirectory("RPC4") -endif() -if(RAKNET_SAMPLE_SendEmail) - add_subdirectory("SendEmail") -endif() -if(RAKNET_SAMPLE_ServerClientTest2) - add_subdirectory("ServerClientTest2") -endif() -if(RAKNET_SAMPLE_StatisticsHistoryTest) - add_subdirectory("StatisticsHistoryTest") -endif() -if(RAKNET_SAMPLE_SteamLobby) - #add_subdirectory("SteamLobby") -endif() -if(RAKNET_SAMPLE_TeamManager) - add_subdirectory("TeamManager") -endif() -if(RAKNET_SAMPLE_TestDLL) - add_subdirectory("TestDLL") -endif() -if(RAKNET_SAMPLE_Tests) - add_subdirectory("Tests") -endif() -if(RAKNET_SAMPLE_ThreadTest) - add_subdirectory("ThreadTest") -endif() -if(RAKNET_SAMPLE_Timestamping) - add_subdirectory("Timestamping") -endif() -if(RAKNET_SAMPLE_TitleValidationDB_PostgreSQL) - add_subdirectory("TitleValidationDB_PostgreSQL") -endif() -if(RAKNET_SAMPLE_TwoWayAuthentication) - add_subdirectory("TwoWayAuthentication") -endif() -if(RAKNET_SAMPLE_UDPForwarder) - add_subdirectory("UDPForwarder") -endif() -if(RAKNET_SAMPLE_Vita) - #add_subdirectory("Vita") -endif() -if(RAKNET_SAMPLE_XBOX360) - #add_subdirectory("XBOX360") -endif() +cmake_minimum_required(VERSION 3.10) + +option( RAKNET_SAMPLE_AutopatcherClient "" True ) +#option( RAKNET_SAMPLE_AutopatcherClientGFx3_0 "" True ) +option( RAKNET_SAMPLE_AutopatcherClientRestarter "" True ) +option( RAKNET_SAMPLE_AutopatcherServer "" True ) +option( RAKNET_SAMPLE_AutoPatcherServer_MySQL "" True ) +option( RAKNET_SAMPLE_BigPacketTest "" True ) +option( RAKNET_SAMPLE_BurstTest "" True ) +option( RAKNET_SAMPLE_Chat_Example "" True ) +option( RAKNET_SAMPLE_CloudClient "" True ) +option( RAKNET_SAMPLE_CloudServer "" True ) +option( RAKNET_SAMPLE_CloudTest "" True ) +option( RAKNET_SAMPLE_CommandConsoleClient "" True ) +option( RAKNET_SAMPLE_CommandConsoleServer "" True ) +option( RAKNET_SAMPLE_ComprehensivePCGame "" True ) +option( RAKNET_SAMPLE_ComprehensiveTest "" True ) +#option( RAKNET_SAMPLE_CrashRelauncher "" True ) +option( RAKNET_SAMPLE_CrashReporter "" True ) +option( RAKNET_SAMPLE_CrossConnectionTest "" True ) +option( RAKNET_SAMPLE_DirectoryDeltaTransfer "" True ) +option( RAKNET_SAMPLE_Dropped_Connection_Test "" True ) +option( RAKNET_SAMPLE_Encryption "" True ) +option( RAKNET_SAMPLE_FCMHost "" True ) +option( RAKNET_SAMPLE_FCMHostSimultaneous "" True ) +option( RAKNET_SAMPLE_FCMVerifiedJoinSimultaneous "" True ) +option( RAKNET_SAMPLE_FileListTransfer "" True ) +option( RAKNET_SAMPLE_Flow_Control_Test "" True ) +option( RAKNET_SAMPLE_Fully_Connected_Mesh "" True ) +#option( RAKNET_SAMPLE_GFWL "" True ) +#option( RAKNET_SAMPLE_iOS "" True ) +option( RAKNET_SAMPLE_LANServerDiscovery "" True ) +option( RAKNET_SAMPLE_Lobby2Client "" True ) +#option( RAKNET_SAMPLE_Lobby2ClientGFx3_0 "" True ) +#option( RAKNET_SAMPLE_Lobby2Client_PS3 "" True ) +#option( RAKNET_SAMPLE_Lobby2Server_PGSQL "" True ) +#option( RAKNET_SAMPLE_LobbyDB_PostgreSQL "" True ) +#option( RAKNET_SAMPLE_LoopbackPerformanceTest "" True ) +#option( RAKNET_SAMPLE_Marmalade "" True ) +option( RAKNET_SAMPLE_MasterServer "" True ) +option( RAKNET_SAMPLE_MessageFilter "" True ) +option( RAKNET_SAMPLE_MessageSizeTest "" True ) +option( RAKNET_SAMPLE_NATCompleteClient "" True ) +option( RAKNET_SAMPLE_NATCompleteServer "" True ) +option( RAKNET_SAMPLE_OfflineMessagesTest "" True ) +option( RAKNET_SAMPLE_PacketLogger "" True ) +option( RAKNET_SAMPLE_PHPDirectoryServer2 "" True ) +option( RAKNET_SAMPLE_Ping "" True ) +#option( RAKNET_SAMPLE_PS3 "" True ) +option( RAKNET_SAMPLE_RackspaceConsole "" True ) +option( RAKNET_SAMPLE_RakVoice "" True ) +option( RAKNET_SAMPLE_RakVoiceDSound "" True ) +option( RAKNET_SAMPLE_RakVoiceFMOD "" True ) +#option( RAKNET_SAMPLE_RakVoiceFMODAsDLL "" True ) +#option( RAKNET_SAMPLE_RankingServerDB "" True ) +#option( RAKNET_SAMPLE_RankingServerDBTest "" True ) +#option( RAKNET_SAMPLE_ReadyEvent "" True ) +option( RAKNET_SAMPLE_Reliable_Ordered_Test "" True ) +option( RAKNET_SAMPLE_ReplicaManager3 "" True ) +#option( RAKNET_SAMPLE_Rooms "" True ) +#option( RAKNET_SAMPLE_RoomsBrowserGFx3 "" True ) +option( RAKNET_SAMPLE_Router2 "" True ) +option( RAKNET_SAMPLE_RPC3 "" True ) +option( RAKNET_SAMPLE_RPC4 "" True ) +option( RAKNET_SAMPLE_SendEmail "" True ) +option( RAKNET_SAMPLE_ServerClientTest2 "" True ) +option( RAKNET_SAMPLE_StatisticsHistoryTest "" True ) +#option( RAKNET_SAMPLE_SteamLobby "" True ) +option( RAKNET_SAMPLE_TeamManager "" True ) +option( RAKNET_SAMPLE_TestDLL "" True ) +option( RAKNET_SAMPLE_Tests "" True ) +option( RAKNET_SAMPLE_ThreadTest "" True ) +option( RAKNET_SAMPLE_Timestamping "" True ) +option( RAKNET_SAMPLE_TitleValidationDB_PostgreSQL "" True ) +option( RAKNET_SAMPLE_TwoWayAuthentication "" True ) +option( RAKNET_SAMPLE_UDPForwarder "" True ) +#option( RAKNET_SAMPLE_Vita "" True ) +#option( RAKNET_SAMPLE_XBOX360 "" True ) + +if(RAKNET_SAMPLE_AutopatcherClient) + add_subdirectory("AutopatcherClient") +endif() +if(RAKNET_SAMPLE_AutopatcherClientGFx3_0) + #add_subdirectory("AutopatcherClientGFx3.0") +endif() +if(RAKNET_SAMPLE_AutopatcherClientRestarter) + add_subdirectory("AutopatcherClientRestarter") +endif() +if(RAKNET_SAMPLE_AutopatcherServer) + add_subdirectory("AutopatcherServer") +endif() +if(RAKNET_SAMPLE_AutoPatcherServer_MySQL) + add_subdirectory("AutoPatcherServer_MySQL") +endif() +if(RAKNET_SAMPLE_BigPacketTest) + add_subdirectory("BigPacketTest") +endif() +if(RAKNET_SAMPLE_BurstTest) + add_subdirectory("BurstTest") +endif() +if(RAKNET_SAMPLE_Chat_Example) + add_subdirectory("Chat Example") +endif() +if(RAKNET_SAMPLE_CloudClient) + add_subdirectory("CloudClient") +endif() +if(RAKNET_SAMPLE_CloudServer) + add_subdirectory("CloudServer") +endif() +if(RAKNET_SAMPLE_CloudTest) + add_subdirectory("CloudTest") +endif() +if(RAKNET_SAMPLE_CommandConsoleClient) + add_subdirectory("CommandConsoleClient") +endif() +if(RAKNET_SAMPLE_CommandConsoleServer) + add_subdirectory("CommandConsoleServer") +endif() +if(RAKNET_SAMPLE_ComprehensivePCGame) + add_subdirectory("ComprehensivePCGame") +endif() +if(RAKNET_SAMPLE_ComprehensiveTest) + add_subdirectory("ComprehensiveTest") +endif() +if(RAKNET_SAMPLE_CrashRelauncher) + #add_subdirectory("CrashRelauncher") +endif() +if(RAKNET_SAMPLE_CrashReporter) + add_subdirectory("CrashReporter") +endif() +if(RAKNET_SAMPLE_CrossConnectionTest) + add_subdirectory("CrossConnectionTest") +endif() +if(RAKNET_SAMPLE_DirectoryDeltaTransfer) + add_subdirectory("DirectoryDeltaTransfer") +endif() +if(RAKNET_SAMPLE_Dropped_Connection_Test) + add_subdirectory("Dropped Connection Test") +endif() +if(RAKNET_SAMPLE_Encryption) + add_subdirectory("Encryption") +endif() +if(RAKNET_SAMPLE_FCMHost) + add_subdirectory("FCMHost") +endif() +if(RAKNET_SAMPLE_FCMHostSimultaneous) + add_subdirectory("FCMHostSimultaneous") +endif() +if(RAKNET_SAMPLE_FCMVerifiedJoinSimultaneous) + add_subdirectory("FCMVerifiedJoinSimultaneous") +endif() +if(RAKNET_SAMPLE_FileListTransfer) + add_subdirectory("FileListTransfer") +endif() +if(RAKNET_SAMPLE_Flow_Control_Test) + add_subdirectory("Flow Control Test") +endif() +if(RAKNET_SAMPLE_Fully_Connected_Mesh) + add_subdirectory("Fully Connected Mesh") +endif() +if(RAKNET_SAMPLE_GFWL) + #add_subdirectory("GFWL") +endif() +if(RAKNET_SAMPLE_iOS) + #add_subdirectory("iOS") +endif() +if(RAKNET_SAMPLE_LANServerDiscovery) + add_subdirectory("LANServerDiscovery") +endif() +if(RAKNET_SAMPLE_Lobby2Client) + add_subdirectory("Lobby2Client") +endif() +if(RAKNET_SAMPLE_Lobby2ClientGFx3_0) + #add_subdirectory("Lobby2ClientGFx3.0") +endif() +if(RAKNET_SAMPLE_Lobby2Client_PS3) + #add_subdirectory("Lobby2Client_PS3") +endif() +if(RAKNET_SAMPLE_Lobby2Server_PGSQL) + #add_subdirectory("Lobby2Server_PGSQL") +endif() +if(RAKNET_SAMPLE_LobbyDB_PostgreSQL) + #add_subdirectory("LobbyDB_PostgreSQL") +endif() +if(RAKNET_SAMPLE_LoopbackPerformanceTest) + #add_subdirectory("LoopbackPerformanceTest") +endif() +if(RAKNET_SAMPLE_Marmalade) + #add_subdirectory("Marmalade") +endif() +if(RAKNET_SAMPLE_MasterServer) + add_subdirectory("MasterServer") +endif() +if(RAKNET_SAMPLE_MessageFilter) + add_subdirectory("MessageFilter") +endif() +if(RAKNET_SAMPLE_MessageSizeTest) + add_subdirectory("MessageSizeTest") +endif() +if(RAKNET_SAMPLE_NATCompleteClient) + add_subdirectory("NATCompleteClient") +endif() +if(RAKNET_SAMPLE_NATCompleteServer) + add_subdirectory("NATCompleteServer") +endif() +if(RAKNET_SAMPLE_OfflineMessagesTest) + add_subdirectory("OfflineMessagesTest") +endif() +if(RAKNET_SAMPLE_PacketLogger) + add_subdirectory("PacketLogger") +endif() +if(RAKNET_SAMPLE_PHPDirectoryServer2) + add_subdirectory("PHPDirectoryServer2") +endif() +if(RAKNET_SAMPLE_Ping) + add_subdirectory("Ping") +endif() +if(RAKNET_SAMPLE_PS3) + #add_subdirectory("PS3") +endif() +if(RAKNET_SAMPLE_RackspaceConsole) + add_subdirectory("RackspaceConsole") +endif() +if(RAKNET_SAMPLE_RakVoice) + add_subdirectory("RakVoice") +endif() +if(RAKNET_SAMPLE_RakVoiceDSound) + add_subdirectory("RakVoiceDSound") +endif() +if(RAKNET_SAMPLE_RakVoiceFMOD) + add_subdirectory("RakVoiceFMOD") +endif() +if(RAKNET_SAMPLE_RakVoiceFMODAsDLL) + #add_subdirectory("RakVoiceFMODAsDLL") +endif() +if(RAKNET_SAMPLE_RankingServerDB) + #add_subdirectory("RankingServerDB") +endif() +if(RAKNET_SAMPLE_RankingServerDBTest) + #add_subdirectory("RankingServerDBTest") +endif() +if(RAKNET_SAMPLE_ReadyEvent) + #add_subdirectory("ReadyEvent") +endif() +if(RAKNET_SAMPLE_Reliable_Ordered_Test) + add_subdirectory("Reliable Ordered Test") +endif() +if(RAKNET_SAMPLE_ReplicaManager3) + add_subdirectory("ReplicaManager3") +endif() +if(RAKNET_SAMPLE_Rooms) + #add_subdirectory("Rooms") +endif() +if(RAKNET_SAMPLE_RoomsBrowserGFx3) + #add_subdirectory("RoomsBrowserGFx3") +endif() +if(RAKNET_SAMPLE_Router2) + add_subdirectory("Router2") +endif() +if(RAKNET_SAMPLE_RPC3) + add_subdirectory("RPC3") +endif() +if(RAKNET_SAMPLE_RPC4) + add_subdirectory("RPC4") +endif() +if(RAKNET_SAMPLE_SendEmail) + add_subdirectory("SendEmail") +endif() +if(RAKNET_SAMPLE_ServerClientTest2) + add_subdirectory("ServerClientTest2") +endif() +if(RAKNET_SAMPLE_StatisticsHistoryTest) + add_subdirectory("StatisticsHistoryTest") +endif() +if(RAKNET_SAMPLE_SteamLobby) + #add_subdirectory("SteamLobby") +endif() +if(RAKNET_SAMPLE_TeamManager) + add_subdirectory("TeamManager") +endif() +if(RAKNET_SAMPLE_TestDLL) + add_subdirectory("TestDLL") +endif() +if(RAKNET_SAMPLE_Tests) + add_subdirectory("Tests") +endif() +if(RAKNET_SAMPLE_ThreadTest) + add_subdirectory("ThreadTest") +endif() +if(RAKNET_SAMPLE_Timestamping) + add_subdirectory("Timestamping") +endif() +if(RAKNET_SAMPLE_TitleValidationDB_PostgreSQL) + add_subdirectory("TitleValidationDB_PostgreSQL") +endif() +if(RAKNET_SAMPLE_TwoWayAuthentication) + add_subdirectory("TwoWayAuthentication") +endif() +if(RAKNET_SAMPLE_UDPForwarder) + add_subdirectory("UDPForwarder") +endif() +if(RAKNET_SAMPLE_Vita) + #add_subdirectory("Vita") +endif() +if(RAKNET_SAMPLE_XBOX360) + #add_subdirectory("XBOX360") +endif() diff --git a/Samples/Chat Example/CMakeLists.txt b/Samples/Chat Example/CMakeLists.txt index b2d6678e6..c3bfc6dd6 100644 --- a/Samples/Chat Example/CMakeLists.txt +++ b/Samples/Chat Example/CMakeLists.txt @@ -1,17 +1,17 @@ -cmake_minimum_required(VERSION 2.6) -project("Chat Example") -include_directories(${RAKNETHEADERFILES} ./) -add_executable(ChatServer "Chat Example Server.cpp" readme.txt) -add_executable(ChatClient "Chat Example Client.cpp" readme.txt) - -target_link_libraries(ChatServer ${RAKNET_COMMON_LIBS}) -VSUBFOLDER(ChatServer "Samples") - -target_link_libraries(ChatClient ${RAKNET_COMMON_LIBS}) -VSUBFOLDER(ChatClient "Samples") - - - - - - +cmake_minimum_required(VERSION 3.10) +project("Chat Example") +include_directories(${RAKNETHEADERFILES} ./) +add_executable(ChatServer "Chat Example Server.cpp" readme.txt) +add_executable(ChatClient "Chat Example Client.cpp" readme.txt) + +target_link_libraries(ChatServer ${RAKNET_COMMON_LIBS}) +VSUBFOLDER(ChatServer "Samples") + +target_link_libraries(ChatClient ${RAKNET_COMMON_LIBS}) +VSUBFOLDER(ChatClient "Samples") + + + + + + diff --git a/Samples/CloudClient/CMakeLists.txt b/Samples/CloudClient/CMakeLists.txt index 8174b4532..9ac5f32b3 100644 --- a/Samples/CloudClient/CMakeLists.txt +++ b/Samples/CloudClient/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Samples") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Samples") + + + + + + diff --git a/Samples/CloudServer/CMakeLists.txt b/Samples/CloudServer/CMakeLists.txt index 8174b4532..9ac5f32b3 100644 --- a/Samples/CloudServer/CMakeLists.txt +++ b/Samples/CloudServer/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Samples") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Samples") + + + + + + diff --git a/Samples/CloudTest/CMakeLists.txt b/Samples/CloudTest/CMakeLists.txt index 8174b4532..9ac5f32b3 100644 --- a/Samples/CloudTest/CMakeLists.txt +++ b/Samples/CloudTest/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Samples") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Samples") + + + + + + diff --git a/Samples/CommandConsoleClient/CMakeLists.txt b/Samples/CommandConsoleClient/CMakeLists.txt index 529dd4db7..f25c9e02e 100644 --- a/Samples/CommandConsoleClient/CMakeLists.txt +++ b/Samples/CommandConsoleClient/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Samples/Command Console") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Samples/Command Console") + + + + + + diff --git a/Samples/CommandConsoleServer/CMakeLists.txt b/Samples/CommandConsoleServer/CMakeLists.txt index 106722d49..3cc0375ec 100644 --- a/Samples/CommandConsoleServer/CMakeLists.txt +++ b/Samples/CommandConsoleServer/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Samples/Command Console") - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Samples/Command Console") + + + + + diff --git a/Samples/ComprehensivePCGame/CMakeLists.txt b/Samples/ComprehensivePCGame/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/ComprehensivePCGame/CMakeLists.txt +++ b/Samples/ComprehensivePCGame/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/ComprehensiveTest/CMakeLists.txt b/Samples/ComprehensiveTest/CMakeLists.txt index 7b2b62c17..e6c77d71a 100644 --- a/Samples/ComprehensiveTest/CMakeLists.txt +++ b/Samples/ComprehensiveTest/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Internal Tests") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Internal Tests") + + + + + + diff --git a/Samples/CrashReporter/CMakeLists.txt b/Samples/CrashReporter/CMakeLists.txt index 7087605e7..8d2e9b53d 100644 --- a/Samples/CrashReporter/CMakeLists.txt +++ b/Samples/CrashReporter/CMakeLists.txt @@ -1,12 +1,12 @@ -cmake_minimum_required(VERSION 2.6) - -IF (WIN32 AND NOT UNIX) - GETCURRENTFOLDER() - STANDARDSUBPROJECTWITHOPTIONS(${current_folder} "" "" "Dbghelp.lib") -ENDIF(WIN32 AND NOT UNIX) - - - - - - +cmake_minimum_required(VERSION 3.10) + +IF (WIN32 AND NOT UNIX) + GETCURRENTFOLDER() + STANDARDSUBPROJECTWITHOPTIONS(${current_folder} "" "" "Dbghelp.lib") +ENDIF(WIN32 AND NOT UNIX) + + + + + + diff --git a/Samples/CrossConnectionTest/CMakeLists.txt b/Samples/CrossConnectionTest/CMakeLists.txt index 35cfbdff8..4a37a4041 100644 --- a/Samples/CrossConnectionTest/CMakeLists.txt +++ b/Samples/CrossConnectionTest/CMakeLists.txt @@ -1,8 +1,8 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Internal Tests") - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Internal Tests") + + + + diff --git a/Samples/DirectoryDeltaTransfer/CMakeLists.txt b/Samples/DirectoryDeltaTransfer/CMakeLists.txt index 94ea075ed..bc544abfa 100644 --- a/Samples/DirectoryDeltaTransfer/CMakeLists.txt +++ b/Samples/DirectoryDeltaTransfer/CMakeLists.txt @@ -1,14 +1,14 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() - -project(${current_folder}) -include_directories(${RAKNETHEADERFILES} ./) -add_executable(${current_folder} DirectoryDeltaTransferTest.cpp readme.txt) -target_link_libraries(${current_folder} ${RAKNET_COMMON_LIBS}) -set_target_properties(${current_folder} PROPERTIES PROJECT_GROUP Samples) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() + +project(${current_folder}) +include_directories(${RAKNETHEADERFILES} ./) +add_executable(${current_folder} DirectoryDeltaTransferTest.cpp readme.txt) +target_link_libraries(${current_folder} ${RAKNET_COMMON_LIBS}) +set_target_properties(${current_folder} PROPERTIES PROJECT_GROUP Samples) + + + + + + diff --git a/Samples/Dropped Connection Test/CMakeLists.txt b/Samples/Dropped Connection Test/CMakeLists.txt index dcf912c44..e9b73a0e0 100644 --- a/Samples/Dropped Connection Test/CMakeLists.txt +++ b/Samples/Dropped Connection Test/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(DroppedConnectionTest) -VSUBFOLDER(DroppedConnectionTest "Internal Tests") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(DroppedConnectionTest) +VSUBFOLDER(DroppedConnectionTest "Internal Tests") + + + + + + diff --git a/Samples/Encryption/CMakeLists.txt b/Samples/Encryption/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/Encryption/CMakeLists.txt +++ b/Samples/Encryption/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/FCMHost/CMakeLists.txt b/Samples/FCMHost/CMakeLists.txt index 7b2b62c17..e6c77d71a 100644 --- a/Samples/FCMHost/CMakeLists.txt +++ b/Samples/FCMHost/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Internal Tests") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Internal Tests") + + + + + + diff --git a/Samples/FCMHostSimultaneous/CMakeLists.txt b/Samples/FCMHostSimultaneous/CMakeLists.txt index 7b2b62c17..e6c77d71a 100644 --- a/Samples/FCMHostSimultaneous/CMakeLists.txt +++ b/Samples/FCMHostSimultaneous/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Internal Tests") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Internal Tests") + + + + + + diff --git a/Samples/FCMVerifiedJoinSimultaneous/CMakeLists.txt b/Samples/FCMVerifiedJoinSimultaneous/CMakeLists.txt index 7b2b62c17..e6c77d71a 100644 --- a/Samples/FCMVerifiedJoinSimultaneous/CMakeLists.txt +++ b/Samples/FCMVerifiedJoinSimultaneous/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Internal Tests") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Internal Tests") + + + + + + diff --git a/Samples/FileListTransfer/CMakeLists.txt b/Samples/FileListTransfer/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/FileListTransfer/CMakeLists.txt +++ b/Samples/FileListTransfer/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/Flow Control Test/CMakeLists.txt b/Samples/Flow Control Test/CMakeLists.txt index 1c856f905..99d1a2a5a 100644 --- a/Samples/Flow Control Test/CMakeLists.txt +++ b/Samples/Flow Control Test/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(FlowControlTest) -VSUBFOLDER(FlowControlTest "Internal Tests") - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(FlowControlTest) +VSUBFOLDER(FlowControlTest "Internal Tests") + + + + + diff --git a/Samples/Fully Connected Mesh/CMakeLists.txt b/Samples/Fully Connected Mesh/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/Fully Connected Mesh/CMakeLists.txt +++ b/Samples/Fully Connected Mesh/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/LANServerDiscovery/CMakeLists.txt b/Samples/LANServerDiscovery/CMakeLists.txt index 8174b4532..9ac5f32b3 100644 --- a/Samples/LANServerDiscovery/CMakeLists.txt +++ b/Samples/LANServerDiscovery/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Samples") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Samples") + + + + + + diff --git a/Samples/Lobby2Client/CMakeLists.txt b/Samples/Lobby2Client/CMakeLists.txt index ee9ea5cca..2d50e0ce7 100644 --- a/Samples/Lobby2Client/CMakeLists.txt +++ b/Samples/Lobby2Client/CMakeLists.txt @@ -1,16 +1,16 @@ -cmake_minimum_required(VERSION 2.6) -project(Lobby2Client) -include_directories(${RAKNETHEADERFILES} ./ ${RakNet_SOURCE_DIR}/DependentExtensions/Lobby2 ${RakNet_SOURCE_DIR}/DependentExtensions/Lobby2/Rooms) -FILE(GLOB LOBBYFILES ${RakNet_SOURCE_DIR}/DependentExtensions/Lobby2/*.cpp ${RakNet_SOURCE_DIR}/DependentExtensions/Lobby2/*.h) -SOURCE_GROUP(Main FILES "Lobby2ClientSample.cpp") -add_executable(Lobby2Client "Lobby2ClientSample.cpp" ${LOBBYFILES}) +cmake_minimum_required(VERSION 3.10) +project(Lobby2Client) +include_directories(${RAKNETHEADERFILES} ./ ${RakNet_SOURCE_DIR}/DependentExtensions/Lobby2 ${RakNet_SOURCE_DIR}/DependentExtensions/Lobby2/Rooms) +FILE(GLOB LOBBYFILES ${RakNet_SOURCE_DIR}/DependentExtensions/Lobby2/*.cpp ${RakNet_SOURCE_DIR}/DependentExtensions/Lobby2/*.h) +SOURCE_GROUP(Main FILES "Lobby2ClientSample.cpp") +add_executable(Lobby2Client "Lobby2ClientSample.cpp" ${LOBBYFILES}) target_link_libraries(Lobby2Client ${RAKNET_COMMON_LIBS}) -IF(WIN32 AND NOT UNIX) - VSUBFOLDER(Lobby2Client "Samples/Lobby2") -ENDIF(WIN32 AND NOT UNIX) - - - - - - +IF(WIN32 AND NOT UNIX) + VSUBFOLDER(Lobby2Client "Samples/Lobby2") +ENDIF(WIN32 AND NOT UNIX) + + + + + + diff --git a/Samples/MasterServer/CMakeLists.txt b/Samples/MasterServer/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/MasterServer/CMakeLists.txt +++ b/Samples/MasterServer/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/MessageFilter/CMakeLists.txt b/Samples/MessageFilter/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/MessageFilter/CMakeLists.txt +++ b/Samples/MessageFilter/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/MessageSizeTest/CMakeLists.txt b/Samples/MessageSizeTest/CMakeLists.txt index 7b2b62c17..e6c77d71a 100644 --- a/Samples/MessageSizeTest/CMakeLists.txt +++ b/Samples/MessageSizeTest/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Internal Tests") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Internal Tests") + + + + + + diff --git a/Samples/NATCompleteClient/CMakeLists.txt b/Samples/NATCompleteClient/CMakeLists.txt index 43a9a3b33..33954492b 100644 --- a/Samples/NATCompleteClient/CMakeLists.txt +++ b/Samples/NATCompleteClient/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 3.10) GETCURRENTFOLDER() SET(EXTRALIBS "") #Extra libraries, we have none #note the .h files are included so they show up in the windows project @@ -6,10 +6,10 @@ FILE(GLOB XML_FILES ${RakNet_SOURCE_DIR}/DependentExtensions/XML/*.cpp ${RakNet_ FILE(GLOB UPNP_FILES ${RakNet_SOURCE_DIR}/DependentExtensions/UPNP/*.cpp UPNP_FILES ${RakNet_SOURCE_DIR}/DependentExtensions/UPNP/*.h) #grab all files from UPNP folder and put them in the UPNP_FILES var SET(EXTRASOURCES ${UPNP_FILES} ${XML_FILES}) #set the grabbed files as extra sources SET(EXTRAINCLUDES ${RakNet_SOURCE_DIR}/DependentExtensions/XML UPNP_FILES ${RakNet_SOURCE_DIR}/DependentExtensions/UPNP)#add the include directories -STANDARDSUBPROJECTWITHOPTIONSSET(${current_folder}) -VSUBFOLDER(${current_folder} "Samples/NAT Punchthrough") - - - - - +STANDARDSUBPROJECTWITHOPTIONSSET(${current_folder}) +VSUBFOLDER(${current_folder} "Samples/NAT Punchthrough") + + + + + diff --git a/Samples/NATCompleteServer/CMakeLists.txt b/Samples/NATCompleteServer/CMakeLists.txt index 824b01d0f..145cc5d88 100644 --- a/Samples/NATCompleteServer/CMakeLists.txt +++ b/Samples/NATCompleteServer/CMakeLists.txt @@ -1,3 +1,3 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() STANDARDSUBPROJECT(${current_folder}) \ No newline at end of file diff --git a/Samples/OfflineMessagesTest/CMakeLists.txt b/Samples/OfflineMessagesTest/CMakeLists.txt index 8174b4532..9ac5f32b3 100644 --- a/Samples/OfflineMessagesTest/CMakeLists.txt +++ b/Samples/OfflineMessagesTest/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Samples") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Samples") + + + + + + diff --git a/Samples/PHPDirectoryServer2/CMakeLists.txt b/Samples/PHPDirectoryServer2/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/PHPDirectoryServer2/CMakeLists.txt +++ b/Samples/PHPDirectoryServer2/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/PacketLogger/CMakeLists.txt b/Samples/PacketLogger/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/PacketLogger/CMakeLists.txt +++ b/Samples/PacketLogger/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/Ping/CMakeLists.txt b/Samples/Ping/CMakeLists.txt index 8174b4532..9ac5f32b3 100644 --- a/Samples/Ping/CMakeLists.txt +++ b/Samples/Ping/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Samples") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Samples") + + + + + + diff --git a/Samples/RPC3/CMakeLists.txt b/Samples/RPC3/CMakeLists.txt index 4e2346f38..4f28ad58c 100644 --- a/Samples/RPC3/CMakeLists.txt +++ b/Samples/RPC3/CMakeLists.txt @@ -1,25 +1,25 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -FINDBOOST() - -set(LibRPC3_SOURCE_DIR ${RakNet_SOURCE_DIR}/DependentExtensions/RPC3/) - -project(RPC3) -IF (WIN32 AND NOT UNIX) - SET(PLUGINFILES ${LibRPC3_SOURCE_DIR}/RPC3.cpp ${LibRPC3_SOURCE_DIR}/RPC3.h ${LibRPC3_SOURCE_DIR}/RPC3_Boost.h) - SOURCE_GROUP(Plugin FILES ${PLUGINFILES}) - SOURCE_GROUP(Sample FILES RPC3Sample.cpp) - SET(EXTRALIBS "") - SET(EXTRASOURCES ${PLUGINFILES}) -ELSE(WIN32 AND NOT UNIX) - SET(EXTRALIBS LibRPC3) - SET(EXTRASOURCES "") -ENDIF(WIN32 AND NOT UNIX) -SET(EXTRAINCLUDES ${LibRPC3_SOURCE_DIR} ${Boost_INCLUDE_DIRS}) -STANDARDSUBPROJECTWITHOPTIONSSET(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +FINDBOOST() + +set(LibRPC3_SOURCE_DIR ${RakNet_SOURCE_DIR}/DependentExtensions/RPC3/) + +project(RPC3) +IF (WIN32 AND NOT UNIX) + SET(PLUGINFILES ${LibRPC3_SOURCE_DIR}/RPC3.cpp ${LibRPC3_SOURCE_DIR}/RPC3.h ${LibRPC3_SOURCE_DIR}/RPC3_Boost.h) + SOURCE_GROUP(Plugin FILES ${PLUGINFILES}) + SOURCE_GROUP(Sample FILES RPC3Sample.cpp) + SET(EXTRALIBS "") + SET(EXTRASOURCES ${PLUGINFILES}) +ELSE(WIN32 AND NOT UNIX) + SET(EXTRALIBS LibRPC3) + SET(EXTRASOURCES "") +ENDIF(WIN32 AND NOT UNIX) +SET(EXTRAINCLUDES ${LibRPC3_SOURCE_DIR} ${Boost_INCLUDE_DIRS}) +STANDARDSUBPROJECTWITHOPTIONSSET(${current_folder}) + + + + + + diff --git a/Samples/RPC4/CMakeLists.txt b/Samples/RPC4/CMakeLists.txt index 8a253caa2..82323e186 100644 --- a/Samples/RPC4/CMakeLists.txt +++ b/Samples/RPC4/CMakeLists.txt @@ -1,8 +1,8 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Samples") - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Samples") + + + + diff --git a/Samples/RackspaceConsole/CMakeLists.txt b/Samples/RackspaceConsole/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/RackspaceConsole/CMakeLists.txt +++ b/Samples/RackspaceConsole/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/RakVoice/CMakeLists.txt b/Samples/RakVoice/CMakeLists.txt index b8fa6bf17..35024bc24 100644 --- a/Samples/RakVoice/CMakeLists.txt +++ b/Samples/RakVoice/CMakeLists.txt @@ -1,33 +1,33 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -FINDPORTAUDIO() -project(${current_folder}) - -IF(WIN32 AND NOT UNIX) - FILE(GLOB ALL_CPP_SRCS *.cpp ${RakNet_SOURCE_DIR}/DependentExtensions/RakVoice.cpp) - FILE(GLOB ALL_HEADER_SRCS *.h ${RakNet_SOURCE_DIR}/DependentExtensions/RakVoice.h) - FILE(GLOB PORTAUDIOFILES ${portaudio_SOURCE_DIR}/pa_common/*.c ${portaudio_SOURCE_DIR}/pa_win_wmme/pa_win_wmme.c ${portaudio_SOURCE_DIR}/pa_common/*.h) - FILE(GLOB SPEEXFILES ${speex_SOURCE_DIR}/win32/*.h ${speex_SOURCE_DIR}/include/*.h ${speex_SOURCE_DIR}/libspeex/*.h ${speex_SOURCE_DIR}/include/speex/*.h ${speex_SOURCE_DIR}/libspeex/*.c) - LIST(REMOVE_ITEM SPEEXFILES - ${speex_SOURCE_DIR}/libspeex/pcm_wrapper.h) - LIST(REMOVE_ITEM SPEEXFILES - ${speex_SOURCE_DIR}/libspeex/pcm_wrapper.c) - SOURCE_GROUP(Speex FILES ${SPEEXFILES}) - SOURCE_GROUP(PortAudio FILES ${PORTAUDIOFILES}) - ADDCPPDEF(HAVE_CONFIG_H) - include_directories(${RAKNETHEADERFILES} ./ ${PORTAUDIO_INCLUDE_DIR} ${RakNet_SOURCE_DIR}/DependentExtensions ${speex_SOURCE_DIR}/include ${portaudio_SOURCE_DIR} ${speex_SOURCE_DIR}/win32) - add_executable(${current_folder} ${ALL_CPP_SRCS} ${ALL_HEADER_SRCS} ${PORTAUDIOFILES} ${SPEEXFILES} readme.txt) - target_link_libraries(${current_folder} ${RAKNET_COMMON_LIBS}) - VSUBFOLDER(${current_folder} Samples/Voice) -ELSE(WIN32 AND NOT UNIX) - FILE(GLOB ALL_CPP_SRCS *.cpp) - FILE(GLOB ALL_HEADER_SRCS *.h) - include_directories(${RAKNETHEADERFILES} ./ ${PORTAUDIO_INCLUDE_DIR} ${RakNet_SOURCE_DIR}/DependentExtensions) - add_executable(${current_folder} ${ALL_CPP_SRCS} ${ALL_HEADER_SRCS}) - target_link_libraries(${current_folder} ${RAKNET_COMMON_LIBS} ${PORTAUDIO_LIBRARIES} LibRakVoice) -ENDIF(WIN32 AND NOT UNIX) - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +FINDPORTAUDIO() +project(${current_folder}) + +IF(WIN32 AND NOT UNIX) + FILE(GLOB ALL_CPP_SRCS *.cpp ${RakNet_SOURCE_DIR}/DependentExtensions/RakVoice.cpp) + FILE(GLOB ALL_HEADER_SRCS *.h ${RakNet_SOURCE_DIR}/DependentExtensions/RakVoice.h) + FILE(GLOB PORTAUDIOFILES ${portaudio_SOURCE_DIR}/pa_common/*.c ${portaudio_SOURCE_DIR}/pa_win_wmme/pa_win_wmme.c ${portaudio_SOURCE_DIR}/pa_common/*.h) + FILE(GLOB SPEEXFILES ${speex_SOURCE_DIR}/win32/*.h ${speex_SOURCE_DIR}/include/*.h ${speex_SOURCE_DIR}/libspeex/*.h ${speex_SOURCE_DIR}/include/speex/*.h ${speex_SOURCE_DIR}/libspeex/*.c) + LIST(REMOVE_ITEM SPEEXFILES + ${speex_SOURCE_DIR}/libspeex/pcm_wrapper.h) + LIST(REMOVE_ITEM SPEEXFILES + ${speex_SOURCE_DIR}/libspeex/pcm_wrapper.c) + SOURCE_GROUP(Speex FILES ${SPEEXFILES}) + SOURCE_GROUP(PortAudio FILES ${PORTAUDIOFILES}) + ADDCPPDEF(HAVE_CONFIG_H) + include_directories(${RAKNETHEADERFILES} ./ ${PORTAUDIO_INCLUDE_DIR} ${RakNet_SOURCE_DIR}/DependentExtensions ${speex_SOURCE_DIR}/include ${portaudio_SOURCE_DIR} ${speex_SOURCE_DIR}/win32) + add_executable(${current_folder} ${ALL_CPP_SRCS} ${ALL_HEADER_SRCS} ${PORTAUDIOFILES} ${SPEEXFILES} readme.txt) + target_link_libraries(${current_folder} ${RAKNET_COMMON_LIBS}) + VSUBFOLDER(${current_folder} Samples/Voice) +ELSE(WIN32 AND NOT UNIX) + FILE(GLOB ALL_CPP_SRCS *.cpp) + FILE(GLOB ALL_HEADER_SRCS *.h) + include_directories(${RAKNETHEADERFILES} ./ ${PORTAUDIO_INCLUDE_DIR} ${RakNet_SOURCE_DIR}/DependentExtensions) + add_executable(${current_folder} ${ALL_CPP_SRCS} ${ALL_HEADER_SRCS}) + target_link_libraries(${current_folder} ${RAKNET_COMMON_LIBS} ${PORTAUDIO_LIBRARIES} LibRakVoice) +ENDIF(WIN32 AND NOT UNIX) + + + + + diff --git a/Samples/RakVoiceDSound/CMakeLists.txt b/Samples/RakVoiceDSound/CMakeLists.txt index 666d41346..69436c0aa 100644 --- a/Samples/RakVoiceDSound/CMakeLists.txt +++ b/Samples/RakVoiceDSound/CMakeLists.txt @@ -1,30 +1,30 @@ -cmake_minimum_required(VERSION 2.6) - - -IF(WIN32 AND NOT UNIX) - GETCURRENTFOLDER() - FINDPORTAUDIO() - FINDDSOUND() - project(${current_folder}) - FILE(GLOB ALL_CPP_SRCS *.cpp ${RakNet_SOURCE_DIR}/DependentExtensions/RakVoice.cpp) - FILE(GLOB ALL_HEADER_SRCS *.h ${RakNet_SOURCE_DIR}/DependentExtensions/RakVoice.h) - FILE(GLOB SPEEXFILES ${speex_SOURCE_DIR}/win32/*.h ${speex_SOURCE_DIR}/include/*.h ${speex_SOURCE_DIR}/libspeex/*.h ${speex_SOURCE_DIR}/include/speex/*.h ${speex_SOURCE_DIR}/libspeex/*.c) - FINDREADMES() - LIST(REMOVE_ITEM SPEEXFILES - ${speex_SOURCE_DIR}/libspeex/pcm_wrapper.h) - LIST(REMOVE_ITEM SPEEXFILES - ${speex_SOURCE_DIR}/libspeex/pcm_wrapper.c) - SOURCE_GROUP(Speex FILES ${SPEEXFILES}) - ADDCPPDEF(HAVE_CONFIG_H) - ADDCPPDEF(_UNICODE) - ADDCPPDEF(UNICODE) - include_directories(${RAKNETHEADERFILES} ./ ${PORTAUDIO_INCLUDE_DIR} ${RakNet_SOURCE_DIR}/DependentExtensions ${speex_SOURCE_DIR}/include ${portaudio_SOURCE_DIR} ${speex_SOURCE_DIR}/win32 ${DSOUND_INCLUDE_DIR}) - add_executable(${current_folder} ${ALL_CPP_SRCS} ${ALL_HEADER_SRCS} ${SPEEXFILES} ${READMES}) - target_link_libraries(${current_folder} ${RAKNET_COMMON_LIBS} winmm.lib ${DSOUND_LIBRARIES}) - VSUBFOLDER(${current_folder} Samples/Voice) -ENDIF(WIN32 AND NOT UNIX) - - - - - +cmake_minimum_required(VERSION 3.10) + + +IF(WIN32 AND NOT UNIX) + GETCURRENTFOLDER() + FINDPORTAUDIO() + FINDDSOUND() + project(${current_folder}) + FILE(GLOB ALL_CPP_SRCS *.cpp ${RakNet_SOURCE_DIR}/DependentExtensions/RakVoice.cpp) + FILE(GLOB ALL_HEADER_SRCS *.h ${RakNet_SOURCE_DIR}/DependentExtensions/RakVoice.h) + FILE(GLOB SPEEXFILES ${speex_SOURCE_DIR}/win32/*.h ${speex_SOURCE_DIR}/include/*.h ${speex_SOURCE_DIR}/libspeex/*.h ${speex_SOURCE_DIR}/include/speex/*.h ${speex_SOURCE_DIR}/libspeex/*.c) + FINDREADMES() + LIST(REMOVE_ITEM SPEEXFILES + ${speex_SOURCE_DIR}/libspeex/pcm_wrapper.h) + LIST(REMOVE_ITEM SPEEXFILES + ${speex_SOURCE_DIR}/libspeex/pcm_wrapper.c) + SOURCE_GROUP(Speex FILES ${SPEEXFILES}) + ADDCPPDEF(HAVE_CONFIG_H) + ADDCPPDEF(_UNICODE) + ADDCPPDEF(UNICODE) + include_directories(${RAKNETHEADERFILES} ./ ${PORTAUDIO_INCLUDE_DIR} ${RakNet_SOURCE_DIR}/DependentExtensions ${speex_SOURCE_DIR}/include ${portaudio_SOURCE_DIR} ${speex_SOURCE_DIR}/win32 ${DSOUND_INCLUDE_DIR}) + add_executable(${current_folder} ${ALL_CPP_SRCS} ${ALL_HEADER_SRCS} ${SPEEXFILES} ${READMES}) + target_link_libraries(${current_folder} ${RAKNET_COMMON_LIBS} winmm.lib ${DSOUND_LIBRARIES}) + VSUBFOLDER(${current_folder} Samples/Voice) +ENDIF(WIN32 AND NOT UNIX) + + + + + diff --git a/Samples/RakVoiceFMOD/CMakeLists.txt b/Samples/RakVoiceFMOD/CMakeLists.txt index b373894b8..f7f1e7031 100644 --- a/Samples/RakVoiceFMOD/CMakeLists.txt +++ b/Samples/RakVoiceFMOD/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 3.10) GETCURRENTFOLDER() FINDPORTAUDIO() FINDFMODEX() diff --git a/Samples/RelayPluginTest/CMakeLists.txt b/Samples/RelayPluginTest/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/RelayPluginTest/CMakeLists.txt +++ b/Samples/RelayPluginTest/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/Reliable Ordered Test/CMakeLists.txt b/Samples/Reliable Ordered Test/CMakeLists.txt index 17d7d2152..ad17344e4 100644 --- a/Samples/Reliable Ordered Test/CMakeLists.txt +++ b/Samples/Reliable Ordered Test/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(ReliableOrderedTest) -VSUBFOLDER(ReliableOrderedTest "Internal Tests") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(ReliableOrderedTest) +VSUBFOLDER(ReliableOrderedTest "Internal Tests") + + + + + + diff --git a/Samples/ReplicaManager3/CMakeLists.txt b/Samples/ReplicaManager3/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/ReplicaManager3/CMakeLists.txt +++ b/Samples/ReplicaManager3/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/Router2/CMakeLists.txt b/Samples/Router2/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/Router2/CMakeLists.txt +++ b/Samples/Router2/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/SendEmail/CMakeLists.txt b/Samples/SendEmail/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/SendEmail/CMakeLists.txt +++ b/Samples/SendEmail/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/ServerClientTest2/CMakeLists.txt b/Samples/ServerClientTest2/CMakeLists.txt index 7b2b62c17..e6c77d71a 100644 --- a/Samples/ServerClientTest2/CMakeLists.txt +++ b/Samples/ServerClientTest2/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Internal Tests") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Internal Tests") + + + + + + diff --git a/Samples/StatisticsHistoryTest/CMakeLists.txt b/Samples/StatisticsHistoryTest/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/StatisticsHistoryTest/CMakeLists.txt +++ b/Samples/StatisticsHistoryTest/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/TeamManager/CMakeLists.txt b/Samples/TeamManager/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/TeamManager/CMakeLists.txt +++ b/Samples/TeamManager/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/TestDLL/CMakeLists.txt b/Samples/TestDLL/CMakeLists.txt index d49812301..485d6a86c 100644 --- a/Samples/TestDLL/CMakeLists.txt +++ b/Samples/TestDLL/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Internal Tests") - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Internal Tests") + + + + + diff --git a/Samples/Tests/CMakeLists.txt b/Samples/Tests/CMakeLists.txt index d49812301..485d6a86c 100644 --- a/Samples/Tests/CMakeLists.txt +++ b/Samples/Tests/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Internal Tests") - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Internal Tests") + + + + + diff --git a/Samples/ThreadTest/CMakeLists.txt b/Samples/ThreadTest/CMakeLists.txt index 7b2b62c17..e6c77d71a 100644 --- a/Samples/ThreadTest/CMakeLists.txt +++ b/Samples/ThreadTest/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Internal Tests") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Internal Tests") + + + + + + diff --git a/Samples/Timestamping/CMakeLists.txt b/Samples/Timestamping/CMakeLists.txt index 8174b4532..9ac5f32b3 100644 --- a/Samples/Timestamping/CMakeLists.txt +++ b/Samples/Timestamping/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Samples") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Samples") + + + + + + diff --git a/Samples/TitleValidationDB_PostgreSQL/CMakeLists.txt b/Samples/TitleValidationDB_PostgreSQL/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/TitleValidationDB_PostgreSQL/CMakeLists.txt +++ b/Samples/TitleValidationDB_PostgreSQL/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/TwoWayAuthentication/CMakeLists.txt b/Samples/TwoWayAuthentication/CMakeLists.txt index 30ed82149..db44338bd 100644 --- a/Samples/TwoWayAuthentication/CMakeLists.txt +++ b/Samples/TwoWayAuthentication/CMakeLists.txt @@ -1,9 +1,9 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) + + + + + + diff --git a/Samples/UDPForwarder/CMakeLists.txt b/Samples/UDPForwarder/CMakeLists.txt index c1b03be4f..bb9998d79 100644 --- a/Samples/UDPForwarder/CMakeLists.txt +++ b/Samples/UDPForwarder/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 2.6) -GETCURRENTFOLDER() -STANDARDSUBPROJECT(${current_folder}) -VSUBFOLDER(${current_folder} "Samples/NAT Punchthrough") - - - - - - +cmake_minimum_required(VERSION 3.10) +GETCURRENTFOLDER() +STANDARDSUBPROJECT(${current_folder}) +VSUBFOLDER(${current_folder} "Samples/NAT Punchthrough") + + + + + + diff --git a/Source/AutopatcherPatchContext.h b/Source/AutopatcherPatchContext.h index aed8c3e20..3591e8e22 100644 --- a/Source/AutopatcherPatchContext.h +++ b/Source/AutopatcherPatchContext.h @@ -1,28 +1,26 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __AUTOPATCHER_PATCH_CONTEXT_H -#define __AUTOPATCHER_PATCH_CONTEXT_H - -enum PatchContext -{ - PC_HASH_1_WITH_PATCH, - PC_HASH_2_WITH_PATCH, - PC_WRITE_FILE, - PC_ERROR_FILE_WRITE_FAILURE, - PC_ERROR_PATCH_TARGET_MISSING, - PC_ERROR_PATCH_APPLICATION_FAILURE, - PC_ERROR_PATCH_RESULT_CHECKSUM_FAILURE, - PC_NOTICE_WILL_COPY_ON_RESTART, - PC_NOTICE_FILE_DOWNLOADED, - PC_NOTICE_FILE_DOWNLOADED_PATCH, -}; - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +enum PatchContext +{ + PC_HASH_1_WITH_PATCH, + PC_HASH_2_WITH_PATCH, + PC_WRITE_FILE, + PC_ERROR_FILE_WRITE_FAILURE, + PC_ERROR_PATCH_TARGET_MISSING, + PC_ERROR_PATCH_APPLICATION_FAILURE, + PC_ERROR_PATCH_RESULT_CHECKSUM_FAILURE, + PC_NOTICE_WILL_COPY_ON_RESTART, + PC_NOTICE_FILE_DOWNLOADED, + PC_NOTICE_FILE_DOWNLOADED_PATCH, +}; + diff --git a/Source/AutopatcherRepositoryInterface.h b/Source/AutopatcherRepositoryInterface.h index 9bec896db..ebbcdcbd8 100644 --- a/Source/AutopatcherRepositoryInterface.h +++ b/Source/AutopatcherRepositoryInterface.h @@ -1,77 +1,74 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// -/// \file AutopatcherRepositoryInterface.h -/// \brief An interface used by AutopatcherServer to get the data necessary to run an autopatcher. -/// - - -#ifndef __AUTOPATCHER_REPOSITORY_INTERFACE_H -#define __AUTOPATCHER_REPOSITORY_INTERFACE_H - -#include "IncrementalReadInterface.h" -#include "SimpleMutex.h" - -namespace RakNet -{ -/// Forward declarations -class FileList; -class BitStream; - -/// An interface used by AutopatcherServer to get the data necessary to run an autopatcher. This is up to you to implement for custom repository solutions. -class AutopatcherRepositoryInterface : public IncrementalReadInterface -{ -public: - /// Get list of files added and deleted since a certain date. This is used by AutopatcherServer and not usually explicitly called. - /// \param[in] applicationName A null terminated string identifying the application - /// \param[out] addedFiles A list of the current versions of filenames with hashes as their data that were created after \a sinceData - /// \param[out] deletedFiles A list of the current versions of filenames that were deleted after \a sinceData - /// \param[in] An input date, in whatever format your repository uses - /// \param[out] currentDate The current server date, in whatever format your repository uses - /// \return True on success, false on failure. - virtual bool GetChangelistSinceDate(const char *applicationName, FileList *addedOrModifiedFilesWithHashData, FileList *deletedFiles, double sinceDate)=0; - - /// Get patches (or files) for every file in input, assuming that input has a hash for each of those files. - /// \param[in] applicationName A null terminated string identifying the application - /// \param[in] input A list of files with SHA1_LENGTH byte hashes to get from the database. - /// \param[out] patchList You should return list of files with either the filedata or the patch. This is a subset of \a input. The context data for each file will be either PC_WRITE_FILE (to just write the file) or PC_HASH_WITH_PATCH (to patch). If PC_HASH_WITH_PATCH, then the file contains a SHA1_LENGTH byte patch followed by the hash. The datalength is patchlength + SHA1_LENGTH - /// \param[out] currentDate The current server date, in whatever format your repository uses - /// \return 1 on success, 0 on database failure, -1 on tried to download original unmodified file - virtual int GetPatches(const char *applicationName, FileList *input, bool allowDownloadOfOriginalUnmodifiedFiles, FileList *patchList)=0; - - /// For the most recent update, return files that were patched, added, or deleted. For files that were patched, return both the patch in \a patchedFiles and the current version in \a updatedFiles - /// \param[in,out] applicationName Name of the application to get patches for. If empty, uses the most recently updated application, and the string will be updated to reflect this name. - /// \param[out] patchedFiles A list of patched files with op PC_HASH_2_WITH_PATCH. It has 2 hashes, the priorHash and the currentHash. The currentHash is checked on the client after patching for patch success. The priorHash is checked in AutopatcherServer::OnGetPatch() to see if the client is able to hash with the version they currently have - /// \param[out] patchedFiles A list of new files. It contains the actual data in addition to the filename - /// \param[out] addedOrModifiedFileHashes A list of file hashes that were either modified or new. This is returned to the client when replying to ID_AUTOPATCHER_CREATION_LIST, which tells the client what files have changed on the server since a certain date - /// \param[out] deletedFiles A list of the current versions of filenames that were deleted in the most recent patch - /// \param[out] whenPatched time in seconds since epoch when patched. Use time() function to get this in C - /// \return true on success, false on failure - virtual bool GetMostRecentChangelistWithPatches( - RakNet::RakString &applicationName, - FileList *patchedFiles, - FileList *updatedFiles, - FileList *addedOrModifiedFileHashes, - FileList *deletedFiles, - double *priorRowPatchTime, - double *mostRecentRowPatchTime)=0; - - /// \return Whatever this function returns is sent from the AutopatcherServer to the AutopatcherClient when one of the above functions returns false. - virtual const char *GetLastError(void) const=0; - - /// \return Passed to FileListTransfer::Send() as the _chunkSize parameter. - virtual const int GetIncrementalReadChunkSize(void) const=0; -}; - -} // namespace RakNet - -#endif - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// +/// \file AutopatcherRepositoryInterface.h +/// \brief An interface used by AutopatcherServer to get the data necessary to run an autopatcher. +/// + + +#pragma once + +#include "IncrementalReadInterface.h" +#include "SimpleMutex.h" + +namespace RakNet +{ +/// Forward declarations +class FileList; +class BitStream; + +/// An interface used by AutopatcherServer to get the data necessary to run an autopatcher. This is up to you to implement for custom repository solutions. +class AutopatcherRepositoryInterface : public IncrementalReadInterface +{ +public: + /// Get list of files added and deleted since a certain date. This is used by AutopatcherServer and not usually explicitly called. + /// \param[in] applicationName A null terminated string identifying the application + /// \param[out] addedFiles A list of the current versions of filenames with hashes as their data that were created after \a sinceData + /// \param[out] deletedFiles A list of the current versions of filenames that were deleted after \a sinceData + /// \param[in] An input date, in whatever format your repository uses + /// \param[out] currentDate The current server date, in whatever format your repository uses + /// \return True on success, false on failure. + virtual bool GetChangelistSinceDate(const char *applicationName, FileList *addedOrModifiedFilesWithHashData, FileList *deletedFiles, double sinceDate)=0; + + /// Get patches (or files) for every file in input, assuming that input has a hash for each of those files. + /// \param[in] applicationName A null terminated string identifying the application + /// \param[in] input A list of files with SHA1_LENGTH byte hashes to get from the database. + /// \param[out] patchList You should return list of files with either the filedata or the patch. This is a subset of \a input. The context data for each file will be either PC_WRITE_FILE (to just write the file) or PC_HASH_WITH_PATCH (to patch). If PC_HASH_WITH_PATCH, then the file contains a SHA1_LENGTH byte patch followed by the hash. The datalength is patchlength + SHA1_LENGTH + /// \param[out] currentDate The current server date, in whatever format your repository uses + /// \return 1 on success, 0 on database failure, -1 on tried to download original unmodified file + virtual int GetPatches(const char *applicationName, FileList *input, bool allowDownloadOfOriginalUnmodifiedFiles, FileList *patchList)=0; + + /// For the most recent update, return files that were patched, added, or deleted. For files that were patched, return both the patch in \a patchedFiles and the current version in \a updatedFiles + /// \param[in,out] applicationName Name of the application to get patches for. If empty, uses the most recently updated application, and the string will be updated to reflect this name. + /// \param[out] patchedFiles A list of patched files with op PC_HASH_2_WITH_PATCH. It has 2 hashes, the priorHash and the currentHash. The currentHash is checked on the client after patching for patch success. The priorHash is checked in AutopatcherServer::OnGetPatch() to see if the client is able to hash with the version they currently have + /// \param[out] patchedFiles A list of new files. It contains the actual data in addition to the filename + /// \param[out] addedOrModifiedFileHashes A list of file hashes that were either modified or new. This is returned to the client when replying to ID_AUTOPATCHER_CREATION_LIST, which tells the client what files have changed on the server since a certain date + /// \param[out] deletedFiles A list of the current versions of filenames that were deleted in the most recent patch + /// \param[out] whenPatched time in seconds since epoch when patched. Use time() function to get this in C + /// \return true on success, false on failure + virtual bool GetMostRecentChangelistWithPatches( + RakNet::RakString &applicationName, + FileList *patchedFiles, + FileList *updatedFiles, + FileList *addedOrModifiedFileHashes, + FileList *deletedFiles, + double *priorRowPatchTime, + double *mostRecentRowPatchTime)=0; + + /// \return Whatever this function returns is sent from the AutopatcherServer to the AutopatcherClient when one of the above functions returns false. + virtual const char *GetLastError(void) const=0; + + /// \return Passed to FileListTransfer::Send() as the _chunkSize parameter. + virtual const int GetIncrementalReadChunkSize(void) const=0; +}; + +} // namespace RakNet + diff --git a/Source/Base64Encoder.h b/Source/Base64Encoder.h index d9fb3e679..1320c80e5 100644 --- a/Source/Base64Encoder.h +++ b/Source/Base64Encoder.h @@ -1,26 +1,24 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __BASE_64_ENCODER_H -#define __BASE_64_ENCODER_H - -#include "Export.h" - -extern "C" { -/// \brief Returns how many bytes were written. -// outputData should be at least the size of inputData * 2 + 6 -int Base64Encoding(const unsigned char *inputData, int dataLength, char *outputData); -} - -extern "C" { -const char *Base64Map(void); -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include "Export.h" + +extern "C" { +/// \brief Returns how many bytes were written. +// outputData should be at least the size of inputData * 2 + 6 +int Base64Encoding(const unsigned char *inputData, int dataLength, char *outputData); +} + +extern "C" { +const char *Base64Map(void); +} + diff --git a/Source/BitStream.h b/Source/BitStream.h index ccb2e5d0c..bcf81aecf 100644 --- a/Source/BitStream.h +++ b/Source/BitStream.h @@ -1,2048 +1,2038 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file BitStream.h -/// \brief This class allows you to write and read native types as a string of bits. -/// \details BitStream is used extensively throughout RakNet and is designed to be used by users as well. -/// - - -#if defined(_MSC_VER) && _MSC_VER < 1299 // VC6 doesn't support template specialization -#include "BitStream_NoTemplate.h" -#else - -#ifndef __BITSTREAM_H -#define __BITSTREAM_H - -#include "RakMemoryOverride.h" -#include "RakNetDefines.h" -#include "Export.h" -#include "RakNetTypes.h" -#include "RakString.h" -#include "RakWString.h" -#include "RakAssert.h" -#include -#include - -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -// MSWin uses _copysign, others use copysign... -#ifndef _WIN32 -#define _copysign copysign -#endif - -namespace RakNet -{ - /// This class allows you to write and read native types as a string of bits. BitStream is used extensively throughout RakNet and is designed to be used by users as well. - /// \sa BitStreamSample.txt - class RAK_DLL_EXPORT BitStream - { - - public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(BitStream) - - /// Default Constructor - BitStream(); - - /// \brief Create the bitstream, with some number of bytes to immediately allocate. - /// \details There is no benefit to calling this, unless you know exactly how many bytes you need and it is greater than BITSTREAM_STACK_ALLOCATION_SIZE. - /// In that case all it does is save you one or more realloc calls. - /// \param[in] initialBytesToAllocate the number of bytes to pre-allocate. - BitStream( const unsigned int initialBytesToAllocate ); - - /// \brief Initialize the BitStream, immediately setting the data it contains to a predefined pointer. - /// \details Set \a _copyData to true if you want to make an internal copy of the data you are passing. Set it to false to just save a pointer to the data. - /// You shouldn't call Write functions with \a _copyData as false, as this will write to unallocated memory - /// 99% of the time you will use this function to cast Packet::data to a bitstream for reading, in which case you should write something as follows: - /// \code - /// RakNet::BitStream bs(packet->data, packet->length, false); - /// \endcode - /// \param[in] _data An array of bytes. - /// \param[in] lengthInBytes Size of the \a _data. - /// \param[in] _copyData true or false to make a copy of \a _data or not. - BitStream( unsigned char* _data, const unsigned int lengthInBytes, bool _copyData ); - - // Destructor - ~BitStream(); - - /// Resets the bitstream for reuse. - void Reset( void ); - - /// \brief Bidirectional serialize/deserialize any integral type to/from a bitstream. - /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. - /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data - /// \param[in] inOutTemplateVar The value to write - /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. - template - bool Serialize(bool writeToBitstream, templateType &inOutTemplateVar); - - /// \brief Bidirectional serialize/deserialize any integral type to/from a bitstream. - /// \details If the current value is different from the last value - /// the current value will be written. Otherwise, a single bit will be written - /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data - /// \param[in] inOutCurrentValue The current value to write - /// \param[in] lastValue The last value to compare against. Only used if \a writeToBitstream is true. - /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. - template - bool SerializeDelta(bool writeToBitstream, templateType &inOutCurrentValue, const templateType &lastValue); - - /// \brief Bidirectional version of SerializeDelta when you don't know what the last value is, or there is no last value. - /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data - /// \param[in] inOutCurrentValue The current value to write - /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. - template - bool SerializeDelta(bool writeToBitstream, templateType &inOutCurrentValue); - - /// \brief Bidirectional serialize/deserialize any integral type to/from a bitstream. - /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. - /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte - /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. - /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type - /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data - /// \param[in] inOutTemplateVar The value to write - /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. - template - bool SerializeCompressed(bool writeToBitstream, templateType &inOutTemplateVar); - - /// \brief Bidirectional serialize/deserialize any integral type to/from a bitstream. - /// \details If the current value is different from the last value - /// the current value will be written. Otherwise, a single bit will be written - /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. - /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type - /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte - /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data - /// \param[in] inOutCurrentValue The current value to write - /// \param[in] lastValue The last value to compare against. Only used if \a writeToBitstream is true. - /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. - template - bool SerializeCompressedDelta(bool writeToBitstream, templateType &inOutCurrentValue, const templateType &lastValue); - - /// \brief Save as SerializeCompressedDelta(templateType ¤tValue, const templateType &lastValue) when we have an unknown second parameter - /// \return true on data read. False on insufficient data in bitstream - template - bool SerializeCompressedDelta(bool writeToBitstream, templateType &inOutTemplateVar); - - /// \brief Bidirectional serialize/deserialize an array or casted stream or raw data. This does NOT do endian swapping. - /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data - /// \param[in] inOutByteArray a byte buffer - /// \param[in] numberOfBytes the size of \a input in bytes - /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. - bool Serialize(bool writeToBitstream, char* inOutByteArray, const unsigned int numberOfBytes ); - - /// \brief Serialize a float into 2 bytes, spanning the range between \a floatMin and \a floatMax - /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data - /// \param[in] inOutFloat The float to write - /// \param[in] floatMin Predetermined minimum value of f - /// \param[in] floatMax Predetermined maximum value of f - bool SerializeFloat16(bool writeToBitstream, float &inOutFloat, float floatMin, float floatMax); - - /// Serialize one type casted to another (smaller) type, to save bandwidth - /// serializationType should be uint8_t, uint16_t, uint24_t, or uint32_t - /// Example: int num=53; SerializeCasted(true, num); would use 1 byte to write what would otherwise be an integer (4 or 8 bytes) - /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data - /// \param[in] value The value to serialize - template - bool SerializeCasted( bool writeToBitstream, sourceType &value ); - - /// Given the minimum and maximum values for an integer type, figure out the minimum number of bits to represent the range - /// Then serialize only those bits - /// \note A static is used so that the required number of bits for (maximum-minimum) is only calculated once. This does require that \a minimum and \maximum are fixed values for a given line of code for the life of the program - /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data - /// \param[in] value Integer value to write, which should be between \a minimum and \a maximum - /// \param[in] minimum Minimum value of \a value - /// \param[in] maximum Maximum value of \a value - /// \param[in] allowOutsideRange If true, all sends will take an extra bit, however value can deviate from outside \a minimum and \a maximum. If false, will assert if the value deviates - template - bool SerializeBitsFromIntegerRange( bool writeToBitstream, templateType &value, const templateType minimum, const templateType maximum, bool allowOutsideRange=false ); - /// \param[in] requiredBits Primarily for internal use, called from above function() after calculating number of bits needed to represent maximum-minimum - template - bool SerializeBitsFromIntegerRange( bool writeToBitstream, templateType &value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange=false ); - - /// \brief Bidirectional serialize/deserialize a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes. - /// \details Will further compress y or z axis aligned vectors. - /// Accurate to 1/32767.5. - /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data - /// \param[in] x x - /// \param[in] y y - /// \param[in] z z - /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. - template // templateType for this function must be a float or double - bool SerializeNormVector(bool writeToBitstream, templateType &x, templateType &y, templateType &z ); - - /// \brief Bidirectional serialize/deserialize a vector, using 10 bytes instead of 12. - /// \details Loses accuracy to about 3/10ths and only saves 2 bytes, so only use if accuracy is not important. - /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data - /// \param[in] x x - /// \param[in] y y - /// \param[in] z z - /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. - template // templateType for this function must be a float or double - bool SerializeVector(bool writeToBitstream, templateType &x, templateType &y, templateType &z ); - - /// \brief Bidirectional serialize/deserialize a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. Slightly lossy. - /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data - /// \param[in] w w - /// \param[in] x x - /// \param[in] y y - /// \param[in] z z - /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. - template // templateType for this function must be a float or double - bool SerializeNormQuat(bool writeToBitstream, templateType &w, templateType &x, templateType &y, templateType &z); - - /// \brief Bidirectional serialize/deserialize an orthogonal matrix by creating a quaternion, and writing 3 components of the quaternion in 2 bytes each. - /// \details Use 6 bytes instead of 36 - /// Lossy, although the result is renormalized - /// \return true on success, false on failure. - template // templateType for this function must be a float or double - bool SerializeOrthMatrix( - bool writeToBitstream, - templateType &m00, templateType &m01, templateType &m02, - templateType &m10, templateType &m11, templateType &m12, - templateType &m20, templateType &m21, templateType &m22 ); - - /// \brief Bidirectional serialize/deserialize numberToSerialize bits to/from the input. - /// \details Right aligned data means in the case of a partial byte, the bits are aligned - /// from the right (bit 0) rather than the left (as in the normal - /// internal representation) You would set this to true when - /// writing user data, and false when copying bitstream data, such - /// as writing one bitstream to another - /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data - /// \param[in] inOutByteArray The data - /// \param[in] numberOfBitsToSerialize The number of bits to write - /// \param[in] rightAlignedBits if true data will be right aligned - /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. - bool SerializeBits(bool writeToBitstream, unsigned char* inOutByteArray, const BitSize_t numberOfBitsToSerialize, const bool rightAlignedBits = true ); - - /// \brief Write any integral type to a bitstream. - /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. - /// \param[in] inTemplateVar The value to write - template - void Write(const templateType &inTemplateVar); - - /// \brief Write the dereferenced pointer to any integral type to a bitstream. - /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. - /// \param[in] inTemplateVar The value to write - template - void WritePtr(templateType *inTemplateVar); - - /// \brief Write any integral type to a bitstream. - /// \details If the current value is different from the last value - /// the current value will be written. Otherwise, a single bit will be written - /// \param[in] currentValue The current value to write - /// \param[in] lastValue The last value to compare against - template - void WriteDelta(const templateType ¤tValue, const templateType &lastValue); - - /// \brief WriteDelta when you don't know what the last value is, or there is no last value. - /// \param[in] currentValue The current value to write - template - void WriteDelta(const templateType ¤tValue); - - /// \brief Write any integral type to a bitstream. - /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. - /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte - /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. - /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type - /// \param[in] inTemplateVar The value to write - template - void WriteCompressed(const templateType &inTemplateVar); - - /// \brief Write any integral type to a bitstream. - /// \details If the current value is different from the last value - /// the current value will be written. Otherwise, a single bit will be written - /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. - /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type - /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte - /// \param[in] currentValue The current value to write - /// \param[in] lastValue The last value to compare against - template - void WriteCompressedDelta(const templateType ¤tValue, const templateType &lastValue); - - /// \brief Save as WriteCompressedDelta(const templateType ¤tValue, const templateType &lastValue) when we have an unknown second parameter - template - void WriteCompressedDelta(const templateType ¤tValue); - - /// \brief Read any integral type from a bitstream. - /// \details Define __BITSTREAM_NATIVE_END if you need endian swapping. - /// \param[in] outTemplateVar The value to read - /// \return true on success, false on failure. - template - bool Read(templateType &outTemplateVar); - - /// \brief Read any integral type from a bitstream. - /// \details If the written value differed from the value compared against in the write function, - /// var will be updated. Otherwise it will retain the current value. - /// ReadDelta is only valid from a previous call to WriteDelta - /// \param[in] outTemplateVar The value to read - /// \return true on success, false on failure. - template - bool ReadDelta(templateType &outTemplateVar); - - /// \brief Read any integral type from a bitstream. - /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. - /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. - /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type - /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte - /// \param[in] outTemplateVar The value to read - /// \return true on success, false on failure. - template - bool ReadCompressed(templateType &outTemplateVar); - - /// \brief Read any integral type from a bitstream. - /// \details If the written value differed from the value compared against in the write function, - /// var will be updated. Otherwise it will retain the current value. - /// the current value will be updated. - /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. - /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type - /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte - /// ReadCompressedDelta is only valid from a previous call to WriteDelta - /// \param[in] outTemplateVar The value to read - /// \return true on success, false on failure. - template - bool ReadCompressedDelta(templateType &outTemplateVar); - - /// \brief Read one bitstream to another. - /// \param[in] numberOfBits bits to read - /// \param bitStream the bitstream to read into from - /// \return true on success, false on failure. - bool Read( BitStream *bitStream, BitSize_t numberOfBits ); - bool Read( BitStream *bitStream ); - bool Read( BitStream &bitStream, BitSize_t numberOfBits ); - bool Read( BitStream &bitStream ); - - /// \brief Write an array or casted stream or raw data. This does NOT do endian swapping. - /// \param[in] inputByteArray a byte buffer - /// \param[in] numberOfBytes the size of \a input in bytes - void Write( const char* inputByteArray, const unsigned int numberOfBytes ); - - /// \brief Write one bitstream to another. - /// \param[in] numberOfBits bits to write - /// \param bitStream the bitstream to copy from - void Write( BitStream *bitStream, BitSize_t numberOfBits ); - void Write( BitStream *bitStream ); - void Write( BitStream &bitStream, BitSize_t numberOfBits ); - void Write( BitStream &bitStream );\ - - /// \brief Write a float into 2 bytes, spanning the range between \a floatMin and \a floatMax - /// \param[in] x The float to write - /// \param[in] floatMin Predetermined minimum value of f - /// \param[in] floatMax Predetermined maximum value of f - void WriteFloat16( float x, float floatMin, float floatMax ); - - /// Write one type serialized as another (smaller) type, to save bandwidth - /// serializationType should be uint8_t, uint16_t, uint24_t, or uint32_t - /// Example: int num=53; WriteCasted(num); would use 1 byte to write what would otherwise be an integer (4 or 8 bytes) - /// \param[in] value The value to write - template - void WriteCasted( const sourceType &value ); - - /// Given the minimum and maximum values for an integer type, figure out the minimum number of bits to represent the range - /// Then write only those bits - /// \note A static is used so that the required number of bits for (maximum-minimum) is only calculated once. This does require that \a minimum and \maximum are fixed values for a given line of code for the life of the program - /// \param[in] value Integer value to write, which should be between \a minimum and \a maximum - /// \param[in] minimum Minimum value of \a value - /// \param[in] maximum Maximum value of \a value - /// \param[in] allowOutsideRange If true, all sends will take an extra bit, however value can deviate from outside \a minimum and \a maximum. If false, will assert if the value deviates. This should match the corresponding value passed to Read(). - template - void WriteBitsFromIntegerRange( const templateType value, const templateType minimum, const templateType maximum, bool allowOutsideRange=false ); - /// \param[in] requiredBits Primarily for internal use, called from above function() after calculating number of bits needed to represent maximum-minimum - template - void WriteBitsFromIntegerRange( const templateType value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange=false ); - - /// \brief Write a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes. - /// \details Will further compress y or z axis aligned vectors. - /// Accurate to 1/32767.5. - /// \param[in] x x - /// \param[in] y y - /// \param[in] z z - template // templateType for this function must be a float or double - void WriteNormVector( templateType x, templateType y, templateType z ); - - /// \brief Write a vector, using 10 bytes instead of 12. - /// \details Loses accuracy to about 3/10ths and only saves 2 bytes, - /// so only use if accuracy is not important. - /// \param[in] x x - /// \param[in] y y - /// \param[in] z z - template // templateType for this function must be a float or double - void WriteVector( templateType x, templateType y, templateType z ); - - /// \brief Write a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. Slightly lossy. - /// \param[in] w w - /// \param[in] x x - /// \param[in] y y - /// \param[in] z z - template // templateType for this function must be a float or double - void WriteNormQuat( templateType w, templateType x, templateType y, templateType z); - - /// \brief Write an orthogonal matrix by creating a quaternion, and writing 3 components of the quaternion in 2 bytes each. - /// \details Use 6 bytes instead of 36 - /// Lossy, although the result is renormalized - template // templateType for this function must be a float or double - void WriteOrthMatrix( - templateType m00, templateType m01, templateType m02, - templateType m10, templateType m11, templateType m12, - templateType m20, templateType m21, templateType m22 ); - - /// \brief Read an array or casted stream of byte. - /// \details The array is raw data. There is no automatic endian conversion with this function - /// \param[in] output The result byte array. It should be larger than @em numberOfBytes. - /// \param[in] numberOfBytes The number of byte to read - /// \return true on success false if there is some missing bytes. - bool Read( char* output, const unsigned int numberOfBytes ); - - /// \brief Read a float into 2 bytes, spanning the range between \a floatMin and \a floatMax - /// \param[in] outFloat The float to read - /// \param[in] floatMin Predetermined minimum value of f - /// \param[in] floatMax Predetermined maximum value of f - bool ReadFloat16( float &outFloat, float floatMin, float floatMax ); - - /// Read one type serialized to another (smaller) type, to save bandwidth - /// serializationType should be uint8_t, uint16_t, uint24_t, or uint32_t - /// Example: int num; ReadCasted(num); would read 1 bytefrom the stream, and put the value in an integer - /// \param[in] value The value to write - template - bool ReadCasted( sourceType &value ); - - /// Given the minimum and maximum values for an integer type, figure out the minimum number of bits to represent the range - /// Then read only those bits - /// \note A static is used so that the required number of bits for (maximum-minimum) is only calculated once. This does require that \a minimum and \maximum are fixed values for a given line of code for the life of the program - /// \param[in] value Integer value to read, which should be between \a minimum and \a maximum - /// \param[in] minimum Minimum value of \a value - /// \param[in] maximum Maximum value of \a value - /// \param[in] allowOutsideRange If true, all sends will take an extra bit, however value can deviate from outside \a minimum and \a maximum. If false, will assert if the value deviates. This should match the corresponding value passed to Write(). - template - bool ReadBitsFromIntegerRange( templateType &value, const templateType minimum, const templateType maximum, bool allowOutsideRange=false ); - /// \param[in] requiredBits Primarily for internal use, called from above function() after calculating number of bits needed to represent maximum-minimum - template - bool ReadBitsFromIntegerRange( templateType &value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange=false ); - - /// \brief Read a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes. - /// \details Will further compress y or z axis aligned vectors. - /// Accurate to 1/32767.5. - /// \param[in] x x - /// \param[in] y y - /// \param[in] z z - /// \return true on success, false on failure. - template // templateType for this function must be a float or double - bool ReadNormVector( templateType &x, templateType &y, templateType &z ); - - /// \brief Read 3 floats or doubles, using 10 bytes, where those float or doubles comprise a vector. - /// \details Loses accuracy to about 3/10ths and only saves 2 bytes, - /// so only use if accuracy is not important. - /// \param[in] x x - /// \param[in] y y - /// \param[in] z z - /// \return true on success, false on failure. - template // templateType for this function must be a float or double - bool ReadVector( templateType &x, templateType &y, templateType &z ); - - /// \brief Read a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. - /// \param[in] w w - /// \param[in] x x - /// \param[in] y y - /// \param[in] z z - /// \return true on success, false on failure. - template // templateType for this function must be a float or double - bool ReadNormQuat( templateType &w, templateType &x, templateType &y, templateType &z); - - /// \brief Read an orthogonal matrix from a quaternion, reading 3 components of the quaternion in 2 bytes each and extrapolatig the 4th. - /// \details Use 6 bytes instead of 36 - /// Lossy, although the result is renormalized - /// \return true on success, false on failure. - template // templateType for this function must be a float or double - bool ReadOrthMatrix( - templateType &m00, templateType &m01, templateType &m02, - templateType &m10, templateType &m11, templateType &m12, - templateType &m20, templateType &m21, templateType &m22 ); - - /// \brief Sets the read pointer back to the beginning of your data. - void ResetReadPointer( void ); - - /// \brief Sets the write pointer back to the beginning of your data. - void ResetWritePointer( void ); - - /// \brief This is good to call when you are done with the stream to make - /// sure you didn't leave any data left over void - void AssertStreamEmpty( void ); - - /// \brief RAKNET_DEBUG_PRINTF the bits in the stream. Great for debugging. - void PrintBits( char *out ) const; - void PrintBits( void ) const; - void PrintHex( char *out ) const; - void PrintHex( void ) const; - - /// \brief Ignore data we don't intend to read - /// \param[in] numberOfBits The number of bits to ignore - void IgnoreBits( const BitSize_t numberOfBits ); - - /// \brief Ignore data we don't intend to read - /// \param[in] numberOfBits The number of bytes to ignore - void IgnoreBytes( const unsigned int numberOfBytes ); - - /// \brief Move the write pointer to a position on the array. - /// \param[in] offset the offset from the start of the array. - /// \attention - /// \details Dangerous if you don't know what you are doing! - /// For efficiency reasons you can only write mid-stream if your data is byte aligned. - void SetWriteOffset( const BitSize_t offset ); - - /// \brief Returns the length in bits of the stream - inline BitSize_t GetNumberOfBitsUsed( void ) const {return GetWriteOffset();} - inline BitSize_t GetWriteOffset( void ) const {return numberOfBitsUsed;} - - /// \brief Returns the length in bytes of the stream - inline BitSize_t GetNumberOfBytesUsed( void ) const {return BITS_TO_BYTES( numberOfBitsUsed );} - - /// \brief Returns the number of bits into the stream that we have read - inline BitSize_t GetReadOffset( void ) const {return readOffset;} - - /// \brief Sets the read bit index - void SetReadOffset( const BitSize_t newReadOffset ) {readOffset=newReadOffset;} - - /// \brief Returns the number of bits left in the stream that haven't been read - inline BitSize_t GetNumberOfUnreadBits( void ) const {return numberOfBitsUsed - readOffset;} - - /// \brief Makes a copy of the internal data for you \a _data will point to - /// the stream. Partial bytes are left aligned. - /// \param[out] _data The allocated copy of GetData() - /// \return The length in bits of the stream. - BitSize_t CopyData( unsigned char** _data ) const; - - /// \internal - /// Set the stream to some initial data. - void SetData( unsigned char *inByteArray ); - - /// Gets the data that BitStream is writing to / reading from. - /// Partial bytes are left aligned. - /// \return A pointer to the internal state - inline unsigned char* GetData( void ) const {return data;} - - /// \brief Write numberToWrite bits from the input source. - /// \details Right aligned data means in the case of a partial byte, the bits are aligned - /// from the right (bit 0) rather than the left (as in the normal - /// internal representation) You would set this to true when - /// writing user data, and false when copying bitstream data, such - /// as writing one bitstream to another. - /// \param[in] inByteArray The data - /// \param[in] numberOfBitsToWrite The number of bits to write - /// \param[in] rightAlignedBits if true data will be right aligned - void WriteBits( const unsigned char* inByteArray, BitSize_t numberOfBitsToWrite, const bool rightAlignedBits = true ); - - /// \brief Align the bitstream to the byte boundary and then write the - /// specified number of bits. - /// \details This is faster than WriteBits but - /// wastes the bits to do the alignment and requires you to call - /// ReadAlignedBits at the corresponding read position. - /// \param[in] inByteArray The data - /// \param[in] numberOfBytesToWrite The size of input. - void WriteAlignedBytes( const unsigned char *inByteArray, const unsigned int numberOfBytesToWrite ); - - // Endian swap bytes already in the bitstream - void EndianSwapBytes( int byteOffset, int length ); - - /// \brief Aligns the bitstream, writes inputLength, and writes input. Won't write beyond maxBytesToWrite - /// \param[in] inByteArray The data - /// \param[in] inputLength The size of input. - /// \param[in] maxBytesToWrite Max bytes to write - void WriteAlignedBytesSafe( const char *inByteArray, const unsigned int inputLength, const unsigned int maxBytesToWrite ); - - /// \brief Read bits, starting at the next aligned bits. - /// \details Note that the modulus 8 starting offset of the sequence must be the same as - /// was used with WriteBits. This will be a problem with packet - /// coalescence unless you byte align the coalesced packets. - /// \param[in] inOutByteArray The byte array larger than @em numberOfBytesToRead - /// \param[in] numberOfBytesToRead The number of byte to read from the internal state - /// \return true if there is enough byte. - bool ReadAlignedBytes( unsigned char *inOutByteArray, const unsigned int numberOfBytesToRead ); - - /// \brief Reads what was written by WriteAlignedBytesSafe. - /// \param[in] inOutByteArray The data - /// \param[in] maxBytesToRead Maximum number of bytes to read - /// \return true on success, false on failure. - bool ReadAlignedBytesSafe( char *inOutByteArray, int &inputLength, const int maxBytesToRead ); - bool ReadAlignedBytesSafe( char *inOutByteArray, unsigned int &inputLength, const unsigned int maxBytesToRead ); - - /// \brief Same as ReadAlignedBytesSafe() but allocates the memory for you using new, rather than assuming it is safe to write to - /// \param[in] outByteArray outByteArray will be deleted if it is not a pointer to 0 - /// \return true on success, false on failure. - bool ReadAlignedBytesSafeAlloc( char **outByteArray, int &inputLength, const unsigned int maxBytesToRead ); - bool ReadAlignedBytesSafeAlloc( char **outByteArray, unsigned int &inputLength, const unsigned int maxBytesToRead ); - - /// \brief Align the next write and/or read to a byte boundary. - /// \details This can be used to 'waste' bits to byte align for efficiency reasons It - /// can also be used to force coalesced bitstreams to start on byte - /// boundaries so so WriteAlignedBits and ReadAlignedBits both - /// calculate the same offset when aligning. - inline void AlignWriteToByteBoundary( void ) {numberOfBitsUsed += 8 - ( (( numberOfBitsUsed - 1 ) & 7) + 1 );} - - /// \brief Align the next write and/or read to a byte boundary. - /// \details This can be used to 'waste' bits to byte align for efficiency reasons It - /// can also be used to force coalesced bitstreams to start on byte - /// boundaries so so WriteAlignedBits and ReadAlignedBits both - /// calculate the same offset when aligning. - inline void AlignReadToByteBoundary( void ) {readOffset += 8 - ( (( readOffset - 1 ) & 7 ) + 1 );} - - /// \brief Read \a numberOfBitsToRead bits to the output source. - /// \details alignBitsToRight should be set to true to convert internal - /// bitstream data to userdata. It should be false if you used - /// WriteBits with rightAlignedBits false - /// \param[in] inOutByteArray The resulting bits array - /// \param[in] numberOfBitsToRead The number of bits to read - /// \param[in] alignBitsToRight if true bits will be right aligned. - /// \return true if there is enough bits to read - bool ReadBits( unsigned char *inOutByteArray, BitSize_t numberOfBitsToRead, const bool alignBitsToRight = true ); - - /// \brief Write a 0 - void Write0( void ); - - /// \brief Write a 1 - void Write1( void ); - - /// \brief Reads 1 bit and returns true if that bit is 1 and false if it is 0. - bool ReadBit( void ); - - /// \brief If we used the constructor version with copy data off, this - /// *makes sure it is set to on and the data pointed to is copied. - void AssertCopyData( void ); - - /// \brief Use this if you pass a pointer copy to the constructor - /// *(_copyData==false) and want to overallocate to prevent - /// reallocation. - void SetNumberOfBitsAllocated( const BitSize_t lengthInBits ); - - /// \brief Reallocates (if necessary) in preparation of writing numberOfBitsToWrite - void AddBitsAndReallocate( const BitSize_t numberOfBitsToWrite ); - - /// \internal - /// \return How many bits have been allocated internally - BitSize_t GetNumberOfBitsAllocated(void) const; - - /// \brief Read strings, non reference. - bool Read(char *varString); - bool Read(unsigned char *varString); - - /// Write zeros until the bitstream is filled up to \a bytes - void PadWithZeroToByteLength( unsigned int bytes ); - - /// Get the number of leading zeros for a number - /// \param[in] x Number to test - static int NumberOfLeadingZeroes( uint8_t x ); - static int NumberOfLeadingZeroes( uint16_t x ); - static int NumberOfLeadingZeroes( uint32_t x ); - static int NumberOfLeadingZeroes( uint64_t x ); - static int NumberOfLeadingZeroes( int8_t x ); - static int NumberOfLeadingZeroes( int16_t x ); - static int NumberOfLeadingZeroes( int32_t x ); - static int NumberOfLeadingZeroes( int64_t x ); - - /// \internal Unrolled inner loop, for when performance is critical - void WriteAlignedVar8(const char *inByteArray); - /// \internal Unrolled inner loop, for when performance is critical - bool ReadAlignedVar8(char *inOutByteArray); - /// \internal Unrolled inner loop, for when performance is critical - void WriteAlignedVar16(const char *inByteArray); - /// \internal Unrolled inner loop, for when performance is critical - bool ReadAlignedVar16(char *inOutByteArray); - /// \internal Unrolled inner loop, for when performance is critical - void WriteAlignedVar32(const char *inByteArray); - /// \internal Unrolled inner loop, for when performance is critical - bool ReadAlignedVar32(char *inOutByteArray); - - inline void Write(const char * const inStringVar) - { - RakString::Serialize(inStringVar, this); - } - inline void Write(const wchar_t * const inStringVar) - { - RakWString::Serialize(inStringVar, this); - } - inline void Write(const unsigned char * const inTemplateVar) - { - Write((const char*)inTemplateVar); - } - inline void Write(char * const inTemplateVar) - { - Write((const char*)inTemplateVar); - } - inline void Write(unsigned char * const inTemplateVar) - { - Write((const char*)inTemplateVar); - } - inline void WriteCompressed(const char * const inStringVar) - { - RakString::SerializeCompressed(inStringVar,this,0,false); - } - inline void WriteCompressed(const wchar_t * const inStringVar) - { - RakWString::Serialize(inStringVar,this); - } - inline void WriteCompressed(const unsigned char * const inTemplateVar) - { - WriteCompressed((const char*) inTemplateVar); - } - inline void WriteCompressed(char * const inTemplateVar) - { - WriteCompressed((const char*) inTemplateVar); - } - inline void WriteCompressed(unsigned char * const inTemplateVar) - { - WriteCompressed((const char*) inTemplateVar); - } - - /// ---- Member function template specialization declarations ---- - // Used for VC7 -#if defined(_MSC_VER) && _MSC_VER == 1300 - /// Write a bool to a bitstream. - /// \param[in] var The value to write - template <> - void Write(const bool &var); - - /// Write a systemAddress to a bitstream - /// \param[in] var The value to write - template <> - void Write(const SystemAddress &var); - - /// Write a uint24_t to a bitstream - /// \param[in] var The value to write - template <> - void Write(const uint24_t &var); - - /// Write a RakNetGUID to a bitsteam - /// \param[in] var The value to write - template <> - void Write(const RakNetGuid &var); - - /// Write a string to a bitstream - /// \param[in] var The value to write - template <> - void Write(const char* const &var); - template <> - void Write(const unsigned char* const &var); - template <> - void Write(char* const &var); - template <> - void Write(unsigned char* const &var); - template <> - void Write(const RakString &var); - template <> - void Write(const RakWString &var); - - /// \brief Write a systemAddress. - /// \details If the current value is different from the last value - /// the current value will be written. Otherwise, a single bit will be written - /// \param[in] currentValue The current value to write - /// \param[in] lastValue The last value to compare against - template <> - void WriteDelta(const SystemAddress ¤tValue, const SystemAddress &lastValue); - - template <> - void WriteDelta(const uint24_t ¤tValue, const uint24_t &lastValue); - - template <> - void WriteDelta(const RakNetGUID ¤tValue, const RakNetGUID &lastValue); - - /// \brief Write a bool delta. - /// \details Same thing as just calling Write - /// \param[in] currentValue The current value to write - /// \param[in] lastValue The last value to compare against - template <> - void WriteDelta(const bool ¤tValue, const bool &lastValue); - - template <> - void WriteCompressed(const SystemAddress &var); - - template <> - void WriteCompressed(const uint24_t &var); - - template <> - void WriteCompressed(const RakNetGUID &var); - - template <> - void WriteCompressed(const bool &var); - - /// For values between -1 and 1 - template <> - void WriteCompressed(const float &var); - - /// For values between -1 and 1 - template <> - void WriteCompressed(const double &var); - - /// Compressed string - template <> - void WriteCompressed(const char* var); - template <> - void WriteCompressed(const unsigned char* var); - template <> - void WriteCompressed(char* var); - template <> - void WriteCompressed(unsigned char* var); - template <> - void WriteCompressed(const RakString &var); - template <> - void WriteCompressed(const RakWString &var); - - /// \brief Write a bool delta. - /// \details Same thing as just calling Write - /// \param[in] currentValue The current value to write - /// \param[in] lastValue The last value to compare against - template <> - void WriteCompressedDelta(const bool ¤tValue, const bool &lastValue); - - /// \brief Save as WriteCompressedDelta(bool currentValue, const templateType &lastValue) - /// when we have an unknown second bool - template <> - void WriteCompressedDelta(const bool ¤tValue); - - /// \brief Read a bool from a bitstream. - /// \param[in] var The value to read - /// \return true on success, false on failure. - template <> - bool Read(bool &var); - - /// \brief Read a systemAddress from a bitstream. - /// \param[in] var The value to read - /// \return true on success, false on failure. - template <> - bool Read(SystemAddress &var); - - template <> - bool Read(uint24_t &var); - - template <> - bool Read(RakNetGUID &var); - - /// \brief Read a String from a bitstream. - /// \param[in] var The value to read - /// \return true on success, false on failure. - template <> - bool Read(char *&var); - template <> - bool Read(wchar_t *&var); - template <> - bool Read(unsigned char *&var); - template <> - bool Read(RakString &var); - template <> - bool Read(RakWString &var); - - /// \brief Read a bool from a bitstream. - /// \param[in] var The value to read - /// \return true on success, false on failure. - template <> - bool ReadDelta(bool &var); - - template <> - bool ReadCompressed(SystemAddress &var); - - template <> - bool ReadCompressed(uint24_t &var); - - template <> - bool ReadCompressed(RakNetGUID &var); - - template <> - bool ReadCompressed(bool &var); - - template <> - bool ReadCompressed(float &var); - - /// For values between -1 and 1 - /// \return true on success, false on failure. - template <> - bool ReadCompressed(double &var); - - template <> - bool ReadCompressed(char* &var); - template <> - bool ReadCompressed(wchar_t* &var); - template <> - bool ReadCompressed(unsigned char *&var); - template <> - bool ReadCompressed(RakString &var); - template <> - bool ReadCompressed(RakWString &var); - - /// \brief Read a bool from a bitstream. - /// \param[in] var The value to read - /// \return true on success, false on failure. - template <> - bool ReadCompressedDelta(bool &var); -#endif - - inline static bool DoEndianSwap(void) { -#ifndef __BITSTREAM_NATIVE_END - return IsNetworkOrder()==false; -#else - return false; -#endif - } - inline static bool IsBigEndian(void) - { - return IsNetworkOrder(); - } - inline static bool IsNetworkOrder(void) {bool r = IsNetworkOrderInternal(); return r;} - // Not inline, won't compile on PC due to winsock include errors - static bool IsNetworkOrderInternal(void); - static void ReverseBytes(unsigned char *inByteArray, unsigned char *inOutByteArray, const unsigned int length); - static void ReverseBytesInPlace(unsigned char *inOutData,const unsigned int length); - - private: - - BitStream( const BitStream &invalid) { - (void) invalid; - RakAssert(0); - } - - BitStream& operator = ( const BitStream& invalid ) { - (void) invalid; - RakAssert(0); - static BitStream i; - return i; - } - - /// \brief Assume the input source points to a native type, compress and write it. - void WriteCompressed( const unsigned char* inByteArray, const unsigned int size, const bool unsignedData ); - - /// \brief Assume the input source points to a compressed native type. Decompress and read it. - bool ReadCompressed( unsigned char* inOutByteArray, const unsigned int size, const bool unsignedData ); - - - BitSize_t numberOfBitsUsed; - - BitSize_t numberOfBitsAllocated; - - BitSize_t readOffset; - - unsigned char *data; - - /// true if the internal buffer is copy of the data passed to the constructor - bool copyData; - - /// BitStreams that use less than BITSTREAM_STACK_ALLOCATION_SIZE use the stack, rather than the heap to store data. It switches over if BITSTREAM_STACK_ALLOCATION_SIZE is exceeded - unsigned char stackData[BITSTREAM_STACK_ALLOCATION_SIZE]; - }; - - template - inline bool BitStream::Serialize(bool writeToBitstream, templateType &inOutTemplateVar) - { - if (writeToBitstream) - Write(inOutTemplateVar); - else - return Read(inOutTemplateVar); - return true; - } - - template - inline bool BitStream::SerializeDelta(bool writeToBitstream, templateType &inOutCurrentValue, const templateType &lastValue) - { - if (writeToBitstream) - WriteDelta(inOutCurrentValue, lastValue); - else - return ReadDelta(inOutCurrentValue); - return true; - } - - template - inline bool BitStream::SerializeDelta(bool writeToBitstream, templateType &inOutCurrentValue) - { - if (writeToBitstream) - WriteDelta(inOutCurrentValue); - else - return ReadDelta(inOutCurrentValue); - return true; - } - - template - inline bool BitStream::SerializeCompressed(bool writeToBitstream, templateType &inOutTemplateVar) - { - if (writeToBitstream) - WriteCompressed(inOutTemplateVar); - else - return ReadCompressed(inOutTemplateVar); - return true; - } - - template - inline bool BitStream::SerializeCompressedDelta(bool writeToBitstream, templateType &inOutCurrentValue, const templateType &lastValue) - { - if (writeToBitstream) - WriteCompressedDelta(inOutCurrentValue,lastValue); - else - return ReadCompressedDelta(inOutCurrentValue); - return true; - } -//Stoppedhere - template - inline bool BitStream::SerializeCompressedDelta(bool writeToBitstream, templateType &inOutCurrentValue) - { - if (writeToBitstream) - WriteCompressedDelta(inOutCurrentValue); - else - return ReadCompressedDelta(inOutCurrentValue); - return true; - } - - inline bool BitStream::Serialize(bool writeToBitstream, char* inOutByteArray, const unsigned int numberOfBytes ) - { - if (writeToBitstream) - Write(inOutByteArray, numberOfBytes); - else - return Read(inOutByteArray, numberOfBytes); - return true; - } - - template - bool BitStream::SerializeCasted( bool writeToBitstream, sourceType &value ) - { - if (writeToBitstream) WriteCasted(value); - else return ReadCasted(value); - return true; - } - - template - bool BitStream::SerializeBitsFromIntegerRange( bool writeToBitstream, templateType &value, const templateType minimum, const templateType maximum, bool allowOutsideRange ) - { - int requiredBits=BYTES_TO_BITS(sizeof(templateType))-NumberOfLeadingZeroes(templateType(maximum-minimum)); - return SerializeBitsFromIntegerRange(writeToBitstream,value,minimum,maximum,requiredBits,allowOutsideRange); - } - template - bool BitStream::SerializeBitsFromIntegerRange( bool writeToBitstream, templateType &value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange ) - { - if (writeToBitstream) WriteBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange); - else return ReadBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange); - return true; - } - - template - inline bool BitStream::SerializeNormVector(bool writeToBitstream, templateType &x, templateType &y, templateType &z ) - { - if (writeToBitstream) - WriteNormVector(x,y,z); - else - return ReadNormVector(x,y,z); - return true; - } - - template - inline bool BitStream::SerializeVector(bool writeToBitstream, templateType &x, templateType &y, templateType &z ) - { - if (writeToBitstream) - WriteVector(x,y,z); - else - return ReadVector(x,y,z); - return true; - } - - template - inline bool BitStream::SerializeNormQuat(bool writeToBitstream, templateType &w, templateType &x, templateType &y, templateType &z) - { - if (writeToBitstream) - WriteNormQuat(w,x,y,z); - else - return ReadNormQuat(w,x,y,z); - return true; - } - - template - inline bool BitStream::SerializeOrthMatrix( - bool writeToBitstream, - templateType &m00, templateType &m01, templateType &m02, - templateType &m10, templateType &m11, templateType &m12, - templateType &m20, templateType &m21, templateType &m22 ) - { - if (writeToBitstream) - WriteOrthMatrix(m00,m01,m02,m10,m11,m12,m20,m21,m22); - else - return ReadOrthMatrix(m00,m01,m02,m10,m11,m12,m20,m21,m22); - return true; - } - - inline bool BitStream::SerializeBits(bool writeToBitstream, unsigned char* inOutByteArray, const BitSize_t numberOfBitsToSerialize, const bool rightAlignedBits ) - { - if (writeToBitstream) - WriteBits(inOutByteArray,numberOfBitsToSerialize,rightAlignedBits); - else - return ReadBits(inOutByteArray,numberOfBitsToSerialize,rightAlignedBits); - return true; - } - - template - inline void BitStream::Write(const templateType &inTemplateVar) - { -#ifdef _MSC_VER -#pragma warning(disable:4127) // conditional expression is constant -#endif - if (sizeof(inTemplateVar)==1) - WriteBits( ( unsigned char* ) & inTemplateVar, sizeof( templateType ) * 8, true ); - else - { -#ifndef __BITSTREAM_NATIVE_END - if (DoEndianSwap()) - { - unsigned char output[sizeof(templateType)]; - ReverseBytes((unsigned char*)&inTemplateVar, output, sizeof(templateType)); - WriteBits( ( unsigned char* ) output, sizeof(templateType) * 8, true ); - } - else -#endif - WriteBits( ( unsigned char* ) & inTemplateVar, sizeof(templateType) * 8, true ); - } - } - - template - inline void BitStream::WritePtr(templateType *inTemplateVar) - { -#ifdef _MSC_VER -#pragma warning(disable:4127) // conditional expression is constant -#endif - if (sizeof(templateType)==1) - WriteBits( ( unsigned char* ) inTemplateVar, sizeof( templateType ) * 8, true ); - else - { -#ifndef __BITSTREAM_NATIVE_END - if (DoEndianSwap()) - { - unsigned char output[sizeof(templateType)]; - ReverseBytes((unsigned char*) inTemplateVar, output, sizeof(templateType)); - WriteBits( ( unsigned char* ) output, sizeof(templateType) * 8, true ); - } - else -#endif - WriteBits( ( unsigned char* ) inTemplateVar, sizeof(templateType) * 8, true ); - } - } - - /// \brief Write a bool to a bitstream. - /// \param[in] inTemplateVar The value to write - template <> - inline void BitStream::Write(const bool &inTemplateVar) - { - if ( inTemplateVar ) - Write1(); - else - Write0(); - } - - - /// \brief Write a systemAddress to a bitstream. - /// \param[in] inTemplateVar The value to write - template <> - inline void BitStream::Write(const SystemAddress &inTemplateVar) - { - Write(inTemplateVar.GetIPVersion()); - if (inTemplateVar.GetIPVersion()==4) - { - // Hide the address so routers don't modify it - SystemAddress var2=inTemplateVar; - uint32_t binaryAddress=~inTemplateVar.address.addr4.sin_addr.s_addr; - // Don't endian swap the address or port - WriteBits((unsigned char*)&binaryAddress, sizeof(binaryAddress)*8, true); - unsigned short p = var2.GetPortNetworkOrder(); - WriteBits((unsigned char*)&p, sizeof(unsigned short)*8, true); - } - else - { -#if RAKNET_SUPPORT_IPV6==1 - // Don't endian swap - WriteBits((const unsigned char*) &inTemplateVar.address.addr6, sizeof(inTemplateVar.address.addr6)*8, true); -#endif - } - } - - template <> - inline void BitStream::Write(const uint24_t &inTemplateVar) - { - AlignWriteToByteBoundary(); - AddBitsAndReallocate(3*8); - - if (IsBigEndian()==false) - { - data[( numberOfBitsUsed >> 3 ) + 0] = ((unsigned char *)&inTemplateVar.val)[0]; - data[( numberOfBitsUsed >> 3 ) + 1] = ((unsigned char *)&inTemplateVar.val)[1]; - data[( numberOfBitsUsed >> 3 ) + 2] = ((unsigned char *)&inTemplateVar.val)[2]; - } - else - { - data[( numberOfBitsUsed >> 3 ) + 0] = ((unsigned char *)&inTemplateVar.val)[3]; - data[( numberOfBitsUsed >> 3 ) + 1] = ((unsigned char *)&inTemplateVar.val)[2]; - data[( numberOfBitsUsed >> 3 ) + 2] = ((unsigned char *)&inTemplateVar.val)[1]; - } - - numberOfBitsUsed+=3*8; - } - - template <> - inline void BitStream::Write(const RakNetGUID &inTemplateVar) - { - Write(inTemplateVar.g); - } - - /// \brief Write a string to a bitstream. - /// \param[in] var The value to write - template <> - inline void BitStream::Write(const RakString &inTemplateVar) - { - inTemplateVar.Serialize(this); - } - template <> - inline void BitStream::Write(const RakWString &inTemplateVar) - { - inTemplateVar.Serialize(this); - } - template <> - inline void BitStream::Write(const char * const &inStringVar) - { - RakString::Serialize(inStringVar, this); - } - template <> - inline void BitStream::Write(const wchar_t * const &inStringVar) - { - RakWString::Serialize(inStringVar, this); - } - template <> - inline void BitStream::Write(const unsigned char * const &inTemplateVar) - { - Write((const char*)inTemplateVar); - } - template <> - inline void BitStream::Write(char * const &inTemplateVar) - { - Write((const char*)inTemplateVar); - } - template <> - inline void BitStream::Write(unsigned char * const &inTemplateVar) - { - Write((const char*)inTemplateVar); - } - - /// \brief Write any integral type to a bitstream. - /// \details If the current value is different from the last value - /// the current value will be written. Otherwise, a single bit will be written - /// \param[in] currentValue The current value to write - /// \param[in] lastValue The last value to compare against - template - inline void BitStream::WriteDelta(const templateType ¤tValue, const templateType &lastValue) - { - if (currentValue==lastValue) - { - Write(false); - } - else - { - Write(true); - Write(currentValue); - } - } - - /// \brief Write a bool delta. Same thing as just calling Write - /// \param[in] currentValue The current value to write - /// \param[in] lastValue The last value to compare against - template <> - inline void BitStream::WriteDelta(const bool ¤tValue, const bool &lastValue) - { - (void) lastValue; - - Write(currentValue); - } - - /// \brief WriteDelta when you don't know what the last value is, or there is no last value. - /// \param[in] currentValue The current value to write - template - inline void BitStream::WriteDelta(const templateType ¤tValue) - { - Write(true); - Write(currentValue); - } - - /// \brief Write any integral type to a bitstream. - /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. - /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. - /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type - /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte - /// \param[in] inTemplateVar The value to write - template - inline void BitStream::WriteCompressed(const templateType &inTemplateVar) - { -#ifdef _MSC_VER -#pragma warning(disable:4127) // conditional expression is constant -#endif - if (sizeof(inTemplateVar)==1) - WriteCompressed( ( unsigned char* ) & inTemplateVar, sizeof( templateType ) * 8, true ); - else - { -#ifndef __BITSTREAM_NATIVE_END -#ifdef _MSC_VER -#pragma warning(disable:4244) // '=' : conversion from 'unsigned long' to 'unsigned short', possible loss of data -#endif - - if (DoEndianSwap()) - { - unsigned char output[sizeof(templateType)]; - ReverseBytes((unsigned char*)&inTemplateVar, output, sizeof(templateType)); - WriteCompressed( ( unsigned char* ) output, sizeof(templateType) * 8, true ); - } - else -#endif - WriteCompressed( ( unsigned char* ) & inTemplateVar, sizeof(templateType) * 8, true ); - } - } - - template <> - inline void BitStream::WriteCompressed(const SystemAddress &inTemplateVar) - { - Write(inTemplateVar); - } - - template <> - inline void BitStream::WriteCompressed(const RakNetGUID &inTemplateVar) - { - Write(inTemplateVar); - } - - template <> - inline void BitStream::WriteCompressed(const uint24_t &var) - { - Write(var); - } - - template <> - inline void BitStream::WriteCompressed(const bool &inTemplateVar) - { - Write(inTemplateVar); - } - - /// For values between -1 and 1 - template <> - inline void BitStream::WriteCompressed(const float &inTemplateVar) - { - RakAssert(inTemplateVar > -1.01f && inTemplateVar < 1.01f); - float varCopy=inTemplateVar; - if (varCopy < -1.0f) - varCopy=-1.0f; - if (varCopy > 1.0f) - varCopy=1.0f; - Write((unsigned short)((varCopy+1.0f)*32767.5f)); - } - - /// For values between -1 and 1 - template <> - inline void BitStream::WriteCompressed(const double &inTemplateVar) - { - RakAssert(inTemplateVar > -1.01 && inTemplateVar < 1.01); - double varCopy=inTemplateVar; - if (varCopy < -1.0f) - varCopy=-1.0f; - if (varCopy > 1.0f) - varCopy=1.0f; - Write((uint32_t)((varCopy+1.0)*2147483648.0)); - } - - /// Compress the string - template <> - inline void BitStream::WriteCompressed(const RakString &inTemplateVar) - { - inTemplateVar.SerializeCompressed(this,0,false); - } - template <> - inline void BitStream::WriteCompressed(const RakWString &inTemplateVar) - { - inTemplateVar.Serialize(this); - } - template <> - inline void BitStream::WriteCompressed(const char * const &inStringVar) - { - RakString::SerializeCompressed(inStringVar,this,0,false); - } - template <> - inline void BitStream::WriteCompressed(const wchar_t * const &inStringVar) - { - RakWString::Serialize(inStringVar,this); - } - template <> - inline void BitStream::WriteCompressed(const unsigned char * const &inTemplateVar) - { - WriteCompressed((const char*) inTemplateVar); - } - template <> - inline void BitStream::WriteCompressed(char * const &inTemplateVar) - { - WriteCompressed((const char*) inTemplateVar); - } - template <> - inline void BitStream::WriteCompressed(unsigned char * const &inTemplateVar) - { - WriteCompressed((const char*) inTemplateVar); - } - - - /// \brief Write any integral type to a bitstream. - /// \details If the current value is different from the last value - /// the current value will be written. Otherwise, a single bit will be written - /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. - /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type - /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte - /// \param[in] currentValue The current value to write - /// \param[in] lastValue The last value to compare against - template - inline void BitStream::WriteCompressedDelta(const templateType ¤tValue, const templateType &lastValue) - { - if (currentValue==lastValue) - { - Write(false); - } - else - { - Write(true); - WriteCompressed(currentValue); - } - } - - /// \brief Write a bool delta. Same thing as just calling Write - /// \param[in] currentValue The current value to write - /// \param[in] lastValue The last value to compare against - template <> - inline void BitStream::WriteCompressedDelta(const bool ¤tValue, const bool &lastValue) - { - (void) lastValue; - - Write(currentValue); - } - - /// \brief Save as WriteCompressedDelta(const templateType ¤tValue, const templateType &lastValue) - /// when we have an unknown second parameter - template - inline void BitStream::WriteCompressedDelta(const templateType ¤tValue) - { - Write(true); - WriteCompressed(currentValue); - } - - /// \brief Save as WriteCompressedDelta(bool currentValue, const templateType &lastValue) - /// when we have an unknown second bool - template <> - inline void BitStream::WriteCompressedDelta(const bool ¤tValue) - { - Write(currentValue); - } - - /// \brief Read any integral type from a bitstream. Define __BITSTREAM_NATIVE_END if you need endian swapping. - /// \param[in] outTemplateVar The value to read - template - inline bool BitStream::Read(templateType &outTemplateVar) - { -#ifdef _MSC_VER -#pragma warning(disable:4127) // conditional expression is constant -#endif - if (sizeof(outTemplateVar)==1) - return ReadBits( ( unsigned char* ) &outTemplateVar, sizeof(templateType) * 8, true ); - else - { -#ifndef __BITSTREAM_NATIVE_END -#ifdef _MSC_VER -#pragma warning(disable:4244) // '=' : conversion from 'unsigned long' to 'unsigned short', possible loss of data -#endif - if (DoEndianSwap()) - { - unsigned char output[sizeof(templateType)]; - if (ReadBits( ( unsigned char* ) output, sizeof(templateType) * 8, true )) - { - ReverseBytes(output, (unsigned char*)&outTemplateVar, sizeof(templateType)); - return true; - } - return false; - } - else -#endif - return ReadBits( ( unsigned char* ) & outTemplateVar, sizeof(templateType) * 8, true ); - } - } - - /// \brief Read a bool from a bitstream. - /// \param[in] outTemplateVar The value to read - template <> - inline bool BitStream::Read(bool &outTemplateVar) - { - if ( readOffset + 1 > numberOfBitsUsed ) - return false; - - if ( data[ readOffset >> 3 ] & ( 0x80 >> ( readOffset & 7 ) ) ) // Is it faster to just write it out here? - outTemplateVar = true; - else - outTemplateVar = false; - - // Has to be on a different line for Mac - readOffset++; - - return true; - } - - /// \brief Read a systemAddress from a bitstream. - /// \param[in] outTemplateVar The value to read - template <> - inline bool BitStream::Read(SystemAddress &outTemplateVar) - { - unsigned char ipVersion; - Read(ipVersion); - if (ipVersion==4) - { - outTemplateVar.address.addr4.sin_family=AF_INET; - // Read(var.binaryAddress); - // Don't endian swap the address or port - uint32_t binaryAddress; - ReadBits( ( unsigned char* ) & binaryAddress, sizeof(binaryAddress) * 8, true ); - // Unhide the IP address, done to prevent routers from changing it - outTemplateVar.address.addr4.sin_addr.s_addr=~binaryAddress; - bool b = ReadBits(( unsigned char* ) & outTemplateVar.address.addr4.sin_port, sizeof(outTemplateVar.address.addr4.sin_port) * 8, true); - outTemplateVar.debugPort=ntohs(outTemplateVar.address.addr4.sin_port); - return b; - } - else - { -#if RAKNET_SUPPORT_IPV6==1 - bool b = ReadBits((unsigned char*) &outTemplateVar.address.addr6, sizeof(outTemplateVar.address.addr6)*8, true); - outTemplateVar.debugPort=ntohs(outTemplateVar.address.addr6.sin6_port); - return b; -#else - return false; -#endif - } - } - - template <> - inline bool BitStream::Read(uint24_t &outTemplateVar) - { - AlignReadToByteBoundary(); - if ( readOffset + 3*8 > numberOfBitsUsed ) - return false; - - if (IsBigEndian()==false) - { - ((unsigned char *)&outTemplateVar.val)[0]=data[ (readOffset >> 3) + 0]; - ((unsigned char *)&outTemplateVar.val)[1]=data[ (readOffset >> 3) + 1]; - ((unsigned char *)&outTemplateVar.val)[2]=data[ (readOffset >> 3) + 2]; - ((unsigned char *)&outTemplateVar.val)[3]=0; - } - else - { - - ((unsigned char *)&outTemplateVar.val)[3]=data[ (readOffset >> 3) + 0]; - ((unsigned char *)&outTemplateVar.val)[2]=data[ (readOffset >> 3) + 1]; - ((unsigned char *)&outTemplateVar.val)[1]=data[ (readOffset >> 3) + 2]; - ((unsigned char *)&outTemplateVar.val)[0]=0; - } - - readOffset+=3*8; - return true; - } - - template <> - inline bool BitStream::Read(RakNetGUID &outTemplateVar) - { - return Read(outTemplateVar.g); - } - - - template <> - inline bool BitStream::Read(RakString &outTemplateVar) - { - return outTemplateVar.Deserialize(this); - } - template <> - inline bool BitStream::Read(RakWString &outTemplateVar) - { - return outTemplateVar.Deserialize(this); - } - template <> - inline bool BitStream::Read(char *&varString) - { - return RakString::Deserialize(varString,this); - } - template <> - inline bool BitStream::Read(wchar_t *&varString) - { - return RakWString::Deserialize(varString,this); - } - template <> - inline bool BitStream::Read(unsigned char *&varString) - { - return RakString::Deserialize((char*) varString,this); - } - - /// \brief Read any integral type from a bitstream. - /// \details If the written value differed from the value compared against in the write function, - /// var will be updated. Otherwise it will retain the current value. - /// ReadDelta is only valid from a previous call to WriteDelta - /// \param[in] outTemplateVar The value to read - template - inline bool BitStream::ReadDelta(templateType &outTemplateVar) - { - bool dataWritten; - bool success; - success=Read(dataWritten); - if (dataWritten) - success=Read(outTemplateVar); - return success; - } - - /// \brief Read a bool from a bitstream. - /// \param[in] outTemplateVar The value to read - template <> - inline bool BitStream::ReadDelta(bool &outTemplateVar) - { - return Read(outTemplateVar); - } - - /// \brief Read any integral type from a bitstream. - /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. - /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. - /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type - /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte - /// \param[in] outTemplateVar The value to read - template - inline bool BitStream::ReadCompressed(templateType &outTemplateVar) - { -#ifdef _MSC_VER -#pragma warning(disable:4127) // conditional expression is constant -#endif - if (sizeof(outTemplateVar)==1) - return ReadCompressed( ( unsigned char* ) &outTemplateVar, sizeof(templateType) * 8, true ); - else - { -#ifndef __BITSTREAM_NATIVE_END - if (DoEndianSwap()) - { - unsigned char output[sizeof(templateType)]; - if (ReadCompressed( ( unsigned char* ) output, sizeof(templateType) * 8, true )) - { - ReverseBytes(output, (unsigned char*)&outTemplateVar, sizeof(templateType)); - return true; - } - return false; - } - else -#endif - return ReadCompressed( ( unsigned char* ) & outTemplateVar, sizeof(templateType) * 8, true ); - } - } - - template <> - inline bool BitStream::ReadCompressed(SystemAddress &outTemplateVar) - { - return Read(outTemplateVar); - } - - template <> - inline bool BitStream::ReadCompressed(uint24_t &outTemplateVar) - { - return Read(outTemplateVar); - } - - template <> - inline bool BitStream::ReadCompressed(RakNetGUID &outTemplateVar) - { - return Read(outTemplateVar); - } - - template <> - inline bool BitStream::ReadCompressed(bool &outTemplateVar) - { - return Read(outTemplateVar); - } - - /// For values between -1 and 1 - template <> - inline bool BitStream::ReadCompressed(float &outTemplateVar) - { - unsigned short compressedFloat; - if (Read(compressedFloat)) - { - outTemplateVar = ((float)compressedFloat / 32767.5f - 1.0f); - return true; - } - return false; - } - - /// For values between -1 and 1 - template <> - inline bool BitStream::ReadCompressed(double &outTemplateVar) - { - uint32_t compressedFloat; - if (Read(compressedFloat)) - { - outTemplateVar = ((double)compressedFloat / 2147483648.0 - 1.0); - return true; - } - return false; - } - - /// For strings - template <> - inline bool BitStream::ReadCompressed(RakString &outTemplateVar) - { - return outTemplateVar.DeserializeCompressed(this,false); - } - template <> - inline bool BitStream::ReadCompressed(RakWString &outTemplateVar) - { - return outTemplateVar.Deserialize(this); - } - template <> - inline bool BitStream::ReadCompressed(char *&outTemplateVar) - { - return RakString::DeserializeCompressed(outTemplateVar,this,false); - } - template <> - inline bool BitStream::ReadCompressed(wchar_t *&outTemplateVar) - { - return RakWString::Deserialize(outTemplateVar,this); - } - template <> - inline bool BitStream::ReadCompressed(unsigned char *&outTemplateVar) - { - return RakString::DeserializeCompressed((char*) outTemplateVar,this,false); - } - - /// \brief Read any integral type from a bitstream. - /// \details If the written value differed from the value compared against in the write function, - /// var will be updated. Otherwise it will retain the current value. - /// the current value will be updated. - /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. - /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type - /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte - /// ReadCompressedDelta is only valid from a previous call to WriteDelta - /// \param[in] outTemplateVar The value to read - template - inline bool BitStream::ReadCompressedDelta(templateType &outTemplateVar) - { - bool dataWritten; - bool success; - success=Read(dataWritten); - if (dataWritten) - success=ReadCompressed(outTemplateVar); - return success; - } - - /// \brief Read a bool from a bitstream. - /// \param[in] outTemplateVar The value to read - template <> - inline bool BitStream::ReadCompressedDelta(bool &outTemplateVar) - { - return Read(outTemplateVar); - } - - template - void BitStream::WriteCasted( const sourceType &value ) - { - destinationType val = (destinationType) value; - Write(val); - } - - template - void BitStream::WriteBitsFromIntegerRange( const templateType value, const templateType minimum,const templateType maximum, bool allowOutsideRange ) - { - int requiredBits=BYTES_TO_BITS(sizeof(templateType))-NumberOfLeadingZeroes(templateType(maximum-minimum)); - WriteBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange); - } - template - void BitStream::WriteBitsFromIntegerRange( const templateType value, const templateType minimum,const templateType maximum, const int requiredBits, bool allowOutsideRange ) - { - RakAssert(maximum>=minimum); - RakAssert(allowOutsideRange==true || (value>=minimum && value<=maximum)); - if (allowOutsideRange) - { - if (valuemaximum) - { - Write(true); - Write(value); - return; - } - Write(false); - } - templateType valueOffMin=value-minimum; - if (IsBigEndian()==true) - { - unsigned char output[sizeof(templateType)]; - ReverseBytes((unsigned char*)&valueOffMin, output, sizeof(templateType)); - WriteBits(output,requiredBits); - } - else - { - WriteBits((unsigned char*) &valueOffMin,requiredBits); - } - } - - template // templateType for this function must be a float or double - void BitStream::WriteNormVector( templateType x, templateType y, templateType z ) - { -#ifdef _DEBUG - RakAssert(x <= 1.01 && y <= 1.01 && z <= 1.01 && x >= -1.01 && y >= -1.01 && z >= -1.01); -#endif - - WriteFloat16((float)x,-1.0f,1.0f); - WriteFloat16((float)y,-1.0f,1.0f); - WriteFloat16((float)z,-1.0f,1.0f); - } - - template // templateType for this function must be a float or double - void BitStream::WriteVector( templateType x, templateType y, templateType z ) - { - templateType magnitude = sqrt(x * x + y * y + z * z); - Write((float)magnitude); - if (magnitude > 0.00001f) - { - WriteCompressed((float)(x/magnitude)); - WriteCompressed((float)(y/magnitude)); - WriteCompressed((float)(z/magnitude)); - // Write((unsigned short)((x/magnitude+1.0f)*32767.5f)); - // Write((unsigned short)((y/magnitude+1.0f)*32767.5f)); - // Write((unsigned short)((z/magnitude+1.0f)*32767.5f)); - } - } - - template // templateType for this function must be a float or double - void BitStream::WriteNormQuat( templateType w, templateType x, templateType y, templateType z) - { - Write((bool)(w<0.0)); - Write((bool)(x<0.0)); - Write((bool)(y<0.0)); - Write((bool)(z<0.0)); - Write((unsigned short)(fabs(x)*65535.0)); - Write((unsigned short)(fabs(y)*65535.0)); - Write((unsigned short)(fabs(z)*65535.0)); - // Leave out w and calculate it on the target - } - - template // templateType for this function must be a float or double - void BitStream::WriteOrthMatrix( - templateType m00, templateType m01, templateType m02, - templateType m10, templateType m11, templateType m12, - templateType m20, templateType m21, templateType m22 ) - { - - double qw; - double qx; - double qy; - double qz; - - // Convert matrix to quat - // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/ - float sum; - sum = 1 + m00 + m11 + m22; - if (sum < 0.0f) sum=0.0f; - qw = sqrt( sum ) / 2; - sum = 1 + m00 - m11 - m22; - if (sum < 0.0f) sum=0.0f; - qx = sqrt( sum ) / 2; - sum = 1 - m00 + m11 - m22; - if (sum < 0.0f) sum=0.0f; - qy = sqrt( sum ) / 2; - sum = 1 - m00 - m11 + m22; - if (sum < 0.0f) sum=0.0f; - qz = sqrt( sum ) / 2; - if (qw < 0.0) qw=0.0; - if (qx < 0.0) qx=0.0; - if (qy < 0.0) qy=0.0; - if (qz < 0.0) qz=0.0; - qx = _copysign( (double) qx, (double) (m21 - m12) ); - qy = _copysign( (double) qy, (double) (m02 - m20) ); - qz = _copysign( (double) qz, (double) (m10 - m01) ); - - WriteNormQuat(qw,qx,qy,qz); - } - - template - bool BitStream::ReadCasted( sourceType &value ) - { - serializationType val; - bool success = Read(val); - value=(sourceType) val; - return success; - } - - template - bool BitStream::ReadBitsFromIntegerRange( templateType &value, const templateType minimum, const templateType maximum, bool allowOutsideRange ) - { - int requiredBits=BYTES_TO_BITS(sizeof(templateType))-NumberOfLeadingZeroes(templateType(maximum-minimum)); - return ReadBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange); - } - template - bool BitStream::ReadBitsFromIntegerRange( templateType &value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange ) - { - RakAssert(maximum>=minimum); - if (allowOutsideRange) - { - bool isOutsideRange; - Read(isOutsideRange); - if (isOutsideRange) - return Read(value); - } - unsigned char output[sizeof(templateType)]; - memset(output,0,sizeof(output)); - bool success = ReadBits(output,requiredBits); - if (success) - { - if (IsBigEndian()==true) - ReverseBytesInPlace(output,sizeof(output)); - memcpy(&value,output,sizeof(output)); - - value+=minimum; - } - - return success; - } - - template // templateType for this function must be a float or double - bool BitStream::ReadNormVector( templateType &x, templateType &y, templateType &z ) - { - float xIn,yIn,zIn; - ReadFloat16(xIn,-1.0f,1.0f); - ReadFloat16(yIn,-1.0f,1.0f); - ReadFloat16(zIn,-1.0f,1.0f); - x=xIn; - y=yIn; - z=zIn; - return true; - } - - template // templateType for this function must be a float or double - bool BitStream::ReadVector( templateType &x, templateType &y, templateType &z ) - { - float magnitude; - //unsigned short sx,sy,sz; - if (!Read(magnitude)) - return false; - if (magnitude>0.00001f) - { - // Read(sx); - // Read(sy); - // if (!Read(sz)) - // return false; - // x=((float)sx / 32767.5f - 1.0f) * magnitude; - // y=((float)sy / 32767.5f - 1.0f) * magnitude; - // z=((float)sz / 32767.5f - 1.0f) * magnitude; - float cx=0.0f,cy=0.0f,cz=0.0f; - ReadCompressed(cx); - ReadCompressed(cy); - if (!ReadCompressed(cz)) - return false; - x=cx; - y=cy; - z=cz; - x*=magnitude; - y*=magnitude; - z*=magnitude; - } - else - { - x=0.0; - y=0.0; - z=0.0; - } - return true; - } - - template // templateType for this function must be a float or double - bool BitStream::ReadNormQuat( templateType &w, templateType &x, templateType &y, templateType &z) - { - bool cwNeg=false, cxNeg=false, cyNeg=false, czNeg=false; - unsigned short cx,cy,cz; - Read(cwNeg); - Read(cxNeg); - Read(cyNeg); - Read(czNeg); - Read(cx); - Read(cy); - if (!Read(cz)) - return false; - - // Calculate w from x,y,z - x=(templateType)(cx/65535.0); - y=(templateType)(cy/65535.0); - z=(templateType)(cz/65535.0); - if (cxNeg) x=-x; - if (cyNeg) y=-y; - if (czNeg) z=-z; - float difference = 1.0f - x*x - y*y - z*z; - if (difference < 0.0f) - difference=0.0f; - w = (templateType)(sqrt(difference)); - if (cwNeg) - w=-w; - - return true; - } - - template // templateType for this function must be a float or double - bool BitStream::ReadOrthMatrix( - templateType &m00, templateType &m01, templateType &m02, - templateType &m10, templateType &m11, templateType &m12, - templateType &m20, templateType &m21, templateType &m22 ) - { - float qw,qx,qy,qz; - if (!ReadNormQuat(qw,qx,qy,qz)) - return false; - - // Quat to orthogonal rotation matrix - // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/index.htm - double sqw = (double)qw*(double)qw; - double sqx = (double)qx*(double)qx; - double sqy = (double)qy*(double)qy; - double sqz = (double)qz*(double)qz; - m00 = (templateType)(sqx - sqy - sqz + sqw); // since sqw + sqx + sqy + sqz =1 - m11 = (templateType)(-sqx + sqy - sqz + sqw); - m22 = (templateType)(-sqx - sqy + sqz + sqw); - - double tmp1 = (double)qx*(double)qy; - double tmp2 = (double)qz*(double)qw; - m10 = (templateType)(2.0 * (tmp1 + tmp2)); - m01 = (templateType)(2.0 * (tmp1 - tmp2)); - - tmp1 = (double)qx*(double)qz; - tmp2 = (double)qy*(double)qw; - m20 =(templateType)(2.0 * (tmp1 - tmp2)); - m02 = (templateType)(2.0 * (tmp1 + tmp2)); - tmp1 = (double)qy*(double)qz; - tmp2 = (double)qx*(double)qw; - m21 = (templateType)(2.0 * (tmp1 + tmp2)); - m12 = (templateType)(2.0 * (tmp1 - tmp2)); - - return true; - } - - template - BitStream& operator<<(BitStream& out, templateType& c) - { - out.Write(c); - return out; - } - template - BitStream& operator>>(BitStream& in, templateType& c) - { - bool success = in.Read(c); - (void)success; - - RakAssert(success); - return in; - } - -} - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif - -#endif - -#endif // VC6 +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file BitStream.h +/// \brief This class allows you to write and read native types as a string of bits. +/// \details BitStream is used extensively throughout RakNet and is designed to be used by users as well. +/// + +#pragma once + +#include "RakMemoryOverride.h" +#include "RakNetDefines.h" +#include "Export.h" +#include "RakNetTypes.h" +#include "RakString.h" +#include "RakWString.h" +#include "RakAssert.h" +#include +#include + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +// MSWin uses _copysign, others use copysign... +#ifndef _WIN32 +#define _copysign copysign +#endif + +namespace RakNet +{ + /// This class allows you to write and read native types as a string of bits. BitStream is used extensively throughout RakNet and is designed to be used by users as well. + /// \sa BitStreamSample.txt + class RAK_DLL_EXPORT BitStream + { + + public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(BitStream) + + /// Default Constructor + BitStream(); + + /// \brief Create the bitstream, with some number of bytes to immediately allocate. + /// \details There is no benefit to calling this, unless you know exactly how many bytes you need and it is greater than BITSTREAM_STACK_ALLOCATION_SIZE. + /// In that case all it does is save you one or more realloc calls. + /// \param[in] initialBytesToAllocate the number of bytes to pre-allocate. + BitStream( const unsigned int initialBytesToAllocate ); + + /// \brief Initialize the BitStream, immediately setting the data it contains to a predefined pointer. + /// \details Set \a _copyData to true if you want to make an internal copy of the data you are passing. Set it to false to just save a pointer to the data. + /// You shouldn't call Write functions with \a _copyData as false, as this will write to unallocated memory + /// 99% of the time you will use this function to cast Packet::data to a bitstream for reading, in which case you should write something as follows: + /// \code + /// RakNet::BitStream bs(packet->data, packet->length, false); + /// \endcode + /// \param[in] _data An array of bytes. + /// \param[in] lengthInBytes Size of the \a _data. + /// \param[in] _copyData true or false to make a copy of \a _data or not. + BitStream( unsigned char* _data, const unsigned int lengthInBytes, bool _copyData ); + + // Destructor + ~BitStream(); + + /// Resets the bitstream for reuse. + void Reset( void ); + + /// \brief Bidirectional serialize/deserialize any integral type to/from a bitstream. + /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. + /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data + /// \param[in] inOutTemplateVar The value to write + /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. + template + bool Serialize(bool writeToBitstream, templateType &inOutTemplateVar); + + /// \brief Bidirectional serialize/deserialize any integral type to/from a bitstream. + /// \details If the current value is different from the last value + /// the current value will be written. Otherwise, a single bit will be written + /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data + /// \param[in] inOutCurrentValue The current value to write + /// \param[in] lastValue The last value to compare against. Only used if \a writeToBitstream is true. + /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. + template + bool SerializeDelta(bool writeToBitstream, templateType &inOutCurrentValue, const templateType &lastValue); + + /// \brief Bidirectional version of SerializeDelta when you don't know what the last value is, or there is no last value. + /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data + /// \param[in] inOutCurrentValue The current value to write + /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. + template + bool SerializeDelta(bool writeToBitstream, templateType &inOutCurrentValue); + + /// \brief Bidirectional serialize/deserialize any integral type to/from a bitstream. + /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. + /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte + /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. + /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type + /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data + /// \param[in] inOutTemplateVar The value to write + /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. + template + bool SerializeCompressed(bool writeToBitstream, templateType &inOutTemplateVar); + + /// \brief Bidirectional serialize/deserialize any integral type to/from a bitstream. + /// \details If the current value is different from the last value + /// the current value will be written. Otherwise, a single bit will be written + /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. + /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type + /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte + /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data + /// \param[in] inOutCurrentValue The current value to write + /// \param[in] lastValue The last value to compare against. Only used if \a writeToBitstream is true. + /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. + template + bool SerializeCompressedDelta(bool writeToBitstream, templateType &inOutCurrentValue, const templateType &lastValue); + + /// \brief Save as SerializeCompressedDelta(templateType ¤tValue, const templateType &lastValue) when we have an unknown second parameter + /// \return true on data read. False on insufficient data in bitstream + template + bool SerializeCompressedDelta(bool writeToBitstream, templateType &inOutTemplateVar); + + /// \brief Bidirectional serialize/deserialize an array or casted stream or raw data. This does NOT do endian swapping. + /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data + /// \param[in] inOutByteArray a byte buffer + /// \param[in] numberOfBytes the size of \a input in bytes + /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. + bool Serialize(bool writeToBitstream, char* inOutByteArray, const unsigned int numberOfBytes ); + + /// \brief Serialize a float into 2 bytes, spanning the range between \a floatMin and \a floatMax + /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data + /// \param[in] inOutFloat The float to write + /// \param[in] floatMin Predetermined minimum value of f + /// \param[in] floatMax Predetermined maximum value of f + bool SerializeFloat16(bool writeToBitstream, float &inOutFloat, float floatMin, float floatMax); + + /// Serialize one type casted to another (smaller) type, to save bandwidth + /// serializationType should be uint8_t, uint16_t, uint24_t, or uint32_t + /// Example: int num=53; SerializeCasted(true, num); would use 1 byte to write what would otherwise be an integer (4 or 8 bytes) + /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data + /// \param[in] value The value to serialize + template + bool SerializeCasted( bool writeToBitstream, sourceType &value ); + + /// Given the minimum and maximum values for an integer type, figure out the minimum number of bits to represent the range + /// Then serialize only those bits + /// \note A static is used so that the required number of bits for (maximum-minimum) is only calculated once. This does require that \a minimum and \maximum are fixed values for a given line of code for the life of the program + /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data + /// \param[in] value Integer value to write, which should be between \a minimum and \a maximum + /// \param[in] minimum Minimum value of \a value + /// \param[in] maximum Maximum value of \a value + /// \param[in] allowOutsideRange If true, all sends will take an extra bit, however value can deviate from outside \a minimum and \a maximum. If false, will assert if the value deviates + template + bool SerializeBitsFromIntegerRange( bool writeToBitstream, templateType &value, const templateType minimum, const templateType maximum, bool allowOutsideRange=false ); + /// \param[in] requiredBits Primarily for internal use, called from above function() after calculating number of bits needed to represent maximum-minimum + template + bool SerializeBitsFromIntegerRange( bool writeToBitstream, templateType &value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange=false ); + + /// \brief Bidirectional serialize/deserialize a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes. + /// \details Will further compress y or z axis aligned vectors. + /// Accurate to 1/32767.5. + /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data + /// \param[in] x x + /// \param[in] y y + /// \param[in] z z + /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. + template // templateType for this function must be a float or double + bool SerializeNormVector(bool writeToBitstream, templateType &x, templateType &y, templateType &z ); + + /// \brief Bidirectional serialize/deserialize a vector, using 10 bytes instead of 12. + /// \details Loses accuracy to about 3/10ths and only saves 2 bytes, so only use if accuracy is not important. + /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data + /// \param[in] x x + /// \param[in] y y + /// \param[in] z z + /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. + template // templateType for this function must be a float or double + bool SerializeVector(bool writeToBitstream, templateType &x, templateType &y, templateType &z ); + + /// \brief Bidirectional serialize/deserialize a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. Slightly lossy. + /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data + /// \param[in] w w + /// \param[in] x x + /// \param[in] y y + /// \param[in] z z + /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. + template // templateType for this function must be a float or double + bool SerializeNormQuat(bool writeToBitstream, templateType &w, templateType &x, templateType &y, templateType &z); + + /// \brief Bidirectional serialize/deserialize an orthogonal matrix by creating a quaternion, and writing 3 components of the quaternion in 2 bytes each. + /// \details Use 6 bytes instead of 36 + /// Lossy, although the result is renormalized + /// \return true on success, false on failure. + template // templateType for this function must be a float or double + bool SerializeOrthMatrix( + bool writeToBitstream, + templateType &m00, templateType &m01, templateType &m02, + templateType &m10, templateType &m11, templateType &m12, + templateType &m20, templateType &m21, templateType &m22 ); + + /// \brief Bidirectional serialize/deserialize numberToSerialize bits to/from the input. + /// \details Right aligned data means in the case of a partial byte, the bits are aligned + /// from the right (bit 0) rather than the left (as in the normal + /// internal representation) You would set this to true when + /// writing user data, and false when copying bitstream data, such + /// as writing one bitstream to another + /// \param[in] writeToBitstream true to write from your data to this bitstream. False to read from this bitstream and write to your data + /// \param[in] inOutByteArray The data + /// \param[in] numberOfBitsToSerialize The number of bits to write + /// \param[in] rightAlignedBits if true data will be right aligned + /// \return true if \a writeToBitstream is true. true if \a writeToBitstream is false and the read was successful. false if \a writeToBitstream is false and the read was not successful. + bool SerializeBits(bool writeToBitstream, unsigned char* inOutByteArray, const BitSize_t numberOfBitsToSerialize, const bool rightAlignedBits = true ); + + /// \brief Write any integral type to a bitstream. + /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. + /// \param[in] inTemplateVar The value to write + template + void Write(const templateType &inTemplateVar); + + /// \brief Write the dereferenced pointer to any integral type to a bitstream. + /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. + /// \param[in] inTemplateVar The value to write + template + void WritePtr(templateType *inTemplateVar); + + /// \brief Write any integral type to a bitstream. + /// \details If the current value is different from the last value + /// the current value will be written. Otherwise, a single bit will be written + /// \param[in] currentValue The current value to write + /// \param[in] lastValue The last value to compare against + template + void WriteDelta(const templateType ¤tValue, const templateType &lastValue); + + /// \brief WriteDelta when you don't know what the last value is, or there is no last value. + /// \param[in] currentValue The current value to write + template + void WriteDelta(const templateType ¤tValue); + + /// \brief Write any integral type to a bitstream. + /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. + /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte + /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. + /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type + /// \param[in] inTemplateVar The value to write + template + void WriteCompressed(const templateType &inTemplateVar); + + /// \brief Write any integral type to a bitstream. + /// \details If the current value is different from the last value + /// the current value will be written. Otherwise, a single bit will be written + /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. + /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type + /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte + /// \param[in] currentValue The current value to write + /// \param[in] lastValue The last value to compare against + template + void WriteCompressedDelta(const templateType ¤tValue, const templateType &lastValue); + + /// \brief Save as WriteCompressedDelta(const templateType ¤tValue, const templateType &lastValue) when we have an unknown second parameter + template + void WriteCompressedDelta(const templateType ¤tValue); + + /// \brief Read any integral type from a bitstream. + /// \details Define __BITSTREAM_NATIVE_END if you need endian swapping. + /// \param[in] outTemplateVar The value to read + /// \return true on success, false on failure. + template + bool Read(templateType &outTemplateVar); + + /// \brief Read any integral type from a bitstream. + /// \details If the written value differed from the value compared against in the write function, + /// var will be updated. Otherwise it will retain the current value. + /// ReadDelta is only valid from a previous call to WriteDelta + /// \param[in] outTemplateVar The value to read + /// \return true on success, false on failure. + template + bool ReadDelta(templateType &outTemplateVar); + + /// \brief Read any integral type from a bitstream. + /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. + /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. + /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type + /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte + /// \param[in] outTemplateVar The value to read + /// \return true on success, false on failure. + template + bool ReadCompressed(templateType &outTemplateVar); + + /// \brief Read any integral type from a bitstream. + /// \details If the written value differed from the value compared against in the write function, + /// var will be updated. Otherwise it will retain the current value. + /// the current value will be updated. + /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. + /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type + /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte + /// ReadCompressedDelta is only valid from a previous call to WriteDelta + /// \param[in] outTemplateVar The value to read + /// \return true on success, false on failure. + template + bool ReadCompressedDelta(templateType &outTemplateVar); + + /// \brief Read one bitstream to another. + /// \param[in] numberOfBits bits to read + /// \param bitStream the bitstream to read into from + /// \return true on success, false on failure. + bool Read( BitStream *bitStream, BitSize_t numberOfBits ); + bool Read( BitStream *bitStream ); + bool Read( BitStream &bitStream, BitSize_t numberOfBits ); + bool Read( BitStream &bitStream ); + + /// \brief Write an array or casted stream or raw data. This does NOT do endian swapping. + /// \param[in] inputByteArray a byte buffer + /// \param[in] numberOfBytes the size of \a input in bytes + void Write( const char* inputByteArray, const unsigned int numberOfBytes ); + + /// \brief Write one bitstream to another. + /// \param[in] numberOfBits bits to write + /// \param bitStream the bitstream to copy from + void Write( BitStream *bitStream, BitSize_t numberOfBits ); + void Write( BitStream *bitStream ); + void Write( BitStream &bitStream, BitSize_t numberOfBits ); + void Write( BitStream &bitStream );\ + + /// \brief Write a float into 2 bytes, spanning the range between \a floatMin and \a floatMax + /// \param[in] x The float to write + /// \param[in] floatMin Predetermined minimum value of f + /// \param[in] floatMax Predetermined maximum value of f + void WriteFloat16( float x, float floatMin, float floatMax ); + + /// Write one type serialized as another (smaller) type, to save bandwidth + /// serializationType should be uint8_t, uint16_t, uint24_t, or uint32_t + /// Example: int num=53; WriteCasted(num); would use 1 byte to write what would otherwise be an integer (4 or 8 bytes) + /// \param[in] value The value to write + template + void WriteCasted( const sourceType &value ); + + /// Given the minimum and maximum values for an integer type, figure out the minimum number of bits to represent the range + /// Then write only those bits + /// \note A static is used so that the required number of bits for (maximum-minimum) is only calculated once. This does require that \a minimum and \maximum are fixed values for a given line of code for the life of the program + /// \param[in] value Integer value to write, which should be between \a minimum and \a maximum + /// \param[in] minimum Minimum value of \a value + /// \param[in] maximum Maximum value of \a value + /// \param[in] allowOutsideRange If true, all sends will take an extra bit, however value can deviate from outside \a minimum and \a maximum. If false, will assert if the value deviates. This should match the corresponding value passed to Read(). + template + void WriteBitsFromIntegerRange( const templateType value, const templateType minimum, const templateType maximum, bool allowOutsideRange=false ); + /// \param[in] requiredBits Primarily for internal use, called from above function() after calculating number of bits needed to represent maximum-minimum + template + void WriteBitsFromIntegerRange( const templateType value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange=false ); + + /// \brief Write a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes. + /// \details Will further compress y or z axis aligned vectors. + /// Accurate to 1/32767.5. + /// \param[in] x x + /// \param[in] y y + /// \param[in] z z + template // templateType for this function must be a float or double + void WriteNormVector( templateType x, templateType y, templateType z ); + + /// \brief Write a vector, using 10 bytes instead of 12. + /// \details Loses accuracy to about 3/10ths and only saves 2 bytes, + /// so only use if accuracy is not important. + /// \param[in] x x + /// \param[in] y y + /// \param[in] z z + template // templateType for this function must be a float or double + void WriteVector( templateType x, templateType y, templateType z ); + + /// \brief Write a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. Slightly lossy. + /// \param[in] w w + /// \param[in] x x + /// \param[in] y y + /// \param[in] z z + template // templateType for this function must be a float or double + void WriteNormQuat( templateType w, templateType x, templateType y, templateType z); + + /// \brief Write an orthogonal matrix by creating a quaternion, and writing 3 components of the quaternion in 2 bytes each. + /// \details Use 6 bytes instead of 36 + /// Lossy, although the result is renormalized + template // templateType for this function must be a float or double + void WriteOrthMatrix( + templateType m00, templateType m01, templateType m02, + templateType m10, templateType m11, templateType m12, + templateType m20, templateType m21, templateType m22 ); + + /// \brief Read an array or casted stream of byte. + /// \details The array is raw data. There is no automatic endian conversion with this function + /// \param[in] output The result byte array. It should be larger than @em numberOfBytes. + /// \param[in] numberOfBytes The number of byte to read + /// \return true on success false if there is some missing bytes. + bool Read( char* output, const unsigned int numberOfBytes ); + + /// \brief Read a float into 2 bytes, spanning the range between \a floatMin and \a floatMax + /// \param[in] outFloat The float to read + /// \param[in] floatMin Predetermined minimum value of f + /// \param[in] floatMax Predetermined maximum value of f + bool ReadFloat16( float &outFloat, float floatMin, float floatMax ); + + /// Read one type serialized to another (smaller) type, to save bandwidth + /// serializationType should be uint8_t, uint16_t, uint24_t, or uint32_t + /// Example: int num; ReadCasted(num); would read 1 bytefrom the stream, and put the value in an integer + /// \param[in] value The value to write + template + bool ReadCasted( sourceType &value ); + + /// Given the minimum and maximum values for an integer type, figure out the minimum number of bits to represent the range + /// Then read only those bits + /// \note A static is used so that the required number of bits for (maximum-minimum) is only calculated once. This does require that \a minimum and \maximum are fixed values for a given line of code for the life of the program + /// \param[in] value Integer value to read, which should be between \a minimum and \a maximum + /// \param[in] minimum Minimum value of \a value + /// \param[in] maximum Maximum value of \a value + /// \param[in] allowOutsideRange If true, all sends will take an extra bit, however value can deviate from outside \a minimum and \a maximum. If false, will assert if the value deviates. This should match the corresponding value passed to Write(). + template + bool ReadBitsFromIntegerRange( templateType &value, const templateType minimum, const templateType maximum, bool allowOutsideRange=false ); + /// \param[in] requiredBits Primarily for internal use, called from above function() after calculating number of bits needed to represent maximum-minimum + template + bool ReadBitsFromIntegerRange( templateType &value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange=false ); + + /// \brief Read a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes. + /// \details Will further compress y or z axis aligned vectors. + /// Accurate to 1/32767.5. + /// \param[in] x x + /// \param[in] y y + /// \param[in] z z + /// \return true on success, false on failure. + template // templateType for this function must be a float or double + bool ReadNormVector( templateType &x, templateType &y, templateType &z ); + + /// \brief Read 3 floats or doubles, using 10 bytes, where those float or doubles comprise a vector. + /// \details Loses accuracy to about 3/10ths and only saves 2 bytes, + /// so only use if accuracy is not important. + /// \param[in] x x + /// \param[in] y y + /// \param[in] z z + /// \return true on success, false on failure. + template // templateType for this function must be a float or double + bool ReadVector( templateType &x, templateType &y, templateType &z ); + + /// \brief Read a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. + /// \param[in] w w + /// \param[in] x x + /// \param[in] y y + /// \param[in] z z + /// \return true on success, false on failure. + template // templateType for this function must be a float or double + bool ReadNormQuat( templateType &w, templateType &x, templateType &y, templateType &z); + + /// \brief Read an orthogonal matrix from a quaternion, reading 3 components of the quaternion in 2 bytes each and extrapolatig the 4th. + /// \details Use 6 bytes instead of 36 + /// Lossy, although the result is renormalized + /// \return true on success, false on failure. + template // templateType for this function must be a float or double + bool ReadOrthMatrix( + templateType &m00, templateType &m01, templateType &m02, + templateType &m10, templateType &m11, templateType &m12, + templateType &m20, templateType &m21, templateType &m22 ); + + /// \brief Sets the read pointer back to the beginning of your data. + void ResetReadPointer( void ); + + /// \brief Sets the write pointer back to the beginning of your data. + void ResetWritePointer( void ); + + /// \brief This is good to call when you are done with the stream to make + /// sure you didn't leave any data left over void + void AssertStreamEmpty( void ); + + /// \brief RAKNET_DEBUG_PRINTF the bits in the stream. Great for debugging. + void PrintBits( char *out ) const; + void PrintBits( void ) const; + void PrintHex( char *out ) const; + void PrintHex( void ) const; + + /// \brief Ignore data we don't intend to read + /// \param[in] numberOfBits The number of bits to ignore + void IgnoreBits( const BitSize_t numberOfBits ); + + /// \brief Ignore data we don't intend to read + /// \param[in] numberOfBits The number of bytes to ignore + void IgnoreBytes( const unsigned int numberOfBytes ); + + /// \brief Move the write pointer to a position on the array. + /// \param[in] offset the offset from the start of the array. + /// \attention + /// \details Dangerous if you don't know what you are doing! + /// For efficiency reasons you can only write mid-stream if your data is byte aligned. + void SetWriteOffset( const BitSize_t offset ); + + /// \brief Returns the length in bits of the stream + inline BitSize_t GetNumberOfBitsUsed( void ) const {return GetWriteOffset();} + inline BitSize_t GetWriteOffset( void ) const {return numberOfBitsUsed;} + + /// \brief Returns the length in bytes of the stream + inline BitSize_t GetNumberOfBytesUsed( void ) const {return BITS_TO_BYTES( numberOfBitsUsed );} + + /// \brief Returns the number of bits into the stream that we have read + inline BitSize_t GetReadOffset( void ) const {return readOffset;} + + /// \brief Sets the read bit index + void SetReadOffset( const BitSize_t newReadOffset ) {readOffset=newReadOffset;} + + /// \brief Returns the number of bits left in the stream that haven't been read + inline BitSize_t GetNumberOfUnreadBits( void ) const {return numberOfBitsUsed - readOffset;} + + /// \brief Makes a copy of the internal data for you \a _data will point to + /// the stream. Partial bytes are left aligned. + /// \param[out] _data The allocated copy of GetData() + /// \return The length in bits of the stream. + BitSize_t CopyData( unsigned char** _data ) const; + + /// \internal + /// Set the stream to some initial data. + void SetData( unsigned char *inByteArray ); + + /// Gets the data that BitStream is writing to / reading from. + /// Partial bytes are left aligned. + /// \return A pointer to the internal state + inline unsigned char* GetData( void ) const {return data;} + + /// \brief Write numberToWrite bits from the input source. + /// \details Right aligned data means in the case of a partial byte, the bits are aligned + /// from the right (bit 0) rather than the left (as in the normal + /// internal representation) You would set this to true when + /// writing user data, and false when copying bitstream data, such + /// as writing one bitstream to another. + /// \param[in] inByteArray The data + /// \param[in] numberOfBitsToWrite The number of bits to write + /// \param[in] rightAlignedBits if true data will be right aligned + void WriteBits( const unsigned char* inByteArray, BitSize_t numberOfBitsToWrite, const bool rightAlignedBits = true ); + + /// \brief Align the bitstream to the byte boundary and then write the + /// specified number of bits. + /// \details This is faster than WriteBits but + /// wastes the bits to do the alignment and requires you to call + /// ReadAlignedBits at the corresponding read position. + /// \param[in] inByteArray The data + /// \param[in] numberOfBytesToWrite The size of input. + void WriteAlignedBytes( const unsigned char *inByteArray, const unsigned int numberOfBytesToWrite ); + + // Endian swap bytes already in the bitstream + void EndianSwapBytes( int byteOffset, int length ); + + /// \brief Aligns the bitstream, writes inputLength, and writes input. Won't write beyond maxBytesToWrite + /// \param[in] inByteArray The data + /// \param[in] inputLength The size of input. + /// \param[in] maxBytesToWrite Max bytes to write + void WriteAlignedBytesSafe( const char *inByteArray, const unsigned int inputLength, const unsigned int maxBytesToWrite ); + + /// \brief Read bits, starting at the next aligned bits. + /// \details Note that the modulus 8 starting offset of the sequence must be the same as + /// was used with WriteBits. This will be a problem with packet + /// coalescence unless you byte align the coalesced packets. + /// \param[in] inOutByteArray The byte array larger than @em numberOfBytesToRead + /// \param[in] numberOfBytesToRead The number of byte to read from the internal state + /// \return true if there is enough byte. + bool ReadAlignedBytes( unsigned char *inOutByteArray, const unsigned int numberOfBytesToRead ); + + /// \brief Reads what was written by WriteAlignedBytesSafe. + /// \param[in] inOutByteArray The data + /// \param[in] maxBytesToRead Maximum number of bytes to read + /// \return true on success, false on failure. + bool ReadAlignedBytesSafe( char *inOutByteArray, int &inputLength, const int maxBytesToRead ); + bool ReadAlignedBytesSafe( char *inOutByteArray, unsigned int &inputLength, const unsigned int maxBytesToRead ); + + /// \brief Same as ReadAlignedBytesSafe() but allocates the memory for you using new, rather than assuming it is safe to write to + /// \param[in] outByteArray outByteArray will be deleted if it is not a pointer to 0 + /// \return true on success, false on failure. + bool ReadAlignedBytesSafeAlloc( char **outByteArray, int &inputLength, const unsigned int maxBytesToRead ); + bool ReadAlignedBytesSafeAlloc( char **outByteArray, unsigned int &inputLength, const unsigned int maxBytesToRead ); + + /// \brief Align the next write and/or read to a byte boundary. + /// \details This can be used to 'waste' bits to byte align for efficiency reasons It + /// can also be used to force coalesced bitstreams to start on byte + /// boundaries so so WriteAlignedBits and ReadAlignedBits both + /// calculate the same offset when aligning. + inline void AlignWriteToByteBoundary( void ) {numberOfBitsUsed += 8 - ( (( numberOfBitsUsed - 1 ) & 7) + 1 );} + + /// \brief Align the next write and/or read to a byte boundary. + /// \details This can be used to 'waste' bits to byte align for efficiency reasons It + /// can also be used to force coalesced bitstreams to start on byte + /// boundaries so so WriteAlignedBits and ReadAlignedBits both + /// calculate the same offset when aligning. + inline void AlignReadToByteBoundary( void ) {readOffset += 8 - ( (( readOffset - 1 ) & 7 ) + 1 );} + + /// \brief Read \a numberOfBitsToRead bits to the output source. + /// \details alignBitsToRight should be set to true to convert internal + /// bitstream data to userdata. It should be false if you used + /// WriteBits with rightAlignedBits false + /// \param[in] inOutByteArray The resulting bits array + /// \param[in] numberOfBitsToRead The number of bits to read + /// \param[in] alignBitsToRight if true bits will be right aligned. + /// \return true if there is enough bits to read + bool ReadBits( unsigned char *inOutByteArray, BitSize_t numberOfBitsToRead, const bool alignBitsToRight = true ); + + /// \brief Write a 0 + void Write0( void ); + + /// \brief Write a 1 + void Write1( void ); + + /// \brief Reads 1 bit and returns true if that bit is 1 and false if it is 0. + bool ReadBit( void ); + + /// \brief If we used the constructor version with copy data off, this + /// *makes sure it is set to on and the data pointed to is copied. + void AssertCopyData( void ); + + /// \brief Use this if you pass a pointer copy to the constructor + /// *(_copyData==false) and want to overallocate to prevent + /// reallocation. + void SetNumberOfBitsAllocated( const BitSize_t lengthInBits ); + + /// \brief Reallocates (if necessary) in preparation of writing numberOfBitsToWrite + void AddBitsAndReallocate( const BitSize_t numberOfBitsToWrite ); + + /// \internal + /// \return How many bits have been allocated internally + BitSize_t GetNumberOfBitsAllocated(void) const; + + /// \brief Read strings, non reference. + bool Read(char *varString); + bool Read(unsigned char *varString); + + /// Write zeros until the bitstream is filled up to \a bytes + void PadWithZeroToByteLength( unsigned int bytes ); + + /// Get the number of leading zeros for a number + /// \param[in] x Number to test + static int NumberOfLeadingZeroes( uint8_t x ); + static int NumberOfLeadingZeroes( uint16_t x ); + static int NumberOfLeadingZeroes( uint32_t x ); + static int NumberOfLeadingZeroes( uint64_t x ); + static int NumberOfLeadingZeroes( int8_t x ); + static int NumberOfLeadingZeroes( int16_t x ); + static int NumberOfLeadingZeroes( int32_t x ); + static int NumberOfLeadingZeroes( int64_t x ); + + /// \internal Unrolled inner loop, for when performance is critical + void WriteAlignedVar8(const char *inByteArray); + /// \internal Unrolled inner loop, for when performance is critical + bool ReadAlignedVar8(char *inOutByteArray); + /// \internal Unrolled inner loop, for when performance is critical + void WriteAlignedVar16(const char *inByteArray); + /// \internal Unrolled inner loop, for when performance is critical + bool ReadAlignedVar16(char *inOutByteArray); + /// \internal Unrolled inner loop, for when performance is critical + void WriteAlignedVar32(const char *inByteArray); + /// \internal Unrolled inner loop, for when performance is critical + bool ReadAlignedVar32(char *inOutByteArray); + + inline void Write(const char * const inStringVar) + { + RakString::Serialize(inStringVar, this); + } + inline void Write(const wchar_t * const inStringVar) + { + RakWString::Serialize(inStringVar, this); + } + inline void Write(const unsigned char * const inTemplateVar) + { + Write((const char*)inTemplateVar); + } + inline void Write(char * const inTemplateVar) + { + Write((const char*)inTemplateVar); + } + inline void Write(unsigned char * const inTemplateVar) + { + Write((const char*)inTemplateVar); + } + inline void WriteCompressed(const char * const inStringVar) + { + RakString::SerializeCompressed(inStringVar,this,0,false); + } + inline void WriteCompressed(const wchar_t * const inStringVar) + { + RakWString::Serialize(inStringVar,this); + } + inline void WriteCompressed(const unsigned char * const inTemplateVar) + { + WriteCompressed((const char*) inTemplateVar); + } + inline void WriteCompressed(char * const inTemplateVar) + { + WriteCompressed((const char*) inTemplateVar); + } + inline void WriteCompressed(unsigned char * const inTemplateVar) + { + WriteCompressed((const char*) inTemplateVar); + } + + /// ---- Member function template specialization declarations ---- + // Used for VC7 +#if defined(_MSC_VER) && _MSC_VER == 1300 + /// Write a bool to a bitstream. + /// \param[in] var The value to write + template <> + void Write(const bool &var); + + /// Write a systemAddress to a bitstream + /// \param[in] var The value to write + template <> + void Write(const SystemAddress &var); + + /// Write a uint24_t to a bitstream + /// \param[in] var The value to write + template <> + void Write(const uint24_t &var); + + /// Write a RakNetGUID to a bitsteam + /// \param[in] var The value to write + template <> + void Write(const RakNetGuid &var); + + /// Write a string to a bitstream + /// \param[in] var The value to write + template <> + void Write(const char* const &var); + template <> + void Write(const unsigned char* const &var); + template <> + void Write(char* const &var); + template <> + void Write(unsigned char* const &var); + template <> + void Write(const RakString &var); + template <> + void Write(const RakWString &var); + + /// \brief Write a systemAddress. + /// \details If the current value is different from the last value + /// the current value will be written. Otherwise, a single bit will be written + /// \param[in] currentValue The current value to write + /// \param[in] lastValue The last value to compare against + template <> + void WriteDelta(const SystemAddress ¤tValue, const SystemAddress &lastValue); + + template <> + void WriteDelta(const uint24_t ¤tValue, const uint24_t &lastValue); + + template <> + void WriteDelta(const RakNetGUID ¤tValue, const RakNetGUID &lastValue); + + /// \brief Write a bool delta. + /// \details Same thing as just calling Write + /// \param[in] currentValue The current value to write + /// \param[in] lastValue The last value to compare against + template <> + void WriteDelta(const bool ¤tValue, const bool &lastValue); + + template <> + void WriteCompressed(const SystemAddress &var); + + template <> + void WriteCompressed(const uint24_t &var); + + template <> + void WriteCompressed(const RakNetGUID &var); + + template <> + void WriteCompressed(const bool &var); + + /// For values between -1 and 1 + template <> + void WriteCompressed(const float &var); + + /// For values between -1 and 1 + template <> + void WriteCompressed(const double &var); + + /// Compressed string + template <> + void WriteCompressed(const char* var); + template <> + void WriteCompressed(const unsigned char* var); + template <> + void WriteCompressed(char* var); + template <> + void WriteCompressed(unsigned char* var); + template <> + void WriteCompressed(const RakString &var); + template <> + void WriteCompressed(const RakWString &var); + + /// \brief Write a bool delta. + /// \details Same thing as just calling Write + /// \param[in] currentValue The current value to write + /// \param[in] lastValue The last value to compare against + template <> + void WriteCompressedDelta(const bool ¤tValue, const bool &lastValue); + + /// \brief Save as WriteCompressedDelta(bool currentValue, const templateType &lastValue) + /// when we have an unknown second bool + template <> + void WriteCompressedDelta(const bool ¤tValue); + + /// \brief Read a bool from a bitstream. + /// \param[in] var The value to read + /// \return true on success, false on failure. + template <> + bool Read(bool &var); + + /// \brief Read a systemAddress from a bitstream. + /// \param[in] var The value to read + /// \return true on success, false on failure. + template <> + bool Read(SystemAddress &var); + + template <> + bool Read(uint24_t &var); + + template <> + bool Read(RakNetGUID &var); + + /// \brief Read a String from a bitstream. + /// \param[in] var The value to read + /// \return true on success, false on failure. + template <> + bool Read(char *&var); + template <> + bool Read(wchar_t *&var); + template <> + bool Read(unsigned char *&var); + template <> + bool Read(RakString &var); + template <> + bool Read(RakWString &var); + + /// \brief Read a bool from a bitstream. + /// \param[in] var The value to read + /// \return true on success, false on failure. + template <> + bool ReadDelta(bool &var); + + template <> + bool ReadCompressed(SystemAddress &var); + + template <> + bool ReadCompressed(uint24_t &var); + + template <> + bool ReadCompressed(RakNetGUID &var); + + template <> + bool ReadCompressed(bool &var); + + template <> + bool ReadCompressed(float &var); + + /// For values between -1 and 1 + /// \return true on success, false on failure. + template <> + bool ReadCompressed(double &var); + + template <> + bool ReadCompressed(char* &var); + template <> + bool ReadCompressed(wchar_t* &var); + template <> + bool ReadCompressed(unsigned char *&var); + template <> + bool ReadCompressed(RakString &var); + template <> + bool ReadCompressed(RakWString &var); + + /// \brief Read a bool from a bitstream. + /// \param[in] var The value to read + /// \return true on success, false on failure. + template <> + bool ReadCompressedDelta(bool &var); +#endif + + inline static bool DoEndianSwap(void) { +#ifndef __BITSTREAM_NATIVE_END + return IsNetworkOrder()==false; +#else + return false; +#endif + } + inline static bool IsBigEndian(void) + { + return IsNetworkOrder(); + } + inline static bool IsNetworkOrder(void) {bool r = IsNetworkOrderInternal(); return r;} + // Not inline, won't compile on PC due to winsock include errors + static bool IsNetworkOrderInternal(void); + static void ReverseBytes(unsigned char *inByteArray, unsigned char *inOutByteArray, const unsigned int length); + static void ReverseBytesInPlace(unsigned char *inOutData,const unsigned int length); + + private: + + BitStream( const BitStream &invalid) { + (void) invalid; + RakAssert(0); + } + + BitStream& operator = ( const BitStream& invalid ) { + (void) invalid; + RakAssert(0); + static BitStream i; + return i; + } + + /// \brief Assume the input source points to a native type, compress and write it. + void WriteCompressed( const unsigned char* inByteArray, const unsigned int size, const bool unsignedData ); + + /// \brief Assume the input source points to a compressed native type. Decompress and read it. + bool ReadCompressed( unsigned char* inOutByteArray, const unsigned int size, const bool unsignedData ); + + + BitSize_t numberOfBitsUsed; + + BitSize_t numberOfBitsAllocated; + + BitSize_t readOffset; + + unsigned char *data; + + /// true if the internal buffer is copy of the data passed to the constructor + bool copyData; + + /// BitStreams that use less than BITSTREAM_STACK_ALLOCATION_SIZE use the stack, rather than the heap to store data. It switches over if BITSTREAM_STACK_ALLOCATION_SIZE is exceeded + unsigned char stackData[BITSTREAM_STACK_ALLOCATION_SIZE]; + }; + + template + inline bool BitStream::Serialize(bool writeToBitstream, templateType &inOutTemplateVar) + { + if (writeToBitstream) + Write(inOutTemplateVar); + else + return Read(inOutTemplateVar); + return true; + } + + template + inline bool BitStream::SerializeDelta(bool writeToBitstream, templateType &inOutCurrentValue, const templateType &lastValue) + { + if (writeToBitstream) + WriteDelta(inOutCurrentValue, lastValue); + else + return ReadDelta(inOutCurrentValue); + return true; + } + + template + inline bool BitStream::SerializeDelta(bool writeToBitstream, templateType &inOutCurrentValue) + { + if (writeToBitstream) + WriteDelta(inOutCurrentValue); + else + return ReadDelta(inOutCurrentValue); + return true; + } + + template + inline bool BitStream::SerializeCompressed(bool writeToBitstream, templateType &inOutTemplateVar) + { + if (writeToBitstream) + WriteCompressed(inOutTemplateVar); + else + return ReadCompressed(inOutTemplateVar); + return true; + } + + template + inline bool BitStream::SerializeCompressedDelta(bool writeToBitstream, templateType &inOutCurrentValue, const templateType &lastValue) + { + if (writeToBitstream) + WriteCompressedDelta(inOutCurrentValue,lastValue); + else + return ReadCompressedDelta(inOutCurrentValue); + return true; + } +//Stoppedhere + template + inline bool BitStream::SerializeCompressedDelta(bool writeToBitstream, templateType &inOutCurrentValue) + { + if (writeToBitstream) + WriteCompressedDelta(inOutCurrentValue); + else + return ReadCompressedDelta(inOutCurrentValue); + return true; + } + + inline bool BitStream::Serialize(bool writeToBitstream, char* inOutByteArray, const unsigned int numberOfBytes ) + { + if (writeToBitstream) + Write(inOutByteArray, numberOfBytes); + else + return Read(inOutByteArray, numberOfBytes); + return true; + } + + template + bool BitStream::SerializeCasted( bool writeToBitstream, sourceType &value ) + { + if (writeToBitstream) WriteCasted(value); + else return ReadCasted(value); + return true; + } + + template + bool BitStream::SerializeBitsFromIntegerRange( bool writeToBitstream, templateType &value, const templateType minimum, const templateType maximum, bool allowOutsideRange ) + { + int requiredBits=BYTES_TO_BITS(sizeof(templateType))-NumberOfLeadingZeroes(templateType(maximum-minimum)); + return SerializeBitsFromIntegerRange(writeToBitstream,value,minimum,maximum,requiredBits,allowOutsideRange); + } + template + bool BitStream::SerializeBitsFromIntegerRange( bool writeToBitstream, templateType &value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange ) + { + if (writeToBitstream) WriteBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange); + else return ReadBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange); + return true; + } + + template + inline bool BitStream::SerializeNormVector(bool writeToBitstream, templateType &x, templateType &y, templateType &z ) + { + if (writeToBitstream) + WriteNormVector(x,y,z); + else + return ReadNormVector(x,y,z); + return true; + } + + template + inline bool BitStream::SerializeVector(bool writeToBitstream, templateType &x, templateType &y, templateType &z ) + { + if (writeToBitstream) + WriteVector(x,y,z); + else + return ReadVector(x,y,z); + return true; + } + + template + inline bool BitStream::SerializeNormQuat(bool writeToBitstream, templateType &w, templateType &x, templateType &y, templateType &z) + { + if (writeToBitstream) + WriteNormQuat(w,x,y,z); + else + return ReadNormQuat(w,x,y,z); + return true; + } + + template + inline bool BitStream::SerializeOrthMatrix( + bool writeToBitstream, + templateType &m00, templateType &m01, templateType &m02, + templateType &m10, templateType &m11, templateType &m12, + templateType &m20, templateType &m21, templateType &m22 ) + { + if (writeToBitstream) + WriteOrthMatrix(m00,m01,m02,m10,m11,m12,m20,m21,m22); + else + return ReadOrthMatrix(m00,m01,m02,m10,m11,m12,m20,m21,m22); + return true; + } + + inline bool BitStream::SerializeBits(bool writeToBitstream, unsigned char* inOutByteArray, const BitSize_t numberOfBitsToSerialize, const bool rightAlignedBits ) + { + if (writeToBitstream) + WriteBits(inOutByteArray,numberOfBitsToSerialize,rightAlignedBits); + else + return ReadBits(inOutByteArray,numberOfBitsToSerialize,rightAlignedBits); + return true; + } + + template + inline void BitStream::Write(const templateType &inTemplateVar) + { +#ifdef _MSC_VER +#pragma warning(disable:4127) // conditional expression is constant +#endif + if (sizeof(inTemplateVar)==1) + WriteBits( ( unsigned char* ) & inTemplateVar, sizeof( templateType ) * 8, true ); + else + { +#ifndef __BITSTREAM_NATIVE_END + if (DoEndianSwap()) + { + unsigned char output[sizeof(templateType)]; + ReverseBytes((unsigned char*)&inTemplateVar, output, sizeof(templateType)); + WriteBits( ( unsigned char* ) output, sizeof(templateType) * 8, true ); + } + else +#endif + WriteBits( ( unsigned char* ) & inTemplateVar, sizeof(templateType) * 8, true ); + } + } + + template + inline void BitStream::WritePtr(templateType *inTemplateVar) + { +#ifdef _MSC_VER +#pragma warning(disable:4127) // conditional expression is constant +#endif + if (sizeof(templateType)==1) + WriteBits( ( unsigned char* ) inTemplateVar, sizeof( templateType ) * 8, true ); + else + { +#ifndef __BITSTREAM_NATIVE_END + if (DoEndianSwap()) + { + unsigned char output[sizeof(templateType)]; + ReverseBytes((unsigned char*) inTemplateVar, output, sizeof(templateType)); + WriteBits( ( unsigned char* ) output, sizeof(templateType) * 8, true ); + } + else +#endif + WriteBits( ( unsigned char* ) inTemplateVar, sizeof(templateType) * 8, true ); + } + } + + /// \brief Write a bool to a bitstream. + /// \param[in] inTemplateVar The value to write + template <> + inline void BitStream::Write(const bool &inTemplateVar) + { + if ( inTemplateVar ) + Write1(); + else + Write0(); + } + + + /// \brief Write a systemAddress to a bitstream. + /// \param[in] inTemplateVar The value to write + template <> + inline void BitStream::Write(const SystemAddress &inTemplateVar) + { + Write(inTemplateVar.GetIPVersion()); + if (inTemplateVar.GetIPVersion()==4) + { + // Hide the address so routers don't modify it + SystemAddress var2=inTemplateVar; + uint32_t binaryAddress=~inTemplateVar.address.addr4.sin_addr.s_addr; + // Don't endian swap the address or port + WriteBits((unsigned char*)&binaryAddress, sizeof(binaryAddress)*8, true); + unsigned short p = var2.GetPortNetworkOrder(); + WriteBits((unsigned char*)&p, sizeof(unsigned short)*8, true); + } + else + { +#if RAKNET_SUPPORT_IPV6==1 + // Don't endian swap + WriteBits((const unsigned char*) &inTemplateVar.address.addr6, sizeof(inTemplateVar.address.addr6)*8, true); +#endif + } + } + + template <> + inline void BitStream::Write(const uint24_t &inTemplateVar) + { + AlignWriteToByteBoundary(); + AddBitsAndReallocate(3*8); + + if (IsBigEndian()==false) + { + data[( numberOfBitsUsed >> 3 ) + 0] = ((unsigned char *)&inTemplateVar.val)[0]; + data[( numberOfBitsUsed >> 3 ) + 1] = ((unsigned char *)&inTemplateVar.val)[1]; + data[( numberOfBitsUsed >> 3 ) + 2] = ((unsigned char *)&inTemplateVar.val)[2]; + } + else + { + data[( numberOfBitsUsed >> 3 ) + 0] = ((unsigned char *)&inTemplateVar.val)[3]; + data[( numberOfBitsUsed >> 3 ) + 1] = ((unsigned char *)&inTemplateVar.val)[2]; + data[( numberOfBitsUsed >> 3 ) + 2] = ((unsigned char *)&inTemplateVar.val)[1]; + } + + numberOfBitsUsed+=3*8; + } + + template <> + inline void BitStream::Write(const RakNetGUID &inTemplateVar) + { + Write(inTemplateVar.g); + } + + /// \brief Write a string to a bitstream. + /// \param[in] var The value to write + template <> + inline void BitStream::Write(const RakString &inTemplateVar) + { + inTemplateVar.Serialize(this); + } + template <> + inline void BitStream::Write(const RakWString &inTemplateVar) + { + inTemplateVar.Serialize(this); + } + template <> + inline void BitStream::Write(const char * const &inStringVar) + { + RakString::Serialize(inStringVar, this); + } + template <> + inline void BitStream::Write(const wchar_t * const &inStringVar) + { + RakWString::Serialize(inStringVar, this); + } + template <> + inline void BitStream::Write(const unsigned char * const &inTemplateVar) + { + Write((const char*)inTemplateVar); + } + template <> + inline void BitStream::Write(char * const &inTemplateVar) + { + Write((const char*)inTemplateVar); + } + template <> + inline void BitStream::Write(unsigned char * const &inTemplateVar) + { + Write((const char*)inTemplateVar); + } + + /// \brief Write any integral type to a bitstream. + /// \details If the current value is different from the last value + /// the current value will be written. Otherwise, a single bit will be written + /// \param[in] currentValue The current value to write + /// \param[in] lastValue The last value to compare against + template + inline void BitStream::WriteDelta(const templateType ¤tValue, const templateType &lastValue) + { + if (currentValue==lastValue) + { + Write(false); + } + else + { + Write(true); + Write(currentValue); + } + } + + /// \brief Write a bool delta. Same thing as just calling Write + /// \param[in] currentValue The current value to write + /// \param[in] lastValue The last value to compare against + template <> + inline void BitStream::WriteDelta(const bool ¤tValue, const bool &lastValue) + { + (void) lastValue; + + Write(currentValue); + } + + /// \brief WriteDelta when you don't know what the last value is, or there is no last value. + /// \param[in] currentValue The current value to write + template + inline void BitStream::WriteDelta(const templateType ¤tValue) + { + Write(true); + Write(currentValue); + } + + /// \brief Write any integral type to a bitstream. + /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. + /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. + /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type + /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte + /// \param[in] inTemplateVar The value to write + template + inline void BitStream::WriteCompressed(const templateType &inTemplateVar) + { +#ifdef _MSC_VER +#pragma warning(disable:4127) // conditional expression is constant +#endif + if (sizeof(inTemplateVar)==1) + WriteCompressed( ( unsigned char* ) & inTemplateVar, sizeof( templateType ) * 8, true ); + else + { +#ifndef __BITSTREAM_NATIVE_END +#ifdef _MSC_VER +#pragma warning(disable:4244) // '=' : conversion from 'unsigned long' to 'unsigned short', possible loss of data +#endif + + if (DoEndianSwap()) + { + unsigned char output[sizeof(templateType)]; + ReverseBytes((unsigned char*)&inTemplateVar, output, sizeof(templateType)); + WriteCompressed( ( unsigned char* ) output, sizeof(templateType) * 8, true ); + } + else +#endif + WriteCompressed( ( unsigned char* ) & inTemplateVar, sizeof(templateType) * 8, true ); + } + } + + template <> + inline void BitStream::WriteCompressed(const SystemAddress &inTemplateVar) + { + Write(inTemplateVar); + } + + template <> + inline void BitStream::WriteCompressed(const RakNetGUID &inTemplateVar) + { + Write(inTemplateVar); + } + + template <> + inline void BitStream::WriteCompressed(const uint24_t &var) + { + Write(var); + } + + template <> + inline void BitStream::WriteCompressed(const bool &inTemplateVar) + { + Write(inTemplateVar); + } + + /// For values between -1 and 1 + template <> + inline void BitStream::WriteCompressed(const float &inTemplateVar) + { + RakAssert(inTemplateVar > -1.01f && inTemplateVar < 1.01f); + float varCopy=inTemplateVar; + if (varCopy < -1.0f) + varCopy=-1.0f; + if (varCopy > 1.0f) + varCopy=1.0f; + Write((unsigned short)((varCopy+1.0f)*32767.5f)); + } + + /// For values between -1 and 1 + template <> + inline void BitStream::WriteCompressed(const double &inTemplateVar) + { + RakAssert(inTemplateVar > -1.01 && inTemplateVar < 1.01); + double varCopy=inTemplateVar; + if (varCopy < -1.0f) + varCopy=-1.0f; + if (varCopy > 1.0f) + varCopy=1.0f; + Write((uint32_t)((varCopy+1.0)*2147483648.0)); + } + + /// Compress the string + template <> + inline void BitStream::WriteCompressed(const RakString &inTemplateVar) + { + inTemplateVar.SerializeCompressed(this,0,false); + } + template <> + inline void BitStream::WriteCompressed(const RakWString &inTemplateVar) + { + inTemplateVar.Serialize(this); + } + template <> + inline void BitStream::WriteCompressed(const char * const &inStringVar) + { + RakString::SerializeCompressed(inStringVar,this,0,false); + } + template <> + inline void BitStream::WriteCompressed(const wchar_t * const &inStringVar) + { + RakWString::Serialize(inStringVar,this); + } + template <> + inline void BitStream::WriteCompressed(const unsigned char * const &inTemplateVar) + { + WriteCompressed((const char*) inTemplateVar); + } + template <> + inline void BitStream::WriteCompressed(char * const &inTemplateVar) + { + WriteCompressed((const char*) inTemplateVar); + } + template <> + inline void BitStream::WriteCompressed(unsigned char * const &inTemplateVar) + { + WriteCompressed((const char*) inTemplateVar); + } + + + /// \brief Write any integral type to a bitstream. + /// \details If the current value is different from the last value + /// the current value will be written. Otherwise, a single bit will be written + /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. + /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type + /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte + /// \param[in] currentValue The current value to write + /// \param[in] lastValue The last value to compare against + template + inline void BitStream::WriteCompressedDelta(const templateType ¤tValue, const templateType &lastValue) + { + if (currentValue==lastValue) + { + Write(false); + } + else + { + Write(true); + WriteCompressed(currentValue); + } + } + + /// \brief Write a bool delta. Same thing as just calling Write + /// \param[in] currentValue The current value to write + /// \param[in] lastValue The last value to compare against + template <> + inline void BitStream::WriteCompressedDelta(const bool ¤tValue, const bool &lastValue) + { + (void) lastValue; + + Write(currentValue); + } + + /// \brief Save as WriteCompressedDelta(const templateType ¤tValue, const templateType &lastValue) + /// when we have an unknown second parameter + template + inline void BitStream::WriteCompressedDelta(const templateType ¤tValue) + { + Write(true); + WriteCompressed(currentValue); + } + + /// \brief Save as WriteCompressedDelta(bool currentValue, const templateType &lastValue) + /// when we have an unknown second bool + template <> + inline void BitStream::WriteCompressedDelta(const bool ¤tValue) + { + Write(currentValue); + } + + /// \brief Read any integral type from a bitstream. Define __BITSTREAM_NATIVE_END if you need endian swapping. + /// \param[in] outTemplateVar The value to read + template + inline bool BitStream::Read(templateType &outTemplateVar) + { +#ifdef _MSC_VER +#pragma warning(disable:4127) // conditional expression is constant +#endif + if (sizeof(outTemplateVar)==1) + return ReadBits( ( unsigned char* ) &outTemplateVar, sizeof(templateType) * 8, true ); + else + { +#ifndef __BITSTREAM_NATIVE_END +#ifdef _MSC_VER +#pragma warning(disable:4244) // '=' : conversion from 'unsigned long' to 'unsigned short', possible loss of data +#endif + if (DoEndianSwap()) + { + unsigned char output[sizeof(templateType)]; + if (ReadBits( ( unsigned char* ) output, sizeof(templateType) * 8, true )) + { + ReverseBytes(output, (unsigned char*)&outTemplateVar, sizeof(templateType)); + return true; + } + return false; + } + else +#endif + return ReadBits( ( unsigned char* ) & outTemplateVar, sizeof(templateType) * 8, true ); + } + } + + /// \brief Read a bool from a bitstream. + /// \param[in] outTemplateVar The value to read + template <> + inline bool BitStream::Read(bool &outTemplateVar) + { + if ( readOffset + 1 > numberOfBitsUsed ) + return false; + + if ( data[ readOffset >> 3 ] & ( 0x80 >> ( readOffset & 7 ) ) ) // Is it faster to just write it out here? + outTemplateVar = true; + else + outTemplateVar = false; + + // Has to be on a different line for Mac + readOffset++; + + return true; + } + + /// \brief Read a systemAddress from a bitstream. + /// \param[in] outTemplateVar The value to read + template <> + inline bool BitStream::Read(SystemAddress &outTemplateVar) + { + unsigned char ipVersion; + Read(ipVersion); + if (ipVersion==4) + { + outTemplateVar.address.addr4.sin_family=AF_INET; + // Read(var.binaryAddress); + // Don't endian swap the address or port + uint32_t binaryAddress; + ReadBits( ( unsigned char* ) & binaryAddress, sizeof(binaryAddress) * 8, true ); + // Unhide the IP address, done to prevent routers from changing it + outTemplateVar.address.addr4.sin_addr.s_addr=~binaryAddress; + bool b = ReadBits(( unsigned char* ) & outTemplateVar.address.addr4.sin_port, sizeof(outTemplateVar.address.addr4.sin_port) * 8, true); + outTemplateVar.debugPort=ntohs(outTemplateVar.address.addr4.sin_port); + return b; + } + else + { +#if RAKNET_SUPPORT_IPV6==1 + bool b = ReadBits((unsigned char*) &outTemplateVar.address.addr6, sizeof(outTemplateVar.address.addr6)*8, true); + outTemplateVar.debugPort=ntohs(outTemplateVar.address.addr6.sin6_port); + return b; +#else + return false; +#endif + } + } + + template <> + inline bool BitStream::Read(uint24_t &outTemplateVar) + { + AlignReadToByteBoundary(); + if ( readOffset + 3*8 > numberOfBitsUsed ) + return false; + + if (IsBigEndian()==false) + { + ((unsigned char *)&outTemplateVar.val)[0]=data[ (readOffset >> 3) + 0]; + ((unsigned char *)&outTemplateVar.val)[1]=data[ (readOffset >> 3) + 1]; + ((unsigned char *)&outTemplateVar.val)[2]=data[ (readOffset >> 3) + 2]; + ((unsigned char *)&outTemplateVar.val)[3]=0; + } + else + { + + ((unsigned char *)&outTemplateVar.val)[3]=data[ (readOffset >> 3) + 0]; + ((unsigned char *)&outTemplateVar.val)[2]=data[ (readOffset >> 3) + 1]; + ((unsigned char *)&outTemplateVar.val)[1]=data[ (readOffset >> 3) + 2]; + ((unsigned char *)&outTemplateVar.val)[0]=0; + } + + readOffset+=3*8; + return true; + } + + template <> + inline bool BitStream::Read(RakNetGUID &outTemplateVar) + { + return Read(outTemplateVar.g); + } + + + template <> + inline bool BitStream::Read(RakString &outTemplateVar) + { + return outTemplateVar.Deserialize(this); + } + template <> + inline bool BitStream::Read(RakWString &outTemplateVar) + { + return outTemplateVar.Deserialize(this); + } + template <> + inline bool BitStream::Read(char *&varString) + { + return RakString::Deserialize(varString,this); + } + template <> + inline bool BitStream::Read(wchar_t *&varString) + { + return RakWString::Deserialize(varString,this); + } + template <> + inline bool BitStream::Read(unsigned char *&varString) + { + return RakString::Deserialize((char*) varString,this); + } + + /// \brief Read any integral type from a bitstream. + /// \details If the written value differed from the value compared against in the write function, + /// var will be updated. Otherwise it will retain the current value. + /// ReadDelta is only valid from a previous call to WriteDelta + /// \param[in] outTemplateVar The value to read + template + inline bool BitStream::ReadDelta(templateType &outTemplateVar) + { + bool dataWritten; + bool success; + success=Read(dataWritten); + if (dataWritten) + success=Read(outTemplateVar); + return success; + } + + /// \brief Read a bool from a bitstream. + /// \param[in] outTemplateVar The value to read + template <> + inline bool BitStream::ReadDelta(bool &outTemplateVar) + { + return Read(outTemplateVar); + } + + /// \brief Read any integral type from a bitstream. + /// \details Undefine __BITSTREAM_NATIVE_END if you need endian swapping. + /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. + /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type + /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte + /// \param[in] outTemplateVar The value to read + template + inline bool BitStream::ReadCompressed(templateType &outTemplateVar) + { +#ifdef _MSC_VER +#pragma warning(disable:4127) // conditional expression is constant +#endif + if (sizeof(outTemplateVar)==1) + return ReadCompressed( ( unsigned char* ) &outTemplateVar, sizeof(templateType) * 8, true ); + else + { +#ifndef __BITSTREAM_NATIVE_END + if (DoEndianSwap()) + { + unsigned char output[sizeof(templateType)]; + if (ReadCompressed( ( unsigned char* ) output, sizeof(templateType) * 8, true )) + { + ReverseBytes(output, (unsigned char*)&outTemplateVar, sizeof(templateType)); + return true; + } + return false; + } + else +#endif + return ReadCompressed( ( unsigned char* ) & outTemplateVar, sizeof(templateType) * 8, true ); + } + } + + template <> + inline bool BitStream::ReadCompressed(SystemAddress &outTemplateVar) + { + return Read(outTemplateVar); + } + + template <> + inline bool BitStream::ReadCompressed(uint24_t &outTemplateVar) + { + return Read(outTemplateVar); + } + + template <> + inline bool BitStream::ReadCompressed(RakNetGUID &outTemplateVar) + { + return Read(outTemplateVar); + } + + template <> + inline bool BitStream::ReadCompressed(bool &outTemplateVar) + { + return Read(outTemplateVar); + } + + /// For values between -1 and 1 + template <> + inline bool BitStream::ReadCompressed(float &outTemplateVar) + { + unsigned short compressedFloat; + if (Read(compressedFloat)) + { + outTemplateVar = ((float)compressedFloat / 32767.5f - 1.0f); + return true; + } + return false; + } + + /// For values between -1 and 1 + template <> + inline bool BitStream::ReadCompressed(double &outTemplateVar) + { + uint32_t compressedFloat; + if (Read(compressedFloat)) + { + outTemplateVar = ((double)compressedFloat / 2147483648.0 - 1.0); + return true; + } + return false; + } + + /// For strings + template <> + inline bool BitStream::ReadCompressed(RakString &outTemplateVar) + { + return outTemplateVar.DeserializeCompressed(this,false); + } + template <> + inline bool BitStream::ReadCompressed(RakWString &outTemplateVar) + { + return outTemplateVar.Deserialize(this); + } + template <> + inline bool BitStream::ReadCompressed(char *&outTemplateVar) + { + return RakString::DeserializeCompressed(outTemplateVar,this,false); + } + template <> + inline bool BitStream::ReadCompressed(wchar_t *&outTemplateVar) + { + return RakWString::Deserialize(outTemplateVar,this); + } + template <> + inline bool BitStream::ReadCompressed(unsigned char *&outTemplateVar) + { + return RakString::DeserializeCompressed((char*) outTemplateVar,this,false); + } + + /// \brief Read any integral type from a bitstream. + /// \details If the written value differed from the value compared against in the write function, + /// var will be updated. Otherwise it will retain the current value. + /// the current value will be updated. + /// For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. + /// For non-floating point, this is lossless, but only has benefit if you use less than half the bits of the type + /// If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte + /// ReadCompressedDelta is only valid from a previous call to WriteDelta + /// \param[in] outTemplateVar The value to read + template + inline bool BitStream::ReadCompressedDelta(templateType &outTemplateVar) + { + bool dataWritten; + bool success; + success=Read(dataWritten); + if (dataWritten) + success=ReadCompressed(outTemplateVar); + return success; + } + + /// \brief Read a bool from a bitstream. + /// \param[in] outTemplateVar The value to read + template <> + inline bool BitStream::ReadCompressedDelta(bool &outTemplateVar) + { + return Read(outTemplateVar); + } + + template + void BitStream::WriteCasted( const sourceType &value ) + { + destinationType val = (destinationType) value; + Write(val); + } + + template + void BitStream::WriteBitsFromIntegerRange( const templateType value, const templateType minimum,const templateType maximum, bool allowOutsideRange ) + { + int requiredBits=BYTES_TO_BITS(sizeof(templateType))-NumberOfLeadingZeroes(templateType(maximum-minimum)); + WriteBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange); + } + template + void BitStream::WriteBitsFromIntegerRange( const templateType value, const templateType minimum,const templateType maximum, const int requiredBits, bool allowOutsideRange ) + { + RakAssert(maximum>=minimum); + RakAssert(allowOutsideRange==true || (value>=minimum && value<=maximum)); + if (allowOutsideRange) + { + if (valuemaximum) + { + Write(true); + Write(value); + return; + } + Write(false); + } + templateType valueOffMin=value-minimum; + if (IsBigEndian()==true) + { + unsigned char output[sizeof(templateType)]; + ReverseBytes((unsigned char*)&valueOffMin, output, sizeof(templateType)); + WriteBits(output,requiredBits); + } + else + { + WriteBits((unsigned char*) &valueOffMin,requiredBits); + } + } + + template // templateType for this function must be a float or double + void BitStream::WriteNormVector( templateType x, templateType y, templateType z ) + { +#ifdef _DEBUG + RakAssert(x <= 1.01 && y <= 1.01 && z <= 1.01 && x >= -1.01 && y >= -1.01 && z >= -1.01); +#endif + + WriteFloat16((float)x,-1.0f,1.0f); + WriteFloat16((float)y,-1.0f,1.0f); + WriteFloat16((float)z,-1.0f,1.0f); + } + + template // templateType for this function must be a float or double + void BitStream::WriteVector( templateType x, templateType y, templateType z ) + { + templateType magnitude = sqrt(x * x + y * y + z * z); + Write((float)magnitude); + if (magnitude > 0.00001f) + { + WriteCompressed((float)(x/magnitude)); + WriteCompressed((float)(y/magnitude)); + WriteCompressed((float)(z/magnitude)); + // Write((unsigned short)((x/magnitude+1.0f)*32767.5f)); + // Write((unsigned short)((y/magnitude+1.0f)*32767.5f)); + // Write((unsigned short)((z/magnitude+1.0f)*32767.5f)); + } + } + + template // templateType for this function must be a float or double + void BitStream::WriteNormQuat( templateType w, templateType x, templateType y, templateType z) + { + Write((bool)(w<0.0)); + Write((bool)(x<0.0)); + Write((bool)(y<0.0)); + Write((bool)(z<0.0)); + Write((unsigned short)(fabs(x)*65535.0)); + Write((unsigned short)(fabs(y)*65535.0)); + Write((unsigned short)(fabs(z)*65535.0)); + // Leave out w and calculate it on the target + } + + template // templateType for this function must be a float or double + void BitStream::WriteOrthMatrix( + templateType m00, templateType m01, templateType m02, + templateType m10, templateType m11, templateType m12, + templateType m20, templateType m21, templateType m22 ) + { + + double qw; + double qx; + double qy; + double qz; + + // Convert matrix to quat + // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/ + float sum; + sum = 1 + m00 + m11 + m22; + if (sum < 0.0f) sum=0.0f; + qw = sqrt( sum ) / 2; + sum = 1 + m00 - m11 - m22; + if (sum < 0.0f) sum=0.0f; + qx = sqrt( sum ) / 2; + sum = 1 - m00 + m11 - m22; + if (sum < 0.0f) sum=0.0f; + qy = sqrt( sum ) / 2; + sum = 1 - m00 - m11 + m22; + if (sum < 0.0f) sum=0.0f; + qz = sqrt( sum ) / 2; + if (qw < 0.0) qw=0.0; + if (qx < 0.0) qx=0.0; + if (qy < 0.0) qy=0.0; + if (qz < 0.0) qz=0.0; + qx = _copysign( (double) qx, (double) (m21 - m12) ); + qy = _copysign( (double) qy, (double) (m02 - m20) ); + qz = _copysign( (double) qz, (double) (m10 - m01) ); + + WriteNormQuat(qw,qx,qy,qz); + } + + template + bool BitStream::ReadCasted( sourceType &value ) + { + serializationType val; + bool success = Read(val); + value=(sourceType) val; + return success; + } + + template + bool BitStream::ReadBitsFromIntegerRange( templateType &value, const templateType minimum, const templateType maximum, bool allowOutsideRange ) + { + int requiredBits=BYTES_TO_BITS(sizeof(templateType))-NumberOfLeadingZeroes(templateType(maximum-minimum)); + return ReadBitsFromIntegerRange(value,minimum,maximum,requiredBits,allowOutsideRange); + } + template + bool BitStream::ReadBitsFromIntegerRange( templateType &value, const templateType minimum, const templateType maximum, const int requiredBits, bool allowOutsideRange ) + { + RakAssert(maximum>=minimum); + if (allowOutsideRange) + { + bool isOutsideRange; + Read(isOutsideRange); + if (isOutsideRange) + return Read(value); + } + unsigned char output[sizeof(templateType)]; + memset(output,0,sizeof(output)); + bool success = ReadBits(output,requiredBits); + if (success) + { + if (IsBigEndian()==true) + ReverseBytesInPlace(output,sizeof(output)); + memcpy(&value,output,sizeof(output)); + + value+=minimum; + } + + return success; + } + + template // templateType for this function must be a float or double + bool BitStream::ReadNormVector( templateType &x, templateType &y, templateType &z ) + { + float xIn,yIn,zIn; + ReadFloat16(xIn,-1.0f,1.0f); + ReadFloat16(yIn,-1.0f,1.0f); + ReadFloat16(zIn,-1.0f,1.0f); + x=xIn; + y=yIn; + z=zIn; + return true; + } + + template // templateType for this function must be a float or double + bool BitStream::ReadVector( templateType &x, templateType &y, templateType &z ) + { + float magnitude; + //unsigned short sx,sy,sz; + if (!Read(magnitude)) + return false; + if (magnitude>0.00001f) + { + // Read(sx); + // Read(sy); + // if (!Read(sz)) + // return false; + // x=((float)sx / 32767.5f - 1.0f) * magnitude; + // y=((float)sy / 32767.5f - 1.0f) * magnitude; + // z=((float)sz / 32767.5f - 1.0f) * magnitude; + float cx=0.0f,cy=0.0f,cz=0.0f; + ReadCompressed(cx); + ReadCompressed(cy); + if (!ReadCompressed(cz)) + return false; + x=cx; + y=cy; + z=cz; + x*=magnitude; + y*=magnitude; + z*=magnitude; + } + else + { + x=0.0; + y=0.0; + z=0.0; + } + return true; + } + + template // templateType for this function must be a float or double + bool BitStream::ReadNormQuat( templateType &w, templateType &x, templateType &y, templateType &z) + { + bool cwNeg=false, cxNeg=false, cyNeg=false, czNeg=false; + unsigned short cx,cy,cz; + Read(cwNeg); + Read(cxNeg); + Read(cyNeg); + Read(czNeg); + Read(cx); + Read(cy); + if (!Read(cz)) + return false; + + // Calculate w from x,y,z + x=(templateType)(cx/65535.0); + y=(templateType)(cy/65535.0); + z=(templateType)(cz/65535.0); + if (cxNeg) x=-x; + if (cyNeg) y=-y; + if (czNeg) z=-z; + float difference = 1.0f - x*x - y*y - z*z; + if (difference < 0.0f) + difference=0.0f; + w = (templateType)(sqrt(difference)); + if (cwNeg) + w=-w; + + return true; + } + + template // templateType for this function must be a float or double + bool BitStream::ReadOrthMatrix( + templateType &m00, templateType &m01, templateType &m02, + templateType &m10, templateType &m11, templateType &m12, + templateType &m20, templateType &m21, templateType &m22 ) + { + float qw,qx,qy,qz; + if (!ReadNormQuat(qw,qx,qy,qz)) + return false; + + // Quat to orthogonal rotation matrix + // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/index.htm + double sqw = (double)qw*(double)qw; + double sqx = (double)qx*(double)qx; + double sqy = (double)qy*(double)qy; + double sqz = (double)qz*(double)qz; + m00 = (templateType)(sqx - sqy - sqz + sqw); // since sqw + sqx + sqy + sqz =1 + m11 = (templateType)(-sqx + sqy - sqz + sqw); + m22 = (templateType)(-sqx - sqy + sqz + sqw); + + double tmp1 = (double)qx*(double)qy; + double tmp2 = (double)qz*(double)qw; + m10 = (templateType)(2.0 * (tmp1 + tmp2)); + m01 = (templateType)(2.0 * (tmp1 - tmp2)); + + tmp1 = (double)qx*(double)qz; + tmp2 = (double)qy*(double)qw; + m20 =(templateType)(2.0 * (tmp1 - tmp2)); + m02 = (templateType)(2.0 * (tmp1 + tmp2)); + tmp1 = (double)qy*(double)qz; + tmp2 = (double)qx*(double)qw; + m21 = (templateType)(2.0 * (tmp1 + tmp2)); + m12 = (templateType)(2.0 * (tmp1 - tmp2)); + + return true; + } + + template + BitStream& operator<<(BitStream& out, templateType& c) + { + out.Write(c); + return out; + } + template + BitStream& operator>>(BitStream& in, templateType& c) + { + bool success = in.Read(c); + (void)success; + + RakAssert(success); + return in; + } + +} + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif diff --git a/Source/CCRakNetSlidingWindow.h b/Source/CCRakNetSlidingWindow.h index 36c4a5c72..6fdcd4609 100644 --- a/Source/CCRakNetSlidingWindow.h +++ b/Source/CCRakNetSlidingWindow.h @@ -1,220 +1,218 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/* -http://www.ssfnet.org/Exchange/tcp/tcpTutorialNotes.html - -cwnd=max bytes allowed on wire at once - -Start: -cwnd=mtu -ssthresh=unlimited - -Slow start: -On ack cwnd*=2 - -congestion avoidance: -On ack during new period -cwnd+=mtu*mtu/cwnd - -on loss or duplicate ack during period: -sshtresh=cwnd/2 -cwnd=MTU -This reenters slow start - -If cwnd < ssthresh, then use slow start -else use congestion avoidance - - -*/ - -#include "RakNetDefines.h" - -#if USE_SLIDING_WINDOW_CONGESTION_CONTROL==1 - -#ifndef __CONGESTION_CONTROL_SLIDING_WINDOW_H -#define __CONGESTION_CONTROL_SLIDING_WINDOW_H - -#include "NativeTypes.h" -#include "RakNetTime.h" -#include "RakNetTypes.h" -#include "DS_Queue.h" - -/// Sizeof an UDP header in byte -#define UDP_HEADER_SIZE 28 - -#define CC_DEBUG_PRINTF_1(x) -#define CC_DEBUG_PRINTF_2(x,y) -#define CC_DEBUG_PRINTF_3(x,y,z) -#define CC_DEBUG_PRINTF_4(x,y,z,a) -#define CC_DEBUG_PRINTF_5(x,y,z,a,b) -//#define CC_DEBUG_PRINTF_1(x) printf(x) -//#define CC_DEBUG_PRINTF_2(x,y) printf(x,y) -//#define CC_DEBUG_PRINTF_3(x,y,z) printf(x,y,z) -//#define CC_DEBUG_PRINTF_4(x,y,z,a) printf(x,y,z,a) -//#define CC_DEBUG_PRINTF_5(x,y,z,a,b) printf(x,y,z,a,b) - -/// Set to 4 if you are using the iPod Touch TG. See http://www.jenkinssoftware.com/forum/index.php?topic=2717.0 -#define CC_TIME_TYPE_BYTES 8 - -#if CC_TIME_TYPE_BYTES==8 -typedef RakNet::TimeUS CCTimeType; -#else -typedef RakNet::TimeMS CCTimeType; -#endif - -typedef RakNet::uint24_t DatagramSequenceNumberType; -typedef double BytesPerMicrosecond; -typedef double BytesPerSecond; -typedef double MicrosecondsPerByte; - -namespace RakNet -{ - -class CCRakNetSlidingWindow -{ - public: - - CCRakNetSlidingWindow(); - ~CCRakNetSlidingWindow(); - - /// Reset all variables to their initial states, for a new connection - void Init(CCTimeType curTime, uint32_t maxDatagramPayload); - - /// Update over time - void Update(CCTimeType curTime, bool hasDataToSendOrResend); - - int GetRetransmissionBandwidth(CCTimeType curTime, CCTimeType timeSinceLastTick, uint32_t unacknowledgedBytes, bool isContinuousSend); - int GetTransmissionBandwidth(CCTimeType curTime, CCTimeType timeSinceLastTick, uint32_t unacknowledgedBytes, bool isContinuousSend); - - /// Acks do not have to be sent immediately. Instead, they can be buffered up such that groups of acks are sent at a time - /// This reduces overall bandwidth usage - /// How long they can be buffered depends on the retransmit time of the sender - /// Should call once per update tick, and send if needed - bool ShouldSendACKs(CCTimeType curTime, CCTimeType estimatedTimeToNextTick); - - /// Every data packet sent must contain a sequence number - /// Call this function to get it. The sequence number is passed into OnGotPacketPair() - DatagramSequenceNumberType GetAndIncrementNextDatagramSequenceNumber(void); - DatagramSequenceNumberType GetNextDatagramSequenceNumber(void); - - /// Call this when you send packets - /// Every 15th and 16th packets should be sent as a packet pair if possible - /// When packets marked as a packet pair arrive, pass to OnGotPacketPair() - /// When any packets arrive, (additionally) pass to OnGotPacket - /// Packets should contain our system time, so we can pass rtt to OnNonDuplicateAck() - void OnSendBytes(CCTimeType curTime, uint32_t numBytes); - - /// Call this when you get a packet pair - void OnGotPacketPair(DatagramSequenceNumberType datagramSequenceNumber, uint32_t sizeInBytes, CCTimeType curTime); - - /// Call this when you get a packet (including packet pairs) - /// If the DatagramSequenceNumberType is out of order, skippedMessageCount will be non-zero - /// In that case, send a NAK for every sequence number up to that count - bool OnGotPacket(DatagramSequenceNumberType datagramSequenceNumber, bool isContinuousSend, CCTimeType curTime, uint32_t sizeInBytes, uint32_t *skippedMessageCount); - - /// Call when you get a NAK, with the sequence number of the lost message - /// Affects the congestion control - void OnResend(CCTimeType curTime, RakNet::TimeUS nextActionTime); - void OnNAK(CCTimeType curTime, DatagramSequenceNumberType nakSequenceNumber); - - /// Call this when an ACK arrives. - /// hasBAndAS are possibly written with the ack, see OnSendAck() - /// B and AS are used in the calculations in UpdateWindowSizeAndAckOnAckPerSyn - /// B and AS are updated at most once per SYN - void OnAck(CCTimeType curTime, CCTimeType rtt, bool hasBAndAS, BytesPerMicrosecond _B, BytesPerMicrosecond _AS, double totalUserDataBytesAcked, bool isContinuousSend, DatagramSequenceNumberType sequenceNumber ); - void OnDuplicateAck( CCTimeType curTime, DatagramSequenceNumberType sequenceNumber ); - - /// Call when you send an ack, to see if the ack should have the B and AS parameters transmitted - /// Call before calling OnSendAck() - void OnSendAckGetBAndAS(CCTimeType curTime, bool *hasBAndAS, BytesPerMicrosecond *_B, BytesPerMicrosecond *_AS); - - /// Call when we send an ack, to write B and AS if needed - /// B and AS are only written once per SYN, to prevent slow calculations - /// Also updates SND, the period between sends, since data is written out - /// Be sure to call OnSendAckGetBAndAS() before calling OnSendAck(), since whether you write it or not affects \a numBytes - void OnSendAck(CCTimeType curTime, uint32_t numBytes); - - /// Call when we send a NACK - /// Also updates SND, the period between sends, since data is written out - void OnSendNACK(CCTimeType curTime, uint32_t numBytes); - - /// Retransmission time out for the sender - /// If the time difference between when a message was last transmitted, and the current time is greater than RTO then packet is eligible for retransmission, pending congestion control - /// RTO = (RTT + 4 * RTTVar) + SYN - /// If we have been continuously sending for the last RTO, and no ACK or NAK at all, SND*=2; - /// This is per message, which is different from UDT, but RakNet supports packetloss with continuing data where UDT is only RELIABLE_ORDERED - /// Minimum value is 100 milliseconds - CCTimeType GetRTOForRetransmission(unsigned char timesSent) const; - - /// Set the maximum amount of data that can be sent in one datagram - /// Default to MAXIMUM_MTU_SIZE-UDP_HEADER_SIZE - void SetMTU(uint32_t bytes); - - /// Return what was set by SetMTU() - uint32_t GetMTU(void) const; - - /// Query for statistics - BytesPerMicrosecond GetLocalSendRate(void) const {return 0;} - BytesPerMicrosecond GetLocalReceiveRate(CCTimeType currentTime) const; - BytesPerMicrosecond GetRemoveReceiveRate(void) const {return 0;} - //BytesPerMicrosecond GetEstimatedBandwidth(void) const {return B;} - BytesPerMicrosecond GetEstimatedBandwidth(void) const {return GetLinkCapacityBytesPerSecond()*1000000.0;} - double GetLinkCapacityBytesPerSecond(void) const {return 0;} - - /// Query for statistics - double GetRTT(void) const; - - bool GetIsInSlowStart(void) const {return IsInSlowStart();} - uint32_t GetCWNDLimit(void) const {return (uint32_t) 0;} - - - /// Is a > b, accounting for variable overflow? - static bool GreaterThan(DatagramSequenceNumberType a, DatagramSequenceNumberType b); - /// Is a < b, accounting for variable overflow? - static bool LessThan(DatagramSequenceNumberType a, DatagramSequenceNumberType b); -// void SetTimeBetweenSendsLimit(unsigned int bitsPerSecond); - uint64_t GetBytesPerSecondLimitByCongestionControl(void) const; - - protected: - - // Maximum amount of bytes that the user can send, e.g. the size of one full datagram - uint32_t MAXIMUM_MTU_INCLUDING_UDP_HEADER; - - double cwnd; // max bytes on wire - double ssThresh; // Threshhold between slow start and congestion avoidance - - /// When we get an ack, if oldestUnsentAck==0, set it to the current time - /// When we send out acks, set oldestUnsentAck to 0 - CCTimeType oldestUnsentAck; - - CCTimeType GetSenderRTOForACK(void) const; - - /// Every outgoing datagram is assigned a sequence number, which increments by 1 every assignment - DatagramSequenceNumberType nextDatagramSequenceNumber; - DatagramSequenceNumberType nextCongestionControlBlock; - bool backoffThisBlock, speedUpThisBlock; - /// Track which datagram sequence numbers have arrived. - /// If a sequence number is skipped, send a NAK for all skipped messages - DatagramSequenceNumberType expectedNextSequenceNumber; - - bool _isContinuousSend; - - bool IsInSlowStart(void) const; - - double lastRtt, estimatedRTT, deviationRtt; - -}; - -} - -#endif - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/* +http://www.ssfnet.org/Exchange/tcp/tcpTutorialNotes.html + +cwnd=max bytes allowed on wire at once + +Start: +cwnd=mtu +ssthresh=unlimited + +Slow start: +On ack cwnd*=2 + +congestion avoidance: +On ack during new period +cwnd+=mtu*mtu/cwnd + +on loss or duplicate ack during period: +sshtresh=cwnd/2 +cwnd=MTU +This reenters slow start + +If cwnd < ssthresh, then use slow start +else use congestion avoidance + + +*/ + +#include "RakNetDefines.h" + +#if USE_SLIDING_WINDOW_CONGESTION_CONTROL==1 + +#pragma once + +#include "NativeTypes.h" +#include "RakNetTime.h" +#include "RakNetTypes.h" +#include "DS_Queue.h" + +/// Sizeof an UDP header in byte +#define UDP_HEADER_SIZE 28 + +#define CC_DEBUG_PRINTF_1(x) +#define CC_DEBUG_PRINTF_2(x,y) +#define CC_DEBUG_PRINTF_3(x,y,z) +#define CC_DEBUG_PRINTF_4(x,y,z,a) +#define CC_DEBUG_PRINTF_5(x,y,z,a,b) +//#define CC_DEBUG_PRINTF_1(x) printf(x) +//#define CC_DEBUG_PRINTF_2(x,y) printf(x,y) +//#define CC_DEBUG_PRINTF_3(x,y,z) printf(x,y,z) +//#define CC_DEBUG_PRINTF_4(x,y,z,a) printf(x,y,z,a) +//#define CC_DEBUG_PRINTF_5(x,y,z,a,b) printf(x,y,z,a,b) + +/// Set to 4 if you are using the iPod Touch TG. See http://www.jenkinssoftware.com/forum/index.php?topic=2717.0 +#define CC_TIME_TYPE_BYTES 8 + +#if CC_TIME_TYPE_BYTES==8 +typedef RakNet::TimeUS CCTimeType; +#else +typedef RakNet::TimeMS CCTimeType; +#endif + +typedef RakNet::uint24_t DatagramSequenceNumberType; +typedef double BytesPerMicrosecond; +typedef double BytesPerSecond; +typedef double MicrosecondsPerByte; + +namespace RakNet +{ + +class CCRakNetSlidingWindow +{ + public: + + CCRakNetSlidingWindow(); + ~CCRakNetSlidingWindow(); + + /// Reset all variables to their initial states, for a new connection + void Init(CCTimeType curTime, uint32_t maxDatagramPayload); + + /// Update over time + void Update(CCTimeType curTime, bool hasDataToSendOrResend); + + int GetRetransmissionBandwidth(CCTimeType curTime, CCTimeType timeSinceLastTick, uint32_t unacknowledgedBytes, bool isContinuousSend); + int GetTransmissionBandwidth(CCTimeType curTime, CCTimeType timeSinceLastTick, uint32_t unacknowledgedBytes, bool isContinuousSend); + + /// Acks do not have to be sent immediately. Instead, they can be buffered up such that groups of acks are sent at a time + /// This reduces overall bandwidth usage + /// How long they can be buffered depends on the retransmit time of the sender + /// Should call once per update tick, and send if needed + bool ShouldSendACKs(CCTimeType curTime, CCTimeType estimatedTimeToNextTick); + + /// Every data packet sent must contain a sequence number + /// Call this function to get it. The sequence number is passed into OnGotPacketPair() + DatagramSequenceNumberType GetAndIncrementNextDatagramSequenceNumber(void); + DatagramSequenceNumberType GetNextDatagramSequenceNumber(void); + + /// Call this when you send packets + /// Every 15th and 16th packets should be sent as a packet pair if possible + /// When packets marked as a packet pair arrive, pass to OnGotPacketPair() + /// When any packets arrive, (additionally) pass to OnGotPacket + /// Packets should contain our system time, so we can pass rtt to OnNonDuplicateAck() + void OnSendBytes(CCTimeType curTime, uint32_t numBytes); + + /// Call this when you get a packet pair + void OnGotPacketPair(DatagramSequenceNumberType datagramSequenceNumber, uint32_t sizeInBytes, CCTimeType curTime); + + /// Call this when you get a packet (including packet pairs) + /// If the DatagramSequenceNumberType is out of order, skippedMessageCount will be non-zero + /// In that case, send a NAK for every sequence number up to that count + bool OnGotPacket(DatagramSequenceNumberType datagramSequenceNumber, bool isContinuousSend, CCTimeType curTime, uint32_t sizeInBytes, uint32_t *skippedMessageCount); + + /// Call when you get a NAK, with the sequence number of the lost message + /// Affects the congestion control + void OnResend(CCTimeType curTime, RakNet::TimeUS nextActionTime); + void OnNAK(CCTimeType curTime, DatagramSequenceNumberType nakSequenceNumber); + + /// Call this when an ACK arrives. + /// hasBAndAS are possibly written with the ack, see OnSendAck() + /// B and AS are used in the calculations in UpdateWindowSizeAndAckOnAckPerSyn + /// B and AS are updated at most once per SYN + void OnAck(CCTimeType curTime, CCTimeType rtt, bool hasBAndAS, BytesPerMicrosecond _B, BytesPerMicrosecond _AS, double totalUserDataBytesAcked, bool isContinuousSend, DatagramSequenceNumberType sequenceNumber ); + void OnDuplicateAck( CCTimeType curTime, DatagramSequenceNumberType sequenceNumber ); + + /// Call when you send an ack, to see if the ack should have the B and AS parameters transmitted + /// Call before calling OnSendAck() + void OnSendAckGetBAndAS(CCTimeType curTime, bool *hasBAndAS, BytesPerMicrosecond *_B, BytesPerMicrosecond *_AS); + + /// Call when we send an ack, to write B and AS if needed + /// B and AS are only written once per SYN, to prevent slow calculations + /// Also updates SND, the period between sends, since data is written out + /// Be sure to call OnSendAckGetBAndAS() before calling OnSendAck(), since whether you write it or not affects \a numBytes + void OnSendAck(CCTimeType curTime, uint32_t numBytes); + + /// Call when we send a NACK + /// Also updates SND, the period between sends, since data is written out + void OnSendNACK(CCTimeType curTime, uint32_t numBytes); + + /// Retransmission time out for the sender + /// If the time difference between when a message was last transmitted, and the current time is greater than RTO then packet is eligible for retransmission, pending congestion control + /// RTO = (RTT + 4 * RTTVar) + SYN + /// If we have been continuously sending for the last RTO, and no ACK or NAK at all, SND*=2; + /// This is per message, which is different from UDT, but RakNet supports packetloss with continuing data where UDT is only RELIABLE_ORDERED + /// Minimum value is 100 milliseconds + CCTimeType GetRTOForRetransmission(unsigned char timesSent) const; + + /// Set the maximum amount of data that can be sent in one datagram + /// Default to MAXIMUM_MTU_SIZE-UDP_HEADER_SIZE + void SetMTU(uint32_t bytes); + + /// Return what was set by SetMTU() + uint32_t GetMTU(void) const; + + /// Query for statistics + BytesPerMicrosecond GetLocalSendRate(void) const {return 0;} + BytesPerMicrosecond GetLocalReceiveRate(CCTimeType currentTime) const; + BytesPerMicrosecond GetRemoveReceiveRate(void) const {return 0;} + //BytesPerMicrosecond GetEstimatedBandwidth(void) const {return B;} + BytesPerMicrosecond GetEstimatedBandwidth(void) const {return GetLinkCapacityBytesPerSecond()*1000000.0;} + double GetLinkCapacityBytesPerSecond(void) const {return 0;} + + /// Query for statistics + double GetRTT(void) const; + + bool GetIsInSlowStart(void) const {return IsInSlowStart();} + uint32_t GetCWNDLimit(void) const {return (uint32_t) 0;} + + + /// Is a > b, accounting for variable overflow? + static bool GreaterThan(DatagramSequenceNumberType a, DatagramSequenceNumberType b); + /// Is a < b, accounting for variable overflow? + static bool LessThan(DatagramSequenceNumberType a, DatagramSequenceNumberType b); +// void SetTimeBetweenSendsLimit(unsigned int bitsPerSecond); + uint64_t GetBytesPerSecondLimitByCongestionControl(void) const; + + protected: + + // Maximum amount of bytes that the user can send, e.g. the size of one full datagram + uint32_t MAXIMUM_MTU_INCLUDING_UDP_HEADER; + + double cwnd; // max bytes on wire + double ssThresh; // Threshhold between slow start and congestion avoidance + + /// When we get an ack, if oldestUnsentAck==0, set it to the current time + /// When we send out acks, set oldestUnsentAck to 0 + CCTimeType oldestUnsentAck; + + CCTimeType GetSenderRTOForACK(void) const; + + /// Every outgoing datagram is assigned a sequence number, which increments by 1 every assignment + DatagramSequenceNumberType nextDatagramSequenceNumber; + DatagramSequenceNumberType nextCongestionControlBlock; + bool backoffThisBlock, speedUpThisBlock; + /// Track which datagram sequence numbers have arrived. + /// If a sequence number is skipped, send a NAK for all skipped messages + DatagramSequenceNumberType expectedNextSequenceNumber; + + bool _isContinuousSend; + + bool IsInSlowStart(void) const; + + double lastRtt, estimatedRTT, deviationRtt; + +}; + +} + +#endif + diff --git a/Source/CCRakNetUDT.h b/Source/CCRakNetUDT.h index 0a4d7c4e7..188106717 100644 --- a/Source/CCRakNetUDT.h +++ b/Source/CCRakNetUDT.h @@ -1,404 +1,402 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "RakNetDefines.h" - -#if USE_SLIDING_WINDOW_CONGESTION_CONTROL!=1 - -#ifndef __CONGESTION_CONTROL_UDT_H -#define __CONGESTION_CONTROL_UDT_H - -#include "NativeTypes.h" -#include "RakNetTime.h" -#include "RakNetTypes.h" -#include "DS_Queue.h" - -/// Set to 4 if you are using the iPod Touch TG. See http://www.jenkinssoftware.com/forum/index.php?topic=2717.0 -#define CC_TIME_TYPE_BYTES 8 - -namespace RakNet -{ - -#if CC_TIME_TYPE_BYTES==8 -typedef uint64_t CCTimeType; -#else -typedef uint32_t CCTimeType; -#endif - -typedef uint24_t DatagramSequenceNumberType; -typedef double BytesPerMicrosecond; -typedef double BytesPerSecond; -typedef double MicrosecondsPerByte; - -/// CC_RAKNET_UDT_PACKET_HISTORY_LENGTH should be a power of 2 for the writeIndex variables to wrap properly -#define CC_RAKNET_UDT_PACKET_HISTORY_LENGTH 64 -#define RTT_HISTORY_LENGTH 64 - -/// Sizeof an UDP header in byte -#define UDP_HEADER_SIZE 28 - -#define CC_DEBUG_PRINTF_1(x) -#define CC_DEBUG_PRINTF_2(x,y) -#define CC_DEBUG_PRINTF_3(x,y,z) -#define CC_DEBUG_PRINTF_4(x,y,z,a) -#define CC_DEBUG_PRINTF_5(x,y,z,a,b) -//#define CC_DEBUG_PRINTF_1(x) printf(x) -//#define CC_DEBUG_PRINTF_2(x,y) printf(x,y) -//#define CC_DEBUG_PRINTF_3(x,y,z) printf(x,y,z) -//#define CC_DEBUG_PRINTF_4(x,y,z,a) printf(x,y,z,a) -//#define CC_DEBUG_PRINTF_5(x,y,z,a,b) printf(x,y,z,a,b) - -/// \brief Encapsulates UDT congestion control, as used by RakNet -/// Requirements: -///
    -///
  1. Each datagram is no more than MAXIMUM_MTU_SIZE, after accounting for the UDP header -///
  2. Each datagram containing a user message has a sequence number which is set after calling OnSendBytes(). Set it by calling GetAndIncrementNextDatagramSequenceNumber() -///
  3. System is designed to be used from a single thread. -///
  4. Each packet should have a timeout time based on GetSenderRTOForACK(). If this time elapses, add the packet to the head of the send list for retransmission. -///
-/// -/// Recommended: -///
    -///
  1. Call sendto in its own thread. This takes a significant amount of time in high speed networks. -///
-/// -/// Algorithm: -///
    -///
  1. On a new connection, call Init() -///
  2. On a periodic interval (SYN time is the best) call Update(). Also call ShouldSendACKs(), and send buffered ACKS if it returns true. -///
  3. Call OnSendAck() when sending acks. -///
  4. When you want to send or resend data, call GetNumberOfBytesToSend(). It will return you enough bytes to keep you busy for \a estimatedTimeToNextTick. You can send more than this to fill out a datagram, or to send packet pairs -///
  5. Call OnSendBytes() when sending datagrams. -///
  6. When data arrives, record the sequence number and buffer an ACK for it, to be sent from Update() if ShouldSendACKs() returns true -///
  7. Every 16 packets that you send, send two of them back to back (a packet pair) as long as both packets are the same size. If you don't have two packets the same size, it is fine to defer this until you do. -///
  8. When you get a packet, call OnGotPacket(). If the packet is also either of a packet pair, call OnGotPacketPair() -///
  9. If you get a packet, and the sequence number is not 1 + the last sequence number, send a NAK. On the remote system, call OnNAK() and resend that message. -///
  10. If you get an ACK, remove that message from retransmission. Call OnNonDuplicateAck(). -///
  11. If a message is not ACKed for GetRTOForRetransmission(), resend it. -///
-class CCRakNetUDT -{ - public: - - CCRakNetUDT(); - ~CCRakNetUDT(); - - /// Reset all variables to their initial states, for a new connection - void Init(CCTimeType curTime, uint32_t maxDatagramPayload); - - /// Update over time - void Update(CCTimeType curTime, bool hasDataToSendOrResend); - - int GetRetransmissionBandwidth(CCTimeType curTime, CCTimeType timeSinceLastTick, uint32_t unacknowledgedBytes, bool isContinuousSend); - int GetTransmissionBandwidth(CCTimeType curTime, CCTimeType timeSinceLastTick, uint32_t unacknowledgedBytes, bool isContinuousSend); - - /// Acks do not have to be sent immediately. Instead, they can be buffered up such that groups of acks are sent at a time - /// This reduces overall bandwidth usage - /// How long they can be buffered depends on the retransmit time of the sender - /// Should call once per update tick, and send if needed - bool ShouldSendACKs(CCTimeType curTime, CCTimeType estimatedTimeToNextTick); - - /// Every data packet sent must contain a sequence number - /// Call this function to get it. The sequence number is passed into OnGotPacketPair() - DatagramSequenceNumberType GetAndIncrementNextDatagramSequenceNumber(void); - DatagramSequenceNumberType GetNextDatagramSequenceNumber(void); - - /// Call this when you send packets - /// Every 15th and 16th packets should be sent as a packet pair if possible - /// When packets marked as a packet pair arrive, pass to OnGotPacketPair() - /// When any packets arrive, (additionally) pass to OnGotPacket - /// Packets should contain our system time, so we can pass rtt to OnNonDuplicateAck() - void OnSendBytes(CCTimeType curTime, uint32_t numBytes); - - /// Call this when you get a packet pair - void OnGotPacketPair(DatagramSequenceNumberType datagramSequenceNumber, uint32_t sizeInBytes, CCTimeType curTime); - - /// Call this when you get a packet (including packet pairs) - /// If the DatagramSequenceNumberType is out of order, skippedMessageCount will be non-zero - /// In that case, send a NAK for every sequence number up to that count - bool OnGotPacket(DatagramSequenceNumberType datagramSequenceNumber, bool isContinuousSend, CCTimeType curTime, uint32_t sizeInBytes, uint32_t *skippedMessageCount); - - /// Call when you get a NAK, with the sequence number of the lost message - /// Affects the congestion control - void OnResend(CCTimeType curTime, RakNet::TimeUS nextActionTime); - void OnNAK(CCTimeType curTime, DatagramSequenceNumberType nakSequenceNumber); - - /// Call this when an ACK arrives. - /// hasBAndAS are possibly written with the ack, see OnSendAck() - /// B and AS are used in the calculations in UpdateWindowSizeAndAckOnAckPerSyn - /// B and AS are updated at most once per SYN - void OnAck(CCTimeType curTime, CCTimeType rtt, bool hasBAndAS, BytesPerMicrosecond _B, BytesPerMicrosecond _AS, double totalUserDataBytesAcked, bool isContinuousSend, DatagramSequenceNumberType sequenceNumber ); - void OnDuplicateAck( CCTimeType curTime, DatagramSequenceNumberType sequenceNumber ) {} - - /// Call when you send an ack, to see if the ack should have the B and AS parameters transmitted - /// Call before calling OnSendAck() - void OnSendAckGetBAndAS(CCTimeType curTime, bool *hasBAndAS, BytesPerMicrosecond *_B, BytesPerMicrosecond *_AS); - - /// Call when we send an ack, to write B and AS if needed - /// B and AS are only written once per SYN, to prevent slow calculations - /// Also updates SND, the period between sends, since data is written out - /// Be sure to call OnSendAckGetBAndAS() before calling OnSendAck(), since whether you write it or not affects \a numBytes - void OnSendAck(CCTimeType curTime, uint32_t numBytes); - - /// Call when we send a NACK - /// Also updates SND, the period between sends, since data is written out - void OnSendNACK(CCTimeType curTime, uint32_t numBytes); - - /// Retransmission time out for the sender - /// If the time difference between when a message was last transmitted, and the current time is greater than RTO then packet is eligible for retransmission, pending congestion control - /// RTO = (RTT + 4 * RTTVar) + SYN - /// If we have been continuously sending for the last RTO, and no ACK or NAK at all, SND*=2; - /// This is per message, which is different from UDT, but RakNet supports packetloss with continuing data where UDT is only RELIABLE_ORDERED - /// Minimum value is 100 milliseconds - CCTimeType GetRTOForRetransmission(unsigned char timesSent) const; - - /// Set the maximum amount of data that can be sent in one datagram - /// Default to MAXIMUM_MTU_SIZE-UDP_HEADER_SIZE - void SetMTU(uint32_t bytes); - - /// Return what was set by SetMTU() - uint32_t GetMTU(void) const; - - /// Query for statistics - BytesPerMicrosecond GetLocalSendRate(void) const {return 1.0 / SND;} - BytesPerMicrosecond GetLocalReceiveRate(CCTimeType currentTime) const; - BytesPerMicrosecond GetRemoveReceiveRate(void) const {return AS;} - //BytesPerMicrosecond GetEstimatedBandwidth(void) const {return B;} - BytesPerMicrosecond GetEstimatedBandwidth(void) const {return GetLinkCapacityBytesPerSecond()*1000000.0;} - double GetLinkCapacityBytesPerSecond(void) const {return estimatedLinkCapacityBytesPerSecond;}; - - /// Query for statistics - double GetRTT(void) const; - - bool GetIsInSlowStart(void) const {return isInSlowStart;} - uint32_t GetCWNDLimit(void) const {return (uint32_t) (CWND*MAXIMUM_MTU_INCLUDING_UDP_HEADER);} - - - /// Is a > b, accounting for variable overflow? - static bool GreaterThan(DatagramSequenceNumberType a, DatagramSequenceNumberType b); - /// Is a < b, accounting for variable overflow? - static bool LessThan(DatagramSequenceNumberType a, DatagramSequenceNumberType b); -// void SetTimeBetweenSendsLimit(unsigned int bitsPerSecond); - uint64_t GetBytesPerSecondLimitByCongestionControl(void) const; - - protected: - // --------------------------- PROTECTED VARIABLES --------------------------- - /// time interval between bytes, in microseconds. - /// Only used when slowStart==false - /// Increased over time as we continually get messages - /// Decreased on NAK and timeout - /// Starts at 0 (invalid) - MicrosecondsPerByte SND; - - /// Supportive window mechanism, controlling the maximum number of in-flight packets - /// Used both during and after slow-start, but primarily during slow-start - /// Starts at 2, which is also the low threshhold - /// Max is the socket receive buffer / MTU - /// CWND = AS * (RTT + SYN) + 16 - double CWND; - - /// When we do an update process on the SYN interval, nextSYNUpdate is set to the next time we should update - /// Normally this is nextSYNUpdate+=SYN, in order to update on a consistent schedule - /// However, if this would result in an immediate update yet again, it is set to SYN microseconds past the current time (in case the thread did not update for a long time) - CCTimeType nextSYNUpdate; - - - /// Index into packetPairRecieptHistory where we will next write - /// The history is always full (starting with default values) so no read index is needed - int packetPairRecieptHistoryWriteIndex; - - /// Sent to the sender by the receiver from packetPairRecieptHistory whenever a back to back packet arrives on the receiver - /// Updated by B = B * .875 + incomingB * .125 - //BytesPerMicrosecond B; - - /// Running round trip time (ping*2) - /// Only sender needs to know this - /// Initialized to UNSET - /// Set to rtt on first calculation - /// Updated gradually by RTT = RTT * 0.875 + rtt * 0.125 - double RTT; - - /// Round trip time variance - /// Only sender needs to know this - /// Initialized to UNSET - /// Set to rtt on first calculation - // double RTTVar; - /// Update: Use min/max, RTTVar follows current variance too closely resulting in packetloss - double minRTT, maxRTT; - - /// Used to calculate packet arrival rate (in UDT) but data arrival rate (in RakNet, where not all datagrams are the same size) - /// Filter is used to cull lowest half of values for bytesPerMicrosecond, to discount spikes and inactivity - /// Referred to in the documentation as AS, data arrival rate - /// AS is sent to the sender and calculated every 10th ack - /// Each node represents (curTime-lastPacketArrivalTime)/bytes - /// Used with ReceiverCalculateDataArrivalRate(); - BytesPerMicrosecond packetArrivalHistory[CC_RAKNET_UDT_PACKET_HISTORY_LENGTH]; - BytesPerMicrosecond packetArrivalHistoryContinuousGaps[CC_RAKNET_UDT_PACKET_HISTORY_LENGTH]; - unsigned char packetArrivalHistoryContinuousGapsIndex; - uint64_t continuousBytesReceived; - CCTimeType continuousBytesReceivedStartTime; - unsigned int packetArrivalHistoryWriteCount; - - /// Index into packetArrivalHistory where we will next write - /// The history is always full (starting with default values) so no read index is needed - int packetArrivalHistoryWriteIndex; - - /// Tracks the time the last packet that arrived, so BytesPerMicrosecond can be calculated for packetArrivalHistory when a new packet arrives - CCTimeType lastPacketArrivalTime; - - /// Data arrival rate from the sender to the receiver, as told to us by the receiver - /// Used to calculate initial sending rate when slow start stops - BytesPerMicrosecond AS; - - /// When the receiver last calculated and send B and AS, from packetArrivalHistory and packetPairRecieptHistory - /// Used to prevent it from being calculated and send too frequently, as they are slow operations - CCTimeType lastTransmitOfBAndAS; - - /// New connections start in slow start - /// During slow start, SND is not used, only CWND - /// Slow start ends when we get a NAK, or the maximum size of CWND is reached - /// SND is initialized to the inverse of the receiver's packet arrival rate when slow start ends - bool isInSlowStart; - - /// How many NAKs arrived this congestion period - /// Initialized to 1 when the congestion period starts - uint32_t NAKCount; - - /// How many NAKs do you get on average during a congestion period? - /// Starts at 1 - /// Used to generate a random number, DecRandom, between 1 and AvgNAKNum - uint32_t AvgNAKNum; - - /// How many times we have decremented SND this congestion period. Used to limit the number of decrements to 5 - uint32_t DecCount; - - /// Every DecInterval NAKs per congestion period, we decrease the send rate - uint32_t DecInterval; - - /// Every outgoing datagram is assigned a sequence number, which increments by 1 every assignment - DatagramSequenceNumberType nextDatagramSequenceNumber; - - /// If a packet is marked as a packet pair, lastPacketPairPacketArrivalTime is set to the time it arrives - /// This is used so when the 2nd packet of the pair arrives, we can calculate the time interval between the two - CCTimeType lastPacketPairPacketArrivalTime; - - /// If a packet is marked as a packet pair, lastPacketPairSequenceNumber is checked to see if the last packet we got - /// was the packet immediately before the one that arrived - /// If so, we can use lastPacketPairPacketArrivalTime to get the time between the two packets, and thus estimate the link capacity - /// Initialized to -1, so the first packet of a packet pair won't be treated as the second - DatagramSequenceNumberType lastPacketPairSequenceNumber; - - /// Used to cap UpdateWindowSizeAndAckOnAckPerSyn() to once speed increase per SYN - /// This is to prevent speeding up faster than congestion control can compensate for - CCTimeType lastUpdateWindowSizeAndAck; - - /// Every time SND is halved due to timeout, the RTO is increased - /// This is to prevent massive retransmissions to an unresponsive system - /// Reset on any data arriving - double ExpCount; - - /// Total number of user data bytes sent - /// Used to adjust the window size, on ACK, during slow start - uint64_t totalUserDataBytesSent; - - /// When we get an ack, if oldestUnsentAck==0, set it to the current time - /// When we send out acks, set oldestUnsentAck to 0 - CCTimeType oldestUnsentAck; - - // Maximum amount of bytes that the user can send, e.g. the size of one full datagram - uint32_t MAXIMUM_MTU_INCLUDING_UDP_HEADER; - - // Max window size - double CWND_MAX_THRESHOLD; - - /// Track which datagram sequence numbers have arrived. - /// If a sequence number is skipped, send a NAK for all skipped messages - DatagramSequenceNumberType expectedNextSequenceNumber; - - // How many times have we sent B and AS? Used to force it to send at least CC_RAKNET_UDT_PACKET_HISTORY_LENGTH times - // Otherwise, the default values in the array generate inaccuracy - uint32_t sendBAndASCount; - - /// Most recent values read into the corresponding lists - /// Used during the beginning of a connection, when the median filter is still inaccurate - BytesPerMicrosecond mostRecentPacketArrivalHistory; - - bool hasWrittenToPacketPairReceiptHistory; - -// uint32_t rttHistory[RTT_HISTORY_LENGTH]; -// uint32_t rttHistoryIndex; -// uint32_t rttHistoryWriteCount; -// uint32_t rttSum, rttLow; -// CCTimeType lastSndUpdateTime; - double estimatedLinkCapacityBytesPerSecond; - - // --------------------------- PROTECTED METHODS --------------------------- - /// Update nextSYNUpdate by SYN, or the same amount past the current time if no updates have occurred for a long time - void SetNextSYNUpdate(CCTimeType currentTime); - - /// Returns the rate of data arrival, based on packets arriving on the sender. - BytesPerMicrosecond ReceiverCalculateDataArrivalRate(CCTimeType curTime) const; - /// Returns the median of the data arrival rate - BytesPerMicrosecond ReceiverCalculateDataArrivalRateMedian(void) const; - - /// Calculates the median an array of BytesPerMicrosecond - static BytesPerMicrosecond CalculateListMedianRecursive(const BytesPerMicrosecond inputList[CC_RAKNET_UDT_PACKET_HISTORY_LENGTH], int inputListLength, int lessThanSum, int greaterThanSum); -// static uint32_t CalculateListMedianRecursive(const uint32_t inputList[RTT_HISTORY_LENGTH], int inputListLength, int lessThanSum, int greaterThanSum); - - /// Same as GetRTOForRetransmission, but does not factor in ExpCount - /// This is because the receiver does not know ExpCount for the sender, and even if it did, acks shouldn't be delayed for this reason - CCTimeType GetSenderRTOForACK(void) const; - - /// Stop slow start, and enter normal transfer rate - void EndSlowStart(void); - - /// Does the named conversion - inline double BytesPerMicrosecondToPacketsPerMillisecond(BytesPerMicrosecond in); - - /// Update the round trip time, from ACK or ACK2 - //void UpdateRTT(CCTimeType rtt); - - /// Update the corresponding variables pre-slow start - void UpdateWindowSizeAndAckOnAckPreSlowStart(double totalUserDataBytesAcked); - - /// Update the corresponding variables post-slow start - void UpdateWindowSizeAndAckOnAckPerSyn(CCTimeType curTime, CCTimeType rtt, bool isContinuousSend, DatagramSequenceNumberType sequenceNumber); - - - /// Sets halveSNDOnNoDataTime to the future, and also resets ExpCount, which is used to multiple the RTO on no data arriving at all - void ResetOnDataArrivalHalveSNDOnNoDataTime(CCTimeType curTime); - - // Init array - void InitPacketArrivalHistory(void); - - // Printf - void PrintLowBandwidthWarning(void); - - // Bug: SND can sometimes get super high - have seen 11693 - void CapMinSnd(const char *file, int line); - - void DecreaseTimeBetweenSends(void); - void IncreaseTimeBetweenSends(void); - - int bytesCanSendThisTick; - - CCTimeType lastRttOnIncreaseSendRate; - CCTimeType lastRtt; - - DatagramSequenceNumberType nextCongestionControlBlock; - bool hadPacketlossThisBlock; - DataStructures::Queue pingsLastInterval; -}; - -} - -#endif - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "RakNetDefines.h" + +#if USE_SLIDING_WINDOW_CONGESTION_CONTROL!=1 + +#pragma once + +#include "NativeTypes.h" +#include "RakNetTime.h" +#include "RakNetTypes.h" +#include "DS_Queue.h" + +/// Set to 4 if you are using the iPod Touch TG. See http://www.jenkinssoftware.com/forum/index.php?topic=2717.0 +#define CC_TIME_TYPE_BYTES 8 + +namespace RakNet +{ + +#if CC_TIME_TYPE_BYTES==8 +typedef uint64_t CCTimeType; +#else +typedef uint32_t CCTimeType; +#endif + +typedef uint24_t DatagramSequenceNumberType; +typedef double BytesPerMicrosecond; +typedef double BytesPerSecond; +typedef double MicrosecondsPerByte; + +/// CC_RAKNET_UDT_PACKET_HISTORY_LENGTH should be a power of 2 for the writeIndex variables to wrap properly +#define CC_RAKNET_UDT_PACKET_HISTORY_LENGTH 64 +#define RTT_HISTORY_LENGTH 64 + +/// Sizeof an UDP header in byte +#define UDP_HEADER_SIZE 28 + +#define CC_DEBUG_PRINTF_1(x) +#define CC_DEBUG_PRINTF_2(x,y) +#define CC_DEBUG_PRINTF_3(x,y,z) +#define CC_DEBUG_PRINTF_4(x,y,z,a) +#define CC_DEBUG_PRINTF_5(x,y,z,a,b) +//#define CC_DEBUG_PRINTF_1(x) printf(x) +//#define CC_DEBUG_PRINTF_2(x,y) printf(x,y) +//#define CC_DEBUG_PRINTF_3(x,y,z) printf(x,y,z) +//#define CC_DEBUG_PRINTF_4(x,y,z,a) printf(x,y,z,a) +//#define CC_DEBUG_PRINTF_5(x,y,z,a,b) printf(x,y,z,a,b) + +/// \brief Encapsulates UDT congestion control, as used by RakNet +/// Requirements: +///
    +///
  1. Each datagram is no more than MAXIMUM_MTU_SIZE, after accounting for the UDP header +///
  2. Each datagram containing a user message has a sequence number which is set after calling OnSendBytes(). Set it by calling GetAndIncrementNextDatagramSequenceNumber() +///
  3. System is designed to be used from a single thread. +///
  4. Each packet should have a timeout time based on GetSenderRTOForACK(). If this time elapses, add the packet to the head of the send list for retransmission. +///
+/// +/// Recommended: +///
    +///
  1. Call sendto in its own thread. This takes a significant amount of time in high speed networks. +///
+/// +/// Algorithm: +///
    +///
  1. On a new connection, call Init() +///
  2. On a periodic interval (SYN time is the best) call Update(). Also call ShouldSendACKs(), and send buffered ACKS if it returns true. +///
  3. Call OnSendAck() when sending acks. +///
  4. When you want to send or resend data, call GetNumberOfBytesToSend(). It will return you enough bytes to keep you busy for \a estimatedTimeToNextTick. You can send more than this to fill out a datagram, or to send packet pairs +///
  5. Call OnSendBytes() when sending datagrams. +///
  6. When data arrives, record the sequence number and buffer an ACK for it, to be sent from Update() if ShouldSendACKs() returns true +///
  7. Every 16 packets that you send, send two of them back to back (a packet pair) as long as both packets are the same size. If you don't have two packets the same size, it is fine to defer this until you do. +///
  8. When you get a packet, call OnGotPacket(). If the packet is also either of a packet pair, call OnGotPacketPair() +///
  9. If you get a packet, and the sequence number is not 1 + the last sequence number, send a NAK. On the remote system, call OnNAK() and resend that message. +///
  10. If you get an ACK, remove that message from retransmission. Call OnNonDuplicateAck(). +///
  11. If a message is not ACKed for GetRTOForRetransmission(), resend it. +///
+class CCRakNetUDT +{ + public: + + CCRakNetUDT(); + ~CCRakNetUDT(); + + /// Reset all variables to their initial states, for a new connection + void Init(CCTimeType curTime, uint32_t maxDatagramPayload); + + /// Update over time + void Update(CCTimeType curTime, bool hasDataToSendOrResend); + + int GetRetransmissionBandwidth(CCTimeType curTime, CCTimeType timeSinceLastTick, uint32_t unacknowledgedBytes, bool isContinuousSend); + int GetTransmissionBandwidth(CCTimeType curTime, CCTimeType timeSinceLastTick, uint32_t unacknowledgedBytes, bool isContinuousSend); + + /// Acks do not have to be sent immediately. Instead, they can be buffered up such that groups of acks are sent at a time + /// This reduces overall bandwidth usage + /// How long they can be buffered depends on the retransmit time of the sender + /// Should call once per update tick, and send if needed + bool ShouldSendACKs(CCTimeType curTime, CCTimeType estimatedTimeToNextTick); + + /// Every data packet sent must contain a sequence number + /// Call this function to get it. The sequence number is passed into OnGotPacketPair() + DatagramSequenceNumberType GetAndIncrementNextDatagramSequenceNumber(void); + DatagramSequenceNumberType GetNextDatagramSequenceNumber(void); + + /// Call this when you send packets + /// Every 15th and 16th packets should be sent as a packet pair if possible + /// When packets marked as a packet pair arrive, pass to OnGotPacketPair() + /// When any packets arrive, (additionally) pass to OnGotPacket + /// Packets should contain our system time, so we can pass rtt to OnNonDuplicateAck() + void OnSendBytes(CCTimeType curTime, uint32_t numBytes); + + /// Call this when you get a packet pair + void OnGotPacketPair(DatagramSequenceNumberType datagramSequenceNumber, uint32_t sizeInBytes, CCTimeType curTime); + + /// Call this when you get a packet (including packet pairs) + /// If the DatagramSequenceNumberType is out of order, skippedMessageCount will be non-zero + /// In that case, send a NAK for every sequence number up to that count + bool OnGotPacket(DatagramSequenceNumberType datagramSequenceNumber, bool isContinuousSend, CCTimeType curTime, uint32_t sizeInBytes, uint32_t *skippedMessageCount); + + /// Call when you get a NAK, with the sequence number of the lost message + /// Affects the congestion control + void OnResend(CCTimeType curTime, RakNet::TimeUS nextActionTime); + void OnNAK(CCTimeType curTime, DatagramSequenceNumberType nakSequenceNumber); + + /// Call this when an ACK arrives. + /// hasBAndAS are possibly written with the ack, see OnSendAck() + /// B and AS are used in the calculations in UpdateWindowSizeAndAckOnAckPerSyn + /// B and AS are updated at most once per SYN + void OnAck(CCTimeType curTime, CCTimeType rtt, bool hasBAndAS, BytesPerMicrosecond _B, BytesPerMicrosecond _AS, double totalUserDataBytesAcked, bool isContinuousSend, DatagramSequenceNumberType sequenceNumber ); + void OnDuplicateAck( CCTimeType curTime, DatagramSequenceNumberType sequenceNumber ) {} + + /// Call when you send an ack, to see if the ack should have the B and AS parameters transmitted + /// Call before calling OnSendAck() + void OnSendAckGetBAndAS(CCTimeType curTime, bool *hasBAndAS, BytesPerMicrosecond *_B, BytesPerMicrosecond *_AS); + + /// Call when we send an ack, to write B and AS if needed + /// B and AS are only written once per SYN, to prevent slow calculations + /// Also updates SND, the period between sends, since data is written out + /// Be sure to call OnSendAckGetBAndAS() before calling OnSendAck(), since whether you write it or not affects \a numBytes + void OnSendAck(CCTimeType curTime, uint32_t numBytes); + + /// Call when we send a NACK + /// Also updates SND, the period between sends, since data is written out + void OnSendNACK(CCTimeType curTime, uint32_t numBytes); + + /// Retransmission time out for the sender + /// If the time difference between when a message was last transmitted, and the current time is greater than RTO then packet is eligible for retransmission, pending congestion control + /// RTO = (RTT + 4 * RTTVar) + SYN + /// If we have been continuously sending for the last RTO, and no ACK or NAK at all, SND*=2; + /// This is per message, which is different from UDT, but RakNet supports packetloss with continuing data where UDT is only RELIABLE_ORDERED + /// Minimum value is 100 milliseconds + CCTimeType GetRTOForRetransmission(unsigned char timesSent) const; + + /// Set the maximum amount of data that can be sent in one datagram + /// Default to MAXIMUM_MTU_SIZE-UDP_HEADER_SIZE + void SetMTU(uint32_t bytes); + + /// Return what was set by SetMTU() + uint32_t GetMTU(void) const; + + /// Query for statistics + BytesPerMicrosecond GetLocalSendRate(void) const {return 1.0 / SND;} + BytesPerMicrosecond GetLocalReceiveRate(CCTimeType currentTime) const; + BytesPerMicrosecond GetRemoveReceiveRate(void) const {return AS;} + //BytesPerMicrosecond GetEstimatedBandwidth(void) const {return B;} + BytesPerMicrosecond GetEstimatedBandwidth(void) const {return GetLinkCapacityBytesPerSecond()*1000000.0;} + double GetLinkCapacityBytesPerSecond(void) const {return estimatedLinkCapacityBytesPerSecond;}; + + /// Query for statistics + double GetRTT(void) const; + + bool GetIsInSlowStart(void) const {return isInSlowStart;} + uint32_t GetCWNDLimit(void) const {return (uint32_t) (CWND*MAXIMUM_MTU_INCLUDING_UDP_HEADER);} + + + /// Is a > b, accounting for variable overflow? + static bool GreaterThan(DatagramSequenceNumberType a, DatagramSequenceNumberType b); + /// Is a < b, accounting for variable overflow? + static bool LessThan(DatagramSequenceNumberType a, DatagramSequenceNumberType b); +// void SetTimeBetweenSendsLimit(unsigned int bitsPerSecond); + uint64_t GetBytesPerSecondLimitByCongestionControl(void) const; + + protected: + // --------------------------- PROTECTED VARIABLES --------------------------- + /// time interval between bytes, in microseconds. + /// Only used when slowStart==false + /// Increased over time as we continually get messages + /// Decreased on NAK and timeout + /// Starts at 0 (invalid) + MicrosecondsPerByte SND; + + /// Supportive window mechanism, controlling the maximum number of in-flight packets + /// Used both during and after slow-start, but primarily during slow-start + /// Starts at 2, which is also the low threshhold + /// Max is the socket receive buffer / MTU + /// CWND = AS * (RTT + SYN) + 16 + double CWND; + + /// When we do an update process on the SYN interval, nextSYNUpdate is set to the next time we should update + /// Normally this is nextSYNUpdate+=SYN, in order to update on a consistent schedule + /// However, if this would result in an immediate update yet again, it is set to SYN microseconds past the current time (in case the thread did not update for a long time) + CCTimeType nextSYNUpdate; + + + /// Index into packetPairRecieptHistory where we will next write + /// The history is always full (starting with default values) so no read index is needed + int packetPairRecieptHistoryWriteIndex; + + /// Sent to the sender by the receiver from packetPairRecieptHistory whenever a back to back packet arrives on the receiver + /// Updated by B = B * .875 + incomingB * .125 + //BytesPerMicrosecond B; + + /// Running round trip time (ping*2) + /// Only sender needs to know this + /// Initialized to UNSET + /// Set to rtt on first calculation + /// Updated gradually by RTT = RTT * 0.875 + rtt * 0.125 + double RTT; + + /// Round trip time variance + /// Only sender needs to know this + /// Initialized to UNSET + /// Set to rtt on first calculation + // double RTTVar; + /// Update: Use min/max, RTTVar follows current variance too closely resulting in packetloss + double minRTT, maxRTT; + + /// Used to calculate packet arrival rate (in UDT) but data arrival rate (in RakNet, where not all datagrams are the same size) + /// Filter is used to cull lowest half of values for bytesPerMicrosecond, to discount spikes and inactivity + /// Referred to in the documentation as AS, data arrival rate + /// AS is sent to the sender and calculated every 10th ack + /// Each node represents (curTime-lastPacketArrivalTime)/bytes + /// Used with ReceiverCalculateDataArrivalRate(); + BytesPerMicrosecond packetArrivalHistory[CC_RAKNET_UDT_PACKET_HISTORY_LENGTH]; + BytesPerMicrosecond packetArrivalHistoryContinuousGaps[CC_RAKNET_UDT_PACKET_HISTORY_LENGTH]; + unsigned char packetArrivalHistoryContinuousGapsIndex; + uint64_t continuousBytesReceived; + CCTimeType continuousBytesReceivedStartTime; + unsigned int packetArrivalHistoryWriteCount; + + /// Index into packetArrivalHistory where we will next write + /// The history is always full (starting with default values) so no read index is needed + int packetArrivalHistoryWriteIndex; + + /// Tracks the time the last packet that arrived, so BytesPerMicrosecond can be calculated for packetArrivalHistory when a new packet arrives + CCTimeType lastPacketArrivalTime; + + /// Data arrival rate from the sender to the receiver, as told to us by the receiver + /// Used to calculate initial sending rate when slow start stops + BytesPerMicrosecond AS; + + /// When the receiver last calculated and send B and AS, from packetArrivalHistory and packetPairRecieptHistory + /// Used to prevent it from being calculated and send too frequently, as they are slow operations + CCTimeType lastTransmitOfBAndAS; + + /// New connections start in slow start + /// During slow start, SND is not used, only CWND + /// Slow start ends when we get a NAK, or the maximum size of CWND is reached + /// SND is initialized to the inverse of the receiver's packet arrival rate when slow start ends + bool isInSlowStart; + + /// How many NAKs arrived this congestion period + /// Initialized to 1 when the congestion period starts + uint32_t NAKCount; + + /// How many NAKs do you get on average during a congestion period? + /// Starts at 1 + /// Used to generate a random number, DecRandom, between 1 and AvgNAKNum + uint32_t AvgNAKNum; + + /// How many times we have decremented SND this congestion period. Used to limit the number of decrements to 5 + uint32_t DecCount; + + /// Every DecInterval NAKs per congestion period, we decrease the send rate + uint32_t DecInterval; + + /// Every outgoing datagram is assigned a sequence number, which increments by 1 every assignment + DatagramSequenceNumberType nextDatagramSequenceNumber; + + /// If a packet is marked as a packet pair, lastPacketPairPacketArrivalTime is set to the time it arrives + /// This is used so when the 2nd packet of the pair arrives, we can calculate the time interval between the two + CCTimeType lastPacketPairPacketArrivalTime; + + /// If a packet is marked as a packet pair, lastPacketPairSequenceNumber is checked to see if the last packet we got + /// was the packet immediately before the one that arrived + /// If so, we can use lastPacketPairPacketArrivalTime to get the time between the two packets, and thus estimate the link capacity + /// Initialized to -1, so the first packet of a packet pair won't be treated as the second + DatagramSequenceNumberType lastPacketPairSequenceNumber; + + /// Used to cap UpdateWindowSizeAndAckOnAckPerSyn() to once speed increase per SYN + /// This is to prevent speeding up faster than congestion control can compensate for + CCTimeType lastUpdateWindowSizeAndAck; + + /// Every time SND is halved due to timeout, the RTO is increased + /// This is to prevent massive retransmissions to an unresponsive system + /// Reset on any data arriving + double ExpCount; + + /// Total number of user data bytes sent + /// Used to adjust the window size, on ACK, during slow start + uint64_t totalUserDataBytesSent; + + /// When we get an ack, if oldestUnsentAck==0, set it to the current time + /// When we send out acks, set oldestUnsentAck to 0 + CCTimeType oldestUnsentAck; + + // Maximum amount of bytes that the user can send, e.g. the size of one full datagram + uint32_t MAXIMUM_MTU_INCLUDING_UDP_HEADER; + + // Max window size + double CWND_MAX_THRESHOLD; + + /// Track which datagram sequence numbers have arrived. + /// If a sequence number is skipped, send a NAK for all skipped messages + DatagramSequenceNumberType expectedNextSequenceNumber; + + // How many times have we sent B and AS? Used to force it to send at least CC_RAKNET_UDT_PACKET_HISTORY_LENGTH times + // Otherwise, the default values in the array generate inaccuracy + uint32_t sendBAndASCount; + + /// Most recent values read into the corresponding lists + /// Used during the beginning of a connection, when the median filter is still inaccurate + BytesPerMicrosecond mostRecentPacketArrivalHistory; + + bool hasWrittenToPacketPairReceiptHistory; + +// uint32_t rttHistory[RTT_HISTORY_LENGTH]; +// uint32_t rttHistoryIndex; +// uint32_t rttHistoryWriteCount; +// uint32_t rttSum, rttLow; +// CCTimeType lastSndUpdateTime; + double estimatedLinkCapacityBytesPerSecond; + + // --------------------------- PROTECTED METHODS --------------------------- + /// Update nextSYNUpdate by SYN, or the same amount past the current time if no updates have occurred for a long time + void SetNextSYNUpdate(CCTimeType currentTime); + + /// Returns the rate of data arrival, based on packets arriving on the sender. + BytesPerMicrosecond ReceiverCalculateDataArrivalRate(CCTimeType curTime) const; + /// Returns the median of the data arrival rate + BytesPerMicrosecond ReceiverCalculateDataArrivalRateMedian(void) const; + + /// Calculates the median an array of BytesPerMicrosecond + static BytesPerMicrosecond CalculateListMedianRecursive(const BytesPerMicrosecond inputList[CC_RAKNET_UDT_PACKET_HISTORY_LENGTH], int inputListLength, int lessThanSum, int greaterThanSum); +// static uint32_t CalculateListMedianRecursive(const uint32_t inputList[RTT_HISTORY_LENGTH], int inputListLength, int lessThanSum, int greaterThanSum); + + /// Same as GetRTOForRetransmission, but does not factor in ExpCount + /// This is because the receiver does not know ExpCount for the sender, and even if it did, acks shouldn't be delayed for this reason + CCTimeType GetSenderRTOForACK(void) const; + + /// Stop slow start, and enter normal transfer rate + void EndSlowStart(void); + + /// Does the named conversion + inline double BytesPerMicrosecondToPacketsPerMillisecond(BytesPerMicrosecond in); + + /// Update the round trip time, from ACK or ACK2 + //void UpdateRTT(CCTimeType rtt); + + /// Update the corresponding variables pre-slow start + void UpdateWindowSizeAndAckOnAckPreSlowStart(double totalUserDataBytesAcked); + + /// Update the corresponding variables post-slow start + void UpdateWindowSizeAndAckOnAckPerSyn(CCTimeType curTime, CCTimeType rtt, bool isContinuousSend, DatagramSequenceNumberType sequenceNumber); + + + /// Sets halveSNDOnNoDataTime to the future, and also resets ExpCount, which is used to multiple the RTO on no data arriving at all + void ResetOnDataArrivalHalveSNDOnNoDataTime(CCTimeType curTime); + + // Init array + void InitPacketArrivalHistory(void); + + // Printf + void PrintLowBandwidthWarning(void); + + // Bug: SND can sometimes get super high - have seen 11693 + void CapMinSnd(const char *file, int line); + + void DecreaseTimeBetweenSends(void); + void IncreaseTimeBetweenSends(void); + + int bytesCanSendThisTick; + + CCTimeType lastRttOnIncreaseSendRate; + CCTimeType lastRtt; + + DatagramSequenceNumberType nextCongestionControlBlock; + bool hadPacketlossThisBlock; + DataStructures::Queue pingsLastInterval; +}; + +} + +#endif + diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 65660a1a7..4a618ee30 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -1 +1 @@ -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 3.10) diff --git a/Source/CheckSum.h b/Source/CheckSum.h index 0cca48b35..cb2fcf448 100644 --- a/Source/CheckSum.h +++ b/Source/CheckSum.h @@ -1,63 +1,61 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// -/// \file CheckSum.cpp -/// \brief [Internal] CheckSum implementation from http://www.flounder.com/checksum.htm -/// - -#ifndef __CHECKSUM_H -#define __CHECKSUM_H - -#include "RakMemoryOverride.h" - -/// Generates and validates checksums -class CheckSum -{ - -public: - - /// Default constructor - - CheckSum() - { - Clear(); - } - - void Clear() - { - sum = 0; - r = 55665; - c1 = 52845; - c2 = 22719; - } - - void Add ( unsigned int w ); - - - void Add ( unsigned short w ); - - void Add ( unsigned char* b, unsigned int length ); - - void Add ( unsigned char b ); - - unsigned int Get () - { - return sum; - } - -protected: - unsigned short r; - unsigned short c1; - unsigned short c2; - unsigned int sum; -}; - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// +/// \file CheckSum.cpp +/// \brief [Internal] CheckSum implementation from http://www.flounder.com/checksum.htm +/// + +#pragma once + +#include "RakMemoryOverride.h" + +/// Generates and validates checksums +class CheckSum +{ + +public: + + /// Default constructor + + CheckSum() + { + Clear(); + } + + void Clear() + { + sum = 0; + r = 55665; + c1 = 52845; + c2 = 22719; + } + + void Add ( unsigned int w ); + + + void Add ( unsigned short w ); + + void Add ( unsigned char* b, unsigned int length ); + + void Add ( unsigned char b ); + + unsigned int Get () + { + return sum; + } + +protected: + unsigned short r; + unsigned short c1; + unsigned short c2; + unsigned int sum; +}; + diff --git a/Source/CloudClient.h b/Source/CloudClient.h index bacf25c93..7e22ad98c 100644 --- a/Source/CloudClient.h +++ b/Source/CloudClient.h @@ -1,171 +1,169 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file CloudClient.h -/// \brief Queries CloudMemoryServer to download data that other clients have uploaded -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_CloudClient==1 - -#ifndef __CLOUD_CLIENT_H -#define __CLOUD_CLIENT_H - -#include "PluginInterface2.h" -#include "CloudCommon.h" -#include "RakMemoryOverride.h" -#include "DS_Hash.h" - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; -class CloudClientCallback; - -/// \defgroup CLOUD_GROUP CloudComputing -/// \brief Contains the CloudClient and CloudServer plugins -/// \details The CloudServer plugins operates on requests from the CloudClient plugin. The servers are in a fully connected mesh topology, which the clients are connected to any server. Clients can interact with each other by posting and subscribing to memory updates, without being directly connected or even knowing about each other. -/// \ingroup PLUGINS_GROUP - -/// \brief Performs Post() and Get() operations on CloudMemoryServer -/// \details A CloudClient is a computer connected to one or more servers in a cloud configuration. Operations by one CloudClient can be received and subscribed to by other instances of CloudClient, without those clients being connected, even on different servers. -/// \ingroup CLOUD_GROUP -class RAK_DLL_EXPORT CloudClient : public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(CloudClient) - - CloudClient(); - virtual ~CloudClient(); - - /// \brief Set the default callbacks for OnGetReponse(), OnSubscriptionNotification(), and OnSubscriptionDataDeleted() - /// \details Pointers to CloudAllocator and CloudClientCallback can be stored by the system if desired. If a callback is not provided to OnGetReponse(), OnSubscriptionNotification(), OnSubscriptionDataDeleted(), the callback passed here will be used instead. - /// \param[in] _allocator An instance of CloudAllocator - /// \param[in] _callback An instance of CloudClientCallback - virtual void SetDefaultCallbacks(CloudAllocator *_allocator, CloudClientCallback *_callback); - - /// \brief Uploads data to the cloud - /// \details Data uploaded to the cloud will be stored by the server sent to, identified by \a systemIdentifier. - /// As long as you are connected to this server, the data will persist. Queries for that data by the Get() operation will - /// return the RakNetGUID and SystemAddress of the uploader, as well as the data itself. - /// Furthermore, if any clients are subscribed to the particular CloudKey passed, those clients will get update notices that the data has changed - /// Passing data with the same \a cloudKey more than once will overwrite the prior value. - /// This call will silently fail if CloudServer::SetMaxUploadBytesPerClient() is exceeded - /// \param[in] cloudKey Identifies the data being uploaded - /// \param[in] data A pointer to data to upload. This pointer does not need to persist past the call - /// \param[in] dataLengthBytes The length in bytes of \a data - /// \param[in] systemIdentifier A remote system running CloudServer that we are already connected to. - virtual void Post(CloudKey *cloudKey, const unsigned char *data, uint32_t dataLengthBytes, RakNetGUID systemIdentifier); - - /// \brief Releases one or more data previously uploaded with Post() - /// \details If a remote system has subscribed to one or more of the \a keys uploaded, they will get ID_CLOUD_SUBSCRIPTION_NOTIFICATION notifications containing the last value uploaded before deletions - /// \param[in] cloudKey Identifies the data to release. It is possible to remove uploads from multiple Post() calls at once. - /// \param[in] systemIdentifier A remote system running CloudServer that we are already connected to. - virtual void Release(DataStructures::List &keys, RakNetGUID systemIdentifier); - - /// \brief Gets data from the cloud - /// \details For a given query containing one or more keys, return data that matches those keys. - /// The values will be returned in the ID_CLOUD_GET_RESPONSE packet, which should be passed to OnGetReponse() and will invoke CloudClientCallback::OnGet() - /// CloudQuery::startingRowIndex is used to skip the first n values that would normally be returned.. - /// CloudQuery::maxRowsToReturn is used to limit the number of rows returned. The number of rows returned may also be limited by CloudServer::SetMaxBytesPerDownload(); - /// CloudQuery::subscribeToResults if set to true, will cause ID_CLOUD_SUBSCRIPTION_NOTIFICATION to be returned to us when any of the keys in the query are updated or are deleted. - /// ID_CLOUD_GET_RESPONSE will be returned even if subscribing to the result list. Only later updates will return ID_CLOUD_SUBSCRIPTION_NOTIFICATION. - /// Calling Get() with CloudQuery::subscribeToResults false, when you are already subscribed, does not remove the subscription. Use Unsubscribe() for this. - /// Resubscribing using the same CloudKey but a different or no \a specificSystems overwrites the subscribed systems for those keys. - /// \param[in] cloudQuery One or more keys, and optional parameters to perform with the Get - /// \param[in] systemIdentifier A remote system running CloudServer that we are already connected to. - /// \param[in] specificSystems It is possible to get or subscribe to updates only for specific uploading CloudClient instances. Pass the desired instances here. The overload that does not have the specificSystems parameter is treated as subscribing to all updates from all clients. - virtual bool Get(CloudQuery *cloudQuery, RakNetGUID systemIdentifier); - virtual bool Get(CloudQuery *cloudQuery, DataStructures::List &specificSystems, RakNetGUID systemIdentifier); - virtual bool Get(CloudQuery *cloudQuery, DataStructures::List &specificSystems, RakNetGUID systemIdentifier); - - /// \brief Unsubscribe from updates previously subscribed to using Get() with the CloudQuery::subscribeToResults set to true - /// The \a keys and \a specificSystems parameters are logically treated as AND when checking subscriptions on the server - /// The overload that does not take specificSystems unsubscribes to all passed keys, regardless of system - /// You cannot unsubscribe specific systems when previously subscribed to updates from any system. To do this, first Unsubscribe() from all systems, and call Get() with the \a specificSystems parameter explicilty listing the systems you want to subscribe to. - virtual void Unsubscribe(DataStructures::List &keys, RakNetGUID systemIdentifier); - virtual void Unsubscribe(DataStructures::List &keys, DataStructures::List &specificSystems, RakNetGUID systemIdentifier); - virtual void Unsubscribe(DataStructures::List &keys, DataStructures::List &specificSystems, RakNetGUID systemIdentifier); - - /// \brief Call this when you get ID_CLOUD_GET_RESPONSE - /// If \a callback or \a allocator are 0, the default callbacks passed to SetDefaultCallbacks() are used - /// \param[in] packet Packet structure returned from RakPeerInterface - /// \param[in] _callback Callback to be called from the function containing output parameters. If 0, default is used. - /// \param[in] _allocator Allocator to be used to allocate data. If 0, default is used. - virtual void OnGetReponse(Packet *packet, CloudClientCallback *_callback=0, CloudAllocator *_allocator=0); - - /// \brief Call this when you get ID_CLOUD_GET_RESPONSE - /// Different form of OnGetReponse that returns to a structure that you pass, instead of using a callback - /// You are responsible for deallocation with this form - /// If \a allocator is 0, the default callback passed to SetDefaultCallbacks() are used - /// \param[out] cloudQueryResult A pointer to a structure that will be filled out with data - /// \param[in] packet Packet structure returned from RakPeerInterface - /// \param[in] _allocator Allocator to be used to allocate data. If 0, default is used. - virtual void OnGetReponse(CloudQueryResult *cloudQueryResult, Packet *packet, CloudAllocator *_allocator=0); - - /// \brief Call this when you get ID_CLOUD_SUBSCRIPTION_NOTIFICATION - /// If \a callback or \a allocator are 0, the default callbacks passed to SetDefaultCallbacks() are used - /// \param[in] packet Packet structure returned from RakPeerInterface - /// \param[in] _callback Callback to be called from the function containing output parameters. If 0, default is used. - /// \param[in] _allocator Allocator to be used to allocate data. If 0, default is used. - virtual void OnSubscriptionNotification(Packet *packet, CloudClientCallback *_callback=0, CloudAllocator *_allocator=0); - - /// \brief Call this when you get ID_CLOUD_SUBSCRIPTION_NOTIFICATION - /// Different form of OnSubscriptionNotification that returns to a structure that you pass, instead of using a callback - /// You are responsible for deallocation with this form - /// If \a allocator is 0, the default callback passed to SetDefaultCallbacks() are used - /// \param[out] wasUpdated If true, the row was updated. If false, it was deleted. \a result will contain the last value just before deletion - /// \param[out] row A pointer to a structure that will be filled out with data - /// \param[in] packet Packet structure returned from RakPeerInterface - /// \param[in] _allocator Allocator to be used to allocate data. If 0, default is used. - virtual void OnSubscriptionNotification(bool *wasUpdated, CloudQueryRow *row, Packet *packet, CloudAllocator *_allocator=0); - - /// If you never specified an allocator, and used the non-callback form of OnGetReponse(), deallocate cloudQueryResult with this function - virtual void DeallocateWithDefaultAllocator(CloudQueryResult *cloudQueryResult); - - /// If you never specified an allocator, and used the non-callback form of OnSubscriptionNotification(), deallocate row with this function - virtual void DeallocateWithDefaultAllocator(CloudQueryRow *row); - -protected: - PluginReceiveResult OnReceive(Packet *packet); - - CloudClientCallback *callback; - CloudAllocator *allocator; - - CloudAllocator unsetDefaultAllocator; -}; - -/// \ingroup CLOUD_GROUP -/// Parses ID_CLOUD_GET_RESPONSE and ID_CLOUD_SUBSCRIPTION_NOTIFICATION in a convenient callback form -class RAK_DLL_EXPORT CloudClientCallback -{ -public: - CloudClientCallback() {} - virtual ~CloudClientCallback() {} - - /// \brief Called in response to ID_CLOUD_GET_RESPONSE - /// \param[out] result Contains the original query passed to Get(), and a list of rows returned. - /// \param[out] deallocateRowsAfterReturn CloudQueryResult::rowsReturned will be deallocated after the function returns by default. Set to false to not deallocate these pointers. The pointers are allocated through CloudAllocator. - virtual void OnGet(RakNet::CloudQueryResult *result, bool *deallocateRowsAfterReturn) {(void) result; (void) deallocateRowsAfterReturn;} - - /// \brief Called in response to ID_CLOUD_SUBSCRIPTION_NOTIFICATION - /// \param[out] result Contains the row updated - /// \param[out] wasUpdated If true, the row was updated. If false, it was deleted. \a result will contain the last value just before deletion - /// \param[out] deallocateRowAfterReturn \a result will be deallocated after the function returns by default. Set to false to not deallocate these pointers. The pointers are allocated through CloudAllocator. - virtual void OnSubscriptionNotification(RakNet::CloudQueryRow *result, bool wasUpdated, bool *deallocateRowAfterReturn) {(void) result; (void) wasUpdated; (void) deallocateRowAfterReturn;} -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file CloudClient.h +/// \brief Queries CloudMemoryServer to download data that other clients have uploaded +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_CloudClient==1 + +#pragma once + +#include "PluginInterface2.h" +#include "CloudCommon.h" +#include "RakMemoryOverride.h" +#include "DS_Hash.h" + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; +class CloudClientCallback; + +/// \defgroup CLOUD_GROUP CloudComputing +/// \brief Contains the CloudClient and CloudServer plugins +/// \details The CloudServer plugins operates on requests from the CloudClient plugin. The servers are in a fully connected mesh topology, which the clients are connected to any server. Clients can interact with each other by posting and subscribing to memory updates, without being directly connected or even knowing about each other. +/// \ingroup PLUGINS_GROUP + +/// \brief Performs Post() and Get() operations on CloudMemoryServer +/// \details A CloudClient is a computer connected to one or more servers in a cloud configuration. Operations by one CloudClient can be received and subscribed to by other instances of CloudClient, without those clients being connected, even on different servers. +/// \ingroup CLOUD_GROUP +class RAK_DLL_EXPORT CloudClient : public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(CloudClient) + + CloudClient(); + virtual ~CloudClient(); + + /// \brief Set the default callbacks for OnGetReponse(), OnSubscriptionNotification(), and OnSubscriptionDataDeleted() + /// \details Pointers to CloudAllocator and CloudClientCallback can be stored by the system if desired. If a callback is not provided to OnGetReponse(), OnSubscriptionNotification(), OnSubscriptionDataDeleted(), the callback passed here will be used instead. + /// \param[in] _allocator An instance of CloudAllocator + /// \param[in] _callback An instance of CloudClientCallback + virtual void SetDefaultCallbacks(CloudAllocator *_allocator, CloudClientCallback *_callback); + + /// \brief Uploads data to the cloud + /// \details Data uploaded to the cloud will be stored by the server sent to, identified by \a systemIdentifier. + /// As long as you are connected to this server, the data will persist. Queries for that data by the Get() operation will + /// return the RakNetGUID and SystemAddress of the uploader, as well as the data itself. + /// Furthermore, if any clients are subscribed to the particular CloudKey passed, those clients will get update notices that the data has changed + /// Passing data with the same \a cloudKey more than once will overwrite the prior value. + /// This call will silently fail if CloudServer::SetMaxUploadBytesPerClient() is exceeded + /// \param[in] cloudKey Identifies the data being uploaded + /// \param[in] data A pointer to data to upload. This pointer does not need to persist past the call + /// \param[in] dataLengthBytes The length in bytes of \a data + /// \param[in] systemIdentifier A remote system running CloudServer that we are already connected to. + virtual void Post(CloudKey *cloudKey, const unsigned char *data, uint32_t dataLengthBytes, RakNetGUID systemIdentifier); + + /// \brief Releases one or more data previously uploaded with Post() + /// \details If a remote system has subscribed to one or more of the \a keys uploaded, they will get ID_CLOUD_SUBSCRIPTION_NOTIFICATION notifications containing the last value uploaded before deletions + /// \param[in] cloudKey Identifies the data to release. It is possible to remove uploads from multiple Post() calls at once. + /// \param[in] systemIdentifier A remote system running CloudServer that we are already connected to. + virtual void Release(DataStructures::List &keys, RakNetGUID systemIdentifier); + + /// \brief Gets data from the cloud + /// \details For a given query containing one or more keys, return data that matches those keys. + /// The values will be returned in the ID_CLOUD_GET_RESPONSE packet, which should be passed to OnGetReponse() and will invoke CloudClientCallback::OnGet() + /// CloudQuery::startingRowIndex is used to skip the first n values that would normally be returned.. + /// CloudQuery::maxRowsToReturn is used to limit the number of rows returned. The number of rows returned may also be limited by CloudServer::SetMaxBytesPerDownload(); + /// CloudQuery::subscribeToResults if set to true, will cause ID_CLOUD_SUBSCRIPTION_NOTIFICATION to be returned to us when any of the keys in the query are updated or are deleted. + /// ID_CLOUD_GET_RESPONSE will be returned even if subscribing to the result list. Only later updates will return ID_CLOUD_SUBSCRIPTION_NOTIFICATION. + /// Calling Get() with CloudQuery::subscribeToResults false, when you are already subscribed, does not remove the subscription. Use Unsubscribe() for this. + /// Resubscribing using the same CloudKey but a different or no \a specificSystems overwrites the subscribed systems for those keys. + /// \param[in] cloudQuery One or more keys, and optional parameters to perform with the Get + /// \param[in] systemIdentifier A remote system running CloudServer that we are already connected to. + /// \param[in] specificSystems It is possible to get or subscribe to updates only for specific uploading CloudClient instances. Pass the desired instances here. The overload that does not have the specificSystems parameter is treated as subscribing to all updates from all clients. + virtual bool Get(CloudQuery *cloudQuery, RakNetGUID systemIdentifier); + virtual bool Get(CloudQuery *cloudQuery, DataStructures::List &specificSystems, RakNetGUID systemIdentifier); + virtual bool Get(CloudQuery *cloudQuery, DataStructures::List &specificSystems, RakNetGUID systemIdentifier); + + /// \brief Unsubscribe from updates previously subscribed to using Get() with the CloudQuery::subscribeToResults set to true + /// The \a keys and \a specificSystems parameters are logically treated as AND when checking subscriptions on the server + /// The overload that does not take specificSystems unsubscribes to all passed keys, regardless of system + /// You cannot unsubscribe specific systems when previously subscribed to updates from any system. To do this, first Unsubscribe() from all systems, and call Get() with the \a specificSystems parameter explicilty listing the systems you want to subscribe to. + virtual void Unsubscribe(DataStructures::List &keys, RakNetGUID systemIdentifier); + virtual void Unsubscribe(DataStructures::List &keys, DataStructures::List &specificSystems, RakNetGUID systemIdentifier); + virtual void Unsubscribe(DataStructures::List &keys, DataStructures::List &specificSystems, RakNetGUID systemIdentifier); + + /// \brief Call this when you get ID_CLOUD_GET_RESPONSE + /// If \a callback or \a allocator are 0, the default callbacks passed to SetDefaultCallbacks() are used + /// \param[in] packet Packet structure returned from RakPeerInterface + /// \param[in] _callback Callback to be called from the function containing output parameters. If 0, default is used. + /// \param[in] _allocator Allocator to be used to allocate data. If 0, default is used. + virtual void OnGetReponse(Packet *packet, CloudClientCallback *_callback=0, CloudAllocator *_allocator=0); + + /// \brief Call this when you get ID_CLOUD_GET_RESPONSE + /// Different form of OnGetReponse that returns to a structure that you pass, instead of using a callback + /// You are responsible for deallocation with this form + /// If \a allocator is 0, the default callback passed to SetDefaultCallbacks() are used + /// \param[out] cloudQueryResult A pointer to a structure that will be filled out with data + /// \param[in] packet Packet structure returned from RakPeerInterface + /// \param[in] _allocator Allocator to be used to allocate data. If 0, default is used. + virtual void OnGetReponse(CloudQueryResult *cloudQueryResult, Packet *packet, CloudAllocator *_allocator=0); + + /// \brief Call this when you get ID_CLOUD_SUBSCRIPTION_NOTIFICATION + /// If \a callback or \a allocator are 0, the default callbacks passed to SetDefaultCallbacks() are used + /// \param[in] packet Packet structure returned from RakPeerInterface + /// \param[in] _callback Callback to be called from the function containing output parameters. If 0, default is used. + /// \param[in] _allocator Allocator to be used to allocate data. If 0, default is used. + virtual void OnSubscriptionNotification(Packet *packet, CloudClientCallback *_callback=0, CloudAllocator *_allocator=0); + + /// \brief Call this when you get ID_CLOUD_SUBSCRIPTION_NOTIFICATION + /// Different form of OnSubscriptionNotification that returns to a structure that you pass, instead of using a callback + /// You are responsible for deallocation with this form + /// If \a allocator is 0, the default callback passed to SetDefaultCallbacks() are used + /// \param[out] wasUpdated If true, the row was updated. If false, it was deleted. \a result will contain the last value just before deletion + /// \param[out] row A pointer to a structure that will be filled out with data + /// \param[in] packet Packet structure returned from RakPeerInterface + /// \param[in] _allocator Allocator to be used to allocate data. If 0, default is used. + virtual void OnSubscriptionNotification(bool *wasUpdated, CloudQueryRow *row, Packet *packet, CloudAllocator *_allocator=0); + + /// If you never specified an allocator, and used the non-callback form of OnGetReponse(), deallocate cloudQueryResult with this function + virtual void DeallocateWithDefaultAllocator(CloudQueryResult *cloudQueryResult); + + /// If you never specified an allocator, and used the non-callback form of OnSubscriptionNotification(), deallocate row with this function + virtual void DeallocateWithDefaultAllocator(CloudQueryRow *row); + +protected: + PluginReceiveResult OnReceive(Packet *packet); + + CloudClientCallback *callback; + CloudAllocator *allocator; + + CloudAllocator unsetDefaultAllocator; +}; + +/// \ingroup CLOUD_GROUP +/// Parses ID_CLOUD_GET_RESPONSE and ID_CLOUD_SUBSCRIPTION_NOTIFICATION in a convenient callback form +class RAK_DLL_EXPORT CloudClientCallback +{ +public: + CloudClientCallback() {} + virtual ~CloudClientCallback() {} + + /// \brief Called in response to ID_CLOUD_GET_RESPONSE + /// \param[out] result Contains the original query passed to Get(), and a list of rows returned. + /// \param[out] deallocateRowsAfterReturn CloudQueryResult::rowsReturned will be deallocated after the function returns by default. Set to false to not deallocate these pointers. The pointers are allocated through CloudAllocator. + virtual void OnGet(RakNet::CloudQueryResult *result, bool *deallocateRowsAfterReturn) {(void) result; (void) deallocateRowsAfterReturn;} + + /// \brief Called in response to ID_CLOUD_SUBSCRIPTION_NOTIFICATION + /// \param[out] result Contains the row updated + /// \param[out] wasUpdated If true, the row was updated. If false, it was deleted. \a result will contain the last value just before deletion + /// \param[out] deallocateRowAfterReturn \a result will be deallocated after the function returns by default. Set to false to not deallocate these pointers. The pointers are allocated through CloudAllocator. + virtual void OnSubscriptionNotification(RakNet::CloudQueryRow *result, bool wasUpdated, bool *deallocateRowAfterReturn) {(void) result; (void) wasUpdated; (void) deallocateRowAfterReturn;} +}; + +} // namespace RakNet + +#endif + diff --git a/Source/CloudCommon.h b/Source/CloudCommon.h index e0bf122f8..e5a77f545 100644 --- a/Source/CloudCommon.h +++ b/Source/CloudCommon.h @@ -1,150 +1,148 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_CloudClient==1 || _RAKNET_SUPPORT_CloudServer==1 - -#ifndef __CLOUD_COMMON_H -#define __CLOUD_COMMON_H - -#include "RakNetTypes.h" -#include "RakString.h" - -namespace RakNet -{ - -class BitStream; -struct CloudQueryRow; - -/// Allocates CloudQueryRow and the row data. Override to use derived classes or different allocators -/// \ingroup CLOUD_GROUP -class RAK_DLL_EXPORT CloudAllocator -{ -public: - CloudAllocator() {} - virtual ~CloudAllocator() {} - - /// \brief Allocate a row - virtual CloudQueryRow* AllocateCloudQueryRow(void); - /// \brief Free a row - virtual void DeallocateCloudQueryRow(CloudQueryRow *row); - /// \brief Allocate CloudQueryRow::data - virtual unsigned char *AllocateRowData(uint32_t bytesNeededForData); - /// \brief Free CloudQueryRow::data - virtual void DeallocateRowData(void *data); -}; - -/// Serves as a key to identify data uploaded to or queried from the server. -/// \ingroup CLOUD_GROUP -struct RAK_DLL_EXPORT CloudKey -{ - CloudKey() {} - CloudKey(RakNet::RakString _primaryKey, uint32_t _secondaryKey) : primaryKey(_primaryKey), secondaryKey(_secondaryKey) {} - ~CloudKey() {} - - /// Identifies the primary key. This is intended to be a major category, such as the name of the application - /// Must be non-empty - RakNet::RakString primaryKey; - - /// Identifies the secondary key. This is intended to be a subcategory enumeration, such as PLAYER_LIST or RUNNING_SCORES - uint32_t secondaryKey; - - /// \internal - void Serialize(bool writeToBitstream, BitStream *bitStream); -}; - -/// \internal -int CloudKeyComp(const CloudKey &key, const CloudKey &data); - -/// Data members used to query the cloud -/// \ingroup CLOUD_GROUP -struct RAK_DLL_EXPORT CloudQuery -{ - CloudQuery() {startingRowIndex=0; maxRowsToReturn=0; subscribeToResults=false;} - - /// List of keys to query. Must be at least of length 1. - /// This query is run on uploads from all clients, and those that match the combination of primaryKey and secondaryKey are potentially returned - /// If you pass more than one key at a time, the results are concatenated so if you need to differentiate between queries then send two different queries - DataStructures::List keys; - - /// If limiting the number of rows to return, this is the starting offset into the list. Has no effect unless maxRowsToReturn is > 0 - uint32_t startingRowIndex; - - /// Maximum number of rows to return. Actual number may still be less than this. Pass 0 to mean no-limit. - uint32_t maxRowsToReturn; - - /// If true, automatically get updates as the results returned to you change. Unsubscribe with CloudMemoryClient::Unsubscribe() - bool subscribeToResults; - - /// \internal - void Serialize(bool writeToBitstream, BitStream *bitStream); -}; - -/// \ingroup CLOUD_GROUP -struct RAK_DLL_EXPORT CloudQueryRow -{ - /// Key used to identify this data - CloudKey key; - - /// Data uploaded - unsigned char *data; - - /// Length of data uploaded - uint32_t length; - - /// System address of server that is holding this data, and the client is connected to - SystemAddress serverSystemAddress; - - /// System address of client that uploaded this data - SystemAddress clientSystemAddress; - - /// RakNetGUID of server that is holding this data, and the client is connected to - RakNetGUID serverGUID; - - /// RakNetGUID of client that uploaded this data - RakNetGUID clientGUID; - - /// \internal - void Serialize(bool writeToBitstream, BitStream *bitStream, CloudAllocator *allocator); -}; - -/// \ingroup CLOUD_GROUP -struct RAK_DLL_EXPORT CloudQueryResult -{ - /// Query originally passed to Download() - CloudQuery cloudQuery; - - /// Results returned from query. If there were multiple keys in CloudQuery::keys then see resultKeyIndices - DataStructures::List rowsReturned; - - /// If there were multiple keys in CloudQuery::keys, then each key is processed in order and the result concatenated to rowsReturned - /// The starting index of each query is written to resultKeyIndices - /// For example, if CloudQuery::keys had 4 keys, returning 3 rows, 0, rows, 5 rows, and 12 rows then - /// resultKeyIndices would be 0, 3, 3, 8 - DataStructures::List resultKeyIndices; - - /// Whatever was passed to CloudClient::Get() as CloudQuery::subscribeToResults - bool subscribeToResults; - - /// \internal - void Serialize(bool writeToBitstream, BitStream *bitStream, CloudAllocator *allocator); - /// \internal - void SerializeHeader(bool writeToBitstream, BitStream *bitStream); - /// \internal - void SerializeNumRows(bool writeToBitstream, uint32_t &numRows, BitStream *bitStream); - /// \internal - void SerializeCloudQueryRows(bool writeToBitstream, uint32_t &numRows, BitStream *bitStream, CloudAllocator *allocator); -}; - -} // Namespace RakNet - -#endif // __CLOUD_COMMON_H - -#endif // #if _RAKNET_SUPPORT_CloudClient==1 || _RAKNET_SUPPORT_CloudServer==1 +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_CloudClient==1 || _RAKNET_SUPPORT_CloudServer==1 + +#pragma once + +#include "RakNetTypes.h" +#include "RakString.h" + +namespace RakNet +{ + +class BitStream; +struct CloudQueryRow; + +/// Allocates CloudQueryRow and the row data. Override to use derived classes or different allocators +/// \ingroup CLOUD_GROUP +class RAK_DLL_EXPORT CloudAllocator +{ +public: + CloudAllocator() {} + virtual ~CloudAllocator() {} + + /// \brief Allocate a row + virtual CloudQueryRow* AllocateCloudQueryRow(void); + /// \brief Free a row + virtual void DeallocateCloudQueryRow(CloudQueryRow *row); + /// \brief Allocate CloudQueryRow::data + virtual unsigned char *AllocateRowData(uint32_t bytesNeededForData); + /// \brief Free CloudQueryRow::data + virtual void DeallocateRowData(void *data); +}; + +/// Serves as a key to identify data uploaded to or queried from the server. +/// \ingroup CLOUD_GROUP +struct RAK_DLL_EXPORT CloudKey +{ + CloudKey() {} + CloudKey(RakNet::RakString _primaryKey, uint32_t _secondaryKey) : primaryKey(_primaryKey), secondaryKey(_secondaryKey) {} + ~CloudKey() {} + + /// Identifies the primary key. This is intended to be a major category, such as the name of the application + /// Must be non-empty + RakNet::RakString primaryKey; + + /// Identifies the secondary key. This is intended to be a subcategory enumeration, such as PLAYER_LIST or RUNNING_SCORES + uint32_t secondaryKey; + + /// \internal + void Serialize(bool writeToBitstream, BitStream *bitStream); +}; + +/// \internal +int CloudKeyComp(const CloudKey &key, const CloudKey &data); + +/// Data members used to query the cloud +/// \ingroup CLOUD_GROUP +struct RAK_DLL_EXPORT CloudQuery +{ + CloudQuery() {startingRowIndex=0; maxRowsToReturn=0; subscribeToResults=false;} + + /// List of keys to query. Must be at least of length 1. + /// This query is run on uploads from all clients, and those that match the combination of primaryKey and secondaryKey are potentially returned + /// If you pass more than one key at a time, the results are concatenated so if you need to differentiate between queries then send two different queries + DataStructures::List keys; + + /// If limiting the number of rows to return, this is the starting offset into the list. Has no effect unless maxRowsToReturn is > 0 + uint32_t startingRowIndex; + + /// Maximum number of rows to return. Actual number may still be less than this. Pass 0 to mean no-limit. + uint32_t maxRowsToReturn; + + /// If true, automatically get updates as the results returned to you change. Unsubscribe with CloudMemoryClient::Unsubscribe() + bool subscribeToResults; + + /// \internal + void Serialize(bool writeToBitstream, BitStream *bitStream); +}; + +/// \ingroup CLOUD_GROUP +struct RAK_DLL_EXPORT CloudQueryRow +{ + /// Key used to identify this data + CloudKey key; + + /// Data uploaded + unsigned char *data; + + /// Length of data uploaded + uint32_t length; + + /// System address of server that is holding this data, and the client is connected to + SystemAddress serverSystemAddress; + + /// System address of client that uploaded this data + SystemAddress clientSystemAddress; + + /// RakNetGUID of server that is holding this data, and the client is connected to + RakNetGUID serverGUID; + + /// RakNetGUID of client that uploaded this data + RakNetGUID clientGUID; + + /// \internal + void Serialize(bool writeToBitstream, BitStream *bitStream, CloudAllocator *allocator); +}; + +/// \ingroup CLOUD_GROUP +struct RAK_DLL_EXPORT CloudQueryResult +{ + /// Query originally passed to Download() + CloudQuery cloudQuery; + + /// Results returned from query. If there were multiple keys in CloudQuery::keys then see resultKeyIndices + DataStructures::List rowsReturned; + + /// If there were multiple keys in CloudQuery::keys, then each key is processed in order and the result concatenated to rowsReturned + /// The starting index of each query is written to resultKeyIndices + /// For example, if CloudQuery::keys had 4 keys, returning 3 rows, 0, rows, 5 rows, and 12 rows then + /// resultKeyIndices would be 0, 3, 3, 8 + DataStructures::List resultKeyIndices; + + /// Whatever was passed to CloudClient::Get() as CloudQuery::subscribeToResults + bool subscribeToResults; + + /// \internal + void Serialize(bool writeToBitstream, BitStream *bitStream, CloudAllocator *allocator); + /// \internal + void SerializeHeader(bool writeToBitstream, BitStream *bitStream); + /// \internal + void SerializeNumRows(bool writeToBitstream, uint32_t &numRows, BitStream *bitStream); + /// \internal + void SerializeCloudQueryRows(bool writeToBitstream, uint32_t &numRows, BitStream *bitStream, CloudAllocator *allocator); +}; + +} // Namespace RakNet + +#endif // __CLOUD_COMMON_H + diff --git a/Source/CloudServer.h b/Source/CloudServer.h index fbed68797..96a15de34 100644 --- a/Source/CloudServer.h +++ b/Source/CloudServer.h @@ -1,383 +1,381 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file CloudServer.h -/// \brief Stores client data, and allows cross-server communication to retrieve this data -/// \details TODO -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_CloudServer==1 - -#ifndef __CLOUD_SERVER_H -#define __CLOUD_SERVER_H - -#include "PluginInterface2.h" -#include "RakMemoryOverride.h" -#include "NativeTypes.h" -#include "RakString.h" -#include "DS_Hash.h" -#include "CloudCommon.h" -#include "DS_OrderedList.h" - -/// If the data is smaller than this value, an allocation is avoid. However, this value exists for every row -#define CLOUD_SERVER_DATA_STACK_SIZE 32 - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; - -/// \brief Zero or more instances of CloudServerQueryFilter can be attached to CloudServer to restrict client queries -/// All attached instances of CloudServerQueryFilter on each corresponding operation, from all directly connected clients -/// If any attached instance returns false for a given operation, that operation is silently rejected -/// \ingroup CLOUD_GROUP -class RAK_DLL_EXPORT CloudServerQueryFilter -{ -public: - CloudServerQueryFilter() {} - virtual ~CloudServerQueryFilter() {} - - /// Called when a local client wants to post data - /// \return true to allow, false to reject - virtual bool OnPostRequest(RakNetGUID clientGuid, SystemAddress clientAddress, CloudKey key, uint32_t dataLength, const char *data)=0; - - /// Called when a local client wants to release data that it has previously uploaded - /// \return true to allow, false to reject - virtual bool OnReleaseRequest(RakNetGUID clientGuid, SystemAddress clientAddress, DataStructures::List &cloudKeys)=0; - - /// Called when a local client wants to query data - /// If you return false, the client will get no response at all - /// \return true to allow, false to reject - virtual bool OnGetRequest(RakNetGUID clientGuid, SystemAddress clientAddress, CloudQuery &query, DataStructures::List &specificSystems)=0; - - /// Called when a local client wants to stop getting updates for data - /// If you return false, the client will keep getting updates for that data - /// \return true to allow, false to reject - virtual bool OnUnsubscribeRequest(RakNetGUID clientGuid, SystemAddress clientAddress, DataStructures::List &cloudKeys, DataStructures::List &specificSystems)=0; -}; - -/// \brief Stores client data, and allows cross-server communication to retrieve this data -/// \ingroup CLOUD_GROUP -class RAK_DLL_EXPORT CloudServer : public PluginInterface2, CloudAllocator -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(CloudServer) - - CloudServer(); - virtual ~CloudServer(); - - /// \brief Max bytes a client can upload - /// Data in excess of this value is silently ignored - /// defaults to 0 (unlimited) - /// \param[in] bytes Max bytes a client can upload. 0 means unlimited. - void SetMaxUploadBytesPerClient(uint64_t bytes); - - /// \brief Max bytes returned by a download. If the number of bytes would exceed this amount, the returned list is truncated - /// However, if this would result in no rows downloaded, then one row will be returned. - /// \param[in] bytes Max bytes a client can download from a single Get(). 0 means unlimited. - void SetMaxBytesPerDownload(uint64_t bytes); - - /// \brief Add a server, which is assumed to be connected in a fully connected mesh to all other servers and also running the CloudServer plugin - /// The other system must also call AddServer before getting the subscription data, or it will be rejected. - /// Sending a message telling the other system to call AddServer(), followed by calling AddServer() locally, would be sufficient for this to work. - /// \note This sends subscription data to the other system, using RELIABLE_ORDERED on channel 0 - /// \param[in] systemIdentifier Identifier of the remote system - void AddServer(RakNetGUID systemIdentifier); - - /// \brief Removes a server added through AddServer() - /// \param[in] systemIdentifier Identifier of the remote system - void RemoveServer(RakNetGUID systemIdentifier); - - /// Return list of servers added with AddServer() - /// \param[out] remoteServers List of servers added - void GetRemoteServers(DataStructures::List &remoteServersOut); - - /// \brief Frees all memory. Does not remove query filters - void Clear(void); - - /// \brief Report the specified SystemAddress to client queries, rather than what RakPeer reads. - /// This is useful if you already know your public IP - /// This only applies to future updates, so call it before updating to apply to all queries - /// \param[in] forcedAddress The systmeAddress to return in queries. Use UNASSIGNED_SYSTEM_ADDRESS (default) to use what RakPeer returns - void ForceExternalSystemAddress(SystemAddress forcedAddress); - - /// \brief Adds a callback called on each query. If all filters returns true for an operation, the operation is allowed. - /// If the filter was already added, the function silently fails - /// \param[in] filter An externally allocated instance of CloudServerQueryFilter. The instance must remain valid until it is removed with RemoveQueryFilter() or RemoveAllQueryFilters() - void AddQueryFilter(CloudServerQueryFilter* filter); - - /// \brief Removes a callback added with AddQueryFilter() - /// The instance is not deleted, only unreferenced. It is up to the user to delete the instance, if necessary - /// \param[in] filter An externally allocated instance of CloudServerQueryFilter. The instance must remain valid until it is removed with RemoveQueryFilter() or RemoveAllQueryFilters() - void RemoveQueryFilter(CloudServerQueryFilter* filter); - - /// \brief Removes all instances of CloudServerQueryFilter added with AddQueryFilter(). - /// The instances are not deleted, only unreferenced. It is up to the user to delete the instances, if necessary - void RemoveAllQueryFilters(void); - -protected: - virtual void Update(void); - virtual PluginReceiveResult OnReceive(Packet *packet); - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - virtual void OnRakPeerShutdown(void); - - - virtual void OnPostRequest(Packet *packet); - virtual void OnReleaseRequest(Packet *packet); - virtual void OnGetRequest(Packet *packet); - virtual void OnUnsubscribeRequest(Packet *packet); - virtual void OnServerToServerGetRequest(Packet *packet); - virtual void OnServerToServerGetResponse(Packet *packet); - - uint64_t maxUploadBytesPerClient, maxBytesPerDowload; - - // ---------------------------------------------------------------------------- - // For a given data key, quickly look up one or all systems that have uploaded - // ---------------------------------------------------------------------------- - struct CloudData - { - CloudData() {} - ~CloudData() {if (allocatedData) rakFree_Ex(allocatedData, _FILE_AND_LINE_);} - bool IsUnused(void) const {return isUploaded==false && specificSubscribers.Size()==0;} - void Clear(void) {if (dataPtr==allocatedData) rakFree_Ex(allocatedData, _FILE_AND_LINE_); allocatedData=0; dataPtr=0; dataLengthBytes=0; isUploaded=false;} - - unsigned char stackData[CLOUD_SERVER_DATA_STACK_SIZE]; - unsigned char *allocatedData; // Uses allocatedData instead of stackData if length of data exceeds CLOUD_SERVER_DATA_STACK_SIZE - unsigned char *dataPtr; // Points to either stackData or allocatedData - uint32_t dataLengthBytes; - bool isUploaded; - - /// System address of server that is holding this data, and the client is connected to - SystemAddress serverSystemAddress; - - /// System address of client that uploaded this data - SystemAddress clientSystemAddress; - - /// RakNetGUID of server that is holding this data, and the client is connected to - RakNetGUID serverGUID; - - /// RakNetGUID of client that uploaded this data - RakNetGUID clientGUID; - - /// When the key data changes from this particular system, notify these subscribers - /// This list mutually exclusive with CloudDataList::nonSpecificSubscribers - DataStructures::OrderedList specificSubscribers; - }; - void WriteCloudQueryRowFromResultList(unsigned int i, DataStructures::List &cloudDataResultList, DataStructures::List &cloudKeyResultList, BitStream *bsOut); - void WriteCloudQueryRowFromResultList(DataStructures::List &cloudDataResultList, DataStructures::List &cloudKeyResultList, BitStream *bsOut); - - static int KeyDataPtrComp( const RakNetGUID &key, CloudData* const &data ); - struct CloudDataList - { - bool IsUnused(void) const {return keyData.Size()==0 && nonSpecificSubscribers.Size()==0;} - bool IsNotUploaded(void) const {return uploaderCount==0;} - bool RemoveSubscriber(RakNetGUID g) { - bool objectExists; - unsigned int index; - index = nonSpecificSubscribers.GetIndexFromKey(g, &objectExists); - if (objectExists) - { - subscriberCount--; - nonSpecificSubscribers.RemoveAtIndex(index); - return true; - } - return false; - } - - unsigned int uploaderCount, subscriberCount; - CloudKey key; - - // Data uploaded from or subscribed to for various systems - DataStructures::OrderedList keyData; - - /// When the key data changes from any system, notify these subscribers - /// This list mutually exclusive with CloudData::specificSubscribers - DataStructures::OrderedList nonSpecificSubscribers; - }; - - static int KeyDataListComp( const CloudKey &key, CloudDataList * const &data ); - DataStructures::OrderedList dataRepository; - - struct KeySubscriberID - { - CloudKey key; - DataStructures::OrderedList specificSystemsSubscribedTo; - }; - static int KeySubscriberIDComp(const CloudKey &key, KeySubscriberID * const &data ); - - // Remote systems - struct RemoteCloudClient - { - bool IsUnused(void) const {return uploadedKeys.Size()==0 && subscribedKeys.Size()==0;} - - DataStructures::OrderedList uploadedKeys; - DataStructures::OrderedList subscribedKeys; - uint64_t uploadedBytes; - }; - DataStructures::Hash remoteSystems; - - // For a given user, release all subscribed and uploaded keys - void ReleaseSystem(RakNetGUID clientAddress ); - - // For a given user, release a set of keys - void ReleaseKeys(RakNetGUID clientAddress, DataStructures::List &keys ); - - void NotifyClientSubscribersOfDataChange( CloudData *cloudData, CloudKey &key, DataStructures::OrderedList &subscribers, bool wasUpdated ); - void NotifyClientSubscribersOfDataChange( CloudQueryRow *row, DataStructures::OrderedList &subscribers, bool wasUpdated ); - void NotifyServerSubscribersOfDataChange( CloudData *cloudData, CloudKey &key, bool wasUpdated ); - - struct RemoteServer - { - RakNetGUID serverAddress; - // This server needs to know about these keys when they are updated or deleted - DataStructures::OrderedList subscribedKeys; - // This server has uploaded these keys, and needs to know about Get() requests - DataStructures::OrderedList uploadedKeys; - - // Just for processing - bool workingFlag; - - // If false, we don't know what keys they have yet, so send everything - bool gotSubscribedAndUploadedKeys; - }; - - static int RemoteServerComp(const RakNetGUID &key, RemoteServer* const &data ); - DataStructures::OrderedList remoteServers; - - struct BufferedGetResponseFromServer - { - void Clear(CloudAllocator *allocator); - - RakNetGUID serverAddress; - CloudQueryResult queryResult; - bool gotResult; - }; - - struct CloudQueryWithAddresses - { - // Inputs - CloudQuery cloudQuery; - DataStructures::List specificSystems; - - void Serialize(bool writeToBitstream, BitStream *bitStream); - }; - - static int BufferedGetResponseFromServerComp(const RakNetGUID &key, BufferedGetResponseFromServer* const &data ); - struct GetRequest - { - void Clear(CloudAllocator *allocator); - bool AllRemoteServersHaveResponded(void) const; - CloudQueryWithAddresses cloudQueryWithAddresses; - - // When request started. If takes too long for a response from another system, can abort remaining systems - RakNet::Time requestStartTime; - - // Assigned by server that gets the request to identify response. See nextGetRequestId - uint32_t requestId; - - RakNetGUID requestingClient; - - DataStructures::OrderedList remoteServerResponses; - }; - static int GetRequestComp(const uint32_t &key, GetRequest* const &data ); - DataStructures::OrderedList getRequests; - RakNet::Time nextGetRequestsCheck; - - uint32_t nextGetRequestId; - - void ProcessAndTransmitGetRequest(GetRequest *getRequest); - - void ProcessCloudQueryWithAddresses( - CloudServer::CloudQueryWithAddresses &cloudQueryWithAddresses, - DataStructures::List &cloudDataResultList, - DataStructures::List &cloudKeyResultList - ); - - void SendUploadedAndSubscribedKeysToServer( RakNetGUID systemAddress ); - void SendUploadedKeyToServers( CloudKey &cloudKey ); - void SendSubscribedKeyToServers( CloudKey &cloudKey ); - void RemoveUploadedKeyFromServers( CloudKey &cloudKey ); - void RemoveSubscribedKeyFromServers( CloudKey &cloudKey ); - - void OnSendUploadedAndSubscribedKeysToServer( Packet *packet ); - void OnSendUploadedKeyToServers( Packet *packet ); - void OnSendSubscribedKeyToServers( Packet *packet ); - void OnRemoveUploadedKeyFromServers( Packet *packet ); - void OnRemoveSubscribedKeyFromServers( Packet *packet ); - void OnServerDataChanged( Packet *packet ); - - void GetServersWithUploadedKeys( - DataStructures::List &keys, - DataStructures::List &remoteServersWithData - ); - - CloudServer::CloudDataList *GetOrAllocateCloudDataList(CloudKey key, bool *dataRepositoryExists, unsigned int &dataRepositoryIndex); - - void UnsubscribeFromKey(RemoteCloudClient *remoteCloudClient, RakNetGUID remoteCloudClientGuid, unsigned int keySubscriberIndex, CloudKey &cloudKey, DataStructures::List &specificSystems); - void RemoveSpecificSubscriber(RakNetGUID specificSubscriber, CloudDataList *cloudDataList, RakNetGUID remoteCloudClientGuid); - - DataStructures::List queryFilters; - - SystemAddress forceAddress; -}; - - -} // namespace RakNet - -#endif - - -// Key subscription -// -// A given system can subscribe to one or more keys. -// The subscription can be further be defined as only subscribing to keys uploaded by or changed by a given system. -// It is possible to subscribe to keys not yet uploaded, or uploaded to another system -// -// Operations: -// -// 1. SubscribeToKey() - Get() operation with subscription -// A. Add to key subscription list for the client, which contains a keyId / specificUploaderList pair -// B. Send to remote servers that for this key, they should send us updates -// C. (Done, get operation returns current values) -// -// 2. UpdateData() - Post() operation -// A. Find all subscribers to this data, for the uploading system. -// B. Send them the uploaded data -// C. Find all servers that subscribe to this data -// D. Send them the uploaded data -// -// 3. DeleteData() - Release() operation -// A. Find all subscribers to this data, for the deleting system. -// B. Inform them of the deletion -// C. Find all servers that subscribe to this data -// D. Inform them of the deletion -// -// 4. Unsubscribe() -// A. Find this subscriber, and remove their subscription -// B. If no one else is subscribing to this key for any system, notify remote servers we no longer need subscription updates -// -// Internal operations: -// -// 1. Find if any connected client has subscribed to a given key -// A. This is used add and remove our subscription for this key to remote servers -// -// 2. For a given key and updating address, find all connected clients that care -// A. First find connected clients that have subscribed to this key, regardless of address -// B. Then find connected clients that have subscribed to this key for this particular address -// -// 3. Find all remote servers that have subscribed to a given key -// A. This is so when the key is updated or deleted, we know who to send it to -// -// 4. For a given client (such as on disconnect), remove all records of their subscriptions - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file CloudServer.h +/// \brief Stores client data, and allows cross-server communication to retrieve this data +/// \details TODO +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_CloudServer==1 + +#pragma once + +#include "PluginInterface2.h" +#include "RakMemoryOverride.h" +#include "NativeTypes.h" +#include "RakString.h" +#include "DS_Hash.h" +#include "CloudCommon.h" +#include "DS_OrderedList.h" + +/// If the data is smaller than this value, an allocation is avoid. However, this value exists for every row +#define CLOUD_SERVER_DATA_STACK_SIZE 32 + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; + +/// \brief Zero or more instances of CloudServerQueryFilter can be attached to CloudServer to restrict client queries +/// All attached instances of CloudServerQueryFilter on each corresponding operation, from all directly connected clients +/// If any attached instance returns false for a given operation, that operation is silently rejected +/// \ingroup CLOUD_GROUP +class RAK_DLL_EXPORT CloudServerQueryFilter +{ +public: + CloudServerQueryFilter() {} + virtual ~CloudServerQueryFilter() {} + + /// Called when a local client wants to post data + /// \return true to allow, false to reject + virtual bool OnPostRequest(RakNetGUID clientGuid, SystemAddress clientAddress, CloudKey key, uint32_t dataLength, const char *data)=0; + + /// Called when a local client wants to release data that it has previously uploaded + /// \return true to allow, false to reject + virtual bool OnReleaseRequest(RakNetGUID clientGuid, SystemAddress clientAddress, DataStructures::List &cloudKeys)=0; + + /// Called when a local client wants to query data + /// If you return false, the client will get no response at all + /// \return true to allow, false to reject + virtual bool OnGetRequest(RakNetGUID clientGuid, SystemAddress clientAddress, CloudQuery &query, DataStructures::List &specificSystems)=0; + + /// Called when a local client wants to stop getting updates for data + /// If you return false, the client will keep getting updates for that data + /// \return true to allow, false to reject + virtual bool OnUnsubscribeRequest(RakNetGUID clientGuid, SystemAddress clientAddress, DataStructures::List &cloudKeys, DataStructures::List &specificSystems)=0; +}; + +/// \brief Stores client data, and allows cross-server communication to retrieve this data +/// \ingroup CLOUD_GROUP +class RAK_DLL_EXPORT CloudServer : public PluginInterface2, CloudAllocator +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(CloudServer) + + CloudServer(); + virtual ~CloudServer(); + + /// \brief Max bytes a client can upload + /// Data in excess of this value is silently ignored + /// defaults to 0 (unlimited) + /// \param[in] bytes Max bytes a client can upload. 0 means unlimited. + void SetMaxUploadBytesPerClient(uint64_t bytes); + + /// \brief Max bytes returned by a download. If the number of bytes would exceed this amount, the returned list is truncated + /// However, if this would result in no rows downloaded, then one row will be returned. + /// \param[in] bytes Max bytes a client can download from a single Get(). 0 means unlimited. + void SetMaxBytesPerDownload(uint64_t bytes); + + /// \brief Add a server, which is assumed to be connected in a fully connected mesh to all other servers and also running the CloudServer plugin + /// The other system must also call AddServer before getting the subscription data, or it will be rejected. + /// Sending a message telling the other system to call AddServer(), followed by calling AddServer() locally, would be sufficient for this to work. + /// \note This sends subscription data to the other system, using RELIABLE_ORDERED on channel 0 + /// \param[in] systemIdentifier Identifier of the remote system + void AddServer(RakNetGUID systemIdentifier); + + /// \brief Removes a server added through AddServer() + /// \param[in] systemIdentifier Identifier of the remote system + void RemoveServer(RakNetGUID systemIdentifier); + + /// Return list of servers added with AddServer() + /// \param[out] remoteServers List of servers added + void GetRemoteServers(DataStructures::List &remoteServersOut); + + /// \brief Frees all memory. Does not remove query filters + void Clear(void); + + /// \brief Report the specified SystemAddress to client queries, rather than what RakPeer reads. + /// This is useful if you already know your public IP + /// This only applies to future updates, so call it before updating to apply to all queries + /// \param[in] forcedAddress The systmeAddress to return in queries. Use UNASSIGNED_SYSTEM_ADDRESS (default) to use what RakPeer returns + void ForceExternalSystemAddress(SystemAddress forcedAddress); + + /// \brief Adds a callback called on each query. If all filters returns true for an operation, the operation is allowed. + /// If the filter was already added, the function silently fails + /// \param[in] filter An externally allocated instance of CloudServerQueryFilter. The instance must remain valid until it is removed with RemoveQueryFilter() or RemoveAllQueryFilters() + void AddQueryFilter(CloudServerQueryFilter* filter); + + /// \brief Removes a callback added with AddQueryFilter() + /// The instance is not deleted, only unreferenced. It is up to the user to delete the instance, if necessary + /// \param[in] filter An externally allocated instance of CloudServerQueryFilter. The instance must remain valid until it is removed with RemoveQueryFilter() or RemoveAllQueryFilters() + void RemoveQueryFilter(CloudServerQueryFilter* filter); + + /// \brief Removes all instances of CloudServerQueryFilter added with AddQueryFilter(). + /// The instances are not deleted, only unreferenced. It is up to the user to delete the instances, if necessary + void RemoveAllQueryFilters(void); + +protected: + virtual void Update(void); + virtual PluginReceiveResult OnReceive(Packet *packet); + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + virtual void OnRakPeerShutdown(void); + + + virtual void OnPostRequest(Packet *packet); + virtual void OnReleaseRequest(Packet *packet); + virtual void OnGetRequest(Packet *packet); + virtual void OnUnsubscribeRequest(Packet *packet); + virtual void OnServerToServerGetRequest(Packet *packet); + virtual void OnServerToServerGetResponse(Packet *packet); + + uint64_t maxUploadBytesPerClient, maxBytesPerDowload; + + // ---------------------------------------------------------------------------- + // For a given data key, quickly look up one or all systems that have uploaded + // ---------------------------------------------------------------------------- + struct CloudData + { + CloudData() {} + ~CloudData() {if (allocatedData) rakFree_Ex(allocatedData, _FILE_AND_LINE_);} + bool IsUnused(void) const {return isUploaded==false && specificSubscribers.Size()==0;} + void Clear(void) {if (dataPtr==allocatedData) rakFree_Ex(allocatedData, _FILE_AND_LINE_); allocatedData=0; dataPtr=0; dataLengthBytes=0; isUploaded=false;} + + unsigned char stackData[CLOUD_SERVER_DATA_STACK_SIZE]; + unsigned char *allocatedData; // Uses allocatedData instead of stackData if length of data exceeds CLOUD_SERVER_DATA_STACK_SIZE + unsigned char *dataPtr; // Points to either stackData or allocatedData + uint32_t dataLengthBytes; + bool isUploaded; + + /// System address of server that is holding this data, and the client is connected to + SystemAddress serverSystemAddress; + + /// System address of client that uploaded this data + SystemAddress clientSystemAddress; + + /// RakNetGUID of server that is holding this data, and the client is connected to + RakNetGUID serverGUID; + + /// RakNetGUID of client that uploaded this data + RakNetGUID clientGUID; + + /// When the key data changes from this particular system, notify these subscribers + /// This list mutually exclusive with CloudDataList::nonSpecificSubscribers + DataStructures::OrderedList specificSubscribers; + }; + void WriteCloudQueryRowFromResultList(unsigned int i, DataStructures::List &cloudDataResultList, DataStructures::List &cloudKeyResultList, BitStream *bsOut); + void WriteCloudQueryRowFromResultList(DataStructures::List &cloudDataResultList, DataStructures::List &cloudKeyResultList, BitStream *bsOut); + + static int KeyDataPtrComp( const RakNetGUID &key, CloudData* const &data ); + struct CloudDataList + { + bool IsUnused(void) const {return keyData.Size()==0 && nonSpecificSubscribers.Size()==0;} + bool IsNotUploaded(void) const {return uploaderCount==0;} + bool RemoveSubscriber(RakNetGUID g) { + bool objectExists; + unsigned int index; + index = nonSpecificSubscribers.GetIndexFromKey(g, &objectExists); + if (objectExists) + { + subscriberCount--; + nonSpecificSubscribers.RemoveAtIndex(index); + return true; + } + return false; + } + + unsigned int uploaderCount, subscriberCount; + CloudKey key; + + // Data uploaded from or subscribed to for various systems + DataStructures::OrderedList keyData; + + /// When the key data changes from any system, notify these subscribers + /// This list mutually exclusive with CloudData::specificSubscribers + DataStructures::OrderedList nonSpecificSubscribers; + }; + + static int KeyDataListComp( const CloudKey &key, CloudDataList * const &data ); + DataStructures::OrderedList dataRepository; + + struct KeySubscriberID + { + CloudKey key; + DataStructures::OrderedList specificSystemsSubscribedTo; + }; + static int KeySubscriberIDComp(const CloudKey &key, KeySubscriberID * const &data ); + + // Remote systems + struct RemoteCloudClient + { + bool IsUnused(void) const {return uploadedKeys.Size()==0 && subscribedKeys.Size()==0;} + + DataStructures::OrderedList uploadedKeys; + DataStructures::OrderedList subscribedKeys; + uint64_t uploadedBytes; + }; + DataStructures::Hash remoteSystems; + + // For a given user, release all subscribed and uploaded keys + void ReleaseSystem(RakNetGUID clientAddress ); + + // For a given user, release a set of keys + void ReleaseKeys(RakNetGUID clientAddress, DataStructures::List &keys ); + + void NotifyClientSubscribersOfDataChange( CloudData *cloudData, CloudKey &key, DataStructures::OrderedList &subscribers, bool wasUpdated ); + void NotifyClientSubscribersOfDataChange( CloudQueryRow *row, DataStructures::OrderedList &subscribers, bool wasUpdated ); + void NotifyServerSubscribersOfDataChange( CloudData *cloudData, CloudKey &key, bool wasUpdated ); + + struct RemoteServer + { + RakNetGUID serverAddress; + // This server needs to know about these keys when they are updated or deleted + DataStructures::OrderedList subscribedKeys; + // This server has uploaded these keys, and needs to know about Get() requests + DataStructures::OrderedList uploadedKeys; + + // Just for processing + bool workingFlag; + + // If false, we don't know what keys they have yet, so send everything + bool gotSubscribedAndUploadedKeys; + }; + + static int RemoteServerComp(const RakNetGUID &key, RemoteServer* const &data ); + DataStructures::OrderedList remoteServers; + + struct BufferedGetResponseFromServer + { + void Clear(CloudAllocator *allocator); + + RakNetGUID serverAddress; + CloudQueryResult queryResult; + bool gotResult; + }; + + struct CloudQueryWithAddresses + { + // Inputs + CloudQuery cloudQuery; + DataStructures::List specificSystems; + + void Serialize(bool writeToBitstream, BitStream *bitStream); + }; + + static int BufferedGetResponseFromServerComp(const RakNetGUID &key, BufferedGetResponseFromServer* const &data ); + struct GetRequest + { + void Clear(CloudAllocator *allocator); + bool AllRemoteServersHaveResponded(void) const; + CloudQueryWithAddresses cloudQueryWithAddresses; + + // When request started. If takes too long for a response from another system, can abort remaining systems + RakNet::Time requestStartTime; + + // Assigned by server that gets the request to identify response. See nextGetRequestId + uint32_t requestId; + + RakNetGUID requestingClient; + + DataStructures::OrderedList remoteServerResponses; + }; + static int GetRequestComp(const uint32_t &key, GetRequest* const &data ); + DataStructures::OrderedList getRequests; + RakNet::Time nextGetRequestsCheck; + + uint32_t nextGetRequestId; + + void ProcessAndTransmitGetRequest(GetRequest *getRequest); + + void ProcessCloudQueryWithAddresses( + CloudServer::CloudQueryWithAddresses &cloudQueryWithAddresses, + DataStructures::List &cloudDataResultList, + DataStructures::List &cloudKeyResultList + ); + + void SendUploadedAndSubscribedKeysToServer( RakNetGUID systemAddress ); + void SendUploadedKeyToServers( CloudKey &cloudKey ); + void SendSubscribedKeyToServers( CloudKey &cloudKey ); + void RemoveUploadedKeyFromServers( CloudKey &cloudKey ); + void RemoveSubscribedKeyFromServers( CloudKey &cloudKey ); + + void OnSendUploadedAndSubscribedKeysToServer( Packet *packet ); + void OnSendUploadedKeyToServers( Packet *packet ); + void OnSendSubscribedKeyToServers( Packet *packet ); + void OnRemoveUploadedKeyFromServers( Packet *packet ); + void OnRemoveSubscribedKeyFromServers( Packet *packet ); + void OnServerDataChanged( Packet *packet ); + + void GetServersWithUploadedKeys( + DataStructures::List &keys, + DataStructures::List &remoteServersWithData + ); + + CloudServer::CloudDataList *GetOrAllocateCloudDataList(CloudKey key, bool *dataRepositoryExists, unsigned int &dataRepositoryIndex); + + void UnsubscribeFromKey(RemoteCloudClient *remoteCloudClient, RakNetGUID remoteCloudClientGuid, unsigned int keySubscriberIndex, CloudKey &cloudKey, DataStructures::List &specificSystems); + void RemoveSpecificSubscriber(RakNetGUID specificSubscriber, CloudDataList *cloudDataList, RakNetGUID remoteCloudClientGuid); + + DataStructures::List queryFilters; + + SystemAddress forceAddress; +}; + + +} // namespace RakNet + +#endif + + +// Key subscription +// +// A given system can subscribe to one or more keys. +// The subscription can be further be defined as only subscribing to keys uploaded by or changed by a given system. +// It is possible to subscribe to keys not yet uploaded, or uploaded to another system +// +// Operations: +// +// 1. SubscribeToKey() - Get() operation with subscription +// A. Add to key subscription list for the client, which contains a keyId / specificUploaderList pair +// B. Send to remote servers that for this key, they should send us updates +// C. (Done, get operation returns current values) +// +// 2. UpdateData() - Post() operation +// A. Find all subscribers to this data, for the uploading system. +// B. Send them the uploaded data +// C. Find all servers that subscribe to this data +// D. Send them the uploaded data +// +// 3. DeleteData() - Release() operation +// A. Find all subscribers to this data, for the deleting system. +// B. Inform them of the deletion +// C. Find all servers that subscribe to this data +// D. Inform them of the deletion +// +// 4. Unsubscribe() +// A. Find this subscriber, and remove their subscription +// B. If no one else is subscribing to this key for any system, notify remote servers we no longer need subscription updates +// +// Internal operations: +// +// 1. Find if any connected client has subscribed to a given key +// A. This is used add and remove our subscription for this key to remote servers +// +// 2. For a given key and updating address, find all connected clients that care +// A. First find connected clients that have subscribed to this key, regardless of address +// B. Then find connected clients that have subscribed to this key for this particular address +// +// 3. Find all remote servers that have subscribed to a given key +// A. This is so when the key is updated or deleted, we know who to send it to +// +// 4. For a given client (such as on disconnect), remove all records of their subscriptions + diff --git a/Source/CommandParserInterface.h b/Source/CommandParserInterface.h index 4645a7219..3ef3a1f2b 100644 --- a/Source/CommandParserInterface.h +++ b/Source/CommandParserInterface.h @@ -1,147 +1,144 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file CommandParserInterface.h -/// \brief Contains CommandParserInterface , from which you derive custom command parsers -/// - - -#ifndef __COMMAND_PARSER_INTERFACE -#define __COMMAND_PARSER_INTERFACE - -#include "RakMemoryOverride.h" -#include "RakNetTypes.h" -#include "DS_OrderedList.h" -#include "Export.h" - -namespace RakNet -{ -/// Forward declarations -class TransportInterface; - -/// \internal -/// Contains the information related to one command registered with RegisterCommand() -/// Implemented so I can have an automatic help system via SendCommandList() -struct RAK_DLL_EXPORT RegisteredCommand -{ - const char *command; - const char *commandHelp; - unsigned char parameterCount; -}; - -/// List of commands registered with RegisterCommand() -int RAK_DLL_EXPORT RegisteredCommandComp( const char* const & key, const RegisteredCommand &data ); - -/// \brief The interface used by command parsers. -/// \details CommandParserInterface provides a set of functions and interfaces that plug into the ConsoleServer class. -/// Each CommandParserInterface works at the same time as other interfaces in the system. -class RAK_DLL_EXPORT CommandParserInterface -{ -public: - CommandParserInterface(); - virtual ~CommandParserInterface(); - - /// You are responsible for overriding this function and returning a static string, which will identifier your parser. - /// This should return a static string - /// \return The name that you return. - virtual const char *GetName(void) const=0; - - /// \brief A callback for when \a systemAddress has connected to us. - /// \param[in] systemAddress The player that has connected. - /// \param[in] transport The transport interface that sent us this information. Can be used to send messages to this or other players. - virtual void OnNewIncomingConnection(const SystemAddress &systemAddress, TransportInterface *transport); - - /// \brief A callback for when \a systemAddress has disconnected, either gracefully or forcefully - /// \param[in] systemAddress The player that has disconnected. - /// \param[in] transport The transport interface that sent us this information. - virtual void OnConnectionLost(const SystemAddress &systemAddress, TransportInterface *transport); - - /// \brief A callback for when you are expected to send a brief description of your parser to \a systemAddress - /// \param[in] transport The transport interface we can use to write to - /// \param[in] systemAddress The player that requested help. - virtual void SendHelp(TransportInterface *transport, const SystemAddress &systemAddress)=0; - - /// \brief Given \a command with parameters \a parameterList , do whatever processing you wish. - /// \param[in] command The command to process - /// \param[in] numParameters How many parameters were passed along with the command - /// \param[in] parameterList The list of parameters. parameterList[0] is the first parameter and so on. - /// \param[in] transport The transport interface we can use to write to - /// \param[in] systemAddress The player that sent this command. - /// \param[in] originalString The string that was actually sent over the network, in case you want to do your own parsing - virtual bool OnCommand(const char *command, unsigned numParameters, char **parameterList, TransportInterface *transport, const SystemAddress &systemAddress, const char *originalString)=0; - - /// \brief This is called every time transport interface is registered. - /// \details If you want to save a copy of the TransportInterface pointer - /// This is the place to do it - /// \param[in] transport The new TransportInterface - virtual void OnTransportChange(TransportInterface *transport); - - /// \internal - /// Scan commandList and return the associated array - /// \param[in] command The string to find - /// \param[out] rc Contains the result of this operation - /// \return True if we found the command, false otherwise - virtual bool GetRegisteredCommand(const char *command, RegisteredCommand *rc); - - /// \internal - /// Goes through str, replacing the delineating character with 0's. - /// \param[in] str The string sent by the transport interface - /// \param[in] delineator The character to scan for to use as a delineator - /// \param[in] delineatorToggle When encountered the delineator replacement is toggled on and off - /// \param[out] numParameters How many pointers were written to \a parameterList - /// \param[out] parameterList An array of pointers to characters. Will hold pointers to locations inside \a str - /// \param[in] parameterListLength How big the \a parameterList array is - static void ParseConsoleString(char *str, const char delineator, unsigned char delineatorToggle, unsigned *numParameters, char **parameterList, unsigned parameterListLength); - - /// \internal - /// Goes through the variable commandList and sends the command portion of each struct - /// \param[in] transport The transport interface we can use to write to - /// \param[in] systemAddress The player to write to - virtual void SendCommandList(TransportInterface *transport, const SystemAddress &systemAddress); - - static const unsigned char VARIABLE_NUMBER_OF_PARAMETERS; - - // Currently only takes static strings - doesn't make a copy of what you pass. - // parameterCount is the number of parameters that the sender has to include with the command. - // Pass 255 to parameterCount to indicate variable number of parameters - - /// Registers a command. - /// \param[in] parameterCount How many parameters your command requires. If you want to accept a variable number of commands, pass CommandParserInterface::VARIABLE_NUMBER_OF_PARAMETERS - /// \param[in] command A pointer to a STATIC string that has your command. I keep a copy of the pointer here so don't deallocate the string. - /// \param[in] commandHelp A pointer to a STATIC string that has the help information for your command. I keep a copy of the pointer here so don't deallocate the string. - virtual void RegisterCommand(unsigned char parameterCount, const char *command, const char *commandHelp); - - /// \brief Just writes a string to the remote system based on the result ( \a res ) of your operation - /// \details This is not necessary to call, but makes it easier to return results of function calls. - /// \param[in] res The result to write - /// \param[in] command The command that this result came from - /// \param[in] transport The transport interface that will be written to - /// \param[in] systemAddress The player this result will be sent to - virtual void ReturnResult(bool res, const char *command, TransportInterface *transport, const SystemAddress &systemAddress); - virtual void ReturnResult(char *res, const char *command, TransportInterface *transport, const SystemAddress &systemAddress); - virtual void ReturnResult(SystemAddress res, const char *command, TransportInterface *transport, const SystemAddress &systemAddress); - virtual void ReturnResult(int res, const char *command,TransportInterface *transport, const SystemAddress &systemAddress); - - /// \brief Just writes a string to the remote system when you are calling a function that has no return value. - /// \details This is not necessary to call, but makes it easier to return results of function calls. - /// \param[in] res The result to write - /// \param[in] command The command that this result came from - /// \param[in] transport The transport interface that will be written to - /// \param[in] systemAddress The player this result will be sent to - virtual void ReturnResult(const char *command,TransportInterface *transport, const SystemAddress &systemAddress); - -protected: - DataStructures::OrderedList commandList; -}; - -} // namespace RakNet - -#endif - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file CommandParserInterface.h +/// \brief Contains CommandParserInterface , from which you derive custom command parsers +/// + + +#pragma once + +#include "RakMemoryOverride.h" +#include "RakNetTypes.h" +#include "DS_OrderedList.h" +#include "Export.h" + +namespace RakNet +{ +/// Forward declarations +class TransportInterface; + +/// \internal +/// Contains the information related to one command registered with RegisterCommand() +/// Implemented so I can have an automatic help system via SendCommandList() +struct RAK_DLL_EXPORT RegisteredCommand +{ + const char *command; + const char *commandHelp; + unsigned char parameterCount; +}; + +/// List of commands registered with RegisterCommand() +int RAK_DLL_EXPORT RegisteredCommandComp( const char* const & key, const RegisteredCommand &data ); + +/// \brief The interface used by command parsers. +/// \details CommandParserInterface provides a set of functions and interfaces that plug into the ConsoleServer class. +/// Each CommandParserInterface works at the same time as other interfaces in the system. +class RAK_DLL_EXPORT CommandParserInterface +{ +public: + CommandParserInterface(); + virtual ~CommandParserInterface(); + + /// You are responsible for overriding this function and returning a static string, which will identifier your parser. + /// This should return a static string + /// \return The name that you return. + virtual const char *GetName(void) const=0; + + /// \brief A callback for when \a systemAddress has connected to us. + /// \param[in] systemAddress The player that has connected. + /// \param[in] transport The transport interface that sent us this information. Can be used to send messages to this or other players. + virtual void OnNewIncomingConnection(const SystemAddress &systemAddress, TransportInterface *transport); + + /// \brief A callback for when \a systemAddress has disconnected, either gracefully or forcefully + /// \param[in] systemAddress The player that has disconnected. + /// \param[in] transport The transport interface that sent us this information. + virtual void OnConnectionLost(const SystemAddress &systemAddress, TransportInterface *transport); + + /// \brief A callback for when you are expected to send a brief description of your parser to \a systemAddress + /// \param[in] transport The transport interface we can use to write to + /// \param[in] systemAddress The player that requested help. + virtual void SendHelp(TransportInterface *transport, const SystemAddress &systemAddress)=0; + + /// \brief Given \a command with parameters \a parameterList , do whatever processing you wish. + /// \param[in] command The command to process + /// \param[in] numParameters How many parameters were passed along with the command + /// \param[in] parameterList The list of parameters. parameterList[0] is the first parameter and so on. + /// \param[in] transport The transport interface we can use to write to + /// \param[in] systemAddress The player that sent this command. + /// \param[in] originalString The string that was actually sent over the network, in case you want to do your own parsing + virtual bool OnCommand(const char *command, unsigned numParameters, char **parameterList, TransportInterface *transport, const SystemAddress &systemAddress, const char *originalString)=0; + + /// \brief This is called every time transport interface is registered. + /// \details If you want to save a copy of the TransportInterface pointer + /// This is the place to do it + /// \param[in] transport The new TransportInterface + virtual void OnTransportChange(TransportInterface *transport); + + /// \internal + /// Scan commandList and return the associated array + /// \param[in] command The string to find + /// \param[out] rc Contains the result of this operation + /// \return True if we found the command, false otherwise + virtual bool GetRegisteredCommand(const char *command, RegisteredCommand *rc); + + /// \internal + /// Goes through str, replacing the delineating character with 0's. + /// \param[in] str The string sent by the transport interface + /// \param[in] delineator The character to scan for to use as a delineator + /// \param[in] delineatorToggle When encountered the delineator replacement is toggled on and off + /// \param[out] numParameters How many pointers were written to \a parameterList + /// \param[out] parameterList An array of pointers to characters. Will hold pointers to locations inside \a str + /// \param[in] parameterListLength How big the \a parameterList array is + static void ParseConsoleString(char *str, const char delineator, unsigned char delineatorToggle, unsigned *numParameters, char **parameterList, unsigned parameterListLength); + + /// \internal + /// Goes through the variable commandList and sends the command portion of each struct + /// \param[in] transport The transport interface we can use to write to + /// \param[in] systemAddress The player to write to + virtual void SendCommandList(TransportInterface *transport, const SystemAddress &systemAddress); + + static const unsigned char VARIABLE_NUMBER_OF_PARAMETERS; + + // Currently only takes static strings - doesn't make a copy of what you pass. + // parameterCount is the number of parameters that the sender has to include with the command. + // Pass 255 to parameterCount to indicate variable number of parameters + + /// Registers a command. + /// \param[in] parameterCount How many parameters your command requires. If you want to accept a variable number of commands, pass CommandParserInterface::VARIABLE_NUMBER_OF_PARAMETERS + /// \param[in] command A pointer to a STATIC string that has your command. I keep a copy of the pointer here so don't deallocate the string. + /// \param[in] commandHelp A pointer to a STATIC string that has the help information for your command. I keep a copy of the pointer here so don't deallocate the string. + virtual void RegisterCommand(unsigned char parameterCount, const char *command, const char *commandHelp); + + /// \brief Just writes a string to the remote system based on the result ( \a res ) of your operation + /// \details This is not necessary to call, but makes it easier to return results of function calls. + /// \param[in] res The result to write + /// \param[in] command The command that this result came from + /// \param[in] transport The transport interface that will be written to + /// \param[in] systemAddress The player this result will be sent to + virtual void ReturnResult(bool res, const char *command, TransportInterface *transport, const SystemAddress &systemAddress); + virtual void ReturnResult(char *res, const char *command, TransportInterface *transport, const SystemAddress &systemAddress); + virtual void ReturnResult(SystemAddress res, const char *command, TransportInterface *transport, const SystemAddress &systemAddress); + virtual void ReturnResult(int res, const char *command,TransportInterface *transport, const SystemAddress &systemAddress); + + /// \brief Just writes a string to the remote system when you are calling a function that has no return value. + /// \details This is not necessary to call, but makes it easier to return results of function calls. + /// \param[in] res The result to write + /// \param[in] command The command that this result came from + /// \param[in] transport The transport interface that will be written to + /// \param[in] systemAddress The player this result will be sent to + virtual void ReturnResult(const char *command,TransportInterface *transport, const SystemAddress &systemAddress); + +protected: + DataStructures::OrderedList commandList; +}; + +} // namespace RakNet + diff --git a/Source/ConnectionGraph2.h b/Source/ConnectionGraph2.h index 2236eda98..aaef9fac5 100644 --- a/Source/ConnectionGraph2.h +++ b/Source/ConnectionGraph2.h @@ -1,126 +1,124 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file ConnectionGraph2.h -/// \brief Connection graph plugin, version 2. Tells new systems about existing and new connections -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_ConnectionGraph2==1 - -#ifndef __CONNECTION_GRAPH_2_H -#define __CONNECTION_GRAPH_2_H - -#include "RakMemoryOverride.h" -#include "RakNetTypes.h" -#include "PluginInterface2.h" -#include "DS_List.h" -#include "DS_WeightedGraph.h" -#include "GetTime.h" -#include "Export.h" - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; - -/// \brief A one hop connection graph. -/// \details Sends ID_REMOTE_CONNECTION_LOST, ID_REMOTE_DISCONNECTION_NOTIFICATION, ID_REMOTE_NEW_INCOMING_CONNECTION
-/// All identifiers are followed by SystemAddress, then RakNetGUID -/// Also stores the list for you, which you can access with GetConnectionListForRemoteSystem -/// \ingroup CONNECTION_GRAPH_GROUP -class RAK_DLL_EXPORT ConnectionGraph2 : public PluginInterface2 -{ -public: - - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(ConnectionGraph2) - - ConnectionGraph2(); - ~ConnectionGraph2(); - - /// \brief Given a remote system identified by RakNetGUID, return the list of SystemAddresses and RakNetGUIDs they are connected to - /// \param[in] remoteSystemGuid Which system we are referring to. This only works for remote systems, not ourselves. - /// \param[out] saOut A preallocated array to hold the output list of SystemAddress. Can be 0 if you don't care. - /// \param[out] guidOut A preallocated array to hold the output list of RakNetGUID. Can be 0 if you don't care. - /// \param[in,out] outLength On input, the size of \a saOut and \a guidOut. On output, modified to reflect the number of elements actually written - /// \return True if \a remoteSystemGuid was found. Otherwise false, and \a saOut, \a guidOut remain unchanged. \a outLength will be set to 0. - bool GetConnectionListForRemoteSystem(RakNetGUID remoteSystemGuid, SystemAddress *saOut, RakNetGUID *guidOut, unsigned int *outLength); - - /// Returns if g1 is connected to g2 - bool ConnectionExists(RakNetGUID g1, RakNetGUID g2); - - /// Returns the average ping between two systems in the connection graph. Returns -1 if no connection exists between those systems - uint16_t GetPingBetweenSystems(RakNetGUID g1, RakNetGUID g2) const; - - /// Returns the system with the lowest average ping among all its connections. - /// If you need one system in the peer to peer group to relay data, have the FullyConnectedMesh2 host call this function after host migration, and use that system - RakNetGUID GetLowestAveragePingSystem(void) const; - - /// \brief If called with false, then new connections are only added to the connection graph when you call ProcessNewConnection(); - /// \details This is useful if you want to perform validation before connecting a system to a mesh, or if you want a submesh (for example a server cloud) - /// \param[in] b True to automatically call ProcessNewConnection() on any new connection, false to not do so. Defaults to true. - void SetAutoProcessNewConnections(bool b); - - /// \brief Returns value passed to SetAutoProcessNewConnections() - /// \return Value passed to SetAutoProcessNewConnections(), or the default of true if it was never called - bool GetAutoProcessNewConnections(void) const; - - /// \brief If you call SetAutoProcessNewConnections(false);, then you will need to manually call ProcessNewConnection() on new connections - /// \details On ID_NEW_INCOMING_CONNECTION or ID_CONNECTION_REQUEST_ACCEPTED, adds that system to the graph - /// Do not call ProcessNewConnection() manually otherwise - /// \param[in] The packet->SystemAddress member - /// \param[in] The packet->guid member - void AddParticipant(const SystemAddress &systemAddress, RakNetGUID rakNetGUID); - - /// Get the participants added with AddParticipant() - /// \param[out] participantList Participants added with AddParticipant(); - void GetParticipantList(DataStructures::OrderedList &participantList); - - /// \internal - struct SystemAddressAndGuid - { - SystemAddress systemAddress; - RakNetGUID guid; - uint16_t sendersPingToThatSystem; - }; - /// \internal - static int SystemAddressAndGuidComp( const SystemAddressAndGuid &key, const SystemAddressAndGuid &data ); - - /// \internal - struct RemoteSystem - { - DataStructures::OrderedList remoteConnections; - RakNetGUID guid; - }; - /// \internal - static int RemoteSystemComp( const RakNetGUID &key, RemoteSystem * const &data ); - -protected: - /// \internal - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - /// \internal - virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); - /// \internal - virtual PluginReceiveResult OnReceive(Packet *packet); - - // List of systems I am connected to, which in turn stores which systems they are connected to - DataStructures::OrderedList remoteSystems; - - bool autoProcessNewConnections; - -}; - -} // namespace RakNet - -#endif // #ifndef __CONNECTION_GRAPH_2_H - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file ConnectionGraph2.h +/// \brief Connection graph plugin, version 2. Tells new systems about existing and new connections +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_ConnectionGraph2==1 + +#pragma once + +#include "RakMemoryOverride.h" +#include "RakNetTypes.h" +#include "PluginInterface2.h" +#include "DS_List.h" +#include "DS_WeightedGraph.h" +#include "GetTime.h" +#include "Export.h" + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; + +/// \brief A one hop connection graph. +/// \details Sends ID_REMOTE_CONNECTION_LOST, ID_REMOTE_DISCONNECTION_NOTIFICATION, ID_REMOTE_NEW_INCOMING_CONNECTION
+/// All identifiers are followed by SystemAddress, then RakNetGUID +/// Also stores the list for you, which you can access with GetConnectionListForRemoteSystem +/// \ingroup CONNECTION_GRAPH_GROUP +class RAK_DLL_EXPORT ConnectionGraph2 : public PluginInterface2 +{ +public: + + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(ConnectionGraph2) + + ConnectionGraph2(); + ~ConnectionGraph2(); + + /// \brief Given a remote system identified by RakNetGUID, return the list of SystemAddresses and RakNetGUIDs they are connected to + /// \param[in] remoteSystemGuid Which system we are referring to. This only works for remote systems, not ourselves. + /// \param[out] saOut A preallocated array to hold the output list of SystemAddress. Can be 0 if you don't care. + /// \param[out] guidOut A preallocated array to hold the output list of RakNetGUID. Can be 0 if you don't care. + /// \param[in,out] outLength On input, the size of \a saOut and \a guidOut. On output, modified to reflect the number of elements actually written + /// \return True if \a remoteSystemGuid was found. Otherwise false, and \a saOut, \a guidOut remain unchanged. \a outLength will be set to 0. + bool GetConnectionListForRemoteSystem(RakNetGUID remoteSystemGuid, SystemAddress *saOut, RakNetGUID *guidOut, unsigned int *outLength); + + /// Returns if g1 is connected to g2 + bool ConnectionExists(RakNetGUID g1, RakNetGUID g2); + + /// Returns the average ping between two systems in the connection graph. Returns -1 if no connection exists between those systems + uint16_t GetPingBetweenSystems(RakNetGUID g1, RakNetGUID g2) const; + + /// Returns the system with the lowest average ping among all its connections. + /// If you need one system in the peer to peer group to relay data, have the FullyConnectedMesh2 host call this function after host migration, and use that system + RakNetGUID GetLowestAveragePingSystem(void) const; + + /// \brief If called with false, then new connections are only added to the connection graph when you call ProcessNewConnection(); + /// \details This is useful if you want to perform validation before connecting a system to a mesh, or if you want a submesh (for example a server cloud) + /// \param[in] b True to automatically call ProcessNewConnection() on any new connection, false to not do so. Defaults to true. + void SetAutoProcessNewConnections(bool b); + + /// \brief Returns value passed to SetAutoProcessNewConnections() + /// \return Value passed to SetAutoProcessNewConnections(), or the default of true if it was never called + bool GetAutoProcessNewConnections(void) const; + + /// \brief If you call SetAutoProcessNewConnections(false);, then you will need to manually call ProcessNewConnection() on new connections + /// \details On ID_NEW_INCOMING_CONNECTION or ID_CONNECTION_REQUEST_ACCEPTED, adds that system to the graph + /// Do not call ProcessNewConnection() manually otherwise + /// \param[in] The packet->SystemAddress member + /// \param[in] The packet->guid member + void AddParticipant(const SystemAddress &systemAddress, RakNetGUID rakNetGUID); + + /// Get the participants added with AddParticipant() + /// \param[out] participantList Participants added with AddParticipant(); + void GetParticipantList(DataStructures::OrderedList &participantList); + + /// \internal + struct SystemAddressAndGuid + { + SystemAddress systemAddress; + RakNetGUID guid; + uint16_t sendersPingToThatSystem; + }; + /// \internal + static int SystemAddressAndGuidComp( const SystemAddressAndGuid &key, const SystemAddressAndGuid &data ); + + /// \internal + struct RemoteSystem + { + DataStructures::OrderedList remoteConnections; + RakNetGUID guid; + }; + /// \internal + static int RemoteSystemComp( const RakNetGUID &key, RemoteSystem * const &data ); + +protected: + /// \internal + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + /// \internal + virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); + /// \internal + virtual PluginReceiveResult OnReceive(Packet *packet); + + // List of systems I am connected to, which in turn stores which systems they are connected to + DataStructures::OrderedList remoteSystems; + + bool autoProcessNewConnections; + +}; + +} // namespace RakNet + +#endif // #ifndef __CONNECTION_GRAPH_2_H + diff --git a/Source/ConsoleServer.h b/Source/ConsoleServer.h index 71b9f2e50..d71937209 100644 --- a/Source/ConsoleServer.h +++ b/Source/ConsoleServer.h @@ -1,84 +1,82 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file ConsoleServer.h -/// \brief Contains ConsoleServer , used to plugin to your game to accept remote console-based connections -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_ConsoleServer==1 - -#ifndef __CONSOLE_SERVER_H -#define __CONSOLE_SERVER_H - -#include "RakMemoryOverride.h" -#include "DS_List.h" -#include "RakNetTypes.h" -#include "Export.h" - -namespace RakNet -{ -/// Forward declarations -class TransportInterface; -class CommandParserInterface; - - -/// \brief The main entry point for the server portion of your remote console application support. -/// \details ConsoleServer takes one TransportInterface and one or more CommandParserInterface (s) -/// The TransportInterface will be used to send data between the server and the client. The connecting client must support the -/// protocol used by your derivation of TransportInterface . TelnetTransport and RakNetTransport are two such derivations . -/// When a command is sent by a remote console, it will be processed by your implementations of CommandParserInterface -class RAK_DLL_EXPORT ConsoleServer -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(ConsoleServer) - - ConsoleServer(); - ~ConsoleServer(); - - /// \brief Call this with a derivation of TransportInterface so that the console server can send and receive commands - /// \param[in] transportInterface Your interface to use. - /// \param[in] port The port to host on. Telnet uses port 23 by default. RakNet can use whatever you want. - void SetTransportProvider(TransportInterface *transportInterface, unsigned short port); - - /// \brief Add an implementation of CommandParserInterface to the list of command parsers. - /// \param[in] commandParserInterface The command parser referred to - void AddCommandParser(CommandParserInterface *commandParserInterface); - - /// \brief Remove an implementation of CommandParserInterface previously added with AddCommandParser(). - /// \param[in] commandParserInterface The command parser referred to - void RemoveCommandParser(CommandParserInterface *commandParserInterface); - - /// \brief Call update to read packet sent from your TransportInterface. - /// You should do this fairly frequently. - void Update(void); - - /// \brief Sets a prompt to show when waiting for user input. - /// \details Pass an empty string to clear the prompt - /// Defaults to no prompt - /// \param[in] _prompt Null-terminated string of the prompt to use. If you want a newline, be sure to use /r/n - void SetPrompt(const char *_prompt); - -protected: - void ListParsers(SystemAddress systemAddress); - void ShowPrompt(SystemAddress systemAddress); - TransportInterface *transport; - DataStructures::List commandParserList; - char* password[256]; - char *prompt; -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file ConsoleServer.h +/// \brief Contains ConsoleServer , used to plugin to your game to accept remote console-based connections +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_ConsoleServer==1 + +#pragma once + +#include "RakMemoryOverride.h" +#include "DS_List.h" +#include "RakNetTypes.h" +#include "Export.h" + +namespace RakNet +{ +/// Forward declarations +class TransportInterface; +class CommandParserInterface; + + +/// \brief The main entry point for the server portion of your remote console application support. +/// \details ConsoleServer takes one TransportInterface and one or more CommandParserInterface (s) +/// The TransportInterface will be used to send data between the server and the client. The connecting client must support the +/// protocol used by your derivation of TransportInterface . TelnetTransport and RakNetTransport are two such derivations . +/// When a command is sent by a remote console, it will be processed by your implementations of CommandParserInterface +class RAK_DLL_EXPORT ConsoleServer +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(ConsoleServer) + + ConsoleServer(); + ~ConsoleServer(); + + /// \brief Call this with a derivation of TransportInterface so that the console server can send and receive commands + /// \param[in] transportInterface Your interface to use. + /// \param[in] port The port to host on. Telnet uses port 23 by default. RakNet can use whatever you want. + void SetTransportProvider(TransportInterface *transportInterface, unsigned short port); + + /// \brief Add an implementation of CommandParserInterface to the list of command parsers. + /// \param[in] commandParserInterface The command parser referred to + void AddCommandParser(CommandParserInterface *commandParserInterface); + + /// \brief Remove an implementation of CommandParserInterface previously added with AddCommandParser(). + /// \param[in] commandParserInterface The command parser referred to + void RemoveCommandParser(CommandParserInterface *commandParserInterface); + + /// \brief Call update to read packet sent from your TransportInterface. + /// You should do this fairly frequently. + void Update(void); + + /// \brief Sets a prompt to show when waiting for user input. + /// \details Pass an empty string to clear the prompt + /// Defaults to no prompt + /// \param[in] _prompt Null-terminated string of the prompt to use. If you want a newline, be sure to use /r/n + void SetPrompt(const char *_prompt); + +protected: + void ListParsers(SystemAddress systemAddress); + void ShowPrompt(SystemAddress systemAddress); + TransportInterface *transport; + DataStructures::List commandParserList; + char* password[256]; + char *prompt; +}; + +} // namespace RakNet + +#endif + diff --git a/Source/DR_SHA1.cpp b/Source/DR_SHA1.cpp index 9561c4bae..237e089d8 100644 --- a/Source/DR_SHA1.cpp +++ b/Source/DR_SHA1.cpp @@ -1,312 +1,312 @@ -/* - 100% free public domain implementation of the SHA-1 algorithm - by Dominik Reichl - Web: http://www.dominik-reichl.de/ - - See header file for version history and test vectors. -*/ - -// If compiling with MFC, you might want to add #include "StdAfx.h" - -#define _CRT_SECURE_NO_WARNINGS -#include "DR_SHA1.h" -#include - -#define SHA1_MAX_FILE_BUFFER (32 * 20 * 820) - -// Rotate p_val32 by p_nBits bits to the left -#ifndef ROL32 -#ifdef _MSC_VER -#define ROL32(p_val32,p_nBits) _rotl(p_val32,p_nBits) -#else -#define ROL32(p_val32,p_nBits) (((p_val32)<<(p_nBits))|((p_val32)>>(32-(p_nBits)))) -#endif -#endif - -#ifdef SHA1_LITTLE_ENDIAN -#define SHABLK0(i) (m_block->l[i] = \ - (ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF)) -#else -#define SHABLK0(i) (m_block->l[i]) -#endif - -#define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ \ - m_block->l[(i+8)&15] ^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1)) - -// SHA-1 rounds -#define S_R0(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);} -#define S_R1(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);} -#define S_R2(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5);w=ROL32(w,30);} -#define S_R3(v,w,x,y,z,i) {z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5);w=ROL32(w,30);} -#define S_R4(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5);w=ROL32(w,30);} - -#pragma warning(push) -// Disable compiler warning 'Conditional expression is constant' -#pragma warning(disable: 4127) - -CSHA1::CSHA1() -{ - m_block = (SHA1_WORKSPACE_BLOCK*)m_workspace; - - Reset(); -} - -#ifdef SHA1_WIPE_VARIABLES -CSHA1::~CSHA1() -{ - Reset(); -} -#endif - -void CSHA1::Reset() -{ - // SHA1 initialization constants - m_state[0] = 0x67452301; - m_state[1] = 0xEFCDAB89; - m_state[2] = 0x98BADCFE; - m_state[3] = 0x10325476; - m_state[4] = 0xC3D2E1F0; - - m_count[0] = 0; - m_count[1] = 0; -} - -void CSHA1::Transform(UINT_32* pState, const UINT_8* pBuffer) -{ - UINT_32 a = pState[0], b = pState[1], c = pState[2], d = pState[3], e = pState[4]; - - memcpy(m_block, pBuffer, 64); - - // 4 rounds of 20 operations each, loop unrolled - S_R0(a,b,c,d,e, 0); S_R0(e,a,b,c,d, 1); S_R0(d,e,a,b,c, 2); S_R0(c,d,e,a,b, 3); - S_R0(b,c,d,e,a, 4); S_R0(a,b,c,d,e, 5); S_R0(e,a,b,c,d, 6); S_R0(d,e,a,b,c, 7); - S_R0(c,d,e,a,b, 8); S_R0(b,c,d,e,a, 9); S_R0(a,b,c,d,e,10); S_R0(e,a,b,c,d,11); - S_R0(d,e,a,b,c,12); S_R0(c,d,e,a,b,13); S_R0(b,c,d,e,a,14); S_R0(a,b,c,d,e,15); - S_R1(e,a,b,c,d,16); S_R1(d,e,a,b,c,17); S_R1(c,d,e,a,b,18); S_R1(b,c,d,e,a,19); - S_R2(a,b,c,d,e,20); S_R2(e,a,b,c,d,21); S_R2(d,e,a,b,c,22); S_R2(c,d,e,a,b,23); - S_R2(b,c,d,e,a,24); S_R2(a,b,c,d,e,25); S_R2(e,a,b,c,d,26); S_R2(d,e,a,b,c,27); - S_R2(c,d,e,a,b,28); S_R2(b,c,d,e,a,29); S_R2(a,b,c,d,e,30); S_R2(e,a,b,c,d,31); - S_R2(d,e,a,b,c,32); S_R2(c,d,e,a,b,33); S_R2(b,c,d,e,a,34); S_R2(a,b,c,d,e,35); - S_R2(e,a,b,c,d,36); S_R2(d,e,a,b,c,37); S_R2(c,d,e,a,b,38); S_R2(b,c,d,e,a,39); - S_R3(a,b,c,d,e,40); S_R3(e,a,b,c,d,41); S_R3(d,e,a,b,c,42); S_R3(c,d,e,a,b,43); - S_R3(b,c,d,e,a,44); S_R3(a,b,c,d,e,45); S_R3(e,a,b,c,d,46); S_R3(d,e,a,b,c,47); - S_R3(c,d,e,a,b,48); S_R3(b,c,d,e,a,49); S_R3(a,b,c,d,e,50); S_R3(e,a,b,c,d,51); - S_R3(d,e,a,b,c,52); S_R3(c,d,e,a,b,53); S_R3(b,c,d,e,a,54); S_R3(a,b,c,d,e,55); - S_R3(e,a,b,c,d,56); S_R3(d,e,a,b,c,57); S_R3(c,d,e,a,b,58); S_R3(b,c,d,e,a,59); - S_R4(a,b,c,d,e,60); S_R4(e,a,b,c,d,61); S_R4(d,e,a,b,c,62); S_R4(c,d,e,a,b,63); - S_R4(b,c,d,e,a,64); S_R4(a,b,c,d,e,65); S_R4(e,a,b,c,d,66); S_R4(d,e,a,b,c,67); - S_R4(c,d,e,a,b,68); S_R4(b,c,d,e,a,69); S_R4(a,b,c,d,e,70); S_R4(e,a,b,c,d,71); - S_R4(d,e,a,b,c,72); S_R4(c,d,e,a,b,73); S_R4(b,c,d,e,a,74); S_R4(a,b,c,d,e,75); - S_R4(e,a,b,c,d,76); S_R4(d,e,a,b,c,77); S_R4(c,d,e,a,b,78); S_R4(b,c,d,e,a,79); - - // Add the working vars back into state - pState[0] += a; - pState[1] += b; - pState[2] += c; - pState[3] += d; - pState[4] += e; - - // Wipe variables -#ifdef SHA1_WIPE_VARIABLES - a = b = c = d = e = 0; -#endif -} - -void CSHA1::Update(const UINT_8* pbData, UINT_32 uLen) -{ - UINT_32 j = ((m_count[0] >> 3) & 0x3F); - - if((m_count[0] += (uLen << 3)) < (uLen << 3)) - ++m_count[1]; // Overflow - - m_count[1] += (uLen >> 29); - - UINT_32 i; - if((j + uLen) > 63) - { - i = 64 - j; - memcpy(&m_buffer[j], pbData, i); - Transform(m_state, m_buffer); - - for( ; (i + 63) < uLen; i += 64) - Transform(m_state, &pbData[i]); - - j = 0; - } - else i = 0; - - if((uLen - i) != 0) - memcpy(&m_buffer[j], &pbData[i], uLen - i); -} - -#ifdef SHA1_UTILITY_FUNCTIONS -bool CSHA1::HashFile(const TCHAR* tszFileName) -{ - if(tszFileName == NULL) return false; - - FILE* fpIn = _tfopen(tszFileName, _T("rb")); - if(fpIn == NULL) return false; - - UINT_8* pbData = new UINT_8[SHA1_MAX_FILE_BUFFER]; - if(pbData == NULL) { fclose(fpIn); return false; } - - bool bSuccess = true; - while(true) - { - const size_t uRead = fread(pbData, 1, SHA1_MAX_FILE_BUFFER, fpIn); - - if(uRead > 0) - Update(pbData, static_cast(uRead)); - - if(uRead < SHA1_MAX_FILE_BUFFER) - { - if(feof(fpIn) == 0) bSuccess = false; - break; - } - } - - fclose(fpIn); - delete[] pbData; - return bSuccess; -} -#endif - -void CSHA1::Final() -{ - UINT_32 i; - - UINT_8 pbFinalCount[8]; - for(i = 0; i < 8; ++i) - pbFinalCount[i] = static_cast((m_count[((i >= 4) ? 0 : 1)] >> - ((3 - (i & 3)) * 8) ) & 0xFF); // Endian independent - - Update((UINT_8*)"\200", 1); - - while((m_count[0] & 504) != 448) - Update((UINT_8*)"\0", 1); - - Update(pbFinalCount, 8); // Cause a Transform() - - for(i = 0; i < 20; ++i) - m_digest[i] = static_cast((m_state[i >> 2] >> ((3 - - (i & 3)) * 8)) & 0xFF); - - // Wipe variables for security reasons -#ifdef SHA1_WIPE_VARIABLES - memset(m_buffer, 0, 64); - memset(m_state, 0, 20); - memset(m_count, 0, 8); - memset(pbFinalCount, 0, 8); - Transform(m_state, m_buffer); -#endif -} - -#ifdef SHA1_UTILITY_FUNCTIONS -bool CSHA1::ReportHash(TCHAR* tszReport, REPORT_TYPE rtReportType) const -{ - if(tszReport == NULL) return false; - - TCHAR tszTemp[16]; - - if((rtReportType == REPORT_HEX) || (rtReportType == REPORT_HEX_SHORT)) - { - _sntprintf(tszTemp, 15, _T("%02X"), m_digest[0]); - _tcscpy(tszReport, tszTemp); - - const TCHAR* lpFmt = ((rtReportType == REPORT_HEX) ? _T(" %02X") : _T("%02X")); - for(size_t i = 1; i < 20; ++i) - { - _sntprintf(tszTemp, 15, lpFmt, m_digest[i]); - _tcscat(tszReport, tszTemp); - } - } - else if(rtReportType == REPORT_DIGIT) - { - _sntprintf(tszTemp, 15, _T("%u"), m_digest[0]); - _tcscpy(tszReport, tszTemp); - - for(size_t i = 1; i < 20; ++i) - { - _sntprintf(tszTemp, 15, _T(" %u"), m_digest[i]); - _tcscat(tszReport, tszTemp); - } - } - else return false; - - return true; -} -#endif - -#ifdef SHA1_STL_FUNCTIONS -bool CSHA1::ReportHashStl(std::basic_string& strOut, REPORT_TYPE rtReportType) const -{ - TCHAR tszOut[84]; - const bool bResult = ReportHash(tszOut, rtReportType); - if(bResult) strOut = tszOut; - return bResult; -} -#endif - -bool CSHA1::GetHash(UINT_8* pbDest20) const -{ - if(pbDest20 == NULL) return false; - memcpy(pbDest20, m_digest, 20); - return true; -} - -// Get the raw message digest -// Added by Kevin to be quicker -unsigned char * CSHA1::GetHash( void ) const -{ - return ( unsigned char * ) m_digest; -} - -// http://cseweb.ucsd.edu/~mihir/papers/hmac-cb.pdf -// Sample code: http://www.opensource.apple.com/source/freeradius/freeradius-11/freeradius/src/lib/hmac.c -void CSHA1::HMAC(unsigned char *sharedKey, int sharedKeyLength, unsigned char *data, int dataLength, unsigned char output[SHA1_LENGTH]) -{ - // 1. Append zeros to the end of K to create a 64 byte string - static const int sha1BlockLength=64; - - if (sharedKeyLength > sha1BlockLength) - sharedKeyLength = sha1BlockLength; - - // ipad = the byte 0x36 repeated 64 times - // opad = the byte 0x5C repeated 64 times - unsigned char keyWithIpad[sha1BlockLength]; - unsigned char keyWithOpad[sha1BlockLength]; - - memset( keyWithIpad, 0, sizeof(keyWithIpad)); - memset( keyWithOpad, 0, sizeof(keyWithOpad)); - memcpy( keyWithIpad, sharedKey, sharedKeyLength); - memcpy( keyWithOpad, sharedKey, sharedKeyLength); - - for (int i = 0; i < sha1BlockLength; i++) { - keyWithIpad[i] ^= 0x36; - keyWithOpad[i] ^= 0x5c; - } - - // 3. Append the data stream Text to the 64 byte string resulting from step (2) - // 4. Apply H to the stream generated in step (3) - CSHA1 firstHash; - firstHash.Reset(); - firstHash.Update( keyWithIpad, sha1BlockLength ); - firstHash.Update( data, dataLength ); - firstHash.Final(); - - // 6. Append the H (hash) result from step (4) to the 64 byte string resulting from step (5) - // 7. Apply H to the stream generated in step (6) and output the result - CSHA1 secondHash; - secondHash.Reset(); - secondHash.Update( keyWithOpad, sha1BlockLength ); - secondHash.Update( firstHash.GetHash(), SHA1_LENGTH ); - secondHash.Final(); - - memcpy(output, secondHash.GetHash(), SHA1_LENGTH); - - // char report[128]; - // memset(report,0,128); - // secondHash.ReportHash( report, 0 ); -} - -#pragma warning(pop) +/* + 100% free public domain implementation of the SHA-1 algorithm + by Dominik Reichl + Web: http://www.dominik-reichl.de/ + + See header file for version history and test vectors. +*/ + +// If compiling with MFC, you might want to add #include "StdAfx.h" + +#define _CRT_SECURE_NO_WARNINGS +#include "DR_SHA1.h" +#include + +#define SHA1_MAX_FILE_BUFFER (32 * 20 * 820) + +// Rotate p_val32 by p_nBits bits to the left +#ifndef ROL32 +#ifdef _MSC_VER +#define ROL32(p_val32,p_nBits) _rotl(p_val32,p_nBits) +#else +#define ROL32(p_val32,p_nBits) (((p_val32)<<(p_nBits))|((p_val32)>>(32-(p_nBits)))) +#endif +#endif + +#ifdef SHA1_LITTLE_ENDIAN +#define SHABLK0(i) (m_block->l[i] = \ + (ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF)) +#else +#define SHABLK0(i) (m_block->l[i]) +#endif + +#define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ \ + m_block->l[(i+8)&15] ^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1)) + +// SHA-1 rounds +#define S_R0(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);} +#define S_R1(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);} +#define S_R2(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5);w=ROL32(w,30);} +#define S_R3(v,w,x,y,z,i) {z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5);w=ROL32(w,30);} +#define S_R4(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5);w=ROL32(w,30);} + +#pragma warning(push) +// Disable compiler warning 'Conditional expression is constant' +#pragma warning(disable: 4127) + +CSHA1::CSHA1() +{ + m_block = (SHA1_WORKSPACE_BLOCK*)m_workspace; + + Reset(); +} + +#ifdef SHA1_WIPE_VARIABLES +CSHA1::~CSHA1() +{ + Reset(); +} +#endif + +void CSHA1::Reset() +{ + // SHA1 initialization constants + m_state[0] = 0x67452301; + m_state[1] = 0xEFCDAB89; + m_state[2] = 0x98BADCFE; + m_state[3] = 0x10325476; + m_state[4] = 0xC3D2E1F0; + + m_count[0] = 0; + m_count[1] = 0; +} + +void CSHA1::Transform(UINT_32* pState, const UINT_8* pBuffer) +{ + UINT_32 a = pState[0], b = pState[1], c = pState[2], d = pState[3], e = pState[4]; + + memcpy(m_block, pBuffer, 64); + + // 4 rounds of 20 operations each, loop unrolled + S_R0(a,b,c,d,e, 0); S_R0(e,a,b,c,d, 1); S_R0(d,e,a,b,c, 2); S_R0(c,d,e,a,b, 3); + S_R0(b,c,d,e,a, 4); S_R0(a,b,c,d,e, 5); S_R0(e,a,b,c,d, 6); S_R0(d,e,a,b,c, 7); + S_R0(c,d,e,a,b, 8); S_R0(b,c,d,e,a, 9); S_R0(a,b,c,d,e,10); S_R0(e,a,b,c,d,11); + S_R0(d,e,a,b,c,12); S_R0(c,d,e,a,b,13); S_R0(b,c,d,e,a,14); S_R0(a,b,c,d,e,15); + S_R1(e,a,b,c,d,16); S_R1(d,e,a,b,c,17); S_R1(c,d,e,a,b,18); S_R1(b,c,d,e,a,19); + S_R2(a,b,c,d,e,20); S_R2(e,a,b,c,d,21); S_R2(d,e,a,b,c,22); S_R2(c,d,e,a,b,23); + S_R2(b,c,d,e,a,24); S_R2(a,b,c,d,e,25); S_R2(e,a,b,c,d,26); S_R2(d,e,a,b,c,27); + S_R2(c,d,e,a,b,28); S_R2(b,c,d,e,a,29); S_R2(a,b,c,d,e,30); S_R2(e,a,b,c,d,31); + S_R2(d,e,a,b,c,32); S_R2(c,d,e,a,b,33); S_R2(b,c,d,e,a,34); S_R2(a,b,c,d,e,35); + S_R2(e,a,b,c,d,36); S_R2(d,e,a,b,c,37); S_R2(c,d,e,a,b,38); S_R2(b,c,d,e,a,39); + S_R3(a,b,c,d,e,40); S_R3(e,a,b,c,d,41); S_R3(d,e,a,b,c,42); S_R3(c,d,e,a,b,43); + S_R3(b,c,d,e,a,44); S_R3(a,b,c,d,e,45); S_R3(e,a,b,c,d,46); S_R3(d,e,a,b,c,47); + S_R3(c,d,e,a,b,48); S_R3(b,c,d,e,a,49); S_R3(a,b,c,d,e,50); S_R3(e,a,b,c,d,51); + S_R3(d,e,a,b,c,52); S_R3(c,d,e,a,b,53); S_R3(b,c,d,e,a,54); S_R3(a,b,c,d,e,55); + S_R3(e,a,b,c,d,56); S_R3(d,e,a,b,c,57); S_R3(c,d,e,a,b,58); S_R3(b,c,d,e,a,59); + S_R4(a,b,c,d,e,60); S_R4(e,a,b,c,d,61); S_R4(d,e,a,b,c,62); S_R4(c,d,e,a,b,63); + S_R4(b,c,d,e,a,64); S_R4(a,b,c,d,e,65); S_R4(e,a,b,c,d,66); S_R4(d,e,a,b,c,67); + S_R4(c,d,e,a,b,68); S_R4(b,c,d,e,a,69); S_R4(a,b,c,d,e,70); S_R4(e,a,b,c,d,71); + S_R4(d,e,a,b,c,72); S_R4(c,d,e,a,b,73); S_R4(b,c,d,e,a,74); S_R4(a,b,c,d,e,75); + S_R4(e,a,b,c,d,76); S_R4(d,e,a,b,c,77); S_R4(c,d,e,a,b,78); S_R4(b,c,d,e,a,79); + + // Add the working vars back into state + pState[0] += a; + pState[1] += b; + pState[2] += c; + pState[3] += d; + pState[4] += e; + + // Wipe variables +#ifdef SHA1_WIPE_VARIABLES + a = b = c = d = e = 0; +#endif +} + +void CSHA1::Update(const UINT_8* pbData, UINT_32 uLen) +{ + UINT_32 j = ((m_count[0] >> 3) & 0x3F); + + if((m_count[0] += (uLen << 3)) < (uLen << 3)) + ++m_count[1]; // Overflow + + m_count[1] += (uLen >> 29); + + UINT_32 i; + if((j + uLen) > 63) + { + i = 64 - j; + memcpy(&m_buffer[j], pbData, i); + Transform(m_state, m_buffer); + + for( ; (i + 63) < uLen; i += 64) + Transform(m_state, &pbData[i]); + + j = 0; + } + else i = 0; + + if((uLen - i) != 0) + memcpy(&m_buffer[j], &pbData[i], uLen - i); +} + +#ifdef SHA1_UTILITY_FUNCTIONS +bool CSHA1::HashFile(const TCHAR* tszFileName) +{ + if(tszFileName == nullptr) return false; + + FILE* fpIn = _tfopen(tszFileName, _T("rb")); + if(fpIn == nullptr) return false; + + UINT_8* pbData = new UINT_8[SHA1_MAX_FILE_BUFFER]; + if(pbData == nullptr) { fclose(fpIn); return false; } + + bool bSuccess = true; + while(true) + { + const size_t uRead = fread(pbData, 1, SHA1_MAX_FILE_BUFFER, fpIn); + + if(uRead > 0) + Update(pbData, static_cast(uRead)); + + if(uRead < SHA1_MAX_FILE_BUFFER) + { + if(feof(fpIn) == 0) bSuccess = false; + break; + } + } + + fclose(fpIn); + delete[] pbData; + return bSuccess; +} +#endif + +void CSHA1::Final() +{ + UINT_32 i; + + UINT_8 pbFinalCount[8]; + for(i = 0; i < 8; ++i) + pbFinalCount[i] = static_cast((m_count[((i >= 4) ? 0 : 1)] >> + ((3 - (i & 3)) * 8) ) & 0xFF); // Endian independent + + Update((UINT_8*)"\200", 1); + + while((m_count[0] & 504) != 448) + Update((UINT_8*)"\0", 1); + + Update(pbFinalCount, 8); // Cause a Transform() + + for(i = 0; i < 20; ++i) + m_digest[i] = static_cast((m_state[i >> 2] >> ((3 - + (i & 3)) * 8)) & 0xFF); + + // Wipe variables for security reasons +#ifdef SHA1_WIPE_VARIABLES + memset(m_buffer, 0, 64); + memset(m_state, 0, 20); + memset(m_count, 0, 8); + memset(pbFinalCount, 0, 8); + Transform(m_state, m_buffer); +#endif +} + +#ifdef SHA1_UTILITY_FUNCTIONS +bool CSHA1::ReportHash(TCHAR* tszReport, REPORT_TYPE rtReportType) const +{ + if(tszReport == nullptr) return false; + + TCHAR tszTemp[16]; + + if((rtReportType == REPORT_HEX) || (rtReportType == REPORT_HEX_SHORT)) + { + _sntprintf(tszTemp, 15, _T("%02X"), m_digest[0]); + _tcscpy(tszReport, tszTemp); + + const TCHAR* lpFmt = ((rtReportType == REPORT_HEX) ? _T(" %02X") : _T("%02X")); + for(size_t i = 1; i < 20; ++i) + { + _sntprintf(tszTemp, 15, lpFmt, m_digest[i]); + _tcscat(tszReport, tszTemp); + } + } + else if(rtReportType == REPORT_DIGIT) + { + _sntprintf(tszTemp, 15, _T("%u"), m_digest[0]); + _tcscpy(tszReport, tszTemp); + + for(size_t i = 1; i < 20; ++i) + { + _sntprintf(tszTemp, 15, _T(" %u"), m_digest[i]); + _tcscat(tszReport, tszTemp); + } + } + else return false; + + return true; +} +#endif + +#ifdef SHA1_STL_FUNCTIONS +bool CSHA1::ReportHashStl(std::basic_string& strOut, REPORT_TYPE rtReportType) const +{ + TCHAR tszOut[84]; + const bool bResult = ReportHash(tszOut, rtReportType); + if(bResult) strOut = tszOut; + return bResult; +} +#endif + +bool CSHA1::GetHash(UINT_8* pbDest20) const +{ + if(pbDest20 == nullptr) return false; + memcpy(pbDest20, m_digest, 20); + return true; +} + +// Get the raw message digest +// Added by Kevin to be quicker +unsigned char * CSHA1::GetHash( void ) const +{ + return ( unsigned char * ) m_digest; +} + +// http://cseweb.ucsd.edu/~mihir/papers/hmac-cb.pdf +// Sample code: http://www.opensource.apple.com/source/freeradius/freeradius-11/freeradius/src/lib/hmac.c +void CSHA1::HMAC(unsigned char *sharedKey, int sharedKeyLength, unsigned char *data, int dataLength, unsigned char output[SHA1_LENGTH]) +{ + // 1. Append zeros to the end of K to create a 64 byte string + static const int sha1BlockLength=64; + + if (sharedKeyLength > sha1BlockLength) + sharedKeyLength = sha1BlockLength; + + // ipad = the byte 0x36 repeated 64 times + // opad = the byte 0x5C repeated 64 times + unsigned char keyWithIpad[sha1BlockLength]; + unsigned char keyWithOpad[sha1BlockLength]; + + memset( keyWithIpad, 0, sizeof(keyWithIpad)); + memset( keyWithOpad, 0, sizeof(keyWithOpad)); + memcpy( keyWithIpad, sharedKey, sharedKeyLength); + memcpy( keyWithOpad, sharedKey, sharedKeyLength); + + for (int i = 0; i < sha1BlockLength; i++) { + keyWithIpad[i] ^= 0x36; + keyWithOpad[i] ^= 0x5c; + } + + // 3. Append the data stream Text to the 64 byte string resulting from step (2) + // 4. Apply H to the stream generated in step (3) + CSHA1 firstHash; + firstHash.Reset(); + firstHash.Update( keyWithIpad, sha1BlockLength ); + firstHash.Update( data, dataLength ); + firstHash.Final(); + + // 6. Append the H (hash) result from step (4) to the 64 byte string resulting from step (5) + // 7. Apply H to the stream generated in step (6) and output the result + CSHA1 secondHash; + secondHash.Reset(); + secondHash.Update( keyWithOpad, sha1BlockLength ); + secondHash.Update( firstHash.GetHash(), SHA1_LENGTH ); + secondHash.Final(); + + memcpy(output, secondHash.GetHash(), SHA1_LENGTH); + + // char report[128]; + // memset(report,0,128); + // secondHash.ReportHash( report, 0 ); +} + +#pragma warning(pop) diff --git a/Source/DR_SHA1.h b/Source/DR_SHA1.h index 41e6fd6d2..2d5128c24 100644 --- a/Source/DR_SHA1.h +++ b/Source/DR_SHA1.h @@ -1,301 +1,299 @@ -/* - 100% free public domain implementation of the SHA-1 algorithm - by Dominik Reichl - Web: http://www.dominik-reichl.de/ - - Version 2.1 - 2012-06-19 - - Deconstructor (resetting internal variables) is now only - implemented if SHA1_WIPE_VARIABLES is defined (which is the - default). - - Renamed inclusion guard to contain a GUID. - - Demo application is now using C++/STL objects and functions. - - Unicode build of the demo application now outputs the hashes of both - the ANSI and Unicode representations of strings. - - Various other demo application improvements. - - Version 2.0 - 2012-06-14 - - Added 'limits.h' include. - - Renamed inclusion guard and macros for compliancy (names beginning - with an underscore are reserved). - - Version 1.9 - 2011-11-10 - - Added Unicode test vectors. - - Improved support for hashing files using the HashFile method that - are larger than 4 GB. - - Improved file hashing performance (by using a larger buffer). - - Disabled unnecessary compiler warnings. - - Internal variables are now private. - - Version 1.8 - 2009-03-16 - - Converted project files to Visual Studio 2008 format. - - Added Unicode support for HashFile utility method. - - Added support for hashing files using the HashFile method that are - larger than 2 GB. - - HashFile now returns an error code instead of copying an error - message into the output buffer. - - GetHash now returns an error code and validates the input parameter. - - Added ReportHashStl STL utility method. - - Added REPORT_HEX_SHORT reporting mode. - - Improved Linux compatibility of test program. - - Version 1.7 - 2006-12-21 - - Fixed buffer underrun warning that appeared when compiling with - Borland C Builder (thanks to Rex Bloom and Tim Gallagher for the - patch). - - Breaking change: ReportHash writes the final hash to the start - of the buffer, i.e. it's not appending it to the string anymore. - - Made some function parameters const. - - Added Visual Studio 2005 project files to demo project. - - Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches) - - You can set the endianness in your files, no need to modify the - header file of the CSHA1 class anymore. - - Aligned data support. - - Made support/compilation of the utility functions (ReportHash and - HashFile) optional (useful when bytes count, for example in embedded - environments). - - Version 1.5 - 2005-01-01 - - 64-bit compiler compatibility added. - - Made variable wiping optional (define SHA1_WIPE_VARIABLES). - - Removed unnecessary variable initializations. - - ROL32 improvement for the Microsoft compiler (using _rotl). - - Version 1.4 - 2004-07-22 - - CSHA1 now compiles fine with GCC 3.3 under Mac OS X (thanks to Larry - Hastings). - - Version 1.3 - 2003-08-17 - - Fixed a small memory bug and made a buffer array a class member to - ensure correct working when using multiple CSHA1 class instances at - one time. - - Version 1.2 - 2002-11-16 - - Borlands C++ compiler seems to have problems with string addition - using sprintf. Fixed the bug which caused the digest report function - not to work properly. CSHA1 is now Borland compatible. - - Version 1.1 - 2002-10-11 - - Removed two unnecessary header file includes and changed BOOL to - bool. Fixed some minor bugs in the web page contents. - - Version 1.0 - 2002-06-20 - - First official release. - - ================ Test Vectors ================ - - SHA1("abc" in ANSI) = - A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D - SHA1("abc" in Unicode LE) = - 9F04F41A 84851416 2050E3D6 8C1A7ABB 441DC2B5 - - SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" - in ANSI) = - 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 - SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" - in Unicode LE) = - 51D7D876 9AC72C40 9C5B0E3F 69C60ADC 9A039014 - - SHA1(A million repetitions of "a" in ANSI) = - 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F - SHA1(A million repetitions of "a" in Unicode LE) = - C4609560 A108A0C6 26AA7F2B 38A65566 739353C5 -*/ - -#ifndef SHA1_H_A545E61D43E9404E8D736869AB3CBFE7 -#define SHA1_H_A545E61D43E9404E8D736869AB3CBFE7 - -// KevinJ: -#include "RakMemoryOverride.h" -#include // Needed for file access - -#include // Needed for memset and memcpy - -#include // Needed for strcat and strcpy -#include "Export.h" -//#define MAX_FILE_READ_BUFFER 8000 -#define SHA1_LENGTH 20 - - - -#if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS) -#define SHA1_UTILITY_FUNCTIONS -#endif - -#if !defined(SHA1_STL_FUNCTIONS) && !defined(SHA1_NO_STL_FUNCTIONS) -#define SHA1_STL_FUNCTIONS -#if !defined(SHA1_UTILITY_FUNCTIONS) -#error STL functions require SHA1_UTILITY_FUNCTIONS. -#endif -#endif - - -#include - -#include - -#ifdef SHA1_UTILITY_FUNCTIONS -#include -#include -#endif - -#ifdef SHA1_STL_FUNCTIONS -#include -#endif - -#ifdef _MSC_VER -#include -#endif - -// You can define the endian mode in your files without modifying the SHA-1 -// source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN -// in your files, before including the DR_SHA1.h header file. If you don't -// define anything, the class defaults to little endian. -#if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN) -#define SHA1_LITTLE_ENDIAN -#endif - -// If you want variable wiping, #define SHA1_WIPE_VARIABLES, if not, -// #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it -// defaults to wiping. -#if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES) -#define SHA1_WIPE_VARIABLES -#endif - -#if defined(SHA1_HAS_TCHAR) -#include -#else -#ifdef _MSC_VER -#include -#else -#ifndef TCHAR -#define TCHAR char -#endif -#ifndef _T -#define _T(__x) (__x) -#define _tmain main -#define _tprintf printf -#define _getts gets -#define _tcslen strlen -#define _tfopen fopen -#define _tcscpy strcpy -#define _tcscat strcat -#define _sntprintf snprintf -#endif -#endif -#endif - -/////////////////////////////////////////////////////////////////////////// -// Define variable types - -#ifndef UINT_8 -#ifdef _MSC_VER // Compiling with Microsoft compiler -#define UINT_8 unsigned __int8 -#else // !_MSC_VER -#define UINT_8 unsigned char -#endif // _MSC_VER -#endif - -#ifndef UINT_32 -#ifdef _MSC_VER // Compiling with Microsoft compiler -#define UINT_32 unsigned __int32 -#else // !_MSC_VER -#if (ULONG_MAX == 0xFFFFFFFFUL) -#define UINT_32 unsigned long -#else -#define UINT_32 unsigned int -#endif -#endif // _MSC_VER -#endif // UINT_32 - -#ifndef INT_64 -#ifdef _MSC_VER // Compiling with Microsoft compiler -#define INT_64 __int64 -#else // !_MSC_VER -#define INT_64 long long -#endif // _MSC_VER -#endif // INT_64 - -#ifndef UINT_64 -#ifdef _MSC_VER // Compiling with Microsoft compiler -#define UINT_64 unsigned __int64 -#else // !_MSC_VER -#define UINT_64 unsigned long long -#endif // _MSC_VER -#endif // UINT_64 - -/////////////////////////////////////////////////////////////////////////// -// Declare SHA-1 workspace - -typedef union -{ - UINT_8 c[64]; - UINT_32 l[16]; -} SHA1_WORKSPACE_BLOCK; - -class RAK_DLL_EXPORT CSHA1 -{ -public: -#ifdef SHA1_UTILITY_FUNCTIONS - // Different formats for ReportHash(Stl) - enum REPORT_TYPE - { - REPORT_HEX = 0, - REPORT_DIGIT = 1, - REPORT_HEX_SHORT = 2 - }; -#endif - - // Constructor and destructor - CSHA1(); - -#ifdef SHA1_WIPE_VARIABLES - ~CSHA1(); -#endif - - void Reset(); - - // Hash in binary data and strings - void Update(const UINT_8* pbData, UINT_32 uLen); - -#ifdef SHA1_UTILITY_FUNCTIONS - // Hash in file contents - bool HashFile(const TCHAR* tszFileName); -#endif - - // Finalize hash; call it before using ReportHash(Stl) - void Final(); - -#ifdef SHA1_UTILITY_FUNCTIONS - bool ReportHash(TCHAR* tszReport, REPORT_TYPE rtReportType = REPORT_HEX) const; -#endif - -#ifdef SHA1_STL_FUNCTIONS - bool ReportHashStl(std::basic_string& strOut, REPORT_TYPE rtReportType = - REPORT_HEX) const; -#endif - - // Get the raw message digest (20 bytes) - bool GetHash(UINT_8* pbDest20) const; - -unsigned char * GetHash( void ) const; -// KevinJ: http://cseweb.ucsd.edu/~mihir/papers/hmac-cb.pdf - static void HMAC(unsigned char *sharedKey, int sharedKeyLength, unsigned char *data, int dataLength, unsigned char output[SHA1_LENGTH]); - -private: - // Private SHA-1 transformation - void Transform(UINT_32* pState, const UINT_8* pBuffer); - - // Member variables - UINT_32 m_state[5]; - UINT_32 m_count[2]; - UINT_32 m_reserved0[1]; // Memory alignment padding - UINT_8 m_buffer[64]; - UINT_8 m_digest[20]; - UINT_32 m_reserved1[3]; // Memory alignment padding - - UINT_8 m_workspace[64]; - SHA1_WORKSPACE_BLOCK* m_block; // SHA1 pointer to the byte array above -}; - -#endif // SHA1_H_A545E61D43E9404E8D736869AB3CBFE7 +/* + 100% free public domain implementation of the SHA-1 algorithm + by Dominik Reichl + Web: http://www.dominik-reichl.de/ + + Version 2.1 - 2012-06-19 + - Deconstructor (resetting internal variables) is now only + implemented if SHA1_WIPE_VARIABLES is defined (which is the + default). + - Renamed inclusion guard to contain a GUID. + - Demo application is now using C++/STL objects and functions. + - Unicode build of the demo application now outputs the hashes of both + the ANSI and Unicode representations of strings. + - Various other demo application improvements. + + Version 2.0 - 2012-06-14 + - Added 'limits.h' include. + - Renamed inclusion guard and macros for compliancy (names beginning + with an underscore are reserved). + + Version 1.9 - 2011-11-10 + - Added Unicode test vectors. + - Improved support for hashing files using the HashFile method that + are larger than 4 GB. + - Improved file hashing performance (by using a larger buffer). + - Disabled unnecessary compiler warnings. + - Internal variables are now private. + + Version 1.8 - 2009-03-16 + - Converted project files to Visual Studio 2008 format. + - Added Unicode support for HashFile utility method. + - Added support for hashing files using the HashFile method that are + larger than 2 GB. + - HashFile now returns an error code instead of copying an error + message into the output buffer. + - GetHash now returns an error code and validates the input parameter. + - Added ReportHashStl STL utility method. + - Added REPORT_HEX_SHORT reporting mode. + - Improved Linux compatibility of test program. + + Version 1.7 - 2006-12-21 + - Fixed buffer underrun warning that appeared when compiling with + Borland C Builder (thanks to Rex Bloom and Tim Gallagher for the + patch). + - Breaking change: ReportHash writes the final hash to the start + of the buffer, i.e. it's not appending it to the string anymore. + - Made some function parameters const. + - Added Visual Studio 2005 project files to demo project. + + Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches) + - You can set the endianness in your files, no need to modify the + header file of the CSHA1 class anymore. + - Aligned data support. + - Made support/compilation of the utility functions (ReportHash and + HashFile) optional (useful when bytes count, for example in embedded + environments). + + Version 1.5 - 2005-01-01 + - 64-bit compiler compatibility added. + - Made variable wiping optional (define SHA1_WIPE_VARIABLES). + - Removed unnecessary variable initializations. + - ROL32 improvement for the Microsoft compiler (using _rotl). + + Version 1.4 - 2004-07-22 + - CSHA1 now compiles fine with GCC 3.3 under Mac OS X (thanks to Larry + Hastings). + + Version 1.3 - 2003-08-17 + - Fixed a small memory bug and made a buffer array a class member to + ensure correct working when using multiple CSHA1 class instances at + one time. + + Version 1.2 - 2002-11-16 + - Borlands C++ compiler seems to have problems with string addition + using sprintf. Fixed the bug which caused the digest report function + not to work properly. CSHA1 is now Borland compatible. + + Version 1.1 - 2002-10-11 + - Removed two unnecessary header file includes and changed BOOL to + bool. Fixed some minor bugs in the web page contents. + + Version 1.0 - 2002-06-20 + - First official release. + + ================ Test Vectors ================ + + SHA1("abc" in ANSI) = + A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D + SHA1("abc" in Unicode LE) = + 9F04F41A 84851416 2050E3D6 8C1A7ABB 441DC2B5 + + SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + in ANSI) = + 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 + SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + in Unicode LE) = + 51D7D876 9AC72C40 9C5B0E3F 69C60ADC 9A039014 + + SHA1(A million repetitions of "a" in ANSI) = + 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F + SHA1(A million repetitions of "a" in Unicode LE) = + C4609560 A108A0C6 26AA7F2B 38A65566 739353C5 +*/ + +#pragma once + +// KevinJ: +#include "RakMemoryOverride.h" +#include // Needed for file access + +#include // Needed for memset and memcpy + +#include // Needed for strcat and strcpy +#include "Export.h" +//#define MAX_FILE_READ_BUFFER 8000 +#define SHA1_LENGTH 20 + + + +#if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS) +#define SHA1_UTILITY_FUNCTIONS +#endif + +#if !defined(SHA1_STL_FUNCTIONS) && !defined(SHA1_NO_STL_FUNCTIONS) +#define SHA1_STL_FUNCTIONS +#if !defined(SHA1_UTILITY_FUNCTIONS) +#error STL functions require SHA1_UTILITY_FUNCTIONS. +#endif +#endif + + +#include + +#include + +#ifdef SHA1_UTILITY_FUNCTIONS +#include +#include +#endif + +#ifdef SHA1_STL_FUNCTIONS +#include +#endif + +#ifdef _MSC_VER +#include +#endif + +// You can define the endian mode in your files without modifying the SHA-1 +// source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN +// in your files, before including the DR_SHA1.h header file. If you don't +// define anything, the class defaults to little endian. +#if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN) +#define SHA1_LITTLE_ENDIAN +#endif + +// If you want variable wiping, #define SHA1_WIPE_VARIABLES, if not, +// #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it +// defaults to wiping. +#if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES) +#define SHA1_WIPE_VARIABLES +#endif + +#if defined(SHA1_HAS_TCHAR) +#include +#else +#ifdef _MSC_VER +#include +#else +#ifndef TCHAR +#define TCHAR char +#endif +#ifndef _T +#define _T(__x) (__x) +#define _tmain main +#define _tprintf printf +#define _getts gets +#define _tcslen strlen +#define _tfopen fopen +#define _tcscpy strcpy +#define _tcscat strcat +#define _sntprintf snprintf +#endif +#endif +#endif + +/////////////////////////////////////////////////////////////////////////// +// Define variable types + +#ifndef UINT_8 +#ifdef _MSC_VER // Compiling with Microsoft compiler +#define UINT_8 unsigned __int8 +#else // !_MSC_VER +#define UINT_8 unsigned char +#endif // _MSC_VER +#endif + +#ifndef UINT_32 +#ifdef _MSC_VER // Compiling with Microsoft compiler +#define UINT_32 unsigned __int32 +#else // !_MSC_VER +#if (ULONG_MAX == 0xFFFFFFFFUL) +#define UINT_32 unsigned long +#else +#define UINT_32 unsigned int +#endif +#endif // _MSC_VER +#endif // UINT_32 + +#ifndef INT_64 +#ifdef _MSC_VER // Compiling with Microsoft compiler +#define INT_64 __int64 +#else // !_MSC_VER +#define INT_64 long long +#endif // _MSC_VER +#endif // INT_64 + +#ifndef UINT_64 +#ifdef _MSC_VER // Compiling with Microsoft compiler +#define UINT_64 unsigned __int64 +#else // !_MSC_VER +#define UINT_64 unsigned long long +#endif // _MSC_VER +#endif // UINT_64 + +/////////////////////////////////////////////////////////////////////////// +// Declare SHA-1 workspace + +typedef union +{ + UINT_8 c[64]; + UINT_32 l[16]; +} SHA1_WORKSPACE_BLOCK; + +class RAK_DLL_EXPORT CSHA1 +{ +public: +#ifdef SHA1_UTILITY_FUNCTIONS + // Different formats for ReportHash(Stl) + enum REPORT_TYPE + { + REPORT_HEX = 0, + REPORT_DIGIT = 1, + REPORT_HEX_SHORT = 2 + }; +#endif + + // Constructor and destructor + CSHA1(); + +#ifdef SHA1_WIPE_VARIABLES + ~CSHA1(); +#endif + + void Reset(); + + // Hash in binary data and strings + void Update(const UINT_8* pbData, UINT_32 uLen); + +#ifdef SHA1_UTILITY_FUNCTIONS + // Hash in file contents + bool HashFile(const TCHAR* tszFileName); +#endif + + // Finalize hash; call it before using ReportHash(Stl) + void Final(); + +#ifdef SHA1_UTILITY_FUNCTIONS + bool ReportHash(TCHAR* tszReport, REPORT_TYPE rtReportType = REPORT_HEX) const; +#endif + +#ifdef SHA1_STL_FUNCTIONS + bool ReportHashStl(std::basic_string& strOut, REPORT_TYPE rtReportType = + REPORT_HEX) const; +#endif + + // Get the raw message digest (20 bytes) + bool GetHash(UINT_8* pbDest20) const; + +unsigned char * GetHash( void ) const; +// KevinJ: http://cseweb.ucsd.edu/~mihir/papers/hmac-cb.pdf + static void HMAC(unsigned char *sharedKey, int sharedKeyLength, unsigned char *data, int dataLength, unsigned char output[SHA1_LENGTH]); + +private: + // Private SHA-1 transformation + void Transform(UINT_32* pState, const UINT_8* pBuffer); + + // Member variables + UINT_32 m_state[5]; + UINT_32 m_count[2]; + UINT_32 m_reserved0[1]; // Memory alignment padding + UINT_8 m_buffer[64]; + UINT_8 m_digest[20]; + UINT_32 m_reserved1[3]; // Memory alignment padding + + UINT_8 m_workspace[64]; + SHA1_WORKSPACE_BLOCK* m_block; // SHA1 pointer to the byte array above +}; + diff --git a/Source/DS_BPlusTree.h b/Source/DS_BPlusTree.h index 8211b6e5c..404349c6c 100644 --- a/Source/DS_BPlusTree.h +++ b/Source/DS_BPlusTree.h @@ -1,1154 +1,1152 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_BPlusTree.h -/// - - -#ifndef __B_PLUS_TREE_CPP -#define __B_PLUS_TREE_CPP - -#include "DS_MemoryPool.h" -#include "DS_Queue.h" -#include -#include "Export.h" - -// Java -// http://www.seanster.com/BplusTree/BplusTree.html - -// Overview -// http://babbage.clarku.edu/~achou/cs160/B+Trees/B+Trees.htm - -// Deletion -// http://dbpubs.stanford.edu:8090/pub/1995-19 - -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -#include "RakMemoryOverride.h" - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - /// Used in the BPlusTree. Used for both leaf and index nodes. - /// Don't use a constructor or destructor, due to the memory pool I am using - template - struct RAK_DLL_EXPORT Page - { - // We use the same data structure for both leaf and index nodes. - // It uses a little more memory for index nodes but reduces - // memory fragmentation, allocations, and deallocations. - bool isLeaf; - - // Used for both leaf and index nodes. - // For a leaf it means the number of elements in data - // For an index it means the number of keys and is one less than the number of children pointers. - int size; - - // Used for both leaf and index nodes. - KeyType keys[order]; - - // Used only for leaf nodes. Data is the actual data, while next is the pointer to the next leaf (for B+) - DataType data[order]; - Page *next; - Page *previous; - - // Used only for index nodes. Pointers to the children of this node. - Page *children[order+1]; - }; - - /// A BPlus tree - /// Written with efficiency and speed in mind. - template - class RAK_DLL_EXPORT BPlusTree - { - public: - struct ReturnAction - { - KeyType key1; - KeyType key2; - enum - { - NO_ACTION, - REPLACE_KEY1_WITH_KEY2, - PUSH_KEY_TO_PARENT, - SET_BRANCH_KEY, - } action; // 0=none, 1=replace key1 with key2 - }; - - BPlusTree(); - ~BPlusTree(); - void SetPoolPageSize(int size); // Set the page size for the memory pool. Optionsl - bool Get(const KeyType key, DataType &out) const; - bool Delete(const KeyType key); - bool Delete(const KeyType key, DataType &out); - bool Insert(const KeyType key, const DataType &data); - void Clear(void); - unsigned Size(void) const; - bool IsEmpty(void) const; - Page *GetListHead(void) const; - DataType GetDataHead(void) const; - void PrintLeaves(void); - void ForEachLeaf(void (*func)(Page * leaf, int index)); - void ForEachData(void (*func)(DataType input, int index)); - void PrintGraph(void); - protected: - void ValidateTreeRecursive(Page *cur); - void DeleteFromPageAtIndex(const int index, Page *cur); - static void PrintLeaf(Page * leaf, int index); - void FreePages(void); - bool GetIndexOf(const KeyType key, Page *page, int *out) const; - void ShiftKeysLeft(Page *cur); - bool CanRotateLeft(Page *cur, int childIndex); - bool CanRotateRight(Page *cur, int childIndex); - void RotateRight(Page *cur, int childIndex, ReturnAction *returnAction); - void RotateLeft(Page *cur, int childIndex, ReturnAction *returnAction); - Page* InsertIntoNode(const KeyType key, const DataType &childData, int insertionIndex, Page *nodeData, Page *cur, ReturnAction* returnAction); - Page* InsertBranchDown(const KeyType key, const DataType &data,Page *cur, ReturnAction* returnAction, bool *success); - Page* GetLeafFromKey(const KeyType key) const; - bool FindDeleteRebalance(const KeyType key, Page *cur, bool *underflow, KeyType rightRootKey, ReturnAction *returnAction, DataType &out); - bool FixUnderflow(int branchIndex, Page *cur, KeyType rightRootKey, ReturnAction *returnAction); - void ShiftNodeLeft(Page *cur); - void ShiftNodeRight(Page *cur); - - MemoryPool > pagePool; - Page *root, *leftmostLeaf; - }; - - template - BPlusTree::BPlusTree () - { - RakAssert(order>1); - root=0; - leftmostLeaf=0; - } - template - BPlusTree::~BPlusTree () - { - Clear(); - } - template - void BPlusTree::SetPoolPageSize(int size) - { - pagePool.SetPageSize(size); - } - template - bool BPlusTree::Get(const KeyType key, DataType &out) const - { - if (root==0) - return false; - - Page* leaf = GetLeafFromKey(key); - int childIndex; - - if (GetIndexOf(key, leaf, &childIndex)) - { - out=leaf->data[childIndex]; - return true; - } - return false; - } - template - void BPlusTree::DeleteFromPageAtIndex(const int index, Page *cur) - { - int i; - for (i=index; i < cur->size-1; i++) - cur->keys[i]=cur->keys[i+1]; - if (cur->isLeaf) - { - for (i=index; i < cur->size-1; i++) - cur->data[i]=cur->data[i+1]; - } - else - { - for (i=index; i < cur->size-1; i++) - cur->children[i+1]=cur->children[i+2]; - } - cur->size--; - } - template - bool BPlusTree::Delete(const KeyType key) - { - DataType temp; - return Delete(key, temp); - } - template - bool BPlusTree::Delete(const KeyType key, DataType &out) - { - if (root==0) - return false; - - ReturnAction returnAction; - returnAction.action=ReturnAction::NO_ACTION; - int childIndex; - bool underflow=false; - if (root==leftmostLeaf) - { - if (GetIndexOf(key, root, &childIndex)==false) - return false; - out=root->data[childIndex]; - DeleteFromPageAtIndex(childIndex,root); - if (root->size==0) - { - pagePool.Release(root, _FILE_AND_LINE_); - root=0; - leftmostLeaf=0; - } - return true; - } - else if (FindDeleteRebalance(key, root, &underflow,root->keys[0], &returnAction, out)==false) - return false; - -// RakAssert(returnAction.action==ReturnAction::NO_ACTION); - - if (underflow && root->size==0) - { - // Move the root down. - Page *oldRoot=root; - root=root->children[0]; - pagePool.Release(oldRoot, _FILE_AND_LINE_); - // memset(oldRoot,0,sizeof(root)); - } - - return true; - } - template - bool BPlusTree::FindDeleteRebalance(const KeyType key, Page *cur, bool *underflow, KeyType rightRootKey, ReturnAction *returnAction, DataType &out) - { - // Get index of child to follow. - int branchIndex, childIndex; - if (GetIndexOf(key, cur, &childIndex)) - branchIndex=childIndex+1; - else - branchIndex=childIndex; - - // If child is not a leaf, call recursively - if (cur->children[branchIndex]->isLeaf==false) - { - if (branchIndexsize) - rightRootKey=cur->keys[branchIndex]; // Shift right to left - else - rightRootKey=cur->keys[branchIndex-1]; // Shift center to left - - if (FindDeleteRebalance(key, cur->children[branchIndex], underflow, rightRootKey, returnAction, out)==false) - return false; - - // Call again in case the root key changed - if (branchIndexsize) - rightRootKey=cur->keys[branchIndex]; // Shift right to left - else - rightRootKey=cur->keys[branchIndex-1]; // Shift center to left - - if (returnAction->action==ReturnAction::SET_BRANCH_KEY && branchIndex!=childIndex) - { - returnAction->action=ReturnAction::NO_ACTION; - cur->keys[childIndex]=returnAction->key1; - - if (branchIndexsize) - rightRootKey=cur->keys[branchIndex]; // Shift right to left - else - rightRootKey=cur->keys[branchIndex-1]; // Shift center to left - } - } - else - { - // If child is a leaf, get the index of the key. If the item is not found, cancel delete. - if (GetIndexOf(key, cur->children[branchIndex], &childIndex)==false) - return false; - - // Delete: - // Remove childIndex from the child at branchIndex - out=cur->children[branchIndex]->data[childIndex]; - DeleteFromPageAtIndex(childIndex, cur->children[branchIndex]); - - if (childIndex==0) - { - if (branchIndex>0) - cur->keys[branchIndex-1]=cur->children[branchIndex]->keys[0]; - - if (branchIndex==0) - { - returnAction->action=ReturnAction::SET_BRANCH_KEY; - returnAction->key1=cur->children[0]->keys[0]; - } - } - - if (cur->children[branchIndex]->size < order/2) - *underflow=true; - else - *underflow=false; - } - - // Fix underflow: - if (*underflow) - { - *underflow=FixUnderflow(branchIndex, cur, rightRootKey, returnAction); - } - - return true; - } - template - bool BPlusTree::FixUnderflow(int branchIndex, Page *cur, KeyType rightRootKey, ReturnAction *returnAction) - { - // Borrow from a neighbor that has excess. - Page *source; - Page *dest; - - if (branchIndex>0 && cur->children[branchIndex-1]->size > order/2) - { - dest=cur->children[branchIndex]; - source=cur->children[branchIndex-1]; - - // Left has excess - ShiftNodeRight(dest); - if (dest->isLeaf) - { - dest->keys[0]=source->keys[source->size-1]; - dest->data[0]=source->data[source->size-1]; - } - else - { - dest->children[0]=source->children[source->size]; - dest->keys[0]=cur->keys[branchIndex-1]; - } - // Update the parent key for the child (middle) - cur->keys[branchIndex-1]=source->keys[source->size-1]; - source->size--; - - // if (branchIndex==0) - // { - // returnAction->action=ReturnAction::SET_BRANCH_KEY; - // returnAction->key1=dest->keys[0]; - // } - - // No underflow - return false; - } - else if (branchIndexsize && cur->children[branchIndex+1]->size > order/2) - { - dest=cur->children[branchIndex]; - source=cur->children[branchIndex+1]; - - // Right has excess - if (dest->isLeaf) - { - dest->keys[dest->size]=source->keys[0]; - dest->data[dest->size]=source->data[0]; - - // The first key in the leaf after shifting is the parent key for the right branch - cur->keys[branchIndex]=source->keys[1]; - -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - if (order<=3 && dest->size==0) - { - if (branchIndex==0) - { - returnAction->action=ReturnAction::SET_BRANCH_KEY; - returnAction->key1=dest->keys[0]; - } - else - cur->keys[branchIndex-1]=cur->children[branchIndex]->keys[0]; - } - } - else - { - if (returnAction->action==ReturnAction::NO_ACTION) - { - returnAction->action=ReturnAction::SET_BRANCH_KEY; - returnAction->key1=dest->keys[0]; - } - - dest->keys[dest->size]=rightRootKey; - dest->children[dest->size+1]=source->children[0]; - - // The shifted off key is the leftmost key for a node - cur->keys[branchIndex]=source->keys[0]; - } - - - dest->size++; - ShiftNodeLeft(source); - - //cur->keys[branchIndex]=source->keys[0]; - -// returnAction->action=ReturnAction::SET_BRANCH_KEY; -// returnAction->key1=dest->keys[dest->size-1]; - - // No underflow - return false; - } - else - { - int sourceIndex; - - // If no neighbors have excess, merge two branches. - // - // To merge two leaves, just copy the data and keys over. - // - // To merge two branches, copy the pointers and keys over, using rightRootKey as the key for the extra pointer - if (branchIndexsize) - { - // Merge right child to current child and delete right child. - dest=cur->children[branchIndex]; - source=cur->children[branchIndex+1]; - } - else - { - // Move current child to left and delete current child - dest=cur->children[branchIndex-1]; - source=cur->children[branchIndex]; - } - - // Merge - if (dest->isLeaf) - { - for (sourceIndex=0; sourceIndexsize; sourceIndex++) - { - dest->keys[dest->size]=source->keys[sourceIndex]; - dest->data[dest->size++]=source->data[sourceIndex]; - } - } - else - { - // We want the tree root key of the source, not the current. - dest->keys[dest->size]=rightRootKey; - dest->children[dest->size++ + 1]=source->children[0]; - for (sourceIndex=0; sourceIndexsize; sourceIndex++) - { - dest->keys[dest->size]=source->keys[sourceIndex]; - dest->children[dest->size++ + 1]=source->children[sourceIndex + 1]; - } - } - -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - if (order<=3 && branchIndex>0 && cur->children[branchIndex]->isLeaf) // With order==2 it is possible to delete data[0], which is not possible with higher orders. - cur->keys[branchIndex-1]=cur->children[branchIndex]->keys[0]; - - if (branchIndexsize) - { - // Update the parent key, removing the source (right) - DeleteFromPageAtIndex(branchIndex, cur); - } - else - { - if (branchIndex>0) - { - // Update parent key, removing the source (current) - DeleteFromPageAtIndex(branchIndex-1, cur); - } - } - - if (branchIndex==0 && dest->isLeaf) - { - returnAction->action=ReturnAction::SET_BRANCH_KEY; - returnAction->key1=dest->keys[0]; - } - - if (source==leftmostLeaf) - leftmostLeaf=source->next; - - if (source->isLeaf) - { - if (source->previous) - source->previous->next=source->next; - if (source->next) - source->next->previous=source->previous; - } - - // Free the source node - pagePool.Release(source, _FILE_AND_LINE_); - // memset(source,0,sizeof(root)); - - // Return underflow or not of parent. - return cur->size < order/2; - } - } - template - void BPlusTree::ShiftNodeRight(Page *cur) - { - int i; - for (i=cur->size; i>0; i--) - cur->keys[i]=cur->keys[i-1]; - if (cur->isLeaf) - { - for (i=cur->size; i>0; i--) - cur->data[i]=cur->data[i-1]; - } - else - { - for (i=cur->size+1; i>0; i--) - cur->children[i]=cur->children[i-1]; - } - - cur->size++; - } - template - void BPlusTree::ShiftNodeLeft(Page *cur) - { - int i; - for (i=0; i < cur->size-1; i++) - cur->keys[i]=cur->keys[i+1]; - if (cur->isLeaf) - { - for (i=0; i < cur->size; i++) - cur->data[i]=cur->data[i+1]; - } - else - { - for (i=0; i < cur->size; i++) - cur->children[i]=cur->children[i+1]; - } - cur->size--; - } - template - Page* BPlusTree::InsertIntoNode(const KeyType key, const DataType &leafData, int insertionIndex, Page *nodeData, Page *cur, ReturnAction* returnAction) - { - int i; - if (cur->size < order) - { - for (i=cur->size; i > insertionIndex; i--) - cur->keys[i]=cur->keys[i-1]; - if (cur->isLeaf) - { - for (i=cur->size; i > insertionIndex; i--) - cur->data[i]=cur->data[i-1]; - } - else - { - for (i=cur->size+1; i > insertionIndex+1; i--) - cur->children[i]=cur->children[i-1]; - } - cur->keys[insertionIndex]=key; - if (cur->isLeaf) - cur->data[insertionIndex]=leafData; - else - cur->children[insertionIndex+1]=nodeData; - - cur->size++; - } - else - { - Page* newPage = pagePool.Allocate( _FILE_AND_LINE_ ); - newPage->isLeaf=cur->isLeaf; - if (cur->isLeaf) - { - newPage->next=cur->next; - if (cur->next) - cur->next->previous=newPage; - newPage->previous=cur; - cur->next=newPage; - } - - int destIndex, sourceIndex; - - if (insertionIndex>=(order+1)/2) - { - destIndex=0; - sourceIndex=order/2; - - for (; sourceIndex < insertionIndex; sourceIndex++, destIndex++) - { - newPage->keys[destIndex]=cur->keys[sourceIndex]; - } - newPage->keys[destIndex++]=key; - for (; sourceIndex < order; sourceIndex++, destIndex++) - { - newPage->keys[destIndex]=cur->keys[sourceIndex]; - } - - destIndex=0; - sourceIndex=order/2; - if (cur->isLeaf) - { - for (; sourceIndex < insertionIndex; sourceIndex++, destIndex++) - { - newPage->data[destIndex]=cur->data[sourceIndex]; - } - newPage->data[destIndex++]=leafData; - for (; sourceIndex < order; sourceIndex++, destIndex++) - { - newPage->data[destIndex]=cur->data[sourceIndex]; - } - } - else - { - - for (; sourceIndex < insertionIndex; sourceIndex++, destIndex++) - { - newPage->children[destIndex]=cur->children[sourceIndex+1]; - } - newPage->children[destIndex++]=nodeData; - - // sourceIndex+1 is sort of a hack but it works - because there is one extra child than keys - // skip past the last child for cur - for (; sourceIndex+1 < cur->size+1; sourceIndex++, destIndex++) - { - newPage->children[destIndex]=cur->children[sourceIndex+1]; - } - - // the first key is the middle key. Remove it from the page and push it to the parent - returnAction->action=ReturnAction::PUSH_KEY_TO_PARENT; - returnAction->key1=newPage->keys[0]; - for (int j=0; j < destIndex-1; j++) - newPage->keys[j]=newPage->keys[j+1]; - - } - cur->size=order/2; - } - else - { - destIndex=0; - sourceIndex=(order+1)/2-1; - for (; sourceIndex < order; sourceIndex++, destIndex++) - newPage->keys[destIndex]=cur->keys[sourceIndex]; - destIndex=0; - if (cur->isLeaf) - { - sourceIndex=(order+1)/2-1; - for (; sourceIndex < order; sourceIndex++, destIndex++) - newPage->data[destIndex]=cur->data[sourceIndex]; - } - else - { - sourceIndex=(order+1)/2; - for (; sourceIndex < order+1; sourceIndex++, destIndex++) - newPage->children[destIndex]=cur->children[sourceIndex]; - - // the first key is the middle key. Remove it from the page and push it to the parent - returnAction->action=ReturnAction::PUSH_KEY_TO_PARENT; - returnAction->key1=newPage->keys[0]; - for (int j=0; j < destIndex-1; j++) - newPage->keys[j]=newPage->keys[j+1]; - } - cur->size=(order+1)/2-1; - if (cur->size) - { - bool b = GetIndexOf(key, cur, &insertionIndex); - (void) b; - RakAssert(b==false); - } - else - insertionIndex=0; - InsertIntoNode(key, leafData, insertionIndex, nodeData, cur, returnAction); - } - - newPage->size=destIndex; - - return newPage; - } - - return 0; - } - - template - bool BPlusTree::CanRotateLeft(Page *cur, int childIndex) - { - return childIndex>0 && cur->children[childIndex-1]->size - void BPlusTree::RotateLeft(Page *cur, int childIndex, ReturnAction *returnAction) - { - Page *dest = cur->children[childIndex-1]; - Page *source = cur->children[childIndex]; - returnAction->key1=source->keys[0]; - dest->keys[dest->size]=source->keys[0]; - dest->data[dest->size]=source->data[0]; - dest->size++; - for (int i=0; i < source->size-1; i++) - { - source->keys[i]=source->keys[i+1]; - source->data[i]=source->data[i+1]; - } - source->size--; - cur->keys[childIndex-1]=source->keys[0]; - returnAction->key2=source->keys[0]; - } - - template - bool BPlusTree::CanRotateRight(Page *cur, int childIndex) - { - return childIndex < cur->size && cur->children[childIndex+1]->size - void BPlusTree::RotateRight(Page *cur, int childIndex, ReturnAction *returnAction) - { - Page *dest = cur->children[childIndex+1]; - Page *source = cur->children[childIndex]; - returnAction->key1=dest->keys[0]; - for (int i= dest->size; i > 0; i--) - { - dest->keys[i]=dest->keys[i-1]; - dest->data[i]=dest->data[i-1]; - } - dest->keys[0]=source->keys[source->size-1]; - dest->data[0]=source->data[source->size-1]; - dest->size++; - source->size--; - - cur->keys[childIndex]=dest->keys[0]; - returnAction->key2=dest->keys[0]; - } - template - Page* BPlusTree::GetLeafFromKey(const KeyType key) const - { - Page* cur = root; - int childIndex; - while (cur->isLeaf==false) - { - // When searching, if we match the exact key we go down the pointer after that index - if (GetIndexOf(key, cur, &childIndex)) - childIndex++; - cur = cur->children[childIndex]; - } - return cur; - } - - template - Page* BPlusTree::InsertBranchDown(const KeyType key, const DataType &data,Page *cur, ReturnAction *returnAction, bool *success) - { - int childIndex; - int branchIndex; - if (GetIndexOf(key, cur, &childIndex)) - branchIndex=childIndex+1; - else - branchIndex=childIndex; - Page* newPage; - if (cur->isLeaf==false) - { - if (cur->children[branchIndex]->isLeaf==true && cur->children[branchIndex]->size==order) - { - if (branchIndex==childIndex+1) - { - *success=false; - return 0; // Already exists - } - - if (CanRotateLeft(cur, branchIndex)) - { - returnAction->action=ReturnAction::REPLACE_KEY1_WITH_KEY2; - if (key > cur->children[branchIndex]->keys[0]) - { - RotateLeft(cur, branchIndex, returnAction); - - int insertionIndex; - GetIndexOf(key, cur->children[branchIndex], &insertionIndex); - InsertIntoNode(key, data, insertionIndex, 0, cur->children[branchIndex], 0); - } - else - { - // Move head element to left and replace it with key,data - Page* dest=cur->children[branchIndex-1]; - Page* source=cur->children[branchIndex]; - returnAction->key1=source->keys[0]; - returnAction->key2=key; - dest->keys[dest->size]=source->keys[0]; - dest->data[dest->size]=source->data[0]; - dest->size++; - source->keys[0]=key; - source->data[0]=data; - } - cur->keys[branchIndex-1]=cur->children[branchIndex]->keys[0]; - - return 0; - } - else if (CanRotateRight(cur, branchIndex)) - { - returnAction->action=ReturnAction::REPLACE_KEY1_WITH_KEY2; - - if (key < cur->children[branchIndex]->keys[cur->children[branchIndex]->size-1]) - { - RotateRight(cur, branchIndex, returnAction); - - int insertionIndex; - GetIndexOf(key, cur->children[branchIndex], &insertionIndex); - InsertIntoNode(key, data, insertionIndex, 0, cur->children[branchIndex], 0); - - } - else - { - // Insert to the head of the right leaf instead and change our key - returnAction->key1=cur->children[branchIndex+1]->keys[0]; - InsertIntoNode(key, data, 0, 0, cur->children[branchIndex+1], 0); - returnAction->key2=key; - } - cur->keys[branchIndex]=cur->children[branchIndex+1]->keys[0]; - return 0; - } - } - - newPage=InsertBranchDown(key,data,cur->children[branchIndex], returnAction, success); - if (returnAction->action==ReturnAction::REPLACE_KEY1_WITH_KEY2) - { - if (branchIndex>0 && cur->keys[branchIndex-1]==returnAction->key1) - cur->keys[branchIndex-1]=returnAction->key2; - } - if (newPage) - { - if (newPage->isLeaf==false) - { - RakAssert(returnAction->action==ReturnAction::PUSH_KEY_TO_PARENT); - newPage->size--; - return InsertIntoNode(returnAction->key1, data, branchIndex, newPage, cur, returnAction); - } - else - { - return InsertIntoNode(newPage->keys[0], data, branchIndex, newPage, cur, returnAction); - } - } - } - else - { - if (branchIndex==childIndex+1) - { - *success=false; - return 0; // Already exists - } - else - { - return InsertIntoNode(key, data, branchIndex, 0, cur, returnAction); - } - } - - return 0; - } - template - bool BPlusTree::Insert(const KeyType key, const DataType &data) - { - if (root==0) - { - // Allocate root and make root a leaf - root = pagePool.Allocate( _FILE_AND_LINE_ ); - root->isLeaf=true; - leftmostLeaf=root; - root->size=1; - root->keys[0]=key; - root->data[0]=data; - root->next=0; - root->previous=0; - } - else - { - bool success=true; - ReturnAction returnAction; - returnAction.action=ReturnAction::NO_ACTION; - Page* newPage = InsertBranchDown(key, data, root, &returnAction, &success); - if (success==false) - return false; - if (newPage) - { - KeyType newKey; - if (newPage->isLeaf==false) - { - // One key is pushed up through the stack. I store that at keys[0] but it has to be removed for the page to be correct - RakAssert(returnAction.action==ReturnAction::PUSH_KEY_TO_PARENT); - newKey=returnAction.key1; - newPage->size--; - } - else - newKey = newPage->keys[0]; - // propagate the root - Page* newRoot = pagePool.Allocate( _FILE_AND_LINE_ ); - newRoot->isLeaf=false; - newRoot->size=1; - newRoot->keys[0]=newKey; - newRoot->children[0]=root; - newRoot->children[1]=newPage; - root=newRoot; - } - } - - return true; - } - template - void BPlusTree::ShiftKeysLeft(Page *cur) - { - int i; - for (i=0; i < cur->size; i++) - cur->keys[i]=cur->keys[i+1]; - } - template - void BPlusTree::Clear(void) - { - if (root) - { - FreePages(); - leftmostLeaf=0; - root=0; - } - pagePool.Clear(_FILE_AND_LINE_); - } - template - unsigned BPlusTree::Size(void) const - { - unsigned int count=0; - DataStructures::Page *cur = GetListHead(); - while (cur) - { - count+=cur->size; - cur=cur->next; - } - return count; - } - template - bool BPlusTree::IsEmpty(void) const - { - return root==0; - } - template - bool BPlusTree::GetIndexOf(const KeyType key, Page *page, int *out) const - { - RakAssert(page->size>0); - int index, upperBound, lowerBound; - upperBound=page->size-1; - lowerBound=0; - index = page->size/2; - -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - while (1) - { - if (key==page->keys[index]) - { - *out=index; - return true; - } - else if (keykeys[index]) - upperBound=index-1; - else - lowerBound=index+1; - - index=lowerBound+(upperBound-lowerBound)/2; - - if (lowerBound>upperBound) - { - *out=lowerBound; - return false; // No match - } - } - } - template - void BPlusTree::FreePages(void) - { - DataStructures::Queue *> queue; - DataStructures::Page *ptr; - int i; - queue.Push(root, _FILE_AND_LINE_ ); - while (queue.Size()) - { - ptr=queue.Pop(); - if (ptr->isLeaf==false) - { - for (i=0; i < ptr->size+1; i++) - queue.Push(ptr->children[i], _FILE_AND_LINE_ ); - } - pagePool.Release(ptr, _FILE_AND_LINE_); - // memset(ptr,0,sizeof(root)); - }; - } - template - Page *BPlusTree::GetListHead(void) const - { - return leftmostLeaf; - } - template - DataType BPlusTree::GetDataHead(void) const - { - return leftmostLeaf->data[0]; - } - template - void BPlusTree::ForEachLeaf(void (*func)(Page * leaf, int index)) - { - int count=0; - DataStructures::Page *cur = GetListHead(); - while (cur) - { - func(cur, count++); - cur=cur->next; - } - } - template - void BPlusTree::ForEachData(void (*func)(DataType input, int index)) - { - int count=0,i; - DataStructures::Page *cur = GetListHead(); - while (cur) - { - for (i=0; i < cur->size; i++) - func(cur->data[i], count++); - cur=cur->next; - } - } - template - void BPlusTree::PrintLeaf(Page * leaf, int index) - { - int i; - RAKNET_DEBUG_PRINTF("%i] SELF=%p\n", index+1, leaf); - for (i=0; i < leaf->size; i++) - RAKNET_DEBUG_PRINTF(" %i. %i\n", i+1, leaf->data[i]); - } - template - void BPlusTree::PrintLeaves(void) - { - ForEachLeaf(PrintLeaf); - } - - template - void BPlusTree::ValidateTreeRecursive(Page *cur) - { - RakAssert(cur==root || cur->size>=order/2); - - if (cur->children[0]->isLeaf) - { - RakAssert(cur->children[0]->keys[0] < cur->keys[0]); - for (int i=0; i < cur->size; i++) - { - RakAssert(cur->children[i+1]->keys[0]==cur->keys[i]); - } - } - else - { - for (int i=0; i < cur->size+1; i++) - ValidateTreeRecursive(cur->children[i]); - } - } - - template - void BPlusTree::PrintGraph(void) - { - DataStructures::Queue *> queue; - queue.Push(root,_FILE_AND_LINE_); - queue.Push(0,_FILE_AND_LINE_); - DataStructures::Page *ptr; - int i,j; - if (root) - { - RAKNET_DEBUG_PRINTF("%p(", root); - for (i=0; i < root->size; i++) - { - RAKNET_DEBUG_PRINTF("%i ", root->keys[i]); - } - RAKNET_DEBUG_PRINTF(") "); - RAKNET_DEBUG_PRINTF("\n"); - } - while (queue.Size()) - { - ptr=queue.Pop(); - if (ptr==0) - RAKNET_DEBUG_PRINTF("\n"); - else if (ptr->isLeaf==false) - { - for (i=0; i < ptr->size+1; i++) - { - RAKNET_DEBUG_PRINTF("%p(", ptr->children[i]); - //RAKNET_DEBUG_PRINTF("(", ptr->children[i]); - for (j=0; j < ptr->children[i]->size; j++) - RAKNET_DEBUG_PRINTF("%i ", ptr->children[i]->keys[j]); - RAKNET_DEBUG_PRINTF(") "); - queue.Push(ptr->children[i],_FILE_AND_LINE_); - } - queue.Push(0,_FILE_AND_LINE_); - RAKNET_DEBUG_PRINTF(" -- "); - } - } - RAKNET_DEBUG_PRINTF("\n"); - } -} -#ifdef _MSC_VER -#pragma warning( pop ) -#endif - -#endif - -// Code to test this hellish data structure. -/* -#include "DS_BPlusTree.h" -#include - -// Handle underflow on root. If there is only one item left then I can go downwards. -// Make sure I keep the leftmost pointer valid by traversing it -// When I free a leaf, be sure to adjust the pointers around it. - -#include "Rand.h" - -void main(void) -{ - DataStructures::BPlusTree btree; - DataStructures::List haveList, removedList; - int temp; - int i, j, index; - int testSize; - bool b; - - for (testSize=0; testSize < 514; testSize++) - { - RAKNET_DEBUG_PRINTF("TestSize=%i\n", testSize); - - for (i=0; i < testSize; i++) - haveList.Insert(i); - - for (i=0; i < testSize; i++) - { - index=i+randomMT()%(testSize-i); - temp=haveList[index]; - haveList[index]=haveList[i]; - haveList[i]=temp; - } - - for (i=0; i +#include "Export.h" + +// Java +// http://www.seanster.com/BplusTree/BplusTree.html + +// Overview +// http://babbage.clarku.edu/~achou/cs160/B+Trees/B+Trees.htm + +// Deletion +// http://dbpubs.stanford.edu:8090/pub/1995-19 + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +#include "RakMemoryOverride.h" + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + /// Used in the BPlusTree. Used for both leaf and index nodes. + /// Don't use a constructor or destructor, due to the memory pool I am using + template + struct RAK_DLL_EXPORT Page + { + // We use the same data structure for both leaf and index nodes. + // It uses a little more memory for index nodes but reduces + // memory fragmentation, allocations, and deallocations. + bool isLeaf; + + // Used for both leaf and index nodes. + // For a leaf it means the number of elements in data + // For an index it means the number of keys and is one less than the number of children pointers. + int size; + + // Used for both leaf and index nodes. + KeyType keys[order]; + + // Used only for leaf nodes. Data is the actual data, while next is the pointer to the next leaf (for B+) + DataType data[order]; + Page *next; + Page *previous; + + // Used only for index nodes. Pointers to the children of this node. + Page *children[order+1]; + }; + + /// A BPlus tree + /// Written with efficiency and speed in mind. + template + class RAK_DLL_EXPORT BPlusTree + { + public: + struct ReturnAction + { + KeyType key1; + KeyType key2; + enum + { + NO_ACTION, + REPLACE_KEY1_WITH_KEY2, + PUSH_KEY_TO_PARENT, + SET_BRANCH_KEY, + } action; // 0=none, 1=replace key1 with key2 + }; + + BPlusTree(); + ~BPlusTree(); + void SetPoolPageSize(int size); // Set the page size for the memory pool. Optionsl + bool Get(const KeyType key, DataType &out) const; + bool Delete(const KeyType key); + bool Delete(const KeyType key, DataType &out); + bool Insert(const KeyType key, const DataType &data); + void Clear(void); + unsigned Size(void) const; + bool IsEmpty(void) const; + Page *GetListHead(void) const; + DataType GetDataHead(void) const; + void PrintLeaves(void); + void ForEachLeaf(void (*func)(Page * leaf, int index)); + void ForEachData(void (*func)(DataType input, int index)); + void PrintGraph(void); + protected: + void ValidateTreeRecursive(Page *cur); + void DeleteFromPageAtIndex(const int index, Page *cur); + static void PrintLeaf(Page * leaf, int index); + void FreePages(void); + bool GetIndexOf(const KeyType key, Page *page, int *out) const; + void ShiftKeysLeft(Page *cur); + bool CanRotateLeft(Page *cur, int childIndex); + bool CanRotateRight(Page *cur, int childIndex); + void RotateRight(Page *cur, int childIndex, ReturnAction *returnAction); + void RotateLeft(Page *cur, int childIndex, ReturnAction *returnAction); + Page* InsertIntoNode(const KeyType key, const DataType &childData, int insertionIndex, Page *nodeData, Page *cur, ReturnAction* returnAction); + Page* InsertBranchDown(const KeyType key, const DataType &data,Page *cur, ReturnAction* returnAction, bool *success); + Page* GetLeafFromKey(const KeyType key) const; + bool FindDeleteRebalance(const KeyType key, Page *cur, bool *underflow, KeyType rightRootKey, ReturnAction *returnAction, DataType &out); + bool FixUnderflow(int branchIndex, Page *cur, KeyType rightRootKey, ReturnAction *returnAction); + void ShiftNodeLeft(Page *cur); + void ShiftNodeRight(Page *cur); + + MemoryPool > pagePool; + Page *root, *leftmostLeaf; + }; + + template + BPlusTree::BPlusTree () + { + RakAssert(order>1); + root=0; + leftmostLeaf=0; + } + template + BPlusTree::~BPlusTree () + { + Clear(); + } + template + void BPlusTree::SetPoolPageSize(int size) + { + pagePool.SetPageSize(size); + } + template + bool BPlusTree::Get(const KeyType key, DataType &out) const + { + if (root==0) + return false; + + Page* leaf = GetLeafFromKey(key); + int childIndex; + + if (GetIndexOf(key, leaf, &childIndex)) + { + out=leaf->data[childIndex]; + return true; + } + return false; + } + template + void BPlusTree::DeleteFromPageAtIndex(const int index, Page *cur) + { + int i; + for (i=index; i < cur->size-1; i++) + cur->keys[i]=cur->keys[i+1]; + if (cur->isLeaf) + { + for (i=index; i < cur->size-1; i++) + cur->data[i]=cur->data[i+1]; + } + else + { + for (i=index; i < cur->size-1; i++) + cur->children[i+1]=cur->children[i+2]; + } + cur->size--; + } + template + bool BPlusTree::Delete(const KeyType key) + { + DataType temp; + return Delete(key, temp); + } + template + bool BPlusTree::Delete(const KeyType key, DataType &out) + { + if (root==0) + return false; + + ReturnAction returnAction; + returnAction.action=ReturnAction::NO_ACTION; + int childIndex; + bool underflow=false; + if (root==leftmostLeaf) + { + if (GetIndexOf(key, root, &childIndex)==false) + return false; + out=root->data[childIndex]; + DeleteFromPageAtIndex(childIndex,root); + if (root->size==0) + { + pagePool.Release(root, _FILE_AND_LINE_); + root=0; + leftmostLeaf=0; + } + return true; + } + else if (FindDeleteRebalance(key, root, &underflow,root->keys[0], &returnAction, out)==false) + return false; + +// RakAssert(returnAction.action==ReturnAction::NO_ACTION); + + if (underflow && root->size==0) + { + // Move the root down. + Page *oldRoot=root; + root=root->children[0]; + pagePool.Release(oldRoot, _FILE_AND_LINE_); + // memset(oldRoot,0,sizeof(root)); + } + + return true; + } + template + bool BPlusTree::FindDeleteRebalance(const KeyType key, Page *cur, bool *underflow, KeyType rightRootKey, ReturnAction *returnAction, DataType &out) + { + // Get index of child to follow. + int branchIndex, childIndex; + if (GetIndexOf(key, cur, &childIndex)) + branchIndex=childIndex+1; + else + branchIndex=childIndex; + + // If child is not a leaf, call recursively + if (cur->children[branchIndex]->isLeaf==false) + { + if (branchIndexsize) + rightRootKey=cur->keys[branchIndex]; // Shift right to left + else + rightRootKey=cur->keys[branchIndex-1]; // Shift center to left + + if (FindDeleteRebalance(key, cur->children[branchIndex], underflow, rightRootKey, returnAction, out)==false) + return false; + + // Call again in case the root key changed + if (branchIndexsize) + rightRootKey=cur->keys[branchIndex]; // Shift right to left + else + rightRootKey=cur->keys[branchIndex-1]; // Shift center to left + + if (returnAction->action==ReturnAction::SET_BRANCH_KEY && branchIndex!=childIndex) + { + returnAction->action=ReturnAction::NO_ACTION; + cur->keys[childIndex]=returnAction->key1; + + if (branchIndexsize) + rightRootKey=cur->keys[branchIndex]; // Shift right to left + else + rightRootKey=cur->keys[branchIndex-1]; // Shift center to left + } + } + else + { + // If child is a leaf, get the index of the key. If the item is not found, cancel delete. + if (GetIndexOf(key, cur->children[branchIndex], &childIndex)==false) + return false; + + // Delete: + // Remove childIndex from the child at branchIndex + out=cur->children[branchIndex]->data[childIndex]; + DeleteFromPageAtIndex(childIndex, cur->children[branchIndex]); + + if (childIndex==0) + { + if (branchIndex>0) + cur->keys[branchIndex-1]=cur->children[branchIndex]->keys[0]; + + if (branchIndex==0) + { + returnAction->action=ReturnAction::SET_BRANCH_KEY; + returnAction->key1=cur->children[0]->keys[0]; + } + } + + if (cur->children[branchIndex]->size < order/2) + *underflow=true; + else + *underflow=false; + } + + // Fix underflow: + if (*underflow) + { + *underflow=FixUnderflow(branchIndex, cur, rightRootKey, returnAction); + } + + return true; + } + template + bool BPlusTree::FixUnderflow(int branchIndex, Page *cur, KeyType rightRootKey, ReturnAction *returnAction) + { + // Borrow from a neighbor that has excess. + Page *source; + Page *dest; + + if (branchIndex>0 && cur->children[branchIndex-1]->size > order/2) + { + dest=cur->children[branchIndex]; + source=cur->children[branchIndex-1]; + + // Left has excess + ShiftNodeRight(dest); + if (dest->isLeaf) + { + dest->keys[0]=source->keys[source->size-1]; + dest->data[0]=source->data[source->size-1]; + } + else + { + dest->children[0]=source->children[source->size]; + dest->keys[0]=cur->keys[branchIndex-1]; + } + // Update the parent key for the child (middle) + cur->keys[branchIndex-1]=source->keys[source->size-1]; + source->size--; + + // if (branchIndex==0) + // { + // returnAction->action=ReturnAction::SET_BRANCH_KEY; + // returnAction->key1=dest->keys[0]; + // } + + // No underflow + return false; + } + else if (branchIndexsize && cur->children[branchIndex+1]->size > order/2) + { + dest=cur->children[branchIndex]; + source=cur->children[branchIndex+1]; + + // Right has excess + if (dest->isLeaf) + { + dest->keys[dest->size]=source->keys[0]; + dest->data[dest->size]=source->data[0]; + + // The first key in the leaf after shifting is the parent key for the right branch + cur->keys[branchIndex]=source->keys[1]; + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + if (order<=3 && dest->size==0) + { + if (branchIndex==0) + { + returnAction->action=ReturnAction::SET_BRANCH_KEY; + returnAction->key1=dest->keys[0]; + } + else + cur->keys[branchIndex-1]=cur->children[branchIndex]->keys[0]; + } + } + else + { + if (returnAction->action==ReturnAction::NO_ACTION) + { + returnAction->action=ReturnAction::SET_BRANCH_KEY; + returnAction->key1=dest->keys[0]; + } + + dest->keys[dest->size]=rightRootKey; + dest->children[dest->size+1]=source->children[0]; + + // The shifted off key is the leftmost key for a node + cur->keys[branchIndex]=source->keys[0]; + } + + + dest->size++; + ShiftNodeLeft(source); + + //cur->keys[branchIndex]=source->keys[0]; + +// returnAction->action=ReturnAction::SET_BRANCH_KEY; +// returnAction->key1=dest->keys[dest->size-1]; + + // No underflow + return false; + } + else + { + int sourceIndex; + + // If no neighbors have excess, merge two branches. + // + // To merge two leaves, just copy the data and keys over. + // + // To merge two branches, copy the pointers and keys over, using rightRootKey as the key for the extra pointer + if (branchIndexsize) + { + // Merge right child to current child and delete right child. + dest=cur->children[branchIndex]; + source=cur->children[branchIndex+1]; + } + else + { + // Move current child to left and delete current child + dest=cur->children[branchIndex-1]; + source=cur->children[branchIndex]; + } + + // Merge + if (dest->isLeaf) + { + for (sourceIndex=0; sourceIndexsize; sourceIndex++) + { + dest->keys[dest->size]=source->keys[sourceIndex]; + dest->data[dest->size++]=source->data[sourceIndex]; + } + } + else + { + // We want the tree root key of the source, not the current. + dest->keys[dest->size]=rightRootKey; + dest->children[dest->size++ + 1]=source->children[0]; + for (sourceIndex=0; sourceIndexsize; sourceIndex++) + { + dest->keys[dest->size]=source->keys[sourceIndex]; + dest->children[dest->size++ + 1]=source->children[sourceIndex + 1]; + } + } + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + if (order<=3 && branchIndex>0 && cur->children[branchIndex]->isLeaf) // With order==2 it is possible to delete data[0], which is not possible with higher orders. + cur->keys[branchIndex-1]=cur->children[branchIndex]->keys[0]; + + if (branchIndexsize) + { + // Update the parent key, removing the source (right) + DeleteFromPageAtIndex(branchIndex, cur); + } + else + { + if (branchIndex>0) + { + // Update parent key, removing the source (current) + DeleteFromPageAtIndex(branchIndex-1, cur); + } + } + + if (branchIndex==0 && dest->isLeaf) + { + returnAction->action=ReturnAction::SET_BRANCH_KEY; + returnAction->key1=dest->keys[0]; + } + + if (source==leftmostLeaf) + leftmostLeaf=source->next; + + if (source->isLeaf) + { + if (source->previous) + source->previous->next=source->next; + if (source->next) + source->next->previous=source->previous; + } + + // Free the source node + pagePool.Release(source, _FILE_AND_LINE_); + // memset(source,0,sizeof(root)); + + // Return underflow or not of parent. + return cur->size < order/2; + } + } + template + void BPlusTree::ShiftNodeRight(Page *cur) + { + int i; + for (i=cur->size; i>0; i--) + cur->keys[i]=cur->keys[i-1]; + if (cur->isLeaf) + { + for (i=cur->size; i>0; i--) + cur->data[i]=cur->data[i-1]; + } + else + { + for (i=cur->size+1; i>0; i--) + cur->children[i]=cur->children[i-1]; + } + + cur->size++; + } + template + void BPlusTree::ShiftNodeLeft(Page *cur) + { + int i; + for (i=0; i < cur->size-1; i++) + cur->keys[i]=cur->keys[i+1]; + if (cur->isLeaf) + { + for (i=0; i < cur->size; i++) + cur->data[i]=cur->data[i+1]; + } + else + { + for (i=0; i < cur->size; i++) + cur->children[i]=cur->children[i+1]; + } + cur->size--; + } + template + Page* BPlusTree::InsertIntoNode(const KeyType key, const DataType &leafData, int insertionIndex, Page *nodeData, Page *cur, ReturnAction* returnAction) + { + int i; + if (cur->size < order) + { + for (i=cur->size; i > insertionIndex; i--) + cur->keys[i]=cur->keys[i-1]; + if (cur->isLeaf) + { + for (i=cur->size; i > insertionIndex; i--) + cur->data[i]=cur->data[i-1]; + } + else + { + for (i=cur->size+1; i > insertionIndex+1; i--) + cur->children[i]=cur->children[i-1]; + } + cur->keys[insertionIndex]=key; + if (cur->isLeaf) + cur->data[insertionIndex]=leafData; + else + cur->children[insertionIndex+1]=nodeData; + + cur->size++; + } + else + { + Page* newPage = pagePool.Allocate( _FILE_AND_LINE_ ); + newPage->isLeaf=cur->isLeaf; + if (cur->isLeaf) + { + newPage->next=cur->next; + if (cur->next) + cur->next->previous=newPage; + newPage->previous=cur; + cur->next=newPage; + } + + int destIndex, sourceIndex; + + if (insertionIndex>=(order+1)/2) + { + destIndex=0; + sourceIndex=order/2; + + for (; sourceIndex < insertionIndex; sourceIndex++, destIndex++) + { + newPage->keys[destIndex]=cur->keys[sourceIndex]; + } + newPage->keys[destIndex++]=key; + for (; sourceIndex < order; sourceIndex++, destIndex++) + { + newPage->keys[destIndex]=cur->keys[sourceIndex]; + } + + destIndex=0; + sourceIndex=order/2; + if (cur->isLeaf) + { + for (; sourceIndex < insertionIndex; sourceIndex++, destIndex++) + { + newPage->data[destIndex]=cur->data[sourceIndex]; + } + newPage->data[destIndex++]=leafData; + for (; sourceIndex < order; sourceIndex++, destIndex++) + { + newPage->data[destIndex]=cur->data[sourceIndex]; + } + } + else + { + + for (; sourceIndex < insertionIndex; sourceIndex++, destIndex++) + { + newPage->children[destIndex]=cur->children[sourceIndex+1]; + } + newPage->children[destIndex++]=nodeData; + + // sourceIndex+1 is sort of a hack but it works - because there is one extra child than keys + // skip past the last child for cur + for (; sourceIndex+1 < cur->size+1; sourceIndex++, destIndex++) + { + newPage->children[destIndex]=cur->children[sourceIndex+1]; + } + + // the first key is the middle key. Remove it from the page and push it to the parent + returnAction->action=ReturnAction::PUSH_KEY_TO_PARENT; + returnAction->key1=newPage->keys[0]; + for (int j=0; j < destIndex-1; j++) + newPage->keys[j]=newPage->keys[j+1]; + + } + cur->size=order/2; + } + else + { + destIndex=0; + sourceIndex=(order+1)/2-1; + for (; sourceIndex < order; sourceIndex++, destIndex++) + newPage->keys[destIndex]=cur->keys[sourceIndex]; + destIndex=0; + if (cur->isLeaf) + { + sourceIndex=(order+1)/2-1; + for (; sourceIndex < order; sourceIndex++, destIndex++) + newPage->data[destIndex]=cur->data[sourceIndex]; + } + else + { + sourceIndex=(order+1)/2; + for (; sourceIndex < order+1; sourceIndex++, destIndex++) + newPage->children[destIndex]=cur->children[sourceIndex]; + + // the first key is the middle key. Remove it from the page and push it to the parent + returnAction->action=ReturnAction::PUSH_KEY_TO_PARENT; + returnAction->key1=newPage->keys[0]; + for (int j=0; j < destIndex-1; j++) + newPage->keys[j]=newPage->keys[j+1]; + } + cur->size=(order+1)/2-1; + if (cur->size) + { + bool b = GetIndexOf(key, cur, &insertionIndex); + (void) b; + RakAssert(b==false); + } + else + insertionIndex=0; + InsertIntoNode(key, leafData, insertionIndex, nodeData, cur, returnAction); + } + + newPage->size=destIndex; + + return newPage; + } + + return 0; + } + + template + bool BPlusTree::CanRotateLeft(Page *cur, int childIndex) + { + return childIndex>0 && cur->children[childIndex-1]->size + void BPlusTree::RotateLeft(Page *cur, int childIndex, ReturnAction *returnAction) + { + Page *dest = cur->children[childIndex-1]; + Page *source = cur->children[childIndex]; + returnAction->key1=source->keys[0]; + dest->keys[dest->size]=source->keys[0]; + dest->data[dest->size]=source->data[0]; + dest->size++; + for (int i=0; i < source->size-1; i++) + { + source->keys[i]=source->keys[i+1]; + source->data[i]=source->data[i+1]; + } + source->size--; + cur->keys[childIndex-1]=source->keys[0]; + returnAction->key2=source->keys[0]; + } + + template + bool BPlusTree::CanRotateRight(Page *cur, int childIndex) + { + return childIndex < cur->size && cur->children[childIndex+1]->size + void BPlusTree::RotateRight(Page *cur, int childIndex, ReturnAction *returnAction) + { + Page *dest = cur->children[childIndex+1]; + Page *source = cur->children[childIndex]; + returnAction->key1=dest->keys[0]; + for (int i= dest->size; i > 0; i--) + { + dest->keys[i]=dest->keys[i-1]; + dest->data[i]=dest->data[i-1]; + } + dest->keys[0]=source->keys[source->size-1]; + dest->data[0]=source->data[source->size-1]; + dest->size++; + source->size--; + + cur->keys[childIndex]=dest->keys[0]; + returnAction->key2=dest->keys[0]; + } + template + Page* BPlusTree::GetLeafFromKey(const KeyType key) const + { + Page* cur = root; + int childIndex; + while (cur->isLeaf==false) + { + // When searching, if we match the exact key we go down the pointer after that index + if (GetIndexOf(key, cur, &childIndex)) + childIndex++; + cur = cur->children[childIndex]; + } + return cur; + } + + template + Page* BPlusTree::InsertBranchDown(const KeyType key, const DataType &data,Page *cur, ReturnAction *returnAction, bool *success) + { + int childIndex; + int branchIndex; + if (GetIndexOf(key, cur, &childIndex)) + branchIndex=childIndex+1; + else + branchIndex=childIndex; + Page* newPage; + if (cur->isLeaf==false) + { + if (cur->children[branchIndex]->isLeaf==true && cur->children[branchIndex]->size==order) + { + if (branchIndex==childIndex+1) + { + *success=false; + return 0; // Already exists + } + + if (CanRotateLeft(cur, branchIndex)) + { + returnAction->action=ReturnAction::REPLACE_KEY1_WITH_KEY2; + if (key > cur->children[branchIndex]->keys[0]) + { + RotateLeft(cur, branchIndex, returnAction); + + int insertionIndex; + GetIndexOf(key, cur->children[branchIndex], &insertionIndex); + InsertIntoNode(key, data, insertionIndex, 0, cur->children[branchIndex], 0); + } + else + { + // Move head element to left and replace it with key,data + Page* dest=cur->children[branchIndex-1]; + Page* source=cur->children[branchIndex]; + returnAction->key1=source->keys[0]; + returnAction->key2=key; + dest->keys[dest->size]=source->keys[0]; + dest->data[dest->size]=source->data[0]; + dest->size++; + source->keys[0]=key; + source->data[0]=data; + } + cur->keys[branchIndex-1]=cur->children[branchIndex]->keys[0]; + + return 0; + } + else if (CanRotateRight(cur, branchIndex)) + { + returnAction->action=ReturnAction::REPLACE_KEY1_WITH_KEY2; + + if (key < cur->children[branchIndex]->keys[cur->children[branchIndex]->size-1]) + { + RotateRight(cur, branchIndex, returnAction); + + int insertionIndex; + GetIndexOf(key, cur->children[branchIndex], &insertionIndex); + InsertIntoNode(key, data, insertionIndex, 0, cur->children[branchIndex], 0); + + } + else + { + // Insert to the head of the right leaf instead and change our key + returnAction->key1=cur->children[branchIndex+1]->keys[0]; + InsertIntoNode(key, data, 0, 0, cur->children[branchIndex+1], 0); + returnAction->key2=key; + } + cur->keys[branchIndex]=cur->children[branchIndex+1]->keys[0]; + return 0; + } + } + + newPage=InsertBranchDown(key,data,cur->children[branchIndex], returnAction, success); + if (returnAction->action==ReturnAction::REPLACE_KEY1_WITH_KEY2) + { + if (branchIndex>0 && cur->keys[branchIndex-1]==returnAction->key1) + cur->keys[branchIndex-1]=returnAction->key2; + } + if (newPage) + { + if (newPage->isLeaf==false) + { + RakAssert(returnAction->action==ReturnAction::PUSH_KEY_TO_PARENT); + newPage->size--; + return InsertIntoNode(returnAction->key1, data, branchIndex, newPage, cur, returnAction); + } + else + { + return InsertIntoNode(newPage->keys[0], data, branchIndex, newPage, cur, returnAction); + } + } + } + else + { + if (branchIndex==childIndex+1) + { + *success=false; + return 0; // Already exists + } + else + { + return InsertIntoNode(key, data, branchIndex, 0, cur, returnAction); + } + } + + return 0; + } + template + bool BPlusTree::Insert(const KeyType key, const DataType &data) + { + if (root==0) + { + // Allocate root and make root a leaf + root = pagePool.Allocate( _FILE_AND_LINE_ ); + root->isLeaf=true; + leftmostLeaf=root; + root->size=1; + root->keys[0]=key; + root->data[0]=data; + root->next=0; + root->previous=0; + } + else + { + bool success=true; + ReturnAction returnAction; + returnAction.action=ReturnAction::NO_ACTION; + Page* newPage = InsertBranchDown(key, data, root, &returnAction, &success); + if (success==false) + return false; + if (newPage) + { + KeyType newKey; + if (newPage->isLeaf==false) + { + // One key is pushed up through the stack. I store that at keys[0] but it has to be removed for the page to be correct + RakAssert(returnAction.action==ReturnAction::PUSH_KEY_TO_PARENT); + newKey=returnAction.key1; + newPage->size--; + } + else + newKey = newPage->keys[0]; + // propagate the root + Page* newRoot = pagePool.Allocate( _FILE_AND_LINE_ ); + newRoot->isLeaf=false; + newRoot->size=1; + newRoot->keys[0]=newKey; + newRoot->children[0]=root; + newRoot->children[1]=newPage; + root=newRoot; + } + } + + return true; + } + template + void BPlusTree::ShiftKeysLeft(Page *cur) + { + int i; + for (i=0; i < cur->size; i++) + cur->keys[i]=cur->keys[i+1]; + } + template + void BPlusTree::Clear(void) + { + if (root) + { + FreePages(); + leftmostLeaf=0; + root=0; + } + pagePool.Clear(_FILE_AND_LINE_); + } + template + unsigned BPlusTree::Size(void) const + { + unsigned int count=0; + DataStructures::Page *cur = GetListHead(); + while (cur) + { + count+=cur->size; + cur=cur->next; + } + return count; + } + template + bool BPlusTree::IsEmpty(void) const + { + return root==0; + } + template + bool BPlusTree::GetIndexOf(const KeyType key, Page *page, int *out) const + { + RakAssert(page->size>0); + int index, upperBound, lowerBound; + upperBound=page->size-1; + lowerBound=0; + index = page->size/2; + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + while (1) + { + if (key==page->keys[index]) + { + *out=index; + return true; + } + else if (keykeys[index]) + upperBound=index-1; + else + lowerBound=index+1; + + index=lowerBound+(upperBound-lowerBound)/2; + + if (lowerBound>upperBound) + { + *out=lowerBound; + return false; // No match + } + } + } + template + void BPlusTree::FreePages(void) + { + DataStructures::Queue *> queue; + DataStructures::Page *ptr; + int i; + queue.Push(root, _FILE_AND_LINE_ ); + while (queue.Size()) + { + ptr=queue.Pop(); + if (ptr->isLeaf==false) + { + for (i=0; i < ptr->size+1; i++) + queue.Push(ptr->children[i], _FILE_AND_LINE_ ); + } + pagePool.Release(ptr, _FILE_AND_LINE_); + // memset(ptr,0,sizeof(root)); + }; + } + template + Page *BPlusTree::GetListHead(void) const + { + return leftmostLeaf; + } + template + DataType BPlusTree::GetDataHead(void) const + { + return leftmostLeaf->data[0]; + } + template + void BPlusTree::ForEachLeaf(void (*func)(Page * leaf, int index)) + { + int count=0; + DataStructures::Page *cur = GetListHead(); + while (cur) + { + func(cur, count++); + cur=cur->next; + } + } + template + void BPlusTree::ForEachData(void (*func)(DataType input, int index)) + { + int count=0,i; + DataStructures::Page *cur = GetListHead(); + while (cur) + { + for (i=0; i < cur->size; i++) + func(cur->data[i], count++); + cur=cur->next; + } + } + template + void BPlusTree::PrintLeaf(Page * leaf, int index) + { + int i; + RAKNET_DEBUG_PRINTF("%i] SELF=%p\n", index+1, leaf); + for (i=0; i < leaf->size; i++) + RAKNET_DEBUG_PRINTF(" %i. %i\n", i+1, leaf->data[i]); + } + template + void BPlusTree::PrintLeaves(void) + { + ForEachLeaf(PrintLeaf); + } + + template + void BPlusTree::ValidateTreeRecursive(Page *cur) + { + RakAssert(cur==root || cur->size>=order/2); + + if (cur->children[0]->isLeaf) + { + RakAssert(cur->children[0]->keys[0] < cur->keys[0]); + for (int i=0; i < cur->size; i++) + { + RakAssert(cur->children[i+1]->keys[0]==cur->keys[i]); + } + } + else + { + for (int i=0; i < cur->size+1; i++) + ValidateTreeRecursive(cur->children[i]); + } + } + + template + void BPlusTree::PrintGraph(void) + { + DataStructures::Queue *> queue; + queue.Push(root,_FILE_AND_LINE_); + queue.Push(0,_FILE_AND_LINE_); + DataStructures::Page *ptr; + int i,j; + if (root) + { + RAKNET_DEBUG_PRINTF("%p(", root); + for (i=0; i < root->size; i++) + { + RAKNET_DEBUG_PRINTF("%i ", root->keys[i]); + } + RAKNET_DEBUG_PRINTF(") "); + RAKNET_DEBUG_PRINTF("\n"); + } + while (queue.Size()) + { + ptr=queue.Pop(); + if (ptr==0) + RAKNET_DEBUG_PRINTF("\n"); + else if (ptr->isLeaf==false) + { + for (i=0; i < ptr->size+1; i++) + { + RAKNET_DEBUG_PRINTF("%p(", ptr->children[i]); + //RAKNET_DEBUG_PRINTF("(", ptr->children[i]); + for (j=0; j < ptr->children[i]->size; j++) + RAKNET_DEBUG_PRINTF("%i ", ptr->children[i]->keys[j]); + RAKNET_DEBUG_PRINTF(") "); + queue.Push(ptr->children[i],_FILE_AND_LINE_); + } + queue.Push(0,_FILE_AND_LINE_); + RAKNET_DEBUG_PRINTF(" -- "); + } + } + RAKNET_DEBUG_PRINTF("\n"); + } +} +#ifdef _MSC_VER +#pragma warning( pop ) +#endif + + +// Code to test this hellish data structure. +/* +#include "DS_BPlusTree.h" +#include + +// Handle underflow on root. If there is only one item left then I can go downwards. +// Make sure I keep the leftmost pointer valid by traversing it +// When I free a leaf, be sure to adjust the pointers around it. + +#include "Rand.h" + +void main(void) +{ + DataStructures::BPlusTree btree; + DataStructures::List haveList, removedList; + int temp; + int i, j, index; + int testSize; + bool b; + + for (testSize=0; testSize < 514; testSize++) + { + RAKNET_DEBUG_PRINTF("TestSize=%i\n", testSize); + + for (i=0; i < testSize; i++) + haveList.Insert(i); + + for (i=0; i < testSize; i++) + { + index=i+randomMT()%(testSize-i); + temp=haveList[index]; + haveList[index]=haveList[i]; + haveList[i]=temp; + } + + for (i=0; i - * - * OR - * - * AVLBalancedBinarySearchTree - * - * Use the AVL balanced tree if you want the tree to be balanced after every deletion and addition. This avoids the potential - * worst case scenario where ordered input to a binary search tree gives linear search time results. It's not needed - * if input will be evenly distributed, in which case the search time is O (log n). The search time for the AVL - * balanced binary tree is O (log n) irregardless of input. - * - * Has the following member functions - * unsigned int Height() - Returns the height of the tree at the optional specified starting index. Default is the root - * add(element) - adds an element to the BinarySearchTree - * bool del(element) - deletes the node containing element if the element is in the tree as defined by a comparison with the == operator. Returns true on success, false if the element is not found - * bool IsInelement) - returns true if element is in the tree as defined by a comparison with the == operator. Otherwise returns false - * DisplayInorder(array) - Fills an array with an inorder search of the elements in the tree. USER IS REPONSIBLE FOR ALLOCATING THE ARRAY!. - * DisplayPreorder(array) - Fills an array with an preorder search of the elements in the tree. USER IS REPONSIBLE FOR ALLOCATING THE ARRAY!. - * DisplayPostorder(array) - Fills an array with an postorder search of the elements in the tree. USER IS REPONSIBLE FOR ALLOCATING THE ARRAY!. - * DisplayBreadthFirstSearch(array) - Fills an array with a breadth first search of the elements in the tree. USER IS REPONSIBLE FOR ALLOCATING THE ARRAY!. - * clear - Destroys the tree. Same as calling the destructor - * unsigned int Height() - Returns the height of the tree - * unsigned int size() - returns the size of the BinarySearchTree - * GetPointerToNode(element) - returns a pointer to the comparision element in the tree, allowing for direct modification when necessary with complex data types. - * Be warned, it is possible to corrupt the tree if the element used for comparisons is modified. Returns NULL if the item is not found - * - * - * EXAMPLE - * @code - * BinarySearchTree A; - * A.Add(10); - * A.Add(15); - * A.Add(5); - * int* array = RakNet::OP_NEW(A.Size(), _FILE_AND_LINE_ ); - * A.DisplayInorder(array); - * array[0]; // returns 5 - * array[1]; // returns 10 - * array[2]; // returns 15 - * @endcode - * compress - reallocates memory to fit the number of elements. Best used when the number of elements decreases - * - * clear - empties the BinarySearchTree and returns storage - * The assignment and copy constructors are defined - * - * \note The template type must have the copy constructor and - * assignment operator defined and must work with >, <, and == All - * elements in the tree MUST be distinct The assignment operator is - * defined between BinarySearchTree and AVLBalancedBinarySearchTree - * as long as they are of the same template type. However, passing a - * BinarySearchTree to an AVLBalancedBinarySearchTree will lose its - * structure unless it happened to be AVL balanced to begin with - * Requires queue_linked_list.cpp for the breadth first search used - * in the copy constructor, overloaded assignment operator, and - * display_breadth_first_search. - * - * - */ - template - class RAK_DLL_EXPORT BinarySearchTree - { - - public: - - struct node - { - BinarySearchTreeType* item; - node* left; - node* right; - }; - - BinarySearchTree(); - virtual ~BinarySearchTree(); - BinarySearchTree( const BinarySearchTree& original_type ); - BinarySearchTree& operator= ( const BinarySearchTree& original_copy ); - unsigned int Size( void ); - void Clear( const char *file, unsigned int line ); - unsigned int Height( node* starting_node = 0 ); - node* Add ( const BinarySearchTreeType& input, const char *file, unsigned int line ); - node* Del( const BinarySearchTreeType& input, const char *file, unsigned int line ); - bool IsIn( const BinarySearchTreeType& input ); - void DisplayInorder( BinarySearchTreeType* return_array ); - void DisplayPreorder( BinarySearchTreeType* return_array ); - void DisplayPostorder( BinarySearchTreeType* return_array ); - void DisplayBreadthFirstSearch( BinarySearchTreeType* return_array ); - BinarySearchTreeType*& GetPointerToNode( const BinarySearchTreeType& element ); - - protected: - - node* root; - - enum Direction_Types - { - NOT_FOUND, LEFT, RIGHT, ROOT - } direction; - unsigned int HeightRecursive( node* current ); - unsigned int BinarySearchTree_size; - node*& Find( const BinarySearchTreeType& element, node** parent ); - node*& FindParent( const BinarySearchTreeType& element ); - void DisplayPostorderRecursive( node* current, BinarySearchTreeType* return_array, unsigned int& index ); - void FixTree( node* current ); - - }; - - /// An AVLBalancedBinarySearchTree is a binary tree that is always balanced - template - class RAK_DLL_EXPORT AVLBalancedBinarySearchTree : public BinarySearchTree - { - - public: - AVLBalancedBinarySearchTree() {} - virtual ~AVLBalancedBinarySearchTree(); - void Add ( const BinarySearchTreeType& input ); - void Del( const BinarySearchTreeType& input ); - BinarySearchTree& operator= ( BinarySearchTree& original_copy ) - { - return BinarySearchTree::operator= ( original_copy ); - } - - private: - void BalanceTree( typename BinarySearchTree::node* current, bool rotateOnce ); - void RotateRight( typename BinarySearchTree::node *C ); - void RotateLeft( typename BinarySearchTree::node* C ); - void DoubleRotateRight( typename BinarySearchTree::node *A ); - void DoubleRotateLeft( typename BinarySearchTree::node* A ); - bool RightHigher( typename BinarySearchTree::node* A ); - bool LeftHigher( typename BinarySearchTree::node* A ); - }; - - template - void AVLBalancedBinarySearchTree::BalanceTree( typename BinarySearchTree::node* current, bool rotateOnce ) - { - int left_height, right_height; - - while ( current ) - { - if ( current->left == 0 ) - left_height = 0; - else - left_height = Height( current->left ); - - if ( current->right == 0 ) - right_height = 0; - else - right_height = Height( current->right ); - - if ( right_height - left_height == 2 ) - { - if ( RightHigher( current->right ) ) - RotateLeft( current->right ); - else - DoubleRotateLeft( current ); - - if ( rotateOnce ) - break; - } - - else - if ( right_height - left_height == -2 ) - { - if ( LeftHigher( current->left ) ) - RotateRight( current->left ); - else - DoubleRotateRight( current ); - - if ( rotateOnce ) - break; - } - - if ( current == this->root ) - break; - - current = FindParent( *( current->item ) ); - - } - } - - template - void AVLBalancedBinarySearchTree::Add ( const BinarySearchTreeType& input ) - { - - typename BinarySearchTree::node * current = BinarySearchTree::Add ( input, _FILE_AND_LINE_ ); - BalanceTree( current, true ); - } - - template - void AVLBalancedBinarySearchTree::Del( const BinarySearchTreeType& input ) - { - typename BinarySearchTree::node * current = BinarySearchTree::Del( input, _FILE_AND_LINE_ ); - BalanceTree( current, false ); - - } - - template - bool AVLBalancedBinarySearchTree::RightHigher( typename BinarySearchTree::node *A ) - { - if ( A == 0 ) - return false; - - return Height( A->right ) > Height( A->left ); - } - - template - bool AVLBalancedBinarySearchTree::LeftHigher( typename BinarySearchTree::node *A ) - { - if ( A == 0 ) - return false; - - return Height( A->left ) > Height( A->right ); - } - - template - void AVLBalancedBinarySearchTree::RotateRight( typename BinarySearchTree::node *C ) - { - typename BinarySearchTree::node * A, *B, *D; - /* - RIGHT ROTATION - - A = parent(b) - b= parent(c) - c = node to rotate around - - A - | // Either direction - B - / \ - C - / \ - D - - TO - - A - | // Either Direction - C - / \ - B - / \ - D - - - - - */ - - B = FindParent( *( C->item ) ); - A = FindParent( *( B->item ) ); - D = C->right; - - if ( A ) - { - // Direction was set by the last find_parent call - - if ( this->direction == this->LEFT ) - A->left = C; - else - A->right = C; - } - - else - this->root = C; // If B has no parent parent then B must have been the root node - - B->left = D; - - C->right = B; - } - - template - void AVLBalancedBinarySearchTree::DoubleRotateRight( typename BinarySearchTree::node *A ) - { - // The left side of the left child must be higher for the tree to balance with a right rotation. If it isn't, rotate it left before the normal rotation so it is. - RotateLeft( A->left->right ); - RotateRight( A->left ); - } - - template - void AVLBalancedBinarySearchTree::RotateLeft( typename BinarySearchTree::node *C ) - { - typename BinarySearchTree::node * A, *B, *D; - /* - RIGHT ROTATION - - A = parent(b) - b= parent(c) - c = node to rotate around - - A - | // Either direction - B - / \ - C - / \ - D - - TO - - A - | // Either Direction - C - / \ - B - / \ - D - - - - - */ - - B = FindParent( *( C->item ) ); - A = FindParent( *( B->item ) ); - D = C->left; - - if ( A ) - { - // Direction was set by the last find_parent call - - if ( this->direction == this->LEFT ) - A->left = C; - else - A->right = C; - } - - else - this->root = C; // If B has no parent parent then B must have been the root node - - B->right = D; - - C->left = B; - } - - template - void AVLBalancedBinarySearchTree::DoubleRotateLeft( typename BinarySearchTree::node *A ) - { - // The left side of the right child must be higher for the tree to balance with a left rotation. If it isn't, rotate it right before the normal rotation so it is. - RotateRight( A->right->left ); - RotateLeft( A->right ); - } - - template - AVLBalancedBinarySearchTree::~AVLBalancedBinarySearchTree() - { - this->Clear(_FILE_AND_LINE_); - } - - template - unsigned int BinarySearchTree::Size( void ) - { - return BinarySearchTree_size; - } - - template - unsigned int BinarySearchTree::Height( typename BinarySearchTree::node* starting_node ) - { - if ( BinarySearchTree_size == 0 || starting_node == 0 ) - return 0; - else - return HeightRecursive( starting_node ); - } - - // Recursively return the height of a binary tree - template - unsigned int BinarySearchTree::HeightRecursive( typename BinarySearchTree::node* current ) - { - unsigned int left_height = 0, right_height = 0; - - if ( ( current->left == 0 ) && ( current->right == 0 ) ) - return 1; // Leaf - - if ( current->left != 0 ) - left_height = 1 + HeightRecursive( current->left ); - - if ( current->right != 0 ) - right_height = 1 + HeightRecursive( current->right ); - - if ( left_height > right_height ) - return left_height; - else - return right_height; - } - - template - BinarySearchTree::BinarySearchTree() - { - BinarySearchTree_size = 0; - root = 0; - } - - template - BinarySearchTree::~BinarySearchTree() - { - this->Clear(_FILE_AND_LINE_); - } - - template - BinarySearchTreeType*& BinarySearchTree::GetPointerToNode( const BinarySearchTreeType& element ) - { - static typename BinarySearchTree::node * tempnode; - static BinarySearchTreeType* dummyptr = 0; - tempnode = Find ( element, &tempnode ); - - if ( this->direction == this->NOT_FOUND ) - return dummyptr; - - return tempnode->item; - } - - template - typename BinarySearchTree::node*& BinarySearchTree::Find( const BinarySearchTreeType& element, typename BinarySearchTree::node** parent ) - { - static typename BinarySearchTree::node * current; - - current = this->root; - *parent = 0; - this->direction = this->ROOT; - - if ( BinarySearchTree_size == 0 ) - { - this->direction = this->NOT_FOUND; - return current = 0; - } - - // Check if the item is at the root - if ( element == *( current->item ) ) - { - this->direction = this->ROOT; - return current; - } - -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - while ( true ) - { - // Move pointer - - if ( element < *( current->item ) ) - { - *parent = current; - this->direction = this->LEFT; - current = current->left; - } - - else - if ( element > *( current->item ) ) - { - *parent = current; - this->direction = this->RIGHT; - current = current->right; - } - - if ( current == 0 ) - break; - - // Check if new position holds the item - if ( element == *( current->item ) ) - { - return current; - } - } - - - this->direction = this->NOT_FOUND; - return current = 0; - } - - template - typename BinarySearchTree::node*& BinarySearchTree::FindParent( const BinarySearchTreeType& element ) - { - static typename BinarySearchTree::node * parent; - Find ( element, &parent ); - return parent; - } - - // Performs a series of value swaps starting with current to fix the tree if needed - template - void BinarySearchTree::FixTree( typename BinarySearchTree::node* current ) - { - BinarySearchTreeType temp; - - while ( 1 ) - { - if ( ( ( current->left ) != 0 ) && ( *( current->item ) < *( current->left->item ) ) ) - { - // Swap the current value with the one to the left - temp = *( current->left->item ); - *( current->left->item ) = *( current->item ); - *( current->item ) = temp; - current = current->left; - } - - else - if ( ( ( current->right ) != 0 ) && ( *( current->item ) > *( current->right->item ) ) ) - { - // Swap the current value with the one to the right - temp = *( current->right->item ); - *( current->right->item ) = *( current->item ); - *( current->item ) = temp; - current = current->right; - } - - else - break; // current points to the right place so quit - } - } - - template - typename BinarySearchTree::node* BinarySearchTree::Del( const BinarySearchTreeType& input, const char *file, unsigned int line ) - { - typename BinarySearchTree::node * node_to_delete, *current, *parent; - - if ( BinarySearchTree_size == 0 ) - return 0; - - if ( BinarySearchTree_size == 1 ) - { - Clear(file, line); - return 0; - } - - node_to_delete = Find( input, &parent ); - - if ( direction == NOT_FOUND ) - return 0; // Couldn't find the element - - current = node_to_delete; - - // Replace the deleted node with the appropriate value - if ( ( current->right ) == 0 && ( current->left ) == 0 ) // Leaf node, just remove it - { - - if ( parent ) - { - if ( direction == LEFT ) - parent->left = 0; - else - parent->right = 0; - } - - RakNet::OP_DELETE(node_to_delete->item, file, line); - RakNet::OP_DELETE(node_to_delete, file, line); - BinarySearchTree_size--; - return parent; - } - else - if ( ( current->right ) != 0 && ( current->left ) == 0 ) // Node has only one child, delete it and cause the parent to point to that child - { - - if ( parent ) - { - if ( direction == RIGHT ) - parent->right = current->right; - else - parent->left = current->right; - } - - else - root = current->right; // Without a parent this must be the root node - - RakNet::OP_DELETE(node_to_delete->item, file, line); - - RakNet::OP_DELETE(node_to_delete, file, line); - - BinarySearchTree_size--; - - return parent; - } - else - if ( ( current->right ) == 0 && ( current->left ) != 0 ) // Node has only one child, delete it and cause the parent to point to that child - { - - if ( parent ) - { - if ( direction == RIGHT ) - parent->right = current->left; - else - parent->left = current->left; - } - - else - root = current->left; // Without a parent this must be the root node - - RakNet::OP_DELETE(node_to_delete->item, file, line); - - RakNet::OP_DELETE(node_to_delete, file, line); - - BinarySearchTree_size--; - - return parent; - } - else // Go right, then as left as far as you can - { - parent = current; - direction = RIGHT; - current = current->right; // Must have a right branch because the if statements above indicated that it has 2 branches - - while ( current->left ) - { - direction = LEFT; - parent = current; - current = current->left; - } - - // Replace the value held by the node to RakNet::OP_DELETE(with the value pointed to by current, _FILE_AND_LINE_); - *( node_to_delete->item ) = *( current->item ); - - // Delete current. - // If it is a leaf node just delete it - if ( current->right == 0 ) - { - if ( direction == RIGHT ) - parent->right = 0; - else - parent->left = 0; - - RakNet::OP_DELETE(current->item, file, line); - - RakNet::OP_DELETE(current, file, line); - - BinarySearchTree_size--; - - return parent; - } - - else - { - // Skip this node and make its parent point to its right branch - - if ( direction == RIGHT ) - parent->right = current->right; - else - parent->left = current->right; - - RakNet::OP_DELETE(current->item, file, line); - - RakNet::OP_DELETE(current, file, line); - - BinarySearchTree_size--; - - return parent; - } - } - } - - template - typename BinarySearchTree::node* BinarySearchTree::Add ( const BinarySearchTreeType& input, const char *file, unsigned int line ) - { - typename BinarySearchTree::node * current; - - // Add the new element to the tree according to the following alogrithm: - // 1. If the current node is empty add the new leaf - // 2. If the element is less than the current node then go down the left branch - // 3. If the element is greater than the current node then go down the right branch - - if ( BinarySearchTree_size == 0 ) - { - BinarySearchTree_size = 1; - root = RakNet::OP_NEW( file, line ); - root->item = RakNet::OP_NEW( file, line ); - *( root->item ) = input; - root->left = 0; - root->right = 0; - - return root; - } - - else - { - // start at the root - current = root; - -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - while ( true ) // This loop traverses the tree to find a spot for insertion - { - - if ( input < *( current->item ) ) - { - if ( current->left == 0 ) - { - current->left = RakNet::OP_NEW( file, line ); - current->left->item = RakNet::OP_NEW( file, line ); - current = current->left; - current->left = 0; - current->right = 0; - *( current->item ) = input; - - BinarySearchTree_size++; - return current; - } - - else - { - current = current->left; - } - } - - else - if ( input > *( current->item ) ) - { - if ( current->right == 0 ) - { - current->right = RakNet::OP_NEW( file, line ); - current->right->item = RakNet::OP_NEW( file, line ); - current = current->right; - current->left = 0; - current->right = 0; - *( current->item ) = input; - - BinarySearchTree_size++; - return current; - } - - else - { - current = current->right; - } - } - - else - return 0; // ((input == current->item) == true) which is not allowed since the tree only takes discrete values. Do nothing - } - } - } - - template - bool BinarySearchTree::IsIn( const BinarySearchTreeType& input ) - { - typename BinarySearchTree::node * parent; - find( input, &parent ); - - if ( direction != NOT_FOUND ) - return true; - else - return false; - } - - - template - void BinarySearchTree::DisplayInorder( BinarySearchTreeType* return_array ) - { - typename BinarySearchTree::node * current, *parent; - bool just_printed = false; - - unsigned int index = 0; - - current = root; - - if ( BinarySearchTree_size == 0 ) - return ; // Do nothing for an empty tree - - else - if ( BinarySearchTree_size == 1 ) - { - return_array[ 0 ] = *( root->item ); - return ; - } - - - direction = ROOT; // Reset the direction - - while ( index != BinarySearchTree_size ) - { - // direction is set by the find function and holds the direction of the parent to the last node visited. It is used to prevent revisiting nodes - - if ( ( current->left != 0 ) && ( direction != LEFT ) && ( direction != RIGHT ) ) - { - // Go left if the following 2 conditions are true - // I can go left - // I did not just move up from a right child - // I did not just move up from a left child - - current = current->left; - direction = ROOT; // Reset the direction - } - - else - if ( ( direction != RIGHT ) && ( just_printed == false ) ) - { - // Otherwise, print the current node if the following 3 conditions are true: - // I did not just move up from a right child - // I did not print this ndoe last cycle - - return_array[ index++ ] = *( current->item ); - just_printed = true; - } - - else - if ( ( current->right != 0 ) && ( direction != RIGHT ) ) - { - // Otherwise, go right if the following 2 conditions are true - // I did not just move up from a right child - // I can go right - - current = current->right; - direction = ROOT; // Reset the direction - just_printed = false; - } - - else - { - // Otherwise I've done everything I can. Move up the tree one node - parent = FindParent( *( current->item ) ); - current = parent; - just_printed = false; - } - } - } - - template - void BinarySearchTree::DisplayPreorder( BinarySearchTreeType* return_array ) - { - typename BinarySearchTree::node * current, *parent; - - unsigned int index = 0; - - current = root; - - if ( BinarySearchTree_size == 0 ) - return ; // Do nothing for an empty tree - - else - if ( BinarySearchTree_size == 1 ) - { - return_array[ 0 ] = *( root->item ); - return ; - } - - - direction = ROOT; // Reset the direction - return_array[ index++ ] = *( current->item ); - - while ( index != BinarySearchTree_size ) - { - // direction is set by the find function and holds the direction of the parent to the last node visited. It is used to prevent revisiting nodes - - if ( ( current->left != 0 ) && ( direction != LEFT ) && ( direction != RIGHT ) ) - { - - current = current->left; - direction = ROOT; - - // Everytime you move a node print it - return_array[ index++ ] = *( current->item ); - } - - else - if ( ( current->right != 0 ) && ( direction != RIGHT ) ) - { - current = current->right; - direction = ROOT; - - // Everytime you move a node print it - return_array[ index++ ] = *( current->item ); - } - - else - { - // Otherwise I've done everything I can. Move up the tree one node - parent = FindParent( *( current->item ) ); - current = parent; - } - } - } - - template - inline void BinarySearchTree::DisplayPostorder( BinarySearchTreeType* return_array ) - { - unsigned int index = 0; - - if ( BinarySearchTree_size == 0 ) - return ; // Do nothing for an empty tree - - else - if ( BinarySearchTree_size == 1 ) - { - return_array[ 0 ] = *( root->item ); - return ; - } - - DisplayPostorderRecursive( root, return_array, index ); - } - - - // Recursively do a postorder traversal - template - void BinarySearchTree::DisplayPostorderRecursive( typename BinarySearchTree::node* current, BinarySearchTreeType* return_array, unsigned int& index ) - { - if ( current->left != 0 ) - DisplayPostorderRecursive( current->left, return_array, index ); - - if ( current->right != 0 ) - DisplayPostorderRecursive( current->right, return_array, index ); - - return_array[ index++ ] = *( current->item ); - - } - - - template - void BinarySearchTree::DisplayBreadthFirstSearch( BinarySearchTreeType* return_array ) - { - typename BinarySearchTree::node * current; - unsigned int index = 0; - - // Display the tree using a breadth first search - // Put the children of the current node into the queue - // Pop the queue, put its children into the queue, repeat until queue is empty - - if ( BinarySearchTree_size == 0 ) - return ; // Do nothing for an empty tree - - else - if ( BinarySearchTree_size == 1 ) - { - return_array[ 0 ] = *( root->item ); - return ; - } - - else - { - DataStructures::QueueLinkedList tree_queue; - - // Add the root of the tree I am copying from - tree_queue.Push( root ); - - do - { - current = tree_queue.Pop(); - return_array[ index++ ] = *( current->item ); - - // Add the child or children of the tree I am copying from to the queue - - if ( current->left != 0 ) - tree_queue.Push( current->left ); - - if ( current->right != 0 ) - tree_queue.Push( current->right ); - - } - - while ( tree_queue.Size() > 0 ); - } - } - - - template - BinarySearchTree::BinarySearchTree( const BinarySearchTree& original_copy ) - { - typename BinarySearchTree::node * current; - // Copy the tree using a breadth first search - // Put the children of the current node into the queue - // Pop the queue, put its children into the queue, repeat until queue is empty - - // This is a copy of the constructor. A bug in Visual C++ made it so if I just put the constructor call here the variable assignments were ignored. - BinarySearchTree_size = 0; - root = 0; - - if ( original_copy.BinarySearchTree_size == 0 ) - { - BinarySearchTree_size = 0; - } - - else - { - DataStructures::QueueLinkedList tree_queue; - - // Add the root of the tree I am copying from - tree_queue.Push( original_copy.root ); - - do - { - current = tree_queue.Pop(); - - Add ( *( current->item ), _FILE_AND_LINE_ ) - - ; - - // Add the child or children of the tree I am copying from to the queue - if ( current->left != 0 ) - tree_queue.Push( current->left ); - - if ( current->right != 0 ) - tree_queue.Push( current->right ); - - } - - while ( tree_queue.Size() > 0 ); - } - } - - template - BinarySearchTree& BinarySearchTree::operator= ( const BinarySearchTree& original_copy ) - { - typename BinarySearchTree::node * current; - - if ( ( &original_copy ) == this ) - return *this; - - Clear( _FILE_AND_LINE_ ); // Remove the current tree - - // This is a copy of the constructor. A bug in Visual C++ made it so if I just put the constructor call here the variable assignments were ignored. - BinarySearchTree_size = 0; - - root = 0; - - - // Copy the tree using a breadth first search - // Put the children of the current node into the queue - // Pop the queue, put its children into the queue, repeat until queue is empty - if ( original_copy.BinarySearchTree_size == 0 ) - { - BinarySearchTree_size = 0; - } - - else - { - DataStructures::QueueLinkedList tree_queue; - - // Add the root of the tree I am copying from - tree_queue.Push( original_copy.root ); - - do - { - current = tree_queue.Pop(); - - Add ( *( current->item ), _FILE_AND_LINE_ ) - - ; - - // Add the child or children of the tree I am copying from to the queue - if ( current->left != 0 ) - tree_queue.Push( current->left ); - - if ( current->right != 0 ) - tree_queue.Push( current->right ); - - } - - while ( tree_queue.Size() > 0 ); - } - - return *this; - } - - template - inline void BinarySearchTree::Clear ( const char *file, unsigned int line ) - { - typename BinarySearchTree::node * current, *parent; - - current = root; - - while ( BinarySearchTree_size > 0 ) - { - if ( BinarySearchTree_size == 1 ) - { - RakNet::OP_DELETE(root->item, file, line); - RakNet::OP_DELETE(root, file, line); - root = 0; - BinarySearchTree_size = 0; - } - - else - { - if ( current->left != 0 ) - { - current = current->left; - } - - else - if ( current->right != 0 ) - { - current = current->right; - } - - else // leaf - { - // Not root node so must have a parent - parent = FindParent( *( current->item ) ); - - if ( ( parent->left ) == current ) - parent->left = 0; - else - parent->right = 0; - - RakNet::OP_DELETE(current->item, file, line); - - RakNet::OP_DELETE(current, file, line); - - current = parent; - - BinarySearchTree_size--; - } - } - } - } - -} // End namespace - -#endif - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_BinarySearchTree.h +/// \internal +/// \brief A binary search tree, and an AVL balanced BST derivation. +/// + + +#pragma once + +#include "DS_QueueLinkedList.h" +#include "RakMemoryOverride.h" +#include "Export.h" + + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + /** + * \brief A binary search tree and an AVL balanced binary search tree. + * \details + * Initilize with the following structure + * + * BinarySearchTree + * + * OR + * + * AVLBalancedBinarySearchTree + * + * Use the AVL balanced tree if you want the tree to be balanced after every deletion and addition. This avoids the potential + * worst case scenario where ordered input to a binary search tree gives linear search time results. It's not needed + * if input will be evenly distributed, in which case the search time is O (log n). The search time for the AVL + * balanced binary tree is O (log n) irregardless of input. + * + * Has the following member functions + * unsigned int Height() - Returns the height of the tree at the optional specified starting index. Default is the root + * add(element) - adds an element to the BinarySearchTree + * bool del(element) - deletes the node containing element if the element is in the tree as defined by a comparison with the == operator. Returns true on success, false if the element is not found + * bool IsInelement) - returns true if element is in the tree as defined by a comparison with the == operator. Otherwise returns false + * DisplayInorder(array) - Fills an array with an inorder search of the elements in the tree. USER IS REPONSIBLE FOR ALLOCATING THE ARRAY!. + * DisplayPreorder(array) - Fills an array with an preorder search of the elements in the tree. USER IS REPONSIBLE FOR ALLOCATING THE ARRAY!. + * DisplayPostorder(array) - Fills an array with an postorder search of the elements in the tree. USER IS REPONSIBLE FOR ALLOCATING THE ARRAY!. + * DisplayBreadthFirstSearch(array) - Fills an array with a breadth first search of the elements in the tree. USER IS REPONSIBLE FOR ALLOCATING THE ARRAY!. + * clear - Destroys the tree. Same as calling the destructor + * unsigned int Height() - Returns the height of the tree + * unsigned int size() - returns the size of the BinarySearchTree + * GetPointerToNode(element) - returns a pointer to the comparision element in the tree, allowing for direct modification when necessary with complex data types. + * Be warned, it is possible to corrupt the tree if the element used for comparisons is modified. Returns nullptr if the item is not found + * + * + * EXAMPLE + * @code + * BinarySearchTree A; + * A.Add(10); + * A.Add(15); + * A.Add(5); + * int* array = RakNet::OP_NEW(A.Size(), _FILE_AND_LINE_ ); + * A.DisplayInorder(array); + * array[0]; // returns 5 + * array[1]; // returns 10 + * array[2]; // returns 15 + * @endcode + * compress - reallocates memory to fit the number of elements. Best used when the number of elements decreases + * + * clear - empties the BinarySearchTree and returns storage + * The assignment and copy constructors are defined + * + * \note The template type must have the copy constructor and + * assignment operator defined and must work with >, <, and == All + * elements in the tree MUST be distinct The assignment operator is + * defined between BinarySearchTree and AVLBalancedBinarySearchTree + * as long as they are of the same template type. However, passing a + * BinarySearchTree to an AVLBalancedBinarySearchTree will lose its + * structure unless it happened to be AVL balanced to begin with + * Requires queue_linked_list.cpp for the breadth first search used + * in the copy constructor, overloaded assignment operator, and + * display_breadth_first_search. + * + * + */ + template + class RAK_DLL_EXPORT BinarySearchTree + { + + public: + + struct node + { + BinarySearchTreeType* item; + node* left; + node* right; + }; + + BinarySearchTree(); + virtual ~BinarySearchTree(); + BinarySearchTree( const BinarySearchTree& original_type ); + BinarySearchTree& operator= ( const BinarySearchTree& original_copy ); + unsigned int Size( void ); + void Clear( const char *file, unsigned int line ); + unsigned int Height( node* starting_node = 0 ); + node* Add ( const BinarySearchTreeType& input, const char *file, unsigned int line ); + node* Del( const BinarySearchTreeType& input, const char *file, unsigned int line ); + bool IsIn( const BinarySearchTreeType& input ); + void DisplayInorder( BinarySearchTreeType* return_array ); + void DisplayPreorder( BinarySearchTreeType* return_array ); + void DisplayPostorder( BinarySearchTreeType* return_array ); + void DisplayBreadthFirstSearch( BinarySearchTreeType* return_array ); + BinarySearchTreeType*& GetPointerToNode( const BinarySearchTreeType& element ); + + protected: + + node* root; + + enum Direction_Types + { + NOT_FOUND, LEFT, RIGHT, ROOT + } direction; + unsigned int HeightRecursive( node* current ); + unsigned int BinarySearchTree_size; + node*& Find( const BinarySearchTreeType& element, node** parent ); + node*& FindParent( const BinarySearchTreeType& element ); + void DisplayPostorderRecursive( node* current, BinarySearchTreeType* return_array, unsigned int& index ); + void FixTree( node* current ); + + }; + + /// An AVLBalancedBinarySearchTree is a binary tree that is always balanced + template + class RAK_DLL_EXPORT AVLBalancedBinarySearchTree : public BinarySearchTree + { + + public: + AVLBalancedBinarySearchTree() {} + virtual ~AVLBalancedBinarySearchTree(); + void Add ( const BinarySearchTreeType& input ); + void Del( const BinarySearchTreeType& input ); + BinarySearchTree& operator= ( BinarySearchTree& original_copy ) + { + return BinarySearchTree::operator= ( original_copy ); + } + + private: + void BalanceTree( typename BinarySearchTree::node* current, bool rotateOnce ); + void RotateRight( typename BinarySearchTree::node *C ); + void RotateLeft( typename BinarySearchTree::node* C ); + void DoubleRotateRight( typename BinarySearchTree::node *A ); + void DoubleRotateLeft( typename BinarySearchTree::node* A ); + bool RightHigher( typename BinarySearchTree::node* A ); + bool LeftHigher( typename BinarySearchTree::node* A ); + }; + + template + void AVLBalancedBinarySearchTree::BalanceTree( typename BinarySearchTree::node* current, bool rotateOnce ) + { + int left_height, right_height; + + while ( current ) + { + if ( current->left == 0 ) + left_height = 0; + else + left_height = Height( current->left ); + + if ( current->right == 0 ) + right_height = 0; + else + right_height = Height( current->right ); + + if ( right_height - left_height == 2 ) + { + if ( RightHigher( current->right ) ) + RotateLeft( current->right ); + else + DoubleRotateLeft( current ); + + if ( rotateOnce ) + break; + } + + else + if ( right_height - left_height == -2 ) + { + if ( LeftHigher( current->left ) ) + RotateRight( current->left ); + else + DoubleRotateRight( current ); + + if ( rotateOnce ) + break; + } + + if ( current == this->root ) + break; + + current = FindParent( *( current->item ) ); + + } + } + + template + void AVLBalancedBinarySearchTree::Add ( const BinarySearchTreeType& input ) + { + + typename BinarySearchTree::node * current = BinarySearchTree::Add ( input, _FILE_AND_LINE_ ); + BalanceTree( current, true ); + } + + template + void AVLBalancedBinarySearchTree::Del( const BinarySearchTreeType& input ) + { + typename BinarySearchTree::node * current = BinarySearchTree::Del( input, _FILE_AND_LINE_ ); + BalanceTree( current, false ); + + } + + template + bool AVLBalancedBinarySearchTree::RightHigher( typename BinarySearchTree::node *A ) + { + if ( A == 0 ) + return false; + + return Height( A->right ) > Height( A->left ); + } + + template + bool AVLBalancedBinarySearchTree::LeftHigher( typename BinarySearchTree::node *A ) + { + if ( A == 0 ) + return false; + + return Height( A->left ) > Height( A->right ); + } + + template + void AVLBalancedBinarySearchTree::RotateRight( typename BinarySearchTree::node *C ) + { + typename BinarySearchTree::node * A, *B, *D; + /* + RIGHT ROTATION + + A = parent(b) + b= parent(c) + c = node to rotate around + + A + | // Either direction + B + / \ + C + / \ + D + + TO + + A + | // Either Direction + C + / \ + B + / \ + D + + + + + */ + + B = FindParent( *( C->item ) ); + A = FindParent( *( B->item ) ); + D = C->right; + + if ( A ) + { + // Direction was set by the last find_parent call + + if ( this->direction == this->LEFT ) + A->left = C; + else + A->right = C; + } + + else + this->root = C; // If B has no parent parent then B must have been the root node + + B->left = D; + + C->right = B; + } + + template + void AVLBalancedBinarySearchTree::DoubleRotateRight( typename BinarySearchTree::node *A ) + { + // The left side of the left child must be higher for the tree to balance with a right rotation. If it isn't, rotate it left before the normal rotation so it is. + RotateLeft( A->left->right ); + RotateRight( A->left ); + } + + template + void AVLBalancedBinarySearchTree::RotateLeft( typename BinarySearchTree::node *C ) + { + typename BinarySearchTree::node * A, *B, *D; + /* + RIGHT ROTATION + + A = parent(b) + b= parent(c) + c = node to rotate around + + A + | // Either direction + B + / \ + C + / \ + D + + TO + + A + | // Either Direction + C + / \ + B + / \ + D + + + + + */ + + B = FindParent( *( C->item ) ); + A = FindParent( *( B->item ) ); + D = C->left; + + if ( A ) + { + // Direction was set by the last find_parent call + + if ( this->direction == this->LEFT ) + A->left = C; + else + A->right = C; + } + + else + this->root = C; // If B has no parent parent then B must have been the root node + + B->right = D; + + C->left = B; + } + + template + void AVLBalancedBinarySearchTree::DoubleRotateLeft( typename BinarySearchTree::node *A ) + { + // The left side of the right child must be higher for the tree to balance with a left rotation. If it isn't, rotate it right before the normal rotation so it is. + RotateRight( A->right->left ); + RotateLeft( A->right ); + } + + template + AVLBalancedBinarySearchTree::~AVLBalancedBinarySearchTree() + { + this->Clear(_FILE_AND_LINE_); + } + + template + unsigned int BinarySearchTree::Size( void ) + { + return BinarySearchTree_size; + } + + template + unsigned int BinarySearchTree::Height( typename BinarySearchTree::node* starting_node ) + { + if ( BinarySearchTree_size == 0 || starting_node == 0 ) + return 0; + else + return HeightRecursive( starting_node ); + } + + // Recursively return the height of a binary tree + template + unsigned int BinarySearchTree::HeightRecursive( typename BinarySearchTree::node* current ) + { + unsigned int left_height = 0, right_height = 0; + + if ( ( current->left == 0 ) && ( current->right == 0 ) ) + return 1; // Leaf + + if ( current->left != 0 ) + left_height = 1 + HeightRecursive( current->left ); + + if ( current->right != 0 ) + right_height = 1 + HeightRecursive( current->right ); + + if ( left_height > right_height ) + return left_height; + else + return right_height; + } + + template + BinarySearchTree::BinarySearchTree() + { + BinarySearchTree_size = 0; + root = 0; + } + + template + BinarySearchTree::~BinarySearchTree() + { + this->Clear(_FILE_AND_LINE_); + } + + template + BinarySearchTreeType*& BinarySearchTree::GetPointerToNode( const BinarySearchTreeType& element ) + { + static typename BinarySearchTree::node * tempnode; + static BinarySearchTreeType* dummyptr = 0; + tempnode = Find ( element, &tempnode ); + + if ( this->direction == this->NOT_FOUND ) + return dummyptr; + + return tempnode->item; + } + + template + typename BinarySearchTree::node*& BinarySearchTree::Find( const BinarySearchTreeType& element, typename BinarySearchTree::node** parent ) + { + static typename BinarySearchTree::node * current; + + current = this->root; + *parent = 0; + this->direction = this->ROOT; + + if ( BinarySearchTree_size == 0 ) + { + this->direction = this->NOT_FOUND; + return current = 0; + } + + // Check if the item is at the root + if ( element == *( current->item ) ) + { + this->direction = this->ROOT; + return current; + } + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + while ( true ) + { + // Move pointer + + if ( element < *( current->item ) ) + { + *parent = current; + this->direction = this->LEFT; + current = current->left; + } + + else + if ( element > *( current->item ) ) + { + *parent = current; + this->direction = this->RIGHT; + current = current->right; + } + + if ( current == 0 ) + break; + + // Check if new position holds the item + if ( element == *( current->item ) ) + { + return current; + } + } + + + this->direction = this->NOT_FOUND; + return current = 0; + } + + template + typename BinarySearchTree::node*& BinarySearchTree::FindParent( const BinarySearchTreeType& element ) + { + static typename BinarySearchTree::node * parent; + Find ( element, &parent ); + return parent; + } + + // Performs a series of value swaps starting with current to fix the tree if needed + template + void BinarySearchTree::FixTree( typename BinarySearchTree::node* current ) + { + BinarySearchTreeType temp; + + while ( 1 ) + { + if ( ( ( current->left ) != 0 ) && ( *( current->item ) < *( current->left->item ) ) ) + { + // Swap the current value with the one to the left + temp = *( current->left->item ); + *( current->left->item ) = *( current->item ); + *( current->item ) = temp; + current = current->left; + } + + else + if ( ( ( current->right ) != 0 ) && ( *( current->item ) > *( current->right->item ) ) ) + { + // Swap the current value with the one to the right + temp = *( current->right->item ); + *( current->right->item ) = *( current->item ); + *( current->item ) = temp; + current = current->right; + } + + else + break; // current points to the right place so quit + } + } + + template + typename BinarySearchTree::node* BinarySearchTree::Del( const BinarySearchTreeType& input, const char *file, unsigned int line ) + { + typename BinarySearchTree::node * node_to_delete, *current, *parent; + + if ( BinarySearchTree_size == 0 ) + return 0; + + if ( BinarySearchTree_size == 1 ) + { + Clear(file, line); + return 0; + } + + node_to_delete = Find( input, &parent ); + + if ( direction == NOT_FOUND ) + return 0; // Couldn't find the element + + current = node_to_delete; + + // Replace the deleted node with the appropriate value + if ( ( current->right ) == 0 && ( current->left ) == 0 ) // Leaf node, just remove it + { + + if ( parent ) + { + if ( direction == LEFT ) + parent->left = 0; + else + parent->right = 0; + } + + RakNet::OP_DELETE(node_to_delete->item, file, line); + RakNet::OP_DELETE(node_to_delete, file, line); + BinarySearchTree_size--; + return parent; + } + else + if ( ( current->right ) != 0 && ( current->left ) == 0 ) // Node has only one child, delete it and cause the parent to point to that child + { + + if ( parent ) + { + if ( direction == RIGHT ) + parent->right = current->right; + else + parent->left = current->right; + } + + else + root = current->right; // Without a parent this must be the root node + + RakNet::OP_DELETE(node_to_delete->item, file, line); + + RakNet::OP_DELETE(node_to_delete, file, line); + + BinarySearchTree_size--; + + return parent; + } + else + if ( ( current->right ) == 0 && ( current->left ) != 0 ) // Node has only one child, delete it and cause the parent to point to that child + { + + if ( parent ) + { + if ( direction == RIGHT ) + parent->right = current->left; + else + parent->left = current->left; + } + + else + root = current->left; // Without a parent this must be the root node + + RakNet::OP_DELETE(node_to_delete->item, file, line); + + RakNet::OP_DELETE(node_to_delete, file, line); + + BinarySearchTree_size--; + + return parent; + } + else // Go right, then as left as far as you can + { + parent = current; + direction = RIGHT; + current = current->right; // Must have a right branch because the if statements above indicated that it has 2 branches + + while ( current->left ) + { + direction = LEFT; + parent = current; + current = current->left; + } + + // Replace the value held by the node to RakNet::OP_DELETE(with the value pointed to by current, _FILE_AND_LINE_); + *( node_to_delete->item ) = *( current->item ); + + // Delete current. + // If it is a leaf node just delete it + if ( current->right == 0 ) + { + if ( direction == RIGHT ) + parent->right = 0; + else + parent->left = 0; + + RakNet::OP_DELETE(current->item, file, line); + + RakNet::OP_DELETE(current, file, line); + + BinarySearchTree_size--; + + return parent; + } + + else + { + // Skip this node and make its parent point to its right branch + + if ( direction == RIGHT ) + parent->right = current->right; + else + parent->left = current->right; + + RakNet::OP_DELETE(current->item, file, line); + + RakNet::OP_DELETE(current, file, line); + + BinarySearchTree_size--; + + return parent; + } + } + } + + template + typename BinarySearchTree::node* BinarySearchTree::Add ( const BinarySearchTreeType& input, const char *file, unsigned int line ) + { + typename BinarySearchTree::node * current; + + // Add the new element to the tree according to the following alogrithm: + // 1. If the current node is empty add the new leaf + // 2. If the element is less than the current node then go down the left branch + // 3. If the element is greater than the current node then go down the right branch + + if ( BinarySearchTree_size == 0 ) + { + BinarySearchTree_size = 1; + root = RakNet::OP_NEW( file, line ); + root->item = RakNet::OP_NEW( file, line ); + *( root->item ) = input; + root->left = 0; + root->right = 0; + + return root; + } + + else + { + // start at the root + current = root; + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + while ( true ) // This loop traverses the tree to find a spot for insertion + { + + if ( input < *( current->item ) ) + { + if ( current->left == 0 ) + { + current->left = RakNet::OP_NEW( file, line ); + current->left->item = RakNet::OP_NEW( file, line ); + current = current->left; + current->left = 0; + current->right = 0; + *( current->item ) = input; + + BinarySearchTree_size++; + return current; + } + + else + { + current = current->left; + } + } + + else + if ( input > *( current->item ) ) + { + if ( current->right == 0 ) + { + current->right = RakNet::OP_NEW( file, line ); + current->right->item = RakNet::OP_NEW( file, line ); + current = current->right; + current->left = 0; + current->right = 0; + *( current->item ) = input; + + BinarySearchTree_size++; + return current; + } + + else + { + current = current->right; + } + } + + else + return 0; // ((input == current->item) == true) which is not allowed since the tree only takes discrete values. Do nothing + } + } + } + + template + bool BinarySearchTree::IsIn( const BinarySearchTreeType& input ) + { + typename BinarySearchTree::node * parent; + find( input, &parent ); + + if ( direction != NOT_FOUND ) + return true; + else + return false; + } + + + template + void BinarySearchTree::DisplayInorder( BinarySearchTreeType* return_array ) + { + typename BinarySearchTree::node * current, *parent; + bool just_printed = false; + + unsigned int index = 0; + + current = root; + + if ( BinarySearchTree_size == 0 ) + return ; // Do nothing for an empty tree + + else + if ( BinarySearchTree_size == 1 ) + { + return_array[ 0 ] = *( root->item ); + return ; + } + + + direction = ROOT; // Reset the direction + + while ( index != BinarySearchTree_size ) + { + // direction is set by the find function and holds the direction of the parent to the last node visited. It is used to prevent revisiting nodes + + if ( ( current->left != 0 ) && ( direction != LEFT ) && ( direction != RIGHT ) ) + { + // Go left if the following 2 conditions are true + // I can go left + // I did not just move up from a right child + // I did not just move up from a left child + + current = current->left; + direction = ROOT; // Reset the direction + } + + else + if ( ( direction != RIGHT ) && ( just_printed == false ) ) + { + // Otherwise, print the current node if the following 3 conditions are true: + // I did not just move up from a right child + // I did not print this ndoe last cycle + + return_array[ index++ ] = *( current->item ); + just_printed = true; + } + + else + if ( ( current->right != 0 ) && ( direction != RIGHT ) ) + { + // Otherwise, go right if the following 2 conditions are true + // I did not just move up from a right child + // I can go right + + current = current->right; + direction = ROOT; // Reset the direction + just_printed = false; + } + + else + { + // Otherwise I've done everything I can. Move up the tree one node + parent = FindParent( *( current->item ) ); + current = parent; + just_printed = false; + } + } + } + + template + void BinarySearchTree::DisplayPreorder( BinarySearchTreeType* return_array ) + { + typename BinarySearchTree::node * current, *parent; + + unsigned int index = 0; + + current = root; + + if ( BinarySearchTree_size == 0 ) + return ; // Do nothing for an empty tree + + else + if ( BinarySearchTree_size == 1 ) + { + return_array[ 0 ] = *( root->item ); + return ; + } + + + direction = ROOT; // Reset the direction + return_array[ index++ ] = *( current->item ); + + while ( index != BinarySearchTree_size ) + { + // direction is set by the find function and holds the direction of the parent to the last node visited. It is used to prevent revisiting nodes + + if ( ( current->left != 0 ) && ( direction != LEFT ) && ( direction != RIGHT ) ) + { + + current = current->left; + direction = ROOT; + + // Everytime you move a node print it + return_array[ index++ ] = *( current->item ); + } + + else + if ( ( current->right != 0 ) && ( direction != RIGHT ) ) + { + current = current->right; + direction = ROOT; + + // Everytime you move a node print it + return_array[ index++ ] = *( current->item ); + } + + else + { + // Otherwise I've done everything I can. Move up the tree one node + parent = FindParent( *( current->item ) ); + current = parent; + } + } + } + + template + inline void BinarySearchTree::DisplayPostorder( BinarySearchTreeType* return_array ) + { + unsigned int index = 0; + + if ( BinarySearchTree_size == 0 ) + return ; // Do nothing for an empty tree + + else + if ( BinarySearchTree_size == 1 ) + { + return_array[ 0 ] = *( root->item ); + return ; + } + + DisplayPostorderRecursive( root, return_array, index ); + } + + + // Recursively do a postorder traversal + template + void BinarySearchTree::DisplayPostorderRecursive( typename BinarySearchTree::node* current, BinarySearchTreeType* return_array, unsigned int& index ) + { + if ( current->left != 0 ) + DisplayPostorderRecursive( current->left, return_array, index ); + + if ( current->right != 0 ) + DisplayPostorderRecursive( current->right, return_array, index ); + + return_array[ index++ ] = *( current->item ); + + } + + + template + void BinarySearchTree::DisplayBreadthFirstSearch( BinarySearchTreeType* return_array ) + { + typename BinarySearchTree::node * current; + unsigned int index = 0; + + // Display the tree using a breadth first search + // Put the children of the current node into the queue + // Pop the queue, put its children into the queue, repeat until queue is empty + + if ( BinarySearchTree_size == 0 ) + return ; // Do nothing for an empty tree + + else + if ( BinarySearchTree_size == 1 ) + { + return_array[ 0 ] = *( root->item ); + return ; + } + + else + { + DataStructures::QueueLinkedList tree_queue; + + // Add the root of the tree I am copying from + tree_queue.Push( root ); + + do + { + current = tree_queue.Pop(); + return_array[ index++ ] = *( current->item ); + + // Add the child or children of the tree I am copying from to the queue + + if ( current->left != 0 ) + tree_queue.Push( current->left ); + + if ( current->right != 0 ) + tree_queue.Push( current->right ); + + } + + while ( tree_queue.Size() > 0 ); + } + } + + + template + BinarySearchTree::BinarySearchTree( const BinarySearchTree& original_copy ) + { + typename BinarySearchTree::node * current; + // Copy the tree using a breadth first search + // Put the children of the current node into the queue + // Pop the queue, put its children into the queue, repeat until queue is empty + + // This is a copy of the constructor. A bug in Visual C++ made it so if I just put the constructor call here the variable assignments were ignored. + BinarySearchTree_size = 0; + root = 0; + + if ( original_copy.BinarySearchTree_size == 0 ) + { + BinarySearchTree_size = 0; + } + + else + { + DataStructures::QueueLinkedList tree_queue; + + // Add the root of the tree I am copying from + tree_queue.Push( original_copy.root ); + + do + { + current = tree_queue.Pop(); + + Add ( *( current->item ), _FILE_AND_LINE_ ) + + ; + + // Add the child or children of the tree I am copying from to the queue + if ( current->left != 0 ) + tree_queue.Push( current->left ); + + if ( current->right != 0 ) + tree_queue.Push( current->right ); + + } + + while ( tree_queue.Size() > 0 ); + } + } + + template + BinarySearchTree& BinarySearchTree::operator= ( const BinarySearchTree& original_copy ) + { + typename BinarySearchTree::node * current; + + if ( ( &original_copy ) == this ) + return *this; + + Clear( _FILE_AND_LINE_ ); // Remove the current tree + + // This is a copy of the constructor. A bug in Visual C++ made it so if I just put the constructor call here the variable assignments were ignored. + BinarySearchTree_size = 0; + + root = 0; + + + // Copy the tree using a breadth first search + // Put the children of the current node into the queue + // Pop the queue, put its children into the queue, repeat until queue is empty + if ( original_copy.BinarySearchTree_size == 0 ) + { + BinarySearchTree_size = 0; + } + + else + { + DataStructures::QueueLinkedList tree_queue; + + // Add the root of the tree I am copying from + tree_queue.Push( original_copy.root ); + + do + { + current = tree_queue.Pop(); + + Add ( *( current->item ), _FILE_AND_LINE_ ) + + ; + + // Add the child or children of the tree I am copying from to the queue + if ( current->left != 0 ) + tree_queue.Push( current->left ); + + if ( current->right != 0 ) + tree_queue.Push( current->right ); + + } + + while ( tree_queue.Size() > 0 ); + } + + return *this; + } + + template + inline void BinarySearchTree::Clear ( const char *file, unsigned int line ) + { + typename BinarySearchTree::node * current, *parent; + + current = root; + + while ( BinarySearchTree_size > 0 ) + { + if ( BinarySearchTree_size == 1 ) + { + RakNet::OP_DELETE(root->item, file, line); + RakNet::OP_DELETE(root, file, line); + root = 0; + BinarySearchTree_size = 0; + } + + else + { + if ( current->left != 0 ) + { + current = current->left; + } + + else + if ( current->right != 0 ) + { + current = current->right; + } + + else // leaf + { + // Not root node so must have a parent + parent = FindParent( *( current->item ) ); + + if ( ( parent->left ) == current ) + parent->left = 0; + else + parent->right = 0; + + RakNet::OP_DELETE(current->item, file, line); + + RakNet::OP_DELETE(current, file, line); + + current = parent; + + BinarySearchTree_size--; + } + } + } + } + +} // End namespace + +#endif + +#ifdef _MSC_VER +#pragma warning( pop ) diff --git a/Source/DS_BytePool.h b/Source/DS_BytePool.h index 91751f866..903fa33a4 100644 --- a/Source/DS_BytePool.h +++ b/Source/DS_BytePool.h @@ -1,54 +1,52 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_BytePool.h -/// - - -#ifndef __BYTE_POOL_H -#define __BYTE_POOL_H - -#include "RakMemoryOverride.h" -#include "DS_MemoryPool.h" -#include "Export.h" -#include "SimpleMutex.h" -#include "RakAssert.h" - -// #define _DISABLE_BYTE_POOL -// #define _THREADSAFE_BYTE_POOL - -namespace DataStructures -{ - // Allocate some number of bytes from pools. Uses the heap if necessary. - class RAK_DLL_EXPORT BytePool - { - public: - BytePool(); - ~BytePool(); - // Should be at least 8 times bigger than 8192 - void SetPageSize(int size); - unsigned char* Allocate(int bytesWanted, const char *file, unsigned int line); - void Release(unsigned char *data, const char *file, unsigned int line); - void Clear(const char *file, unsigned int line); - protected: - MemoryPool pool128; - MemoryPool pool512; - MemoryPool pool2048; - MemoryPool pool8192; -#ifdef _THREADSAFE_BYTE_POOL - SimpleMutex mutex128; - SimpleMutex mutex512; - SimpleMutex mutex2048; - SimpleMutex mutex8192; -#endif - }; -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_BytePool.h +/// + + +#pragma once + +#include "RakMemoryOverride.h" +#include "DS_MemoryPool.h" +#include "Export.h" +#include "SimpleMutex.h" +#include "RakAssert.h" + +// #define _DISABLE_BYTE_POOL +// #define _THREADSAFE_BYTE_POOL + +namespace DataStructures +{ + // Allocate some number of bytes from pools. Uses the heap if necessary. + class RAK_DLL_EXPORT BytePool + { + public: + BytePool(); + ~BytePool(); + // Should be at least 8 times bigger than 8192 + void SetPageSize(int size); + unsigned char* Allocate(int bytesWanted, const char *file, unsigned int line); + void Release(unsigned char *data, const char *file, unsigned int line); + void Clear(const char *file, unsigned int line); + protected: + MemoryPool pool128; + MemoryPool pool512; + MemoryPool pool2048; + MemoryPool pool8192; +#ifdef _THREADSAFE_BYTE_POOL + SimpleMutex mutex128; + SimpleMutex mutex512; + SimpleMutex mutex2048; + SimpleMutex mutex8192; +#endif + }; +} + diff --git a/Source/DS_ByteQueue.h b/Source/DS_ByteQueue.h index 02e2e2bbf..c3fce4228 100644 --- a/Source/DS_ByteQueue.h +++ b/Source/DS_ByteQueue.h @@ -1,47 +1,45 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_ByteQueue.h -/// \internal -/// \brief Byte queue -/// - - -#ifndef __BYTE_QUEUE_H -#define __BYTE_QUEUE_H - -#include "RakMemoryOverride.h" -#include "Export.h" - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - class ByteQueue - { - public: - ByteQueue(); - ~ByteQueue(); - void WriteBytes(const char *in, unsigned length, const char *file, unsigned int line); - bool ReadBytes(char *out, unsigned maxLengthToRead, bool peek); - unsigned GetBytesWritten(void) const; - char* PeekContiguousBytes(unsigned int *outLength) const; - void IncrementReadOffset(unsigned length); - void DecrementReadOffset(unsigned length); - void Clear(const char *file, unsigned int line); - void Print(void); - - protected: - char *data; - unsigned readOffset, writeOffset, lengthAllocated; - }; -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_ByteQueue.h +/// \internal +/// \brief Byte queue +/// + + +#pragma once + +#include "RakMemoryOverride.h" +#include "Export.h" + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + class ByteQueue + { + public: + ByteQueue(); + ~ByteQueue(); + void WriteBytes(const char *in, unsigned length, const char *file, unsigned int line); + bool ReadBytes(char *out, unsigned maxLengthToRead, bool peek); + unsigned GetBytesWritten(void) const; + char* PeekContiguousBytes(unsigned int *outLength) const; + void IncrementReadOffset(unsigned length); + void DecrementReadOffset(unsigned length); + void Clear(const char *file, unsigned int line); + void Print(void); + + protected: + char *data; + unsigned readOffset, writeOffset, lengthAllocated; + }; +} + diff --git a/Source/DS_Hash.h b/Source/DS_Hash.h index 46c0ccacf..5b4e15a1e 100644 --- a/Source/DS_Hash.h +++ b/Source/DS_Hash.h @@ -1,357 +1,355 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \internal -/// \brief Hashing container -/// - - -#ifndef __HASH_H -#define __HASH_H - -#include "RakAssert.h" -#include // memmove -#include "Export.h" -#include "RakMemoryOverride.h" -#include "RakString.h" - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - struct HashIndex - { - unsigned int primaryIndex; - unsigned int secondaryIndex; - bool IsInvalid(void) const {return primaryIndex==(unsigned int) -1;} - void SetInvalid(void) {primaryIndex=(unsigned int) -1; secondaryIndex=(unsigned int) -1;} - }; - - /// \brief Using a string as a identifier for a node, store an allocated pointer to that node - template - class RAK_DLL_EXPORT Hash - { - public: - /// Default constructor - Hash(); - - // Destructor - ~Hash(); - - void Push(key_type key, const data_type &input, const char *file, unsigned int line ); - data_type* Peek(key_type key ); - bool Pop(data_type& out, key_type key, const char *file, unsigned int line ); - bool RemoveAtIndex(HashIndex index, const char *file, unsigned int line ); - bool Remove(key_type key, const char *file, unsigned int line ); - HashIndex GetIndexOf(key_type key); - bool HasData(key_type key); - data_type& ItemAtIndex(const HashIndex &index); - key_type KeyAtIndex(const HashIndex &index); - void GetAsList(DataStructures::List &itemList,DataStructures::List &keyList,const char *file, unsigned int line) const; - unsigned int Size(void) const; - - /// \brief Clear the list - void Clear( const char *file, unsigned int line ); - - struct Node - { - Node(key_type strIn, const data_type &_data) {string=strIn; data=_data;} - key_type string; - data_type data; - // Next in the list for this key - Node *next; - }; - - protected: - void ClearIndex(unsigned int index,const char *file, unsigned int line); - Node **nodeList; - unsigned int size; - }; - - template - Hash::Hash() - { - nodeList=0; - size=0; - } - - template - Hash::~Hash() - { - Clear(_FILE_AND_LINE_); - } - - template - void Hash::Push(key_type key, const data_type &input, const char *file, unsigned int line ) - { - unsigned long hashIndex = (*hashFunction)(key) % HASH_SIZE; - if (nodeList==0) - { - nodeList=RakNet::OP_NEW_ARRAY(HASH_SIZE,file,line); - memset(nodeList,0,sizeof(Node *)*HASH_SIZE); - } - - Node *newNode=RakNet::OP_NEW_2(file,line,key,input); - newNode->next=nodeList[hashIndex]; - nodeList[hashIndex]=newNode; - - size++; - } - - template - data_type* Hash::Peek(key_type key ) - { - if (nodeList==0) - return 0; - - unsigned long hashIndex = (*hashFunction)(key) % HASH_SIZE; - Node *node = nodeList[hashIndex]; - while (node!=0) - { - if (node->string==key) - return &node->data; - node=node->next; - } - return 0; - } - - template - bool Hash::Pop(data_type& out, key_type key, const char *file, unsigned int line ) - { - if (nodeList==0) - return false; - - unsigned long hashIndex = (*hashFunction)(key) % HASH_SIZE; - Node *node = nodeList[hashIndex]; - if (node==0) - return false; - if (node->next==0) - { - // Only one item. - if (node->string==key) - { - // Delete last item - out=node->data; - ClearIndex(hashIndex,_FILE_AND_LINE_); - return true; - } - else - { - // Single item doesn't match - return false; - } - } - else if (node->string==key) - { - // First item does match, but more than one item - out=node->data; - nodeList[hashIndex]=node->next; - RakNet::OP_DELETE(node,file,line); - size--; - return true; - } - - Node *last=node; - node=node->next; - - while (node!=0) - { - // First item does not match, but subsequent item might - if (node->string==key) - { - out=node->data; - // Skip over subsequent item - last->next=node->next; - // Delete existing item - RakNet::OP_DELETE(node,file,line); - size--; - return true; - } - last=node; - node=node->next; - } - return false; - } - - template - bool Hash::RemoveAtIndex(HashIndex index, const char *file, unsigned int line ) - { - if (index.IsInvalid()) - return false; - - Node *node = nodeList[index.primaryIndex]; - if (node==0) - return false; - if (node->next==0) - { - // Delete last item - ClearIndex(index.primaryIndex,file,line); - return true; - } - else if (index.secondaryIndex==0) - { - // First item does match, but more than one item - nodeList[index.primaryIndex]=node->next; - RakNet::OP_DELETE(node,file,line); - size--; - return true; - } - - Node *last=node; - node=node->next; - --index.secondaryIndex; - - while (index.secondaryIndex!=0) - { - last=node; - node=node->next; - --index.secondaryIndex; - } - - // Skip over subsequent item - last->next=node->next; - // Delete existing item - RakNet::OP_DELETE(node,file,line); - size--; - return true; - } - - template - bool Hash::Remove(key_type key, const char *file, unsigned int line ) - { - return RemoveAtIndex(GetIndexOf(key),file,line); - } - - template - HashIndex Hash::GetIndexOf(key_type key) - { - if (nodeList==0) - { - HashIndex temp; - temp.SetInvalid(); - return temp; - } - HashIndex idx; - idx.primaryIndex=(*hashFunction)(key) % HASH_SIZE; - Node *node = nodeList[idx.primaryIndex]; - if (node==0) - { - idx.SetInvalid(); - return idx; - } - idx.secondaryIndex=0; - while (node!=0) - { - if (node->string==key) - { - return idx; - } - node=node->next; - idx.secondaryIndex++; - } - - idx.SetInvalid(); - return idx; - } - - template - bool Hash::HasData(key_type key) - { - return GetIndexOf(key).IsInvalid()==false; - } - - template - data_type& Hash::ItemAtIndex(const HashIndex &index) - { - Node *node = nodeList[index.primaryIndex]; - RakAssert(node); - unsigned int i; - for (i=0; i < index.secondaryIndex; i++) - { - node=node->next; - RakAssert(node); - } - return node->data; - } - - template - key_type Hash::KeyAtIndex(const HashIndex &index) - { - Node *node = nodeList[index.primaryIndex]; - RakAssert(node); - unsigned int i; - for (i=0; i < index.secondaryIndex; i++) - { - node=node->next; - RakAssert(node); - } - return node->string; - } - - template - void Hash::Clear(const char *file, unsigned int line) - { - if (nodeList) - { - unsigned int i; - for (i=0; i < HASH_SIZE; i++) - ClearIndex(i,file,line); - RakNet::OP_DELETE_ARRAY(nodeList,file,line); - nodeList=0; - size=0; - } - } - - template - void Hash::ClearIndex(unsigned int index,const char *file, unsigned int line) - { - Node *node = nodeList[index]; - Node *next; - while (node) - { - next=node->next; - RakNet::OP_DELETE(node,file,line); - node=next; - size--; - } - nodeList[index]=0; - } - - template - void Hash::GetAsList(DataStructures::List &itemList,DataStructures::List &keyList,const char *file, unsigned int line) const - { - if (nodeList==0) - return; - itemList.Clear(false,_FILE_AND_LINE_); - keyList.Clear(false,_FILE_AND_LINE_); - - Node *node; - unsigned int i; - for (i=0; i < HASH_SIZE; i++) - { - if (nodeList[i]) - { - node=nodeList[i]; - while (node) - { - itemList.Push(node->data,file,line); - keyList.Push(node->string,file,line); - node=node->next; - } - } - } - } - template - unsigned int Hash::Size(void) const - { - return size; - } -} -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \internal +/// \brief Hashing container +/// + + +#pragma once + +#include "RakAssert.h" +#include // memmove +#include "Export.h" +#include "RakMemoryOverride.h" +#include "RakString.h" + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + struct HashIndex + { + unsigned int primaryIndex; + unsigned int secondaryIndex; + bool IsInvalid(void) const {return primaryIndex==(unsigned int) -1;} + void SetInvalid(void) {primaryIndex=(unsigned int) -1; secondaryIndex=(unsigned int) -1;} + }; + + /// \brief Using a string as a identifier for a node, store an allocated pointer to that node + template + class RAK_DLL_EXPORT Hash + { + public: + /// Default constructor + Hash(); + + // Destructor + ~Hash(); + + void Push(key_type key, const data_type &input, const char *file, unsigned int line ); + data_type* Peek(key_type key ); + bool Pop(data_type& out, key_type key, const char *file, unsigned int line ); + bool RemoveAtIndex(HashIndex index, const char *file, unsigned int line ); + bool Remove(key_type key, const char *file, unsigned int line ); + HashIndex GetIndexOf(key_type key); + bool HasData(key_type key); + data_type& ItemAtIndex(const HashIndex &index); + key_type KeyAtIndex(const HashIndex &index); + void GetAsList(DataStructures::List &itemList,DataStructures::List &keyList,const char *file, unsigned int line) const; + unsigned int Size(void) const; + + /// \brief Clear the list + void Clear( const char *file, unsigned int line ); + + struct Node + { + Node(key_type strIn, const data_type &_data) {string=strIn; data=_data;} + key_type string; + data_type data; + // Next in the list for this key + Node *next; + }; + + protected: + void ClearIndex(unsigned int index,const char *file, unsigned int line); + Node **nodeList; + unsigned int size; + }; + + template + Hash::Hash() + { + nodeList=0; + size=0; + } + + template + Hash::~Hash() + { + Clear(_FILE_AND_LINE_); + } + + template + void Hash::Push(key_type key, const data_type &input, const char *file, unsigned int line ) + { + unsigned long hashIndex = (*hashFunction)(key) % HASH_SIZE; + if (nodeList==0) + { + nodeList=RakNet::OP_NEW_ARRAY(HASH_SIZE,file,line); + memset(nodeList,0,sizeof(Node *)*HASH_SIZE); + } + + Node *newNode=RakNet::OP_NEW_2(file,line,key,input); + newNode->next=nodeList[hashIndex]; + nodeList[hashIndex]=newNode; + + size++; + } + + template + data_type* Hash::Peek(key_type key ) + { + if (nodeList==0) + return 0; + + unsigned long hashIndex = (*hashFunction)(key) % HASH_SIZE; + Node *node = nodeList[hashIndex]; + while (node!=0) + { + if (node->string==key) + return &node->data; + node=node->next; + } + return 0; + } + + template + bool Hash::Pop(data_type& out, key_type key, const char *file, unsigned int line ) + { + if (nodeList==0) + return false; + + unsigned long hashIndex = (*hashFunction)(key) % HASH_SIZE; + Node *node = nodeList[hashIndex]; + if (node==0) + return false; + if (node->next==0) + { + // Only one item. + if (node->string==key) + { + // Delete last item + out=node->data; + ClearIndex(hashIndex,_FILE_AND_LINE_); + return true; + } + else + { + // Single item doesn't match + return false; + } + } + else if (node->string==key) + { + // First item does match, but more than one item + out=node->data; + nodeList[hashIndex]=node->next; + RakNet::OP_DELETE(node,file,line); + size--; + return true; + } + + Node *last=node; + node=node->next; + + while (node!=0) + { + // First item does not match, but subsequent item might + if (node->string==key) + { + out=node->data; + // Skip over subsequent item + last->next=node->next; + // Delete existing item + RakNet::OP_DELETE(node,file,line); + size--; + return true; + } + last=node; + node=node->next; + } + return false; + } + + template + bool Hash::RemoveAtIndex(HashIndex index, const char *file, unsigned int line ) + { + if (index.IsInvalid()) + return false; + + Node *node = nodeList[index.primaryIndex]; + if (node==0) + return false; + if (node->next==0) + { + // Delete last item + ClearIndex(index.primaryIndex,file,line); + return true; + } + else if (index.secondaryIndex==0) + { + // First item does match, but more than one item + nodeList[index.primaryIndex]=node->next; + RakNet::OP_DELETE(node,file,line); + size--; + return true; + } + + Node *last=node; + node=node->next; + --index.secondaryIndex; + + while (index.secondaryIndex!=0) + { + last=node; + node=node->next; + --index.secondaryIndex; + } + + // Skip over subsequent item + last->next=node->next; + // Delete existing item + RakNet::OP_DELETE(node,file,line); + size--; + return true; + } + + template + bool Hash::Remove(key_type key, const char *file, unsigned int line ) + { + return RemoveAtIndex(GetIndexOf(key),file,line); + } + + template + HashIndex Hash::GetIndexOf(key_type key) + { + if (nodeList==0) + { + HashIndex temp; + temp.SetInvalid(); + return temp; + } + HashIndex idx; + idx.primaryIndex=(*hashFunction)(key) % HASH_SIZE; + Node *node = nodeList[idx.primaryIndex]; + if (node==0) + { + idx.SetInvalid(); + return idx; + } + idx.secondaryIndex=0; + while (node!=0) + { + if (node->string==key) + { + return idx; + } + node=node->next; + idx.secondaryIndex++; + } + + idx.SetInvalid(); + return idx; + } + + template + bool Hash::HasData(key_type key) + { + return GetIndexOf(key).IsInvalid()==false; + } + + template + data_type& Hash::ItemAtIndex(const HashIndex &index) + { + Node *node = nodeList[index.primaryIndex]; + RakAssert(node); + unsigned int i; + for (i=0; i < index.secondaryIndex; i++) + { + node=node->next; + RakAssert(node); + } + return node->data; + } + + template + key_type Hash::KeyAtIndex(const HashIndex &index) + { + Node *node = nodeList[index.primaryIndex]; + RakAssert(node); + unsigned int i; + for (i=0; i < index.secondaryIndex; i++) + { + node=node->next; + RakAssert(node); + } + return node->string; + } + + template + void Hash::Clear(const char *file, unsigned int line) + { + if (nodeList) + { + unsigned int i; + for (i=0; i < HASH_SIZE; i++) + ClearIndex(i,file,line); + RakNet::OP_DELETE_ARRAY(nodeList,file,line); + nodeList=0; + size=0; + } + } + + template + void Hash::ClearIndex(unsigned int index,const char *file, unsigned int line) + { + Node *node = nodeList[index]; + Node *next; + while (node) + { + next=node->next; + RakNet::OP_DELETE(node,file,line); + node=next; + size--; + } + nodeList[index]=0; + } + + template + void Hash::GetAsList(DataStructures::List &itemList,DataStructures::List &keyList,const char *file, unsigned int line) const + { + if (nodeList==0) + return; + itemList.Clear(false,_FILE_AND_LINE_); + keyList.Clear(false,_FILE_AND_LINE_); + + Node *node; + unsigned int i; + for (i=0; i < HASH_SIZE; i++) + { + if (nodeList[i]) + { + node=nodeList[i]; + while (node) + { + itemList.Push(node->data,file,line); + keyList.Push(node->string,file,line); + node=node->next; + } + } + } + } + template + unsigned int Hash::Size(void) const + { + return size; + } +} diff --git a/Source/DS_Heap.h b/Source/DS_Heap.h index c20042ea1..77d49318a 100644 --- a/Source/DS_Heap.h +++ b/Source/DS_Heap.h @@ -1,305 +1,303 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_Heap.h -/// \internal -/// \brief Heap (Also serves as a priority queue) -/// - - - -#ifndef __RAKNET_HEAP_H -#define __RAKNET_HEAP_H - -#include "RakMemoryOverride.h" -#include "DS_List.h" -#include "Export.h" -#include "RakAssert.h" - -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - template - class RAK_DLL_EXPORT Heap - { - public: - struct HeapNode - { - HeapNode() {} - HeapNode(const weight_type &w, const data_type &d) : weight(w), data(d) {} - weight_type weight; // I'm assuming key is a native numerical type - float or int - data_type data; - }; - - Heap(); - ~Heap(); - void Push(const weight_type &weight, const data_type &data, const char *file, unsigned int line); - /// Call before calling PushSeries, for a new series of items - void StartSeries(void) {optimizeNextSeriesPush=false;} - /// If you are going to push a list of items, where the weights of the items on the list are in order and follow the heap order, PushSeries is faster than Push() - void PushSeries(const weight_type &weight, const data_type &data, const char *file, unsigned int line); - data_type Pop(const unsigned startingIndex); - data_type Peek(const unsigned startingIndex=0) const; - weight_type PeekWeight(const unsigned startingIndex=0) const; - void Clear(bool doNotDeallocateSmallBlocks, const char *file, unsigned int line); - data_type& operator[] ( const unsigned int position ) const; - unsigned Size(void) const; - - protected: - unsigned LeftChild(const unsigned i) const; - unsigned RightChild(const unsigned i) const; - unsigned Parent(const unsigned i) const; - void Swap(const unsigned i, const unsigned j); - DataStructures::List heap; - bool optimizeNextSeriesPush; - }; - - template - Heap::Heap() - { - optimizeNextSeriesPush=false; - } - - template - Heap::~Heap() - { - //Clear(true, _FILE_AND_LINE_); - } - - template - void Heap::PushSeries(const weight_type &weight, const data_type &data, const char *file, unsigned int line) - { - if (optimizeNextSeriesPush==false) - { - // If the weight of what we are inserting is greater than / less than in order of the heap of every sibling and sibling of parent, then can optimize next push - unsigned currentIndex = heap.Size(); - unsigned parentIndex; - if (currentIndex>0) - { - for (parentIndex = Parent(currentIndex); parentIndex < currentIndex; parentIndex++) - { -#ifdef _MSC_VER -#pragma warning(disable:4127) // conditional expression is constant -#endif - if (isMaxHeap) - { - // Every child is less than its parent - if (weight>heap[parentIndex].weight) - { - // Can't optimize - Push(weight,data,file,line); - return; - } - } - else - { - // Every child is greater than than its parent - if (weight - void Heap::Push(const weight_type &weight, const data_type &data, const char *file, unsigned int line) - { - unsigned currentIndex = heap.Size(); - unsigned parentIndex; - heap.Insert(HeapNode(weight, data), file, line); - while (currentIndex!=0) - { - parentIndex = Parent(currentIndex); -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - if (isMaxHeap) - { - if (heap[parentIndex].weight < weight) - { - Swap(currentIndex, parentIndex); - currentIndex=parentIndex; - } - else - break; - } - else - { - if (heap[parentIndex].weight > weight) - { - Swap(currentIndex, parentIndex); - currentIndex=parentIndex; - } - else - break; - } - } - } - - template - data_type Heap::Pop(const unsigned startingIndex) - { - // While we have children, swap out with the larger of the two children. - - // This line will assert on an empty heap - data_type returnValue=heap[startingIndex].data; - - // Move the last element to the head, and re-heapify - heap[startingIndex]=heap[heap.Size()-1]; - - unsigned currentIndex,leftChild,rightChild; - weight_type currentWeight; - currentIndex=startingIndex; - currentWeight=heap[startingIndex].weight; - heap.RemoveFromEnd(); - -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - while (1) - { - leftChild=LeftChild(currentIndex); - rightChild=RightChild(currentIndex); - if (leftChild >= heap.Size()) - { - // Done - return returnValue; - } - if (rightChild >= heap.Size()) - { - // Only left node. - if ((isMaxHeap==true && currentWeight < heap[leftChild].weight) || - (isMaxHeap==false && currentWeight > heap[leftChild].weight)) - Swap(leftChild, currentIndex); - - return returnValue; - } - else - { - // Swap with the bigger/smaller of the two children and continue - if (isMaxHeap) - { - if (heap[leftChild].weight <= currentWeight && heap[rightChild].weight <= currentWeight) - return returnValue; - - if (heap[leftChild].weight > heap[rightChild].weight) - { - Swap(leftChild, currentIndex); - currentIndex=leftChild; - } - else - { - Swap(rightChild, currentIndex); - currentIndex=rightChild; - } - } - else - { - if (heap[leftChild].weight >= currentWeight && heap[rightChild].weight >= currentWeight) - return returnValue; - - if (heap[leftChild].weight < heap[rightChild].weight) - { - Swap(leftChild, currentIndex); - currentIndex=leftChild; - } - else - { - Swap(rightChild, currentIndex); - currentIndex=rightChild; - } - } - } - } - } - - template - inline data_type Heap::Peek(const unsigned startingIndex) const - { - return heap[startingIndex].data; - } - - template - inline weight_type Heap::PeekWeight(const unsigned startingIndex) const - { - return heap[startingIndex].weight; - } - - template - void Heap::Clear(bool doNotDeallocateSmallBlocks, const char *file, unsigned int line) - { - heap.Clear(doNotDeallocateSmallBlocks, file, line); - } - - template - inline data_type& Heap::operator[] ( const unsigned int position ) const - { - return heap[position].data; - } - template - unsigned Heap::Size(void) const - { - return heap.Size(); - } - - template - inline unsigned Heap::LeftChild(const unsigned i) const - { - return i*2+1; - } - - template - inline unsigned Heap::RightChild(const unsigned i) const - { - return i*2+2; - } - - template - inline unsigned Heap::Parent(const unsigned i) const - { -#ifdef _DEBUG - RakAssert(i!=0); -#endif - return (i-1)/2; - } - - template - void Heap::Swap(const unsigned i, const unsigned j) - { - HeapNode temp; - temp=heap[i]; - heap[i]=heap[j]; - heap[j]=temp; - } -} - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_Heap.h +/// \internal +/// \brief Heap (Also serves as a priority queue) +/// + + + +#pragma once + +#include "RakMemoryOverride.h" +#include "DS_List.h" +#include "Export.h" +#include "RakAssert.h" + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + template + class RAK_DLL_EXPORT Heap + { + public: + struct HeapNode + { + HeapNode() {} + HeapNode(const weight_type &w, const data_type &d) : weight(w), data(d) {} + weight_type weight; // I'm assuming key is a native numerical type - float or int + data_type data; + }; + + Heap(); + ~Heap(); + void Push(const weight_type &weight, const data_type &data, const char *file, unsigned int line); + /// Call before calling PushSeries, for a new series of items + void StartSeries(void) {optimizeNextSeriesPush=false;} + /// If you are going to push a list of items, where the weights of the items on the list are in order and follow the heap order, PushSeries is faster than Push() + void PushSeries(const weight_type &weight, const data_type &data, const char *file, unsigned int line); + data_type Pop(const unsigned startingIndex); + data_type Peek(const unsigned startingIndex=0) const; + weight_type PeekWeight(const unsigned startingIndex=0) const; + void Clear(bool doNotDeallocateSmallBlocks, const char *file, unsigned int line); + data_type& operator[] ( const unsigned int position ) const; + unsigned Size(void) const; + + protected: + unsigned LeftChild(const unsigned i) const; + unsigned RightChild(const unsigned i) const; + unsigned Parent(const unsigned i) const; + void Swap(const unsigned i, const unsigned j); + DataStructures::List heap; + bool optimizeNextSeriesPush; + }; + + template + Heap::Heap() + { + optimizeNextSeriesPush=false; + } + + template + Heap::~Heap() + { + //Clear(true, _FILE_AND_LINE_); + } + + template + void Heap::PushSeries(const weight_type &weight, const data_type &data, const char *file, unsigned int line) + { + if (optimizeNextSeriesPush==false) + { + // If the weight of what we are inserting is greater than / less than in order of the heap of every sibling and sibling of parent, then can optimize next push + unsigned currentIndex = heap.Size(); + unsigned parentIndex; + if (currentIndex>0) + { + for (parentIndex = Parent(currentIndex); parentIndex < currentIndex; parentIndex++) + { +#ifdef _MSC_VER +#pragma warning(disable:4127) // conditional expression is constant +#endif + if (isMaxHeap) + { + // Every child is less than its parent + if (weight>heap[parentIndex].weight) + { + // Can't optimize + Push(weight,data,file,line); + return; + } + } + else + { + // Every child is greater than than its parent + if (weight + void Heap::Push(const weight_type &weight, const data_type &data, const char *file, unsigned int line) + { + unsigned currentIndex = heap.Size(); + unsigned parentIndex; + heap.Insert(HeapNode(weight, data), file, line); + while (currentIndex!=0) + { + parentIndex = Parent(currentIndex); +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + if (isMaxHeap) + { + if (heap[parentIndex].weight < weight) + { + Swap(currentIndex, parentIndex); + currentIndex=parentIndex; + } + else + break; + } + else + { + if (heap[parentIndex].weight > weight) + { + Swap(currentIndex, parentIndex); + currentIndex=parentIndex; + } + else + break; + } + } + } + + template + data_type Heap::Pop(const unsigned startingIndex) + { + // While we have children, swap out with the larger of the two children. + + // This line will assert on an empty heap + data_type returnValue=heap[startingIndex].data; + + // Move the last element to the head, and re-heapify + heap[startingIndex]=heap[heap.Size()-1]; + + unsigned currentIndex,leftChild,rightChild; + weight_type currentWeight; + currentIndex=startingIndex; + currentWeight=heap[startingIndex].weight; + heap.RemoveFromEnd(); + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + while (1) + { + leftChild=LeftChild(currentIndex); + rightChild=RightChild(currentIndex); + if (leftChild >= heap.Size()) + { + // Done + return returnValue; + } + if (rightChild >= heap.Size()) + { + // Only left node. + if ((isMaxHeap==true && currentWeight < heap[leftChild].weight) || + (isMaxHeap==false && currentWeight > heap[leftChild].weight)) + Swap(leftChild, currentIndex); + + return returnValue; + } + else + { + // Swap with the bigger/smaller of the two children and continue + if (isMaxHeap) + { + if (heap[leftChild].weight <= currentWeight && heap[rightChild].weight <= currentWeight) + return returnValue; + + if (heap[leftChild].weight > heap[rightChild].weight) + { + Swap(leftChild, currentIndex); + currentIndex=leftChild; + } + else + { + Swap(rightChild, currentIndex); + currentIndex=rightChild; + } + } + else + { + if (heap[leftChild].weight >= currentWeight && heap[rightChild].weight >= currentWeight) + return returnValue; + + if (heap[leftChild].weight < heap[rightChild].weight) + { + Swap(leftChild, currentIndex); + currentIndex=leftChild; + } + else + { + Swap(rightChild, currentIndex); + currentIndex=rightChild; + } + } + } + } + } + + template + inline data_type Heap::Peek(const unsigned startingIndex) const + { + return heap[startingIndex].data; + } + + template + inline weight_type Heap::PeekWeight(const unsigned startingIndex) const + { + return heap[startingIndex].weight; + } + + template + void Heap::Clear(bool doNotDeallocateSmallBlocks, const char *file, unsigned int line) + { + heap.Clear(doNotDeallocateSmallBlocks, file, line); + } + + template + inline data_type& Heap::operator[] ( const unsigned int position ) const + { + return heap[position].data; + } + template + unsigned Heap::Size(void) const + { + return heap.Size(); + } + + template + inline unsigned Heap::LeftChild(const unsigned i) const + { + return i*2+1; + } + + template + inline unsigned Heap::RightChild(const unsigned i) const + { + return i*2+2; + } + + template + inline unsigned Heap::Parent(const unsigned i) const + { +#ifdef _DEBUG + RakAssert(i!=0); +#endif + return (i-1)/2; + } + + template + void Heap::Swap(const unsigned i, const unsigned j) + { + HeapNode temp; + temp=heap[i]; + heap[i]=heap[j]; + heap[j]=temp; + } +} + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif + diff --git a/Source/DS_HuffmanEncodingTree.h b/Source/DS_HuffmanEncodingTree.h index 174cae787..62f178fc3 100644 --- a/Source/DS_HuffmanEncodingTree.h +++ b/Source/DS_HuffmanEncodingTree.h @@ -1,74 +1,72 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_HuffmanEncodingTree.h -/// \brief \b [Internal] Generates a huffman encoding tree, used for string and global compression. -/// - - -#ifndef __HUFFMAN_ENCODING_TREE -#define __HUFFMAN_ENCODING_TREE - -#include "RakMemoryOverride.h" -#include "DS_HuffmanEncodingTreeNode.h" -#include "BitStream.h" -#include "Export.h" -#include "DS_LinkedList.h" - -namespace RakNet -{ - -/// This generates special cases of the huffman encoding tree using 8 bit keys with the additional condition that unused combinations of 8 bits are treated as a frequency of 1 -class RAK_DLL_EXPORT HuffmanEncodingTree -{ - -public: - HuffmanEncodingTree(); - ~HuffmanEncodingTree(); - - /// \brief Pass an array of bytes to array and a preallocated BitStream to receive the output. - /// \param [in] input Array of bytes to encode - /// \param [in] sizeInBytes size of \a input - /// \param [out] output The bitstream to write to - void EncodeArray( unsigned char *input, size_t sizeInBytes, RakNet::BitStream * output ); - - // \brief Decodes an array encoded by EncodeArray(). - unsigned DecodeArray( RakNet::BitStream * input, BitSize_t sizeInBits, size_t maxCharsToWrite, unsigned char *output ); - void DecodeArray( unsigned char *input, BitSize_t sizeInBits, RakNet::BitStream * output ); - - /// \brief Given a frequency table of 256 elements, all with a frequency of 1 or more, generate the tree. - void GenerateFromFrequencyTable( unsigned int frequencyTable[ 256 ] ); - - /// \brief Free the memory used by the tree. - void FreeMemory( void ); - -private: - - /// The root node of the tree - - HuffmanEncodingTreeNode *root; - - /// Used to hold bit encoding for one character - - - struct CharacterEncoding - { - unsigned char* encoding; - unsigned short bitLength; - }; - - CharacterEncoding encodingTable[ 256 ]; - - void InsertNodeIntoSortedList( HuffmanEncodingTreeNode * node, DataStructures::LinkedList *huffmanEncodingTreeNodeList ) const; -}; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_HuffmanEncodingTree.h +/// \brief \b [Internal] Generates a huffman encoding tree, used for string and global compression. +/// + + +#pragma once + +#include "RakMemoryOverride.h" +#include "DS_HuffmanEncodingTreeNode.h" +#include "BitStream.h" +#include "Export.h" +#include "DS_LinkedList.h" + +namespace RakNet +{ + +/// This generates special cases of the huffman encoding tree using 8 bit keys with the additional condition that unused combinations of 8 bits are treated as a frequency of 1 +class RAK_DLL_EXPORT HuffmanEncodingTree +{ + +public: + HuffmanEncodingTree(); + ~HuffmanEncodingTree(); + + /// \brief Pass an array of bytes to array and a preallocated BitStream to receive the output. + /// \param [in] input Array of bytes to encode + /// \param [in] sizeInBytes size of \a input + /// \param [out] output The bitstream to write to + void EncodeArray( unsigned char *input, size_t sizeInBytes, RakNet::BitStream * output ); + + // \brief Decodes an array encoded by EncodeArray(). + unsigned DecodeArray( RakNet::BitStream * input, BitSize_t sizeInBits, size_t maxCharsToWrite, unsigned char *output ); + void DecodeArray( unsigned char *input, BitSize_t sizeInBits, RakNet::BitStream * output ); + + /// \brief Given a frequency table of 256 elements, all with a frequency of 1 or more, generate the tree. + void GenerateFromFrequencyTable( unsigned int frequencyTable[ 256 ] ); + + /// \brief Free the memory used by the tree. + void FreeMemory( void ); + +private: + + /// The root node of the tree + + HuffmanEncodingTreeNode *root; + + /// Used to hold bit encoding for one character + + + struct CharacterEncoding + { + unsigned char* encoding; + unsigned short bitLength; + }; + + CharacterEncoding encodingTable[ 256 ]; + + void InsertNodeIntoSortedList( HuffmanEncodingTreeNode * node, DataStructures::LinkedList *huffmanEncodingTreeNodeList ) const; +}; + +} // namespace RakNet + diff --git a/Source/DS_HuffmanEncodingTreeFactory.h b/Source/DS_HuffmanEncodingTreeFactory.h index b7257eb9c..1813efc17 100644 --- a/Source/DS_HuffmanEncodingTreeFactory.h +++ b/Source/DS_HuffmanEncodingTreeFactory.h @@ -1,64 +1,62 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_HuffmanEncodingTreeFactory.h -/// \internal -/// \brief Creates instances of the class HuffmanEncodingTree -/// - - -#ifndef __HUFFMAN_ENCODING_TREE_FACTORY -#define __HUFFMAN_ENCODING_TREE_FACTORY - -#include "RakMemoryOverride.h" - -namespace RakNet { -/// Forward declarations -class HuffmanEncodingTree; - -/// \brief Creates instances of the class HuffmanEncodingTree -/// \details This class takes a frequency table and given that frequence table, will generate an instance of HuffmanEncodingTree -class HuffmanEncodingTreeFactory -{ -public: - /// Default constructor - HuffmanEncodingTreeFactory(); - - /// \brief Reset the frequency table. - /// \details You don't need to call this unless you want to reuse the class for a new tree - void Reset( void ); - - /// \brief Pass an array of bytes to this to add those elements to the frequency table. - /// \param[in] array the data to insert into the frequency table - /// \param[in] size the size of the data to insert - void AddToFrequencyTable( unsigned char *array, int size ); - - /// \brief Copies the frequency table to the array passed. Retrieve the frequency table. - /// \param[in] _frequency The frequency table used currently - void GetFrequencyTable( unsigned int _frequency[ 256 ] ); - - /// \brief Returns the frequency table as a pointer. - /// \return the address of the frenquency table - unsigned int * GetFrequencyTable( void ); - - /// \brief Generate a HuffmanEncodingTree. - /// \details You can also use GetFrequencyTable and GenerateFromFrequencyTable in the tree itself - /// \return The generated instance of HuffmanEncodingTree - HuffmanEncodingTree * GenerateTree( void ); - -private: - - /// Frequency table - unsigned int frequency[ 256 ]; -}; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_HuffmanEncodingTreeFactory.h +/// \internal +/// \brief Creates instances of the class HuffmanEncodingTree +/// + + +#pragma once + +#include "RakMemoryOverride.h" + +namespace RakNet { +/// Forward declarations +class HuffmanEncodingTree; + +/// \brief Creates instances of the class HuffmanEncodingTree +/// \details This class takes a frequency table and given that frequence table, will generate an instance of HuffmanEncodingTree +class HuffmanEncodingTreeFactory +{ +public: + /// Default constructor + HuffmanEncodingTreeFactory(); + + /// \brief Reset the frequency table. + /// \details You don't need to call this unless you want to reuse the class for a new tree + void Reset( void ); + + /// \brief Pass an array of bytes to this to add those elements to the frequency table. + /// \param[in] array the data to insert into the frequency table + /// \param[in] size the size of the data to insert + void AddToFrequencyTable( unsigned char *array, int size ); + + /// \brief Copies the frequency table to the array passed. Retrieve the frequency table. + /// \param[in] _frequency The frequency table used currently + void GetFrequencyTable( unsigned int _frequency[ 256 ] ); + + /// \brief Returns the frequency table as a pointer. + /// \return the address of the frenquency table + unsigned int * GetFrequencyTable( void ); + + /// \brief Generate a HuffmanEncodingTree. + /// \details You can also use GetFrequencyTable and GenerateFromFrequencyTable in the tree itself + /// \return The generated instance of HuffmanEncodingTree + HuffmanEncodingTree * GenerateTree( void ); + +private: + + /// Frequency table + unsigned int frequency[ 256 ]; +}; + +} // namespace RakNet + diff --git a/Source/DS_HuffmanEncodingTreeNode.h b/Source/DS_HuffmanEncodingTreeNode.h index 0015ad1d9..a30299e13 100644 --- a/Source/DS_HuffmanEncodingTreeNode.h +++ b/Source/DS_HuffmanEncodingTreeNode.h @@ -1,27 +1,25 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief \b [Internal] A single node in the Huffman Encoding Tree. -/// - -#ifndef __HUFFMAN_ENCODING_TREE_NODE -#define __HUFFMAN_ENCODING_TREE_NODE - -struct HuffmanEncodingTreeNode -{ - unsigned char value; - unsigned weight; - HuffmanEncodingTreeNode *left; - HuffmanEncodingTreeNode *right; - HuffmanEncodingTreeNode *parent; -}; - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief \b [Internal] A single node in the Huffman Encoding Tree. +/// + +#pragma once + +struct HuffmanEncodingTreeNode +{ + unsigned char value; + unsigned weight; + HuffmanEncodingTreeNode *left; + HuffmanEncodingTreeNode *right; + HuffmanEncodingTreeNode *parent; +}; + diff --git a/Source/DS_LinkedList.h b/Source/DS_LinkedList.h index 05f42f197..07fdd318e 100644 --- a/Source/DS_LinkedList.h +++ b/Source/DS_LinkedList.h @@ -1,1258 +1,1256 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_LinkedList.h -/// \internal -/// \brief Straightforward linked list data structure. -/// - - -#ifndef __LINKED_LIST_H -#define __LINKED_LIST_H - -#include "Export.h" -#include "RakMemoryOverride.h" - -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - // Prototype to prevent error in CircularLinkedList class when a reference is made to a LinkedList class - template - class RAK_DLL_EXPORT LinkedList; - - /** - * \brief (Circular) Linked List ADT (Doubly Linked Pointer to Node Style) - - * - * \details - * Initilize with the following command - * LinkedList - * OR - * CircularLinkedList - * - * Has the following member functions - * - size: returns number of elements in the linked list - * - insert(item): inserts @em item at the current position in - * the LinkedList. - * - add(item): inserts @em item after the current position in - * the LinkedList. Does not increment the position - * - replace(item): replaces the element at the current position @em item. - * - peek: returns the element at the current position - * - pop: returns the element at the current position and deletes it - * - del: deletes the current element. Does nothing for an empty list. - * - clear: empties the LinkedList and returns storage - * - bool IsInitem): Does a linear search for @em item. Does not set - * the position to it, only returns true on item found, false otherwise - * - bool find(item): Does a linear search for @em item and sets the current - * position to point to it if and only if the item is found. Returns true - * on item found, false otherwise - * - sort: Sorts the elements of the list with a mergesort and sets the - * current pointer to the first element - * - concatenate(list L): This appends L to the current list - * - ++(prefix): moves the pointer one element up in the list and returns the - * appropriate copy of the element in the list - * - --(prefix): moves the pointer one element back in the list and returns - * the appropriate copy of the element in the list - * - beginning - moves the pointer to the start of the list. For circular - * linked lists this is first 'position' created. You should call this - * after the sort function to read the first value. - * - end - moves the pointer to the end of the list. For circular linked - * lists this is one less than the first 'position' created - * The assignment and copy constructor operators are defined - * - * \note - * 1. LinkedList and CircularLinkedList are exactly the same except LinkedList - * won't let you wrap around the root and lets you jump to two positions - * relative to the root/ - * 2. Postfix ++ and -- can be used but simply call the prefix versions. - * - * - * EXAMPLE: - * @code - * LinkedList A; // Creates a Linked List of integers called A - * CircularLinkedList B; // Creates a Circular Linked List of - * // integers called B - * - * A.Insert(20); // Adds 20 to A. A: 20 - current is 20 - * A.Insert(5); // Adds 5 to A. A: 5 20 - current is 5 - * A.Insert(1); // Adds 1 to A. A: 1 5 20 - current is 1 - * - * A.IsIn1); // returns true - * A.IsIn200); // returns false - * A.Find(5); // returns true and sets current to 5 - * A.Peek(); // returns 5 - * A.Find(1); // returns true and sets current to 1 - * - * (++A).Peek(); // Returns 5 - * A.Peek(); // Returns 5 - * - * A.Replace(10); // Replaces 5 with 10. - * A.Peek(); // Returns 10 - * - * A.Beginning(); // Current points to the beginning of the list at 1 - * - * (++A).Peek(); // Returns 5 - * A.Peek(); // Returns 10 - * - * A.Del(); // Deletes 10. Current points to the next element, which is 20 - * A.Peek(); // Returns 20 - * - * A.Beginning(); // Current points to the beginning of the list at 1 - * - * (++A).Peek(); // Returns 5 - * A.Peek(); // Returns 20 - * - * A.Clear(_FILE_AND_LINE_); // Deletes all nodes in A - * - * A.Insert(5); // A: 5 - current is 5 - * A.Insert(6); // A: 6 5 - current is 6 - * A.Insert(7); // A: 7 6 5 - current is 7 - * - * A.Clear(_FILE_AND_LINE_); - * B.Clear(_FILE_AND_LINE_); - * - * B.Add(10); - * B.Add(20); - * B.Add(30); - * B.Add(5); - * B.Add(2); - * B.Add(25); - * // Sorts the numbers in the list and sets the current pointer to the - * // first element - * B.sort(); - * - * // Postfix ++ just calls the prefix version and has no functional - * // difference. - * B.Peek(); // Returns 2 - * B++; - * B.Peek(); // Returns 5 - * B++; - * B.Peek(); // Returns 10 - * B++; - * B.Peek(); // Returns 20 - * B++; - * B.Peek(); // Returns 25 - * B++; - * B.Peek(); // Returns 30 - * @endcode - */ - template - - class CircularLinkedList - { - - public: - - struct node - { - CircularLinkedListType item; - - node* previous; - node* next; - }; - - CircularLinkedList(); - ~CircularLinkedList(); - CircularLinkedList( const CircularLinkedList& original_copy ); - // CircularLinkedList(LinkedList original_copy) {CircularLinkedList(original_copy);} // Converts linked list to circular type - bool operator= ( const CircularLinkedList& original_copy ); - CircularLinkedList& operator++(); // CircularLinkedList A; ++A; - CircularLinkedList& operator++( int ); // Circular_Linked List A; A++; - CircularLinkedList& operator--(); // CircularLinkedList A; --A; - CircularLinkedList& operator--( int ); // Circular_Linked List A; A--; - bool IsIn( const CircularLinkedListType& input ); - bool Find( const CircularLinkedListType& input ); - void Insert( const CircularLinkedListType& input ); - - CircularLinkedListType& Add ( const CircularLinkedListType& input ) - - ; // Adds after the current position - void Replace( const CircularLinkedListType& input ); - - void Del( void ); - - unsigned int Size( void ); - - CircularLinkedListType& Peek( void ); - - CircularLinkedListType Pop( void ); - - void Clear( void ); - - void Sort( void ); - - void Beginning( void ); - - void End( void ); - - void Concatenate( const CircularLinkedList& L ); - - protected: - unsigned int list_size; - - node *root; - - node *position; - - node* FindPointer( const CircularLinkedListType& input ); - - private: - CircularLinkedList Merge( CircularLinkedList L1, CircularLinkedList L2 ); - - CircularLinkedList Mergesort( const CircularLinkedList& L ); - }; - - template - - class LinkedList : public CircularLinkedList - { - - public: - LinkedList() - {} - - LinkedList( const LinkedList& original_copy ); - ~LinkedList(); - bool operator= ( const LinkedList& original_copy ); - LinkedList& operator++(); // LinkedList A; ++A; - LinkedList& operator++( int ); // Linked List A; A++; - LinkedList& operator--(); // LinkedList A; --A; - LinkedList& operator--( int ); // Linked List A; A--; - - private: - LinkedList Merge( LinkedList L1, LinkedList L2 ); - LinkedList Mergesort( const LinkedList& L ); - - }; - - - template - inline void CircularLinkedList::Beginning( void ) - { - if ( this->root ) - this->position = this->root; - } - - template - inline void CircularLinkedList::End( void ) - { - if ( this->root ) - this->position = this->root->previous; - } - - template - bool LinkedList::operator= ( const LinkedList& original_copy ) - { - typename LinkedList::node * original_copy_pointer, *last, *save_position; - - if ( ( &original_copy ) != this ) - { - - this->Clear(); - - - if ( original_copy.list_size == 0 ) - { - this->root = 0; - this->position = 0; - this->list_size = 0; - } - - else - if ( original_copy.list_size == 1 ) - { - this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - this->root->next = this->root; - this->root->previous = this->root; - this->list_size = 1; - this->position = this->root; - // *(root->item)=*((original_copy.root)->item); - this->root->item = original_copy.root->item; - } - - else - { - // Setup the first part of the root node - original_copy_pointer = original_copy.root; - this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - this->position = this->root; - // *(root->item)=*((original_copy.root)->item); - this->root->item = original_copy.root->item; - - if ( original_copy_pointer == original_copy.position ) - save_position = this->position; - - do - { - - - // Save the current element - last = this->position; - - // Point to the next node in the source list - original_copy_pointer = original_copy_pointer->next; - - // Create a new node and point position to it - this->position = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // position->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - - // Copy the item to the new node - // *(position->item)=*(original_copy_pointer->item); - this->position->item = original_copy_pointer->item; - - if ( original_copy_pointer == original_copy.position ) - save_position = this->position; - - - // Set the previous pointer for the new node - ( this->position->previous ) = last; - - // Set the next pointer for the old node to the new node - ( last->next ) = this->position; - - } - - while ( ( original_copy_pointer->next ) != ( original_copy.root ) ); - - // Complete the circle. Set the next pointer of the newest node to the root and the previous pointer of the root to the newest node - this->position->next = this->root; - - this->root->previous = this->position; - - this->list_size = original_copy.list_size; - - this->position = save_position; - } - } - - return true; - } - - - template - CircularLinkedList::CircularLinkedList() - { - this->root = 0; - this->position = 0; - this->list_size = 0; - } - - template - CircularLinkedList::~CircularLinkedList() - { - this->Clear(); - } - - template - LinkedList::~LinkedList() - { - this->Clear(); - } - - template - LinkedList::LinkedList( const LinkedList& original_copy ) - { - typename LinkedList::node * original_copy_pointer, *last, *save_position; - - if ( original_copy.list_size == 0 ) - { - this->root = 0; - this->position = 0; - this->list_size = 0; - return ; - } - - else - if ( original_copy.list_size == 1 ) - { - this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - this->root->next = this->root; - this->root->previous = this->root; - this->list_size = 1; - this->position = this->root; - // *(root->item) = *((original_copy.root)->item); - this->root->item = original_copy.root->item; - } - - else - { - // Setup the first part of the root node - original_copy_pointer = original_copy.root; - this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - this->position = this->root; - // *(root->item)=*((original_copy.root)->item); - this->root->item = original_copy.root->item; - - if ( original_copy_pointer == original_copy.position ) - save_position = this->position; - - do - { - // Save the current element - last = this->position; - - // Point to the next node in the source list - original_copy_pointer = original_copy_pointer->next; - - // Create a new node and point position to it - this->position = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // position->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - - // Copy the item to the new node - // *(position->item)=*(original_copy_pointer->item); - this->position->item = original_copy_pointer->item; - - if ( original_copy_pointer == original_copy.position ) - save_position = this->position; - - // Set the previous pointer for the new node - ( this->position->previous ) = last; - - // Set the next pointer for the old node to the new node - ( last->next ) = this->position; - - } - - while ( ( original_copy_pointer->next ) != ( original_copy.root ) ); - - // Complete the circle. Set the next pointer of the newest node to the root and the previous pointer of the root to the newest node - this->position->next = this->root; - - this->root->previous = this->position; - - this->list_size = original_copy.list_size; - - this->position = save_position; - } - } - -#ifdef _MSC_VER -#pragma warning( disable : 4701 ) // warning C4701: local variable may be used without having been initialized -#endif - template - CircularLinkedList::CircularLinkedList( const CircularLinkedList& original_copy ) - { - node * original_copy_pointer; - node *last; - node *save_position; - - if ( original_copy.list_size == 0 ) - { - this->root = 0; - this->position = 0; - this->list_size = 0; - return ; - } - - else - if ( original_copy.list_size == 1 ) - { - this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - this->root->next = this->root; - this->root->previous = this->root; - this->list_size = 1; - this->position = this->root; - // *(root->item) = *((original_copy.root)->item); - this->root->item = original_copy.root->item; - } - - else - { - // Setup the first part of the root node - original_copy_pointer = original_copy.root; - this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - this->position = this->root; - // *(root->item)=*((original_copy.root)->item); - this->root->item = original_copy.root->item; - - if ( original_copy_pointer == original_copy.position ) - save_position = this->position; - - do - { - - - // Save the current element - last = this->position; - - // Point to the next node in the source list - original_copy_pointer = original_copy_pointer->next; - - // Create a new node and point position to it - this->position = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // position->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - - // Copy the item to the new node - // *(position->item)=*(original_copy_pointer->item); - this->position->item = original_copy_pointer->item; - - if ( original_copy_pointer == original_copy.position ) - save_position = position; - - // Set the previous pointer for the new node - ( this->position->previous ) = last; - - // Set the next pointer for the old node to the new node - ( last->next ) = this->position; - - } - - while ( ( original_copy_pointer->next ) != ( original_copy.root ) ); - - // Complete the circle. Set the next pointer of the newest node to the root and the previous pointer of the root to the newest node - this->position->next = this->root; - - this->root->previous = position; - - this->list_size = original_copy.list_size; - - this->position = save_position; - } - } - -#ifdef _MSC_VER -#pragma warning( disable : 4701 ) // warning C4701: local variable may be used without having been initialized -#endif - template - bool CircularLinkedList::operator= ( const CircularLinkedList& original_copy ) - { - node * original_copy_pointer; - node *last; - node *save_position; - - if ( ( &original_copy ) != this ) - { - - this->Clear(); - - - if ( original_copy.list_size == 0 ) - { - this->root = 0; - this->position = 0; - this->list_size = 0; - } - - else - if ( original_copy.list_size == 1 ) - { - this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - this->root->next = this->root; - this->root->previous = this->root; - this->list_size = 1; - this->position = this->root; - // *(root->item)=*((original_copy.root)->item); - this->root->item = original_copy.root->item; - } - - else - { - // Setup the first part of the root node - original_copy_pointer = original_copy.root; - this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - this->position = this->root; - // *(root->item)=*((original_copy.root)->item); - this->root->item = original_copy.root->item; - - if ( original_copy_pointer == original_copy.position ) - save_position = this->position; - - do - { - // Save the current element - last = this->position; - - // Point to the next node in the source list - original_copy_pointer = original_copy_pointer->next; - - // Create a new node and point position to it - this->position = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // position->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - - // Copy the item to the new node - // *(position->item)=*(original_copy_pointer->item); - this->position->item = original_copy_pointer->item; - - if ( original_copy_pointer == original_copy.position ) - save_position = this->position; - - // Set the previous pointer for the new node - ( this->position->previous ) = last; - - // Set the next pointer for the old node to the new node - ( last->next ) = this->position; - - } - - while ( ( original_copy_pointer->next ) != ( original_copy.root ) ); - - // Complete the circle. Set the next pointer of the newest node to the root and the previous pointer of the root to the newest node - this->position->next = this->root; - - this->root->previous = this->position; - - this->list_size = original_copy.list_size; - - this->position = save_position; - } - } - - return true; - } - - template - void CircularLinkedList::Insert( const CircularLinkedListType& input ) - { - node * new_node; - - if ( list_size == 0 ) - { - this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - //*(root->item)=input; - this->root->item = input; - this->root->next = this->root; - this->root->previous = this->root; - this->list_size = 1; - this->position = this->root; - } - - else - if ( list_size == 1 ) - { - this->position = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // position->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - this->root->next = this->position; - this->root->previous = this->position; - this->position->previous = this->root; - this->position->next = this->root; - // *(position->item)=input; - this->position->item = input; - this->root = this->position; // Since we're inserting into a 1 element list the old root is now the second item - this->list_size = 2; - } - - else - { - /* - - B - | - A --- C - - position->previous=A - new_node=B - position=C - - Note that the order of the following statements is important */ - - new_node = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // new_node->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - - // *(new_node->item)=input; - new_node->item = input; - - // Point next of A to B - ( this->position->previous ) ->next = new_node; - - // Point last of B to A - new_node->previous = this->position->previous; - - // Point last of C to B - this->position->previous = new_node; - - // Point next of B to C - new_node->next = this->position; - - // Since the root pointer is bound to a node rather than an index this moves it back if you insert an element at the root - - if ( this->position == this->root ) - { - this->root = new_node; - this->position = this->root; - } - - // Increase the recorded size of the list by one - this->list_size++; - } - } - - template - CircularLinkedListType& CircularLinkedList::Add ( const CircularLinkedListType& input ) - { - node * new_node; - - if ( this->list_size == 0 ) - { - this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // *(root->item)=input; - this->root->item = input; - this->root->next = this->root; - this->root->previous = this->root; - this->list_size = 1; - this->position = this->root; - // return *(position->item); - return this->position->item; - } - - else - if ( list_size == 1 ) - { - this->position = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // position->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - this->root->next = this->position; - this->root->previous = this->position; - this->position->previous = this->root; - this->position->next = this->root; - // *(position->item)=input; - this->position->item = input; - this->list_size = 2; - this->position = this->root; // Don't move the position from the root - // return *(position->item); - return this->position->item; - } - - else - { - /* - - B - | - A --- C - - new_node=B - position=A - position->next=C - - Note that the order of the following statements is important */ - - new_node = RakNet::OP_NEW( _FILE_AND_LINE_ ); - // new_node->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); - - // *(new_node->item)=input; - new_node->item = input; - - // Point last of B to A - new_node->previous = this->position; - - // Point next of B to C - new_node->next = ( this->position->next ); - - // Point last of C to B - ( this->position->next ) ->previous = new_node; - - // Point next of A to B - ( this->position->next ) = new_node; - - // Increase the recorded size of the list by one - this->list_size++; - - // return *(new_node->item); - return new_node->item; - } - } - - template - inline void CircularLinkedList::Replace( const CircularLinkedListType& input ) - { - if ( this->list_size > 0 ) - // *(position->item)=input; - this->position->item = input; - } - - template - void CircularLinkedList::Del() - { - node * new_position; - - if ( this->list_size == 0 ) - return ; - - else - if ( this->list_size == 1 ) - { - // RakNet::OP_DELETE(root->item, _FILE_AND_LINE_); - RakNet::OP_DELETE(this->root, _FILE_AND_LINE_); - this->root = this->position = 0; - this->list_size = 0; - } - - else - { - ( this->position->previous ) ->next = this->position->next; - ( this->position->next ) ->previous = this->position->previous; - new_position = this->position->next; - - if ( this->position == this->root ) - this->root = new_position; - - // RakNet::OP_DELETE(position->item, _FILE_AND_LINE_); - RakNet::OP_DELETE(this->position, _FILE_AND_LINE_); - - this->position = new_position; - - this->list_size--; - } - } - - template - bool CircularLinkedList::IsIn(const CircularLinkedListType& input ) - { - node * return_value, *old_position; - - old_position = this->position; - - return_value = FindPointer( input ); - this->position = old_position; - - if ( return_value != 0 ) - return true; - else - return false; // Can't find the item don't do anything - } - - template - bool CircularLinkedList::Find( const CircularLinkedListType& input ) - { - node * return_value; - - return_value = FindPointer( input ); - - if ( return_value != 0 ) - { - this->position = return_value; - return true; - } - - else - return false; // Can't find the item don't do anything - } - - template - typename CircularLinkedList::node* CircularLinkedList::FindPointer( const CircularLinkedListType& input ) - { - node * current; - - if ( this->list_size == 0 ) - return 0; - - current = this->root; - - // Search for the item starting from the root node and incrementing the pointer after every check - // If you wind up pointing at the root again you looped around the list so didn't find the item, in which case return 0 - do - { - // if (*(current->item) == input) return current; - - if ( current->item == input ) - return current; - - current = current->next; - } - - while ( current != this->root ); - - return 0; - - } - - template - inline unsigned int CircularLinkedList::Size( void ) - { - return this->list_size; - } - - template - inline CircularLinkedListType& CircularLinkedList::Peek( void ) - { - // return *(position->item); - return this->position->item; - } - - template - CircularLinkedListType CircularLinkedList::Pop( void ) - { - CircularLinkedListType element; - element = Peek(); - Del(); - return CircularLinkedListType( element ); // return temporary - } - - // Prefix - template - CircularLinkedList& CircularLinkedList::operator++() - { - if ( this->list_size != 0 ) - position = position->next; - - return *this; - } - - /* - // Postfix - template - CircularLinkedList& CircularLinkedList::operator++(int) - { - CircularLinkedList before; - before=*this; - operator++(); - return before; - } - */ - - template - CircularLinkedList& CircularLinkedList::operator++( int ) - { - return this->operator++(); - } - - // Prefix - template - CircularLinkedList& CircularLinkedList::operator--() - { - if ( this->list_size != 0 ) - this->position = this->position->previous; - - return *this; - } - - /* - // Postfix - template - CircularLinkedList& CircularLinkedList::operator--(int) - { - CircularLinkedList before; - before=*this; - operator--(); - return before; - } - */ - - template - CircularLinkedList& CircularLinkedList::operator--( int ) - { - return this->operator--(); - } - - template - void CircularLinkedList::Clear( void ) - { - if ( this->list_size == 0 ) - return ; - else - if ( this->list_size == 1 ) // {RakNet::OP_DELETE(root->item); RakNet::OP_DELETE(root, _FILE_AND_LINE_);} - { - RakNet::OP_DELETE(this->root, _FILE_AND_LINE_); - } - - else - { - node* current; - node* temp; - - current = this->root; - - do - { - temp = current; - current = current->next; - // RakNet::OP_DELETE(temp->item, _FILE_AND_LINE_); - RakNet::OP_DELETE(temp, _FILE_AND_LINE_); - } - - while ( current != this->root ); - } - - this->list_size = 0; - this->root = 0; - this->position = 0; - } - - template - inline void CircularLinkedList::Concatenate( const CircularLinkedList& L ) - { - unsigned int counter; - node* ptr; - - if ( L.list_size == 0 ) - return ; - - if ( this->list_size == 0 ) - * this = L; - - ptr = L.root; - - this->position = this->root->previous; - - // Cycle through each element in L and add it to the current list - for ( counter = 0; counter < L.list_size; counter++ ) - { - // Add item after the current item pointed to - // add(*(ptr->item)); - - Add ( ptr->item ); - - // Update pointers. Moving ptr keeps the current pointer at the end of the list since the add function does not move the pointer - ptr = ptr->next; - - this->position = this->position->next; - } - } - - template - inline void CircularLinkedList::Sort( void ) - { - if ( this->list_size <= 1 ) - return ; - - // Call equal operator to assign result of mergesort to current object - *this = Mergesort( *this ); - - this->position = this->root; - } - - template - CircularLinkedList CircularLinkedList::Mergesort( const CircularLinkedList& L ) - { - unsigned int counter; - node* location; - CircularLinkedList L1; - CircularLinkedList L2; - - location = L.root; - - // Split the list into two equal size sublists, L1 and L2 - - for ( counter = 0; counter < L.list_size / 2; counter++ ) - { - // L1.add (*(location->item)); - L1.Add ( location->item ); - location = location->next; - } - - for ( ;counter < L.list_size; counter++ ) - { - // L2.Add(*(location->item)); - L2.Add ( location->item ); - location = location->next; - } - - // Recursively sort the sublists - if ( L1.list_size > 1 ) - L1 = Mergesort( L1 ); - - if ( L2.list_size > 1 ) - L2 = Mergesort( L2 ); - - // Merge the two sublists - return Merge( L1, L2 ); - } - - template - CircularLinkedList CircularLinkedList::Merge( CircularLinkedList L1, CircularLinkedList L2 ) - { - CircularLinkedList X; - CircularLinkedListType element; - L1.position = L1.root; - L2.position = L2.root; - - // While neither list is empty - - while ( ( L1.list_size != 0 ) && ( L2.list_size != 0 ) ) - { - // Compare the first items of L1 and L2 - // Remove the smaller of the two items from the list - - if ( ( ( L1.root ) ->item ) < ( ( L2.root ) ->item ) ) - // if ((*((L1.root)->item)) < (*((L2.root)->item))) - { - // element = *((L1.root)->item); - element = ( L1.root ) ->item; - L1.Del(); - } - else - { - // element = *((L2.root)->item); - element = ( L2.root ) ->item; - L2.Del(); - } - - // Add this item to the end of X - X.Add( element ); - - X++; - } - - // Add the remaining list to X - if ( L1.list_size != 0 ) - X.Concatenate( L1 ); - else - X.Concatenate( L2 ); - - return X; - } - - template - LinkedList LinkedList::Mergesort( const LinkedList& L ) - { - unsigned int counter; - typename LinkedList::node* location; - LinkedList L1; - LinkedList L2; - - location = L.root; - - // Split the list into two equal size sublists, L1 and L2 - - for ( counter = 0; counter < L.LinkedList_size / 2; counter++ ) - { - // L1.add (*(location->item)); - L1.Add ( location->item ); - location = location->next; - } - - for ( ;counter < L.LinkedList_size; counter++ ) - { - // L2.Add(*(location->item)); - L2.Add ( location->item ); - location = location->next; - } - - // Recursively sort the sublists - if ( L1.list_size > 1 ) - L1 = Mergesort( L1 ); - - if ( L2.list_size > 1 ) - L2 = Mergesort( L2 ); - - // Merge the two sublists - return Merge( L1, L2 ); - } - - template - LinkedList LinkedList::Merge( LinkedList L1, LinkedList L2 ) - { - LinkedList X; - LinkedListType element; - L1.position = L1.root; - L2.position = L2.root; - - // While neither list is empty - - while ( ( L1.LinkedList_size != 0 ) && ( L2.LinkedList_size != 0 ) ) - { - // Compare the first items of L1 and L2 - // Remove the smaller of the two items from the list - - if ( ( ( L1.root ) ->item ) < ( ( L2.root ) ->item ) ) - // if ((*((L1.root)->item)) < (*((L2.root)->item))) - { - element = ( L1.root ) ->item; - // element = *((L1.root)->item); - L1.Del(); - } - else - { - element = ( L2.root ) ->item; - // element = *((L2.root)->item); - L2.Del(); - } - - // Add this item to the end of X - X.Add( element ); - } - - // Add the remaining list to X - if ( L1.LinkedList_size != 0 ) - X.concatenate( L1 ); - else - X.concatenate( L2 ); - - return X; - } - - - // Prefix - template - LinkedList& LinkedList::operator++() - { - if ( ( this->list_size != 0 ) && ( this->position->next != this->root ) ) - this->position = this->position->next; - - return *this; - } - - /* - // Postfix - template - LinkedList& LinkedList::operator++(int) - { - LinkedList before; - before=*this; - operator++(); - return before; - } - */ - // Postfix - template - LinkedList& LinkedList::operator++( int ) - { - return this->operator++(); - } - - // Prefix - template - LinkedList& LinkedList::operator--() - { - if ( ( this->list_size != 0 ) && ( this->position != this->root ) ) - this->position = this->position->previous; - - return *this; - } - - /* - // Postfix - template - LinkedList& LinkedList::operator--(int) - { - LinkedList before; - before=*this; - operator--(); - return before; - } - */ - - // Postfix - template - LinkedList& LinkedList::operator--( int ) - { - return this->operator--(); - } - -} // End namespace - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_LinkedList.h +/// \internal +/// \brief Straightforward linked list data structure. +/// + + +#pragma once + +#include "Export.h" +#include "RakMemoryOverride.h" + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + // Prototype to prevent error in CircularLinkedList class when a reference is made to a LinkedList class + template + class RAK_DLL_EXPORT LinkedList; + + /** + * \brief (Circular) Linked List ADT (Doubly Linked Pointer to Node Style) - + * + * \details + * Initilize with the following command + * LinkedList + * OR + * CircularLinkedList + * + * Has the following member functions + * - size: returns number of elements in the linked list + * - insert(item): inserts @em item at the current position in + * the LinkedList. + * - add(item): inserts @em item after the current position in + * the LinkedList. Does not increment the position + * - replace(item): replaces the element at the current position @em item. + * - peek: returns the element at the current position + * - pop: returns the element at the current position and deletes it + * - del: deletes the current element. Does nothing for an empty list. + * - clear: empties the LinkedList and returns storage + * - bool IsInitem): Does a linear search for @em item. Does not set + * the position to it, only returns true on item found, false otherwise + * - bool find(item): Does a linear search for @em item and sets the current + * position to point to it if and only if the item is found. Returns true + * on item found, false otherwise + * - sort: Sorts the elements of the list with a mergesort and sets the + * current pointer to the first element + * - concatenate(list L): This appends L to the current list + * - ++(prefix): moves the pointer one element up in the list and returns the + * appropriate copy of the element in the list + * - --(prefix): moves the pointer one element back in the list and returns + * the appropriate copy of the element in the list + * - beginning - moves the pointer to the start of the list. For circular + * linked lists this is first 'position' created. You should call this + * after the sort function to read the first value. + * - end - moves the pointer to the end of the list. For circular linked + * lists this is one less than the first 'position' created + * The assignment and copy constructor operators are defined + * + * \note + * 1. LinkedList and CircularLinkedList are exactly the same except LinkedList + * won't let you wrap around the root and lets you jump to two positions + * relative to the root/ + * 2. Postfix ++ and -- can be used but simply call the prefix versions. + * + * + * EXAMPLE: + * @code + * LinkedList A; // Creates a Linked List of integers called A + * CircularLinkedList B; // Creates a Circular Linked List of + * // integers called B + * + * A.Insert(20); // Adds 20 to A. A: 20 - current is 20 + * A.Insert(5); // Adds 5 to A. A: 5 20 - current is 5 + * A.Insert(1); // Adds 1 to A. A: 1 5 20 - current is 1 + * + * A.IsIn1); // returns true + * A.IsIn200); // returns false + * A.Find(5); // returns true and sets current to 5 + * A.Peek(); // returns 5 + * A.Find(1); // returns true and sets current to 1 + * + * (++A).Peek(); // Returns 5 + * A.Peek(); // Returns 5 + * + * A.Replace(10); // Replaces 5 with 10. + * A.Peek(); // Returns 10 + * + * A.Beginning(); // Current points to the beginning of the list at 1 + * + * (++A).Peek(); // Returns 5 + * A.Peek(); // Returns 10 + * + * A.Del(); // Deletes 10. Current points to the next element, which is 20 + * A.Peek(); // Returns 20 + * + * A.Beginning(); // Current points to the beginning of the list at 1 + * + * (++A).Peek(); // Returns 5 + * A.Peek(); // Returns 20 + * + * A.Clear(_FILE_AND_LINE_); // Deletes all nodes in A + * + * A.Insert(5); // A: 5 - current is 5 + * A.Insert(6); // A: 6 5 - current is 6 + * A.Insert(7); // A: 7 6 5 - current is 7 + * + * A.Clear(_FILE_AND_LINE_); + * B.Clear(_FILE_AND_LINE_); + * + * B.Add(10); + * B.Add(20); + * B.Add(30); + * B.Add(5); + * B.Add(2); + * B.Add(25); + * // Sorts the numbers in the list and sets the current pointer to the + * // first element + * B.sort(); + * + * // Postfix ++ just calls the prefix version and has no functional + * // difference. + * B.Peek(); // Returns 2 + * B++; + * B.Peek(); // Returns 5 + * B++; + * B.Peek(); // Returns 10 + * B++; + * B.Peek(); // Returns 20 + * B++; + * B.Peek(); // Returns 25 + * B++; + * B.Peek(); // Returns 30 + * @endcode + */ + template + + class CircularLinkedList + { + + public: + + struct node + { + CircularLinkedListType item; + + node* previous; + node* next; + }; + + CircularLinkedList(); + ~CircularLinkedList(); + CircularLinkedList( const CircularLinkedList& original_copy ); + // CircularLinkedList(LinkedList original_copy) {CircularLinkedList(original_copy);} // Converts linked list to circular type + bool operator= ( const CircularLinkedList& original_copy ); + CircularLinkedList& operator++(); // CircularLinkedList A; ++A; + CircularLinkedList& operator++( int ); // Circular_Linked List A; A++; + CircularLinkedList& operator--(); // CircularLinkedList A; --A; + CircularLinkedList& operator--( int ); // Circular_Linked List A; A--; + bool IsIn( const CircularLinkedListType& input ); + bool Find( const CircularLinkedListType& input ); + void Insert( const CircularLinkedListType& input ); + + CircularLinkedListType& Add ( const CircularLinkedListType& input ) + + ; // Adds after the current position + void Replace( const CircularLinkedListType& input ); + + void Del( void ); + + unsigned int Size( void ); + + CircularLinkedListType& Peek( void ); + + CircularLinkedListType Pop( void ); + + void Clear( void ); + + void Sort( void ); + + void Beginning( void ); + + void End( void ); + + void Concatenate( const CircularLinkedList& L ); + + protected: + unsigned int list_size; + + node *root; + + node *position; + + node* FindPointer( const CircularLinkedListType& input ); + + private: + CircularLinkedList Merge( CircularLinkedList L1, CircularLinkedList L2 ); + + CircularLinkedList Mergesort( const CircularLinkedList& L ); + }; + + template + + class LinkedList : public CircularLinkedList + { + + public: + LinkedList() + {} + + LinkedList( const LinkedList& original_copy ); + ~LinkedList(); + bool operator= ( const LinkedList& original_copy ); + LinkedList& operator++(); // LinkedList A; ++A; + LinkedList& operator++( int ); // Linked List A; A++; + LinkedList& operator--(); // LinkedList A; --A; + LinkedList& operator--( int ); // Linked List A; A--; + + private: + LinkedList Merge( LinkedList L1, LinkedList L2 ); + LinkedList Mergesort( const LinkedList& L ); + + }; + + + template + inline void CircularLinkedList::Beginning( void ) + { + if ( this->root ) + this->position = this->root; + } + + template + inline void CircularLinkedList::End( void ) + { + if ( this->root ) + this->position = this->root->previous; + } + + template + bool LinkedList::operator= ( const LinkedList& original_copy ) + { + typename LinkedList::node * original_copy_pointer, *last, *save_position; + + if ( ( &original_copy ) != this ) + { + + this->Clear(); + + + if ( original_copy.list_size == 0 ) + { + this->root = 0; + this->position = 0; + this->list_size = 0; + } + + else + if ( original_copy.list_size == 1 ) + { + this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + this->root->next = this->root; + this->root->previous = this->root; + this->list_size = 1; + this->position = this->root; + // *(root->item)=*((original_copy.root)->item); + this->root->item = original_copy.root->item; + } + + else + { + // Setup the first part of the root node + original_copy_pointer = original_copy.root; + this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + this->position = this->root; + // *(root->item)=*((original_copy.root)->item); + this->root->item = original_copy.root->item; + + if ( original_copy_pointer == original_copy.position ) + save_position = this->position; + + do + { + + + // Save the current element + last = this->position; + + // Point to the next node in the source list + original_copy_pointer = original_copy_pointer->next; + + // Create a new node and point position to it + this->position = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // position->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + + // Copy the item to the new node + // *(position->item)=*(original_copy_pointer->item); + this->position->item = original_copy_pointer->item; + + if ( original_copy_pointer == original_copy.position ) + save_position = this->position; + + + // Set the previous pointer for the new node + ( this->position->previous ) = last; + + // Set the next pointer for the old node to the new node + ( last->next ) = this->position; + + } + + while ( ( original_copy_pointer->next ) != ( original_copy.root ) ); + + // Complete the circle. Set the next pointer of the newest node to the root and the previous pointer of the root to the newest node + this->position->next = this->root; + + this->root->previous = this->position; + + this->list_size = original_copy.list_size; + + this->position = save_position; + } + } + + return true; + } + + + template + CircularLinkedList::CircularLinkedList() + { + this->root = 0; + this->position = 0; + this->list_size = 0; + } + + template + CircularLinkedList::~CircularLinkedList() + { + this->Clear(); + } + + template + LinkedList::~LinkedList() + { + this->Clear(); + } + + template + LinkedList::LinkedList( const LinkedList& original_copy ) + { + typename LinkedList::node * original_copy_pointer, *last, *save_position; + + if ( original_copy.list_size == 0 ) + { + this->root = 0; + this->position = 0; + this->list_size = 0; + return ; + } + + else + if ( original_copy.list_size == 1 ) + { + this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + this->root->next = this->root; + this->root->previous = this->root; + this->list_size = 1; + this->position = this->root; + // *(root->item) = *((original_copy.root)->item); + this->root->item = original_copy.root->item; + } + + else + { + // Setup the first part of the root node + original_copy_pointer = original_copy.root; + this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + this->position = this->root; + // *(root->item)=*((original_copy.root)->item); + this->root->item = original_copy.root->item; + + if ( original_copy_pointer == original_copy.position ) + save_position = this->position; + + do + { + // Save the current element + last = this->position; + + // Point to the next node in the source list + original_copy_pointer = original_copy_pointer->next; + + // Create a new node and point position to it + this->position = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // position->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + + // Copy the item to the new node + // *(position->item)=*(original_copy_pointer->item); + this->position->item = original_copy_pointer->item; + + if ( original_copy_pointer == original_copy.position ) + save_position = this->position; + + // Set the previous pointer for the new node + ( this->position->previous ) = last; + + // Set the next pointer for the old node to the new node + ( last->next ) = this->position; + + } + + while ( ( original_copy_pointer->next ) != ( original_copy.root ) ); + + // Complete the circle. Set the next pointer of the newest node to the root and the previous pointer of the root to the newest node + this->position->next = this->root; + + this->root->previous = this->position; + + this->list_size = original_copy.list_size; + + this->position = save_position; + } + } + +#ifdef _MSC_VER +#pragma warning( disable : 4701 ) // warning C4701: local variable may be used without having been initialized +#endif + template + CircularLinkedList::CircularLinkedList( const CircularLinkedList& original_copy ) + { + node * original_copy_pointer; + node *last; + node *save_position; + + if ( original_copy.list_size == 0 ) + { + this->root = 0; + this->position = 0; + this->list_size = 0; + return ; + } + + else + if ( original_copy.list_size == 1 ) + { + this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + this->root->next = this->root; + this->root->previous = this->root; + this->list_size = 1; + this->position = this->root; + // *(root->item) = *((original_copy.root)->item); + this->root->item = original_copy.root->item; + } + + else + { + // Setup the first part of the root node + original_copy_pointer = original_copy.root; + this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + this->position = this->root; + // *(root->item)=*((original_copy.root)->item); + this->root->item = original_copy.root->item; + + if ( original_copy_pointer == original_copy.position ) + save_position = this->position; + + do + { + + + // Save the current element + last = this->position; + + // Point to the next node in the source list + original_copy_pointer = original_copy_pointer->next; + + // Create a new node and point position to it + this->position = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // position->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + + // Copy the item to the new node + // *(position->item)=*(original_copy_pointer->item); + this->position->item = original_copy_pointer->item; + + if ( original_copy_pointer == original_copy.position ) + save_position = position; + + // Set the previous pointer for the new node + ( this->position->previous ) = last; + + // Set the next pointer for the old node to the new node + ( last->next ) = this->position; + + } + + while ( ( original_copy_pointer->next ) != ( original_copy.root ) ); + + // Complete the circle. Set the next pointer of the newest node to the root and the previous pointer of the root to the newest node + this->position->next = this->root; + + this->root->previous = position; + + this->list_size = original_copy.list_size; + + this->position = save_position; + } + } + +#ifdef _MSC_VER +#pragma warning( disable : 4701 ) // warning C4701: local variable may be used without having been initialized +#endif + template + bool CircularLinkedList::operator= ( const CircularLinkedList& original_copy ) + { + node * original_copy_pointer; + node *last; + node *save_position; + + if ( ( &original_copy ) != this ) + { + + this->Clear(); + + + if ( original_copy.list_size == 0 ) + { + this->root = 0; + this->position = 0; + this->list_size = 0; + } + + else + if ( original_copy.list_size == 1 ) + { + this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + this->root->next = this->root; + this->root->previous = this->root; + this->list_size = 1; + this->position = this->root; + // *(root->item)=*((original_copy.root)->item); + this->root->item = original_copy.root->item; + } + + else + { + // Setup the first part of the root node + original_copy_pointer = original_copy.root; + this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + this->position = this->root; + // *(root->item)=*((original_copy.root)->item); + this->root->item = original_copy.root->item; + + if ( original_copy_pointer == original_copy.position ) + save_position = this->position; + + do + { + // Save the current element + last = this->position; + + // Point to the next node in the source list + original_copy_pointer = original_copy_pointer->next; + + // Create a new node and point position to it + this->position = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // position->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + + // Copy the item to the new node + // *(position->item)=*(original_copy_pointer->item); + this->position->item = original_copy_pointer->item; + + if ( original_copy_pointer == original_copy.position ) + save_position = this->position; + + // Set the previous pointer for the new node + ( this->position->previous ) = last; + + // Set the next pointer for the old node to the new node + ( last->next ) = this->position; + + } + + while ( ( original_copy_pointer->next ) != ( original_copy.root ) ); + + // Complete the circle. Set the next pointer of the newest node to the root and the previous pointer of the root to the newest node + this->position->next = this->root; + + this->root->previous = this->position; + + this->list_size = original_copy.list_size; + + this->position = save_position; + } + } + + return true; + } + + template + void CircularLinkedList::Insert( const CircularLinkedListType& input ) + { + node * new_node; + + if ( list_size == 0 ) + { + this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + //*(root->item)=input; + this->root->item = input; + this->root->next = this->root; + this->root->previous = this->root; + this->list_size = 1; + this->position = this->root; + } + + else + if ( list_size == 1 ) + { + this->position = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // position->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + this->root->next = this->position; + this->root->previous = this->position; + this->position->previous = this->root; + this->position->next = this->root; + // *(position->item)=input; + this->position->item = input; + this->root = this->position; // Since we're inserting into a 1 element list the old root is now the second item + this->list_size = 2; + } + + else + { + /* + + B + | + A --- C + + position->previous=A + new_node=B + position=C + + Note that the order of the following statements is important */ + + new_node = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // new_node->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + + // *(new_node->item)=input; + new_node->item = input; + + // Point next of A to B + ( this->position->previous ) ->next = new_node; + + // Point last of B to A + new_node->previous = this->position->previous; + + // Point last of C to B + this->position->previous = new_node; + + // Point next of B to C + new_node->next = this->position; + + // Since the root pointer is bound to a node rather than an index this moves it back if you insert an element at the root + + if ( this->position == this->root ) + { + this->root = new_node; + this->position = this->root; + } + + // Increase the recorded size of the list by one + this->list_size++; + } + } + + template + CircularLinkedListType& CircularLinkedList::Add ( const CircularLinkedListType& input ) + { + node * new_node; + + if ( this->list_size == 0 ) + { + this->root = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // root->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // *(root->item)=input; + this->root->item = input; + this->root->next = this->root; + this->root->previous = this->root; + this->list_size = 1; + this->position = this->root; + // return *(position->item); + return this->position->item; + } + + else + if ( list_size == 1 ) + { + this->position = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // position->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + this->root->next = this->position; + this->root->previous = this->position; + this->position->previous = this->root; + this->position->next = this->root; + // *(position->item)=input; + this->position->item = input; + this->list_size = 2; + this->position = this->root; // Don't move the position from the root + // return *(position->item); + return this->position->item; + } + + else + { + /* + + B + | + A --- C + + new_node=B + position=A + position->next=C + + Note that the order of the following statements is important */ + + new_node = RakNet::OP_NEW( _FILE_AND_LINE_ ); + // new_node->item = RakNet::OP_NEW( _FILE_AND_LINE_ ); + + // *(new_node->item)=input; + new_node->item = input; + + // Point last of B to A + new_node->previous = this->position; + + // Point next of B to C + new_node->next = ( this->position->next ); + + // Point last of C to B + ( this->position->next ) ->previous = new_node; + + // Point next of A to B + ( this->position->next ) = new_node; + + // Increase the recorded size of the list by one + this->list_size++; + + // return *(new_node->item); + return new_node->item; + } + } + + template + inline void CircularLinkedList::Replace( const CircularLinkedListType& input ) + { + if ( this->list_size > 0 ) + // *(position->item)=input; + this->position->item = input; + } + + template + void CircularLinkedList::Del() + { + node * new_position; + + if ( this->list_size == 0 ) + return ; + + else + if ( this->list_size == 1 ) + { + // RakNet::OP_DELETE(root->item, _FILE_AND_LINE_); + RakNet::OP_DELETE(this->root, _FILE_AND_LINE_); + this->root = this->position = 0; + this->list_size = 0; + } + + else + { + ( this->position->previous ) ->next = this->position->next; + ( this->position->next ) ->previous = this->position->previous; + new_position = this->position->next; + + if ( this->position == this->root ) + this->root = new_position; + + // RakNet::OP_DELETE(position->item, _FILE_AND_LINE_); + RakNet::OP_DELETE(this->position, _FILE_AND_LINE_); + + this->position = new_position; + + this->list_size--; + } + } + + template + bool CircularLinkedList::IsIn(const CircularLinkedListType& input ) + { + node * return_value, *old_position; + + old_position = this->position; + + return_value = FindPointer( input ); + this->position = old_position; + + if ( return_value != 0 ) + return true; + else + return false; // Can't find the item don't do anything + } + + template + bool CircularLinkedList::Find( const CircularLinkedListType& input ) + { + node * return_value; + + return_value = FindPointer( input ); + + if ( return_value != 0 ) + { + this->position = return_value; + return true; + } + + else + return false; // Can't find the item don't do anything + } + + template + typename CircularLinkedList::node* CircularLinkedList::FindPointer( const CircularLinkedListType& input ) + { + node * current; + + if ( this->list_size == 0 ) + return 0; + + current = this->root; + + // Search for the item starting from the root node and incrementing the pointer after every check + // If you wind up pointing at the root again you looped around the list so didn't find the item, in which case return 0 + do + { + // if (*(current->item) == input) return current; + + if ( current->item == input ) + return current; + + current = current->next; + } + + while ( current != this->root ); + + return 0; + + } + + template + inline unsigned int CircularLinkedList::Size( void ) + { + return this->list_size; + } + + template + inline CircularLinkedListType& CircularLinkedList::Peek( void ) + { + // return *(position->item); + return this->position->item; + } + + template + CircularLinkedListType CircularLinkedList::Pop( void ) + { + CircularLinkedListType element; + element = Peek(); + Del(); + return CircularLinkedListType( element ); // return temporary + } + + // Prefix + template + CircularLinkedList& CircularLinkedList::operator++() + { + if ( this->list_size != 0 ) + position = position->next; + + return *this; + } + + /* + // Postfix + template + CircularLinkedList& CircularLinkedList::operator++(int) + { + CircularLinkedList before; + before=*this; + operator++(); + return before; + } + */ + + template + CircularLinkedList& CircularLinkedList::operator++( int ) + { + return this->operator++(); + } + + // Prefix + template + CircularLinkedList& CircularLinkedList::operator--() + { + if ( this->list_size != 0 ) + this->position = this->position->previous; + + return *this; + } + + /* + // Postfix + template + CircularLinkedList& CircularLinkedList::operator--(int) + { + CircularLinkedList before; + before=*this; + operator--(); + return before; + } + */ + + template + CircularLinkedList& CircularLinkedList::operator--( int ) + { + return this->operator--(); + } + + template + void CircularLinkedList::Clear( void ) + { + if ( this->list_size == 0 ) + return ; + else + if ( this->list_size == 1 ) // {RakNet::OP_DELETE(root->item); RakNet::OP_DELETE(root, _FILE_AND_LINE_);} + { + RakNet::OP_DELETE(this->root, _FILE_AND_LINE_); + } + + else + { + node* current; + node* temp; + + current = this->root; + + do + { + temp = current; + current = current->next; + // RakNet::OP_DELETE(temp->item, _FILE_AND_LINE_); + RakNet::OP_DELETE(temp, _FILE_AND_LINE_); + } + + while ( current != this->root ); + } + + this->list_size = 0; + this->root = 0; + this->position = 0; + } + + template + inline void CircularLinkedList::Concatenate( const CircularLinkedList& L ) + { + unsigned int counter; + node* ptr; + + if ( L.list_size == 0 ) + return ; + + if ( this->list_size == 0 ) + * this = L; + + ptr = L.root; + + this->position = this->root->previous; + + // Cycle through each element in L and add it to the current list + for ( counter = 0; counter < L.list_size; counter++ ) + { + // Add item after the current item pointed to + // add(*(ptr->item)); + + Add ( ptr->item ); + + // Update pointers. Moving ptr keeps the current pointer at the end of the list since the add function does not move the pointer + ptr = ptr->next; + + this->position = this->position->next; + } + } + + template + inline void CircularLinkedList::Sort( void ) + { + if ( this->list_size <= 1 ) + return ; + + // Call equal operator to assign result of mergesort to current object + *this = Mergesort( *this ); + + this->position = this->root; + } + + template + CircularLinkedList CircularLinkedList::Mergesort( const CircularLinkedList& L ) + { + unsigned int counter; + node* location; + CircularLinkedList L1; + CircularLinkedList L2; + + location = L.root; + + // Split the list into two equal size sublists, L1 and L2 + + for ( counter = 0; counter < L.list_size / 2; counter++ ) + { + // L1.add (*(location->item)); + L1.Add ( location->item ); + location = location->next; + } + + for ( ;counter < L.list_size; counter++ ) + { + // L2.Add(*(location->item)); + L2.Add ( location->item ); + location = location->next; + } + + // Recursively sort the sublists + if ( L1.list_size > 1 ) + L1 = Mergesort( L1 ); + + if ( L2.list_size > 1 ) + L2 = Mergesort( L2 ); + + // Merge the two sublists + return Merge( L1, L2 ); + } + + template + CircularLinkedList CircularLinkedList::Merge( CircularLinkedList L1, CircularLinkedList L2 ) + { + CircularLinkedList X; + CircularLinkedListType element; + L1.position = L1.root; + L2.position = L2.root; + + // While neither list is empty + + while ( ( L1.list_size != 0 ) && ( L2.list_size != 0 ) ) + { + // Compare the first items of L1 and L2 + // Remove the smaller of the two items from the list + + if ( ( ( L1.root ) ->item ) < ( ( L2.root ) ->item ) ) + // if ((*((L1.root)->item)) < (*((L2.root)->item))) + { + // element = *((L1.root)->item); + element = ( L1.root ) ->item; + L1.Del(); + } + else + { + // element = *((L2.root)->item); + element = ( L2.root ) ->item; + L2.Del(); + } + + // Add this item to the end of X + X.Add( element ); + + X++; + } + + // Add the remaining list to X + if ( L1.list_size != 0 ) + X.Concatenate( L1 ); + else + X.Concatenate( L2 ); + + return X; + } + + template + LinkedList LinkedList::Mergesort( const LinkedList& L ) + { + unsigned int counter; + typename LinkedList::node* location; + LinkedList L1; + LinkedList L2; + + location = L.root; + + // Split the list into two equal size sublists, L1 and L2 + + for ( counter = 0; counter < L.LinkedList_size / 2; counter++ ) + { + // L1.add (*(location->item)); + L1.Add ( location->item ); + location = location->next; + } + + for ( ;counter < L.LinkedList_size; counter++ ) + { + // L2.Add(*(location->item)); + L2.Add ( location->item ); + location = location->next; + } + + // Recursively sort the sublists + if ( L1.list_size > 1 ) + L1 = Mergesort( L1 ); + + if ( L2.list_size > 1 ) + L2 = Mergesort( L2 ); + + // Merge the two sublists + return Merge( L1, L2 ); + } + + template + LinkedList LinkedList::Merge( LinkedList L1, LinkedList L2 ) + { + LinkedList X; + LinkedListType element; + L1.position = L1.root; + L2.position = L2.root; + + // While neither list is empty + + while ( ( L1.LinkedList_size != 0 ) && ( L2.LinkedList_size != 0 ) ) + { + // Compare the first items of L1 and L2 + // Remove the smaller of the two items from the list + + if ( ( ( L1.root ) ->item ) < ( ( L2.root ) ->item ) ) + // if ((*((L1.root)->item)) < (*((L2.root)->item))) + { + element = ( L1.root ) ->item; + // element = *((L1.root)->item); + L1.Del(); + } + else + { + element = ( L2.root ) ->item; + // element = *((L2.root)->item); + L2.Del(); + } + + // Add this item to the end of X + X.Add( element ); + } + + // Add the remaining list to X + if ( L1.LinkedList_size != 0 ) + X.concatenate( L1 ); + else + X.concatenate( L2 ); + + return X; + } + + + // Prefix + template + LinkedList& LinkedList::operator++() + { + if ( ( this->list_size != 0 ) && ( this->position->next != this->root ) ) + this->position = this->position->next; + + return *this; + } + + /* + // Postfix + template + LinkedList& LinkedList::operator++(int) + { + LinkedList before; + before=*this; + operator++(); + return before; + } + */ + // Postfix + template + LinkedList& LinkedList::operator++( int ) + { + return this->operator++(); + } + + // Prefix + template + LinkedList& LinkedList::operator--() + { + if ( ( this->list_size != 0 ) && ( this->position != this->root ) ) + this->position = this->position->previous; + + return *this; + } + + /* + // Postfix + template + LinkedList& LinkedList::operator--(int) + { + LinkedList before; + before=*this; + operator--(); + return before; + } + */ + + // Postfix + template + LinkedList& LinkedList::operator--( int ) + { + return this->operator--(); + } + +} // End namespace + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif + diff --git a/Source/DS_List.h b/Source/DS_List.h index 7ebe7b549..90c1ff781 100644 --- a/Source/DS_List.h +++ b/Source/DS_List.h @@ -1,525 +1,526 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_List.h -/// \internal -/// \brief Array based list. -/// \details Usually the Queue class is used instead, since it has all the same functionality and is only worse at random access. -/// - - -#ifndef __LIST_H -#define __LIST_H - -#include "RakAssert.h" -#include // memmove -#include "Export.h" -#include "RakMemoryOverride.h" - -/// Maximum unsigned long -static const unsigned int MAX_UNSIGNED_LONG = 4294967295U; - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - /// \brief Array based implementation of a list. - /// \note ONLY USE THIS FOR SHALLOW COPIES. I don't bother with operator= to improve performance. - template - class RAK_DLL_EXPORT List - { - public: - /// Default constructor - List(); - - // Destructor - ~List(); - - /// \brief Copy constructor. - /// \param[in] original_copy The list to duplicate - List( const List& original_copy ); - - /// \brief Assign one list to another. - List& operator= ( const List& original_copy ); - - /// \brief Access an element by its index in the array. - /// \param[in] position The index into the array. - /// \return The element at position \a position. - list_type& operator[] ( const unsigned int position ) const; - - /// \brief Access an element by its index in the array. - /// \param[in] position The index into the array. - /// \return The element at position \a position. - list_type& Get ( const unsigned int position ) const; - - /// \brief Push an element at the end of the stack. - /// \param[in] input The new element. - void Push(const list_type &input, const char *file, unsigned int line ); - - /// \brief Pop an element from the end of the stack. - /// \pre Size()>0 - /// \return The element at the end. - list_type& Pop(void); - - /// \brief Insert an element at position \a position in the list. - /// \param[in] input The new element. - /// \param[in] position The position of the new element. - void Insert( const list_type &input, const unsigned int position, const char *file, unsigned int line ); - - /// \brief Insert at the end of the list. - /// \param[in] input The new element. - void Insert( const list_type &input, const char *file, unsigned int line ); - - /// \brief Replace the value at \a position by \a input. - /// \details If the size of the list is less than @em position, it increase the capacity of - /// the list and fill slot with @em filler. - /// \param[in] input The element to replace at position @em position. - /// \param[in] filler The element use to fill new allocated capacity. - /// \param[in] position The position of input in the list. - void Replace( const list_type &input, const list_type filler, const unsigned int position, const char *file, unsigned int line ); - - /// \brief Replace the last element of the list by \a input. - /// \param[in] input The element used to replace the last element. - void Replace( const list_type &input ); - - /// \brief Delete the element at position \a position. - /// \param[in] position The index of the element to delete - void RemoveAtIndex( const unsigned int position ); - - /// \brief Delete the element at position \a position. - /// \note - swaps middle with end of list, only use if list order does not matter - /// \param[in] position The index of the element to delete - void RemoveAtIndexFast( const unsigned int position ); - - /// \brief Delete the element at the end of the list. - void RemoveFromEnd(const unsigned num=1); - - /// \brief Returns the index of the specified item or MAX_UNSIGNED_LONG if not found. - /// \param[in] input The element to check for - /// \return The index or position of @em input in the list. - /// \retval MAX_UNSIGNED_LONG The object is not in the list - /// \retval [Integer] The index of the element in the list - unsigned int GetIndexOf( const list_type &input ) const; - - /// \return The number of elements in the list - unsigned int Size( void ) const; - - /// \brief Clear the list - void Clear( bool doNotDeallocateSmallBlocks, const char *file, unsigned int line ); - - /// \brief Preallocate the list, so it needs fewer reallocations at runtime. - void Preallocate( unsigned countNeeded, const char *file, unsigned int line ); - - /// \brief Frees overallocated members, to use the minimum memory necessary. - /// \attention - /// This is a slow operation - void Compress( const char *file, unsigned int line ); - - private: - /// An array of user values - list_type* listArray; - - /// Number of elements in the list - unsigned int list_size; - - /// Size of \a array - unsigned int allocation_size; - }; - template - List::List() - { - allocation_size = 0; - listArray = 0; - list_size = 0; - } - - template - List::~List() - { - if (allocation_size>0) - RakNet::OP_DELETE_ARRAY(listArray, _FILE_AND_LINE_); - } - - - template - List::List( const List& original_copy ) - { - // Allocate memory for copy - - if ( original_copy.list_size == 0 ) - { - list_size = 0; - allocation_size = 0; - } - else - { - listArray = RakNet::OP_NEW_ARRAY( original_copy.list_size , _FILE_AND_LINE_ ); - - for ( unsigned int counter = 0; counter < original_copy.list_size; ++counter ) - listArray[ counter ] = original_copy.listArray[ counter ]; - - // Don't call constructors, assignment operators, etc. - //memcpy(listArray, original_copy.listArray, original_copy.list_size*sizeof(list_type)); - - list_size = allocation_size = original_copy.list_size; - } - } - - template - List& List::operator= ( const List& original_copy ) - { - if ( ( &original_copy ) != this ) - { - Clear( false, _FILE_AND_LINE_ ); - - // Allocate memory for copy - - if ( original_copy.list_size == 0 ) - { - list_size = 0; - allocation_size = 0; - } - - else - { - listArray = RakNet::OP_NEW_ARRAY( original_copy.list_size , _FILE_AND_LINE_ ); - - for ( unsigned int counter = 0; counter < original_copy.list_size; ++counter ) - listArray[ counter ] = original_copy.listArray[ counter ]; - // Don't call constructors, assignment operators, etc. - //memcpy(listArray, original_copy.listArray, original_copy.list_size*sizeof(list_type)); - - list_size = allocation_size = original_copy.list_size; - } - } - - return *this; - } - - - template - inline list_type& List::operator[] ( const unsigned int position ) const - { - #ifdef _DEBUG - if (position>=list_size) - { - RakAssert ( position < list_size ); - } - #endif - return listArray[ position ]; - } - - // Just here for debugging - template - inline list_type& List::Get ( const unsigned int position ) const - { - return listArray[ position ]; - } - - template - void List::Push(const list_type &input, const char *file, unsigned int line) - { - Insert(input, file, line); - } - - template - inline list_type& List::Pop(void) - { -#ifdef _DEBUG - RakAssert(list_size>0); -#endif - --list_size; - return listArray[list_size]; - } - - template - void List::Insert( const list_type &input, const unsigned int position, const char *file, unsigned int line ) - { -#ifdef _DEBUG - if (position>list_size) - { - RakAssert( position <= list_size ); - } -#endif - - // Reallocate list if necessary - if ( list_size == allocation_size ) - { - // allocate twice the currently allocated memory - list_type * new_array; - - if ( allocation_size == 0 ) - allocation_size = 16; - else - allocation_size *= 2; - - new_array = RakNet::OP_NEW_ARRAY( allocation_size , file, line ); - - // copy old array over - for ( unsigned int counter = 0; counter < list_size; ++counter ) - new_array[ counter ] = listArray[ counter ]; - - // Don't call constructors, assignment operators, etc. - //memcpy(new_array, listArray, list_size*sizeof(list_type)); - - // set old array to point to the newly allocated and twice as large array - RakNet::OP_DELETE_ARRAY(listArray, file, line); - - listArray = new_array; - } - - // Move the elements in the list to make room - for ( unsigned int counter = list_size; counter != position; counter-- ) - listArray[ counter ] = listArray[ counter - 1 ]; - - // Don't call constructors, assignment operators, etc. - //memmove(listArray+position+1, listArray+position, (list_size-position)*sizeof(list_type)); - - // Insert the new item at the correct spot - listArray[ position ] = input; - - ++list_size; - - } - - - template - void List::Insert( const list_type &input, const char *file, unsigned int line ) - { - // Reallocate list if necessary - - if ( list_size == allocation_size ) - { - // allocate twice the currently allocated memory - list_type * new_array; - - if ( allocation_size == 0 ) - allocation_size = 16; - else - allocation_size *= 2; - - new_array = RakNet::OP_NEW_ARRAY( allocation_size , file, line ); - - if (listArray) - { - // copy old array over - for ( unsigned int counter = 0; counter < list_size; ++counter ) - new_array[ counter ] = listArray[ counter ]; - - // Don't call constructors, assignment operators, etc. - //memcpy(new_array, listArray, list_size*sizeof(list_type)); - - // set old array to point to the newly allocated and twice as large array - RakNet::OP_DELETE_ARRAY(listArray, file, line); - } - - listArray = new_array; - } - - // Insert the new item at the correct spot - listArray[ list_size ] = input; - - ++list_size; - } - - template - inline void List::Replace( const list_type &input, const list_type filler, const unsigned int position, const char *file, unsigned int line ) - { - if ( ( list_size > 0 ) && ( position < list_size ) ) - { - // Direct replacement - listArray[ position ] = input; - } - else - { - if ( position >= allocation_size ) - { - // Reallocate the list to size position and fill in blanks with filler - list_type * new_array; - allocation_size = position + 1; - - new_array = RakNet::OP_NEW_ARRAY( allocation_size , file, line ); - - // copy old array over - - for ( unsigned int counter = 0; counter < list_size; ++counter ) - new_array[ counter ] = listArray[ counter ]; - - // Don't call constructors, assignment operators, etc. - //memcpy(new_array, listArray, list_size*sizeof(list_type)); - - // set old array to point to the newly allocated array - RakNet::OP_DELETE_ARRAY(listArray, file, line); - - listArray = new_array; - } - - // Fill in holes with filler - while ( list_size < position ) - listArray[ list_size++ ] = filler; - - // Fill in the last element with the new item - listArray[ list_size++ ] = input; - -#ifdef _DEBUG - - RakAssert( list_size == position + 1 ); - -#endif - - } - } - - template - inline void List::Replace( const list_type &input ) - { - if ( list_size > 0 ) - listArray[ list_size - 1 ] = input; - } - - template - void List::RemoveAtIndex( const unsigned int position ) - { -#ifdef _DEBUG - if (position >= list_size) - { - RakAssert( position < list_size ); - return; - } -#endif - - if ( position < list_size ) - { - // Compress the array - for ( unsigned int counter = position; counter < list_size - 1 ; ++counter ) - listArray[ counter ] = listArray[ counter + 1 ]; - // Don't call constructors, assignment operators, etc. - // memmove(listArray+position, listArray+position+1, (list_size-1-position) * sizeof(list_type)); - - RemoveFromEnd(); - } - } - - template - void List::RemoveAtIndexFast( const unsigned int position ) - { -#ifdef _DEBUG - if (position >= list_size) - { - RakAssert( position < list_size ); - return; - } -#endif - --list_size; - listArray[position]=listArray[list_size]; - } - - template - inline void List::RemoveFromEnd( const unsigned num ) - { - // Delete the last elements on the list. No compression needed -#ifdef _DEBUG - RakAssert(list_size>=num); -#endif - list_size-=num; - } - - template - unsigned int List::GetIndexOf( const list_type &input ) const - { - for ( unsigned int i = 0; i < list_size; ++i ) - if ( listArray[ i ] == input ) - return i; - - return MAX_UNSIGNED_LONG; - } - - template - inline unsigned int List::Size( void ) const - { - return list_size; - } - - template - void List::Clear( bool doNotDeallocateSmallBlocks, const char *file, unsigned int line ) - { - if ( allocation_size == 0 ) - return; - - if (allocation_size>512 || doNotDeallocateSmallBlocks==false) - { - RakNet::OP_DELETE_ARRAY(listArray, file, line); - allocation_size = 0; - listArray = 0; - } - list_size = 0; - } - - template - void List::Compress( const char *file, unsigned int line ) - { - list_type * new_array; - - if ( allocation_size == 0 ) - return ; - - new_array = RakNet::OP_NEW_ARRAY( allocation_size , file, line ); - - // copy old array over - for ( unsigned int counter = 0; counter < list_size; ++counter ) - new_array[ counter ] = listArray[ counter ]; - - // Don't call constructors, assignment operators, etc. - //memcpy(new_array, listArray, list_size*sizeof(list_type)); - - // set old array to point to the newly allocated array - RakNet::OP_DELETE_ARRAY(listArray, file, line); - - listArray = new_array; - } - - template - void List::Preallocate( unsigned countNeeded, const char *file, unsigned int line ) - { - unsigned amountToAllocate = allocation_size; - if (allocation_size==0) - amountToAllocate=16; - while (amountToAllocate < countNeeded) - amountToAllocate<<=1; - - if ( allocation_size < amountToAllocate) - { - // allocate twice the currently allocated memory - list_type * new_array; - - allocation_size=amountToAllocate; - - new_array = RakNet::OP_NEW_ARRAY< list_type >( allocation_size , file, line ); - - if (listArray) - { - // copy old array over - for ( unsigned int counter = 0; counter < list_size; ++counter ) - new_array[ counter ] = listArray[ counter ]; - - // Don't call constructors, assignment operators, etc. - //memcpy(new_array, listArray, list_size*sizeof(list_type)); - - // set old array to point to the newly allocated and twice as large array - RakNet::OP_DELETE_ARRAY(listArray, file, line); - } - - listArray = new_array; - } - } - -} // End namespace - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_List.h +/// \internal +/// \brief Array based list. +/// \details Usually the Queue class is used instead, since it has all the same functionality and is only worse at random access. +/// + + +#pragma once + +#include "RakAssert.h" +#include // memmove +#include "Export.h" +#include "RakMemoryOverride.h" + +/// Maximum unsigned long +static const unsigned int MAX_UNSIGNED_LONG = 4294967295U; + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + /// \brief Array based implementation of a list. + /// \note ONLY USE THIS FOR SHALLOW COPIES. I don't bother with operator= to improve performance. + template + class RAK_DLL_EXPORT List + { + public: + /// Default constructor + List(); + + // Destructor + ~List(); + + /// \brief Copy constructor. + /// \param[in] original_copy The list to duplicate + List( const List& original_copy ); + + /// \brief Assign one list to another. + List& operator= ( const List& original_copy ); + + /// \brief Access an element by its index in the array. + /// \param[in] position The index into the array. + /// \return The element at position \a position. + list_type& operator[] ( const unsigned int position ) const; + + /// \brief Access an element by its index in the array. + /// \param[in] position The index into the array. + /// \return The element at position \a position. + list_type& Get ( const unsigned int position ) const; + + /// \brief Push an element at the end of the stack. + /// \param[in] input The new element. + void Push(const list_type &input, const char *file, unsigned int line ); + + /// \brief Pop an element from the end of the stack. + /// \pre Size()>0 + /// \return The element at the end. + list_type& Pop(void); + + /// \brief Insert an element at position \a position in the list. + /// \param[in] input The new element. + /// \param[in] position The position of the new element. + void Insert( const list_type &input, const unsigned int position, const char *file, unsigned int line ); + + /// \brief Insert at the end of the list. + /// \param[in] input The new element. + void Insert( const list_type &input, const char *file, unsigned int line ); + + /// \brief Replace the value at \a position by \a input. + /// \details If the size of the list is less than @em position, it increase the capacity of + /// the list and fill slot with @em filler. + /// \param[in] input The element to replace at position @em position. + /// \param[in] filler The element use to fill new allocated capacity. + /// \param[in] position The position of input in the list. + void Replace( const list_type &input, const list_type filler, const unsigned int position, const char *file, unsigned int line ); + + /// \brief Replace the last element of the list by \a input. + /// \param[in] input The element used to replace the last element. + void Replace( const list_type &input ); + + /// \brief Delete the element at position \a position. + /// \param[in] position The index of the element to delete + void RemoveAtIndex( const unsigned int position ); + + /// \brief Delete the element at position \a position. + /// \note - swaps middle with end of list, only use if list order does not matter + /// \param[in] position The index of the element to delete + void RemoveAtIndexFast( const unsigned int position ); + + /// \brief Delete the element at the end of the list. + void RemoveFromEnd(const unsigned num=1); + + /// \brief Returns the index of the specified item or MAX_UNSIGNED_LONG if not found. + /// \param[in] input The element to check for + /// \return The index or position of @em input in the list. + /// \retval MAX_UNSIGNED_LONG The object is not in the list + /// \retval [Integer] The index of the element in the list + unsigned int GetIndexOf( const list_type &input ) const; + + /// \return The number of elements in the list + unsigned int Size( void ) const; + + /// \brief Clear the list + void Clear( bool doNotDeallocateSmallBlocks, const char *file, unsigned int line ); + + /// \brief Preallocate the list, so it needs fewer reallocations at runtime. + void Preallocate( unsigned countNeeded, const char *file, unsigned int line ); + + /// \brief Frees overallocated members, to use the minimum memory necessary. + /// \attention + /// This is a slow operation + void Compress( const char *file, unsigned int line ); + + private: + /// An array of user values + list_type* listArray; + + /// Number of elements in the list + unsigned int list_size; + + /// Size of \a array + unsigned int allocation_size; + }; + template + List::List() + { + allocation_size = 0; + listArray = 0; + list_size = 0; + } + + template + List::~List() + { + if (allocation_size>0) + RakNet::OP_DELETE_ARRAY(listArray, _FILE_AND_LINE_); + } + + + template + List::List( const List& original_copy ) + { + // Allocate memory for copy + + if ( original_copy.list_size == 0 ) + { + list_size = 0; + allocation_size = 0; + } + else + { + listArray = RakNet::OP_NEW_ARRAY( original_copy.list_size , _FILE_AND_LINE_ ); + + for ( unsigned int counter = 0; counter < original_copy.list_size; ++counter ) + listArray[ counter ] = original_copy.listArray[ counter ]; + + // Don't call constructors, assignment operators, etc. + //memcpy(listArray, original_copy.listArray, original_copy.list_size*sizeof(list_type)); + + list_size = allocation_size = original_copy.list_size; + } + } + + template + List& List::operator= ( const List& original_copy ) + { + if ( ( &original_copy ) != this ) + { + Clear( false, _FILE_AND_LINE_ ); + + // Allocate memory for copy + + if ( original_copy.list_size == 0 ) + { + list_size = 0; + allocation_size = 0; + } + + else + { + listArray = RakNet::OP_NEW_ARRAY( original_copy.list_size , _FILE_AND_LINE_ ); + + for ( unsigned int counter = 0; counter < original_copy.list_size; ++counter ) + listArray[ counter ] = original_copy.listArray[ counter ]; + // Don't call constructors, assignment operators, etc. + //memcpy(listArray, original_copy.listArray, original_copy.list_size*sizeof(list_type)); + + list_size = allocation_size = original_copy.list_size; + } + } + + return *this; + } + + + template + inline list_type& List::operator[] ( const unsigned int position ) const + { + #ifdef _DEBUG + if (position>=list_size) + { + RakAssert ( position < list_size ); + } + #endif + return listArray[ position ]; + } + + // Just here for debugging + template + inline list_type& List::Get ( const unsigned int position ) const + { + return listArray[ position ]; + } + + template + void List::Push(const list_type &input, const char *file, unsigned int line) + { + Insert(input, file, line); + } + + template + inline list_type& List::Pop(void) + { +#ifdef _DEBUG + RakAssert(list_size>0); +#endif + --list_size; + return listArray[list_size]; + } + + template + void List::Insert( const list_type &input, const unsigned int position, const char *file, unsigned int line ) + { +#ifdef _DEBUG + if (position>list_size) + { + RakAssert( position <= list_size ); + } +#endif + + // Reallocate list if necessary + if ( list_size == allocation_size ) + { + // allocate twice the currently allocated memory + list_type * new_array; + + if ( allocation_size == 0 ) + allocation_size = 16; + else + allocation_size *= 2; + + new_array = RakNet::OP_NEW_ARRAY( allocation_size , file, line ); + + // copy old array over + for ( unsigned int counter = 0; counter < list_size; ++counter ) + new_array[ counter ] = listArray[ counter ]; + + // Don't call constructors, assignment operators, etc. + //memcpy(new_array, listArray, list_size*sizeof(list_type)); + + // set old array to point to the newly allocated and twice as large array + RakNet::OP_DELETE_ARRAY(listArray, file, line); + + listArray = new_array; + } + + // Move the elements in the list to make room + // Use > instead of != so that an out-of-range position (possible in + // release builds where the assert above is compiled out) does not + // cause an infinite loop due to unsigned integer wraparound. + for ( unsigned int counter = list_size; counter > position; counter-- ) + listArray[ counter ] = listArray[ counter - 1 ]; + + // Don't call constructors, assignment operators, etc. + //memmove(listArray+position+1, listArray+position, (list_size-position)*sizeof(list_type)); + + // Insert the new item at the correct spot + listArray[ position ] = input; + + ++list_size; + + } + + + template + void List::Insert( const list_type &input, const char *file, unsigned int line ) + { + // Reallocate list if necessary + + if ( list_size == allocation_size ) + { + // allocate twice the currently allocated memory + list_type * new_array; + + if ( allocation_size == 0 ) + allocation_size = 16; + else + allocation_size *= 2; + + new_array = RakNet::OP_NEW_ARRAY( allocation_size , file, line ); + + if (listArray) + { + // copy old array over + for ( unsigned int counter = 0; counter < list_size; ++counter ) + new_array[ counter ] = listArray[ counter ]; + + // Don't call constructors, assignment operators, etc. + //memcpy(new_array, listArray, list_size*sizeof(list_type)); + + // set old array to point to the newly allocated and twice as large array + RakNet::OP_DELETE_ARRAY(listArray, file, line); + } + + listArray = new_array; + } + + // Insert the new item at the correct spot + listArray[ list_size ] = input; + + ++list_size; + } + + template + inline void List::Replace( const list_type &input, const list_type filler, const unsigned int position, const char *file, unsigned int line ) + { + if ( ( list_size > 0 ) && ( position < list_size ) ) + { + // Direct replacement + listArray[ position ] = input; + } + else + { + if ( position >= allocation_size ) + { + // Reallocate the list to size position and fill in blanks with filler + list_type * new_array; + allocation_size = position + 1; + + new_array = RakNet::OP_NEW_ARRAY( allocation_size , file, line ); + + // copy old array over + + for ( unsigned int counter = 0; counter < list_size; ++counter ) + new_array[ counter ] = listArray[ counter ]; + + // Don't call constructors, assignment operators, etc. + //memcpy(new_array, listArray, list_size*sizeof(list_type)); + + // set old array to point to the newly allocated array + RakNet::OP_DELETE_ARRAY(listArray, file, line); + + listArray = new_array; + } + + // Fill in holes with filler + while ( list_size < position ) + listArray[ list_size++ ] = filler; + + // Fill in the last element with the new item + listArray[ list_size++ ] = input; + +#ifdef _DEBUG + + RakAssert( list_size == position + 1 ); + +#endif + + } + } + + template + inline void List::Replace( const list_type &input ) + { + if ( list_size > 0 ) + listArray[ list_size - 1 ] = input; + } + + template + void List::RemoveAtIndex( const unsigned int position ) + { +#ifdef _DEBUG + if (position >= list_size) + { + RakAssert( position < list_size ); + return; + } +#endif + + if ( position < list_size ) + { + // Compress the array + for ( unsigned int counter = position; counter < list_size - 1 ; ++counter ) + listArray[ counter ] = listArray[ counter + 1 ]; + // Don't call constructors, assignment operators, etc. + // memmove(listArray+position, listArray+position+1, (list_size-1-position) * sizeof(list_type)); + + RemoveFromEnd(); + } + } + + template + void List::RemoveAtIndexFast( const unsigned int position ) + { +#ifdef _DEBUG + if (position >= list_size) + { + RakAssert( position < list_size ); + return; + } +#endif + --list_size; + listArray[position]=listArray[list_size]; + } + + template + inline void List::RemoveFromEnd( const unsigned num ) + { + // Delete the last elements on the list. No compression needed +#ifdef _DEBUG + RakAssert(list_size>=num); +#endif + list_size-=num; + } + + template + unsigned int List::GetIndexOf( const list_type &input ) const + { + for ( unsigned int i = 0; i < list_size; ++i ) + if ( listArray[ i ] == input ) + return i; + + return MAX_UNSIGNED_LONG; + } + + template + inline unsigned int List::Size( void ) const + { + return list_size; + } + + template + void List::Clear( bool doNotDeallocateSmallBlocks, const char *file, unsigned int line ) + { + if ( allocation_size == 0 ) + return; + + if (allocation_size>512 || doNotDeallocateSmallBlocks==false) + { + RakNet::OP_DELETE_ARRAY(listArray, file, line); + allocation_size = 0; + listArray = 0; + } + list_size = 0; + } + + template + void List::Compress( const char *file, unsigned int line ) + { + list_type * new_array; + + if ( allocation_size == 0 ) + return ; + + new_array = RakNet::OP_NEW_ARRAY( allocation_size , file, line ); + + // copy old array over + for ( unsigned int counter = 0; counter < list_size; ++counter ) + new_array[ counter ] = listArray[ counter ]; + + // Don't call constructors, assignment operators, etc. + //memcpy(new_array, listArray, list_size*sizeof(list_type)); + + // set old array to point to the newly allocated array + RakNet::OP_DELETE_ARRAY(listArray, file, line); + + listArray = new_array; + } + + template + void List::Preallocate( unsigned countNeeded, const char *file, unsigned int line ) + { + unsigned amountToAllocate = allocation_size; + if (allocation_size==0) + amountToAllocate=16; + while (amountToAllocate < countNeeded) + amountToAllocate<<=1; + + if ( allocation_size < amountToAllocate) + { + // allocate twice the currently allocated memory + list_type * new_array; + + allocation_size=amountToAllocate; + + new_array = RakNet::OP_NEW_ARRAY< list_type >( allocation_size , file, line ); + + if (listArray) + { + // copy old array over + for ( unsigned int counter = 0; counter < list_size; ++counter ) + new_array[ counter ] = listArray[ counter ]; + + // Don't call constructors, assignment operators, etc. + //memcpy(new_array, listArray, list_size*sizeof(list_type)); + + // set old array to point to the newly allocated and twice as large array + RakNet::OP_DELETE_ARRAY(listArray, file, line); + } + + listArray = new_array; + } + } + +} // End namespace + diff --git a/Source/DS_Map.h b/Source/DS_Map.h index e77c569cd..15b91ae7b 100644 --- a/Source/DS_Map.h +++ b/Source/DS_Map.h @@ -1,328 +1,326 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_Map.h -/// \internal -/// \brief Map -/// - - -#ifndef __RAKNET_MAP_H -#define __RAKNET_MAP_H - -#include "DS_OrderedList.h" -#include "Export.h" -#include "RakMemoryOverride.h" -#include "RakAssert.h" - -// If I want to change this to a red-black tree, this is a good site: http://www.cs.auckland.ac.nz/software/AlgAnim/red_black.html -// This makes insertions and deletions faster. But then traversals are slow, while they are currently fast. - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - /// The default comparison has to be first so it can be called as a default parameter. - /// It then is followed by MapNode, followed by NodeComparisonFunc - template - int defaultMapKeyComparison(const key_type &a, const key_type &b) - { - if (a > - class RAK_DLL_EXPORT Map - { - public: - static void IMPLEMENT_DEFAULT_COMPARISON(void) {DataStructures::defaultMapKeyComparison(key_type(),key_type());} - - struct MapNode - { - MapNode() {} - MapNode(key_type _key, data_type _data) : mapNodeKey(_key), mapNodeData(_data) {} - MapNode& operator = ( const MapNode& input ) {mapNodeKey=input.mapNodeKey; mapNodeData=input.mapNodeData; return *this;} - MapNode( const MapNode & input) {mapNodeKey=input.mapNodeKey; mapNodeData=input.mapNodeData;} - key_type mapNodeKey; - data_type mapNodeData; - }; - - // Has to be a static because the comparison callback for DataStructures::OrderedList is a C function - static int NodeComparisonFunc(const key_type &a, const MapNode &b) - { -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - return key_comparison_func(a, b.mapNodeKey); - } - - Map(); - ~Map(); - Map( const Map& original_copy ); - Map& operator= ( const Map& original_copy ); - - data_type& Get(const key_type &key) const; - data_type Pop(const key_type &key); - // Add if needed - void Set(const key_type &key, const data_type &data); - // Must already exist - void SetExisting(const key_type &key, const data_type &data); - // Must add - void SetNew(const key_type &key, const data_type &data); - bool Has(const key_type &key) const; - bool Delete(const key_type &key); - data_type& operator[] ( const unsigned int position ) const; - key_type GetKeyAtIndex( const unsigned int position ) const; - unsigned GetIndexAtKey( const key_type &key ); - void RemoveAtIndex(const unsigned index); - void Clear(void); - unsigned Size(void) const; - - protected: - DataStructures::OrderedList< key_type,MapNode,&Map::NodeComparisonFunc > mapNodeList; - - void SaveLastSearch(const key_type &key, unsigned index) const; - bool HasSavedSearchResult(const key_type &key) const; - - unsigned lastSearchIndex; - key_type lastSearchKey; - bool lastSearchIndexValid; - }; - - template - Map::Map() - { - lastSearchIndexValid=false; - } - - template - Map::~Map() - { - Clear(); - } - - template - Map::Map( const Map& original_copy ) - { - mapNodeList=original_copy.mapNodeList; - lastSearchIndex=original_copy.lastSearchIndex; - lastSearchKey=original_copy.lastSearchKey; - lastSearchIndexValid=original_copy.lastSearchIndexValid; - } - - template - Map& Map::operator= ( const Map& original_copy ) - { - mapNodeList=original_copy.mapNodeList; - lastSearchIndex=original_copy.lastSearchIndex; - lastSearchKey=original_copy.lastSearchKey; - lastSearchIndexValid=original_copy.lastSearchIndexValid; - return *this; - } - - template - data_type& Map::Get(const key_type &key) const - { - if (HasSavedSearchResult(key)) - return mapNodeList[lastSearchIndex].mapNodeData; - - bool objectExists; - unsigned index; - index=mapNodeList.GetIndexFromKey(key, &objectExists); - RakAssert(objectExists); - SaveLastSearch(key,index); - return mapNodeList[index].mapNodeData; - } - - template - unsigned Map::GetIndexAtKey( const key_type &key ) - { - if (HasSavedSearchResult(key)) - return lastSearchIndex; - - bool objectExists; - unsigned index; - index=mapNodeList.GetIndexFromKey(key, &objectExists); - if (objectExists==false) - { - RakAssert(objectExists); - } - SaveLastSearch(key,index); - return index; - } - - template - void Map::RemoveAtIndex(const unsigned index) - { - mapNodeList.RemoveAtIndex(index); - lastSearchIndexValid=false; - } - - template - data_type Map::Pop(const key_type &key) - { - bool objectExists; - unsigned index; - if (HasSavedSearchResult(key)) - index=lastSearchIndex; - else - { - index=mapNodeList.GetIndexFromKey(key, &objectExists); - RakAssert(objectExists); - } - data_type tmp = mapNodeList[index].mapNodeData; - mapNodeList.RemoveAtIndex(index); - lastSearchIndexValid=false; - return tmp; - } - - template - void Map::Set(const key_type &key, const data_type &data) - { - bool objectExists; - unsigned index; - - if (HasSavedSearchResult(key)) - { - mapNodeList[lastSearchIndex].mapNodeData=data; - return; - } - - index=mapNodeList.GetIndexFromKey(key, &objectExists); - - if (objectExists) - { - SaveLastSearch(key,index); - mapNodeList[index].mapNodeData=data; - } - else - { - SaveLastSearch(key,mapNodeList.Insert(key,MapNode(key,data), true, _FILE_AND_LINE_)); - } - } - - template - void Map::SetExisting(const key_type &key, const data_type &data) - { - bool objectExists; - unsigned index; - - if (HasSavedSearchResult(key)) - { - index=lastSearchIndex; - } - else - { - index=mapNodeList.GetIndexFromKey(key, &objectExists); - RakAssert(objectExists); - SaveLastSearch(key,index); - } - - mapNodeList[index].mapNodeData=data; - } - - template - void Map::SetNew(const key_type &key, const data_type &data) - { -#ifdef _DEBUG - bool objectExists; - mapNodeList.GetIndexFromKey(key, &objectExists); - RakAssert(objectExists==false); -#endif - SaveLastSearch(key,mapNodeList.Insert(key,MapNode(key,data), true, _FILE_AND_LINE_)); - } - - template - bool Map::Has(const key_type &key) const - { - if (HasSavedSearchResult(key)) - return true; - - bool objectExists; - unsigned index; - index=mapNodeList.GetIndexFromKey(key, &objectExists); - if (objectExists) - SaveLastSearch(key,index); - return objectExists; - } - - template - bool Map::Delete(const key_type &key) - { - if (HasSavedSearchResult(key)) - { - lastSearchIndexValid=false; - mapNodeList.RemoveAtIndex(lastSearchIndex); - return true; - } - - bool objectExists; - unsigned index; - index=mapNodeList.GetIndexFromKey(key, &objectExists); - if (objectExists) - { - lastSearchIndexValid=false; - mapNodeList.RemoveAtIndex(index); - return true; - } - else - return false; - } - - template - void Map::Clear(void) - { - lastSearchIndexValid=false; - mapNodeList.Clear(false, _FILE_AND_LINE_); - } - - template - data_type& Map::operator[]( const unsigned int position ) const - { - return mapNodeList[position].mapNodeData; - } - - template - key_type Map::GetKeyAtIndex( const unsigned int position ) const - { - return mapNodeList[position].mapNodeKey; - } - - template - unsigned Map::Size(void) const - { - return mapNodeList.Size(); - } - - template - void Map::SaveLastSearch(const key_type &key, const unsigned index) const - { - (void) key; - (void) index; - - /* - lastSearchIndex=index; - lastSearchKey=key; - lastSearchIndexValid=true; - */ - } - - template - bool Map::HasSavedSearchResult(const key_type &key) const - { - (void) key; - - // Not threadsafe! - return false; - // return lastSearchIndexValid && key_comparison_func(key,lastSearchKey)==0; - } -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_Map.h +/// \internal +/// \brief Map +/// + + +#pragma once + +#include "DS_OrderedList.h" +#include "Export.h" +#include "RakMemoryOverride.h" +#include "RakAssert.h" + +// If I want to change this to a red-black tree, this is a good site: http://www.cs.auckland.ac.nz/software/AlgAnim/red_black.html +// This makes insertions and deletions faster. But then traversals are slow, while they are currently fast. + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + /// The default comparison has to be first so it can be called as a default parameter. + /// It then is followed by MapNode, followed by NodeComparisonFunc + template + int defaultMapKeyComparison(const key_type &a, const key_type &b) + { + if (a > + class RAK_DLL_EXPORT Map + { + public: + static void IMPLEMENT_DEFAULT_COMPARISON(void) {DataStructures::defaultMapKeyComparison(key_type(),key_type());} + + struct MapNode + { + MapNode() {} + MapNode(key_type _key, data_type _data) : mapNodeKey(_key), mapNodeData(_data) {} + MapNode& operator = ( const MapNode& input ) {mapNodeKey=input.mapNodeKey; mapNodeData=input.mapNodeData; return *this;} + MapNode( const MapNode & input) {mapNodeKey=input.mapNodeKey; mapNodeData=input.mapNodeData;} + key_type mapNodeKey; + data_type mapNodeData; + }; + + // Has to be a static because the comparison callback for DataStructures::OrderedList is a C function + static int NodeComparisonFunc(const key_type &a, const MapNode &b) + { +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + return key_comparison_func(a, b.mapNodeKey); + } + + Map(); + ~Map(); + Map( const Map& original_copy ); + Map& operator= ( const Map& original_copy ); + + data_type& Get(const key_type &key) const; + data_type Pop(const key_type &key); + // Add if needed + void Set(const key_type &key, const data_type &data); + // Must already exist + void SetExisting(const key_type &key, const data_type &data); + // Must add + void SetNew(const key_type &key, const data_type &data); + bool Has(const key_type &key) const; + bool Delete(const key_type &key); + data_type& operator[] ( const unsigned int position ) const; + key_type GetKeyAtIndex( const unsigned int position ) const; + unsigned GetIndexAtKey( const key_type &key ); + void RemoveAtIndex(const unsigned index); + void Clear(void); + unsigned Size(void) const; + + protected: + DataStructures::OrderedList< key_type,MapNode,&Map::NodeComparisonFunc > mapNodeList; + + void SaveLastSearch(const key_type &key, unsigned index) const; + bool HasSavedSearchResult(const key_type &key) const; + + unsigned lastSearchIndex; + key_type lastSearchKey; + bool lastSearchIndexValid; + }; + + template + Map::Map() + { + lastSearchIndexValid=false; + } + + template + Map::~Map() + { + Clear(); + } + + template + Map::Map( const Map& original_copy ) + { + mapNodeList=original_copy.mapNodeList; + lastSearchIndex=original_copy.lastSearchIndex; + lastSearchKey=original_copy.lastSearchKey; + lastSearchIndexValid=original_copy.lastSearchIndexValid; + } + + template + Map& Map::operator= ( const Map& original_copy ) + { + mapNodeList=original_copy.mapNodeList; + lastSearchIndex=original_copy.lastSearchIndex; + lastSearchKey=original_copy.lastSearchKey; + lastSearchIndexValid=original_copy.lastSearchIndexValid; + return *this; + } + + template + data_type& Map::Get(const key_type &key) const + { + if (HasSavedSearchResult(key)) + return mapNodeList[lastSearchIndex].mapNodeData; + + bool objectExists; + unsigned index; + index=mapNodeList.GetIndexFromKey(key, &objectExists); + RakAssert(objectExists); + SaveLastSearch(key,index); + return mapNodeList[index].mapNodeData; + } + + template + unsigned Map::GetIndexAtKey( const key_type &key ) + { + if (HasSavedSearchResult(key)) + return lastSearchIndex; + + bool objectExists; + unsigned index; + index=mapNodeList.GetIndexFromKey(key, &objectExists); + if (objectExists==false) + { + RakAssert(objectExists); + } + SaveLastSearch(key,index); + return index; + } + + template + void Map::RemoveAtIndex(const unsigned index) + { + mapNodeList.RemoveAtIndex(index); + lastSearchIndexValid=false; + } + + template + data_type Map::Pop(const key_type &key) + { + bool objectExists; + unsigned index; + if (HasSavedSearchResult(key)) + index=lastSearchIndex; + else + { + index=mapNodeList.GetIndexFromKey(key, &objectExists); + RakAssert(objectExists); + } + data_type tmp = mapNodeList[index].mapNodeData; + mapNodeList.RemoveAtIndex(index); + lastSearchIndexValid=false; + return tmp; + } + + template + void Map::Set(const key_type &key, const data_type &data) + { + bool objectExists; + unsigned index; + + if (HasSavedSearchResult(key)) + { + mapNodeList[lastSearchIndex].mapNodeData=data; + return; + } + + index=mapNodeList.GetIndexFromKey(key, &objectExists); + + if (objectExists) + { + SaveLastSearch(key,index); + mapNodeList[index].mapNodeData=data; + } + else + { + SaveLastSearch(key,mapNodeList.Insert(key,MapNode(key,data), true, _FILE_AND_LINE_)); + } + } + + template + void Map::SetExisting(const key_type &key, const data_type &data) + { + bool objectExists; + unsigned index; + + if (HasSavedSearchResult(key)) + { + index=lastSearchIndex; + } + else + { + index=mapNodeList.GetIndexFromKey(key, &objectExists); + RakAssert(objectExists); + SaveLastSearch(key,index); + } + + mapNodeList[index].mapNodeData=data; + } + + template + void Map::SetNew(const key_type &key, const data_type &data) + { +#ifdef _DEBUG + bool objectExists; + mapNodeList.GetIndexFromKey(key, &objectExists); + RakAssert(objectExists==false); +#endif + SaveLastSearch(key,mapNodeList.Insert(key,MapNode(key,data), true, _FILE_AND_LINE_)); + } + + template + bool Map::Has(const key_type &key) const + { + if (HasSavedSearchResult(key)) + return true; + + bool objectExists; + unsigned index; + index=mapNodeList.GetIndexFromKey(key, &objectExists); + if (objectExists) + SaveLastSearch(key,index); + return objectExists; + } + + template + bool Map::Delete(const key_type &key) + { + if (HasSavedSearchResult(key)) + { + lastSearchIndexValid=false; + mapNodeList.RemoveAtIndex(lastSearchIndex); + return true; + } + + bool objectExists; + unsigned index; + index=mapNodeList.GetIndexFromKey(key, &objectExists); + if (objectExists) + { + lastSearchIndexValid=false; + mapNodeList.RemoveAtIndex(index); + return true; + } + else + return false; + } + + template + void Map::Clear(void) + { + lastSearchIndexValid=false; + mapNodeList.Clear(false, _FILE_AND_LINE_); + } + + template + data_type& Map::operator[]( const unsigned int position ) const + { + return mapNodeList[position].mapNodeData; + } + + template + key_type Map::GetKeyAtIndex( const unsigned int position ) const + { + return mapNodeList[position].mapNodeKey; + } + + template + unsigned Map::Size(void) const + { + return mapNodeList.Size(); + } + + template + void Map::SaveLastSearch(const key_type &key, const unsigned index) const + { + (void) key; + (void) index; + + /* + lastSearchIndex=index; + lastSearchKey=key; + lastSearchIndexValid=true; + */ + } + + template + bool Map::HasSavedSearchResult(const key_type &key) const + { + (void) key; + + // Not threadsafe! + return false; + // return lastSearchIndexValid && key_comparison_func(key,lastSearchKey)==0; + } +} + diff --git a/Source/DS_MemoryPool.h b/Source/DS_MemoryPool.h index c4a947ede..da92e3d69 100644 --- a/Source/DS_MemoryPool.h +++ b/Source/DS_MemoryPool.h @@ -1,356 +1,354 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_MemoryPool.h -/// - - -#ifndef __MEMORY_POOL_H -#define __MEMORY_POOL_H - -#ifndef __APPLE__ -// Use stdlib and not malloc for compatibility -#include -#endif -#include "RakAssert.h" -#include "Export.h" - -#include "RakMemoryOverride.h" - -// DS_MEMORY_POOL_MAX_FREE_PAGES must be > 1 -#define DS_MEMORY_POOL_MAX_FREE_PAGES 4 - -//#define _DISABLE_MEMORY_POOL - -namespace DataStructures -{ - /// Very fast memory pool for allocating and deallocating structures that don't have constructors or destructors. - /// Contains a list of pages, each of which has an array of the user structures - template - class RAK_DLL_EXPORT MemoryPool - { - public: - struct Page; - struct MemoryWithPage - { - MemoryBlockType userMemory; - Page *parentPage; - }; - struct Page - { - MemoryWithPage** availableStack; - int availableStackSize; - MemoryWithPage* block; - Page *next, *prev; - }; - - MemoryPool(); - ~MemoryPool(); - void SetPageSize(int size); // Defaults to 16384 bytes - MemoryBlockType *Allocate(const char *file, unsigned int line); - void Release(MemoryBlockType *m, const char *file, unsigned int line); - void Clear(const char *file, unsigned int line); - - int GetAvailablePagesSize(void) const {return availablePagesSize;} - int GetUnavailablePagesSize(void) const {return unavailablePagesSize;} - int GetMemoryPoolPageSize(void) const {return memoryPoolPageSize;} - protected: - int BlocksPerPage(void) const; - void AllocateFirst(void); - bool InitPage(Page *page, Page *prev, const char *file, unsigned int line); - - // availablePages contains pages which have room to give the user new blocks. We return these blocks from the head of the list - // unavailablePages are pages which are totally full, and from which we do not return new blocks. - // Pages move from the head of unavailablePages to the tail of availablePages, and from the head of availablePages to the tail of unavailablePages - Page *availablePages, *unavailablePages; - int availablePagesSize, unavailablePagesSize; - int memoryPoolPageSize; - }; - - template - MemoryPool::MemoryPool() - { -#ifndef _DISABLE_MEMORY_POOL - //AllocateFirst(); - availablePagesSize=0; - unavailablePagesSize=0; - memoryPoolPageSize=16384; -#endif - } - template - MemoryPool::~MemoryPool() - { -#ifndef _DISABLE_MEMORY_POOL - Clear(_FILE_AND_LINE_); -#endif - } - - template - void MemoryPool::SetPageSize(int size) - { - memoryPoolPageSize=size; - } - - template - MemoryBlockType* MemoryPool::Allocate(const char *file, unsigned int line) - { -#ifdef _DISABLE_MEMORY_POOL - return (MemoryBlockType*) rakMalloc_Ex(sizeof(MemoryBlockType), file, line); -#else - - if (availablePagesSize>0) - { - MemoryBlockType *retVal; - Page *curPage; - curPage=availablePages; - retVal = (MemoryBlockType*) curPage->availableStack[--(curPage->availableStackSize)]; - if (curPage->availableStackSize==0) - { - --availablePagesSize; - availablePages=curPage->next; - RakAssert(availablePagesSize==0 || availablePages->availableStackSize>0); - curPage->next->prev=curPage->prev; - curPage->prev->next=curPage->next; - - if (unavailablePagesSize++==0) - { - unavailablePages=curPage; - curPage->next=curPage; - curPage->prev=curPage; - } - else - { - curPage->next=unavailablePages; - curPage->prev=unavailablePages->prev; - unavailablePages->prev->next=curPage; - unavailablePages->prev=curPage; - } - } - - RakAssert(availablePagesSize==0 || availablePages->availableStackSize>0); - return retVal; - } - - availablePages = (Page *) rakMalloc_Ex(sizeof(Page), file, line); - if (availablePages==0) - return 0; - availablePagesSize=1; - if (InitPage(availablePages, availablePages, file, line)==false) - return 0; - // If this assert hits, we couldn't allocate even 1 block per page. Increase the page size - RakAssert(availablePages->availableStackSize>1); - - return (MemoryBlockType *) availablePages->availableStack[--availablePages->availableStackSize]; -#endif - } - template - void MemoryPool::Release(MemoryBlockType *m, const char *file, unsigned int line) - { -#ifdef _DISABLE_MEMORY_POOL - rakFree_Ex(m, file, line); - return; -#else - // Find the page this block is in and return it. - Page *curPage; - MemoryWithPage *memoryWithPage = (MemoryWithPage*)m; - curPage=memoryWithPage->parentPage; - - if (curPage->availableStackSize==0) - { - // The page is in the unavailable list so move it to the available list - curPage->availableStack[curPage->availableStackSize++]=memoryWithPage; - unavailablePagesSize--; - - // As this page is no longer totally empty, move it to the end of available pages - curPage->next->prev=curPage->prev; - curPage->prev->next=curPage->next; - - if (unavailablePagesSize>0 && curPage==unavailablePages) - unavailablePages=unavailablePages->next; - - if (availablePagesSize++==0) - { - availablePages=curPage; - curPage->next=curPage; - curPage->prev=curPage; - } - else - { - curPage->next=availablePages; - curPage->prev=availablePages->prev; - availablePages->prev->next=curPage; - availablePages->prev=curPage; - } - } - else - { - curPage->availableStack[curPage->availableStackSize++]=memoryWithPage; - - if (curPage->availableStackSize==BlocksPerPage() && - availablePagesSize>=DS_MEMORY_POOL_MAX_FREE_PAGES) - { - // After a certain point, just deallocate empty pages rather than keep them around - if (curPage==availablePages) - { - availablePages=curPage->next; - RakAssert(availablePages->availableStackSize>0); - } - curPage->prev->next=curPage->next; - curPage->next->prev=curPage->prev; - availablePagesSize--; - rakFree_Ex(curPage->availableStack, file, line ); - rakFree_Ex(curPage->block, file, line ); - rakFree_Ex(curPage, file, line ); - } - } -#endif - } - template - void MemoryPool::Clear(const char *file, unsigned int line) - { -#ifdef _DISABLE_MEMORY_POOL - return; -#else - Page *cur, *freed; - - if (availablePagesSize>0) - { - cur = availablePages; -#ifdef _MSC_VER -#pragma warning(disable:4127) // conditional expression is constant -#endif - while (true) - // do - { - rakFree_Ex(cur->availableStack, file, line ); - rakFree_Ex(cur->block, file, line ); - freed=cur; - cur=cur->next; - if (cur==availablePages) - { - rakFree_Ex(freed, file, line ); - break; - } - rakFree_Ex(freed, file, line ); - }// while(cur!=availablePages); - } - - if (unavailablePagesSize>0) - { - cur = unavailablePages; - while (1) - //do - { - rakFree_Ex(cur->availableStack, file, line ); - rakFree_Ex(cur->block, file, line ); - freed=cur; - cur=cur->next; - if (cur==unavailablePages) - { - rakFree_Ex(freed, file, line ); - break; - } - rakFree_Ex(freed, file, line ); - } // while(cur!=unavailablePages); - } - - availablePagesSize=0; - unavailablePagesSize=0; -#endif - } - template - int MemoryPool::BlocksPerPage(void) const - { - return memoryPoolPageSize / sizeof(MemoryWithPage); - } - template - bool MemoryPool::InitPage(Page *page, Page *prev, const char *file, unsigned int line) - { - int i=0; - const int bpp = BlocksPerPage(); - page->block=(MemoryWithPage*) rakMalloc_Ex(memoryPoolPageSize, file, line); - if (page->block==0) - return false; - page->availableStack=(MemoryWithPage**)rakMalloc_Ex(sizeof(MemoryWithPage*)*bpp, file, line); - if (page->availableStack==0) - { - rakFree_Ex(page->block, file, line ); - return false; - } - MemoryWithPage *curBlock = page->block; - MemoryWithPage **curStack = page->availableStack; - while (i < bpp) - { - curBlock->parentPage=page; - curStack[i]=curBlock++; - i++; - } - page->availableStackSize=bpp; - page->next=availablePages; - page->prev=prev; - return true; - } -} - -#endif - -/* -#include "DS_MemoryPool.h" -#include "DS_List.h" - -struct TestMemoryPool -{ - int allocationId; -}; - -int main(void) -{ - DataStructures::MemoryPool memoryPool; - DataStructures::List returnList; - - for (int i=0; i < 100000; i++) - returnList.Push(memoryPool.Allocate(_FILE_AND_LINE_), _FILE_AND_LINE_); - for (int i=0; i < returnList.Size(); i+=2) - { - memoryPool.Release(returnList[i], _FILE_AND_LINE_); - returnList.RemoveAtIndexFast(i); - } - for (int i=0; i < 100000; i++) - returnList.Push(memoryPool.Allocate(_FILE_AND_LINE_), _FILE_AND_LINE_); - while (returnList.Size()) - { - memoryPool.Release(returnList[returnList.Size()-1], _FILE_AND_LINE_); - returnList.RemoveAtIndex(returnList.Size()-1); - } - for (int i=0; i < 100000; i++) - returnList.Push(memoryPool.Allocate(_FILE_AND_LINE_), _FILE_AND_LINE_); - while (returnList.Size()) - { - memoryPool.Release(returnList[returnList.Size()-1], _FILE_AND_LINE_); - returnList.RemoveAtIndex(returnList.Size()-1); - } - for (int i=0; i < 100000; i++) - returnList.Push(memoryPool.Allocate(_FILE_AND_LINE_), _FILE_AND_LINE_); - for (int i=100000-1; i <= 0; i-=2) - { - memoryPool.Release(returnList[i], _FILE_AND_LINE_); - returnList.RemoveAtIndexFast(i); - } - for (int i=0; i < 100000; i++) - returnList.Push(memoryPool.Allocate(_FILE_AND_LINE_), _FILE_AND_LINE_); - while (returnList.Size()) - { - memoryPool.Release(returnList[returnList.Size()-1], _FILE_AND_LINE_); - returnList.RemoveAtIndex(returnList.Size()-1); - } - - return 0; -} -*/ +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_MemoryPool.h +/// + + +#pragma once + +#ifndef __APPLE__ +// Use stdlib and not malloc for compatibility +#include +#endif +#include "RakAssert.h" +#include "Export.h" + +#include "RakMemoryOverride.h" + +// DS_MEMORY_POOL_MAX_FREE_PAGES must be > 1 +#define DS_MEMORY_POOL_MAX_FREE_PAGES 4 + +//#define _DISABLE_MEMORY_POOL + +namespace DataStructures +{ + /// Very fast memory pool for allocating and deallocating structures that don't have constructors or destructors. + /// Contains a list of pages, each of which has an array of the user structures + template + class RAK_DLL_EXPORT MemoryPool + { + public: + struct Page; + struct MemoryWithPage + { + MemoryBlockType userMemory; + Page *parentPage; + }; + struct Page + { + MemoryWithPage** availableStack; + int availableStackSize; + MemoryWithPage* block; + Page *next, *prev; + }; + + MemoryPool(); + ~MemoryPool(); + void SetPageSize(int size); // Defaults to 16384 bytes + MemoryBlockType *Allocate(const char *file, unsigned int line); + void Release(MemoryBlockType *m, const char *file, unsigned int line); + void Clear(const char *file, unsigned int line); + + int GetAvailablePagesSize(void) const {return availablePagesSize;} + int GetUnavailablePagesSize(void) const {return unavailablePagesSize;} + int GetMemoryPoolPageSize(void) const {return memoryPoolPageSize;} + protected: + int BlocksPerPage(void) const; + void AllocateFirst(void); + bool InitPage(Page *page, Page *prev, const char *file, unsigned int line); + + // availablePages contains pages which have room to give the user new blocks. We return these blocks from the head of the list + // unavailablePages are pages which are totally full, and from which we do not return new blocks. + // Pages move from the head of unavailablePages to the tail of availablePages, and from the head of availablePages to the tail of unavailablePages + Page *availablePages, *unavailablePages; + int availablePagesSize, unavailablePagesSize; + int memoryPoolPageSize; + }; + + template + MemoryPool::MemoryPool() + { +#ifndef _DISABLE_MEMORY_POOL + //AllocateFirst(); + availablePagesSize=0; + unavailablePagesSize=0; + memoryPoolPageSize=16384; +#endif + } + template + MemoryPool::~MemoryPool() + { +#ifndef _DISABLE_MEMORY_POOL + Clear(_FILE_AND_LINE_); +#endif + } + + template + void MemoryPool::SetPageSize(int size) + { + memoryPoolPageSize=size; + } + + template + MemoryBlockType* MemoryPool::Allocate(const char *file, unsigned int line) + { +#ifdef _DISABLE_MEMORY_POOL + return (MemoryBlockType*) rakMalloc_Ex(sizeof(MemoryBlockType), file, line); +#else + + if (availablePagesSize>0) + { + MemoryBlockType *retVal; + Page *curPage; + curPage=availablePages; + retVal = (MemoryBlockType*) curPage->availableStack[--(curPage->availableStackSize)]; + if (curPage->availableStackSize==0) + { + --availablePagesSize; + availablePages=curPage->next; + RakAssert(availablePagesSize==0 || availablePages->availableStackSize>0); + curPage->next->prev=curPage->prev; + curPage->prev->next=curPage->next; + + if (unavailablePagesSize++==0) + { + unavailablePages=curPage; + curPage->next=curPage; + curPage->prev=curPage; + } + else + { + curPage->next=unavailablePages; + curPage->prev=unavailablePages->prev; + unavailablePages->prev->next=curPage; + unavailablePages->prev=curPage; + } + } + + RakAssert(availablePagesSize==0 || availablePages->availableStackSize>0); + return retVal; + } + + availablePages = (Page *) rakMalloc_Ex(sizeof(Page), file, line); + if (availablePages==0) + return 0; + availablePagesSize=1; + if (InitPage(availablePages, availablePages, file, line)==false) + return 0; + // If this assert hits, we couldn't allocate even 1 block per page. Increase the page size + RakAssert(availablePages->availableStackSize>1); + + return (MemoryBlockType *) availablePages->availableStack[--availablePages->availableStackSize]; +#endif + } + template + void MemoryPool::Release(MemoryBlockType *m, const char *file, unsigned int line) + { +#ifdef _DISABLE_MEMORY_POOL + rakFree_Ex(m, file, line); + return; +#else + // Find the page this block is in and return it. + Page *curPage; + MemoryWithPage *memoryWithPage = (MemoryWithPage*)m; + curPage=memoryWithPage->parentPage; + + if (curPage->availableStackSize==0) + { + // The page is in the unavailable list so move it to the available list + curPage->availableStack[curPage->availableStackSize++]=memoryWithPage; + unavailablePagesSize--; + + // As this page is no longer totally empty, move it to the end of available pages + curPage->next->prev=curPage->prev; + curPage->prev->next=curPage->next; + + if (unavailablePagesSize>0 && curPage==unavailablePages) + unavailablePages=unavailablePages->next; + + if (availablePagesSize++==0) + { + availablePages=curPage; + curPage->next=curPage; + curPage->prev=curPage; + } + else + { + curPage->next=availablePages; + curPage->prev=availablePages->prev; + availablePages->prev->next=curPage; + availablePages->prev=curPage; + } + } + else + { + curPage->availableStack[curPage->availableStackSize++]=memoryWithPage; + + if (curPage->availableStackSize==BlocksPerPage() && + availablePagesSize>=DS_MEMORY_POOL_MAX_FREE_PAGES) + { + // After a certain point, just deallocate empty pages rather than keep them around + if (curPage==availablePages) + { + availablePages=curPage->next; + RakAssert(availablePages->availableStackSize>0); + } + curPage->prev->next=curPage->next; + curPage->next->prev=curPage->prev; + availablePagesSize--; + rakFree_Ex(curPage->availableStack, file, line ); + rakFree_Ex(curPage->block, file, line ); + rakFree_Ex(curPage, file, line ); + } + } +#endif + } + template + void MemoryPool::Clear(const char *file, unsigned int line) + { +#ifdef _DISABLE_MEMORY_POOL + return; +#else + Page *cur, *freed; + + if (availablePagesSize>0) + { + cur = availablePages; +#ifdef _MSC_VER +#pragma warning(disable:4127) // conditional expression is constant +#endif + while (true) + // do + { + rakFree_Ex(cur->availableStack, file, line ); + rakFree_Ex(cur->block, file, line ); + freed=cur; + cur=cur->next; + if (cur==availablePages) + { + rakFree_Ex(freed, file, line ); + break; + } + rakFree_Ex(freed, file, line ); + }// while(cur!=availablePages); + } + + if (unavailablePagesSize>0) + { + cur = unavailablePages; + while (1) + //do + { + rakFree_Ex(cur->availableStack, file, line ); + rakFree_Ex(cur->block, file, line ); + freed=cur; + cur=cur->next; + if (cur==unavailablePages) + { + rakFree_Ex(freed, file, line ); + break; + } + rakFree_Ex(freed, file, line ); + } // while(cur!=unavailablePages); + } + + availablePagesSize=0; + unavailablePagesSize=0; +#endif + } + template + int MemoryPool::BlocksPerPage(void) const + { + return memoryPoolPageSize / sizeof(MemoryWithPage); + } + template + bool MemoryPool::InitPage(Page *page, Page *prev, const char *file, unsigned int line) + { + int i=0; + const int bpp = BlocksPerPage(); + page->block=(MemoryWithPage*) rakMalloc_Ex(memoryPoolPageSize, file, line); + if (page->block==0) + return false; + page->availableStack=(MemoryWithPage**)rakMalloc_Ex(sizeof(MemoryWithPage*)*bpp, file, line); + if (page->availableStack==0) + { + rakFree_Ex(page->block, file, line ); + return false; + } + MemoryWithPage *curBlock = page->block; + MemoryWithPage **curStack = page->availableStack; + while (i < bpp) + { + curBlock->parentPage=page; + curStack[i]=curBlock++; + i++; + } + page->availableStackSize=bpp; + page->next=availablePages; + page->prev=prev; + return true; + } +} + + +/* +#include "DS_MemoryPool.h" +#include "DS_List.h" + +struct TestMemoryPool +{ + int allocationId; +}; + +int main(void) +{ + DataStructures::MemoryPool memoryPool; + DataStructures::List returnList; + + for (int i=0; i < 100000; i++) + returnList.Push(memoryPool.Allocate(_FILE_AND_LINE_), _FILE_AND_LINE_); + for (int i=0; i < returnList.Size(); i+=2) + { + memoryPool.Release(returnList[i], _FILE_AND_LINE_); + returnList.RemoveAtIndexFast(i); + } + for (int i=0; i < 100000; i++) + returnList.Push(memoryPool.Allocate(_FILE_AND_LINE_), _FILE_AND_LINE_); + while (returnList.Size()) + { + memoryPool.Release(returnList[returnList.Size()-1], _FILE_AND_LINE_); + returnList.RemoveAtIndex(returnList.Size()-1); + } + for (int i=0; i < 100000; i++) + returnList.Push(memoryPool.Allocate(_FILE_AND_LINE_), _FILE_AND_LINE_); + while (returnList.Size()) + { + memoryPool.Release(returnList[returnList.Size()-1], _FILE_AND_LINE_); + returnList.RemoveAtIndex(returnList.Size()-1); + } + for (int i=0; i < 100000; i++) + returnList.Push(memoryPool.Allocate(_FILE_AND_LINE_), _FILE_AND_LINE_); + for (int i=100000-1; i <= 0; i-=2) + { + memoryPool.Release(returnList[i], _FILE_AND_LINE_); + returnList.RemoveAtIndexFast(i); + } + for (int i=0; i < 100000; i++) + returnList.Push(memoryPool.Allocate(_FILE_AND_LINE_), _FILE_AND_LINE_); + while (returnList.Size()) + { + memoryPool.Release(returnList[returnList.Size()-1], _FILE_AND_LINE_); + returnList.RemoveAtIndex(returnList.Size()-1); + } + + return 0; +} +*/ diff --git a/Source/DS_Multilist.h b/Source/DS_Multilist.h index 8bd73f07d..6ffae4ab8 100644 --- a/Source/DS_Multilist.h +++ b/Source/DS_Multilist.h @@ -1,1650 +1,1648 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_Multilist.h -/// \internal -/// \brief ADT that can represent an unordered list, ordered list, stack, or queue with a common interface -/// - -#ifndef __MULTILIST_H -#define __MULTILIST_H - -#include "RakAssert.h" -#include // memmove -#include "Export.h" -#include "RakMemoryOverride.h" -#include "NativeTypes.h" - - -#ifdef _MSC_VER -#pragma warning( push ) -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#pragma warning( disable : 4512 ) // warning C4512: assignment operator could not be generated -#endif - -/// What algorithm to use to store the data for the Multilist -enum MultilistType -{ - /// Removing from the middle of the list will swap the end of the list rather than shift the elements. Push and Pop operate on the tail. - ML_UNORDERED_LIST, - /// A normal list, with the list order preserved. Push and Pop operate on the tail. - ML_STACK, - /// A queue. Push and Pop operate on the head - ML_QUEUE, - /// A list that is always kept in order. Elements must be unique, and compare against each other consistently using <, ==, and > - ML_ORDERED_LIST, - /// A list whose type can change at runtime - ML_VARIABLE_DURING_RUNTIME -}; - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - /// Can be used with Multilist::ForEach - /// Assuming the Multilist holds pointers, will delete those pointers - template - void DeletePtr_RakNet(templateType &ptr, const char *file, unsigned int line ) {RakNet::OP_DELETE(ptr, file, line);} - - /// Can be used with Multilist::ForEach - /// Assuming the Multilist holds pointers, will delete those pointers - template - void DeletePtr(templateType &ptr) {delete ptr;} - - /// The following is invalid. - /// bool operator<( const MyClass *myClass, const int &inputKey ) {return myClass->value < inputKey;} - /// At least one type has to be a reference to a class - /// MLKeyRef is a helper class to turn a native type into a class, so you can compare that native type against a pointer to a different class - /// Used for he Multilist, when _DataType != _KeyType - template < class templateType > - class MLKeyRef - { - public: - MLKeyRef(const templateType& input) : val(input) {} - const templateType &Get(void) const {return val;} - bool operator<( const templateType &right ) {return val < right;} - bool operator>( const templateType &right ) {return val > right;} - bool operator==( const templateType &right ) {return val == right;} - protected: - const templateType &val; - }; - - /// For the Multilist, when _DataType != _KeyType, you must define the comparison operators between the key and the data - /// This is non-trivial due to the need to use MLKeyRef in case the type held is a pointer to a structure or class and the key type is not a class - /// For convenience, this macro will implement the comparison operators under the following conditions - /// 1. _DataType is a pointer to a class or structure - /// 2. The key is a member variable of _DataType - #define DEFINE_MULTILIST_PTR_TO_MEMBER_COMPARISONS( _CLASS_NAME_, _KEY_TYPE_, _MEMBER_VARIABLE_NAME_ ) \ - bool operator<( const DataStructures::MLKeyRef<_KEY_TYPE_> &inputKey, const _CLASS_NAME_ *cls ) {return inputKey.Get() < cls->_MEMBER_VARIABLE_NAME_;} \ - bool operator>( const DataStructures::MLKeyRef<_KEY_TYPE_> &inputKey, const _CLASS_NAME_ *cls ) {return inputKey.Get() > cls->_MEMBER_VARIABLE_NAME_;} \ - bool operator==( const DataStructures::MLKeyRef<_KEY_TYPE_> &inputKey, const _CLASS_NAME_ *cls ) {return inputKey.Get() == cls->_MEMBER_VARIABLE_NAME_;} - - typedef uint32_t DefaultIndexType; - - /// \brief The multilist, representing an abstract data type that generally holds lists. - /// \param[in] _MultilistType What type of list this is, \sa MultilistType - /// \param[in] _DataType What type of data this list holds. - /// \param[in] _KeyType If a function takes a key to sort on, what type of key this is. The comparison operator between _DataType and _KeyType must be defined - /// \param[in] _IndexType What variable type to use for indices - template - class RAK_DLL_EXPORT Multilist - { - public: - Multilist(); - ~Multilist(); - Multilist( const Multilist& source ); - Multilist& operator= ( const Multilist& source ); - _DataType& operator[] ( const _IndexType position ) const; - /// Unordered list, stack is LIFO - /// QUEUE is FIFO - /// Ordered list is inserted in order - void Push(const _DataType &d, const char *file=__FILE__, unsigned int line=__LINE__ ); - void Push(const _DataType &d, const _KeyType &key, const char *file=__FILE__, unsigned int line=__LINE__ ); - - /// \brief Gets or removes and gets an element from the list, according to the same rules as Push(). - /// Ordered list is LIFO for the purposes of Pop and Peek. - _DataType &Pop(const char *file=__FILE__, unsigned int line=__LINE__); - _DataType &Peek(void) const; - - /// \brief Same as Push(), except FIFO and LIFO are reversed. - /// Ordered list still inserts in order. - void PushOpposite(const _DataType &d, const char *file=__FILE__, unsigned int line=__LINE__ ); - void PushOpposite(const _DataType &d, const _KeyType &key, const char *file=__FILE__, unsigned int line=__LINE__ ); - - /// \brief Same as Pop() and Peek(), except FIFO and LIFO are reversed. - _DataType &PopOpposite(const char *file=__FILE__, unsigned int line=__LINE__); - _DataType &PeekOpposite(void) const; - - /// \brief Stack,Queue: Inserts at index indicated, elements are shifted. - /// Ordered list: Inserts, position is ignored - void InsertAtIndex(const _DataType &d, _IndexType index, const char *file=__FILE__, unsigned int line=__LINE__); - - /// \brief Unordered list, removes at index indicated, swaps last element with that element. - /// Otherwise, array is shifted left to overwrite removed element - /// \details Index[0] returns the same as Pop() for a queue. - /// Same as PopOpposite() for the list and ordered list - void RemoveAtIndex(_IndexType position, const char *file=__FILE__, unsigned int line=__LINE__); - - /// \brief Find the index of \a key, and remove at that index. - bool RemoveAtKey(_KeyType key, bool assertIfDoesNotExist, const char *file=__FILE__, unsigned int line=__LINE__); - - /// \brief Finds the index of \a key. Return -1 if the key is not found. - _IndexType GetIndexOf(_KeyType key) const; - - /// \brief Returns where in the list we should insert the item, to preserve list order. - /// Returns -1 if the item is already in the list - _IndexType GetInsertionIndex(_KeyType key) const; - - /// \brief Finds the index of \a key. Return 0 if the key is not found. Useful if _DataType is always non-zero pointers. - _DataType GetPtr(_KeyType key) const; - - /// \brief Iterate over the list, calling the function pointer on each element. - void ForEach(void (*func)(_DataType &item, const char *file, unsigned int line), const char *file, unsigned int line); - void ForEach(void (*func)(_DataType &item)); - - /// \brief Returns if the list is empty. - bool IsEmpty(void) const; - - /// \brief Returns the number of elements used in the list. - _IndexType GetSize(void) const; - - /// \brief Empties the list. The list is not deallocated if it is small, - /// unless \a deallocateSmallBlocks is true - void Clear( bool deallocateSmallBlocks=true, const char *file=__FILE__, unsigned int line=__LINE__ ); - - /// \brief Empties the list, first calling RakNet::OP_Delete on all items. - /// \details The list is not deallocated if it is small, unless \a deallocateSmallBlocks is true - void ClearPointers( bool deallocateSmallBlocks=true, const char *file=__FILE__, unsigned int line=__LINE__ ); - - /// \brief Empty one item from the list, first calling RakNet::OP_Delete on that item. - void ClearPointer( _KeyType key, const char *file=__FILE__, unsigned int line=__LINE__ ); - - /// \brief Reverses the elements in the list, and flips the sort order - /// returned by GetSortOrder() if IsSorted() returns true at the time the function is called - void ReverseList(void); - - /// \brief Reallocates the list to a larger size. - /// If \a size is smaller than the value returned by GetSize(), the call does nothing. - void Reallocate(_IndexType size, const char *file=__FILE__, unsigned int line=__LINE__); - - /// \brief Sorts the list unless it is an ordered list, in which it does nothing as the list is assumed to already be sorted. - /// \details However, if \a force is true, it will also resort the ordered list, useful if the comparison operator between _KeyType and _DataType would now return different results - /// Once the list is sorted, further operations to lookup by key will be log2(n) until the list is modified - void Sort(bool force); - - /// \brief Sets the list to be remembered as sorted. - /// \details Optimization if the source is sorted already - void TagSorted(void); - - /// \brief Defaults to ascending. - /// \details Used by Sort(), and by ML_ORDERED_LIST - void SetSortOrder(bool ascending); - - /// \brief Returns true if ascending. - bool GetSortOrder(void) const; - - /// \brief Returns true if the list is currently believed to be in a sorted state. - /// \details Doesn't actually check for sortedness, just if Sort() - /// was recently called, or MultilistType is ML_ORDERED_LIST - bool IsSorted(void) const; - - /// Returns what type of list this is - MultilistType GetMultilistType(void) const; - - /// \brief Changes what type of list this is. - /// \pre Template must be defined with ML_VARIABLE_DURING_RUNTIME for this to do anything - /// \param[in] mlType Any value of the enum MultilistType, except ML_VARIABLE_DURING_RUNTIME - void SetMultilistType(MultilistType newType); - - /// \brief Returns the intersection of two lists. - /// Intersection is items common to both lists. - static void FindIntersection( - Multilist& source1, - Multilist& source2, - Multilist& intersection, - Multilist& uniqueToSource1, - Multilist& uniqueToSource2); - - protected: - void ReallocateIfNeeded(const char *file, unsigned int line); - void DeallocateIfNeeded(const char *file, unsigned int line); - void ReallocToSize(_IndexType newAllocationSize, const char *file, unsigned int line); - void ReverseListInternal(void); - void InsertInOrderedList(const _DataType &d, const _KeyType &key); - _IndexType GetIndexFromKeyInSortedList(const _KeyType &key, bool *objectExists) const; - void InsertShiftArrayRight(const _DataType &d, _IndexType index); - void DeleteShiftArrayLeft(_IndexType index); - void QSortAscending(_IndexType left, _IndexType right); - void QSortDescending(_IndexType left, _IndexType right); - void CopySource( const Multilist& source ); - - /// An array of user values - _DataType* data; - - /// Number of elements in the list - _IndexType dataSize; - - /// Size of \a array - _IndexType allocationSize; - - /// Array index for the head of the queue - _IndexType queueHead; - - /// Array index for the tail of the queue - _IndexType queueTail; - - /// How many bytes the user chose to preallocate - /// Won't automatically deallocate below this - _IndexType preallocationSize; - - enum - { - ML_UNSORTED, - ML_SORTED_ASCENDING, - ML_SORTED_DESCENDING - } sortState; - - bool ascendingSort; - - // In case we are using the variable type multilist - MultilistType variableMultilistType; - }; - - template - Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Multilist() - { - data=0; - dataSize=0; - allocationSize=0; - ascendingSort=true; - sortState=ML_UNSORTED; - queueHead=0; - queueTail=0; - preallocationSize=0; - - if (_MultilistType==ML_ORDERED_LIST) - sortState=ML_SORTED_ASCENDING; - else - sortState=ML_UNSORTED; - - if (_MultilistType==ML_VARIABLE_DURING_RUNTIME) - variableMultilistType=ML_UNORDERED_LIST; - } - - template - Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::~Multilist() - { - if (data!=0) - RakNet::OP_DELETE_ARRAY(data, _FILE_AND_LINE_); - } - - template - Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Multilist( const Multilist& source ) - { - CopySource(source); - } - - template - Multilist<_MultilistType, _DataType, _KeyType, _IndexType>& Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::operator= ( const Multilist& source ) - { - Clear(true); - CopySource(source); - return *this; - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::CopySource( const Multilist& source ) - { - dataSize=source.GetSize(); - ascendingSort=source.ascendingSort; - sortState=source.sortState; - queueHead=0; - queueTail=dataSize; - preallocationSize=source.preallocationSize; - variableMultilistType=source.variableMultilistType; - if (source.data==0) - { - data=0; - allocationSize=0; - } - else - { - allocationSize=dataSize; - data = RakNet::OP_NEW_ARRAY<_DataType>(dataSize,_FILE_AND_LINE_); - _IndexType i; - for (i=0; i < dataSize; i++) - data[i]=source[i]; - } - } - - template - _DataType& Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::operator[] ( const _IndexType position ) const - { - RakAssert(position= allocationSize ) - return data[ queueHead + position - allocationSize ]; - else - return data[ queueHead + position ]; - } - - return data[position]; - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Push(const _DataType &d, const char *file, unsigned int line ) - { - Push(d,d,file,line); - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Push(const _DataType &d, const _KeyType &key, const char *file, unsigned int line ) - { - ReallocateIfNeeded(file,line); - - if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK) - { - data[dataSize]=d; - dataSize++; - } - else if (GetMultilistType()==ML_QUEUE) - { - data[queueTail++] = d; - - if ( queueTail == allocationSize ) - queueTail = 0; - dataSize++; - } - else - { - RakAssert(GetMultilistType()==ML_ORDERED_LIST); - InsertInOrderedList(d,key); - } - - if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_QUEUE) - { - // Break sort if no longer sorted - if (sortState!=ML_UNSORTED && dataSize>1) - { - if (ascendingSort) - { - if ( MLKeyRef<_KeyType>(key) < operator[](dataSize-2) ) - sortState=ML_UNSORTED; - } - else - { - if ( MLKeyRef<_KeyType>(key) > operator[](dataSize-2) ) - sortState=ML_UNSORTED; - } - - sortState=ML_UNSORTED; - } - } - } - - template - _DataType &Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Pop(const char *file, unsigned int line) - { - RakAssert(IsEmpty()==false); - DeallocateIfNeeded(file,line); - if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST) - { - dataSize--; - return data[dataSize]; - } - else - { - RakAssert(GetMultilistType()==ML_QUEUE); - - if ( ++queueHead == allocationSize ) - queueHead = 0; - - if ( queueHead == 0 ) - return data[ allocationSize -1 ]; - - dataSize--; - return data[ queueHead -1 ]; - } - } - - template - _DataType &Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Peek(void) const - { - RakAssert(IsEmpty()==false); - if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST) - { - return data[dataSize-1]; - } - else - { - RakAssert(GetMultilistType()==ML_QUEUE); - return data[ queueHead ]; - } - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::PushOpposite(const _DataType &d, const char *file, unsigned int line ) - { - PushOpposite(d,d,file,line); - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::PushOpposite(const _DataType &d, const _KeyType &key, const char *file, unsigned int line ) - { - ReallocateIfNeeded(file,line); - - // Unordered list Push at back - if (GetMultilistType()==ML_UNORDERED_LIST) - { - data[dataSize]=d; - dataSize++; - } - else if (GetMultilistType()==ML_STACK) - { - // Stack push at front of the list, instead of back as normal - InsertAtIndex(d,0,file,line); - } - else if (GetMultilistType()==ML_QUEUE) - { - // Queue push at front of the list, instead of back as normal - InsertAtIndex(d,0,file,line); - } - else - { - RakAssert(GetMultilistType()==ML_ORDERED_LIST); - InsertInOrderedList(d,key); - } - - if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_QUEUE) - { - // Break sort if no longer sorted - if (sortState!=ML_UNSORTED && dataSize>1) - { - if (ascendingSort) - { - if ( MLKeyRef<_KeyType>(key) > operator[](1) ) - sortState=ML_UNSORTED; - } - else - { - if ( MLKeyRef<_KeyType>(key) < operator[](1) ) - sortState=ML_UNSORTED; - } - } - } - } - - template - _DataType &Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::PopOpposite(const char *file, unsigned int line) - { - RakAssert(IsEmpty()==false); - if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST) - { - // Copy leftmost to end - ReallocateIfNeeded(file,line); - data[dataSize]=data[0]; - DeleteShiftArrayLeft(0); - --dataSize; - // Assuming still leaves at least one element past the end of the list allocated - DeallocateIfNeeded(file,line); - // Return end - return data[dataSize+1]; - } - else - { - RakAssert(GetMultilistType()==ML_QUEUE); - // Deallocate first, since we are returning off the existing list - DeallocateIfNeeded(file,line); - dataSize--; - - if (queueTail==0) - queueTail=allocationSize-1; - else - --queueTail; - - return data[queueTail]; - } - } - - template - _DataType &Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::PeekOpposite(void) const - { - RakAssert(IsEmpty()==false); - if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST) - { - return data[0]; - } - else - { - RakAssert(GetMultilistType()==ML_QUEUE); - _IndexType priorIndex; - if (queueTail==0) - priorIndex=allocationSize-1; - else - priorIndex=queueTail-1; - - return data[priorIndex]; - } - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::InsertAtIndex(const _DataType &d, _IndexType index, const char *file, unsigned int line) - { - ReallocateIfNeeded(file,line); - - if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST) - { - if (index>=dataSize) - { - // insert at end - data[dataSize]=d; - - dataSize++; - } - else - { - // insert at index - InsertShiftArrayRight(d,index); - } - } - else - { - data[queueTail++] = d; - - if ( queueTail == allocationSize ) - queueTail = 0; - - ++dataSize; - - if (dataSize==1) - return; - - _IndexType writeIndex, readIndex, trueWriteIndex, trueReadIndex; - writeIndex=dataSize-1; - readIndex=writeIndex-1; - while (readIndex >= index) - { - if ( queueHead + writeIndex >= allocationSize ) - trueWriteIndex = queueHead + writeIndex - allocationSize; - else - trueWriteIndex = queueHead + writeIndex; - - if ( queueHead + readIndex >= allocationSize ) - trueReadIndex = queueHead + readIndex - allocationSize; - else - trueReadIndex = queueHead + readIndex; - - data[trueWriteIndex]=data[trueReadIndex]; - - if (readIndex==0) - break; - writeIndex--; - readIndex--; - } - - if ( queueHead + index >= allocationSize ) - trueWriteIndex = queueHead + index - allocationSize; - else - trueWriteIndex = queueHead + index; - - data[trueWriteIndex]=d; - } - - if (_MultilistType!=ML_ORDERED_LIST) - sortState=ML_UNSORTED; - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::RemoveAtIndex(_IndexType position, const char *file, unsigned int line) - { - RakAssert(position < dataSize); - RakAssert(IsEmpty()==false); - - if (GetMultilistType()==ML_UNORDERED_LIST) - { - // Copy tail to current - data[position]=data[dataSize-1]; - } - else if (GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST) - { - DeleteShiftArrayLeft(position); - } - else - { - RakAssert(GetMultilistType()==ML_QUEUE); - - _IndexType index, next; - - if ( queueHead + position >= allocationSize ) - index = queueHead + position - allocationSize; - else - index = queueHead + position; - - next = index + 1; - - if ( next == allocationSize ) - next = 0; - - while ( next != queueTail ) - { - // Overwrite the previous element - data[ index ] = data[ next ]; - index = next; - //next = (next + 1) % allocationSize; - - if ( ++next == allocationSize ) - next = 0; - } - - // Move the queueTail back - if ( queueTail == 0 ) - queueTail = allocationSize - 1; - else - --queueTail; - } - - - dataSize--; - DeallocateIfNeeded(file,line); - } - - template - bool Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::RemoveAtKey(_KeyType key, bool assertIfDoesNotExist, const char *file, unsigned int line) - { - _IndexType index = GetIndexOf(key); - if (index==(_IndexType)-1) - { - RakAssert(assertIfDoesNotExist==false && "RemoveAtKey element not found"); - return false; - } - RemoveAtIndex(index,file,line); - return true; - } - - template - _IndexType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetIndexOf(_KeyType key) const - { - _IndexType i; - if (IsSorted()) - { - bool objectExists; - i=GetIndexFromKeyInSortedList(key, &objectExists); - if (objectExists) - return i; - return (_IndexType)-1; - } - else if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK) - { - for (i=0; i < dataSize; i++) - { - if (MLKeyRef<_KeyType>(key)==data[i]) - return i; - } - return (_IndexType)-1; - } - else - { - RakAssert( GetMultilistType()==ML_QUEUE ); - - for (i=0; i < dataSize; i++) - { - if (MLKeyRef<_KeyType>(key)==operator[](i)) - return i; - } - return (_IndexType)-1; - } - } - - template - _IndexType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetInsertionIndex(_KeyType key) const - { - _IndexType i; - if (IsSorted()) - { - bool objectExists; - i=GetIndexFromKeyInSortedList(key, &objectExists); - if (objectExists) - return (_IndexType)-1; - return i; - } - else if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK) - { - for (i=0; i < dataSize; i++) - { - if (MLKeyRef<_KeyType>(key)==data[i]) - return (_IndexType)-1; - } - return dataSize; - } - else - { - RakAssert( GetMultilistType()==ML_QUEUE ); - - for (i=0; i < dataSize; i++) - { - if (MLKeyRef<_KeyType>(key)==operator[](i)) - return (_IndexType)-1; - } - return dataSize; - } - } - - template - _DataType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetPtr(_KeyType key) const - { - _IndexType i = GetIndexOf(key); - if (i==(_IndexType)-1) - return 0; - return data[i]; - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ForEach(void (*func)(_DataType &item, const char *file, unsigned int line), const char *file, unsigned int line) - { - _IndexType i; - for (i=0; i < dataSize; i++) - func(operator[](i), file, line); - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ForEach(void (*func)(_DataType &item)) - { - _IndexType i; - for (i=0; i < dataSize; i++) - func(operator[](i)); - } - - template - bool Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::IsEmpty(void) const - { - return dataSize==0; - } - - template - _IndexType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetSize(void) const - { - return dataSize; - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Clear( bool deallocateSmallBlocks, const char *file, unsigned int line ) - { - dataSize=0; - if (GetMultilistType()==ML_ORDERED_LIST) - if (ascendingSort) - sortState=ML_SORTED_ASCENDING; - else - sortState=ML_SORTED_DESCENDING; - else - sortState=ML_UNSORTED; - queueHead=0; - queueTail=0; - - if (deallocateSmallBlocks && allocationSize < 128 && data) - { - RakNet::OP_DELETE_ARRAY(data,file,line); - data=0; - allocationSize=0; - } - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ClearPointers( bool deallocateSmallBlocks, const char *file, unsigned int line ) - { - _IndexType i; - for (i=0; i < dataSize; i++) - RakNet::OP_DELETE(operator[](i), file, line); - Clear(deallocateSmallBlocks, file, line); - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ClearPointer( _KeyType key, const char *file, unsigned int line ) - { - _IndexType i; - i = GetIndexOf(key); - if (i!=-1) - { - RakNet::OP_DELETE(operator[](i), file, line); - RemoveAtIndex(i); - } - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ReverseList(void) - { - if (IsSorted()) - ascendingSort=!ascendingSort; - - ReverseListInternal(); - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Reallocate(_IndexType size, const char *file, unsigned int line) - { - _IndexType newAllocationSize; - if (size < dataSize) - newAllocationSize=dataSize; - else - newAllocationSize=size; - preallocationSize=size; - ReallocToSize(newAllocationSize,file,line); - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Sort(bool force) - { - if (IsSorted() && force==false) - return; - - if (dataSize>1) - { - if (ascendingSort) - QSortAscending(0,dataSize-1); - else - QSortDescending(0,dataSize-1); - } - - TagSorted(); - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::TagSorted(void) - { - if (ascendingSort) - sortState=ML_SORTED_ASCENDING; - else - sortState=ML_SORTED_DESCENDING; - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::QSortAscending(_IndexType leftEdge, _IndexType rightEdge) - { - _DataType temp; - _IndexType left=leftEdge; - _IndexType right=rightEdge; - _IndexType pivotIndex=left++; - - while (left data[pivotIndex]) - { - --left; - - data[pivotIndex]=data[left]; - data[left]=temp; - } - else - { - data[pivotIndex]=data[left]; - data[left]=temp; - - --left; - } - - if (left!=leftEdge) - QSortAscending(leftEdge, left); - - if (right!=rightEdge) - QSortAscending(right, rightEdge); - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::QSortDescending(_IndexType leftEdge, _IndexType rightEdge) - { - _DataType temp; - _IndexType left=leftEdge; - _IndexType right=rightEdge; - _IndexType pivotIndex=left++; - - while (left= data[pivotIndex]) - { - ++left; - } - else - { - temp=data[left]; - data[left]=data[right]; - data[right]=temp; - --right; - } - } - - temp=data[pivotIndex]; - - // Move pivot to center - if (data[left] < data[pivotIndex]) - { - --left; - - data[pivotIndex]=data[left]; - data[left]=temp; - } - else - { - data[pivotIndex]=data[left]; - data[left]=temp; - - --left; - } - - if (left!=leftEdge) - QSortDescending(leftEdge, left); - - if (right!=rightEdge) - QSortDescending(right, rightEdge); - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::SetSortOrder(bool ascending) - { - if (ascendingSort!=ascending && IsSorted()) - { - ascendingSort=ascending; - // List is sorted, and the sort order has changed. So reverse the list - ReverseListInternal(); - } - else - ascendingSort=ascending; - } - - template - bool Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetSortOrder(void) const - { - return ascendingSort; - } - - template - bool Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::IsSorted(void) const - { - return GetMultilistType()==ML_ORDERED_LIST || sortState!=ML_UNSORTED; - } - - template - MultilistType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetMultilistType(void) const - { - if (_MultilistType==ML_VARIABLE_DURING_RUNTIME) - return variableMultilistType; - return _MultilistType; - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::SetMultilistType(MultilistType newType) - { - RakAssert(_MultilistType==ML_VARIABLE_DURING_RUNTIME); - switch (variableMultilistType) - { - case ML_UNORDERED_LIST: - switch (newType) - { - case ML_UNORDERED_LIST: - // No change - break; - case ML_STACK: - // Same data format - break; - case ML_QUEUE: - queueHead=0; - queueTail=dataSize; - break; - case ML_ORDERED_LIST: - Sort(false); - break; - } - break; - case ML_STACK: - switch (newType) - { - case ML_UNORDERED_LIST: - // Same data format - break; - case ML_STACK: - // No change - break; - case ML_QUEUE: - queueHead=0; - queueTail=dataSize; - break; - case ML_ORDERED_LIST: - Sort(false); - break; - } - break; - case ML_QUEUE: - switch (newType) - { - case ML_UNORDERED_LIST: - case ML_STACK: - case ML_ORDERED_LIST: - if (queueTail < queueHead) - { - // Realign data if wrapped - ReallocToSize(dataSize, _FILE_AND_LINE_); - } - else - { - // Else can just copy starting at head - _IndexType i; - for (i=0; i < dataSize; i++) - data[i]=operator[](i); - } - if (newType==ML_ORDERED_LIST) - Sort(false); - break; - case ML_QUEUE: - // No change - break; - } - break; - case ML_ORDERED_LIST: - switch (newType) - { - case ML_UNORDERED_LIST: - case ML_STACK: - case ML_QUEUE: - // Same data format - // Tag as sorted - if (ascendingSort) - sortState=ML_SORTED_ASCENDING; - else - sortState=ML_SORTED_DESCENDING; - if (newType==ML_QUEUE) - { - queueHead=0; - queueTail=dataSize; - } - break; - case ML_ORDERED_LIST: - // No change - break; - } - break; - } - - variableMultilistType=newType; - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::FindIntersection( - Multilist& source1, - Multilist& source2, - Multilist& intersection, - Multilist& uniqueToSource1, - Multilist& uniqueToSource2) - { - _IndexType index1=0, index2=0; - source1.SetSortOrder(true); - source2.SetSortOrder(true); - source1.Sort(false); - source2.Sort(false); - intersection.Clear(true,_FILE_AND_LINE_); - uniqueToSource1.Clear(true,_FILE_AND_LINE_); - uniqueToSource2.Clear(true,_FILE_AND_LINE_); - - while (index1 < source1.GetSize() && index2 < source2.GetSize()) - { - if (source1[index1] - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ReallocateIfNeeded(const char *file, unsigned int line) - { - if (dataSize65536) - newAllocationSize=allocationSize+65536; - else - { - newAllocationSize=allocationSize<<1; // * 2 - // Protect against underflow - if (newAllocationSize < allocationSize) - newAllocationSize=allocationSize+65536; - } - - ReallocToSize(newAllocationSize,file,line); - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::DeallocateIfNeeded(const char *file, unsigned int line) - { - if (allocationSize<512) - return; - if (dataSize >= allocationSize/3 ) - return; - if (dataSize <= preallocationSize ) - return; - - _IndexType newAllocationSize = dataSize<<1; // * 2 - - ReallocToSize(newAllocationSize,file,line); - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ReallocToSize(_IndexType newAllocationSize, const char *file, unsigned int line) - { - _DataType* newData = RakNet::OP_NEW_ARRAY<_DataType>(newAllocationSize,file,line); - _IndexType i; - for (i=0; i < dataSize; i++) - newData[i]=operator[](i); - if (dataSize>0) - { - RakNet::OP_DELETE_ARRAY(data,file,line); - if (GetMultilistType()==ML_QUEUE) - { - queueHead=0; - queueTail=dataSize; - } - } - data=newData; - allocationSize=newAllocationSize; - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ReverseListInternal(void) - { - _DataType temp; - _IndexType i; - for (i=0; i < dataSize/2; i++) - { - temp=operator[](i); - operator[](i)=operator[](dataSize-1-i); - operator[](dataSize-1-i)=temp; - } - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::InsertInOrderedList(const _DataType &d, const _KeyType &key) - { - RakAssert(GetMultilistType()==ML_ORDERED_LIST); - - bool objectExists; - _IndexType index; - index = GetIndexFromKeyInSortedList(key, &objectExists); - - // if (objectExists) - // { - // Ordered list only allows unique insertions - // RakAssert("Duplicate insertion into ordered list" && false); - // return; - // } - - if (index>=dataSize) - { - // insert at end - data[dataSize]=d; - dataSize++; - } - else - { - // insert at index - InsertShiftArrayRight(d,index); - } - } - - template - _IndexType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetIndexFromKeyInSortedList(const _KeyType &key, bool *objectExists) const - { - RakAssert(IsSorted()); - _IndexType index, upperBound, lowerBound; - - if (dataSize==0) - { - *objectExists=false; - return 0; - } - - upperBound=dataSize-1; - lowerBound=0; - index = dataSize/2; - -#ifdef _MSC_VER - #pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - while (1) - { - if (MLKeyRef<_KeyType>(key) > operator[](index) ) - { - if (ascendingSort) - lowerBound=index+1; - else - upperBound=index-1; - } - else if (MLKeyRef<_KeyType>(key) < operator[](index) ) - { - if (ascendingSort) - upperBound=index-1; - else - lowerBound=index+1; - } - else - { - // == - *objectExists=true; - return index; - } - - index=lowerBound+(upperBound-lowerBound)/2; - - if (lowerBound>upperBound || upperBound==(_IndexType)-1) - { - *objectExists=false; - return lowerBound; // No match - } - } - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::InsertShiftArrayRight(const _DataType &d, _IndexType index) - { - RakAssert(_MultilistType!=ML_QUEUE); - - // Move the elements in the list to make room - _IndexType i; - for ( i = dataSize; i != index; i-- ) - data[ i ] = data[ i - 1 ]; - - // Insert the new item at the correct spot - data[ index ] = d; - - ++dataSize; - } - - template - void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::DeleteShiftArrayLeft( _IndexType index ) - { - RakAssert(index < dataSize); - RakAssert(_MultilistType!=ML_QUEUE); - - _IndexType i; - for ( i = index; i < dataSize-1; i++ ) - data[i]=data[i+1]; - } -}; - -/* -struct KeyAndValue -{ - int key; - short value; -}; - -DEFINE_MULTILIST_PTR_TO_MEMBER_COMPARISONS(KeyAndValue,int,key) - -void MultilistUnitTest(void) -{ - DataStructures::DefaultIndexType oldSize; - DataStructures::Multilist ml1; - ml1.Reallocate(64); - RakAssert(ml1.IsEmpty()); - ml1.Push(53); - RakAssert(ml1.Peek()==53); - RakAssert(ml1.IsEmpty()==false); - RakAssert(ml1.Pop()==53); - RakAssert(ml1.IsEmpty()==true); - for (int i=0; i < 512; i++) - ml1.Push(i); - RakAssert(ml1.GetIndexOf(200)==200); - RakAssert(ml1.PeekOpposite()==0); - RakAssert(ml1.PopOpposite()==0); - RakAssert(ml1.PeekOpposite()==1); - RakAssert(ml1.Peek()==511); - ml1.ReverseList(); - for (int i=0; i < 511; i++) - RakAssert(ml1[i]==511-i); - RakAssert(ml1.PeekOpposite()==511); - RakAssert(ml1.Peek()==1); - oldSize = ml1.GetSize(); - ml1.RemoveAtIndex(0); - RakAssert(ml1.GetSize()==oldSize-1); - RakAssert(ml1.PeekOpposite()==1); - ml1.Clear(_FILE_AND_LINE_); - RakAssert(ml1.IsEmpty()==true); - - ml1.Sort(true); - ml1.Clear(_FILE_AND_LINE_); - - ml1.Push(100); - ml1.Sort(true); - ml1.Clear(_FILE_AND_LINE_); - - ml1.Push(50); - ml1.Push(100); - ml1.Sort(true); - ml1.Clear(_FILE_AND_LINE_); - - ml1.Push(100); - ml1.Push(50); - ml1.Sort(true); - ml1.Clear(_FILE_AND_LINE_); - - ml1.Push(100); - ml1.Push(50); - ml1.Push(150); - ml1.Push(25); - ml1.Push(175); - ml1.Sort(true); - RakAssert(ml1[0]==25); - RakAssert(ml1[1]==50); - RakAssert(ml1[2]==100); - RakAssert(ml1[3]==150); - RakAssert(ml1[4]==175); - RakAssert(ml1.GetIndexOf(25)==0); - RakAssert(ml1.GetIndexOf(50)==1); - RakAssert(ml1.GetIndexOf(100)==2); - RakAssert(ml1.GetIndexOf(150)==3); - RakAssert(ml1.GetIndexOf(175)==4); - ml1.Clear(_FILE_AND_LINE_); - - ml1.Push(1); - ml1.Push(2); - ml1.Push(3); - ml1.Push(4); - ml1.Push(5); - ml1.Sort(true); - RakAssert(ml1[0]==1); - RakAssert(ml1[1]==2); - RakAssert(ml1[2]==3); - RakAssert(ml1[3]==4); - RakAssert(ml1[4]==5); - RakAssert(ml1.GetIndexOf(1)==0); - RakAssert(ml1.GetIndexOf(2)==1); - RakAssert(ml1.GetIndexOf(3)==2); - RakAssert(ml1.GetIndexOf(4)==3); - RakAssert(ml1.GetIndexOf(5)==4); - ml1.Clear(_FILE_AND_LINE_); - - ml1.Push(5); - ml1.Push(4); - ml1.Push(3); - ml1.Push(2); - ml1.Push(1); - ml1.Sort(true); - RakAssert(ml1[0]==1); - RakAssert(ml1[1]==2); - RakAssert(ml1[2]==3); - RakAssert(ml1[3]==4); - RakAssert(ml1[4]==5); - RakAssert(ml1.GetIndexOf(1)==0); - RakAssert(ml1.GetIndexOf(2)==1); - RakAssert(ml1.GetIndexOf(3)==2); - RakAssert(ml1.GetIndexOf(4)==3); - RakAssert(ml1.GetIndexOf(5)==4); - ml1.Sort(true); - RakAssert(ml1[0]==1); - RakAssert(ml1[1]==2); - RakAssert(ml1[2]==3); - RakAssert(ml1[3]==4); - RakAssert(ml1[4]==5); - RakAssert(ml1.GetIndexOf(1)==0); - RakAssert(ml1.GetIndexOf(2)==1); - RakAssert(ml1.GetIndexOf(3)==2); - RakAssert(ml1.GetIndexOf(4)==3); - RakAssert(ml1.GetIndexOf(5)==4); - ml1.Clear(_FILE_AND_LINE_); - - DataStructures::Multilist ml2; - ml2.Reallocate(64); - RakAssert(ml2.IsEmpty()); - ml2.Push(53); - RakAssert(ml2.Peek()==53); - RakAssert(ml2.IsEmpty()==false); - RakAssert(ml2.Pop()==53); - RakAssert(ml2.IsEmpty()==true); - for (int i=0; i < 512; i++) - ml2.Push(i); - RakAssert(ml2.GetIndexOf(200)==200); - RakAssert(ml2.PeekOpposite()==0); - RakAssert(ml2.PopOpposite()==0); - RakAssert(ml2.PeekOpposite()==1); - RakAssert(ml2.Peek()==511); - ml2.ReverseList(); - for (int i=0; i < 511; i++) - RakAssert(ml2[i]==511-i); - RakAssert(ml2.PeekOpposite()==511); - RakAssert(ml2.Peek()==1); - oldSize = ml2.GetSize(); - ml2.RemoveAtIndex(0); - RakAssert(ml2.GetSize()==oldSize-1); - RakAssert(ml2.Peek()==1); - RakAssert(ml2.PeekOpposite()==510); - ml2.Clear(_FILE_AND_LINE_); - RakAssert(ml2.IsEmpty()==true); - - DataStructures::Multilist ml3; - RakAssert(ml3.IsEmpty()); - ml3.Push(53); - RakAssert(ml3.Peek()==53); - RakAssert(ml3.IsEmpty()==false); - RakAssert(ml3.Pop()==53); - RakAssert(ml3.IsEmpty()==true); - for (int i=0; i < 512; i++) - ml3.Push(i); - RakAssert(ml3.GetIndexOf(200)==200); - RakAssert(ml3.PeekOpposite()==511); - RakAssert(ml3.PopOpposite()==511); - RakAssert(ml3.PeekOpposite()==510); - RakAssert(ml3.Peek()==0); - ml3.ReverseList(); - for (int i=0; i < 511; i++) - RakAssert(ml3[i]==511-1-i); - RakAssert(ml3.PeekOpposite()==0); - RakAssert(ml3.Peek()==510); - oldSize = ml3.GetSize(); - ml3.RemoveAtIndex(0); - RakAssert(ml3.GetSize()==oldSize-1); - RakAssert(ml3.Peek()==509); - RakAssert(ml3.PeekOpposite()==0); - ml3.Clear(_FILE_AND_LINE_); - RakAssert(ml3.IsEmpty()==true); - - ml3.PushOpposite(100); - ml3.PushOpposite(50); - ml3.PushOpposite(150); - ml3.PushOpposite(25); - ml3.PushOpposite(175); - ml3.Sort(true); - RakAssert(ml3[0]==25); - RakAssert(ml3[1]==50); - RakAssert(ml3[2]==100); - RakAssert(ml3[3]==150); - RakAssert(ml3[4]==175); - RakAssert(ml3.GetIndexOf(25)==0); - RakAssert(ml3.GetIndexOf(50)==1); - RakAssert(ml3.GetIndexOf(100)==2); - RakAssert(ml3.GetIndexOf(150)==3); - RakAssert(ml3.GetIndexOf(175)==4); - ml3.Clear(_FILE_AND_LINE_); - - ml3.PushOpposite(1); - ml3.PushOpposite(2); - ml3.PushOpposite(3); - ml3.PushOpposite(4); - ml3.PushOpposite(5); - ml3.Sort(true); - RakAssert(ml3[0]==1); - RakAssert(ml3[1]==2); - RakAssert(ml3[2]==3); - RakAssert(ml3[3]==4); - RakAssert(ml3[4]==5); - RakAssert(ml3.GetIndexOf(1)==0); - RakAssert(ml3.GetIndexOf(2)==1); - RakAssert(ml3.GetIndexOf(3)==2); - RakAssert(ml3.GetIndexOf(4)==3); - RakAssert(ml3.GetIndexOf(5)==4); - ml3.Clear(_FILE_AND_LINE_); - - ml3.PushOpposite(5); - ml3.PushOpposite(4); - ml3.PushOpposite(3); - ml3.PushOpposite(2); - ml3.PushOpposite(1); - ml3.Sort(true); - RakAssert(ml3[0]==1); - RakAssert(ml3[1]==2); - RakAssert(ml3[2]==3); - RakAssert(ml3[3]==4); - RakAssert(ml3[4]==5); - RakAssert(ml3.GetIndexOf(1)==0); - RakAssert(ml3.GetIndexOf(2)==1); - RakAssert(ml3.GetIndexOf(3)==2); - RakAssert(ml3.GetIndexOf(4)==3); - RakAssert(ml3.GetIndexOf(5)==4); - ml3.Sort(true); - RakAssert(ml3[0]==1); - RakAssert(ml3[1]==2); - RakAssert(ml3[2]==3); - RakAssert(ml3[3]==4); - RakAssert(ml3[4]==5); - RakAssert(ml3.GetIndexOf(1)==0); - RakAssert(ml3.GetIndexOf(2)==1); - RakAssert(ml3.GetIndexOf(3)==2); - RakAssert(ml3.GetIndexOf(4)==3); - RakAssert(ml3.GetIndexOf(5)==4); - - ml3.SetSortOrder(false); - ml3.Sort(false); - RakAssert(ml3[0]==5); - RakAssert(ml3[1]==4); - RakAssert(ml3[2]==3); - RakAssert(ml3[3]==2); - RakAssert(ml3[4]==1); - RakAssert(ml3.GetIndexOf(1)==4); - RakAssert(ml3.GetIndexOf(2)==3); - RakAssert(ml3.GetIndexOf(3)==2); - RakAssert(ml3.GetIndexOf(4)==1); - RakAssert(ml3.GetIndexOf(5)==0); - - ml3.Clear(_FILE_AND_LINE_); - - DataStructures::Multilist ml4; - ml4.Reallocate(64); - RakAssert(ml4.IsEmpty()); - ml4.Push(53); - RakAssert(ml4.Peek()==53); - RakAssert(ml4.IsEmpty()==false); - RakAssert(ml4.Pop()==53); - RakAssert(ml4.IsEmpty()==true); - for (int i=0; i < 512; i++) - ml4.Push(i); - RakAssert(ml4.GetIndexOf(200)==200); - RakAssert(ml4.PeekOpposite()==0); - RakAssert(ml4.PopOpposite()==0); - RakAssert(ml4.PeekOpposite()==1); - RakAssert(ml4.Peek()==511); - ml4.ReverseList(); - for (int i=0; i < 511; i++) - RakAssert(ml4[i]==511-i); - RakAssert(ml4.PeekOpposite()==511); - RakAssert(ml4.Peek()==1); - oldSize = ml4.GetSize(); - ml4.RemoveAtIndex(0); - RakAssert(ml4.GetSize()==oldSize-1); - RakAssert(ml4.Peek()==1); - RakAssert(ml4.PeekOpposite()==510); - ml4.Clear(_FILE_AND_LINE_); - RakAssert(ml4.IsEmpty()==true); - - DataStructures::Multilist ml5; - - for (int i=0; i < 16; i++) - { - KeyAndValue *kav = new KeyAndValue; - kav->key=i; - kav->value=i+100; - ml5.Push(kav,kav->key); - } - - RakAssert(ml5.GetIndexOf(0)==0); - RakAssert(ml5.GetIndexOf(5)==5); - RakAssert(ml5.GetIndexOf(15)==15); - RakAssert(ml5.GetIndexOf(16)==-1); - ml5.RemoveAtKey(0,true); - RakAssert(ml5.GetIndexOf(1)==0); - KeyAndValue *iPtr = ml5.GetPtr(5); - RakAssert(iPtr); - RakAssert(iPtr->value=105); - iPtr = ml5.GetPtr(1234); - RakAssert(iPtr==0); - ml5.ForEach(DataStructures::DeletePtr); - - - DataStructures::Multilist ml6; - ml6.Push(2); - ml6.Push(1); - ml6.Push(6); - ml6.Push(3); - RakAssert(ml6.Peek()==3); - ml6.SetMultilistType(ML_STACK); - RakAssert(ml6.Peek()==3); - ml6.SetMultilistType(ML_QUEUE); - RakAssert(ml6.Peek()==2); - ml6.SetMultilistType(ML_ORDERED_LIST); - RakAssert(ml6.Peek()=6); - ml6.SetMultilistType(ML_STACK); - RakAssert(ml6.Peek()==6); - ml6.SetMultilistType(ML_QUEUE); - RakAssert(ml6.Peek()==1); -} - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif -*/ - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_Multilist.h +/// \internal +/// \brief ADT that can represent an unordered list, ordered list, stack, or queue with a common interface +/// + +#pragma once + +#include "RakAssert.h" +#include // memmove +#include "Export.h" +#include "RakMemoryOverride.h" +#include "NativeTypes.h" + + +#ifdef _MSC_VER +#pragma warning( push ) +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#pragma warning( disable : 4512 ) // warning C4512: assignment operator could not be generated +#endif + +/// What algorithm to use to store the data for the Multilist +enum MultilistType +{ + /// Removing from the middle of the list will swap the end of the list rather than shift the elements. Push and Pop operate on the tail. + ML_UNORDERED_LIST, + /// A normal list, with the list order preserved. Push and Pop operate on the tail. + ML_STACK, + /// A queue. Push and Pop operate on the head + ML_QUEUE, + /// A list that is always kept in order. Elements must be unique, and compare against each other consistently using <, ==, and > + ML_ORDERED_LIST, + /// A list whose type can change at runtime + ML_VARIABLE_DURING_RUNTIME +}; + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + /// Can be used with Multilist::ForEach + /// Assuming the Multilist holds pointers, will delete those pointers + template + void DeletePtr_RakNet(templateType &ptr, const char *file, unsigned int line ) {RakNet::OP_DELETE(ptr, file, line);} + + /// Can be used with Multilist::ForEach + /// Assuming the Multilist holds pointers, will delete those pointers + template + void DeletePtr(templateType &ptr) {delete ptr;} + + /// The following is invalid. + /// bool operator<( const MyClass *myClass, const int &inputKey ) {return myClass->value < inputKey;} + /// At least one type has to be a reference to a class + /// MLKeyRef is a helper class to turn a native type into a class, so you can compare that native type against a pointer to a different class + /// Used for he Multilist, when _DataType != _KeyType + template < class templateType > + class MLKeyRef + { + public: + MLKeyRef(const templateType& input) : val(input) {} + const templateType &Get(void) const {return val;} + bool operator<( const templateType &right ) {return val < right;} + bool operator>( const templateType &right ) {return val > right;} + bool operator==( const templateType &right ) {return val == right;} + protected: + const templateType &val; + }; + + /// For the Multilist, when _DataType != _KeyType, you must define the comparison operators between the key and the data + /// This is non-trivial due to the need to use MLKeyRef in case the type held is a pointer to a structure or class and the key type is not a class + /// For convenience, this macro will implement the comparison operators under the following conditions + /// 1. _DataType is a pointer to a class or structure + /// 2. The key is a member variable of _DataType + #define DEFINE_MULTILIST_PTR_TO_MEMBER_COMPARISONS( _CLASS_NAME_, _KEY_TYPE_, _MEMBER_VARIABLE_NAME_ ) \ + bool operator<( const DataStructures::MLKeyRef<_KEY_TYPE_> &inputKey, const _CLASS_NAME_ *cls ) {return inputKey.Get() < cls->_MEMBER_VARIABLE_NAME_;} \ + bool operator>( const DataStructures::MLKeyRef<_KEY_TYPE_> &inputKey, const _CLASS_NAME_ *cls ) {return inputKey.Get() > cls->_MEMBER_VARIABLE_NAME_;} \ + bool operator==( const DataStructures::MLKeyRef<_KEY_TYPE_> &inputKey, const _CLASS_NAME_ *cls ) {return inputKey.Get() == cls->_MEMBER_VARIABLE_NAME_;} + + typedef uint32_t DefaultIndexType; + + /// \brief The multilist, representing an abstract data type that generally holds lists. + /// \param[in] _MultilistType What type of list this is, \sa MultilistType + /// \param[in] _DataType What type of data this list holds. + /// \param[in] _KeyType If a function takes a key to sort on, what type of key this is. The comparison operator between _DataType and _KeyType must be defined + /// \param[in] _IndexType What variable type to use for indices + template + class RAK_DLL_EXPORT Multilist + { + public: + Multilist(); + ~Multilist(); + Multilist( const Multilist& source ); + Multilist& operator= ( const Multilist& source ); + _DataType& operator[] ( const _IndexType position ) const; + /// Unordered list, stack is LIFO + /// QUEUE is FIFO + /// Ordered list is inserted in order + void Push(const _DataType &d, const char *file=__FILE__, unsigned int line=__LINE__ ); + void Push(const _DataType &d, const _KeyType &key, const char *file=__FILE__, unsigned int line=__LINE__ ); + + /// \brief Gets or removes and gets an element from the list, according to the same rules as Push(). + /// Ordered list is LIFO for the purposes of Pop and Peek. + _DataType &Pop(const char *file=__FILE__, unsigned int line=__LINE__); + _DataType &Peek(void) const; + + /// \brief Same as Push(), except FIFO and LIFO are reversed. + /// Ordered list still inserts in order. + void PushOpposite(const _DataType &d, const char *file=__FILE__, unsigned int line=__LINE__ ); + void PushOpposite(const _DataType &d, const _KeyType &key, const char *file=__FILE__, unsigned int line=__LINE__ ); + + /// \brief Same as Pop() and Peek(), except FIFO and LIFO are reversed. + _DataType &PopOpposite(const char *file=__FILE__, unsigned int line=__LINE__); + _DataType &PeekOpposite(void) const; + + /// \brief Stack,Queue: Inserts at index indicated, elements are shifted. + /// Ordered list: Inserts, position is ignored + void InsertAtIndex(const _DataType &d, _IndexType index, const char *file=__FILE__, unsigned int line=__LINE__); + + /// \brief Unordered list, removes at index indicated, swaps last element with that element. + /// Otherwise, array is shifted left to overwrite removed element + /// \details Index[0] returns the same as Pop() for a queue. + /// Same as PopOpposite() for the list and ordered list + void RemoveAtIndex(_IndexType position, const char *file=__FILE__, unsigned int line=__LINE__); + + /// \brief Find the index of \a key, and remove at that index. + bool RemoveAtKey(_KeyType key, bool assertIfDoesNotExist, const char *file=__FILE__, unsigned int line=__LINE__); + + /// \brief Finds the index of \a key. Return -1 if the key is not found. + _IndexType GetIndexOf(_KeyType key) const; + + /// \brief Returns where in the list we should insert the item, to preserve list order. + /// Returns -1 if the item is already in the list + _IndexType GetInsertionIndex(_KeyType key) const; + + /// \brief Finds the index of \a key. Return 0 if the key is not found. Useful if _DataType is always non-zero pointers. + _DataType GetPtr(_KeyType key) const; + + /// \brief Iterate over the list, calling the function pointer on each element. + void ForEach(void (*func)(_DataType &item, const char *file, unsigned int line), const char *file, unsigned int line); + void ForEach(void (*func)(_DataType &item)); + + /// \brief Returns if the list is empty. + bool IsEmpty(void) const; + + /// \brief Returns the number of elements used in the list. + _IndexType GetSize(void) const; + + /// \brief Empties the list. The list is not deallocated if it is small, + /// unless \a deallocateSmallBlocks is true + void Clear( bool deallocateSmallBlocks=true, const char *file=__FILE__, unsigned int line=__LINE__ ); + + /// \brief Empties the list, first calling RakNet::OP_Delete on all items. + /// \details The list is not deallocated if it is small, unless \a deallocateSmallBlocks is true + void ClearPointers( bool deallocateSmallBlocks=true, const char *file=__FILE__, unsigned int line=__LINE__ ); + + /// \brief Empty one item from the list, first calling RakNet::OP_Delete on that item. + void ClearPointer( _KeyType key, const char *file=__FILE__, unsigned int line=__LINE__ ); + + /// \brief Reverses the elements in the list, and flips the sort order + /// returned by GetSortOrder() if IsSorted() returns true at the time the function is called + void ReverseList(void); + + /// \brief Reallocates the list to a larger size. + /// If \a size is smaller than the value returned by GetSize(), the call does nothing. + void Reallocate(_IndexType size, const char *file=__FILE__, unsigned int line=__LINE__); + + /// \brief Sorts the list unless it is an ordered list, in which it does nothing as the list is assumed to already be sorted. + /// \details However, if \a force is true, it will also resort the ordered list, useful if the comparison operator between _KeyType and _DataType would now return different results + /// Once the list is sorted, further operations to lookup by key will be log2(n) until the list is modified + void Sort(bool force); + + /// \brief Sets the list to be remembered as sorted. + /// \details Optimization if the source is sorted already + void TagSorted(void); + + /// \brief Defaults to ascending. + /// \details Used by Sort(), and by ML_ORDERED_LIST + void SetSortOrder(bool ascending); + + /// \brief Returns true if ascending. + bool GetSortOrder(void) const; + + /// \brief Returns true if the list is currently believed to be in a sorted state. + /// \details Doesn't actually check for sortedness, just if Sort() + /// was recently called, or MultilistType is ML_ORDERED_LIST + bool IsSorted(void) const; + + /// Returns what type of list this is + MultilistType GetMultilistType(void) const; + + /// \brief Changes what type of list this is. + /// \pre Template must be defined with ML_VARIABLE_DURING_RUNTIME for this to do anything + /// \param[in] mlType Any value of the enum MultilistType, except ML_VARIABLE_DURING_RUNTIME + void SetMultilistType(MultilistType newType); + + /// \brief Returns the intersection of two lists. + /// Intersection is items common to both lists. + static void FindIntersection( + Multilist& source1, + Multilist& source2, + Multilist& intersection, + Multilist& uniqueToSource1, + Multilist& uniqueToSource2); + + protected: + void ReallocateIfNeeded(const char *file, unsigned int line); + void DeallocateIfNeeded(const char *file, unsigned int line); + void ReallocToSize(_IndexType newAllocationSize, const char *file, unsigned int line); + void ReverseListInternal(void); + void InsertInOrderedList(const _DataType &d, const _KeyType &key); + _IndexType GetIndexFromKeyInSortedList(const _KeyType &key, bool *objectExists) const; + void InsertShiftArrayRight(const _DataType &d, _IndexType index); + void DeleteShiftArrayLeft(_IndexType index); + void QSortAscending(_IndexType left, _IndexType right); + void QSortDescending(_IndexType left, _IndexType right); + void CopySource( const Multilist& source ); + + /// An array of user values + _DataType* data; + + /// Number of elements in the list + _IndexType dataSize; + + /// Size of \a array + _IndexType allocationSize; + + /// Array index for the head of the queue + _IndexType queueHead; + + /// Array index for the tail of the queue + _IndexType queueTail; + + /// How many bytes the user chose to preallocate + /// Won't automatically deallocate below this + _IndexType preallocationSize; + + enum + { + ML_UNSORTED, + ML_SORTED_ASCENDING, + ML_SORTED_DESCENDING + } sortState; + + bool ascendingSort; + + // In case we are using the variable type multilist + MultilistType variableMultilistType; + }; + + template + Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Multilist() + { + data=0; + dataSize=0; + allocationSize=0; + ascendingSort=true; + sortState=ML_UNSORTED; + queueHead=0; + queueTail=0; + preallocationSize=0; + + if (_MultilistType==ML_ORDERED_LIST) + sortState=ML_SORTED_ASCENDING; + else + sortState=ML_UNSORTED; + + if (_MultilistType==ML_VARIABLE_DURING_RUNTIME) + variableMultilistType=ML_UNORDERED_LIST; + } + + template + Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::~Multilist() + { + if (data!=0) + RakNet::OP_DELETE_ARRAY(data, _FILE_AND_LINE_); + } + + template + Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Multilist( const Multilist& source ) + { + CopySource(source); + } + + template + Multilist<_MultilistType, _DataType, _KeyType, _IndexType>& Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::operator= ( const Multilist& source ) + { + Clear(true); + CopySource(source); + return *this; + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::CopySource( const Multilist& source ) + { + dataSize=source.GetSize(); + ascendingSort=source.ascendingSort; + sortState=source.sortState; + queueHead=0; + queueTail=dataSize; + preallocationSize=source.preallocationSize; + variableMultilistType=source.variableMultilistType; + if (source.data==0) + { + data=0; + allocationSize=0; + } + else + { + allocationSize=dataSize; + data = RakNet::OP_NEW_ARRAY<_DataType>(dataSize,_FILE_AND_LINE_); + _IndexType i; + for (i=0; i < dataSize; i++) + data[i]=source[i]; + } + } + + template + _DataType& Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::operator[] ( const _IndexType position ) const + { + RakAssert(position= allocationSize ) + return data[ queueHead + position - allocationSize ]; + else + return data[ queueHead + position ]; + } + + return data[position]; + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Push(const _DataType &d, const char *file, unsigned int line ) + { + Push(d,d,file,line); + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Push(const _DataType &d, const _KeyType &key, const char *file, unsigned int line ) + { + ReallocateIfNeeded(file,line); + + if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK) + { + data[dataSize]=d; + dataSize++; + } + else if (GetMultilistType()==ML_QUEUE) + { + data[queueTail++] = d; + + if ( queueTail == allocationSize ) + queueTail = 0; + dataSize++; + } + else + { + RakAssert(GetMultilistType()==ML_ORDERED_LIST); + InsertInOrderedList(d,key); + } + + if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_QUEUE) + { + // Break sort if no longer sorted + if (sortState!=ML_UNSORTED && dataSize>1) + { + if (ascendingSort) + { + if ( MLKeyRef<_KeyType>(key) < operator[](dataSize-2) ) + sortState=ML_UNSORTED; + } + else + { + if ( MLKeyRef<_KeyType>(key) > operator[](dataSize-2) ) + sortState=ML_UNSORTED; + } + + sortState=ML_UNSORTED; + } + } + } + + template + _DataType &Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Pop(const char *file, unsigned int line) + { + RakAssert(IsEmpty()==false); + DeallocateIfNeeded(file,line); + if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST) + { + dataSize--; + return data[dataSize]; + } + else + { + RakAssert(GetMultilistType()==ML_QUEUE); + + if ( ++queueHead == allocationSize ) + queueHead = 0; + + if ( queueHead == 0 ) + return data[ allocationSize -1 ]; + + dataSize--; + return data[ queueHead -1 ]; + } + } + + template + _DataType &Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Peek(void) const + { + RakAssert(IsEmpty()==false); + if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST) + { + return data[dataSize-1]; + } + else + { + RakAssert(GetMultilistType()==ML_QUEUE); + return data[ queueHead ]; + } + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::PushOpposite(const _DataType &d, const char *file, unsigned int line ) + { + PushOpposite(d,d,file,line); + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::PushOpposite(const _DataType &d, const _KeyType &key, const char *file, unsigned int line ) + { + ReallocateIfNeeded(file,line); + + // Unordered list Push at back + if (GetMultilistType()==ML_UNORDERED_LIST) + { + data[dataSize]=d; + dataSize++; + } + else if (GetMultilistType()==ML_STACK) + { + // Stack push at front of the list, instead of back as normal + InsertAtIndex(d,0,file,line); + } + else if (GetMultilistType()==ML_QUEUE) + { + // Queue push at front of the list, instead of back as normal + InsertAtIndex(d,0,file,line); + } + else + { + RakAssert(GetMultilistType()==ML_ORDERED_LIST); + InsertInOrderedList(d,key); + } + + if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_QUEUE) + { + // Break sort if no longer sorted + if (sortState!=ML_UNSORTED && dataSize>1) + { + if (ascendingSort) + { + if ( MLKeyRef<_KeyType>(key) > operator[](1) ) + sortState=ML_UNSORTED; + } + else + { + if ( MLKeyRef<_KeyType>(key) < operator[](1) ) + sortState=ML_UNSORTED; + } + } + } + } + + template + _DataType &Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::PopOpposite(const char *file, unsigned int line) + { + RakAssert(IsEmpty()==false); + if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST) + { + // Copy leftmost to end + ReallocateIfNeeded(file,line); + data[dataSize]=data[0]; + DeleteShiftArrayLeft(0); + --dataSize; + // Assuming still leaves at least one element past the end of the list allocated + DeallocateIfNeeded(file,line); + // Return end + return data[dataSize+1]; + } + else + { + RakAssert(GetMultilistType()==ML_QUEUE); + // Deallocate first, since we are returning off the existing list + DeallocateIfNeeded(file,line); + dataSize--; + + if (queueTail==0) + queueTail=allocationSize-1; + else + --queueTail; + + return data[queueTail]; + } + } + + template + _DataType &Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::PeekOpposite(void) const + { + RakAssert(IsEmpty()==false); + if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST) + { + return data[0]; + } + else + { + RakAssert(GetMultilistType()==ML_QUEUE); + _IndexType priorIndex; + if (queueTail==0) + priorIndex=allocationSize-1; + else + priorIndex=queueTail-1; + + return data[priorIndex]; + } + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::InsertAtIndex(const _DataType &d, _IndexType index, const char *file, unsigned int line) + { + ReallocateIfNeeded(file,line); + + if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST) + { + if (index>=dataSize) + { + // insert at end + data[dataSize]=d; + + dataSize++; + } + else + { + // insert at index + InsertShiftArrayRight(d,index); + } + } + else + { + data[queueTail++] = d; + + if ( queueTail == allocationSize ) + queueTail = 0; + + ++dataSize; + + if (dataSize==1) + return; + + _IndexType writeIndex, readIndex, trueWriteIndex, trueReadIndex; + writeIndex=dataSize-1; + readIndex=writeIndex-1; + while (readIndex >= index) + { + if ( queueHead + writeIndex >= allocationSize ) + trueWriteIndex = queueHead + writeIndex - allocationSize; + else + trueWriteIndex = queueHead + writeIndex; + + if ( queueHead + readIndex >= allocationSize ) + trueReadIndex = queueHead + readIndex - allocationSize; + else + trueReadIndex = queueHead + readIndex; + + data[trueWriteIndex]=data[trueReadIndex]; + + if (readIndex==0) + break; + writeIndex--; + readIndex--; + } + + if ( queueHead + index >= allocationSize ) + trueWriteIndex = queueHead + index - allocationSize; + else + trueWriteIndex = queueHead + index; + + data[trueWriteIndex]=d; + } + + if (_MultilistType!=ML_ORDERED_LIST) + sortState=ML_UNSORTED; + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::RemoveAtIndex(_IndexType position, const char *file, unsigned int line) + { + RakAssert(position < dataSize); + RakAssert(IsEmpty()==false); + + if (GetMultilistType()==ML_UNORDERED_LIST) + { + // Copy tail to current + data[position]=data[dataSize-1]; + } + else if (GetMultilistType()==ML_STACK || GetMultilistType()==ML_ORDERED_LIST) + { + DeleteShiftArrayLeft(position); + } + else + { + RakAssert(GetMultilistType()==ML_QUEUE); + + _IndexType index, next; + + if ( queueHead + position >= allocationSize ) + index = queueHead + position - allocationSize; + else + index = queueHead + position; + + next = index + 1; + + if ( next == allocationSize ) + next = 0; + + while ( next != queueTail ) + { + // Overwrite the previous element + data[ index ] = data[ next ]; + index = next; + //next = (next + 1) % allocationSize; + + if ( ++next == allocationSize ) + next = 0; + } + + // Move the queueTail back + if ( queueTail == 0 ) + queueTail = allocationSize - 1; + else + --queueTail; + } + + + dataSize--; + DeallocateIfNeeded(file,line); + } + + template + bool Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::RemoveAtKey(_KeyType key, bool assertIfDoesNotExist, const char *file, unsigned int line) + { + _IndexType index = GetIndexOf(key); + if (index==(_IndexType)-1) + { + RakAssert(assertIfDoesNotExist==false && "RemoveAtKey element not found"); + return false; + } + RemoveAtIndex(index,file,line); + return true; + } + + template + _IndexType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetIndexOf(_KeyType key) const + { + _IndexType i; + if (IsSorted()) + { + bool objectExists; + i=GetIndexFromKeyInSortedList(key, &objectExists); + if (objectExists) + return i; + return (_IndexType)-1; + } + else if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK) + { + for (i=0; i < dataSize; i++) + { + if (MLKeyRef<_KeyType>(key)==data[i]) + return i; + } + return (_IndexType)-1; + } + else + { + RakAssert( GetMultilistType()==ML_QUEUE ); + + for (i=0; i < dataSize; i++) + { + if (MLKeyRef<_KeyType>(key)==operator[](i)) + return i; + } + return (_IndexType)-1; + } + } + + template + _IndexType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetInsertionIndex(_KeyType key) const + { + _IndexType i; + if (IsSorted()) + { + bool objectExists; + i=GetIndexFromKeyInSortedList(key, &objectExists); + if (objectExists) + return (_IndexType)-1; + return i; + } + else if (GetMultilistType()==ML_UNORDERED_LIST || GetMultilistType()==ML_STACK) + { + for (i=0; i < dataSize; i++) + { + if (MLKeyRef<_KeyType>(key)==data[i]) + return (_IndexType)-1; + } + return dataSize; + } + else + { + RakAssert( GetMultilistType()==ML_QUEUE ); + + for (i=0; i < dataSize; i++) + { + if (MLKeyRef<_KeyType>(key)==operator[](i)) + return (_IndexType)-1; + } + return dataSize; + } + } + + template + _DataType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetPtr(_KeyType key) const + { + _IndexType i = GetIndexOf(key); + if (i==(_IndexType)-1) + return 0; + return data[i]; + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ForEach(void (*func)(_DataType &item, const char *file, unsigned int line), const char *file, unsigned int line) + { + _IndexType i; + for (i=0; i < dataSize; i++) + func(operator[](i), file, line); + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ForEach(void (*func)(_DataType &item)) + { + _IndexType i; + for (i=0; i < dataSize; i++) + func(operator[](i)); + } + + template + bool Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::IsEmpty(void) const + { + return dataSize==0; + } + + template + _IndexType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetSize(void) const + { + return dataSize; + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Clear( bool deallocateSmallBlocks, const char *file, unsigned int line ) + { + dataSize=0; + if (GetMultilistType()==ML_ORDERED_LIST) + if (ascendingSort) + sortState=ML_SORTED_ASCENDING; + else + sortState=ML_SORTED_DESCENDING; + else + sortState=ML_UNSORTED; + queueHead=0; + queueTail=0; + + if (deallocateSmallBlocks && allocationSize < 128 && data) + { + RakNet::OP_DELETE_ARRAY(data,file,line); + data=0; + allocationSize=0; + } + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ClearPointers( bool deallocateSmallBlocks, const char *file, unsigned int line ) + { + _IndexType i; + for (i=0; i < dataSize; i++) + RakNet::OP_DELETE(operator[](i), file, line); + Clear(deallocateSmallBlocks, file, line); + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ClearPointer( _KeyType key, const char *file, unsigned int line ) + { + _IndexType i; + i = GetIndexOf(key); + if (i!=-1) + { + RakNet::OP_DELETE(operator[](i), file, line); + RemoveAtIndex(i); + } + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ReverseList(void) + { + if (IsSorted()) + ascendingSort=!ascendingSort; + + ReverseListInternal(); + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Reallocate(_IndexType size, const char *file, unsigned int line) + { + _IndexType newAllocationSize; + if (size < dataSize) + newAllocationSize=dataSize; + else + newAllocationSize=size; + preallocationSize=size; + ReallocToSize(newAllocationSize,file,line); + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::Sort(bool force) + { + if (IsSorted() && force==false) + return; + + if (dataSize>1) + { + if (ascendingSort) + QSortAscending(0,dataSize-1); + else + QSortDescending(0,dataSize-1); + } + + TagSorted(); + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::TagSorted(void) + { + if (ascendingSort) + sortState=ML_SORTED_ASCENDING; + else + sortState=ML_SORTED_DESCENDING; + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::QSortAscending(_IndexType leftEdge, _IndexType rightEdge) + { + _DataType temp; + _IndexType left=leftEdge; + _IndexType right=rightEdge; + _IndexType pivotIndex=left++; + + while (left data[pivotIndex]) + { + --left; + + data[pivotIndex]=data[left]; + data[left]=temp; + } + else + { + data[pivotIndex]=data[left]; + data[left]=temp; + + --left; + } + + if (left!=leftEdge) + QSortAscending(leftEdge, left); + + if (right!=rightEdge) + QSortAscending(right, rightEdge); + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::QSortDescending(_IndexType leftEdge, _IndexType rightEdge) + { + _DataType temp; + _IndexType left=leftEdge; + _IndexType right=rightEdge; + _IndexType pivotIndex=left++; + + while (left= data[pivotIndex]) + { + ++left; + } + else + { + temp=data[left]; + data[left]=data[right]; + data[right]=temp; + --right; + } + } + + temp=data[pivotIndex]; + + // Move pivot to center + if (data[left] < data[pivotIndex]) + { + --left; + + data[pivotIndex]=data[left]; + data[left]=temp; + } + else + { + data[pivotIndex]=data[left]; + data[left]=temp; + + --left; + } + + if (left!=leftEdge) + QSortDescending(leftEdge, left); + + if (right!=rightEdge) + QSortDescending(right, rightEdge); + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::SetSortOrder(bool ascending) + { + if (ascendingSort!=ascending && IsSorted()) + { + ascendingSort=ascending; + // List is sorted, and the sort order has changed. So reverse the list + ReverseListInternal(); + } + else + ascendingSort=ascending; + } + + template + bool Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetSortOrder(void) const + { + return ascendingSort; + } + + template + bool Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::IsSorted(void) const + { + return GetMultilistType()==ML_ORDERED_LIST || sortState!=ML_UNSORTED; + } + + template + MultilistType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetMultilistType(void) const + { + if (_MultilistType==ML_VARIABLE_DURING_RUNTIME) + return variableMultilistType; + return _MultilistType; + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::SetMultilistType(MultilistType newType) + { + RakAssert(_MultilistType==ML_VARIABLE_DURING_RUNTIME); + switch (variableMultilistType) + { + case ML_UNORDERED_LIST: + switch (newType) + { + case ML_UNORDERED_LIST: + // No change + break; + case ML_STACK: + // Same data format + break; + case ML_QUEUE: + queueHead=0; + queueTail=dataSize; + break; + case ML_ORDERED_LIST: + Sort(false); + break; + } + break; + case ML_STACK: + switch (newType) + { + case ML_UNORDERED_LIST: + // Same data format + break; + case ML_STACK: + // No change + break; + case ML_QUEUE: + queueHead=0; + queueTail=dataSize; + break; + case ML_ORDERED_LIST: + Sort(false); + break; + } + break; + case ML_QUEUE: + switch (newType) + { + case ML_UNORDERED_LIST: + case ML_STACK: + case ML_ORDERED_LIST: + if (queueTail < queueHead) + { + // Realign data if wrapped + ReallocToSize(dataSize, _FILE_AND_LINE_); + } + else + { + // Else can just copy starting at head + _IndexType i; + for (i=0; i < dataSize; i++) + data[i]=operator[](i); + } + if (newType==ML_ORDERED_LIST) + Sort(false); + break; + case ML_QUEUE: + // No change + break; + } + break; + case ML_ORDERED_LIST: + switch (newType) + { + case ML_UNORDERED_LIST: + case ML_STACK: + case ML_QUEUE: + // Same data format + // Tag as sorted + if (ascendingSort) + sortState=ML_SORTED_ASCENDING; + else + sortState=ML_SORTED_DESCENDING; + if (newType==ML_QUEUE) + { + queueHead=0; + queueTail=dataSize; + } + break; + case ML_ORDERED_LIST: + // No change + break; + } + break; + } + + variableMultilistType=newType; + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::FindIntersection( + Multilist& source1, + Multilist& source2, + Multilist& intersection, + Multilist& uniqueToSource1, + Multilist& uniqueToSource2) + { + _IndexType index1=0, index2=0; + source1.SetSortOrder(true); + source2.SetSortOrder(true); + source1.Sort(false); + source2.Sort(false); + intersection.Clear(true,_FILE_AND_LINE_); + uniqueToSource1.Clear(true,_FILE_AND_LINE_); + uniqueToSource2.Clear(true,_FILE_AND_LINE_); + + while (index1 < source1.GetSize() && index2 < source2.GetSize()) + { + if (source1[index1] + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ReallocateIfNeeded(const char *file, unsigned int line) + { + if (dataSize65536) + newAllocationSize=allocationSize+65536; + else + { + newAllocationSize=allocationSize<<1; // * 2 + // Protect against underflow + if (newAllocationSize < allocationSize) + newAllocationSize=allocationSize+65536; + } + + ReallocToSize(newAllocationSize,file,line); + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::DeallocateIfNeeded(const char *file, unsigned int line) + { + if (allocationSize<512) + return; + if (dataSize >= allocationSize/3 ) + return; + if (dataSize <= preallocationSize ) + return; + + _IndexType newAllocationSize = dataSize<<1; // * 2 + + ReallocToSize(newAllocationSize,file,line); + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ReallocToSize(_IndexType newAllocationSize, const char *file, unsigned int line) + { + _DataType* newData = RakNet::OP_NEW_ARRAY<_DataType>(newAllocationSize,file,line); + _IndexType i; + for (i=0; i < dataSize; i++) + newData[i]=operator[](i); + if (dataSize>0) + { + RakNet::OP_DELETE_ARRAY(data,file,line); + if (GetMultilistType()==ML_QUEUE) + { + queueHead=0; + queueTail=dataSize; + } + } + data=newData; + allocationSize=newAllocationSize; + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::ReverseListInternal(void) + { + _DataType temp; + _IndexType i; + for (i=0; i < dataSize/2; i++) + { + temp=operator[](i); + operator[](i)=operator[](dataSize-1-i); + operator[](dataSize-1-i)=temp; + } + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::InsertInOrderedList(const _DataType &d, const _KeyType &key) + { + RakAssert(GetMultilistType()==ML_ORDERED_LIST); + + bool objectExists; + _IndexType index; + index = GetIndexFromKeyInSortedList(key, &objectExists); + + // if (objectExists) + // { + // Ordered list only allows unique insertions + // RakAssert("Duplicate insertion into ordered list" && false); + // return; + // } + + if (index>=dataSize) + { + // insert at end + data[dataSize]=d; + dataSize++; + } + else + { + // insert at index + InsertShiftArrayRight(d,index); + } + } + + template + _IndexType Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::GetIndexFromKeyInSortedList(const _KeyType &key, bool *objectExists) const + { + RakAssert(IsSorted()); + _IndexType index, upperBound, lowerBound; + + if (dataSize==0) + { + *objectExists=false; + return 0; + } + + upperBound=dataSize-1; + lowerBound=0; + index = dataSize/2; + +#ifdef _MSC_VER + #pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + while (1) + { + if (MLKeyRef<_KeyType>(key) > operator[](index) ) + { + if (ascendingSort) + lowerBound=index+1; + else + upperBound=index-1; + } + else if (MLKeyRef<_KeyType>(key) < operator[](index) ) + { + if (ascendingSort) + upperBound=index-1; + else + lowerBound=index+1; + } + else + { + // == + *objectExists=true; + return index; + } + + index=lowerBound+(upperBound-lowerBound)/2; + + if (lowerBound>upperBound || upperBound==(_IndexType)-1) + { + *objectExists=false; + return lowerBound; // No match + } + } + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::InsertShiftArrayRight(const _DataType &d, _IndexType index) + { + RakAssert(_MultilistType!=ML_QUEUE); + + // Move the elements in the list to make room + _IndexType i; + for ( i = dataSize; i != index; i-- ) + data[ i ] = data[ i - 1 ]; + + // Insert the new item at the correct spot + data[ index ] = d; + + ++dataSize; + } + + template + void Multilist<_MultilistType, _DataType, _KeyType, _IndexType>::DeleteShiftArrayLeft( _IndexType index ) + { + RakAssert(index < dataSize); + RakAssert(_MultilistType!=ML_QUEUE); + + _IndexType i; + for ( i = index; i < dataSize-1; i++ ) + data[i]=data[i+1]; + } +}; + +/* +struct KeyAndValue +{ + int key; + short value; +}; + +DEFINE_MULTILIST_PTR_TO_MEMBER_COMPARISONS(KeyAndValue,int,key) + +void MultilistUnitTest(void) +{ + DataStructures::DefaultIndexType oldSize; + DataStructures::Multilist ml1; + ml1.Reallocate(64); + RakAssert(ml1.IsEmpty()); + ml1.Push(53); + RakAssert(ml1.Peek()==53); + RakAssert(ml1.IsEmpty()==false); + RakAssert(ml1.Pop()==53); + RakAssert(ml1.IsEmpty()==true); + for (int i=0; i < 512; i++) + ml1.Push(i); + RakAssert(ml1.GetIndexOf(200)==200); + RakAssert(ml1.PeekOpposite()==0); + RakAssert(ml1.PopOpposite()==0); + RakAssert(ml1.PeekOpposite()==1); + RakAssert(ml1.Peek()==511); + ml1.ReverseList(); + for (int i=0; i < 511; i++) + RakAssert(ml1[i]==511-i); + RakAssert(ml1.PeekOpposite()==511); + RakAssert(ml1.Peek()==1); + oldSize = ml1.GetSize(); + ml1.RemoveAtIndex(0); + RakAssert(ml1.GetSize()==oldSize-1); + RakAssert(ml1.PeekOpposite()==1); + ml1.Clear(_FILE_AND_LINE_); + RakAssert(ml1.IsEmpty()==true); + + ml1.Sort(true); + ml1.Clear(_FILE_AND_LINE_); + + ml1.Push(100); + ml1.Sort(true); + ml1.Clear(_FILE_AND_LINE_); + + ml1.Push(50); + ml1.Push(100); + ml1.Sort(true); + ml1.Clear(_FILE_AND_LINE_); + + ml1.Push(100); + ml1.Push(50); + ml1.Sort(true); + ml1.Clear(_FILE_AND_LINE_); + + ml1.Push(100); + ml1.Push(50); + ml1.Push(150); + ml1.Push(25); + ml1.Push(175); + ml1.Sort(true); + RakAssert(ml1[0]==25); + RakAssert(ml1[1]==50); + RakAssert(ml1[2]==100); + RakAssert(ml1[3]==150); + RakAssert(ml1[4]==175); + RakAssert(ml1.GetIndexOf(25)==0); + RakAssert(ml1.GetIndexOf(50)==1); + RakAssert(ml1.GetIndexOf(100)==2); + RakAssert(ml1.GetIndexOf(150)==3); + RakAssert(ml1.GetIndexOf(175)==4); + ml1.Clear(_FILE_AND_LINE_); + + ml1.Push(1); + ml1.Push(2); + ml1.Push(3); + ml1.Push(4); + ml1.Push(5); + ml1.Sort(true); + RakAssert(ml1[0]==1); + RakAssert(ml1[1]==2); + RakAssert(ml1[2]==3); + RakAssert(ml1[3]==4); + RakAssert(ml1[4]==5); + RakAssert(ml1.GetIndexOf(1)==0); + RakAssert(ml1.GetIndexOf(2)==1); + RakAssert(ml1.GetIndexOf(3)==2); + RakAssert(ml1.GetIndexOf(4)==3); + RakAssert(ml1.GetIndexOf(5)==4); + ml1.Clear(_FILE_AND_LINE_); + + ml1.Push(5); + ml1.Push(4); + ml1.Push(3); + ml1.Push(2); + ml1.Push(1); + ml1.Sort(true); + RakAssert(ml1[0]==1); + RakAssert(ml1[1]==2); + RakAssert(ml1[2]==3); + RakAssert(ml1[3]==4); + RakAssert(ml1[4]==5); + RakAssert(ml1.GetIndexOf(1)==0); + RakAssert(ml1.GetIndexOf(2)==1); + RakAssert(ml1.GetIndexOf(3)==2); + RakAssert(ml1.GetIndexOf(4)==3); + RakAssert(ml1.GetIndexOf(5)==4); + ml1.Sort(true); + RakAssert(ml1[0]==1); + RakAssert(ml1[1]==2); + RakAssert(ml1[2]==3); + RakAssert(ml1[3]==4); + RakAssert(ml1[4]==5); + RakAssert(ml1.GetIndexOf(1)==0); + RakAssert(ml1.GetIndexOf(2)==1); + RakAssert(ml1.GetIndexOf(3)==2); + RakAssert(ml1.GetIndexOf(4)==3); + RakAssert(ml1.GetIndexOf(5)==4); + ml1.Clear(_FILE_AND_LINE_); + + DataStructures::Multilist ml2; + ml2.Reallocate(64); + RakAssert(ml2.IsEmpty()); + ml2.Push(53); + RakAssert(ml2.Peek()==53); + RakAssert(ml2.IsEmpty()==false); + RakAssert(ml2.Pop()==53); + RakAssert(ml2.IsEmpty()==true); + for (int i=0; i < 512; i++) + ml2.Push(i); + RakAssert(ml2.GetIndexOf(200)==200); + RakAssert(ml2.PeekOpposite()==0); + RakAssert(ml2.PopOpposite()==0); + RakAssert(ml2.PeekOpposite()==1); + RakAssert(ml2.Peek()==511); + ml2.ReverseList(); + for (int i=0; i < 511; i++) + RakAssert(ml2[i]==511-i); + RakAssert(ml2.PeekOpposite()==511); + RakAssert(ml2.Peek()==1); + oldSize = ml2.GetSize(); + ml2.RemoveAtIndex(0); + RakAssert(ml2.GetSize()==oldSize-1); + RakAssert(ml2.Peek()==1); + RakAssert(ml2.PeekOpposite()==510); + ml2.Clear(_FILE_AND_LINE_); + RakAssert(ml2.IsEmpty()==true); + + DataStructures::Multilist ml3; + RakAssert(ml3.IsEmpty()); + ml3.Push(53); + RakAssert(ml3.Peek()==53); + RakAssert(ml3.IsEmpty()==false); + RakAssert(ml3.Pop()==53); + RakAssert(ml3.IsEmpty()==true); + for (int i=0; i < 512; i++) + ml3.Push(i); + RakAssert(ml3.GetIndexOf(200)==200); + RakAssert(ml3.PeekOpposite()==511); + RakAssert(ml3.PopOpposite()==511); + RakAssert(ml3.PeekOpposite()==510); + RakAssert(ml3.Peek()==0); + ml3.ReverseList(); + for (int i=0; i < 511; i++) + RakAssert(ml3[i]==511-1-i); + RakAssert(ml3.PeekOpposite()==0); + RakAssert(ml3.Peek()==510); + oldSize = ml3.GetSize(); + ml3.RemoveAtIndex(0); + RakAssert(ml3.GetSize()==oldSize-1); + RakAssert(ml3.Peek()==509); + RakAssert(ml3.PeekOpposite()==0); + ml3.Clear(_FILE_AND_LINE_); + RakAssert(ml3.IsEmpty()==true); + + ml3.PushOpposite(100); + ml3.PushOpposite(50); + ml3.PushOpposite(150); + ml3.PushOpposite(25); + ml3.PushOpposite(175); + ml3.Sort(true); + RakAssert(ml3[0]==25); + RakAssert(ml3[1]==50); + RakAssert(ml3[2]==100); + RakAssert(ml3[3]==150); + RakAssert(ml3[4]==175); + RakAssert(ml3.GetIndexOf(25)==0); + RakAssert(ml3.GetIndexOf(50)==1); + RakAssert(ml3.GetIndexOf(100)==2); + RakAssert(ml3.GetIndexOf(150)==3); + RakAssert(ml3.GetIndexOf(175)==4); + ml3.Clear(_FILE_AND_LINE_); + + ml3.PushOpposite(1); + ml3.PushOpposite(2); + ml3.PushOpposite(3); + ml3.PushOpposite(4); + ml3.PushOpposite(5); + ml3.Sort(true); + RakAssert(ml3[0]==1); + RakAssert(ml3[1]==2); + RakAssert(ml3[2]==3); + RakAssert(ml3[3]==4); + RakAssert(ml3[4]==5); + RakAssert(ml3.GetIndexOf(1)==0); + RakAssert(ml3.GetIndexOf(2)==1); + RakAssert(ml3.GetIndexOf(3)==2); + RakAssert(ml3.GetIndexOf(4)==3); + RakAssert(ml3.GetIndexOf(5)==4); + ml3.Clear(_FILE_AND_LINE_); + + ml3.PushOpposite(5); + ml3.PushOpposite(4); + ml3.PushOpposite(3); + ml3.PushOpposite(2); + ml3.PushOpposite(1); + ml3.Sort(true); + RakAssert(ml3[0]==1); + RakAssert(ml3[1]==2); + RakAssert(ml3[2]==3); + RakAssert(ml3[3]==4); + RakAssert(ml3[4]==5); + RakAssert(ml3.GetIndexOf(1)==0); + RakAssert(ml3.GetIndexOf(2)==1); + RakAssert(ml3.GetIndexOf(3)==2); + RakAssert(ml3.GetIndexOf(4)==3); + RakAssert(ml3.GetIndexOf(5)==4); + ml3.Sort(true); + RakAssert(ml3[0]==1); + RakAssert(ml3[1]==2); + RakAssert(ml3[2]==3); + RakAssert(ml3[3]==4); + RakAssert(ml3[4]==5); + RakAssert(ml3.GetIndexOf(1)==0); + RakAssert(ml3.GetIndexOf(2)==1); + RakAssert(ml3.GetIndexOf(3)==2); + RakAssert(ml3.GetIndexOf(4)==3); + RakAssert(ml3.GetIndexOf(5)==4); + + ml3.SetSortOrder(false); + ml3.Sort(false); + RakAssert(ml3[0]==5); + RakAssert(ml3[1]==4); + RakAssert(ml3[2]==3); + RakAssert(ml3[3]==2); + RakAssert(ml3[4]==1); + RakAssert(ml3.GetIndexOf(1)==4); + RakAssert(ml3.GetIndexOf(2)==3); + RakAssert(ml3.GetIndexOf(3)==2); + RakAssert(ml3.GetIndexOf(4)==1); + RakAssert(ml3.GetIndexOf(5)==0); + + ml3.Clear(_FILE_AND_LINE_); + + DataStructures::Multilist ml4; + ml4.Reallocate(64); + RakAssert(ml4.IsEmpty()); + ml4.Push(53); + RakAssert(ml4.Peek()==53); + RakAssert(ml4.IsEmpty()==false); + RakAssert(ml4.Pop()==53); + RakAssert(ml4.IsEmpty()==true); + for (int i=0; i < 512; i++) + ml4.Push(i); + RakAssert(ml4.GetIndexOf(200)==200); + RakAssert(ml4.PeekOpposite()==0); + RakAssert(ml4.PopOpposite()==0); + RakAssert(ml4.PeekOpposite()==1); + RakAssert(ml4.Peek()==511); + ml4.ReverseList(); + for (int i=0; i < 511; i++) + RakAssert(ml4[i]==511-i); + RakAssert(ml4.PeekOpposite()==511); + RakAssert(ml4.Peek()==1); + oldSize = ml4.GetSize(); + ml4.RemoveAtIndex(0); + RakAssert(ml4.GetSize()==oldSize-1); + RakAssert(ml4.Peek()==1); + RakAssert(ml4.PeekOpposite()==510); + ml4.Clear(_FILE_AND_LINE_); + RakAssert(ml4.IsEmpty()==true); + + DataStructures::Multilist ml5; + + for (int i=0; i < 16; i++) + { + KeyAndValue *kav = new KeyAndValue; + kav->key=i; + kav->value=i+100; + ml5.Push(kav,kav->key); + } + + RakAssert(ml5.GetIndexOf(0)==0); + RakAssert(ml5.GetIndexOf(5)==5); + RakAssert(ml5.GetIndexOf(15)==15); + RakAssert(ml5.GetIndexOf(16)==-1); + ml5.RemoveAtKey(0,true); + RakAssert(ml5.GetIndexOf(1)==0); + KeyAndValue *iPtr = ml5.GetPtr(5); + RakAssert(iPtr); + RakAssert(iPtr->value=105); + iPtr = ml5.GetPtr(1234); + RakAssert(iPtr==0); + ml5.ForEach(DataStructures::DeletePtr); + + + DataStructures::Multilist ml6; + ml6.Push(2); + ml6.Push(1); + ml6.Push(6); + ml6.Push(3); + RakAssert(ml6.Peek()==3); + ml6.SetMultilistType(ML_STACK); + RakAssert(ml6.Peek()==3); + ml6.SetMultilistType(ML_QUEUE); + RakAssert(ml6.Peek()==2); + ml6.SetMultilistType(ML_ORDERED_LIST); + RakAssert(ml6.Peek()=6); + ml6.SetMultilistType(ML_STACK); + RakAssert(ml6.Peek()==6); + ml6.SetMultilistType(ML_QUEUE); + RakAssert(ml6.Peek()==1); +} + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif +*/ + diff --git a/Source/DS_OrderedChannelHeap.h b/Source/DS_OrderedChannelHeap.h index d4e1f46c4..b97a6424d 100644 --- a/Source/DS_OrderedChannelHeap.h +++ b/Source/DS_OrderedChannelHeap.h @@ -1,251 +1,249 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_OrderedChannelHeap.h -/// \internal -/// \brief Ordered Channel Heap . This is a heap where you add to it on multiple ordered channels, with each channel having a different weight. -/// - - -#ifndef __RAKNET_ORDERED_CHANNEL_HEAP_H -#define __RAKNET_ORDERED_CHANNEL_HEAP_H - -#include "DS_Heap.h" -#include "DS_Map.h" -#include "DS_Queue.h" -#include "Export.h" -#include "RakAssert.h" -#include "Rand.h" - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - template > - class RAK_DLL_EXPORT OrderedChannelHeap - { - public: - static void IMPLEMENT_DEFAULT_COMPARISON(void) {DataStructures::defaultMapKeyComparison(channel_key_type(),channel_key_type());} - - OrderedChannelHeap(); - ~OrderedChannelHeap(); - void Push(const channel_key_type &channelID, const heap_data_type &data); - void PushAtHead(const unsigned index, const channel_key_type &channelID, const heap_data_type &data); - heap_data_type Pop(const unsigned startingIndex=0); - heap_data_type Peek(const unsigned startingIndex) const; - void AddChannel(const channel_key_type &channelID, const double weight); - void RemoveChannel(channel_key_type channelID); - void Clear(void); - heap_data_type& operator[] ( const unsigned int position ) const; - unsigned ChannelSize(const channel_key_type &channelID); - unsigned Size(void) const; - - struct QueueAndWeight - { - DataStructures::Queue randResultQueue; - double weight; - bool signalDeletion; - }; - - struct HeapChannelAndData - { - HeapChannelAndData() {} - HeapChannelAndData(const channel_key_type &_channel, const heap_data_type &_data) : data(_data), channel(_channel) {} - heap_data_type data; - channel_key_type channel; - }; - - protected: - DataStructures::Map map; - DataStructures::Heap heap; - void GreatestRandResult(void); - }; - - template - OrderedChannelHeap::OrderedChannelHeap() - { - } - - template - OrderedChannelHeap::~OrderedChannelHeap() - { - Clear(); - } - - template - void OrderedChannelHeap::Push(const channel_key_type &channelID, const heap_data_type &data) - { - PushAtHead(MAX_UNSIGNED_LONG, channelID, data); - } - - template - void OrderedChannelHeap::GreatestRandResult(void) - { - double greatest; - unsigned i; - greatest=0.0; - for (i=0; i < map.Size(); i++) - { - if (map[i]->randResultQueue.Size() && map[i]->randResultQueue[0]>greatest) - greatest=map[i]->randResultQueue[0]; - } - return greatest; - } - - template - void OrderedChannelHeap::PushAtHead(const unsigned index, const channel_key_type &channelID, const heap_data_type &data) - { - // If an assert hits here then this is an unknown channel. Call AddChannel first. - QueueAndWeight *queueAndWeight=map.Get(channelID); - double maxRange, minRange, rnd; - if (queueAndWeight->randResultQueue.Size()==0) - { - // Set maxRange to the greatest random number waiting to be returned, rather than 1.0 necessarily - // This is so weights are scaled similarly among channels. For example, if the head weight for a used channel was .25 - // and then we added another channel, the new channel would need to choose between .25 and 0 - // If we chose between 1.0 and 0, it would be 1/.25 (4x) more likely to be at the head of the heap than it should be - maxRange=GreatestRandResult(); - if (maxRange==0.0) - maxRange=1.0; - minRange=0.0; - } - else if (index >= queueAndWeight->randResultQueue.Size()) - { - maxRange=queueAndWeight->randResultQueue[queueAndWeight->randResultQueue.Size()-1]*.99999999; - minRange=0.0; - } - else - { - if (index==0) - { - maxRange=GreatestRandResult(); - if (maxRange==queueAndWeight->randResultQueue[0]) - maxRange=1.0; - } - else if (index >= queueAndWeight->randResultQueue.Size()) - maxRange=queueAndWeight->randResultQueue[queueAndWeight->randResultQueue.Size()-1]*.99999999; - else - maxRange=queueAndWeight->randResultQueue[index-1]*.99999999; - - minRange=maxRange=queueAndWeight->randResultQueue[index]*1.00000001; - } - -#ifdef _DEBUG - RakAssert(maxRange!=0.0); -#endif - rnd=frandomMT() * (maxRange - minRange); - if (rnd==0.0) - rnd=maxRange/2.0; - - if (index >= queueAndWeight->randResultQueue.Size()) - queueAndWeight->randResultQueue.Push(rnd); - else - queueAndWeight->randResultQueue.PushAtHead(rnd, index); - - heap.Push(rnd*queueAndWeight->weight, HeapChannelAndData(channelID, data)); - } - - template - heap_data_type OrderedChannelHeap::Pop(const unsigned startingIndex) - { - RakAssert(startingIndex < heap.Size()); - - QueueAndWeight *queueAndWeight=map.Get(heap[startingIndex].channel); - if (startingIndex!=0) - { - // Ugly - have to count in the heap how many nodes have the same channel, so we know where to delete from in the queue - unsigned indiceCount=0; - unsigned i; - for (i=0; i < startingIndex; i++) - if (channel_key_comparison_func(heap[i].channel,heap[startingIndex].channel)==0) - indiceCount++; - queueAndWeight->randResultQueue.RemoveAtIndex(indiceCount); - } - else - { - // TODO - ordered channel heap uses progressively lower values as items are inserted. But this won't give relative ordering among channels. I have to renormalize after every pop. - queueAndWeight->randResultQueue.Pop(); - } - - // Try to remove the channel after every pop, because doing so is not valid while there are elements in the list. - if (queueAndWeight->signalDeletion) - RemoveChannel(heap[startingIndex].channel); - - return heap.Pop(startingIndex).data; - } - - template - heap_data_type OrderedChannelHeap::Peek(const unsigned startingIndex) const - { - HeapChannelAndData heapChannelAndData = heap.Peek(startingIndex); - return heapChannelAndData.data; - } - - template - void OrderedChannelHeap::AddChannel(const channel_key_type &channelID, const double weight) - { - QueueAndWeight *qaw = RakNet::OP_NEW( _FILE_AND_LINE_ ); - qaw->weight=weight; - qaw->signalDeletion=false; - map.SetNew(channelID, qaw); - } - - template - void OrderedChannelHeap::RemoveChannel(channel_key_type channelID) - { - if (map.Has(channelID)) - { - unsigned i; - i=map.GetIndexAtKey(channelID); - if (map[i]->randResultQueue.Size()==0) - { - RakNet::OP_DELETE(map[i], _FILE_AND_LINE_); - map.RemoveAtIndex(i); - } - else - { - // Signal this channel for deletion later, because the heap has nodes with this channel right now - map[i]->signalDeletion=true; - } - } - } - - template - unsigned OrderedChannelHeap::Size(void) const - { - return heap.Size(); - } - - template - heap_data_type& OrderedChannelHeap::operator[]( const unsigned int position ) const - { - return heap[position].data; - } - - - template - unsigned OrderedChannelHeap::ChannelSize(const channel_key_type &channelID) - { - QueueAndWeight *queueAndWeight=map.Get(channelID); - return queueAndWeight->randResultQueue.Size(); - } - - template - void OrderedChannelHeap::Clear(void) - { - unsigned i; - for (i=0; i < map.Size(); i++) - RakNet::OP_DELETE(map[i], _FILE_AND_LINE_); - map.Clear(_FILE_AND_LINE_); - heap.Clear(_FILE_AND_LINE_); - } -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_OrderedChannelHeap.h +/// \internal +/// \brief Ordered Channel Heap . This is a heap where you add to it on multiple ordered channels, with each channel having a different weight. +/// + + +#pragma once + +#include "DS_Heap.h" +#include "DS_Map.h" +#include "DS_Queue.h" +#include "Export.h" +#include "RakAssert.h" +#include "Rand.h" + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + template > + class RAK_DLL_EXPORT OrderedChannelHeap + { + public: + static void IMPLEMENT_DEFAULT_COMPARISON(void) {DataStructures::defaultMapKeyComparison(channel_key_type(),channel_key_type());} + + OrderedChannelHeap(); + ~OrderedChannelHeap(); + void Push(const channel_key_type &channelID, const heap_data_type &data); + void PushAtHead(const unsigned index, const channel_key_type &channelID, const heap_data_type &data); + heap_data_type Pop(const unsigned startingIndex=0); + heap_data_type Peek(const unsigned startingIndex) const; + void AddChannel(const channel_key_type &channelID, const double weight); + void RemoveChannel(channel_key_type channelID); + void Clear(void); + heap_data_type& operator[] ( const unsigned int position ) const; + unsigned ChannelSize(const channel_key_type &channelID); + unsigned Size(void) const; + + struct QueueAndWeight + { + DataStructures::Queue randResultQueue; + double weight; + bool signalDeletion; + }; + + struct HeapChannelAndData + { + HeapChannelAndData() {} + HeapChannelAndData(const channel_key_type &_channel, const heap_data_type &_data) : data(_data), channel(_channel) {} + heap_data_type data; + channel_key_type channel; + }; + + protected: + DataStructures::Map map; + DataStructures::Heap heap; + void GreatestRandResult(void); + }; + + template + OrderedChannelHeap::OrderedChannelHeap() + { + } + + template + OrderedChannelHeap::~OrderedChannelHeap() + { + Clear(); + } + + template + void OrderedChannelHeap::Push(const channel_key_type &channelID, const heap_data_type &data) + { + PushAtHead(MAX_UNSIGNED_LONG, channelID, data); + } + + template + void OrderedChannelHeap::GreatestRandResult(void) + { + double greatest; + unsigned i; + greatest=0.0; + for (i=0; i < map.Size(); i++) + { + if (map[i]->randResultQueue.Size() && map[i]->randResultQueue[0]>greatest) + greatest=map[i]->randResultQueue[0]; + } + return greatest; + } + + template + void OrderedChannelHeap::PushAtHead(const unsigned index, const channel_key_type &channelID, const heap_data_type &data) + { + // If an assert hits here then this is an unknown channel. Call AddChannel first. + QueueAndWeight *queueAndWeight=map.Get(channelID); + double maxRange, minRange, rnd; + if (queueAndWeight->randResultQueue.Size()==0) + { + // Set maxRange to the greatest random number waiting to be returned, rather than 1.0 necessarily + // This is so weights are scaled similarly among channels. For example, if the head weight for a used channel was .25 + // and then we added another channel, the new channel would need to choose between .25 and 0 + // If we chose between 1.0 and 0, it would be 1/.25 (4x) more likely to be at the head of the heap than it should be + maxRange=GreatestRandResult(); + if (maxRange==0.0) + maxRange=1.0; + minRange=0.0; + } + else if (index >= queueAndWeight->randResultQueue.Size()) + { + maxRange=queueAndWeight->randResultQueue[queueAndWeight->randResultQueue.Size()-1]*.99999999; + minRange=0.0; + } + else + { + if (index==0) + { + maxRange=GreatestRandResult(); + if (maxRange==queueAndWeight->randResultQueue[0]) + maxRange=1.0; + } + else if (index >= queueAndWeight->randResultQueue.Size()) + maxRange=queueAndWeight->randResultQueue[queueAndWeight->randResultQueue.Size()-1]*.99999999; + else + maxRange=queueAndWeight->randResultQueue[index-1]*.99999999; + + minRange=maxRange=queueAndWeight->randResultQueue[index]*1.00000001; + } + +#ifdef _DEBUG + RakAssert(maxRange!=0.0); +#endif + rnd=frandomMT() * (maxRange - minRange); + if (rnd==0.0) + rnd=maxRange/2.0; + + if (index >= queueAndWeight->randResultQueue.Size()) + queueAndWeight->randResultQueue.Push(rnd); + else + queueAndWeight->randResultQueue.PushAtHead(rnd, index); + + heap.Push(rnd*queueAndWeight->weight, HeapChannelAndData(channelID, data)); + } + + template + heap_data_type OrderedChannelHeap::Pop(const unsigned startingIndex) + { + RakAssert(startingIndex < heap.Size()); + + QueueAndWeight *queueAndWeight=map.Get(heap[startingIndex].channel); + if (startingIndex!=0) + { + // Ugly - have to count in the heap how many nodes have the same channel, so we know where to delete from in the queue + unsigned indiceCount=0; + unsigned i; + for (i=0; i < startingIndex; i++) + if (channel_key_comparison_func(heap[i].channel,heap[startingIndex].channel)==0) + indiceCount++; + queueAndWeight->randResultQueue.RemoveAtIndex(indiceCount); + } + else + { + // TODO - ordered channel heap uses progressively lower values as items are inserted. But this won't give relative ordering among channels. I have to renormalize after every pop. + queueAndWeight->randResultQueue.Pop(); + } + + // Try to remove the channel after every pop, because doing so is not valid while there are elements in the list. + if (queueAndWeight->signalDeletion) + RemoveChannel(heap[startingIndex].channel); + + return heap.Pop(startingIndex).data; + } + + template + heap_data_type OrderedChannelHeap::Peek(const unsigned startingIndex) const + { + HeapChannelAndData heapChannelAndData = heap.Peek(startingIndex); + return heapChannelAndData.data; + } + + template + void OrderedChannelHeap::AddChannel(const channel_key_type &channelID, const double weight) + { + QueueAndWeight *qaw = RakNet::OP_NEW( _FILE_AND_LINE_ ); + qaw->weight=weight; + qaw->signalDeletion=false; + map.SetNew(channelID, qaw); + } + + template + void OrderedChannelHeap::RemoveChannel(channel_key_type channelID) + { + if (map.Has(channelID)) + { + unsigned i; + i=map.GetIndexAtKey(channelID); + if (map[i]->randResultQueue.Size()==0) + { + RakNet::OP_DELETE(map[i], _FILE_AND_LINE_); + map.RemoveAtIndex(i); + } + else + { + // Signal this channel for deletion later, because the heap has nodes with this channel right now + map[i]->signalDeletion=true; + } + } + } + + template + unsigned OrderedChannelHeap::Size(void) const + { + return heap.Size(); + } + + template + heap_data_type& OrderedChannelHeap::operator[]( const unsigned int position ) const + { + return heap[position].data; + } + + + template + unsigned OrderedChannelHeap::ChannelSize(const channel_key_type &channelID) + { + QueueAndWeight *queueAndWeight=map.Get(channelID); + return queueAndWeight->randResultQueue.Size(); + } + + template + void OrderedChannelHeap::Clear(void) + { + unsigned i; + for (i=0; i < map.Size(); i++) + RakNet::OP_DELETE(map[i], _FILE_AND_LINE_); + map.Clear(_FILE_AND_LINE_); + heap.Clear(_FILE_AND_LINE_); + } +} + diff --git a/Source/DS_OrderedList.h b/Source/DS_OrderedList.h index fe536bf67..efa11e98a 100644 --- a/Source/DS_OrderedList.h +++ b/Source/DS_OrderedList.h @@ -1,278 +1,276 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_OrderedList.h -/// \internal -/// \brief Quicksort ordered list. -/// - -#include "DS_List.h" -#include "RakMemoryOverride.h" -#include "Export.h" - -#ifndef __ORDERED_LIST_H -#define __ORDERED_LIST_H - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - template - int defaultOrderedListComparison(const key_type &a, const data_type &b) - { - if (a > - class RAK_DLL_EXPORT OrderedList - { - public: - static void IMPLEMENT_DEFAULT_COMPARISON(void) {DataStructures::defaultOrderedListComparison(key_type(),data_type());} - - OrderedList(); - ~OrderedList(); - OrderedList( const OrderedList& original_copy ); - OrderedList& operator= ( const OrderedList& original_copy ); - - /// comparisonFunction must take a key_type and a data_type and return <0, ==0, or >0 - /// If the data type has comparison operators already defined then you can just use defaultComparison - bool HasData(const key_type &key, int (*cf)(const key_type&, const data_type&)=default_comparison_function) const; - // GetIndexFromKey returns where the insert should go at the same time checks if it is there - unsigned GetIndexFromKey(const key_type &key, bool *objectExists, int (*cf)(const key_type&, const data_type&)=default_comparison_function) const; - data_type GetElementFromKey(const key_type &key, int (*cf)(const key_type&, const data_type&)=default_comparison_function) const; - bool GetElementFromKey(const key_type &key, data_type &element, int (*cf)(const key_type&, const data_type&)=default_comparison_function) const; - unsigned Insert(const key_type &key, const data_type &data, bool assertOnDuplicate, const char *file, unsigned int line, int (*cf)(const key_type&, const data_type&)=default_comparison_function); - unsigned Remove(const key_type &key, int (*cf)(const key_type&, const data_type&)=default_comparison_function); - unsigned RemoveIfExists(const key_type &key, int (*cf)(const key_type&, const data_type&)=default_comparison_function); - data_type& operator[] ( const unsigned int position ) const; - void RemoveAtIndex(const unsigned index); - void InsertAtIndex(const data_type &data, const unsigned index, const char *file, unsigned int line); - void InsertAtEnd(const data_type &data, const char *file, unsigned int line); - void RemoveFromEnd(const unsigned num=1); - void Clear(bool doNotDeallocate, const char *file, unsigned int line); - unsigned Size(void) const; - - protected: - DataStructures::List orderedList; - }; - - template - OrderedList::OrderedList() - { - } - - template - OrderedList::~OrderedList() - { - Clear(false, _FILE_AND_LINE_); - } - - template - OrderedList::OrderedList( const OrderedList& original_copy ) - { - orderedList=original_copy.orderedList; - } - - template - OrderedList& OrderedList::operator= ( const OrderedList& original_copy ) - { - orderedList=original_copy.orderedList; - return *this; - } - - template - bool OrderedList::HasData(const key_type &key, int (*cf)(const key_type&, const data_type&)) const - { - bool objectExists; - GetIndexFromKey(key, &objectExists, cf); - return objectExists; - } - - template - data_type OrderedList::GetElementFromKey(const key_type &key, int (*cf)(const key_type&, const data_type&)) const - { - bool objectExists; - unsigned index; - index = GetIndexFromKey(key, &objectExists, cf); - RakAssert(objectExists); - return orderedList[index]; - } - template - bool OrderedList::GetElementFromKey(const key_type &key, data_type &element, int (*cf)(const key_type&, const data_type&)) const - { - bool objectExists; - unsigned index; - index = GetIndexFromKey(key, &objectExists, cf); - if (objectExists) - element = orderedList[index]; - return objectExists; - } - template - unsigned OrderedList::GetIndexFromKey(const key_type &key, bool *objectExists, int (*cf)(const key_type&, const data_type&)) const - { - int index, upperBound, lowerBound; - int res; - - if (orderedList.Size()==0) - { - *objectExists=false; - return 0; - } - - upperBound=(int)orderedList.Size()-1; - lowerBound=0; - index = (int)orderedList.Size()/2; - -#ifdef _MSC_VER - #pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - while (1) - { - res = cf(key,orderedList[index]); - if (res==0) - { - *objectExists=true; - return (unsigned)index; - } - else if (res<0) - { - upperBound=index-1; - } - else// if (res>0) - { - - lowerBound=index+1; - } - - index=lowerBound+(upperBound-lowerBound)/2; - - if (lowerBound>upperBound) - { - *objectExists=false; - return (unsigned)lowerBound; // No match - } - - if (index < 0 || index >= (int) orderedList.Size()) - { - // This should never hit unless the comparison function was inconsistent - RakAssert(index && 0); - *objectExists=false; - return 0; - } - } - } - - template - unsigned OrderedList::Insert(const key_type &key, const data_type &data, bool assertOnDuplicate, const char *file, unsigned int line, int (*cf)(const key_type&, const data_type&)) - { - (void) assertOnDuplicate; - bool objectExists; - unsigned index; - index = GetIndexFromKey(key, &objectExists, cf); - - // Don't allow duplicate insertion. - if (objectExists) - { - // This is usually a bug! - RakAssert(assertOnDuplicate==false); - return (unsigned)-1; - } - - if (index>=orderedList.Size()) - { - orderedList.Insert(data, file, line); - return orderedList.Size()-1; - } - else - { - orderedList.Insert(data,index, file, line); - return index; - } - } - - template - unsigned OrderedList::Remove(const key_type &key, int (*cf)(const key_type&, const data_type&)) - { - bool objectExists; - unsigned index; - index = GetIndexFromKey(key, &objectExists, cf); - - // Can't find the element to remove if this assert hits - // RakAssert(objectExists==true); - if (objectExists==false) - { - RakAssert(objectExists==true); - return 0; - } - - orderedList.RemoveAtIndex(index); - return index; - } - - template - unsigned OrderedList::RemoveIfExists(const key_type &key, int (*cf)(const key_type&, const data_type&)) - { - bool objectExists; - unsigned index; - index = GetIndexFromKey(key, &objectExists, cf); - - // Can't find the element to remove if this assert hits - if (objectExists==false) - return 0; - - orderedList.RemoveAtIndex(index); - return index; - } - - template - void OrderedList::RemoveAtIndex(const unsigned index) - { - orderedList.RemoveAtIndex(index); - } - - template - void OrderedList::InsertAtIndex(const data_type &data, const unsigned index, const char *file, unsigned int line) - { - orderedList.Insert(data, index, file, line); - } - - template - void OrderedList::InsertAtEnd(const data_type &data, const char *file, unsigned int line) - { - orderedList.Insert(data, file, line); - } - - template - void OrderedList::RemoveFromEnd(const unsigned num) - { - orderedList.RemoveFromEnd(num); - } - - template - void OrderedList::Clear(bool doNotDeallocate, const char *file, unsigned int line) - { - orderedList.Clear(doNotDeallocate, file, line); - } - - template - data_type& OrderedList::operator[]( const unsigned int position ) const - { - return orderedList[position]; - } - - template - unsigned OrderedList::Size(void) const - { - return orderedList.Size(); - } -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_OrderedList.h +/// \internal +/// \brief Quicksort ordered list. +/// + +#include "DS_List.h" +#include "RakMemoryOverride.h" +#include "Export.h" + +#pragma once + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + template + int defaultOrderedListComparison(const key_type &a, const data_type &b) + { + if (a > + class RAK_DLL_EXPORT OrderedList + { + public: + static void IMPLEMENT_DEFAULT_COMPARISON(void) {DataStructures::defaultOrderedListComparison(key_type(),data_type());} + + OrderedList(); + ~OrderedList(); + OrderedList( const OrderedList& original_copy ); + OrderedList& operator= ( const OrderedList& original_copy ); + + /// comparisonFunction must take a key_type and a data_type and return <0, ==0, or >0 + /// If the data type has comparison operators already defined then you can just use defaultComparison + bool HasData(const key_type &key, int (*cf)(const key_type&, const data_type&)=default_comparison_function) const; + // GetIndexFromKey returns where the insert should go at the same time checks if it is there + unsigned GetIndexFromKey(const key_type &key, bool *objectExists, int (*cf)(const key_type&, const data_type&)=default_comparison_function) const; + data_type GetElementFromKey(const key_type &key, int (*cf)(const key_type&, const data_type&)=default_comparison_function) const; + bool GetElementFromKey(const key_type &key, data_type &element, int (*cf)(const key_type&, const data_type&)=default_comparison_function) const; + unsigned Insert(const key_type &key, const data_type &data, bool assertOnDuplicate, const char *file, unsigned int line, int (*cf)(const key_type&, const data_type&)=default_comparison_function); + unsigned Remove(const key_type &key, int (*cf)(const key_type&, const data_type&)=default_comparison_function); + unsigned RemoveIfExists(const key_type &key, int (*cf)(const key_type&, const data_type&)=default_comparison_function); + data_type& operator[] ( const unsigned int position ) const; + void RemoveAtIndex(const unsigned index); + void InsertAtIndex(const data_type &data, const unsigned index, const char *file, unsigned int line); + void InsertAtEnd(const data_type &data, const char *file, unsigned int line); + void RemoveFromEnd(const unsigned num=1); + void Clear(bool doNotDeallocate, const char *file, unsigned int line); + unsigned Size(void) const; + + protected: + DataStructures::List orderedList; + }; + + template + OrderedList::OrderedList() + { + } + + template + OrderedList::~OrderedList() + { + Clear(false, _FILE_AND_LINE_); + } + + template + OrderedList::OrderedList( const OrderedList& original_copy ) + { + orderedList=original_copy.orderedList; + } + + template + OrderedList& OrderedList::operator= ( const OrderedList& original_copy ) + { + orderedList=original_copy.orderedList; + return *this; + } + + template + bool OrderedList::HasData(const key_type &key, int (*cf)(const key_type&, const data_type&)) const + { + bool objectExists; + GetIndexFromKey(key, &objectExists, cf); + return objectExists; + } + + template + data_type OrderedList::GetElementFromKey(const key_type &key, int (*cf)(const key_type&, const data_type&)) const + { + bool objectExists; + unsigned index; + index = GetIndexFromKey(key, &objectExists, cf); + RakAssert(objectExists); + return orderedList[index]; + } + template + bool OrderedList::GetElementFromKey(const key_type &key, data_type &element, int (*cf)(const key_type&, const data_type&)) const + { + bool objectExists; + unsigned index; + index = GetIndexFromKey(key, &objectExists, cf); + if (objectExists) + element = orderedList[index]; + return objectExists; + } + template + unsigned OrderedList::GetIndexFromKey(const key_type &key, bool *objectExists, int (*cf)(const key_type&, const data_type&)) const + { + int index, upperBound, lowerBound; + int res; + + if (orderedList.Size()==0) + { + *objectExists=false; + return 0; + } + + upperBound=(int)orderedList.Size()-1; + lowerBound=0; + index = (int)orderedList.Size()/2; + +#ifdef _MSC_VER + #pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + while (1) + { + res = cf(key,orderedList[index]); + if (res==0) + { + *objectExists=true; + return (unsigned)index; + } + else if (res<0) + { + upperBound=index-1; + } + else// if (res>0) + { + + lowerBound=index+1; + } + + index=lowerBound+(upperBound-lowerBound)/2; + + if (lowerBound>upperBound) + { + *objectExists=false; + return (unsigned)lowerBound; // No match + } + + if (index < 0 || index >= (int) orderedList.Size()) + { + // This should never hit unless the comparison function was inconsistent + RakAssert(index && 0); + *objectExists=false; + return 0; + } + } + } + + template + unsigned OrderedList::Insert(const key_type &key, const data_type &data, bool assertOnDuplicate, const char *file, unsigned int line, int (*cf)(const key_type&, const data_type&)) + { + (void) assertOnDuplicate; + bool objectExists; + unsigned index; + index = GetIndexFromKey(key, &objectExists, cf); + + // Don't allow duplicate insertion. + if (objectExists) + { + // This is usually a bug! + RakAssert(assertOnDuplicate==false); + return (unsigned)-1; + } + + if (index>=orderedList.Size()) + { + orderedList.Insert(data, file, line); + return orderedList.Size()-1; + } + else + { + orderedList.Insert(data,index, file, line); + return index; + } + } + + template + unsigned OrderedList::Remove(const key_type &key, int (*cf)(const key_type&, const data_type&)) + { + bool objectExists; + unsigned index; + index = GetIndexFromKey(key, &objectExists, cf); + + // Can't find the element to remove if this assert hits + // RakAssert(objectExists==true); + if (objectExists==false) + { + RakAssert(objectExists==true); + return 0; + } + + orderedList.RemoveAtIndex(index); + return index; + } + + template + unsigned OrderedList::RemoveIfExists(const key_type &key, int (*cf)(const key_type&, const data_type&)) + { + bool objectExists; + unsigned index; + index = GetIndexFromKey(key, &objectExists, cf); + + // Can't find the element to remove if this assert hits + if (objectExists==false) + return 0; + + orderedList.RemoveAtIndex(index); + return index; + } + + template + void OrderedList::RemoveAtIndex(const unsigned index) + { + orderedList.RemoveAtIndex(index); + } + + template + void OrderedList::InsertAtIndex(const data_type &data, const unsigned index, const char *file, unsigned int line) + { + orderedList.Insert(data, index, file, line); + } + + template + void OrderedList::InsertAtEnd(const data_type &data, const char *file, unsigned int line) + { + orderedList.Insert(data, file, line); + } + + template + void OrderedList::RemoveFromEnd(const unsigned num) + { + orderedList.RemoveFromEnd(num); + } + + template + void OrderedList::Clear(bool doNotDeallocate, const char *file, unsigned int line) + { + orderedList.Clear(doNotDeallocate, file, line); + } + + template + data_type& OrderedList::operator[]( const unsigned int position ) const + { + return orderedList[position]; + } + + template + unsigned OrderedList::Size(void) const + { + return orderedList.Size(); + } +} + diff --git a/Source/DS_Queue.h b/Source/DS_Queue.h index 0a79aa4b1..46f367ee7 100644 --- a/Source/DS_Queue.h +++ b/Source/DS_Queue.h @@ -1,461 +1,458 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_Queue.h -/// \internal -/// \brief A queue used by RakNet. -/// - - -#ifndef __QUEUE_H -#define __QUEUE_H - -// Template classes have to have all the code in the header file -#include "RakAssert.h" -#include "Export.h" -#include "RakMemoryOverride.h" - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - /// \brief A queue implemented as an array with a read and write index. - template - class RAK_DLL_EXPORT Queue - { - public: - Queue(); - ~Queue(); - Queue( Queue& original_copy ); - bool operator= ( const Queue& original_copy ); - void Push( const queue_type& input, const char *file, unsigned int line ); - void PushAtHead( const queue_type& input, unsigned index, const char *file, unsigned int line ); - queue_type& operator[] ( unsigned int position ) const; // Not a normal thing you do with a queue but can be used for efficiency - void RemoveAtIndex( unsigned int position ); // Not a normal thing you do with a queue but can be used for efficiency - inline queue_type Peek( void ) const; - inline queue_type PeekTail( void ) const; - inline queue_type Pop( void ); - inline queue_type PopTail( void ); - // Debug: Set pointer to 0, for memory leak detection - inline queue_type PopDeref( void ); - inline unsigned int Size( void ) const; - inline bool IsEmpty(void) const; - inline unsigned int AllocationSize( void ) const; - inline void Clear( const char *file, unsigned int line ); - void Compress( const char *file, unsigned int line ); - bool Find ( const queue_type& q ); - void ClearAndForceAllocation( int size, const char *file, unsigned int line ); // Force a memory allocation to a certain larger size - - private: - queue_type* array; - unsigned int head; // Array index for the head of the queue - unsigned int tail; // Array index for the tail of the queue - unsigned int allocation_size; - }; - - - template - inline unsigned int Queue::Size( void ) const - { - if ( head <= tail ) - return tail -head; - else - return allocation_size -head + tail; - } - - template - inline bool Queue::IsEmpty(void) const - { - return head==tail; - } - - template - inline unsigned int Queue::AllocationSize( void ) const - { - return allocation_size; - } - - template - Queue::Queue() - { - //allocation_size = 16; - //array = RakNet::OP_NEW_ARRAY(allocation_size, _FILE_AND_LINE_ ); - allocation_size = 0; - array=0; - head = 0; - tail = 0; - } - - template - Queue::~Queue() - { - if (allocation_size>0) - RakNet::OP_DELETE_ARRAY(array, _FILE_AND_LINE_); - } - - template - inline queue_type Queue::Pop( void ) - { -#ifdef _DEBUG - RakAssert( head != tail); -#endif - //head=(head+1) % allocation_size; - - if ( ++head == allocation_size ) - head = 0; - - if ( head == 0 ) - return ( queue_type ) array[ allocation_size -1 ]; - - return ( queue_type ) array[ head -1 ]; - } - - template - inline queue_type Queue::PopTail( void ) - { -#ifdef _DEBUG - RakAssert( head != tail ); -#endif - if (tail!=0) - { - --tail; - return ( queue_type ) array[ tail ]; - } - else - { - tail=allocation_size-1; - return ( queue_type ) array[ tail ]; - } - } - - template - inline queue_type Queue::PopDeref( void ) - { - if ( ++head == allocation_size ) - head = 0; - - queue_type q; - if ( head == 0 ) - { - q=array[ allocation_size -1 ]; - array[ allocation_size -1 ]=0; - return q; - } - - q=array[ head -1 ]; - array[ head -1 ]=0; - return q; - } - - template - void Queue::PushAtHead( const queue_type& input, unsigned index, const char *file, unsigned int line ) - { - RakAssert(index <= Size()); - - // Just force a reallocation, will be overwritten - Push(input, file, line ); - - if (Size()==1) - return; - - unsigned writeIndex, readIndex, trueWriteIndex, trueReadIndex; - writeIndex=Size()-1; - readIndex=writeIndex-1; - while (readIndex >= index) - { - if ( head + writeIndex >= allocation_size ) - trueWriteIndex = head + writeIndex - allocation_size; - else - trueWriteIndex = head + writeIndex; - - if ( head + readIndex >= allocation_size ) - trueReadIndex = head + readIndex - allocation_size; - else - trueReadIndex = head + readIndex; - - array[trueWriteIndex]=array[trueReadIndex]; - - if (readIndex==0) - break; - writeIndex--; - readIndex--; - } - - if ( head + index >= allocation_size ) - trueWriteIndex = head + index - allocation_size; - else - trueWriteIndex = head + index; - - array[trueWriteIndex]=input; - } - - - template - inline queue_type Queue::Peek( void ) const - { -#ifdef _DEBUG - RakAssert( head != tail ); -#endif - - return ( queue_type ) array[ head ]; - } - - template - inline queue_type Queue::PeekTail( void ) const - { -#ifdef _DEBUG - RakAssert( head != tail ); -#endif - if (tail!=0) - return ( queue_type ) array[ tail-1 ]; - else - return ( queue_type ) array[ allocation_size-1 ]; - } - - template - void Queue::Push( const queue_type& input, const char *file, unsigned int line ) - { - if ( allocation_size == 0 ) - { - array = RakNet::OP_NEW_ARRAY(16, file, line ); - head = 0; - tail = 1; - array[ 0 ] = input; - allocation_size = 16; - return ; - } - - array[ tail++ ] = input; - - if ( tail == allocation_size ) - tail = 0; - - if ( tail == head ) - { - // unsigned int index=tail; - - // Need to allocate more memory. - queue_type * new_array; - new_array = RakNet::OP_NEW_ARRAY((int)allocation_size * 2, file, line ); -#ifdef _DEBUG - RakAssert( new_array ); -#endif - if (new_array==0) - return; - - for ( unsigned int counter = 0; counter < allocation_size; ++counter ) - new_array[ counter ] = array[ ( head + counter ) % ( allocation_size ) ]; - - head = 0; - - tail = allocation_size; - - allocation_size *= 2; - - // Delete the old array and move the pointer to the new array - RakNet::OP_DELETE_ARRAY(array, file, line); - - array = new_array; - } - - } - - template - Queue::Queue( Queue& original_copy ) - { - // Allocate memory for copy - - if ( original_copy.Size() == 0 ) - { - allocation_size = 0; - } - - else - { - array = RakNet::OP_NEW_ARRAY( original_copy.Size() + 1 , _FILE_AND_LINE_ ); - - for ( unsigned int counter = 0; counter < original_copy.Size(); ++counter ) - array[ counter ] = original_copy.array[ ( original_copy.head + counter ) % ( original_copy.allocation_size ) ]; - - head = 0; - - tail = original_copy.Size(); - - allocation_size = original_copy.Size() + 1; - } - } - - template - bool Queue::operator= ( const Queue& original_copy ) - { - if ( ( &original_copy ) == this ) - return false; - - Clear(_FILE_AND_LINE_); - - // Allocate memory for copy - if ( original_copy.Size() == 0 ) - { - allocation_size = 0; - } - - else - { - array = RakNet::OP_NEW_ARRAY( original_copy.Size() + 1 , _FILE_AND_LINE_ ); - - for ( unsigned int counter = 0; counter < original_copy.Size(); ++counter ) - array[ counter ] = original_copy.array[ ( original_copy.head + counter ) % ( original_copy.allocation_size ) ]; - - head = 0; - - tail = original_copy.Size(); - - allocation_size = original_copy.Size() + 1; - } - - return true; - } - - template - inline void Queue::Clear ( const char *file, unsigned int line ) - { - if ( allocation_size == 0 ) - return ; - - if (allocation_size > 32) - { - RakNet::OP_DELETE_ARRAY(array, file, line); - allocation_size = 0; - } - - head = 0; - tail = 0; - } - - template - void Queue::Compress ( const char *file, unsigned int line ) - { - queue_type* new_array; - unsigned int newAllocationSize; - if (allocation_size==0) - return; - - newAllocationSize=1; - while (newAllocationSize <= Size()) - newAllocationSize<<=1; // Must be a better way to do this but I'm too dumb to figure it out quickly :) - - new_array = RakNet::OP_NEW_ARRAY(newAllocationSize, file, line ); - - for (unsigned int counter=0; counter < Size(); ++counter) - new_array[counter] = array[(head + counter)%(allocation_size)]; - - tail=Size(); - allocation_size=newAllocationSize; - head=0; - - // Delete the old array and move the pointer to the new array - RakNet::OP_DELETE_ARRAY(array, file, line); - array=new_array; - } - - template - bool Queue::Find ( const queue_type &q ) - { - if ( allocation_size == 0 ) - return false; - - unsigned int counter = head; - - while ( counter != tail ) - { - if ( array[ counter ] == q ) - return true; - - counter = ( counter + 1 ) % allocation_size; - } - - return false; - } - - template - void Queue::ClearAndForceAllocation( int size, const char *file, unsigned int line ) - { - RakNet::OP_DELETE_ARRAY(array, file, line); - if (size>0) - array = RakNet::OP_NEW_ARRAY(size, file, line ); - else - array=0; - allocation_size = size; - head = 0; - tail = 0; - } - - template - inline queue_type& Queue::operator[] ( unsigned int position ) const - { -#ifdef _DEBUG - RakAssert( position < Size() ); -#endif - //return array[(head + position) % allocation_size]; - - if ( head + position >= allocation_size ) - return array[ head + position - allocation_size ]; - else - return array[ head + position ]; - } - - template - void Queue::RemoveAtIndex( unsigned int position ) - { -#ifdef _DEBUG - RakAssert( position < Size() ); - RakAssert( head != tail ); -#endif - - if ( head == tail || position >= Size() ) - return ; - - unsigned int index; - - unsigned int next; - - //index = (head + position) % allocation_size; - if ( head + position >= allocation_size ) - index = head + position - allocation_size; - else - index = head + position; - - //next = (index + 1) % allocation_size; - next = index + 1; - - if ( next == allocation_size ) - next = 0; - - while ( next != tail ) - { - // Overwrite the previous element - array[ index ] = array[ next ]; - index = next; - //next = (next + 1) % allocation_size; - - if ( ++next == allocation_size ) - next = 0; - } - - // Move the tail back - if ( tail == 0 ) - tail = allocation_size - 1; - else - --tail; - } -} // End namespace - -#endif - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_Queue.h +/// \internal +/// \brief A queue used by RakNet. +/// + + +#pragma once + +// Template classes have to have all the code in the header file +#include "RakAssert.h" +#include "Export.h" +#include "RakMemoryOverride.h" + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + /// \brief A queue implemented as an array with a read and write index. + template + class RAK_DLL_EXPORT Queue + { + public: + Queue(); + ~Queue(); + Queue( Queue& original_copy ); + bool operator= ( const Queue& original_copy ); + void Push( const queue_type& input, const char *file, unsigned int line ); + void PushAtHead( const queue_type& input, unsigned index, const char *file, unsigned int line ); + queue_type& operator[] ( unsigned int position ) const; // Not a normal thing you do with a queue but can be used for efficiency + void RemoveAtIndex( unsigned int position ); // Not a normal thing you do with a queue but can be used for efficiency + inline queue_type Peek( void ) const; + inline queue_type PeekTail( void ) const; + inline queue_type Pop( void ); + inline queue_type PopTail( void ); + // Debug: Set pointer to 0, for memory leak detection + inline queue_type PopDeref( void ); + inline unsigned int Size( void ) const; + inline bool IsEmpty(void) const; + inline unsigned int AllocationSize( void ) const; + inline void Clear( const char *file, unsigned int line ); + void Compress( const char *file, unsigned int line ); + bool Find ( const queue_type& q ); + void ClearAndForceAllocation( int size, const char *file, unsigned int line ); // Force a memory allocation to a certain larger size + + private: + queue_type* array; + unsigned int head; // Array index for the head of the queue + unsigned int tail; // Array index for the tail of the queue + unsigned int allocation_size; + }; + + + template + inline unsigned int Queue::Size( void ) const + { + if ( head <= tail ) + return tail -head; + else + return allocation_size -head + tail; + } + + template + inline bool Queue::IsEmpty(void) const + { + return head==tail; + } + + template + inline unsigned int Queue::AllocationSize( void ) const + { + return allocation_size; + } + + template + Queue::Queue() + { + //allocation_size = 16; + //array = RakNet::OP_NEW_ARRAY(allocation_size, _FILE_AND_LINE_ ); + allocation_size = 0; + array=0; + head = 0; + tail = 0; + } + + template + Queue::~Queue() + { + if (allocation_size>0) + RakNet::OP_DELETE_ARRAY(array, _FILE_AND_LINE_); + } + + template + inline queue_type Queue::Pop( void ) + { +#ifdef _DEBUG + RakAssert( head != tail); +#endif + //head=(head+1) % allocation_size; + + if ( ++head == allocation_size ) + head = 0; + + if ( head == 0 ) + return ( queue_type ) array[ allocation_size -1 ]; + + return ( queue_type ) array[ head -1 ]; + } + + template + inline queue_type Queue::PopTail( void ) + { +#ifdef _DEBUG + RakAssert( head != tail ); +#endif + if (tail!=0) + { + --tail; + return ( queue_type ) array[ tail ]; + } + else + { + tail=allocation_size-1; + return ( queue_type ) array[ tail ]; + } + } + + template + inline queue_type Queue::PopDeref( void ) + { + if ( ++head == allocation_size ) + head = 0; + + queue_type q; + if ( head == 0 ) + { + q=array[ allocation_size -1 ]; + array[ allocation_size -1 ]=0; + return q; + } + + q=array[ head -1 ]; + array[ head -1 ]=0; + return q; + } + + template + void Queue::PushAtHead( const queue_type& input, unsigned index, const char *file, unsigned int line ) + { + RakAssert(index <= Size()); + + // Just force a reallocation, will be overwritten + Push(input, file, line ); + + if (Size()==1) + return; + + unsigned writeIndex, readIndex, trueWriteIndex, trueReadIndex; + writeIndex=Size()-1; + readIndex=writeIndex-1; + while (readIndex >= index) + { + if ( head + writeIndex >= allocation_size ) + trueWriteIndex = head + writeIndex - allocation_size; + else + trueWriteIndex = head + writeIndex; + + if ( head + readIndex >= allocation_size ) + trueReadIndex = head + readIndex - allocation_size; + else + trueReadIndex = head + readIndex; + + array[trueWriteIndex]=array[trueReadIndex]; + + if (readIndex==0) + break; + writeIndex--; + readIndex--; + } + + if ( head + index >= allocation_size ) + trueWriteIndex = head + index - allocation_size; + else + trueWriteIndex = head + index; + + array[trueWriteIndex]=input; + } + + + template + inline queue_type Queue::Peek( void ) const + { +#ifdef _DEBUG + RakAssert( head != tail ); +#endif + + return ( queue_type ) array[ head ]; + } + + template + inline queue_type Queue::PeekTail( void ) const + { +#ifdef _DEBUG + RakAssert( head != tail ); +#endif + if (tail!=0) + return ( queue_type ) array[ tail-1 ]; + else + return ( queue_type ) array[ allocation_size-1 ]; + } + + template + void Queue::Push( const queue_type& input, const char *file, unsigned int line ) + { + if ( allocation_size == 0 ) + { + array = RakNet::OP_NEW_ARRAY(16, file, line ); + head = 0; + tail = 1; + array[ 0 ] = input; + allocation_size = 16; + return ; + } + + array[ tail++ ] = input; + + if ( tail == allocation_size ) + tail = 0; + + if ( tail == head ) + { + // unsigned int index=tail; + + // Need to allocate more memory. + queue_type * new_array; + new_array = RakNet::OP_NEW_ARRAY((int)allocation_size * 2, file, line ); +#ifdef _DEBUG + RakAssert( new_array ); +#endif + if (new_array==0) + return; + + for ( unsigned int counter = 0; counter < allocation_size; ++counter ) + new_array[ counter ] = array[ ( head + counter ) % ( allocation_size ) ]; + + head = 0; + + tail = allocation_size; + + allocation_size *= 2; + + // Delete the old array and move the pointer to the new array + RakNet::OP_DELETE_ARRAY(array, file, line); + + array = new_array; + } + + } + + template + Queue::Queue( Queue& original_copy ) + { + // Allocate memory for copy + + if ( original_copy.Size() == 0 ) + { + allocation_size = 0; + } + + else + { + array = RakNet::OP_NEW_ARRAY( original_copy.Size() + 1 , _FILE_AND_LINE_ ); + + for ( unsigned int counter = 0; counter < original_copy.Size(); ++counter ) + array[ counter ] = original_copy.array[ ( original_copy.head + counter ) % ( original_copy.allocation_size ) ]; + + head = 0; + + tail = original_copy.Size(); + + allocation_size = original_copy.Size() + 1; + } + } + + template + bool Queue::operator= ( const Queue& original_copy ) + { + if ( ( &original_copy ) == this ) + return false; + + Clear(_FILE_AND_LINE_); + + // Allocate memory for copy + if ( original_copy.Size() == 0 ) + { + allocation_size = 0; + } + + else + { + array = RakNet::OP_NEW_ARRAY( original_copy.Size() + 1 , _FILE_AND_LINE_ ); + + for ( unsigned int counter = 0; counter < original_copy.Size(); ++counter ) + array[ counter ] = original_copy.array[ ( original_copy.head + counter ) % ( original_copy.allocation_size ) ]; + + head = 0; + + tail = original_copy.Size(); + + allocation_size = original_copy.Size() + 1; + } + + return true; + } + + template + inline void Queue::Clear ( const char *file, unsigned int line ) + { + if ( allocation_size == 0 ) + return ; + + if (allocation_size > 32) + { + RakNet::OP_DELETE_ARRAY(array, file, line); + allocation_size = 0; + } + + head = 0; + tail = 0; + } + + template + void Queue::Compress ( const char *file, unsigned int line ) + { + queue_type* new_array; + unsigned int newAllocationSize; + if (allocation_size==0) + return; + + newAllocationSize=1; + while (newAllocationSize <= Size()) + newAllocationSize<<=1; // Must be a better way to do this but I'm too dumb to figure it out quickly :) + + new_array = RakNet::OP_NEW_ARRAY(newAllocationSize, file, line ); + + for (unsigned int counter=0; counter < Size(); ++counter) + new_array[counter] = array[(head + counter)%(allocation_size)]; + + tail=Size(); + allocation_size=newAllocationSize; + head=0; + + // Delete the old array and move the pointer to the new array + RakNet::OP_DELETE_ARRAY(array, file, line); + array=new_array; + } + + template + bool Queue::Find ( const queue_type &q ) + { + if ( allocation_size == 0 ) + return false; + + unsigned int counter = head; + + while ( counter != tail ) + { + if ( array[ counter ] == q ) + return true; + + counter = ( counter + 1 ) % allocation_size; + } + + return false; + } + + template + void Queue::ClearAndForceAllocation( int size, const char *file, unsigned int line ) + { + RakNet::OP_DELETE_ARRAY(array, file, line); + if (size>0) + array = RakNet::OP_NEW_ARRAY(size, file, line ); + else + array=0; + allocation_size = size; + head = 0; + tail = 0; + } + + template + inline queue_type& Queue::operator[] ( unsigned int position ) const + { +#ifdef _DEBUG + RakAssert( position < Size() ); +#endif + //return array[(head + position) % allocation_size]; + + if ( head + position >= allocation_size ) + return array[ head + position - allocation_size ]; + else + return array[ head + position ]; + } + + template + void Queue::RemoveAtIndex( unsigned int position ) + { +#ifdef _DEBUG + RakAssert( position < Size() ); + RakAssert( head != tail ); +#endif + + if ( head == tail || position >= Size() ) + return ; + + unsigned int index; + + unsigned int next; + + //index = (head + position) % allocation_size; + if ( head + position >= allocation_size ) + index = head + position - allocation_size; + else + index = head + position; + + //next = (index + 1) % allocation_size; + next = index + 1; + + if ( next == allocation_size ) + next = 0; + + while ( next != tail ) + { + // Overwrite the previous element + array[ index ] = array[ next ]; + index = next; + //next = (next + 1) % allocation_size; + + if ( ++next == allocation_size ) + next = 0; + } + + // Move the tail back + if ( tail == 0 ) + tail = allocation_size - 1; + else + --tail; + } +} // End namespace + diff --git a/Source/DS_QueueLinkedList.h b/Source/DS_QueueLinkedList.h index 6fc3adb78..da56ac9d3 100644 --- a/Source/DS_QueueLinkedList.h +++ b/Source/DS_QueueLinkedList.h @@ -1,110 +1,108 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_QueueLinkedList.h -/// \internal -/// \brief A queue implemented as a linked list. -/// - - -#ifndef __QUEUE_LINKED_LIST_H -#define __QUEUE_LINKED_LIST_H - -#include "DS_LinkedList.h" -#include "Export.h" -#include "RakMemoryOverride.h" - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - /// \brief A queue implemented using a linked list. Rarely used. - template - class RAK_DLL_EXPORT QueueLinkedList - { - - public: - QueueLinkedList(); - QueueLinkedList( const QueueLinkedList& original_copy ); - bool operator= ( const QueueLinkedList& original_copy ); - QueueType Pop( void ); - QueueType& Peek( void ); - QueueType& EndPeek( void ); - void Push( const QueueType& input ); - unsigned int Size( void ); - void Clear( void ); - void Compress( void ); - - private: - LinkedList data; - }; - - template - QueueLinkedList::QueueLinkedList() - { - } - - template - inline unsigned int QueueLinkedList::Size() - { - return data.Size(); - } - - template - inline QueueType QueueLinkedList::Pop( void ) - { - data.Beginning(); - return ( QueueType ) data.Pop(); - } - - template - inline QueueType& QueueLinkedList::Peek( void ) - { - data.Beginning(); - return ( QueueType ) data.Peek(); - } - - template - inline QueueType& QueueLinkedList::EndPeek( void ) - { - data.End(); - return ( QueueType ) data.Peek(); - } - - template - void QueueLinkedList::Push( const QueueType& input ) - { - data.End(); - data.Add( input ); - } - - template - QueueLinkedList::QueueLinkedList( const QueueLinkedList& original_copy ) - { - data = original_copy.data; - } - - template - bool QueueLinkedList::operator= ( const QueueLinkedList& original_copy ) - { - if ( ( &original_copy ) == this ) - return false; - - data = original_copy.data; - } - - template - void QueueLinkedList::Clear ( void ) - { - data.Clear(); - } -} // End namespace - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_QueueLinkedList.h +/// \internal +/// \brief A queue implemented as a linked list. +/// + + +#pragma once + +#include "DS_LinkedList.h" +#include "Export.h" +#include "RakMemoryOverride.h" + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + /// \brief A queue implemented using a linked list. Rarely used. + template + class RAK_DLL_EXPORT QueueLinkedList + { + + public: + QueueLinkedList(); + QueueLinkedList( const QueueLinkedList& original_copy ); + bool operator= ( const QueueLinkedList& original_copy ); + QueueType Pop( void ); + QueueType& Peek( void ); + QueueType& EndPeek( void ); + void Push( const QueueType& input ); + unsigned int Size( void ); + void Clear( void ); + void Compress( void ); + + private: + LinkedList data; + }; + + template + QueueLinkedList::QueueLinkedList() + { + } + + template + inline unsigned int QueueLinkedList::Size() + { + return data.Size(); + } + + template + inline QueueType QueueLinkedList::Pop( void ) + { + data.Beginning(); + return ( QueueType ) data.Pop(); + } + + template + inline QueueType& QueueLinkedList::Peek( void ) + { + data.Beginning(); + return ( QueueType ) data.Peek(); + } + + template + inline QueueType& QueueLinkedList::EndPeek( void ) + { + data.End(); + return ( QueueType ) data.Peek(); + } + + template + void QueueLinkedList::Push( const QueueType& input ) + { + data.End(); + data.Add( input ); + } + + template + QueueLinkedList::QueueLinkedList( const QueueLinkedList& original_copy ) + { + data = original_copy.data; + } + + template + bool QueueLinkedList::operator= ( const QueueLinkedList& original_copy ) + { + if ( ( &original_copy ) == this ) + return false; + + data = original_copy.data; + } + + template + void QueueLinkedList::Clear ( void ) + { + data.Clear(); + } +} // End namespace + diff --git a/Source/DS_RangeList.h b/Source/DS_RangeList.h index 2e6c97554..c8c71776c 100644 --- a/Source/DS_RangeList.h +++ b/Source/DS_RangeList.h @@ -1,243 +1,241 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_RangeList.h -/// \internal -/// \brief A queue implemented as a linked list. -/// - - -#ifndef __RANGE_LIST_H -#define __RANGE_LIST_H - -#include "DS_OrderedList.h" -#include "BitStream.h" -#include "RakMemoryOverride.h" -#include "RakAssert.h" - -namespace DataStructures -{ - template - struct RangeNode - { - RangeNode() {} - ~RangeNode() {} - RangeNode(range_type min, range_type max) {minIndex=min; maxIndex=max;} - range_type minIndex; - range_type maxIndex; - }; - - - template - int RangeNodeComp(const range_type &a, const RangeNode &b) - { - if (a - class RAK_DLL_EXPORT RangeList - { - public: - RangeList(); - ~RangeList(); - void Insert(range_type index); - void Clear(void); - unsigned Size(void) const; - unsigned RangeSum(void) const; - RakNet::BitSize_t Serialize(RakNet::BitStream *in, RakNet::BitSize_t maxBits, bool clearSerialized); - bool Deserialize(RakNet::BitStream *out); - - DataStructures::OrderedList , RangeNodeComp > ranges; - }; - - template - RakNet::BitSize_t RangeList::Serialize(RakNet::BitStream *in, RakNet::BitSize_t maxBits, bool clearSerialized) - { - RakAssert(ranges.Size() < (unsigned short)-1); - RakNet::BitStream tempBS; - RakNet::BitSize_t bitsWritten; - unsigned short countWritten; - unsigned i; - countWritten=0; - bitsWritten=0; - for (i=0; i < ranges.Size(); i++) - { - if ((int)sizeof(unsigned short)*8+bitsWritten+(int)sizeof(range_type)*8*2+1>maxBits) - break; - unsigned char minEqualsMax; - if (ranges[i].minIndex==ranges[i].maxIndex) - minEqualsMax=1; - else - minEqualsMax=0; - tempBS.Write(minEqualsMax); // Use one byte, intead of one bit, for speed, as this is done a lot - tempBS.Write(ranges[i].minIndex); - bitsWritten+=sizeof(range_type)*8+8; - if (ranges[i].minIndex!=ranges[i].maxIndex) - { - tempBS.Write(ranges[i].maxIndex); - bitsWritten+=sizeof(range_type)*8; - } - countWritten++; - } - - in->AlignWriteToByteBoundary(); - RakNet::BitSize_t before=in->GetWriteOffset(); - in->Write(countWritten); - bitsWritten+=in->GetWriteOffset()-before; - // RAKNET_DEBUG_PRINTF("%i ", in->GetNumberOfBitsUsed()); - in->Write(&tempBS, tempBS.GetNumberOfBitsUsed()); - // RAKNET_DEBUG_PRINTF("%i %i \n", tempBS.GetNumberOfBitsUsed(),in->GetNumberOfBitsUsed()); - - if (clearSerialized && countWritten) - { - unsigned rangeSize=ranges.Size(); - for (i=0; i < rangeSize-countWritten; i++) - { - ranges[i]=ranges[i+countWritten]; - } - ranges.RemoveFromEnd(countWritten); - } - - return bitsWritten; - } - template - bool RangeList::Deserialize(RakNet::BitStream *out) - { - ranges.Clear(true, _FILE_AND_LINE_); - unsigned short count; - out->AlignReadToByteBoundary(); - out->Read(count); - unsigned short i; - range_type min,max; - unsigned char maxEqualToMin=0; - - for (i=0; i < count; i++) - { - out->Read(maxEqualToMin); - if (out->Read(min)==false) - return false; - if (maxEqualToMin==false) - { - if (out->Read(max)==false) - return false; - if (max(min,max), _FILE_AND_LINE_); - } - return true; - } - - template - RangeList::RangeList() - { - RangeNodeComp(0, RangeNode()); - } - - template - RangeList::~RangeList() - { - Clear(); - } - - template - void RangeList::Insert(range_type index) - { - if (ranges.Size()==0) - { - ranges.Insert(index, RangeNode(index, index), true, _FILE_AND_LINE_); - return; - } - - bool objectExists; - unsigned insertionIndex=ranges.GetIndexFromKey(index, &objectExists); - if (insertionIndex==ranges.Size()) - { - if (index == ranges[insertionIndex-1].maxIndex+(range_type)1) - ranges[insertionIndex-1].maxIndex++; - else if (index > ranges[insertionIndex-1].maxIndex+(range_type)1) - { - // Insert at end - ranges.Insert(index, RangeNode(index, index), true, _FILE_AND_LINE_); - } - - return; - } - - if (index < ranges[insertionIndex].minIndex-(range_type)1) - { - // Insert here - ranges.InsertAtIndex(RangeNode(index, index), insertionIndex, _FILE_AND_LINE_); - - return; - } - else if (index == ranges[insertionIndex].minIndex-(range_type)1) - { - // Decrease minIndex and join left - ranges[insertionIndex].minIndex--; - if (insertionIndex>0 && ranges[insertionIndex-1].maxIndex+(range_type)1==ranges[insertionIndex].minIndex) - { - ranges[insertionIndex-1].maxIndex=ranges[insertionIndex].maxIndex; - ranges.RemoveAtIndex(insertionIndex); - } - - return; - } - else if (index >= ranges[insertionIndex].minIndex && index <= ranges[insertionIndex].maxIndex) - { - // Already exists - return; - } - else if (index == ranges[insertionIndex].maxIndex+(range_type)1) - { - // Increase maxIndex and join right - ranges[insertionIndex].maxIndex++; - if (insertionIndex - void RangeList::Clear(void) - { - ranges.Clear(true, _FILE_AND_LINE_); - } - - template - unsigned RangeList::Size(void) const - { - return ranges.Size(); - } - - template - unsigned RangeList::RangeSum(void) const - { - unsigned sum=0,i; - for (i=0; i < ranges.Size(); i++) - sum+=ranges[i].maxIndex-ranges[i].minIndex+1; - return sum; - } - -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_RangeList.h +/// \internal +/// \brief A queue implemented as a linked list. +/// + + +#pragma once + +#include "DS_OrderedList.h" +#include "BitStream.h" +#include "RakMemoryOverride.h" +#include "RakAssert.h" + +namespace DataStructures +{ + template + struct RangeNode + { + RangeNode() {} + ~RangeNode() {} + RangeNode(range_type min, range_type max) {minIndex=min; maxIndex=max;} + range_type minIndex; + range_type maxIndex; + }; + + + template + int RangeNodeComp(const range_type &a, const RangeNode &b) + { + if (a + class RAK_DLL_EXPORT RangeList + { + public: + RangeList(); + ~RangeList(); + void Insert(range_type index); + void Clear(void); + unsigned Size(void) const; + unsigned RangeSum(void) const; + RakNet::BitSize_t Serialize(RakNet::BitStream *in, RakNet::BitSize_t maxBits, bool clearSerialized); + bool Deserialize(RakNet::BitStream *out); + + DataStructures::OrderedList , RangeNodeComp > ranges; + }; + + template + RakNet::BitSize_t RangeList::Serialize(RakNet::BitStream *in, RakNet::BitSize_t maxBits, bool clearSerialized) + { + RakAssert(ranges.Size() < (unsigned short)-1); + RakNet::BitStream tempBS; + RakNet::BitSize_t bitsWritten; + unsigned short countWritten; + unsigned i; + countWritten=0; + bitsWritten=0; + for (i=0; i < ranges.Size(); i++) + { + if ((int)sizeof(unsigned short)*8+bitsWritten+(int)sizeof(range_type)*8*2+1>maxBits) + break; + unsigned char minEqualsMax; + if (ranges[i].minIndex==ranges[i].maxIndex) + minEqualsMax=1; + else + minEqualsMax=0; + tempBS.Write(minEqualsMax); // Use one byte, intead of one bit, for speed, as this is done a lot + tempBS.Write(ranges[i].minIndex); + bitsWritten+=sizeof(range_type)*8+8; + if (ranges[i].minIndex!=ranges[i].maxIndex) + { + tempBS.Write(ranges[i].maxIndex); + bitsWritten+=sizeof(range_type)*8; + } + countWritten++; + } + + in->AlignWriteToByteBoundary(); + RakNet::BitSize_t before=in->GetWriteOffset(); + in->Write(countWritten); + bitsWritten+=in->GetWriteOffset()-before; + // RAKNET_DEBUG_PRINTF("%i ", in->GetNumberOfBitsUsed()); + in->Write(&tempBS, tempBS.GetNumberOfBitsUsed()); + // RAKNET_DEBUG_PRINTF("%i %i \n", tempBS.GetNumberOfBitsUsed(),in->GetNumberOfBitsUsed()); + + if (clearSerialized && countWritten) + { + unsigned rangeSize=ranges.Size(); + for (i=0; i < rangeSize-countWritten; i++) + { + ranges[i]=ranges[i+countWritten]; + } + ranges.RemoveFromEnd(countWritten); + } + + return bitsWritten; + } + template + bool RangeList::Deserialize(RakNet::BitStream *out) + { + ranges.Clear(true, _FILE_AND_LINE_); + unsigned short count; + out->AlignReadToByteBoundary(); + out->Read(count); + unsigned short i; + range_type min,max; + unsigned char maxEqualToMin=0; + + for (i=0; i < count; i++) + { + out->Read(maxEqualToMin); + if (out->Read(min)==false) + return false; + if (maxEqualToMin==false) + { + if (out->Read(max)==false) + return false; + if (max(min,max), _FILE_AND_LINE_); + } + return true; + } + + template + RangeList::RangeList() + { + RangeNodeComp(0, RangeNode()); + } + + template + RangeList::~RangeList() + { + Clear(); + } + + template + void RangeList::Insert(range_type index) + { + if (ranges.Size()==0) + { + ranges.Insert(index, RangeNode(index, index), true, _FILE_AND_LINE_); + return; + } + + bool objectExists; + unsigned insertionIndex=ranges.GetIndexFromKey(index, &objectExists); + if (insertionIndex==ranges.Size()) + { + if (index == ranges[insertionIndex-1].maxIndex+(range_type)1) + ranges[insertionIndex-1].maxIndex++; + else if (index > ranges[insertionIndex-1].maxIndex+(range_type)1) + { + // Insert at end + ranges.Insert(index, RangeNode(index, index), true, _FILE_AND_LINE_); + } + + return; + } + + if (index < ranges[insertionIndex].minIndex-(range_type)1) + { + // Insert here + ranges.InsertAtIndex(RangeNode(index, index), insertionIndex, _FILE_AND_LINE_); + + return; + } + else if (index == ranges[insertionIndex].minIndex-(range_type)1) + { + // Decrease minIndex and join left + ranges[insertionIndex].minIndex--; + if (insertionIndex>0 && ranges[insertionIndex-1].maxIndex+(range_type)1==ranges[insertionIndex].minIndex) + { + ranges[insertionIndex-1].maxIndex=ranges[insertionIndex].maxIndex; + ranges.RemoveAtIndex(insertionIndex); + } + + return; + } + else if (index >= ranges[insertionIndex].minIndex && index <= ranges[insertionIndex].maxIndex) + { + // Already exists + return; + } + else if (index == ranges[insertionIndex].maxIndex+(range_type)1) + { + // Increase maxIndex and join right + ranges[insertionIndex].maxIndex++; + if (insertionIndex + void RangeList::Clear(void) + { + ranges.Clear(true, _FILE_AND_LINE_); + } + + template + unsigned RangeList::Size(void) const + { + return ranges.Size(); + } + + template + unsigned RangeList::RangeSum(void) const + { + unsigned sum=0,i; + for (i=0; i < ranges.Size(); i++) + sum+=ranges[i].maxIndex-ranges[i].minIndex+1; + return sum; + } + +} + diff --git a/Source/DS_Table.h b/Source/DS_Table.h index 7d03bc955..be91c333d 100644 --- a/Source/DS_Table.h +++ b/Source/DS_Table.h @@ -1,351 +1,349 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_Table.h -/// - - -#ifndef __TABLE_H -#define __TABLE_H - -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -#include "DS_List.h" -#include "DS_BPlusTree.h" -#include "RakMemoryOverride.h" -#include "Export.h" -#include "RakString.h" - -#define _TABLE_BPLUS_TREE_ORDER 16 -#define _TABLE_MAX_COLUMN_NAME_LENGTH 64 - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - - /// \brief Holds a set of columns, a set of rows, and rows times columns cells. - /// \details The table data structure is useful if you want to store a set of structures and perform queries on those structures.
- /// This is a relatively simple and fast implementation of the types of tables commonly used in databases.
- /// See TableSerializer to serialize data members of the table.
- /// See LightweightDatabaseClient and LightweightDatabaseServer to transmit the table over the network. - class RAK_DLL_EXPORT Table - { - public: - - enum ColumnType - { - // Cell::i used - NUMERIC, - - // Cell::c used to hold a null terminated string. - STRING, - - // Cell::c holds data. Cell::i holds data length of c in bytes. - BINARY, - - // Cell::c holds data. Not deallocated. Set manually by assigning ptr. - POINTER, - }; - - - /// Holds the actual data in the table - // Note: If this structure is changed the struct in the swig files need to be changed as well - struct RAK_DLL_EXPORT Cell - { - Cell(); - ~Cell(); - Cell(double numericValue, char *charValue, void *ptr, ColumnType type); - void SetByType(double numericValue, char *charValue, void *ptr, ColumnType type); - void Clear(void); - - /// Numeric - void Set(int input); - void Set(unsigned int input); - void Set(double input); - - /// String - void Set(const char *input); - - /// Binary - void Set(const char *input, int inputLength); - - /// Pointer - void SetPtr(void* p); - - /// Numeric - void Get(int *output); - void Get(double *output); - - /// String - void Get(char *output); - - /// Binary - void Get(char *output, int *outputLength); - - RakNet::RakString ToString(ColumnType columnType); - - // assignment operator and copy constructor - Cell& operator = ( const Cell& input ); - Cell( const Cell & input); - - ColumnType EstimateColumnType(void) const; - - bool isEmpty; - double i; - char *c; - void *ptr; - }; - - /// Stores the name and type of the column - /// \internal - // Note: If this structure is changed the struct in the swig files need to be changed as well - struct RAK_DLL_EXPORT ColumnDescriptor - { - ColumnDescriptor(); - ~ColumnDescriptor(); - ColumnDescriptor(const char cn[_TABLE_MAX_COLUMN_NAME_LENGTH],ColumnType ct); - - char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH]; - ColumnType columnType; - }; - - /// Stores the list of cells for this row, and a special flag used for internal sorting - // Note: If this structure is changed the struct in the swig files need to be changed as well - struct RAK_DLL_EXPORT Row - { - // list of cells - DataStructures::List cells; - - /// Numeric - void UpdateCell(unsigned columnIndex, double value); - - /// String - void UpdateCell(unsigned columnIndex, const char *str); - - /// Binary - void UpdateCell(unsigned columnIndex, int byteLength, const char *data); - }; - - // Operations to perform for cell comparison - enum FilterQueryType - { - QF_EQUAL, - QF_NOT_EQUAL, - QF_GREATER_THAN, - QF_GREATER_THAN_EQ, - QF_LESS_THAN, - QF_LESS_THAN_EQ, - QF_IS_EMPTY, - QF_NOT_EMPTY, - }; - - // Compare the cell value for a row at columnName to the cellValue using operation. - // Note: If this structure is changed the struct in the swig files need to be changed as well - struct RAK_DLL_EXPORT FilterQuery - { - FilterQuery(); - ~FilterQuery(); - FilterQuery(unsigned column, Cell *cell, FilterQueryType op); - - // If columnName is specified, columnIndex will be looked up using it. - char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH]; - unsigned columnIndex; - Cell *cellValue; - FilterQueryType operation; - }; - - /// Increasing or decreasing sort order - enum SortQueryType - { - QS_INCREASING_ORDER, - QS_DECREASING_ORDER, - }; - - // Sort on increasing or decreasing order for a particular column - // Note: If this structure is changed the struct in the swig files need to be changed as well - struct RAK_DLL_EXPORT SortQuery - { - /// The index of the table column we are sorting on - unsigned columnIndex; - - /// See SortQueryType - SortQueryType operation; - }; - - // Constructor - Table(); - - // Destructor - ~Table(); - - /// \brief Adds a column to the table - /// \param[in] columnName The name of the column - /// \param[in] columnType What type of data this column will hold - /// \return The index of the new column - unsigned AddColumn(const char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH], ColumnType columnType); - - /// \brief Removes a column by index - /// \param[in] columnIndex The index of the column to remove - void RemoveColumn(unsigned columnIndex); - - /// \brief Gets the index of a column by name - /// \details Column indices are stored in the order they are added. - /// \param[in] columnName The name of the column - /// \return The index of the column, or (unsigned)-1 if no such column - unsigned ColumnIndex(char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH]) const; - unsigned ColumnIndex(const char *columnName) const; - - /// \brief Gives the string name of the column at a certain index - /// \param[in] index The index of the column - /// \return The name of the column, or 0 if an invalid index - char* ColumnName(unsigned index) const; - - /// \brief Returns the type of a column, referenced by index - /// \param[in] index The index of the column - /// \return The type of the column - ColumnType GetColumnType(unsigned index) const; - - /// Returns the number of columns - /// \return The number of columns in the table - unsigned GetColumnCount(void) const; - - /// Returns the number of rows - /// \return The number of rows in the table - unsigned GetRowCount(void) const; - - /// \brief Adds a row to the table - /// \details New rows are added with empty values for all cells. However, if you specify initialCelLValues you can specify initial values - /// It's up to you to ensure that the values in the specific cells match the type of data used by that row - /// rowId can be considered the primary key for the row. It is much faster to lookup a row by its rowId than by searching keys. - /// rowId must be unique - /// Rows are stored in sorted order in the table, using rowId as the sort key - /// \param[in] rowId The UNIQUE primary key for the row. This can never be changed. - /// \param[in] initialCellValues Initial values to give the row (optional) - /// \return The newly added row - Table::Row* AddRow(unsigned rowId); - Table::Row* AddRow(unsigned rowId, DataStructures::List &initialCellValues); - Table::Row* AddRow(unsigned rowId, DataStructures::List &initialCellValues, bool copyCells=false); - - /// \brief Removes a row specified by rowId. - /// \param[in] rowId The ID of the row - /// \return true if the row was deleted. False if not. - bool RemoveRow(unsigned rowId); - - /// \brief Removes all the rows with IDs that the specified table also has. - /// \param[in] tableContainingRowIDs The IDs of the rows - void RemoveRows(Table *tableContainingRowIDs); - - /// \brief Updates a particular cell in the table. - /// \note If you are going to update many cells of a particular row, it is more efficient to call GetRow and perform the operations on the row directly. - /// \note Row pointers do not change, so you can also write directly to the rows for more efficiency. - /// \param[in] rowId The ID of the row - /// \param[in] columnIndex The column of the cell - /// \param[in] value The data to set - bool UpdateCell(unsigned rowId, unsigned columnIndex, int value); - bool UpdateCell(unsigned rowId, unsigned columnIndex, char *str); - bool UpdateCell(unsigned rowId, unsigned columnIndex, int byteLength, char *data); - bool UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, int value); - bool UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, char *str); - bool UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, int byteLength, char *data); - - /// \brief Note this is much less efficient to call than GetRow, then working with the cells directly. - /// Numeric, string, binary - void GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, int *output); - void GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, char *output); - void GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, char *output, int *outputLength); - - /// \brief Gets a row. More efficient to do this and access Row::cells than to repeatedly call GetCell. - /// You can also update cells in rows from this function. - /// \param[in] rowId The ID of the row - /// \return The desired row, or 0 if no such row. - Row* GetRowByID(unsigned rowId) const; - - /// \brief Gets a row at a specific index. - /// rowIndex should be less than GetRowCount() - /// \param[in] rowIndex The index of the row - /// \param[out] key The ID of the row returned - /// \return The desired row, or 0 if no such row. - Row* GetRowByIndex(unsigned rowIndex, unsigned *key) const; - - /// \brief Queries the table, optionally returning only a subset of columns and rows. - /// \param[in] columnSubset An array of column indices. Only columns in this array are returned. Pass 0 for all columns - /// \param[in] numColumnSubset The number of elements in \a columnSubset - /// \param[in] inclusionFilters An array of FilterQuery. All filters must pass for the row to be returned. - /// \param[in] numInclusionFilters The number of elements in \a inclusionFilters - /// \param[in] rowIds An arrow of row IDs. Only these rows with these IDs are returned. Pass 0 for all rows. - /// \param[in] numRowIDs The number of elements in \a rowIds - /// \param[out] result The result of the query. If no rows are returned, the table will only have columns. - void QueryTable(unsigned *columnIndicesSubset, unsigned numColumnSubset, FilterQuery *inclusionFilters, unsigned numInclusionFilters, unsigned *rowIds, unsigned numRowIDs, Table *result); - - /// \brief Sorts the table by rows - /// \details You can sort the table in ascending or descending order on one or more columns - /// Columns have precedence in the order they appear in the \a sortQueries array - /// If a row cell on column n has the same value as a a different row on column n, then the row will be compared on column n+1 - /// \param[in] sortQueries A list of SortQuery structures, defining the sorts to perform on the table - /// \param[in] numColumnSubset The number of elements in \a numSortQueries - /// \param[out] out The address of an array of Rows, which will receive the sorted output. The array must be long enough to contain all returned rows, up to GetRowCount() - void SortTable(Table::SortQuery *sortQueries, unsigned numSortQueries, Table::Row** out); - - /// \brief Frees all memory in the table. - void Clear(void); - - /// \brief Prints out the names of all the columns. - /// \param[out] out A pointer to an array of bytes which will hold the output. - /// \param[in] outLength The size of the \a out array - /// \param[in] columnDelineator What character to print to delineate columns - void PrintColumnHeaders(char *out, int outLength, char columnDelineator) const; - - /// \brief Writes a text representation of the row to \a out. - /// \param[out] out A pointer to an array of bytes which will hold the output. - /// \param[in] outLength The size of the \a out array - /// \param[in] columnDelineator What character to print to delineate columns - /// \param[in] printDelineatorForBinary Binary output is not printed. True to still print the delineator. - /// \param[in] inputRow The row to print - void PrintRow(char *out, int outLength, char columnDelineator, bool printDelineatorForBinary, Table::Row* inputRow) const; - - /// \brief Direct access to make things easier. - const DataStructures::List& GetColumns(void) const; - - /// \brief Direct access to make things easier. - const DataStructures::BPlusTree& GetRows(void) const; - - /// \brief Get the head of a linked list containing all the row data. - DataStructures::Page * GetListHead(void); - - /// \brief Get the first free row id. - /// This could be made more efficient. - unsigned GetAvailableRowId(void) const; - - Table& operator = ( const Table& input ); - - protected: - Table::Row* AddRowColumns(unsigned rowId, Row *row, DataStructures::List columnIndices); - - void DeleteRow(Row *row); - - void QueryRow(DataStructures::List &inclusionFilterColumnIndices, DataStructures::List &columnIndicesToReturn, unsigned key, Table::Row* row, FilterQuery *inclusionFilters, Table *result); - - // 16 is arbitrary and is the order of the BPlus tree. Higher orders are better for searching while lower orders are better for - // Insertions and deletions. - DataStructures::BPlusTree rows; - - // Columns in the table. - DataStructures::List columns; - }; -} - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_Table.h +/// + + +#pragma once + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +#include "DS_List.h" +#include "DS_BPlusTree.h" +#include "RakMemoryOverride.h" +#include "Export.h" +#include "RakString.h" + +#define _TABLE_BPLUS_TREE_ORDER 16 +#define _TABLE_MAX_COLUMN_NAME_LENGTH 64 + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + + /// \brief Holds a set of columns, a set of rows, and rows times columns cells. + /// \details The table data structure is useful if you want to store a set of structures and perform queries on those structures.
+ /// This is a relatively simple and fast implementation of the types of tables commonly used in databases.
+ /// See TableSerializer to serialize data members of the table.
+ /// See LightweightDatabaseClient and LightweightDatabaseServer to transmit the table over the network. + class RAK_DLL_EXPORT Table + { + public: + + enum ColumnType + { + // Cell::i used + NUMERIC, + + // Cell::c used to hold a null terminated string. + STRING, + + // Cell::c holds data. Cell::i holds data length of c in bytes. + BINARY, + + // Cell::c holds data. Not deallocated. Set manually by assigning ptr. + POINTER, + }; + + + /// Holds the actual data in the table + // Note: If this structure is changed the struct in the swig files need to be changed as well + struct RAK_DLL_EXPORT Cell + { + Cell(); + ~Cell(); + Cell(double numericValue, char *charValue, void *ptr, ColumnType type); + void SetByType(double numericValue, char *charValue, void *ptr, ColumnType type); + void Clear(void); + + /// Numeric + void Set(int input); + void Set(unsigned int input); + void Set(double input); + + /// String + void Set(const char *input); + + /// Binary + void Set(const char *input, int inputLength); + + /// Pointer + void SetPtr(void* p); + + /// Numeric + void Get(int *output); + void Get(double *output); + + /// String + void Get(char *output); + + /// Binary + void Get(char *output, int *outputLength); + + RakNet::RakString ToString(ColumnType columnType); + + // assignment operator and copy constructor + Cell& operator = ( const Cell& input ); + Cell( const Cell & input); + + ColumnType EstimateColumnType(void) const; + + bool isEmpty; + double i; + char *c; + void *ptr; + }; + + /// Stores the name and type of the column + /// \internal + // Note: If this structure is changed the struct in the swig files need to be changed as well + struct RAK_DLL_EXPORT ColumnDescriptor + { + ColumnDescriptor(); + ~ColumnDescriptor(); + ColumnDescriptor(const char cn[_TABLE_MAX_COLUMN_NAME_LENGTH],ColumnType ct); + + char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH]; + ColumnType columnType; + }; + + /// Stores the list of cells for this row, and a special flag used for internal sorting + // Note: If this structure is changed the struct in the swig files need to be changed as well + struct RAK_DLL_EXPORT Row + { + // list of cells + DataStructures::List cells; + + /// Numeric + void UpdateCell(unsigned columnIndex, double value); + + /// String + void UpdateCell(unsigned columnIndex, const char *str); + + /// Binary + void UpdateCell(unsigned columnIndex, int byteLength, const char *data); + }; + + // Operations to perform for cell comparison + enum FilterQueryType + { + QF_EQUAL, + QF_NOT_EQUAL, + QF_GREATER_THAN, + QF_GREATER_THAN_EQ, + QF_LESS_THAN, + QF_LESS_THAN_EQ, + QF_IS_EMPTY, + QF_NOT_EMPTY, + }; + + // Compare the cell value for a row at columnName to the cellValue using operation. + // Note: If this structure is changed the struct in the swig files need to be changed as well + struct RAK_DLL_EXPORT FilterQuery + { + FilterQuery(); + ~FilterQuery(); + FilterQuery(unsigned column, Cell *cell, FilterQueryType op); + + // If columnName is specified, columnIndex will be looked up using it. + char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH]; + unsigned columnIndex; + Cell *cellValue; + FilterQueryType operation; + }; + + /// Increasing or decreasing sort order + enum SortQueryType + { + QS_INCREASING_ORDER, + QS_DECREASING_ORDER, + }; + + // Sort on increasing or decreasing order for a particular column + // Note: If this structure is changed the struct in the swig files need to be changed as well + struct RAK_DLL_EXPORT SortQuery + { + /// The index of the table column we are sorting on + unsigned columnIndex; + + /// See SortQueryType + SortQueryType operation; + }; + + // Constructor + Table(); + + // Destructor + ~Table(); + + /// \brief Adds a column to the table + /// \param[in] columnName The name of the column + /// \param[in] columnType What type of data this column will hold + /// \return The index of the new column + unsigned AddColumn(const char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH], ColumnType columnType); + + /// \brief Removes a column by index + /// \param[in] columnIndex The index of the column to remove + void RemoveColumn(unsigned columnIndex); + + /// \brief Gets the index of a column by name + /// \details Column indices are stored in the order they are added. + /// \param[in] columnName The name of the column + /// \return The index of the column, or (unsigned)-1 if no such column + unsigned ColumnIndex(char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH]) const; + unsigned ColumnIndex(const char *columnName) const; + + /// \brief Gives the string name of the column at a certain index + /// \param[in] index The index of the column + /// \return The name of the column, or 0 if an invalid index + char* ColumnName(unsigned index) const; + + /// \brief Returns the type of a column, referenced by index + /// \param[in] index The index of the column + /// \return The type of the column + ColumnType GetColumnType(unsigned index) const; + + /// Returns the number of columns + /// \return The number of columns in the table + unsigned GetColumnCount(void) const; + + /// Returns the number of rows + /// \return The number of rows in the table + unsigned GetRowCount(void) const; + + /// \brief Adds a row to the table + /// \details New rows are added with empty values for all cells. However, if you specify initialCelLValues you can specify initial values + /// It's up to you to ensure that the values in the specific cells match the type of data used by that row + /// rowId can be considered the primary key for the row. It is much faster to lookup a row by its rowId than by searching keys. + /// rowId must be unique + /// Rows are stored in sorted order in the table, using rowId as the sort key + /// \param[in] rowId The UNIQUE primary key for the row. This can never be changed. + /// \param[in] initialCellValues Initial values to give the row (optional) + /// \return The newly added row + Table::Row* AddRow(unsigned rowId); + Table::Row* AddRow(unsigned rowId, DataStructures::List &initialCellValues); + Table::Row* AddRow(unsigned rowId, DataStructures::List &initialCellValues, bool copyCells=false); + + /// \brief Removes a row specified by rowId. + /// \param[in] rowId The ID of the row + /// \return true if the row was deleted. False if not. + bool RemoveRow(unsigned rowId); + + /// \brief Removes all the rows with IDs that the specified table also has. + /// \param[in] tableContainingRowIDs The IDs of the rows + void RemoveRows(Table *tableContainingRowIDs); + + /// \brief Updates a particular cell in the table. + /// \note If you are going to update many cells of a particular row, it is more efficient to call GetRow and perform the operations on the row directly. + /// \note Row pointers do not change, so you can also write directly to the rows for more efficiency. + /// \param[in] rowId The ID of the row + /// \param[in] columnIndex The column of the cell + /// \param[in] value The data to set + bool UpdateCell(unsigned rowId, unsigned columnIndex, int value); + bool UpdateCell(unsigned rowId, unsigned columnIndex, char *str); + bool UpdateCell(unsigned rowId, unsigned columnIndex, int byteLength, char *data); + bool UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, int value); + bool UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, char *str); + bool UpdateCellByIndex(unsigned rowIndex, unsigned columnIndex, int byteLength, char *data); + + /// \brief Note this is much less efficient to call than GetRow, then working with the cells directly. + /// Numeric, string, binary + void GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, int *output); + void GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, char *output); + void GetCellValueByIndex(unsigned rowIndex, unsigned columnIndex, char *output, int *outputLength); + + /// \brief Gets a row. More efficient to do this and access Row::cells than to repeatedly call GetCell. + /// You can also update cells in rows from this function. + /// \param[in] rowId The ID of the row + /// \return The desired row, or 0 if no such row. + Row* GetRowByID(unsigned rowId) const; + + /// \brief Gets a row at a specific index. + /// rowIndex should be less than GetRowCount() + /// \param[in] rowIndex The index of the row + /// \param[out] key The ID of the row returned + /// \return The desired row, or 0 if no such row. + Row* GetRowByIndex(unsigned rowIndex, unsigned *key) const; + + /// \brief Queries the table, optionally returning only a subset of columns and rows. + /// \param[in] columnSubset An array of column indices. Only columns in this array are returned. Pass 0 for all columns + /// \param[in] numColumnSubset The number of elements in \a columnSubset + /// \param[in] inclusionFilters An array of FilterQuery. All filters must pass for the row to be returned. + /// \param[in] numInclusionFilters The number of elements in \a inclusionFilters + /// \param[in] rowIds An arrow of row IDs. Only these rows with these IDs are returned. Pass 0 for all rows. + /// \param[in] numRowIDs The number of elements in \a rowIds + /// \param[out] result The result of the query. If no rows are returned, the table will only have columns. + void QueryTable(unsigned *columnIndicesSubset, unsigned numColumnSubset, FilterQuery *inclusionFilters, unsigned numInclusionFilters, unsigned *rowIds, unsigned numRowIDs, Table *result); + + /// \brief Sorts the table by rows + /// \details You can sort the table in ascending or descending order on one or more columns + /// Columns have precedence in the order they appear in the \a sortQueries array + /// If a row cell on column n has the same value as a a different row on column n, then the row will be compared on column n+1 + /// \param[in] sortQueries A list of SortQuery structures, defining the sorts to perform on the table + /// \param[in] numColumnSubset The number of elements in \a numSortQueries + /// \param[out] out The address of an array of Rows, which will receive the sorted output. The array must be long enough to contain all returned rows, up to GetRowCount() + void SortTable(Table::SortQuery *sortQueries, unsigned numSortQueries, Table::Row** out); + + /// \brief Frees all memory in the table. + void Clear(void); + + /// \brief Prints out the names of all the columns. + /// \param[out] out A pointer to an array of bytes which will hold the output. + /// \param[in] outLength The size of the \a out array + /// \param[in] columnDelineator What character to print to delineate columns + void PrintColumnHeaders(char *out, int outLength, char columnDelineator) const; + + /// \brief Writes a text representation of the row to \a out. + /// \param[out] out A pointer to an array of bytes which will hold the output. + /// \param[in] outLength The size of the \a out array + /// \param[in] columnDelineator What character to print to delineate columns + /// \param[in] printDelineatorForBinary Binary output is not printed. True to still print the delineator. + /// \param[in] inputRow The row to print + void PrintRow(char *out, int outLength, char columnDelineator, bool printDelineatorForBinary, Table::Row* inputRow) const; + + /// \brief Direct access to make things easier. + const DataStructures::List& GetColumns(void) const; + + /// \brief Direct access to make things easier. + const DataStructures::BPlusTree& GetRows(void) const; + + /// \brief Get the head of a linked list containing all the row data. + DataStructures::Page * GetListHead(void); + + /// \brief Get the first free row id. + /// This could be made more efficient. + unsigned GetAvailableRowId(void) const; + + Table& operator = ( const Table& input ); + + protected: + Table::Row* AddRowColumns(unsigned rowId, Row *row, DataStructures::List columnIndices); + + void DeleteRow(Row *row); + + void QueryRow(DataStructures::List &inclusionFilterColumnIndices, DataStructures::List &columnIndicesToReturn, unsigned key, Table::Row* row, FilterQuery *inclusionFilters, Table *result); + + // 16 is arbitrary and is the order of the BPlus tree. Higher orders are better for searching while lower orders are better for + // Insertions and deletions. + DataStructures::BPlusTree rows; + + // Columns in the table. + DataStructures::List columns; + }; +} + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif + diff --git a/Source/DS_ThreadsafeAllocatingQueue.h b/Source/DS_ThreadsafeAllocatingQueue.h index 547655e5c..12c67ad4a 100644 --- a/Source/DS_ThreadsafeAllocatingQueue.h +++ b/Source/DS_ThreadsafeAllocatingQueue.h @@ -1,184 +1,182 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_ThreadsafeAllocatingQueue.h -/// \internal -/// A threadsafe queue, that also uses a memory pool for allocation - -#ifndef __THREADSAFE_ALLOCATING_QUEUE -#define __THREADSAFE_ALLOCATING_QUEUE - -#include "DS_Queue.h" -#include "SimpleMutex.h" -#include "DS_MemoryPool.h" - -// #if defined(new) -// #pragma push_macro("new") -// #undef new -// #define RMO_NEW_UNDEF_ALLOCATING_QUEUE -// #endif - -namespace DataStructures -{ - -template -class RAK_DLL_EXPORT ThreadsafeAllocatingQueue -{ -public: - // Queue operations - void Push(structureType *s); - structureType *PopInaccurate(void); - structureType *Pop(void); - void SetPageSize(int size); - bool IsEmpty(void); - structureType * operator[] ( unsigned int position ); - void RemoveAtIndex( unsigned int position ); - unsigned int Size( void ); - - // Memory pool operations - structureType *Allocate(const char *file, unsigned int line); - void Deallocate(structureType *s, const char *file, unsigned int line); - void Clear(const char *file, unsigned int line); -protected: - - mutable MemoryPool memoryPool; - RakNet::SimpleMutex memoryPoolMutex; - Queue queue; - RakNet::SimpleMutex queueMutex; -}; - -template -void ThreadsafeAllocatingQueue::Push(structureType *s) -{ - queueMutex.Lock(); - queue.Push(s, _FILE_AND_LINE_ ); - queueMutex.Unlock(); -} - -template -structureType *ThreadsafeAllocatingQueue::PopInaccurate(void) -{ - structureType *s; - if (queue.IsEmpty()) - return 0; - queueMutex.Lock(); - if (queue.IsEmpty()==false) - s=queue.Pop(); - else - s=0; - queueMutex.Unlock(); - return s; -} - -template -structureType *ThreadsafeAllocatingQueue::Pop(void) -{ - structureType *s; - queueMutex.Lock(); - if (queue.IsEmpty()) - { - queueMutex.Unlock(); - return 0; - } - s=queue.Pop(); - queueMutex.Unlock(); - return s; -} - -template -structureType *ThreadsafeAllocatingQueue::Allocate(const char *file, unsigned int line) -{ - structureType *s; - memoryPoolMutex.Lock(); - s=memoryPool.Allocate(file, line); - memoryPoolMutex.Unlock(); - // Call new operator, memoryPool doesn't do this - s = new ((void*)s) structureType; - return s; -} -template -void ThreadsafeAllocatingQueue::Deallocate(structureType *s, const char *file, unsigned int line) -{ - // Call delete operator, memory pool doesn't do this - s->~structureType(); - memoryPoolMutex.Lock(); - memoryPool.Release(s, file, line); - memoryPoolMutex.Unlock(); -} - -template -void ThreadsafeAllocatingQueue::Clear(const char *file, unsigned int line) -{ - memoryPoolMutex.Lock(); - for (unsigned int i=0; i < queue.Size(); i++) - { - queue[i]->~structureType(); - memoryPool.Release(queue[i], file, line); - } - queue.Clear(file, line); - memoryPoolMutex.Unlock(); - memoryPoolMutex.Lock(); - memoryPool.Clear(file, line); - memoryPoolMutex.Unlock(); -} - -template -void ThreadsafeAllocatingQueue::SetPageSize(int size) -{ - memoryPool.SetPageSize(size); -} - -template -bool ThreadsafeAllocatingQueue::IsEmpty(void) -{ - bool isEmpty; - queueMutex.Lock(); - isEmpty=queue.IsEmpty(); - queueMutex.Unlock(); - return isEmpty; -} - -template -structureType * ThreadsafeAllocatingQueue::operator[] ( unsigned int position ) -{ - structureType *s; - queueMutex.Lock(); - s=queue[position]; - queueMutex.Unlock(); - return s; -} - -template -void ThreadsafeAllocatingQueue::RemoveAtIndex( unsigned int position ) -{ - queueMutex.Lock(); - queue.RemoveAtIndex(position); - queueMutex.Unlock(); -} - -template -unsigned int ThreadsafeAllocatingQueue::Size( void ) -{ - unsigned int s; - queueMutex.Lock(); - s=queue.Size(); - queueMutex.Unlock(); - return s; -} - -} - - -// #if defined(RMO_NEW_UNDEF_ALLOCATING_QUEUE) -// #pragma pop_macro("new") -// #undef RMO_NEW_UNDEF_ALLOCATING_QUEUE -// #endif - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_ThreadsafeAllocatingQueue.h +/// \internal +/// A threadsafe queue, that also uses a memory pool for allocation + +#pragma once + +#include "DS_Queue.h" +#include "SimpleMutex.h" +#include "DS_MemoryPool.h" + +// #if defined(new) +// #pragma push_macro("new") +// #undef new +// #define RMO_NEW_UNDEF_ALLOCATING_QUEUE +// #endif + +namespace DataStructures +{ + +template +class RAK_DLL_EXPORT ThreadsafeAllocatingQueue +{ +public: + // Queue operations + void Push(structureType *s); + structureType *PopInaccurate(void); + structureType *Pop(void); + void SetPageSize(int size); + bool IsEmpty(void); + structureType * operator[] ( unsigned int position ); + void RemoveAtIndex( unsigned int position ); + unsigned int Size( void ); + + // Memory pool operations + structureType *Allocate(const char *file, unsigned int line); + void Deallocate(structureType *s, const char *file, unsigned int line); + void Clear(const char *file, unsigned int line); +protected: + + mutable MemoryPool memoryPool; + RakNet::SimpleMutex memoryPoolMutex; + Queue queue; + RakNet::SimpleMutex queueMutex; +}; + +template +void ThreadsafeAllocatingQueue::Push(structureType *s) +{ + queueMutex.Lock(); + queue.Push(s, _FILE_AND_LINE_ ); + queueMutex.Unlock(); +} + +template +structureType *ThreadsafeAllocatingQueue::PopInaccurate(void) +{ + structureType *s; + if (queue.IsEmpty()) + return 0; + queueMutex.Lock(); + if (queue.IsEmpty()==false) + s=queue.Pop(); + else + s=0; + queueMutex.Unlock(); + return s; +} + +template +structureType *ThreadsafeAllocatingQueue::Pop(void) +{ + structureType *s; + queueMutex.Lock(); + if (queue.IsEmpty()) + { + queueMutex.Unlock(); + return 0; + } + s=queue.Pop(); + queueMutex.Unlock(); + return s; +} + +template +structureType *ThreadsafeAllocatingQueue::Allocate(const char *file, unsigned int line) +{ + structureType *s; + memoryPoolMutex.Lock(); + s=memoryPool.Allocate(file, line); + memoryPoolMutex.Unlock(); + // Call new operator, memoryPool doesn't do this + s = new ((void*)s) structureType; + return s; +} +template +void ThreadsafeAllocatingQueue::Deallocate(structureType *s, const char *file, unsigned int line) +{ + // Call delete operator, memory pool doesn't do this + s->~structureType(); + memoryPoolMutex.Lock(); + memoryPool.Release(s, file, line); + memoryPoolMutex.Unlock(); +} + +template +void ThreadsafeAllocatingQueue::Clear(const char *file, unsigned int line) +{ + memoryPoolMutex.Lock(); + for (unsigned int i=0; i < queue.Size(); i++) + { + queue[i]->~structureType(); + memoryPool.Release(queue[i], file, line); + } + queue.Clear(file, line); + memoryPoolMutex.Unlock(); + memoryPoolMutex.Lock(); + memoryPool.Clear(file, line); + memoryPoolMutex.Unlock(); +} + +template +void ThreadsafeAllocatingQueue::SetPageSize(int size) +{ + memoryPool.SetPageSize(size); +} + +template +bool ThreadsafeAllocatingQueue::IsEmpty(void) +{ + bool isEmpty; + queueMutex.Lock(); + isEmpty=queue.IsEmpty(); + queueMutex.Unlock(); + return isEmpty; +} + +template +structureType * ThreadsafeAllocatingQueue::operator[] ( unsigned int position ) +{ + structureType *s; + queueMutex.Lock(); + s=queue[position]; + queueMutex.Unlock(); + return s; +} + +template +void ThreadsafeAllocatingQueue::RemoveAtIndex( unsigned int position ) +{ + queueMutex.Lock(); + queue.RemoveAtIndex(position); + queueMutex.Unlock(); +} + +template +unsigned int ThreadsafeAllocatingQueue::Size( void ) +{ + unsigned int s; + queueMutex.Lock(); + s=queue.Size(); + queueMutex.Unlock(); + return s; +} + +} + + +// #if defined(RMO_NEW_UNDEF_ALLOCATING_QUEUE) +// #pragma pop_macro("new") +// #undef RMO_NEW_UNDEF_ALLOCATING_QUEUE +// #endif + diff --git a/Source/DS_Tree.h b/Source/DS_Tree.h index 74a898801..42cb0bf98 100644 --- a/Source/DS_Tree.h +++ b/Source/DS_Tree.h @@ -1,106 +1,104 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_Tree.h -/// \internal -/// \brief Just a regular tree -/// - - - -#ifndef __DS_TREE_H -#define __DS_TREE_H - -#include "Export.h" -#include "DS_List.h" -#include "DS_Queue.h" -#include "RakMemoryOverride.h" - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - template - class RAK_DLL_EXPORT Tree - { - public: - Tree(); - Tree(TreeType &inputData); - ~Tree(); - void LevelOrderTraversal(DataStructures::List &output); - void AddChild(TreeType &newData); - void DeleteDecendants(void); - - TreeType data; - DataStructures::List children; - }; - - template - Tree::Tree() - { - - } - - template - Tree::Tree(TreeType &inputData) - { - data=inputData; - } - - template - Tree::~Tree() - { - DeleteDecendants(); - } - - template - void Tree::LevelOrderTraversal(DataStructures::List &output) - { - unsigned i; - Tree *node; - DataStructures::Queue*> queue; - - for (i=0; i < children.Size(); i++) - queue.Push(children[i]); - - while (queue.Size()) - { - node=queue.Pop(); - output.Insert(node, _FILE_AND_LINE_); - for (i=0; i < node->children.Size(); i++) - queue.Push(node->children[i]); - } - } - - template - void Tree::AddChild(TreeType &newData) - { - children.Insert(RakNet::OP_NEW(newData, _FILE_AND_LINE_)); - } - - template - void Tree::DeleteDecendants(void) - { - /* - DataStructures::List output; - LevelOrderTraversal(output); - unsigned i; - for (i=0; i < output.Size(); i++) - RakNet::OP_DELETE(output[i], _FILE_AND_LINE_); -*/ - - // Already recursive to do this - unsigned int i; - for (i=0; i < children.Size(); i++) - RakNet::OP_DELETE(children[i], _FILE_AND_LINE_); - } -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_Tree.h +/// \internal +/// \brief Just a regular tree +/// + + + +#pragma once + +#include "Export.h" +#include "DS_List.h" +#include "DS_Queue.h" +#include "RakMemoryOverride.h" + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + template + class RAK_DLL_EXPORT Tree + { + public: + Tree(); + Tree(TreeType &inputData); + ~Tree(); + void LevelOrderTraversal(DataStructures::List &output); + void AddChild(TreeType &newData); + void DeleteDecendants(void); + + TreeType data; + DataStructures::List children; + }; + + template + Tree::Tree() + { + + } + + template + Tree::Tree(TreeType &inputData) + { + data=inputData; + } + + template + Tree::~Tree() + { + DeleteDecendants(); + } + + template + void Tree::LevelOrderTraversal(DataStructures::List &output) + { + unsigned i; + Tree *node; + DataStructures::Queue*> queue; + + for (i=0; i < children.Size(); i++) + queue.Push(children[i]); + + while (queue.Size()) + { + node=queue.Pop(); + output.Insert(node, _FILE_AND_LINE_); + for (i=0; i < node->children.Size(); i++) + queue.Push(node->children[i]); + } + } + + template + void Tree::AddChild(TreeType &newData) + { + children.Insert(RakNet::OP_NEW(newData, _FILE_AND_LINE_)); + } + + template + void Tree::DeleteDecendants(void) + { + /* + DataStructures::List output; + LevelOrderTraversal(output); + unsigned i; + for (i=0; i < output.Size(); i++) + RakNet::OP_DELETE(output[i], _FILE_AND_LINE_); +*/ + + // Already recursive to do this + unsigned int i; + for (i=0; i < children.Size(); i++) + RakNet::OP_DELETE(children[i], _FILE_AND_LINE_); + } +} + diff --git a/Source/DS_WeightedGraph.h b/Source/DS_WeightedGraph.h index f40fce3e4..0aa09b8a2 100644 --- a/Source/DS_WeightedGraph.h +++ b/Source/DS_WeightedGraph.h @@ -1,544 +1,542 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DS_WeightedGraph.h -/// \internal -/// \brief Weighted graph. -/// \details I'm assuming the indices are complex map types, rather than sequential numbers (which could be implemented much more efficiently). -/// - - -#ifndef __WEIGHTED_GRAPH_H -#define __WEIGHTED_GRAPH_H - -#include "DS_OrderedList.h" -#include "DS_Map.h" -#include "DS_Heap.h" -#include "DS_Queue.h" -#include "DS_Tree.h" -#include "RakAssert.h" -#include "RakMemoryOverride.h" -#ifdef _DEBUG -#include -#endif - -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - template - class RAK_DLL_EXPORT WeightedGraph - { - public: - static void IMPLEMENT_DEFAULT_COMPARISON(void) {DataStructures::defaultMapKeyComparison(node_type(),node_type());} - - WeightedGraph(); - ~WeightedGraph(); - WeightedGraph( const WeightedGraph& original_copy ); - WeightedGraph& operator= ( const WeightedGraph& original_copy ); - void AddNode(const node_type &node); - void RemoveNode(const node_type &node); - void AddConnection(const node_type &node1, const node_type &node2, weight_type weight); - void RemoveConnection(const node_type &node1, const node_type &node2); - bool HasConnection(const node_type &node1, const node_type &node2); - void Print(void); - void Clear(void); - bool GetShortestPath(DataStructures::List &path, node_type startNode, node_type endNode, weight_type INFINITE_WEIGHT); - bool GetSpanningTree(DataStructures::Tree &outTree, DataStructures::List *inputNodes, node_type startNode, weight_type INFINITE_WEIGHT ); - unsigned GetNodeCount(void) const; - unsigned GetConnectionCount(unsigned nodeIndex) const; - void GetConnectionAtIndex(unsigned nodeIndex, unsigned connectionIndex, node_type &outNode, weight_type &outWeight) const; - node_type GetNodeAtIndex(unsigned nodeIndex) const; - - protected: - void ClearDijkstra(void); - void GenerateDisjktraMatrix(node_type startNode, weight_type INFINITE_WEIGHT); - - DataStructures::Map *> adjacencyLists; - - // All these variables are for path finding with Dijkstra - // 08/23/06 Won't compile as a DLL inside this struct - // struct - // { - bool isValidPath; - node_type rootNode; - DataStructures::OrderedList costMatrixIndices; - weight_type *costMatrix; - node_type *leastNodeArray; - // } dijkstra; - - struct NodeAndParent - { - DataStructures::Tree*node; - DataStructures::Tree*parent; - }; - }; - - template - WeightedGraph::WeightedGraph() - { - isValidPath=false; - costMatrix=0; - } - - template - WeightedGraph::~WeightedGraph() - { - Clear(); - } - - template - WeightedGraph::WeightedGraph( const WeightedGraph& original_copy ) - { - adjacencyLists=original_copy.adjacencyLists; - - isValidPath=original_copy.isValidPath; - if (isValidPath) - { - rootNode=original_copy.rootNode; - costMatrixIndices=original_copy.costMatrixIndices; - costMatrix = RakNet::OP_NEW_ARRAY(costMatrixIndices.Size() * costMatrixIndices.Size(), _FILE_AND_LINE_ ); - leastNodeArray = RakNet::OP_NEW_ARRAY(costMatrixIndices.Size(), _FILE_AND_LINE_ ); - memcpy(costMatrix, original_copy.costMatrix, costMatrixIndices.Size() * costMatrixIndices.Size() * sizeof(weight_type)); - memcpy(leastNodeArray, original_copy.leastNodeArray, costMatrixIndices.Size() * sizeof(weight_type)); - } - } - - template - WeightedGraph& WeightedGraph::operator=( const WeightedGraph& original_copy ) - { - adjacencyLists=original_copy.adjacencyLists; - - isValidPath=original_copy.isValidPath; - if (isValidPath) - { - rootNode=original_copy.rootNode; - costMatrixIndices=original_copy.costMatrixIndices; - costMatrix = RakNet::OP_NEW_ARRAY(costMatrixIndices.Size() * costMatrixIndices.Size(), _FILE_AND_LINE_ ); - leastNodeArray = RakNet::OP_NEW_ARRAY(costMatrixIndices.Size(), _FILE_AND_LINE_ ); - memcpy(costMatrix, original_copy.costMatrix, costMatrixIndices.Size() * costMatrixIndices.Size() * sizeof(weight_type)); - memcpy(leastNodeArray, original_copy.leastNodeArray, costMatrixIndices.Size() * sizeof(weight_type)); - } - - return *this; - } - - template - void WeightedGraph::AddNode(const node_type &node) - { - adjacencyLists.SetNew(node, RakNet::OP_NEW >( _FILE_AND_LINE_) ); - } - - template - void WeightedGraph::RemoveNode(const node_type &node) - { - unsigned i; - DataStructures::Queue removeNodeQueue; - - removeNodeQueue.Push(node, _FILE_AND_LINE_ ); - while (removeNodeQueue.Size()) - { - RakNet::OP_DELETE(adjacencyLists.Pop(removeNodeQueue.Pop()), _FILE_AND_LINE_); - - // Remove this node from all of the other lists as well - for (i=0; i < adjacencyLists.Size(); i++) - { - adjacencyLists[i]->Delete(node); - -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - if (allow_unlinkedNodes==false && adjacencyLists[i]->Size()==0) - removeNodeQueue.Push(adjacencyLists.GetKeyAtIndex(i), _FILE_AND_LINE_ ); - } - } - - ClearDijkstra(); - } - - template - bool WeightedGraph::HasConnection(const node_type &node1, const node_type &node2) - { - if (node1==node2) - return false; - if (adjacencyLists.Has(node1)==false) - return false; - return adjacencyLists.Get(node1)->Has(node2); - } - - template - void WeightedGraph::AddConnection(const node_type &node1, const node_type &node2, weight_type weight) - { - if (node1==node2) - return; - - if (adjacencyLists.Has(node1)==false) - AddNode(node1); - adjacencyLists.Get(node1)->Set(node2, weight); - if (adjacencyLists.Has(node2)==false) - AddNode(node2); - adjacencyLists.Get(node2)->Set(node1, weight); - } - - template - void WeightedGraph::RemoveConnection(const node_type &node1, const node_type &node2) - { - adjacencyLists.Get(node2)->Delete(node1); - adjacencyLists.Get(node1)->Delete(node2); - -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - if (allow_unlinkedNodes==false) // If we do not allow _unlinked nodes, then if there are no connections, remove the node - { - if (adjacencyLists.Get(node1)->Size()==0) - RemoveNode(node1); // Will also remove node1 from the adjacency list of node2 - if (adjacencyLists.Has(node2) && adjacencyLists.Get(node2)->Size()==0) - RemoveNode(node2); - } - - ClearDijkstra(); - } - - template - void WeightedGraph::Clear(void) - { - unsigned i; - for (i=0; i < adjacencyLists.Size(); i++) - RakNet::OP_DELETE(adjacencyLists[i], _FILE_AND_LINE_); - adjacencyLists.Clear(); - - ClearDijkstra(); - } - - template - bool WeightedGraph::GetShortestPath(DataStructures::List &path, node_type startNode, node_type endNode, weight_type INFINITE_WEIGHT) - { - path.Clear(false, _FILE_AND_LINE_); - if (startNode==endNode) - { - path.Insert(startNode, _FILE_AND_LINE_); - path.Insert(endNode, _FILE_AND_LINE_); - return true; - } - - if (isValidPath==false || rootNode!=startNode) - { - ClearDijkstra(); - GenerateDisjktraMatrix(startNode, INFINITE_WEIGHT); - } - - // return the results - bool objectExists; - unsigned col,row; - weight_type currentWeight; - DataStructures::Queue outputQueue; - col=costMatrixIndices.GetIndexFromKey(endNode, &objectExists); - if (costMatrixIndices.Size()<2) - { - return false; - } - if (objectExists==false) - { - return false; - } - node_type vertex; - row=costMatrixIndices.Size()-2; - if (row==0) - { - path.Insert(startNode, _FILE_AND_LINE_); - path.Insert(endNode, _FILE_AND_LINE_); - return true; - } - currentWeight=costMatrix[row*adjacencyLists.Size() + col]; - if (currentWeight==INFINITE_WEIGHT) - { - // No path - return true; - } - vertex=endNode; - outputQueue.PushAtHead(vertex, 0, _FILE_AND_LINE_); - row--; -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - while (1) - { - while (costMatrix[row*adjacencyLists.Size() + col] == currentWeight) - { - if (row==0) - { - path.Insert(startNode, _FILE_AND_LINE_); - for (col=0; outputQueue.Size(); col++) - path.Insert(outputQueue.Pop(), _FILE_AND_LINE_); - return true; - } - --row; - } - - vertex=leastNodeArray[row]; - outputQueue.PushAtHead(vertex, 0, _FILE_AND_LINE_); - if (row==0) - break; - col=costMatrixIndices.GetIndexFromKey(vertex, &objectExists); - currentWeight=costMatrix[row*adjacencyLists.Size() + col]; - } - - path.Insert(startNode, _FILE_AND_LINE_); - for (col=0; outputQueue.Size(); col++) - path.Insert(outputQueue.Pop(), _FILE_AND_LINE_); - return true; - } - - template - node_type WeightedGraph::GetNodeAtIndex(unsigned nodeIndex) const - { - return adjacencyLists.GetKeyAtIndex(nodeIndex); - } - - template - unsigned WeightedGraph::GetNodeCount(void) const - { - return adjacencyLists.Size(); - } - - template - unsigned WeightedGraph::GetConnectionCount(unsigned nodeIndex) const - { - return adjacencyLists[nodeIndex]->Size(); - } - - template - void WeightedGraph::GetConnectionAtIndex(unsigned nodeIndex, unsigned connectionIndex, node_type &outNode, weight_type &outWeight) const - { - outWeight=adjacencyLists[nodeIndex]->operator[](connectionIndex); - outNode=adjacencyLists[nodeIndex]->GetKeyAtIndex(connectionIndex); - } - - template - bool WeightedGraph::GetSpanningTree(DataStructures::Tree &outTree, DataStructures::List *inputNodes, node_type startNode, weight_type INFINITE_WEIGHT ) - { - // Find the shortest path from the start node to each of the input nodes. Add this path to a new WeightedGraph if the result is reachable - DataStructures::List path; - DataStructures::WeightedGraph outGraph; - bool res; - unsigned i,j; - for (i=0; i < inputNodes->Size(); i++) - { - res=GetShortestPath(path, startNode, (*inputNodes)[i], INFINITE_WEIGHT); - if (res && path.Size()>0) - { - for (j=0; j < path.Size()-1; j++) - { - // Don't bother looking up the weight - outGraph.AddConnection(path[j], path[j+1], INFINITE_WEIGHT); - } - } - } - - // Copy the graph to a tree. - DataStructures::Queue nodesToProcess; - DataStructures::Tree *current; - DataStructures::Map *adjacencyList; - node_type key; - NodeAndParent nap, nap2; - outTree.DeleteDecendants(); - outTree.data=startNode; - current=&outTree; - if (outGraph.adjacencyLists.Has(startNode)==false) - return false; - adjacencyList = outGraph.adjacencyLists.Get(startNode); - - for (i=0; i < adjacencyList->Size(); i++) - { - nap2.node=RakNet::OP_NEW >( _FILE_AND_LINE_ ); - nap2.node->data=adjacencyList->GetKeyAtIndex(i); - nap2.parent=current; - nodesToProcess.Push(nap2, _FILE_AND_LINE_ ); - current->children.Insert(nap2.node, _FILE_AND_LINE_); - } - - while (nodesToProcess.Size()) - { - nap=nodesToProcess.Pop(); - current=nap.node; - adjacencyList = outGraph.adjacencyLists.Get(nap.node->data); - - for (i=0; i < adjacencyList->Size(); i++) - { - key=adjacencyList->GetKeyAtIndex(i); - if (key!=nap.parent->data) - { - nap2.node=RakNet::OP_NEW >( _FILE_AND_LINE_ ); - nap2.node->data=key; - nap2.parent=current; - nodesToProcess.Push(nap2, _FILE_AND_LINE_ ); - current->children.Insert(nap2.node, _FILE_AND_LINE_); - } - } - } - - return true; - } - - template - void WeightedGraph::GenerateDisjktraMatrix(node_type startNode, weight_type INFINITE_WEIGHT) - { - if (adjacencyLists.Size()==0) - return; - - costMatrix = RakNet::OP_NEW_ARRAY(adjacencyLists.Size() * adjacencyLists.Size(), _FILE_AND_LINE_ ); - leastNodeArray = RakNet::OP_NEW_ARRAY(adjacencyLists.Size(), _FILE_AND_LINE_ ); - - node_type currentNode; - unsigned col, row, row2, openSetIndex; - node_type adjacentKey; - unsigned adjacentIndex; - weight_type edgeWeight, currentNodeWeight, adjacentNodeWeight; - DataStructures::Map *adjacencyList; - DataStructures::Heap minHeap; - DataStructures::Map openSet; - - for (col=0; col < adjacencyLists.Size(); col++) - { - // This should be already sorted, so it's a bit inefficient to do an insertion sort, but what the heck - costMatrixIndices.Insert(adjacencyLists.GetKeyAtIndex(col),adjacencyLists.GetKeyAtIndex(col), true, _FILE_AND_LINE_); - } - for (col=0; col < adjacencyLists.Size() * adjacencyLists.Size(); col++) - costMatrix[col]=INFINITE_WEIGHT; - currentNode=startNode; - row=0; - currentNodeWeight=0; - rootNode=startNode; - - // Clear the starting node column - if (adjacencyLists.Size()) - { - adjacentIndex=adjacencyLists.GetIndexAtKey(startNode); - for (row2=0; row2 < adjacencyLists.Size(); row2++) - costMatrix[row2*adjacencyLists.Size() + adjacentIndex]=0; - } - - while (row < adjacencyLists.Size()-1) - { - adjacencyList = adjacencyLists.Get(currentNode); - // Go through all connections from the current node. If the new weight is less than the current weight, then update that weight. - for (col=0; col < adjacencyList->Size(); col++) - { - edgeWeight=(*adjacencyList)[col]; - adjacentKey=adjacencyList->GetKeyAtIndex(col); - adjacentIndex=adjacencyLists.GetIndexAtKey(adjacentKey); - adjacentNodeWeight=costMatrix[row*adjacencyLists.Size() + adjacentIndex]; - - if (currentNodeWeight + edgeWeight < adjacentNodeWeight) - { - // Update the weight for the adjacent node - for (row2=row; row2 < adjacencyLists.Size(); row2++) - costMatrix[row2*adjacencyLists.Size() + adjacentIndex]=currentNodeWeight + edgeWeight; - openSet.Set(adjacentKey, currentNodeWeight + edgeWeight); - } - } - - // Find the lowest in the open set - minHeap.Clear(true,_FILE_AND_LINE_); - for (openSetIndex=0; openSetIndex < openSet.Size(); openSetIndex++) - minHeap.Push(openSet[openSetIndex], openSet.GetKeyAtIndex(openSetIndex),_FILE_AND_LINE_); - - /* - unsigned i,j; - for (i=0; i < adjacencyLists.Size()-1; i++) - { - for (j=0; j < adjacencyLists.Size(); j++) - { - RAKNET_DEBUG_PRINTF("%2i ", costMatrix[i*adjacencyLists.Size() + j]); - } - RAKNET_DEBUG_PRINTF("Node=%i", leastNodeArray[i]); - RAKNET_DEBUG_PRINTF("\n"); - } - */ - - if (minHeap.Size()==0) - { - // Unreachable nodes - isValidPath=true; - return; - } - - currentNodeWeight=minHeap.PeekWeight(0); - leastNodeArray[row]=minHeap.Pop(0); - currentNode=leastNodeArray[row]; - openSet.Delete(currentNode); - row++; - } - - /* -#ifdef _DEBUG - unsigned i,j; - for (i=0; i < adjacencyLists.Size()-1; i++) - { - for (j=0; j < adjacencyLists.Size(); j++) - { - RAKNET_DEBUG_PRINTF("%2i ", costMatrix[i*adjacencyLists.Size() + j]); - } - RAKNET_DEBUG_PRINTF("Node=%i", leastNodeArray[i]); - RAKNET_DEBUG_PRINTF("\n"); - } -#endif - */ - - isValidPath=true; - } - - template - void WeightedGraph::ClearDijkstra(void) - { - if (isValidPath) - { - isValidPath=false; - RakNet::OP_DELETE_ARRAY(costMatrix, _FILE_AND_LINE_); - RakNet::OP_DELETE_ARRAY(leastNodeArray, _FILE_AND_LINE_); - costMatrixIndices.Clear(false, _FILE_AND_LINE_); - } - } - - template - void WeightedGraph::Print(void) - { -#ifdef _DEBUG - unsigned i,j; - for (i=0; i < adjacencyLists.Size(); i++) - { - //RAKNET_DEBUG_PRINTF("%i connected to ", i); - RAKNET_DEBUG_PRINTF("%s connected to ", adjacencyLists.GetKeyAtIndex(i).systemAddress.ToString()); - - if (adjacencyLists[i]->Size()==0) - RAKNET_DEBUG_PRINTF(""); - else - { - for (j=0; j < adjacencyLists[i]->Size(); j++) - // RAKNET_DEBUG_PRINTF("%i (%.2f) ", adjacencyLists.GetIndexAtKey(adjacencyLists[i]->GetKeyAtIndex(j)), (float) adjacencyLists[i]->operator[](j) ); - RAKNET_DEBUG_PRINTF("%s (%.2f) ", adjacencyLists[i]->GetKeyAtIndex(j).systemAddress.ToString(), (float) adjacencyLists[i]->operator[](j) ); - } - - RAKNET_DEBUG_PRINTF("\n"); - } -#endif - } -} - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DS_WeightedGraph.h +/// \internal +/// \brief Weighted graph. +/// \details I'm assuming the indices are complex map types, rather than sequential numbers (which could be implemented much more efficiently). +/// + + +#pragma once + +#include "DS_OrderedList.h" +#include "DS_Map.h" +#include "DS_Heap.h" +#include "DS_Queue.h" +#include "DS_Tree.h" +#include "RakAssert.h" +#include "RakMemoryOverride.h" +#ifdef _DEBUG +#include +#endif + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + template + class RAK_DLL_EXPORT WeightedGraph + { + public: + static void IMPLEMENT_DEFAULT_COMPARISON(void) {DataStructures::defaultMapKeyComparison(node_type(),node_type());} + + WeightedGraph(); + ~WeightedGraph(); + WeightedGraph( const WeightedGraph& original_copy ); + WeightedGraph& operator= ( const WeightedGraph& original_copy ); + void AddNode(const node_type &node); + void RemoveNode(const node_type &node); + void AddConnection(const node_type &node1, const node_type &node2, weight_type weight); + void RemoveConnection(const node_type &node1, const node_type &node2); + bool HasConnection(const node_type &node1, const node_type &node2); + void Print(void); + void Clear(void); + bool GetShortestPath(DataStructures::List &path, node_type startNode, node_type endNode, weight_type INFINITE_WEIGHT); + bool GetSpanningTree(DataStructures::Tree &outTree, DataStructures::List *inputNodes, node_type startNode, weight_type INFINITE_WEIGHT ); + unsigned GetNodeCount(void) const; + unsigned GetConnectionCount(unsigned nodeIndex) const; + void GetConnectionAtIndex(unsigned nodeIndex, unsigned connectionIndex, node_type &outNode, weight_type &outWeight) const; + node_type GetNodeAtIndex(unsigned nodeIndex) const; + + protected: + void ClearDijkstra(void); + void GenerateDisjktraMatrix(node_type startNode, weight_type INFINITE_WEIGHT); + + DataStructures::Map *> adjacencyLists; + + // All these variables are for path finding with Dijkstra + // 08/23/06 Won't compile as a DLL inside this struct + // struct + // { + bool isValidPath; + node_type rootNode; + DataStructures::OrderedList costMatrixIndices; + weight_type *costMatrix; + node_type *leastNodeArray; + // } dijkstra; + + struct NodeAndParent + { + DataStructures::Tree*node; + DataStructures::Tree*parent; + }; + }; + + template + WeightedGraph::WeightedGraph() + { + isValidPath=false; + costMatrix=0; + } + + template + WeightedGraph::~WeightedGraph() + { + Clear(); + } + + template + WeightedGraph::WeightedGraph( const WeightedGraph& original_copy ) + { + adjacencyLists=original_copy.adjacencyLists; + + isValidPath=original_copy.isValidPath; + if (isValidPath) + { + rootNode=original_copy.rootNode; + costMatrixIndices=original_copy.costMatrixIndices; + costMatrix = RakNet::OP_NEW_ARRAY(costMatrixIndices.Size() * costMatrixIndices.Size(), _FILE_AND_LINE_ ); + leastNodeArray = RakNet::OP_NEW_ARRAY(costMatrixIndices.Size(), _FILE_AND_LINE_ ); + memcpy(costMatrix, original_copy.costMatrix, costMatrixIndices.Size() * costMatrixIndices.Size() * sizeof(weight_type)); + memcpy(leastNodeArray, original_copy.leastNodeArray, costMatrixIndices.Size() * sizeof(weight_type)); + } + } + + template + WeightedGraph& WeightedGraph::operator=( const WeightedGraph& original_copy ) + { + adjacencyLists=original_copy.adjacencyLists; + + isValidPath=original_copy.isValidPath; + if (isValidPath) + { + rootNode=original_copy.rootNode; + costMatrixIndices=original_copy.costMatrixIndices; + costMatrix = RakNet::OP_NEW_ARRAY(costMatrixIndices.Size() * costMatrixIndices.Size(), _FILE_AND_LINE_ ); + leastNodeArray = RakNet::OP_NEW_ARRAY(costMatrixIndices.Size(), _FILE_AND_LINE_ ); + memcpy(costMatrix, original_copy.costMatrix, costMatrixIndices.Size() * costMatrixIndices.Size() * sizeof(weight_type)); + memcpy(leastNodeArray, original_copy.leastNodeArray, costMatrixIndices.Size() * sizeof(weight_type)); + } + + return *this; + } + + template + void WeightedGraph::AddNode(const node_type &node) + { + adjacencyLists.SetNew(node, RakNet::OP_NEW >( _FILE_AND_LINE_) ); + } + + template + void WeightedGraph::RemoveNode(const node_type &node) + { + unsigned i; + DataStructures::Queue removeNodeQueue; + + removeNodeQueue.Push(node, _FILE_AND_LINE_ ); + while (removeNodeQueue.Size()) + { + RakNet::OP_DELETE(adjacencyLists.Pop(removeNodeQueue.Pop()), _FILE_AND_LINE_); + + // Remove this node from all of the other lists as well + for (i=0; i < adjacencyLists.Size(); i++) + { + adjacencyLists[i]->Delete(node); + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + if (allow_unlinkedNodes==false && adjacencyLists[i]->Size()==0) + removeNodeQueue.Push(adjacencyLists.GetKeyAtIndex(i), _FILE_AND_LINE_ ); + } + } + + ClearDijkstra(); + } + + template + bool WeightedGraph::HasConnection(const node_type &node1, const node_type &node2) + { + if (node1==node2) + return false; + if (adjacencyLists.Has(node1)==false) + return false; + return adjacencyLists.Get(node1)->Has(node2); + } + + template + void WeightedGraph::AddConnection(const node_type &node1, const node_type &node2, weight_type weight) + { + if (node1==node2) + return; + + if (adjacencyLists.Has(node1)==false) + AddNode(node1); + adjacencyLists.Get(node1)->Set(node2, weight); + if (adjacencyLists.Has(node2)==false) + AddNode(node2); + adjacencyLists.Get(node2)->Set(node1, weight); + } + + template + void WeightedGraph::RemoveConnection(const node_type &node1, const node_type &node2) + { + adjacencyLists.Get(node2)->Delete(node1); + adjacencyLists.Get(node1)->Delete(node2); + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + if (allow_unlinkedNodes==false) // If we do not allow _unlinked nodes, then if there are no connections, remove the node + { + if (adjacencyLists.Get(node1)->Size()==0) + RemoveNode(node1); // Will also remove node1 from the adjacency list of node2 + if (adjacencyLists.Has(node2) && adjacencyLists.Get(node2)->Size()==0) + RemoveNode(node2); + } + + ClearDijkstra(); + } + + template + void WeightedGraph::Clear(void) + { + unsigned i; + for (i=0; i < adjacencyLists.Size(); i++) + RakNet::OP_DELETE(adjacencyLists[i], _FILE_AND_LINE_); + adjacencyLists.Clear(); + + ClearDijkstra(); + } + + template + bool WeightedGraph::GetShortestPath(DataStructures::List &path, node_type startNode, node_type endNode, weight_type INFINITE_WEIGHT) + { + path.Clear(false, _FILE_AND_LINE_); + if (startNode==endNode) + { + path.Insert(startNode, _FILE_AND_LINE_); + path.Insert(endNode, _FILE_AND_LINE_); + return true; + } + + if (isValidPath==false || rootNode!=startNode) + { + ClearDijkstra(); + GenerateDisjktraMatrix(startNode, INFINITE_WEIGHT); + } + + // return the results + bool objectExists; + unsigned col,row; + weight_type currentWeight; + DataStructures::Queue outputQueue; + col=costMatrixIndices.GetIndexFromKey(endNode, &objectExists); + if (costMatrixIndices.Size()<2) + { + return false; + } + if (objectExists==false) + { + return false; + } + node_type vertex; + row=costMatrixIndices.Size()-2; + if (row==0) + { + path.Insert(startNode, _FILE_AND_LINE_); + path.Insert(endNode, _FILE_AND_LINE_); + return true; + } + currentWeight=costMatrix[row*adjacencyLists.Size() + col]; + if (currentWeight==INFINITE_WEIGHT) + { + // No path + return true; + } + vertex=endNode; + outputQueue.PushAtHead(vertex, 0, _FILE_AND_LINE_); + row--; +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + while (1) + { + while (costMatrix[row*adjacencyLists.Size() + col] == currentWeight) + { + if (row==0) + { + path.Insert(startNode, _FILE_AND_LINE_); + for (col=0; outputQueue.Size(); col++) + path.Insert(outputQueue.Pop(), _FILE_AND_LINE_); + return true; + } + --row; + } + + vertex=leastNodeArray[row]; + outputQueue.PushAtHead(vertex, 0, _FILE_AND_LINE_); + if (row==0) + break; + col=costMatrixIndices.GetIndexFromKey(vertex, &objectExists); + currentWeight=costMatrix[row*adjacencyLists.Size() + col]; + } + + path.Insert(startNode, _FILE_AND_LINE_); + for (col=0; outputQueue.Size(); col++) + path.Insert(outputQueue.Pop(), _FILE_AND_LINE_); + return true; + } + + template + node_type WeightedGraph::GetNodeAtIndex(unsigned nodeIndex) const + { + return adjacencyLists.GetKeyAtIndex(nodeIndex); + } + + template + unsigned WeightedGraph::GetNodeCount(void) const + { + return adjacencyLists.Size(); + } + + template + unsigned WeightedGraph::GetConnectionCount(unsigned nodeIndex) const + { + return adjacencyLists[nodeIndex]->Size(); + } + + template + void WeightedGraph::GetConnectionAtIndex(unsigned nodeIndex, unsigned connectionIndex, node_type &outNode, weight_type &outWeight) const + { + outWeight=adjacencyLists[nodeIndex]->operator[](connectionIndex); + outNode=adjacencyLists[nodeIndex]->GetKeyAtIndex(connectionIndex); + } + + template + bool WeightedGraph::GetSpanningTree(DataStructures::Tree &outTree, DataStructures::List *inputNodes, node_type startNode, weight_type INFINITE_WEIGHT ) + { + // Find the shortest path from the start node to each of the input nodes. Add this path to a new WeightedGraph if the result is reachable + DataStructures::List path; + DataStructures::WeightedGraph outGraph; + bool res; + unsigned i,j; + for (i=0; i < inputNodes->Size(); i++) + { + res=GetShortestPath(path, startNode, (*inputNodes)[i], INFINITE_WEIGHT); + if (res && path.Size()>0) + { + for (j=0; j < path.Size()-1; j++) + { + // Don't bother looking up the weight + outGraph.AddConnection(path[j], path[j+1], INFINITE_WEIGHT); + } + } + } + + // Copy the graph to a tree. + DataStructures::Queue nodesToProcess; + DataStructures::Tree *current; + DataStructures::Map *adjacencyList; + node_type key; + NodeAndParent nap, nap2; + outTree.DeleteDecendants(); + outTree.data=startNode; + current=&outTree; + if (outGraph.adjacencyLists.Has(startNode)==false) + return false; + adjacencyList = outGraph.adjacencyLists.Get(startNode); + + for (i=0; i < adjacencyList->Size(); i++) + { + nap2.node=RakNet::OP_NEW >( _FILE_AND_LINE_ ); + nap2.node->data=adjacencyList->GetKeyAtIndex(i); + nap2.parent=current; + nodesToProcess.Push(nap2, _FILE_AND_LINE_ ); + current->children.Insert(nap2.node, _FILE_AND_LINE_); + } + + while (nodesToProcess.Size()) + { + nap=nodesToProcess.Pop(); + current=nap.node; + adjacencyList = outGraph.adjacencyLists.Get(nap.node->data); + + for (i=0; i < adjacencyList->Size(); i++) + { + key=adjacencyList->GetKeyAtIndex(i); + if (key!=nap.parent->data) + { + nap2.node=RakNet::OP_NEW >( _FILE_AND_LINE_ ); + nap2.node->data=key; + nap2.parent=current; + nodesToProcess.Push(nap2, _FILE_AND_LINE_ ); + current->children.Insert(nap2.node, _FILE_AND_LINE_); + } + } + } + + return true; + } + + template + void WeightedGraph::GenerateDisjktraMatrix(node_type startNode, weight_type INFINITE_WEIGHT) + { + if (adjacencyLists.Size()==0) + return; + + costMatrix = RakNet::OP_NEW_ARRAY(adjacencyLists.Size() * adjacencyLists.Size(), _FILE_AND_LINE_ ); + leastNodeArray = RakNet::OP_NEW_ARRAY(adjacencyLists.Size(), _FILE_AND_LINE_ ); + + node_type currentNode; + unsigned col, row, row2, openSetIndex; + node_type adjacentKey; + unsigned adjacentIndex; + weight_type edgeWeight, currentNodeWeight, adjacentNodeWeight; + DataStructures::Map *adjacencyList; + DataStructures::Heap minHeap; + DataStructures::Map openSet; + + for (col=0; col < adjacencyLists.Size(); col++) + { + // This should be already sorted, so it's a bit inefficient to do an insertion sort, but what the heck + costMatrixIndices.Insert(adjacencyLists.GetKeyAtIndex(col),adjacencyLists.GetKeyAtIndex(col), true, _FILE_AND_LINE_); + } + for (col=0; col < adjacencyLists.Size() * adjacencyLists.Size(); col++) + costMatrix[col]=INFINITE_WEIGHT; + currentNode=startNode; + row=0; + currentNodeWeight=0; + rootNode=startNode; + + // Clear the starting node column + if (adjacencyLists.Size()) + { + adjacentIndex=adjacencyLists.GetIndexAtKey(startNode); + for (row2=0; row2 < adjacencyLists.Size(); row2++) + costMatrix[row2*adjacencyLists.Size() + adjacentIndex]=0; + } + + while (row < adjacencyLists.Size()-1) + { + adjacencyList = adjacencyLists.Get(currentNode); + // Go through all connections from the current node. If the new weight is less than the current weight, then update that weight. + for (col=0; col < adjacencyList->Size(); col++) + { + edgeWeight=(*adjacencyList)[col]; + adjacentKey=adjacencyList->GetKeyAtIndex(col); + adjacentIndex=adjacencyLists.GetIndexAtKey(adjacentKey); + adjacentNodeWeight=costMatrix[row*adjacencyLists.Size() + adjacentIndex]; + + if (currentNodeWeight + edgeWeight < adjacentNodeWeight) + { + // Update the weight for the adjacent node + for (row2=row; row2 < adjacencyLists.Size(); row2++) + costMatrix[row2*adjacencyLists.Size() + adjacentIndex]=currentNodeWeight + edgeWeight; + openSet.Set(adjacentKey, currentNodeWeight + edgeWeight); + } + } + + // Find the lowest in the open set + minHeap.Clear(true,_FILE_AND_LINE_); + for (openSetIndex=0; openSetIndex < openSet.Size(); openSetIndex++) + minHeap.Push(openSet[openSetIndex], openSet.GetKeyAtIndex(openSetIndex),_FILE_AND_LINE_); + + /* + unsigned i,j; + for (i=0; i < adjacencyLists.Size()-1; i++) + { + for (j=0; j < adjacencyLists.Size(); j++) + { + RAKNET_DEBUG_PRINTF("%2i ", costMatrix[i*adjacencyLists.Size() + j]); + } + RAKNET_DEBUG_PRINTF("Node=%i", leastNodeArray[i]); + RAKNET_DEBUG_PRINTF("\n"); + } + */ + + if (minHeap.Size()==0) + { + // Unreachable nodes + isValidPath=true; + return; + } + + currentNodeWeight=minHeap.PeekWeight(0); + leastNodeArray[row]=minHeap.Pop(0); + currentNode=leastNodeArray[row]; + openSet.Delete(currentNode); + row++; + } + + /* +#ifdef _DEBUG + unsigned i,j; + for (i=0; i < adjacencyLists.Size()-1; i++) + { + for (j=0; j < adjacencyLists.Size(); j++) + { + RAKNET_DEBUG_PRINTF("%2i ", costMatrix[i*adjacencyLists.Size() + j]); + } + RAKNET_DEBUG_PRINTF("Node=%i", leastNodeArray[i]); + RAKNET_DEBUG_PRINTF("\n"); + } +#endif + */ + + isValidPath=true; + } + + template + void WeightedGraph::ClearDijkstra(void) + { + if (isValidPath) + { + isValidPath=false; + RakNet::OP_DELETE_ARRAY(costMatrix, _FILE_AND_LINE_); + RakNet::OP_DELETE_ARRAY(leastNodeArray, _FILE_AND_LINE_); + costMatrixIndices.Clear(false, _FILE_AND_LINE_); + } + } + + template + void WeightedGraph::Print(void) + { +#ifdef _DEBUG + unsigned i,j; + for (i=0; i < adjacencyLists.Size(); i++) + { + //RAKNET_DEBUG_PRINTF("%i connected to ", i); + RAKNET_DEBUG_PRINTF("%s connected to ", adjacencyLists.GetKeyAtIndex(i).systemAddress.ToString()); + + if (adjacencyLists[i]->Size()==0) + RAKNET_DEBUG_PRINTF(""); + else + { + for (j=0; j < adjacencyLists[i]->Size(); j++) + // RAKNET_DEBUG_PRINTF("%i (%.2f) ", adjacencyLists.GetIndexAtKey(adjacencyLists[i]->GetKeyAtIndex(j)), (float) adjacencyLists[i]->operator[](j) ); + RAKNET_DEBUG_PRINTF("%s (%.2f) ", adjacencyLists[i]->GetKeyAtIndex(j).systemAddress.ToString(), (float) adjacencyLists[i]->operator[](j) ); + } + + RAKNET_DEBUG_PRINTF("\n"); + } +#endif + } +} + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif + diff --git a/Source/DataCompressor.h b/Source/DataCompressor.h index 0950d2113..2ea40b8e5 100644 --- a/Source/DataCompressor.h +++ b/Source/DataCompressor.h @@ -1,40 +1,38 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DataCompressor.h -/// \brief DataCompressor does compression on a block of data. -/// \details Not very good compression, but it's small and fast so is something you can use per-message at runtime. -/// - - -#ifndef __DATA_COMPRESSOR_H -#define __DATA_COMPRESSOR_H - -#include "RakMemoryOverride.h" -#include "DS_HuffmanEncodingTree.h" -#include "Export.h" - -namespace RakNet -{ - -/// \brief Does compression on a block of data. Not very good compression, but it's small and fast so is something you can compute at runtime. -class RAK_DLL_EXPORT DataCompressor -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(DataCompressor) - - static void Compress( unsigned char *userData, unsigned sizeInBytes, RakNet::BitStream * output ); - static unsigned DecompressAndAllocate( RakNet::BitStream * input, unsigned char **output ); -}; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DataCompressor.h +/// \brief DataCompressor does compression on a block of data. +/// \details Not very good compression, but it's small and fast so is something you can use per-message at runtime. +/// + + +#pragma once + +#include "RakMemoryOverride.h" +#include "DS_HuffmanEncodingTree.h" +#include "Export.h" + +namespace RakNet +{ + +/// \brief Does compression on a block of data. Not very good compression, but it's small and fast so is something you can compute at runtime. +class RAK_DLL_EXPORT DataCompressor +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(DataCompressor) + + static void Compress( unsigned char *userData, unsigned sizeInBytes, RakNet::BitStream * output ); + static unsigned DecompressAndAllocate( RakNet::BitStream * input, unsigned char **output ); +}; + +} // namespace RakNet + diff --git a/Source/DirectoryDeltaTransfer.h b/Source/DirectoryDeltaTransfer.h index 27d8504e1..0ec6d0f0b 100644 --- a/Source/DirectoryDeltaTransfer.h +++ b/Source/DirectoryDeltaTransfer.h @@ -1,172 +1,170 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DirectoryDeltaTransfer.h -/// \brief Simple class to send changes between directories. -/// \details In essence, a simple autopatcher that can be used for transmitting levels, skins, etc. -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_DirectoryDeltaTransfer==1 && _RAKNET_SUPPORT_FileOperations==1 - -#ifndef __DIRECTORY_DELTA_TRANSFER_H -#define __DIRECTORY_DELTA_TRANSFER_H - -#include "RakMemoryOverride.h" -#include "RakNetTypes.h" -#include "Export.h" -#include "PluginInterface2.h" -#include "DS_Map.h" -#include "PacketPriority.h" - -/// \defgroup DIRECTORY_DELTA_TRANSFER_GROUP DirectoryDeltaTransfer -/// \brief Simple class to send changes between directories -/// \details -/// \ingroup PLUGINS_GROUP - -/// \brief Simple class to send changes between directories. In essence, a simple autopatcher that can be used for transmitting levels, skins, etc. -/// \details -/// \sa AutopatcherClient class for database driven patching, including binary deltas and search by date. -/// -/// To use, first set the path to your application. For example "C:/Games/MyRPG/"
-/// To allow other systems to download files, call AddUploadsFromSubdirectory, where the parameter is a path relative
-/// to the path to your application. This includes subdirectories.
-/// For example:
-/// SetApplicationDirectory("C:/Games/MyRPG/");
-/// AddUploadsFromSubdirectory("Mods/Skins/");
-/// would allow downloads from
-/// "C:/Games/MyRPG/Mods/Skins/*.*" as well as "C:/Games/MyRPG/Mods/Skins/Level1/*.*"
-/// It would NOT allow downloads from C:/Games/MyRPG/Levels, nor would it allow downloads from C:/Windows
-/// While pathToApplication can be anything you want, applicationSubdirectory must match either partially or fully between systems. -/// \ingroup DIRECTORY_DELTA_TRANSFER_GROUP - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; -class FileList; -struct Packet; -struct InternalPacket; -struct DownloadRequest; -class FileListTransfer; -class FileListTransferCBInterface; -class FileListProgress; -class IncrementalReadInterface; - -class RAK_DLL_EXPORT DirectoryDeltaTransfer : public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(DirectoryDeltaTransfer) - - // Constructor - DirectoryDeltaTransfer(); - - // Destructor - virtual ~DirectoryDeltaTransfer(); - - /// \brief This plugin has a dependency on the FileListTransfer plugin, which it uses to actually send the files. - /// \details So you need an instance of that plugin registered with RakPeerInterface, and a pointer to that interface should be passed here. - /// \param[in] flt A pointer to a registered instance of FileListTransfer - void SetFileListTransferPlugin(FileListTransfer *flt); - - /// \brief Set the local root directory to base all file uploads and downloads off of. - /// \param[in] pathToApplication This path will be prepended to \a applicationSubdirectory in AddUploadsFromSubdirectory to find the actual path on disk. - void SetApplicationDirectory(const char *pathToApplication); - - /// \brief What parameters to use for the RakPeerInterface::Send() call when uploading files. - /// \param[in] _priority See RakPeerInterface::Send() - /// \param[in] _orderingChannel See RakPeerInterface::Send() - void SetUploadSendParameters(PacketPriority _priority, char _orderingChannel); - - /// \brief Add all files in the specified subdirectory recursively. - /// \details \a subdir is appended to \a pathToApplication in SetApplicationDirectory(). - /// All files in the resultant directory and subdirectories are then hashed so that users can download them. - /// \pre You must call SetFileListTransferPlugin with a valid FileListTransfer plugin - /// \param[in] subdir Concatenated with pathToApplication to form the final path from which to allow uploads. - void AddUploadsFromSubdirectory(const char *subdir); - - /// \brief Downloads files from the matching parameter \a subdir in AddUploadsFromSubdirectory. - /// \details \a subdir must contain all starting characters in \a subdir in AddUploadsFromSubdirectory - /// Therefore, - /// AddUploadsFromSubdirectory("Levels/Level1/"); would allow you to download using DownloadFromSubdirectory("Levels/Level1/Textures/"... - /// but it would NOT allow you to download from DownloadFromSubdirectory("Levels/"... or DownloadFromSubdirectory("Levels/Level2/"... - /// \pre You must call SetFileListTransferPlugin with a valid FileListTransfer plugin - /// \note Blocking. Will block while hashes of the local files are generated - /// \param[in] subdir A directory passed to AddUploadsFromSubdirectory on the remote system. The passed dir can be more specific than the remote dir. - /// \param[in] outputSubdir The directory to write the output to. Usually this will match \a subdir but it can be different if you want. - /// \param[in] prependAppDirToOutputSubdir True to prepend outputSubdir with pathToApplication when determining the final output path. Usually you want this to be true. - /// \param[in] host The address of the remote system to send the message to. - /// \param[in] onFileCallback Callback to call per-file (optional). When fileIndex+1==setCount in the callback then the download is done - /// \param[in] _priority See RakPeerInterface::Send() - /// \param[in] _orderingChannel See RakPeerInterface::Send() - /// \param[in] cb Callback to get progress updates. Pass 0 to not use. - /// \return A set ID, identifying this download set. Returns 65535 on host unreachable. - unsigned short DownloadFromSubdirectory(const char *subdir, const char *outputSubdir, bool prependAppDirToOutputSubdir, SystemAddress host, FileListTransferCBInterface *onFileCallback, PacketPriority _priority, char _orderingChannel, FileListProgress *cb); - - /// \brief Downloads files from the matching parameter \a subdir in AddUploadsFromSubdirectory. - /// \details \a subdir must contain all starting characters in \a subdir in AddUploadsFromSubdirectory - /// Therefore, - /// AddUploadsFromSubdirectory("Levels/Level1/"); would allow you to download using DownloadFromSubdirectory("Levels/Level1/Textures/"... - /// but it would NOT allow you to download from DownloadFromSubdirectory("Levels/"... or DownloadFromSubdirectory("Levels/Level2/"... - /// \pre You must call SetFileListTransferPlugin with a valid FileListTransfer plugin - /// \note Nonblocking, but requires call to GenerateHashes() - /// \param[in] localFiles Hashes of local files already on the harddrive. Populate with GenerateHashes(), which you may wish to call from a thread - /// \param[in] subdir A directory passed to AddUploadsFromSubdirectory on the remote system. The passed dir can be more specific than the remote dir. - /// \param[in] outputSubdir The directory to write the output to. Usually this will match \a subdir but it can be different if you want. - /// \param[in] prependAppDirToOutputSubdir True to prepend outputSubdir with pathToApplication when determining the final output path. Usually you want this to be true. - /// \param[in] host The address of the remote system to send the message to. - /// \param[in] onFileCallback Callback to call per-file (optional). When fileIndex+1==setCount in the callback then the download is done - /// \param[in] _priority See RakPeerInterface::Send() - /// \param[in] _orderingChannel See RakPeerInterface::Send() - /// \param[in] cb Callback to get progress updates. Pass 0 to not use. - /// \return A set ID, identifying this download set. Returns 65535 on host unreachable. - unsigned short DownloadFromSubdirectory(FileList &localFiles, const char *subdir, const char *outputSubdir, bool prependAppDirToOutputSubdir, SystemAddress host, FileListTransferCBInterface *onFileCallback, PacketPriority _priority, char _orderingChannel, FileListProgress *cb); - - /// Hash files already on the harddrive, in preparation for a call to DownloadFromSubdirectory(). Passed to second version of DownloadFromSubdirectory() - /// This is slow, and it is exposed so you can call it from a thread before calling DownloadFromSubdirectory() - /// \param[out] localFiles List of hashed files populated from \a outputSubdir and \a prependAppDirToOutputSubdir - /// \param[in] outputSubdir The directory to write the output to. Usually this will match \a subdir but it can be different if you want. - /// \param[in] prependAppDirToOutputSubdir True to prepend outputSubdir with pathToApplication when determining the final output path. Usually you want this to be true. - void GenerateHashes(FileList &localFiles, const char *outputSubdir, bool prependAppDirToOutputSubdir); - - /// \brief Clear all allowed uploads previously set with AddUploadsFromSubdirectory - void ClearUploads(void); - - /// \brief Returns how many files are available for upload - /// \return How many files are available for upload - unsigned GetNumberOfFilesForUpload(void) const; - - /// \brief Normally, if a remote system requests files, those files are all loaded into memory and sent immediately. - /// \details This function allows the files to be read in incremental chunks, saving memory - /// \param[in] _incrementalReadInterface If a file in \a fileList has no data, filePullInterface will be used to read the file in chunks of size \a chunkSize - /// \param[in] _chunkSize How large of a block of a file to send at once - void SetDownloadRequestIncrementalReadInterface(IncrementalReadInterface *_incrementalReadInterface, unsigned int _chunkSize); - - /// \internal For plugin handling - virtual PluginReceiveResult OnReceive(Packet *packet); -protected: - void OnDownloadRequest(Packet *packet); - - char applicationDirectory[512]; - FileListTransfer *fileListTransfer; - FileList *availableUploads; - PacketPriority priority; - char orderingChannel; - IncrementalReadInterface *incrementalReadInterface; - unsigned int chunkSize; -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DirectoryDeltaTransfer.h +/// \brief Simple class to send changes between directories. +/// \details In essence, a simple autopatcher that can be used for transmitting levels, skins, etc. +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_DirectoryDeltaTransfer==1 && _RAKNET_SUPPORT_FileOperations==1 + +#pragma once + +#include "RakMemoryOverride.h" +#include "RakNetTypes.h" +#include "Export.h" +#include "PluginInterface2.h" +#include "DS_Map.h" +#include "PacketPriority.h" + +/// \defgroup DIRECTORY_DELTA_TRANSFER_GROUP DirectoryDeltaTransfer +/// \brief Simple class to send changes between directories +/// \details +/// \ingroup PLUGINS_GROUP + +/// \brief Simple class to send changes between directories. In essence, a simple autopatcher that can be used for transmitting levels, skins, etc. +/// \details +/// \sa AutopatcherClient class for database driven patching, including binary deltas and search by date. +/// +/// To use, first set the path to your application. For example "C:/Games/MyRPG/"
+/// To allow other systems to download files, call AddUploadsFromSubdirectory, where the parameter is a path relative
+/// to the path to your application. This includes subdirectories.
+/// For example:
+/// SetApplicationDirectory("C:/Games/MyRPG/");
+/// AddUploadsFromSubdirectory("Mods/Skins/");
+/// would allow downloads from
+/// "C:/Games/MyRPG/Mods/Skins/*.*" as well as "C:/Games/MyRPG/Mods/Skins/Level1/*.*"
+/// It would NOT allow downloads from C:/Games/MyRPG/Levels, nor would it allow downloads from C:/Windows
+/// While pathToApplication can be anything you want, applicationSubdirectory must match either partially or fully between systems. +/// \ingroup DIRECTORY_DELTA_TRANSFER_GROUP + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; +class FileList; +struct Packet; +struct InternalPacket; +struct DownloadRequest; +class FileListTransfer; +class FileListTransferCBInterface; +class FileListProgress; +class IncrementalReadInterface; + +class RAK_DLL_EXPORT DirectoryDeltaTransfer : public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(DirectoryDeltaTransfer) + + // Constructor + DirectoryDeltaTransfer(); + + // Destructor + virtual ~DirectoryDeltaTransfer(); + + /// \brief This plugin has a dependency on the FileListTransfer plugin, which it uses to actually send the files. + /// \details So you need an instance of that plugin registered with RakPeerInterface, and a pointer to that interface should be passed here. + /// \param[in] flt A pointer to a registered instance of FileListTransfer + void SetFileListTransferPlugin(FileListTransfer *flt); + + /// \brief Set the local root directory to base all file uploads and downloads off of. + /// \param[in] pathToApplication This path will be prepended to \a applicationSubdirectory in AddUploadsFromSubdirectory to find the actual path on disk. + void SetApplicationDirectory(const char *pathToApplication); + + /// \brief What parameters to use for the RakPeerInterface::Send() call when uploading files. + /// \param[in] _priority See RakPeerInterface::Send() + /// \param[in] _orderingChannel See RakPeerInterface::Send() + void SetUploadSendParameters(PacketPriority _priority, char _orderingChannel); + + /// \brief Add all files in the specified subdirectory recursively. + /// \details \a subdir is appended to \a pathToApplication in SetApplicationDirectory(). + /// All files in the resultant directory and subdirectories are then hashed so that users can download them. + /// \pre You must call SetFileListTransferPlugin with a valid FileListTransfer plugin + /// \param[in] subdir Concatenated with pathToApplication to form the final path from which to allow uploads. + void AddUploadsFromSubdirectory(const char *subdir); + + /// \brief Downloads files from the matching parameter \a subdir in AddUploadsFromSubdirectory. + /// \details \a subdir must contain all starting characters in \a subdir in AddUploadsFromSubdirectory + /// Therefore, + /// AddUploadsFromSubdirectory("Levels/Level1/"); would allow you to download using DownloadFromSubdirectory("Levels/Level1/Textures/"... + /// but it would NOT allow you to download from DownloadFromSubdirectory("Levels/"... or DownloadFromSubdirectory("Levels/Level2/"... + /// \pre You must call SetFileListTransferPlugin with a valid FileListTransfer plugin + /// \note Blocking. Will block while hashes of the local files are generated + /// \param[in] subdir A directory passed to AddUploadsFromSubdirectory on the remote system. The passed dir can be more specific than the remote dir. + /// \param[in] outputSubdir The directory to write the output to. Usually this will match \a subdir but it can be different if you want. + /// \param[in] prependAppDirToOutputSubdir True to prepend outputSubdir with pathToApplication when determining the final output path. Usually you want this to be true. + /// \param[in] host The address of the remote system to send the message to. + /// \param[in] onFileCallback Callback to call per-file (optional). When fileIndex+1==setCount in the callback then the download is done + /// \param[in] _priority See RakPeerInterface::Send() + /// \param[in] _orderingChannel See RakPeerInterface::Send() + /// \param[in] cb Callback to get progress updates. Pass 0 to not use. + /// \return A set ID, identifying this download set. Returns 65535 on host unreachable. + unsigned short DownloadFromSubdirectory(const char *subdir, const char *outputSubdir, bool prependAppDirToOutputSubdir, SystemAddress host, FileListTransferCBInterface *onFileCallback, PacketPriority _priority, char _orderingChannel, FileListProgress *cb); + + /// \brief Downloads files from the matching parameter \a subdir in AddUploadsFromSubdirectory. + /// \details \a subdir must contain all starting characters in \a subdir in AddUploadsFromSubdirectory + /// Therefore, + /// AddUploadsFromSubdirectory("Levels/Level1/"); would allow you to download using DownloadFromSubdirectory("Levels/Level1/Textures/"... + /// but it would NOT allow you to download from DownloadFromSubdirectory("Levels/"... or DownloadFromSubdirectory("Levels/Level2/"... + /// \pre You must call SetFileListTransferPlugin with a valid FileListTransfer plugin + /// \note Nonblocking, but requires call to GenerateHashes() + /// \param[in] localFiles Hashes of local files already on the harddrive. Populate with GenerateHashes(), which you may wish to call from a thread + /// \param[in] subdir A directory passed to AddUploadsFromSubdirectory on the remote system. The passed dir can be more specific than the remote dir. + /// \param[in] outputSubdir The directory to write the output to. Usually this will match \a subdir but it can be different if you want. + /// \param[in] prependAppDirToOutputSubdir True to prepend outputSubdir with pathToApplication when determining the final output path. Usually you want this to be true. + /// \param[in] host The address of the remote system to send the message to. + /// \param[in] onFileCallback Callback to call per-file (optional). When fileIndex+1==setCount in the callback then the download is done + /// \param[in] _priority See RakPeerInterface::Send() + /// \param[in] _orderingChannel See RakPeerInterface::Send() + /// \param[in] cb Callback to get progress updates. Pass 0 to not use. + /// \return A set ID, identifying this download set. Returns 65535 on host unreachable. + unsigned short DownloadFromSubdirectory(FileList &localFiles, const char *subdir, const char *outputSubdir, bool prependAppDirToOutputSubdir, SystemAddress host, FileListTransferCBInterface *onFileCallback, PacketPriority _priority, char _orderingChannel, FileListProgress *cb); + + /// Hash files already on the harddrive, in preparation for a call to DownloadFromSubdirectory(). Passed to second version of DownloadFromSubdirectory() + /// This is slow, and it is exposed so you can call it from a thread before calling DownloadFromSubdirectory() + /// \param[out] localFiles List of hashed files populated from \a outputSubdir and \a prependAppDirToOutputSubdir + /// \param[in] outputSubdir The directory to write the output to. Usually this will match \a subdir but it can be different if you want. + /// \param[in] prependAppDirToOutputSubdir True to prepend outputSubdir with pathToApplication when determining the final output path. Usually you want this to be true. + void GenerateHashes(FileList &localFiles, const char *outputSubdir, bool prependAppDirToOutputSubdir); + + /// \brief Clear all allowed uploads previously set with AddUploadsFromSubdirectory + void ClearUploads(void); + + /// \brief Returns how many files are available for upload + /// \return How many files are available for upload + unsigned GetNumberOfFilesForUpload(void) const; + + /// \brief Normally, if a remote system requests files, those files are all loaded into memory and sent immediately. + /// \details This function allows the files to be read in incremental chunks, saving memory + /// \param[in] _incrementalReadInterface If a file in \a fileList has no data, filePullInterface will be used to read the file in chunks of size \a chunkSize + /// \param[in] _chunkSize How large of a block of a file to send at once + void SetDownloadRequestIncrementalReadInterface(IncrementalReadInterface *_incrementalReadInterface, unsigned int _chunkSize); + + /// \internal For plugin handling + virtual PluginReceiveResult OnReceive(Packet *packet); +protected: + void OnDownloadRequest(Packet *packet); + + char applicationDirectory[512]; + FileListTransfer *fileListTransfer; + FileList *availableUploads; + PacketPriority priority; + char orderingChannel; + IncrementalReadInterface *incrementalReadInterface; + unsigned int chunkSize; +}; + +} // namespace RakNet + +#endif + diff --git a/Source/DynDNS.h b/Source/DynDNS.h index d8ade0195..01ac50991 100644 --- a/Source/DynDNS.h +++ b/Source/DynDNS.h @@ -1,110 +1,108 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file DynDNS.h -/// \brief Helper to class to update DynDNS -/// This can be used to determine what permissions are should be allowed to the other system -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_DynDNS==1 && _RAKNET_SUPPORT_TCPInterface==1 - -#ifndef __DYN_DNS_H -#define __DYN_DNS_H - -#include "RakString.h" - -namespace RakNet -{ - -class TCPInterface; - -enum DynDnsResultCode -{ - // ----- Success ----- - RC_SUCCESS, - RC_DNS_ALREADY_SET, // RakNet detects no action is needed - - // ----- Ignorable failure (treat same as success) ----- - RC_NO_CHANGE, // DynDNS detects no action is needed (treated as abuse though) - - // ----- User error ----- - RC_NOT_DONATOR, // You have to pay to do this - RC_NO_HOST, // This host does not exist at all - RC_BAD_AUTH, // You set the wrong password - RC_NOT_YOURS, // This is not your host - - // ----- Permanent failure ----- - RC_ABUSE, // Your host has been blocked, too many failures disable your account - RC_TCP_FAILED_TO_START, // TCP port already in use - RC_TCP_DID_NOT_CONNECT, // DynDNS down? - RC_UNKNOWN_RESULT, // DynDNS returned a result code that was not documented as of 12/4/2010 on http://www.dyndns.com/developers/specs/flow.pdf - RC_PARSING_FAILURE, // Can't read the result returned, format change? - RC_CONNECTION_LOST_WITHOUT_RESPONSE, // Lost the connection to DynDNS while communicating - RC_BAD_AGENT, // ??? - RC_BAD_SYS, // ??? - RC_DNS_ERROR, // ??? - RC_NOT_FQDN, // ??? - RC_NUM_HOST, // ??? - RC_911, // ??? - RC_DYNDNS_TIMEOUT // DynDNS did not respond -}; - -// Can only process one at a time with the current implementation -class RAK_DLL_EXPORT DynDNS -{ -public: - DynDNS(); - ~DynDNS(); - - // Pass 0 for newIPAddress to autodetect whatever you are uploading from - // usernameAndPassword should be in the format username:password - void UpdateHostIPAsynch(const char *dnsHost, const char *newIPAddress, const char *usernameAndPassword ); - void Update(void); - - // Output - bool IsRunning(void) const {return connectPhase!=CP_IDLE;} - bool IsCompleted(void) const {return connectPhase==CP_IDLE;} - RakNet::DynDnsResultCode GetCompletedResultCode(void) {return result;} - const char *GetCompletedDescription(void) const {return resultDescription;} - bool WasResultSuccessful(void) const {return result==RC_SUCCESS || result==RC_DNS_ALREADY_SET || result==RC_NO_CHANGE;} - char *GetMyPublicIP(void) const {return (char*) myIPStr;} // We get our public IP as part of the process. This is valid once completed - -protected: - void Stop(void); - void SetCompleted(RakNet::DynDnsResultCode _result, const char *_resultDescription) {Stop(); result=_result; resultDescription=_resultDescription;} - - enum ConnectPhase - { - CP_CONNECTING_TO_CHECKIP, - CP_WAITING_FOR_CHECKIP_RESPONSE, - CP_CONNECTING_TO_DYNDNS, - CP_WAITING_FOR_DYNDNS_RESPONSE, - CP_IDLE - }; - - TCPInterface *tcp; - RakNet::RakString getString; - SystemAddress serverAddress; - ConnectPhase connectPhase; - RakNet::RakString host; - RakNet::Time phaseTimeout; - SystemAddress checkIpAddress; - const char *resultDescription; - RakNet::DynDnsResultCode result; - char myIPStr[32]; -}; - -} // namespace RakNet - -#endif // __DYN_DNS_H - -#endif // _RAKNET_SUPPORT_DynDNS +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file DynDNS.h +/// \brief Helper to class to update DynDNS +/// This can be used to determine what permissions are should be allowed to the other system +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_DynDNS==1 && _RAKNET_SUPPORT_TCPInterface==1 + +#pragma once + +#include "RakString.h" + +namespace RakNet +{ + +class TCPInterface; + +enum DynDnsResultCode +{ + // ----- Success ----- + RC_SUCCESS, + RC_DNS_ALREADY_SET, // RakNet detects no action is needed + + // ----- Ignorable failure (treat same as success) ----- + RC_NO_CHANGE, // DynDNS detects no action is needed (treated as abuse though) + + // ----- User error ----- + RC_NOT_DONATOR, // You have to pay to do this + RC_NO_HOST, // This host does not exist at all + RC_BAD_AUTH, // You set the wrong password + RC_NOT_YOURS, // This is not your host + + // ----- Permanent failure ----- + RC_ABUSE, // Your host has been blocked, too many failures disable your account + RC_TCP_FAILED_TO_START, // TCP port already in use + RC_TCP_DID_NOT_CONNECT, // DynDNS down? + RC_UNKNOWN_RESULT, // DynDNS returned a result code that was not documented as of 12/4/2010 on http://www.dyndns.com/developers/specs/flow.pdf + RC_PARSING_FAILURE, // Can't read the result returned, format change? + RC_CONNECTION_LOST_WITHOUT_RESPONSE, // Lost the connection to DynDNS while communicating + RC_BAD_AGENT, // ??? + RC_BAD_SYS, // ??? + RC_DNS_ERROR, // ??? + RC_NOT_FQDN, // ??? + RC_NUM_HOST, // ??? + RC_911, // ??? + RC_DYNDNS_TIMEOUT // DynDNS did not respond +}; + +// Can only process one at a time with the current implementation +class RAK_DLL_EXPORT DynDNS +{ +public: + DynDNS(); + ~DynDNS(); + + // Pass 0 for newIPAddress to autodetect whatever you are uploading from + // usernameAndPassword should be in the format username:password + void UpdateHostIPAsynch(const char *dnsHost, const char *newIPAddress, const char *usernameAndPassword ); + void Update(void); + + // Output + bool IsRunning(void) const {return connectPhase!=CP_IDLE;} + bool IsCompleted(void) const {return connectPhase==CP_IDLE;} + RakNet::DynDnsResultCode GetCompletedResultCode(void) {return result;} + const char *GetCompletedDescription(void) const {return resultDescription;} + bool WasResultSuccessful(void) const {return result==RC_SUCCESS || result==RC_DNS_ALREADY_SET || result==RC_NO_CHANGE;} + char *GetMyPublicIP(void) const {return (char*) myIPStr;} // We get our public IP as part of the process. This is valid once completed + +protected: + void Stop(void); + void SetCompleted(RakNet::DynDnsResultCode _result, const char *_resultDescription) {Stop(); result=_result; resultDescription=_resultDescription;} + + enum ConnectPhase + { + CP_CONNECTING_TO_CHECKIP, + CP_WAITING_FOR_CHECKIP_RESPONSE, + CP_CONNECTING_TO_DYNDNS, + CP_WAITING_FOR_DYNDNS_RESPONSE, + CP_IDLE + }; + + TCPInterface *tcp; + RakNet::RakString getString; + SystemAddress serverAddress; + ConnectPhase connectPhase; + RakNet::RakString host; + RakNet::Time phaseTimeout; + SystemAddress checkIpAddress; + const char *resultDescription; + RakNet::DynDnsResultCode result; + char myIPStr[32]; +}; + +} // namespace RakNet + +#endif // __DYN_DNS_H + diff --git a/Source/EmailSender.h b/Source/EmailSender.h index 6a8e4a216..a1ebc76e2 100644 --- a/Source/EmailSender.h +++ b/Source/EmailSender.h @@ -1,65 +1,63 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file EmailSender.h -/// \brief Rudimentary class to send email from code. Don't expect anything fancy. -/// - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_EmailSender==1 && _RAKNET_SUPPORT_TCPInterface==1 && _RAKNET_SUPPORT_FileOperations==1 - -#ifndef __EMAIL_SENDER_H -#define __EMAIL_SENDER_H - -#include "RakNetTypes.h" -#include "RakMemoryOverride.h" -#include "Export.h" -#include "Rand.h" -#include "TCPInterface.h" - -namespace RakNet -{ -/// Forward declarations -class FileList; -class TCPInterface; - -/// \brief Rudimentary class to send email from code. -class RAK_DLL_EXPORT EmailSender -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(EmailSender) - - /// \brief Sends an email. - /// \param[in] hostAddress The address of the email server. - /// \param[in] hostPort The port of the email server (usually 25) - /// \param[in] sender The email address you are sending from. - /// \param[in] recipient The email address you are sending to. - /// \param[in] senderName The email address you claim to be sending from - /// \param[in] recipientName The email address you claim to be sending to - /// \param[in] subject Email subject - /// \param[in] body Email body - /// \param[in] attachedFiles List of files to attach to the email. (Can be 0 to send none). - /// \param[in] doPrintf true to output SMTP info to console(for debugging?) - /// \param[in] password Used if the server uses AUTHENTICATE PLAIN over TLS (such as gmail) - /// \return 0 on success, otherwise a string indicating the error message - const char *Send(const char *hostAddress, unsigned short hostPort, const char *sender, const char *recipient, const char *senderName, const char *recipientName, const char *subject, const char *body, FileList *attachedFiles, bool doPrintf, const char *password); - -protected: - const char *GetResponse(TCPInterface *tcpInterface, const SystemAddress &emailServer, bool doPrintf); - RakNetRandom rakNetRandom; -}; - -} // namespace RakNet - -#endif - - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file EmailSender.h +/// \brief Rudimentary class to send email from code. Don't expect anything fancy. +/// + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_EmailSender==1 && _RAKNET_SUPPORT_TCPInterface==1 && _RAKNET_SUPPORT_FileOperations==1 + +#pragma once + +#include "RakNetTypes.h" +#include "RakMemoryOverride.h" +#include "Export.h" +#include "Rand.h" +#include "TCPInterface.h" + +namespace RakNet +{ +/// Forward declarations +class FileList; +class TCPInterface; + +/// \brief Rudimentary class to send email from code. +class RAK_DLL_EXPORT EmailSender +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(EmailSender) + + /// \brief Sends an email. + /// \param[in] hostAddress The address of the email server. + /// \param[in] hostPort The port of the email server (usually 25) + /// \param[in] sender The email address you are sending from. + /// \param[in] recipient The email address you are sending to. + /// \param[in] senderName The email address you claim to be sending from + /// \param[in] recipientName The email address you claim to be sending to + /// \param[in] subject Email subject + /// \param[in] body Email body + /// \param[in] attachedFiles List of files to attach to the email. (Can be 0 to send none). + /// \param[in] doPrintf true to output SMTP info to console(for debugging?) + /// \param[in] password Used if the server uses AUTHENTICATE PLAIN over TLS (such as gmail) + /// \return 0 on success, otherwise a string indicating the error message + const char *Send(const char *hostAddress, unsigned short hostPort, const char *sender, const char *recipient, const char *senderName, const char *recipientName, const char *subject, const char *body, FileList *attachedFiles, bool doPrintf, const char *password); + +protected: + const char *GetResponse(TCPInterface *tcpInterface, const SystemAddress &emailServer, bool doPrintf); + RakNetRandom rakNetRandom; +}; + +} // namespace RakNet + +#endif + + diff --git a/Source/EpochTimeToString.h b/Source/EpochTimeToString.h index 9ef95aa1b..d25c95d57 100644 --- a/Source/EpochTimeToString.h +++ b/Source/EpochTimeToString.h @@ -1,24 +1,21 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - - -/// \file EpochTimeToString.h -/// - - -#ifndef __EPOCH_TIME_TO_STRING_H -#define __EPOCH_TIME_TO_STRING_H - -#include "Export.h" - -RAK_DLL_EXPORT char * EpochTimeToString(long long time); - -#endif - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + + +/// \file EpochTimeToString.h +/// + + +#pragma once + +#include "Export.h" + +RAK_DLL_EXPORT char * EpochTimeToString(long long time); + diff --git a/Source/Export.h b/Source/Export.h index a908295eb..7ef8b1b66 100644 --- a/Source/Export.h +++ b/Source/Export.h @@ -1,23 +1,26 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "RakNetDefines.h" - -#if defined(_WIN32) && !(defined(__GNUC__) || defined(__GCCXML__)) && !defined(_RAKNET_LIB) && defined(_RAKNET_DLL) -#define RAK_DLL_EXPORT __declspec(dllexport) -#else -#define RAK_DLL_EXPORT -#endif - -#define STATIC_FACTORY_DECLARATIONS(x) static x* GetInstance(void); \ -static void DestroyInstance( x *i); - -#define STATIC_FACTORY_DEFINITIONS(x,y) x* x::GetInstance(void) {return RakNet::OP_NEW( _FILE_AND_LINE_ );} \ -void x::DestroyInstance( x *i) {RakNet::OP_DELETE(( y* ) i, _FILE_AND_LINE_);} +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + + +#include "RakNetDefines.h" + +#if defined(_WIN32) && !(defined(__GNUC__) || defined(__GCCXML__)) && !defined(_RAKNET_LIB) && defined(_RAKNET_DLL) +#define RAK_DLL_EXPORT __declspec(dllexport) +#else +#define RAK_DLL_EXPORT +#endif + +#define STATIC_FACTORY_DECLARATIONS(x) static x* GetInstance(void); \ +static void DestroyInstance( x *i); + +#define STATIC_FACTORY_DEFINITIONS(x,y) x* x::GetInstance(void) {return RakNet::OP_NEW( _FILE_AND_LINE_ );} \ +void x::DestroyInstance( x *i) {RakNet::OP_DELETE(( y* ) i, _FILE_AND_LINE_);} diff --git a/Source/FileList.h b/Source/FileList.h index 497cafff3..f84715514 100644 --- a/Source/FileList.h +++ b/Source/FileList.h @@ -1,266 +1,264 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file FileList.h -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_FileOperations==1 - -#ifndef __FILE_LIST -#define __FILE_LIST - -#include "Export.h" -#include "DS_List.h" -#include "RakMemoryOverride.h" -#include "RakNetTypes.h" -#include "FileListNodeContext.h" -#include "RakString.h" - -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -namespace RakNet -{ - class BitStream; -} - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; -class FileList; - - -/// Represents once instance of a file -struct FileListNode -{ - /// Name of the file - RakNet::RakString filename; - - /// Full path to the file, which may be different than filename - RakNet::RakString fullPathToFile; - - /// File data (may be null if not ready) - char *data; - - /// Length of \a data. May be greater than fileLength if prepended with a file hash - BitSize_t dataLengthBytes; - - /// Length of the file - unsigned fileLengthBytes; - - /// User specific data for whatever, describing this file. - FileListNodeContext context; - - /// If true, data and dataLengthBytes should be empty. This is just storing the filename - bool isAReference; -}; - -/// Callback interface set with FileList::SetCallback() in case you want progress notifications when FileList::AddFilesFromDirectory() is called -class RAK_DLL_EXPORT FileListProgress -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(FileListProgress) - - FileListProgress() {} - virtual ~FileListProgress() {} - - /// First callback called when FileList::AddFilesFromDirectory() starts - virtual void OnAddFilesFromDirectoryStarted(FileList *fileList, char *dir) { - (void) fileList; - (void) dir; - } - - /// Called for each directory, when that directory begins processing - virtual void OnDirectory(FileList *fileList, char *dir, unsigned int directoriesRemaining) { - (void) fileList; - (void) dir; - (void) directoriesRemaining; - } - - /// Called for each file, when that file begins processing - virtual void OnFile(FileList *fileList, char *dir, char *fileName, unsigned int fileSize) { - (void) fileList; - (void) dir; - (void) fileName; - (void) fileSize; - } - - /// \brief This function is called when we are sending a file to a remote system. - /// \param[in] fileName The name of the file being sent - /// \param[in] fileLengthBytes How long the file is - /// \param[in] offset The offset in bytes into the file that we are sending - /// \param[in] bytesBeingSent How many bytes we are sending this push - /// \param[in] done If this file is now done with this push - /// \param[in] targetSystem Who we are sending to - virtual void OnFilePush(const char *fileName, unsigned int fileLengthBytes, unsigned int offset, unsigned int bytesBeingSent, bool done, SystemAddress targetSystem, unsigned short setId) - { - (void) fileName; - (void) fileLengthBytes; - (void) offset; - (void) bytesBeingSent; - (void) done; - (void) targetSystem; - (void) setId; - } - - /// \brief This function is called when all files have been read and are being transferred to a remote system - virtual void OnFilePushesComplete( SystemAddress systemAddress, unsigned short setId ) - { - (void) systemAddress; - (void) setId; - } - - /// \brief This function is called when a send to a system was aborted (probably due to disconnection) - virtual void OnSendAborted( SystemAddress systemAddress ) - { - (void) systemAddress; - } -}; - -/// Implementation of FileListProgress to use RAKNET_DEBUG_PRINTF -class RAK_DLL_EXPORT FLP_Printf : public FileListProgress -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(FLP_Printf) - - FLP_Printf() {} - virtual ~FLP_Printf() {} - - /// First callback called when FileList::AddFilesFromDirectory() starts - virtual void OnAddFilesFromDirectoryStarted(FileList *fileList, char *dir); - - /// Called for each directory, when that directory begins processing - virtual void OnDirectory(FileList *fileList, char *dir, unsigned int directoriesRemaining); - - /// \brief This function is called when all files have been transferred to a particular remote system - virtual void OnFilePushesComplete( SystemAddress systemAddress, unsigned short setID ); - - /// \brief This function is called when a send to a system was aborted (probably due to disconnection) - virtual void OnSendAborted( SystemAddress systemAddress ); -}; - -class RAK_DLL_EXPORT FileList -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(FileList) - - FileList(); - ~FileList(); - /// \brief Add all the files at a given directory. - /// \param[in] applicationDirectory The first part of the path. This is not stored as part of the filename. Use \ as the path delineator. - /// \param[in] subDirectory The rest of the path to the file. This is stored as a prefix to the filename - /// \param[in] writeHash The first 4 bytes is a hash of the file, with the remainder the actual file data (should \a writeData be true) - /// \param[in] writeData Write the contents of each file - /// \param[in] recursive Whether or not to visit subdirectories - /// \param[in] context User defined byte to store with each file. Use for whatever you want. - void AddFilesFromDirectory(const char *applicationDirectory, const char *subDirectory, bool writeHash, bool writeData, bool recursive, FileListNodeContext context); - - /// Deallocate all memory - void Clear(void); - - /// Write all encoded data into a bitstream - void Serialize(RakNet::BitStream *outBitStream); - - /// Read all encoded data from a bitstream. Clear() is called before deserializing. - bool Deserialize(RakNet::BitStream *inBitStream); - - /// \brief Given the existing set of files, search applicationDirectory for the same files. - /// \details For each file that is missing or different, add that file to \a missingOrChangedFiles. Note: the file contents are not written, and only the hash if written if \a alwaysWriteHash is true - /// alwaysWriteHash and neverWriteHash are optimizations to avoid reading the file contents to generate the hash if not necessary because the file is missing or has different lengths anyway. - /// \param[in] applicationDirectory The first part of the path. This is not stored as part of the filename. Use \ as the path delineator. - /// \param[out] missingOrChangedFiles Output list written to - /// \param[in] alwaysWriteHash If true, and neverWriteHash is false, will hash the file content of the file on disk, and write that as the file data with a length of SHA1_LENGTH bytes. If false, if the file length is different, will only write the filename. - /// \param[in] neverWriteHash If true, will never write the hash, even if available. If false, will write the hash if the file lengths are the same and it was forced to do a comparison. - void ListMissingOrChangedFiles(const char *applicationDirectory, FileList *missingOrChangedFiles, bool alwaysWriteHash, bool neverWriteHash); - - /// \brief Return the files that need to be written to make \a input match this current FileList. - /// \details Specify dirSubset to only consider files that start with this path - /// specify remoteSubdir to assume that all filenames in input start with this path, so strip it off when comparing filenames. - /// \param[in] input Full list of files - /// \param[out] output Files that we need to match input - /// \param[in] dirSubset If the filename does not start with this path, just skip this file. - /// \param[in] remoteSubdir Remove this from the filenames of \a input when comparing to existing filenames. - void GetDeltaToCurrent(FileList *input, FileList *output, const char *dirSubset, const char *remoteSubdir); - - /// \brief Assuming FileList contains a list of filenames presumably without data, read the data for these filenames - /// \param[in] applicationDirectory Prepend this path to each filename. Trailing slash will be added if necessary. Use \ as the path delineator. - /// \param[in] writeFileData True to read and store the file data. The first SHA1_LENGTH bytes will contain the hash if \a writeFileHash is true - /// \param[in] writeFileHash True to read and store the hash of the file data. The first SHA1_LENGTH bytes will contain the hash if \a writeFileHash is true - /// \param[in] removeUnknownFiles If a file does not exist on disk but is in the file list, remove it from the file list? - void PopulateDataFromDisk(const char *applicationDirectory, bool writeFileData, bool writeFileHash, bool removeUnknownFiles); - - /// By default, GetDeltaToCurrent tags files as non-references, meaning they are assumed to be populated later - /// This tags all files as references, required for IncrementalReadInterface to process them incrementally - void FlagFilesAsReferences(void); - - /// \brief Write all files to disk, prefixing the paths with applicationDirectory - /// \param[in] applicationDirectory path prefix - void WriteDataToDisk(const char *applicationDirectory); - - /// \brief Add a file, given data already in memory. - /// \param[in] filename Name of a file, optionally prefixed with a partial or complete path. Use \ as the path delineator. - /// \param[in] fullPathToFile Full path to the file on disk - /// \param[in] data Contents to write - /// \param[in] dataLength length of the data, which may be greater than fileLength should you prefix extra data, such as the hash - /// \param[in] fileLength Length of the file - /// \param[in] context User defined byte to store with each file. Use for whatever you want. - /// \param[in] isAReference Means that this is just a reference to a file elsewhere - does not actually have any data - /// \param[in] takeDataPointer If true, do not allocate dataLength. Just take the pointer passed to the \a data parameter - void AddFile(const char *filename, const char *fullPathToFile, const char *data, const unsigned dataLength, const unsigned fileLength, FileListNodeContext context, bool isAReference=false, bool takeDataPointer=false); - - /// \brief Add a file, reading it from disk. - /// \param[in] filepath Complete path to the file, including the filename itself - /// \param[in] filename filename to store internally, anything you want, but usually either the complete path or a subset of the complete path. - /// \param[in] context User defined byte to store with each file. Use for whatever you want. - void AddFile(const char *filepath, const char *filename, FileListNodeContext context); - - /// \brief Delete all files stored in the file list. - /// \param[in] applicationDirectory Prefixed to the path to each filename. Use \ as the path delineator. - void DeleteFiles(const char *applicationDirectory); - - /// \brief Adds a callback to get progress reports about what the file list instances do. - /// \param[in] cb A pointer to an externally defined instance of FileListProgress. This pointer is held internally, so should remain valid as long as this class is valid. - void AddCallback(FileListProgress *cb); - - /// \brief Removes a callback - /// \param[in] cb A pointer to an externally defined instance of FileListProgress that was previously added with AddCallback() - void RemoveCallback(FileListProgress *cb); - - /// \brief Removes all callbacks - void ClearCallbacks(void); - - /// Returns all callbacks added with AddCallback() - /// \param[out] callbacks The list is set to the list of callbacks - void GetCallbacks(DataStructures::List &callbacks); - - // Here so you can read it, but don't modify it - DataStructures::List fileList; - - static bool FixEndingSlash(char *str); -protected: - DataStructures::List fileListProgressCallbacks; -}; - -} // namespace RakNet - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif - -#endif - -#endif // _RAKNET_SUPPORT_FileOperations +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file FileList.h +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_FileOperations==1 + +#pragma once + +#include "Export.h" +#include "DS_List.h" +#include "RakMemoryOverride.h" +#include "RakNetTypes.h" +#include "FileListNodeContext.h" +#include "RakString.h" + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +namespace RakNet +{ + class BitStream; +} + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; +class FileList; + + +/// Represents once instance of a file +struct FileListNode +{ + /// Name of the file + RakNet::RakString filename; + + /// Full path to the file, which may be different than filename + RakNet::RakString fullPathToFile; + + /// File data (may be null if not ready) + char *data; + + /// Length of \a data. May be greater than fileLength if prepended with a file hash + BitSize_t dataLengthBytes; + + /// Length of the file + unsigned fileLengthBytes; + + /// User specific data for whatever, describing this file. + FileListNodeContext context; + + /// If true, data and dataLengthBytes should be empty. This is just storing the filename + bool isAReference; +}; + +/// Callback interface set with FileList::SetCallback() in case you want progress notifications when FileList::AddFilesFromDirectory() is called +class RAK_DLL_EXPORT FileListProgress +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(FileListProgress) + + FileListProgress() {} + virtual ~FileListProgress() {} + + /// First callback called when FileList::AddFilesFromDirectory() starts + virtual void OnAddFilesFromDirectoryStarted(FileList *fileList, char *dir) { + (void) fileList; + (void) dir; + } + + /// Called for each directory, when that directory begins processing + virtual void OnDirectory(FileList *fileList, char *dir, unsigned int directoriesRemaining) { + (void) fileList; + (void) dir; + (void) directoriesRemaining; + } + + /// Called for each file, when that file begins processing + virtual void OnFile(FileList *fileList, char *dir, char *fileName, unsigned int fileSize) { + (void) fileList; + (void) dir; + (void) fileName; + (void) fileSize; + } + + /// \brief This function is called when we are sending a file to a remote system. + /// \param[in] fileName The name of the file being sent + /// \param[in] fileLengthBytes How long the file is + /// \param[in] offset The offset in bytes into the file that we are sending + /// \param[in] bytesBeingSent How many bytes we are sending this push + /// \param[in] done If this file is now done with this push + /// \param[in] targetSystem Who we are sending to + virtual void OnFilePush(const char *fileName, unsigned int fileLengthBytes, unsigned int offset, unsigned int bytesBeingSent, bool done, SystemAddress targetSystem, unsigned short setId) + { + (void) fileName; + (void) fileLengthBytes; + (void) offset; + (void) bytesBeingSent; + (void) done; + (void) targetSystem; + (void) setId; + } + + /// \brief This function is called when all files have been read and are being transferred to a remote system + virtual void OnFilePushesComplete( SystemAddress systemAddress, unsigned short setId ) + { + (void) systemAddress; + (void) setId; + } + + /// \brief This function is called when a send to a system was aborted (probably due to disconnection) + virtual void OnSendAborted( SystemAddress systemAddress ) + { + (void) systemAddress; + } +}; + +/// Implementation of FileListProgress to use RAKNET_DEBUG_PRINTF +class RAK_DLL_EXPORT FLP_Printf : public FileListProgress +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(FLP_Printf) + + FLP_Printf() {} + virtual ~FLP_Printf() {} + + /// First callback called when FileList::AddFilesFromDirectory() starts + virtual void OnAddFilesFromDirectoryStarted(FileList *fileList, char *dir); + + /// Called for each directory, when that directory begins processing + virtual void OnDirectory(FileList *fileList, char *dir, unsigned int directoriesRemaining); + + /// \brief This function is called when all files have been transferred to a particular remote system + virtual void OnFilePushesComplete( SystemAddress systemAddress, unsigned short setID ); + + /// \brief This function is called when a send to a system was aborted (probably due to disconnection) + virtual void OnSendAborted( SystemAddress systemAddress ); +}; + +class RAK_DLL_EXPORT FileList +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(FileList) + + FileList(); + ~FileList(); + /// \brief Add all the files at a given directory. + /// \param[in] applicationDirectory The first part of the path. This is not stored as part of the filename. Use \ as the path delineator. + /// \param[in] subDirectory The rest of the path to the file. This is stored as a prefix to the filename + /// \param[in] writeHash The first 4 bytes is a hash of the file, with the remainder the actual file data (should \a writeData be true) + /// \param[in] writeData Write the contents of each file + /// \param[in] recursive Whether or not to visit subdirectories + /// \param[in] context User defined byte to store with each file. Use for whatever you want. + void AddFilesFromDirectory(const char *applicationDirectory, const char *subDirectory, bool writeHash, bool writeData, bool recursive, FileListNodeContext context); + + /// Deallocate all memory + void Clear(void); + + /// Write all encoded data into a bitstream + void Serialize(RakNet::BitStream *outBitStream); + + /// Read all encoded data from a bitstream. Clear() is called before deserializing. + bool Deserialize(RakNet::BitStream *inBitStream); + + /// \brief Given the existing set of files, search applicationDirectory for the same files. + /// \details For each file that is missing or different, add that file to \a missingOrChangedFiles. Note: the file contents are not written, and only the hash if written if \a alwaysWriteHash is true + /// alwaysWriteHash and neverWriteHash are optimizations to avoid reading the file contents to generate the hash if not necessary because the file is missing or has different lengths anyway. + /// \param[in] applicationDirectory The first part of the path. This is not stored as part of the filename. Use \ as the path delineator. + /// \param[out] missingOrChangedFiles Output list written to + /// \param[in] alwaysWriteHash If true, and neverWriteHash is false, will hash the file content of the file on disk, and write that as the file data with a length of SHA1_LENGTH bytes. If false, if the file length is different, will only write the filename. + /// \param[in] neverWriteHash If true, will never write the hash, even if available. If false, will write the hash if the file lengths are the same and it was forced to do a comparison. + void ListMissingOrChangedFiles(const char *applicationDirectory, FileList *missingOrChangedFiles, bool alwaysWriteHash, bool neverWriteHash); + + /// \brief Return the files that need to be written to make \a input match this current FileList. + /// \details Specify dirSubset to only consider files that start with this path + /// specify remoteSubdir to assume that all filenames in input start with this path, so strip it off when comparing filenames. + /// \param[in] input Full list of files + /// \param[out] output Files that we need to match input + /// \param[in] dirSubset If the filename does not start with this path, just skip this file. + /// \param[in] remoteSubdir Remove this from the filenames of \a input when comparing to existing filenames. + void GetDeltaToCurrent(FileList *input, FileList *output, const char *dirSubset, const char *remoteSubdir); + + /// \brief Assuming FileList contains a list of filenames presumably without data, read the data for these filenames + /// \param[in] applicationDirectory Prepend this path to each filename. Trailing slash will be added if necessary. Use \ as the path delineator. + /// \param[in] writeFileData True to read and store the file data. The first SHA1_LENGTH bytes will contain the hash if \a writeFileHash is true + /// \param[in] writeFileHash True to read and store the hash of the file data. The first SHA1_LENGTH bytes will contain the hash if \a writeFileHash is true + /// \param[in] removeUnknownFiles If a file does not exist on disk but is in the file list, remove it from the file list? + void PopulateDataFromDisk(const char *applicationDirectory, bool writeFileData, bool writeFileHash, bool removeUnknownFiles); + + /// By default, GetDeltaToCurrent tags files as non-references, meaning they are assumed to be populated later + /// This tags all files as references, required for IncrementalReadInterface to process them incrementally + void FlagFilesAsReferences(void); + + /// \brief Write all files to disk, prefixing the paths with applicationDirectory + /// \param[in] applicationDirectory path prefix + void WriteDataToDisk(const char *applicationDirectory); + + /// \brief Add a file, given data already in memory. + /// \param[in] filename Name of a file, optionally prefixed with a partial or complete path. Use \ as the path delineator. + /// \param[in] fullPathToFile Full path to the file on disk + /// \param[in] data Contents to write + /// \param[in] dataLength length of the data, which may be greater than fileLength should you prefix extra data, such as the hash + /// \param[in] fileLength Length of the file + /// \param[in] context User defined byte to store with each file. Use for whatever you want. + /// \param[in] isAReference Means that this is just a reference to a file elsewhere - does not actually have any data + /// \param[in] takeDataPointer If true, do not allocate dataLength. Just take the pointer passed to the \a data parameter + void AddFile(const char *filename, const char *fullPathToFile, const char *data, const unsigned dataLength, const unsigned fileLength, FileListNodeContext context, bool isAReference=false, bool takeDataPointer=false); + + /// \brief Add a file, reading it from disk. + /// \param[in] filepath Complete path to the file, including the filename itself + /// \param[in] filename filename to store internally, anything you want, but usually either the complete path or a subset of the complete path. + /// \param[in] context User defined byte to store with each file. Use for whatever you want. + void AddFile(const char *filepath, const char *filename, FileListNodeContext context); + + /// \brief Delete all files stored in the file list. + /// \param[in] applicationDirectory Prefixed to the path to each filename. Use \ as the path delineator. + void DeleteFiles(const char *applicationDirectory); + + /// \brief Adds a callback to get progress reports about what the file list instances do. + /// \param[in] cb A pointer to an externally defined instance of FileListProgress. This pointer is held internally, so should remain valid as long as this class is valid. + void AddCallback(FileListProgress *cb); + + /// \brief Removes a callback + /// \param[in] cb A pointer to an externally defined instance of FileListProgress that was previously added with AddCallback() + void RemoveCallback(FileListProgress *cb); + + /// \brief Removes all callbacks + void ClearCallbacks(void); + + /// Returns all callbacks added with AddCallback() + /// \param[out] callbacks The list is set to the list of callbacks + void GetCallbacks(DataStructures::List &callbacks); + + // Here so you can read it, but don't modify it + DataStructures::List fileList; + + static bool FixEndingSlash(char *str); +protected: + DataStructures::List fileListProgressCallbacks; +}; + +} // namespace RakNet + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif + +#endif + diff --git a/Source/FileListNodeContext.h b/Source/FileListNodeContext.h index cba071f67..a8825b80e 100644 --- a/Source/FileListNodeContext.h +++ b/Source/FileListNodeContext.h @@ -1,57 +1,55 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file FileListNodeContext.h -/// - - -#ifndef __FILE_LIST_NODE_CONTEXT_H -#define __FILE_LIST_NODE_CONTEXT_H - -#include "BitStream.h" - -struct FileListNodeContext -{ - FileListNodeContext() {dataPtr=0; dataLength=0;} - FileListNodeContext(unsigned char o, uint32_t f1, uint32_t f2, uint32_t f3) : op(o), flnc_extraData1(f1), flnc_extraData2(f2), flnc_extraData3(f3) {dataPtr=0; dataLength=0;} - ~FileListNodeContext() {} - - unsigned char op; - uint32_t flnc_extraData1; - uint32_t flnc_extraData2; - uint32_t flnc_extraData3; - void *dataPtr; - unsigned int dataLength; -}; - -inline RakNet::BitStream& operator<<(RakNet::BitStream& out, FileListNodeContext& in) -{ - out.Write(in.op); - out.Write(in.flnc_extraData1); - out.Write(in.flnc_extraData2); - out.Write(in.flnc_extraData3); - return out; -} -inline RakNet::BitStream& operator>>(RakNet::BitStream& in, FileListNodeContext& out) -{ - in.Read(out.op); - bool success = in.Read(out.flnc_extraData1); - (void) success; - assert(success); - success = in.Read(out.flnc_extraData2); - (void) success; - assert(success); - success = in.Read(out.flnc_extraData3); - (void) success; - assert(success); - return in; -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file FileListNodeContext.h +/// + + +#pragma once + +#include "BitStream.h" + +struct FileListNodeContext +{ + FileListNodeContext() {dataPtr=0; dataLength=0;} + FileListNodeContext(unsigned char o, uint32_t f1, uint32_t f2, uint32_t f3) : op(o), flnc_extraData1(f1), flnc_extraData2(f2), flnc_extraData3(f3) {dataPtr=0; dataLength=0;} + ~FileListNodeContext() {} + + unsigned char op; + uint32_t flnc_extraData1; + uint32_t flnc_extraData2; + uint32_t flnc_extraData3; + void *dataPtr; + unsigned int dataLength; +}; + +inline RakNet::BitStream& operator<<(RakNet::BitStream& out, FileListNodeContext& in) +{ + out.Write(in.op); + out.Write(in.flnc_extraData1); + out.Write(in.flnc_extraData2); + out.Write(in.flnc_extraData3); + return out; +} +inline RakNet::BitStream& operator>>(RakNet::BitStream& in, FileListNodeContext& out) +{ + in.Read(out.op); + bool success = in.Read(out.flnc_extraData1); + (void) success; + assert(success); + success = in.Read(out.flnc_extraData2); + (void) success; + assert(success); + success = in.Read(out.flnc_extraData3); + (void) success; + assert(success); + return in; +} + diff --git a/Source/FileListTransfer.h b/Source/FileListTransfer.h index 7df01ecc1..c51d9ec56 100644 --- a/Source/FileListTransfer.h +++ b/Source/FileListTransfer.h @@ -1,185 +1,183 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file FileListTransfer.h -/// \brief A plugin to provide a simple way to compress and incrementally send the files in the FileList structure. -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_FileListTransfer==1 && _RAKNET_SUPPORT_FileOperations==1 - -#ifndef __FILE_LIST_TRANFER_H -#define __FILE_LIST_TRANFER_H - -#include "RakNetTypes.h" -#include "Export.h" -#include "PluginInterface2.h" -#include "DS_Map.h" -#include "RakNetTypes.h" -#include "PacketPriority.h" -#include "RakMemoryOverride.h" -#include "FileList.h" -#include "DS_Queue.h" -#include "SimpleMutex.h" -#include "ThreadPool.h" - -namespace RakNet -{ -/// Forward declarations -class IncrementalReadInterface; -class FileListTransferCBInterface; -class FileListProgress; -struct FileListReceiver; - -/// \defgroup FILE_LIST_TRANSFER_GROUP FileListTransfer -/// \brief A plugin to provide a simple way to compress and incrementally send the files in the FileList structure. -/// \details -/// \ingroup PLUGINS_GROUP - -/// \brief A plugin to provide a simple way to compress and incrementally send the files in the FileList structure. -/// \details Similar to the DirectoryDeltaTransfer plugin, except that it doesn't send deltas based on pre-existing files or actually write the files to disk. -/// -/// Usage: -/// Call SetupReceive to allow one file set to arrive. The value returned by FileListTransfer::SetupReceive()
-/// is the setID that is allowed.
-/// It's up to you to transmit this value to the other system, along with information indicating what kind of files you want to get.
-/// The other system should then prepare a FileList and call FileListTransfer::Send(), passing the return value of FileListTransfer::SetupReceive()
-/// as the \a setID parameter to FileListTransfer::Send() -/// \ingroup FILE_LIST_TRANSFER_GROUP -class RAK_DLL_EXPORT FileListTransfer : public PluginInterface2 -{ -public: - - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(FileListTransfer) - - FileListTransfer(); - virtual ~FileListTransfer(); - - /// \brief Optionally start worker threads when using _incrementalReadInterface for the Send() operation - /// \param[in] numThreads how many worker threads to start - /// \param[in] threadPriority Passed to the thread creation routine. Use THREAD_PRIORITY_NORMAL for Windows. For Linux based systems, you MUST pass something reasonable based on the thread priorities for your application. - void StartIncrementalReadThreads(int numThreads, int threadPriority=-99999); - - /// \brief Allows one corresponding Send() call from another system to arrive. - /// \param[in] handler The class to call on each file - /// \param[in] deleteHandler True to delete the handler when it is no longer needed. False to not do so. - /// \param[in] allowedSender Which system to allow files from. - /// \return A set ID value, which should be passed as the \a setID value to the Send() call on the other system. This value will be returned in the callback and is unique per file set. Returns 65535 on failure (not connected to sender) - unsigned short SetupReceive(FileListTransferCBInterface *handler, bool deleteHandler, SystemAddress allowedSender); - - /// \brief Send the FileList structure to another system, which must have previously called SetupReceive(). - /// \param[in] fileList A list of files. The data contained in FileList::data will be sent incrementally and compressed among all files in the set - /// \param[in] rakPeer The instance of RakNet to use to send the message. Pass 0 to use the instance the plugin is attached to - /// \param[in] recipient The address of the system to send to - /// \param[in] setID The return value of SetupReceive() which was previously called on \a recipient - /// \param[in] priority Passed to RakPeerInterface::Send() - /// \param[in] orderingChannel Passed to RakPeerInterface::Send() - /// \param[in] _incrementalReadInterface If a file in \a fileList has no data, _incrementalReadInterface will be used to read the file in chunks of size \a chunkSize - /// \param[in] _chunkSize How large of a block of a file to read/send at once. Large values use more memory but transfer slightly faster. - void Send(FileList *fileList, RakNet::RakPeerInterface *rakPeer, SystemAddress recipient, unsigned short setID, PacketPriority priority, char orderingChannel, IncrementalReadInterface *_incrementalReadInterface=0, unsigned int _chunkSize=262144*4*16); - - /// Return number of files waiting to go out to a particular address - unsigned int GetPendingFilesToAddress(SystemAddress recipient); - - /// \brief Stop a download. - void CancelReceive(unsigned short setId); - - /// \brief Remove all handlers associated with a particular system address. - void RemoveReceiver(SystemAddress systemAddress); - - /// \brief Is a handler passed to SetupReceive still running? - bool IsHandlerActive(unsigned short setId); - - /// \brief Adds a callback to get progress reports about what the file list instances do. - /// \param[in] cb A pointer to an externally defined instance of FileListProgress. This pointer is held internally, so should remain valid as long as this class is valid. - void AddCallback(FileListProgress *cb); - - /// \brief Removes a callback - /// \param[in] cb A pointer to an externally defined instance of FileListProgress that was previously added with AddCallback() - void RemoveCallback(FileListProgress *cb); - - /// \brief Removes all callbacks - void ClearCallbacks(void); - - /// Returns all callbacks added with AddCallback() - /// \param[out] callbacks The list is set to the list of callbacks - void GetCallbacks(DataStructures::List &callbacks); - - /// \internal For plugin handling - virtual PluginReceiveResult OnReceive(Packet *packet); - /// \internal For plugin handling - virtual void OnRakPeerShutdown(void); - /// \internal For plugin handling - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - /// \internal For plugin handling - virtual void Update(void); - -protected: - bool DecodeSetHeader(Packet *packet); - bool DecodeFile(Packet *packet, bool fullFile); - - void Clear(void); - - void OnReferencePush(Packet *packet, bool fullFile); - void OnReferencePushAck(Packet *packet); - void SendIRIToAddress(SystemAddress systemAddress, unsigned short setId); - - DataStructures::Map fileListReceivers; - unsigned short setId; - DataStructures::List fileListProgressCallbacks; - - struct FileToPush - { - FileListNode fileListNode; - PacketPriority packetPriority; - char orderingChannel; - unsigned int currentOffset; - ////unsigned short setID; - unsigned int setIndex; - IncrementalReadInterface *incrementalReadInterface; - unsigned int chunkSize; - }; - struct FileToPushRecipient - { - unsigned int refCount; - SimpleMutex refCountMutex; - void DeleteThis(void); - void AddRef(void); - void Deref(void); - - SystemAddress systemAddress; - unsigned short setId; - - //// SimpleMutex filesToPushMutex; - DataStructures::Queue filesToPush; - }; - DataStructures::List< FileToPushRecipient* > fileToPushRecipientList; - SimpleMutex fileToPushRecipientListMutex; - void RemoveFromList(FileToPushRecipient *ftpr); - - struct ThreadData - { - FileListTransfer *fileListTransfer; - SystemAddress systemAddress; - unsigned short setId; - }; - - ThreadPool threadPool; - - friend int SendIRIToAddressCB(FileListTransfer::ThreadData threadData, bool *returnOutput, void* perThreadData); -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file FileListTransfer.h +/// \brief A plugin to provide a simple way to compress and incrementally send the files in the FileList structure. +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_FileListTransfer==1 && _RAKNET_SUPPORT_FileOperations==1 + +#pragma once + +#include "RakNetTypes.h" +#include "Export.h" +#include "PluginInterface2.h" +#include "DS_Map.h" +#include "RakNetTypes.h" +#include "PacketPriority.h" +#include "RakMemoryOverride.h" +#include "FileList.h" +#include "DS_Queue.h" +#include "SimpleMutex.h" +#include "ThreadPool.h" + +namespace RakNet +{ +/// Forward declarations +class IncrementalReadInterface; +class FileListTransferCBInterface; +class FileListProgress; +struct FileListReceiver; + +/// \defgroup FILE_LIST_TRANSFER_GROUP FileListTransfer +/// \brief A plugin to provide a simple way to compress and incrementally send the files in the FileList structure. +/// \details +/// \ingroup PLUGINS_GROUP + +/// \brief A plugin to provide a simple way to compress and incrementally send the files in the FileList structure. +/// \details Similar to the DirectoryDeltaTransfer plugin, except that it doesn't send deltas based on pre-existing files or actually write the files to disk. +/// +/// Usage: +/// Call SetupReceive to allow one file set to arrive. The value returned by FileListTransfer::SetupReceive()
+/// is the setID that is allowed.
+/// It's up to you to transmit this value to the other system, along with information indicating what kind of files you want to get.
+/// The other system should then prepare a FileList and call FileListTransfer::Send(), passing the return value of FileListTransfer::SetupReceive()
+/// as the \a setID parameter to FileListTransfer::Send() +/// \ingroup FILE_LIST_TRANSFER_GROUP +class RAK_DLL_EXPORT FileListTransfer : public PluginInterface2 +{ +public: + + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(FileListTransfer) + + FileListTransfer(); + virtual ~FileListTransfer(); + + /// \brief Optionally start worker threads when using _incrementalReadInterface for the Send() operation + /// \param[in] numThreads how many worker threads to start + /// \param[in] threadPriority Passed to the thread creation routine. Use THREAD_PRIORITY_NORMAL for Windows. For Linux based systems, you MUST pass something reasonable based on the thread priorities for your application. + void StartIncrementalReadThreads(int numThreads, int threadPriority=-99999); + + /// \brief Allows one corresponding Send() call from another system to arrive. + /// \param[in] handler The class to call on each file + /// \param[in] deleteHandler True to delete the handler when it is no longer needed. False to not do so. + /// \param[in] allowedSender Which system to allow files from. + /// \return A set ID value, which should be passed as the \a setID value to the Send() call on the other system. This value will be returned in the callback and is unique per file set. Returns 65535 on failure (not connected to sender) + unsigned short SetupReceive(FileListTransferCBInterface *handler, bool deleteHandler, SystemAddress allowedSender); + + /// \brief Send the FileList structure to another system, which must have previously called SetupReceive(). + /// \param[in] fileList A list of files. The data contained in FileList::data will be sent incrementally and compressed among all files in the set + /// \param[in] rakPeer The instance of RakNet to use to send the message. Pass 0 to use the instance the plugin is attached to + /// \param[in] recipient The address of the system to send to + /// \param[in] setID The return value of SetupReceive() which was previously called on \a recipient + /// \param[in] priority Passed to RakPeerInterface::Send() + /// \param[in] orderingChannel Passed to RakPeerInterface::Send() + /// \param[in] _incrementalReadInterface If a file in \a fileList has no data, _incrementalReadInterface will be used to read the file in chunks of size \a chunkSize + /// \param[in] _chunkSize How large of a block of a file to read/send at once. Large values use more memory but transfer slightly faster. + void Send(FileList *fileList, RakNet::RakPeerInterface *rakPeer, SystemAddress recipient, unsigned short setID, PacketPriority priority, char orderingChannel, IncrementalReadInterface *_incrementalReadInterface=0, unsigned int _chunkSize=262144*4*16); + + /// Return number of files waiting to go out to a particular address + unsigned int GetPendingFilesToAddress(SystemAddress recipient); + + /// \brief Stop a download. + void CancelReceive(unsigned short setId); + + /// \brief Remove all handlers associated with a particular system address. + void RemoveReceiver(SystemAddress systemAddress); + + /// \brief Is a handler passed to SetupReceive still running? + bool IsHandlerActive(unsigned short setId); + + /// \brief Adds a callback to get progress reports about what the file list instances do. + /// \param[in] cb A pointer to an externally defined instance of FileListProgress. This pointer is held internally, so should remain valid as long as this class is valid. + void AddCallback(FileListProgress *cb); + + /// \brief Removes a callback + /// \param[in] cb A pointer to an externally defined instance of FileListProgress that was previously added with AddCallback() + void RemoveCallback(FileListProgress *cb); + + /// \brief Removes all callbacks + void ClearCallbacks(void); + + /// Returns all callbacks added with AddCallback() + /// \param[out] callbacks The list is set to the list of callbacks + void GetCallbacks(DataStructures::List &callbacks); + + /// \internal For plugin handling + virtual PluginReceiveResult OnReceive(Packet *packet); + /// \internal For plugin handling + virtual void OnRakPeerShutdown(void); + /// \internal For plugin handling + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + /// \internal For plugin handling + virtual void Update(void); + +protected: + bool DecodeSetHeader(Packet *packet); + bool DecodeFile(Packet *packet, bool fullFile); + + void Clear(void); + + void OnReferencePush(Packet *packet, bool fullFile); + void OnReferencePushAck(Packet *packet); + void SendIRIToAddress(SystemAddress systemAddress, unsigned short setId); + + DataStructures::Map fileListReceivers; + unsigned short setId; + DataStructures::List fileListProgressCallbacks; + + struct FileToPush + { + FileListNode fileListNode; + PacketPriority packetPriority; + char orderingChannel; + unsigned int currentOffset; + ////unsigned short setID; + unsigned int setIndex; + IncrementalReadInterface *incrementalReadInterface; + unsigned int chunkSize; + }; + struct FileToPushRecipient + { + unsigned int refCount; + SimpleMutex refCountMutex; + void DeleteThis(void); + void AddRef(void); + void Deref(void); + + SystemAddress systemAddress; + unsigned short setId; + + //// SimpleMutex filesToPushMutex; + DataStructures::Queue filesToPush; + }; + DataStructures::List< FileToPushRecipient* > fileToPushRecipientList; + SimpleMutex fileToPushRecipientListMutex; + void RemoveFromList(FileToPushRecipient *ftpr); + + struct ThreadData + { + FileListTransfer *fileListTransfer; + SystemAddress systemAddress; + unsigned short setId; + }; + + ThreadPool threadPool; + + friend int SendIRIToAddressCB(FileListTransfer::ThreadData threadData, bool *returnOutput, void* perThreadData); +}; + +} // namespace RakNet + +#endif + diff --git a/Source/FileListTransferCBInterface.h b/Source/FileListTransferCBInterface.h index 61b168f46..92a9ff1cd 100644 --- a/Source/FileListTransferCBInterface.h +++ b/Source/FileListTransferCBInterface.h @@ -1,162 +1,159 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file FileListTransferCBInterface.h -/// - - -#ifndef __FILE_LIST_TRANSFER_CALLBACK_INTERFACE_H -#define __FILE_LIST_TRANSFER_CALLBACK_INTERFACE_H - -#include "RakMemoryOverride.h" -#include "FileListNodeContext.h" - -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -namespace RakNet -{ - -/// \brief Used by FileListTransfer plugin as a callback for when we get a file. -/// \details You get the last file when fileIndex==numberOfFilesInThisSet -/// \sa FileListTransfer -class FileListTransferCBInterface -{ -public: - // Note: If this structure is changed the struct in the swig files need to be changed as well - struct OnFileStruct - { - /// \brief The index into the set of files, from 0 to numberOfFilesInThisSet - unsigned fileIndex; - - /// \brief The name of the file - char fileName[512]; - - /// \brief The data pointed to by the file - char *fileData; - - /// \brief The amount of data to be downloaded for this file - BitSize_t byteLengthOfThisFile; - - /// \brief How many bytes of this file has been downloaded - BitSize_t bytesDownloadedForThisFile; - - /// \brief Files are transmitted in sets, where more than one set of files can be transmitted at the same time. - /// \details This is the identifier for the set, which is returned by FileListTransfer::SetupReceive - unsigned short setID; - - /// \brief The number of files that are in this set. - unsigned numberOfFilesInThisSet; - - /// \brief The total length of the transmitted files for this set, after being uncompressed - unsigned byteLengthOfThisSet; - - /// \brief The total length, in bytes, downloaded for this set. - unsigned bytesDownloadedForThisSet; - - /// \brief User data passed to one of the functions in the FileList class. - /// \details However, on error, this is instead changed to one of the enumerations in the PatchContext structure. - FileListNodeContext context; - - /// \brief Who sent this file - SystemAddress senderSystemAddress; - - /// \brief Who sent this file. Not valid when using TCP, only RakPeer (UDP) - RakNetGUID senderGuid; - }; - - // Note: If this structure is changed the struct in the swig files need to be changed as well - struct FileProgressStruct - { - /// \param[out] onFileStruct General information about this file, such as the filename and the first \a partLength bytes. You do NOT need to save this data yourself. The complete file will arrive normally. - OnFileStruct *onFileStruct; - /// \param[out] partCount The zero based index into partTotal. The percentage complete done of this file is 100 * (partCount+1)/partTotal - unsigned int partCount; - /// \param[out] partTotal The total number of parts this file was split into. Each part will be roughly the MTU size, minus the UDP header and RakNet headers - unsigned int partTotal; - /// \param[out] dataChunkLength How many bytes long firstDataChunk and iriDataChunk are - unsigned int dataChunkLength; - /// \param[out] firstDataChunk The first \a partLength of the final file. If you store identifying information about the file in the first \a partLength bytes, you can read them while the download is taking place. If this hasn't arrived yet, firstDataChunk will be 0 - char *firstDataChunk; - /// \param[out] iriDataChunk If the remote system is sending this file using IncrementalReadInterface, then this is the chunk we just downloaded. It will not exist in memory after this callback. You should either store this to disk, or in memory. If it is 0, then the file is smaller than one chunk, and will be held in memory automatically - char *iriDataChunk; - /// \param[out] iriWriteOffset Offset in bytes from the start of the file for the data pointed to by iriDataChunk - unsigned int iriWriteOffset; - /// \param[out] Who sent this file - SystemAddress senderSystemAddress; - /// \param[out] Who sent this file. Not valid when using TCP, only RakPeer (UDP) - RakNetGUID senderGuid; - /// \param[in] allocateIrIDataChunkAutomatically If true, then RakNet will hold iriDataChunk for you and return it in OnFile. Defaults to true - bool allocateIrIDataChunkAutomatically; - }; - - struct DownloadCompleteStruct - { - /// \brief Files are transmitted in sets, where more than one set of files can be transmitted at the same time. - /// \details This is the identifier for the set, which is returned by FileListTransfer::SetupReceive - unsigned short setID; - - /// \brief The number of files that are in this set. - unsigned numberOfFilesInThisSet; - - /// \brief The total length of the transmitted files for this set, after being uncompressed - unsigned byteLengthOfThisSet; - - /// \brief Who sent this file - SystemAddress senderSystemAddress; - - /// \brief Who sent this file. Not valid when using TCP, only RakPeer (UDP) - RakNetGUID senderGuid; - }; - - FileListTransferCBInterface() {} - virtual ~FileListTransferCBInterface() {} - - /// \brief Got a file. - /// \details This structure is only valid for the duration of this function call. - /// \return Return true to have RakNet delete the memory allocated to hold this file for this function call. - virtual bool OnFile(OnFileStruct *onFileStruct)=0; - - /// \brief Got part of a big file internally in RakNet - /// \details This is called in one of two circumstances: Either the transport layer is returning ID_PROGRESS_NOTIFICATION, or you got a block via IncrementalReadInterface - /// If the transport layer is returning ID_PROGRESS_NOTIFICATION (see RakPeer::SetSplitMessageProgressInterval()) then FileProgressStruct::iriDataChunk will be 0. - /// If this is a block via IncrementalReadInterface, then iriDataChunk will point to the block just downloaded. - /// If not using IncrementalReadInterface, then you only care about partCount and partTotal to tell how far the download has progressed. YOu can use firstDataChunk to read the first part of the file if desired. The file is usable when you get the OnFile callback. - /// If using IncrementalReadInterface and you let RakNet buffer the files in memory (default), then it is the same as above. The file is usable when you get the OnFile callback. - /// If using IncrementalReadInterface and you do not let RakNet buffer the files in memory, then set allocateIrIDataChunkAutomatically to false. Write the file to disk whenever you get OnFileProgress and iriDataChunk is not 0, and ignore OnFile. - virtual void OnFileProgress(FileProgressStruct *fps)=0; - - /// \brief Called while the handler is active by FileListTransfer - /// \details Return false when you are done with the class. - /// At that point OnDereference will be called and the class will no longer be maintained by the FileListTransfer plugin. - virtual bool Update(void) {return true;} - - /// \brief Called when the download is completed. - /// \details If you are finished with this class, return false. - /// At that point OnDereference will be called and the class will no longer be maintained by the FileListTransfer plugin. - /// Otherwise return true, and Update will continue to be called. - virtual bool OnDownloadComplete(DownloadCompleteStruct *dcs) {(void) dcs; return false;} - - /// \brief This function is called when this instance is about to be dereferenced by the FileListTransfer plugin. - /// \details Update will no longer be called. - /// It will will be deleted automatically if true was passed to FileListTransfer::SetupReceive::deleteHandler - /// Otherwise it is up to you to delete it yourself. - virtual void OnDereference(void) {} -}; - -} // namespace RakNet - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif - -#endif - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file FileListTransferCBInterface.h +/// + + +#pragma once + +#include "RakMemoryOverride.h" +#include "FileListNodeContext.h" + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +namespace RakNet +{ + +/// \brief Used by FileListTransfer plugin as a callback for when we get a file. +/// \details You get the last file when fileIndex==numberOfFilesInThisSet +/// \sa FileListTransfer +class FileListTransferCBInterface +{ +public: + // Note: If this structure is changed the struct in the swig files need to be changed as well + struct OnFileStruct + { + /// \brief The index into the set of files, from 0 to numberOfFilesInThisSet + unsigned fileIndex; + + /// \brief The name of the file + char fileName[512]; + + /// \brief The data pointed to by the file + char *fileData; + + /// \brief The amount of data to be downloaded for this file + BitSize_t byteLengthOfThisFile; + + /// \brief How many bytes of this file has been downloaded + BitSize_t bytesDownloadedForThisFile; + + /// \brief Files are transmitted in sets, where more than one set of files can be transmitted at the same time. + /// \details This is the identifier for the set, which is returned by FileListTransfer::SetupReceive + unsigned short setID; + + /// \brief The number of files that are in this set. + unsigned numberOfFilesInThisSet; + + /// \brief The total length of the transmitted files for this set, after being uncompressed + unsigned byteLengthOfThisSet; + + /// \brief The total length, in bytes, downloaded for this set. + unsigned bytesDownloadedForThisSet; + + /// \brief User data passed to one of the functions in the FileList class. + /// \details However, on error, this is instead changed to one of the enumerations in the PatchContext structure. + FileListNodeContext context; + + /// \brief Who sent this file + SystemAddress senderSystemAddress; + + /// \brief Who sent this file. Not valid when using TCP, only RakPeer (UDP) + RakNetGUID senderGuid; + }; + + // Note: If this structure is changed the struct in the swig files need to be changed as well + struct FileProgressStruct + { + /// \param[out] onFileStruct General information about this file, such as the filename and the first \a partLength bytes. You do NOT need to save this data yourself. The complete file will arrive normally. + OnFileStruct *onFileStruct; + /// \param[out] partCount The zero based index into partTotal. The percentage complete done of this file is 100 * (partCount+1)/partTotal + unsigned int partCount; + /// \param[out] partTotal The total number of parts this file was split into. Each part will be roughly the MTU size, minus the UDP header and RakNet headers + unsigned int partTotal; + /// \param[out] dataChunkLength How many bytes long firstDataChunk and iriDataChunk are + unsigned int dataChunkLength; + /// \param[out] firstDataChunk The first \a partLength of the final file. If you store identifying information about the file in the first \a partLength bytes, you can read them while the download is taking place. If this hasn't arrived yet, firstDataChunk will be 0 + char *firstDataChunk; + /// \param[out] iriDataChunk If the remote system is sending this file using IncrementalReadInterface, then this is the chunk we just downloaded. It will not exist in memory after this callback. You should either store this to disk, or in memory. If it is 0, then the file is smaller than one chunk, and will be held in memory automatically + char *iriDataChunk; + /// \param[out] iriWriteOffset Offset in bytes from the start of the file for the data pointed to by iriDataChunk + unsigned int iriWriteOffset; + /// \param[out] Who sent this file + SystemAddress senderSystemAddress; + /// \param[out] Who sent this file. Not valid when using TCP, only RakPeer (UDP) + RakNetGUID senderGuid; + /// \param[in] allocateIrIDataChunkAutomatically If true, then RakNet will hold iriDataChunk for you and return it in OnFile. Defaults to true + bool allocateIrIDataChunkAutomatically; + }; + + struct DownloadCompleteStruct + { + /// \brief Files are transmitted in sets, where more than one set of files can be transmitted at the same time. + /// \details This is the identifier for the set, which is returned by FileListTransfer::SetupReceive + unsigned short setID; + + /// \brief The number of files that are in this set. + unsigned numberOfFilesInThisSet; + + /// \brief The total length of the transmitted files for this set, after being uncompressed + unsigned byteLengthOfThisSet; + + /// \brief Who sent this file + SystemAddress senderSystemAddress; + + /// \brief Who sent this file. Not valid when using TCP, only RakPeer (UDP) + RakNetGUID senderGuid; + }; + + FileListTransferCBInterface() {} + virtual ~FileListTransferCBInterface() {} + + /// \brief Got a file. + /// \details This structure is only valid for the duration of this function call. + /// \return Return true to have RakNet delete the memory allocated to hold this file for this function call. + virtual bool OnFile(OnFileStruct *onFileStruct)=0; + + /// \brief Got part of a big file internally in RakNet + /// \details This is called in one of two circumstances: Either the transport layer is returning ID_PROGRESS_NOTIFICATION, or you got a block via IncrementalReadInterface + /// If the transport layer is returning ID_PROGRESS_NOTIFICATION (see RakPeer::SetSplitMessageProgressInterval()) then FileProgressStruct::iriDataChunk will be 0. + /// If this is a block via IncrementalReadInterface, then iriDataChunk will point to the block just downloaded. + /// If not using IncrementalReadInterface, then you only care about partCount and partTotal to tell how far the download has progressed. YOu can use firstDataChunk to read the first part of the file if desired. The file is usable when you get the OnFile callback. + /// If using IncrementalReadInterface and you let RakNet buffer the files in memory (default), then it is the same as above. The file is usable when you get the OnFile callback. + /// If using IncrementalReadInterface and you do not let RakNet buffer the files in memory, then set allocateIrIDataChunkAutomatically to false. Write the file to disk whenever you get OnFileProgress and iriDataChunk is not 0, and ignore OnFile. + virtual void OnFileProgress(FileProgressStruct *fps)=0; + + /// \brief Called while the handler is active by FileListTransfer + /// \details Return false when you are done with the class. + /// At that point OnDereference will be called and the class will no longer be maintained by the FileListTransfer plugin. + virtual bool Update(void) {return true;} + + /// \brief Called when the download is completed. + /// \details If you are finished with this class, return false. + /// At that point OnDereference will be called and the class will no longer be maintained by the FileListTransfer plugin. + /// Otherwise return true, and Update will continue to be called. + virtual bool OnDownloadComplete(DownloadCompleteStruct *dcs) {(void) dcs; return false;} + + /// \brief This function is called when this instance is about to be dereferenced by the FileListTransfer plugin. + /// \details Update will no longer be called. + /// It will will be deleted automatically if true was passed to FileListTransfer::SetupReceive::deleteHandler + /// Otherwise it is up to you to delete it yourself. + virtual void OnDereference(void) {} +}; + +} // namespace RakNet + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif + diff --git a/Source/FileOperations.h b/Source/FileOperations.h index 12191149e..ed39133c5 100644 --- a/Source/FileOperations.h +++ b/Source/FileOperations.h @@ -1,32 +1,30 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file FileOperations.h -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_FileOperations==1 - -#ifndef __FILE_OPERATIONS_H -#define __FILE_OPERATIONS_H - -#include "Export.h" - -bool RAK_DLL_EXPORT WriteFileWithDirectories( const char *path, char *data, unsigned dataLength ); -bool RAK_DLL_EXPORT IsSlash(unsigned char c); -void RAK_DLL_EXPORT AddSlash( char *input ); -void RAK_DLL_EXPORT QuoteIfSpaces(char *str); -bool RAK_DLL_EXPORT DirectoryExists(const char *directory); -unsigned int RAK_DLL_EXPORT GetFileLength(const char *path); - -#endif - -#endif // _RAKNET_SUPPORT_FileOperations +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file FileOperations.h +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_FileOperations==1 + +#pragma once + +#include "Export.h" + +bool RAK_DLL_EXPORT WriteFileWithDirectories( const char *path, char *data, unsigned dataLength ); +bool RAK_DLL_EXPORT IsSlash(unsigned char c); +void RAK_DLL_EXPORT AddSlash( char *input ); +void RAK_DLL_EXPORT QuoteIfSpaces(char *str); +bool RAK_DLL_EXPORT DirectoryExists(const char *directory); +unsigned int RAK_DLL_EXPORT GetFileLength(const char *path); + +#endif + diff --git a/Source/FormatString.h b/Source/FormatString.h index 50670b8a0..0b9be899d 100644 --- a/Source/FormatString.h +++ b/Source/FormatString.h @@ -1,30 +1,27 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file FormatString.h -/// - - -#ifndef __FORMAT_STRING_H -#define __FORMAT_STRING_H - -#include "Export.h" - -extern "C" { -char * FormatString(const char *format, ...); -} -// Threadsafe -extern "C" { -char * FormatStringTS(char *output, const char *format, ...); -} - - -#endif - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file FormatString.h +/// + + +#pragma once + +#include "Export.h" + +extern "C" { +char * FormatString(const char *format, ...); +} +// Threadsafe +extern "C" { +char * FormatStringTS(char *output, const char *format, ...); +} + + diff --git a/Source/FullyConnectedMesh2.h b/Source/FullyConnectedMesh2.h index 5a1f4a4ea..e1a81f225 100644 --- a/Source/FullyConnectedMesh2.h +++ b/Source/FullyConnectedMesh2.h @@ -1,424 +1,422 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file FullyConnectedMesh2.h -/// \brief Fully connected mesh plugin, revision 2. -/// \details This will connect RakPeer to all connecting peers, and all peers the connecting peer knows about. -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_FullyConnectedMesh2==1 - -#ifndef __FULLY_CONNECTED_MESH_2_H -#define __FULLY_CONNECTED_MESH_2_H - -#include "PluginInterface2.h" -#include "RakMemoryOverride.h" -#include "NativeTypes.h" -#include "DS_List.h" -#include "RakString.h" -#include "BitStream.h" - -typedef int64_t FCM2Guid; - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; - -/// \brief Fully connected mesh plugin, revision 2 -/// \details This will connect RakPeer to all connecting peers, and all peers the connecting peer knows about.
-/// It will also calculate which system has been running longest, to find out who should be host, if you need one system to act as a host -/// \pre You must also install the ConnectionGraph2 plugin in order to use SetConnectOnNewRemoteConnection() -/// \ingroup FULLY_CONNECTED_MESH_GROUP -class RAK_DLL_EXPORT FullyConnectedMesh2 : public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(FullyConnectedMesh2) - - FullyConnectedMesh2(); - virtual ~FullyConnectedMesh2(); - - /// When the message ID_REMOTE_NEW_INCOMING_CONNECTION arrives, we try to connect to that system - /// If \a attemptConnection is false, you can manually connect to all systems listed in ID_REMOTE_NEW_INCOMING_CONNECTION with ConnectToRemoteNewIncomingConnections() - /// \note This will not work on any console. It will also not work if NAT punchthrough is needed. Generally, this should be false and you should connect manually. It is here for legacy reasons. - /// \param[in] attemptConnection If true, we try to connect to any systems we are notified about with ID_REMOTE_NEW_INCOMING_CONNECTION, which comes from the ConnectionGraph2 plugin. Defaults to true. - /// \param[in] pw The password to use to connect with. Only used if \a attemptConnection is true - void SetConnectOnNewRemoteConnection(bool attemptConnection, RakNet::RakString pw); - - /// \brief The connected host is whichever system we are connected to that has been running the longest. - /// \details Will return UNASSIGNED_RAKNET_GUID if we are not connected to anyone, or if we are connected and are calculating the host - /// If includeCalculating is true, will return the estimated calculated host as long as the calculation is nearly complete - /// includeCalculating should be true if you are taking action based on another system becoming host, because not all host calculations may complete at the exact same time - /// \sa ConnectionGraph2::GetLowestAveragePingSystem() . If you need one system in the peer to peer group to relay data, have the host call this function after host migration, and use that system - /// \return System address of whichever system is host. - RakNetGUID GetConnectedHost(void) const; - SystemAddress GetConnectedHostAddr(void) const; - - /// \return System address of whichever system is host. Always returns something, even though it may be our own system. - RakNetGUID GetHostSystem(void) const; - - /// \return If our system is host - bool IsHostSystem(void) const; - - /// Get the list of connected systems, from oldest connected to newest - /// This is also the order that the hosts will be chosen in - void GetHostOrder(DataStructures::List &hostList); - - /// \param[in] includeCalculating If true, and we are currently calculating a new host, return the new host if the calculation is nearly complete - /// \return If our system is host - bool IsConnectedHost(void) const; - - /// \brief Automatically add new connections to the fully connected mesh. - /// Each remote system that you want to check should be added as a participant, either through SetAutoparticipateConnections() or by calling this function - /// \details Defaults to true. - /// \param[in] b As stated - void SetAutoparticipateConnections(bool b); - - /// Clear our own host order, and recalculate as if we had just reconnected - /// Call this to reset the running time of the host just before joining/creating a game room for networking - void ResetHostCalculation(void); - - /// \brief if SetAutoparticipateConnections() is called with false, then you need to use AddParticipant before these systems will be added to the mesh - /// FullyConnectedMesh2 will track who is the who host among a fully connected mesh of participants - /// Each remote system that you want to check should be added as a participant, either through SetAutoparticipateConnections() or by calling this function - /// \param[in] participant The new participant - /// \param[in] userContext Static data to be passed around with each participant, which can be queried with GetParticipantData(). - /// \sa StartVerifiedJoin() - void AddParticipant(RakNetGUID rakNetGuid); - - /// Get the participants added with AddParticipant() - /// \param[out] participantList Participants added with AddParticipant(); - void GetParticipantList(DataStructures::List &participantList); - - /// \brief Returns if a participant is in the participant list - /// \param[in] RakNetGUID of the participant to query - /// \return True if in the list - bool HasParticipant(RakNetGUID participantGuid); - - /// \brief Reads userData written with SetMyContext() - /// \param[in] RakNetGUID of the participant to query - /// \param[out] userContext Pointer to BitStream to be written to - /// \return True if data was written - // bool GetParticipantContext(RakNetGUID participantGuid, BitStream *userContext); - - /// Set data for other systems to read with GetParticipantContext - /// \param[in] userContext Pointer to BitStream to be read from - // void SetMyContext(BitStream *userContext); - - /// Connect to all systems from ID_REMOTE_NEW_INCOMING_CONNECTION - /// You can call this if SetConnectOnNewRemoteConnection is false - /// \param[in] packet The packet containing ID_REMOTE_NEW_INCOMING_CONNECTION - /// \param[in] connectionPassword Password passed to RakPeerInterface::Connect() - /// \param[in] connectionPasswordLength Password length passed to RakPeerInterface::Connect() - void ConnectToRemoteNewIncomingConnections(Packet *packet); - - /// \brief Clear all memory and reset everything - void Clear(void); - - unsigned int GetParticipantCount(void) const; - void GetParticipantCount(unsigned int *participantListSize) const; - - /// In the simple case of forming a peer to peer mesh: - /// - /// 1. AddParticipant() is called on the host whenever you get a new connection - /// 2. The host sends all participants to the new client - /// 3. The client connects to the participant list - /// - /// However, the above steps assumes connections to all systems in the mesh always complete. - /// When there is a risk of failure, such as if relying on NATPunchthroughClient, you may not want to call AddParticipant() until are connections have completed to all other particpants - /// StartVerifiedJoin() can manage the overhead of the negotiation involved so the programmer only has to deal with overall success or failure - /// - /// Processing: - /// 1. Send the RakNetGUID and SystemAddress values of GetParticipantList() to the client with ID_FCM2_VERIFIED_JOIN_START - /// 2. The client, on ID_FCM2_VERIFIED_JOIN_START, can execute NatPunchthroughClient::OpenNAT() (optional), followed by RakPeerInterface::Connect() if punchthrough success, for each system returned from GetVerifiedJoinRequiredProcessingList() - /// 3. After all participants in step 2 have connected, failed to connect, or failed NatPunchthrough, the client automatically sends the results to the server. - /// 4. The server compares the results of the operations in step 2 with the values from GetParticpantList(). - /// 4A. If the client failed to connect to a current participant, return ID_FCM2_VERIFIED_JOIN_FAILED to the client. CloseConnection() is automatically called on the client for the failed participants. - /// 4B. If AddParticipant() was called between steps 1 and 4, go back to step 1, transmitting new participants. - /// 4C. If the client successfully connected to all participants, the server gets ID_FCM2_VERIFIED_JOIN_CAPABLE. The server programmer, on the same frame, should execute RespondOnVerifiedJoinCapable() to either accept or reject the client. - /// 5. If the client got ID_FCM2_VERIFIED_JOIN_ACCEPTED, AddParticipant() is automatically called for each system in the mesh. - /// 6. If the client got ID_FCM2_VERIFIED_JOIN_REJECTED, CloseConnection() is automatically called for each system in the mesh. The connection is NOT automatically closed to the original host that sent StartVerifiedJoin(). - /// 7. If the client's connection to the server was lost before getting ID_FCM2_VERIFIED_JOIN_ACCEPTED or ID_FCM2_VERIFIED_JOIN_REJECTED, return to the programmer ID_FCM2_VERIFIED_JOIN_FAILED and call RakPeerInterface::CloseConnection() - /// - /// \brief Notify the client of GetParticipantList() in order to connect to each of those systems until the mesh has been completed - /// \param[in] client The system to send ID_FCM2_VERIFIED_JOIN_START to - virtual void StartVerifiedJoin(RakNetGUID client); - - /// \brief On ID_FCM2_VERIFIED_JOIN_CAPABLE , accept or reject the new connection - /// \code - /// fullyConnectedMesh->RespondOnVerifiedJoinCapable(packet, true, 0); - /// \endcode - /// \param[in] packet The system that sent ID_FCM2_VERIFIED_JOIN_CAPABLE. Based on \accept, ID_FCM2_VERIFIED_JOIN_ACCEPTED or ID_FCM2_VERIFIED_JOIN_REJECTED will be sent in reply - /// \param[in] accept True to accept, and thereby automatically call AddParticipant() on all systems on the mesh. False to reject, and call CloseConnection() to all mesh systems on the target - /// \param[in] additionalData Any additional data you want to add to the ID_FCM2_VERIFIED_JOIN_ACCEPTED or ID_FCM2_VERIFIED_JOIN_REJECTED messages - /// \sa WriteVJCUserData() - virtual void RespondOnVerifiedJoinCapable(Packet *packet, bool accept, BitStream *additionalData); - - /// \brief On ID_FCM2_VERIFIED_JOIN_START, read the SystemAddress and RakNetGUID values of each system to connect to - /// \code - /// DataStructures::List addresses; - /// DataStructures::List guids; - /// fullyConnectedMesh->GetVerifiedJoinRequiredProcessingList(packet->guid, addresses, guids); - /// for (unsigned int i=0; i < addresses.Size(); i++) - /// rakPeer[i]->Connect(addresses[i].ToString(false), addresses[i].GetPort(), 0, 0); - /// \endcode - /// \param[in] host Which system sent ID_FCM2_VERIFIED_JOIN_START - /// \param[out] addresses SystemAddress values of systems to connect to. List has the same number and order as \a guids - /// \param[out] guids RakNetGUID values of systems to connect to. List has the same number and order as \a guids - /// \param[out] userData What was written with WriteVJSUserData - virtual void GetVerifiedJoinRequiredProcessingList(RakNetGUID host, - DataStructures::List &addresses, - DataStructures::List &guids, - DataStructures::List &userData); - - /// \brief On ID_FCM2_VERIFIED_JOIN_ACCEPTED, read additional data passed to RespondOnVerifiedJoinCapable() - /// \code - /// bool thisSystemAccepted; - /// DataStructures::List systemsAccepted; - /// RakNet::BitStream additionalData; - /// fullyConnectedMesh->GetVerifiedJoinAcceptedAdditionalData(packet, &thisSystemAccepted, systemsAccepted, &additionalData); - /// \endcode - /// \param[in] packet Packet containing the ID_FCM2_VERIFIED_JOIN_ACCEPTED message - /// \param[out] thisSystemAccepted If true, it was this instance of RakPeerInterface that was accepted. If false, this is notification for another system - /// \param[out] systemsAccepted Which system(s) were added with AddParticipant(). If \a thisSystemAccepted is false, this list will only have length 1 - /// \param[out] additionalData \a additionalData parameter passed to RespondOnVerifiedJoinCapable() - virtual void GetVerifiedJoinAcceptedAdditionalData(Packet *packet, bool *thisSystemAccepted, DataStructures::List &systemsAccepted, BitStream *additionalData); - - /// \brief On ID_FCM2_VERIFIED_JOIN_REJECTED, read additional data passed to RespondOnVerifiedJoinCapable() - /// \details This does not automatically close the connection. The following code will do so: - /// \code - /// rakPeer[i]->CloseConnection(packet->guid, true); - /// \endcode - /// \param[in] packet Packet containing the ID_FCM2_VERIFIED_JOIN_REJECTED message - /// \param[out] additionalData \a additionalData parameter passed to RespondOnVerifiedJoinCapable(). - virtual void GetVerifiedJoinRejectedAdditionalData(Packet *packet, BitStream *additionalData); - - /// Override to write data when ID_FCM2_VERIFIED_JOIN_CAPABLE is sent - virtual void WriteVJCUserData(RakNet::BitStream *bsOut) {(void) bsOut;} - - /// Use to read data written from WriteVJCUserData() - /// \code - /// RakNet::BitStream bsIn(packet->data,packet->length,false); - /// FullyConnectedMesh2::SkipToVJCUserData(&bsIn); - /// // Your code here - static void SkipToVJCUserData(RakNet::BitStream *bsIn); - - /// Write custom user data to be sent with ID_FCM2_VERIFIED_JOIN_START, per user - /// \param[out] bsOut Write your data here, if any. Has to match what is read by ReadVJSUserData - /// \param[in] userGuid The RakNetGuid of the user you are writing for - /// \param[in] userContext The data set with SetMyContext() for that system. May be empty. To properly write userContext, you will need to first write userContext->GetNumberOfBitsUsed(), followed by bsOut->Write(userContext); - //virtual void WriteVJSUserData(RakNet::BitStream *bsOut, RakNetGUID userGuid, BitStream *userContext) {(void) bsOut; (void) userGuid; (void) userContext;} - virtual void WriteVJSUserData(RakNet::BitStream *bsOut, RakNetGUID userGuid) {(void) bsOut; (void) userGuid;} - - /// \internal - RakNet::TimeUS GetElapsedRuntime(void); - - /// \internal - virtual PluginReceiveResult OnReceive(Packet *packet); - /// \internal - virtual void OnRakPeerStartup(void); - /// \internal - virtual void OnAttach(void); - /// \internal - virtual void OnRakPeerShutdown(void); - /// \internal - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - /// \internal - virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); - /// \internal - virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason); - - /// \internal - struct FCM2Participant - { - FCM2Participant() {} - FCM2Participant(const FCM2Guid &_fcm2Guid, const RakNetGUID &_rakNetGuid) : fcm2Guid(_fcm2Guid), rakNetGuid(_rakNetGuid) {} - - // Low half is a random number. - // High half is the order we connected in (totalConnectionCount) - FCM2Guid fcm2Guid; - RakNetGUID rakNetGuid; - // BitStream userContext; - }; - - enum JoinInProgressState - { - JIPS_PROCESSING, - JIPS_FAILED, - JIPS_CONNECTED, - JIPS_UNNECESSARY, - }; - - struct VerifiedJoinInProgressMember - { - SystemAddress systemAddress; - RakNetGUID guid; - JoinInProgressState joinInProgressState; - BitStream *userData; - - bool workingFlag; - }; - - /// \internal - struct VerifiedJoinInProgress - { - RakNetGUID requester; - DataStructures::List vjipMembers; - //bool sentResults; - }; - - /// \internal for debugging - unsigned int GetTotalConnectionCount(void) const; - -protected: - void PushNewHost(const RakNetGUID &guid, RakNetGUID oldHost); - void SendOurFCMGuid(SystemAddress addr); - void SendFCMGuidRequest(RakNetGUID rakNetGuid); - void SendConnectionCountResponse(SystemAddress addr, unsigned int responseTotalConnectionCount); - void OnRequestFCMGuid(Packet *packet); - //void OnUpdateUserContext(Packet *packet); - void OnRespondConnectionCount(Packet *packet); - void OnInformFCMGuid(Packet *packet); - void OnUpdateMinTotalConnectionCount(Packet *packet); - void AssignOurFCMGuid(void); - void CalculateHost(RakNetGUID *rakNetGuid, FCM2Guid *fcm2Guid); - // bool AddParticipantInternal( RakNetGUID rakNetGuid, FCM2Guid theirFCMGuid, BitStream *userContext ); - bool AddParticipantInternal( RakNetGUID rakNetGuid, FCM2Guid theirFCMGuid ); - void CalculateAndPushHost(void); - bool ParticipantListComplete(void); - void IncrementTotalConnectionCount(unsigned int i); - PluginReceiveResult OnVerifiedJoinStart(Packet *packet); - PluginReceiveResult OnVerifiedJoinCapable(Packet *packet); - virtual void OnVerifiedJoinFailed(RakNetGUID hostGuid, bool callCloseConnection); - virtual void OnVerifiedJoinAccepted(Packet *packet); - virtual void OnVerifiedJoinRejected(Packet *packet); - unsigned int GetJoinsInProgressIndex(RakNetGUID requester) const; - void UpdateVerifiedJoinInProgressMember(const AddressOrGUID systemIdentifier, RakNetGUID guidToAssign, JoinInProgressState newState); - bool ProcessVerifiedJoinInProgressIfCompleted(VerifiedJoinInProgress *vjip); - void ReadVerifiedJoinInProgressMember(RakNet::BitStream *bsIn, VerifiedJoinInProgressMember *vjipm); - unsigned int GetVerifiedJoinInProgressMemberIndex(const AddressOrGUID systemIdentifier, VerifiedJoinInProgress *vjip); - void DecomposeJoinCapable(Packet *packet, VerifiedJoinInProgress *vjip); - void WriteVerifiedJoinCapable(RakNet::BitStream *bsOut, VerifiedJoinInProgress *vjip); - void CategorizeVJIP(VerifiedJoinInProgress *vjip, - DataStructures::List &participatingMembersOnClientSucceeded, - DataStructures::List &participatingMembersOnClientFailed, - DataStructures::List &participatingMembersNotOnClient, - DataStructures::List &clientMembersNotParticipatingSucceeded, - DataStructures::List &clientMembersNotParticipatingFailed); - - // Used to track how long RakNet has been running. This is so we know who has been running longest - RakNet::TimeUS startupTime; - - // Option for SetAutoparticipateConnections - bool autoParticipateConnections; - - // totalConnectionCount is roughly maintained across all systems, and increments by 1 each time a new system connects to the mesh - // It is always kept at the highest known value - // It is used as the high 4 bytes for new FCMGuids. This causes newer values of FCM2Guid to be higher than lower values. The lowest value is the host. - unsigned int totalConnectionCount; - - // Our own ourFCMGuid. Starts at unassigned (0). Assigned once we send ID_FCM2_REQUEST_FCMGUID and get back ID_FCM2_RESPOND_CONNECTION_COUNT - FCM2Guid ourFCMGuid; - - /// List of systems we know the FCM2Guid for - DataStructures::List fcm2ParticipantList; - - RakNetGUID lastPushedHost; - - // Optimization: Store last calculated host in these variables. - RakNetGUID hostRakNetGuid; - FCM2Guid hostFCM2Guid; - - RakNet::RakString connectionPassword; - bool connectOnNewRemoteConnections; - - DataStructures::List joinsInProgress; - BitStream myContext; -}; - -} // namespace RakNet - -/* -Startup() -ourFCMGuid=unknown -totalConnectionCount=0 -Set startupTime - -AddParticipant() -if (sender by guid is a participant) -return; -AddParticipantInternal(guid); -if (ourFCMGuid==unknown) -Send to that system a request for their fcmGuid, totalConnectionCount. Inform startupTime. -else -Send to that system a request for their fcmGuid. Inform total connection count, our fcmGuid - -OnRequestGuid() -if (sender by guid is not a participant) -{ - // They added us as a participant, but we didn't add them. This can be caused by lag where both participants are not added at the same time. - // It doesn't affect the outcome as long as we still process the data - AddParticipantInternal(guid); -} -if (ourFCMGuid==unknown) -{ - if (includedStartupTime) - { - // Nobody has a fcmGuid - - if (their startup time is greater than our startup time) - ReplyConnectionCount(1); - else - ReplyConnectionCount(2); - } - else - { - // They have a fcmGuid, we do not - - SetMaxTotalConnectionCount(remoteCount); - AssignTheirGuid() - GenerateOurGuid(); - SendOurGuid(all); - } -} -else -{ - if (includedStartupTime) - { - // We have a fcmGuid they do not - - ReplyConnectionCount(totalConnectionCount+1); - SendOurGuid(sender); - } - else - { - // We both have fcmGuids - - SetMaxTotalConnectionCount(remoteCount); - AssignTheirGuid(); - SendOurGuid(sender); - } -} - -OnReplyConnectionCount() -SetMaxTotalConnectionCount(remoteCount); -GenerateOurGuid(); -SendOurGuid(allParticipants); - -OnReceiveTheirGuid() -AssignTheirGuid() -*/ - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file FullyConnectedMesh2.h +/// \brief Fully connected mesh plugin, revision 2. +/// \details This will connect RakPeer to all connecting peers, and all peers the connecting peer knows about. +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_FullyConnectedMesh2==1 + +#pragma once + +#include "PluginInterface2.h" +#include "RakMemoryOverride.h" +#include "NativeTypes.h" +#include "DS_List.h" +#include "RakString.h" +#include "BitStream.h" + +typedef int64_t FCM2Guid; + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; + +/// \brief Fully connected mesh plugin, revision 2 +/// \details This will connect RakPeer to all connecting peers, and all peers the connecting peer knows about.
+/// It will also calculate which system has been running longest, to find out who should be host, if you need one system to act as a host +/// \pre You must also install the ConnectionGraph2 plugin in order to use SetConnectOnNewRemoteConnection() +/// \ingroup FULLY_CONNECTED_MESH_GROUP +class RAK_DLL_EXPORT FullyConnectedMesh2 : public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(FullyConnectedMesh2) + + FullyConnectedMesh2(); + virtual ~FullyConnectedMesh2(); + + /// When the message ID_REMOTE_NEW_INCOMING_CONNECTION arrives, we try to connect to that system + /// If \a attemptConnection is false, you can manually connect to all systems listed in ID_REMOTE_NEW_INCOMING_CONNECTION with ConnectToRemoteNewIncomingConnections() + /// \note This will not work on any console. It will also not work if NAT punchthrough is needed. Generally, this should be false and you should connect manually. It is here for legacy reasons. + /// \param[in] attemptConnection If true, we try to connect to any systems we are notified about with ID_REMOTE_NEW_INCOMING_CONNECTION, which comes from the ConnectionGraph2 plugin. Defaults to true. + /// \param[in] pw The password to use to connect with. Only used if \a attemptConnection is true + void SetConnectOnNewRemoteConnection(bool attemptConnection, RakNet::RakString pw); + + /// \brief The connected host is whichever system we are connected to that has been running the longest. + /// \details Will return UNASSIGNED_RAKNET_GUID if we are not connected to anyone, or if we are connected and are calculating the host + /// If includeCalculating is true, will return the estimated calculated host as long as the calculation is nearly complete + /// includeCalculating should be true if you are taking action based on another system becoming host, because not all host calculations may complete at the exact same time + /// \sa ConnectionGraph2::GetLowestAveragePingSystem() . If you need one system in the peer to peer group to relay data, have the host call this function after host migration, and use that system + /// \return System address of whichever system is host. + RakNetGUID GetConnectedHost(void) const; + SystemAddress GetConnectedHostAddr(void) const; + + /// \return System address of whichever system is host. Always returns something, even though it may be our own system. + RakNetGUID GetHostSystem(void) const; + + /// \return If our system is host + bool IsHostSystem(void) const; + + /// Get the list of connected systems, from oldest connected to newest + /// This is also the order that the hosts will be chosen in + void GetHostOrder(DataStructures::List &hostList); + + /// \param[in] includeCalculating If true, and we are currently calculating a new host, return the new host if the calculation is nearly complete + /// \return If our system is host + bool IsConnectedHost(void) const; + + /// \brief Automatically add new connections to the fully connected mesh. + /// Each remote system that you want to check should be added as a participant, either through SetAutoparticipateConnections() or by calling this function + /// \details Defaults to true. + /// \param[in] b As stated + void SetAutoparticipateConnections(bool b); + + /// Clear our own host order, and recalculate as if we had just reconnected + /// Call this to reset the running time of the host just before joining/creating a game room for networking + void ResetHostCalculation(void); + + /// \brief if SetAutoparticipateConnections() is called with false, then you need to use AddParticipant before these systems will be added to the mesh + /// FullyConnectedMesh2 will track who is the who host among a fully connected mesh of participants + /// Each remote system that you want to check should be added as a participant, either through SetAutoparticipateConnections() or by calling this function + /// \param[in] participant The new participant + /// \param[in] userContext Static data to be passed around with each participant, which can be queried with GetParticipantData(). + /// \sa StartVerifiedJoin() + void AddParticipant(RakNetGUID rakNetGuid); + + /// Get the participants added with AddParticipant() + /// \param[out] participantList Participants added with AddParticipant(); + void GetParticipantList(DataStructures::List &participantList); + + /// \brief Returns if a participant is in the participant list + /// \param[in] RakNetGUID of the participant to query + /// \return True if in the list + bool HasParticipant(RakNetGUID participantGuid); + + /// \brief Reads userData written with SetMyContext() + /// \param[in] RakNetGUID of the participant to query + /// \param[out] userContext Pointer to BitStream to be written to + /// \return True if data was written + // bool GetParticipantContext(RakNetGUID participantGuid, BitStream *userContext); + + /// Set data for other systems to read with GetParticipantContext + /// \param[in] userContext Pointer to BitStream to be read from + // void SetMyContext(BitStream *userContext); + + /// Connect to all systems from ID_REMOTE_NEW_INCOMING_CONNECTION + /// You can call this if SetConnectOnNewRemoteConnection is false + /// \param[in] packet The packet containing ID_REMOTE_NEW_INCOMING_CONNECTION + /// \param[in] connectionPassword Password passed to RakPeerInterface::Connect() + /// \param[in] connectionPasswordLength Password length passed to RakPeerInterface::Connect() + void ConnectToRemoteNewIncomingConnections(Packet *packet); + + /// \brief Clear all memory and reset everything + void Clear(void); + + unsigned int GetParticipantCount(void) const; + void GetParticipantCount(unsigned int *participantListSize) const; + + /// In the simple case of forming a peer to peer mesh: + /// + /// 1. AddParticipant() is called on the host whenever you get a new connection + /// 2. The host sends all participants to the new client + /// 3. The client connects to the participant list + /// + /// However, the above steps assumes connections to all systems in the mesh always complete. + /// When there is a risk of failure, such as if relying on NATPunchthroughClient, you may not want to call AddParticipant() until are connections have completed to all other particpants + /// StartVerifiedJoin() can manage the overhead of the negotiation involved so the programmer only has to deal with overall success or failure + /// + /// Processing: + /// 1. Send the RakNetGUID and SystemAddress values of GetParticipantList() to the client with ID_FCM2_VERIFIED_JOIN_START + /// 2. The client, on ID_FCM2_VERIFIED_JOIN_START, can execute NatPunchthroughClient::OpenNAT() (optional), followed by RakPeerInterface::Connect() if punchthrough success, for each system returned from GetVerifiedJoinRequiredProcessingList() + /// 3. After all participants in step 2 have connected, failed to connect, or failed NatPunchthrough, the client automatically sends the results to the server. + /// 4. The server compares the results of the operations in step 2 with the values from GetParticpantList(). + /// 4A. If the client failed to connect to a current participant, return ID_FCM2_VERIFIED_JOIN_FAILED to the client. CloseConnection() is automatically called on the client for the failed participants. + /// 4B. If AddParticipant() was called between steps 1 and 4, go back to step 1, transmitting new participants. + /// 4C. If the client successfully connected to all participants, the server gets ID_FCM2_VERIFIED_JOIN_CAPABLE. The server programmer, on the same frame, should execute RespondOnVerifiedJoinCapable() to either accept or reject the client. + /// 5. If the client got ID_FCM2_VERIFIED_JOIN_ACCEPTED, AddParticipant() is automatically called for each system in the mesh. + /// 6. If the client got ID_FCM2_VERIFIED_JOIN_REJECTED, CloseConnection() is automatically called for each system in the mesh. The connection is NOT automatically closed to the original host that sent StartVerifiedJoin(). + /// 7. If the client's connection to the server was lost before getting ID_FCM2_VERIFIED_JOIN_ACCEPTED or ID_FCM2_VERIFIED_JOIN_REJECTED, return to the programmer ID_FCM2_VERIFIED_JOIN_FAILED and call RakPeerInterface::CloseConnection() + /// + /// \brief Notify the client of GetParticipantList() in order to connect to each of those systems until the mesh has been completed + /// \param[in] client The system to send ID_FCM2_VERIFIED_JOIN_START to + virtual void StartVerifiedJoin(RakNetGUID client); + + /// \brief On ID_FCM2_VERIFIED_JOIN_CAPABLE , accept or reject the new connection + /// \code + /// fullyConnectedMesh->RespondOnVerifiedJoinCapable(packet, true, 0); + /// \endcode + /// \param[in] packet The system that sent ID_FCM2_VERIFIED_JOIN_CAPABLE. Based on \accept, ID_FCM2_VERIFIED_JOIN_ACCEPTED or ID_FCM2_VERIFIED_JOIN_REJECTED will be sent in reply + /// \param[in] accept True to accept, and thereby automatically call AddParticipant() on all systems on the mesh. False to reject, and call CloseConnection() to all mesh systems on the target + /// \param[in] additionalData Any additional data you want to add to the ID_FCM2_VERIFIED_JOIN_ACCEPTED or ID_FCM2_VERIFIED_JOIN_REJECTED messages + /// \sa WriteVJCUserData() + virtual void RespondOnVerifiedJoinCapable(Packet *packet, bool accept, BitStream *additionalData); + + /// \brief On ID_FCM2_VERIFIED_JOIN_START, read the SystemAddress and RakNetGUID values of each system to connect to + /// \code + /// DataStructures::List addresses; + /// DataStructures::List guids; + /// fullyConnectedMesh->GetVerifiedJoinRequiredProcessingList(packet->guid, addresses, guids); + /// for (unsigned int i=0; i < addresses.Size(); i++) + /// rakPeer[i]->Connect(addresses[i].ToString(false), addresses[i].GetPort(), 0, 0); + /// \endcode + /// \param[in] host Which system sent ID_FCM2_VERIFIED_JOIN_START + /// \param[out] addresses SystemAddress values of systems to connect to. List has the same number and order as \a guids + /// \param[out] guids RakNetGUID values of systems to connect to. List has the same number and order as \a guids + /// \param[out] userData What was written with WriteVJSUserData + virtual void GetVerifiedJoinRequiredProcessingList(RakNetGUID host, + DataStructures::List &addresses, + DataStructures::List &guids, + DataStructures::List &userData); + + /// \brief On ID_FCM2_VERIFIED_JOIN_ACCEPTED, read additional data passed to RespondOnVerifiedJoinCapable() + /// \code + /// bool thisSystemAccepted; + /// DataStructures::List systemsAccepted; + /// RakNet::BitStream additionalData; + /// fullyConnectedMesh->GetVerifiedJoinAcceptedAdditionalData(packet, &thisSystemAccepted, systemsAccepted, &additionalData); + /// \endcode + /// \param[in] packet Packet containing the ID_FCM2_VERIFIED_JOIN_ACCEPTED message + /// \param[out] thisSystemAccepted If true, it was this instance of RakPeerInterface that was accepted. If false, this is notification for another system + /// \param[out] systemsAccepted Which system(s) were added with AddParticipant(). If \a thisSystemAccepted is false, this list will only have length 1 + /// \param[out] additionalData \a additionalData parameter passed to RespondOnVerifiedJoinCapable() + virtual void GetVerifiedJoinAcceptedAdditionalData(Packet *packet, bool *thisSystemAccepted, DataStructures::List &systemsAccepted, BitStream *additionalData); + + /// \brief On ID_FCM2_VERIFIED_JOIN_REJECTED, read additional data passed to RespondOnVerifiedJoinCapable() + /// \details This does not automatically close the connection. The following code will do so: + /// \code + /// rakPeer[i]->CloseConnection(packet->guid, true); + /// \endcode + /// \param[in] packet Packet containing the ID_FCM2_VERIFIED_JOIN_REJECTED message + /// \param[out] additionalData \a additionalData parameter passed to RespondOnVerifiedJoinCapable(). + virtual void GetVerifiedJoinRejectedAdditionalData(Packet *packet, BitStream *additionalData); + + /// Override to write data when ID_FCM2_VERIFIED_JOIN_CAPABLE is sent + virtual void WriteVJCUserData(RakNet::BitStream *bsOut) {(void) bsOut;} + + /// Use to read data written from WriteVJCUserData() + /// \code + /// RakNet::BitStream bsIn(packet->data,packet->length,false); + /// FullyConnectedMesh2::SkipToVJCUserData(&bsIn); + /// // Your code here + static void SkipToVJCUserData(RakNet::BitStream *bsIn); + + /// Write custom user data to be sent with ID_FCM2_VERIFIED_JOIN_START, per user + /// \param[out] bsOut Write your data here, if any. Has to match what is read by ReadVJSUserData + /// \param[in] userGuid The RakNetGuid of the user you are writing for + /// \param[in] userContext The data set with SetMyContext() for that system. May be empty. To properly write userContext, you will need to first write userContext->GetNumberOfBitsUsed(), followed by bsOut->Write(userContext); + //virtual void WriteVJSUserData(RakNet::BitStream *bsOut, RakNetGUID userGuid, BitStream *userContext) {(void) bsOut; (void) userGuid; (void) userContext;} + virtual void WriteVJSUserData(RakNet::BitStream *bsOut, RakNetGUID userGuid) {(void) bsOut; (void) userGuid;} + + /// \internal + RakNet::TimeUS GetElapsedRuntime(void); + + /// \internal + virtual PluginReceiveResult OnReceive(Packet *packet); + /// \internal + virtual void OnRakPeerStartup(void); + /// \internal + virtual void OnAttach(void); + /// \internal + virtual void OnRakPeerShutdown(void); + /// \internal + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + /// \internal + virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); + /// \internal + virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason); + + /// \internal + struct FCM2Participant + { + FCM2Participant() {} + FCM2Participant(const FCM2Guid &_fcm2Guid, const RakNetGUID &_rakNetGuid) : fcm2Guid(_fcm2Guid), rakNetGuid(_rakNetGuid) {} + + // Low half is a random number. + // High half is the order we connected in (totalConnectionCount) + FCM2Guid fcm2Guid; + RakNetGUID rakNetGuid; + // BitStream userContext; + }; + + enum JoinInProgressState + { + JIPS_PROCESSING, + JIPS_FAILED, + JIPS_CONNECTED, + JIPS_UNNECESSARY, + }; + + struct VerifiedJoinInProgressMember + { + SystemAddress systemAddress; + RakNetGUID guid; + JoinInProgressState joinInProgressState; + BitStream *userData; + + bool workingFlag; + }; + + /// \internal + struct VerifiedJoinInProgress + { + RakNetGUID requester; + DataStructures::List vjipMembers; + //bool sentResults; + }; + + /// \internal for debugging + unsigned int GetTotalConnectionCount(void) const; + +protected: + void PushNewHost(const RakNetGUID &guid, RakNetGUID oldHost); + void SendOurFCMGuid(SystemAddress addr); + void SendFCMGuidRequest(RakNetGUID rakNetGuid); + void SendConnectionCountResponse(SystemAddress addr, unsigned int responseTotalConnectionCount); + void OnRequestFCMGuid(Packet *packet); + //void OnUpdateUserContext(Packet *packet); + void OnRespondConnectionCount(Packet *packet); + void OnInformFCMGuid(Packet *packet); + void OnUpdateMinTotalConnectionCount(Packet *packet); + void AssignOurFCMGuid(void); + void CalculateHost(RakNetGUID *rakNetGuid, FCM2Guid *fcm2Guid); + // bool AddParticipantInternal( RakNetGUID rakNetGuid, FCM2Guid theirFCMGuid, BitStream *userContext ); + bool AddParticipantInternal( RakNetGUID rakNetGuid, FCM2Guid theirFCMGuid ); + void CalculateAndPushHost(void); + bool ParticipantListComplete(void); + void IncrementTotalConnectionCount(unsigned int i); + PluginReceiveResult OnVerifiedJoinStart(Packet *packet); + PluginReceiveResult OnVerifiedJoinCapable(Packet *packet); + virtual void OnVerifiedJoinFailed(RakNetGUID hostGuid, bool callCloseConnection); + virtual void OnVerifiedJoinAccepted(Packet *packet); + virtual void OnVerifiedJoinRejected(Packet *packet); + unsigned int GetJoinsInProgressIndex(RakNetGUID requester) const; + void UpdateVerifiedJoinInProgressMember(const AddressOrGUID systemIdentifier, RakNetGUID guidToAssign, JoinInProgressState newState); + bool ProcessVerifiedJoinInProgressIfCompleted(VerifiedJoinInProgress *vjip); + void ReadVerifiedJoinInProgressMember(RakNet::BitStream *bsIn, VerifiedJoinInProgressMember *vjipm); + unsigned int GetVerifiedJoinInProgressMemberIndex(const AddressOrGUID systemIdentifier, VerifiedJoinInProgress *vjip); + void DecomposeJoinCapable(Packet *packet, VerifiedJoinInProgress *vjip); + void WriteVerifiedJoinCapable(RakNet::BitStream *bsOut, VerifiedJoinInProgress *vjip); + void CategorizeVJIP(VerifiedJoinInProgress *vjip, + DataStructures::List &participatingMembersOnClientSucceeded, + DataStructures::List &participatingMembersOnClientFailed, + DataStructures::List &participatingMembersNotOnClient, + DataStructures::List &clientMembersNotParticipatingSucceeded, + DataStructures::List &clientMembersNotParticipatingFailed); + + // Used to track how long RakNet has been running. This is so we know who has been running longest + RakNet::TimeUS startupTime; + + // Option for SetAutoparticipateConnections + bool autoParticipateConnections; + + // totalConnectionCount is roughly maintained across all systems, and increments by 1 each time a new system connects to the mesh + // It is always kept at the highest known value + // It is used as the high 4 bytes for new FCMGuids. This causes newer values of FCM2Guid to be higher than lower values. The lowest value is the host. + unsigned int totalConnectionCount; + + // Our own ourFCMGuid. Starts at unassigned (0). Assigned once we send ID_FCM2_REQUEST_FCMGUID and get back ID_FCM2_RESPOND_CONNECTION_COUNT + FCM2Guid ourFCMGuid; + + /// List of systems we know the FCM2Guid for + DataStructures::List fcm2ParticipantList; + + RakNetGUID lastPushedHost; + + // Optimization: Store last calculated host in these variables. + RakNetGUID hostRakNetGuid; + FCM2Guid hostFCM2Guid; + + RakNet::RakString connectionPassword; + bool connectOnNewRemoteConnections; + + DataStructures::List joinsInProgress; + BitStream myContext; +}; + +} // namespace RakNet + +/* +Startup() +ourFCMGuid=unknown +totalConnectionCount=0 +Set startupTime + +AddParticipant() +if (sender by guid is a participant) +return; +AddParticipantInternal(guid); +if (ourFCMGuid==unknown) +Send to that system a request for their fcmGuid, totalConnectionCount. Inform startupTime. +else +Send to that system a request for their fcmGuid. Inform total connection count, our fcmGuid + +OnRequestGuid() +if (sender by guid is not a participant) +{ + // They added us as a participant, but we didn't add them. This can be caused by lag where both participants are not added at the same time. + // It doesn't affect the outcome as long as we still process the data + AddParticipantInternal(guid); +} +if (ourFCMGuid==unknown) +{ + if (includedStartupTime) + { + // Nobody has a fcmGuid + + if (their startup time is greater than our startup time) + ReplyConnectionCount(1); + else + ReplyConnectionCount(2); + } + else + { + // They have a fcmGuid, we do not + + SetMaxTotalConnectionCount(remoteCount); + AssignTheirGuid() + GenerateOurGuid(); + SendOurGuid(all); + } +} +else +{ + if (includedStartupTime) + { + // We have a fcmGuid they do not + + ReplyConnectionCount(totalConnectionCount+1); + SendOurGuid(sender); + } + else + { + // We both have fcmGuids + + SetMaxTotalConnectionCount(remoteCount); + AssignTheirGuid(); + SendOurGuid(sender); + } +} + +OnReplyConnectionCount() +SetMaxTotalConnectionCount(remoteCount); +GenerateOurGuid(); +SendOurGuid(allParticipants); + +OnReceiveTheirGuid() +AssignTheirGuid() +*/ + +#endif + diff --git a/Source/GetTime.h b/Source/GetTime.h index 8dee237f8..af5e9d10c 100644 --- a/Source/GetTime.h +++ b/Source/GetTime.h @@ -1,42 +1,40 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file GetTime.h -/// \brief Returns the value from QueryPerformanceCounter. This is the function RakNet uses to represent time. This time won't match the time returned by GetTimeCount(). See http://www.jenkinssoftware.com/forum/index.php?topic=2798.0 -/// - - -#ifndef __GET_TIME_H -#define __GET_TIME_H - -#include "Export.h" -#include "RakNetTime.h" // For RakNet::TimeMS - -namespace RakNet -{ - /// Same as GetTimeMS - /// Holds the time in either a 32 or 64 bit variable, depending on __GET_TIME_64BIT - RakNet::Time RAK_DLL_EXPORT GetTime( void ); - - /// Return the time as 32 bit - /// \note The maximum delta between returned calls is 1 second - however, RakNet calls this constantly anyway. See NormalizeTime() in the cpp. - RakNet::TimeMS RAK_DLL_EXPORT GetTimeMS( void ); - - /// Return the time as 64 bit - /// \note The maximum delta between returned calls is 1 second - however, RakNet calls this constantly anyway. See NormalizeTime() in the cpp. - RakNet::TimeUS RAK_DLL_EXPORT GetTimeUS( void ); - - /// a > b? - extern RAK_DLL_EXPORT bool GreaterThan(RakNet::Time a, RakNet::Time b); - /// a < b? - extern RAK_DLL_EXPORT bool LessThan(RakNet::Time a, RakNet::Time b); -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file GetTime.h +/// \brief Returns the value from QueryPerformanceCounter. This is the function RakNet uses to represent time. This time won't match the time returned by GetTimeCount(). See http://www.jenkinssoftware.com/forum/index.php?topic=2798.0 +/// + + +#pragma once + +#include "Export.h" +#include "RakNetTime.h" // For RakNet::TimeMS + +namespace RakNet +{ + /// Same as GetTimeMS + /// Holds the time in either a 32 or 64 bit variable, depending on __GET_TIME_64BIT + RakNet::Time RAK_DLL_EXPORT GetTime( void ); + + /// Return the time as 32 bit + /// \note The maximum delta between returned calls is 1 second - however, RakNet calls this constantly anyway. See NormalizeTime() in the cpp. + RakNet::TimeMS RAK_DLL_EXPORT GetTimeMS( void ); + + /// Return the time as 64 bit + /// \note The maximum delta between returned calls is 1 second - however, RakNet calls this constantly anyway. See NormalizeTime() in the cpp. + RakNet::TimeUS RAK_DLL_EXPORT GetTimeUS( void ); + + /// a > b? + extern RAK_DLL_EXPORT bool GreaterThan(RakNet::Time a, RakNet::Time b); + /// a < b? + extern RAK_DLL_EXPORT bool LessThan(RakNet::Time a, RakNet::Time b); +} + diff --git a/Source/Getche.h b/Source/Getche.h index 855e79898..f8eeff694 100644 --- a/Source/Getche.h +++ b/Source/Getche.h @@ -1,19 +1,22 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#if defined(_WIN32) -#include /* getche() */ - -#else -#include -#include -#include -char getche(); -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + + +#if defined(_WIN32) +#include /* getche() */ + +#else +#include +#include +#include +char getche(); +#endif diff --git a/Source/Gets.h b/Source/Gets.h index 059523a5d..7572aa712 100644 --- a/Source/Gets.h +++ b/Source/Gets.h @@ -1,24 +1,22 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __GETS__H_ -#define __GETS__H_ - -#ifdef __cplusplus -extern "C" { -#endif - -char * Gets ( char * str, int num ); - -#ifdef __cplusplus -} -#endif - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +char * Gets ( char * str, int num ); + +#ifdef __cplusplus +} +#endif + diff --git a/Source/GridSectorizer.h b/Source/GridSectorizer.h index 4cb250871..1cd8aab86 100644 --- a/Source/GridSectorizer.h +++ b/Source/GridSectorizer.h @@ -1,78 +1,76 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef _GRID_SECTORIZER_H -#define _GRID_SECTORIZER_H - -//#define _USE_ORDERED_LIST - -#include "RakMemoryOverride.h" - -#ifdef _USE_ORDERED_LIST -#include "DS_OrderedList.h" -#else -#include "DS_List.h" -#endif - -class GridSectorizer -{ -public: - GridSectorizer(); - ~GridSectorizer(); - - // _cellWidth, _cellHeight is the width and height of each cell in world units - // minX, minY, maxX, maxY are the world dimensions (can be changed to dynamically allocate later if needed) - void Init(const float _maxCellWidth, const float _maxCellHeight, const float minX, const float minY, const float maxX, const float maxY); - - // Adds a pointer to the grid with bounding rectangle dimensions - void AddEntry(void *entry, const float minX, const float minY, const float maxX, const float maxY); - -#ifdef _USE_ORDERED_LIST - - // Removes a pointer, as above - void RemoveEntry(void *entry, const float minX, const float minY, const float maxX, const float maxY); - - // Adds and removes in one pass, more efficient than calling both functions consecutively - void MoveEntry(void *entry, const float sourceMinX, const float sourceMinY, const float sourceMaxX, const float sourceMaxY, - const float destMinX, const float destMinY, const float destMaxX, const float destMaxY); - -#endif - - // Adds to intersectionList all entries in a certain radius - void GetEntries(DataStructures::List& intersectionList, const float minX, const float minY, const float maxX, const float maxY); - - void Clear(void); - -protected: - int WorldToCellX(const float input) const; - int WorldToCellY(const float input) const; - int WorldToCellXOffsetAndClamped(const float input) const; - int WorldToCellYOffsetAndClamped(const float input) const; - - // Returns true or false if a position crosses cells in the grid. If false, you don't need to move entries - bool PositionCrossesCells(const float originX, const float originY, const float destinationX, const float destinationY) const; - - float cellOriginX, cellOriginY; - float cellWidth, cellHeight; - float invCellWidth, invCellHeight; - float gridWidth, gridHeight; - int gridCellWidthCount, gridCellHeightCount; - - - // int gridWidth, gridHeight; - -#ifdef _USE_ORDERED_LIST - DataStructures::OrderedList* grid; -#else - DataStructures::List* grid; -#endif -}; - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +//#define _USE_ORDERED_LIST + +#include "RakMemoryOverride.h" + +#ifdef _USE_ORDERED_LIST +#include "DS_OrderedList.h" +#else +#include "DS_List.h" +#endif + +class GridSectorizer +{ +public: + GridSectorizer(); + ~GridSectorizer(); + + // _cellWidth, _cellHeight is the width and height of each cell in world units + // minX, minY, maxX, maxY are the world dimensions (can be changed to dynamically allocate later if needed) + void Init(const float _maxCellWidth, const float _maxCellHeight, const float minX, const float minY, const float maxX, const float maxY); + + // Adds a pointer to the grid with bounding rectangle dimensions + void AddEntry(void *entry, const float minX, const float minY, const float maxX, const float maxY); + +#ifdef _USE_ORDERED_LIST + + // Removes a pointer, as above + void RemoveEntry(void *entry, const float minX, const float minY, const float maxX, const float maxY); + + // Adds and removes in one pass, more efficient than calling both functions consecutively + void MoveEntry(void *entry, const float sourceMinX, const float sourceMinY, const float sourceMaxX, const float sourceMaxY, + const float destMinX, const float destMinY, const float destMaxX, const float destMaxY); + +#endif + + // Adds to intersectionList all entries in a certain radius + void GetEntries(DataStructures::List& intersectionList, const float minX, const float minY, const float maxX, const float maxY); + + void Clear(void); + +protected: + int WorldToCellX(const float input) const; + int WorldToCellY(const float input) const; + int WorldToCellXOffsetAndClamped(const float input) const; + int WorldToCellYOffsetAndClamped(const float input) const; + + // Returns true or false if a position crosses cells in the grid. If false, you don't need to move entries + bool PositionCrossesCells(const float originX, const float originY, const float destinationX, const float destinationY) const; + + float cellOriginX, cellOriginY; + float cellWidth, cellHeight; + float invCellWidth, invCellHeight; + float gridWidth, gridHeight; + int gridCellWidthCount, gridCellHeightCount; + + + // int gridWidth, gridHeight; + +#ifdef _USE_ORDERED_LIST + DataStructures::OrderedList* grid; +#else + DataStructures::List* grid; +#endif +}; + diff --git a/Source/HTTPConnection.h b/Source/HTTPConnection.h index 198700d1d..5b9e3e1d3 100644 --- a/Source/HTTPConnection.h +++ b/Source/HTTPConnection.h @@ -1,175 +1,173 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file HTTPConnection.h -/// \brief Contains HTTPConnection, used to communicate with web servers -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_HTTPConnection==1 && _RAKNET_SUPPORT_TCPInterface==1 - -#ifndef __HTTP_CONNECTION -#define __HTTP_CONNECTION - -#include "Export.h" -#include "RakString.h" -#include "RakMemoryOverride.h" -#include "RakNetTypes.h" -#include "DS_Queue.h" - -namespace RakNet -{ -/// Forward declarations -class TCPInterface; -struct SystemAddress; - -/// \brief Use HTTPConnection to communicate with a web server. -/// \details Start an instance of TCPInterface via the Start() command. -/// Instantiate a new instance of HTTPConnection, and associate TCPInterface with the class in the constructor. -/// Use Post() to send commands to the web server, and ProcessDataPacket() to update the connection with packets returned from TCPInterface that have the system address of the web server -/// This class will handle connecting and reconnecting as necessary. -/// -/// Note that only one Post() can be handled at a time. -/// \deprecated, use HTTPConnection2 -class RAK_DLL_EXPORT HTTPConnection -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(HTTPConnection) - - /// Returns a HTTP object associated with this tcp connection - HTTPConnection(); - virtual ~HTTPConnection(); - - /// \pre tcp should already be started - void Init(TCPInterface *_tcp, const char *host, unsigned short port=80); - - /// Submit data to the HTTP server - /// HTTP only allows one request at a time per connection - /// - /// \pre IsBusy()==false - /// \param path the path on the remote server you want to POST to. For example "index.html" - /// \param data A NULL terminated string to submit to the server - /// \param contentType "Content-Type:" passed to post. - void Post(const char *path, const char *data, const char *_contentType="application/x-www-form-urlencoded"); - - /// Get a file from a webserver - /// \param path the path on the remote server you want to GET from. For example "index.html" - void Get(const char *path); - - /// Is there a Read result ready? - bool HasRead(void) const; - - /// Get one result from the server - /// \pre HasResult must return true - RakNet::RakString Read(void); - - /// Call periodically to do time-based updates - void Update(void); - - /// Returns the address of the server we are connected to - SystemAddress GetServerAddress(void) const; - - /// Process an HTTP data packet returned from TCPInterface - /// Returns true when we have gotten all the data from the HTTP server. - /// If this returns true then it's safe to Post() another request - /// Deallocate the packet as usual via TCPInterface - /// \param packet NULL or a packet associated with our host and port - void ProcessTCPPacket(Packet *packet); - - /// Results of HTTP requests. Standard response codes are < 999 - /// ( define HTTP codes and our internal codes as needed ) - enum ResponseCodes { NoBody=1001, OK=200, Deleted=1002 }; - - HTTPConnection& operator=(const HTTPConnection& rhs){(void) rhs; return *this;} - - /// Encapsulates a raw HTTP response and response code - struct BadResponse - { - public: - BadResponse() {code=0;} - - BadResponse(const unsigned char *_data, int _code) - : data((const char *)_data), code(_code) {} - - BadResponse(const char *_data, int _code) - : data(_data), code(_code) {} - - operator int () const { return code; } - - RakNet::RakString data; - int code; // ResponseCodes - }; - - /// Queued events of failed exchanges with the HTTP server - bool HasBadResponse(int *code, RakNet::RakString *data); - - /// Returns false if the connection is not doing anything else - bool IsBusy(void) const; - - /// \internal - int GetState(void) const; - - struct OutgoingCommand - { - RakNet::RakString remotePath; - RakNet::RakString data; - RakNet::RakString contentType; - bool isPost; - }; - - DataStructures::Queue outgoingCommand; - OutgoingCommand currentProcessingCommand; - -private: - SystemAddress server; - TCPInterface *tcp; - RakNet::RakString host; - unsigned short port; - DataStructures::Queue badResponses; - - enum ConnectionState - { - CS_NONE, - CS_DISCONNECTING, - CS_CONNECTING, - CS_CONNECTED, - CS_PROCESSING, - } connectionState; - - RakNet::RakString incomingData; - DataStructures::Queue results; - - void CloseConnection(); - - /* - enum { RAK_HTTP_INITIAL, - RAK_HTTP_STARTING, - RAK_HTTP_CONNECTING, - RAK_HTTP_ESTABLISHED, - RAK_HTTP_REQUEST_SENT, - RAK_HTTP_IDLE } state; - - RakNet::RakString outgoing, incoming, path, contentType; - void Process(Packet *packet); // the workhorse - - // this helps check the various status lists in TCPInterface - typedef SystemAddress (TCPInterface::*StatusCheckFunction)(void); - bool InList(StatusCheckFunction func); - */ - -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file HTTPConnection.h +/// \brief Contains HTTPConnection, used to communicate with web servers +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_HTTPConnection==1 && _RAKNET_SUPPORT_TCPInterface==1 + +#pragma once + +#include "Export.h" +#include "RakString.h" +#include "RakMemoryOverride.h" +#include "RakNetTypes.h" +#include "DS_Queue.h" + +namespace RakNet +{ +/// Forward declarations +class TCPInterface; +struct SystemAddress; + +/// \brief Use HTTPConnection to communicate with a web server. +/// \details Start an instance of TCPInterface via the Start() command. +/// Instantiate a new instance of HTTPConnection, and associate TCPInterface with the class in the constructor. +/// Use Post() to send commands to the web server, and ProcessDataPacket() to update the connection with packets returned from TCPInterface that have the system address of the web server +/// This class will handle connecting and reconnecting as necessary. +/// +/// Note that only one Post() can be handled at a time. +/// \deprecated, use HTTPConnection2 +class RAK_DLL_EXPORT HTTPConnection +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(HTTPConnection) + + /// Returns a HTTP object associated with this tcp connection + HTTPConnection(); + virtual ~HTTPConnection(); + + /// \pre tcp should already be started + void Init(TCPInterface *_tcp, const char *host, unsigned short port=80); + + /// Submit data to the HTTP server + /// HTTP only allows one request at a time per connection + /// + /// \pre IsBusy()==false + /// \param path the path on the remote server you want to POST to. For example "index.html" + /// \param data A nullptr terminated string to submit to the server + /// \param contentType "Content-Type:" passed to post. + void Post(const char *path, const char *data, const char *_contentType="application/x-www-form-urlencoded"); + + /// Get a file from a webserver + /// \param path the path on the remote server you want to GET from. For example "index.html" + void Get(const char *path); + + /// Is there a Read result ready? + bool HasRead(void) const; + + /// Get one result from the server + /// \pre HasResult must return true + RakNet::RakString Read(void); + + /// Call periodically to do time-based updates + void Update(void); + + /// Returns the address of the server we are connected to + SystemAddress GetServerAddress(void) const; + + /// Process an HTTP data packet returned from TCPInterface + /// Returns true when we have gotten all the data from the HTTP server. + /// If this returns true then it's safe to Post() another request + /// Deallocate the packet as usual via TCPInterface + /// \param packet nullptr or a packet associated with our host and port + void ProcessTCPPacket(Packet *packet); + + /// Results of HTTP requests. Standard response codes are < 999 + /// ( define HTTP codes and our internal codes as needed ) + enum ResponseCodes { NoBody=1001, OK=200, Deleted=1002 }; + + HTTPConnection& operator=(const HTTPConnection& rhs){(void) rhs; return *this;} + + /// Encapsulates a raw HTTP response and response code + struct BadResponse + { + public: + BadResponse() {code=0;} + + BadResponse(const unsigned char *_data, int _code) + : data((const char *)_data), code(_code) {} + + BadResponse(const char *_data, int _code) + : data(_data), code(_code) {} + + operator int () const { return code; } + + RakNet::RakString data; + int code; // ResponseCodes + }; + + /// Queued events of failed exchanges with the HTTP server + bool HasBadResponse(int *code, RakNet::RakString *data); + + /// Returns false if the connection is not doing anything else + bool IsBusy(void) const; + + /// \internal + int GetState(void) const; + + struct OutgoingCommand + { + RakNet::RakString remotePath; + RakNet::RakString data; + RakNet::RakString contentType; + bool isPost; + }; + + DataStructures::Queue outgoingCommand; + OutgoingCommand currentProcessingCommand; + +private: + SystemAddress server; + TCPInterface *tcp; + RakNet::RakString host; + unsigned short port; + DataStructures::Queue badResponses; + + enum ConnectionState + { + CS_NONE, + CS_DISCONNECTING, + CS_CONNECTING, + CS_CONNECTED, + CS_PROCESSING, + } connectionState; + + RakNet::RakString incomingData; + DataStructures::Queue results; + + void CloseConnection(); + + /* + enum { RAK_HTTP_INITIAL, + RAK_HTTP_STARTING, + RAK_HTTP_CONNECTING, + RAK_HTTP_ESTABLISHED, + RAK_HTTP_REQUEST_SENT, + RAK_HTTP_IDLE } state; + + RakNet::RakString outgoing, incoming, path, contentType; + void Process(Packet *packet); // the workhorse + + // this helps check the various status lists in TCPInterface + typedef SystemAddress (TCPInterface::*StatusCheckFunction)(void); + bool InList(StatusCheckFunction func); + */ + +}; + +} // namespace RakNet + +#endif + diff --git a/Source/HTTPConnection2.cpp b/Source/HTTPConnection2.cpp index 5e96df0c1..05a55ad4b 100644 --- a/Source/HTTPConnection2.cpp +++ b/Source/HTTPConnection2.cpp @@ -1,621 +1,621 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_HTTPConnection2==1 && _RAKNET_SUPPORT_TCPInterface==1 - -#include "HTTPConnection2.h" -#include "TCPInterface.h" - -using namespace RakNet; - -STATIC_FACTORY_DEFINITIONS(HTTPConnection2,HTTPConnection2); - -HTTPConnection2::HTTPConnection2() -{ -} -HTTPConnection2::~HTTPConnection2() -{ - -} -bool HTTPConnection2::TransmitRequest(const char* stringToTransmit, const char* host, unsigned short port, bool useSSL, int ipVersion, SystemAddress useAddress, void *userData) -{ - Request *request = RakNet::OP_NEW(_FILE_AND_LINE_); - request->host=host; - request->chunked = false; - if (useAddress!=UNASSIGNED_SYSTEM_ADDRESS) - { - request->hostEstimatedAddress=useAddress; - if (IsConnected(useAddress)==false) - { - RakNet::OP_DELETE(request, _FILE_AND_LINE_); - return false; - } - } - else - { - if (request->hostEstimatedAddress.FromString(host, '|', ipVersion)==false) - { - RakNet::OP_DELETE(request, _FILE_AND_LINE_); - return false; - } - } - request->hostEstimatedAddress.SetPortHostOrder(port); - request->port=port; - request->stringToTransmit=stringToTransmit; - request->contentLength=-1; - request->contentOffset=0; - request->useSSL=useSSL; - request->ipVersion=ipVersion; - request->userData=userData; - - if (IsConnected(request->hostEstimatedAddress)) - { - sentRequestsMutex.Lock(); - if (sentRequests.Size()==0) - { - request->hostCompletedAddress=request->hostEstimatedAddress; - sentRequests.Push(request, _FILE_AND_LINE_); - sentRequestsMutex.Unlock(); - - SendRequest(request); - } - else - { - // Request pending, push it - pendingRequestsMutex.Lock(); - pendingRequests.Push(request, _FILE_AND_LINE_); - pendingRequestsMutex.Unlock(); - - sentRequestsMutex.Unlock(); - } - } - else - { - pendingRequestsMutex.Lock(); - pendingRequests.Push(request, _FILE_AND_LINE_); - pendingRequestsMutex.Unlock(); - - if (ipVersion!=6) - { - tcpInterface->Connect(host, port, false, AF_INET); - } - else - { - #if RAKNET_SUPPORT_IPV6 - tcpInterface->Connect(host, port, false, AF_INET6); - #else - RakAssert("HTTPConnection2::TransmitRequest needs define RAKNET_SUPPORT_IPV6" && 0); - #endif - } - } - return true; -} -bool HTTPConnection2::GetResponse( RakString &stringTransmitted, RakString &hostTransmitted, RakString &responseReceived, SystemAddress &hostReceived, int &contentOffset ) -{ - void *userData; - return GetResponse(stringTransmitted, hostTransmitted, responseReceived, hostReceived, contentOffset, &userData); - -} -bool HTTPConnection2::GetResponse( RakString &stringTransmitted, RakString &hostTransmitted, RakString &responseReceived, SystemAddress &hostReceived, int &contentOffset, void **userData ) -{ - completedRequestsMutex.Lock(); - if (completedRequests.Size()>0) - { - Request *completedRequest = completedRequests[0]; - completedRequests.RemoveAtIndexFast(0); - completedRequestsMutex.Unlock(); - - responseReceived = completedRequest->stringReceived; - hostReceived = completedRequest->hostCompletedAddress; - stringTransmitted = completedRequest->stringToTransmit; - hostTransmitted = completedRequest->host; - contentOffset = completedRequest->contentOffset; - *userData = completedRequest->userData; - - RakNet::OP_DELETE(completedRequest, _FILE_AND_LINE_); - return true; - } - else - { - completedRequestsMutex.Unlock(); - } - return false; -} -bool HTTPConnection2::IsBusy(void) const -{ - return pendingRequests.Size()>0 || sentRequests.Size()>0; -} -bool HTTPConnection2::HasResponse(void) const -{ - return completedRequests.Size()>0; -} -int ReadChunkSize( char *txtStart, char **txtEnd ) -{ -// char lengthStr[32]; -// memset(lengthStr, 0, 32); -// memcpy(lengthStr, txtStart, txtEnd - txtStart); - return strtoul(txtStart, txtEnd,16); - // return atoi(lengthStr); -} -void ReadChunkBlock( size_t ¤tChunkSize, size_t &bytesReadSoFar, char *txtIn, RakString &txtOut) -{ - size_t bytesToRead; - size_t sLen; - - do - { - bytesToRead = currentChunkSize - bytesReadSoFar; - sLen = strlen(txtIn); - if (sLen < bytesToRead) - bytesToRead = sLen; - txtOut.AppendBytes(txtIn, bytesToRead); - txtIn += bytesToRead; - bytesReadSoFar += bytesToRead; - if (*txtIn == 0) - { - // currentChunkSize=0; - return; - } - // char *newLine = strstr(txtIn, "\r\n"); - if (txtIn[0] && txtIn[0]=='\r' && txtIn[1] && txtIn[1]=='\n' ) - txtIn += 2; // Newline - char *newLine; - currentChunkSize = ReadChunkSize(txtIn, &newLine); - RakAssert(currentChunkSize < 50000); // Sanity check - if (currentChunkSize == 0) - return; - if (newLine == 0) - return; - bytesReadSoFar=0; - txtIn = newLine + 2; - } while (txtIn); -} -PluginReceiveResult HTTPConnection2::OnReceive(Packet *packet) -{ - unsigned int i; - - bool locked=true; - sentRequestsMutex.Lock(); - for (i=0; i < sentRequests.Size(); i++) - { - Request *sentRequest = sentRequests[i]; - if (sentRequest->hostCompletedAddress==packet->systemAddress) - { - sentRequests.RemoveAtIndexFast(i); - locked=false; - sentRequestsMutex.Unlock(); - - /* - static FILE * pFile = 0; - if (pFile==0) - { - long lSize; - char * buffer; - size_t result; - - pFile = fopen ( "string_received.txt" , "rb" ); - if (pFile==NULL) {fputs ("File error",stderr); exit (1);} - - // obtain file size: - fseek (pFile , 0 , SEEK_END); - lSize = ftell (pFile); - rewind (pFile); - - // allocate memory to contain the whole file: - buffer = (char*) malloc (sizeof(char)*lSize); - if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);} - - // copy the file into the buffer: - result = fread (buffer,1,lSize,pFile); - if (result != lSize) {fputs ("Reading error",stderr); exit (3);} - - packet->data=(unsigned char*) buffer; - packet->length=lSize; - } - */ - - - const char *isFirstChunk = strstr((char*) packet->data, "Transfer-Encoding: chunked"); - if (isFirstChunk) - { - //printf((char*) packet->data); - - locked=false; - sentRequestsMutex.Unlock(); - - sentRequest->chunked = true; - char *chunkStrStart = strstr((char*) packet->data, "\r\n\r\n"); - RakAssert(chunkStrStart); - - chunkStrStart += 4; // strlen("\r\n\r\n"); - char *body_header; // = strstr(chunkStrStart, "\r\n"); - sentRequest->thisChunkSize = ReadChunkSize(chunkStrStart, &body_header); - sentRequest->bytesReadForThisChunk = 0; - sentRequest->contentOffset = 0; - - if (sentRequest->thisChunkSize == 0) - { - // Done - completedRequestsMutex.Lock(); - completedRequests.Push(sentRequest, _FILE_AND_LINE_); - completedRequestsMutex.Unlock(); - - // If there is another command waiting for this server, send it - SendPendingRequestToConnectedSystem(packet->systemAddress); - } - else - { - - // char *offset = strstr((char*) packet->data+1, "2000"); - - body_header+=2; - ReadChunkBlock(sentRequest->thisChunkSize, sentRequest->bytesReadForThisChunk, body_header, sentRequest->stringReceived); - - if (sentRequest->thisChunkSize==0) - { - // Done - completedRequestsMutex.Lock(); - completedRequests.Push(sentRequest, _FILE_AND_LINE_); - completedRequestsMutex.Unlock(); - - // If there is another command waiting for this server, send it - SendPendingRequestToConnectedSystem(packet->systemAddress); - } - else - { - // Not done - sentRequestsMutex.Lock(); - sentRequests.Push(sentRequest, _FILE_AND_LINE_); - sentRequestsMutex.Unlock(); - } - } - } - else if (sentRequest->chunked) - { - ReadChunkBlock(sentRequest->thisChunkSize, sentRequest->bytesReadForThisChunk, (char*) packet->data, sentRequest->stringReceived); - - if (sentRequest->thisChunkSize==0) - { - // Done - completedRequestsMutex.Lock(); - completedRequests.Push(sentRequest, _FILE_AND_LINE_); - completedRequestsMutex.Unlock(); - - // If there is another command waiting for this server, send it - SendPendingRequestToConnectedSystem(packet->systemAddress); - } - else - { - // Not done - sentRequestsMutex.Lock(); - sentRequests.Push(sentRequest, _FILE_AND_LINE_); - sentRequestsMutex.Unlock(); - } - - } - else - { - sentRequest->stringReceived+=packet->data; - - if (sentRequest->contentLength==-1) - { - const char *length_header = strstr(sentRequest->stringReceived.C_String(), "Content-Length: "); - if(length_header) - { - length_header += 16; // strlen("Content-Length: "); - - unsigned int clLength; - for (clLength=0; length_header[clLength] && length_header[clLength] >= '0' && length_header[clLength] <= '9'; clLength++) - ; - if (clLength>0 && (length_header[clLength]=='\r' || length_header[clLength]=='\n')) - { - sentRequest->contentLength = RakString::ReadIntFromSubstring(length_header, 0, clLength); - } - } - } - - // If we know the content length, find \r\n\r\n - if (sentRequest->contentLength != -1) - { - if (sentRequest->contentLength > 0) - { - const char *body_header = strstr(sentRequest->stringReceived.C_String(), "\r\n\r\n"); - if (body_header) - { - body_header += 4; // strlen("\r\n\r\n"); - size_t slen = strlen(body_header); - //RakAssert(slen <= (size_t) sentRequest->contentLength); - if (slen >= (size_t) sentRequest->contentLength) - { - sentRequest->contentOffset = body_header - sentRequest->stringReceived.C_String(); - completedRequestsMutex.Lock(); - completedRequests.Push(sentRequest, _FILE_AND_LINE_); - completedRequestsMutex.Unlock(); - - // If there is another command waiting for this server, send it - SendPendingRequestToConnectedSystem(packet->systemAddress); - } - else - { - sentRequestsMutex.Lock(); - sentRequests.Push(sentRequest, _FILE_AND_LINE_); - sentRequestsMutex.Unlock(); - } - } - - else - { - sentRequestsMutex.Lock(); - sentRequests.Push(sentRequest, _FILE_AND_LINE_); - sentRequestsMutex.Unlock(); - } - } - else - { - sentRequest->contentOffset=-1; - completedRequestsMutex.Lock(); - completedRequests.Push(sentRequest, _FILE_AND_LINE_); - completedRequestsMutex.Unlock(); - - // If there is another command waiting for this server, send it - SendPendingRequestToConnectedSystem(packet->systemAddress); - } - } - else - { - const char *firstNewlineSet = strstr(sentRequest->stringReceived.C_String(), "\r\n\r\n"); - if (firstNewlineSet!=0) - { - int offset = firstNewlineSet - sentRequest->stringReceived.C_String(); - if (sentRequest->stringReceived.C_String()[offset+4]==0) - sentRequest->contentOffset=-1; - else - sentRequest->contentOffset=offset+4; - completedRequestsMutex.Lock(); - completedRequests.Push(sentRequest, _FILE_AND_LINE_); - completedRequestsMutex.Unlock(); - - // If there is another command waiting for this server, send it - SendPendingRequestToConnectedSystem(packet->systemAddress); - } - else - { - sentRequestsMutex.Lock(); - sentRequests.Push(sentRequest, _FILE_AND_LINE_); - sentRequestsMutex.Unlock(); - } - } - } - - - break; - } - } - - if (locked==true) - sentRequestsMutex.Unlock(); - - return RR_CONTINUE_PROCESSING; -} - -void HTTPConnection2::OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming) -{ - (void) rakNetGUID; - (void) isIncoming; // unknown - - SendPendingRequestToConnectedSystem(systemAddress); -} -void HTTPConnection2::SendPendingRequestToConnectedSystem(SystemAddress sa) -{ - if (sa==UNASSIGNED_SYSTEM_ADDRESS) - return; - - unsigned int requestsSent=0; - - // Search through requests to find a match for this instance of TCPInterface and SystemAddress - unsigned int i; - i=0; - pendingRequestsMutex.Lock(); - while (i < pendingRequests.Size()) - { - Request *request = pendingRequests[i]; - if (request->hostEstimatedAddress==sa) - { - pendingRequests.RemoveAtIndex(i); - // Send this request - request->hostCompletedAddress=sa; - - sentRequestsMutex.Lock(); - sentRequests.Push(request, _FILE_AND_LINE_); - sentRequestsMutex.Unlock(); - - pendingRequestsMutex.Unlock(); - -#if OPEN_SSL_CLIENT_SUPPORT==1 - if (request->useSSL) - tcpInterface->StartSSLClient(sa); -#endif - - SendRequest(request); - requestsSent++; - pendingRequestsMutex.Lock(); - break; - } - else - { - i++; - } - } - pendingRequestsMutex.Unlock(); - - if (requestsSent==0) - { - pendingRequestsMutex.Lock(); - if (pendingRequests.Size() > 0) - { - // Just assign - Request *request = pendingRequests[0]; - pendingRequests.RemoveAtIndex(0); - - request->hostCompletedAddress=sa; - - sentRequestsMutex.Lock(); - sentRequests.Push(request, _FILE_AND_LINE_); - sentRequestsMutex.Unlock(); - pendingRequestsMutex.Unlock(); - - // Send -#if OPEN_SSL_CLIENT_SUPPORT==1 - if (request->useSSL) - tcpInterface->StartSSLClient(sa); -#endif - - - SendRequest(request); - } - else - { - pendingRequestsMutex.Unlock(); - } - } -} -void HTTPConnection2::RemovePendingRequest(SystemAddress sa) -{ - unsigned int i; - i=0; - pendingRequestsMutex.Lock(); - for (i=0; i < pendingRequests.Size(); i++) - { - Request *request = pendingRequests[i]; - if (request->hostEstimatedAddress==sa) - { - pendingRequests.RemoveAtIndex(i); - RakNet::OP_DELETE(request, _FILE_AND_LINE_); - } - else - i++; - } - - pendingRequestsMutex.Unlock(); -} -void HTTPConnection2::SendNextPendingRequest(void) -{ - // Send a pending request - pendingRequestsMutex.Lock(); - if (pendingRequests.Size()>0) - { - Request *pendingRequest = pendingRequests.Peek(); - pendingRequestsMutex.Unlock(); - - if (pendingRequest->ipVersion!=6) - { - tcpInterface->Connect(pendingRequest->host.C_String(), pendingRequest->port, false, AF_INET); - } - else - { -#if RAKNET_SUPPORT_IPV6 - tcpInterface->Connect(pendingRequest->host.C_String(), pendingRequest->port, false, AF_INET6); -#else - RakAssert("HTTPConnection2::TransmitRequest needs define RAKNET_SUPPORT_IPV6" && 0); -#endif - } - } - else - { - pendingRequestsMutex.Unlock(); - } -} - -void HTTPConnection2::OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason) -{ - (void) failedConnectionAttemptReason; - if (packet->systemAddress==UNASSIGNED_SYSTEM_ADDRESS) - return; - - RemovePendingRequest(packet->systemAddress); - - SendNextPendingRequest(); -} -void HTTPConnection2::OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ) -{ - (void) lostConnectionReason; - (void) rakNetGUID; - - if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS) - return; - - // Update sent requests to completed requests - unsigned int i; - i=0; - sentRequestsMutex.Lock(); - while (i < sentRequests.Size()) - { - if (sentRequests[i]->hostCompletedAddress==systemAddress) - { - Request *sentRequest = sentRequests[i]; - if (sentRequest->chunked==false && sentRequest->stringReceived.IsEmpty()==false) - { - if (strstr(sentRequest->stringReceived.C_String(), "Content-Length: ")) - { - char *body_header = strstr((char*) sentRequest->stringReceived.C_String(), "\r\n\r\n"); - if (body_header) - { - body_header += 4; // strlen("\r\n\r\n"); - sentRequest->contentOffset = body_header - sentRequest->stringReceived.C_String(); - } - else - { - sentRequest->contentOffset = 0; - } - - } - else - { - sentRequest->contentOffset = 0; - } - } - - - completedRequestsMutex.Lock(); - completedRequests.Push(sentRequests[i], _FILE_AND_LINE_); - completedRequestsMutex.Unlock(); - - sentRequests.RemoveAtIndexFast(i); - } - else - { - i++; - } - } - sentRequestsMutex.Unlock(); - - SendNextPendingRequest(); -} -bool HTTPConnection2::IsConnected(SystemAddress sa) -{ - SystemAddress remoteSystems[64]; - unsigned short numberOfSystems=64; - tcpInterface->GetConnectionList(remoteSystems, &numberOfSystems); - for (unsigned int i=0; i < numberOfSystems; i++) - { - if (remoteSystems[i]==sa) - { - return true; - } - } - return false; -} -void HTTPConnection2::SendRequest(Request *request) -{ - tcpInterface->Send(request->stringToTransmit.C_String(), (unsigned int) request->stringToTransmit.GetLength(), request->hostCompletedAddress, false); -} - -#endif // #if _RAKNET_SUPPORT_HTTPConnection2==1 && _RAKNET_SUPPORT_TCPInterface==1 +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_HTTPConnection2==1 && _RAKNET_SUPPORT_TCPInterface==1 + +#include "HTTPConnection2.h" +#include "TCPInterface.h" + +using namespace RakNet; + +STATIC_FACTORY_DEFINITIONS(HTTPConnection2,HTTPConnection2); + +HTTPConnection2::HTTPConnection2() +{ +} +HTTPConnection2::~HTTPConnection2() +{ + +} +bool HTTPConnection2::TransmitRequest(const char* stringToTransmit, const char* host, unsigned short port, bool useSSL, int ipVersion, SystemAddress useAddress, void *userData) +{ + Request *request = RakNet::OP_NEW(_FILE_AND_LINE_); + request->host=host; + request->chunked = false; + if (useAddress!=UNASSIGNED_SYSTEM_ADDRESS) + { + request->hostEstimatedAddress=useAddress; + if (IsConnected(useAddress)==false) + { + RakNet::OP_DELETE(request, _FILE_AND_LINE_); + return false; + } + } + else + { + if (request->hostEstimatedAddress.FromString(host, '|', ipVersion)==false) + { + RakNet::OP_DELETE(request, _FILE_AND_LINE_); + return false; + } + } + request->hostEstimatedAddress.SetPortHostOrder(port); + request->port=port; + request->stringToTransmit=stringToTransmit; + request->contentLength=-1; + request->contentOffset=0; + request->useSSL=useSSL; + request->ipVersion=ipVersion; + request->userData=userData; + + if (IsConnected(request->hostEstimatedAddress)) + { + sentRequestsMutex.Lock(); + if (sentRequests.Size()==0) + { + request->hostCompletedAddress=request->hostEstimatedAddress; + sentRequests.Push(request, _FILE_AND_LINE_); + sentRequestsMutex.Unlock(); + + SendRequest(request); + } + else + { + // Request pending, push it + pendingRequestsMutex.Lock(); + pendingRequests.Push(request, _FILE_AND_LINE_); + pendingRequestsMutex.Unlock(); + + sentRequestsMutex.Unlock(); + } + } + else + { + pendingRequestsMutex.Lock(); + pendingRequests.Push(request, _FILE_AND_LINE_); + pendingRequestsMutex.Unlock(); + + if (ipVersion!=6) + { + tcpInterface->Connect(host, port, false, AF_INET); + } + else + { + #if RAKNET_SUPPORT_IPV6 + tcpInterface->Connect(host, port, false, AF_INET6); + #else + RakAssert("HTTPConnection2::TransmitRequest needs define RAKNET_SUPPORT_IPV6" && 0); + #endif + } + } + return true; +} +bool HTTPConnection2::GetResponse( RakString &stringTransmitted, RakString &hostTransmitted, RakString &responseReceived, SystemAddress &hostReceived, int &contentOffset ) +{ + void *userData; + return GetResponse(stringTransmitted, hostTransmitted, responseReceived, hostReceived, contentOffset, &userData); + +} +bool HTTPConnection2::GetResponse( RakString &stringTransmitted, RakString &hostTransmitted, RakString &responseReceived, SystemAddress &hostReceived, int &contentOffset, void **userData ) +{ + completedRequestsMutex.Lock(); + if (completedRequests.Size()>0) + { + Request *completedRequest = completedRequests[0]; + completedRequests.RemoveAtIndexFast(0); + completedRequestsMutex.Unlock(); + + responseReceived = completedRequest->stringReceived; + hostReceived = completedRequest->hostCompletedAddress; + stringTransmitted = completedRequest->stringToTransmit; + hostTransmitted = completedRequest->host; + contentOffset = completedRequest->contentOffset; + *userData = completedRequest->userData; + + RakNet::OP_DELETE(completedRequest, _FILE_AND_LINE_); + return true; + } + else + { + completedRequestsMutex.Unlock(); + } + return false; +} +bool HTTPConnection2::IsBusy(void) const +{ + return pendingRequests.Size()>0 || sentRequests.Size()>0; +} +bool HTTPConnection2::HasResponse(void) const +{ + return completedRequests.Size()>0; +} +int ReadChunkSize( char *txtStart, char **txtEnd ) +{ +// char lengthStr[32]; +// memset(lengthStr, 0, 32); +// memcpy(lengthStr, txtStart, txtEnd - txtStart); + return strtoul(txtStart, txtEnd,16); + // return atoi(lengthStr); +} +void ReadChunkBlock( size_t ¤tChunkSize, size_t &bytesReadSoFar, char *txtIn, RakString &txtOut) +{ + size_t bytesToRead; + size_t sLen; + + do + { + bytesToRead = currentChunkSize - bytesReadSoFar; + sLen = strlen(txtIn); + if (sLen < bytesToRead) + bytesToRead = sLen; + txtOut.AppendBytes(txtIn, bytesToRead); + txtIn += bytesToRead; + bytesReadSoFar += bytesToRead; + if (*txtIn == 0) + { + // currentChunkSize=0; + return; + } + // char *newLine = strstr(txtIn, "\r\n"); + if (txtIn[0] && txtIn[0]=='\r' && txtIn[1] && txtIn[1]=='\n' ) + txtIn += 2; // Newline + char *newLine; + currentChunkSize = ReadChunkSize(txtIn, &newLine); + RakAssert(currentChunkSize < 50000); // Sanity check + if (currentChunkSize == 0) + return; + if (newLine == 0) + return; + bytesReadSoFar=0; + txtIn = newLine + 2; + } while (txtIn); +} +PluginReceiveResult HTTPConnection2::OnReceive(Packet *packet) +{ + unsigned int i; + + bool locked=true; + sentRequestsMutex.Lock(); + for (i=0; i < sentRequests.Size(); i++) + { + Request *sentRequest = sentRequests[i]; + if (sentRequest->hostCompletedAddress==packet->systemAddress) + { + sentRequests.RemoveAtIndexFast(i); + locked=false; + sentRequestsMutex.Unlock(); + + /* + static FILE * pFile = 0; + if (pFile==0) + { + long lSize; + char * buffer; + size_t result; + + pFile = fopen ( "string_received.txt" , "rb" ); + if (pFile==nullptr) {fputs ("File error",stderr); exit (1);} + + // obtain file size: + fseek (pFile , 0 , SEEK_END); + lSize = ftell (pFile); + rewind (pFile); + + // allocate memory to contain the whole file: + buffer = (char*) malloc (sizeof(char)*lSize); + if (buffer == nullptr) {fputs ("Memory error",stderr); exit (2);} + + // copy the file into the buffer: + result = fread (buffer,1,lSize,pFile); + if (result != lSize) {fputs ("Reading error",stderr); exit (3);} + + packet->data=(unsigned char*) buffer; + packet->length=lSize; + } + */ + + + const char *isFirstChunk = strstr((char*) packet->data, "Transfer-Encoding: chunked"); + if (isFirstChunk) + { + //printf((char*) packet->data); + + locked=false; + sentRequestsMutex.Unlock(); + + sentRequest->chunked = true; + char *chunkStrStart = strstr((char*) packet->data, "\r\n\r\n"); + RakAssert(chunkStrStart); + + chunkStrStart += 4; // strlen("\r\n\r\n"); + char *body_header; // = strstr(chunkStrStart, "\r\n"); + sentRequest->thisChunkSize = ReadChunkSize(chunkStrStart, &body_header); + sentRequest->bytesReadForThisChunk = 0; + sentRequest->contentOffset = 0; + + if (sentRequest->thisChunkSize == 0) + { + // Done + completedRequestsMutex.Lock(); + completedRequests.Push(sentRequest, _FILE_AND_LINE_); + completedRequestsMutex.Unlock(); + + // If there is another command waiting for this server, send it + SendPendingRequestToConnectedSystem(packet->systemAddress); + } + else + { + + // char *offset = strstr((char*) packet->data+1, "2000"); + + body_header+=2; + ReadChunkBlock(sentRequest->thisChunkSize, sentRequest->bytesReadForThisChunk, body_header, sentRequest->stringReceived); + + if (sentRequest->thisChunkSize==0) + { + // Done + completedRequestsMutex.Lock(); + completedRequests.Push(sentRequest, _FILE_AND_LINE_); + completedRequestsMutex.Unlock(); + + // If there is another command waiting for this server, send it + SendPendingRequestToConnectedSystem(packet->systemAddress); + } + else + { + // Not done + sentRequestsMutex.Lock(); + sentRequests.Push(sentRequest, _FILE_AND_LINE_); + sentRequestsMutex.Unlock(); + } + } + } + else if (sentRequest->chunked) + { + ReadChunkBlock(sentRequest->thisChunkSize, sentRequest->bytesReadForThisChunk, (char*) packet->data, sentRequest->stringReceived); + + if (sentRequest->thisChunkSize==0) + { + // Done + completedRequestsMutex.Lock(); + completedRequests.Push(sentRequest, _FILE_AND_LINE_); + completedRequestsMutex.Unlock(); + + // If there is another command waiting for this server, send it + SendPendingRequestToConnectedSystem(packet->systemAddress); + } + else + { + // Not done + sentRequestsMutex.Lock(); + sentRequests.Push(sentRequest, _FILE_AND_LINE_); + sentRequestsMutex.Unlock(); + } + + } + else + { + sentRequest->stringReceived+=packet->data; + + if (sentRequest->contentLength==-1) + { + const char *length_header = strstr(sentRequest->stringReceived.C_String(), "Content-Length: "); + if(length_header) + { + length_header += 16; // strlen("Content-Length: "); + + unsigned int clLength; + for (clLength=0; length_header[clLength] && length_header[clLength] >= '0' && length_header[clLength] <= '9'; clLength++) + ; + if (clLength>0 && (length_header[clLength]=='\r' || length_header[clLength]=='\n')) + { + sentRequest->contentLength = RakString::ReadIntFromSubstring(length_header, 0, clLength); + } + } + } + + // If we know the content length, find \r\n\r\n + if (sentRequest->contentLength != -1) + { + if (sentRequest->contentLength > 0) + { + const char *body_header = strstr(sentRequest->stringReceived.C_String(), "\r\n\r\n"); + if (body_header) + { + body_header += 4; // strlen("\r\n\r\n"); + size_t slen = strlen(body_header); + //RakAssert(slen <= (size_t) sentRequest->contentLength); + if (slen >= (size_t) sentRequest->contentLength) + { + sentRequest->contentOffset = body_header - sentRequest->stringReceived.C_String(); + completedRequestsMutex.Lock(); + completedRequests.Push(sentRequest, _FILE_AND_LINE_); + completedRequestsMutex.Unlock(); + + // If there is another command waiting for this server, send it + SendPendingRequestToConnectedSystem(packet->systemAddress); + } + else + { + sentRequestsMutex.Lock(); + sentRequests.Push(sentRequest, _FILE_AND_LINE_); + sentRequestsMutex.Unlock(); + } + } + + else + { + sentRequestsMutex.Lock(); + sentRequests.Push(sentRequest, _FILE_AND_LINE_); + sentRequestsMutex.Unlock(); + } + } + else + { + sentRequest->contentOffset=-1; + completedRequestsMutex.Lock(); + completedRequests.Push(sentRequest, _FILE_AND_LINE_); + completedRequestsMutex.Unlock(); + + // If there is another command waiting for this server, send it + SendPendingRequestToConnectedSystem(packet->systemAddress); + } + } + else + { + const char *firstNewlineSet = strstr(sentRequest->stringReceived.C_String(), "\r\n\r\n"); + if (firstNewlineSet!=0) + { + int offset = firstNewlineSet - sentRequest->stringReceived.C_String(); + if (sentRequest->stringReceived.C_String()[offset+4]==0) + sentRequest->contentOffset=-1; + else + sentRequest->contentOffset=offset+4; + completedRequestsMutex.Lock(); + completedRequests.Push(sentRequest, _FILE_AND_LINE_); + completedRequestsMutex.Unlock(); + + // If there is another command waiting for this server, send it + SendPendingRequestToConnectedSystem(packet->systemAddress); + } + else + { + sentRequestsMutex.Lock(); + sentRequests.Push(sentRequest, _FILE_AND_LINE_); + sentRequestsMutex.Unlock(); + } + } + } + + + break; + } + } + + if (locked==true) + sentRequestsMutex.Unlock(); + + return RR_CONTINUE_PROCESSING; +} + +void HTTPConnection2::OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming) +{ + (void) rakNetGUID; + (void) isIncoming; // unknown + + SendPendingRequestToConnectedSystem(systemAddress); +} +void HTTPConnection2::SendPendingRequestToConnectedSystem(SystemAddress sa) +{ + if (sa==UNASSIGNED_SYSTEM_ADDRESS) + return; + + unsigned int requestsSent=0; + + // Search through requests to find a match for this instance of TCPInterface and SystemAddress + unsigned int i; + i=0; + pendingRequestsMutex.Lock(); + while (i < pendingRequests.Size()) + { + Request *request = pendingRequests[i]; + if (request->hostEstimatedAddress==sa) + { + pendingRequests.RemoveAtIndex(i); + // Send this request + request->hostCompletedAddress=sa; + + sentRequestsMutex.Lock(); + sentRequests.Push(request, _FILE_AND_LINE_); + sentRequestsMutex.Unlock(); + + pendingRequestsMutex.Unlock(); + +#if OPEN_SSL_CLIENT_SUPPORT==1 + if (request->useSSL) + tcpInterface->StartSSLClient(sa); +#endif + + SendRequest(request); + requestsSent++; + pendingRequestsMutex.Lock(); + break; + } + else + { + i++; + } + } + pendingRequestsMutex.Unlock(); + + if (requestsSent==0) + { + pendingRequestsMutex.Lock(); + if (pendingRequests.Size() > 0) + { + // Just assign + Request *request = pendingRequests[0]; + pendingRequests.RemoveAtIndex(0); + + request->hostCompletedAddress=sa; + + sentRequestsMutex.Lock(); + sentRequests.Push(request, _FILE_AND_LINE_); + sentRequestsMutex.Unlock(); + pendingRequestsMutex.Unlock(); + + // Send +#if OPEN_SSL_CLIENT_SUPPORT==1 + if (request->useSSL) + tcpInterface->StartSSLClient(sa); +#endif + + + SendRequest(request); + } + else + { + pendingRequestsMutex.Unlock(); + } + } +} +void HTTPConnection2::RemovePendingRequest(SystemAddress sa) +{ + unsigned int i; + i=0; + pendingRequestsMutex.Lock(); + for (i=0; i < pendingRequests.Size(); i++) + { + Request *request = pendingRequests[i]; + if (request->hostEstimatedAddress==sa) + { + pendingRequests.RemoveAtIndex(i); + RakNet::OP_DELETE(request, _FILE_AND_LINE_); + } + else + i++; + } + + pendingRequestsMutex.Unlock(); +} +void HTTPConnection2::SendNextPendingRequest(void) +{ + // Send a pending request + pendingRequestsMutex.Lock(); + if (pendingRequests.Size()>0) + { + Request *pendingRequest = pendingRequests.Peek(); + pendingRequestsMutex.Unlock(); + + if (pendingRequest->ipVersion!=6) + { + tcpInterface->Connect(pendingRequest->host.C_String(), pendingRequest->port, false, AF_INET); + } + else + { +#if RAKNET_SUPPORT_IPV6 + tcpInterface->Connect(pendingRequest->host.C_String(), pendingRequest->port, false, AF_INET6); +#else + RakAssert("HTTPConnection2::TransmitRequest needs define RAKNET_SUPPORT_IPV6" && 0); +#endif + } + } + else + { + pendingRequestsMutex.Unlock(); + } +} + +void HTTPConnection2::OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason) +{ + (void) failedConnectionAttemptReason; + if (packet->systemAddress==UNASSIGNED_SYSTEM_ADDRESS) + return; + + RemovePendingRequest(packet->systemAddress); + + SendNextPendingRequest(); +} +void HTTPConnection2::OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ) +{ + (void) lostConnectionReason; + (void) rakNetGUID; + + if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS) + return; + + // Update sent requests to completed requests + unsigned int i; + i=0; + sentRequestsMutex.Lock(); + while (i < sentRequests.Size()) + { + if (sentRequests[i]->hostCompletedAddress==systemAddress) + { + Request *sentRequest = sentRequests[i]; + if (sentRequest->chunked==false && sentRequest->stringReceived.IsEmpty()==false) + { + if (strstr(sentRequest->stringReceived.C_String(), "Content-Length: ")) + { + char *body_header = strstr((char*) sentRequest->stringReceived.C_String(), "\r\n\r\n"); + if (body_header) + { + body_header += 4; // strlen("\r\n\r\n"); + sentRequest->contentOffset = body_header - sentRequest->stringReceived.C_String(); + } + else + { + sentRequest->contentOffset = 0; + } + + } + else + { + sentRequest->contentOffset = 0; + } + } + + + completedRequestsMutex.Lock(); + completedRequests.Push(sentRequests[i], _FILE_AND_LINE_); + completedRequestsMutex.Unlock(); + + sentRequests.RemoveAtIndexFast(i); + } + else + { + i++; + } + } + sentRequestsMutex.Unlock(); + + SendNextPendingRequest(); +} +bool HTTPConnection2::IsConnected(SystemAddress sa) +{ + SystemAddress remoteSystems[64]; + unsigned short numberOfSystems=64; + tcpInterface->GetConnectionList(remoteSystems, &numberOfSystems); + for (unsigned int i=0; i < numberOfSystems; i++) + { + if (remoteSystems[i]==sa) + { + return true; + } + } + return false; +} +void HTTPConnection2::SendRequest(Request *request) +{ + tcpInterface->Send(request->stringToTransmit.C_String(), (unsigned int) request->stringToTransmit.GetLength(), request->hostCompletedAddress, false); +} + +#endif // #if _RAKNET_SUPPORT_HTTPConnection2==1 && _RAKNET_SUPPORT_TCPInterface==1 diff --git a/Source/HTTPConnection2.h b/Source/HTTPConnection2.h index 9c734aec0..b11b2923d 100644 --- a/Source/HTTPConnection2.h +++ b/Source/HTTPConnection2.h @@ -1,124 +1,122 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file HTTPConnection2.h -/// \brief Contains HTTPConnection2, used to communicate with web servers -/// - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_HTTPConnection2==1 && _RAKNET_SUPPORT_TCPInterface==1 - -#ifndef __HTTP_CONNECTION_2 -#define __HTTP_CONNECTION_2 - -#include "Export.h" -#include "RakString.h" -#include "RakMemoryOverride.h" -#include "RakNetTypes.h" -#include "DS_List.h" -#include "DS_Queue.h" -#include "PluginInterface2.h" -#include "SimpleMutex.h" - -namespace RakNet -{ -/// Forward declarations -class TCPInterface; -struct SystemAddress; - -/// \brief Use HTTPConnection2 to communicate with a web server. -/// \details Start an instance of TCPInterface via the Start() command. -/// This class will handle connecting to transmit a request -class RAK_DLL_EXPORT HTTPConnection2 : public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(HTTPConnection2) - - HTTPConnection2(); - virtual ~HTTPConnection2(); - - /// \brief Connect to, then transmit a request to a TCP based server - /// \param[in] tcp An instance of TCPInterface that previously had TCPInterface::Start() called - /// \param[in] stringToTransmit What string to transmit. See RakString::FormatForPOST(), RakString::FormatForGET(), RakString::FormatForDELETE() - /// \param[in] host The IP address to connect to - /// \param[in] port The port to connect to - /// \param[in] useSSL If to use SSL to connect. OPEN_SSL_CLIENT_SUPPORT must be defined to 1 in RakNetDefines.h or RakNetDefinesOverrides.h - /// \param[in] ipVersion 4 for IPV4, 6 for IPV6 - /// \param[in] useAddress Assume we are connected to this address and send to it, rather than do a lookup - /// \param[in] userData - /// \return false if host is not a valid IP address or domain name - bool TransmitRequest(const char* stringToTransmit, const char* host, unsigned short port=80, bool useSSL=false, int ipVersion=4, SystemAddress useAddress=UNASSIGNED_SYSTEM_ADDRESS, void *userData=0); - - /// \brief Check for and return a response from a prior call to TransmitRequest() - /// As TCP is stream based, you may get a webserver reply over several calls to TCPInterface::Receive() - /// HTTPConnection2 will store Packet::data and return the response to you either when the connection to the webserver is lost, or enough data has been received() - /// This will only potentially return true after a call to ProcessTCPPacket() or OnLostConnection() - /// \param[out] stringTransmitted The original string transmitted - /// \param[out] hostTransmitted The parameter of the same name passed to TransmitRequest() - /// \param[out] responseReceived The response, if any - /// \param[out] hostReceived The SystemAddress from ProcessTCPPacket() or OnLostConnection() - /// \param[out] contentOffset The offset from the start of responseReceived to the data body. Equivalent to searching for \r\n\r\n in responseReceived. - /// \param[out] userData Whatever you passed to TransmitRequest - /// \return true if there was a response. false if not. - bool GetResponse( RakString &stringTransmitted, RakString &hostTransmitted, RakString &responseReceived, SystemAddress &hostReceived, int &contentOffset, void **userData ); - bool GetResponse( RakString &stringTransmitted, RakString &hostTransmitted, RakString &responseReceived, SystemAddress &hostReceived, int &contentOffset ); - - /// \brief Return if any requests are pending - bool IsBusy(void) const; - - /// \brief Return if any requests are waiting to be read by the user - bool HasResponse(void) const; - - struct Request - { - RakString stringToTransmit; - RakString stringReceived; - RakString host; - SystemAddress hostEstimatedAddress; - SystemAddress hostCompletedAddress; - unsigned short port; - bool useSSL; - int contentOffset; - int contentLength; - int ipVersion; - void *userData; - bool chunked; - size_t thisChunkSize; - size_t bytesReadForThisChunk; - }; - - /// \internal - virtual PluginReceiveResult OnReceive(Packet *packet); - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); - virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason); - -protected: - - bool IsConnected(SystemAddress sa); - void SendRequest(Request *request); - void RemovePendingRequest(SystemAddress sa); - void SendNextPendingRequest(void); - void SendPendingRequestToConnectedSystem(SystemAddress sa); - - DataStructures::Queue pendingRequests; - DataStructures::List sentRequests; - DataStructures::List completedRequests; - - SimpleMutex pendingRequestsMutex, sentRequestsMutex, completedRequestsMutex; - -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file HTTPConnection2.h +/// \brief Contains HTTPConnection2, used to communicate with web servers +/// + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_HTTPConnection2==1 && _RAKNET_SUPPORT_TCPInterface==1 + +#pragma once + +#include "Export.h" +#include "RakString.h" +#include "RakMemoryOverride.h" +#include "RakNetTypes.h" +#include "DS_List.h" +#include "DS_Queue.h" +#include "PluginInterface2.h" +#include "SimpleMutex.h" + +namespace RakNet +{ +/// Forward declarations +class TCPInterface; +struct SystemAddress; + +/// \brief Use HTTPConnection2 to communicate with a web server. +/// \details Start an instance of TCPInterface via the Start() command. +/// This class will handle connecting to transmit a request +class RAK_DLL_EXPORT HTTPConnection2 : public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(HTTPConnection2) + + HTTPConnection2(); + virtual ~HTTPConnection2(); + + /// \brief Connect to, then transmit a request to a TCP based server + /// \param[in] tcp An instance of TCPInterface that previously had TCPInterface::Start() called + /// \param[in] stringToTransmit What string to transmit. See RakString::FormatForPOST(), RakString::FormatForGET(), RakString::FormatForDELETE() + /// \param[in] host The IP address to connect to + /// \param[in] port The port to connect to + /// \param[in] useSSL If to use SSL to connect. OPEN_SSL_CLIENT_SUPPORT must be defined to 1 in RakNetDefines.h or RakNetDefinesOverrides.h + /// \param[in] ipVersion 4 for IPV4, 6 for IPV6 + /// \param[in] useAddress Assume we are connected to this address and send to it, rather than do a lookup + /// \param[in] userData + /// \return false if host is not a valid IP address or domain name + bool TransmitRequest(const char* stringToTransmit, const char* host, unsigned short port=80, bool useSSL=false, int ipVersion=4, SystemAddress useAddress=UNASSIGNED_SYSTEM_ADDRESS, void *userData=0); + + /// \brief Check for and return a response from a prior call to TransmitRequest() + /// As TCP is stream based, you may get a webserver reply over several calls to TCPInterface::Receive() + /// HTTPConnection2 will store Packet::data and return the response to you either when the connection to the webserver is lost, or enough data has been received() + /// This will only potentially return true after a call to ProcessTCPPacket() or OnLostConnection() + /// \param[out] stringTransmitted The original string transmitted + /// \param[out] hostTransmitted The parameter of the same name passed to TransmitRequest() + /// \param[out] responseReceived The response, if any + /// \param[out] hostReceived The SystemAddress from ProcessTCPPacket() or OnLostConnection() + /// \param[out] contentOffset The offset from the start of responseReceived to the data body. Equivalent to searching for \r\n\r\n in responseReceived. + /// \param[out] userData Whatever you passed to TransmitRequest + /// \return true if there was a response. false if not. + bool GetResponse( RakString &stringTransmitted, RakString &hostTransmitted, RakString &responseReceived, SystemAddress &hostReceived, int &contentOffset, void **userData ); + bool GetResponse( RakString &stringTransmitted, RakString &hostTransmitted, RakString &responseReceived, SystemAddress &hostReceived, int &contentOffset ); + + /// \brief Return if any requests are pending + bool IsBusy(void) const; + + /// \brief Return if any requests are waiting to be read by the user + bool HasResponse(void) const; + + struct Request + { + RakString stringToTransmit; + RakString stringReceived; + RakString host; + SystemAddress hostEstimatedAddress; + SystemAddress hostCompletedAddress; + unsigned short port; + bool useSSL; + int contentOffset; + int contentLength; + int ipVersion; + void *userData; + bool chunked; + size_t thisChunkSize; + size_t bytesReadForThisChunk; + }; + + /// \internal + virtual PluginReceiveResult OnReceive(Packet *packet); + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); + virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason); + +protected: + + bool IsConnected(SystemAddress sa); + void SendRequest(Request *request); + void RemovePendingRequest(SystemAddress sa); + void SendNextPendingRequest(void); + void SendPendingRequestToConnectedSystem(SystemAddress sa); + + DataStructures::Queue pendingRequests; + DataStructures::List sentRequests; + DataStructures::List completedRequests; + + SimpleMutex pendingRequestsMutex, sentRequestsMutex, completedRequestsMutex; + +}; + +} // namespace RakNet + +#endif + diff --git a/Source/IncrementalReadInterface.h b/Source/IncrementalReadInterface.h index f85043e15..4e0f199b4 100644 --- a/Source/IncrementalReadInterface.h +++ b/Source/IncrementalReadInterface.h @@ -1,38 +1,36 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __INCREMENTAL_READ_INTERFACE_H -#define __INCREMENTAL_READ_INTERFACE_H - -#include "FileListNodeContext.h" -#include "Export.h" - -namespace RakNet -{ - -class RAK_DLL_EXPORT IncrementalReadInterface -{ -public: - IncrementalReadInterface() {} - virtual ~IncrementalReadInterface() {} - - /// Read part of a file into \a destination - /// Return the number of bytes written. Return 0 when file is done. - /// \param[in] filename Filename to read - /// \param[in] startReadBytes What offset from the start of the file to read from - /// \param[in] numBytesToRead How many bytes to read. This is also how many bytes have been allocated to preallocatedDestination - /// \param[out] preallocatedDestination Write your data here - /// \return The number of bytes read, or 0 if none - virtual unsigned int GetFilePart( const char *filename, unsigned int startReadBytes, unsigned int numBytesToRead, void *preallocatedDestination, FileListNodeContext context); -}; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include "FileListNodeContext.h" +#include "Export.h" + +namespace RakNet +{ + +class RAK_DLL_EXPORT IncrementalReadInterface +{ +public: + IncrementalReadInterface() {} + virtual ~IncrementalReadInterface() {} + + /// Read part of a file into \a destination + /// Return the number of bytes written. Return 0 when file is done. + /// \param[in] filename Filename to read + /// \param[in] startReadBytes What offset from the start of the file to read from + /// \param[in] numBytesToRead How many bytes to read. This is also how many bytes have been allocated to preallocatedDestination + /// \param[out] preallocatedDestination Write your data here + /// \return The number of bytes read, or 0 if none + virtual unsigned int GetFilePart( const char *filename, unsigned int startReadBytes, unsigned int numBytesToRead, void *preallocatedDestination, FileListNodeContext context); +}; + +} // namespace RakNet + diff --git a/Source/InternalPacket.h b/Source/InternalPacket.h index b21134744..eedd39bd1 100644 --- a/Source/InternalPacket.h +++ b/Source/InternalPacket.h @@ -1,130 +1,127 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief \b [Internal] A class which stores a user message, and all information associated with sending and receiving that message. -/// - -#ifndef __INTERNAL_PACKET_H -#define __INTERNAL_PACKET_H - -#include "PacketPriority.h" -#include "RakNetTypes.h" -#include "RakMemoryOverride.h" -#include "RakNetDefines.h" -#include "NativeTypes.h" -#include "RakNetDefines.h" -#if USE_SLIDING_WINDOW_CONGESTION_CONTROL!=1 -#include "CCRakNetUDT.h" -#else -#include "CCRakNetSlidingWindow.h" -#endif - -namespace RakNet { - -typedef uint16_t SplitPacketIdType; -typedef uint32_t SplitPacketIndexType; - -/// This is the counter used for holding packet numbers, so we can detect duplicate packets. It should be large enough that if the variables -/// Internally assumed to be 4 bytes, but written as 3 bytes in ReliabilityLayer::WriteToBitStreamFromInternalPacket -typedef uint24_t MessageNumberType; - -/// This is the counter used for holding ordered packet numbers, so we can detect out-of-order packets. It should be large enough that if the variables -/// were to wrap, the newly wrapped values would no longer be in use. Warning: Too large of a value wastes bandwidth! -typedef MessageNumberType OrderingIndexType; - -typedef RakNet::TimeUS RemoteSystemTimeType; - -struct InternalPacketFixedSizeTransmissionHeader -{ - /// A unique numerical identifier given to this user message. Used to identify reliable messages on the network - MessageNumberType reliableMessageNumber; - ///The ID used as identification for ordering messages. Also included in sequenced messages - OrderingIndexType orderingIndex; - // Used only with sequenced messages - OrderingIndexType sequencingIndex; - ///What ordering channel this packet is on, if the reliability type uses ordering channels - unsigned char orderingChannel; - ///The ID of the split packet, if we have split packets. This is the maximum number of split messages we can send simultaneously per connection. - SplitPacketIdType splitPacketId; - ///If this is a split packet, the index into the array of subsplit packets - SplitPacketIndexType splitPacketIndex; - ///The size of the array of subsplit packets - SplitPacketIndexType splitPacketCount;; - ///How many bits long the data is - BitSize_t dataBitLength; - ///What type of reliability algorithm to use with this packet - PacketReliability reliability; - // Not endian safe - // unsigned char priority : 3; - // unsigned char reliability : 5; -}; - -/// Used in InternalPacket when pointing to sharedDataBlock, rather than allocating itself -struct InternalPacketRefCountedData -{ - unsigned char *sharedDataBlock; - unsigned int refCount; -}; - -/// Holds a user message, and related information -/// Don't use a constructor or destructor, due to the memory pool I am using -struct InternalPacket : public InternalPacketFixedSizeTransmissionHeader -{ - /// Identifies the order in which this number was sent. Used locally - MessageNumberType messageInternalOrder; - /// Has this message number been assigned yet? We don't assign until the message is actually sent. - /// This fixes a bug where pre-determining message numbers and then sending a message on a different channel creates a huge gap. - /// This causes performance problems and causes those messages to timeout. - bool messageNumberAssigned; - /// Was this packet number used this update to track windowing drops or increases? Each packet number is only used once per update. -// bool allowWindowUpdate; - ///When this packet was created - RakNet::TimeUS creationTime; - ///The resendNext time to take action on this packet - RakNet::TimeUS nextActionTime; - // For debugging - RakNet::TimeUS retransmissionTime; - // Size of the header when encoded into a bitstream - BitSize_t headerLength; - /// Buffer is a pointer to the actual data, assuming this packet has data at all - unsigned char *data; - /// How to alloc and delete the data member - enum AllocationScheme - { - /// Data is allocated using rakMalloc. Just free it - NORMAL, - - /// data points to a larger block of data, where the larger block is reference counted. internalPacketRefCountedData is used in this case - REF_COUNTED, - - /// If allocation scheme is STACK, data points to stackData and should not be deallocated - /// This is only used when sending. Received packets are deallocated in RakPeer - STACK - } allocationScheme; - InternalPacketRefCountedData *refCountedData; - /// How many attempts we made at sending this message - unsigned char timesSent; - /// The priority level of this packet - PacketPriority priority; - /// If the reliability type requires a receipt, then return this number with it - uint32_t sendReceiptSerial; - - // Used for the resend queue - // Linked list implementation so I can remove from the list via a pointer, without finding it in the list - InternalPacket *resendPrev, *resendNext,*unreliablePrev,*unreliableNext; - - unsigned char stackData[128]; -}; - -} // namespace RakNet - -#endif - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief \b [Internal] A class which stores a user message, and all information associated with sending and receiving that message. +/// + +#pragma once + +#include "PacketPriority.h" +#include "RakNetTypes.h" +#include "RakMemoryOverride.h" +#include "RakNetDefines.h" +#include "NativeTypes.h" +#include "RakNetDefines.h" +#if USE_SLIDING_WINDOW_CONGESTION_CONTROL!=1 +#include "CCRakNetUDT.h" +#else +#include "CCRakNetSlidingWindow.h" +#endif + +namespace RakNet { + +typedef uint16_t SplitPacketIdType; +typedef uint32_t SplitPacketIndexType; + +/// This is the counter used for holding packet numbers, so we can detect duplicate packets. It should be large enough that if the variables +/// Internally assumed to be 4 bytes, but written as 3 bytes in ReliabilityLayer::WriteToBitStreamFromInternalPacket +typedef uint24_t MessageNumberType; + +/// This is the counter used for holding ordered packet numbers, so we can detect out-of-order packets. It should be large enough that if the variables +/// were to wrap, the newly wrapped values would no longer be in use. Warning: Too large of a value wastes bandwidth! +typedef MessageNumberType OrderingIndexType; + +typedef RakNet::TimeUS RemoteSystemTimeType; + +struct InternalPacketFixedSizeTransmissionHeader +{ + /// A unique numerical identifier given to this user message. Used to identify reliable messages on the network + MessageNumberType reliableMessageNumber; + ///The ID used as identification for ordering messages. Also included in sequenced messages + OrderingIndexType orderingIndex; + // Used only with sequenced messages + OrderingIndexType sequencingIndex; + ///What ordering channel this packet is on, if the reliability type uses ordering channels + unsigned char orderingChannel; + ///The ID of the split packet, if we have split packets. This is the maximum number of split messages we can send simultaneously per connection. + SplitPacketIdType splitPacketId; + ///If this is a split packet, the index into the array of subsplit packets + SplitPacketIndexType splitPacketIndex; + ///The size of the array of subsplit packets + SplitPacketIndexType splitPacketCount;; + ///How many bits long the data is + BitSize_t dataBitLength; + ///What type of reliability algorithm to use with this packet + PacketReliability reliability; + // Not endian safe + // unsigned char priority : 3; + // unsigned char reliability : 5; +}; + +/// Used in InternalPacket when pointing to sharedDataBlock, rather than allocating itself +struct InternalPacketRefCountedData +{ + unsigned char *sharedDataBlock; + unsigned int refCount; +}; + +/// Holds a user message, and related information +/// Don't use a constructor or destructor, due to the memory pool I am using +struct InternalPacket : public InternalPacketFixedSizeTransmissionHeader +{ + /// Identifies the order in which this number was sent. Used locally + MessageNumberType messageInternalOrder; + /// Has this message number been assigned yet? We don't assign until the message is actually sent. + /// This fixes a bug where pre-determining message numbers and then sending a message on a different channel creates a huge gap. + /// This causes performance problems and causes those messages to timeout. + bool messageNumberAssigned; + /// Was this packet number used this update to track windowing drops or increases? Each packet number is only used once per update. +// bool allowWindowUpdate; + ///When this packet was created + RakNet::TimeUS creationTime; + ///The resendNext time to take action on this packet + RakNet::TimeUS nextActionTime; + // For debugging + RakNet::TimeUS retransmissionTime; + // Size of the header when encoded into a bitstream + BitSize_t headerLength; + /// Buffer is a pointer to the actual data, assuming this packet has data at all + unsigned char *data; + /// How to alloc and delete the data member + enum AllocationScheme + { + /// Data is allocated using rakMalloc. Just free it + NORMAL, + + /// data points to a larger block of data, where the larger block is reference counted. internalPacketRefCountedData is used in this case + REF_COUNTED, + + /// If allocation scheme is STACK, data points to stackData and should not be deallocated + /// This is only used when sending. Received packets are deallocated in RakPeer + STACK + } allocationScheme; + InternalPacketRefCountedData *refCountedData; + /// How many attempts we made at sending this message + unsigned char timesSent; + /// The priority level of this packet + PacketPriority priority; + /// If the reliability type requires a receipt, then return this number with it + uint32_t sendReceiptSerial; + + // Used for the resend queue + // Linked list implementation so I can remove from the list via a pointer, without finding it in the list + InternalPacket *resendPrev, *resendNext,*unreliablePrev,*unreliableNext; + + unsigned char stackData[128]; +}; + +} // namespace RakNet + diff --git a/Source/Itoa.h b/Source/Itoa.h index f508cfc30..2d43665d5 100644 --- a/Source/Itoa.h +++ b/Source/Itoa.h @@ -1,25 +1,23 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __RAK_ITOA_H -#define __RAK_ITOA_H - -#ifdef __cplusplus -extern "C" { -#endif - -char* Itoa( int value, char* result, int base ); - -#ifdef __cplusplus -} -#endif - - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +char* Itoa( int value, char* result, int base ); + +#ifdef __cplusplus +} +#endif + + diff --git a/Source/Kbhit.h b/Source/Kbhit.h index c311c15d1..33d6501b2 100644 --- a/Source/Kbhit.h +++ b/Source/Kbhit.h @@ -1,84 +1,87 @@ -/***************************************************************************** -kbhit() and getch() for Linux/UNIX -Chris Giese http://my.execpc.com/~geezer -Release date: ? -This code is public domain (no copyright). -You can do whatever you want with it. -*****************************************************************************/ -#if defined(_WIN32) -#include /* kbhit(), getch() */ - -#else -#include /* struct timeval, select() */ -/* ICANON, ECHO, TCSANOW, struct termios */ -#include /* tcgetattr(), tcsetattr() */ -#include /* atexit(), exit() */ -#include /* read() */ -#include /* printf() */ -#include /* memcpy */ - -static struct termios g_old_kbd_mode; -/***************************************************************************** -*****************************************************************************/ -static void cooked(void) -{ - tcsetattr(0, TCSANOW, &g_old_kbd_mode); -} -/***************************************************************************** -*****************************************************************************/ -static void raw(void) -{ - static char init; -/**/ - struct termios new_kbd_mode; - - if(init) - return; -/* put keyboard (stdin, actually) in raw, unbuffered mode */ - tcgetattr(0, &g_old_kbd_mode); - memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios)); - new_kbd_mode.c_lflag &= ~(ICANON /*| ECHO */ ); - new_kbd_mode.c_cc[VTIME] = 0; - new_kbd_mode.c_cc[VMIN] = 1; - tcsetattr(0, TCSANOW, &new_kbd_mode); -/* when we exit, go back to normal, "cooked" mode */ - atexit(cooked); - - init = 1; -} -/***************************************************************************** -*****************************************************************************/ -static int kbhit(void) -{ - struct timeval timeout; - fd_set read_handles; - int status; - - raw(); -/* check stdin (fd 0) for activity */ - FD_ZERO(&read_handles); - FD_SET(0, &read_handles); - timeout.tv_sec = timeout.tv_usec = 0; - status = select(0 + 1, &read_handles, NULL, NULL, &timeout); - if(status < 0) - { - printf("select() failed in kbhit()\n"); - exit(1); - } - return status; -} -/***************************************************************************** -*****************************************************************************/ -static int getch(void) -{ - unsigned char temp; - - raw(); -/* stdin = fd 0 */ - if(read(0, &temp, 1) != 1) - return 0; - return temp; -} -#endif - - +/***************************************************************************** +kbhit() and getch() for Linux/UNIX +Chris Giese http://my.execpc.com/~geezer +Release date: ? +This code is public domain (no copyright). +You can do whatever you want with it. +*****************************************************************************/ + +#pragma once + +#if defined(_WIN32) +#include /* kbhit(), getch() */ + +#else +#include /* struct timeval, select() */ +/* ICANON, ECHO, TCSANOW, struct termios */ +#include /* tcgetattr(), tcsetattr() */ +#include /* atexit(), exit() */ +#include /* read() */ +#include /* printf() */ +#include /* memcpy */ + +static struct termios g_old_kbd_mode; +/***************************************************************************** +*****************************************************************************/ +static void cooked(void) +{ + tcsetattr(0, TCSANOW, &g_old_kbd_mode); +} +/***************************************************************************** +*****************************************************************************/ +static void raw(void) +{ + static char init; +/**/ + struct termios new_kbd_mode; + + if(init) + return; +/* put keyboard (stdin, actually) in raw, unbuffered mode */ + tcgetattr(0, &g_old_kbd_mode); + memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios)); + new_kbd_mode.c_lflag &= ~(ICANON /*| ECHO */ ); + new_kbd_mode.c_cc[VTIME] = 0; + new_kbd_mode.c_cc[VMIN] = 1; + tcsetattr(0, TCSANOW, &new_kbd_mode); +/* when we exit, go back to normal, "cooked" mode */ + atexit(cooked); + + init = 1; +} +/***************************************************************************** +*****************************************************************************/ +static int kbhit(void) +{ + struct timeval timeout; + fd_set read_handles; + int status; + + raw(); +/* check stdin (fd 0) for activity */ + FD_ZERO(&read_handles); + FD_SET(0, &read_handles); + timeout.tv_sec = timeout.tv_usec = 0; + status = select(0 + 1, &read_handles, nullptr, nullptr, &timeout); + if(status < 0) + { + printf("select() failed in kbhit()\n"); + exit(1); + } + return status; +} +/***************************************************************************** +*****************************************************************************/ +static int getch(void) +{ + unsigned char temp; + + raw(); +/* stdin = fd 0 */ + if(read(0, &temp, 1) != 1) + return 0; + return temp; +} +#endif + + diff --git a/Source/LinuxStrings.h b/Source/LinuxStrings.h index c76a9f8a2..6c3b0a34e 100644 --- a/Source/LinuxStrings.h +++ b/Source/LinuxStrings.h @@ -1,40 +1,38 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef _GCC_WIN_STRINGS -#define _GCC_WIN_STRINGS - -#if defined(__native_client__) - #ifndef _stricmp - int _stricmp(const char* s1, const char* s2); - #endif - int _strnicmp(const char* s1, const char* s2, size_t n); - char *_strlwr(char * str ); - #define _vsnprintf vsnprintf -#else - #if (defined(__GNUC__) || defined(__GCCXML__) || defined(__S3E__) ) && !defined(_WIN32) - #ifndef _stricmp - int _stricmp(const char* s1, const char* s2); - #endif - int _strnicmp(const char* s1, const char* s2, size_t n); - // http://www.jenkinssoftware.com/forum/index.php?topic=5010.msg20920#msg20920 - // #ifndef _vsnprintf - #define _vsnprintf vsnprintf - // #endif -#ifndef __APPLE__ - char *_strlwr(char * str ); //this won't compile on OSX for some reason -#endif - - - - #endif -#endif - -#endif // _GCC_WIN_STRINGS +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#if defined(__native_client__) + #ifndef _stricmp + int _stricmp(const char* s1, const char* s2); + #endif + int _strnicmp(const char* s1, const char* s2, size_t n); + char *_strlwr(char * str ); + #define _vsnprintf vsnprintf +#else + #if (defined(__GNUC__) || defined(__GCCXML__) || defined(__S3E__) ) && !defined(_WIN32) + #ifndef _stricmp + int _stricmp(const char* s1, const char* s2); + #endif + int _strnicmp(const char* s1, const char* s2, size_t n); + // http://www.jenkinssoftware.com/forum/index.php?topic=5010.msg20920#msg20920 + // #ifndef _vsnprintf + #define _vsnprintf vsnprintf + // #endif +#ifndef __APPLE__ + char *_strlwr(char * str ); //this won't compile on OSX for some reason +#endif + + + + #endif +#endif + diff --git a/Source/LocklessTypes.h b/Source/LocklessTypes.h index 9d9c3cec0..6f03d342e 100644 --- a/Source/LocklessTypes.h +++ b/Source/LocklessTypes.h @@ -1,50 +1,48 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __LOCKLESS_TYPES_H -#define __LOCKLESS_TYPES_H - -#include "Export.h" -#include "NativeTypes.h" -#include "WindowsIncludes.h" -#if defined(ANDROID) || defined(__S3E__) || defined(__APPLE__) -// __sync_fetch_and_add not supported apparently -#include "SimpleMutex.h" -#endif - -namespace RakNet -{ - -class RAK_DLL_EXPORT LocklessUint32_t -{ -public: - LocklessUint32_t(); - explicit LocklessUint32_t(uint32_t initial); - // Returns variable value after changing it - uint32_t Increment(void); - // Returns variable value after changing it - uint32_t Decrement(void); - uint32_t GetValue(void) const {return value;} - -protected: -#ifdef _WIN32 - volatile LONG value; -#elif defined(ANDROID) || defined(__S3E__) || defined(__APPLE__) - // __sync_fetch_and_add not supported apparently - SimpleMutex mutex; - uint32_t value; -#else - volatile uint32_t value; -#endif -}; - -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include "Export.h" +#include "NativeTypes.h" +#include "WindowsIncludes.h" +#if defined(ANDROID) || defined(__S3E__) || defined(__APPLE__) +// __sync_fetch_and_add not supported apparently +#include "SimpleMutex.h" +#endif + +namespace RakNet +{ + +class RAK_DLL_EXPORT LocklessUint32_t +{ +public: + LocklessUint32_t(); + explicit LocklessUint32_t(uint32_t initial); + // Returns variable value after changing it + uint32_t Increment(void); + // Returns variable value after changing it + uint32_t Decrement(void); + uint32_t GetValue(void) const {return value;} + +protected: +#ifdef _WIN32 + volatile LONG value; +#elif defined(ANDROID) || defined(__S3E__) || defined(__APPLE__) + // __sync_fetch_and_add not supported apparently + SimpleMutex mutex; + uint32_t value; +#else + volatile uint32_t value; +#endif +}; + +} + diff --git a/Source/LogCommandParser.h b/Source/LogCommandParser.h index 26e3521d0..584f109a7 100644 --- a/Source/LogCommandParser.h +++ b/Source/LogCommandParser.h @@ -1,127 +1,125 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Contains LogCommandParser , Used to send logs to connected consoles -/// - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_LogCommandParser==1 - -#ifndef __LOG_COMMAND_PARSER -#define __LOG_COMMAND_PARSER - -#include "CommandParserInterface.h" -#include "Export.h" - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; - -/// \brief Adds the ability to send logging output to a remote console -class RAK_DLL_EXPORT LogCommandParser : public CommandParserInterface -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(LogCommandParser) - - LogCommandParser(); - ~LogCommandParser(); - - /// Given \a command with parameters \a parameterList , do whatever processing you wish. - /// \param[in] command The command to process - /// \param[in] numParameters How many parameters were passed along with the command - /// \param[in] parameterList The list of parameters. parameterList[0] is the first parameter and so on. - /// \param[in] transport The transport interface we can use to write to - /// \param[in] systemAddress The player that sent this command. - /// \param[in] originalString The string that was actually sent over the network, in case you want to do your own parsing - bool OnCommand(const char *command, unsigned numParameters, char **parameterList, TransportInterface *transport, const SystemAddress &systemAddress, const char *originalString); - - /// You are responsible for overriding this function and returning a static string, which will identifier your parser. - /// This should return a static string - /// \return The name that you return. - const char *GetName(void) const; - - /// A callback for when you are expected to send a brief description of your parser to \a systemAddress - /// \param[in] transport The transport interface we can use to write to - /// \param[in] systemAddress The player that requested help. - void SendHelp(TransportInterface *transport, const SystemAddress &systemAddress); - - /// All logs must be associated with a channel. This is a filter so that remote clients only get logs for a system they care about. - // If you call Log with a channel that is unknown, that channel will automatically be added - /// \param[in] channelName A persistent string naming the channel. Don't deallocate this string. - void AddChannel(const char *channelName); - - /// Write a log to a channel. - /// Logs are not buffered, so only remote consoles connected and subscribing at the time you write will get the output. - /// \param[in] format Same as RAKNET_DEBUG_PRINTF() - /// \param[in] ... Same as RAKNET_DEBUG_PRINTF() - void WriteLog(const char *channelName, const char *format, ...); - - /// A callback for when \a systemAddress has connected to us. - /// \param[in] systemAddress The player that has connected. - /// \param[in] transport The transport interface that sent us this information. Can be used to send messages to this or other players. - void OnNewIncomingConnection(const SystemAddress &systemAddress, TransportInterface *transport); - - /// A callback for when \a systemAddress has disconnected, either gracefully or forcefully - /// \param[in] systemAddress The player that has disconnected. - /// \param[in] transport The transport interface that sent us this information. - void OnConnectionLost(const SystemAddress &systemAddress, TransportInterface *transport); - - /// This is called every time transport interface is registered. If you want to save a copy of the TransportInterface pointer - /// This is the place to do it - /// \param[in] transport The new TransportInterface - void OnTransportChange(TransportInterface *transport); -protected: - /// Sends the currently active channels to the user - /// \param[in] systemAddress The player to send to - /// \param[in] transport The transport interface to use to send the channels - void PrintChannels(const SystemAddress &systemAddress, TransportInterface *transport) const; - - /// Unsubscribe a user from a channel (or from all channels) - /// \param[in] systemAddress The player to unsubscribe to - /// \param[in] channelName If 0, then unsubscribe from all channels. Otherwise unsubscribe from the named channel - unsigned Unsubscribe(const SystemAddress &systemAddress, const char *channelName); - - /// Subscribe a user to a channel (or to all channels) - /// \param[in] systemAddress The player to subscribe to - /// \param[in] channelName If 0, then subscribe from all channels. Otherwise subscribe to the named channel - unsigned Subscribe(const SystemAddress &systemAddress, const char *channelName); - - /// Given the name of a channel, return the index into channelNames where it is located - /// \param[in] channelName The name of the channel - unsigned GetChannelIndexFromName(const char *channelName); - - /// One of these structures is created per player - struct SystemAddressAndChannel - { - /// The ID of the player - SystemAddress systemAddress; - - /// Bitwise representations of the channels subscribed to. If bit 0 is set, then we subscribe to channelNames[0] and so on. - unsigned channels; - }; - - /// The list of remote users. Added to when users subscribe, removed when they disconnect or unsubscribe - DataStructures::List remoteUsers; - - /// Names of the channels at each bit, or 0 for an unused channel - const char *channelNames[32]; - - /// This is so I can save the current transport provider, solely so I can use it without having the user pass it to Log - TransportInterface *trans; -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Contains LogCommandParser , Used to send logs to connected consoles +/// + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_LogCommandParser==1 + +#pragma once + +#include "CommandParserInterface.h" +#include "Export.h" + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; + +/// \brief Adds the ability to send logging output to a remote console +class RAK_DLL_EXPORT LogCommandParser : public CommandParserInterface +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(LogCommandParser) + + LogCommandParser(); + ~LogCommandParser(); + + /// Given \a command with parameters \a parameterList , do whatever processing you wish. + /// \param[in] command The command to process + /// \param[in] numParameters How many parameters were passed along with the command + /// \param[in] parameterList The list of parameters. parameterList[0] is the first parameter and so on. + /// \param[in] transport The transport interface we can use to write to + /// \param[in] systemAddress The player that sent this command. + /// \param[in] originalString The string that was actually sent over the network, in case you want to do your own parsing + bool OnCommand(const char *command, unsigned numParameters, char **parameterList, TransportInterface *transport, const SystemAddress &systemAddress, const char *originalString); + + /// You are responsible for overriding this function and returning a static string, which will identifier your parser. + /// This should return a static string + /// \return The name that you return. + const char *GetName(void) const; + + /// A callback for when you are expected to send a brief description of your parser to \a systemAddress + /// \param[in] transport The transport interface we can use to write to + /// \param[in] systemAddress The player that requested help. + void SendHelp(TransportInterface *transport, const SystemAddress &systemAddress); + + /// All logs must be associated with a channel. This is a filter so that remote clients only get logs for a system they care about. + // If you call Log with a channel that is unknown, that channel will automatically be added + /// \param[in] channelName A persistent string naming the channel. Don't deallocate this string. + void AddChannel(const char *channelName); + + /// Write a log to a channel. + /// Logs are not buffered, so only remote consoles connected and subscribing at the time you write will get the output. + /// \param[in] format Same as RAKNET_DEBUG_PRINTF() + /// \param[in] ... Same as RAKNET_DEBUG_PRINTF() + void WriteLog(const char *channelName, const char *format, ...); + + /// A callback for when \a systemAddress has connected to us. + /// \param[in] systemAddress The player that has connected. + /// \param[in] transport The transport interface that sent us this information. Can be used to send messages to this or other players. + void OnNewIncomingConnection(const SystemAddress &systemAddress, TransportInterface *transport); + + /// A callback for when \a systemAddress has disconnected, either gracefully or forcefully + /// \param[in] systemAddress The player that has disconnected. + /// \param[in] transport The transport interface that sent us this information. + void OnConnectionLost(const SystemAddress &systemAddress, TransportInterface *transport); + + /// This is called every time transport interface is registered. If you want to save a copy of the TransportInterface pointer + /// This is the place to do it + /// \param[in] transport The new TransportInterface + void OnTransportChange(TransportInterface *transport); +protected: + /// Sends the currently active channels to the user + /// \param[in] systemAddress The player to send to + /// \param[in] transport The transport interface to use to send the channels + void PrintChannels(const SystemAddress &systemAddress, TransportInterface *transport) const; + + /// Unsubscribe a user from a channel (or from all channels) + /// \param[in] systemAddress The player to unsubscribe to + /// \param[in] channelName If 0, then unsubscribe from all channels. Otherwise unsubscribe from the named channel + unsigned Unsubscribe(const SystemAddress &systemAddress, const char *channelName); + + /// Subscribe a user to a channel (or to all channels) + /// \param[in] systemAddress The player to subscribe to + /// \param[in] channelName If 0, then subscribe from all channels. Otherwise subscribe to the named channel + unsigned Subscribe(const SystemAddress &systemAddress, const char *channelName); + + /// Given the name of a channel, return the index into channelNames where it is located + /// \param[in] channelName The name of the channel + unsigned GetChannelIndexFromName(const char *channelName); + + /// One of these structures is created per player + struct SystemAddressAndChannel + { + /// The ID of the player + SystemAddress systemAddress; + + /// Bitwise representations of the channels subscribed to. If bit 0 is set, then we subscribe to channelNames[0] and so on. + unsigned channels; + }; + + /// The list of remote users. Added to when users subscribe, removed when they disconnect or unsubscribe + DataStructures::List remoteUsers; + + /// Names of the channels at each bit, or 0 for an unused channel + const char *channelNames[32]; + + /// This is so I can save the current transport provider, solely so I can use it without having the user pass it to Log + TransportInterface *trans; +}; + +} // namespace RakNet + +#endif + diff --git a/Source/MTUSize.h b/Source/MTUSize.h index a08b436ab..03ba754d2 100644 --- a/Source/MTUSize.h +++ b/Source/MTUSize.h @@ -1,38 +1,41 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief \b [Internal] Defines the default maximum transfer unit. -/// - - -#ifndef MAXIMUM_MTU_SIZE - -/// \li \em 17914 16 Mbit/Sec Token Ring -/// \li \em 4464 4 Mbits/Sec Token Ring -/// \li \em 4352 FDDI -/// \li \em 1500. The largest Ethernet packet size \b recommended. This is the typical setting for non-PPPoE, non-VPN connections. The default value for NETGEAR routers, adapters and switches. -/// \li \em 1492. The size PPPoE prefers. -/// \li \em 1472. Maximum size to use for pinging. (Bigger packets are fragmented.) -/// \li \em 1468. The size DHCP prefers. -/// \li \em 1460. Usable by AOL if you don't have large email attachments, etc. -/// \li \em 1430. The size VPN and PPTP prefer. -/// \li \em 1400. Maximum size for AOL DSL. -/// \li \em 576. Typical value to connect to dial-up ISPs. -/// The largest value for an UDP datagram - - - -#define MAXIMUM_MTU_SIZE 1492 - - -#define MINIMUM_MTU_SIZE 400 - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + + +/// \file +/// \brief \b [Internal] Defines the default maximum transfer unit. +/// + + +#ifndef MAXIMUM_MTU_SIZE + +/// \li \em 17914 16 Mbit/Sec Token Ring +/// \li \em 4464 4 Mbits/Sec Token Ring +/// \li \em 4352 FDDI +/// \li \em 1500. The largest Ethernet packet size \b recommended. This is the typical setting for non-PPPoE, non-VPN connections. The default value for NETGEAR routers, adapters and switches. +/// \li \em 1492. The size PPPoE prefers. +/// \li \em 1472. Maximum size to use for pinging. (Bigger packets are fragmented.) +/// \li \em 1468. The size DHCP prefers. +/// \li \em 1460. Usable by AOL if you don't have large email attachments, etc. +/// \li \em 1430. The size VPN and PPTP prefer. +/// \li \em 1400. Maximum size for AOL DSL. +/// \li \em 576. Typical value to connect to dial-up ISPs. +/// The largest value for an UDP datagram + + + +#define MAXIMUM_MTU_SIZE 1492 + + +#define MINIMUM_MTU_SIZE 400 + +#endif diff --git a/Source/MessageFilter.h b/Source/MessageFilter.h index 8df8b8ca2..afb9cc754 100644 --- a/Source/MessageFilter.h +++ b/Source/MessageFilter.h @@ -1,198 +1,196 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Message filter plugin. Assigns systems to FilterSets. Each FilterSet limits what messages are allowed. This is a security related plugin. -/// - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_MessageFilter==1 - -#ifndef __MESSAGE_FILTER_PLUGIN_H -#define __MESSAGE_FILTER_PLUGIN_H - -#include "RakNetTypes.h" -#include "PluginInterface2.h" -#include "DS_OrderedList.h" -#include "DS_Hash.h" -#include "Export.h" - -/// MessageIdentifier (ID_*) values shoudln't go higher than this. Change it if you do. -#define MESSAGE_FILTER_MAX_MESSAGE_ID 256 - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; - -/// \internal Has to be public so some of the shittier compilers can use it. -int RAK_DLL_EXPORT MessageFilterStrComp( char *const &key,char *const &data ); - -/// \internal Has to be public so some of the shittier compilers can use it. -struct FilterSet -{ - bool banOnFilterTimeExceed; - bool kickOnDisallowedMessage; - bool banOnDisallowedMessage; - RakNet::TimeMS disallowedMessageBanTimeMS; - RakNet::TimeMS timeExceedBanTimeMS; - RakNet::TimeMS maxMemberTimeMS; - void (*invalidMessageCallback)(RakPeerInterface *peer, AddressOrGUID systemAddress, int filterSetID, void *userData, unsigned char messageID); - void *disallowedCallbackUserData; - void (*timeoutCallback)(RakPeerInterface *peer, AddressOrGUID systemAddress, int filterSetID, void *userData); - void *timeoutUserData; - int filterSetID; - bool allowedIDs[MESSAGE_FILTER_MAX_MESSAGE_ID]; - DataStructures::OrderedList allowedRPC4; -}; - -/// \internal Has to be public so some of the shittier compilers can use it. -int RAK_DLL_EXPORT FilterSetComp( const int &key, FilterSet * const &data ); - -/// \internal Has to be public so some of the shittier compilers can use it. -struct FilteredSystem -{ - FilterSet *filter; - RakNet::TimeMS timeEnteredThisSet; -}; - -/// \defgroup MESSAGEFILTER_GROUP MessageFilter -/// \brief Remote incoming packets from unauthorized systems -/// \details -/// \ingroup PLUGINS_GROUP - -/// \brief Assigns systems to FilterSets. Each FilterSet limits what kinds of messages are allowed. -/// \details The MessageFilter plugin is used for security where you limit what systems can send what kind of messages.
-/// You implicitly define FilterSets, and add allowed message IDs to these FilterSets.
-/// You then add systems to these filters, such that those systems are limited to sending what the filters allows.
-/// You can automatically assign systems to a filter.
-/// You can automatically kick and possibly ban users that stay in a filter too long, or send the wrong message.
-/// Each system is a member of either zero or one filters.
-/// Add this plugin before any plugin you wish to filter (most likely just add this plugin before any other). -/// \ingroup MESSAGEFILTER_GROUP -class RAK_DLL_EXPORT MessageFilter : public PluginInterface2 -{ -public: - - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(MessageFilter) - - MessageFilter(); - virtual ~MessageFilter(); - - // -------------------------------------------------------------------------------------------- - // User functions - // -------------------------------------------------------------------------------------------- - - /// Automatically add all new systems to a particular filter - /// Defaults to -1 - /// \param[in] filterSetID Which filter to add new systems to. <0 for do not add. - void SetAutoAddNewConnectionsToFilter(int filterSetID); - - /// Allow a range of message IDs - /// Always allowed by default: ID_CONNECTION_REQUEST_ACCEPTED through ID_DOWNLOAD_PROGRESS - /// Usually you specify a range to make it easier to add new enumerations without having to constantly refer back to this function. - /// \param[in] allow True to allow this message ID, false to disallow. By default, all messageIDs except the noted types are disallowed. This includes messages from other plugins! - /// \param[in] messageIDStart The first ID_* message to allow in the range. Inclusive. - /// \param[in] messageIDEnd The last ID_* message to allow in the range. Inclusive. - /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. - void SetAllowMessageID(bool allow, int messageIDStart, int messageIDEnd,int filterSetID); - - /// Allow a specific RPC4 call - /// \pre MessageFilter must be attached before RPC4 - /// \param[in] uniqueID Identifier passed to RegisterFunction() - /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. - void SetAllowRPC4(bool allow, const char* uniqueID, int filterSetID); - - /// What action to take on a disallowed message. You can kick or not. You can add them to the ban list for some time - /// By default no action is taken. The message is simply ignored. - /// param[in] 0 for permanent ban, >0 for ban time in milliseconds. - /// \param[in] kickOnDisallowed kick the system that sent a disallowed message. - /// \param[in] banOnDisallowed ban the system that sent a disallowed message. See \a banTimeMS for the ban duration - /// \param[in] banTimeMS Passed to the milliseconds parameter of RakPeer::AddToBanList. - /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. - void SetActionOnDisallowedMessage(bool kickOnDisallowed, bool banOnDisallowed, RakNet::TimeMS banTimeMS, int filterSetID); - - /// Set a user callback to be called on an invalid message for a particular filterSet - /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. - /// \param[in] userData A pointer passed with the callback - /// \param[in] invalidMessageCallback A pointer to a C function to be called back with the specified parameters. - void SetDisallowedMessageCallback(int filterSetID, void *userData, void (*invalidMessageCallback)(RakPeerInterface *peer, AddressOrGUID addressOrGUID, int filterSetID, void *userData, unsigned char messageID)); - - /// Set a user callback to be called when a user is disconnected due to SetFilterMaxTime - /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. - /// \param[in] userData A pointer passed with the callback - /// \param[in] invalidMessageCallback A pointer to a C function to be called back with the specified parameters. - void SetTimeoutCallback(int filterSetID, void *userData, void (*invalidMessageCallback)(RakPeerInterface *peer, AddressOrGUID addressOrGUID, int filterSetID, void *userData)); - - /// Limit how long a connection can stay in a particular filterSetID. After this time, the connection is kicked and possibly banned. - /// By default there is no limit to how long a connection can stay in a particular filter set. - /// \param[in] allowedTimeMS How many milliseconds to allow a connection to stay in this filter set. - /// \param[in] banOnExceed True or false to ban the system, or not, when \a allowedTimeMS is exceeded - /// \param[in] banTimeMS Passed to the milliseconds parameter of RakPeer::AddToBanList. - /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. - void SetFilterMaxTime(int allowedTimeMS, bool banOnExceed, RakNet::TimeMS banTimeMS, int filterSetID); - - /// Get the filterSetID a system is using. Returns -1 for none. - /// \param[in] addressOrGUID The system we are referring to - int GetSystemFilterSet(AddressOrGUID addressOrGUID); - - /// Assign a system to a filter set. - /// Systems are automatically added to filter sets (or not) based on SetAutoAddNewConnectionsToFilter() - /// This function is used to change the filter set a system is using, to add it to a new filter set, or to remove it from all existin filter sets. - /// \param[in] addressOrGUID The system we are referring to - /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. If -1, the system will be removed from all filter sets. - void SetSystemFilterSet(AddressOrGUID addressOrGUID, int filterSetID); - - /// Returns the number of systems subscribed to a particular filter set - /// Using anything other than -1 for \a filterSetID is slow, so you should store the returned value. - /// \param[in] filterSetID The filter set to limit to. Use -1 for none (just returns the total number of filter systems in that case). - unsigned GetSystemCount(int filterSetID) const; - - /// Returns the total number of filter sets. - /// \return The total number of filter sets. - unsigned GetFilterSetCount(void) const; - - /// Returns the ID of a filter set, by index - /// \param[in] An index between 0 and GetFilterSetCount()-1 inclusive - int GetFilterSetIDByIndex(unsigned index); - - /// Delete a FilterSet. All systems formerly subscribed to this filter are now unrestricted. - /// \param[in] filterSetID The ID of the filter set to delete. - void DeleteFilterSet(int filterSetID); - - // -------------------------------------------------------------------------------------------- - // Packet handling functions - // -------------------------------------------------------------------------------------------- - virtual void Update(void); - virtual PluginReceiveResult OnReceive(Packet *packet); - virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - -protected: - - void Clear(void); - void DeallocateFilterSet(FilterSet *filterSet); - FilterSet* GetFilterSetByID(int filterSetID); - void OnInvalidMessage(FilterSet *filterSet, AddressOrGUID systemAddress, unsigned char messageID); - - DataStructures::OrderedList filterList; - // Change to guid - DataStructures::Hash systemList; - - int autoAddNewConnectionsToFilter; - RakNet::Time whenLastTimeoutCheck; -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Message filter plugin. Assigns systems to FilterSets. Each FilterSet limits what messages are allowed. This is a security related plugin. +/// + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_MessageFilter==1 + +#pragma once + +#include "RakNetTypes.h" +#include "PluginInterface2.h" +#include "DS_OrderedList.h" +#include "DS_Hash.h" +#include "Export.h" + +/// MessageIdentifier (ID_*) values shoudln't go higher than this. Change it if you do. +#define MESSAGE_FILTER_MAX_MESSAGE_ID 256 + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; + +/// \internal Has to be public so some of the shittier compilers can use it. +int RAK_DLL_EXPORT MessageFilterStrComp( char *const &key,char *const &data ); + +/// \internal Has to be public so some of the shittier compilers can use it. +struct FilterSet +{ + bool banOnFilterTimeExceed; + bool kickOnDisallowedMessage; + bool banOnDisallowedMessage; + RakNet::TimeMS disallowedMessageBanTimeMS; + RakNet::TimeMS timeExceedBanTimeMS; + RakNet::TimeMS maxMemberTimeMS; + void (*invalidMessageCallback)(RakPeerInterface *peer, AddressOrGUID systemAddress, int filterSetID, void *userData, unsigned char messageID); + void *disallowedCallbackUserData; + void (*timeoutCallback)(RakPeerInterface *peer, AddressOrGUID systemAddress, int filterSetID, void *userData); + void *timeoutUserData; + int filterSetID; + bool allowedIDs[MESSAGE_FILTER_MAX_MESSAGE_ID]; + DataStructures::OrderedList allowedRPC4; +}; + +/// \internal Has to be public so some of the shittier compilers can use it. +int RAK_DLL_EXPORT FilterSetComp( const int &key, FilterSet * const &data ); + +/// \internal Has to be public so some of the shittier compilers can use it. +struct FilteredSystem +{ + FilterSet *filter; + RakNet::TimeMS timeEnteredThisSet; +}; + +/// \defgroup MESSAGEFILTER_GROUP MessageFilter +/// \brief Remote incoming packets from unauthorized systems +/// \details +/// \ingroup PLUGINS_GROUP + +/// \brief Assigns systems to FilterSets. Each FilterSet limits what kinds of messages are allowed. +/// \details The MessageFilter plugin is used for security where you limit what systems can send what kind of messages.
+/// You implicitly define FilterSets, and add allowed message IDs to these FilterSets.
+/// You then add systems to these filters, such that those systems are limited to sending what the filters allows.
+/// You can automatically assign systems to a filter.
+/// You can automatically kick and possibly ban users that stay in a filter too long, or send the wrong message.
+/// Each system is a member of either zero or one filters.
+/// Add this plugin before any plugin you wish to filter (most likely just add this plugin before any other). +/// \ingroup MESSAGEFILTER_GROUP +class RAK_DLL_EXPORT MessageFilter : public PluginInterface2 +{ +public: + + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(MessageFilter) + + MessageFilter(); + virtual ~MessageFilter(); + + // -------------------------------------------------------------------------------------------- + // User functions + // -------------------------------------------------------------------------------------------- + + /// Automatically add all new systems to a particular filter + /// Defaults to -1 + /// \param[in] filterSetID Which filter to add new systems to. <0 for do not add. + void SetAutoAddNewConnectionsToFilter(int filterSetID); + + /// Allow a range of message IDs + /// Always allowed by default: ID_CONNECTION_REQUEST_ACCEPTED through ID_DOWNLOAD_PROGRESS + /// Usually you specify a range to make it easier to add new enumerations without having to constantly refer back to this function. + /// \param[in] allow True to allow this message ID, false to disallow. By default, all messageIDs except the noted types are disallowed. This includes messages from other plugins! + /// \param[in] messageIDStart The first ID_* message to allow in the range. Inclusive. + /// \param[in] messageIDEnd The last ID_* message to allow in the range. Inclusive. + /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. + void SetAllowMessageID(bool allow, int messageIDStart, int messageIDEnd,int filterSetID); + + /// Allow a specific RPC4 call + /// \pre MessageFilter must be attached before RPC4 + /// \param[in] uniqueID Identifier passed to RegisterFunction() + /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. + void SetAllowRPC4(bool allow, const char* uniqueID, int filterSetID); + + /// What action to take on a disallowed message. You can kick or not. You can add them to the ban list for some time + /// By default no action is taken. The message is simply ignored. + /// param[in] 0 for permanent ban, >0 for ban time in milliseconds. + /// \param[in] kickOnDisallowed kick the system that sent a disallowed message. + /// \param[in] banOnDisallowed ban the system that sent a disallowed message. See \a banTimeMS for the ban duration + /// \param[in] banTimeMS Passed to the milliseconds parameter of RakPeer::AddToBanList. + /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. + void SetActionOnDisallowedMessage(bool kickOnDisallowed, bool banOnDisallowed, RakNet::TimeMS banTimeMS, int filterSetID); + + /// Set a user callback to be called on an invalid message for a particular filterSet + /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. + /// \param[in] userData A pointer passed with the callback + /// \param[in] invalidMessageCallback A pointer to a C function to be called back with the specified parameters. + void SetDisallowedMessageCallback(int filterSetID, void *userData, void (*invalidMessageCallback)(RakPeerInterface *peer, AddressOrGUID addressOrGUID, int filterSetID, void *userData, unsigned char messageID)); + + /// Set a user callback to be called when a user is disconnected due to SetFilterMaxTime + /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. + /// \param[in] userData A pointer passed with the callback + /// \param[in] invalidMessageCallback A pointer to a C function to be called back with the specified parameters. + void SetTimeoutCallback(int filterSetID, void *userData, void (*invalidMessageCallback)(RakPeerInterface *peer, AddressOrGUID addressOrGUID, int filterSetID, void *userData)); + + /// Limit how long a connection can stay in a particular filterSetID. After this time, the connection is kicked and possibly banned. + /// By default there is no limit to how long a connection can stay in a particular filter set. + /// \param[in] allowedTimeMS How many milliseconds to allow a connection to stay in this filter set. + /// \param[in] banOnExceed True or false to ban the system, or not, when \a allowedTimeMS is exceeded + /// \param[in] banTimeMS Passed to the milliseconds parameter of RakPeer::AddToBanList. + /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. + void SetFilterMaxTime(int allowedTimeMS, bool banOnExceed, RakNet::TimeMS banTimeMS, int filterSetID); + + /// Get the filterSetID a system is using. Returns -1 for none. + /// \param[in] addressOrGUID The system we are referring to + int GetSystemFilterSet(AddressOrGUID addressOrGUID); + + /// Assign a system to a filter set. + /// Systems are automatically added to filter sets (or not) based on SetAutoAddNewConnectionsToFilter() + /// This function is used to change the filter set a system is using, to add it to a new filter set, or to remove it from all existin filter sets. + /// \param[in] addressOrGUID The system we are referring to + /// \param[in] filterSetID A user defined ID to represent a filter set. If no filter with this ID exists, one will be created with default settings. If -1, the system will be removed from all filter sets. + void SetSystemFilterSet(AddressOrGUID addressOrGUID, int filterSetID); + + /// Returns the number of systems subscribed to a particular filter set + /// Using anything other than -1 for \a filterSetID is slow, so you should store the returned value. + /// \param[in] filterSetID The filter set to limit to. Use -1 for none (just returns the total number of filter systems in that case). + unsigned GetSystemCount(int filterSetID) const; + + /// Returns the total number of filter sets. + /// \return The total number of filter sets. + unsigned GetFilterSetCount(void) const; + + /// Returns the ID of a filter set, by index + /// \param[in] An index between 0 and GetFilterSetCount()-1 inclusive + int GetFilterSetIDByIndex(unsigned index); + + /// Delete a FilterSet. All systems formerly subscribed to this filter are now unrestricted. + /// \param[in] filterSetID The ID of the filter set to delete. + void DeleteFilterSet(int filterSetID); + + // -------------------------------------------------------------------------------------------- + // Packet handling functions + // -------------------------------------------------------------------------------------------- + virtual void Update(void); + virtual PluginReceiveResult OnReceive(Packet *packet); + virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + +protected: + + void Clear(void); + void DeallocateFilterSet(FilterSet *filterSet); + FilterSet* GetFilterSetByID(int filterSetID); + void OnInvalidMessage(FilterSet *filterSet, AddressOrGUID systemAddress, unsigned char messageID); + + DataStructures::OrderedList filterList; + // Change to guid + DataStructures::Hash systemList; + + int autoAddNewConnectionsToFilter; + RakNet::Time whenLastTimeoutCheck; +}; + +} // namespace RakNet + +#endif + diff --git a/Source/MessageIdentifiers.h b/Source/MessageIdentifiers.h index 43e81a912..4db524b05 100644 --- a/Source/MessageIdentifiers.h +++ b/Source/MessageIdentifiers.h @@ -1,437 +1,435 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief All the message identifiers used by RakNet. Message identifiers comprise the first byte of any message. -/// - - -#ifndef __MESSAGE_IDENTIFIERS_H -#define __MESSAGE_IDENTIFIERS_H - -#if defined(RAKNET_USE_CUSTOM_PACKET_IDS) -#include "CustomPacketIdentifiers.h" -#else - -enum OutOfBandIdentifiers -{ - ID_NAT_ESTABLISH_UNIDIRECTIONAL, - ID_NAT_ESTABLISH_BIDIRECTIONAL, - ID_NAT_TYPE_DETECT, - ID_ROUTER_2_REPLY_TO_SENDER_PORT, - ID_ROUTER_2_REPLY_TO_SPECIFIED_PORT, - ID_ROUTER_2_MINI_PUNCH_REPLY, - ID_ROUTER_2_MINI_PUNCH_REPLY_BOUNCE, - ID_XBOX_360_VOICE, - ID_XBOX_360_GET_NETWORK_ROOM, - ID_XBOX_360_RETURN_NETWORK_ROOM, - ID_NAT_PING, - ID_NAT_PONG, -}; - -/// You should not edit the file MessageIdentifiers.h as it is a part of RakNet static library -/// To define your own message id, define an enum following the code example that follows. -/// -/// \code -/// enum { -/// ID_MYPROJECT_MSG_1 = ID_USER_PACKET_ENUM, -/// ID_MYPROJECT_MSG_2, -/// ... -/// }; -/// \endcode -/// -/// \note All these enumerations should be casted to (unsigned char) before writing them to RakNet::BitStream -enum DefaultMessageIDTypes -{ - // - // RESERVED TYPES - DO NOT CHANGE THESE - // All types from RakPeer - // - /// These types are never returned to the user. - /// Ping from a connected system. Update timestamps (internal use only) - ID_CONNECTED_PING, - /// Ping from an unconnected system. Reply but do not update timestamps. (internal use only) - ID_UNCONNECTED_PING, - /// Ping from an unconnected system. Only reply if we have open connections. Do not update timestamps. (internal use only) - ID_UNCONNECTED_PING_OPEN_CONNECTIONS, - /// Pong from a connected system. Update timestamps (internal use only) - ID_CONNECTED_PONG, - /// A reliable packet to detect lost connections (internal use only) - ID_DETECT_LOST_CONNECTIONS, - /// C2S: Initial query: Header(1), OfflineMesageID(16), Protocol number(1), Pad(toMTU), sent with no fragment set. - /// If protocol fails on server, returns ID_INCOMPATIBLE_PROTOCOL_VERSION to client - ID_OPEN_CONNECTION_REQUEST_1, - /// S2C: Header(1), OfflineMesageID(16), server GUID(8), HasSecurity(1), Cookie(4, if HasSecurity) - /// , public key (if do security is true), MTU(2). If public key fails on client, returns ID_PUBLIC_KEY_MISMATCH - ID_OPEN_CONNECTION_REPLY_1, - /// C2S: Header(1), OfflineMesageID(16), Cookie(4, if HasSecurity is true on the server), clientSupportsSecurity(1 bit), - /// handshakeChallenge (if has security on both server and client), remoteBindingAddress(6), MTU(2), client GUID(8) - /// Connection slot allocated if cookie is valid, server is not full, GUID and IP not already in use. - ID_OPEN_CONNECTION_REQUEST_2, - /// S2C: Header(1), OfflineMesageID(16), server GUID(8), mtu(2), doSecurity(1 bit), handshakeAnswer (if do security is true) - ID_OPEN_CONNECTION_REPLY_2, - /// C2S: Header(1), GUID(8), Timestamp, HasSecurity(1), Proof(32) - ID_CONNECTION_REQUEST, - /// RakPeer - Remote system requires secure connections, pass a public key to RakPeerInterface::Connect() - ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY, - /// RakPeer - We passed a public key to RakPeerInterface::Connect(), but the other system did not have security turned on - ID_OUR_SYSTEM_REQUIRES_SECURITY, - /// RakPeer - Wrong public key passed to RakPeerInterface::Connect() - ID_PUBLIC_KEY_MISMATCH, - /// RakPeer - Same as ID_ADVERTISE_SYSTEM, but intended for internal use rather than being passed to the user. - /// Second byte indicates type. Used currently for NAT punchthrough for receiver port advertisement. See ID_NAT_ADVERTISE_RECIPIENT_PORT - ID_OUT_OF_BAND_INTERNAL, - /// If RakPeerInterface::Send() is called where PacketReliability contains _WITH_ACK_RECEIPT, then on a later call to - /// RakPeerInterface::Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS. The message will be 5 bytes long, - /// and bytes 1-4 inclusive will contain a number in native order containing a number that identifies this message. - /// This number will be returned by RakPeerInterface::Send() or RakPeerInterface::SendList(). ID_SND_RECEIPT_ACKED means that - /// the message arrived - ID_SND_RECEIPT_ACKED, - /// If RakPeerInterface::Send() is called where PacketReliability contains UNRELIABLE_WITH_ACK_RECEIPT, then on a later call to - /// RakPeerInterface::Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS. The message will be 5 bytes long, - /// and bytes 1-4 inclusive will contain a number in native order containing a number that identifies this message. This number - /// will be returned by RakPeerInterface::Send() or RakPeerInterface::SendList(). ID_SND_RECEIPT_LOSS means that an ack for the - /// message did not arrive (it may or may not have been delivered, probably not). On disconnect or shutdown, you will not get - /// ID_SND_RECEIPT_LOSS for unsent messages, you should consider those messages as all lost. - ID_SND_RECEIPT_LOSS, - - - // - // USER TYPES - DO NOT CHANGE THESE - // - - /// RakPeer - In a client/server environment, our connection request to the server has been accepted. - ID_CONNECTION_REQUEST_ACCEPTED, - /// RakPeer - Sent to the player when a connection request cannot be completed due to inability to connect. - ID_CONNECTION_ATTEMPT_FAILED, - /// RakPeer - Sent a connect request to a system we are currently connected to. - ID_ALREADY_CONNECTED, - /// RakPeer - A remote system has successfully connected. - ID_NEW_INCOMING_CONNECTION, - /// RakPeer - The system we attempted to connect to is not accepting new connections. - ID_NO_FREE_INCOMING_CONNECTIONS, - /// RakPeer - The system specified in Packet::systemAddress has disconnected from us. For the client, this would mean the - /// server has shutdown. - ID_DISCONNECTION_NOTIFICATION, - /// RakPeer - Reliable packets cannot be delivered to the system specified in Packet::systemAddress. The connection to that - /// system has been closed. - ID_CONNECTION_LOST, - /// RakPeer - We are banned from the system we attempted to connect to. - ID_CONNECTION_BANNED, - /// RakPeer - The remote system is using a password and has refused our connection because we did not set the correct password. - ID_INVALID_PASSWORD, - // RAKNET_PROTOCOL_VERSION in RakNetVersion.h does not match on the remote system what we have on our system - // This means the two systems cannot communicate. - // The 2nd byte of the message contains the value of RAKNET_PROTOCOL_VERSION for the remote system - ID_INCOMPATIBLE_PROTOCOL_VERSION, - // Means that this IP address connected recently, and can't connect again as a security measure. See - /// RakPeer::SetLimitIPConnectionFrequency() - ID_IP_RECENTLY_CONNECTED, - /// RakPeer - The sizeof(RakNetTime) bytes following this byte represent a value which is automatically modified by the difference - /// in system times between the sender and the recipient. Requires that you call SetOccasionalPing. - ID_TIMESTAMP, - /// RakPeer - Pong from an unconnected system. First byte is ID_UNCONNECTED_PONG, second sizeof(RakNet::TimeMS) bytes is the ping, - /// following bytes is system specific enumeration data. - /// Read using bitstreams - ID_UNCONNECTED_PONG, - /// RakPeer - Inform a remote system of our IP/Port. On the recipient, all data past ID_ADVERTISE_SYSTEM is whatever was passed to - /// the data parameter - ID_ADVERTISE_SYSTEM, - // RakPeer - Downloading a large message. Format is ID_DOWNLOAD_PROGRESS (MessageID), partCount (unsigned int), - /// partTotal (unsigned int), - /// partLength (unsigned int), first part data (length <= MAX_MTU_SIZE). See the three parameters partCount, partTotal - /// and partLength in OnFileProgress in FileListTransferCBInterface.h - ID_DOWNLOAD_PROGRESS, - - /// ConnectionGraph2 plugin - In a client/server environment, a client other than ourselves has disconnected gracefully. - /// Packet::systemAddress is modified to reflect the systemAddress of this client. - ID_REMOTE_DISCONNECTION_NOTIFICATION, - /// ConnectionGraph2 plugin - In a client/server environment, a client other than ourselves has been forcefully dropped. - /// Packet::systemAddress is modified to reflect the systemAddress of this client. - ID_REMOTE_CONNECTION_LOST, - /// ConnectionGraph2 plugin: Bytes 1-4 = count. for (count items) contains {SystemAddress, RakNetGUID, 2 byte ping} - ID_REMOTE_NEW_INCOMING_CONNECTION, - - /// FileListTransfer plugin - Setup data - ID_FILE_LIST_TRANSFER_HEADER, - /// FileListTransfer plugin - A file - ID_FILE_LIST_TRANSFER_FILE, - // Ack for reference push, to send more of the file - ID_FILE_LIST_REFERENCE_PUSH_ACK, - - /// DirectoryDeltaTransfer plugin - Request from a remote system for a download of a directory - ID_DDT_DOWNLOAD_REQUEST, - - /// RakNetTransport plugin - Transport provider message, used for remote console - ID_TRANSPORT_STRING, - - /// ReplicaManager plugin - Create an object - ID_REPLICA_MANAGER_CONSTRUCTION, - /// ReplicaManager plugin - Changed scope of an object - ID_REPLICA_MANAGER_SCOPE_CHANGE, - /// ReplicaManager plugin - Serialized data of an object - ID_REPLICA_MANAGER_SERIALIZE, - /// ReplicaManager plugin - New connection, about to send all world objects - ID_REPLICA_MANAGER_DOWNLOAD_STARTED, - /// ReplicaManager plugin - Finished downloading all serialized objects - ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE, - - /// RakVoice plugin - Open a communication channel - ID_RAKVOICE_OPEN_CHANNEL_REQUEST, - /// RakVoice plugin - Communication channel accepted - ID_RAKVOICE_OPEN_CHANNEL_REPLY, - /// RakVoice plugin - Close a communication channel - ID_RAKVOICE_CLOSE_CHANNEL, - /// RakVoice plugin - Voice data - ID_RAKVOICE_DATA, - - /// Autopatcher plugin - Get a list of files that have changed since a certain date - ID_AUTOPATCHER_GET_CHANGELIST_SINCE_DATE, - /// Autopatcher plugin - A list of files to create - ID_AUTOPATCHER_CREATION_LIST, - /// Autopatcher plugin - A list of files to delete - ID_AUTOPATCHER_DELETION_LIST, - /// Autopatcher plugin - A list of files to get patches for - ID_AUTOPATCHER_GET_PATCH, - /// Autopatcher plugin - A list of patches for a list of files - ID_AUTOPATCHER_PATCH_LIST, - /// Autopatcher plugin - Returned to the user: An error from the database repository for the autopatcher. - ID_AUTOPATCHER_REPOSITORY_FATAL_ERROR, - /// Autopatcher plugin - Returned to the user: The server does not allow downloading unmodified game files. - ID_AUTOPATCHER_CANNOT_DOWNLOAD_ORIGINAL_UNMODIFIED_FILES, - /// Autopatcher plugin - Finished getting all files from the autopatcher - ID_AUTOPATCHER_FINISHED_INTERNAL, - ID_AUTOPATCHER_FINISHED, - /// Autopatcher plugin - Returned to the user: You must restart the application to finish patching. - ID_AUTOPATCHER_RESTART_APPLICATION, - - /// NATPunchthrough plugin: internal - ID_NAT_PUNCHTHROUGH_REQUEST, - /// NATPunchthrough plugin: internal - //ID_NAT_GROUP_PUNCHTHROUGH_REQUEST, - /// NATPunchthrough plugin: internal - //ID_NAT_GROUP_PUNCHTHROUGH_REPLY, - /// NATPunchthrough plugin: internal - ID_NAT_CONNECT_AT_TIME, - /// NATPunchthrough plugin: internal - ID_NAT_GET_MOST_RECENT_PORT, - /// NATPunchthrough plugin: internal - ID_NAT_CLIENT_READY, - /// NATPunchthrough plugin: internal - //ID_NAT_GROUP_PUNCHTHROUGH_FAILURE_NOTIFICATION, - - /// NATPunchthrough plugin: Destination system is not connected to the server. Bytes starting at offset 1 contains the - /// RakNetGUID destination field of NatPunchthroughClient::OpenNAT(). - ID_NAT_TARGET_NOT_CONNECTED, - /// NATPunchthrough plugin: Destination system is not responding to ID_NAT_GET_MOST_RECENT_PORT. Possibly the plugin is not installed. - /// Bytes starting at offset 1 contains the RakNetGUID destination field of NatPunchthroughClient::OpenNAT(). - ID_NAT_TARGET_UNRESPONSIVE, - /// NATPunchthrough plugin: The server lost the connection to the destination system while setting up punchthrough. - /// Possibly the plugin is not installed. Bytes starting at offset 1 contains the RakNetGUID destination - /// field of NatPunchthroughClient::OpenNAT(). - ID_NAT_CONNECTION_TO_TARGET_LOST, - /// NATPunchthrough plugin: This punchthrough is already in progress. Possibly the plugin is not installed. - /// Bytes starting at offset 1 contains the RakNetGUID destination field of NatPunchthroughClient::OpenNAT(). - ID_NAT_ALREADY_IN_PROGRESS, - /// NATPunchthrough plugin: This message is generated on the local system, and does not come from the network. - /// packet::guid contains the destination field of NatPunchthroughClient::OpenNAT(). Byte 1 contains 1 if you are the sender, 0 if not - ID_NAT_PUNCHTHROUGH_FAILED, - /// NATPunchthrough plugin: Punchthrough succeeded. See packet::systemAddress and packet::guid. Byte 1 contains 1 if you are the sender, - /// 0 if not. You can now use RakPeer::Connect() or other calls to communicate with this system. - ID_NAT_PUNCHTHROUGH_SUCCEEDED, - - /// ReadyEvent plugin - Set the ready state for a particular system - /// First 4 bytes after the message contains the id - ID_READY_EVENT_SET, - /// ReadyEvent plugin - Unset the ready state for a particular system - /// First 4 bytes after the message contains the id - ID_READY_EVENT_UNSET, - /// All systems are in state ID_READY_EVENT_SET - /// First 4 bytes after the message contains the id - ID_READY_EVENT_ALL_SET, - /// \internal, do not process in your game - /// ReadyEvent plugin - Request of ready event state - used for pulling data when newly connecting - ID_READY_EVENT_QUERY, - - /// Lobby packets. Second byte indicates type. - ID_LOBBY_GENERAL, - - // RPC3, RPC4 error - ID_RPC_REMOTE_ERROR, - /// Plugin based replacement for RPC system - ID_RPC_PLUGIN, - - /// FileListTransfer transferring large files in chunks that are read only when needed, to save memory - ID_FILE_LIST_REFERENCE_PUSH, - /// Force the ready event to all set - ID_READY_EVENT_FORCE_ALL_SET, - - /// Rooms function - ID_ROOMS_EXECUTE_FUNC, - ID_ROOMS_LOGON_STATUS, - ID_ROOMS_HANDLE_CHANGE, - - /// Lobby2 message - ID_LOBBY2_SEND_MESSAGE, - ID_LOBBY2_SERVER_ERROR, - - /// Informs user of a new host GUID. Packet::Guid contains this new host RakNetGuid. The old host can be read out using BitStream->Read(RakNetGuid) starting on byte 1 - /// This is not returned until connected to a remote system - /// If the oldHost is UNASSIGNED_RAKNET_GUID, then this is the first time the host has been determined - ID_FCM2_NEW_HOST, - /// \internal For FullyConnectedMesh2 plugin - ID_FCM2_REQUEST_FCMGUID, - /// \internal For FullyConnectedMesh2 plugin - ID_FCM2_RESPOND_CONNECTION_COUNT, - /// \internal For FullyConnectedMesh2 plugin - ID_FCM2_INFORM_FCMGUID, - /// \internal For FullyConnectedMesh2 plugin - ID_FCM2_UPDATE_MIN_TOTAL_CONNECTION_COUNT, - /// A remote system (not necessarily the host) called FullyConnectedMesh2::StartVerifiedJoin() with our system as the client - /// Use FullyConnectedMesh2::GetVerifiedJoinRequiredProcessingList() to read systems - /// For each system, attempt NatPunchthroughClient::OpenNAT() and/or RakPeerInterface::Connect() - /// When this has been done for all systems, the remote system will automatically be informed of the results - /// \note Only the designated client gets this message - /// \note You won't get this message if you are already connected to all target systems - /// \note If you fail to connect to a system, this does not automatically mean you will get ID_FCM2_VERIFIED_JOIN_FAILED as that system may have been shutting down from the host too - /// \sa FullyConnectedMesh2::StartVerifiedJoin() - ID_FCM2_VERIFIED_JOIN_START, - /// \internal The client has completed processing for all systems designated in ID_FCM2_VERIFIED_JOIN_START - ID_FCM2_VERIFIED_JOIN_CAPABLE, - /// Client failed to connect to a required systems notified via FullyConnectedMesh2::StartVerifiedJoin() - /// RakPeerInterface::CloseConnection() was automatically called for all systems connected due to ID_FCM2_VERIFIED_JOIN_START - /// Programmer should inform the player via the UI that they cannot join this session, and to choose a different session - /// \note Server normally sends us this message, however if connection to the server was lost, message will be returned locally - /// \note Only the designated client gets this message - ID_FCM2_VERIFIED_JOIN_FAILED, - /// The system that called StartVerifiedJoin() got ID_FCM2_VERIFIED_JOIN_CAPABLE from the client and then called RespondOnVerifiedJoinCapable() with true - /// AddParticipant() has automatically been called for this system - /// Use GetVerifiedJoinAcceptedAdditionalData() to read any additional data passed to RespondOnVerifiedJoinCapable() - /// \note All systems in the mesh get this message - /// \sa RespondOnVerifiedJoinCapable() - ID_FCM2_VERIFIED_JOIN_ACCEPTED, - /// The system that called StartVerifiedJoin() got ID_FCM2_VERIFIED_JOIN_CAPABLE from the client and then called RespondOnVerifiedJoinCapable() with false - /// CloseConnection() has been automatically called for each system connected to since ID_FCM2_VERIFIED_JOIN_START. - /// The connection is NOT automatically closed to the original host that sent StartVerifiedJoin() - /// Use GetVerifiedJoinRejectedAdditionalData() to read any additional data passed to RespondOnVerifiedJoinCapable() - /// \note Only the designated client gets this message - /// \sa RespondOnVerifiedJoinCapable() - ID_FCM2_VERIFIED_JOIN_REJECTED, - - /// UDP proxy messages. Second byte indicates type. - ID_UDP_PROXY_GENERAL, - - /// SQLite3Plugin - execute - ID_SQLite3_EXEC, - /// SQLite3Plugin - Remote database is unknown - ID_SQLite3_UNKNOWN_DB, - /// Events happening with SQLiteClientLoggerPlugin - ID_SQLLITE_LOGGER, - - /// Sent to NatTypeDetectionServer - ID_NAT_TYPE_DETECTION_REQUEST, - /// Sent to NatTypeDetectionClient. Byte 1 contains the type of NAT detected. - ID_NAT_TYPE_DETECTION_RESULT, - - /// Used by the router2 plugin - ID_ROUTER_2_INTERNAL, - /// No path is available or can be established to the remote system - /// Packet::guid contains the endpoint guid that we were trying to reach - ID_ROUTER_2_FORWARDING_NO_PATH, - /// \brief You can now call connect, ping, or other operations to the destination system. - /// - /// Connect as follows: - /// - /// RakNet::BitStream bs(packet->data, packet->length, false); - /// bs.IgnoreBytes(sizeof(MessageID)); - /// RakNetGUID endpointGuid; - /// bs.Read(endpointGuid); - /// unsigned short sourceToDestPort; - /// bs.Read(sourceToDestPort); - /// char ipAddressString[32]; - /// packet->systemAddress.ToString(false, ipAddressString); - /// rakPeerInterface->Connect(ipAddressString, sourceToDestPort, 0,0); - ID_ROUTER_2_FORWARDING_ESTABLISHED, - /// The IP address for a forwarded connection has changed - /// Read endpointGuid and port as per ID_ROUTER_2_FORWARDING_ESTABLISHED - ID_ROUTER_2_REROUTED, - - /// \internal Used by the team balancer plugin - ID_TEAM_BALANCER_INTERNAL, - /// Cannot switch to the desired team because it is full. However, if someone on that team leaves, you will - /// get ID_TEAM_BALANCER_TEAM_ASSIGNED later. - /// For TeamBalancer: Byte 1 contains the team you requested to join. Following bytes contain NetworkID of which member - ID_TEAM_BALANCER_REQUESTED_TEAM_FULL, - /// Cannot switch to the desired team because all teams are locked. However, if someone on that team leaves, - /// you will get ID_TEAM_BALANCER_SET_TEAM later. - /// For TeamBalancer: Byte 1 contains the team you requested to join. - ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED, - ID_TEAM_BALANCER_TEAM_REQUESTED_CANCELLED, - /// Team balancer plugin informing you of your team. Byte 1 contains the team you requested to join. Following bytes contain NetworkID of which member. - ID_TEAM_BALANCER_TEAM_ASSIGNED, - - /// Gamebryo Lightspeed integration - ID_LIGHTSPEED_INTEGRATION, - - /// XBOX integration - ID_XBOX_LOBBY, - - /// The password we used to challenge the other system passed, meaning the other system has called TwoWayAuthentication::AddPassword() with the same password we passed to TwoWayAuthentication::Challenge() - /// You can read the identifier used to challenge as follows: - /// RakNet::BitStream bs(packet->data, packet->length, false); bs.IgnoreBytes(sizeof(RakNet::MessageID)); RakNet::RakString password; bs.Read(password); - ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_SUCCESS, - ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_SUCCESS, - /// A remote system sent us a challenge using TwoWayAuthentication::Challenge(), and the challenge failed. - /// If the other system must pass the challenge to stay connected, you should call RakPeer::CloseConnection() to terminate the connection to the other system. - ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_FAILURE, - /// The other system did not add the password we used to TwoWayAuthentication::AddPassword() - /// You can read the identifier used to challenge as follows: - /// RakNet::BitStream bs(packet->data, packet->length, false); bs.IgnoreBytes(sizeof(MessageID)); RakNet::RakString password; bs.Read(password); - ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_FAILURE, - /// The other system did not respond within a timeout threshhold. Either the other system is not running the plugin or the other system was blocking on some operation for a long time. - /// You can read the identifier used to challenge as follows: - /// RakNet::BitStream bs(packet->data, packet->length, false); bs.IgnoreBytes(sizeof(MessageID)); RakNet::RakString password; bs.Read(password); - ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_TIMEOUT, - /// \internal - ID_TWO_WAY_AUTHENTICATION_NEGOTIATION, - - /// CloudClient / CloudServer - ID_CLOUD_POST_REQUEST, - ID_CLOUD_RELEASE_REQUEST, - ID_CLOUD_GET_REQUEST, - ID_CLOUD_GET_RESPONSE, - ID_CLOUD_UNSUBSCRIBE_REQUEST, - ID_CLOUD_SERVER_TO_SERVER_COMMAND, - ID_CLOUD_SUBSCRIPTION_NOTIFICATION, - - // LibVoice - ID_LIB_VOICE, - - ID_RELAY_PLUGIN, - ID_NAT_REQUEST_BOUND_ADDRESSES, - ID_NAT_RESPOND_BOUND_ADDRESSES, - ID_FCM2_UPDATE_USER_CONTEXT, - ID_RESERVED_3, - ID_RESERVED_4, - ID_RESERVED_5, - ID_RESERVED_6, - ID_RESERVED_7, - ID_RESERVED_8, - ID_RESERVED_9, - - // For the user to use. Start your first enumeration at this value. - ID_USER_PACKET_ENUM - //------------------------------------------------------------------------------------------------------------- - -}; - -#endif // RAKNET_USE_CUSTOM_PACKET_IDS - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief All the message identifiers used by RakNet. Message identifiers comprise the first byte of any message. +/// + + +#pragma once + +#if defined(RAKNET_USE_CUSTOM_PACKET_IDS) +#include "CustomPacketIdentifiers.h" +#else + +enum OutOfBandIdentifiers +{ + ID_NAT_ESTABLISH_UNIDIRECTIONAL, + ID_NAT_ESTABLISH_BIDIRECTIONAL, + ID_NAT_TYPE_DETECT, + ID_ROUTER_2_REPLY_TO_SENDER_PORT, + ID_ROUTER_2_REPLY_TO_SPECIFIED_PORT, + ID_ROUTER_2_MINI_PUNCH_REPLY, + ID_ROUTER_2_MINI_PUNCH_REPLY_BOUNCE, + ID_XBOX_360_VOICE, + ID_XBOX_360_GET_NETWORK_ROOM, + ID_XBOX_360_RETURN_NETWORK_ROOM, + ID_NAT_PING, + ID_NAT_PONG, +}; + +/// You should not edit the file MessageIdentifiers.h as it is a part of RakNet static library +/// To define your own message id, define an enum following the code example that follows. +/// +/// \code +/// enum { +/// ID_MYPROJECT_MSG_1 = ID_USER_PACKET_ENUM, +/// ID_MYPROJECT_MSG_2, +/// ... +/// }; +/// \endcode +/// +/// \note All these enumerations should be casted to (unsigned char) before writing them to RakNet::BitStream +enum DefaultMessageIDTypes +{ + // + // RESERVED TYPES - DO NOT CHANGE THESE + // All types from RakPeer + // + /// These types are never returned to the user. + /// Ping from a connected system. Update timestamps (internal use only) + ID_CONNECTED_PING, + /// Ping from an unconnected system. Reply but do not update timestamps. (internal use only) + ID_UNCONNECTED_PING, + /// Ping from an unconnected system. Only reply if we have open connections. Do not update timestamps. (internal use only) + ID_UNCONNECTED_PING_OPEN_CONNECTIONS, + /// Pong from a connected system. Update timestamps (internal use only) + ID_CONNECTED_PONG, + /// A reliable packet to detect lost connections (internal use only) + ID_DETECT_LOST_CONNECTIONS, + /// C2S: Initial query: Header(1), OfflineMesageID(16), Protocol number(1), Pad(toMTU), sent with no fragment set. + /// If protocol fails on server, returns ID_INCOMPATIBLE_PROTOCOL_VERSION to client + ID_OPEN_CONNECTION_REQUEST_1, + /// S2C: Header(1), OfflineMesageID(16), server GUID(8), HasSecurity(1), Cookie(4, if HasSecurity) + /// , public key (if do security is true), MTU(2). If public key fails on client, returns ID_PUBLIC_KEY_MISMATCH + ID_OPEN_CONNECTION_REPLY_1, + /// C2S: Header(1), OfflineMesageID(16), Cookie(4, if HasSecurity is true on the server), clientSupportsSecurity(1 bit), + /// handshakeChallenge (if has security on both server and client), remoteBindingAddress(6), MTU(2), client GUID(8) + /// Connection slot allocated if cookie is valid, server is not full, GUID and IP not already in use. + ID_OPEN_CONNECTION_REQUEST_2, + /// S2C: Header(1), OfflineMesageID(16), server GUID(8), mtu(2), doSecurity(1 bit), handshakeAnswer (if do security is true) + ID_OPEN_CONNECTION_REPLY_2, + /// C2S: Header(1), GUID(8), Timestamp, HasSecurity(1), Proof(32) + ID_CONNECTION_REQUEST, + /// RakPeer - Remote system requires secure connections, pass a public key to RakPeerInterface::Connect() + ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY, + /// RakPeer - We passed a public key to RakPeerInterface::Connect(), but the other system did not have security turned on + ID_OUR_SYSTEM_REQUIRES_SECURITY, + /// RakPeer - Wrong public key passed to RakPeerInterface::Connect() + ID_PUBLIC_KEY_MISMATCH, + /// RakPeer - Same as ID_ADVERTISE_SYSTEM, but intended for internal use rather than being passed to the user. + /// Second byte indicates type. Used currently for NAT punchthrough for receiver port advertisement. See ID_NAT_ADVERTISE_RECIPIENT_PORT + ID_OUT_OF_BAND_INTERNAL, + /// If RakPeerInterface::Send() is called where PacketReliability contains _WITH_ACK_RECEIPT, then on a later call to + /// RakPeerInterface::Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS. The message will be 5 bytes long, + /// and bytes 1-4 inclusive will contain a number in native order containing a number that identifies this message. + /// This number will be returned by RakPeerInterface::Send() or RakPeerInterface::SendList(). ID_SND_RECEIPT_ACKED means that + /// the message arrived + ID_SND_RECEIPT_ACKED, + /// If RakPeerInterface::Send() is called where PacketReliability contains UNRELIABLE_WITH_ACK_RECEIPT, then on a later call to + /// RakPeerInterface::Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS. The message will be 5 bytes long, + /// and bytes 1-4 inclusive will contain a number in native order containing a number that identifies this message. This number + /// will be returned by RakPeerInterface::Send() or RakPeerInterface::SendList(). ID_SND_RECEIPT_LOSS means that an ack for the + /// message did not arrive (it may or may not have been delivered, probably not). On disconnect or shutdown, you will not get + /// ID_SND_RECEIPT_LOSS for unsent messages, you should consider those messages as all lost. + ID_SND_RECEIPT_LOSS, + + + // + // USER TYPES - DO NOT CHANGE THESE + // + + /// RakPeer - In a client/server environment, our connection request to the server has been accepted. + ID_CONNECTION_REQUEST_ACCEPTED, + /// RakPeer - Sent to the player when a connection request cannot be completed due to inability to connect. + ID_CONNECTION_ATTEMPT_FAILED, + /// RakPeer - Sent a connect request to a system we are currently connected to. + ID_ALREADY_CONNECTED, + /// RakPeer - A remote system has successfully connected. + ID_NEW_INCOMING_CONNECTION, + /// RakPeer - The system we attempted to connect to is not accepting new connections. + ID_NO_FREE_INCOMING_CONNECTIONS, + /// RakPeer - The system specified in Packet::systemAddress has disconnected from us. For the client, this would mean the + /// server has shutdown. + ID_DISCONNECTION_NOTIFICATION, + /// RakPeer - Reliable packets cannot be delivered to the system specified in Packet::systemAddress. The connection to that + /// system has been closed. + ID_CONNECTION_LOST, + /// RakPeer - We are banned from the system we attempted to connect to. + ID_CONNECTION_BANNED, + /// RakPeer - The remote system is using a password and has refused our connection because we did not set the correct password. + ID_INVALID_PASSWORD, + // RAKNET_PROTOCOL_VERSION in RakNetVersion.h does not match on the remote system what we have on our system + // This means the two systems cannot communicate. + // The 2nd byte of the message contains the value of RAKNET_PROTOCOL_VERSION for the remote system + ID_INCOMPATIBLE_PROTOCOL_VERSION, + // Means that this IP address connected recently, and can't connect again as a security measure. See + /// RakPeer::SetLimitIPConnectionFrequency() + ID_IP_RECENTLY_CONNECTED, + /// RakPeer - The sizeof(RakNetTime) bytes following this byte represent a value which is automatically modified by the difference + /// in system times between the sender and the recipient. Requires that you call SetOccasionalPing. + ID_TIMESTAMP, + /// RakPeer - Pong from an unconnected system. First byte is ID_UNCONNECTED_PONG, second sizeof(RakNet::TimeMS) bytes is the ping, + /// following bytes is system specific enumeration data. + /// Read using bitstreams + ID_UNCONNECTED_PONG, + /// RakPeer - Inform a remote system of our IP/Port. On the recipient, all data past ID_ADVERTISE_SYSTEM is whatever was passed to + /// the data parameter + ID_ADVERTISE_SYSTEM, + // RakPeer - Downloading a large message. Format is ID_DOWNLOAD_PROGRESS (MessageID), partCount (unsigned int), + /// partTotal (unsigned int), + /// partLength (unsigned int), first part data (length <= MAX_MTU_SIZE). See the three parameters partCount, partTotal + /// and partLength in OnFileProgress in FileListTransferCBInterface.h + ID_DOWNLOAD_PROGRESS, + + /// ConnectionGraph2 plugin - In a client/server environment, a client other than ourselves has disconnected gracefully. + /// Packet::systemAddress is modified to reflect the systemAddress of this client. + ID_REMOTE_DISCONNECTION_NOTIFICATION, + /// ConnectionGraph2 plugin - In a client/server environment, a client other than ourselves has been forcefully dropped. + /// Packet::systemAddress is modified to reflect the systemAddress of this client. + ID_REMOTE_CONNECTION_LOST, + /// ConnectionGraph2 plugin: Bytes 1-4 = count. for (count items) contains {SystemAddress, RakNetGUID, 2 byte ping} + ID_REMOTE_NEW_INCOMING_CONNECTION, + + /// FileListTransfer plugin - Setup data + ID_FILE_LIST_TRANSFER_HEADER, + /// FileListTransfer plugin - A file + ID_FILE_LIST_TRANSFER_FILE, + // Ack for reference push, to send more of the file + ID_FILE_LIST_REFERENCE_PUSH_ACK, + + /// DirectoryDeltaTransfer plugin - Request from a remote system for a download of a directory + ID_DDT_DOWNLOAD_REQUEST, + + /// RakNetTransport plugin - Transport provider message, used for remote console + ID_TRANSPORT_STRING, + + /// ReplicaManager plugin - Create an object + ID_REPLICA_MANAGER_CONSTRUCTION, + /// ReplicaManager plugin - Changed scope of an object + ID_REPLICA_MANAGER_SCOPE_CHANGE, + /// ReplicaManager plugin - Serialized data of an object + ID_REPLICA_MANAGER_SERIALIZE, + /// ReplicaManager plugin - New connection, about to send all world objects + ID_REPLICA_MANAGER_DOWNLOAD_STARTED, + /// ReplicaManager plugin - Finished downloading all serialized objects + ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE, + + /// RakVoice plugin - Open a communication channel + ID_RAKVOICE_OPEN_CHANNEL_REQUEST, + /// RakVoice plugin - Communication channel accepted + ID_RAKVOICE_OPEN_CHANNEL_REPLY, + /// RakVoice plugin - Close a communication channel + ID_RAKVOICE_CLOSE_CHANNEL, + /// RakVoice plugin - Voice data + ID_RAKVOICE_DATA, + + /// Autopatcher plugin - Get a list of files that have changed since a certain date + ID_AUTOPATCHER_GET_CHANGELIST_SINCE_DATE, + /// Autopatcher plugin - A list of files to create + ID_AUTOPATCHER_CREATION_LIST, + /// Autopatcher plugin - A list of files to delete + ID_AUTOPATCHER_DELETION_LIST, + /// Autopatcher plugin - A list of files to get patches for + ID_AUTOPATCHER_GET_PATCH, + /// Autopatcher plugin - A list of patches for a list of files + ID_AUTOPATCHER_PATCH_LIST, + /// Autopatcher plugin - Returned to the user: An error from the database repository for the autopatcher. + ID_AUTOPATCHER_REPOSITORY_FATAL_ERROR, + /// Autopatcher plugin - Returned to the user: The server does not allow downloading unmodified game files. + ID_AUTOPATCHER_CANNOT_DOWNLOAD_ORIGINAL_UNMODIFIED_FILES, + /// Autopatcher plugin - Finished getting all files from the autopatcher + ID_AUTOPATCHER_FINISHED_INTERNAL, + ID_AUTOPATCHER_FINISHED, + /// Autopatcher plugin - Returned to the user: You must restart the application to finish patching. + ID_AUTOPATCHER_RESTART_APPLICATION, + + /// NATPunchthrough plugin: internal + ID_NAT_PUNCHTHROUGH_REQUEST, + /// NATPunchthrough plugin: internal + //ID_NAT_GROUP_PUNCHTHROUGH_REQUEST, + /// NATPunchthrough plugin: internal + //ID_NAT_GROUP_PUNCHTHROUGH_REPLY, + /// NATPunchthrough plugin: internal + ID_NAT_CONNECT_AT_TIME, + /// NATPunchthrough plugin: internal + ID_NAT_GET_MOST_RECENT_PORT, + /// NATPunchthrough plugin: internal + ID_NAT_CLIENT_READY, + /// NATPunchthrough plugin: internal + //ID_NAT_GROUP_PUNCHTHROUGH_FAILURE_NOTIFICATION, + + /// NATPunchthrough plugin: Destination system is not connected to the server. Bytes starting at offset 1 contains the + /// RakNetGUID destination field of NatPunchthroughClient::OpenNAT(). + ID_NAT_TARGET_NOT_CONNECTED, + /// NATPunchthrough plugin: Destination system is not responding to ID_NAT_GET_MOST_RECENT_PORT. Possibly the plugin is not installed. + /// Bytes starting at offset 1 contains the RakNetGUID destination field of NatPunchthroughClient::OpenNAT(). + ID_NAT_TARGET_UNRESPONSIVE, + /// NATPunchthrough plugin: The server lost the connection to the destination system while setting up punchthrough. + /// Possibly the plugin is not installed. Bytes starting at offset 1 contains the RakNetGUID destination + /// field of NatPunchthroughClient::OpenNAT(). + ID_NAT_CONNECTION_TO_TARGET_LOST, + /// NATPunchthrough plugin: This punchthrough is already in progress. Possibly the plugin is not installed. + /// Bytes starting at offset 1 contains the RakNetGUID destination field of NatPunchthroughClient::OpenNAT(). + ID_NAT_ALREADY_IN_PROGRESS, + /// NATPunchthrough plugin: This message is generated on the local system, and does not come from the network. + /// packet::guid contains the destination field of NatPunchthroughClient::OpenNAT(). Byte 1 contains 1 if you are the sender, 0 if not + ID_NAT_PUNCHTHROUGH_FAILED, + /// NATPunchthrough plugin: Punchthrough succeeded. See packet::systemAddress and packet::guid. Byte 1 contains 1 if you are the sender, + /// 0 if not. You can now use RakPeer::Connect() or other calls to communicate with this system. + ID_NAT_PUNCHTHROUGH_SUCCEEDED, + + /// ReadyEvent plugin - Set the ready state for a particular system + /// First 4 bytes after the message contains the id + ID_READY_EVENT_SET, + /// ReadyEvent plugin - Unset the ready state for a particular system + /// First 4 bytes after the message contains the id + ID_READY_EVENT_UNSET, + /// All systems are in state ID_READY_EVENT_SET + /// First 4 bytes after the message contains the id + ID_READY_EVENT_ALL_SET, + /// \internal, do not process in your game + /// ReadyEvent plugin - Request of ready event state - used for pulling data when newly connecting + ID_READY_EVENT_QUERY, + + /// Lobby packets. Second byte indicates type. + ID_LOBBY_GENERAL, + + // RPC3, RPC4 error + ID_RPC_REMOTE_ERROR, + /// Plugin based replacement for RPC system + ID_RPC_PLUGIN, + + /// FileListTransfer transferring large files in chunks that are read only when needed, to save memory + ID_FILE_LIST_REFERENCE_PUSH, + /// Force the ready event to all set + ID_READY_EVENT_FORCE_ALL_SET, + + /// Rooms function + ID_ROOMS_EXECUTE_FUNC, + ID_ROOMS_LOGON_STATUS, + ID_ROOMS_HANDLE_CHANGE, + + /// Lobby2 message + ID_LOBBY2_SEND_MESSAGE, + ID_LOBBY2_SERVER_ERROR, + + /// Informs user of a new host GUID. Packet::Guid contains this new host RakNetGuid. The old host can be read out using BitStream->Read(RakNetGuid) starting on byte 1 + /// This is not returned until connected to a remote system + /// If the oldHost is UNASSIGNED_RAKNET_GUID, then this is the first time the host has been determined + ID_FCM2_NEW_HOST, + /// \internal For FullyConnectedMesh2 plugin + ID_FCM2_REQUEST_FCMGUID, + /// \internal For FullyConnectedMesh2 plugin + ID_FCM2_RESPOND_CONNECTION_COUNT, + /// \internal For FullyConnectedMesh2 plugin + ID_FCM2_INFORM_FCMGUID, + /// \internal For FullyConnectedMesh2 plugin + ID_FCM2_UPDATE_MIN_TOTAL_CONNECTION_COUNT, + /// A remote system (not necessarily the host) called FullyConnectedMesh2::StartVerifiedJoin() with our system as the client + /// Use FullyConnectedMesh2::GetVerifiedJoinRequiredProcessingList() to read systems + /// For each system, attempt NatPunchthroughClient::OpenNAT() and/or RakPeerInterface::Connect() + /// When this has been done for all systems, the remote system will automatically be informed of the results + /// \note Only the designated client gets this message + /// \note You won't get this message if you are already connected to all target systems + /// \note If you fail to connect to a system, this does not automatically mean you will get ID_FCM2_VERIFIED_JOIN_FAILED as that system may have been shutting down from the host too + /// \sa FullyConnectedMesh2::StartVerifiedJoin() + ID_FCM2_VERIFIED_JOIN_START, + /// \internal The client has completed processing for all systems designated in ID_FCM2_VERIFIED_JOIN_START + ID_FCM2_VERIFIED_JOIN_CAPABLE, + /// Client failed to connect to a required systems notified via FullyConnectedMesh2::StartVerifiedJoin() + /// RakPeerInterface::CloseConnection() was automatically called for all systems connected due to ID_FCM2_VERIFIED_JOIN_START + /// Programmer should inform the player via the UI that they cannot join this session, and to choose a different session + /// \note Server normally sends us this message, however if connection to the server was lost, message will be returned locally + /// \note Only the designated client gets this message + ID_FCM2_VERIFIED_JOIN_FAILED, + /// The system that called StartVerifiedJoin() got ID_FCM2_VERIFIED_JOIN_CAPABLE from the client and then called RespondOnVerifiedJoinCapable() with true + /// AddParticipant() has automatically been called for this system + /// Use GetVerifiedJoinAcceptedAdditionalData() to read any additional data passed to RespondOnVerifiedJoinCapable() + /// \note All systems in the mesh get this message + /// \sa RespondOnVerifiedJoinCapable() + ID_FCM2_VERIFIED_JOIN_ACCEPTED, + /// The system that called StartVerifiedJoin() got ID_FCM2_VERIFIED_JOIN_CAPABLE from the client and then called RespondOnVerifiedJoinCapable() with false + /// CloseConnection() has been automatically called for each system connected to since ID_FCM2_VERIFIED_JOIN_START. + /// The connection is NOT automatically closed to the original host that sent StartVerifiedJoin() + /// Use GetVerifiedJoinRejectedAdditionalData() to read any additional data passed to RespondOnVerifiedJoinCapable() + /// \note Only the designated client gets this message + /// \sa RespondOnVerifiedJoinCapable() + ID_FCM2_VERIFIED_JOIN_REJECTED, + + /// UDP proxy messages. Second byte indicates type. + ID_UDP_PROXY_GENERAL, + + /// SQLite3Plugin - execute + ID_SQLite3_EXEC, + /// SQLite3Plugin - Remote database is unknown + ID_SQLite3_UNKNOWN_DB, + /// Events happening with SQLiteClientLoggerPlugin + ID_SQLLITE_LOGGER, + + /// Sent to NatTypeDetectionServer + ID_NAT_TYPE_DETECTION_REQUEST, + /// Sent to NatTypeDetectionClient. Byte 1 contains the type of NAT detected. + ID_NAT_TYPE_DETECTION_RESULT, + + /// Used by the router2 plugin + ID_ROUTER_2_INTERNAL, + /// No path is available or can be established to the remote system + /// Packet::guid contains the endpoint guid that we were trying to reach + ID_ROUTER_2_FORWARDING_NO_PATH, + /// \brief You can now call connect, ping, or other operations to the destination system. + /// + /// Connect as follows: + /// + /// RakNet::BitStream bs(packet->data, packet->length, false); + /// bs.IgnoreBytes(sizeof(MessageID)); + /// RakNetGUID endpointGuid; + /// bs.Read(endpointGuid); + /// unsigned short sourceToDestPort; + /// bs.Read(sourceToDestPort); + /// char ipAddressString[32]; + /// packet->systemAddress.ToString(false, ipAddressString); + /// rakPeerInterface->Connect(ipAddressString, sourceToDestPort, 0,0); + ID_ROUTER_2_FORWARDING_ESTABLISHED, + /// The IP address for a forwarded connection has changed + /// Read endpointGuid and port as per ID_ROUTER_2_FORWARDING_ESTABLISHED + ID_ROUTER_2_REROUTED, + + /// \internal Used by the team balancer plugin + ID_TEAM_BALANCER_INTERNAL, + /// Cannot switch to the desired team because it is full. However, if someone on that team leaves, you will + /// get ID_TEAM_BALANCER_TEAM_ASSIGNED later. + /// For TeamBalancer: Byte 1 contains the team you requested to join. Following bytes contain NetworkID of which member + ID_TEAM_BALANCER_REQUESTED_TEAM_FULL, + /// Cannot switch to the desired team because all teams are locked. However, if someone on that team leaves, + /// you will get ID_TEAM_BALANCER_SET_TEAM later. + /// For TeamBalancer: Byte 1 contains the team you requested to join. + ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED, + ID_TEAM_BALANCER_TEAM_REQUESTED_CANCELLED, + /// Team balancer plugin informing you of your team. Byte 1 contains the team you requested to join. Following bytes contain NetworkID of which member. + ID_TEAM_BALANCER_TEAM_ASSIGNED, + + /// Gamebryo Lightspeed integration + ID_LIGHTSPEED_INTEGRATION, + + /// XBOX integration + ID_XBOX_LOBBY, + + /// The password we used to challenge the other system passed, meaning the other system has called TwoWayAuthentication::AddPassword() with the same password we passed to TwoWayAuthentication::Challenge() + /// You can read the identifier used to challenge as follows: + /// RakNet::BitStream bs(packet->data, packet->length, false); bs.IgnoreBytes(sizeof(RakNet::MessageID)); RakNet::RakString password; bs.Read(password); + ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_SUCCESS, + ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_SUCCESS, + /// A remote system sent us a challenge using TwoWayAuthentication::Challenge(), and the challenge failed. + /// If the other system must pass the challenge to stay connected, you should call RakPeer::CloseConnection() to terminate the connection to the other system. + ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_FAILURE, + /// The other system did not add the password we used to TwoWayAuthentication::AddPassword() + /// You can read the identifier used to challenge as follows: + /// RakNet::BitStream bs(packet->data, packet->length, false); bs.IgnoreBytes(sizeof(MessageID)); RakNet::RakString password; bs.Read(password); + ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_FAILURE, + /// The other system did not respond within a timeout threshhold. Either the other system is not running the plugin or the other system was blocking on some operation for a long time. + /// You can read the identifier used to challenge as follows: + /// RakNet::BitStream bs(packet->data, packet->length, false); bs.IgnoreBytes(sizeof(MessageID)); RakNet::RakString password; bs.Read(password); + ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_TIMEOUT, + /// \internal + ID_TWO_WAY_AUTHENTICATION_NEGOTIATION, + + /// CloudClient / CloudServer + ID_CLOUD_POST_REQUEST, + ID_CLOUD_RELEASE_REQUEST, + ID_CLOUD_GET_REQUEST, + ID_CLOUD_GET_RESPONSE, + ID_CLOUD_UNSUBSCRIBE_REQUEST, + ID_CLOUD_SERVER_TO_SERVER_COMMAND, + ID_CLOUD_SUBSCRIPTION_NOTIFICATION, + + // LibVoice + ID_LIB_VOICE, + + ID_RELAY_PLUGIN, + ID_NAT_REQUEST_BOUND_ADDRESSES, + ID_NAT_RESPOND_BOUND_ADDRESSES, + ID_FCM2_UPDATE_USER_CONTEXT, + ID_RESERVED_3, + ID_RESERVED_4, + ID_RESERVED_5, + ID_RESERVED_6, + ID_RESERVED_7, + ID_RESERVED_8, + ID_RESERVED_9, + + // For the user to use. Start your first enumeration at this value. + ID_USER_PACKET_ENUM + //------------------------------------------------------------------------------------------------------------- + +}; + +#endif // RAKNET_USE_CUSTOM_PACKET_IDS + diff --git a/Source/NatPunchthroughClient.h b/Source/NatPunchthroughClient.h index a1d24f6bb..3d95ef38c 100644 --- a/Source/NatPunchthroughClient.h +++ b/Source/NatPunchthroughClient.h @@ -1,306 +1,304 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Contains the NAT-punchthrough plugin for the client. -/// - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_NatPunchthroughClient==1 - -#ifndef __NAT_PUNCHTHROUGH_CLIENT_H -#define __NAT_PUNCHTHROUGH_CLIENT_H - -#include "RakNetTypes.h" -#include "Export.h" -#include "PluginInterface2.h" -#include "PacketPriority.h" -#include "SocketIncludes.h" -#include "DS_List.h" -#include "RakString.h" -#include "DS_Queue.h" - -// Trendnet TEW-632BRP sometimes starts at port 1024 and increments sequentially. -// Zonnet zsr1134we. Replies go out on the net, but are always absorbed by the remote router?? -// Dlink ebr2310 to Trendnet ok -// Trendnet TEW-652BRP to Trendnet 632BRP OK -// Trendnet TEW-632BRP to Trendnet 632BRP OK -// Buffalo WHR-HP-G54 OK -// Netgear WGR614 ok - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; -struct Packet; -#if _RAKNET_SUPPORT_PacketLogger==1 -class PacketLogger; -#endif - -/// \ingroup NAT_PUNCHTHROUGH_GROUP -struct RAK_DLL_EXPORT PunchthroughConfiguration -{ - /// internal: (15 ms * 2 tries + 30 wait) * 5 ports * 8 players = 2.4 seconds - /// external: (50 ms * 8 sends + 200 wait) * 2 port * 8 players = 9.6 seconds - /// Total: 8 seconds - PunchthroughConfiguration() { - TIME_BETWEEN_PUNCH_ATTEMPTS_INTERNAL=15; - TIME_BETWEEN_PUNCH_ATTEMPTS_EXTERNAL=50; - UDP_SENDS_PER_PORT_INTERNAL=2; - UDP_SENDS_PER_PORT_EXTERNAL=8; - INTERNAL_IP_WAIT_AFTER_ATTEMPTS=30; - MAXIMUM_NUMBER_OF_INTERNAL_IDS_TO_CHECK=5; /// set to 0 to not do lan connects - MAX_PREDICTIVE_PORT_RANGE=2; - EXTERNAL_IP_WAIT_BETWEEN_PORTS=200; - EXTERNAL_IP_WAIT_AFTER_FIRST_TTL=100; - EXTERNAL_IP_WAIT_AFTER_ALL_ATTEMPTS=EXTERNAL_IP_WAIT_BETWEEN_PORTS; - retryOnFailure=false; - } - - /// How much time between each UDP send - RakNet::Time TIME_BETWEEN_PUNCH_ATTEMPTS_INTERNAL; - RakNet::Time TIME_BETWEEN_PUNCH_ATTEMPTS_EXTERNAL; - - /// How many tries for one port before giving up and going to the next port - int UDP_SENDS_PER_PORT_INTERNAL; - int UDP_SENDS_PER_PORT_EXTERNAL; - - /// After giving up on one internal port, how long to wait before trying the next port - int INTERNAL_IP_WAIT_AFTER_ATTEMPTS; - - /// How many external ports to try past the last known starting port - int MAX_PREDICTIVE_PORT_RANGE; - - /// After sending TTL, how long to wait until first punch attempt - int EXTERNAL_IP_WAIT_AFTER_FIRST_TTL; - - /// After giving up on one external port, how long to wait before trying the next port - int EXTERNAL_IP_WAIT_BETWEEN_PORTS; - - /// After trying all external ports, how long to wait before returning ID_NAT_PUNCHTHROUGH_FAILED - int EXTERNAL_IP_WAIT_AFTER_ALL_ATTEMPTS; - - /// Maximum number of internal IP address to try to connect to. - /// Cannot be greater than MAXIMUM_NUMBER_OF_INTERNAL_IDS - /// Should be high enough to try all internal IP addresses on the majority of computers - int MAXIMUM_NUMBER_OF_INTERNAL_IDS_TO_CHECK; - - /// If the first punchthrough attempt fails, try again - /// This sometimes works because the remote router was looking for an incoming message on a higher numbered port before responding to a lower numbered port from the other system - bool retryOnFailure; -}; - -/// \ingroup NAT_PUNCHTHROUGH_GROUP -struct RAK_DLL_EXPORT NatPunchthroughDebugInterface -{ - NatPunchthroughDebugInterface() {} - virtual ~NatPunchthroughDebugInterface() {} - virtual void OnClientMessage(const char *msg)=0; -}; - -/// \ingroup NAT_PUNCHTHROUGH_GROUP -struct RAK_DLL_EXPORT NatPunchthroughDebugInterface_Printf : public NatPunchthroughDebugInterface -{ - virtual void OnClientMessage(const char *msg); -}; - -#if _RAKNET_SUPPORT_PacketLogger==1 -/// \ingroup NAT_PUNCHTHROUGH_GROUP -struct RAK_DLL_EXPORT NatPunchthroughDebugInterface_PacketLogger : public NatPunchthroughDebugInterface -{ - // Set to non-zero to write to the packetlogger! - PacketLogger *pl; - - NatPunchthroughDebugInterface_PacketLogger() {pl=0;} - ~NatPunchthroughDebugInterface_PacketLogger() {} - virtual void OnClientMessage(const char *msg); -}; -#endif - -/// \brief Client code for NATPunchthrough -/// \details Maintain connection to NatPunchthroughServer to process incoming connection attempts through NatPunchthroughClient
-/// Client will send datagrams to port to estimate next port
-/// Will simultaneously connect with another client once ports are estimated. -/// \sa NatTypeDetectionClient -/// See also http://www.jenkinssoftware.com/raknet/manual/natpunchthrough.html -/// \ingroup NAT_PUNCHTHROUGH_GROUP -class RAK_DLL_EXPORT NatPunchthroughClient : public PluginInterface2 -{ -public: - - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(NatPunchthroughClient) - - NatPunchthroughClient(); - ~NatPunchthroughClient(); - - /// If the instance of RakPeer running NATPunchthroughServer was bound to two IP addresses, then you can call FindRouterPortStride() - /// This will determine the stride that your router uses when assigning ports, if your router is full-cone - /// This function is also called automatically when you call OpenNAT - however, calling it earlier when you are connected to the facilitator will speed up the process - /// \param[in] destination The system to punch. Must already be connected to \a facilitator - void FindRouterPortStride(const SystemAddress &facilitator); - - /// Punchthrough a NAT. Doesn't connect, just tries to setup the routing table - /// \param[in] destination The system to punch. Must already be connected to \a facilitator - /// \param[in] facilitator A system we are already connected to running the NatPunchthroughServer plugin - /// \sa OpenNATGroup() - /// You will get ID_NAT_PUNCHTHROUGH_SUCCEEDED on success - /// You will get ID_NAT_TARGET_NOT_CONNECTED, ID_NAT_TARGET_UNRESPONSIVE, ID_NAT_CONNECTION_TO_TARGET_LOST, ID_NAT_ALREADY_IN_PROGRESS, or ID_NAT_PUNCHTHROUGH_FAILED on failures of various types - /// However, if you lose connection to the facilitator, you may not necessarily get above - bool OpenNAT(RakNetGUID destination, const SystemAddress &facilitator); - - /* - /// \deprecated See FullyConnectedMesh2::StartVerifiedJoin() which is more flexible - /// Same as calling OpenNAT for a list of systems, but reply is delayed until all systems pass. - /// This is useful for peer to peer games where you want to connect to every system in the remote session, not just one particular system - /// \note For cloud computing, all systems in the group must be connected to the same facilitator since we're only specifying one - /// You will get ID_NAT_GROUP_PUNCH_SUCCEEDED on success - /// You will get ID_NAT_TARGET_NOT_CONNECTED, ID_NAT_ALREADY_IN_PROGRESS, or ID_NAT_GROUP_PUNCH_FAILED on failures of various types - /// However, if you lose connection to the facilitator, you may not necessarily get above - bool OpenNATGroup(DataStructures::List destinationSystems, const SystemAddress &facilitator); - */ - - /// Modify the system configuration if desired - /// Don't modify the variables in the structure while punchthrough is in progress - PunchthroughConfiguration* GetPunchthroughConfiguration(void); - - /// Sets a callback to be called with debug messages - /// \param[in] i Pointer to an interface. The pointer is stored, so don't delete it while in progress. Pass 0 to clear. - void SetDebugInterface(NatPunchthroughDebugInterface *i); - - /// Get the port mappings you should pass to UPNP (for miniupnpc-1.6.20120410, for the function UPNP_AddPortMapping) - void GetUPNPPortMappings(char *externalPort, char *internalPort, const SystemAddress &natPunchthroughServerAddress); - - /// \internal For plugin handling - virtual void Update(void); - - /// \internal For plugin handling - virtual PluginReceiveResult OnReceive(Packet *packet); - - /// \internal For plugin handling - virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); - - /// \internal For plugin handling - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - - virtual void OnAttach(void); - virtual void OnDetach(void); - virtual void OnRakPeerShutdown(void); - void Clear(void); - - struct SendPing - { - RakNet::Time nextActionTime; - SystemAddress targetAddress; - SystemAddress facilitator; - SystemAddress internalIds[MAXIMUM_NUMBER_OF_INTERNAL_IDS]; - RakNetGUID targetGuid; - bool weAreSender; - int attemptCount; - int retryCount; - int punchingFixedPortAttempts; // only used for TestMode::PUNCHING_FIXED_PORT - uint16_t sessionId; - bool sentTTL; - // Give priority to internal IP addresses because if we are on a LAN, we don't want to try to connect through the internet - enum TestMode - { - TESTING_INTERNAL_IPS, - WAITING_FOR_INTERNAL_IPS_RESPONSE, - //SEND_WITH_TTL, - TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_FACILITATOR_PORT, - TESTING_EXTERNAL_IPS_1024_TO_FACILITATOR_PORT, - TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_1024, - TESTING_EXTERNAL_IPS_1024_TO_1024, - WAITING_AFTER_ALL_ATTEMPTS, - - // The trendnet remaps the remote port to 1024. - // If you continue punching on a different port for the same IP it bans you and the communication becomes unidirectioal - PUNCHING_FIXED_PORT, - - // try port 1024-1028 - } testMode; - } sp; - -protected: - unsigned short mostRecentExternalPort; - //void OnNatGroupPunchthroughRequest(Packet *packet); - void OnFailureNotification(Packet *packet); - //void OnNatGroupPunchthroughReply(Packet *packet); - void OnGetMostRecentPort(Packet *packet); - void OnConnectAtTime(Packet *packet); - unsigned int GetPendingOpenNATIndex(RakNetGUID destination, const SystemAddress &facilitator); - void SendPunchthrough(RakNetGUID destination, const SystemAddress &facilitator); - void QueueOpenNAT(RakNetGUID destination, const SystemAddress &facilitator); - void SendQueuedOpenNAT(void); - void SendTTL(const SystemAddress &sa); - void SendOutOfBand(SystemAddress sa, MessageID oobId); - void OnPunchthroughFailure(void); - void OnReadyForNextPunchthrough(void); - void PushFailure(void); - bool RemoveFromFailureQueue(void); - void PushSuccess(void); - - PunchthroughConfiguration pc; - NatPunchthroughDebugInterface *natPunchthroughDebugInterface; - - // The first time we fail a NAT attempt, we add it to failedAttemptList and try again, since sometimes trying again later fixes the problem - // The second time we fail, we return ID_NAT_PUNCHTHROUGH_FAILED - struct AddrAndGuid - { - SystemAddress addr; - RakNetGUID guid; - }; - DataStructures::List failedAttemptList; - - struct DSTAndFac - { - RakNetGUID destination; - SystemAddress facilitator; - }; - DataStructures::Queue queuedOpenNat; - - void IncrementExternalAttemptCount(RakNet::Time time, RakNet::Time delta); - unsigned short portStride; - enum - { - HAS_PORT_STRIDE, - UNKNOWN_PORT_STRIDE, - CALCULATING_PORT_STRIDE, - INCAPABLE_PORT_STRIDE - } hasPortStride; - RakNet::Time portStrideCalTimeout; - - /* - struct TimeAndGuid - { - RakNet::Time time; - RakNetGUID guid; - }; - DataStructures::List groupRequestsInProgress; - - struct GroupPunchRequest - { - SystemAddress facilitator; - DataStructures::List pendingList; - DataStructures::List passedListGuid; - DataStructures::List passedListAddress; - DataStructures::List failedList; - DataStructures::List ignoredList; - }; - DataStructures::List groupPunchRequests; - void UpdateGroupPunchOnNatResult(SystemAddress facilitator, RakNetGUID targetSystem, SystemAddress targetSystemAddress, int result); // 0=failed, 1=success, 2=ignore - */ -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Contains the NAT-punchthrough plugin for the client. +/// + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_NatPunchthroughClient==1 + +#pragma once + +#include "RakNetTypes.h" +#include "Export.h" +#include "PluginInterface2.h" +#include "PacketPriority.h" +#include "SocketIncludes.h" +#include "DS_List.h" +#include "RakString.h" +#include "DS_Queue.h" + +// Trendnet TEW-632BRP sometimes starts at port 1024 and increments sequentially. +// Zonnet zsr1134we. Replies go out on the net, but are always absorbed by the remote router?? +// Dlink ebr2310 to Trendnet ok +// Trendnet TEW-652BRP to Trendnet 632BRP OK +// Trendnet TEW-632BRP to Trendnet 632BRP OK +// Buffalo WHR-HP-G54 OK +// Netgear WGR614 ok + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; +struct Packet; +#if _RAKNET_SUPPORT_PacketLogger==1 +class PacketLogger; +#endif + +/// \ingroup NAT_PUNCHTHROUGH_GROUP +struct RAK_DLL_EXPORT PunchthroughConfiguration +{ + /// internal: (15 ms * 2 tries + 30 wait) * 5 ports * 8 players = 2.4 seconds + /// external: (50 ms * 8 sends + 200 wait) * 2 port * 8 players = 9.6 seconds + /// Total: 8 seconds + PunchthroughConfiguration() { + TIME_BETWEEN_PUNCH_ATTEMPTS_INTERNAL=15; + TIME_BETWEEN_PUNCH_ATTEMPTS_EXTERNAL=50; + UDP_SENDS_PER_PORT_INTERNAL=2; + UDP_SENDS_PER_PORT_EXTERNAL=8; + INTERNAL_IP_WAIT_AFTER_ATTEMPTS=30; + MAXIMUM_NUMBER_OF_INTERNAL_IDS_TO_CHECK=5; /// set to 0 to not do lan connects + MAX_PREDICTIVE_PORT_RANGE=2; + EXTERNAL_IP_WAIT_BETWEEN_PORTS=200; + EXTERNAL_IP_WAIT_AFTER_FIRST_TTL=100; + EXTERNAL_IP_WAIT_AFTER_ALL_ATTEMPTS=EXTERNAL_IP_WAIT_BETWEEN_PORTS; + retryOnFailure=false; + } + + /// How much time between each UDP send + RakNet::Time TIME_BETWEEN_PUNCH_ATTEMPTS_INTERNAL; + RakNet::Time TIME_BETWEEN_PUNCH_ATTEMPTS_EXTERNAL; + + /// How many tries for one port before giving up and going to the next port + int UDP_SENDS_PER_PORT_INTERNAL; + int UDP_SENDS_PER_PORT_EXTERNAL; + + /// After giving up on one internal port, how long to wait before trying the next port + int INTERNAL_IP_WAIT_AFTER_ATTEMPTS; + + /// How many external ports to try past the last known starting port + int MAX_PREDICTIVE_PORT_RANGE; + + /// After sending TTL, how long to wait until first punch attempt + int EXTERNAL_IP_WAIT_AFTER_FIRST_TTL; + + /// After giving up on one external port, how long to wait before trying the next port + int EXTERNAL_IP_WAIT_BETWEEN_PORTS; + + /// After trying all external ports, how long to wait before returning ID_NAT_PUNCHTHROUGH_FAILED + int EXTERNAL_IP_WAIT_AFTER_ALL_ATTEMPTS; + + /// Maximum number of internal IP address to try to connect to. + /// Cannot be greater than MAXIMUM_NUMBER_OF_INTERNAL_IDS + /// Should be high enough to try all internal IP addresses on the majority of computers + int MAXIMUM_NUMBER_OF_INTERNAL_IDS_TO_CHECK; + + /// If the first punchthrough attempt fails, try again + /// This sometimes works because the remote router was looking for an incoming message on a higher numbered port before responding to a lower numbered port from the other system + bool retryOnFailure; +}; + +/// \ingroup NAT_PUNCHTHROUGH_GROUP +struct RAK_DLL_EXPORT NatPunchthroughDebugInterface +{ + NatPunchthroughDebugInterface() {} + virtual ~NatPunchthroughDebugInterface() {} + virtual void OnClientMessage(const char *msg)=0; +}; + +/// \ingroup NAT_PUNCHTHROUGH_GROUP +struct RAK_DLL_EXPORT NatPunchthroughDebugInterface_Printf : public NatPunchthroughDebugInterface +{ + virtual void OnClientMessage(const char *msg); +}; + +#if _RAKNET_SUPPORT_PacketLogger==1 +/// \ingroup NAT_PUNCHTHROUGH_GROUP +struct RAK_DLL_EXPORT NatPunchthroughDebugInterface_PacketLogger : public NatPunchthroughDebugInterface +{ + // Set to non-zero to write to the packetlogger! + PacketLogger *pl; + + NatPunchthroughDebugInterface_PacketLogger() {pl=0;} + ~NatPunchthroughDebugInterface_PacketLogger() {} + virtual void OnClientMessage(const char *msg); +}; +#endif + +/// \brief Client code for NATPunchthrough +/// \details Maintain connection to NatPunchthroughServer to process incoming connection attempts through NatPunchthroughClient
+/// Client will send datagrams to port to estimate next port
+/// Will simultaneously connect with another client once ports are estimated. +/// \sa NatTypeDetectionClient +/// See also http://www.jenkinssoftware.com/raknet/manual/natpunchthrough.html +/// \ingroup NAT_PUNCHTHROUGH_GROUP +class RAK_DLL_EXPORT NatPunchthroughClient : public PluginInterface2 +{ +public: + + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(NatPunchthroughClient) + + NatPunchthroughClient(); + ~NatPunchthroughClient(); + + /// If the instance of RakPeer running NATPunchthroughServer was bound to two IP addresses, then you can call FindRouterPortStride() + /// This will determine the stride that your router uses when assigning ports, if your router is full-cone + /// This function is also called automatically when you call OpenNAT - however, calling it earlier when you are connected to the facilitator will speed up the process + /// \param[in] destination The system to punch. Must already be connected to \a facilitator + void FindRouterPortStride(const SystemAddress &facilitator); + + /// Punchthrough a NAT. Doesn't connect, just tries to setup the routing table + /// \param[in] destination The system to punch. Must already be connected to \a facilitator + /// \param[in] facilitator A system we are already connected to running the NatPunchthroughServer plugin + /// \sa OpenNATGroup() + /// You will get ID_NAT_PUNCHTHROUGH_SUCCEEDED on success + /// You will get ID_NAT_TARGET_NOT_CONNECTED, ID_NAT_TARGET_UNRESPONSIVE, ID_NAT_CONNECTION_TO_TARGET_LOST, ID_NAT_ALREADY_IN_PROGRESS, or ID_NAT_PUNCHTHROUGH_FAILED on failures of various types + /// However, if you lose connection to the facilitator, you may not necessarily get above + bool OpenNAT(RakNetGUID destination, const SystemAddress &facilitator); + + /* + /// \deprecated See FullyConnectedMesh2::StartVerifiedJoin() which is more flexible + /// Same as calling OpenNAT for a list of systems, but reply is delayed until all systems pass. + /// This is useful for peer to peer games where you want to connect to every system in the remote session, not just one particular system + /// \note For cloud computing, all systems in the group must be connected to the same facilitator since we're only specifying one + /// You will get ID_NAT_GROUP_PUNCH_SUCCEEDED on success + /// You will get ID_NAT_TARGET_NOT_CONNECTED, ID_NAT_ALREADY_IN_PROGRESS, or ID_NAT_GROUP_PUNCH_FAILED on failures of various types + /// However, if you lose connection to the facilitator, you may not necessarily get above + bool OpenNATGroup(DataStructures::List destinationSystems, const SystemAddress &facilitator); + */ + + /// Modify the system configuration if desired + /// Don't modify the variables in the structure while punchthrough is in progress + PunchthroughConfiguration* GetPunchthroughConfiguration(void); + + /// Sets a callback to be called with debug messages + /// \param[in] i Pointer to an interface. The pointer is stored, so don't delete it while in progress. Pass 0 to clear. + void SetDebugInterface(NatPunchthroughDebugInterface *i); + + /// Get the port mappings you should pass to UPNP (for miniupnpc-1.6.20120410, for the function UPNP_AddPortMapping) + void GetUPNPPortMappings(char *externalPort, char *internalPort, const SystemAddress &natPunchthroughServerAddress); + + /// \internal For plugin handling + virtual void Update(void); + + /// \internal For plugin handling + virtual PluginReceiveResult OnReceive(Packet *packet); + + /// \internal For plugin handling + virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); + + /// \internal For plugin handling + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + + virtual void OnAttach(void); + virtual void OnDetach(void); + virtual void OnRakPeerShutdown(void); + void Clear(void); + + struct SendPing + { + RakNet::Time nextActionTime; + SystemAddress targetAddress; + SystemAddress facilitator; + SystemAddress internalIds[MAXIMUM_NUMBER_OF_INTERNAL_IDS]; + RakNetGUID targetGuid; + bool weAreSender; + int attemptCount; + int retryCount; + int punchingFixedPortAttempts; // only used for TestMode::PUNCHING_FIXED_PORT + uint16_t sessionId; + bool sentTTL; + // Give priority to internal IP addresses because if we are on a LAN, we don't want to try to connect through the internet + enum TestMode + { + TESTING_INTERNAL_IPS, + WAITING_FOR_INTERNAL_IPS_RESPONSE, + //SEND_WITH_TTL, + TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_FACILITATOR_PORT, + TESTING_EXTERNAL_IPS_1024_TO_FACILITATOR_PORT, + TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_1024, + TESTING_EXTERNAL_IPS_1024_TO_1024, + WAITING_AFTER_ALL_ATTEMPTS, + + // The trendnet remaps the remote port to 1024. + // If you continue punching on a different port for the same IP it bans you and the communication becomes unidirectioal + PUNCHING_FIXED_PORT, + + // try port 1024-1028 + } testMode; + } sp; + +protected: + unsigned short mostRecentExternalPort; + //void OnNatGroupPunchthroughRequest(Packet *packet); + void OnFailureNotification(Packet *packet); + //void OnNatGroupPunchthroughReply(Packet *packet); + void OnGetMostRecentPort(Packet *packet); + void OnConnectAtTime(Packet *packet); + unsigned int GetPendingOpenNATIndex(RakNetGUID destination, const SystemAddress &facilitator); + void SendPunchthrough(RakNetGUID destination, const SystemAddress &facilitator); + void QueueOpenNAT(RakNetGUID destination, const SystemAddress &facilitator); + void SendQueuedOpenNAT(void); + void SendTTL(const SystemAddress &sa); + void SendOutOfBand(SystemAddress sa, MessageID oobId); + void OnPunchthroughFailure(void); + void OnReadyForNextPunchthrough(void); + void PushFailure(void); + bool RemoveFromFailureQueue(void); + void PushSuccess(void); + + PunchthroughConfiguration pc; + NatPunchthroughDebugInterface *natPunchthroughDebugInterface; + + // The first time we fail a NAT attempt, we add it to failedAttemptList and try again, since sometimes trying again later fixes the problem + // The second time we fail, we return ID_NAT_PUNCHTHROUGH_FAILED + struct AddrAndGuid + { + SystemAddress addr; + RakNetGUID guid; + }; + DataStructures::List failedAttemptList; + + struct DSTAndFac + { + RakNetGUID destination; + SystemAddress facilitator; + }; + DataStructures::Queue queuedOpenNat; + + void IncrementExternalAttemptCount(RakNet::Time time, RakNet::Time delta); + unsigned short portStride; + enum + { + HAS_PORT_STRIDE, + UNKNOWN_PORT_STRIDE, + CALCULATING_PORT_STRIDE, + INCAPABLE_PORT_STRIDE + } hasPortStride; + RakNet::Time portStrideCalTimeout; + + /* + struct TimeAndGuid + { + RakNet::Time time; + RakNetGUID guid; + }; + DataStructures::List groupRequestsInProgress; + + struct GroupPunchRequest + { + SystemAddress facilitator; + DataStructures::List pendingList; + DataStructures::List passedListGuid; + DataStructures::List passedListAddress; + DataStructures::List failedList; + DataStructures::List ignoredList; + }; + DataStructures::List groupPunchRequests; + void UpdateGroupPunchOnNatResult(SystemAddress facilitator, RakNetGUID targetSystem, SystemAddress targetSystemAddress, int result); // 0=failed, 1=success, 2=ignore + */ +}; + +} // namespace RakNet + +#endif + diff --git a/Source/NatPunchthroughServer.h b/Source/NatPunchthroughServer.h index 1836ef9d6..1952a3955 100644 --- a/Source/NatPunchthroughServer.h +++ b/Source/NatPunchthroughServer.h @@ -1,156 +1,154 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Contains the NAT-punchthrough plugin for the server. -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_NatPunchthroughServer==1 - -#ifndef __NAT_PUNCHTHROUGH_SERVER_H -#define __NAT_PUNCHTHROUGH_SERVER_H - -#include "RakNetTypes.h" -#include "Export.h" -#include "PluginInterface2.h" -#include "PacketPriority.h" -#include "SocketIncludes.h" -#include "DS_OrderedList.h" -#include "RakString.h" - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; -struct Packet; -#if _RAKNET_SUPPORT_PacketLogger==1 -class PacketLogger; -#endif - -/// \defgroup NAT_PUNCHTHROUGH_GROUP NatPunchthrough -/// \brief Connect systems despite both systems being behind a router -/// \details -/// \ingroup PLUGINS_GROUP - -/// \ingroup NAT_PUNCHTHROUGH_GROUP -struct RAK_DLL_EXPORT NatPunchthroughServerDebugInterface -{ - NatPunchthroughServerDebugInterface() {} - virtual ~NatPunchthroughServerDebugInterface() {} - virtual void OnServerMessage(const char *msg)=0; -}; - -/// \ingroup NAT_PUNCHTHROUGH_GROUP -struct RAK_DLL_EXPORT NatPunchthroughServerDebugInterface_Printf : public NatPunchthroughServerDebugInterface -{ - virtual void OnServerMessage(const char *msg); -}; - -#if _RAKNET_SUPPORT_PacketLogger==1 -/// \ingroup NAT_PUNCHTHROUGH_GROUP -struct RAK_DLL_EXPORT NatPunchthroughServerDebugInterface_PacketLogger : public NatPunchthroughServerDebugInterface -{ - // Set to non-zero to write to the packetlogger! - PacketLogger *pl; - - NatPunchthroughServerDebugInterface_PacketLogger() {pl=0;} - ~NatPunchthroughServerDebugInterface_PacketLogger() {} - virtual void OnServerMessage(const char *msg); -}; -#endif - -/// \brief Server code for NATPunchthrough -/// \details Maintain connection to NatPunchthroughServer to process incoming connection attempts through NatPunchthroughClient
-/// Server maintains two sockets clients can connect to so as to estimate the next port choice
-/// Server tells other client about port estimate, current public port to the server, and a time to start connection attempts -/// \sa NatTypeDetectionClient -/// See also http://www.jenkinssoftware.com/raknet/manual/natpunchthrough.html -/// \ingroup NAT_PUNCHTHROUGH_GROUP -class RAK_DLL_EXPORT NatPunchthroughServer : public PluginInterface2 -{ -public: - - STATIC_FACTORY_DECLARATIONS(NatPunchthroughServer) - - // Constructor - NatPunchthroughServer(); - - // Destructor - virtual ~NatPunchthroughServer(); - - /// Sets a callback to be called with debug messages - /// \param[in] i Pointer to an interface. The pointer is stored, so don't delete it while in progress. Pass 0 to clear. - void SetDebugInterface(NatPunchthroughServerDebugInterface *i); - - /// \internal For plugin handling - virtual void Update(void); - - /// \internal For plugin handling - virtual PluginReceiveResult OnReceive(Packet *packet); - - /// \internal For plugin handling - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); - - // Each connected user has a ready state. Ready means ready for nat punchthrough. - struct User; - struct ConnectionAttempt - { - ConnectionAttempt() {sender=0; recipient=0; startTime=0; attemptPhase=NAT_ATTEMPT_PHASE_NOT_STARTED;} - User *sender, *recipient; - uint16_t sessionId; - RakNet::Time startTime; - enum - { - NAT_ATTEMPT_PHASE_NOT_STARTED, - NAT_ATTEMPT_PHASE_GETTING_RECENT_PORTS, - } attemptPhase; - }; - struct User - { - RakNetGUID guid; - SystemAddress systemAddress; - unsigned short mostRecentPort; - bool isReady; - DataStructures::OrderedList groupPunchthroughRequests; - - DataStructures::List connectionAttempts; - bool HasConnectionAttemptToUser(User *user); - void DerefConnectionAttempt(ConnectionAttempt *ca); - void DeleteConnectionAttempt(ConnectionAttempt *ca); - void LogConnectionAttempts(RakNet::RakString &rs); - }; - RakNet::Time lastUpdate; - static int NatPunchthroughUserComp( const RakNetGUID &key, User * const &data ); -protected: - void OnNATPunchthroughRequest(Packet *packet); - DataStructures::OrderedList users; - - void OnGetMostRecentPort(Packet *packet); - void OnClientReady(Packet *packet); - - void SendTimestamps(void); - void StartPendingPunchthrough(void); - void StartPunchthroughForUser(User*user); - uint16_t sessionId; - NatPunchthroughServerDebugInterface *natPunchthroughServerDebugInterface; - - SystemAddress boundAddresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS]; - unsigned char boundAddressCount; - -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Contains the NAT-punchthrough plugin for the server. +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_NatPunchthroughServer==1 + +#pragma once + +#include "RakNetTypes.h" +#include "Export.h" +#include "PluginInterface2.h" +#include "PacketPriority.h" +#include "SocketIncludes.h" +#include "DS_OrderedList.h" +#include "RakString.h" + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; +struct Packet; +#if _RAKNET_SUPPORT_PacketLogger==1 +class PacketLogger; +#endif + +/// \defgroup NAT_PUNCHTHROUGH_GROUP NatPunchthrough +/// \brief Connect systems despite both systems being behind a router +/// \details +/// \ingroup PLUGINS_GROUP + +/// \ingroup NAT_PUNCHTHROUGH_GROUP +struct RAK_DLL_EXPORT NatPunchthroughServerDebugInterface +{ + NatPunchthroughServerDebugInterface() {} + virtual ~NatPunchthroughServerDebugInterface() {} + virtual void OnServerMessage(const char *msg)=0; +}; + +/// \ingroup NAT_PUNCHTHROUGH_GROUP +struct RAK_DLL_EXPORT NatPunchthroughServerDebugInterface_Printf : public NatPunchthroughServerDebugInterface +{ + virtual void OnServerMessage(const char *msg); +}; + +#if _RAKNET_SUPPORT_PacketLogger==1 +/// \ingroup NAT_PUNCHTHROUGH_GROUP +struct RAK_DLL_EXPORT NatPunchthroughServerDebugInterface_PacketLogger : public NatPunchthroughServerDebugInterface +{ + // Set to non-zero to write to the packetlogger! + PacketLogger *pl; + + NatPunchthroughServerDebugInterface_PacketLogger() {pl=0;} + ~NatPunchthroughServerDebugInterface_PacketLogger() {} + virtual void OnServerMessage(const char *msg); +}; +#endif + +/// \brief Server code for NATPunchthrough +/// \details Maintain connection to NatPunchthroughServer to process incoming connection attempts through NatPunchthroughClient
+/// Server maintains two sockets clients can connect to so as to estimate the next port choice
+/// Server tells other client about port estimate, current public port to the server, and a time to start connection attempts +/// \sa NatTypeDetectionClient +/// See also http://www.jenkinssoftware.com/raknet/manual/natpunchthrough.html +/// \ingroup NAT_PUNCHTHROUGH_GROUP +class RAK_DLL_EXPORT NatPunchthroughServer : public PluginInterface2 +{ +public: + + STATIC_FACTORY_DECLARATIONS(NatPunchthroughServer) + + // Constructor + NatPunchthroughServer(); + + // Destructor + virtual ~NatPunchthroughServer(); + + /// Sets a callback to be called with debug messages + /// \param[in] i Pointer to an interface. The pointer is stored, so don't delete it while in progress. Pass 0 to clear. + void SetDebugInterface(NatPunchthroughServerDebugInterface *i); + + /// \internal For plugin handling + virtual void Update(void); + + /// \internal For plugin handling + virtual PluginReceiveResult OnReceive(Packet *packet); + + /// \internal For plugin handling + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); + + // Each connected user has a ready state. Ready means ready for nat punchthrough. + struct User; + struct ConnectionAttempt + { + ConnectionAttempt() {sender=0; recipient=0; startTime=0; attemptPhase=NAT_ATTEMPT_PHASE_NOT_STARTED;} + User *sender, *recipient; + uint16_t sessionId; + RakNet::Time startTime; + enum + { + NAT_ATTEMPT_PHASE_NOT_STARTED, + NAT_ATTEMPT_PHASE_GETTING_RECENT_PORTS, + } attemptPhase; + }; + struct User + { + RakNetGUID guid; + SystemAddress systemAddress; + unsigned short mostRecentPort; + bool isReady; + DataStructures::OrderedList groupPunchthroughRequests; + + DataStructures::List connectionAttempts; + bool HasConnectionAttemptToUser(User *user); + void DerefConnectionAttempt(ConnectionAttempt *ca); + void DeleteConnectionAttempt(ConnectionAttempt *ca); + void LogConnectionAttempts(RakNet::RakString &rs); + }; + RakNet::Time lastUpdate; + static int NatPunchthroughUserComp( const RakNetGUID &key, User * const &data ); +protected: + void OnNATPunchthroughRequest(Packet *packet); + DataStructures::OrderedList users; + + void OnGetMostRecentPort(Packet *packet); + void OnClientReady(Packet *packet); + + void SendTimestamps(void); + void StartPendingPunchthrough(void); + void StartPunchthroughForUser(User*user); + uint16_t sessionId; + NatPunchthroughServerDebugInterface *natPunchthroughServerDebugInterface; + + SystemAddress boundAddresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS]; + unsigned char boundAddressCount; + +}; + +} // namespace RakNet + +#endif + diff --git a/Source/NatTypeDetectionClient.h b/Source/NatTypeDetectionClient.h index 2b2766387..a778963ea 100644 --- a/Source/NatTypeDetectionClient.h +++ b/Source/NatTypeDetectionClient.h @@ -1,97 +1,95 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Contains the NAT-type detection code for the client -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_NatTypeDetectionClient==1 - -#ifndef __NAT_TYPE_DETECTION_CLIENT_H -#define __NAT_TYPE_DETECTION_CLIENT_H - -#include "RakNetTypes.h" -#include "Export.h" -#include "PluginInterface2.h" -#include "PacketPriority.h" -#include "SocketIncludes.h" -#include "DS_OrderedList.h" -#include "RakString.h" -#include "NatTypeDetectionCommon.h" - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; -struct Packet; - - /// \brief Client code for NatTypeDetection - /// \details See NatTypeDetectionServer.h for algorithm - /// To use, just connect to the server, and call DetectNAT - /// You will get back ID_NAT_TYPE_DETECTION_RESULT with one of the enumerated values of NATTypeDetectionResult found in NATTypeDetectionCommon.h - /// See also http://www.jenkinssoftware.com/raknet/manual/natpunchthrough.html - /// \sa NatPunchthroughClient - /// \sa NatTypeDetectionServer - /// \ingroup NAT_TYPE_DETECTION_GROUP - class RAK_DLL_EXPORT NatTypeDetectionClient : public PluginInterface2, public RNS2EventHandler - { - public: - - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(NatTypeDetectionClient) - - // Constructor - NatTypeDetectionClient(); - - // Destructor - virtual ~NatTypeDetectionClient(); - - /// Send the message to the server to detect the nat type - /// Server must be running NatTypeDetectionServer - /// We must already be connected to the server - /// \param[in] serverAddress address of the server - void DetectNATType(SystemAddress _serverAddress); - - /// \internal For plugin handling - virtual void Update(void); - - /// \internal For plugin handling - virtual PluginReceiveResult OnReceive(Packet *packet); - - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - virtual void OnRakPeerShutdown(void); - virtual void OnDetach(void); - - virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct); - virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line); - virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line); - protected: - DataStructures::Queue bufferedPackets; - SimpleMutex bufferedPacketsMutex; - - RakNetSocket2* c2; - //unsigned short c2Port; - void Shutdown(void); - void OnCompletion(NATTypeDetectionResult result); - bool IsInProgress(void) const; - - void OnTestPortRestricted(Packet *packet); - SystemAddress serverAddress; - }; - - -} - - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Contains the NAT-type detection code for the client +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_NatTypeDetectionClient==1 + +#pragma once + +#include "RakNetTypes.h" +#include "Export.h" +#include "PluginInterface2.h" +#include "PacketPriority.h" +#include "SocketIncludes.h" +#include "DS_OrderedList.h" +#include "RakString.h" +#include "NatTypeDetectionCommon.h" + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; +struct Packet; + + /// \brief Client code for NatTypeDetection + /// \details See NatTypeDetectionServer.h for algorithm + /// To use, just connect to the server, and call DetectNAT + /// You will get back ID_NAT_TYPE_DETECTION_RESULT with one of the enumerated values of NATTypeDetectionResult found in NATTypeDetectionCommon.h + /// See also http://www.jenkinssoftware.com/raknet/manual/natpunchthrough.html + /// \sa NatPunchthroughClient + /// \sa NatTypeDetectionServer + /// \ingroup NAT_TYPE_DETECTION_GROUP + class RAK_DLL_EXPORT NatTypeDetectionClient : public PluginInterface2, public RNS2EventHandler + { + public: + + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(NatTypeDetectionClient) + + // Constructor + NatTypeDetectionClient(); + + // Destructor + virtual ~NatTypeDetectionClient(); + + /// Send the message to the server to detect the nat type + /// Server must be running NatTypeDetectionServer + /// We must already be connected to the server + /// \param[in] serverAddress address of the server + void DetectNATType(SystemAddress _serverAddress); + + /// \internal For plugin handling + virtual void Update(void); + + /// \internal For plugin handling + virtual PluginReceiveResult OnReceive(Packet *packet); + + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + virtual void OnRakPeerShutdown(void); + virtual void OnDetach(void); + + virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct); + virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line); + virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line); + protected: + DataStructures::Queue bufferedPackets; + SimpleMutex bufferedPacketsMutex; + + RakNetSocket2* c2; + //unsigned short c2Port; + void Shutdown(void); + void OnCompletion(NATTypeDetectionResult result); + bool IsInProgress(void) const; + + void OnTestPortRestricted(Packet *packet); + SystemAddress serverAddress; + }; + + +} + + +#endif + diff --git a/Source/NatTypeDetectionCommon.cpp b/Source/NatTypeDetectionCommon.cpp index 51f8efe21..017262801 100644 --- a/Source/NatTypeDetectionCommon.cpp +++ b/Source/NatTypeDetectionCommon.cpp @@ -1,209 +1,209 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "NatTypeDetectionCommon.h" - -#if _RAKNET_SUPPORT_NatTypeDetectionServer==1 || _RAKNET_SUPPORT_NatTypeDetectionClient==1 - -#include "SocketLayer.h" -#include "SocketIncludes.h" -#include "SocketDefines.h" - -using namespace RakNet; - -bool RakNet::CanConnect(NATTypeDetectionResult type1, NATTypeDetectionResult type2) -{ - /// If one system is NAT_TYPE_SYMMETRIC, the other must be NAT_TYPE_ADDRESS_RESTRICTED or less - /// If one system is NAT_TYPE_PORT_RESTRICTED, the other must be NAT_TYPE_PORT_RESTRICTED or less - bool connectionGraph[NAT_TYPE_COUNT][NAT_TYPE_COUNT] = - { - // None, Full Cone, Address Restricted, Port Restricted, Symmetric, Unknown, InProgress, Supports_UPNP - {true, true, true, true, true, false, false, true}, // None - {true, true, true, true, true, false, false, true}, // Full Cone - {true, true, true, true, true, false, false, true}, // Address restricted - {true, true, true, true, false, false, false, true}, // Port restricted - {true, true, true, false, false, false, false, true}, // Symmetric - {false, false, false, false, false, false, false, false}, // Unknown - {false, false, false, false, false, false, false, false}, // InProgress - {true, true, true, true, true, false, false, true} // Supports_UPNP - }; - - return connectionGraph[(int) type1][(int) type2]; -} - -const char *RakNet::NATTypeDetectionResultToString(NATTypeDetectionResult type) -{ - switch (type) - { - case NAT_TYPE_NONE: - return "None"; - case NAT_TYPE_FULL_CONE: - return "Full cone"; - case NAT_TYPE_ADDRESS_RESTRICTED: - return "Address restricted"; - case NAT_TYPE_PORT_RESTRICTED: - return "Port restricted"; - case NAT_TYPE_SYMMETRIC: - return "Symmetric"; - case NAT_TYPE_UNKNOWN: - return "Unknown"; - case NAT_TYPE_DETECTION_IN_PROGRESS: - return "In Progress"; - case NAT_TYPE_SUPPORTS_UPNP: - return "Supports UPNP"; - case NAT_TYPE_COUNT: - return "NAT_TYPE_COUNT"; - } - return "Error, unknown enum in NATTypeDetectionResult"; -} - -// None and relaxed can connect to anything -// Moderate can connect to moderate or less -// Strict can connect to relaxed or less -const char *RakNet::NATTypeDetectionResultToStringFriendly(NATTypeDetectionResult type) -{ - switch (type) - { - case NAT_TYPE_NONE: - return "Open"; - case NAT_TYPE_FULL_CONE: - return "Relaxed"; - case NAT_TYPE_ADDRESS_RESTRICTED: - return "Relaxed"; - case NAT_TYPE_PORT_RESTRICTED: - return "Moderate"; - case NAT_TYPE_SYMMETRIC: - return "Strict"; - case NAT_TYPE_UNKNOWN: - return "Unknown"; - case NAT_TYPE_DETECTION_IN_PROGRESS: - return "In Progress"; - case NAT_TYPE_SUPPORTS_UPNP: - return "Supports UPNP"; - case NAT_TYPE_COUNT: - return "NAT_TYPE_COUNT"; - } - return "Error, unknown enum in NATTypeDetectionResult"; -} - - -RakNetSocket2* RakNet::CreateNonblockingBoundSocket(const char *bindAddr -#ifdef __native_client__ - ,_PP_Instance_ chromeInstance -#endif - , RNS2EventHandler *eventHandler - ) -{ - RakNetSocket2 *r2 = RakNetSocket2Allocator::AllocRNS2(); -#if defined(__native_client__) - NativeClientBindParameters ncbp; - RNS2_NativeClient * nativeClientSocket = (RNS2_NativeClient*) r2; - ncbp.eventHandler=eventHandler; - ncbp.forceHostAddress=(char*) bindAddr; - ncbp.is_ipv6=false; - ncbp.nativeClientInstance=chromeInstance; - ncbp.port=0; - nativeClientSocket->Bind(&ncbp, _FILE_AND_LINE_); -#elif defined(WINDOWS_STORE_RT) - RakAssert("TODO" && 0); -#else - if (r2->IsBerkleySocket()) - { - RNS2_BerkleyBindParameters bbp; - bbp.port=0; - bbp.hostAddress=(char*)bindAddr; - bbp.addressFamily=AF_INET; - bbp.type=SOCK_DGRAM; - bbp.protocol=0; - bbp.nonBlockingSocket=true; - bbp.setBroadcast=true; - bbp.setIPHdrIncl=false; - bbp.doNotFragment=false; - bbp.pollingThreadPriority=0; - bbp.eventHandler=eventHandler; - bbp.remotePortRakNetWasStartedOn_PS3_PS4_PSP2=0; - RNS2BindResult br = ((RNS2_Berkley*) r2)->Bind(&bbp, _FILE_AND_LINE_); - - if (br==BR_FAILED_TO_BIND_SOCKET) - { - RakNetSocket2Allocator::DeallocRNS2(r2); - return 0; - } - else if (br==BR_FAILED_SEND_TEST) - { - RakNetSocket2Allocator::DeallocRNS2(r2); - return 0; - } - else - { - RakAssert(br==BR_SUCCESS); - } - - ((RNS2_Berkley*) r2)->CreateRecvPollingThread(0); - } - else - { - RakAssert("TODO" && 0); - } -#endif - - return r2; - - /* - #ifdef __native_client__ - RakNetSocket2 *s = SocketLayer::CreateBoundSocket( 0, 0, false, bindAddr, true, 0, AF_INET, chromeInstance ); - #else - RakNetSocket2 *s = SocketLayer::CreateBoundSocket( 0, 0, false, bindAddr, true, 0, AF_INET, 0 ); - #endif - - #ifdef _WIN32 - unsigned long nonblocking = 1; - s->IOCTLSocket( FIONBIO, &nonblocking ); - #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3) || defined(_PS4) || defined(SN_TARGET_PSP2) - int sock_opt=1; - s->SetSockOpt(SOL_SOCKET, SO_NBIO, ( char * ) & sock_opt, sizeof ( sock_opt ) ); - #elif defined(__native_client__) - // Nop - #else - s->Fcntl( F_SETFL, O_NONBLOCK ); - #endif - return s; - */ -} - -/* -int RakNet::NatTypeRecvFrom(char *data, RakNetSocket2* socket, SystemAddress &sender, RNS2EventHandler *eventHandler) -{ -#if defined(__native_client__) - RakAssert("TODO" && 0); -#elif defined(WINDOWS_STORE_RT) - RakAssert("TODO" && 0); -#else - if (socket->IsBerkleySocket()) - { - RNS2RecvStruct *recvFromStruct; - recvFromStruct=AllocRNS2RecvStruct(_FILE_AND_LINE_); - if (recvFromStruct != NULL) - { - recvFromStruct->socket=this; - socket->RecvFromBlocking(recvFromStruct); - } - if (recvFromStruct->bytesRead>0) - { - sender = recvFromStruct->systemAddress; - } - return recvFromStruct->bytesRead; - } - return 0; -#endif -} -*/ - -#endif // #if _RAKNET_SUPPORT_NatTypeDetectionServer==1 || _RAKNET_SUPPORT_NatTypeDetectionClient==1 +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "NatTypeDetectionCommon.h" + +#if _RAKNET_SUPPORT_NatTypeDetectionServer==1 || _RAKNET_SUPPORT_NatTypeDetectionClient==1 + +#include "SocketLayer.h" +#include "SocketIncludes.h" +#include "SocketDefines.h" + +using namespace RakNet; + +bool RakNet::CanConnect(NATTypeDetectionResult type1, NATTypeDetectionResult type2) +{ + /// If one system is NAT_TYPE_SYMMETRIC, the other must be NAT_TYPE_ADDRESS_RESTRICTED or less + /// If one system is NAT_TYPE_PORT_RESTRICTED, the other must be NAT_TYPE_PORT_RESTRICTED or less + bool connectionGraph[NAT_TYPE_COUNT][NAT_TYPE_COUNT] = + { + // None, Full Cone, Address Restricted, Port Restricted, Symmetric, Unknown, InProgress, Supports_UPNP + {true, true, true, true, true, false, false, true}, // None + {true, true, true, true, true, false, false, true}, // Full Cone + {true, true, true, true, true, false, false, true}, // Address restricted + {true, true, true, true, false, false, false, true}, // Port restricted + {true, true, true, false, false, false, false, true}, // Symmetric + {false, false, false, false, false, false, false, false}, // Unknown + {false, false, false, false, false, false, false, false}, // InProgress + {true, true, true, true, true, false, false, true} // Supports_UPNP + }; + + return connectionGraph[(int) type1][(int) type2]; +} + +const char *RakNet::NATTypeDetectionResultToString(NATTypeDetectionResult type) +{ + switch (type) + { + case NAT_TYPE_NONE: + return "None"; + case NAT_TYPE_FULL_CONE: + return "Full cone"; + case NAT_TYPE_ADDRESS_RESTRICTED: + return "Address restricted"; + case NAT_TYPE_PORT_RESTRICTED: + return "Port restricted"; + case NAT_TYPE_SYMMETRIC: + return "Symmetric"; + case NAT_TYPE_UNKNOWN: + return "Unknown"; + case NAT_TYPE_DETECTION_IN_PROGRESS: + return "In Progress"; + case NAT_TYPE_SUPPORTS_UPNP: + return "Supports UPNP"; + case NAT_TYPE_COUNT: + return "NAT_TYPE_COUNT"; + } + return "Error, unknown enum in NATTypeDetectionResult"; +} + +// None and relaxed can connect to anything +// Moderate can connect to moderate or less +// Strict can connect to relaxed or less +const char *RakNet::NATTypeDetectionResultToStringFriendly(NATTypeDetectionResult type) +{ + switch (type) + { + case NAT_TYPE_NONE: + return "Open"; + case NAT_TYPE_FULL_CONE: + return "Relaxed"; + case NAT_TYPE_ADDRESS_RESTRICTED: + return "Relaxed"; + case NAT_TYPE_PORT_RESTRICTED: + return "Moderate"; + case NAT_TYPE_SYMMETRIC: + return "Strict"; + case NAT_TYPE_UNKNOWN: + return "Unknown"; + case NAT_TYPE_DETECTION_IN_PROGRESS: + return "In Progress"; + case NAT_TYPE_SUPPORTS_UPNP: + return "Supports UPNP"; + case NAT_TYPE_COUNT: + return "NAT_TYPE_COUNT"; + } + return "Error, unknown enum in NATTypeDetectionResult"; +} + + +RakNetSocket2* RakNet::CreateNonblockingBoundSocket(const char *bindAddr +#ifdef __native_client__ + ,_PP_Instance_ chromeInstance +#endif + , RNS2EventHandler *eventHandler + ) +{ + RakNetSocket2 *r2 = RakNetSocket2Allocator::AllocRNS2(); +#if defined(__native_client__) + NativeClientBindParameters ncbp; + RNS2_NativeClient * nativeClientSocket = (RNS2_NativeClient*) r2; + ncbp.eventHandler=eventHandler; + ncbp.forceHostAddress=(char*) bindAddr; + ncbp.is_ipv6=false; + ncbp.nativeClientInstance=chromeInstance; + ncbp.port=0; + nativeClientSocket->Bind(&ncbp, _FILE_AND_LINE_); +#elif defined(WINDOWS_STORE_RT) + RakAssert("TODO" && 0); +#else + if (r2->IsBerkleySocket()) + { + RNS2_BerkleyBindParameters bbp; + bbp.port=0; + bbp.hostAddress=(char*)bindAddr; + bbp.addressFamily=AF_INET; + bbp.type=SOCK_DGRAM; + bbp.protocol=0; + bbp.nonBlockingSocket=true; + bbp.setBroadcast=true; + bbp.setIPHdrIncl=false; + bbp.doNotFragment=false; + bbp.pollingThreadPriority=0; + bbp.eventHandler=eventHandler; + bbp.remotePortRakNetWasStartedOn_PS3_PS4_PSP2=0; + RNS2BindResult br = ((RNS2_Berkley*) r2)->Bind(&bbp, _FILE_AND_LINE_); + + if (br==BR_FAILED_TO_BIND_SOCKET) + { + RakNetSocket2Allocator::DeallocRNS2(r2); + return 0; + } + else if (br==BR_FAILED_SEND_TEST) + { + RakNetSocket2Allocator::DeallocRNS2(r2); + return 0; + } + else + { + RakAssert(br==BR_SUCCESS); + } + + ((RNS2_Berkley*) r2)->CreateRecvPollingThread(0); + } + else + { + RakAssert("TODO" && 0); + } +#endif + + return r2; + + /* + #ifdef __native_client__ + RakNetSocket2 *s = SocketLayer::CreateBoundSocket( 0, 0, false, bindAddr, true, 0, AF_INET, chromeInstance ); + #else + RakNetSocket2 *s = SocketLayer::CreateBoundSocket( 0, 0, false, bindAddr, true, 0, AF_INET, 0 ); + #endif + + #ifdef _WIN32 + unsigned long nonblocking = 1; + s->IOCTLSocket( FIONBIO, &nonblocking ); + #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3) || defined(_PS4) || defined(SN_TARGET_PSP2) + int sock_opt=1; + s->SetSockOpt(SOL_SOCKET, SO_NBIO, ( char * ) & sock_opt, sizeof ( sock_opt ) ); + #elif defined(__native_client__) + // Nop + #else + s->Fcntl( F_SETFL, O_NONBLOCK ); + #endif + return s; + */ +} + +/* +int RakNet::NatTypeRecvFrom(char *data, RakNetSocket2* socket, SystemAddress &sender, RNS2EventHandler *eventHandler) +{ +#if defined(__native_client__) + RakAssert("TODO" && 0); +#elif defined(WINDOWS_STORE_RT) + RakAssert("TODO" && 0); +#else + if (socket->IsBerkleySocket()) + { + RNS2RecvStruct *recvFromStruct; + recvFromStruct=AllocRNS2RecvStruct(_FILE_AND_LINE_); + if (recvFromStruct != nullptr) + { + recvFromStruct->socket=this; + socket->RecvFromBlocking(recvFromStruct); + } + if (recvFromStruct->bytesRead>0) + { + sender = recvFromStruct->systemAddress; + } + return recvFromStruct->bytesRead; + } + return 0; +#endif +} +*/ + +#endif // #if _RAKNET_SUPPORT_NatTypeDetectionServer==1 || _RAKNET_SUPPORT_NatTypeDetectionClient==1 diff --git a/Source/NatTypeDetectionCommon.h b/Source/NatTypeDetectionCommon.h index 3c470bca4..e52d5421c 100644 --- a/Source/NatTypeDetectionCommon.h +++ b/Source/NatTypeDetectionCommon.h @@ -1,79 +1,77 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \defgroup NAT_TYPE_DETECTION_GROUP NatTypeDetection -/// \brief Use a remote server with multiple IP addresses to determine what type of NAT your router is using -/// \details -/// \ingroup PLUGINS_GROUP - -#ifndef __NAT_TYPE_DETECTION_COMMON_H -#define __NAT_TYPE_DETECTION_COMMON_H - -#include "NativeFeatureIncludes.h" - -#if _RAKNET_SUPPORT_NatTypeDetectionServer==1 || _RAKNET_SUPPORT_NatTypeDetectionClient==1 - -#include "SocketIncludes.h" -#include "RakNetTypes.h" -#include "RakNetSocket2.h" - -namespace RakNet -{ - - /// All possible types of NATs (except NAT_TYPE_COUNT, which is an internal value) - enum NATTypeDetectionResult - { - /// Works with anyone - NAT_TYPE_NONE, - /// Accepts any datagrams to a port that has been previously used. Will accept the first datagram from the remote peer. - NAT_TYPE_FULL_CONE, - /// Accepts datagrams to a port as long as the datagram source IP address is a system we have already sent to. Will accept the first datagram if both systems send simultaneously. Otherwise, will accept the first datagram after we have sent one datagram. - NAT_TYPE_ADDRESS_RESTRICTED, - /// Same as address-restricted cone NAT, but we had to send to both the correct remote IP address and correct remote port. The same source address and port to a different destination uses the same mapping. - NAT_TYPE_PORT_RESTRICTED, - /// A different port is chosen for every remote destination. The same source address and port to a different destination uses a different mapping. Since the port will be different, the first external punchthrough attempt will fail. For this to work it requires port-prediction (MAX_PREDICTIVE_PORT_RANGE>1) and that the router chooses ports sequentially. - NAT_TYPE_SYMMETRIC, - /// Hasn't been determined. NATTypeDetectionClient does not use this, but other plugins might - NAT_TYPE_UNKNOWN, - /// In progress. NATTypeDetectionClient does not use this, but other plugins might - NAT_TYPE_DETECTION_IN_PROGRESS, - /// Didn't bother figuring it out, as we support UPNP, so it is equivalent to NAT_TYPE_NONE. NATTypeDetectionClient does not use this, but other plugins might - NAT_TYPE_SUPPORTS_UPNP, - /// \internal Must be last - NAT_TYPE_COUNT - }; - - /// \return Can one system with NATTypeDetectionResult \a type1 connect to \a type2 - bool RAK_DLL_EXPORT CanConnect(NATTypeDetectionResult type1, NATTypeDetectionResult type2); - - /// Return a technical string representin the enumeration - RAK_DLL_EXPORT const char * NATTypeDetectionResultToString(NATTypeDetectionResult type); - - /// Return a friendly string representing the enumeration - /// None and relaxed can connect to anything - /// Moderate can connect to moderate or less - /// Strict can connect to relaxed or less - RAK_DLL_EXPORT const char * NATTypeDetectionResultToStringFriendly(NATTypeDetectionResult type); - - /// \internal - RAK_DLL_EXPORT RakNetSocket2* CreateNonblockingBoundSocket(const char *bindAddr -#ifdef __native_client__ - ,_PP_Instance_ chromeInstance -#endif - , RNS2EventHandler *eventHandler - ); - - /// \internal - //int NatTypeRecvFrom(char *data, RakNetSocket2* socket, SystemAddress &sender, RNS2EventHandler *eventHandler); -} - -#endif // #if _RAKNET_SUPPORT_NatTypeDetectionServer==1 || _RAKNET_SUPPORT_NatTypeDetectionClient==1 - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \defgroup NAT_TYPE_DETECTION_GROUP NatTypeDetection +/// \brief Use a remote server with multiple IP addresses to determine what type of NAT your router is using +/// \details +/// \ingroup PLUGINS_GROUP + +#pragma once + +#include "NativeFeatureIncludes.h" + +#if _RAKNET_SUPPORT_NatTypeDetectionServer==1 || _RAKNET_SUPPORT_NatTypeDetectionClient==1 + +#include "SocketIncludes.h" +#include "RakNetTypes.h" +#include "RakNetSocket2.h" + +namespace RakNet +{ + + /// All possible types of NATs (except NAT_TYPE_COUNT, which is an internal value) + enum NATTypeDetectionResult + { + /// Works with anyone + NAT_TYPE_NONE, + /// Accepts any datagrams to a port that has been previously used. Will accept the first datagram from the remote peer. + NAT_TYPE_FULL_CONE, + /// Accepts datagrams to a port as long as the datagram source IP address is a system we have already sent to. Will accept the first datagram if both systems send simultaneously. Otherwise, will accept the first datagram after we have sent one datagram. + NAT_TYPE_ADDRESS_RESTRICTED, + /// Same as address-restricted cone NAT, but we had to send to both the correct remote IP address and correct remote port. The same source address and port to a different destination uses the same mapping. + NAT_TYPE_PORT_RESTRICTED, + /// A different port is chosen for every remote destination. The same source address and port to a different destination uses a different mapping. Since the port will be different, the first external punchthrough attempt will fail. For this to work it requires port-prediction (MAX_PREDICTIVE_PORT_RANGE>1) and that the router chooses ports sequentially. + NAT_TYPE_SYMMETRIC, + /// Hasn't been determined. NATTypeDetectionClient does not use this, but other plugins might + NAT_TYPE_UNKNOWN, + /// In progress. NATTypeDetectionClient does not use this, but other plugins might + NAT_TYPE_DETECTION_IN_PROGRESS, + /// Didn't bother figuring it out, as we support UPNP, so it is equivalent to NAT_TYPE_NONE. NATTypeDetectionClient does not use this, but other plugins might + NAT_TYPE_SUPPORTS_UPNP, + /// \internal Must be last + NAT_TYPE_COUNT + }; + + /// \return Can one system with NATTypeDetectionResult \a type1 connect to \a type2 + bool RAK_DLL_EXPORT CanConnect(NATTypeDetectionResult type1, NATTypeDetectionResult type2); + + /// Return a technical string representin the enumeration + RAK_DLL_EXPORT const char * NATTypeDetectionResultToString(NATTypeDetectionResult type); + + /// Return a friendly string representing the enumeration + /// None and relaxed can connect to anything + /// Moderate can connect to moderate or less + /// Strict can connect to relaxed or less + RAK_DLL_EXPORT const char * NATTypeDetectionResultToStringFriendly(NATTypeDetectionResult type); + + /// \internal + RAK_DLL_EXPORT RakNetSocket2* CreateNonblockingBoundSocket(const char *bindAddr +#ifdef __native_client__ + ,_PP_Instance_ chromeInstance +#endif + , RNS2EventHandler *eventHandler + ); + + /// \internal + //int NatTypeRecvFrom(char *data, RakNetSocket2* socket, SystemAddress &sender, RNS2EventHandler *eventHandler); +} + +#endif // #if _RAKNET_SUPPORT_NatTypeDetectionServer==1 || _RAKNET_SUPPORT_NatTypeDetectionClient==1 + diff --git a/Source/NatTypeDetectionServer.h b/Source/NatTypeDetectionServer.h index d5b24f9cf..f0dd3f85a 100644 --- a/Source/NatTypeDetectionServer.h +++ b/Source/NatTypeDetectionServer.h @@ -1,137 +1,135 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Contains the NAT-type detection code for the server -/// - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_NatTypeDetectionServer==1 - -#ifndef __NAT_TYPE_DETECTION_SERVER_H -#define __NAT_TYPE_DETECTION_SERVER_H - -#include "RakNetTypes.h" -#include "Export.h" -#include "PluginInterface2.h" -#include "PacketPriority.h" -#include "SocketIncludes.h" -#include "DS_OrderedList.h" -#include "RakString.h" -#include "NatTypeDetectionCommon.h" - - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; -struct Packet; - -/// \brief Server code for NatTypeDetection -/// \details -/// Sends to a remote system on certain ports and addresses to determine what type of router, if any, that client is behind -/// Requires that the server have 4 external IP addresses -///
    -///
  1. Server has 1 instance of RakNet. Server has four external ip addresses S1 to S4. Five ports are used in total P1 to P5. RakNet is bound to S1P1. Sockets are bound to S1P2, S2P3, S3P4, S4P5 -///
  2. Client with one port using RakNet (C1). Another port not using anything (C2). -///
  3. C1 connects to S1P1 for normal communication. -///
  4. S4P5 sends to C2. If arrived, no NAT. Done. (If didn't arrive, S4P5 potentially banned, do not use again). -///
  5. S2P3 sends to C1 (Different address, different port, to previously used port on client). If received, Full-cone nat. Done. (If didn't arrive, S2P3 potentially banned, do not use again). -///
  6. S1P2 sends to C1 (Same address, different port, to previously used port on client). If received, address-restricted cone nat. Done. -///
  7. Server via RakNet connection tells C1 to send to to S3P4. If address of C1 as seen by S3P4 is the same as the address of C1 as seen by S1P1 (RakNet connection), then port-restricted cone nat. Done -///
  8. Else symmetric nat. Done. -///
-/// See also http://www.jenkinssoftware.com/raknet/manual/natpunchthrough.html -/// \sa NatPunchthroughServer -/// \sa NatTypeDetectionClient -/// \ingroup NAT_TYPE_DETECTION_GROUP -class RAK_DLL_EXPORT NatTypeDetectionServer : public PluginInterface2, public RNS2EventHandler -{ -public: - - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(NatTypeDetectionServer) - - // Constructor - NatTypeDetectionServer(); - - // Destructor - virtual ~NatTypeDetectionServer(); - - /// Start the system, binding to 3 external IPs not already in useS - /// \param[in] nonRakNetIP2 First unused external IP - /// \param[in] nonRakNetIP3 Second unused external IP - /// \param[in] nonRakNetIP4 Third unused external IP - void Startup( - const char *nonRakNetIP2, - const char *nonRakNetIP3, - const char *nonRakNetIP4 -#ifdef __native_client__ - ,_PP_Instance_ chromeInstance -#endif - ); - - // Releases the sockets created in Startup(); - void Shutdown(void); - - /// \internal For plugin handling - virtual void Update(void); - - /// \internal For plugin handling - virtual PluginReceiveResult OnReceive(Packet *packet); - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - - enum NATDetectionState - { - STATE_NONE, - STATE_TESTING_NONE_1, - STATE_TESTING_NONE_2, - STATE_TESTING_FULL_CONE_1, - STATE_TESTING_FULL_CONE_2, - STATE_TESTING_ADDRESS_RESTRICTED_1, - STATE_TESTING_ADDRESS_RESTRICTED_2, - STATE_TESTING_PORT_RESTRICTED_1, - STATE_TESTING_PORT_RESTRICTED_2, - STATE_DONE, - }; - - struct NATDetectionAttempt - { - SystemAddress systemAddress; - NATDetectionState detectionState; - RakNet::TimeMS nextStateTime; - RakNet::TimeMS timeBetweenAttempts; - unsigned short c2Port; - RakNetGUID guid; - }; - - virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct); - virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line); - virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line); -protected: - DataStructures::Queue bufferedPackets; - SimpleMutex bufferedPacketsMutex; - - void OnDetectionRequest(Packet *packet); - DataStructures::List natDetectionAttempts; - unsigned int GetDetectionAttemptIndex(const SystemAddress &sa); - unsigned int GetDetectionAttemptIndex(RakNetGUID guid); - - // s1p1 is rakpeer itself - RakNetSocket2 *s1p2,*s2p3,*s3p4,*s4p5; - //unsigned short s1p2Port, s2p3Port, s3p4Port, s4p5Port; - char s3p4Address[64]; -}; -} - - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Contains the NAT-type detection code for the server +/// + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_NatTypeDetectionServer==1 + +#pragma once + +#include "RakNetTypes.h" +#include "Export.h" +#include "PluginInterface2.h" +#include "PacketPriority.h" +#include "SocketIncludes.h" +#include "DS_OrderedList.h" +#include "RakString.h" +#include "NatTypeDetectionCommon.h" + + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; +struct Packet; + +/// \brief Server code for NatTypeDetection +/// \details +/// Sends to a remote system on certain ports and addresses to determine what type of router, if any, that client is behind +/// Requires that the server have 4 external IP addresses +///
    +///
  1. Server has 1 instance of RakNet. Server has four external ip addresses S1 to S4. Five ports are used in total P1 to P5. RakNet is bound to S1P1. Sockets are bound to S1P2, S2P3, S3P4, S4P5 +///
  2. Client with one port using RakNet (C1). Another port not using anything (C2). +///
  3. C1 connects to S1P1 for normal communication. +///
  4. S4P5 sends to C2. If arrived, no NAT. Done. (If didn't arrive, S4P5 potentially banned, do not use again). +///
  5. S2P3 sends to C1 (Different address, different port, to previously used port on client). If received, Full-cone nat. Done. (If didn't arrive, S2P3 potentially banned, do not use again). +///
  6. S1P2 sends to C1 (Same address, different port, to previously used port on client). If received, address-restricted cone nat. Done. +///
  7. Server via RakNet connection tells C1 to send to to S3P4. If address of C1 as seen by S3P4 is the same as the address of C1 as seen by S1P1 (RakNet connection), then port-restricted cone nat. Done +///
  8. Else symmetric nat. Done. +///
+/// See also http://www.jenkinssoftware.com/raknet/manual/natpunchthrough.html +/// \sa NatPunchthroughServer +/// \sa NatTypeDetectionClient +/// \ingroup NAT_TYPE_DETECTION_GROUP +class RAK_DLL_EXPORT NatTypeDetectionServer : public PluginInterface2, public RNS2EventHandler +{ +public: + + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(NatTypeDetectionServer) + + // Constructor + NatTypeDetectionServer(); + + // Destructor + virtual ~NatTypeDetectionServer(); + + /// Start the system, binding to 3 external IPs not already in useS + /// \param[in] nonRakNetIP2 First unused external IP + /// \param[in] nonRakNetIP3 Second unused external IP + /// \param[in] nonRakNetIP4 Third unused external IP + void Startup( + const char *nonRakNetIP2, + const char *nonRakNetIP3, + const char *nonRakNetIP4 +#ifdef __native_client__ + ,_PP_Instance_ chromeInstance +#endif + ); + + // Releases the sockets created in Startup(); + void Shutdown(void); + + /// \internal For plugin handling + virtual void Update(void); + + /// \internal For plugin handling + virtual PluginReceiveResult OnReceive(Packet *packet); + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + + enum NATDetectionState + { + STATE_NONE, + STATE_TESTING_NONE_1, + STATE_TESTING_NONE_2, + STATE_TESTING_FULL_CONE_1, + STATE_TESTING_FULL_CONE_2, + STATE_TESTING_ADDRESS_RESTRICTED_1, + STATE_TESTING_ADDRESS_RESTRICTED_2, + STATE_TESTING_PORT_RESTRICTED_1, + STATE_TESTING_PORT_RESTRICTED_2, + STATE_DONE, + }; + + struct NATDetectionAttempt + { + SystemAddress systemAddress; + NATDetectionState detectionState; + RakNet::TimeMS nextStateTime; + RakNet::TimeMS timeBetweenAttempts; + unsigned short c2Port; + RakNetGUID guid; + }; + + virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct); + virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line); + virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line); +protected: + DataStructures::Queue bufferedPackets; + SimpleMutex bufferedPacketsMutex; + + void OnDetectionRequest(Packet *packet); + DataStructures::List natDetectionAttempts; + unsigned int GetDetectionAttemptIndex(const SystemAddress &sa); + unsigned int GetDetectionAttemptIndex(RakNetGUID guid); + + // s1p1 is rakpeer itself + RakNetSocket2 *s1p2,*s2p3,*s3p4,*s4p5; + //unsigned short s1p2Port, s2p3Port, s3p4Port, s4p5Port; + char s3p4Address[64]; +}; +} + + +#endif + diff --git a/Source/NativeFeatureIncludes.h b/Source/NativeFeatureIncludes.h index f3c15f811..5e68a9d3e 100644 --- a/Source/NativeFeatureIncludes.h +++ b/Source/NativeFeatureIncludes.h @@ -1,206 +1,204 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -// If you want to change these defines, put them in NativeFeatureIncludesOverrides so your changes are not lost when updating RakNet -// The user should not edit this file -#include "NativeFeatureIncludesOverrides.h" - -#ifndef __NATIVE_FEATURE_INCLDUES_H -#define __NATIVE_FEATURE_INCLDUES_H - -// Uncomment below defines, and paste to NativeFeatureIncludesOverrides.h, to exclude plugins that you do not want to build into the static library, or DLL -// These are not all the plugins, only those that are in the core library -// Other plugins are located in DependentExtensions -// #define _RAKNET_SUPPORT_ConnectionGraph2 0 -// #define _RAKNET_SUPPORT_DirectoryDeltaTransfer 0 -// #define _RAKNET_SUPPORT_FileListTransfer 0 -// #define _RAKNET_SUPPORT_FullyConnectedMesh2 0 -// #define _RAKNET_SUPPORT_MessageFilter 0 -// #define _RAKNET_SUPPORT_NatPunchthroughClient 0 -// #define _RAKNET_SUPPORT_NatPunchthroughServer 0 -// #define _RAKNET_SUPPORT_NatTypeDetectionClient 0 -// #define _RAKNET_SUPPORT_NatTypeDetectionServer 0 -// #define _RAKNET_SUPPORT_PacketLogger 0 -// #define _RAKNET_SUPPORT_ReadyEvent 0 -// #define _RAKNET_SUPPORT_ReplicaManager3 0 -// #define _RAKNET_SUPPORT_Router2 0 -// #define _RAKNET_SUPPORT_RPC4Plugin 0 -// #define _RAKNET_SUPPORT_TeamBalancer 0 -// #define _RAKNET_SUPPORT_TeamManager 0 -// #define _RAKNET_SUPPORT_UDPProxyClient 0 -// #define _RAKNET_SUPPORT_UDPProxyCoordinator 0 -// #define _RAKNET_SUPPORT_UDPProxyServer 0 -// #define _RAKNET_SUPPORT_ConsoleServer 0 -// #define _RAKNET_SUPPORT_RakNetTransport 0 -// #define _RAKNET_SUPPORT_TelnetTransport 0 -// #define _RAKNET_SUPPORT_TCPInterface 0 -// #define _RAKNET_SUPPORT_LogCommandParser 0 -// #define _RAKNET_SUPPORT_RakNetCommandParser 0 -// #define _RAKNET_SUPPORT_EmailSender 0 -// #define _RAKNET_SUPPORT_HTTPConnection 0 -// #define _RAKNET_SUPPORT_HTTPConnection2 0 -// #define _RAKNET_SUPPORT_PacketizedTCP 0 -// #define _RAKNET_SUPPORT_TwoWayAuthentication 0 - -// SET DEFAULTS IF UNDEFINED -#ifndef LIBCAT_SECURITY -#define LIBCAT_SECURITY 0 -#endif -#ifndef _RAKNET_SUPPORT_ConnectionGraph2 -#define _RAKNET_SUPPORT_ConnectionGraph2 1 -#endif -#ifndef _RAKNET_SUPPORT_DirectoryDeltaTransfer -#define _RAKNET_SUPPORT_DirectoryDeltaTransfer 1 -#endif -#ifndef _RAKNET_SUPPORT_FileListTransfer -#define _RAKNET_SUPPORT_FileListTransfer 1 -#endif -#ifndef _RAKNET_SUPPORT_FullyConnectedMesh -#define _RAKNET_SUPPORT_FullyConnectedMesh 1 -#endif -#ifndef _RAKNET_SUPPORT_FullyConnectedMesh2 -#define _RAKNET_SUPPORT_FullyConnectedMesh2 1 -#endif -#ifndef _RAKNET_SUPPORT_MessageFilter -#define _RAKNET_SUPPORT_MessageFilter 1 -#endif -#ifndef _RAKNET_SUPPORT_NatPunchthroughClient -#define _RAKNET_SUPPORT_NatPunchthroughClient 1 -#endif -#ifndef _RAKNET_SUPPORT_NatPunchthroughServer -#define _RAKNET_SUPPORT_NatPunchthroughServer 1 -#endif -#ifndef _RAKNET_SUPPORT_NatTypeDetectionClient -#define _RAKNET_SUPPORT_NatTypeDetectionClient 1 -#endif -#ifndef _RAKNET_SUPPORT_NatTypeDetectionServer -#define _RAKNET_SUPPORT_NatTypeDetectionServer 1 -#endif -#ifndef _RAKNET_SUPPORT_PacketLogger -#define _RAKNET_SUPPORT_PacketLogger 1 -#endif -#ifndef _RAKNET_SUPPORT_ReadyEvent -#define _RAKNET_SUPPORT_ReadyEvent 1 -#endif -#ifndef _RAKNET_SUPPORT_ReplicaManager3 -#define _RAKNET_SUPPORT_ReplicaManager3 1 -#endif -#ifndef _RAKNET_SUPPORT_Router2 -#define _RAKNET_SUPPORT_Router2 1 -#endif -#ifndef _RAKNET_SUPPORT_RPC4Plugin -#define _RAKNET_SUPPORT_RPC4Plugin 1 -#endif -#ifndef _RAKNET_SUPPORT_TeamBalancer -#define _RAKNET_SUPPORT_TeamBalancer 1 -#endif -#ifndef _RAKNET_SUPPORT_TeamManager -#define _RAKNET_SUPPORT_TeamManager 1 -#endif -#ifndef _RAKNET_SUPPORT_UDPProxyClient -#define _RAKNET_SUPPORT_UDPProxyClient 1 -#endif -#ifndef _RAKNET_SUPPORT_UDPProxyCoordinator -#define _RAKNET_SUPPORT_UDPProxyCoordinator 1 -#endif -#ifndef _RAKNET_SUPPORT_UDPProxyServer -#define _RAKNET_SUPPORT_UDPProxyServer 1 -#endif -#ifndef _RAKNET_SUPPORT_ConsoleServer -#define _RAKNET_SUPPORT_ConsoleServer 1 -#endif -#ifndef _RAKNET_SUPPORT_RakNetTransport -#define _RAKNET_SUPPORT_RakNetTransport 1 -#endif -#ifndef _RAKNET_SUPPORT_TelnetTransport -#define _RAKNET_SUPPORT_TelnetTransport 1 -#endif -#ifndef _RAKNET_SUPPORT_TCPInterface -#define _RAKNET_SUPPORT_TCPInterface 1 -#endif -#ifndef _RAKNET_SUPPORT_LogCommandParser -#define _RAKNET_SUPPORT_LogCommandParser 1 -#endif -#ifndef _RAKNET_SUPPORT_RakNetCommandParser -#define _RAKNET_SUPPORT_RakNetCommandParser 1 -#endif -#ifndef _RAKNET_SUPPORT_EmailSender -#define _RAKNET_SUPPORT_EmailSender 1 -#endif -#ifndef _RAKNET_SUPPORT_HTTPConnection -#define _RAKNET_SUPPORT_HTTPConnection 1 -#endif -#ifndef _RAKNET_SUPPORT_HTTPConnection2 -#define _RAKNET_SUPPORT_HTTPConnection2 1 -#endif -#ifndef _RAKNET_SUPPORT_PacketizedTCP -#define _RAKNET_SUPPORT_PacketizedTCP 1 -#endif -#ifndef _RAKNET_SUPPORT_TwoWayAuthentication -#define _RAKNET_SUPPORT_TwoWayAuthentication 1 -#endif -#ifndef _RAKNET_SUPPORT_CloudClient -#define _RAKNET_SUPPORT_CloudClient 1 -#endif -#ifndef _RAKNET_SUPPORT_CloudServer -#define _RAKNET_SUPPORT_CloudServer 1 -#endif -#ifndef _RAKNET_SUPPORT_DynDNS -#define _RAKNET_SUPPORT_DynDNS 1 -#endif -#ifndef _RAKNET_SUPPORT_Rackspace -#define _RAKNET_SUPPORT_Rackspace 1 -#endif -#ifndef _RAKNET_SUPPORT_FileOperations -#define _RAKNET_SUPPORT_FileOperations 1 -#endif -#ifndef _RAKNET_SUPPORT_UDPForwarder -#define _RAKNET_SUPPORT_UDPForwarder 1 -#endif -#ifndef _RAKNET_SUPPORT_StatisticsHistory -#define _RAKNET_SUPPORT_StatisticsHistory 1 -#endif -#ifndef _RAKNET_SUPPORT_LibVoice -#define _RAKNET_SUPPORT_LibVoice 0 -#endif -#ifndef _RAKNET_SUPPORT_RelayPlugin -#define _RAKNET_SUPPORT_RelayPlugin 1 -#endif - -// Take care of dependencies -#if _RAKNET_SUPPORT_DirectoryDeltaTransfer==1 -#undef _RAKNET_SUPPORT_FileListTransfer -#define _RAKNET_SUPPORT_FileListTransfer 1 -#endif -#if _RAKNET_SUPPORT_FullyConnectedMesh2==1 -#undef _RAKNET_SUPPORT_ConnectionGraph2 -#define _RAKNET_SUPPORT_ConnectionGraph2 1 -#endif -#if _RAKNET_SUPPORT_TelnetTransport==1 -#undef _RAKNET_SUPPORT_PacketizedTCP -#define _RAKNET_SUPPORT_PacketizedTCP 1 -#endif -#if _RAKNET_SUPPORT_PacketizedTCP==1 || _RAKNET_SUPPORT_EmailSender==1 || _RAKNET_SUPPORT_HTTPConnection==1 -#undef _RAKNET_SUPPORT_TCPInterface -#define _RAKNET_SUPPORT_TCPInterface 1 -#endif - - - - - - - - - - - - -#endif // __NATIVE_FEATURE_INCLDUES_H +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +// If you want to change these defines, put them in NativeFeatureIncludesOverrides so your changes are not lost when updating RakNet +// The user should not edit this file +#include "NativeFeatureIncludesOverrides.h" + +#pragma once + +// Uncomment below defines, and paste to NativeFeatureIncludesOverrides.h, to exclude plugins that you do not want to build into the static library, or DLL +// These are not all the plugins, only those that are in the core library +// Other plugins are located in DependentExtensions +// #define _RAKNET_SUPPORT_ConnectionGraph2 0 +// #define _RAKNET_SUPPORT_DirectoryDeltaTransfer 0 +// #define _RAKNET_SUPPORT_FileListTransfer 0 +// #define _RAKNET_SUPPORT_FullyConnectedMesh2 0 +// #define _RAKNET_SUPPORT_MessageFilter 0 +// #define _RAKNET_SUPPORT_NatPunchthroughClient 0 +// #define _RAKNET_SUPPORT_NatPunchthroughServer 0 +// #define _RAKNET_SUPPORT_NatTypeDetectionClient 0 +// #define _RAKNET_SUPPORT_NatTypeDetectionServer 0 +// #define _RAKNET_SUPPORT_PacketLogger 0 +// #define _RAKNET_SUPPORT_ReadyEvent 0 +// #define _RAKNET_SUPPORT_ReplicaManager3 0 +// #define _RAKNET_SUPPORT_Router2 0 +// #define _RAKNET_SUPPORT_RPC4Plugin 0 +// #define _RAKNET_SUPPORT_TeamBalancer 0 +// #define _RAKNET_SUPPORT_TeamManager 0 +// #define _RAKNET_SUPPORT_UDPProxyClient 0 +// #define _RAKNET_SUPPORT_UDPProxyCoordinator 0 +// #define _RAKNET_SUPPORT_UDPProxyServer 0 +// #define _RAKNET_SUPPORT_ConsoleServer 0 +// #define _RAKNET_SUPPORT_RakNetTransport 0 +// #define _RAKNET_SUPPORT_TelnetTransport 0 +// #define _RAKNET_SUPPORT_TCPInterface 0 +// #define _RAKNET_SUPPORT_LogCommandParser 0 +// #define _RAKNET_SUPPORT_RakNetCommandParser 0 +// #define _RAKNET_SUPPORT_EmailSender 0 +// #define _RAKNET_SUPPORT_HTTPConnection 0 +// #define _RAKNET_SUPPORT_HTTPConnection2 0 +// #define _RAKNET_SUPPORT_PacketizedTCP 0 +// #define _RAKNET_SUPPORT_TwoWayAuthentication 0 + +// SET DEFAULTS IF UNDEFINED +#ifndef LIBCAT_SECURITY +#define LIBCAT_SECURITY 0 +#endif +#ifndef _RAKNET_SUPPORT_ConnectionGraph2 +#define _RAKNET_SUPPORT_ConnectionGraph2 1 +#endif +#ifndef _RAKNET_SUPPORT_DirectoryDeltaTransfer +#define _RAKNET_SUPPORT_DirectoryDeltaTransfer 1 +#endif +#ifndef _RAKNET_SUPPORT_FileListTransfer +#define _RAKNET_SUPPORT_FileListTransfer 1 +#endif +#ifndef _RAKNET_SUPPORT_FullyConnectedMesh +#define _RAKNET_SUPPORT_FullyConnectedMesh 1 +#endif +#ifndef _RAKNET_SUPPORT_FullyConnectedMesh2 +#define _RAKNET_SUPPORT_FullyConnectedMesh2 1 +#endif +#ifndef _RAKNET_SUPPORT_MessageFilter +#define _RAKNET_SUPPORT_MessageFilter 1 +#endif +#ifndef _RAKNET_SUPPORT_NatPunchthroughClient +#define _RAKNET_SUPPORT_NatPunchthroughClient 1 +#endif +#ifndef _RAKNET_SUPPORT_NatPunchthroughServer +#define _RAKNET_SUPPORT_NatPunchthroughServer 1 +#endif +#ifndef _RAKNET_SUPPORT_NatTypeDetectionClient +#define _RAKNET_SUPPORT_NatTypeDetectionClient 1 +#endif +#ifndef _RAKNET_SUPPORT_NatTypeDetectionServer +#define _RAKNET_SUPPORT_NatTypeDetectionServer 1 +#endif +#ifndef _RAKNET_SUPPORT_PacketLogger +#define _RAKNET_SUPPORT_PacketLogger 1 +#endif +#ifndef _RAKNET_SUPPORT_ReadyEvent +#define _RAKNET_SUPPORT_ReadyEvent 1 +#endif +#ifndef _RAKNET_SUPPORT_ReplicaManager3 +#define _RAKNET_SUPPORT_ReplicaManager3 1 +#endif +#ifndef _RAKNET_SUPPORT_Router2 +#define _RAKNET_SUPPORT_Router2 1 +#endif +#ifndef _RAKNET_SUPPORT_RPC4Plugin +#define _RAKNET_SUPPORT_RPC4Plugin 1 +#endif +#ifndef _RAKNET_SUPPORT_TeamBalancer +#define _RAKNET_SUPPORT_TeamBalancer 1 +#endif +#ifndef _RAKNET_SUPPORT_TeamManager +#define _RAKNET_SUPPORT_TeamManager 1 +#endif +#ifndef _RAKNET_SUPPORT_UDPProxyClient +#define _RAKNET_SUPPORT_UDPProxyClient 1 +#endif +#ifndef _RAKNET_SUPPORT_UDPProxyCoordinator +#define _RAKNET_SUPPORT_UDPProxyCoordinator 1 +#endif +#ifndef _RAKNET_SUPPORT_UDPProxyServer +#define _RAKNET_SUPPORT_UDPProxyServer 1 +#endif +#ifndef _RAKNET_SUPPORT_ConsoleServer +#define _RAKNET_SUPPORT_ConsoleServer 1 +#endif +#ifndef _RAKNET_SUPPORT_RakNetTransport +#define _RAKNET_SUPPORT_RakNetTransport 1 +#endif +#ifndef _RAKNET_SUPPORT_TelnetTransport +#define _RAKNET_SUPPORT_TelnetTransport 1 +#endif +#ifndef _RAKNET_SUPPORT_TCPInterface +#define _RAKNET_SUPPORT_TCPInterface 1 +#endif +#ifndef _RAKNET_SUPPORT_LogCommandParser +#define _RAKNET_SUPPORT_LogCommandParser 1 +#endif +#ifndef _RAKNET_SUPPORT_RakNetCommandParser +#define _RAKNET_SUPPORT_RakNetCommandParser 1 +#endif +#ifndef _RAKNET_SUPPORT_EmailSender +#define _RAKNET_SUPPORT_EmailSender 1 +#endif +#ifndef _RAKNET_SUPPORT_HTTPConnection +#define _RAKNET_SUPPORT_HTTPConnection 1 +#endif +#ifndef _RAKNET_SUPPORT_HTTPConnection2 +#define _RAKNET_SUPPORT_HTTPConnection2 1 +#endif +#ifndef _RAKNET_SUPPORT_PacketizedTCP +#define _RAKNET_SUPPORT_PacketizedTCP 1 +#endif +#ifndef _RAKNET_SUPPORT_TwoWayAuthentication +#define _RAKNET_SUPPORT_TwoWayAuthentication 1 +#endif +#ifndef _RAKNET_SUPPORT_CloudClient +#define _RAKNET_SUPPORT_CloudClient 1 +#endif +#ifndef _RAKNET_SUPPORT_CloudServer +#define _RAKNET_SUPPORT_CloudServer 1 +#endif +#ifndef _RAKNET_SUPPORT_DynDNS +#define _RAKNET_SUPPORT_DynDNS 1 +#endif +#ifndef _RAKNET_SUPPORT_Rackspace +#define _RAKNET_SUPPORT_Rackspace 1 +#endif +#ifndef _RAKNET_SUPPORT_FileOperations +#define _RAKNET_SUPPORT_FileOperations 1 +#endif +#ifndef _RAKNET_SUPPORT_UDPForwarder +#define _RAKNET_SUPPORT_UDPForwarder 1 +#endif +#ifndef _RAKNET_SUPPORT_StatisticsHistory +#define _RAKNET_SUPPORT_StatisticsHistory 1 +#endif +#ifndef _RAKNET_SUPPORT_LibVoice +#define _RAKNET_SUPPORT_LibVoice 0 +#endif +#ifndef _RAKNET_SUPPORT_RelayPlugin +#define _RAKNET_SUPPORT_RelayPlugin 1 +#endif + +// Take care of dependencies +#if _RAKNET_SUPPORT_DirectoryDeltaTransfer==1 +#undef _RAKNET_SUPPORT_FileListTransfer +#define _RAKNET_SUPPORT_FileListTransfer 1 +#endif +#if _RAKNET_SUPPORT_FullyConnectedMesh2==1 +#undef _RAKNET_SUPPORT_ConnectionGraph2 +#define _RAKNET_SUPPORT_ConnectionGraph2 1 +#endif +#if _RAKNET_SUPPORT_TelnetTransport==1 +#undef _RAKNET_SUPPORT_PacketizedTCP +#define _RAKNET_SUPPORT_PacketizedTCP 1 +#endif +#if _RAKNET_SUPPORT_PacketizedTCP==1 || _RAKNET_SUPPORT_EmailSender==1 || _RAKNET_SUPPORT_HTTPConnection==1 +#undef _RAKNET_SUPPORT_TCPInterface +#define _RAKNET_SUPPORT_TCPInterface 1 +#endif + + + + + + + + + + + + diff --git a/Source/NativeFeatureIncludesOverrides.h b/Source/NativeFeatureIncludesOverrides.h index 5d11b3509..2d54f1678 100644 --- a/Source/NativeFeatureIncludesOverrides.h +++ b/Source/NativeFeatureIncludesOverrides.h @@ -1,19 +1,17 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -// USER EDITABLE FILE -// See NativeFeatureIncludes.h - -#ifndef __NATIVE_FEATURE_INCLDUES_OVERRIDES_H -#define __NATIVE_FEATURE_INCLDUES_OVERRIDES_H - -//#define LIBCAT_SECURITY 1 - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +// USER EDITABLE FILE +// See NativeFeatureIncludes.h + +#pragma once + +//#define LIBCAT_SECURITY 1 + diff --git a/Source/NativeTypes.h b/Source/NativeTypes.h index ef70ffd3c..de3653fc8 100644 --- a/Source/NativeTypes.h +++ b/Source/NativeTypes.h @@ -8,26 +8,6 @@ * */ -#ifndef __NATIVE_TYPES_H -#define __NATIVE_TYPES_H +#pragma once -#if defined(__GNUC__) || defined(__GCCXML__) || defined(__SNC__) || defined(__S3E__) -#include -#elif !defined(_STDINT_H) && !defined(_SN_STDINT_H) && !defined(_SYS_STDINT_H_) && !defined(_STDINT) && !defined(_MACHTYPES_H_) && !defined(_STDINT_H_) - typedef unsigned char uint8_t; - typedef unsigned short uint16_t; - typedef unsigned __int32 uint32_t; - typedef signed char int8_t; - typedef signed short int16_t; - typedef __int32 int32_t; - #if defined(_MSC_VER) && _MSC_VER < 1300 - typedef unsigned __int64 uint64_t; - typedef signed __int64 int64_t; - #else - typedef unsigned long long int uint64_t; - typedef signed long long int64_t; - #endif -#endif - - -#endif +#include diff --git a/Source/NetworkIDManager.h b/Source/NetworkIDManager.h index 794de1daf..b7e63822c 100644 --- a/Source/NetworkIDManager.h +++ b/Source/NetworkIDManager.h @@ -1,80 +1,78 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// - - -#ifndef __NETWORK_ID_MANAGER_H -#define __NETWORK_ID_MANAGER_H - -#include "RakNetTypes.h" -#include "Export.h" -#include "RakMemoryOverride.h" -#include "NetworkIDObject.h" -#include "Rand.h" - -namespace RakNet -{ - -/// Increase this value if you plan to have many persistent objects -/// This value must match on all systems -#define NETWORK_ID_MANAGER_HASH_LENGTH 1024 - -/// This class is simply used to generate a unique number for a group of instances of NetworkIDObject -/// An instance of this class is required to use the ObjectID to pointer lookup system -/// You should have one instance of this class per game instance. -/// Call SetIsNetworkIDAuthority before using any functions of this class, or of NetworkIDObject -class RAK_DLL_EXPORT NetworkIDManager -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(NetworkIDManager) - - NetworkIDManager(); - virtual ~NetworkIDManager(void); - - /// Returns the parent object, or this instance if you don't use a parent. - /// Supports NetworkIDObject anywhere in the inheritance hierarchy - /// \pre You must first call SetNetworkIDManager before using this function - template - returnType GET_OBJECT_FROM_ID(NetworkID x) { - NetworkIDObject *nio = GET_BASE_OBJECT_FROM_ID(x); - if (nio==0) - return 0; - if (nio->GetParent()) - return (returnType) nio->GetParent(); - return (returnType) nio; - } - - // Stop tracking all NetworkID objects - void Clear(void); - - /// \internal - NetworkIDObject *GET_BASE_OBJECT_FROM_ID(NetworkID x); - -protected: - /// \internal - void TrackNetworkIDObject(NetworkIDObject *networkIdObject); - void StopTrackingNetworkIDObject(NetworkIDObject *networkIdObject); - - friend class NetworkIDObject; - - NetworkIDObject *networkIdHash[NETWORK_ID_MANAGER_HASH_LENGTH]; - unsigned int NetworkIDToHashIndex(NetworkID networkId); - uint64_t startingOffset; - /// \internal - NetworkID GetNewNetworkID(void); - -}; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// + + +#pragma once + +#include "RakNetTypes.h" +#include "Export.h" +#include "RakMemoryOverride.h" +#include "NetworkIDObject.h" +#include "Rand.h" + +namespace RakNet +{ + +/// Increase this value if you plan to have many persistent objects +/// This value must match on all systems +#define NETWORK_ID_MANAGER_HASH_LENGTH 1024 + +/// This class is simply used to generate a unique number for a group of instances of NetworkIDObject +/// An instance of this class is required to use the ObjectID to pointer lookup system +/// You should have one instance of this class per game instance. +/// Call SetIsNetworkIDAuthority before using any functions of this class, or of NetworkIDObject +class RAK_DLL_EXPORT NetworkIDManager +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(NetworkIDManager) + + NetworkIDManager(); + virtual ~NetworkIDManager(void); + + /// Returns the parent object, or this instance if you don't use a parent. + /// Supports NetworkIDObject anywhere in the inheritance hierarchy + /// \pre You must first call SetNetworkIDManager before using this function + template + returnType GET_OBJECT_FROM_ID(NetworkID x) { + NetworkIDObject *nio = GET_BASE_OBJECT_FROM_ID(x); + if (nio==0) + return 0; + if (nio->GetParent()) + return (returnType) nio->GetParent(); + return (returnType) nio; + } + + // Stop tracking all NetworkID objects + void Clear(void); + + /// \internal + NetworkIDObject *GET_BASE_OBJECT_FROM_ID(NetworkID x); + +protected: + /// \internal + void TrackNetworkIDObject(NetworkIDObject *networkIdObject); + void StopTrackingNetworkIDObject(NetworkIDObject *networkIdObject); + + friend class NetworkIDObject; + + NetworkIDObject *networkIdHash[NETWORK_ID_MANAGER_HASH_LENGTH]; + unsigned int NetworkIDToHashIndex(NetworkID networkId); + uint64_t startingOffset; + /// \internal + NetworkID GetNewNetworkID(void); + +}; + +} // namespace RakNet + diff --git a/Source/NetworkIDObject.h b/Source/NetworkIDObject.h index 9ee64845e..65f7987ab 100644 --- a/Source/NetworkIDObject.h +++ b/Source/NetworkIDObject.h @@ -1,89 +1,87 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief A class you can derive from to make it easier to represent every networked object with an integer. This way you can refer to objects over the network. -/// - - -#if !defined(__NETWORK_ID_GENERATOR) -#define __NETWORK_ID_GENERATOR - -#include "RakNetTypes.h" -#include "RakMemoryOverride.h" -#include "Export.h" - -namespace RakNet -{ -/// Forward declarations -class NetworkIDManager; - -typedef uint32_t NetworkIDType; - -/// \brief Unique shared ids for each object instance -/// \details A class you can derive from to make it easier to represent every networked object with an integer. This way you can refer to objects over the network. -/// One system should return true for IsNetworkIDAuthority() and the rest should return false. When an object needs to be created, have the the one system create the object. -/// Then have that system send a message to all other systems, and include the value returned from GetNetworkID() in that packet. All other systems should then create the same -/// class of object, and call SetNetworkID() on that class with the NetworkID in the packet. -/// \see the manual for more information on this. -class RAK_DLL_EXPORT NetworkIDObject -{ -public: - // Constructor. NetworkIDs, if IsNetworkIDAuthority() is true, are created here. - NetworkIDObject(); - - // Destructor. Used NetworkIDs, if any, are freed here. - virtual ~NetworkIDObject(); - - /// Sets the manager class from which to request unique network IDs - /// Unlike previous versions, the NetworkIDObject relies on a manager class to provide IDs, rather than using statics, - /// So you can have more than one set of IDs on the same system. - virtual void SetNetworkIDManager( NetworkIDManager *manager); - - /// Returns what was passed to SetNetworkIDManager - virtual NetworkIDManager * GetNetworkIDManager( void ) const; - - /// Returns the NetworkID that you can use to refer to this object over the network. - /// \pre You must first call SetNetworkIDManager before using this function - /// \retval UNASSIGNED_NETWORK_ID UNASSIGNED_NETWORK_ID is returned IsNetworkIDAuthority() is false and SetNetworkID() was not previously called. This is also returned if you call this function in the constructor. - /// \retval 0-65534 Any other value is a valid NetworkID. NetworkIDs start at 0 and go to 65534, wrapping at that point. - virtual NetworkID GetNetworkID( void ); - - /// Sets the NetworkID for this instance. Usually this is called by the clients and determined from the servers. However, if you save multiplayer games you would likely use - /// This on load as well. - virtual void SetNetworkID( NetworkID id ); - - /// Your class does not have to derive from NetworkIDObject, although that is the easiest way to implement this. - /// If you want this to be a member object of another class, rather than inherit, then call SetParent() with a pointer to the parent class instance. - /// GET_OBJECT_FROM_ID will then return the parent rather than this instance. - virtual void SetParent( void *_parent ); - - /// Return what was passed to SetParent - /// \return The value passed to SetParent, or 0 if it was never called. - virtual void* GetParent( void ) const; - -protected: - - /// The network ID of this object - // networkID is assigned when networkIDManager is set. - NetworkID networkID; - NetworkIDManager *networkIDManager; - - /// The parent set by SetParent() - void *parent; - - /// \internal, used by NetworkIDManager - friend class NetworkIDManager; - NetworkIDObject *nextInstanceForNetworkIDManager; -}; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief A class you can derive from to make it easier to represent every networked object with an integer. This way you can refer to objects over the network. +/// + + +#pragma once + +#include "RakNetTypes.h" +#include "RakMemoryOverride.h" +#include "Export.h" + +namespace RakNet +{ +/// Forward declarations +class NetworkIDManager; + +typedef uint32_t NetworkIDType; + +/// \brief Unique shared ids for each object instance +/// \details A class you can derive from to make it easier to represent every networked object with an integer. This way you can refer to objects over the network. +/// One system should return true for IsNetworkIDAuthority() and the rest should return false. When an object needs to be created, have the the one system create the object. +/// Then have that system send a message to all other systems, and include the value returned from GetNetworkID() in that packet. All other systems should then create the same +/// class of object, and call SetNetworkID() on that class with the NetworkID in the packet. +/// \see the manual for more information on this. +class RAK_DLL_EXPORT NetworkIDObject +{ +public: + // Constructor. NetworkIDs, if IsNetworkIDAuthority() is true, are created here. + NetworkIDObject(); + + // Destructor. Used NetworkIDs, if any, are freed here. + virtual ~NetworkIDObject(); + + /// Sets the manager class from which to request unique network IDs + /// Unlike previous versions, the NetworkIDObject relies on a manager class to provide IDs, rather than using statics, + /// So you can have more than one set of IDs on the same system. + virtual void SetNetworkIDManager( NetworkIDManager *manager); + + /// Returns what was passed to SetNetworkIDManager + virtual NetworkIDManager * GetNetworkIDManager( void ) const; + + /// Returns the NetworkID that you can use to refer to this object over the network. + /// \pre You must first call SetNetworkIDManager before using this function + /// \retval UNASSIGNED_NETWORK_ID UNASSIGNED_NETWORK_ID is returned IsNetworkIDAuthority() is false and SetNetworkID() was not previously called. This is also returned if you call this function in the constructor. + /// \retval 0-65534 Any other value is a valid NetworkID. NetworkIDs start at 0 and go to 65534, wrapping at that point. + virtual NetworkID GetNetworkID( void ); + + /// Sets the NetworkID for this instance. Usually this is called by the clients and determined from the servers. However, if you save multiplayer games you would likely use + /// This on load as well. + virtual void SetNetworkID( NetworkID id ); + + /// Your class does not have to derive from NetworkIDObject, although that is the easiest way to implement this. + /// If you want this to be a member object of another class, rather than inherit, then call SetParent() with a pointer to the parent class instance. + /// GET_OBJECT_FROM_ID will then return the parent rather than this instance. + virtual void SetParent( void *_parent ); + + /// Return what was passed to SetParent + /// \return The value passed to SetParent, or 0 if it was never called. + virtual void* GetParent( void ) const; + +protected: + + /// The network ID of this object + // networkID is assigned when networkIDManager is set. + NetworkID networkID; + NetworkIDManager *networkIDManager; + + /// The parent set by SetParent() + void *parent; + + /// \internal, used by NetworkIDManager + friend class NetworkIDManager; + NetworkIDObject *nextInstanceForNetworkIDManager; +}; + +} // namespace RakNet + diff --git a/Source/PacketConsoleLogger.h b/Source/PacketConsoleLogger.h index db9ea1985..71dcb1ecb 100644 --- a/Source/PacketConsoleLogger.h +++ b/Source/PacketConsoleLogger.h @@ -1,45 +1,43 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief This will write all incoming and outgoing network messages to the log command parser, which can be accessed through Telnet -/// - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_LogCommandParser==1 && _RAKNET_SUPPORT_PacketLogger==1 - -#ifndef __PACKET_CONSOLE_LOGGER_H_ -#define __PACKET_CONSOLE_LOGGER_H_ - -#include "PacketLogger.h" - -namespace RakNet -{ -/// Forward declarations -class LogCommandParser; - -/// \ingroup PACKETLOGGER_GROUP -/// \brief Packetlogger that logs to a remote command console -class RAK_DLL_EXPORT PacketConsoleLogger : public PacketLogger -{ -public: - PacketConsoleLogger(); - // Writes to the command parser used for logging, which is accessed through a secondary communication layer (such as Telnet or RakNet) - See ConsoleServer.h - virtual void SetLogCommandParser(LogCommandParser *lcp); - virtual void WriteLog(const char *str); -protected: - LogCommandParser *logCommandParser; -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief This will write all incoming and outgoing network messages to the log command parser, which can be accessed through Telnet +/// + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_LogCommandParser==1 && _RAKNET_SUPPORT_PacketLogger==1 + +#pragma once + +#include "PacketLogger.h" + +namespace RakNet +{ +/// Forward declarations +class LogCommandParser; + +/// \ingroup PACKETLOGGER_GROUP +/// \brief Packetlogger that logs to a remote command console +class RAK_DLL_EXPORT PacketConsoleLogger : public PacketLogger +{ +public: + PacketConsoleLogger(); + // Writes to the command parser used for logging, which is accessed through a secondary communication layer (such as Telnet or RakNet) - See ConsoleServer.h + virtual void SetLogCommandParser(LogCommandParser *lcp); + virtual void WriteLog(const char *str); +protected: + LogCommandParser *logCommandParser; +}; + +} // namespace RakNet + +#endif + diff --git a/Source/PacketFileLogger.h b/Source/PacketFileLogger.h index 97604a166..4e50e13c5 100644 --- a/Source/PacketFileLogger.h +++ b/Source/PacketFileLogger.h @@ -1,45 +1,43 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief This will write all incoming and outgoing network messages to a file -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_PacketLogger==1 - -#ifndef __PACKET_FILE_LOGGER_H_ -#define __PACKET_FILE_LOGGER_H_ - -#include "PacketLogger.h" -#include - -namespace RakNet -{ - -/// \ingroup PACKETLOGGER_GROUP -/// \brief Packetlogger that outputs to a file -class RAK_DLL_EXPORT PacketFileLogger : public PacketLogger -{ -public: - PacketFileLogger(); - virtual ~PacketFileLogger(); - void StartLog(const char *filenamePrefix); - virtual void WriteLog(const char *str); -protected: - FILE *packetLogFile; -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief This will write all incoming and outgoing network messages to a file +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_PacketLogger==1 + +#pragma once + +#include "PacketLogger.h" +#include + +namespace RakNet +{ + +/// \ingroup PACKETLOGGER_GROUP +/// \brief Packetlogger that outputs to a file +class RAK_DLL_EXPORT PacketFileLogger : public PacketLogger +{ +public: + PacketFileLogger(); + virtual ~PacketFileLogger(); + void StartLog(const char *filenamePrefix); + virtual void WriteLog(const char *str); +protected: + FILE *packetLogFile; +}; + +} // namespace RakNet + +#endif + diff --git a/Source/PacketLogger.cpp b/Source/PacketLogger.cpp index 6fa3224bf..593aa1c6a 100644 --- a/Source/PacketLogger.cpp +++ b/Source/PacketLogger.cpp @@ -1,477 +1,477 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_PacketLogger==1 - -#include "PacketLogger.h" -#include "BitStream.h" -#include "DS_List.h" -#include "InternalPacket.h" -#include "RakPeerInterface.h" -#include "MessageIdentifiers.h" -#include "StringCompressor.h" -#include "GetTime.h" -#include -#include -#include -#include "Itoa.h" -#include -#include "SocketIncludes.h" -#include "gettimeofday.h" - -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -using namespace RakNet; - -STATIC_FACTORY_DEFINITIONS(PacketLogger,PacketLogger); - -PacketLogger::PacketLogger() -{ - printId=true; - printAcks=true; - prefix[0]=0; - suffix[0]=0; - logDirectMessages=true; -} -PacketLogger::~PacketLogger() -{ -} -void PacketLogger::FormatLine( -char* into, const char* dir, const char* type, unsigned int reliableMessageNumber, unsigned int frame, unsigned char id -, const BitSize_t bitLen, unsigned long long time, const SystemAddress& local, const SystemAddress& remote, -unsigned int splitPacketId, unsigned int splitPacketIndex, unsigned int splitPacketCount, unsigned int orderingIndex) -{ - char numericID[16]; - const char* idToPrint = NULL; - if(printId) - { - if (splitPacketCount>0 && splitPacketCount!=(unsigned int)-1) - idToPrint="(SPLIT PACKET)"; - else - idToPrint = IDTOString(id); - } - // If printId is false, idToPrint will be NULL, as it will - // in the case of an unrecognized id. Testing printId for false - // would just be redundant. - if(idToPrint == NULL) - { - sprintf(numericID, "%5u", id); - idToPrint = numericID; - } - - FormatLine(into, dir, type, reliableMessageNumber, frame, idToPrint, bitLen, time, local, remote,splitPacketId,splitPacketIndex,splitPacketCount, orderingIndex); -} - -void PacketLogger::FormatLine( -char* into, const char* dir, const char* type, unsigned int reliableMessageNumber, unsigned int frame, const char* idToPrint -, const BitSize_t bitLen, unsigned long long time, const SystemAddress& local, const SystemAddress& remote, -unsigned int splitPacketId, unsigned int splitPacketIndex, unsigned int splitPacketCount, unsigned int orderingIndex) -{ - char str1[64], str2[62]; - local.ToString(true, str1); - remote.ToString(true, str2); - char localtime[128]; - GetLocalTime(localtime); - char str3[64]; - if (reliableMessageNumber==(unsigned int)-1) - { - str3[0]='N'; - str3[1]='/'; - str3[2]='A'; - str3[3]=0; - } - else - { - sprintf(str3,"%5u",reliableMessageNumber); - } - - sprintf(into, "%s,%s%s,%s,%s,%5u,%s,%u,%" PRINTF_64_BIT_MODIFIER "u,%s,%s,%i,%i,%i,%i,%s," - , localtime - , prefix - , dir - , type - , str3 - , frame - , idToPrint - , bitLen - , time - , str1 - , str2 - , splitPacketId - , splitPacketIndex - , splitPacketCount - , orderingIndex - , suffix - ); -} -void PacketLogger::OnDirectSocketSend(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) -{ - if (logDirectMessages==false) - return; - - char str[256]; - FormatLine(str, "Snd", "Raw", 0, 0, data[0], bitsUsed, RakNet::GetTimeMS(), rakPeerInterface->GetExternalID(remoteSystemAddress), remoteSystemAddress, (unsigned int)-1,(unsigned int)-1,(unsigned int)-1,(unsigned int)-1); - AddToLog(str); -} - -void PacketLogger::LogHeader(void) -{ - // Last 5 are splitpacket id, split packet index, split packet count, ordering index, suffix - AddToLog("Clock,S|R,Typ,Reliable#,Frm #,PktID,BitLn,Time ,Local IP:Port ,RemoteIP:Port,SPID,SPIN,SPCO,OI,Suffix,Miscellaneous\n"); -} -void PacketLogger::OnDirectSocketReceive(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) -{ - if (logDirectMessages==false) - return; - - char str[256]; - FormatLine(str, "Rcv", "Raw", 0, 0, data[0], bitsUsed, RakNet::GetTime(), rakPeerInterface->GetInternalID(UNASSIGNED_SYSTEM_ADDRESS), remoteSystemAddress,(unsigned int)-1,(unsigned int)-1,(unsigned int)-1,(unsigned int)-1); - AddToLog(str); -} -void PacketLogger::OnReliabilityLayerNotification(const char *errorMessage, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress, bool isError) -{ - char str[1024]; - char *type; - if (isError) - type=(char*) "RcvErr"; - else - type=(char*) "RcvWrn"; - FormatLine(str, type, errorMessage, 0, 0, "", bitsUsed, RakNet::GetTime(), rakPeerInterface->GetInternalID(UNASSIGNED_SYSTEM_ADDRESS), remoteSystemAddress,(unsigned int)-1,(unsigned int)-1,(unsigned int)-1,(unsigned int)-1); - AddToLog(str); - RakAssert(isError==false); -} -void PacketLogger::OnAck(unsigned int messageNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time) -{ - char str[256]; - char str1[64], str2[62]; - SystemAddress localSystemAddress = rakPeerInterface->GetExternalID(remoteSystemAddress); - localSystemAddress.ToString(true, str1); - remoteSystemAddress.ToString(true, str2); - char localtime[128]; - GetLocalTime(localtime); - - sprintf(str, "%s,Rcv,Ack,%i,,,,%" PRINTF_64_BIT_MODIFIER "u,%s,%s,,,,,," - , localtime - , messageNumber - , (unsigned long long) time - , str1 - , str2 - ); - AddToLog(str); -} -void PacketLogger::OnPushBackPacket(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) -{ - char str[256]; - char str1[64], str2[62]; - SystemAddress localSystemAddress = rakPeerInterface->GetExternalID(remoteSystemAddress); - localSystemAddress.ToString(true, str1); - remoteSystemAddress.ToString(true, str2); - RakNet::TimeMS time = RakNet::GetTimeMS(); - char localtime[128]; - GetLocalTime(localtime); - - sprintf(str, "%s,Lcl,PBP,,,%s,%i,%" PRINTF_64_BIT_MODIFIER "u,%s,%s,,,,,," - , localtime - , BaseIDTOString(data[0]) - , bitsUsed - , (unsigned long long) time - , str1 - , str2 - ); - AddToLog(str); -} -void PacketLogger::OnInternalPacket(InternalPacket *internalPacket, unsigned frameNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time, int isSend) -{ - char str[256]; - const char *sendTypes[] = - { - "Rcv", - "Snd", - "Err1", - "Err2", - "Err3", - "Err4", - "Err5", - "Err6", - }; - const char *sendType = sendTypes[isSend]; - SystemAddress localSystemAddress = rakPeerInterface->GetExternalID(remoteSystemAddress); - - unsigned int reliableMessageNumber; - if (internalPacket->reliability==UNRELIABLE || internalPacket->reliability==UNRELIABLE_SEQUENCED || internalPacket->reliability==UNRELIABLE_WITH_ACK_RECEIPT) - reliableMessageNumber=(unsigned int)-1; - else - reliableMessageNumber=internalPacket->reliableMessageNumber; - - if (internalPacket->data[0]==ID_TIMESTAMP) - { - FormatLine(str, sendType, "Tms", reliableMessageNumber, frameNumber, internalPacket->data[1+sizeof(RakNet::Time)], internalPacket->dataBitLength, (unsigned long long)time, localSystemAddress, remoteSystemAddress, internalPacket->splitPacketId, internalPacket->splitPacketIndex, internalPacket->splitPacketCount, internalPacket->orderingIndex); - } - else - { - FormatLine(str, sendType, "Nrm", reliableMessageNumber, frameNumber, internalPacket->data[0], internalPacket->dataBitLength, (unsigned long long)time, localSystemAddress, remoteSystemAddress, internalPacket->splitPacketId, internalPacket->splitPacketIndex, internalPacket->splitPacketCount, internalPacket->orderingIndex); - } - - AddToLog(str); -} -void PacketLogger::AddToLog(const char *str) -{ - WriteLog(str); -} -void PacketLogger::WriteLog(const char *str) -{ - RAKNET_DEBUG_PRINTF("%s\n", str); -} -void PacketLogger::WriteMiscellaneous(const char *type, const char *msg) -{ - char str[1024]; - char str1[64]; - SystemAddress localSystemAddress = rakPeerInterface->GetInternalID(); - localSystemAddress.ToString(true, str1); - RakNet::TimeMS time = RakNet::GetTimeMS(); - char localtime[128]; - GetLocalTime(localtime); - - sprintf(str, "%s,Lcl,%s,,,,,%" PRINTF_64_BIT_MODIFIER "u,%s,,,,,,,%s" - , localtime - , type - , (unsigned long long) time - , str1 - , msg - ); - - AddToLog(msg); -} -void PacketLogger::SetPrintID(bool print) -{ - printId=print; -} -void PacketLogger::SetPrintAcks(bool print) -{ - printAcks=print; -} -const char* PacketLogger::BaseIDTOString(unsigned char Id) -{ - if (Id >= ID_USER_PACKET_ENUM) - return 0; - - const char *IDTable[((int)ID_USER_PACKET_ENUM)+1]= - { - "ID_CONNECTED_PING", - "ID_UNCONNECTED_PING", - "ID_UNCONNECTED_PING_OPEN_CONNECTIONS", - "ID_CONNECTED_PONG", - "ID_DETECT_LOST_CONNECTIONS", - "ID_OPEN_CONNECTION_REQUEST_1", - "ID_OPEN_CONNECTION_REPLY_1", - "ID_OPEN_CONNECTION_REQUEST_2", - "ID_OPEN_CONNECTION_REPLY_2", - "ID_CONNECTION_REQUEST", - "ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY", - "ID_OUR_SYSTEM_REQUIRES_SECURITY", - "ID_PUBLIC_KEY_MISMATCH", - "ID_OUT_OF_BAND_INTERNAL", - "ID_SND_RECEIPT_ACKED", - "ID_SND_RECEIPT_LOSS", - "ID_CONNECTION_REQUEST_ACCEPTED", - "ID_CONNECTION_ATTEMPT_FAILED", - "ID_ALREADY_CONNECTED", - "ID_NEW_INCOMING_CONNECTION", - "ID_NO_FREE_INCOMING_CONNECTIONS", - "ID_DISCONNECTION_NOTIFICATION", - "ID_CONNECTION_LOST", - "ID_CONNECTION_BANNED", - "ID_INVALID_PASSWORD", - "ID_INCOMPATIBLE_PROTOCOL_VERSION", - "ID_IP_RECENTLY_CONNECTED", - "ID_TIMESTAMP", - "ID_UNCONNECTED_PONG", - "ID_ADVERTISE_SYSTEM", - "ID_DOWNLOAD_PROGRESS", - "ID_REMOTE_DISCONNECTION_NOTIFICATION", - "ID_REMOTE_CONNECTION_LOST", - "ID_REMOTE_NEW_INCOMING_CONNECTION", - "ID_FILE_LIST_TRANSFER_HEADER", - "ID_FILE_LIST_TRANSFER_FILE", - "ID_FILE_LIST_REFERENCE_PUSH_ACK", - "ID_DDT_DOWNLOAD_REQUEST", - "ID_TRANSPORT_STRING", - "ID_REPLICA_MANAGER_CONSTRUCTION", - "ID_REPLICA_MANAGER_SCOPE_CHANGE", - "ID_REPLICA_MANAGER_SERIALIZE", - "ID_REPLICA_MANAGER_DOWNLOAD_STARTED", - "ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE", - "ID_RAKVOICE_OPEN_CHANNEL_REQUEST", - "ID_RAKVOICE_OPEN_CHANNEL_REPLY", - "ID_RAKVOICE_CLOSE_CHANNEL", - "ID_RAKVOICE_DATA", - "ID_AUTOPATCHER_GET_CHANGELIST_SINCE_DATE", - "ID_AUTOPATCHER_CREATION_LIST", - "ID_AUTOPATCHER_DELETION_LIST", - "ID_AUTOPATCHER_GET_PATCH", - "ID_AUTOPATCHER_PATCH_LIST", - "ID_AUTOPATCHER_REPOSITORY_FATAL_ERROR", - "ID_AUTOPATCHER_CANNOT_DOWNLOAD_ORIGINAL_UNMODIFIED_FILES", - "ID_AUTOPATCHER_FINISHED_INTERNAL", - "ID_AUTOPATCHER_FINISHED", - "ID_AUTOPATCHER_RESTART_APPLICATION", - "ID_NAT_PUNCHTHROUGH_REQUEST", - "ID_NAT_CONNECT_AT_TIME", - "ID_NAT_GET_MOST_RECENT_PORT", - "ID_NAT_CLIENT_READY", - "ID_NAT_TARGET_NOT_CONNECTED", - "ID_NAT_TARGET_UNRESPONSIVE", - "ID_NAT_CONNECTION_TO_TARGET_LOST", - "ID_NAT_ALREADY_IN_PROGRESS", - "ID_NAT_PUNCHTHROUGH_FAILED", - "ID_NAT_PUNCHTHROUGH_SUCCEEDED", - "ID_READY_EVENT_SET", - "ID_READY_EVENT_UNSET", - "ID_READY_EVENT_ALL_SET", - "ID_READY_EVENT_QUERY", - "ID_LOBBY_GENERAL", - "ID_RPC_REMOTE_ERROR", - "ID_RPC_PLUGIN", - "ID_FILE_LIST_REFERENCE_PUSH", - "ID_READY_EVENT_FORCE_ALL_SET", - "ID_ROOMS_EXECUTE_FUNC", - "ID_ROOMS_LOGON_STATUS", - "ID_ROOMS_HANDLE_CHANGE", - "ID_LOBBY2_SEND_MESSAGE", - "ID_LOBBY2_SERVER_ERROR", - "ID_FCM2_NEW_HOST", - "ID_FCM2_REQUEST_FCMGUID", - "ID_FCM2_RESPOND_CONNECTION_COUNT", - "ID_FCM2_INFORM_FCMGUID", - "ID_FCM2_UPDATE_MIN_TOTAL_CONNECTION_COUNT", - "ID_FCM2_VERIFIED_JOIN_START", - "ID_FCM2_VERIFIED_JOIN_CAPABLE", - "ID_FCM2_VERIFIED_JOIN_FAILED", - "ID_FCM2_VERIFIED_JOIN_ACCEPTED", - "ID_FCM2_VERIFIED_JOIN_REJECTED", - "ID_UDP_PROXY_GENERAL", - "ID_SQLite3_EXEC", - "ID_SQLite3_UNKNOWN_DB", - "ID_SQLLITE_LOGGER", - "ID_NAT_TYPE_DETECTION_REQUEST", - "ID_NAT_TYPE_DETECTION_RESULT", - "ID_ROUTER_2_INTERNAL", - "ID_ROUTER_2_FORWARDING_NO_PATH", - "ID_ROUTER_2_FORWARDING_ESTABLISHED", - "ID_ROUTER_2_REROUTED", - "ID_TEAM_BALANCER_INTERNAL", - "ID_TEAM_BALANCER_REQUESTED_TEAM_FULL", - "ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED", - "ID_TEAM_BALANCER_TEAM_REQUESTED_CANCELLED", - "ID_TEAM_BALANCER_TEAM_ASSIGNED", - "ID_LIGHTSPEED_INTEGRATION", - "ID_XBOX_LOBBY", - "ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_SUCCESS", - "ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_SUCCESS", - "ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_FAILURE", - "ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_FAILURE", - "ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_TIMEOUT", - "ID_TWO_WAY_AUTHENTICATION_NEGOTIATION", - "ID_CLOUD_POST_REQUEST", - "ID_CLOUD_RELEASE_REQUEST", - "ID_CLOUD_GET_REQUEST", - "ID_CLOUD_GET_RESPONSE", - "ID_CLOUD_UNSUBSCRIBE_REQUEST", - "ID_CLOUD_SERVER_TO_SERVER_COMMAND", - "ID_CLOUD_SUBSCRIPTION_NOTIFICATION", - "ID_LIB_VOICE", - "ID_RELAY_PLUGIN", - "ID_NAT_REQUEST_BOUND_ADDRESSES", - "ID_NAT_RESPOND_BOUND_ADDRESSES", - "ID_FCM2_UPDATE_USER_CONTEXT", - "ID_RESERVED_3", - "ID_RESERVED_4", - "ID_RESERVED_5", - "ID_RESERVED_6", - "ID_RESERVED_7", - "ID_RESERVED_8", - "ID_RESERVED_9", - "ID_USER_PACKET_ENUM" - }; - - return (char*)IDTable[Id]; -} -const char* PacketLogger::UserIDTOString(unsigned char Id) -{ - // Users should override this - static char str[256]; - Itoa(Id, str, 10); - return (const char*) str; -} -const char* PacketLogger::IDTOString(unsigned char Id) -{ - const char *out; - out=BaseIDTOString(Id); - if (out) - return out; - return UserIDTOString(Id); -} -void PacketLogger::SetPrefix(const char *_prefix) -{ - strncpy(prefix, _prefix, 255); - prefix[255]=0; -} -void PacketLogger::SetSuffix(const char *_suffix) -{ - strncpy(suffix, _suffix, 255); - suffix[255]=0; -} -void PacketLogger::GetLocalTime(char buffer[128]) -{ -#if defined(_WIN32) && !defined(__GNUC__) && !defined(__GCCXML__) - time_t rawtime; - struct timeval tv; - // If you get an arror about an incomplete type, just delete this file - struct timezone tz; - gettimeofday(&tv, &tz); - // time ( &rawtime ); - rawtime=tv.tv_sec; - - struct tm * timeinfo; - timeinfo = localtime ( &rawtime ); - strftime (buffer,128,"%x %X",timeinfo); - char buff[32]; - sprintf(buff, ".%i", tv.tv_usec); - strcat(buffer,buff); - - // Commented version puts the time first - /* - struct tm * timeinfo; - timeinfo = localtime ( &rawtime ); - strftime (buffer,128,"%X",timeinfo); - char buff[32]; - sprintf(buff, ".%i ", tv.tv_usec); - strcat(buffer,buff); - char buff2[32]; - strftime (buff2,32,"%x",timeinfo); - strcat(buffer,buff2); - */ -#else - buffer[0]=0; -#endif -} -void PacketLogger::SetLogDirectMessages(bool send) -{ - logDirectMessages=send; -} - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_PacketLogger==1 + +#include "PacketLogger.h" +#include "BitStream.h" +#include "DS_List.h" +#include "InternalPacket.h" +#include "RakPeerInterface.h" +#include "MessageIdentifiers.h" +#include "StringCompressor.h" +#include "GetTime.h" +#include +#include +#include +#include "Itoa.h" +#include +#include "SocketIncludes.h" +#include "gettimeofday.h" + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +using namespace RakNet; + +STATIC_FACTORY_DEFINITIONS(PacketLogger,PacketLogger); + +PacketLogger::PacketLogger() +{ + printId=true; + printAcks=true; + prefix[0]=0; + suffix[0]=0; + logDirectMessages=true; +} +PacketLogger::~PacketLogger() +{ +} +void PacketLogger::FormatLine( +char* into, const char* dir, const char* type, unsigned int reliableMessageNumber, unsigned int frame, unsigned char id +, const BitSize_t bitLen, unsigned long long time, const SystemAddress& local, const SystemAddress& remote, +unsigned int splitPacketId, unsigned int splitPacketIndex, unsigned int splitPacketCount, unsigned int orderingIndex) +{ + char numericID[16]; + const char* idToPrint = nullptr; + if(printId) + { + if (splitPacketCount>0 && splitPacketCount!=(unsigned int)-1) + idToPrint="(SPLIT PACKET)"; + else + idToPrint = IDTOString(id); + } + // If printId is false, idToPrint will be nullptr, as it will + // in the case of an unrecognized id. Testing printId for false + // would just be redundant. + if(idToPrint == nullptr) + { + sprintf(numericID, "%5u", id); + idToPrint = numericID; + } + + FormatLine(into, dir, type, reliableMessageNumber, frame, idToPrint, bitLen, time, local, remote,splitPacketId,splitPacketIndex,splitPacketCount, orderingIndex); +} + +void PacketLogger::FormatLine( +char* into, const char* dir, const char* type, unsigned int reliableMessageNumber, unsigned int frame, const char* idToPrint +, const BitSize_t bitLen, unsigned long long time, const SystemAddress& local, const SystemAddress& remote, +unsigned int splitPacketId, unsigned int splitPacketIndex, unsigned int splitPacketCount, unsigned int orderingIndex) +{ + char str1[64], str2[62]; + local.ToString(true, str1); + remote.ToString(true, str2); + char localtime[128]; + GetLocalTime(localtime); + char str3[64]; + if (reliableMessageNumber==(unsigned int)-1) + { + str3[0]='N'; + str3[1]='/'; + str3[2]='A'; + str3[3]=0; + } + else + { + sprintf(str3,"%5u",reliableMessageNumber); + } + + sprintf(into, "%s,%s%s,%s,%s,%5u,%s,%u,%" PRINTF_64_BIT_MODIFIER "u,%s,%s,%i,%i,%i,%i,%s," + , localtime + , prefix + , dir + , type + , str3 + , frame + , idToPrint + , bitLen + , time + , str1 + , str2 + , splitPacketId + , splitPacketIndex + , splitPacketCount + , orderingIndex + , suffix + ); +} +void PacketLogger::OnDirectSocketSend(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) +{ + if (logDirectMessages==false) + return; + + char str[256]; + FormatLine(str, "Snd", "Raw", 0, 0, data[0], bitsUsed, RakNet::GetTimeMS(), rakPeerInterface->GetExternalID(remoteSystemAddress), remoteSystemAddress, (unsigned int)-1,(unsigned int)-1,(unsigned int)-1,(unsigned int)-1); + AddToLog(str); +} + +void PacketLogger::LogHeader(void) +{ + // Last 5 are splitpacket id, split packet index, split packet count, ordering index, suffix + AddToLog("Clock,S|R,Typ,Reliable#,Frm #,PktID,BitLn,Time ,Local IP:Port ,RemoteIP:Port,SPID,SPIN,SPCO,OI,Suffix,Miscellaneous\n"); +} +void PacketLogger::OnDirectSocketReceive(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) +{ + if (logDirectMessages==false) + return; + + char str[256]; + FormatLine(str, "Rcv", "Raw", 0, 0, data[0], bitsUsed, RakNet::GetTime(), rakPeerInterface->GetInternalID(UNASSIGNED_SYSTEM_ADDRESS), remoteSystemAddress,(unsigned int)-1,(unsigned int)-1,(unsigned int)-1,(unsigned int)-1); + AddToLog(str); +} +void PacketLogger::OnReliabilityLayerNotification(const char *errorMessage, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress, bool isError) +{ + char str[1024]; + char *type; + if (isError) + type=(char*) "RcvErr"; + else + type=(char*) "RcvWrn"; + FormatLine(str, type, errorMessage, 0, 0, "", bitsUsed, RakNet::GetTime(), rakPeerInterface->GetInternalID(UNASSIGNED_SYSTEM_ADDRESS), remoteSystemAddress,(unsigned int)-1,(unsigned int)-1,(unsigned int)-1,(unsigned int)-1); + AddToLog(str); + RakAssert(isError==false); +} +void PacketLogger::OnAck(unsigned int messageNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time) +{ + char str[256]; + char str1[64], str2[62]; + SystemAddress localSystemAddress = rakPeerInterface->GetExternalID(remoteSystemAddress); + localSystemAddress.ToString(true, str1); + remoteSystemAddress.ToString(true, str2); + char localtime[128]; + GetLocalTime(localtime); + + sprintf(str, "%s,Rcv,Ack,%i,,,,%" PRINTF_64_BIT_MODIFIER "u,%s,%s,,,,,," + , localtime + , messageNumber + , (unsigned long long) time + , str1 + , str2 + ); + AddToLog(str); +} +void PacketLogger::OnPushBackPacket(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) +{ + char str[256]; + char str1[64], str2[62]; + SystemAddress localSystemAddress = rakPeerInterface->GetExternalID(remoteSystemAddress); + localSystemAddress.ToString(true, str1); + remoteSystemAddress.ToString(true, str2); + RakNet::TimeMS time = RakNet::GetTimeMS(); + char localtime[128]; + GetLocalTime(localtime); + + sprintf(str, "%s,Lcl,PBP,,,%s,%i,%" PRINTF_64_BIT_MODIFIER "u,%s,%s,,,,,," + , localtime + , BaseIDTOString(data[0]) + , bitsUsed + , (unsigned long long) time + , str1 + , str2 + ); + AddToLog(str); +} +void PacketLogger::OnInternalPacket(InternalPacket *internalPacket, unsigned frameNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time, int isSend) +{ + char str[256]; + const char *sendTypes[] = + { + "Rcv", + "Snd", + "Err1", + "Err2", + "Err3", + "Err4", + "Err5", + "Err6", + }; + const char *sendType = sendTypes[isSend]; + SystemAddress localSystemAddress = rakPeerInterface->GetExternalID(remoteSystemAddress); + + unsigned int reliableMessageNumber; + if (internalPacket->reliability==UNRELIABLE || internalPacket->reliability==UNRELIABLE_SEQUENCED || internalPacket->reliability==UNRELIABLE_WITH_ACK_RECEIPT) + reliableMessageNumber=(unsigned int)-1; + else + reliableMessageNumber=internalPacket->reliableMessageNumber; + + if (internalPacket->data[0]==ID_TIMESTAMP) + { + FormatLine(str, sendType, "Tms", reliableMessageNumber, frameNumber, internalPacket->data[1+sizeof(RakNet::Time)], internalPacket->dataBitLength, (unsigned long long)time, localSystemAddress, remoteSystemAddress, internalPacket->splitPacketId, internalPacket->splitPacketIndex, internalPacket->splitPacketCount, internalPacket->orderingIndex); + } + else + { + FormatLine(str, sendType, "Nrm", reliableMessageNumber, frameNumber, internalPacket->data[0], internalPacket->dataBitLength, (unsigned long long)time, localSystemAddress, remoteSystemAddress, internalPacket->splitPacketId, internalPacket->splitPacketIndex, internalPacket->splitPacketCount, internalPacket->orderingIndex); + } + + AddToLog(str); +} +void PacketLogger::AddToLog(const char *str) +{ + WriteLog(str); +} +void PacketLogger::WriteLog(const char *str) +{ + RAKNET_DEBUG_PRINTF("%s\n", str); +} +void PacketLogger::WriteMiscellaneous(const char *type, const char *msg) +{ + char str[1024]; + char str1[64]; + SystemAddress localSystemAddress = rakPeerInterface->GetInternalID(); + localSystemAddress.ToString(true, str1); + RakNet::TimeMS time = RakNet::GetTimeMS(); + char localtime[128]; + GetLocalTime(localtime); + + sprintf(str, "%s,Lcl,%s,,,,,%" PRINTF_64_BIT_MODIFIER "u,%s,,,,,,,%s" + , localtime + , type + , (unsigned long long) time + , str1 + , msg + ); + + AddToLog(msg); +} +void PacketLogger::SetPrintID(bool print) +{ + printId=print; +} +void PacketLogger::SetPrintAcks(bool print) +{ + printAcks=print; +} +const char* PacketLogger::BaseIDTOString(unsigned char Id) +{ + if (Id >= ID_USER_PACKET_ENUM) + return 0; + + const char *IDTable[((int)ID_USER_PACKET_ENUM)+1]= + { + "ID_CONNECTED_PING", + "ID_UNCONNECTED_PING", + "ID_UNCONNECTED_PING_OPEN_CONNECTIONS", + "ID_CONNECTED_PONG", + "ID_DETECT_LOST_CONNECTIONS", + "ID_OPEN_CONNECTION_REQUEST_1", + "ID_OPEN_CONNECTION_REPLY_1", + "ID_OPEN_CONNECTION_REQUEST_2", + "ID_OPEN_CONNECTION_REPLY_2", + "ID_CONNECTION_REQUEST", + "ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY", + "ID_OUR_SYSTEM_REQUIRES_SECURITY", + "ID_PUBLIC_KEY_MISMATCH", + "ID_OUT_OF_BAND_INTERNAL", + "ID_SND_RECEIPT_ACKED", + "ID_SND_RECEIPT_LOSS", + "ID_CONNECTION_REQUEST_ACCEPTED", + "ID_CONNECTION_ATTEMPT_FAILED", + "ID_ALREADY_CONNECTED", + "ID_NEW_INCOMING_CONNECTION", + "ID_NO_FREE_INCOMING_CONNECTIONS", + "ID_DISCONNECTION_NOTIFICATION", + "ID_CONNECTION_LOST", + "ID_CONNECTION_BANNED", + "ID_INVALID_PASSWORD", + "ID_INCOMPATIBLE_PROTOCOL_VERSION", + "ID_IP_RECENTLY_CONNECTED", + "ID_TIMESTAMP", + "ID_UNCONNECTED_PONG", + "ID_ADVERTISE_SYSTEM", + "ID_DOWNLOAD_PROGRESS", + "ID_REMOTE_DISCONNECTION_NOTIFICATION", + "ID_REMOTE_CONNECTION_LOST", + "ID_REMOTE_NEW_INCOMING_CONNECTION", + "ID_FILE_LIST_TRANSFER_HEADER", + "ID_FILE_LIST_TRANSFER_FILE", + "ID_FILE_LIST_REFERENCE_PUSH_ACK", + "ID_DDT_DOWNLOAD_REQUEST", + "ID_TRANSPORT_STRING", + "ID_REPLICA_MANAGER_CONSTRUCTION", + "ID_REPLICA_MANAGER_SCOPE_CHANGE", + "ID_REPLICA_MANAGER_SERIALIZE", + "ID_REPLICA_MANAGER_DOWNLOAD_STARTED", + "ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE", + "ID_RAKVOICE_OPEN_CHANNEL_REQUEST", + "ID_RAKVOICE_OPEN_CHANNEL_REPLY", + "ID_RAKVOICE_CLOSE_CHANNEL", + "ID_RAKVOICE_DATA", + "ID_AUTOPATCHER_GET_CHANGELIST_SINCE_DATE", + "ID_AUTOPATCHER_CREATION_LIST", + "ID_AUTOPATCHER_DELETION_LIST", + "ID_AUTOPATCHER_GET_PATCH", + "ID_AUTOPATCHER_PATCH_LIST", + "ID_AUTOPATCHER_REPOSITORY_FATAL_ERROR", + "ID_AUTOPATCHER_CANNOT_DOWNLOAD_ORIGINAL_UNMODIFIED_FILES", + "ID_AUTOPATCHER_FINISHED_INTERNAL", + "ID_AUTOPATCHER_FINISHED", + "ID_AUTOPATCHER_RESTART_APPLICATION", + "ID_NAT_PUNCHTHROUGH_REQUEST", + "ID_NAT_CONNECT_AT_TIME", + "ID_NAT_GET_MOST_RECENT_PORT", + "ID_NAT_CLIENT_READY", + "ID_NAT_TARGET_NOT_CONNECTED", + "ID_NAT_TARGET_UNRESPONSIVE", + "ID_NAT_CONNECTION_TO_TARGET_LOST", + "ID_NAT_ALREADY_IN_PROGRESS", + "ID_NAT_PUNCHTHROUGH_FAILED", + "ID_NAT_PUNCHTHROUGH_SUCCEEDED", + "ID_READY_EVENT_SET", + "ID_READY_EVENT_UNSET", + "ID_READY_EVENT_ALL_SET", + "ID_READY_EVENT_QUERY", + "ID_LOBBY_GENERAL", + "ID_RPC_REMOTE_ERROR", + "ID_RPC_PLUGIN", + "ID_FILE_LIST_REFERENCE_PUSH", + "ID_READY_EVENT_FORCE_ALL_SET", + "ID_ROOMS_EXECUTE_FUNC", + "ID_ROOMS_LOGON_STATUS", + "ID_ROOMS_HANDLE_CHANGE", + "ID_LOBBY2_SEND_MESSAGE", + "ID_LOBBY2_SERVER_ERROR", + "ID_FCM2_NEW_HOST", + "ID_FCM2_REQUEST_FCMGUID", + "ID_FCM2_RESPOND_CONNECTION_COUNT", + "ID_FCM2_INFORM_FCMGUID", + "ID_FCM2_UPDATE_MIN_TOTAL_CONNECTION_COUNT", + "ID_FCM2_VERIFIED_JOIN_START", + "ID_FCM2_VERIFIED_JOIN_CAPABLE", + "ID_FCM2_VERIFIED_JOIN_FAILED", + "ID_FCM2_VERIFIED_JOIN_ACCEPTED", + "ID_FCM2_VERIFIED_JOIN_REJECTED", + "ID_UDP_PROXY_GENERAL", + "ID_SQLite3_EXEC", + "ID_SQLite3_UNKNOWN_DB", + "ID_SQLLITE_LOGGER", + "ID_NAT_TYPE_DETECTION_REQUEST", + "ID_NAT_TYPE_DETECTION_RESULT", + "ID_ROUTER_2_INTERNAL", + "ID_ROUTER_2_FORWARDING_NO_PATH", + "ID_ROUTER_2_FORWARDING_ESTABLISHED", + "ID_ROUTER_2_REROUTED", + "ID_TEAM_BALANCER_INTERNAL", + "ID_TEAM_BALANCER_REQUESTED_TEAM_FULL", + "ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED", + "ID_TEAM_BALANCER_TEAM_REQUESTED_CANCELLED", + "ID_TEAM_BALANCER_TEAM_ASSIGNED", + "ID_LIGHTSPEED_INTEGRATION", + "ID_XBOX_LOBBY", + "ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_SUCCESS", + "ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_SUCCESS", + "ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_FAILURE", + "ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_FAILURE", + "ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_TIMEOUT", + "ID_TWO_WAY_AUTHENTICATION_NEGOTIATION", + "ID_CLOUD_POST_REQUEST", + "ID_CLOUD_RELEASE_REQUEST", + "ID_CLOUD_GET_REQUEST", + "ID_CLOUD_GET_RESPONSE", + "ID_CLOUD_UNSUBSCRIBE_REQUEST", + "ID_CLOUD_SERVER_TO_SERVER_COMMAND", + "ID_CLOUD_SUBSCRIPTION_NOTIFICATION", + "ID_LIB_VOICE", + "ID_RELAY_PLUGIN", + "ID_NAT_REQUEST_BOUND_ADDRESSES", + "ID_NAT_RESPOND_BOUND_ADDRESSES", + "ID_FCM2_UPDATE_USER_CONTEXT", + "ID_RESERVED_3", + "ID_RESERVED_4", + "ID_RESERVED_5", + "ID_RESERVED_6", + "ID_RESERVED_7", + "ID_RESERVED_8", + "ID_RESERVED_9", + "ID_USER_PACKET_ENUM" + }; + + return (char*)IDTable[Id]; +} +const char* PacketLogger::UserIDTOString(unsigned char Id) +{ + // Users should override this + static char str[256]; + Itoa(Id, str, 10); + return (const char*) str; +} +const char* PacketLogger::IDTOString(unsigned char Id) +{ + const char *out; + out=BaseIDTOString(Id); + if (out) + return out; + return UserIDTOString(Id); +} +void PacketLogger::SetPrefix(const char *_prefix) +{ + strncpy(prefix, _prefix, 255); + prefix[255]=0; +} +void PacketLogger::SetSuffix(const char *_suffix) +{ + strncpy(suffix, _suffix, 255); + suffix[255]=0; +} +void PacketLogger::GetLocalTime(char buffer[128]) +{ +#if defined(_WIN32) && !defined(__GNUC__) && !defined(__GCCXML__) + time_t rawtime; + struct timeval tv; + // If you get an arror about an incomplete type, just delete this file + struct timezone tz; + gettimeofday(&tv, &tz); + // time ( &rawtime ); + rawtime=tv.tv_sec; + + struct tm * timeinfo; + timeinfo = localtime ( &rawtime ); + strftime (buffer,128,"%x %X",timeinfo); + char buff[32]; + sprintf(buff, ".%i", tv.tv_usec); + strcat(buffer,buff); + + // Commented version puts the time first + /* + struct tm * timeinfo; + timeinfo = localtime ( &rawtime ); + strftime (buffer,128,"%X",timeinfo); + char buff[32]; + sprintf(buff, ".%i ", tv.tv_usec); + strcat(buffer,buff); + char buff2[32]; + strftime (buff2,32,"%x",timeinfo); + strcat(buffer,buff2); + */ +#else + buffer[0]=0; +#endif +} +void PacketLogger::SetLogDirectMessages(bool send) +{ + logDirectMessages=send; +} + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif + +#endif // _RAKNET_SUPPORT_* diff --git a/Source/PacketLogger.h b/Source/PacketLogger.h index 40625d669..9f47a7775 100644 --- a/Source/PacketLogger.h +++ b/Source/PacketLogger.h @@ -1,109 +1,107 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief This will write all incoming and outgoing network messages to the local console screen. See derived functions for other outputs -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_PacketLogger==1 - -#ifndef __PACKET_LOGGER_H -#define __PACKET_LOGGER_H - -#include "RakNetTypes.h" -#include "PluginInterface2.h" -#include "Export.h" - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; - -/// \defgroup PACKETLOGGER_GROUP PacketLogger -/// \brief Print out incoming messages to a target destination -/// \details -/// \ingroup PLUGINS_GROUP - -/// \brief Writes incoming and outgoing messages to the screen. -/// This will write all incoming and outgoing messages to the console window, or to a file if you override it and give it this functionality. -/// \ingroup PACKETLOGGER_GROUP -class RAK_DLL_EXPORT PacketLogger : public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(PacketLogger) - - PacketLogger(); - virtual ~PacketLogger(); - - // Translate the supplied parameters into an output line - overloaded version that takes a MessageIdentifier - // and translates it into a string (numeric or textual representation based on printId); this calls the - // second version which takes a const char* argument for the messageIdentifier - virtual void FormatLine(char* into, const char* dir, const char* type, unsigned int reliableMessageNumber, unsigned int frame - , unsigned char messageIdentifier, const BitSize_t bitLen, unsigned long long time, const SystemAddress& local, const SystemAddress& remote, - unsigned int splitPacketId, unsigned int splitPacketIndex, unsigned int splitPacketCount, unsigned int orderingIndex); - virtual void FormatLine(char* into, const char* dir, const char* type, unsigned int reliableMessageNumber, unsigned int frame - , const char* idToPrint, const BitSize_t bitLen, unsigned long long time, const SystemAddress& local, const SystemAddress& remote, - unsigned int splitPacketId, unsigned int splitPacketIndex, unsigned int splitPacketCount, unsigned int orderingIndex); - - /// Events on low level sends and receives. These functions may be called from different threads at the same time. - virtual void OnDirectSocketSend(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress); - virtual void OnDirectSocketReceive(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress); - virtual void OnReliabilityLayerNotification(const char *errorMessage, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress, bool isError); - virtual void OnInternalPacket(InternalPacket *internalPacket, unsigned frameNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time, int isSend); - virtual void OnAck(unsigned int messageNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time); - virtual void OnPushBackPacket(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress); - - /// Logs out a header for all the data - virtual void LogHeader(void); - - /// Override this to log strings to wherever. Log should be threadsafe - virtual void WriteLog(const char *str); - - // Write informational messages - virtual void WriteMiscellaneous(const char *type, const char *msg); - - - // Set to true to print ID_* instead of numbers - virtual void SetPrintID(bool print); - // Print or hide acks (clears up the screen not to print them but is worse for debugging) - virtual void SetPrintAcks(bool print); - - /// Prepend this string to output logs. - virtual void SetPrefix(const char *_prefix); - - /// Append this string to output logs. (newline is useful here) - virtual void SetSuffix(const char *_suffix); - static const char* BaseIDTOString(unsigned char Id); - - /// Log the direct sends and receives or not. Default true - void SetLogDirectMessages(bool send); -protected: - - virtual bool UsesReliabilityLayer(void) const {return true;} - const char* IDTOString(unsigned char Id); - virtual void AddToLog(const char *str); - // Users should override this - virtual const char* UserIDTOString(unsigned char Id); - void GetLocalTime(char buffer[128]); - bool logDirectMessages; - - bool printId, printAcks; - char prefix[256]; - char suffix[256]; -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief This will write all incoming and outgoing network messages to the local console screen. See derived functions for other outputs +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_PacketLogger==1 + +#pragma once + +#include "RakNetTypes.h" +#include "PluginInterface2.h" +#include "Export.h" + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; + +/// \defgroup PACKETLOGGER_GROUP PacketLogger +/// \brief Print out incoming messages to a target destination +/// \details +/// \ingroup PLUGINS_GROUP + +/// \brief Writes incoming and outgoing messages to the screen. +/// This will write all incoming and outgoing messages to the console window, or to a file if you override it and give it this functionality. +/// \ingroup PACKETLOGGER_GROUP +class RAK_DLL_EXPORT PacketLogger : public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(PacketLogger) + + PacketLogger(); + virtual ~PacketLogger(); + + // Translate the supplied parameters into an output line - overloaded version that takes a MessageIdentifier + // and translates it into a string (numeric or textual representation based on printId); this calls the + // second version which takes a const char* argument for the messageIdentifier + virtual void FormatLine(char* into, const char* dir, const char* type, unsigned int reliableMessageNumber, unsigned int frame + , unsigned char messageIdentifier, const BitSize_t bitLen, unsigned long long time, const SystemAddress& local, const SystemAddress& remote, + unsigned int splitPacketId, unsigned int splitPacketIndex, unsigned int splitPacketCount, unsigned int orderingIndex); + virtual void FormatLine(char* into, const char* dir, const char* type, unsigned int reliableMessageNumber, unsigned int frame + , const char* idToPrint, const BitSize_t bitLen, unsigned long long time, const SystemAddress& local, const SystemAddress& remote, + unsigned int splitPacketId, unsigned int splitPacketIndex, unsigned int splitPacketCount, unsigned int orderingIndex); + + /// Events on low level sends and receives. These functions may be called from different threads at the same time. + virtual void OnDirectSocketSend(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress); + virtual void OnDirectSocketReceive(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress); + virtual void OnReliabilityLayerNotification(const char *errorMessage, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress, bool isError); + virtual void OnInternalPacket(InternalPacket *internalPacket, unsigned frameNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time, int isSend); + virtual void OnAck(unsigned int messageNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time); + virtual void OnPushBackPacket(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress); + + /// Logs out a header for all the data + virtual void LogHeader(void); + + /// Override this to log strings to wherever. Log should be threadsafe + virtual void WriteLog(const char *str); + + // Write informational messages + virtual void WriteMiscellaneous(const char *type, const char *msg); + + + // Set to true to print ID_* instead of numbers + virtual void SetPrintID(bool print); + // Print or hide acks (clears up the screen not to print them but is worse for debugging) + virtual void SetPrintAcks(bool print); + + /// Prepend this string to output logs. + virtual void SetPrefix(const char *_prefix); + + /// Append this string to output logs. (newline is useful here) + virtual void SetSuffix(const char *_suffix); + static const char* BaseIDTOString(unsigned char Id); + + /// Log the direct sends and receives or not. Default true + void SetLogDirectMessages(bool send); +protected: + + virtual bool UsesReliabilityLayer(void) const {return true;} + const char* IDTOString(unsigned char Id); + virtual void AddToLog(const char *str); + // Users should override this + virtual const char* UserIDTOString(unsigned char Id); + void GetLocalTime(char buffer[128]); + bool logDirectMessages; + + bool printId, printAcks; + char prefix[256]; + char suffix[256]; +}; + +} // namespace RakNet + +#endif + diff --git a/Source/PacketOutputWindowLogger.h b/Source/PacketOutputWindowLogger.h index 5591aa036..87c651721 100644 --- a/Source/PacketOutputWindowLogger.h +++ b/Source/PacketOutputWindowLogger.h @@ -1,42 +1,40 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief This will write all incoming and outgoing network messages to a file -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_PacketLogger==1 - -#ifndef __PACKET_OUTPUT_WINDOW_LOGGER_H_ -#define __PACKET_OUTPUT_WINDOW_LOGGER_H_ - -#include "PacketLogger.h" - -namespace RakNet -{ - -/// \ingroup PACKETLOGGER_GROUP -/// \brief Packetlogger that outputs to the output window in the debugger. Windows only. -class RAK_DLL_EXPORT PacketOutputWindowLogger : public PacketLogger -{ -public: - PacketOutputWindowLogger(); - virtual ~PacketOutputWindowLogger(); - virtual void WriteLog(const char *str); -protected: -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief This will write all incoming and outgoing network messages to a file +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_PacketLogger==1 + +#pragma once + +#include "PacketLogger.h" + +namespace RakNet +{ + +/// \ingroup PACKETLOGGER_GROUP +/// \brief Packetlogger that outputs to the output window in the debugger. Windows only. +class RAK_DLL_EXPORT PacketOutputWindowLogger : public PacketLogger +{ +public: + PacketOutputWindowLogger(); + virtual ~PacketOutputWindowLogger(); + virtual void WriteLog(const char *str); +protected: +}; + +} // namespace RakNet + +#endif + diff --git a/Source/PacketPriority.h b/Source/PacketPriority.h index e1da3622d..778e9fa06 100644 --- a/Source/PacketPriority.h +++ b/Source/PacketPriority.h @@ -1,87 +1,85 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief This file contains enumerations for packet priority and reliability enumerations. -/// - - - -#ifndef __PACKET_PRIORITY_H -#define __PACKET_PRIORITY_H - -/// These enumerations are used to describe when packets are delivered. -enum PacketPriority -{ - /// The highest possible priority. These message trigger sends immediately, and are generally not buffered or aggregated into a single datagram. - IMMEDIATE_PRIORITY, - - /// For every 2 IMMEDIATE_PRIORITY messages, 1 HIGH_PRIORITY will be sent. - /// Messages at this priority and lower are buffered to be sent in groups at 10 millisecond intervals to reduce UDP overhead and better measure congestion control. - HIGH_PRIORITY, - - /// For every 2 HIGH_PRIORITY messages, 1 MEDIUM_PRIORITY will be sent. - /// Messages at this priority and lower are buffered to be sent in groups at 10 millisecond intervals to reduce UDP overhead and better measure congestion control. - MEDIUM_PRIORITY, - - /// For every 2 MEDIUM_PRIORITY messages, 1 LOW_PRIORITY will be sent. - /// Messages at this priority and lower are buffered to be sent in groups at 10 millisecond intervals to reduce UDP overhead and better measure congestion control. - LOW_PRIORITY, - - /// \internal - NUMBER_OF_PRIORITIES -}; - -/// These enumerations are used to describe how packets are delivered. -/// \note Note to self: I write this with 3 bits in the stream. If I add more remember to change that -/// \note In ReliabilityLayer::WriteToBitStreamFromInternalPacket I assume there are 5 major types -/// \note Do not reorder, I check on >= UNRELIABLE_WITH_ACK_RECEIPT -enum PacketReliability -{ - /// Same as regular UDP, except that it will also discard duplicate datagrams. RakNet adds (6 to 17) + 21 bits of overhead, 16 of which is used to detect duplicate packets and 6 to 17 of which is used for message length. - UNRELIABLE, - - /// Regular UDP with a sequence counter. Out of order messages will be discarded. - /// Sequenced and ordered messages sent on the same channel will arrive in the order sent. - UNRELIABLE_SEQUENCED, - - /// The message is sent reliably, but not necessarily in any order. Same overhead as UNRELIABLE. - RELIABLE, - - /// This message is reliable and will arrive in the order you sent it. Messages will be delayed while waiting for out of order messages. Same overhead as UNRELIABLE_SEQUENCED. - /// Sequenced and ordered messages sent on the same channel will arrive in the order sent. - RELIABLE_ORDERED, - - /// This message is reliable and will arrive in the sequence you sent it. Out or order messages will be dropped. Same overhead as UNRELIABLE_SEQUENCED. - /// Sequenced and ordered messages sent on the same channel will arrive in the order sent. - RELIABLE_SEQUENCED, - - /// Same as UNRELIABLE, however the user will get either ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS based on the result of sending this message when calling RakPeerInterface::Receive(). Bytes 1-4 will contain the number returned from the Send() function. On disconnect or shutdown, all messages not previously acked should be considered lost. - UNRELIABLE_WITH_ACK_RECEIPT, - - /// Same as UNRELIABLE_SEQUENCED, however the user will get either ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS based on the result of sending this message when calling RakPeerInterface::Receive(). Bytes 1-4 will contain the number returned from the Send() function. On disconnect or shutdown, all messages not previously acked should be considered lost. - /// 05/04/10 You can't have sequenced and ack receipts, because you don't know if the other system discarded the message, meaning you don't know if the message was processed - // UNRELIABLE_SEQUENCED_WITH_ACK_RECEIPT, - - /// Same as RELIABLE. The user will also get ID_SND_RECEIPT_ACKED after the message is delivered when calling RakPeerInterface::Receive(). ID_SND_RECEIPT_ACKED is returned when the message arrives, not necessarily the order when it was sent. Bytes 1-4 will contain the number returned from the Send() function. On disconnect or shutdown, all messages not previously acked should be considered lost. This does not return ID_SND_RECEIPT_LOSS. - RELIABLE_WITH_ACK_RECEIPT, - - /// Same as RELIABLE_ORDERED_ACK_RECEIPT. The user will also get ID_SND_RECEIPT_ACKED after the message is delivered when calling RakPeerInterface::Receive(). ID_SND_RECEIPT_ACKED is returned when the message arrives, not necessarily the order when it was sent. Bytes 1-4 will contain the number returned from the Send() function. On disconnect or shutdown, all messages not previously acked should be considered lost. This does not return ID_SND_RECEIPT_LOSS. - RELIABLE_ORDERED_WITH_ACK_RECEIPT, - - /// Same as RELIABLE_SEQUENCED. The user will also get ID_SND_RECEIPT_ACKED after the message is delivered when calling RakPeerInterface::Receive(). Bytes 1-4 will contain the number returned from the Send() function. On disconnect or shutdown, all messages not previously acked should be considered lost. - /// 05/04/10 You can't have sequenced and ack receipts, because you don't know if the other system discarded the message, meaning you don't know if the message was processed - // RELIABLE_SEQUENCED_WITH_ACK_RECEIPT, - - /// \internal - NUMBER_OF_RELIABILITIES -}; - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief This file contains enumerations for packet priority and reliability enumerations. +/// + + + +#pragma once + +/// These enumerations are used to describe when packets are delivered. +enum PacketPriority +{ + /// The highest possible priority. These message trigger sends immediately, and are generally not buffered or aggregated into a single datagram. + IMMEDIATE_PRIORITY, + + /// For every 2 IMMEDIATE_PRIORITY messages, 1 HIGH_PRIORITY will be sent. + /// Messages at this priority and lower are buffered to be sent in groups at 10 millisecond intervals to reduce UDP overhead and better measure congestion control. + HIGH_PRIORITY, + + /// For every 2 HIGH_PRIORITY messages, 1 MEDIUM_PRIORITY will be sent. + /// Messages at this priority and lower are buffered to be sent in groups at 10 millisecond intervals to reduce UDP overhead and better measure congestion control. + MEDIUM_PRIORITY, + + /// For every 2 MEDIUM_PRIORITY messages, 1 LOW_PRIORITY will be sent. + /// Messages at this priority and lower are buffered to be sent in groups at 10 millisecond intervals to reduce UDP overhead and better measure congestion control. + LOW_PRIORITY, + + /// \internal + NUMBER_OF_PRIORITIES +}; + +/// These enumerations are used to describe how packets are delivered. +/// \note Note to self: I write this with 3 bits in the stream. If I add more remember to change that +/// \note In ReliabilityLayer::WriteToBitStreamFromInternalPacket I assume there are 5 major types +/// \note Do not reorder, I check on >= UNRELIABLE_WITH_ACK_RECEIPT +enum PacketReliability +{ + /// Same as regular UDP, except that it will also discard duplicate datagrams. RakNet adds (6 to 17) + 21 bits of overhead, 16 of which is used to detect duplicate packets and 6 to 17 of which is used for message length. + UNRELIABLE, + + /// Regular UDP with a sequence counter. Out of order messages will be discarded. + /// Sequenced and ordered messages sent on the same channel will arrive in the order sent. + UNRELIABLE_SEQUENCED, + + /// The message is sent reliably, but not necessarily in any order. Same overhead as UNRELIABLE. + RELIABLE, + + /// This message is reliable and will arrive in the order you sent it. Messages will be delayed while waiting for out of order messages. Same overhead as UNRELIABLE_SEQUENCED. + /// Sequenced and ordered messages sent on the same channel will arrive in the order sent. + RELIABLE_ORDERED, + + /// This message is reliable and will arrive in the sequence you sent it. Out or order messages will be dropped. Same overhead as UNRELIABLE_SEQUENCED. + /// Sequenced and ordered messages sent on the same channel will arrive in the order sent. + RELIABLE_SEQUENCED, + + /// Same as UNRELIABLE, however the user will get either ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS based on the result of sending this message when calling RakPeerInterface::Receive(). Bytes 1-4 will contain the number returned from the Send() function. On disconnect or shutdown, all messages not previously acked should be considered lost. + UNRELIABLE_WITH_ACK_RECEIPT, + + /// Same as UNRELIABLE_SEQUENCED, however the user will get either ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS based on the result of sending this message when calling RakPeerInterface::Receive(). Bytes 1-4 will contain the number returned from the Send() function. On disconnect or shutdown, all messages not previously acked should be considered lost. + /// 05/04/10 You can't have sequenced and ack receipts, because you don't know if the other system discarded the message, meaning you don't know if the message was processed + // UNRELIABLE_SEQUENCED_WITH_ACK_RECEIPT, + + /// Same as RELIABLE. The user will also get ID_SND_RECEIPT_ACKED after the message is delivered when calling RakPeerInterface::Receive(). ID_SND_RECEIPT_ACKED is returned when the message arrives, not necessarily the order when it was sent. Bytes 1-4 will contain the number returned from the Send() function. On disconnect or shutdown, all messages not previously acked should be considered lost. This does not return ID_SND_RECEIPT_LOSS. + RELIABLE_WITH_ACK_RECEIPT, + + /// Same as RELIABLE_ORDERED_ACK_RECEIPT. The user will also get ID_SND_RECEIPT_ACKED after the message is delivered when calling RakPeerInterface::Receive(). ID_SND_RECEIPT_ACKED is returned when the message arrives, not necessarily the order when it was sent. Bytes 1-4 will contain the number returned from the Send() function. On disconnect or shutdown, all messages not previously acked should be considered lost. This does not return ID_SND_RECEIPT_LOSS. + RELIABLE_ORDERED_WITH_ACK_RECEIPT, + + /// Same as RELIABLE_SEQUENCED. The user will also get ID_SND_RECEIPT_ACKED after the message is delivered when calling RakPeerInterface::Receive(). Bytes 1-4 will contain the number returned from the Send() function. On disconnect or shutdown, all messages not previously acked should be considered lost. + /// 05/04/10 You can't have sequenced and ack receipts, because you don't know if the other system discarded the message, meaning you don't know if the message was processed + // RELIABLE_SEQUENCED_WITH_ACK_RECEIPT, + + /// \internal + NUMBER_OF_RELIABILITIES +}; + diff --git a/Source/PacketizedTCP.h b/Source/PacketizedTCP.h index 309738196..bb69c7d9e 100644 --- a/Source/PacketizedTCP.h +++ b/Source/PacketizedTCP.h @@ -1,86 +1,84 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief A simple TCP based server allowing sends and receives. Can be connected by any TCP client, including telnet. -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_PacketizedTCP==1 && _RAKNET_SUPPORT_TCPInterface==1 - -#ifndef __PACKETIZED_TCP -#define __PACKETIZED_TCP - -#include "TCPInterface.h" -#include "DS_ByteQueue.h" -#include "DS_Map.h" - -namespace RakNet -{ - -class RAK_DLL_EXPORT PacketizedTCP : public TCPInterface -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(PacketizedTCP) - - PacketizedTCP(); - virtual ~PacketizedTCP(); - - /// Stops the TCP server - void Stop(void); - - /// Sends a byte stream - void Send( const char *data, unsigned length, const SystemAddress &systemAddress, bool broadcast ); - - // Sends a concatenated list of byte streams - bool SendList( const char **data, const unsigned int *lengths, const int numParameters, const SystemAddress &systemAddress, bool broadcast ); - - /// Returns data received - Packet* Receive( void ); - - /// Disconnects a player/address - void CloseConnection( SystemAddress systemAddress ); - - /// Has a previous call to connect succeeded? - /// \return UNASSIGNED_SYSTEM_ADDRESS = no. Anything else means yes. - SystemAddress HasCompletedConnectionAttempt(void); - - /// Has a previous call to connect failed? - /// \return UNASSIGNED_SYSTEM_ADDRESS = no. Anything else means yes. - SystemAddress HasFailedConnectionAttempt(void); - - /// Queued events of new incoming connections - SystemAddress HasNewIncomingConnection(void); - - /// Queued events of lost connections - SystemAddress HasLostConnection(void); - -protected: - void ClearAllConnections(void); - void RemoveFromConnectionList(const SystemAddress &sa); - void AddToConnectionList(const SystemAddress &sa); - void PushNotificationsToQueues(void); - Packet *ReturnOutgoingPacket(void); - - // A single TCP recieve may generate multiple split packets. They are stored in the waitingPackets list until Receive is called - DataStructures::Queue waitingPackets; - DataStructures::Map connections; - - // Mirrors single producer / consumer, but processes them in Receive() before returning to user - DataStructures::Queue _newIncomingConnections, _lostConnections, _failedConnectionAttempts, _completedConnectionAttempts; -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief A simple TCP based server allowing sends and receives. Can be connected by any TCP client, including telnet. +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_PacketizedTCP==1 && _RAKNET_SUPPORT_TCPInterface==1 + +#pragma once + +#include "TCPInterface.h" +#include "DS_ByteQueue.h" +#include "DS_Map.h" + +namespace RakNet +{ + +class RAK_DLL_EXPORT PacketizedTCP : public TCPInterface +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(PacketizedTCP) + + PacketizedTCP(); + virtual ~PacketizedTCP(); + + /// Stops the TCP server + void Stop(void); + + /// Sends a byte stream + void Send( const char *data, unsigned length, const SystemAddress &systemAddress, bool broadcast ); + + // Sends a concatenated list of byte streams + bool SendList( const char **data, const unsigned int *lengths, const int numParameters, const SystemAddress &systemAddress, bool broadcast ); + + /// Returns data received + Packet* Receive( void ); + + /// Disconnects a player/address + void CloseConnection( SystemAddress systemAddress ); + + /// Has a previous call to connect succeeded? + /// \return UNASSIGNED_SYSTEM_ADDRESS = no. Anything else means yes. + SystemAddress HasCompletedConnectionAttempt(void); + + /// Has a previous call to connect failed? + /// \return UNASSIGNED_SYSTEM_ADDRESS = no. Anything else means yes. + SystemAddress HasFailedConnectionAttempt(void); + + /// Queued events of new incoming connections + SystemAddress HasNewIncomingConnection(void); + + /// Queued events of lost connections + SystemAddress HasLostConnection(void); + +protected: + void ClearAllConnections(void); + void RemoveFromConnectionList(const SystemAddress &sa); + void AddToConnectionList(const SystemAddress &sa); + void PushNotificationsToQueues(void); + Packet *ReturnOutgoingPacket(void); + + // A single TCP recieve may generate multiple split packets. They are stored in the waitingPackets list until Receive is called + DataStructures::Queue waitingPackets; + DataStructures::Map connections; + + // Mirrors single producer / consumer, but processes them in Receive() before returning to user + DataStructures::Queue _newIncomingConnections, _lostConnections, _failedConnectionAttempts, _completedConnectionAttempts; +}; + +} // namespace RakNet + +#endif + diff --git a/Source/PluginInterface2.h b/Source/PluginInterface2.h index d3116b203..fbd06acfa 100644 --- a/Source/PluginInterface2.h +++ b/Source/PluginInterface2.h @@ -1,211 +1,208 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief \b RakNet's plugin functionality system, version 2. You can derive from this to create your own plugins. -/// - - -#ifndef __PLUGIN_INTERFACE_2_H -#define __PLUGIN_INTERFACE_2_H - -#include "NativeFeatureIncludes.h" -#include "RakNetTypes.h" -#include "Export.h" -#include "PacketPriority.h" - -namespace RakNet { - -/// Forward declarations -class RakPeerInterface; -class TCPInterface; -struct Packet; -struct InternalPacket; - -/// \defgroup PLUGIN_INTERFACE_GROUP PluginInterface2 - -/// \defgroup PLUGINS_GROUP Plugins -/// \ingroup PLUGIN_INTERFACE_GROUP - -/// For each message that arrives on an instance of RakPeer, the plugins get an opportunity to process them first. This enumeration represents what to do with the message -/// \ingroup PLUGIN_INTERFACE_GROUP -enum PluginReceiveResult -{ - /// The plugin used this message and it shouldn't be given to the user. - RR_STOP_PROCESSING_AND_DEALLOCATE=0, - - /// This message will be processed by other plugins, and at last by the user. - RR_CONTINUE_PROCESSING, - - /// The plugin is going to hold on to this message. Do not deallocate it but do not pass it to other plugins either. - RR_STOP_PROCESSING -}; - -/// Reasons why a connection was lost -/// \ingroup PLUGIN_INTERFACE_GROUP -enum PI2_LostConnectionReason -{ - /// Called RakPeer::CloseConnection() - LCR_CLOSED_BY_USER, - - /// Got ID_DISCONNECTION_NOTIFICATION - LCR_DISCONNECTION_NOTIFICATION, - - /// GOT ID_CONNECTION_LOST - LCR_CONNECTION_LOST -}; - -/// Returns why a connection attempt failed -/// \ingroup PLUGIN_INTERFACE_GROUP -enum PI2_FailedConnectionAttemptReason -{ - FCAR_CONNECTION_ATTEMPT_FAILED, - FCAR_ALREADY_CONNECTED, - FCAR_NO_FREE_INCOMING_CONNECTIONS, - FCAR_SECURITY_PUBLIC_KEY_MISMATCH, - FCAR_CONNECTION_BANNED, - FCAR_INVALID_PASSWORD, - FCAR_INCOMPATIBLE_PROTOCOL, - FCAR_IP_RECENTLY_CONNECTED, - FCAR_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY, - FCAR_OUR_SYSTEM_REQUIRES_SECURITY, - FCAR_PUBLIC_KEY_MISMATCH -}; - -/// RakNet's plugin system. Each plugin processes the following events: -/// -Connection attempts -/// -The result of connection attempts -/// -Each incoming message -/// -Updates over time, when RakPeer::Receive() is called -/// -/// \ingroup PLUGIN_INTERFACE_GROUP -class RAK_DLL_EXPORT PluginInterface2 -{ -public: - PluginInterface2(); - virtual ~PluginInterface2(); - - /// Called when the interface is attached - virtual void OnAttach(void) {} - - /// Called when the interface is detached - virtual void OnDetach(void) {} - - /// Update is called every time a packet is checked for . - virtual void Update(void) {} - - /// OnReceive is called for every packet. - /// \param[in] packet the packet that is being returned to the user - /// \return True to allow the game and other plugins to get this message, false to absorb it - virtual PluginReceiveResult OnReceive(Packet *packet) {(void) packet; return RR_CONTINUE_PROCESSING;} - - /// Called when RakPeer is initialized - virtual void OnRakPeerStartup(void) {} - - /// Called when RakPeer is shutdown - virtual void OnRakPeerShutdown(void) {} - - /// Called when a connection is dropped because the user called RakPeer::CloseConnection() for a particular system - /// \param[in] systemAddress The system whose connection was closed - /// \param[in] rakNetGuid The guid of the specified system - /// \param[in] lostConnectionReason How the connection was closed: manually, connection lost, or notification of disconnection - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ){(void) systemAddress; (void) rakNetGUID; (void) lostConnectionReason;} - - /// Called when we got a new connection - /// \param[in] systemAddress Address of the new connection - /// \param[in] rakNetGuid The guid of the specified system - /// \param[in] isIncoming If true, this is ID_NEW_INCOMING_CONNECTION, or the equivalent - virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming) {(void) systemAddress; (void) rakNetGUID; (void) isIncoming;} - - /// Called when a connection attempt fails - /// \param[in] packet Packet to be returned to the user - /// \param[in] failedConnectionReason Why the connection failed - virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason) {(void) packet; (void) failedConnectionAttemptReason;} - - /// Queried when attached to RakPeer - /// Return true to call OnDirectSocketSend(), OnDirectSocketReceive(), OnReliabilityLayerNotification(), OnInternalPacket(), and OnAck() - /// If true, then you cannot call RakPeer::AttachPlugin() or RakPeer::DetachPlugin() for this plugin, while RakPeer is active - virtual bool UsesReliabilityLayer(void) const {return false;} - - /// Called on a send to the socket, per datagram, that does not go through the reliability layer - /// \pre To be called, UsesReliabilityLayer() must return true - /// \param[in] data The data being sent - /// \param[in] bitsUsed How many bits long \a data is - /// \param[in] remoteSystemAddress Which system this message is being sent to - virtual void OnDirectSocketSend(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) {(void) data; (void) bitsUsed; (void) remoteSystemAddress;} - - /// Called on a receive from the socket, per datagram, that does not go through the reliability layer - /// \pre To be called, UsesReliabilityLayer() must return true - /// \param[in] data The data being sent - /// \param[in] bitsUsed How many bits long \a data is - /// \param[in] remoteSystemAddress Which system this message is being sent to - virtual void OnDirectSocketReceive(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) {(void) data; (void) bitsUsed; (void) remoteSystemAddress;} - - /// Called when the reliability layer rejects a send or receive - /// \pre To be called, UsesReliabilityLayer() must return true - /// \param[in] bitsUsed How many bits long \a data is - /// \param[in] remoteSystemAddress Which system this message is being sent to - virtual void OnReliabilityLayerNotification(const char *errorMessage, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress, bool isError) {(void) errorMessage; (void) bitsUsed; (void) remoteSystemAddress; (void) isError;} - - /// Called on a send or receive of a message within the reliability layer - /// \pre To be called, UsesReliabilityLayer() must return true - /// \param[in] internalPacket The user message, along with all send data. - /// \param[in] frameNumber The number of frames sent or received so far for this player depending on \a isSend . Indicates the frame of this user message. - /// \param[in] remoteSystemAddress The player we sent or got this packet from - /// \param[in] time The current time as returned by RakNet::GetTimeMS() - /// \param[in] isSend Is this callback representing a send event or receive event? - virtual void OnInternalPacket(InternalPacket *internalPacket, unsigned frameNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time, int isSend) {(void) internalPacket; (void) frameNumber; (void) remoteSystemAddress; (void) time; (void) isSend;} - - /// Called when we get an ack for a message we reliably sent - /// \pre To be called, UsesReliabilityLayer() must return true - /// \param[in] messageNumber The numerical identifier for which message this is - /// \param[in] remoteSystemAddress The player we sent or got this packet from - /// \param[in] time The current time as returned by RakNet::GetTimeMS() - virtual void OnAck(unsigned int messageNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time) {(void) messageNumber; (void) remoteSystemAddress; (void) time;} - - /// System called RakPeerInterface::PushBackPacket - /// \param[in] data The data being sent - /// \param[in] bitsUsed How many bits long \a data is - /// \param[in] remoteSystemAddress The player we sent or got this packet from - virtual void OnPushBackPacket(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) {(void) data; (void) bitsUsed; (void) remoteSystemAddress;} - - RakPeerInterface *GetRakPeerInterface(void) const {return rakPeerInterface;} - - RakNetGUID GetMyGUIDUnified(void) const; - - /// \internal - void SetRakPeerInterface( RakPeerInterface *ptr ); - -#if _RAKNET_SUPPORT_TCPInterface==1 - /// \internal - void SetTCPInterface( TCPInterface *ptr ); -#endif - -protected: - // Send through either rakPeerInterface or tcpInterface, whichever is available - void SendUnified( const RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast ); - void SendUnified( const char * data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast ); - bool SendListUnified( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast ); - - Packet *AllocatePacketUnified(unsigned dataSize); - void PushBackPacketUnified(Packet *packet, bool pushAtHead); - void DeallocPacketUnified(Packet *packet); - - // Filled automatically in when attached - RakPeerInterface *rakPeerInterface; -#if _RAKNET_SUPPORT_TCPInterface==1 - TCPInterface *tcpInterface; -#endif -}; - -} // namespace RakNet - -#endif - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief \b RakNet's plugin functionality system, version 2. You can derive from this to create your own plugins. +/// + + +#pragma once + +#include "NativeFeatureIncludes.h" +#include "RakNetTypes.h" +#include "Export.h" +#include "PacketPriority.h" + +namespace RakNet { + +/// Forward declarations +class RakPeerInterface; +class TCPInterface; +struct Packet; +struct InternalPacket; + +/// \defgroup PLUGIN_INTERFACE_GROUP PluginInterface2 + +/// \defgroup PLUGINS_GROUP Plugins +/// \ingroup PLUGIN_INTERFACE_GROUP + +/// For each message that arrives on an instance of RakPeer, the plugins get an opportunity to process them first. This enumeration represents what to do with the message +/// \ingroup PLUGIN_INTERFACE_GROUP +enum PluginReceiveResult +{ + /// The plugin used this message and it shouldn't be given to the user. + RR_STOP_PROCESSING_AND_DEALLOCATE=0, + + /// This message will be processed by other plugins, and at last by the user. + RR_CONTINUE_PROCESSING, + + /// The plugin is going to hold on to this message. Do not deallocate it but do not pass it to other plugins either. + RR_STOP_PROCESSING +}; + +/// Reasons why a connection was lost +/// \ingroup PLUGIN_INTERFACE_GROUP +enum PI2_LostConnectionReason +{ + /// Called RakPeer::CloseConnection() + LCR_CLOSED_BY_USER, + + /// Got ID_DISCONNECTION_NOTIFICATION + LCR_DISCONNECTION_NOTIFICATION, + + /// GOT ID_CONNECTION_LOST + LCR_CONNECTION_LOST +}; + +/// Returns why a connection attempt failed +/// \ingroup PLUGIN_INTERFACE_GROUP +enum PI2_FailedConnectionAttemptReason +{ + FCAR_CONNECTION_ATTEMPT_FAILED, + FCAR_ALREADY_CONNECTED, + FCAR_NO_FREE_INCOMING_CONNECTIONS, + FCAR_SECURITY_PUBLIC_KEY_MISMATCH, + FCAR_CONNECTION_BANNED, + FCAR_INVALID_PASSWORD, + FCAR_INCOMPATIBLE_PROTOCOL, + FCAR_IP_RECENTLY_CONNECTED, + FCAR_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY, + FCAR_OUR_SYSTEM_REQUIRES_SECURITY, + FCAR_PUBLIC_KEY_MISMATCH +}; + +/// RakNet's plugin system. Each plugin processes the following events: +/// -Connection attempts +/// -The result of connection attempts +/// -Each incoming message +/// -Updates over time, when RakPeer::Receive() is called +/// +/// \ingroup PLUGIN_INTERFACE_GROUP +class RAK_DLL_EXPORT PluginInterface2 +{ +public: + PluginInterface2(); + virtual ~PluginInterface2(); + + /// Called when the interface is attached + virtual void OnAttach(void) {} + + /// Called when the interface is detached + virtual void OnDetach(void) {} + + /// Update is called every time a packet is checked for . + virtual void Update(void) {} + + /// OnReceive is called for every packet. + /// \param[in] packet the packet that is being returned to the user + /// \return True to allow the game and other plugins to get this message, false to absorb it + virtual PluginReceiveResult OnReceive(Packet *packet) {(void) packet; return RR_CONTINUE_PROCESSING;} + + /// Called when RakPeer is initialized + virtual void OnRakPeerStartup(void) {} + + /// Called when RakPeer is shutdown + virtual void OnRakPeerShutdown(void) {} + + /// Called when a connection is dropped because the user called RakPeer::CloseConnection() for a particular system + /// \param[in] systemAddress The system whose connection was closed + /// \param[in] rakNetGuid The guid of the specified system + /// \param[in] lostConnectionReason How the connection was closed: manually, connection lost, or notification of disconnection + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ){(void) systemAddress; (void) rakNetGUID; (void) lostConnectionReason;} + + /// Called when we got a new connection + /// \param[in] systemAddress Address of the new connection + /// \param[in] rakNetGuid The guid of the specified system + /// \param[in] isIncoming If true, this is ID_NEW_INCOMING_CONNECTION, or the equivalent + virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming) {(void) systemAddress; (void) rakNetGUID; (void) isIncoming;} + + /// Called when a connection attempt fails + /// \param[in] packet Packet to be returned to the user + /// \param[in] failedConnectionReason Why the connection failed + virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason) {(void) packet; (void) failedConnectionAttemptReason;} + + /// Queried when attached to RakPeer + /// Return true to call OnDirectSocketSend(), OnDirectSocketReceive(), OnReliabilityLayerNotification(), OnInternalPacket(), and OnAck() + /// If true, then you cannot call RakPeer::AttachPlugin() or RakPeer::DetachPlugin() for this plugin, while RakPeer is active + virtual bool UsesReliabilityLayer(void) const {return false;} + + /// Called on a send to the socket, per datagram, that does not go through the reliability layer + /// \pre To be called, UsesReliabilityLayer() must return true + /// \param[in] data The data being sent + /// \param[in] bitsUsed How many bits long \a data is + /// \param[in] remoteSystemAddress Which system this message is being sent to + virtual void OnDirectSocketSend(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) {(void) data; (void) bitsUsed; (void) remoteSystemAddress;} + + /// Called on a receive from the socket, per datagram, that does not go through the reliability layer + /// \pre To be called, UsesReliabilityLayer() must return true + /// \param[in] data The data being sent + /// \param[in] bitsUsed How many bits long \a data is + /// \param[in] remoteSystemAddress Which system this message is being sent to + virtual void OnDirectSocketReceive(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) {(void) data; (void) bitsUsed; (void) remoteSystemAddress;} + + /// Called when the reliability layer rejects a send or receive + /// \pre To be called, UsesReliabilityLayer() must return true + /// \param[in] bitsUsed How many bits long \a data is + /// \param[in] remoteSystemAddress Which system this message is being sent to + virtual void OnReliabilityLayerNotification(const char *errorMessage, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress, bool isError) {(void) errorMessage; (void) bitsUsed; (void) remoteSystemAddress; (void) isError;} + + /// Called on a send or receive of a message within the reliability layer + /// \pre To be called, UsesReliabilityLayer() must return true + /// \param[in] internalPacket The user message, along with all send data. + /// \param[in] frameNumber The number of frames sent or received so far for this player depending on \a isSend . Indicates the frame of this user message. + /// \param[in] remoteSystemAddress The player we sent or got this packet from + /// \param[in] time The current time as returned by RakNet::GetTimeMS() + /// \param[in] isSend Is this callback representing a send event or receive event? + virtual void OnInternalPacket(InternalPacket *internalPacket, unsigned frameNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time, int isSend) {(void) internalPacket; (void) frameNumber; (void) remoteSystemAddress; (void) time; (void) isSend;} + + /// Called when we get an ack for a message we reliably sent + /// \pre To be called, UsesReliabilityLayer() must return true + /// \param[in] messageNumber The numerical identifier for which message this is + /// \param[in] remoteSystemAddress The player we sent or got this packet from + /// \param[in] time The current time as returned by RakNet::GetTimeMS() + virtual void OnAck(unsigned int messageNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time) {(void) messageNumber; (void) remoteSystemAddress; (void) time;} + + /// System called RakPeerInterface::PushBackPacket + /// \param[in] data The data being sent + /// \param[in] bitsUsed How many bits long \a data is + /// \param[in] remoteSystemAddress The player we sent or got this packet from + virtual void OnPushBackPacket(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) {(void) data; (void) bitsUsed; (void) remoteSystemAddress;} + + RakPeerInterface *GetRakPeerInterface(void) const {return rakPeerInterface;} + + RakNetGUID GetMyGUIDUnified(void) const; + + /// \internal + void SetRakPeerInterface( RakPeerInterface *ptr ); + +#if _RAKNET_SUPPORT_TCPInterface==1 + /// \internal + void SetTCPInterface( TCPInterface *ptr ); +#endif + +protected: + // Send through either rakPeerInterface or tcpInterface, whichever is available + void SendUnified( const RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast ); + void SendUnified( const char * data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast ); + bool SendListUnified( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast ); + + Packet *AllocatePacketUnified(unsigned dataSize); + void PushBackPacketUnified(Packet *packet, bool pushAtHead); + void DeallocPacketUnified(Packet *packet); + + // Filled automatically in when attached + RakPeerInterface *rakPeerInterface; +#if _RAKNET_SUPPORT_TCPInterface==1 + TCPInterface *tcpInterface; +#endif +}; + +} // namespace RakNet + diff --git a/Source/RPC4Plugin.h b/Source/RPC4Plugin.h index d527367ed..3e74eacca 100644 --- a/Source/RPC4Plugin.h +++ b/Source/RPC4Plugin.h @@ -1,244 +1,242 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Remote procedure call, supporting C functions only. No external dependencies required. -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_RPC4Plugin==1 - -#ifndef __RPC_4_PLUGIN_H -#define __RPC_4_PLUGIN_H - -#include "PluginInterface2.h" -#include "PacketPriority.h" -#include "RakNetTypes.h" -#include "BitStream.h" -#include "RakString.h" -#include "NetworkIDObject.h" -#include "DS_Hash.h" -#include "DS_OrderedList.h" - -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -/// \defgroup RPC_PLUGIN_GROUP RPC -/// \brief Remote procedure calls, without external dependencies. -/// \details This should not be used at the same time as RPC3. This is a less functional version of RPC3, and is here for users that do not want the Boost dependency of RPC3. -/// \ingroup PLUGINS_GROUP - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; -class NetworkIDManager; - - /// \brief Error codes returned by a remote system as to why an RPC function call cannot execute - /// \details Error code follows packet ID ID_RPC_REMOTE_ERROR, that is packet->data[1]
- /// Name of the function will be appended starting at packet->data[2] - /// \ingroup RPC_PLUGIN_GROUP - enum RPCErrorCodes - { - /// Named function was not registered with RegisterFunction(). Check your spelling. - RPC_ERROR_FUNCTION_NOT_REGISTERED, - }; - - /// \brief Instantiate this class globally if you want to register a function with RPC4 at the global space - class RAK_DLL_EXPORT RPC4GlobalRegistration - { - public: - /// \brief Queue a call to RPC4::RegisterFunction() globally. Actual call occurs once RPC4 is attached to an instance of RakPeer or TCPInterface. - RPC4GlobalRegistration(const char* uniqueID, void ( *functionPointer ) ( RakNet::BitStream *userData, Packet *packet )); - - /// \brief Queue a call to RPC4::RegisterSlot() globally. Actual call occurs once RPC4 is attached to an instance of RakPeer or TCPInterface. - RPC4GlobalRegistration(const char* uniqueID, void ( *functionPointer ) ( RakNet::BitStream *userData, Packet *packet ), int callPriority); - - /// \brief Queue a call to RPC4::RegisterBlockingFunction() globally. Actual call occurs once RPC4 is attached to an instance of RakPeer or TCPInterface. - RPC4GlobalRegistration(const char* uniqueID, void ( *functionPointer ) ( RakNet::BitStream *userData, RakNet::BitStream *returnData, Packet *packet )); - - /// \brief Queue a call to RPC4::RegisterLocalCallback() globally. Actual call occurs once RPC4 is attached to an instance of RakPeer or TCPInterface. - RPC4GlobalRegistration(const char* uniqueID, MessageID messageId); - }; - - /// \brief The RPC4 plugin is just an association between a C function pointer and a string. - /// \details It is for users that want to use RPC, but do not want to use boost. - /// You do not have the automatic serialization or other features of RPC3, and C++ member calls are not supported. - /// \note You cannot use RPC4 at the same time as RPC3Plugin - /// \ingroup RPC_PLUGIN_GROUP - class RAK_DLL_EXPORT RPC4 : public PluginInterface2 - { - public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(RPC4) - - // Constructor - RPC4(); - - // Destructor - virtual ~RPC4(); - - /// \deprecated Use RegisterSlot - /// \brief Register a function pointer to be callable from a remote system - /// \details The hash of the function name will be stored as an association with the function pointer - /// When a call is made to call this function from the \a Call() or CallLoopback() function, the function pointer will be invoked with the passed bitStream to Call() and the actual Packet that RakNet got. - /// \sa RegisterPacketCallback() - /// \param[in] uniqueID Identifier to be associated with \a functionPointer. If this identifier is already in use, the call will return false. - /// \param[in] functionPointer C function pointer to be called - /// \return True if the hash of uniqueID is not in use, false otherwise. - bool RegisterFunction(const char* uniqueID, void ( *functionPointer ) ( RakNet::BitStream *userData, Packet *packet )); - - /// Register a slot, which is a function pointer to one or more implementations that supports this function signature - /// When a signal occurs, all slots with the same identifier are called. - /// \param[in] sharedIdentifier A string to identify the slot. Recommended to be the same as the name of the function. - /// \param[in] functionPtr Pointer to the function. For C, just pass the name of the function. For C++, use ARPC_REGISTER_CPP_FUNCTION - /// \param[in] callPriority Slots are called by order of the highest callPriority first. For slots with the same priority, they are called in the order they are registered - void RegisterSlot(const char *sharedIdentifier, void ( *functionPointer ) ( RakNet::BitStream *userData, Packet *packet ), int callPriority); - - /// \brief Same as \a RegisterFunction, but is called with CallBlocking() instead of Call() and returns a value to the caller - bool RegisterBlockingFunction(const char* uniqueID, void ( *functionPointer ) ( RakNet::BitStream *userData, RakNet::BitStream *returnData, Packet *packet )); - - /// \deprecated Use RegisterSlot and invoke on self only when the packet you want arrives - /// When a RakNet Packet with the specified identifier is returned, execute CallLoopback() on a function previously registered with RegisterFunction() - /// For example, you could call "OnClosedConnection" whenever you get ID_DISCONNECTION_NOTIFICATION or ID_CONNECTION_LOST - /// \param[in] uniqueID Identifier passed to RegisterFunction() - /// \param[in] messageId What RakNet packet ID to call on, for example ID_DISCONNECTION_NOTIFICATION or ID_CONNECTION_LOST - void RegisterLocalCallback(const char* uniqueID, MessageID messageId); - - /// \brief Unregister a function pointer previously registered with RegisterFunction() - /// \param[in] Identifier originally passed to RegisterFunction() - /// \return True if the hash of uniqueID was in use, and hence removed. false otherwise. - bool UnregisterFunction(const char* uniqueID); - - /// \brief Same as UnregisterFunction, except for a blocking function - bool UnregisterBlockingFunction(const char* uniqueID); - - /// Remove the association created with RegisterPacketCallback() - /// \param[in] uniqueID Identifier passed as uniqueID to RegisterLocalCallback() - /// \param[in] messageId Identifier passed as messageId to RegisterLocalCallback() - /// \return True if the combination of uniqueID and messageId was in use, and hence removed - bool UnregisterLocalCallback(const char* uniqueID, MessageID messageId); - - /// Remove the association created with RegisterSlot() - /// \param[in] sharedIdentifier Identifier passed as sharedIdentifier to RegisterSlot() - bool UnregisterSlot(const char* sharedIdentifier); - - /// \deprecated Use RegisterSlot() and Signal() with your own RakNetGUID as the send target - /// Send to the attached instance of RakPeer. See RakPeerInterface::SendLoopback() - /// \param[in] Identifier originally passed to RegisterFunction() on the local system - /// \param[in] bitStream bitStream encoded data to send to the function callback - void CallLoopback( const char* uniqueID, RakNet::BitStream * bitStream ); - - /// \deprecated, use Signal() - /// Send to the specified remote instance of RakPeer. - /// \param[in] uniqueID Identifier originally passed to RegisterFunction() on the remote system(s) - /// \param[in] bitStream bitStream encoded data to send to the function callback - /// \param[in] priority See RakPeerInterface::Send() - /// \param[in] reliability See RakPeerInterface::Send() - /// \param[in] orderingChannel See RakPeerInterface::Send() - /// \param[in] systemIdentifier See RakPeerInterface::Send() - /// \param[in] broadcast See RakPeerInterface::Send() - void Call( const char* uniqueID, RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast ); - - /// \brief Same as call, but don't return until the remote system replies. - /// Broadcasting parameter does not exist, this can only call one remote system - /// \note This function does not return until the remote system responds, disconnects, or was never connected to begin with - /// \param[in] Identifier originally passed to RegisterBlockingFunction() on the remote system(s) - /// \param[in] bitStream bitStream encoded data to send to the function callback - /// \param[in] priority See RakPeerInterface::Send() - /// \param[in] reliability See RakPeerInterface::Send() - /// \param[in] orderingChannel See RakPeerInterface::Send() - /// \param[in] systemIdentifier See RakPeerInterface::Send() - /// \param[out] returnData Written to by the function registered with RegisterBlockingFunction. - /// \return true if successfully called. False on disconnect, function not registered, or not connected to begin with - bool CallBlocking( const char* uniqueID, RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, RakNet::BitStream *returnData ); - - /// Calls zero or more functions identified by sharedIdentifier registered with RegisterSlot() - /// \param[in] sharedIdentifier parameter of the same name passed to RegisterSlot() on the remote system - /// \param[in] bitStream bitStream encoded data to send to the function callback - /// \param[in] priority See RakPeerInterface::Send() - /// \param[in] reliability See RakPeerInterface::Send() - /// \param[in] orderingChannel See RakPeerInterface::Send() - /// \param[in] systemIdentifier See RakPeerInterface::Send() - /// \param[in] broadcast See RakPeerInterface::Send() - /// \param[in] invokeLocal If true, also sends to self. - void Signal(const char *sharedIdentifier, RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, bool invokeLocal); - - /// If called while processing a slot, no further slots for the currently executing signal will be executed - void InterruptSignal(void); - - /// \internal - struct LocalCallback - { - MessageID messageId; - DataStructures::OrderedList functions; - }; - static int LocalCallbackComp(const MessageID &key, LocalCallback* const &data ); - - /// \internal - // Callable object, along with priority to call relative to other objects - struct LocalSlotObject - { - LocalSlotObject() {} - LocalSlotObject(unsigned int _registrationCount,int _callPriority, void ( *_functionPointer ) ( RakNet::BitStream *userData, Packet *packet )) - {registrationCount=_registrationCount;callPriority=_callPriority;functionPointer=_functionPointer;} - ~LocalSlotObject() {} - - // Used so slots are called in the order they are registered - unsigned int registrationCount; - int callPriority; - void ( *functionPointer ) ( RakNet::BitStream *userData, Packet *packet ); - }; - - static int LocalSlotObjectComp( const LocalSlotObject &key, const LocalSlotObject &data ); - - /// \internal - struct LocalSlot - { - DataStructures::OrderedList slotObjects; - }; - DataStructures::Hash localSlots; - - protected: - - // -------------------------------------------------------------------------------------------- - // Packet handling functions - // -------------------------------------------------------------------------------------------- - virtual void OnAttach(void); - virtual PluginReceiveResult OnReceive(Packet *packet); - - DataStructures::Hash registeredNonblockingFunctions; - DataStructures::Hash registeredBlockingFunctions; - DataStructures::OrderedList localCallbacks; - - RakNet::BitStream blockingReturnValue; - bool gotBlockingReturnValue; - - DataStructures::HashIndex GetLocalSlotIndex(const char *sharedIdentifier); - - /// Used so slots are called in the order they are registered - unsigned int nextSlotRegistrationCount; - - bool interruptSignal; - - void InvokeSignal(DataStructures::HashIndex functionIndex, RakNet::BitStream *serializedParameters, Packet *packet); - }; - -} // End namespace - -#endif - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Remote procedure call, supporting C functions only. No external dependencies required. +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_RPC4Plugin==1 + +#pragma once + +#include "PluginInterface2.h" +#include "PacketPriority.h" +#include "RakNetTypes.h" +#include "BitStream.h" +#include "RakString.h" +#include "NetworkIDObject.h" +#include "DS_Hash.h" +#include "DS_OrderedList.h" + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +/// \defgroup RPC_PLUGIN_GROUP RPC +/// \brief Remote procedure calls, without external dependencies. +/// \details This should not be used at the same time as RPC3. This is a less functional version of RPC3, and is here for users that do not want the Boost dependency of RPC3. +/// \ingroup PLUGINS_GROUP + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; +class NetworkIDManager; + + /// \brief Error codes returned by a remote system as to why an RPC function call cannot execute + /// \details Error code follows packet ID ID_RPC_REMOTE_ERROR, that is packet->data[1]
+ /// Name of the function will be appended starting at packet->data[2] + /// \ingroup RPC_PLUGIN_GROUP + enum RPCErrorCodes + { + /// Named function was not registered with RegisterFunction(). Check your spelling. + RPC_ERROR_FUNCTION_NOT_REGISTERED, + }; + + /// \brief Instantiate this class globally if you want to register a function with RPC4 at the global space + class RAK_DLL_EXPORT RPC4GlobalRegistration + { + public: + /// \brief Queue a call to RPC4::RegisterFunction() globally. Actual call occurs once RPC4 is attached to an instance of RakPeer or TCPInterface. + RPC4GlobalRegistration(const char* uniqueID, void ( *functionPointer ) ( RakNet::BitStream *userData, Packet *packet )); + + /// \brief Queue a call to RPC4::RegisterSlot() globally. Actual call occurs once RPC4 is attached to an instance of RakPeer or TCPInterface. + RPC4GlobalRegistration(const char* uniqueID, void ( *functionPointer ) ( RakNet::BitStream *userData, Packet *packet ), int callPriority); + + /// \brief Queue a call to RPC4::RegisterBlockingFunction() globally. Actual call occurs once RPC4 is attached to an instance of RakPeer or TCPInterface. + RPC4GlobalRegistration(const char* uniqueID, void ( *functionPointer ) ( RakNet::BitStream *userData, RakNet::BitStream *returnData, Packet *packet )); + + /// \brief Queue a call to RPC4::RegisterLocalCallback() globally. Actual call occurs once RPC4 is attached to an instance of RakPeer or TCPInterface. + RPC4GlobalRegistration(const char* uniqueID, MessageID messageId); + }; + + /// \brief The RPC4 plugin is just an association between a C function pointer and a string. + /// \details It is for users that want to use RPC, but do not want to use boost. + /// You do not have the automatic serialization or other features of RPC3, and C++ member calls are not supported. + /// \note You cannot use RPC4 at the same time as RPC3Plugin + /// \ingroup RPC_PLUGIN_GROUP + class RAK_DLL_EXPORT RPC4 : public PluginInterface2 + { + public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(RPC4) + + // Constructor + RPC4(); + + // Destructor + virtual ~RPC4(); + + /// \deprecated Use RegisterSlot + /// \brief Register a function pointer to be callable from a remote system + /// \details The hash of the function name will be stored as an association with the function pointer + /// When a call is made to call this function from the \a Call() or CallLoopback() function, the function pointer will be invoked with the passed bitStream to Call() and the actual Packet that RakNet got. + /// \sa RegisterPacketCallback() + /// \param[in] uniqueID Identifier to be associated with \a functionPointer. If this identifier is already in use, the call will return false. + /// \param[in] functionPointer C function pointer to be called + /// \return True if the hash of uniqueID is not in use, false otherwise. + bool RegisterFunction(const char* uniqueID, void ( *functionPointer ) ( RakNet::BitStream *userData, Packet *packet )); + + /// Register a slot, which is a function pointer to one or more implementations that supports this function signature + /// When a signal occurs, all slots with the same identifier are called. + /// \param[in] sharedIdentifier A string to identify the slot. Recommended to be the same as the name of the function. + /// \param[in] functionPtr Pointer to the function. For C, just pass the name of the function. For C++, use ARPC_REGISTER_CPP_FUNCTION + /// \param[in] callPriority Slots are called by order of the highest callPriority first. For slots with the same priority, they are called in the order they are registered + void RegisterSlot(const char *sharedIdentifier, void ( *functionPointer ) ( RakNet::BitStream *userData, Packet *packet ), int callPriority); + + /// \brief Same as \a RegisterFunction, but is called with CallBlocking() instead of Call() and returns a value to the caller + bool RegisterBlockingFunction(const char* uniqueID, void ( *functionPointer ) ( RakNet::BitStream *userData, RakNet::BitStream *returnData, Packet *packet )); + + /// \deprecated Use RegisterSlot and invoke on self only when the packet you want arrives + /// When a RakNet Packet with the specified identifier is returned, execute CallLoopback() on a function previously registered with RegisterFunction() + /// For example, you could call "OnClosedConnection" whenever you get ID_DISCONNECTION_NOTIFICATION or ID_CONNECTION_LOST + /// \param[in] uniqueID Identifier passed to RegisterFunction() + /// \param[in] messageId What RakNet packet ID to call on, for example ID_DISCONNECTION_NOTIFICATION or ID_CONNECTION_LOST + void RegisterLocalCallback(const char* uniqueID, MessageID messageId); + + /// \brief Unregister a function pointer previously registered with RegisterFunction() + /// \param[in] Identifier originally passed to RegisterFunction() + /// \return True if the hash of uniqueID was in use, and hence removed. false otherwise. + bool UnregisterFunction(const char* uniqueID); + + /// \brief Same as UnregisterFunction, except for a blocking function + bool UnregisterBlockingFunction(const char* uniqueID); + + /// Remove the association created with RegisterPacketCallback() + /// \param[in] uniqueID Identifier passed as uniqueID to RegisterLocalCallback() + /// \param[in] messageId Identifier passed as messageId to RegisterLocalCallback() + /// \return True if the combination of uniqueID and messageId was in use, and hence removed + bool UnregisterLocalCallback(const char* uniqueID, MessageID messageId); + + /// Remove the association created with RegisterSlot() + /// \param[in] sharedIdentifier Identifier passed as sharedIdentifier to RegisterSlot() + bool UnregisterSlot(const char* sharedIdentifier); + + /// \deprecated Use RegisterSlot() and Signal() with your own RakNetGUID as the send target + /// Send to the attached instance of RakPeer. See RakPeerInterface::SendLoopback() + /// \param[in] Identifier originally passed to RegisterFunction() on the local system + /// \param[in] bitStream bitStream encoded data to send to the function callback + void CallLoopback( const char* uniqueID, RakNet::BitStream * bitStream ); + + /// \deprecated, use Signal() + /// Send to the specified remote instance of RakPeer. + /// \param[in] uniqueID Identifier originally passed to RegisterFunction() on the remote system(s) + /// \param[in] bitStream bitStream encoded data to send to the function callback + /// \param[in] priority See RakPeerInterface::Send() + /// \param[in] reliability See RakPeerInterface::Send() + /// \param[in] orderingChannel See RakPeerInterface::Send() + /// \param[in] systemIdentifier See RakPeerInterface::Send() + /// \param[in] broadcast See RakPeerInterface::Send() + void Call( const char* uniqueID, RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast ); + + /// \brief Same as call, but don't return until the remote system replies. + /// Broadcasting parameter does not exist, this can only call one remote system + /// \note This function does not return until the remote system responds, disconnects, or was never connected to begin with + /// \param[in] Identifier originally passed to RegisterBlockingFunction() on the remote system(s) + /// \param[in] bitStream bitStream encoded data to send to the function callback + /// \param[in] priority See RakPeerInterface::Send() + /// \param[in] reliability See RakPeerInterface::Send() + /// \param[in] orderingChannel See RakPeerInterface::Send() + /// \param[in] systemIdentifier See RakPeerInterface::Send() + /// \param[out] returnData Written to by the function registered with RegisterBlockingFunction. + /// \return true if successfully called. False on disconnect, function not registered, or not connected to begin with + bool CallBlocking( const char* uniqueID, RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, RakNet::BitStream *returnData ); + + /// Calls zero or more functions identified by sharedIdentifier registered with RegisterSlot() + /// \param[in] sharedIdentifier parameter of the same name passed to RegisterSlot() on the remote system + /// \param[in] bitStream bitStream encoded data to send to the function callback + /// \param[in] priority See RakPeerInterface::Send() + /// \param[in] reliability See RakPeerInterface::Send() + /// \param[in] orderingChannel See RakPeerInterface::Send() + /// \param[in] systemIdentifier See RakPeerInterface::Send() + /// \param[in] broadcast See RakPeerInterface::Send() + /// \param[in] invokeLocal If true, also sends to self. + void Signal(const char *sharedIdentifier, RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, bool invokeLocal); + + /// If called while processing a slot, no further slots for the currently executing signal will be executed + void InterruptSignal(void); + + /// \internal + struct LocalCallback + { + MessageID messageId; + DataStructures::OrderedList functions; + }; + static int LocalCallbackComp(const MessageID &key, LocalCallback* const &data ); + + /// \internal + // Callable object, along with priority to call relative to other objects + struct LocalSlotObject + { + LocalSlotObject() {} + LocalSlotObject(unsigned int _registrationCount,int _callPriority, void ( *_functionPointer ) ( RakNet::BitStream *userData, Packet *packet )) + {registrationCount=_registrationCount;callPriority=_callPriority;functionPointer=_functionPointer;} + ~LocalSlotObject() {} + + // Used so slots are called in the order they are registered + unsigned int registrationCount; + int callPriority; + void ( *functionPointer ) ( RakNet::BitStream *userData, Packet *packet ); + }; + + static int LocalSlotObjectComp( const LocalSlotObject &key, const LocalSlotObject &data ); + + /// \internal + struct LocalSlot + { + DataStructures::OrderedList slotObjects; + }; + DataStructures::Hash localSlots; + + protected: + + // -------------------------------------------------------------------------------------------- + // Packet handling functions + // -------------------------------------------------------------------------------------------- + virtual void OnAttach(void); + virtual PluginReceiveResult OnReceive(Packet *packet); + + DataStructures::Hash registeredNonblockingFunctions; + DataStructures::Hash registeredBlockingFunctions; + DataStructures::OrderedList localCallbacks; + + RakNet::BitStream blockingReturnValue; + bool gotBlockingReturnValue; + + DataStructures::HashIndex GetLocalSlotIndex(const char *sharedIdentifier); + + /// Used so slots are called in the order they are registered + unsigned int nextSlotRegistrationCount; + + bool interruptSignal; + + void InvokeSignal(DataStructures::HashIndex functionIndex, RakNet::BitStream *serializedParameters, Packet *packet); + }; + +} // End namespace + +#endif + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif + diff --git a/Source/Rackspace.h b/Source/Rackspace.h index d55c4e0fe..c5ebc75d3 100644 --- a/Source/Rackspace.h +++ b/Source/Rackspace.h @@ -1,413 +1,411 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file Rackspace.h -/// \brief Helper to class to manage Rackspace servers -/// - - -#include "NativeFeatureIncludes.h" - -#if _RAKNET_SUPPORT_Rackspace==1 && _RAKNET_SUPPORT_TCPInterface==1 - -#include "Export.h" -#include "DS_List.h" -#include "RakNetTypes.h" -#include "DS_Queue.h" -#include "RakString.h" - -#ifndef __RACKSPACE_H -#define __RACKSPACE_H - -namespace RakNet -{ - - class TCPInterface; - struct Packet; - - /// \brief Result codes for Rackspace commands - /// /sa Rackspace::EventTypeToString() - enum RackspaceEventType - { - RET_Success_200, - RET_Success_201, - RET_Success_202, - RET_Success_203, - RET_Success_204, - RET_Cloud_Servers_Fault_500, - RET_Service_Unavailable_503, - RET_Unauthorized_401, - RET_Bad_Request_400, - RET_Over_Limit_413, - RET_Bad_Media_Type_415, - RET_Item_Not_Found_404, - RET_Build_In_Progress_409, - RET_Resize_Not_Allowed_403, - RET_Connection_Closed_Without_Reponse, - RET_Unknown_Failure, - }; - - /// \internal - enum RackspaceOperationType - { - RO_CONNECT_AND_AUTHENTICATE, - RO_LIST_SERVERS, - RO_LIST_SERVERS_WITH_DETAILS, - RO_CREATE_SERVER, - RO_GET_SERVER_DETAILS, - RO_UPDATE_SERVER_NAME_OR_PASSWORD, - RO_DELETE_SERVER, - RO_LIST_SERVER_ADDRESSES, - RO_SHARE_SERVER_ADDRESS, - RO_DELETE_SERVER_ADDRESS, - RO_REBOOT_SERVER, - RO_REBUILD_SERVER, - RO_RESIZE_SERVER, - RO_CONFIRM_RESIZED_SERVER, - RO_REVERT_RESIZED_SERVER, - RO_LIST_FLAVORS, - RO_GET_FLAVOR_DETAILS, - RO_LIST_IMAGES, - RO_CREATE_IMAGE, - RO_GET_IMAGE_DETAILS, - RO_DELETE_IMAGE, - RO_LIST_SHARED_IP_GROUPS, - RO_LIST_SHARED_IP_GROUPS_WITH_DETAILS, - RO_CREATE_SHARED_IP_GROUP, - RO_GET_SHARED_IP_GROUP_DETAILS, - RO_DELETE_SHARED_IP_GROUP, - - RO_NONE, - }; - - /// \brief Callback interface to receive the results of operations - class RAK_DLL_EXPORT Rackspace2EventCallback - { - public: - Rackspace2EventCallback() {} - virtual ~Rackspace2EventCallback() {} - virtual void OnAuthenticationResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnListServersResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnListServersWithDetailsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnCreateServerResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnGetServerDetails(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnUpdateServerNameOrPassword(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnDeleteServer(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnListServerAddresses(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnShareServerAddress(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnDeleteServerAddress(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnRebootServer(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnRebuildServer(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnResizeServer(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnConfirmResizedServer(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnRevertResizedServer(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnListFlavorsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnGetFlavorDetailsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnListImagesResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnCreateImageResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnGetImageDetailsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnDeleteImageResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnListSharedIPGroups(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnListSharedIPGroupsWithDetails(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnCreateSharedIPGroup(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnGetSharedIPGroupDetails(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - virtual void OnDeleteSharedIPGroup(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; - - virtual void OnConnectionAttemptFailure(RackspaceOperationType operationType, const char *url)=0; - }; - - /// \brief Callback interface to receive the results of operations, with a default result - class RAK_DLL_EXPORT RackspaceEventCallback_Default : public Rackspace2EventCallback - { - public: - virtual void ExecuteDefault(const char *callbackName, RackspaceEventType eventType, const char *htmlAdditionalInfo) {(void) callbackName; (void) eventType; (void) htmlAdditionalInfo;} - - virtual void OnAuthenticationResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnAuthenticationResult", eventType, htmlAdditionalInfo);} - virtual void OnListServersResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnListServersResult", eventType, htmlAdditionalInfo);} - virtual void OnListServersWithDetailsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnListServersWithDetailsResult", eventType, htmlAdditionalInfo);} - virtual void OnCreateServerResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnCreateServerResult", eventType, htmlAdditionalInfo);} - virtual void OnGetServerDetails(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnGetServerDetails", eventType, htmlAdditionalInfo);} - virtual void OnUpdateServerNameOrPassword(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnUpdateServerNameOrPassword", eventType, htmlAdditionalInfo);} - virtual void OnDeleteServer(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnDeleteServer", eventType, htmlAdditionalInfo);} - virtual void OnListServerAddresses(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnListServerAddresses", eventType, htmlAdditionalInfo);} - virtual void OnShareServerAddress(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnShareServerAddress", eventType, htmlAdditionalInfo);} - virtual void OnDeleteServerAddress(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnDeleteServerAddress", eventType, htmlAdditionalInfo);} - virtual void OnRebootServer(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnRebootServer", eventType, htmlAdditionalInfo);} - virtual void OnRebuildServer(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnRebuildServer", eventType, htmlAdditionalInfo);} - virtual void OnResizeServer(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnResizeServer", eventType, htmlAdditionalInfo);} - virtual void OnConfirmResizedServer(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnConfirmResizedServer", eventType, htmlAdditionalInfo);} - virtual void OnRevertResizedServer(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnRevertResizedServer", eventType, htmlAdditionalInfo);} - virtual void OnListFlavorsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnListFlavorsResult", eventType, htmlAdditionalInfo);} - virtual void OnGetFlavorDetailsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnGetFlavorDetailsResult", eventType, htmlAdditionalInfo);} - virtual void OnListImagesResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnListImagesResult", eventType, htmlAdditionalInfo);} - virtual void OnCreateImageResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnCreateImageResult", eventType, htmlAdditionalInfo);} - virtual void OnGetImageDetailsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnGetImageDetailsResult", eventType, htmlAdditionalInfo);} - virtual void OnDeleteImageResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnDeleteImageResult", eventType, htmlAdditionalInfo);} - virtual void OnListSharedIPGroups(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnListSharedIPGroups", eventType, htmlAdditionalInfo);} - virtual void OnListSharedIPGroupsWithDetails(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnListSharedIPGroupsWithDetails", eventType, htmlAdditionalInfo);} - virtual void OnCreateSharedIPGroup(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnCreateSharedIPGroup", eventType, htmlAdditionalInfo);} - virtual void OnGetSharedIPGroupDetails(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnGetSharedIPGroupDetails", eventType, htmlAdditionalInfo);} - virtual void OnDeleteSharedIPGroup(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnDeleteSharedIPGroup", eventType, htmlAdditionalInfo);} - - virtual void OnConnectionAttemptFailure(RackspaceOperationType operationType, const char *url) {(void) operationType; (void) url;} - }; - - /// \brief Code that uses the TCPInterface class to communicate with the Rackspace API servers - /// \pre Compile RakNet with OPEN_SSL_CLIENT_SUPPORT set to 1 - /// \pre Packets returned from TCPInterface::OnReceive() must be passed to Rackspace::OnReceive() - /// \pre Packets returned from TCPInterface::HasLostConnection() must be passed to Rackspace::OnClosedConnection() - class RAK_DLL_EXPORT Rackspace - { - public: - Rackspace(); - ~Rackspace(); - - /// \brief Authenticate with Rackspace servers, required before executing any commands. - /// \details All requests to authenticate and operate against Cloud Servers are performed using SSL over HTTP (HTTPS) on TCP port 443. - /// Times out after 24 hours - if you get RET_Authenticate_Unauthorized in the RackspaceEventCallback callback, call again - /// \sa RackspaceEventCallback::OnAuthenticationResult() - /// \param[in] _tcpInterface An instance of TCPInterface, build with OPEN_SSL_CLIENT_SUPPORT 1 and already started - /// \param[in] _authenticationURL See http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf . US-based accounts authenticate through auth.api.rackspacecloud.com. UK-based accounts authenticate through lon.auth.api.rackspacecloud.com - /// \param[in] _rackspaceCloudUsername Username you registered with Rackspace on their website - /// \param[in] _apiAccessKey Obtain your API access key from the Rackspace Cloud Control Panel in the Your Account API Access section. - /// \return The address of the authentication server, or UNASSIGNED_SYSTEM_ADDRESS if the connection attempt failed - SystemAddress Authenticate(TCPInterface *_tcpInterface, const char *_authenticationURL, const char *_rackspaceCloudUsername, const char *_apiAccessKey); - - /// \brief Get a list of running servers - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnListServersResult() - void ListServers(void); - - /// \brief Get a list of running servers, with extended details on each server - /// \sa GetServerDetails() - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnListServersWithDetailsResult() - void ListServersWithDetails(void); - - /// \brief Create a server - /// \details Create a server with a given image (harddrive contents) and flavor (hardware configuration) - /// Get the available images with ListImages() - /// Get the available flavors with ListFlavors() - /// It is possible to configure the server in more detail. See the XML schema at http://docs.rackspacecloud.com/servers/api/v1.0 - /// You can execute such a custom command by calling AddOperation() manually. See the implementation of CreateServer for how to do so. - /// The server takes a while to build. Call GetServerDetails() to get the current build status. Server id to pass to GetServerDetails() is returned in the field - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnCreateServerResult() - /// \param[in] name Name of the server. Only alphanumeric characters, periods, and hyphens are valid. Server Name cannot start or end with a period or hyphen. - /// \param[in] imageId Which image (harddrive contents, including OS) to use - /// \param[in] flavorId Which flavor (hardware config) to use, primarily how much memory is available. - void CreateServer(RakNet::RakString name, RakNet::RakString imageId, RakNet::RakString flavorId); - - /// \brief Get details on a particular server - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnGetServerDetailsResult() - /// \param[in] serverId Which server to get details on. You can call ListServers() to get the list of active servers. - void GetServerDetails(RakNet::RakString serverId); - - /// \brief Changes the name or password for a server - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnUpdateServerNameOrPasswordResult() - /// \param[in] serverId Which server to get details on. You can call ListServers() to get the list of active servers. - /// \param[in] newName The new server name. Leave blank to leave unchanged. Only alphanumeric characters, periods, and hyphens are valid. Server Name cannot start or end with a period or hyphen. - /// \param[in] newPassword The new server password. Leave blank to leave unchanged. - void UpdateServerNameOrPassword(RakNet::RakString serverId, RakNet::RakString newName, RakNet::RakString newPassword); - - /// \brief Deletes a server - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnDeleteServerResult() - /// \param[in] serverId Which server to get details on. You can call ListServers() to get the list of active servers. - void DeleteServer(RakNet::RakString serverId); - - /// \brief Lists the IP addresses available to a server - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnListServerAddressesResult() - /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. - void ListServerAddresses(RakNet::RakString serverId); - - /// \brief Shares an IP address with a server - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnShareServerAddressResult() - /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. - /// \param[in] ipAddress Which IP address. You can call ListServerAddresses() to get the list of addresses for the specified server - void ShareServerAddress(RakNet::RakString serverId, RakNet::RakString ipAddress); - - /// \brief Stops sharing an IP address with a server - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnDeleteServerAddressResult() - /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. - /// \param[in] ipAddress Which IP address. You can call ListServerAddresses() to get the list of addresses for the specified server - void DeleteServerAddress(RakNet::RakString serverId, RakNet::RakString ipAddress); - - /// \brief Reboots a server - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnRebootServerResult() - /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. - /// \param[in] rebootType Should be either "HARD" or "SOFT" - void RebootServer(RakNet::RakString serverId, RakNet::RakString rebootType); - - /// \brief Rebuilds a server with a different image (harddrive contents) - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnRebuildServerResult() - /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. - /// \param[in] imageId Which image (harddrive contents, including OS) to use - void RebuildServer(RakNet::RakString serverId, RakNet::RakString imageId); - - /// \brief Changes the hardware configuration of a server. This does not take effect until you call ConfirmResizedServer() - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnResizeServerResult() - /// \sa RevertResizedServer() - /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. - /// \param[in] flavorId Which flavor (hardware config) to use, primarily how much memory is available. - void ResizeServer(RakNet::RakString serverId, RakNet::RakString flavorId); - - /// \brief Confirm a resize for the specified server - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnConfirmResizedServerResult() - /// \sa ResizeServer() - /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. - void ConfirmResizedServer(RakNet::RakString serverId); - - /// \brief Reverts a resize for the specified server - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnRevertResizedServerResult() - /// \sa ResizeServer() - /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. - void RevertResizedServer(RakNet::RakString serverId); - - /// \brief List all flavors (hardware configs, primarily memory) - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnListFlavorsResult() - void ListFlavors(void); - - /// \brief Get extended details about a specific flavor - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnGetFlavorDetailsResult() - /// \sa ListFlavors() - /// \param[in] flavorId Which flavor (hardware config) - void GetFlavorDetails(RakNet::RakString flavorId); - - /// \brief List all images (software configs, including operating systems), which includes images you create yourself - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnListImagesResult() - /// \sa CreateImage() - void ListImages(void); - - /// \brief Images a running server. This essentially copies the harddrive, and lets you start a server with the same harddrive contents later - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnCreateImageResult() - /// \sa ListImages() - /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. - /// \param[in] imageName What to call this image - void CreateImage(RakNet::RakString serverId, RakNet::RakString imageName); - - /// \brief Get extended details about a particular image - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnGetImageDetailsResult() - /// \sa ListImages() - /// \param[in] imageId Which image - void GetImageDetails(RakNet::RakString imageId); - - /// \brief Delete a custom image created with CreateImage() - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnDeleteImageResult() - /// \sa ListImages() - /// \param[in] imageId Which image - void DeleteImage(RakNet::RakString imageId); - - /// \brief List IP groups - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnListSharedIPGroupsResult() - void ListSharedIPGroups(void); - - /// \brief List IP groups with extended details - /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf - /// \sa RackspaceEventCallback::OnListSharedIPGroupsWithDetailsResult() - void ListSharedIPGroupsWithDetails(void); - - // I don't know what this does - void CreateSharedIPGroup(RakNet::RakString name, RakNet::RakString optionalServerId); - // I don't know what this does - void GetSharedIPGroupDetails(RakNet::RakString groupId); - // I don't know what this does - void DeleteSharedIPGroup(RakNet::RakString groupId); - - /// \brief Adds a callback to the list of callbacks to be called when any of the above functions finish executing - /// The callbacks are called in the order they are added - void AddEventCallback(Rackspace2EventCallback *callback); - /// \brief Removes a callback from the list of callbacks to be called when any of the above functions finish executing - /// The callbacks are called in the order they are added - void RemoveEventCallback(Rackspace2EventCallback *callback); - /// \brief Removes all callbacks - void ClearEventCallbacks(void); - - /// Call this anytime TCPInterface returns a packet - void OnReceive(Packet *packet); - - /// Call this when TCPInterface returns something other than UNASSIGNED_SYSTEM_ADDRESS from HasLostConnection() - void OnClosedConnection(SystemAddress systemAddress); - - /// String representation of each RackspaceEventType - static const char * EventTypeToString(RackspaceEventType eventType); - - /// \brief Mostly for internal use, but you can use it to execute an operation with more complex xml if desired - /// See the Rackspace.cpp on how to use it - void AddOperation(RackspaceOperationType type, RakNet::RakString httpCommand, RakNet::RakString operation, RakNet::RakString xml); - protected: - - DataStructures::List eventCallbacks; - - struct RackspaceOperation - { - RackspaceOperationType type; - // RakNet::RakString stringInfo; - SystemAddress connectionAddress; - bool isPendingAuthentication; - RakNet::RakString incomingStream; - RakNet::RakString httpCommand; - RakNet::RakString operation; - RakNet::RakString xml; - }; - - TCPInterface *tcpInterface; - - // RackspaceOperationType currentOperation; - // DataStructures::Queue nextOperationQueue; - - DataStructures::List operations; - bool HasOperationOfType(RackspaceOperationType t); - unsigned int GetOperationOfTypeIndex(RackspaceOperationType t); - - RakNet::RakString serverManagementURL; - RakNet::RakString serverManagementDomain; - RakNet::RakString serverManagementPath; - RakNet::RakString storageURL; - RakNet::RakString storageDomain; - RakNet::RakString storagePath; - RakNet::RakString cdnManagementURL; - RakNet::RakString cdnManagementDomain; - RakNet::RakString cdnManagementPath; - - RakNet::RakString storageToken; - RakNet::RakString authToken; - RakNet::RakString rackspaceCloudUsername; - RakNet::RakString apiAccessKey; - - bool ExecuteOperation(RackspaceOperation &ro); - void ReadLine(const char *data, const char *stringStart, RakNet::RakString &output); - bool ConnectToServerManagementDomain(RackspaceOperation &ro); - - - }; - -} // namespace RakNet - -#endif // __RACKSPACE_API_H - -#endif // _RAKNET_SUPPORT_Rackspace +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file Rackspace.h +/// \brief Helper to class to manage Rackspace servers +/// + + +#include "NativeFeatureIncludes.h" + +#if _RAKNET_SUPPORT_Rackspace==1 && _RAKNET_SUPPORT_TCPInterface==1 + +#include "Export.h" +#include "DS_List.h" +#include "RakNetTypes.h" +#include "DS_Queue.h" +#include "RakString.h" + +#pragma once + +namespace RakNet +{ + + class TCPInterface; + struct Packet; + + /// \brief Result codes for Rackspace commands + /// /sa Rackspace::EventTypeToString() + enum RackspaceEventType + { + RET_Success_200, + RET_Success_201, + RET_Success_202, + RET_Success_203, + RET_Success_204, + RET_Cloud_Servers_Fault_500, + RET_Service_Unavailable_503, + RET_Unauthorized_401, + RET_Bad_Request_400, + RET_Over_Limit_413, + RET_Bad_Media_Type_415, + RET_Item_Not_Found_404, + RET_Build_In_Progress_409, + RET_Resize_Not_Allowed_403, + RET_Connection_Closed_Without_Reponse, + RET_Unknown_Failure, + }; + + /// \internal + enum RackspaceOperationType + { + RO_CONNECT_AND_AUTHENTICATE, + RO_LIST_SERVERS, + RO_LIST_SERVERS_WITH_DETAILS, + RO_CREATE_SERVER, + RO_GET_SERVER_DETAILS, + RO_UPDATE_SERVER_NAME_OR_PASSWORD, + RO_DELETE_SERVER, + RO_LIST_SERVER_ADDRESSES, + RO_SHARE_SERVER_ADDRESS, + RO_DELETE_SERVER_ADDRESS, + RO_REBOOT_SERVER, + RO_REBUILD_SERVER, + RO_RESIZE_SERVER, + RO_CONFIRM_RESIZED_SERVER, + RO_REVERT_RESIZED_SERVER, + RO_LIST_FLAVORS, + RO_GET_FLAVOR_DETAILS, + RO_LIST_IMAGES, + RO_CREATE_IMAGE, + RO_GET_IMAGE_DETAILS, + RO_DELETE_IMAGE, + RO_LIST_SHARED_IP_GROUPS, + RO_LIST_SHARED_IP_GROUPS_WITH_DETAILS, + RO_CREATE_SHARED_IP_GROUP, + RO_GET_SHARED_IP_GROUP_DETAILS, + RO_DELETE_SHARED_IP_GROUP, + + RO_NONE, + }; + + /// \brief Callback interface to receive the results of operations + class RAK_DLL_EXPORT Rackspace2EventCallback + { + public: + Rackspace2EventCallback() {} + virtual ~Rackspace2EventCallback() {} + virtual void OnAuthenticationResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnListServersResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnListServersWithDetailsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnCreateServerResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnGetServerDetails(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnUpdateServerNameOrPassword(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnDeleteServer(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnListServerAddresses(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnShareServerAddress(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnDeleteServerAddress(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnRebootServer(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnRebuildServer(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnResizeServer(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnConfirmResizedServer(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnRevertResizedServer(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnListFlavorsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnGetFlavorDetailsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnListImagesResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnCreateImageResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnGetImageDetailsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnDeleteImageResult(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnListSharedIPGroups(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnListSharedIPGroupsWithDetails(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnCreateSharedIPGroup(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnGetSharedIPGroupDetails(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + virtual void OnDeleteSharedIPGroup(RackspaceEventType eventType, const char *htmlAdditionalInfo)=0; + + virtual void OnConnectionAttemptFailure(RackspaceOperationType operationType, const char *url)=0; + }; + + /// \brief Callback interface to receive the results of operations, with a default result + class RAK_DLL_EXPORT RackspaceEventCallback_Default : public Rackspace2EventCallback + { + public: + virtual void ExecuteDefault(const char *callbackName, RackspaceEventType eventType, const char *htmlAdditionalInfo) {(void) callbackName; (void) eventType; (void) htmlAdditionalInfo;} + + virtual void OnAuthenticationResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnAuthenticationResult", eventType, htmlAdditionalInfo);} + virtual void OnListServersResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnListServersResult", eventType, htmlAdditionalInfo);} + virtual void OnListServersWithDetailsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnListServersWithDetailsResult", eventType, htmlAdditionalInfo);} + virtual void OnCreateServerResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnCreateServerResult", eventType, htmlAdditionalInfo);} + virtual void OnGetServerDetails(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnGetServerDetails", eventType, htmlAdditionalInfo);} + virtual void OnUpdateServerNameOrPassword(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnUpdateServerNameOrPassword", eventType, htmlAdditionalInfo);} + virtual void OnDeleteServer(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnDeleteServer", eventType, htmlAdditionalInfo);} + virtual void OnListServerAddresses(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnListServerAddresses", eventType, htmlAdditionalInfo);} + virtual void OnShareServerAddress(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnShareServerAddress", eventType, htmlAdditionalInfo);} + virtual void OnDeleteServerAddress(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnDeleteServerAddress", eventType, htmlAdditionalInfo);} + virtual void OnRebootServer(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnRebootServer", eventType, htmlAdditionalInfo);} + virtual void OnRebuildServer(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnRebuildServer", eventType, htmlAdditionalInfo);} + virtual void OnResizeServer(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnResizeServer", eventType, htmlAdditionalInfo);} + virtual void OnConfirmResizedServer(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnConfirmResizedServer", eventType, htmlAdditionalInfo);} + virtual void OnRevertResizedServer(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnRevertResizedServer", eventType, htmlAdditionalInfo);} + virtual void OnListFlavorsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnListFlavorsResult", eventType, htmlAdditionalInfo);} + virtual void OnGetFlavorDetailsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnGetFlavorDetailsResult", eventType, htmlAdditionalInfo);} + virtual void OnListImagesResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnListImagesResult", eventType, htmlAdditionalInfo);} + virtual void OnCreateImageResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnCreateImageResult", eventType, htmlAdditionalInfo);} + virtual void OnGetImageDetailsResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnGetImageDetailsResult", eventType, htmlAdditionalInfo);} + virtual void OnDeleteImageResult(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnDeleteImageResult", eventType, htmlAdditionalInfo);} + virtual void OnListSharedIPGroups(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnListSharedIPGroups", eventType, htmlAdditionalInfo);} + virtual void OnListSharedIPGroupsWithDetails(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnListSharedIPGroupsWithDetails", eventType, htmlAdditionalInfo);} + virtual void OnCreateSharedIPGroup(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnCreateSharedIPGroup", eventType, htmlAdditionalInfo);} + virtual void OnGetSharedIPGroupDetails(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnGetSharedIPGroupDetails", eventType, htmlAdditionalInfo);} + virtual void OnDeleteSharedIPGroup(RackspaceEventType eventType, const char *htmlAdditionalInfo) {ExecuteDefault("OnDeleteSharedIPGroup", eventType, htmlAdditionalInfo);} + + virtual void OnConnectionAttemptFailure(RackspaceOperationType operationType, const char *url) {(void) operationType; (void) url;} + }; + + /// \brief Code that uses the TCPInterface class to communicate with the Rackspace API servers + /// \pre Compile RakNet with OPEN_SSL_CLIENT_SUPPORT set to 1 + /// \pre Packets returned from TCPInterface::OnReceive() must be passed to Rackspace::OnReceive() + /// \pre Packets returned from TCPInterface::HasLostConnection() must be passed to Rackspace::OnClosedConnection() + class RAK_DLL_EXPORT Rackspace + { + public: + Rackspace(); + ~Rackspace(); + + /// \brief Authenticate with Rackspace servers, required before executing any commands. + /// \details All requests to authenticate and operate against Cloud Servers are performed using SSL over HTTP (HTTPS) on TCP port 443. + /// Times out after 24 hours - if you get RET_Authenticate_Unauthorized in the RackspaceEventCallback callback, call again + /// \sa RackspaceEventCallback::OnAuthenticationResult() + /// \param[in] _tcpInterface An instance of TCPInterface, build with OPEN_SSL_CLIENT_SUPPORT 1 and already started + /// \param[in] _authenticationURL See http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf . US-based accounts authenticate through auth.api.rackspacecloud.com. UK-based accounts authenticate through lon.auth.api.rackspacecloud.com + /// \param[in] _rackspaceCloudUsername Username you registered with Rackspace on their website + /// \param[in] _apiAccessKey Obtain your API access key from the Rackspace Cloud Control Panel in the Your Account API Access section. + /// \return The address of the authentication server, or UNASSIGNED_SYSTEM_ADDRESS if the connection attempt failed + SystemAddress Authenticate(TCPInterface *_tcpInterface, const char *_authenticationURL, const char *_rackspaceCloudUsername, const char *_apiAccessKey); + + /// \brief Get a list of running servers + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnListServersResult() + void ListServers(void); + + /// \brief Get a list of running servers, with extended details on each server + /// \sa GetServerDetails() + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnListServersWithDetailsResult() + void ListServersWithDetails(void); + + /// \brief Create a server + /// \details Create a server with a given image (harddrive contents) and flavor (hardware configuration) + /// Get the available images with ListImages() + /// Get the available flavors with ListFlavors() + /// It is possible to configure the server in more detail. See the XML schema at http://docs.rackspacecloud.com/servers/api/v1.0 + /// You can execute such a custom command by calling AddOperation() manually. See the implementation of CreateServer for how to do so. + /// The server takes a while to build. Call GetServerDetails() to get the current build status. Server id to pass to GetServerDetails() is returned in the field + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnCreateServerResult() + /// \param[in] name Name of the server. Only alphanumeric characters, periods, and hyphens are valid. Server Name cannot start or end with a period or hyphen. + /// \param[in] imageId Which image (harddrive contents, including OS) to use + /// \param[in] flavorId Which flavor (hardware config) to use, primarily how much memory is available. + void CreateServer(RakNet::RakString name, RakNet::RakString imageId, RakNet::RakString flavorId); + + /// \brief Get details on a particular server + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnGetServerDetailsResult() + /// \param[in] serverId Which server to get details on. You can call ListServers() to get the list of active servers. + void GetServerDetails(RakNet::RakString serverId); + + /// \brief Changes the name or password for a server + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnUpdateServerNameOrPasswordResult() + /// \param[in] serverId Which server to get details on. You can call ListServers() to get the list of active servers. + /// \param[in] newName The new server name. Leave blank to leave unchanged. Only alphanumeric characters, periods, and hyphens are valid. Server Name cannot start or end with a period or hyphen. + /// \param[in] newPassword The new server password. Leave blank to leave unchanged. + void UpdateServerNameOrPassword(RakNet::RakString serverId, RakNet::RakString newName, RakNet::RakString newPassword); + + /// \brief Deletes a server + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnDeleteServerResult() + /// \param[in] serverId Which server to get details on. You can call ListServers() to get the list of active servers. + void DeleteServer(RakNet::RakString serverId); + + /// \brief Lists the IP addresses available to a server + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnListServerAddressesResult() + /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. + void ListServerAddresses(RakNet::RakString serverId); + + /// \brief Shares an IP address with a server + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnShareServerAddressResult() + /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. + /// \param[in] ipAddress Which IP address. You can call ListServerAddresses() to get the list of addresses for the specified server + void ShareServerAddress(RakNet::RakString serverId, RakNet::RakString ipAddress); + + /// \brief Stops sharing an IP address with a server + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnDeleteServerAddressResult() + /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. + /// \param[in] ipAddress Which IP address. You can call ListServerAddresses() to get the list of addresses for the specified server + void DeleteServerAddress(RakNet::RakString serverId, RakNet::RakString ipAddress); + + /// \brief Reboots a server + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnRebootServerResult() + /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. + /// \param[in] rebootType Should be either "HARD" or "SOFT" + void RebootServer(RakNet::RakString serverId, RakNet::RakString rebootType); + + /// \brief Rebuilds a server with a different image (harddrive contents) + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnRebuildServerResult() + /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. + /// \param[in] imageId Which image (harddrive contents, including OS) to use + void RebuildServer(RakNet::RakString serverId, RakNet::RakString imageId); + + /// \brief Changes the hardware configuration of a server. This does not take effect until you call ConfirmResizedServer() + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnResizeServerResult() + /// \sa RevertResizedServer() + /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. + /// \param[in] flavorId Which flavor (hardware config) to use, primarily how much memory is available. + void ResizeServer(RakNet::RakString serverId, RakNet::RakString flavorId); + + /// \brief Confirm a resize for the specified server + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnConfirmResizedServerResult() + /// \sa ResizeServer() + /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. + void ConfirmResizedServer(RakNet::RakString serverId); + + /// \brief Reverts a resize for the specified server + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnRevertResizedServerResult() + /// \sa ResizeServer() + /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. + void RevertResizedServer(RakNet::RakString serverId); + + /// \brief List all flavors (hardware configs, primarily memory) + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnListFlavorsResult() + void ListFlavors(void); + + /// \brief Get extended details about a specific flavor + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnGetFlavorDetailsResult() + /// \sa ListFlavors() + /// \param[in] flavorId Which flavor (hardware config) + void GetFlavorDetails(RakNet::RakString flavorId); + + /// \brief List all images (software configs, including operating systems), which includes images you create yourself + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnListImagesResult() + /// \sa CreateImage() + void ListImages(void); + + /// \brief Images a running server. This essentially copies the harddrive, and lets you start a server with the same harddrive contents later + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnCreateImageResult() + /// \sa ListImages() + /// \param[in] serverId Which server to operate on. You can call ListServers() to get the list of active servers. + /// \param[in] imageName What to call this image + void CreateImage(RakNet::RakString serverId, RakNet::RakString imageName); + + /// \brief Get extended details about a particular image + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnGetImageDetailsResult() + /// \sa ListImages() + /// \param[in] imageId Which image + void GetImageDetails(RakNet::RakString imageId); + + /// \brief Delete a custom image created with CreateImage() + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnDeleteImageResult() + /// \sa ListImages() + /// \param[in] imageId Which image + void DeleteImage(RakNet::RakString imageId); + + /// \brief List IP groups + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnListSharedIPGroupsResult() + void ListSharedIPGroups(void); + + /// \brief List IP groups with extended details + /// \sa http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20110112.pdf + /// \sa RackspaceEventCallback::OnListSharedIPGroupsWithDetailsResult() + void ListSharedIPGroupsWithDetails(void); + + // I don't know what this does + void CreateSharedIPGroup(RakNet::RakString name, RakNet::RakString optionalServerId); + // I don't know what this does + void GetSharedIPGroupDetails(RakNet::RakString groupId); + // I don't know what this does + void DeleteSharedIPGroup(RakNet::RakString groupId); + + /// \brief Adds a callback to the list of callbacks to be called when any of the above functions finish executing + /// The callbacks are called in the order they are added + void AddEventCallback(Rackspace2EventCallback *callback); + /// \brief Removes a callback from the list of callbacks to be called when any of the above functions finish executing + /// The callbacks are called in the order they are added + void RemoveEventCallback(Rackspace2EventCallback *callback); + /// \brief Removes all callbacks + void ClearEventCallbacks(void); + + /// Call this anytime TCPInterface returns a packet + void OnReceive(Packet *packet); + + /// Call this when TCPInterface returns something other than UNASSIGNED_SYSTEM_ADDRESS from HasLostConnection() + void OnClosedConnection(SystemAddress systemAddress); + + /// String representation of each RackspaceEventType + static const char * EventTypeToString(RackspaceEventType eventType); + + /// \brief Mostly for internal use, but you can use it to execute an operation with more complex xml if desired + /// See the Rackspace.cpp on how to use it + void AddOperation(RackspaceOperationType type, RakNet::RakString httpCommand, RakNet::RakString operation, RakNet::RakString xml); + protected: + + DataStructures::List eventCallbacks; + + struct RackspaceOperation + { + RackspaceOperationType type; + // RakNet::RakString stringInfo; + SystemAddress connectionAddress; + bool isPendingAuthentication; + RakNet::RakString incomingStream; + RakNet::RakString httpCommand; + RakNet::RakString operation; + RakNet::RakString xml; + }; + + TCPInterface *tcpInterface; + + // RackspaceOperationType currentOperation; + // DataStructures::Queue nextOperationQueue; + + DataStructures::List operations; + bool HasOperationOfType(RackspaceOperationType t); + unsigned int GetOperationOfTypeIndex(RackspaceOperationType t); + + RakNet::RakString serverManagementURL; + RakNet::RakString serverManagementDomain; + RakNet::RakString serverManagementPath; + RakNet::RakString storageURL; + RakNet::RakString storageDomain; + RakNet::RakString storagePath; + RakNet::RakString cdnManagementURL; + RakNet::RakString cdnManagementDomain; + RakNet::RakString cdnManagementPath; + + RakNet::RakString storageToken; + RakNet::RakString authToken; + RakNet::RakString rackspaceCloudUsername; + RakNet::RakString apiAccessKey; + + bool ExecuteOperation(RackspaceOperation &ro); + void ReadLine(const char *data, const char *stringStart, RakNet::RakString &output); + bool ConnectToServerManagementDomain(RackspaceOperation &ro); + + + }; + +} // namespace RakNet + +#endif // __RACKSPACE_API_H + diff --git a/Source/RakAlloca.h b/Source/RakAlloca.h index 00da34405..ba5f1ed11 100644 --- a/Source/RakAlloca.h +++ b/Source/RakAlloca.h @@ -1,26 +1,29 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#if defined(__FreeBSD__) -#include - - - - -#elif defined ( __APPLE__ ) || defined ( __APPLE_CC__ ) -#include -#include -#elif defined(_WIN32) -#include -#else -#include -// Alloca needed on Ubuntu apparently -#include -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + + +#if defined(__FreeBSD__) +#include + + + + +#elif defined ( __APPLE__ ) || defined ( __APPLE_CC__ ) +#include +#include +#elif defined(_WIN32) +#include +#else +#include +// Alloca needed on Ubuntu apparently +#include +#endif diff --git a/Source/RakAssert.h b/Source/RakAssert.h index 847ec0152..51a6db906 100644 --- a/Source/RakAssert.h +++ b/Source/RakAssert.h @@ -1,12 +1,15 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include -#include "RakNetDefines.h" +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + + +#include +#include "RakNetDefines.h" diff --git a/Source/RakMemoryOverride.h b/Source/RakMemoryOverride.h index ea3e3141a..0fdbc8d0f 100644 --- a/Source/RakMemoryOverride.h +++ b/Source/RakMemoryOverride.h @@ -1,244 +1,242 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief If _USE_RAK_MEMORY_OVERRIDE is defined, memory allocations go through rakMalloc, rakRealloc, and rakFree -/// - - - -#ifndef __RAK_MEMORY_H -#define __RAK_MEMORY_H - -#include "Export.h" -#include "RakNetDefines.h" -#include - - - - - - - -#include "RakAlloca.h" - -// #if _USE_RAK_MEMORY_OVERRIDE==1 -// #if defined(new) -// #pragma push_macro("new") -// #undef new -// #define RMO_NEW_UNDEF -// #endif -// #endif - - -// These pointers are statically and globally defined in RakMemoryOverride.cpp -// Change them to point to your own allocators if you want. -// Use the functions for a DLL, or just reassign the variable if using source -extern RAK_DLL_EXPORT void * (*rakMalloc) (size_t size); -extern RAK_DLL_EXPORT void * (*rakRealloc) (void *p, size_t size); -extern RAK_DLL_EXPORT void (*rakFree) (void *p); -extern RAK_DLL_EXPORT void * (*rakMalloc_Ex) (size_t size, const char *file, unsigned int line); -extern RAK_DLL_EXPORT void * (*rakRealloc_Ex) (void *p, size_t size, const char *file, unsigned int line); -extern RAK_DLL_EXPORT void (*rakFree_Ex) (void *p, const char *file, unsigned int line); -extern RAK_DLL_EXPORT void (*notifyOutOfMemory) (const char *file, const long line); -extern RAK_DLL_EXPORT void * (*dlMallocMMap) (size_t size); -extern RAK_DLL_EXPORT void * (*dlMallocDirectMMap) (size_t size); -extern RAK_DLL_EXPORT int (*dlMallocMUnmap) (void* ptr, size_t size); - -// Change to a user defined allocation function -void RAK_DLL_EXPORT SetMalloc( void* (*userFunction)(size_t size) ); -void RAK_DLL_EXPORT SetRealloc( void* (*userFunction)(void *p, size_t size) ); -void RAK_DLL_EXPORT SetFree( void (*userFunction)(void *p) ); -void RAK_DLL_EXPORT SetMalloc_Ex( void* (*userFunction)(size_t size, const char *file, unsigned int line) ); -void RAK_DLL_EXPORT SetRealloc_Ex( void* (*userFunction)(void *p, size_t size, const char *file, unsigned int line) ); -void RAK_DLL_EXPORT SetFree_Ex( void (*userFunction)(void *p, const char *file, unsigned int line) ); -// Change to a user defined out of memory function -void RAK_DLL_EXPORT SetNotifyOutOfMemory( void (*userFunction)(const char *file, const long line) ); -void RAK_DLL_EXPORT SetDLMallocMMap( void* (*userFunction)(size_t size) ); -void RAK_DLL_EXPORT SetDLMallocDirectMMap( void* (*userFunction)(size_t size) ); -void RAK_DLL_EXPORT SetDLMallocMUnmap( int (*userFunction)(void* ptr, size_t size) ); - -extern RAK_DLL_EXPORT void * (*GetMalloc()) (size_t size); -extern RAK_DLL_EXPORT void * (*GetRealloc()) (void *p, size_t size); -extern RAK_DLL_EXPORT void (*GetFree()) (void *p); -extern RAK_DLL_EXPORT void * (*GetMalloc_Ex()) (size_t size, const char *file, unsigned int line); -extern RAK_DLL_EXPORT void * (*GetRealloc_Ex()) (void *p, size_t size, const char *file, unsigned int line); -extern RAK_DLL_EXPORT void (*GetFree_Ex()) (void *p, const char *file, unsigned int line); -extern RAK_DLL_EXPORT void *(*GetDLMallocMMap())(size_t size); -extern RAK_DLL_EXPORT void *(*GetDLMallocDirectMMap())(size_t size); -extern RAK_DLL_EXPORT int (*GetDLMallocMUnmap())(void* ptr, size_t size); - -namespace RakNet -{ - - template - RAK_DLL_EXPORT Type* OP_NEW(const char *file, unsigned int line) - { -#if _USE_RAK_MEMORY_OVERRIDE==1 - char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line); - Type *t = new (buffer) Type; - return t; -#else - (void) file; - (void) line; - return new Type; -#endif - } - - template - RAK_DLL_EXPORT Type* OP_NEW_1(const char *file, unsigned int line, const P1 &p1) - { -#if _USE_RAK_MEMORY_OVERRIDE==1 - char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line); - Type *t = new (buffer) Type(p1); - return t; -#else - (void) file; - (void) line; - return new Type(p1); -#endif - } - - template - RAK_DLL_EXPORT Type* OP_NEW_2(const char *file, unsigned int line, const P1 &p1, const P2 &p2) - { -#if _USE_RAK_MEMORY_OVERRIDE==1 - char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line); - Type *t = new (buffer) Type(p1, p2); - return t; -#else - (void) file; - (void) line; - return new Type(p1, p2); -#endif - } - - template - RAK_DLL_EXPORT Type* OP_NEW_3(const char *file, unsigned int line, const P1 &p1, const P2 &p2, const P3 &p3) - { -#if _USE_RAK_MEMORY_OVERRIDE==1 - char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line); - Type *t = new (buffer) Type(p1, p2, p3); - return t; -#else - (void) file; - (void) line; - return new Type(p1, p2, p3); -#endif - } - - template - RAK_DLL_EXPORT Type* OP_NEW_4(const char *file, unsigned int line, const P1 &p1, const P2 &p2, const P3 &p3, const P4 &p4) - { -#if _USE_RAK_MEMORY_OVERRIDE==1 - char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line); - Type *t = new (buffer) Type(p1, p2, p3, p4); - return t; -#else - (void) file; - (void) line; - return new Type(p1, p2, p3, p4); -#endif - } - - - template - RAK_DLL_EXPORT Type* OP_NEW_ARRAY(const int count, const char *file, unsigned int line) - { - if (count==0) - return 0; - -#if _USE_RAK_MEMORY_OVERRIDE==1 -// Type *t; - char *buffer = (char *) (GetMalloc_Ex())(sizeof(int)+sizeof(Type)*count, file, line); - ((int*)buffer)[0]=count; - for (int i=0; i - RAK_DLL_EXPORT void OP_DELETE(Type *buff, const char *file, unsigned int line) - { -#if _USE_RAK_MEMORY_OVERRIDE==1 - if (buff==0) return; - buff->~Type(); - (GetFree_Ex())((char*)buff, file, line ); -#else - (void) file; - (void) line; - delete buff; -#endif - - } - - template - RAK_DLL_EXPORT void OP_DELETE_ARRAY(Type *buff, const char *file, unsigned int line) - { -#if _USE_RAK_MEMORY_OVERRIDE==1 - if (buff==0) - return; - - int count = ((int*)((char*)buff-sizeof(int)))[0]; - Type *t; - for (int i=0; i~Type(); - } - (GetFree_Ex())((char*)buff-sizeof(int), file, line ); -#else - (void) file; - (void) line; - delete [] buff; -#endif - - } - - void RAK_DLL_EXPORT * _RakMalloc (size_t size); - void RAK_DLL_EXPORT * _RakRealloc (void *p, size_t size); - void RAK_DLL_EXPORT _RakFree (void *p); - void RAK_DLL_EXPORT * _RakMalloc_Ex (size_t size, const char *file, unsigned int line); - void RAK_DLL_EXPORT * _RakRealloc_Ex (void *p, size_t size, const char *file, unsigned int line); - void RAK_DLL_EXPORT _RakFree_Ex (void *p, const char *file, unsigned int line); - void RAK_DLL_EXPORT * _DLMallocMMap (size_t size); - void RAK_DLL_EXPORT * _DLMallocDirectMMap (size_t size); - int RAK_DLL_EXPORT _DLMallocMUnmap (void *p, size_t size); - -} - -// Call to make RakNet allocate a large block of memory, and do all subsequent allocations in that memory block -// Initial and reallocations will be done through whatever function is pointed to by yourMMapFunction, and yourDirectMMapFunction (default is malloc) -// Allocations will be freed through whatever function is pointed to by yourMUnmapFunction (default free) -void UseRaknetFixedHeap(size_t initialCapacity, - void * (*yourMMapFunction) (size_t size) = RakNet::_DLMallocMMap, - void * (*yourDirectMMapFunction) (size_t size) = RakNet::_DLMallocDirectMMap, - int (*yourMUnmapFunction) (void *p, size_t size) = RakNet::_DLMallocMUnmap); - -// Free memory allocated from UseRaknetFixedHeap -void FreeRakNetFixedHeap(void); - -// #if _USE_RAK_MEMORY_OVERRIDE==1 -// #if defined(RMO_NEW_UNDEF) -// #pragma pop_macro("new") -// #undef RMO_NEW_UNDEF -// #endif -// #endif - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief If _USE_RAK_MEMORY_OVERRIDE is defined, memory allocations go through rakMalloc, rakRealloc, and rakFree +/// + + + +#pragma once + +#include "Export.h" +#include "RakNetDefines.h" +#include + + + + + + + +#include "RakAlloca.h" + +// #if _USE_RAK_MEMORY_OVERRIDE==1 +// #if defined(new) +// #pragma push_macro("new") +// #undef new +// #define RMO_NEW_UNDEF +// #endif +// #endif + + +// These pointers are statically and globally defined in RakMemoryOverride.cpp +// Change them to point to your own allocators if you want. +// Use the functions for a DLL, or just reassign the variable if using source +extern RAK_DLL_EXPORT void * (*rakMalloc) (size_t size); +extern RAK_DLL_EXPORT void * (*rakRealloc) (void *p, size_t size); +extern RAK_DLL_EXPORT void (*rakFree) (void *p); +extern RAK_DLL_EXPORT void * (*rakMalloc_Ex) (size_t size, const char *file, unsigned int line); +extern RAK_DLL_EXPORT void * (*rakRealloc_Ex) (void *p, size_t size, const char *file, unsigned int line); +extern RAK_DLL_EXPORT void (*rakFree_Ex) (void *p, const char *file, unsigned int line); +extern RAK_DLL_EXPORT void (*notifyOutOfMemory) (const char *file, const long line); +extern RAK_DLL_EXPORT void * (*dlMallocMMap) (size_t size); +extern RAK_DLL_EXPORT void * (*dlMallocDirectMMap) (size_t size); +extern RAK_DLL_EXPORT int (*dlMallocMUnmap) (void* ptr, size_t size); + +// Change to a user defined allocation function +void RAK_DLL_EXPORT SetMalloc( void* (*userFunction)(size_t size) ); +void RAK_DLL_EXPORT SetRealloc( void* (*userFunction)(void *p, size_t size) ); +void RAK_DLL_EXPORT SetFree( void (*userFunction)(void *p) ); +void RAK_DLL_EXPORT SetMalloc_Ex( void* (*userFunction)(size_t size, const char *file, unsigned int line) ); +void RAK_DLL_EXPORT SetRealloc_Ex( void* (*userFunction)(void *p, size_t size, const char *file, unsigned int line) ); +void RAK_DLL_EXPORT SetFree_Ex( void (*userFunction)(void *p, const char *file, unsigned int line) ); +// Change to a user defined out of memory function +void RAK_DLL_EXPORT SetNotifyOutOfMemory( void (*userFunction)(const char *file, const long line) ); +void RAK_DLL_EXPORT SetDLMallocMMap( void* (*userFunction)(size_t size) ); +void RAK_DLL_EXPORT SetDLMallocDirectMMap( void* (*userFunction)(size_t size) ); +void RAK_DLL_EXPORT SetDLMallocMUnmap( int (*userFunction)(void* ptr, size_t size) ); + +extern RAK_DLL_EXPORT void * (*GetMalloc()) (size_t size); +extern RAK_DLL_EXPORT void * (*GetRealloc()) (void *p, size_t size); +extern RAK_DLL_EXPORT void (*GetFree()) (void *p); +extern RAK_DLL_EXPORT void * (*GetMalloc_Ex()) (size_t size, const char *file, unsigned int line); +extern RAK_DLL_EXPORT void * (*GetRealloc_Ex()) (void *p, size_t size, const char *file, unsigned int line); +extern RAK_DLL_EXPORT void (*GetFree_Ex()) (void *p, const char *file, unsigned int line); +extern RAK_DLL_EXPORT void *(*GetDLMallocMMap())(size_t size); +extern RAK_DLL_EXPORT void *(*GetDLMallocDirectMMap())(size_t size); +extern RAK_DLL_EXPORT int (*GetDLMallocMUnmap())(void* ptr, size_t size); + +namespace RakNet +{ + + template + RAK_DLL_EXPORT Type* OP_NEW(const char *file, unsigned int line) + { +#if _USE_RAK_MEMORY_OVERRIDE==1 + char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line); + Type *t = new (buffer) Type; + return t; +#else + (void) file; + (void) line; + return new Type; +#endif + } + + template + RAK_DLL_EXPORT Type* OP_NEW_1(const char *file, unsigned int line, const P1 &p1) + { +#if _USE_RAK_MEMORY_OVERRIDE==1 + char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line); + Type *t = new (buffer) Type(p1); + return t; +#else + (void) file; + (void) line; + return new Type(p1); +#endif + } + + template + RAK_DLL_EXPORT Type* OP_NEW_2(const char *file, unsigned int line, const P1 &p1, const P2 &p2) + { +#if _USE_RAK_MEMORY_OVERRIDE==1 + char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line); + Type *t = new (buffer) Type(p1, p2); + return t; +#else + (void) file; + (void) line; + return new Type(p1, p2); +#endif + } + + template + RAK_DLL_EXPORT Type* OP_NEW_3(const char *file, unsigned int line, const P1 &p1, const P2 &p2, const P3 &p3) + { +#if _USE_RAK_MEMORY_OVERRIDE==1 + char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line); + Type *t = new (buffer) Type(p1, p2, p3); + return t; +#else + (void) file; + (void) line; + return new Type(p1, p2, p3); +#endif + } + + template + RAK_DLL_EXPORT Type* OP_NEW_4(const char *file, unsigned int line, const P1 &p1, const P2 &p2, const P3 &p3, const P4 &p4) + { +#if _USE_RAK_MEMORY_OVERRIDE==1 + char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line); + Type *t = new (buffer) Type(p1, p2, p3, p4); + return t; +#else + (void) file; + (void) line; + return new Type(p1, p2, p3, p4); +#endif + } + + + template + RAK_DLL_EXPORT Type* OP_NEW_ARRAY(const int count, const char *file, unsigned int line) + { + if (count==0) + return 0; + +#if _USE_RAK_MEMORY_OVERRIDE==1 +// Type *t; + char *buffer = (char *) (GetMalloc_Ex())(sizeof(int)+sizeof(Type)*count, file, line); + ((int*)buffer)[0]=count; + for (int i=0; i + RAK_DLL_EXPORT void OP_DELETE(Type *buff, const char *file, unsigned int line) + { +#if _USE_RAK_MEMORY_OVERRIDE==1 + if (buff==0) return; + buff->~Type(); + (GetFree_Ex())((char*)buff, file, line ); +#else + (void) file; + (void) line; + delete buff; +#endif + + } + + template + RAK_DLL_EXPORT void OP_DELETE_ARRAY(Type *buff, const char *file, unsigned int line) + { +#if _USE_RAK_MEMORY_OVERRIDE==1 + if (buff==0) + return; + + int count = ((int*)((char*)buff-sizeof(int)))[0]; + Type *t; + for (int i=0; i~Type(); + } + (GetFree_Ex())((char*)buff-sizeof(int), file, line ); +#else + (void) file; + (void) line; + delete [] buff; +#endif + + } + + void RAK_DLL_EXPORT * _RakMalloc (size_t size); + void RAK_DLL_EXPORT * _RakRealloc (void *p, size_t size); + void RAK_DLL_EXPORT _RakFree (void *p); + void RAK_DLL_EXPORT * _RakMalloc_Ex (size_t size, const char *file, unsigned int line); + void RAK_DLL_EXPORT * _RakRealloc_Ex (void *p, size_t size, const char *file, unsigned int line); + void RAK_DLL_EXPORT _RakFree_Ex (void *p, const char *file, unsigned int line); + void RAK_DLL_EXPORT * _DLMallocMMap (size_t size); + void RAK_DLL_EXPORT * _DLMallocDirectMMap (size_t size); + int RAK_DLL_EXPORT _DLMallocMUnmap (void *p, size_t size); + +} + +// Call to make RakNet allocate a large block of memory, and do all subsequent allocations in that memory block +// Initial and reallocations will be done through whatever function is pointed to by yourMMapFunction, and yourDirectMMapFunction (default is malloc) +// Allocations will be freed through whatever function is pointed to by yourMUnmapFunction (default free) +void UseRaknetFixedHeap(size_t initialCapacity, + void * (*yourMMapFunction) (size_t size) = RakNet::_DLMallocMMap, + void * (*yourDirectMMapFunction) (size_t size) = RakNet::_DLMallocDirectMMap, + int (*yourMUnmapFunction) (void *p, size_t size) = RakNet::_DLMallocMUnmap); + +// Free memory allocated from UseRaknetFixedHeap +void FreeRakNetFixedHeap(void); + +// #if _USE_RAK_MEMORY_OVERRIDE==1 +// #if defined(RMO_NEW_UNDEF) +// #pragma pop_macro("new") +// #undef RMO_NEW_UNDEF +// #endif +// #endif + diff --git a/Source/RakNetCommandParser.h b/Source/RakNetCommandParser.h index af627616e..0bfaa9411 100644 --- a/Source/RakNetCommandParser.h +++ b/Source/RakNetCommandParser.h @@ -1,70 +1,68 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Contains RakNetCommandParser , used to send commands to an instance of RakPeer -/// - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_RakNetCommandParser==1 - -#ifndef __RAKNET_COMMAND_PARSER -#define __RAKNET_COMMAND_PARSER - -#include "CommandParserInterface.h" -#include "Export.h" - -namespace RakNet -{ -class RakPeerInterface; - -/// \brief This allows a console client to call most of the functions in RakPeer -class RAK_DLL_EXPORT RakNetCommandParser : public CommandParserInterface -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(RakNetCommandParser) - - RakNetCommandParser(); - ~RakNetCommandParser(); - - /// Given \a command with parameters \a parameterList , do whatever processing you wish. - /// \param[in] command The command to process - /// \param[in] numParameters How many parameters were passed along with the command - /// \param[in] parameterList The list of parameters. parameterList[0] is the first parameter and so on. - /// \param[in] transport The transport interface we can use to write to - /// \param[in] systemAddress The player that sent this command. - /// \param[in] originalString The string that was actually sent over the network, in case you want to do your own parsing - bool OnCommand(const char *command, unsigned numParameters, char **parameterList, TransportInterface *transport, const SystemAddress &systemAddress, const char *originalString); - - /// You are responsible for overriding this function and returning a static string, which will identifier your parser. - /// This should return a static string - /// \return The name that you return. - const char *GetName(void) const; - - /// A callback for when you are expected to send a brief description of your parser to \a systemAddress - /// \param[in] transport The transport interface we can use to write to - /// \param[in] systemAddress The player that requested help. - void SendHelp(TransportInterface *transport, const SystemAddress &systemAddress); - - /// Records the instance of RakPeer to perform the desired commands on - /// \param[in] rakPeer The RakPeer instance, or a derived class (e.g. RakPeer or RakPeer) - void SetRakPeerInterface(RakNet::RakPeerInterface *rakPeer); -protected: - - /// Which instance of RakPeer we are working on. Set from SetRakPeerInterface() - RakPeerInterface *peer; -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Contains RakNetCommandParser , used to send commands to an instance of RakPeer +/// + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_RakNetCommandParser==1 + +#pragma once + +#include "CommandParserInterface.h" +#include "Export.h" + +namespace RakNet +{ +class RakPeerInterface; + +/// \brief This allows a console client to call most of the functions in RakPeer +class RAK_DLL_EXPORT RakNetCommandParser : public CommandParserInterface +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(RakNetCommandParser) + + RakNetCommandParser(); + ~RakNetCommandParser(); + + /// Given \a command with parameters \a parameterList , do whatever processing you wish. + /// \param[in] command The command to process + /// \param[in] numParameters How many parameters were passed along with the command + /// \param[in] parameterList The list of parameters. parameterList[0] is the first parameter and so on. + /// \param[in] transport The transport interface we can use to write to + /// \param[in] systemAddress The player that sent this command. + /// \param[in] originalString The string that was actually sent over the network, in case you want to do your own parsing + bool OnCommand(const char *command, unsigned numParameters, char **parameterList, TransportInterface *transport, const SystemAddress &systemAddress, const char *originalString); + + /// You are responsible for overriding this function and returning a static string, which will identifier your parser. + /// This should return a static string + /// \return The name that you return. + const char *GetName(void) const; + + /// A callback for when you are expected to send a brief description of your parser to \a systemAddress + /// \param[in] transport The transport interface we can use to write to + /// \param[in] systemAddress The player that requested help. + void SendHelp(TransportInterface *transport, const SystemAddress &systemAddress); + + /// Records the instance of RakPeer to perform the desired commands on + /// \param[in] rakPeer The RakPeer instance, or a derived class (e.g. RakPeer or RakPeer) + void SetRakPeerInterface(RakNet::RakPeerInterface *rakPeer); +protected: + + /// Which instance of RakPeer we are working on. Set from SetRakPeerInterface() + RakPeerInterface *peer; +}; + +} // namespace RakNet + +#endif + diff --git a/Source/RakNetDefines.h b/Source/RakNetDefines.h index ea313c551..20dc39b99 100644 --- a/Source/RakNetDefines.h +++ b/Source/RakNetDefines.h @@ -1,195 +1,193 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __RAKNET_DEFINES_H -#define __RAKNET_DEFINES_H - -// If you want to change these defines, put them in RakNetDefinesOverrides so your changes are not lost when updating RakNet -// The user should not edit this file -#include "RakNetDefinesOverrides.h" - -/// Define __GET_TIME_64BIT to have RakNet::TimeMS use a 64, rather than 32 bit value. A 32 bit value will overflow after about 5 weeks. -/// However, this doubles the bandwidth use for sending times, so don't do it unless you have a reason to. -/// Comment out if you are using the iPod Touch TG. See http://www.jenkinssoftware.com/forum/index.php?topic=2717.0 -/// This must be the same on all systems, or they won't connect -#ifndef __GET_TIME_64BIT -#define __GET_TIME_64BIT 1 -#endif - -// Define _FILE_AND_LINE_ to "",0 if you want to strip out file and line info for memory tracking from the EXE -#ifndef _FILE_AND_LINE_ -#define _FILE_AND_LINE_ __FILE__,__LINE__ -#endif - -/// Define __BITSTREAM_NATIVE_END to NOT support endian swapping in the BitStream class. This is faster and is what you should use -/// unless you actually plan to have different endianness systems connect to each other -/// Enabled by default. -// #define __BITSTREAM_NATIVE_END - -/// Maximum (stack) size to use with _alloca before using new and delete instead. -#ifndef MAX_ALLOCA_STACK_ALLOCATION -#define MAX_ALLOCA_STACK_ALLOCATION 1048576 -#endif - -// Use WaitForSingleObject instead of sleep. -// Defining it plays nicer with other systems, and uses less CPU, but gives worse RakNet performance -// Undefining it uses more CPU time, but is more responsive and faster. -#ifndef _WIN32_WCE -#define USE_WAIT_FOR_MULTIPLE_EVENTS -#endif - -/// Uncomment to use RakMemoryOverride for custom memory tracking -/// See RakMemoryOverride.h. -#ifndef _USE_RAK_MEMORY_OVERRIDE -#define _USE_RAK_MEMORY_OVERRIDE 0 -#endif - -/// If defined, OpenSSL is enabled for the class TCPInterface -/// This is necessary to use the SendEmail class with Google POP servers -/// Note that OpenSSL carries its own license restrictions that you should be aware of. If you don't agree, don't enable this define -/// This also requires that you enable header search paths to DependentExtensions\openssl-1.0.0d -// #define OPEN_SSL_CLIENT_SUPPORT -#ifndef OPEN_SSL_CLIENT_SUPPORT -#define OPEN_SSL_CLIENT_SUPPORT 0 -#endif - -/// Threshold at which to do a malloc / free rather than pushing data onto a fixed stack for the bitstream class -/// Arbitrary size, just picking something likely to be larger than most packets -#ifndef BITSTREAM_STACK_ALLOCATION_SIZE -#define BITSTREAM_STACK_ALLOCATION_SIZE 256 -#endif - -// Redefine if you want to disable or change the target for debug RAKNET_DEBUG_PRINTF -#ifndef RAKNET_DEBUG_PRINTF -#define RAKNET_DEBUG_PRINTF printf -#endif - -// Maximum number of local IP addresses supported -#ifndef MAXIMUM_NUMBER_OF_INTERNAL_IDS -#define MAXIMUM_NUMBER_OF_INTERNAL_IDS 10 -#endif - -#ifndef RakAssert - - - -#if defined(__native_client__) -#define RakAssert(x) -#else -#if defined(_DEBUG) -#define RakAssert(x) assert(x); -#else -#define RakAssert(x) -#endif -#endif -#endif - -/// This controls the amount of memory used per connection. -/// This many datagrams are tracked by datagramNumber. If more than this many datagrams are sent, then an ack for an older datagram would be ignored -/// This results in an unnecessary resend in that case -#ifndef DATAGRAM_MESSAGE_ID_ARRAY_LENGTH -#define DATAGRAM_MESSAGE_ID_ARRAY_LENGTH 512 -#endif - -/// This is the maximum number of reliable user messages that can be on the wire at a time -/// If this is too low, then high ping connections with a large throughput will be underutilized -/// This will be evident because RakNetStatistics::messagesInSend buffer will increase over time, yet at the same time the outgoing bandwidth per second is less than your connection supports -#ifndef RESEND_BUFFER_ARRAY_LENGTH -#define RESEND_BUFFER_ARRAY_LENGTH 512 -#define RESEND_BUFFER_ARRAY_MASK 511 -#endif - -/// Uncomment if you want to link in the DLMalloc library to use with RakMemoryOverride -// #define _LINK_DL_MALLOC - -#ifndef GET_TIME_SPIKE_LIMIT -/// Workaround for http://support.microsoft.com/kb/274323 -/// If two calls between RakNet::GetTime() happen farther apart than this time in microseconds, this delta will be returned instead -/// Note: This will cause ID_TIMESTAMP to be temporarily inaccurate if you set a breakpoint that pauses the UpdateNetworkLoop() thread in RakPeer -/// Define in RakNetDefinesOverrides.h to enable (non-zero) or disable (0) -#define GET_TIME_SPIKE_LIMIT 0 -#endif - -// Use sliding window congestion control instead of ping based congestion control -#ifndef USE_SLIDING_WINDOW_CONGESTION_CONTROL -#define USE_SLIDING_WINDOW_CONGESTION_CONTROL 1 -#endif - -// When a large message is arriving, preallocate the memory for the entire block -// This results in large messages not taking up time to reassembly with memcpy, but is vulnerable to attackers causing the host to run out of memory -#ifndef PREALLOCATE_LARGE_MESSAGES -#define PREALLOCATE_LARGE_MESSAGES 0 -#endif - -#ifndef RAKNET_SUPPORT_IPV6 -#define RAKNET_SUPPORT_IPV6 0 -#endif - - - - - - - - - - - -#ifndef RAKSTRING_TYPE -#if defined(_UNICODE) -#define RAKSTRING_TYPE RakWString -#define RAKSTRING_TYPE_IS_UNICODE 1 -#else -#define RAKSTRING_TYPE RakString -#define RAKSTRING_TYPE_IS_UNICODE 0 -#endif -#endif - -#ifndef RPC4_GLOBAL_REGISTRATION_MAX_FUNCTIONS -#define RPC4_GLOBAL_REGISTRATION_MAX_FUNCTIONS 48 -#endif - -#ifndef RPC4_GLOBAL_REGISTRATION_MAX_FUNCTION_NAME_LENGTH -#define RPC4_GLOBAL_REGISTRATION_MAX_FUNCTION_NAME_LENGTH 48 -#endif - -#ifndef XBOX_BYPASS_SECURITY -#define XBOX_BYPASS_SECURITY 1 -#endif - -// Controls how many allocations occur at once for the memory pool of incoming datagrams waiting to be transferred between the recvfrom thread and the main update thread -// Has large effect on memory usage, per instance of RakPeer. Approximately MAXIMUM_MTU_SIZE*BUFFERED_PACKETS_PAGE_SIZE bytes, once after calling RakPeer::Startup() -#ifndef BUFFERED_PACKETS_PAGE_SIZE -#define BUFFERED_PACKETS_PAGE_SIZE 8 -#endif - -// Controls how many allocations occur at once for the memory pool of incoming or outgoing datagrams. -// Has small effect on memory usage per connection. Uses about 256 bytes*INTERNAL_PACKET_PAGE_SIZE per connection -#ifndef INTERNAL_PACKET_PAGE_SIZE -#define INTERNAL_PACKET_PAGE_SIZE 8 -#endif - -// If defined to 1, the user is responsible for calling RakPeer::RunUpdateCycle and RakPeer::RunRecvfrom -#ifndef RAKPEER_USER_THREADED -#define RAKPEER_USER_THREADED 0 -#endif - -#ifndef USE_ALLOCA -#define USE_ALLOCA 1 -#endif - - - - - - -//#define USE_THREADED_SEND - -#endif // __RAKNET_DEFINES_H +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +// If you want to change these defines, put them in RakNetDefinesOverrides so your changes are not lost when updating RakNet +// The user should not edit this file +#include "RakNetDefinesOverrides.h" + +/// Define __GET_TIME_64BIT to have RakNet::TimeMS use a 64, rather than 32 bit value. A 32 bit value will overflow after about 5 weeks. +/// However, this doubles the bandwidth use for sending times, so don't do it unless you have a reason to. +/// Comment out if you are using the iPod Touch TG. See http://www.jenkinssoftware.com/forum/index.php?topic=2717.0 +/// This must be the same on all systems, or they won't connect +#ifndef __GET_TIME_64BIT +#define __GET_TIME_64BIT 1 +#endif + +// Define _FILE_AND_LINE_ to "",0 if you want to strip out file and line info for memory tracking from the EXE +#ifndef _FILE_AND_LINE_ +#define _FILE_AND_LINE_ __FILE__,__LINE__ +#endif + +/// Define __BITSTREAM_NATIVE_END to NOT support endian swapping in the BitStream class. This is faster and is what you should use +/// unless you actually plan to have different endianness systems connect to each other +/// Enabled by default. +// #define __BITSTREAM_NATIVE_END + +/// Maximum (stack) size to use with _alloca before using new and delete instead. +#ifndef MAX_ALLOCA_STACK_ALLOCATION +#define MAX_ALLOCA_STACK_ALLOCATION 1048576 +#endif + +// Use WaitForSingleObject instead of sleep. +// Defining it plays nicer with other systems, and uses less CPU, but gives worse RakNet performance +// Undefining it uses more CPU time, but is more responsive and faster. +#ifndef _WIN32_WCE +#define USE_WAIT_FOR_MULTIPLE_EVENTS +#endif + +/// Uncomment to use RakMemoryOverride for custom memory tracking +/// See RakMemoryOverride.h. +#ifndef _USE_RAK_MEMORY_OVERRIDE +#define _USE_RAK_MEMORY_OVERRIDE 0 +#endif + +/// If defined, OpenSSL is enabled for the class TCPInterface +/// This is necessary to use the SendEmail class with Google POP servers +/// Note that OpenSSL carries its own license restrictions that you should be aware of. If you don't agree, don't enable this define +/// This also requires that you enable header search paths to DependentExtensions\openssl-1.0.0d +// #define OPEN_SSL_CLIENT_SUPPORT +#ifndef OPEN_SSL_CLIENT_SUPPORT +#define OPEN_SSL_CLIENT_SUPPORT 0 +#endif + +/// Threshold at which to do a malloc / free rather than pushing data onto a fixed stack for the bitstream class +/// Arbitrary size, just picking something likely to be larger than most packets +#ifndef BITSTREAM_STACK_ALLOCATION_SIZE +#define BITSTREAM_STACK_ALLOCATION_SIZE 256 +#endif + +// Redefine if you want to disable or change the target for debug RAKNET_DEBUG_PRINTF +#ifndef RAKNET_DEBUG_PRINTF +#define RAKNET_DEBUG_PRINTF printf +#endif + +// Maximum number of local IP addresses supported +#ifndef MAXIMUM_NUMBER_OF_INTERNAL_IDS +#define MAXIMUM_NUMBER_OF_INTERNAL_IDS 10 +#endif + +#ifndef RakAssert + + + +#if defined(__native_client__) +#define RakAssert(x) +#else +#if defined(_DEBUG) +#define RakAssert(x) assert(x); +#else +#define RakAssert(x) +#endif +#endif +#endif + +/// This controls the amount of memory used per connection. +/// This many datagrams are tracked by datagramNumber. If more than this many datagrams are sent, then an ack for an older datagram would be ignored +/// This results in an unnecessary resend in that case +#ifndef DATAGRAM_MESSAGE_ID_ARRAY_LENGTH +#define DATAGRAM_MESSAGE_ID_ARRAY_LENGTH 512 +#endif + +/// This is the maximum number of reliable user messages that can be on the wire at a time +/// If this is too low, then high ping connections with a large throughput will be underutilized +/// This will be evident because RakNetStatistics::messagesInSend buffer will increase over time, yet at the same time the outgoing bandwidth per second is less than your connection supports +#ifndef RESEND_BUFFER_ARRAY_LENGTH +#define RESEND_BUFFER_ARRAY_LENGTH 512 +#define RESEND_BUFFER_ARRAY_MASK 511 +#endif + +/// Uncomment if you want to link in the DLMalloc library to use with RakMemoryOverride +// #define _LINK_DL_MALLOC + +#ifndef GET_TIME_SPIKE_LIMIT +/// Workaround for http://support.microsoft.com/kb/274323 +/// If two calls between RakNet::GetTime() happen farther apart than this time in microseconds, this delta will be returned instead +/// Note: This will cause ID_TIMESTAMP to be temporarily inaccurate if you set a breakpoint that pauses the UpdateNetworkLoop() thread in RakPeer +/// Define in RakNetDefinesOverrides.h to enable (non-zero) or disable (0) +#define GET_TIME_SPIKE_LIMIT 0 +#endif + +// Use sliding window congestion control instead of ping based congestion control +#ifndef USE_SLIDING_WINDOW_CONGESTION_CONTROL +#define USE_SLIDING_WINDOW_CONGESTION_CONTROL 1 +#endif + +// When a large message is arriving, preallocate the memory for the entire block +// This results in large messages not taking up time to reassembly with memcpy, but is vulnerable to attackers causing the host to run out of memory +#ifndef PREALLOCATE_LARGE_MESSAGES +#define PREALLOCATE_LARGE_MESSAGES 0 +#endif + +#ifndef RAKNET_SUPPORT_IPV6 +#define RAKNET_SUPPORT_IPV6 0 +#endif + + + + + + + + + + + +#ifndef RAKSTRING_TYPE +#if defined(_UNICODE) +#define RAKSTRING_TYPE RakWString +#define RAKSTRING_TYPE_IS_UNICODE 1 +#else +#define RAKSTRING_TYPE RakString +#define RAKSTRING_TYPE_IS_UNICODE 0 +#endif +#endif + +#ifndef RPC4_GLOBAL_REGISTRATION_MAX_FUNCTIONS +#define RPC4_GLOBAL_REGISTRATION_MAX_FUNCTIONS 48 +#endif + +#ifndef RPC4_GLOBAL_REGISTRATION_MAX_FUNCTION_NAME_LENGTH +#define RPC4_GLOBAL_REGISTRATION_MAX_FUNCTION_NAME_LENGTH 48 +#endif + +#ifndef XBOX_BYPASS_SECURITY +#define XBOX_BYPASS_SECURITY 1 +#endif + +// Controls how many allocations occur at once for the memory pool of incoming datagrams waiting to be transferred between the recvfrom thread and the main update thread +// Has large effect on memory usage, per instance of RakPeer. Approximately MAXIMUM_MTU_SIZE*BUFFERED_PACKETS_PAGE_SIZE bytes, once after calling RakPeer::Startup() +#ifndef BUFFERED_PACKETS_PAGE_SIZE +#define BUFFERED_PACKETS_PAGE_SIZE 8 +#endif + +// Controls how many allocations occur at once for the memory pool of incoming or outgoing datagrams. +// Has small effect on memory usage per connection. Uses about 256 bytes*INTERNAL_PACKET_PAGE_SIZE per connection +#ifndef INTERNAL_PACKET_PAGE_SIZE +#define INTERNAL_PACKET_PAGE_SIZE 8 +#endif + +// If defined to 1, the user is responsible for calling RakPeer::RunUpdateCycle and RakPeer::RunRecvfrom +#ifndef RAKPEER_USER_THREADED +#define RAKPEER_USER_THREADED 0 +#endif + +#ifndef USE_ALLOCA +#define USE_ALLOCA 1 +#endif + + + + + + +//#define USE_THREADED_SEND + diff --git a/Source/RakNetDefinesOverrides.h b/Source/RakNetDefinesOverrides.h index 2c9596347..b4ef73356 100644 --- a/Source/RakNetDefinesOverrides.h +++ b/Source/RakNetDefinesOverrides.h @@ -1,12 +1,15 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -// USER EDITABLE FILE - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + + +// USER EDITABLE FILE + diff --git a/Source/RakNetSmartPtr.h b/Source/RakNetSmartPtr.h index 09a2f851e..09c6b02d6 100644 --- a/Source/RakNetSmartPtr.h +++ b/Source/RakNetSmartPtr.h @@ -1,183 +1,181 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __RAKNET_SMART_PTR_H -#define __RAKNET_SMART_PTR_H - -// From http://www.codeproject.com/KB/cpp/SmartPointers.aspx -// with bugs fixed - -#include "RakMemoryOverride.h" -#include "Export.h" - -//static int allocCount=0; -//static int deallocCount=0; - -namespace RakNet -{ - -class RAK_DLL_EXPORT ReferenceCounter -{ -private: - int refCount; - -public: - ReferenceCounter() {refCount=0;} - ~ReferenceCounter() {} - void AddRef() {refCount++;} - int Release() {return --refCount;} - int GetRefCount(void) const {return refCount;} -}; - -template < typename T > class RAK_DLL_EXPORT RakNetSmartPtr -{ -private: - T* ptr; // pointer - ReferenceCounter* reference; // Reference refCount - -public: - RakNetSmartPtr() : ptr(0), reference(0) - { - // Do not allocate by default, wasteful if we just have a list of preallocated and unassigend smart pointers - } - - RakNetSmartPtr(T* pValue) : ptr(pValue) - { - reference = RakNet::OP_NEW(_FILE_AND_LINE_); - reference->AddRef(); - -// allocCount+=2; -// printf("allocCount=%i deallocCount=%i Line=%i\n",allocCount, deallocCount, __LINE__); - } - - RakNetSmartPtr(const RakNetSmartPtr& sp) : ptr(sp.ptr), reference(sp.reference) - { - if (reference) - reference->AddRef(); - } - - ~RakNetSmartPtr() - { - if(reference && reference->Release() == 0) - { - RakNet::OP_DELETE(ptr, _FILE_AND_LINE_); - RakNet::OP_DELETE(reference, _FILE_AND_LINE_); - -// deallocCount+=2; -// printf("allocCount=%i deallocCount=%i Line=%i\n",allocCount, deallocCount, __LINE__); - } - } - - bool IsNull(void) const - { - return ptr==0; - } - - void SetNull(void) - { - if(reference && reference->Release() == 0) - { - RakNet::OP_DELETE(ptr, _FILE_AND_LINE_); - RakNet::OP_DELETE(reference, _FILE_AND_LINE_); - -// deallocCount+=2; -// printf("allocCount=%i deallocCount=%i Line=%i\n",allocCount, deallocCount, __LINE__); - } - ptr=0; - reference=0; - } - - bool IsUnique(void) const - { - return reference->GetRefCount()==1; - } - - // Allow you to change the values of the internal contents of the pointer, without changing what is pointed to by other instances of the smart pointer - void Clone(bool copyContents) - { - if (IsUnique()==false) - { - reference->Release(); - - reference = RakNet::OP_NEW(_FILE_AND_LINE_); - reference->AddRef(); - T* oldPtr=ptr; - ptr=RakNet::OP_NEW(_FILE_AND_LINE_); - if (copyContents) - *ptr=*oldPtr; - } - } - - int GetRefCount(void) const - { - return reference->GetRefCount(); - } - - T& operator* () - { - return *ptr; - } - - const T& operator* () const - { - return *ptr; - } - - T* operator-> () - { - return ptr; - } - - const T* operator-> () const - { - return ptr; - } - - bool operator == (const RakNetSmartPtr& sp) - { - return ptr == sp.ptr; - } - bool operator<( const RakNetSmartPtr &right ) {return ptr < right.ptr;} - bool operator>( const RakNetSmartPtr &right ) {return ptr > right.ptr;} - - bool operator != (const RakNetSmartPtr& sp) - { - return ptr != sp.ptr; - } - - RakNetSmartPtr& operator = (const RakNetSmartPtr& sp) - { - // Assignment operator - - if (this != &sp) // Avoid self assignment - { - if(reference && reference->Release() == 0) - { - RakNet::OP_DELETE(ptr, _FILE_AND_LINE_); - RakNet::OP_DELETE(reference, _FILE_AND_LINE_); - -// deallocCount+=2; -// printf("allocCount=%i deallocCount=%i Line=%i\n",allocCount, deallocCount, __LINE__); - } - - ptr = sp.ptr; - reference = sp.reference; - if (reference) - reference->AddRef(); - } - return *this; - } - - -}; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +// From http://www.codeproject.com/KB/cpp/SmartPointers.aspx +// with bugs fixed + +#include "RakMemoryOverride.h" +#include "Export.h" + +//static int allocCount=0; +//static int deallocCount=0; + +namespace RakNet +{ + +class RAK_DLL_EXPORT ReferenceCounter +{ +private: + int refCount; + +public: + ReferenceCounter() {refCount=0;} + ~ReferenceCounter() {} + void AddRef() {refCount++;} + int Release() {return --refCount;} + int GetRefCount(void) const {return refCount;} +}; + +template < typename T > class RAK_DLL_EXPORT RakNetSmartPtr +{ +private: + T* ptr; // pointer + ReferenceCounter* reference; // Reference refCount + +public: + RakNetSmartPtr() : ptr(0), reference(0) + { + // Do not allocate by default, wasteful if we just have a list of preallocated and unassigend smart pointers + } + + RakNetSmartPtr(T* pValue) : ptr(pValue) + { + reference = RakNet::OP_NEW(_FILE_AND_LINE_); + reference->AddRef(); + +// allocCount+=2; +// printf("allocCount=%i deallocCount=%i Line=%i\n",allocCount, deallocCount, __LINE__); + } + + RakNetSmartPtr(const RakNetSmartPtr& sp) : ptr(sp.ptr), reference(sp.reference) + { + if (reference) + reference->AddRef(); + } + + ~RakNetSmartPtr() + { + if(reference && reference->Release() == 0) + { + RakNet::OP_DELETE(ptr, _FILE_AND_LINE_); + RakNet::OP_DELETE(reference, _FILE_AND_LINE_); + +// deallocCount+=2; +// printf("allocCount=%i deallocCount=%i Line=%i\n",allocCount, deallocCount, __LINE__); + } + } + + bool IsNull(void) const + { + return ptr==0; + } + + void SetNull(void) + { + if(reference && reference->Release() == 0) + { + RakNet::OP_DELETE(ptr, _FILE_AND_LINE_); + RakNet::OP_DELETE(reference, _FILE_AND_LINE_); + +// deallocCount+=2; +// printf("allocCount=%i deallocCount=%i Line=%i\n",allocCount, deallocCount, __LINE__); + } + ptr=0; + reference=0; + } + + bool IsUnique(void) const + { + return reference->GetRefCount()==1; + } + + // Allow you to change the values of the internal contents of the pointer, without changing what is pointed to by other instances of the smart pointer + void Clone(bool copyContents) + { + if (IsUnique()==false) + { + reference->Release(); + + reference = RakNet::OP_NEW(_FILE_AND_LINE_); + reference->AddRef(); + T* oldPtr=ptr; + ptr=RakNet::OP_NEW(_FILE_AND_LINE_); + if (copyContents) + *ptr=*oldPtr; + } + } + + int GetRefCount(void) const + { + return reference->GetRefCount(); + } + + T& operator* () + { + return *ptr; + } + + const T& operator* () const + { + return *ptr; + } + + T* operator-> () + { + return ptr; + } + + const T* operator-> () const + { + return ptr; + } + + bool operator == (const RakNetSmartPtr& sp) + { + return ptr == sp.ptr; + } + bool operator<( const RakNetSmartPtr &right ) {return ptr < right.ptr;} + bool operator>( const RakNetSmartPtr &right ) {return ptr > right.ptr;} + + bool operator != (const RakNetSmartPtr& sp) + { + return ptr != sp.ptr; + } + + RakNetSmartPtr& operator = (const RakNetSmartPtr& sp) + { + // Assignment operator + + if (this != &sp) // Avoid self assignment + { + if(reference && reference->Release() == 0) + { + RakNet::OP_DELETE(ptr, _FILE_AND_LINE_); + RakNet::OP_DELETE(reference, _FILE_AND_LINE_); + +// deallocCount+=2; +// printf("allocCount=%i deallocCount=%i Line=%i\n",allocCount, deallocCount, __LINE__); + } + + ptr = sp.ptr; + reference = sp.reference; + if (reference) + reference->AddRef(); + } + return *this; + } + + +}; + +} // namespace RakNet + diff --git a/Source/RakNetSocket.h b/Source/RakNetSocket.h index 3a4c5de03..c0c978a01 100644 --- a/Source/RakNetSocket.h +++ b/Source/RakNetSocket.h @@ -1,194 +1,192 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/* -#ifndef __RAKNET_SOCKET_H -#define __RAKNET_SOCKET_H - -#include "RakNetTypes.h" -#include "RakNetDefines.h" -#include "Export.h" -#include "SocketIncludes.h" -#include "RakAssert.h" -#include "SocketDefines.h" -#include "MTUSize.h" - -namespace RakNet -{ - -struct RAK_DLL_EXPORT RakNetSocket -{ -public: - RakNetSocket(); - ~RakNetSocket(); - -// void Accept( -// struct sockaddr *addr, -// int *addrlen); - - inline int Connect( - const struct sockaddr *name, - int namelen) {return connect__(s,name,namelen);} - - static RakNetSocket* Create -#ifdef __native_client__ - (_PP_Instance_ _chromeInstance); -#else - (int af, - int type, - int protocol); -#endif - - int Bind( - const struct sockaddr *addr, - int namelen); - - inline int GetSockName( - struct sockaddr *name, - socklen_t * namelen) {return getsockname__(s,name,namelen);} - - inline int GetSockOpt ( - int level, - int optname, - char * optval, - socklen_t *optlen) {return getsockopt__(s,level,optname,optval,optlen);} - - - int IOCTLSocket( - long cmd, - unsigned long *argp); - - int Listen ( - int backlog); - - inline int Recv( - char * buf, - int len, - int flags) {return recv__(s,buf,len,flags);} - - inline int RecvFrom( - char * buf, - int len, - int flags, - struct sockaddr * from, - socklen_t * fromlen) {return recvfrom__(s,buf,len,flags,from,fromlen);} - -// inline int Select( -// int nfds, -// fd_set *readfds, -// fd_set *writefds, -// fd_set *exceptfds, -// struct timeval *timeout) {return select__(nfds,readfds,writefds,exceptfds,timeout);} - - inline int Send( - const char * buf, - int len, - int flags) {return send__(s,buf,len,flags);} - - inline int SendTo( - const char * buf, - int len, - int flags, - const struct sockaddr *to, - int tolen) {return sendto__(s,buf,len,flags,to,tolen);} - - #ifdef _WIN32 - #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3) || defined(_PS4) || defined(SN_TARGET_PSP2) - #else - inline int Fcntl(int cmd, int arg) {return fcntl(s,cmd,arg);} - #endif - - -#if defined(_WIN32) && !defined(WINDOWS_STORE_RT) - inline int _WSASendTo( - LPWSABUF lpBuffers, - DWORD dwBufferCount, - LPDWORD lpNumberOfBytesSent, - DWORD dwFlags, - const struct sockaddr FAR * lpTo, - int iTolen, - LPWSAOVERLAPPED lpOverlapped, - LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine - ) - { return WSASendTo(s,lpBuffers,dwBufferCount,lpNumberOfBytesSent,dwFlags,lpTo,iTolen,lpOverlapped,lpCompletionRoutine);} - -#endif - - int SetSockOpt( - int level, - int optname, - const char * optval, - int optlen); - - int Shutdown( - int how); - - - inline void SetRemotePortRakNetWasStartedOn(unsigned short i) {remotePortRakNetWasStartedOn_PS3_PSP2=i;} - inline void SetUserConnectionSocketIndex(unsigned int i) {userConnectionSocketIndex=i;} - inline void SetBoundAddress(SystemAddress i) {boundAddress=i;} - inline void SetSocketFamily(unsigned short i) {socketFamily=i;} - inline void SetBlockingSocket(bool i) {blockingSocket=i;} - inline void SetExtraSocketOptions(unsigned int i) {extraSocketOptions=i;} - inline void SetChromeInstance(_PP_Instance_ i) {chromeInstance=i;} - inline void SetBoundAddressToLoopback(unsigned char ipVersion) {boundAddress.SetToLoopback(ipVersion);} - - inline SystemAddress GetBoundAddress(void) const {return boundAddress;} - inline unsigned short GetRemotePortRakNetWasStartedOn(void) const {return remotePortRakNetWasStartedOn_PS3_PSP2;} - inline bool GetBlockingSocket(void) {return blockingSocket;} - inline unsigned int GetExtraSocketOptions(void) const {return extraSocketOptions;} - inline unsigned short GetSocketFamily(void) const {return socketFamily;} - inline _PP_Instance_ GetChromeInstance(void) const {return chromeInstance;} - inline unsigned int GetUserConnectionSocketIndex(void) const { - RakAssert(userConnectionSocketIndex!=(unsigned int)-1); - return userConnectionSocketIndex;} - - -#ifdef __native_client__ - // Flag indicating if a SendTo is currently in progress - bool sendInProgress; - - // Data for next queued packet to send, if nextSendSize > 0 - char nextSendBuffer[MAXIMUM_MTU_SIZE]; - - // Size of next queued packet to send, or 0 if no queued packet - int nextSendSize; - - // Destination address of queued packet - PP_NetAddress_Private nextSendAddr; -#endif - - __UDPSOCKET__ s; - -protected: - -#if defined (_WIN32) && defined(USE_WAIT_FOR_MULTIPLE_EVENTS) - void* recvEvent; -#endif - - #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3) || defined(_PS4) || defined(SN_TARGET_PSP2) - /// PS3: Set for the PS3, when using signaling. - /// PS3: Connect with the port returned by signaling. Set this to whatever port RakNet was actually started on - /// PSP2: Set non-zero to use SCE_NET_SOCK_DGRAM_P2P. This should be done for ad-hoc or with - #endif - - unsigned short remotePortRakNetWasStartedOn_PS3_PSP2; - unsigned int userConnectionSocketIndex; - SystemAddress boundAddress; - unsigned short socketFamily; - bool blockingSocket; - unsigned int extraSocketOptions; - _PP_Instance_ chromeInstance; -}; - -} // namespace RakNet - -#endif -*/ +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/* +#pragma once + +#include "RakNetTypes.h" +#include "RakNetDefines.h" +#include "Export.h" +#include "SocketIncludes.h" +#include "RakAssert.h" +#include "SocketDefines.h" +#include "MTUSize.h" + +namespace RakNet +{ + +struct RAK_DLL_EXPORT RakNetSocket +{ +public: + RakNetSocket(); + ~RakNetSocket(); + +// void Accept( +// struct sockaddr *addr, +// int *addrlen); + + inline int Connect( + const struct sockaddr *name, + int namelen) {return connect__(s,name,namelen);} + + static RakNetSocket* Create +#ifdef __native_client__ + (_PP_Instance_ _chromeInstance); +#else + (int af, + int type, + int protocol); +#endif + + int Bind( + const struct sockaddr *addr, + int namelen); + + inline int GetSockName( + struct sockaddr *name, + socklen_t * namelen) {return getsockname__(s,name,namelen);} + + inline int GetSockOpt ( + int level, + int optname, + char * optval, + socklen_t *optlen) {return getsockopt__(s,level,optname,optval,optlen);} + + + int IOCTLSocket( + long cmd, + unsigned long *argp); + + int Listen ( + int backlog); + + inline int Recv( + char * buf, + int len, + int flags) {return recv__(s,buf,len,flags);} + + inline int RecvFrom( + char * buf, + int len, + int flags, + struct sockaddr * from, + socklen_t * fromlen) {return recvfrom__(s,buf,len,flags,from,fromlen);} + +// inline int Select( +// int nfds, +// fd_set *readfds, +// fd_set *writefds, +// fd_set *exceptfds, +// struct timeval *timeout) {return select__(nfds,readfds,writefds,exceptfds,timeout);} + + inline int Send( + const char * buf, + int len, + int flags) {return send__(s,buf,len,flags);} + + inline int SendTo( + const char * buf, + int len, + int flags, + const struct sockaddr *to, + int tolen) {return sendto__(s,buf,len,flags,to,tolen);} + + #ifdef _WIN32 + #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3) || defined(_PS4) || defined(SN_TARGET_PSP2) + #else + inline int Fcntl(int cmd, int arg) {return fcntl(s,cmd,arg);} + #endif + + +#if defined(_WIN32) && !defined(WINDOWS_STORE_RT) + inline int _WSASendTo( + LPWSABUF lpBuffers, + DWORD dwBufferCount, + LPDWORD lpNumberOfBytesSent, + DWORD dwFlags, + const struct sockaddr FAR * lpTo, + int iTolen, + LPWSAOVERLAPPED lpOverlapped, + LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine + ) + { return WSASendTo(s,lpBuffers,dwBufferCount,lpNumberOfBytesSent,dwFlags,lpTo,iTolen,lpOverlapped,lpCompletionRoutine);} + +#endif + + int SetSockOpt( + int level, + int optname, + const char * optval, + int optlen); + + int Shutdown( + int how); + + + inline void SetRemotePortRakNetWasStartedOn(unsigned short i) {remotePortRakNetWasStartedOn_PS3_PSP2=i;} + inline void SetUserConnectionSocketIndex(unsigned int i) {userConnectionSocketIndex=i;} + inline void SetBoundAddress(SystemAddress i) {boundAddress=i;} + inline void SetSocketFamily(unsigned short i) {socketFamily=i;} + inline void SetBlockingSocket(bool i) {blockingSocket=i;} + inline void SetExtraSocketOptions(unsigned int i) {extraSocketOptions=i;} + inline void SetChromeInstance(_PP_Instance_ i) {chromeInstance=i;} + inline void SetBoundAddressToLoopback(unsigned char ipVersion) {boundAddress.SetToLoopback(ipVersion);} + + inline SystemAddress GetBoundAddress(void) const {return boundAddress;} + inline unsigned short GetRemotePortRakNetWasStartedOn(void) const {return remotePortRakNetWasStartedOn_PS3_PSP2;} + inline bool GetBlockingSocket(void) {return blockingSocket;} + inline unsigned int GetExtraSocketOptions(void) const {return extraSocketOptions;} + inline unsigned short GetSocketFamily(void) const {return socketFamily;} + inline _PP_Instance_ GetChromeInstance(void) const {return chromeInstance;} + inline unsigned int GetUserConnectionSocketIndex(void) const { + RakAssert(userConnectionSocketIndex!=(unsigned int)-1); + return userConnectionSocketIndex;} + + +#ifdef __native_client__ + // Flag indicating if a SendTo is currently in progress + bool sendInProgress; + + // Data for next queued packet to send, if nextSendSize > 0 + char nextSendBuffer[MAXIMUM_MTU_SIZE]; + + // Size of next queued packet to send, or 0 if no queued packet + int nextSendSize; + + // Destination address of queued packet + PP_NetAddress_Private nextSendAddr; +#endif + + __UDPSOCKET__ s; + +protected: + +#if defined (_WIN32) && defined(USE_WAIT_FOR_MULTIPLE_EVENTS) + void* recvEvent; +#endif + + #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3) || defined(_PS4) || defined(SN_TARGET_PSP2) + /// PS3: Set for the PS3, when using signaling. + /// PS3: Connect with the port returned by signaling. Set this to whatever port RakNet was actually started on + /// PSP2: Set non-zero to use SCE_NET_SOCK_DGRAM_P2P. This should be done for ad-hoc or with + #endif + + unsigned short remotePortRakNetWasStartedOn_PS3_PSP2; + unsigned int userConnectionSocketIndex; + SystemAddress boundAddress; + unsigned short socketFamily; + bool blockingSocket; + unsigned int extraSocketOptions; + _PP_Instance_ chromeInstance; +}; + +} // namespace RakNet + +*/ diff --git a/Source/RakNetSocket2.cpp b/Source/RakNetSocket2.cpp index 86004601c..144ca183f 100644 --- a/Source/RakNetSocket2.cpp +++ b/Source/RakNetSocket2.cpp @@ -1,513 +1,513 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "RakNetSocket2.h" -#include "RakMemoryOverride.h" -#include "RakAssert.h" -#include "RakSleep.h" -#include "SocketDefines.h" -#include "GetTime.h" -#include -#include // memcpy - -using namespace RakNet; - -#ifdef _WIN32 -#else -#include -#include -#include -#include // error numbers -#if !defined(ANDROID) -#include -#endif -#include -#include -#include -#include -#include -#endif - -#ifdef TEST_NATIVE_CLIENT_ON_WINDOWS -#else -#define RAKNET_SOCKET_2_INLINE_FUNCTIONS -#include "RakNetSocket2_360_720.cpp" -#include "RakNetSocket2_PS3_PS4.cpp" -#include "RakNetSocket2_PS4.cpp" -#include "RakNetSocket2_Windows_Linux.cpp" -#include "RakNetSocket2_Windows_Linux_360.cpp" -#include "RakNetSocket2_Vita.cpp" -#include "RakNetSocket2_NativeClient.cpp" -#include "RakNetSocket2_Berkley.cpp" -#include "RakNetSocket2_Berkley_NativeClient.cpp" -#include "RakNetSocket2_WindowsStore8.cpp" -#undef RAKNET_SOCKET_2_INLINE_FUNCTIONS - -#endif - -#ifndef INVALID_SOCKET -#define INVALID_SOCKET -1 -#endif - -void RakNetSocket2Allocator::DeallocRNS2(RakNetSocket2 *s) {RakNet::OP_DELETE(s,_FILE_AND_LINE_);} -RakNetSocket2::RakNetSocket2() {eventHandler=0;} -RakNetSocket2::~RakNetSocket2() {} -void RakNetSocket2::SetRecvEventHandler(RNS2EventHandler *_eventHandler) {eventHandler=_eventHandler;} -RNS2Type RakNetSocket2::GetSocketType(void) const {return socketType;} -void RakNetSocket2::SetSocketType(RNS2Type t) {socketType=t;} -bool RakNetSocket2::IsBerkleySocket(void) const { - return socketType!=RNS2T_CHROME && socketType!=RNS2T_WINDOWS_STORE_8; -} -SystemAddress RakNetSocket2::GetBoundAddress(void) const {return boundAddress;} - -RakNetSocket2* RakNetSocket2Allocator::AllocRNS2(void) -{ - RakNetSocket2* s2; -#if defined(WINDOWS_STORE_RT) - s2 = RakNet::OP_NEW(_FILE_AND_LINE_); - s2->SetSocketType(RNS2T_WINDOWS_STORE_8); - - - - - - -#elif defined(__native_client__) - s2 = RakNet::OP_NEW(_FILE_AND_LINE_); - s2->SetSocketType(RNS2T_CHROME); - - - - - - - - - -#elif defined(_WIN32) - s2 = RakNet::OP_NEW(_FILE_AND_LINE_); - s2->SetSocketType(RNS2T_WINDOWS); -#else - s2 = RakNet::OP_NEW(_FILE_AND_LINE_); - s2->SetSocketType(RNS2T_LINUX); -#endif - return s2; -} -void RakNetSocket2::GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) -{ -#if defined(WINDOWS_STORE_RT) - RNS2_WindowsStore8::GetMyIP( addresses ); - - - - -#elif defined(__native_client__) - RNS2_NativeClient::GetMyIP( addresses ); - - - - - - -#elif defined(_WIN32) - RNS2_Windows::GetMyIP( addresses ); -#else - RNS2_Linux::GetMyIP( addresses ); -#endif -} - -unsigned int RakNetSocket2::GetUserConnectionSocketIndex(void) const {return userConnectionSocketIndex;} -void RakNetSocket2::SetUserConnectionSocketIndex(unsigned int i) {userConnectionSocketIndex=i;} -RNS2EventHandler * RakNetSocket2::GetEventHandler(void) const {return eventHandler;} - -void RakNetSocket2::DomainNameToIP( const char *domainName, char ip[65] ) { -#if defined(WINDOWS_STORE_RT) - return RNS2_WindowsStore8::DomainNameToIP( domainName, ip ); -#elif defined(__native_client__) - return DomainNameToIP_Berkley( domainName, ip ); - - - - - - - - - - -#elif defined(_WIN32) - return DomainNameToIP_Berkley( domainName, ip ); -#else - return DomainNameToIP_Berkley( domainName, ip ); -#endif -} - -#if defined(WINDOWS_STORE_RT) -#elif defined(__native_client__) -RNS2_NativeClient::RNS2_NativeClient() {bindState = BS_UNBOUND; sendInProgress=false;} -RNS2_NativeClient::~RNS2_NativeClient() -{ - bufferedSendsMutex.Lock(); - while (bufferedSends.Size()) - RakNet::OP_DELETE(bufferedSends.Pop(), _FILE_AND_LINE_); - bufferedSendsMutex.Unlock(); -} -void RNS2_NativeClient::onSocketBound(void* pData, int32_t dataSize) -{ - RAKNET_DEBUG_PRINTF("onSocketBound ==> %d\n", dataSize); - RNS2_NativeClient *csc = (RNS2_NativeClient *)pData; - - //any error codes will be given to us in the dataSize value - if(dataSize < 0) - { - csc->bindState=BS_FAILED; - fprintf(stderr,"onSocketBound exiting, dataSize = %d\n", dataSize); - return; - } - - csc->bindState=BS_BOUND; - - csc->ProcessBufferedSend(); - csc->IssueReceiveCall(); -} -void RNS2_NativeClient::ProcessBufferedSend(void) -{ - // Don't send until bound - if (bindState!=BS_BOUND) - return; - // Fast non-threadsafe check - if (bufferedSends.IsEmpty()==true) - return; - - sendInProgressMutex.Lock(); - if (sendInProgress==true) {sendInProgressMutex.Unlock(); return;} - else {sendInProgress=true;} - sendInProgressMutex.Unlock(); - - RNS2_SendParameters_NativeClient *sp; - bufferedSendsMutex.Lock(); - if (bufferedSends.IsEmpty()==false) - sp=bufferedSends.Pop(); - else - sp=0; - bufferedSendsMutex.Unlock(); - if (sp==0) - { - sendInProgressMutex.Lock(); - sendInProgress=false; - sendInProgressMutex.Unlock(); - return; // Nothing to send after all - } - - SendImmediate(sp); - // sp remains in memory until the callback completes - // DeallocSP(sp); -} -void RNS2_NativeClient::DeallocSP(RNS2_SendParameters_NativeClient *sp) -{ - rakFree_Ex(sp->data, _FILE_AND_LINE_); - RakNet::OP_DELETE(sp, _FILE_AND_LINE_); -} -RNS2_SendParameters_NativeClient* RNS2_NativeClient::CloneSP(RNS2_SendParameters *sp, RNS2_NativeClient *socket2, const char *file, unsigned int line) -{ - RNS2_SendParameters_NativeClient *spNew = RakNet::OP_NEW(file, line); - spNew->data=(char*) rakMalloc(sp->length); - memcpy(spNew->data,sp->data,sp->length); - spNew->length = sp->length; - spNew->socket2=socket2; - spNew->systemAddress=sp->systemAddress; - spNew->ttl=0; // Unused - return spNew; -} -void RNS2_NativeClient::onSendTo(void* pData, int32_t dataSize) -{ - if(dataSize <= 0) - RAKNET_DEBUG_PRINTF("onSendTo: send failed with error %d\n", dataSize); - - RNS2_SendParameters_NativeClient *sp = (RNS2_SendParameters_NativeClient*) pData; - - // Caller will check sendInProgress to send again if needed - sp->socket2->sendInProgressMutex.Lock(); - sp->socket2->sendInProgress=false; - sp->socket2->sendInProgressMutex.Unlock(); - - DeallocSP(sp); - -// if(dataSize == PP_ERROR_ABORTED) -// return; -} -RNS2SendResult RNS2_NativeClient::Send( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ) -{ - if (bindState==BS_FAILED) - return -1; - - // This is called from multiple threads. Always buffer the send, until native client is threadsafe - BufferSend(sendParameters, file, line); - return sendParameters->length; -} -void RNS2_NativeClient::BufferSend( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ) -{ - if (bindState==BS_FAILED) - return; - - RNS2_SendParameters_NativeClient* sp = CloneSP(sendParameters, this, file, line); - bufferedSendsMutex.Lock(); - bufferedSends.Push(sp, file, line); - bufferedSendsMutex.Unlock(); - - // Do not check to send immediately, because this was probably invoked from a thread and native client is not threadsafe -} -void RNS2_NativeClient::GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) {addresses[0]=UNASSIGNED_SYSTEM_ADDRESS; RakAssert("GetMyIP Unsupported?" && 0);} -const NativeClientBindParameters *RNS2_NativeClient::GetBindings(void) const {return &binding;} -void RNS2_NativeClient::Update(void) -{ - // Don't send until bound - if (bindState==BS_BOUND) - { - do - { - ProcessBufferedSend(); - } while (sendInProgress==false && bufferedSends.Size()>1); - } -} - -#else // defined(__native_client__) -bool IRNS2_Berkley::IsPortInUse(unsigned short port, const char *hostAddress, unsigned short addressFamily, int type ) { - RNS2_BerkleyBindParameters bbp; - bbp.remotePortRakNetWasStartedOn_PS3_PS4_PSP2=0; - bbp.port=port; bbp.hostAddress=(char*) hostAddress; bbp.addressFamily=addressFamily; - bbp.type=type; bbp.protocol=0; bbp.nonBlockingSocket=false; - bbp.setBroadcast=false; bbp.doNotFragment=false; bbp.protocol=0; - bbp.setIPHdrIncl=false; - SystemAddress boundAddress; - RNS2_Berkley *rns2 = (RNS2_Berkley*) RakNetSocket2Allocator::AllocRNS2(); - RNS2BindResult bindResult = rns2->Bind(&bbp, _FILE_AND_LINE_); - RakNetSocket2Allocator::DeallocRNS2(rns2); - return bindResult==BR_FAILED_TO_BIND_SOCKET; -} - -#if defined(__APPLE__) -void SocketReadCallback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) -// This C routine is called by CFSocket when there's data waiting on our -// UDP socket. It just redirects the call to Objective-C code. -{ } -#endif - -RNS2BindResult RNS2_Berkley::BindShared( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ) { - RNS2BindResult br; -#if RAKNET_SUPPORT_IPV6==1 - br=BindSharedIPV4And6(bindParameters, file, line); -#else - br=BindSharedIPV4(bindParameters, file, line); -#endif - - if (br!=BR_SUCCESS) - return br; - - unsigned long zero=0; - RNS2_SendParameters bsp; - bsp.data=(char*) &zero; - bsp.length=4; - bsp.systemAddress=boundAddress; - bsp.ttl=0; - RNS2SendResult sr = Send(&bsp, _FILE_AND_LINE_); - if (sr<0) - return BR_FAILED_SEND_TEST; - - memcpy(&binding, bindParameters, sizeof(RNS2_BerkleyBindParameters)); - - /* -#if defined(__APPLE__) - const CFSocketContext context = { 0, this, NULL, NULL, NULL }; - _cfSocket = CFSocketCreateWithNative(NULL, rns2Socket, kCFSocketReadCallBack, SocketReadCallback, &context); -#endif - */ - - return br; -} - -RAK_THREAD_DECLARATION(RNS2_Berkley::RecvFromLoop) -{ - - - - RNS2_Berkley *b = ( RNS2_Berkley * ) arguments; - - b->RecvFromLoopInt(); - return 0; -} -unsigned RNS2_Berkley::RecvFromLoopInt(void) -{ - isRecvFromLoopThreadActive.Increment(); - - while ( endThreads == false ) - { - RNS2RecvStruct *recvFromStruct; - recvFromStruct=binding.eventHandler->AllocRNS2RecvStruct(_FILE_AND_LINE_); - if (recvFromStruct != NULL) - { - recvFromStruct->socket=this; - RecvFromBlocking(recvFromStruct); - - if (recvFromStruct->bytesRead>0) - { - RakAssert(recvFromStruct->systemAddress.GetPort()); - binding.eventHandler->OnRNS2Recv(recvFromStruct); - } - else - { - RakSleep(0); - binding.eventHandler->DeallocRNS2RecvStruct(recvFromStruct, _FILE_AND_LINE_); - } - } - } - isRecvFromLoopThreadActive.Decrement(); - - - - - return 0; - -} -RNS2_Berkley::RNS2_Berkley() -{ - rns2Socket=(RNS2Socket)INVALID_SOCKET; -} -RNS2_Berkley::~RNS2_Berkley() -{ - if (rns2Socket!=INVALID_SOCKET) - { - /* -#if defined(__APPLE__) - CFSocketInvalidate(_cfSocket); -#endif - */ - - closesocket__(rns2Socket); - } - -} -int RNS2_Berkley::CreateRecvPollingThread(int threadPriority) -{ - endThreads=false; - - - - - - - int errorCode = RakNet::RakThread::Create(RecvFromLoop, this, threadPriority); - - return errorCode; -} -void RNS2_Berkley::SignalStopRecvPollingThread(void) -{ - endThreads=true; -} -void RNS2_Berkley::BlockOnStopRecvPollingThread(void) -{ - endThreads=true; - - // Get recvfrom to unblock - RNS2_SendParameters bsp; - unsigned long zero=0; - bsp.data=(char*) &zero; - bsp.length=4; - bsp.systemAddress=boundAddress; - bsp.ttl=0; - Send(&bsp, _FILE_AND_LINE_); - - RakNet::TimeMS timeout = RakNet::GetTimeMS()+1000; - while ( isRecvFromLoopThreadActive.GetValue()>0 && RakNet::GetTimeMS()RakNetSendTo(sendParameters->data, sendParameters->length,sendParameters->systemAddress); - if (len>=0) - return len; - } - return Send_Windows_Linux_360NoVDP(rns2Socket,sendParameters, file, line); -} -void RNS2_Windows::GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) {return GetMyIP_Windows_Linux(addresses);} -void RNS2_Windows::SetSocketLayerOverride(SocketLayerOverride *_slo) {slo = _slo;} -SocketLayerOverride* RNS2_Windows::GetSocketLayerOverride(void) {return slo;} -#else -RNS2BindResult RNS2_Linux::Bind( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ) {return BindShared(bindParameters, file, line);} -RNS2SendResult RNS2_Linux::Send( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ) {return Send_Windows_Linux_360NoVDP(rns2Socket,sendParameters, file, line);} -void RNS2_Linux::GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) {return GetMyIP_Windows_Linux(addresses);} -#endif // Linux - -#endif // defined(__native_client__) +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "RakNetSocket2.h" +#include "RakMemoryOverride.h" +#include "RakAssert.h" +#include "RakSleep.h" +#include "SocketDefines.h" +#include "GetTime.h" +#include +#include // memcpy + +using namespace RakNet; + +#ifdef _WIN32 +#else +#include +#include +#include +#include // error numbers +#if !defined(ANDROID) +#include +#endif +#include +#include +#include +#include +#include +#endif + +#ifdef TEST_NATIVE_CLIENT_ON_WINDOWS +#else +#define RAKNET_SOCKET_2_INLINE_FUNCTIONS +#include "RakNetSocket2_360_720.cpp" +#include "RakNetSocket2_PS3_PS4.cpp" +#include "RakNetSocket2_PS4.cpp" +#include "RakNetSocket2_Windows_Linux.cpp" +#include "RakNetSocket2_Windows_Linux_360.cpp" +#include "RakNetSocket2_Vita.cpp" +#include "RakNetSocket2_NativeClient.cpp" +#include "RakNetSocket2_Berkley.cpp" +#include "RakNetSocket2_Berkley_NativeClient.cpp" +#include "RakNetSocket2_WindowsStore8.cpp" +#undef RAKNET_SOCKET_2_INLINE_FUNCTIONS + +#endif + +#ifndef INVALID_SOCKET +#define INVALID_SOCKET -1 +#endif + +void RakNetSocket2Allocator::DeallocRNS2(RakNetSocket2 *s) {RakNet::OP_DELETE(s,_FILE_AND_LINE_);} +RakNetSocket2::RakNetSocket2() {eventHandler=0;} +RakNetSocket2::~RakNetSocket2() {} +void RakNetSocket2::SetRecvEventHandler(RNS2EventHandler *_eventHandler) {eventHandler=_eventHandler;} +RNS2Type RakNetSocket2::GetSocketType(void) const {return socketType;} +void RakNetSocket2::SetSocketType(RNS2Type t) {socketType=t;} +bool RakNetSocket2::IsBerkleySocket(void) const { + return socketType!=RNS2T_CHROME && socketType!=RNS2T_WINDOWS_STORE_8; +} +SystemAddress RakNetSocket2::GetBoundAddress(void) const {return boundAddress;} + +RakNetSocket2* RakNetSocket2Allocator::AllocRNS2(void) +{ + RakNetSocket2* s2; +#if defined(WINDOWS_STORE_RT) + s2 = RakNet::OP_NEW(_FILE_AND_LINE_); + s2->SetSocketType(RNS2T_WINDOWS_STORE_8); + + + + + + +#elif defined(__native_client__) + s2 = RakNet::OP_NEW(_FILE_AND_LINE_); + s2->SetSocketType(RNS2T_CHROME); + + + + + + + + + +#elif defined(_WIN32) + s2 = RakNet::OP_NEW(_FILE_AND_LINE_); + s2->SetSocketType(RNS2T_WINDOWS); +#else + s2 = RakNet::OP_NEW(_FILE_AND_LINE_); + s2->SetSocketType(RNS2T_LINUX); +#endif + return s2; +} +void RakNetSocket2::GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) +{ +#if defined(WINDOWS_STORE_RT) + RNS2_WindowsStore8::GetMyIP( addresses ); + + + + +#elif defined(__native_client__) + RNS2_NativeClient::GetMyIP( addresses ); + + + + + + +#elif defined(_WIN32) + RNS2_Windows::GetMyIP( addresses ); +#else + RNS2_Linux::GetMyIP( addresses ); +#endif +} + +unsigned int RakNetSocket2::GetUserConnectionSocketIndex(void) const {return userConnectionSocketIndex;} +void RakNetSocket2::SetUserConnectionSocketIndex(unsigned int i) {userConnectionSocketIndex=i;} +RNS2EventHandler * RakNetSocket2::GetEventHandler(void) const {return eventHandler;} + +void RakNetSocket2::DomainNameToIP( const char *domainName, char ip[65] ) { +#if defined(WINDOWS_STORE_RT) + return RNS2_WindowsStore8::DomainNameToIP( domainName, ip ); +#elif defined(__native_client__) + return DomainNameToIP_Berkley( domainName, ip ); + + + + + + + + + + +#elif defined(_WIN32) + return DomainNameToIP_Berkley( domainName, ip ); +#else + return DomainNameToIP_Berkley( domainName, ip ); +#endif +} + +#if defined(WINDOWS_STORE_RT) +#elif defined(__native_client__) +RNS2_NativeClient::RNS2_NativeClient() {bindState = BS_UNBOUND; sendInProgress=false;} +RNS2_NativeClient::~RNS2_NativeClient() +{ + bufferedSendsMutex.Lock(); + while (bufferedSends.Size()) + RakNet::OP_DELETE(bufferedSends.Pop(), _FILE_AND_LINE_); + bufferedSendsMutex.Unlock(); +} +void RNS2_NativeClient::onSocketBound(void* pData, int32_t dataSize) +{ + RAKNET_DEBUG_PRINTF("onSocketBound ==> %d\n", dataSize); + RNS2_NativeClient *csc = (RNS2_NativeClient *)pData; + + //any error codes will be given to us in the dataSize value + if(dataSize < 0) + { + csc->bindState=BS_FAILED; + fprintf(stderr,"onSocketBound exiting, dataSize = %d\n", dataSize); + return; + } + + csc->bindState=BS_BOUND; + + csc->ProcessBufferedSend(); + csc->IssueReceiveCall(); +} +void RNS2_NativeClient::ProcessBufferedSend(void) +{ + // Don't send until bound + if (bindState!=BS_BOUND) + return; + // Fast non-threadsafe check + if (bufferedSends.IsEmpty()==true) + return; + + sendInProgressMutex.Lock(); + if (sendInProgress==true) {sendInProgressMutex.Unlock(); return;} + else {sendInProgress=true;} + sendInProgressMutex.Unlock(); + + RNS2_SendParameters_NativeClient *sp; + bufferedSendsMutex.Lock(); + if (bufferedSends.IsEmpty()==false) + sp=bufferedSends.Pop(); + else + sp=0; + bufferedSendsMutex.Unlock(); + if (sp==0) + { + sendInProgressMutex.Lock(); + sendInProgress=false; + sendInProgressMutex.Unlock(); + return; // Nothing to send after all + } + + SendImmediate(sp); + // sp remains in memory until the callback completes + // DeallocSP(sp); +} +void RNS2_NativeClient::DeallocSP(RNS2_SendParameters_NativeClient *sp) +{ + rakFree_Ex(sp->data, _FILE_AND_LINE_); + RakNet::OP_DELETE(sp, _FILE_AND_LINE_); +} +RNS2_SendParameters_NativeClient* RNS2_NativeClient::CloneSP(RNS2_SendParameters *sp, RNS2_NativeClient *socket2, const char *file, unsigned int line) +{ + RNS2_SendParameters_NativeClient *spNew = RakNet::OP_NEW(file, line); + spNew->data=(char*) rakMalloc(sp->length); + memcpy(spNew->data,sp->data,sp->length); + spNew->length = sp->length; + spNew->socket2=socket2; + spNew->systemAddress=sp->systemAddress; + spNew->ttl=0; // Unused + return spNew; +} +void RNS2_NativeClient::onSendTo(void* pData, int32_t dataSize) +{ + if(dataSize <= 0) + RAKNET_DEBUG_PRINTF("onSendTo: send failed with error %d\n", dataSize); + + RNS2_SendParameters_NativeClient *sp = (RNS2_SendParameters_NativeClient*) pData; + + // Caller will check sendInProgress to send again if needed + sp->socket2->sendInProgressMutex.Lock(); + sp->socket2->sendInProgress=false; + sp->socket2->sendInProgressMutex.Unlock(); + + DeallocSP(sp); + +// if(dataSize == PP_ERROR_ABORTED) +// return; +} +RNS2SendResult RNS2_NativeClient::Send( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ) +{ + if (bindState==BS_FAILED) + return -1; + + // This is called from multiple threads. Always buffer the send, until native client is threadsafe + BufferSend(sendParameters, file, line); + return sendParameters->length; +} +void RNS2_NativeClient::BufferSend( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ) +{ + if (bindState==BS_FAILED) + return; + + RNS2_SendParameters_NativeClient* sp = CloneSP(sendParameters, this, file, line); + bufferedSendsMutex.Lock(); + bufferedSends.Push(sp, file, line); + bufferedSendsMutex.Unlock(); + + // Do not check to send immediately, because this was probably invoked from a thread and native client is not threadsafe +} +void RNS2_NativeClient::GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) {addresses[0]=UNASSIGNED_SYSTEM_ADDRESS; RakAssert("GetMyIP Unsupported?" && 0);} +const NativeClientBindParameters *RNS2_NativeClient::GetBindings(void) const {return &binding;} +void RNS2_NativeClient::Update(void) +{ + // Don't send until bound + if (bindState==BS_BOUND) + { + do + { + ProcessBufferedSend(); + } while (sendInProgress==false && bufferedSends.Size()>1); + } +} + +#else // defined(__native_client__) +bool IRNS2_Berkley::IsPortInUse(unsigned short port, const char *hostAddress, unsigned short addressFamily, int type ) { + RNS2_BerkleyBindParameters bbp; + bbp.remotePortRakNetWasStartedOn_PS3_PS4_PSP2=0; + bbp.port=port; bbp.hostAddress=(char*) hostAddress; bbp.addressFamily=addressFamily; + bbp.type=type; bbp.protocol=0; bbp.nonBlockingSocket=false; + bbp.setBroadcast=false; bbp.doNotFragment=false; bbp.protocol=0; + bbp.setIPHdrIncl=false; + SystemAddress boundAddress; + RNS2_Berkley *rns2 = (RNS2_Berkley*) RakNetSocket2Allocator::AllocRNS2(); + RNS2BindResult bindResult = rns2->Bind(&bbp, _FILE_AND_LINE_); + RakNetSocket2Allocator::DeallocRNS2(rns2); + return bindResult==BR_FAILED_TO_BIND_SOCKET; +} + +#if defined(__APPLE__) +void SocketReadCallback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) +// This C routine is called by CFSocket when there's data waiting on our +// UDP socket. It just redirects the call to Objective-C code. +{ } +#endif + +RNS2BindResult RNS2_Berkley::BindShared( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ) { + RNS2BindResult br; +#if RAKNET_SUPPORT_IPV6==1 + br=BindSharedIPV4And6(bindParameters, file, line); +#else + br=BindSharedIPV4(bindParameters, file, line); +#endif + + if (br!=BR_SUCCESS) + return br; + + unsigned long zero=0; + RNS2_SendParameters bsp; + bsp.data=(char*) &zero; + bsp.length=4; + bsp.systemAddress=boundAddress; + bsp.ttl=0; + RNS2SendResult sr = Send(&bsp, _FILE_AND_LINE_); + if (sr<0) + return BR_FAILED_SEND_TEST; + + memcpy(&binding, bindParameters, sizeof(RNS2_BerkleyBindParameters)); + + /* +#if defined(__APPLE__) + const CFSocketContext context = { 0, this, nullptr, nullptr, nullptr }; + _cfSocket = CFSocketCreateWithNative(nullptr, rns2Socket, kCFSocketReadCallBack, SocketReadCallback, &context); +#endif + */ + + return br; +} + +RAK_THREAD_DECLARATION(RNS2_Berkley::RecvFromLoop) +{ + + + + RNS2_Berkley *b = ( RNS2_Berkley * ) arguments; + + b->RecvFromLoopInt(); + return 0; +} +unsigned RNS2_Berkley::RecvFromLoopInt(void) +{ + isRecvFromLoopThreadActive.Increment(); + + while ( endThreads == false ) + { + RNS2RecvStruct *recvFromStruct; + recvFromStruct=binding.eventHandler->AllocRNS2RecvStruct(_FILE_AND_LINE_); + if (recvFromStruct != nullptr) + { + recvFromStruct->socket=this; + RecvFromBlocking(recvFromStruct); + + if (recvFromStruct->bytesRead>0) + { + RakAssert(recvFromStruct->systemAddress.GetPort()); + binding.eventHandler->OnRNS2Recv(recvFromStruct); + } + else + { + RakSleep(0); + binding.eventHandler->DeallocRNS2RecvStruct(recvFromStruct, _FILE_AND_LINE_); + } + } + } + isRecvFromLoopThreadActive.Decrement(); + + + + + return 0; + +} +RNS2_Berkley::RNS2_Berkley() +{ + rns2Socket=(RNS2Socket)INVALID_SOCKET; +} +RNS2_Berkley::~RNS2_Berkley() +{ + if (rns2Socket!=INVALID_SOCKET) + { + /* +#if defined(__APPLE__) + CFSocketInvalidate(_cfSocket); +#endif + */ + + closesocket__(rns2Socket); + } + +} +int RNS2_Berkley::CreateRecvPollingThread(int threadPriority) +{ + endThreads=false; + + + + + + + int errorCode = RakNet::RakThread::Create(RecvFromLoop, this, threadPriority); + + return errorCode; +} +void RNS2_Berkley::SignalStopRecvPollingThread(void) +{ + endThreads=true; +} +void RNS2_Berkley::BlockOnStopRecvPollingThread(void) +{ + endThreads=true; + + // Get recvfrom to unblock + RNS2_SendParameters bsp; + unsigned long zero=0; + bsp.data=(char*) &zero; + bsp.length=4; + bsp.systemAddress=boundAddress; + bsp.ttl=0; + Send(&bsp, _FILE_AND_LINE_); + + RakNet::TimeMS timeout = RakNet::GetTimeMS()+1000; + while ( isRecvFromLoopThreadActive.GetValue()>0 && RakNet::GetTimeMS()RakNetSendTo(sendParameters->data, sendParameters->length,sendParameters->systemAddress); + if (len>=0) + return len; + } + return Send_Windows_Linux_360NoVDP(rns2Socket,sendParameters, file, line); +} +void RNS2_Windows::GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) {return GetMyIP_Windows_Linux(addresses);} +void RNS2_Windows::SetSocketLayerOverride(SocketLayerOverride *_slo) {slo = _slo;} +SocketLayerOverride* RNS2_Windows::GetSocketLayerOverride(void) {return slo;} +#else +RNS2BindResult RNS2_Linux::Bind( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ) {return BindShared(bindParameters, file, line);} +RNS2SendResult RNS2_Linux::Send( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ) {return Send_Windows_Linux_360NoVDP(rns2Socket,sendParameters, file, line);} +void RNS2_Linux::GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) {return GetMyIP_Windows_Linux(addresses);} +#endif // Linux + +#endif // defined(__native_client__) diff --git a/Source/RakNetSocket2.h b/Source/RakNetSocket2.h index 00410a98c..79ca140d4 100644 --- a/Source/RakNetSocket2.h +++ b/Source/RakNetSocket2.h @@ -1,453 +1,451 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __RAKNET_SOCKET_2_H -#define __RAKNET_SOCKET_2_H - -#include "RakNetTypes.h" -#include "MTUSize.h" -#include "LocklessTypes.h" -#include "RakThread.h" -#include "DS_ThreadsafeAllocatingQueue.h" -#include "Export.h" - -// For CFSocket -// https://developer.apple.com/library/mac/#documentation/CoreFOundation/Reference/CFSocketRef/Reference/reference.html -// Reason: http://sourceforge.net/p/open-dis/discussion/683284/thread/0929d6a0 -#if defined(__APPLE__) -#import -#include -#include -#endif - -// #define TEST_NATIVE_CLIENT_ON_WINDOWS - -#ifdef TEST_NATIVE_CLIENT_ON_WINDOWS -#define __native_client__ -typedef int PP_Resource; -#endif - -namespace RakNet -{ - -class RakNetSocket2; -struct RNS2_BerkleyBindParameters; -struct RNS2_SendParameters; -typedef int RNS2Socket; - -enum RNS2BindResult -{ - BR_SUCCESS, - BR_REQUIRES_RAKNET_SUPPORT_IPV6_DEFINE, - BR_FAILED_TO_BIND_SOCKET, - BR_FAILED_SEND_TEST, -}; - -typedef int RNS2SendResult; - -enum RNS2Type -{ - RNS2T_WINDOWS_STORE_8, - RNS2T_PS3, - RNS2T_PS4, - RNS2T_CHROME, - RNS2T_VITA, - RNS2T_XBOX_360, - RNS2T_XBOX_720, - RNS2T_WINDOWS, - RNS2T_LINUX -}; - -struct RNS2_SendParameters -{ - RNS2_SendParameters() {ttl=0;} - char *data; - int length; - SystemAddress systemAddress; - int ttl; -}; - -struct RNS2RecvStruct -{ - - - - char data[MAXIMUM_MTU_SIZE]; - - int bytesRead; - SystemAddress systemAddress; - RakNet::TimeUS timeRead; - RakNetSocket2 *socket; -}; - -class RakNetSocket2Allocator -{ -public: - static RakNetSocket2* AllocRNS2(void); - static void DeallocRNS2(RakNetSocket2 *s); -}; - -class RAK_DLL_EXPORT RNS2EventHandler -{ -public: - RNS2EventHandler() {} - virtual ~RNS2EventHandler() {} - - // bufferedPackets.Push(recvFromStruct); - // quitAndDataEvents.SetEvent(); - virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct)=0; - virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line)=0; - virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line)=0; - - // recvFromStruct=bufferedPackets.Allocate( _FILE_AND_LINE_ ); - // DataStructures::ThreadsafeAllocatingQueue bufferedPackets; -}; - -class RakNetSocket2 -{ -public: - RakNetSocket2(); - virtual ~RakNetSocket2(); - - // In order for the handler to trigger, some platforms must call PollRecvFrom, some platforms this create an internal thread. - void SetRecvEventHandler(RNS2EventHandler *_eventHandler); - virtual RNS2SendResult Send( RNS2_SendParameters *sendParameters, const char *file, unsigned int line )=0; - RNS2Type GetSocketType(void) const; - void SetSocketType(RNS2Type t); - bool IsBerkleySocket(void) const; - SystemAddress GetBoundAddress(void) const; - unsigned int GetUserConnectionSocketIndex(void) const; - void SetUserConnectionSocketIndex(unsigned int i); - RNS2EventHandler * GetEventHandler(void) const; - - // ----------- STATICS ------------ - static void GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); - static void DomainNameToIP( const char *domainName, char ip[65] ); - -protected: - RNS2EventHandler *eventHandler; - RNS2Type socketType; - SystemAddress boundAddress; - unsigned int userConnectionSocketIndex; -}; - -#if defined(WINDOWS_STORE_RT) - -ref class ListenerContext; - -// #include -//#include -#include "DS_List.h" -class RNS2_WindowsStore8 : public RakNetSocket2 -{ -public: - RNS2_WindowsStore8(); - ~RNS2_WindowsStore8(); - - virtual RNS2SendResult Send( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ); - RNS2BindResult Bind( Platform::String ^localServiceName ); - // ----------- STATICS ------------ - static void GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); - static void DomainNameToIP( const char *domainName, char ip[65] ); - - static int WinRTInet_Addr(const char * cp); - - static int WinRTSetSockOpt(Windows::Networking::Sockets::DatagramSocket ^s, - int level, - int optname, - const char * optval, - socklen_t optlen); - - static int WinRTIOCTLSocket(Windows::Networking::Sockets::DatagramSocket ^s, - long cmd, - unsigned long *argp); - - static int WinRTGetSockName(Windows::Networking::Sockets::DatagramSocket ^s, - struct sockaddr *name, - socklen_t* namelen); - - static RNS2_WindowsStore8 *GetRNS2FromDatagramSocket(Windows::Networking::Sockets::DatagramSocket^ s); -protected: - static DataStructures::List rns2List; - static SimpleMutex rns2ListMutex; - - Windows::Networking::Sockets::DatagramSocket^ listener; - // Platform::Collections::Map ^outputStreamMap; - // Platform::Collections::Map^ m; - //std::map<> m; - ListenerContext^ listenerContext; -}; -#elif defined(__native_client__) -struct NativeClientBindParameters -{ - _PP_Instance_ nativeClientInstance; - unsigned short port; - const char *forceHostAddress; - bool is_ipv6; - RNS2EventHandler *eventHandler; -}; -class RNS2_NativeClient; -struct RNS2_SendParameters_NativeClient : public RNS2_SendParameters -{ - RNS2_NativeClient *socket2; -}; -class RNS2_NativeClient : public RakNetSocket2 -{ -public: - RNS2_NativeClient(); - virtual ~RNS2_NativeClient(); - RNS2BindResult Bind( NativeClientBindParameters *bindParameters, const char *file, unsigned int line ); - RNS2SendResult Send( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ); - const NativeClientBindParameters *GetBindings(void) const; - - // ----------- STATICS ------------ - static bool IsPortInUse(unsigned short port, const char *hostAddress, unsigned short addressFamily, int type ); - static void GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); - - // RNS2_NativeClient doesn't automatically call recvfrom in a thread - user must call Update() from the main thread - // This causes buffered sends to send, until send is asynch pending - // It causes recvfrom events to trigger the callback, and push a message to the event handler - // - // Example: - // - // DataStructures::List< RakNet::RakNetSocket2* > sockets; - // rakPeerInterface->GetSockets(sockets); - // for (unsigned int i=0; i < sockets.Size(); i++) - // { - // ((RNS2_NativeClient*)sockets[i])->Update(); - // } - - void Update(void); -protected: - void ProcessBufferedSend(void); - static void SendImmediate(RNS2_SendParameters_NativeClient *sp); - static void DeallocSP(RNS2_SendParameters_NativeClient *sp); - static RNS2_SendParameters_NativeClient* CloneSP(RNS2_SendParameters *sp, RNS2_NativeClient *socket2, const char *file, unsigned int line); - static void onRecvFrom(void* pData, int32_t dataSize); - void IssueReceiveCall(void); - static void onSocketBound(void* pData, int32_t dataSize); - static void onSendTo(void* pData, int32_t dataSize); - void BufferSend( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ); - PP_Resource rns2Socket; - NativeClientBindParameters binding; - bool sendInProgress; - SimpleMutex sendInProgressMutex; - - enum BindState - { - BS_UNBOUND, - BS_IN_PROGRESS, - BS_BOUND, - BS_FAILED - } bindState; - DataStructures::Queue bufferedSends; - SimpleMutex bufferedSendsMutex; -}; -#else // defined(WINDOWS_STORE_RT) - -struct RNS2_BerkleyBindParameters -{ - // Input parameters - unsigned short port; - char *hostAddress; - unsigned short addressFamily; // AF_INET or AF_INET6 - int type; // SOCK_DGRAM - int protocol; // 0 - bool nonBlockingSocket; - int setBroadcast; - int setIPHdrIncl; - int doNotFragment; - int pollingThreadPriority; - RNS2EventHandler *eventHandler; - unsigned short remotePortRakNetWasStartedOn_PS3_PS4_PSP2; -}; - -// Every platform except Windows Store 8 can use the Berkley sockets interface -class IRNS2_Berkley : public RakNetSocket2 -{ -public: - // ----------- STATICS ------------ - // For addressFamily, use AF_INET - // For type, use SOCK_DGRAM - static bool IsPortInUse(unsigned short port, const char *hostAddress, unsigned short addressFamily, int type ); - - // ----------- MEMBERS ------------ - virtual RNS2BindResult Bind( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line )=0; -}; -// Every platform that uses Berkley sockets, except native client, can compile some common functions -class RNS2_Berkley : public IRNS2_Berkley -{ -public: - RNS2_Berkley(); - virtual ~RNS2_Berkley(); - int CreateRecvPollingThread(int threadPriority); - void SignalStopRecvPollingThread(void); - void BlockOnStopRecvPollingThread(void); - const RNS2_BerkleyBindParameters *GetBindings(void) const; - RNS2Socket GetSocket(void) const; - void SetDoNotFragment( int opt ); - -protected: - // Used by other classes - RNS2BindResult BindShared( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ); - RNS2BindResult BindSharedIPV4( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ); - RNS2BindResult BindSharedIPV4And6( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ); - - static void GetSystemAddressIPV4 ( RNS2Socket rns2Socket, SystemAddress *systemAddressOut ); - static void GetSystemAddressIPV4And6 ( RNS2Socket rns2Socket, SystemAddress *systemAddressOut ); - - // Internal - void SetNonBlockingSocket(unsigned long nonblocking); - void SetSocketOptions(void); - void SetBroadcastSocket(int broadcast); - void SetIPHdrIncl(int ipHdrIncl); - void RecvFromBlocking(RNS2RecvStruct *recvFromStruct); - void RecvFromBlockingIPV4(RNS2RecvStruct *recvFromStruct); - void RecvFromBlockingIPV4And6(RNS2RecvStruct *recvFromStruct); - - RNS2Socket rns2Socket; - RNS2_BerkleyBindParameters binding; - - unsigned RecvFromLoopInt(void); - RakNet::LocklessUint32_t isRecvFromLoopThreadActive; - volatile bool endThreads; - // Constructor not called! - -#if defined(__APPLE__) - // http://sourceforge.net/p/open-dis/discussion/683284/thread/0929d6a0 - CFSocketRef _cfSocket; -#endif - - static RAK_THREAD_DECLARATION(RecvFromLoop); -}; - - - - - - - - - - - - - - - - - -#if defined(_WIN32) || defined(__GNUC__) || defined(__GCCXML__) || defined(__S3E__) -class RNS2_Windows_Linux_360 -{ -public: -protected: - static RNS2SendResult Send_Windows_Linux_360NoVDP( RNS2Socket rns2Socket, RNS2_SendParameters *sendParameters, const char *file, unsigned int line ); -}; -#endif - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -#if defined(_WIN32) - -class RAK_DLL_EXPORT SocketLayerOverride -{ -public: - SocketLayerOverride() {} - virtual ~SocketLayerOverride() {} - - /// Called when SendTo would otherwise occur. - virtual int RakNetSendTo( const char *data, int length, const SystemAddress &systemAddress )=0; - - /// Called when RecvFrom would otherwise occur. Return number of bytes read. Write data into dataOut - // Return -1 to use RakNet's normal recvfrom, 0 to abort RakNet's normal recvfrom, and positive to return data - virtual int RakNetRecvFrom( char dataOut[ MAXIMUM_MTU_SIZE ], SystemAddress *senderOut, bool calledFromMainThread )=0; -}; - -class RNS2_Windows : public RNS2_Berkley, public RNS2_Windows_Linux_360 -{ -public: - RNS2_Windows(); - virtual ~RNS2_Windows(); - RNS2BindResult Bind( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ); - RNS2SendResult Send( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ); - void SetSocketLayerOverride(SocketLayerOverride *_slo); - SocketLayerOverride* GetSocketLayerOverride(void); - // ----------- STATICS ------------ - static void GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); -protected: - static void GetMyIPIPV4( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); - static void GetMyIPIPV4And6( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); - SocketLayerOverride *slo; -}; - -#else -class RNS2_Linux : public RNS2_Berkley, public RNS2_Windows_Linux_360 -{ -public: - RNS2BindResult Bind( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ); - RNS2SendResult Send( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ); - - // ----------- STATICS ------------ - static void GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); -protected: - static void GetMyIPIPV4( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); - static void GetMyIPIPV4And6( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); -}; - -#endif // Linux - -#endif // #elif !defined(WINDOWS_STORE_RT) - -} // namespace RakNet - -#endif // __RAKNET_SOCKET_2_H +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include "RakNetTypes.h" +#include "MTUSize.h" +#include "LocklessTypes.h" +#include "RakThread.h" +#include "DS_ThreadsafeAllocatingQueue.h" +#include "Export.h" + +// For CFSocket +// https://developer.apple.com/library/mac/#documentation/CoreFOundation/Reference/CFSocketRef/Reference/reference.html +// Reason: http://sourceforge.net/p/open-dis/discussion/683284/thread/0929d6a0 +#if defined(__APPLE__) +#import +#include +#include +#endif + +// #define TEST_NATIVE_CLIENT_ON_WINDOWS + +#ifdef TEST_NATIVE_CLIENT_ON_WINDOWS +#define __native_client__ +typedef int PP_Resource; +#endif + +namespace RakNet +{ + +class RakNetSocket2; +struct RNS2_BerkleyBindParameters; +struct RNS2_SendParameters; +typedef int RNS2Socket; + +enum RNS2BindResult +{ + BR_SUCCESS, + BR_REQUIRES_RAKNET_SUPPORT_IPV6_DEFINE, + BR_FAILED_TO_BIND_SOCKET, + BR_FAILED_SEND_TEST, +}; + +typedef int RNS2SendResult; + +enum RNS2Type +{ + RNS2T_WINDOWS_STORE_8, + RNS2T_PS3, + RNS2T_PS4, + RNS2T_CHROME, + RNS2T_VITA, + RNS2T_XBOX_360, + RNS2T_XBOX_720, + RNS2T_WINDOWS, + RNS2T_LINUX +}; + +struct RNS2_SendParameters +{ + RNS2_SendParameters() {ttl=0;} + char *data; + int length; + SystemAddress systemAddress; + int ttl; +}; + +struct RNS2RecvStruct +{ + + + + char data[MAXIMUM_MTU_SIZE]; + + int bytesRead; + SystemAddress systemAddress; + RakNet::TimeUS timeRead; + RakNetSocket2 *socket; +}; + +class RakNetSocket2Allocator +{ +public: + static RakNetSocket2* AllocRNS2(void); + static void DeallocRNS2(RakNetSocket2 *s); +}; + +class RAK_DLL_EXPORT RNS2EventHandler +{ +public: + RNS2EventHandler() {} + virtual ~RNS2EventHandler() {} + + // bufferedPackets.Push(recvFromStruct); + // quitAndDataEvents.SetEvent(); + virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct)=0; + virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line)=0; + virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line)=0; + + // recvFromStruct=bufferedPackets.Allocate( _FILE_AND_LINE_ ); + // DataStructures::ThreadsafeAllocatingQueue bufferedPackets; +}; + +class RakNetSocket2 +{ +public: + RakNetSocket2(); + virtual ~RakNetSocket2(); + + // In order for the handler to trigger, some platforms must call PollRecvFrom, some platforms this create an internal thread. + void SetRecvEventHandler(RNS2EventHandler *_eventHandler); + virtual RNS2SendResult Send( RNS2_SendParameters *sendParameters, const char *file, unsigned int line )=0; + RNS2Type GetSocketType(void) const; + void SetSocketType(RNS2Type t); + bool IsBerkleySocket(void) const; + SystemAddress GetBoundAddress(void) const; + unsigned int GetUserConnectionSocketIndex(void) const; + void SetUserConnectionSocketIndex(unsigned int i); + RNS2EventHandler * GetEventHandler(void) const; + + // ----------- STATICS ------------ + static void GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); + static void DomainNameToIP( const char *domainName, char ip[65] ); + +protected: + RNS2EventHandler *eventHandler; + RNS2Type socketType; + SystemAddress boundAddress; + unsigned int userConnectionSocketIndex; +}; + +#if defined(WINDOWS_STORE_RT) + +ref class ListenerContext; + +// #include +//#include +#include "DS_List.h" +class RNS2_WindowsStore8 : public RakNetSocket2 +{ +public: + RNS2_WindowsStore8(); + ~RNS2_WindowsStore8(); + + virtual RNS2SendResult Send( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ); + RNS2BindResult Bind( Platform::String ^localServiceName ); + // ----------- STATICS ------------ + static void GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); + static void DomainNameToIP( const char *domainName, char ip[65] ); + + static int WinRTInet_Addr(const char * cp); + + static int WinRTSetSockOpt(Windows::Networking::Sockets::DatagramSocket ^s, + int level, + int optname, + const char * optval, + socklen_t optlen); + + static int WinRTIOCTLSocket(Windows::Networking::Sockets::DatagramSocket ^s, + long cmd, + unsigned long *argp); + + static int WinRTGetSockName(Windows::Networking::Sockets::DatagramSocket ^s, + struct sockaddr *name, + socklen_t* namelen); + + static RNS2_WindowsStore8 *GetRNS2FromDatagramSocket(Windows::Networking::Sockets::DatagramSocket^ s); +protected: + static DataStructures::List rns2List; + static SimpleMutex rns2ListMutex; + + Windows::Networking::Sockets::DatagramSocket^ listener; + // Platform::Collections::Map ^outputStreamMap; + // Platform::Collections::Map^ m; + //std::map<> m; + ListenerContext^ listenerContext; +}; +#elif defined(__native_client__) +struct NativeClientBindParameters +{ + _PP_Instance_ nativeClientInstance; + unsigned short port; + const char *forceHostAddress; + bool is_ipv6; + RNS2EventHandler *eventHandler; +}; +class RNS2_NativeClient; +struct RNS2_SendParameters_NativeClient : public RNS2_SendParameters +{ + RNS2_NativeClient *socket2; +}; +class RNS2_NativeClient : public RakNetSocket2 +{ +public: + RNS2_NativeClient(); + virtual ~RNS2_NativeClient(); + RNS2BindResult Bind( NativeClientBindParameters *bindParameters, const char *file, unsigned int line ); + RNS2SendResult Send( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ); + const NativeClientBindParameters *GetBindings(void) const; + + // ----------- STATICS ------------ + static bool IsPortInUse(unsigned short port, const char *hostAddress, unsigned short addressFamily, int type ); + static void GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); + + // RNS2_NativeClient doesn't automatically call recvfrom in a thread - user must call Update() from the main thread + // This causes buffered sends to send, until send is asynch pending + // It causes recvfrom events to trigger the callback, and push a message to the event handler + // + // Example: + // + // DataStructures::List< RakNet::RakNetSocket2* > sockets; + // rakPeerInterface->GetSockets(sockets); + // for (unsigned int i=0; i < sockets.Size(); i++) + // { + // ((RNS2_NativeClient*)sockets[i])->Update(); + // } + + void Update(void); +protected: + void ProcessBufferedSend(void); + static void SendImmediate(RNS2_SendParameters_NativeClient *sp); + static void DeallocSP(RNS2_SendParameters_NativeClient *sp); + static RNS2_SendParameters_NativeClient* CloneSP(RNS2_SendParameters *sp, RNS2_NativeClient *socket2, const char *file, unsigned int line); + static void onRecvFrom(void* pData, int32_t dataSize); + void IssueReceiveCall(void); + static void onSocketBound(void* pData, int32_t dataSize); + static void onSendTo(void* pData, int32_t dataSize); + void BufferSend( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ); + PP_Resource rns2Socket; + NativeClientBindParameters binding; + bool sendInProgress; + SimpleMutex sendInProgressMutex; + + enum BindState + { + BS_UNBOUND, + BS_IN_PROGRESS, + BS_BOUND, + BS_FAILED + } bindState; + DataStructures::Queue bufferedSends; + SimpleMutex bufferedSendsMutex; +}; +#else // defined(WINDOWS_STORE_RT) + +struct RNS2_BerkleyBindParameters +{ + // Input parameters + unsigned short port; + char *hostAddress; + unsigned short addressFamily; // AF_INET or AF_INET6 + int type; // SOCK_DGRAM + int protocol; // 0 + bool nonBlockingSocket; + int setBroadcast; + int setIPHdrIncl; + int doNotFragment; + int pollingThreadPriority; + RNS2EventHandler *eventHandler; + unsigned short remotePortRakNetWasStartedOn_PS3_PS4_PSP2; +}; + +// Every platform except Windows Store 8 can use the Berkley sockets interface +class IRNS2_Berkley : public RakNetSocket2 +{ +public: + // ----------- STATICS ------------ + // For addressFamily, use AF_INET + // For type, use SOCK_DGRAM + static bool IsPortInUse(unsigned short port, const char *hostAddress, unsigned short addressFamily, int type ); + + // ----------- MEMBERS ------------ + virtual RNS2BindResult Bind( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line )=0; +}; +// Every platform that uses Berkley sockets, except native client, can compile some common functions +class RNS2_Berkley : public IRNS2_Berkley +{ +public: + RNS2_Berkley(); + virtual ~RNS2_Berkley(); + int CreateRecvPollingThread(int threadPriority); + void SignalStopRecvPollingThread(void); + void BlockOnStopRecvPollingThread(void); + const RNS2_BerkleyBindParameters *GetBindings(void) const; + RNS2Socket GetSocket(void) const; + void SetDoNotFragment( int opt ); + +protected: + // Used by other classes + RNS2BindResult BindShared( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ); + RNS2BindResult BindSharedIPV4( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ); + RNS2BindResult BindSharedIPV4And6( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ); + + static void GetSystemAddressIPV4 ( RNS2Socket rns2Socket, SystemAddress *systemAddressOut ); + static void GetSystemAddressIPV4And6 ( RNS2Socket rns2Socket, SystemAddress *systemAddressOut ); + + // Internal + void SetNonBlockingSocket(unsigned long nonblocking); + void SetSocketOptions(void); + void SetBroadcastSocket(int broadcast); + void SetIPHdrIncl(int ipHdrIncl); + void RecvFromBlocking(RNS2RecvStruct *recvFromStruct); + void RecvFromBlockingIPV4(RNS2RecvStruct *recvFromStruct); + void RecvFromBlockingIPV4And6(RNS2RecvStruct *recvFromStruct); + + RNS2Socket rns2Socket; + RNS2_BerkleyBindParameters binding; + + unsigned RecvFromLoopInt(void); + RakNet::LocklessUint32_t isRecvFromLoopThreadActive; + volatile bool endThreads; + // Constructor not called! + +#if defined(__APPLE__) + // http://sourceforge.net/p/open-dis/discussion/683284/thread/0929d6a0 + CFSocketRef _cfSocket; +#endif + + static RAK_THREAD_DECLARATION(RecvFromLoop); +}; + + + + + + + + + + + + + + + + + +#if defined(_WIN32) || defined(__GNUC__) || defined(__GCCXML__) || defined(__S3E__) +class RNS2_Windows_Linux_360 +{ +public: +protected: + static RNS2SendResult Send_Windows_Linux_360NoVDP( RNS2Socket rns2Socket, RNS2_SendParameters *sendParameters, const char *file, unsigned int line ); +}; +#endif + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#if defined(_WIN32) + +class RAK_DLL_EXPORT SocketLayerOverride +{ +public: + SocketLayerOverride() {} + virtual ~SocketLayerOverride() {} + + /// Called when SendTo would otherwise occur. + virtual int RakNetSendTo( const char *data, int length, const SystemAddress &systemAddress )=0; + + /// Called when RecvFrom would otherwise occur. Return number of bytes read. Write data into dataOut + // Return -1 to use RakNet's normal recvfrom, 0 to abort RakNet's normal recvfrom, and positive to return data + virtual int RakNetRecvFrom( char dataOut[ MAXIMUM_MTU_SIZE ], SystemAddress *senderOut, bool calledFromMainThread )=0; +}; + +class RNS2_Windows : public RNS2_Berkley, public RNS2_Windows_Linux_360 +{ +public: + RNS2_Windows(); + virtual ~RNS2_Windows(); + RNS2BindResult Bind( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ); + RNS2SendResult Send( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ); + void SetSocketLayerOverride(SocketLayerOverride *_slo); + SocketLayerOverride* GetSocketLayerOverride(void); + // ----------- STATICS ------------ + static void GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); +protected: + static void GetMyIPIPV4( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); + static void GetMyIPIPV4And6( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); + SocketLayerOverride *slo; +}; + +#else +class RNS2_Linux : public RNS2_Berkley, public RNS2_Windows_Linux_360 +{ +public: + RNS2BindResult Bind( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ); + RNS2SendResult Send( RNS2_SendParameters *sendParameters, const char *file, unsigned int line ); + + // ----------- STATICS ------------ + static void GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); +protected: + static void GetMyIPIPV4( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); + static void GetMyIPIPV4And6( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); +}; + +#endif // Linux + +#endif // #elif !defined(WINDOWS_STORE_RT) + +} // namespace RakNet + diff --git a/Source/RakNetSocket2_Berkley.cpp b/Source/RakNetSocket2_Berkley.cpp index 852e65c36..43714ca1a 100644 --- a/Source/RakNetSocket2_Berkley.cpp +++ b/Source/RakNetSocket2_Berkley.cpp @@ -1,554 +1,554 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "EmptyHeader.h" - -#ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS - -#ifndef RAKNETSOCKET2_BERKLEY_CPP -#define RAKNETSOCKET2_BERKLEY_CPP - -// Every platform except windows store 8 and native client supports Berkley sockets -#if !defined(WINDOWS_STORE_RT) && !defined(__native_client__) - -#include "Itoa.h" - -void RNS2_Berkley::SetSocketOptions(void) -{ - int r; - // This doubles the max throughput rate - int sock_opt=1024*256; - r = setsockopt__( rns2Socket, SOL_SOCKET, SO_RCVBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) ); - RakAssert(r==0); - - // Immediate hard close. Don't linger the socket, or recreating the socket quickly on Vista fails. - // Fail with voice and xbox - - sock_opt=0; - r = setsockopt__( rns2Socket, SOL_SOCKET, SO_LINGER, ( char * ) & sock_opt, sizeof ( sock_opt ) ); - // Do not assert, ignore failure - - - - // This doesn't make much difference: 10% maybe - // Not supported on console 2 - sock_opt=1024*16; - r = setsockopt__( rns2Socket, SOL_SOCKET, SO_SNDBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) ); - RakAssert(r==0); - -} - -void RNS2_Berkley::SetNonBlockingSocket(unsigned long nonblocking) -{ -#ifdef _WIN32 - int res = ioctlsocket__( rns2Socket, FIONBIO, &nonblocking ); - RakAssert(res==0); - - - -#else - if (nonblocking) - fcntl( rns2Socket, F_SETFL, O_NONBLOCK ); -#endif -} -void RNS2_Berkley::SetBroadcastSocket(int broadcast) -{ - setsockopt__( rns2Socket, SOL_SOCKET, SO_BROADCAST, ( char * ) & broadcast, sizeof( broadcast ) ); -} -void RNS2_Berkley::SetIPHdrIncl(int ipHdrIncl) -{ - - setsockopt__( rns2Socket, IPPROTO_IP, IP_HDRINCL, ( char * ) & ipHdrIncl, sizeof( ipHdrIncl ) ); - -} -void RNS2_Berkley::SetDoNotFragment( int opt ) -{ - #if defined( IP_DONTFRAGMENT ) - #if defined(_WIN32) && !defined(_DEBUG) - // If this assert hit you improperly linked against WSock32.h - RakAssert(IP_DONTFRAGMENT==14); - #endif - setsockopt__( rns2Socket, boundAddress.GetIPPROTO(), IP_DONTFRAGMENT, ( char * ) & opt, sizeof ( opt ) ); - #endif -} - -void RNS2_Berkley::GetSystemAddressIPV4 ( RNS2Socket rns2Socket, SystemAddress *systemAddressOut ) -{ - sockaddr_in sa; - memset(&sa,0,sizeof(sockaddr_in)); - socklen_t len = sizeof(sa); - //int r = - getsockname__(rns2Socket, (sockaddr*)&sa, &len); - systemAddressOut->SetPortNetworkOrder(sa.sin_port); - systemAddressOut->address.addr4.sin_addr.s_addr=sa.sin_addr.s_addr; - - if (systemAddressOut->address.addr4.sin_addr.s_addr == INADDR_ANY) - { - - - - - - systemAddressOut->address.addr4.sin_addr.s_addr=inet_addr__("127.0.0.1"); - - } -} -void RNS2_Berkley::GetSystemAddressIPV4And6 ( RNS2Socket rns2Socket, SystemAddress *systemAddressOut ) -{ -#if RAKNET_SUPPORT_IPV6==1 - - socklen_t slen; - sockaddr_storage ss; - slen = sizeof(ss); - - if ( getsockname__(rns2Socket, (struct sockaddr *)&ss, &slen)!=0) - { -#if defined(_WIN32) && defined(_DEBUG) - DWORD dwIOError = GetLastError(); - LPVOID messageBuffer; - FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language - ( LPTSTR ) & messageBuffer, 0, NULL ); - // something has gone wrong here... - RAKNET_DEBUG_PRINTF( "getsockname failed:Error code - %d\n%s", dwIOError, messageBuffer ); - - //Free the buffer. - LocalFree( messageBuffer ); -#endif - systemAddressOut->FromString(0); - return; - } - - if (ss.ss_family==AF_INET) - { - memcpy(&systemAddressOut->address.addr4,(sockaddr_in *)&ss,sizeof(sockaddr_in)); - systemAddressOut->debugPort=ntohs(systemAddressOut->address.addr4.sin_port); - - uint32_t zero = 0; - if (memcmp(&systemAddressOut->address.addr4.sin_addr.s_addr, &zero, sizeof(zero))==0) - systemAddressOut->SetToLoopback(4); - // systemAddressOut->address.addr4.sin_port=ntohs(systemAddressOut->address.addr4.sin_port); - } - else - { - memcpy(&systemAddressOut->address.addr6,(sockaddr_in6 *)&ss,sizeof(sockaddr_in6)); - systemAddressOut->debugPort=ntohs(systemAddressOut->address.addr6.sin6_port); - - char zero[16]; - memset(zero,0,sizeof(zero)); - if (memcmp(&systemAddressOut->address.addr4.sin_addr.s_addr, &zero, sizeof(zero))==0) - systemAddressOut->SetToLoopback(6); - - // systemAddressOut->address.addr6.sin6_port=ntohs(systemAddressOut->address.addr6.sin6_port); - } - -#else - (void) rns2Socket; - (void) systemAddressOut; - return; -#endif -} - -#ifdef _MSC_VER -#pragma warning( disable : 4702 ) // warning C4702: unreachable code -#endif -RNS2BindResult RNS2_Berkley::BindSharedIPV4( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ) { - - (void) file; - (void) line; - - int ret; - memset(&boundAddress.address.addr4,0,sizeof(sockaddr_in)); - boundAddress.address.addr4.sin_port = htons( bindParameters->port ); - rns2Socket = (int) socket__( bindParameters->addressFamily, bindParameters->type, bindParameters->protocol ); - if (rns2Socket == -1) - return BR_FAILED_TO_BIND_SOCKET; - - SetSocketOptions(); - SetNonBlockingSocket(bindParameters->nonBlockingSocket); - SetBroadcastSocket(bindParameters->setBroadcast); - SetIPHdrIncl(bindParameters->setIPHdrIncl); - - // Fill in the rest of the address structure - boundAddress.address.addr4.sin_family = AF_INET; - - - - - - if (bindParameters->hostAddress && bindParameters->hostAddress[0]) - { - - - - - - boundAddress.address.addr4.sin_addr.s_addr = inet_addr__( bindParameters->hostAddress ); - - } - else - { - // RAKNET_DEBUG_PRINTF("Binding any on port %i\n", port); - boundAddress.address.addr4.sin_addr.s_addr = INADDR_ANY; - } - - - - - - // bind our name to the socket - ret = bind__( rns2Socket, ( struct sockaddr * ) &boundAddress.address.addr4, sizeof( boundAddress.address.addr4 ) ); - - if ( ret <= -1 ) - { - - - - - - - - -#if defined(_WIN32) - closesocket__(rns2Socket); - return BR_FAILED_TO_BIND_SOCKET; -#elif (defined(__GNUC__) || defined(__GCCXML__) ) && !defined(_WIN32) - closesocket__(rns2Socket); - switch (ret) - { - case EBADF: - RAKNET_DEBUG_PRINTF("bind__(): sockfd is not a valid descriptor.\n"); break; - - case ENOTSOCK: - RAKNET_DEBUG_PRINTF("bind__(): Argument is a descriptor for a file, not a socket.\n"); break; - - case EINVAL: - RAKNET_DEBUG_PRINTF("bind__(): The addrlen is wrong, or the socket was not in the AF_UNIX family.\n"); break; - case EROFS: - RAKNET_DEBUG_PRINTF("bind__(): The socket inode would reside on a read-only file system.\n"); break; - case EFAULT: - RAKNET_DEBUG_PRINTF("bind__(): my_addr points outside the user's accessible address space.\n"); break; - case ENAMETOOLONG: - RAKNET_DEBUG_PRINTF("bind__(): my_addr is too long.\n"); break; - case ENOENT: - RAKNET_DEBUG_PRINTF("bind__(): The file does not exist.\n"); break; - case ENOMEM: - RAKNET_DEBUG_PRINTF("bind__(): Insufficient kernel memory was available.\n"); break; - case ENOTDIR: - RAKNET_DEBUG_PRINTF("bind__(): A component of the path prefix is not a directory.\n"); break; - case EACCES: - // Port reserved on PS4 - RAKNET_DEBUG_PRINTF("bind__(): Search permission is denied on a component of the path prefix.\n"); break; - - case ELOOP: - RAKNET_DEBUG_PRINTF("bind__(): Too many symbolic links were encountered in resolving my_addr.\n"); break; - - default: - RAKNET_DEBUG_PRINTF("Unknown bind__() error %i.\n", ret); break; - } -#endif - - return BR_FAILED_TO_BIND_SOCKET; - } - - GetSystemAddressIPV4(rns2Socket, &boundAddress ); - - return BR_SUCCESS; - -} -RNS2BindResult RNS2_Berkley::BindSharedIPV4And6( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ) { - - (void) file; - (void) line; - (void) bindParameters; - -#if RAKNET_SUPPORT_IPV6==1 - - int ret=0; - struct addrinfo hints; - struct addrinfo *servinfo=0, *aip; // will point to the results - PrepareAddrInfoHints2(&hints); - hints.ai_family=bindParameters->addressFamily; - char portStr[32]; - Itoa(bindParameters->port,portStr,10); - - - // On Ubuntu, "" returns "No address associated with hostname" while 0 works. - if (bindParameters->hostAddress && - (_stricmp(bindParameters->hostAddress,"UNASSIGNED_SYSTEM_ADDRESS")==0 || bindParameters->hostAddress[0]==0)) - { - getaddrinfo(0, portStr, &hints, &servinfo); - } - else - { - getaddrinfo(bindParameters->hostAddress, portStr, &hints, &servinfo); - } - - // Try all returned addresses until one works - for (aip = servinfo; aip != NULL; aip = aip->ai_next) - { - // Open socket. The address type depends on what - // getaddrinfo() gave us. - rns2Socket = socket__(aip->ai_family, aip->ai_socktype, aip->ai_protocol); - - if (rns2Socket == -1) - return BR_FAILED_TO_BIND_SOCKET; - - - - - - - - - - - - - - - - - - - - ret = bind__(rns2Socket, aip->ai_addr, (int) aip->ai_addrlen ); - if (ret>=0) - { - // Is this valid? - memcpy(&boundAddress.address.addr6, aip->ai_addr, sizeof(boundAddress.address.addr6)); - - freeaddrinfo(servinfo); // free the linked-list - - SetSocketOptions(); - SetNonBlockingSocket(bindParameters->nonBlockingSocket); - SetBroadcastSocket(bindParameters->setBroadcast); - SetIPHdrIncl(bindParameters->setIPHdrIncl); - - GetSystemAddressIPV4And6( rns2Socket, &boundAddress ); - - return BR_SUCCESS; - } - else - { - closesocket__(rns2Socket); - } - } - - return BR_FAILED_TO_BIND_SOCKET; - -#else -return BR_REQUIRES_RAKNET_SUPPORT_IPV6_DEFINE; -#endif -} - -void RNS2_Berkley::RecvFromBlockingIPV4And6(RNS2RecvStruct *recvFromStruct) -{ -#if RAKNET_SUPPORT_IPV6==1 - - sockaddr_storage their_addr; - sockaddr* sockAddrPtr; - socklen_t sockLen; - socklen_t* socketlenPtr=(socklen_t*) &sockLen; - memset(&their_addr,0,sizeof(their_addr)); - int dataOutSize; - const int flag=0; - - - - - - - - - - - - { - sockLen=sizeof(their_addr); - sockAddrPtr=(sockaddr*) &their_addr; - } - - - - - dataOutSize=MAXIMUM_MTU_SIZE; - - - recvFromStruct->bytesRead = recvfrom__(rns2Socket, recvFromStruct->data, dataOutSize, flag, sockAddrPtr, socketlenPtr ); - -#if defined(_WIN32) && defined(_DEBUG) && !defined(WINDOWS_PHONE_8) - if (recvFromStruct->bytesRead==-1) - { - DWORD dwIOError = GetLastError(); - if (dwIoError != 10035) - { - LPVOID messageBuffer; - FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language - ( LPTSTR ) & messageBuffer, 0, NULL ); - // I see this hit on XP with IPV6 for some reason - RAKNET_DEBUG_PRINTF( "Warning: recvfrom failed:Error code - %d\n%s", dwIOError, messageBuffer ); - LocalFree( messageBuffer ); - } - } -#endif - - - - - - - - - - - if (recvFromStruct->bytesRead<=0) - return; - recvFromStruct->timeRead=RakNet::GetTimeUS(); - - - - - - - - - - { - if (their_addr.ss_family==AF_INET) - { - memcpy(&recvFromStruct->systemAddress.address.addr4,(sockaddr_in *)&their_addr,sizeof(sockaddr_in)); - recvFromStruct->systemAddress.debugPort=ntohs(recvFromStruct->systemAddress.address.addr4.sin_port); - // systemAddressOut->address.addr4.sin_port=ntohs( systemAddressOut->address.addr4.sin_port ); - } - else - { - memcpy(&recvFromStruct->systemAddress.address.addr6,(sockaddr_in6 *)&their_addr,sizeof(sockaddr_in6)); - recvFromStruct->systemAddress.debugPort=ntohs(recvFromStruct->systemAddress.address.addr6.sin6_port); - // systemAddressOut->address.addr6.sin6_port=ntohs( systemAddressOut->address.addr6.sin6_port ); - } - } - - -#else - (void) recvFromStruct; -#endif -} - -void RNS2_Berkley::RecvFromBlockingIPV4(RNS2RecvStruct *recvFromStruct) -{ - sockaddr* sockAddrPtr; - socklen_t sockLen; - socklen_t* socketlenPtr=(socklen_t*) &sockLen; - sockaddr_in sa; - memset(&sa,0,sizeof(sockaddr_in)); - const int flag=0; - - - - - - - - - - - - - - - - - - - - - - { - sockLen=sizeof(sa); - sa.sin_family = AF_INET; - sa.sin_port=0; - sockAddrPtr=(sockaddr*) &sa; - } - - recvFromStruct->bytesRead = recvfrom__( GetSocket(), recvFromStruct->data, sizeof(recvFromStruct->data), flag, sockAddrPtr, socketlenPtr ); - - - - - - - - - - if (recvFromStruct->bytesRead<=0) - { - /* - DWORD dwIOError = WSAGetLastError(); - - if ( dwIOError == WSAECONNRESET ) - { -#if defined(_DEBUG) - RAKNET_DEBUG_PRINTF( "A previous send operation resulted in an ICMP Port Unreachable message.\n" ); -#endif - - } - else if ( dwIOError != WSAEWOULDBLOCK && dwIOError != WSAEADDRNOTAVAIL) - { -#if defined(_WIN32) && !defined(_XBOX) && !defined(_XBOX_720_COMPILE_AS_WINDOWS) && !defined(X360) && defined(_DEBUG) && !defined(_XBOX_720_COMPILE_AS_WINDOWS) && !defined(WINDOWS_PHONE_8) - LPVOID messageBuffer; - FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language - ( LPTSTR ) & messageBuffer, 0, NULL ); - // something has gone wrong here... - RAKNET_DEBUG_PRINTF( "sendto failed:Error code - %d\n%s", dwIOError, messageBuffer ); - - //Free the buffer. - LocalFree( messageBuffer ); -#endif - } - */ - - return; - } - recvFromStruct->timeRead=RakNet::GetTimeUS(); - - - - - - - - - - { - - recvFromStruct->systemAddress.SetPortNetworkOrder( sa.sin_port ); - recvFromStruct->systemAddress.address.addr4.sin_addr.s_addr=sa.sin_addr.s_addr; - } - - // printf("--- Got %i bytes from %s\n", recvFromStruct->bytesRead, recvFromStruct->systemAddress.ToString()); -} - -void RNS2_Berkley::RecvFromBlocking(RNS2RecvStruct *recvFromStruct) -{ -#if RAKNET_SUPPORT_IPV6==1 - return RecvFromBlockingIPV4And6(recvFromStruct); -#else - return RecvFromBlockingIPV4(recvFromStruct); -#endif -} - -#endif // !defined(WINDOWS_STORE_RT) && !defined(__native_client__) - -#endif // file header - -#endif // #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "EmptyHeader.h" + +#ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS + +#ifndef RAKNETSOCKET2_BERKLEY_CPP +#define RAKNETSOCKET2_BERKLEY_CPP + +// Every platform except windows store 8 and native client supports Berkley sockets +#if !defined(WINDOWS_STORE_RT) && !defined(__native_client__) + +#include "Itoa.h" + +void RNS2_Berkley::SetSocketOptions(void) +{ + int r; + // This doubles the max throughput rate + int sock_opt=1024*256; + r = setsockopt__( rns2Socket, SOL_SOCKET, SO_RCVBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) ); + RakAssert(r==0); + + // Immediate hard close. Don't linger the socket, or recreating the socket quickly on Vista fails. + // Fail with voice and xbox + + sock_opt=0; + r = setsockopt__( rns2Socket, SOL_SOCKET, SO_LINGER, ( char * ) & sock_opt, sizeof ( sock_opt ) ); + // Do not assert, ignore failure + + + + // This doesn't make much difference: 10% maybe + // Not supported on console 2 + sock_opt=1024*16; + r = setsockopt__( rns2Socket, SOL_SOCKET, SO_SNDBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) ); + RakAssert(r==0); + +} + +void RNS2_Berkley::SetNonBlockingSocket(unsigned long nonblocking) +{ +#ifdef _WIN32 + int res = ioctlsocket__( rns2Socket, FIONBIO, &nonblocking ); + RakAssert(res==0); + + + +#else + if (nonblocking) + fcntl( rns2Socket, F_SETFL, O_NONBLOCK ); +#endif +} +void RNS2_Berkley::SetBroadcastSocket(int broadcast) +{ + setsockopt__( rns2Socket, SOL_SOCKET, SO_BROADCAST, ( char * ) & broadcast, sizeof( broadcast ) ); +} +void RNS2_Berkley::SetIPHdrIncl(int ipHdrIncl) +{ + + setsockopt__( rns2Socket, IPPROTO_IP, IP_HDRINCL, ( char * ) & ipHdrIncl, sizeof( ipHdrIncl ) ); + +} +void RNS2_Berkley::SetDoNotFragment( int opt ) +{ + #if defined( IP_DONTFRAGMENT ) + #if defined(_WIN32) && !defined(_DEBUG) + // If this assert hit you improperly linked against WSock32.h + RakAssert(IP_DONTFRAGMENT==14); + #endif + setsockopt__( rns2Socket, boundAddress.GetIPPROTO(), IP_DONTFRAGMENT, ( char * ) & opt, sizeof ( opt ) ); + #endif +} + +void RNS2_Berkley::GetSystemAddressIPV4 ( RNS2Socket rns2Socket, SystemAddress *systemAddressOut ) +{ + sockaddr_in sa; + memset(&sa,0,sizeof(sockaddr_in)); + socklen_t len = sizeof(sa); + //int r = + getsockname__(rns2Socket, (sockaddr*)&sa, &len); + systemAddressOut->SetPortNetworkOrder(sa.sin_port); + systemAddressOut->address.addr4.sin_addr.s_addr=sa.sin_addr.s_addr; + + if (systemAddressOut->address.addr4.sin_addr.s_addr == INADDR_ANY) + { + + + + + + systemAddressOut->address.addr4.sin_addr.s_addr=inet_addr__("127.0.0.1"); + + } +} +void RNS2_Berkley::GetSystemAddressIPV4And6 ( RNS2Socket rns2Socket, SystemAddress *systemAddressOut ) +{ +#if RAKNET_SUPPORT_IPV6==1 + + socklen_t slen; + sockaddr_storage ss; + slen = sizeof(ss); + + if ( getsockname__(rns2Socket, (struct sockaddr *)&ss, &slen)!=0) + { +#if defined(_WIN32) && defined(_DEBUG) + DWORD dwIOError = GetLastError(); + LPVOID messageBuffer; + FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language + ( LPTSTR ) & messageBuffer, 0, nullptr ); + // something has gone wrong here... + RAKNET_DEBUG_PRINTF( "getsockname failed:Error code - %d\n%s", dwIOError, messageBuffer ); + + //Free the buffer. + LocalFree( messageBuffer ); +#endif + systemAddressOut->FromString(0); + return; + } + + if (ss.ss_family==AF_INET) + { + memcpy(&systemAddressOut->address.addr4,(sockaddr_in *)&ss,sizeof(sockaddr_in)); + systemAddressOut->debugPort=ntohs(systemAddressOut->address.addr4.sin_port); + + uint32_t zero = 0; + if (memcmp(&systemAddressOut->address.addr4.sin_addr.s_addr, &zero, sizeof(zero))==0) + systemAddressOut->SetToLoopback(4); + // systemAddressOut->address.addr4.sin_port=ntohs(systemAddressOut->address.addr4.sin_port); + } + else + { + memcpy(&systemAddressOut->address.addr6,(sockaddr_in6 *)&ss,sizeof(sockaddr_in6)); + systemAddressOut->debugPort=ntohs(systemAddressOut->address.addr6.sin6_port); + + char zero[16]; + memset(zero,0,sizeof(zero)); + if (memcmp(&systemAddressOut->address.addr4.sin_addr.s_addr, &zero, sizeof(zero))==0) + systemAddressOut->SetToLoopback(6); + + // systemAddressOut->address.addr6.sin6_port=ntohs(systemAddressOut->address.addr6.sin6_port); + } + +#else + (void) rns2Socket; + (void) systemAddressOut; + return; +#endif +} + +#ifdef _MSC_VER +#pragma warning( disable : 4702 ) // warning C4702: unreachable code +#endif +RNS2BindResult RNS2_Berkley::BindSharedIPV4( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ) { + + (void) file; + (void) line; + + int ret; + memset(&boundAddress.address.addr4,0,sizeof(sockaddr_in)); + boundAddress.address.addr4.sin_port = htons( bindParameters->port ); + rns2Socket = (int) socket__( bindParameters->addressFamily, bindParameters->type, bindParameters->protocol ); + if (rns2Socket == -1) + return BR_FAILED_TO_BIND_SOCKET; + + SetSocketOptions(); + SetNonBlockingSocket(bindParameters->nonBlockingSocket); + SetBroadcastSocket(bindParameters->setBroadcast); + SetIPHdrIncl(bindParameters->setIPHdrIncl); + + // Fill in the rest of the address structure + boundAddress.address.addr4.sin_family = AF_INET; + + + + + + if (bindParameters->hostAddress && bindParameters->hostAddress[0]) + { + + + + + + boundAddress.address.addr4.sin_addr.s_addr = inet_addr__( bindParameters->hostAddress ); + + } + else + { + // RAKNET_DEBUG_PRINTF("Binding any on port %i\n", port); + boundAddress.address.addr4.sin_addr.s_addr = INADDR_ANY; + } + + + + + + // bind our name to the socket + ret = bind__( rns2Socket, ( struct sockaddr * ) &boundAddress.address.addr4, sizeof( boundAddress.address.addr4 ) ); + + if ( ret <= -1 ) + { + + + + + + + + +#if defined(_WIN32) + closesocket__(rns2Socket); + return BR_FAILED_TO_BIND_SOCKET; +#elif (defined(__GNUC__) || defined(__GCCXML__) ) && !defined(_WIN32) + closesocket__(rns2Socket); + switch (ret) + { + case EBADF: + RAKNET_DEBUG_PRINTF("bind__(): sockfd is not a valid descriptor.\n"); break; + + case ENOTSOCK: + RAKNET_DEBUG_PRINTF("bind__(): Argument is a descriptor for a file, not a socket.\n"); break; + + case EINVAL: + RAKNET_DEBUG_PRINTF("bind__(): The addrlen is wrong, or the socket was not in the AF_UNIX family.\n"); break; + case EROFS: + RAKNET_DEBUG_PRINTF("bind__(): The socket inode would reside on a read-only file system.\n"); break; + case EFAULT: + RAKNET_DEBUG_PRINTF("bind__(): my_addr points outside the user's accessible address space.\n"); break; + case ENAMETOOLONG: + RAKNET_DEBUG_PRINTF("bind__(): my_addr is too long.\n"); break; + case ENOENT: + RAKNET_DEBUG_PRINTF("bind__(): The file does not exist.\n"); break; + case ENOMEM: + RAKNET_DEBUG_PRINTF("bind__(): Insufficient kernel memory was available.\n"); break; + case ENOTDIR: + RAKNET_DEBUG_PRINTF("bind__(): A component of the path prefix is not a directory.\n"); break; + case EACCES: + // Port reserved on PS4 + RAKNET_DEBUG_PRINTF("bind__(): Search permission is denied on a component of the path prefix.\n"); break; + + case ELOOP: + RAKNET_DEBUG_PRINTF("bind__(): Too many symbolic links were encountered in resolving my_addr.\n"); break; + + default: + RAKNET_DEBUG_PRINTF("Unknown bind__() error %i.\n", ret); break; + } +#endif + + return BR_FAILED_TO_BIND_SOCKET; + } + + GetSystemAddressIPV4(rns2Socket, &boundAddress ); + + return BR_SUCCESS; + +} +RNS2BindResult RNS2_Berkley::BindSharedIPV4And6( RNS2_BerkleyBindParameters *bindParameters, const char *file, unsigned int line ) { + + (void) file; + (void) line; + (void) bindParameters; + +#if RAKNET_SUPPORT_IPV6==1 + + int ret=0; + struct addrinfo hints; + struct addrinfo *servinfo=0, *aip; // will point to the results + PrepareAddrInfoHints2(&hints); + hints.ai_family=bindParameters->addressFamily; + char portStr[32]; + Itoa(bindParameters->port,portStr,10); + + + // On Ubuntu, "" returns "No address associated with hostname" while 0 works. + if (bindParameters->hostAddress && + (_stricmp(bindParameters->hostAddress,"UNASSIGNED_SYSTEM_ADDRESS")==0 || bindParameters->hostAddress[0]==0)) + { + getaddrinfo(0, portStr, &hints, &servinfo); + } + else + { + getaddrinfo(bindParameters->hostAddress, portStr, &hints, &servinfo); + } + + // Try all returned addresses until one works + for (aip = servinfo; aip != nullptr; aip = aip->ai_next) + { + // Open socket. The address type depends on what + // getaddrinfo() gave us. + rns2Socket = socket__(aip->ai_family, aip->ai_socktype, aip->ai_protocol); + + if (rns2Socket == -1) + return BR_FAILED_TO_BIND_SOCKET; + + + + + + + + + + + + + + + + + + + + ret = bind__(rns2Socket, aip->ai_addr, (int) aip->ai_addrlen ); + if (ret>=0) + { + // Is this valid? + memcpy(&boundAddress.address.addr6, aip->ai_addr, sizeof(boundAddress.address.addr6)); + + freeaddrinfo(servinfo); // free the linked-list + + SetSocketOptions(); + SetNonBlockingSocket(bindParameters->nonBlockingSocket); + SetBroadcastSocket(bindParameters->setBroadcast); + SetIPHdrIncl(bindParameters->setIPHdrIncl); + + GetSystemAddressIPV4And6( rns2Socket, &boundAddress ); + + return BR_SUCCESS; + } + else + { + closesocket__(rns2Socket); + } + } + + return BR_FAILED_TO_BIND_SOCKET; + +#else +return BR_REQUIRES_RAKNET_SUPPORT_IPV6_DEFINE; +#endif +} + +void RNS2_Berkley::RecvFromBlockingIPV4And6(RNS2RecvStruct *recvFromStruct) +{ +#if RAKNET_SUPPORT_IPV6==1 + + sockaddr_storage their_addr; + sockaddr* sockAddrPtr; + socklen_t sockLen; + socklen_t* socketlenPtr=(socklen_t*) &sockLen; + memset(&their_addr,0,sizeof(their_addr)); + int dataOutSize; + const int flag=0; + + + + + + + + + + + + { + sockLen=sizeof(their_addr); + sockAddrPtr=(sockaddr*) &their_addr; + } + + + + + dataOutSize=MAXIMUM_MTU_SIZE; + + + recvFromStruct->bytesRead = recvfrom__(rns2Socket, recvFromStruct->data, dataOutSize, flag, sockAddrPtr, socketlenPtr ); + +#if defined(_WIN32) && defined(_DEBUG) && !defined(WINDOWS_PHONE_8) + if (recvFromStruct->bytesRead==-1) + { + DWORD dwIOError = GetLastError(); + if (dwIoError != 10035) + { + LPVOID messageBuffer; + FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language + ( LPTSTR ) & messageBuffer, 0, nullptr ); + // I see this hit on XP with IPV6 for some reason + RAKNET_DEBUG_PRINTF( "Warning: recvfrom failed:Error code - %d\n%s", dwIOError, messageBuffer ); + LocalFree( messageBuffer ); + } + } +#endif + + + + + + + + + + + if (recvFromStruct->bytesRead<=0) + return; + recvFromStruct->timeRead=RakNet::GetTimeUS(); + + + + + + + + + + { + if (their_addr.ss_family==AF_INET) + { + memcpy(&recvFromStruct->systemAddress.address.addr4,(sockaddr_in *)&their_addr,sizeof(sockaddr_in)); + recvFromStruct->systemAddress.debugPort=ntohs(recvFromStruct->systemAddress.address.addr4.sin_port); + // systemAddressOut->address.addr4.sin_port=ntohs( systemAddressOut->address.addr4.sin_port ); + } + else + { + memcpy(&recvFromStruct->systemAddress.address.addr6,(sockaddr_in6 *)&their_addr,sizeof(sockaddr_in6)); + recvFromStruct->systemAddress.debugPort=ntohs(recvFromStruct->systemAddress.address.addr6.sin6_port); + // systemAddressOut->address.addr6.sin6_port=ntohs( systemAddressOut->address.addr6.sin6_port ); + } + } + + +#else + (void) recvFromStruct; +#endif +} + +void RNS2_Berkley::RecvFromBlockingIPV4(RNS2RecvStruct *recvFromStruct) +{ + sockaddr* sockAddrPtr; + socklen_t sockLen; + socklen_t* socketlenPtr=(socklen_t*) &sockLen; + sockaddr_in sa; + memset(&sa,0,sizeof(sockaddr_in)); + const int flag=0; + + + + + + + + + + + + + + + + + + + + + + { + sockLen=sizeof(sa); + sa.sin_family = AF_INET; + sa.sin_port=0; + sockAddrPtr=(sockaddr*) &sa; + } + + recvFromStruct->bytesRead = recvfrom__( GetSocket(), recvFromStruct->data, sizeof(recvFromStruct->data), flag, sockAddrPtr, socketlenPtr ); + + + + + + + + + + if (recvFromStruct->bytesRead<=0) + { + /* + DWORD dwIOError = WSAGetLastError(); + + if ( dwIOError == WSAECONNRESET ) + { +#if defined(_DEBUG) + RAKNET_DEBUG_PRINTF( "A previous send operation resulted in an ICMP Port Unreachable message.\n" ); +#endif + + } + else if ( dwIOError != WSAEWOULDBLOCK && dwIOError != WSAEADDRNOTAVAIL) + { +#if defined(_WIN32) && !defined(_XBOX) && !defined(_XBOX_720_COMPILE_AS_WINDOWS) && !defined(X360) && defined(_DEBUG) && !defined(_XBOX_720_COMPILE_AS_WINDOWS) && !defined(WINDOWS_PHONE_8) + LPVOID messageBuffer; + FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language + ( LPTSTR ) & messageBuffer, 0, nullptr ); + // something has gone wrong here... + RAKNET_DEBUG_PRINTF( "sendto failed:Error code - %d\n%s", dwIOError, messageBuffer ); + + //Free the buffer. + LocalFree( messageBuffer ); +#endif + } + */ + + return; + } + recvFromStruct->timeRead=RakNet::GetTimeUS(); + + + + + + + + + + { + + recvFromStruct->systemAddress.SetPortNetworkOrder( sa.sin_port ); + recvFromStruct->systemAddress.address.addr4.sin_addr.s_addr=sa.sin_addr.s_addr; + } + + // printf("--- Got %i bytes from %s\n", recvFromStruct->bytesRead, recvFromStruct->systemAddress.ToString()); +} + +void RNS2_Berkley::RecvFromBlocking(RNS2RecvStruct *recvFromStruct) +{ +#if RAKNET_SUPPORT_IPV6==1 + return RecvFromBlockingIPV4And6(recvFromStruct); +#else + return RecvFromBlockingIPV4(recvFromStruct); +#endif +} + +#endif // !defined(WINDOWS_STORE_RT) && !defined(__native_client__) + +#endif // file header + +#endif // #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS diff --git a/Source/RakNetSocket2_Berkley_NativeClient.cpp b/Source/RakNetSocket2_Berkley_NativeClient.cpp index 58106c549..affb00029 100644 --- a/Source/RakNetSocket2_Berkley_NativeClient.cpp +++ b/Source/RakNetSocket2_Berkley_NativeClient.cpp @@ -1,114 +1,114 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "EmptyHeader.h" - -#ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS - -#ifndef RAKNETSOCKET2_BERKLEY_NATIVE_CLIENT_CPP -#define RAKNETSOCKET2_BERKLEY_NATIVE_CLIENT_CPP - -// Every platform except windows store 8 and native client supports Berkley sockets -#if !defined(WINDOWS_STORE_RT) - -#include "Itoa.h" - -// Shared on most platforms, but excluded from the listed - - -void DomainNameToIP_Berkley_IPV4And6( const char *domainName, char ip[65] ) -{ -#if RAKNET_SUPPORT_IPV6==1 - struct addrinfo hints, *res, *p; - int status; - memset(&hints, 0, sizeof hints); - hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version - hints.ai_socktype = SOCK_DGRAM; - - if ((status = getaddrinfo(domainName, NULL, &hints, &res)) != 0) { - memset(ip, 0, sizeof(ip)); - return; - } - - p=res; -// for(p = res;p != NULL; p = p->ai_next) { - void *addr; -// char *ipver; - - // get the pointer to the address itself, - // different fields in IPv4 and IPv6: - if (p->ai_family == AF_INET) - { - struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; - addr = &(ipv4->sin_addr); - strcpy(ip, inet_ntoa( ipv4->sin_addr )); - } - else - { - // TODO - test - struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr; - addr = &(ipv6->sin6_addr); - // inet_ntop function does not exist on windows - // http://www.mail-archive.com/users@ipv6.org/msg02107.html - getnameinfo((struct sockaddr *)ipv6, sizeof(struct sockaddr_in6), ip, 1, NULL, 0, NI_NUMERICHOST); - } - freeaddrinfo(res); // free the linked list -// } -#else - (void) domainName; - (void) ip; -#endif // #if RAKNET_SUPPORT_IPV6==1 -} - - -void DomainNameToIP_Berkley_IPV4( const char *domainName, char ip[65] ) -{ - static struct in_addr addr; - memset(&addr,0,sizeof(in_addr)); - - // Use inet_addr instead? What is the difference? - struct hostent * phe = gethostbyname( domainName ); - - if ( phe == 0 || phe->h_addr_list[ 0 ] == 0 ) - { - //cerr << "Yow! Bad host lookup." << endl; - memset(ip,0,65*sizeof(char)); - return; - } - - if (phe->h_addr_list[ 0 ]==0) - { - memset(ip,0,65*sizeof(char)); - return; - } - - memcpy( &addr, phe->h_addr_list[ 0 ], sizeof( struct in_addr ) ); - strcpy(ip, inet_ntoa( addr )); -} - - - -void DomainNameToIP_Berkley( const char *domainName, char ip[65] ) -{ -#if RAKNET_SUPPORT_IPV6==1 - return DomainNameToIP_Berkley_IPV4And6(domainName, ip); -#else - return DomainNameToIP_Berkley_IPV4(domainName, ip); -#endif -} - - - - -#endif // !defined(WINDOWS_STORE_RT) && !defined(__native_client__) - -#endif // file header - -#endif // #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "EmptyHeader.h" + +#ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS + +#ifndef RAKNETSOCKET2_BERKLEY_NATIVE_CLIENT_CPP +#define RAKNETSOCKET2_BERKLEY_NATIVE_CLIENT_CPP + +// Every platform except windows store 8 and native client supports Berkley sockets +#if !defined(WINDOWS_STORE_RT) + +#include "Itoa.h" + +// Shared on most platforms, but excluded from the listed + + +void DomainNameToIP_Berkley_IPV4And6( const char *domainName, char ip[65] ) +{ +#if RAKNET_SUPPORT_IPV6==1 + struct addrinfo hints, *res, *p; + int status; + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version + hints.ai_socktype = SOCK_DGRAM; + + if ((status = getaddrinfo(domainName, nullptr, &hints, &res)) != 0) { + memset(ip, 0, sizeof(ip)); + return; + } + + p=res; +// for(p = res;p != nullptr; p = p->ai_next) { + void *addr; +// char *ipver; + + // get the pointer to the address itself, + // different fields in IPv4 and IPv6: + if (p->ai_family == AF_INET) + { + struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; + addr = &(ipv4->sin_addr); + strcpy(ip, inet_ntoa( ipv4->sin_addr )); + } + else + { + // TODO - test + struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr; + addr = &(ipv6->sin6_addr); + // inet_ntop function does not exist on windows + // http://www.mail-archive.com/users@ipv6.org/msg02107.html + getnameinfo((struct sockaddr *)ipv6, sizeof(struct sockaddr_in6), ip, 1, nullptr, 0, NI_NUMERICHOST); + } + freeaddrinfo(res); // free the linked list +// } +#else + (void) domainName; + (void) ip; +#endif // #if RAKNET_SUPPORT_IPV6==1 +} + + +void DomainNameToIP_Berkley_IPV4( const char *domainName, char ip[65] ) +{ + static struct in_addr addr; + memset(&addr,0,sizeof(in_addr)); + + // Use inet_addr instead? What is the difference? + struct hostent * phe = gethostbyname( domainName ); + + if ( phe == 0 || phe->h_addr_list[ 0 ] == 0 ) + { + //cerr << "Yow! Bad host lookup." << endl; + memset(ip,0,65*sizeof(char)); + return; + } + + if (phe->h_addr_list[ 0 ]==0) + { + memset(ip,0,65*sizeof(char)); + return; + } + + memcpy( &addr, phe->h_addr_list[ 0 ], sizeof( struct in_addr ) ); + strcpy(ip, inet_ntoa( addr )); +} + + + +void DomainNameToIP_Berkley( const char *domainName, char ip[65] ) +{ +#if RAKNET_SUPPORT_IPV6==1 + return DomainNameToIP_Berkley_IPV4And6(domainName, ip); +#else + return DomainNameToIP_Berkley_IPV4(domainName, ip); +#endif +} + + + + +#endif // !defined(WINDOWS_STORE_RT) && !defined(__native_client__) + +#endif // file header + +#endif // #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS diff --git a/Source/RakNetSocket2_NativeClient.cpp b/Source/RakNetSocket2_NativeClient.cpp index 3d032c5bc..27803ea4d 100644 --- a/Source/RakNetSocket2_NativeClient.cpp +++ b/Source/RakNetSocket2_NativeClient.cpp @@ -1,169 +1,169 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "EmptyHeader.h" - -#ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS - -#ifndef RAKNETSOCKET2_NATIVE_CLIENT_CPP -#define RAKNETSOCKET2_NATIVE_CLIENT_CPP - -#if defined(__native_client__) - -using namespace pp; - -RNS2BindResult RNS2_NativeClient::Bind( NativeClientBindParameters *bindParameters, const char *file, unsigned int line ) -{ - memcpy(&binding, bindParameters, sizeof(NativeClientBindParameters)); - - if(Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4)) - { - rns2Socket = ((PPB_UDPSocket_Private_0_4*) Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4))->Create(bindParameters->nativeClientInstance); - RAKNET_DEBUG_PRINTF("CreateChromeSocket(%d,%s,0x%08x,%d) ==> 0x%08x\n", bindParameters->port, bindParameters->forceHostAddress?bindParameters->forceHostAddress:"(null)",bindParameters->nativeClientInstance,bindParameters->is_ipv6, rns2Socket); - - // Enable the broadcast feature on the socket (must happen before the - // bind call) - ((PPB_UDPSocket_Private_0_4*) pp::Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4))->SetSocketFeature(rns2Socket, PP_UDPSOCKETFEATURE_BROADCAST, PP_MakeBool(PP_TRUE)); - - PP_NetAddress_Private client_addr; - uint8_t ipv6[16], ipv4[4]; - if (bindParameters->forceHostAddress) - { - unsigned int ipIdx=0; - char * pch; - pch = strtok ((char*) bindParameters->forceHostAddress,"."); - if (bindParameters->is_ipv6) - { - while (pch != NULL && ipIdx<16) - { - ipv6[ipIdx++]=atoi(pch); - pch = strtok (NULL, "."); - } - NetAddressPrivate::CreateFromIPv6Address(ipv6,0,bindParameters->port,&client_addr); - } - else - { - while (pch != NULL && ipIdx<4) - { - ipv4[ipIdx++]=atoi(pch); - pch = strtok (NULL, "."); - } - NetAddressPrivate::CreateFromIPv4Address(ipv4,bindParameters->port,&client_addr); - } - } - else - { - NetAddressPrivate::GetAnyAddress(bindParameters->is_ipv6, &client_addr); - NetAddressPrivate::ReplacePort(client_addr, bindParameters->port, &client_addr); - } - - bindState = BS_IN_PROGRESS; - - RAKNET_DEBUG_PRINTF("attempting to bind to %s\n", NetAddressPrivate::Describe(client_addr, true).c_str()); - PP_CompletionCallback cc = PP_MakeCompletionCallback(RNS2_NativeClient::onSocketBound, this); - ((PPB_UDPSocket_Private_0_4*) Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4))->Bind(rns2Socket, &client_addr, cc); - return BR_SUCCESS; - } - return BR_FAILED_TO_BIND_SOCKET; -} - -void RNS2_NativeClient::SendImmediate(RNS2_SendParameters_NativeClient *sp) -{ - // Assuming data does not have to remain valid until callback called - PP_NetAddress_Private client_addr; -#if RAKNET_SUPPORT_IPV6==1 - NetAddressPrivate::CreateFromIPv6Address(sp->systemAddress.address.addr6.sin6_addr.u.Byte,0,sp->systemAddress.GetPort(),&client_addr); -#else - NetAddressPrivate::CreateFromIPv4Address((const uint8_t*) &sp->systemAddress.address.addr4.sin_addr,sp->systemAddress.GetPort(),&client_addr); -#endif - - // sp remains in memory until the callback completes - PP_CompletionCallback cc = PP_MakeCompletionCallback(onSendTo, sp); - ((PPB_UDPSocket_Private_0_4*) Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4))->SendTo(sp->socket2->rns2Socket, sp->data, sp->length, &client_addr, cc); -} - -void RNS2_NativeClient::onRecvFrom(void* pData, int32_t dataSize) -{ - RNS2RecvStruct *recvStruct = (RNS2RecvStruct *) pData; - RNS2_NativeClient *socket2 = (RNS2_NativeClient *) recvStruct->socket; - - //any error codes will be given to us in the dataSize value; see pp_errors.h for a list of errors - if(dataSize <=0 || !pData ) - { - // Free data - socket2->eventHandler->DeallocRNS2RecvStruct(recvStruct, _FILE_AND_LINE_); - - // This value indicates failure due to an asynchronous operation being - // interrupted. The most common cause of this error code is destroying - // a resource that still has a callback pending. All callbacks are - // guaranteed to execute, so any callbacks pending on a destroyed - // resource will be issued with PP_ERROR_ABORTED. - if(dataSize==PP_ERROR_ABORTED) - { - RAKNET_DEBUG_PRINTF("onRecvFrom error PP_ERROR_ABORTED, killing recvfrom loop\n"); - } - else - { - RAKNET_DEBUG_PRINTF("onRecvFrom error %d\n", dataSize); - - // Reissue call - socket2->IssueReceiveCall(); - } - - return; - } - - recvStruct->bytesRead=dataSize; - recvStruct->timeRead=RakNet::GetTimeUS(); - - - PP_NetAddress_Private addr; - bool ok=false; - if(((PPB_UDPSocket_Private_0_4*) Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4))->GetRecvFromAddress(socket2->rns2Socket, &addr) == PP_TRUE) - { - PP_NetAddressFamily_Private family = NetAddressPrivate::GetFamily(addr); - if (family == PP_NETADDRESSFAMILY_IPV4) - { - ok = NetAddressPrivate::GetAddress(addr, &recvStruct->systemAddress.address.addr4.sin_addr, sizeof(in_addr)); - } -#if RAKNET_SUPPORT_IPV6==1 - else - { - ok = NetAddressPrivate::GetAddress(addr, &recvStruct->systemAddress.address.addr6.sin6_addr, sizeof(in6_addr)); - } -#endif - } - - if(ok) - { - recvStruct->systemAddress.SetPortHostOrder(pp::NetAddressPrivate::GetPort(addr)); - socket2->binding.eventHandler->OnRNS2Recv(recvStruct); - } - - // Reissue call - socket2->IssueReceiveCall(); -} -void RNS2_NativeClient::IssueReceiveCall(void) -{ - RNS2RecvStruct *recvFromStruct; - recvFromStruct=binding.eventHandler->AllocRNS2RecvStruct(_FILE_AND_LINE_); - if (recvFromStruct != NULL) - { - recvFromStruct->socket=this; - PP_CompletionCallback cc = PP_MakeCompletionCallback(onRecvFrom, recvFromStruct); - ((PPB_UDPSocket_Private_0_4*) Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4))->RecvFrom(rns2Socket, recvFromStruct->data, MAXIMUM_MTU_SIZE, cc); - } -} - -#endif // defined(__native_client__) - -#endif // file header - -#endif // #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "EmptyHeader.h" + +#ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS + +#ifndef RAKNETSOCKET2_NATIVE_CLIENT_CPP +#define RAKNETSOCKET2_NATIVE_CLIENT_CPP + +#if defined(__native_client__) + +using namespace pp; + +RNS2BindResult RNS2_NativeClient::Bind( NativeClientBindParameters *bindParameters, const char *file, unsigned int line ) +{ + memcpy(&binding, bindParameters, sizeof(NativeClientBindParameters)); + + if(Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4)) + { + rns2Socket = ((PPB_UDPSocket_Private_0_4*) Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4))->Create(bindParameters->nativeClientInstance); + RAKNET_DEBUG_PRINTF("CreateChromeSocket(%d,%s,0x%08x,%d) ==> 0x%08x\n", bindParameters->port, bindParameters->forceHostAddress?bindParameters->forceHostAddress:"(null)",bindParameters->nativeClientInstance,bindParameters->is_ipv6, rns2Socket); + + // Enable the broadcast feature on the socket (must happen before the + // bind call) + ((PPB_UDPSocket_Private_0_4*) pp::Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4))->SetSocketFeature(rns2Socket, PP_UDPSOCKETFEATURE_BROADCAST, PP_MakeBool(PP_TRUE)); + + PP_NetAddress_Private client_addr; + uint8_t ipv6[16], ipv4[4]; + if (bindParameters->forceHostAddress) + { + unsigned int ipIdx=0; + char * pch; + pch = strtok ((char*) bindParameters->forceHostAddress,"."); + if (bindParameters->is_ipv6) + { + while (pch != nullptr && ipIdx<16) + { + ipv6[ipIdx++]=atoi(pch); + pch = strtok (nullptr, "."); + } + NetAddressPrivate::CreateFromIPv6Address(ipv6,0,bindParameters->port,&client_addr); + } + else + { + while (pch != nullptr && ipIdx<4) + { + ipv4[ipIdx++]=atoi(pch); + pch = strtok (nullptr, "."); + } + NetAddressPrivate::CreateFromIPv4Address(ipv4,bindParameters->port,&client_addr); + } + } + else + { + NetAddressPrivate::GetAnyAddress(bindParameters->is_ipv6, &client_addr); + NetAddressPrivate::ReplacePort(client_addr, bindParameters->port, &client_addr); + } + + bindState = BS_IN_PROGRESS; + + RAKNET_DEBUG_PRINTF("attempting to bind to %s\n", NetAddressPrivate::Describe(client_addr, true).c_str()); + PP_CompletionCallback cc = PP_MakeCompletionCallback(RNS2_NativeClient::onSocketBound, this); + ((PPB_UDPSocket_Private_0_4*) Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4))->Bind(rns2Socket, &client_addr, cc); + return BR_SUCCESS; + } + return BR_FAILED_TO_BIND_SOCKET; +} + +void RNS2_NativeClient::SendImmediate(RNS2_SendParameters_NativeClient *sp) +{ + // Assuming data does not have to remain valid until callback called + PP_NetAddress_Private client_addr; +#if RAKNET_SUPPORT_IPV6==1 + NetAddressPrivate::CreateFromIPv6Address(sp->systemAddress.address.addr6.sin6_addr.u.Byte,0,sp->systemAddress.GetPort(),&client_addr); +#else + NetAddressPrivate::CreateFromIPv4Address((const uint8_t*) &sp->systemAddress.address.addr4.sin_addr,sp->systemAddress.GetPort(),&client_addr); +#endif + + // sp remains in memory until the callback completes + PP_CompletionCallback cc = PP_MakeCompletionCallback(onSendTo, sp); + ((PPB_UDPSocket_Private_0_4*) Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4))->SendTo(sp->socket2->rns2Socket, sp->data, sp->length, &client_addr, cc); +} + +void RNS2_NativeClient::onRecvFrom(void* pData, int32_t dataSize) +{ + RNS2RecvStruct *recvStruct = (RNS2RecvStruct *) pData; + RNS2_NativeClient *socket2 = (RNS2_NativeClient *) recvStruct->socket; + + //any error codes will be given to us in the dataSize value; see pp_errors.h for a list of errors + if(dataSize <=0 || !pData ) + { + // Free data + socket2->eventHandler->DeallocRNS2RecvStruct(recvStruct, _FILE_AND_LINE_); + + // This value indicates failure due to an asynchronous operation being + // interrupted. The most common cause of this error code is destroying + // a resource that still has a callback pending. All callbacks are + // guaranteed to execute, so any callbacks pending on a destroyed + // resource will be issued with PP_ERROR_ABORTED. + if(dataSize==PP_ERROR_ABORTED) + { + RAKNET_DEBUG_PRINTF("onRecvFrom error PP_ERROR_ABORTED, killing recvfrom loop\n"); + } + else + { + RAKNET_DEBUG_PRINTF("onRecvFrom error %d\n", dataSize); + + // Reissue call + socket2->IssueReceiveCall(); + } + + return; + } + + recvStruct->bytesRead=dataSize; + recvStruct->timeRead=RakNet::GetTimeUS(); + + + PP_NetAddress_Private addr; + bool ok=false; + if(((PPB_UDPSocket_Private_0_4*) Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4))->GetRecvFromAddress(socket2->rns2Socket, &addr) == PP_TRUE) + { + PP_NetAddressFamily_Private family = NetAddressPrivate::GetFamily(addr); + if (family == PP_NETADDRESSFAMILY_IPV4) + { + ok = NetAddressPrivate::GetAddress(addr, &recvStruct->systemAddress.address.addr4.sin_addr, sizeof(in_addr)); + } +#if RAKNET_SUPPORT_IPV6==1 + else + { + ok = NetAddressPrivate::GetAddress(addr, &recvStruct->systemAddress.address.addr6.sin6_addr, sizeof(in6_addr)); + } +#endif + } + + if(ok) + { + recvStruct->systemAddress.SetPortHostOrder(pp::NetAddressPrivate::GetPort(addr)); + socket2->binding.eventHandler->OnRNS2Recv(recvStruct); + } + + // Reissue call + socket2->IssueReceiveCall(); +} +void RNS2_NativeClient::IssueReceiveCall(void) +{ + RNS2RecvStruct *recvFromStruct; + recvFromStruct=binding.eventHandler->AllocRNS2RecvStruct(_FILE_AND_LINE_); + if (recvFromStruct != nullptr) + { + recvFromStruct->socket=this; + PP_CompletionCallback cc = PP_MakeCompletionCallback(onRecvFrom, recvFromStruct); + ((PPB_UDPSocket_Private_0_4*) Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_PRIVATE_INTERFACE_0_4))->RecvFrom(rns2Socket, recvFromStruct->data, MAXIMUM_MTU_SIZE, cc); + } +} + +#endif // defined(__native_client__) + +#endif // file header + +#endif // #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS diff --git a/Source/RakNetSocket2_Windows_Linux.cpp b/Source/RakNetSocket2_Windows_Linux.cpp index a165253f4..e5ffb258d 100644 --- a/Source/RakNetSocket2_Windows_Linux.cpp +++ b/Source/RakNetSocket2_Windows_Linux.cpp @@ -1,121 +1,121 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "EmptyHeader.h" - -#ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS - -#ifndef RAKNETSOCKET2_WINDOWS_LINUX_CPP -#define RAKNETSOCKET2_WINDOWS_LINUX_CPP - -#if !defined(WINDOWS_STORE_RT) && !defined(__native_client__) - -#if RAKNET_SUPPORT_IPV6==1 - -void PrepareAddrInfoHints2(addrinfo *hints) -{ - memset(hints, 0, sizeof (addrinfo)); // make sure the struct is empty - hints->ai_socktype = SOCK_DGRAM; // UDP sockets - hints->ai_flags = AI_PASSIVE; // fill in my IP for me -} - -void GetMyIP_Windows_Linux_IPV4And6( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) -{ - int idx=0; - char ac[ 80 ]; - int err = gethostname( ac, sizeof( ac ) ); - RakAssert(err != -1); - - struct addrinfo hints; - struct addrinfo *servinfo=0, *aip; // will point to the results - PrepareAddrInfoHints2(&hints); - getaddrinfo(ac, "", &hints, &servinfo); - - for (idx=0, aip = servinfo; aip != NULL && idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; aip = aip->ai_next, idx++) - { - if (aip->ai_family == AF_INET) - { - struct sockaddr_in *ipv4 = (struct sockaddr_in *)aip->ai_addr; - memcpy(&addresses[idx].address.addr4,ipv4,sizeof(sockaddr_in)); - } - else - { - struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)aip->ai_addr; - memcpy(&addresses[idx].address.addr4,ipv6,sizeof(sockaddr_in6)); - } - - } - - freeaddrinfo(servinfo); // free the linked-list - - while (idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS) - { - addresses[idx]=UNASSIGNED_SYSTEM_ADDRESS; - idx++; - } -} - -#else - -#if (defined(__GNUC__) || defined(__GCCXML__)) && !defined(__WIN32__) -#include -#endif -void GetMyIP_Windows_Linux_IPV4( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) -{ - - - - int idx=0; - char ac[ 80 ]; - int err = gethostname( ac, sizeof( ac ) ); - (void) err; - RakAssert(err != -1); - - struct hostent *phe = gethostbyname( ac ); - - if ( phe == 0 ) - { - RakAssert(phe!=0); - return ; - } - for ( idx = 0; idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ++idx ) - { - if (phe->h_addr_list[ idx ] == 0) - break; - - memcpy(&addresses[idx].address.addr4.sin_addr,phe->h_addr_list[ idx ],sizeof(struct in_addr)); - } - - while (idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS) - { - addresses[idx]=UNASSIGNED_SYSTEM_ADDRESS; - idx++; - } - -} - -#endif // RAKNET_SUPPORT_IPV6==1 - - -void GetMyIP_Windows_Linux( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) -{ - #if RAKNET_SUPPORT_IPV6==1 - GetMyIP_Windows_Linux_IPV4And6(addresses); - #else - GetMyIP_Windows_Linux_IPV4(addresses); - #endif -} - - -#endif // Windows and Linux - -#endif // file header - -#endif // #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "EmptyHeader.h" + +#ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS + +#ifndef RAKNETSOCKET2_WINDOWS_LINUX_CPP +#define RAKNETSOCKET2_WINDOWS_LINUX_CPP + +#if !defined(WINDOWS_STORE_RT) && !defined(__native_client__) + +#if RAKNET_SUPPORT_IPV6==1 + +void PrepareAddrInfoHints2(addrinfo *hints) +{ + memset(hints, 0, sizeof (addrinfo)); // make sure the struct is empty + hints->ai_socktype = SOCK_DGRAM; // UDP sockets + hints->ai_flags = AI_PASSIVE; // fill in my IP for me +} + +void GetMyIP_Windows_Linux_IPV4And6( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) +{ + int idx=0; + char ac[ 80 ]; + int err = gethostname( ac, sizeof( ac ) ); + RakAssert(err != -1); + + struct addrinfo hints; + struct addrinfo *servinfo=0, *aip; // will point to the results + PrepareAddrInfoHints2(&hints); + getaddrinfo(ac, "", &hints, &servinfo); + + for (idx=0, aip = servinfo; aip != nullptr && idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; aip = aip->ai_next, idx++) + { + if (aip->ai_family == AF_INET) + { + struct sockaddr_in *ipv4 = (struct sockaddr_in *)aip->ai_addr; + memcpy(&addresses[idx].address.addr4,ipv4,sizeof(sockaddr_in)); + } + else + { + struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)aip->ai_addr; + memcpy(&addresses[idx].address.addr4,ipv6,sizeof(sockaddr_in6)); + } + + } + + freeaddrinfo(servinfo); // free the linked-list + + while (idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS) + { + addresses[idx]=UNASSIGNED_SYSTEM_ADDRESS; + idx++; + } +} + +#else + +#if (defined(__GNUC__) || defined(__GCCXML__)) && !defined(__WIN32__) +#include +#endif +void GetMyIP_Windows_Linux_IPV4( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) +{ + + + + int idx=0; + char ac[ 80 ]; + int err = gethostname( ac, sizeof( ac ) ); + (void) err; + RakAssert(err != -1); + + struct hostent *phe = gethostbyname( ac ); + + if ( phe == 0 ) + { + RakAssert(phe!=0); + return ; + } + for ( idx = 0; idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ++idx ) + { + if (phe->h_addr_list[ idx ] == 0) + break; + + memcpy(&addresses[idx].address.addr4.sin_addr,phe->h_addr_list[ idx ],sizeof(struct in_addr)); + } + + while (idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS) + { + addresses[idx]=UNASSIGNED_SYSTEM_ADDRESS; + idx++; + } + +} + +#endif // RAKNET_SUPPORT_IPV6==1 + + +void GetMyIP_Windows_Linux( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) +{ + #if RAKNET_SUPPORT_IPV6==1 + GetMyIP_Windows_Linux_IPV4And6(addresses); + #else + GetMyIP_Windows_Linux_IPV4(addresses); + #endif +} + + +#endif // Windows and Linux + +#endif // file header + +#endif // #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS diff --git a/Source/RakNetStatistics.cpp b/Source/RakNetStatistics.cpp index 72526c62e..f948d55ea 100644 --- a/Source/RakNetStatistics.cpp +++ b/Source/RakNetStatistics.cpp @@ -1,156 +1,156 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// - - - -#include "RakNetStatistics.h" -#include // sprintf -#include "GetTime.h" -#include "RakString.h" - -using namespace RakNet; - -// Verbosity level currently supports 0 (low), 1 (medium), 2 (high) -// Buffer must be hold enough to hold the output string. See the source to get an idea of how many bytes will be output -void RAK_DLL_EXPORT RakNet::StatisticsToString( RakNetStatistics *s, char *buffer, int verbosityLevel ) -{ - if ( s == 0 ) - { - sprintf( buffer, "stats is a NULL pointer in statsToString\n" ); - return ; - } - - if (verbosityLevel==0) - { - sprintf(buffer, - "Bytes per second sent %" PRINTF_64_BIT_MODIFIER "u\n" - "Bytes per second received %" PRINTF_64_BIT_MODIFIER "u\n" - "Current packetloss %.1f%%\n", - (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_SENT], - (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_RECEIVED], - s->packetlossLastSecond*100.0f - ); - } - else if (verbosityLevel==1) - { - sprintf(buffer, - "Actual bytes per second sent %" PRINTF_64_BIT_MODIFIER "u\n" - "Actual bytes per second received %" PRINTF_64_BIT_MODIFIER "u\n" - "Message bytes per second pushed %" PRINTF_64_BIT_MODIFIER "u\n" - "Total actual bytes sent %" PRINTF_64_BIT_MODIFIER "u\n" - "Total actual bytes received %" PRINTF_64_BIT_MODIFIER "u\n" - "Total message bytes pushed %" PRINTF_64_BIT_MODIFIER "u\n" - "Current packetloss %.1f%%\n" - "Average packetloss %.1f%%\n" - "Elapsed connection time in seconds %" PRINTF_64_BIT_MODIFIER "u\n", - (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_SENT], - (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_RECEIVED], - (long long unsigned int) s->valueOverLastSecond[USER_MESSAGE_BYTES_PUSHED], - (long long unsigned int) s->runningTotal[ACTUAL_BYTES_SENT], - (long long unsigned int) s->runningTotal[ACTUAL_BYTES_RECEIVED], - (long long unsigned int) s->runningTotal[USER_MESSAGE_BYTES_PUSHED], - s->packetlossLastSecond*100.0f, - s->packetlossTotal*100.0f, - (long long unsigned int) (uint64_t)((RakNet::GetTimeUS()-s->connectionStartTime)/1000000) - ); - - if (s->BPSLimitByCongestionControl!=0) - { - char buff2[128]; - sprintf(buff2, - "Send capacity %" PRINTF_64_BIT_MODIFIER "u bytes per second (%.0f%%)\n", - (long long unsigned int) s->BPSLimitByCongestionControl, - 100.0f * s->valueOverLastSecond[ACTUAL_BYTES_SENT] / s->BPSLimitByCongestionControl - ); - strcat(buffer,buff2); - } - if (s->BPSLimitByOutgoingBandwidthLimit!=0) - { - char buff2[128]; - sprintf(buff2, - "Send limit %" PRINTF_64_BIT_MODIFIER "u (%.0f%%)\n", - (long long unsigned int) s->BPSLimitByOutgoingBandwidthLimit, - 100.0f * s->valueOverLastSecond[ACTUAL_BYTES_SENT] / s->BPSLimitByOutgoingBandwidthLimit - ); - strcat(buffer,buff2); - } - } - else - { - sprintf(buffer, - "Actual bytes per second sent %" PRINTF_64_BIT_MODIFIER "u\n" - "Actual bytes per second received %" PRINTF_64_BIT_MODIFIER "u\n" - "Message bytes per second sent %" PRINTF_64_BIT_MODIFIER "u\n" - "Message bytes per second resent %" PRINTF_64_BIT_MODIFIER "u\n" - "Message bytes per second pushed %" PRINTF_64_BIT_MODIFIER "u\n" - "Message bytes per second returned %" PRINTF_64_BIT_MODIFIER "u\n" - "Message bytes per second ignored %" PRINTF_64_BIT_MODIFIER "u\n" - "Total bytes sent %" PRINTF_64_BIT_MODIFIER "u\n" - "Total bytes received %" PRINTF_64_BIT_MODIFIER "u\n" - "Total message bytes sent %" PRINTF_64_BIT_MODIFIER "u\n" - "Total message bytes resent %" PRINTF_64_BIT_MODIFIER "u\n" - "Total message bytes pushed %" PRINTF_64_BIT_MODIFIER "u\n" - "Total message bytes returned %" PRINTF_64_BIT_MODIFIER "u\n" - "Total message bytes ignored %" PRINTF_64_BIT_MODIFIER "u\n" - "Messages in send buffer, by priority %i,%i,%i,%i\n" - "Bytes in send buffer, by priority %i,%i,%i,%i\n" - "Messages in resend buffer %i\n" - "Bytes in resend buffer %" PRINTF_64_BIT_MODIFIER "u\n" - "Current packetloss %.1f%%\n" - "Average packetloss %.1f%%\n" - "Elapsed connection time in seconds %" PRINTF_64_BIT_MODIFIER "u\n", - (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_SENT], - (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_RECEIVED], - (long long unsigned int) s->valueOverLastSecond[USER_MESSAGE_BYTES_SENT], - (long long unsigned int) s->valueOverLastSecond[USER_MESSAGE_BYTES_RESENT], - (long long unsigned int) s->valueOverLastSecond[USER_MESSAGE_BYTES_PUSHED], - (long long unsigned int) s->valueOverLastSecond[USER_MESSAGE_BYTES_RECEIVED_PROCESSED], - (long long unsigned int) s->valueOverLastSecond[USER_MESSAGE_BYTES_RECEIVED_IGNORED], - (long long unsigned int) s->runningTotal[ACTUAL_BYTES_SENT], - (long long unsigned int) s->runningTotal[ACTUAL_BYTES_RECEIVED], - (long long unsigned int) s->runningTotal[USER_MESSAGE_BYTES_SENT], - (long long unsigned int) s->runningTotal[USER_MESSAGE_BYTES_RESENT], - (long long unsigned int) s->runningTotal[USER_MESSAGE_BYTES_PUSHED], - (long long unsigned int) s->runningTotal[USER_MESSAGE_BYTES_RECEIVED_PROCESSED], - (long long unsigned int) s->runningTotal[USER_MESSAGE_BYTES_RECEIVED_IGNORED], - s->messageInSendBuffer[IMMEDIATE_PRIORITY],s->messageInSendBuffer[HIGH_PRIORITY],s->messageInSendBuffer[MEDIUM_PRIORITY],s->messageInSendBuffer[LOW_PRIORITY], - (unsigned int) s->bytesInSendBuffer[IMMEDIATE_PRIORITY],(unsigned int) s->bytesInSendBuffer[HIGH_PRIORITY],(unsigned int) s->bytesInSendBuffer[MEDIUM_PRIORITY],(unsigned int) s->bytesInSendBuffer[LOW_PRIORITY], - s->messagesInResendBuffer, - (long long unsigned int) s->bytesInResendBuffer, - s->packetlossLastSecond*100.0f, - s->packetlossTotal*100.0f, - (long long unsigned int) (uint64_t)((RakNet::GetTimeUS()-s->connectionStartTime)/1000000) - ); - - if (s->BPSLimitByCongestionControl!=0) - { - char buff2[128]; - sprintf(buff2, - "Send capacity %" PRINTF_64_BIT_MODIFIER "u bytes per second (%.0f%%)\n", - (long long unsigned int) s->BPSLimitByCongestionControl, - 100.0f * s->valueOverLastSecond[ACTUAL_BYTES_SENT] / s->BPSLimitByCongestionControl - ); - strcat(buffer,buff2); - } - if (s->BPSLimitByOutgoingBandwidthLimit!=0) - { - char buff2[128]; - sprintf(buff2, - "Send limit %" PRINTF_64_BIT_MODIFIER "u (%.0f%%)\n", - (long long unsigned int) s->BPSLimitByOutgoingBandwidthLimit, - 100.0f * s->valueOverLastSecond[ACTUAL_BYTES_SENT] / s->BPSLimitByOutgoingBandwidthLimit - ); - strcat(buffer,buff2); - } - } -} +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// + + + +#include "RakNetStatistics.h" +#include // sprintf +#include "GetTime.h" +#include "RakString.h" + +using namespace RakNet; + +// Verbosity level currently supports 0 (low), 1 (medium), 2 (high) +// Buffer must be hold enough to hold the output string. See the source to get an idea of how many bytes will be output +void RAK_DLL_EXPORT RakNet::StatisticsToString( RakNetStatistics *s, char *buffer, int verbosityLevel ) +{ + if ( s == 0 ) + { + sprintf( buffer, "stats is a nullptr pointer in statsToString\n" ); + return ; + } + + if (verbosityLevel==0) + { + sprintf(buffer, + "Bytes per second sent %" PRINTF_64_BIT_MODIFIER "u\n" + "Bytes per second received %" PRINTF_64_BIT_MODIFIER "u\n" + "Current packetloss %.1f%%\n", + (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_SENT], + (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_RECEIVED], + s->packetlossLastSecond*100.0f + ); + } + else if (verbosityLevel==1) + { + sprintf(buffer, + "Actual bytes per second sent %" PRINTF_64_BIT_MODIFIER "u\n" + "Actual bytes per second received %" PRINTF_64_BIT_MODIFIER "u\n" + "Message bytes per second pushed %" PRINTF_64_BIT_MODIFIER "u\n" + "Total actual bytes sent %" PRINTF_64_BIT_MODIFIER "u\n" + "Total actual bytes received %" PRINTF_64_BIT_MODIFIER "u\n" + "Total message bytes pushed %" PRINTF_64_BIT_MODIFIER "u\n" + "Current packetloss %.1f%%\n" + "Average packetloss %.1f%%\n" + "Elapsed connection time in seconds %" PRINTF_64_BIT_MODIFIER "u\n", + (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_SENT], + (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_RECEIVED], + (long long unsigned int) s->valueOverLastSecond[USER_MESSAGE_BYTES_PUSHED], + (long long unsigned int) s->runningTotal[ACTUAL_BYTES_SENT], + (long long unsigned int) s->runningTotal[ACTUAL_BYTES_RECEIVED], + (long long unsigned int) s->runningTotal[USER_MESSAGE_BYTES_PUSHED], + s->packetlossLastSecond*100.0f, + s->packetlossTotal*100.0f, + (long long unsigned int) (uint64_t)((RakNet::GetTimeUS()-s->connectionStartTime)/1000000) + ); + + if (s->BPSLimitByCongestionControl!=0) + { + char buff2[128]; + sprintf(buff2, + "Send capacity %" PRINTF_64_BIT_MODIFIER "u bytes per second (%.0f%%)\n", + (long long unsigned int) s->BPSLimitByCongestionControl, + 100.0f * s->valueOverLastSecond[ACTUAL_BYTES_SENT] / s->BPSLimitByCongestionControl + ); + strcat(buffer,buff2); + } + if (s->BPSLimitByOutgoingBandwidthLimit!=0) + { + char buff2[128]; + sprintf(buff2, + "Send limit %" PRINTF_64_BIT_MODIFIER "u (%.0f%%)\n", + (long long unsigned int) s->BPSLimitByOutgoingBandwidthLimit, + 100.0f * s->valueOverLastSecond[ACTUAL_BYTES_SENT] / s->BPSLimitByOutgoingBandwidthLimit + ); + strcat(buffer,buff2); + } + } + else + { + sprintf(buffer, + "Actual bytes per second sent %" PRINTF_64_BIT_MODIFIER "u\n" + "Actual bytes per second received %" PRINTF_64_BIT_MODIFIER "u\n" + "Message bytes per second sent %" PRINTF_64_BIT_MODIFIER "u\n" + "Message bytes per second resent %" PRINTF_64_BIT_MODIFIER "u\n" + "Message bytes per second pushed %" PRINTF_64_BIT_MODIFIER "u\n" + "Message bytes per second returned %" PRINTF_64_BIT_MODIFIER "u\n" + "Message bytes per second ignored %" PRINTF_64_BIT_MODIFIER "u\n" + "Total bytes sent %" PRINTF_64_BIT_MODIFIER "u\n" + "Total bytes received %" PRINTF_64_BIT_MODIFIER "u\n" + "Total message bytes sent %" PRINTF_64_BIT_MODIFIER "u\n" + "Total message bytes resent %" PRINTF_64_BIT_MODIFIER "u\n" + "Total message bytes pushed %" PRINTF_64_BIT_MODIFIER "u\n" + "Total message bytes returned %" PRINTF_64_BIT_MODIFIER "u\n" + "Total message bytes ignored %" PRINTF_64_BIT_MODIFIER "u\n" + "Messages in send buffer, by priority %i,%i,%i,%i\n" + "Bytes in send buffer, by priority %i,%i,%i,%i\n" + "Messages in resend buffer %i\n" + "Bytes in resend buffer %" PRINTF_64_BIT_MODIFIER "u\n" + "Current packetloss %.1f%%\n" + "Average packetloss %.1f%%\n" + "Elapsed connection time in seconds %" PRINTF_64_BIT_MODIFIER "u\n", + (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_SENT], + (long long unsigned int) s->valueOverLastSecond[ACTUAL_BYTES_RECEIVED], + (long long unsigned int) s->valueOverLastSecond[USER_MESSAGE_BYTES_SENT], + (long long unsigned int) s->valueOverLastSecond[USER_MESSAGE_BYTES_RESENT], + (long long unsigned int) s->valueOverLastSecond[USER_MESSAGE_BYTES_PUSHED], + (long long unsigned int) s->valueOverLastSecond[USER_MESSAGE_BYTES_RECEIVED_PROCESSED], + (long long unsigned int) s->valueOverLastSecond[USER_MESSAGE_BYTES_RECEIVED_IGNORED], + (long long unsigned int) s->runningTotal[ACTUAL_BYTES_SENT], + (long long unsigned int) s->runningTotal[ACTUAL_BYTES_RECEIVED], + (long long unsigned int) s->runningTotal[USER_MESSAGE_BYTES_SENT], + (long long unsigned int) s->runningTotal[USER_MESSAGE_BYTES_RESENT], + (long long unsigned int) s->runningTotal[USER_MESSAGE_BYTES_PUSHED], + (long long unsigned int) s->runningTotal[USER_MESSAGE_BYTES_RECEIVED_PROCESSED], + (long long unsigned int) s->runningTotal[USER_MESSAGE_BYTES_RECEIVED_IGNORED], + s->messageInSendBuffer[IMMEDIATE_PRIORITY],s->messageInSendBuffer[HIGH_PRIORITY],s->messageInSendBuffer[MEDIUM_PRIORITY],s->messageInSendBuffer[LOW_PRIORITY], + (unsigned int) s->bytesInSendBuffer[IMMEDIATE_PRIORITY],(unsigned int) s->bytesInSendBuffer[HIGH_PRIORITY],(unsigned int) s->bytesInSendBuffer[MEDIUM_PRIORITY],(unsigned int) s->bytesInSendBuffer[LOW_PRIORITY], + s->messagesInResendBuffer, + (long long unsigned int) s->bytesInResendBuffer, + s->packetlossLastSecond*100.0f, + s->packetlossTotal*100.0f, + (long long unsigned int) (uint64_t)((RakNet::GetTimeUS()-s->connectionStartTime)/1000000) + ); + + if (s->BPSLimitByCongestionControl!=0) + { + char buff2[128]; + sprintf(buff2, + "Send capacity %" PRINTF_64_BIT_MODIFIER "u bytes per second (%.0f%%)\n", + (long long unsigned int) s->BPSLimitByCongestionControl, + 100.0f * s->valueOverLastSecond[ACTUAL_BYTES_SENT] / s->BPSLimitByCongestionControl + ); + strcat(buffer,buff2); + } + if (s->BPSLimitByOutgoingBandwidthLimit!=0) + { + char buff2[128]; + sprintf(buff2, + "Send limit %" PRINTF_64_BIT_MODIFIER "u (%.0f%%)\n", + (long long unsigned int) s->BPSLimitByOutgoingBandwidthLimit, + 100.0f * s->valueOverLastSecond[ACTUAL_BYTES_SENT] / s->BPSLimitByOutgoingBandwidthLimit + ); + strcat(buffer,buff2); + } + } +} diff --git a/Source/RakNetStatistics.h b/Source/RakNetStatistics.h index 7f874ebba..95fbe6137 100644 --- a/Source/RakNetStatistics.h +++ b/Source/RakNetStatistics.h @@ -1,133 +1,131 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief A structure that holds all statistical data returned by RakNet. -/// - - - -#ifndef __RAK_NET_STATISTICS_H -#define __RAK_NET_STATISTICS_H - -#include "PacketPriority.h" -#include "Export.h" -#include "RakNetTypes.h" - -namespace RakNet -{ - -enum RNSPerSecondMetrics -{ - /// How many bytes per pushed via a call to RakPeerInterface::Send() - USER_MESSAGE_BYTES_PUSHED, - - /// How many user message bytes were sent via a call to RakPeerInterface::Send(). This is less than or equal to USER_MESSAGE_BYTES_PUSHED. - /// A message would be pushed, but not yet sent, due to congestion control - USER_MESSAGE_BYTES_SENT, - - /// How many user message bytes were resent. A message is resent if it is marked as reliable, and either the message didn't arrive or the message ack didn't arrive. - USER_MESSAGE_BYTES_RESENT, - - /// How many user message bytes were received, and returned to the user successfully. - USER_MESSAGE_BYTES_RECEIVED_PROCESSED, - - /// How many user message bytes were received, but ignored due to data format errors. This will usually be 0. - USER_MESSAGE_BYTES_RECEIVED_IGNORED, - - /// How many actual bytes were sent, including per-message and per-datagram overhead, and reliable message acks - ACTUAL_BYTES_SENT, - - /// How many actual bytes were received, including overead and acks. - ACTUAL_BYTES_RECEIVED, - - /// \internal - RNS_PER_SECOND_METRICS_COUNT -}; - -/// \brief Network Statisics Usage -/// -/// Store Statistics information related to network usage -struct RAK_DLL_EXPORT RakNetStatistics -{ - /// For each type in RNSPerSecondMetrics, what is the value over the last 1 second? - uint64_t valueOverLastSecond[RNS_PER_SECOND_METRICS_COUNT]; - - /// For each type in RNSPerSecondMetrics, what is the total value over the lifetime of the connection? - uint64_t runningTotal[RNS_PER_SECOND_METRICS_COUNT]; - - /// When did the connection start? - /// \sa RakNet::GetTimeUS() - RakNet::TimeUS connectionStartTime; - - /// Is our current send rate throttled by congestion control? - /// This value should be true if you send more data per second than your bandwidth capacity - bool isLimitedByCongestionControl; - - /// If \a isLimitedByCongestionControl is true, what is the limit, in bytes per second? - uint64_t BPSLimitByCongestionControl; - - /// Is our current send rate throttled by a call to RakPeer::SetPerConnectionOutgoingBandwidthLimit()? - bool isLimitedByOutgoingBandwidthLimit; - - /// If \a isLimitedByOutgoingBandwidthLimit is true, what is the limit, in bytes per second? - uint64_t BPSLimitByOutgoingBandwidthLimit; - - /// For each priority level, how many messages are waiting to be sent out? - unsigned int messageInSendBuffer[NUMBER_OF_PRIORITIES]; - - /// For each priority level, how many bytes are waiting to be sent out? - double bytesInSendBuffer[NUMBER_OF_PRIORITIES]; - - /// How many messages are waiting in the resend buffer? This includes messages waiting for an ack, so should normally be a small value - /// If the value is rising over time, you are exceeding the bandwidth capacity. See BPSLimitByCongestionControl - unsigned int messagesInResendBuffer; - - /// How many bytes are waiting in the resend buffer. See also messagesInResendBuffer - uint64_t bytesInResendBuffer; - - /// Over the last second, what was our packetloss? This number will range from 0.0 (for none) to 1.0 (for 100%) - float packetlossLastSecond; - - /// What is the average total packetloss over the lifetime of the connection? - float packetlossTotal; - - RakNetStatistics& operator +=(const RakNetStatistics& other) - { - unsigned i; - for (i=0; i < NUMBER_OF_PRIORITIES; i++) - { - messageInSendBuffer[i]+=other.messageInSendBuffer[i]; - bytesInSendBuffer[i]+=other.bytesInSendBuffer[i]; - } - - for (i=0; i < RNS_PER_SECOND_METRICS_COUNT; i++) - { - valueOverLastSecond[i]+=other.valueOverLastSecond[i]; - runningTotal[i]+=other.runningTotal[i]; - } - - return *this; - } -}; - -/// Verbosity level currently supports 0 (low), 1 (medium), 2 (high) -/// \param[in] s The Statistical information to format out -/// \param[in] buffer The buffer containing a formated report -/// \param[in] verbosityLevel -/// 0 low -/// 1 medium -/// 2 high -/// 3 debugging congestion control -void RAK_DLL_EXPORT StatisticsToString( RakNetStatistics *s, char *buffer, int verbosityLevel ); - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief A structure that holds all statistical data returned by RakNet. +/// + + + +#pragma once + +#include "PacketPriority.h" +#include "Export.h" +#include "RakNetTypes.h" + +namespace RakNet +{ + +enum RNSPerSecondMetrics +{ + /// How many bytes per pushed via a call to RakPeerInterface::Send() + USER_MESSAGE_BYTES_PUSHED, + + /// How many user message bytes were sent via a call to RakPeerInterface::Send(). This is less than or equal to USER_MESSAGE_BYTES_PUSHED. + /// A message would be pushed, but not yet sent, due to congestion control + USER_MESSAGE_BYTES_SENT, + + /// How many user message bytes were resent. A message is resent if it is marked as reliable, and either the message didn't arrive or the message ack didn't arrive. + USER_MESSAGE_BYTES_RESENT, + + /// How many user message bytes were received, and returned to the user successfully. + USER_MESSAGE_BYTES_RECEIVED_PROCESSED, + + /// How many user message bytes were received, but ignored due to data format errors. This will usually be 0. + USER_MESSAGE_BYTES_RECEIVED_IGNORED, + + /// How many actual bytes were sent, including per-message and per-datagram overhead, and reliable message acks + ACTUAL_BYTES_SENT, + + /// How many actual bytes were received, including overead and acks. + ACTUAL_BYTES_RECEIVED, + + /// \internal + RNS_PER_SECOND_METRICS_COUNT +}; + +/// \brief Network Statisics Usage +/// +/// Store Statistics information related to network usage +struct RAK_DLL_EXPORT RakNetStatistics +{ + /// For each type in RNSPerSecondMetrics, what is the value over the last 1 second? + uint64_t valueOverLastSecond[RNS_PER_SECOND_METRICS_COUNT]; + + /// For each type in RNSPerSecondMetrics, what is the total value over the lifetime of the connection? + uint64_t runningTotal[RNS_PER_SECOND_METRICS_COUNT]; + + /// When did the connection start? + /// \sa RakNet::GetTimeUS() + RakNet::TimeUS connectionStartTime; + + /// Is our current send rate throttled by congestion control? + /// This value should be true if you send more data per second than your bandwidth capacity + bool isLimitedByCongestionControl; + + /// If \a isLimitedByCongestionControl is true, what is the limit, in bytes per second? + uint64_t BPSLimitByCongestionControl; + + /// Is our current send rate throttled by a call to RakPeer::SetPerConnectionOutgoingBandwidthLimit()? + bool isLimitedByOutgoingBandwidthLimit; + + /// If \a isLimitedByOutgoingBandwidthLimit is true, what is the limit, in bytes per second? + uint64_t BPSLimitByOutgoingBandwidthLimit; + + /// For each priority level, how many messages are waiting to be sent out? + unsigned int messageInSendBuffer[NUMBER_OF_PRIORITIES]; + + /// For each priority level, how many bytes are waiting to be sent out? + double bytesInSendBuffer[NUMBER_OF_PRIORITIES]; + + /// How many messages are waiting in the resend buffer? This includes messages waiting for an ack, so should normally be a small value + /// If the value is rising over time, you are exceeding the bandwidth capacity. See BPSLimitByCongestionControl + unsigned int messagesInResendBuffer; + + /// How many bytes are waiting in the resend buffer. See also messagesInResendBuffer + uint64_t bytesInResendBuffer; + + /// Over the last second, what was our packetloss? This number will range from 0.0 (for none) to 1.0 (for 100%) + float packetlossLastSecond; + + /// What is the average total packetloss over the lifetime of the connection? + float packetlossTotal; + + RakNetStatistics& operator +=(const RakNetStatistics& other) + { + unsigned i; + for (i=0; i < NUMBER_OF_PRIORITIES; i++) + { + messageInSendBuffer[i]+=other.messageInSendBuffer[i]; + bytesInSendBuffer[i]+=other.bytesInSendBuffer[i]; + } + + for (i=0; i < RNS_PER_SECOND_METRICS_COUNT; i++) + { + valueOverLastSecond[i]+=other.valueOverLastSecond[i]; + runningTotal[i]+=other.runningTotal[i]; + } + + return *this; + } +}; + +/// Verbosity level currently supports 0 (low), 1 (medium), 2 (high) +/// \param[in] s The Statistical information to format out +/// \param[in] buffer The buffer containing a formated report +/// \param[in] verbosityLevel +/// 0 low +/// 1 medium +/// 2 high +/// 3 debugging congestion control +void RAK_DLL_EXPORT StatisticsToString( RakNetStatistics *s, char *buffer, int verbosityLevel ); + +} // namespace RakNet + diff --git a/Source/RakNetTime.h b/Source/RakNetTime.h index 5c1a1ec15..de0b63a56 100644 --- a/Source/RakNetTime.h +++ b/Source/RakNetTime.h @@ -1,33 +1,31 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __RAKNET_TIME_H -#define __RAKNET_TIME_H - -#include "NativeTypes.h" -#include "RakNetDefines.h" - -namespace RakNet { - -// Define __GET_TIME_64BIT if you want to use large types for GetTime (takes more bandwidth when you transmit time though!) -// You would want to do this if your system is going to run long enough to overflow the millisecond counter (over a month) -#if __GET_TIME_64BIT==1 -typedef uint64_t Time; -typedef uint32_t TimeMS; -typedef uint64_t TimeUS; -#else -typedef uint32_t Time; -typedef uint32_t TimeMS; -typedef uint64_t TimeUS; -#endif - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include "NativeTypes.h" +#include "RakNetDefines.h" + +namespace RakNet { + +// Define __GET_TIME_64BIT if you want to use large types for GetTime (takes more bandwidth when you transmit time though!) +// You would want to do this if your system is going to run long enough to overflow the millisecond counter (over a month) +#if __GET_TIME_64BIT==1 +typedef uint64_t Time; +typedef uint32_t TimeMS; +typedef uint64_t TimeUS; +#else +typedef uint32_t Time; +typedef uint32_t TimeMS; +typedef uint64_t TimeUS; +#endif + +} // namespace RakNet + diff --git a/Source/RakNetTransport2.h b/Source/RakNetTransport2.h index 7cdee8fa8..98d1ae103 100644 --- a/Source/RakNetTransport2.h +++ b/Source/RakNetTransport2.h @@ -1,110 +1,108 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Contains RakNetTransportCommandParser and RakNetTransport used to provide a secure console connection. -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_TelnetTransport==1 - -#ifndef __RAKNET_TRANSPORT_2 -#define __RAKNET_TRANSPORT_2 - -#include "TransportInterface.h" -#include "DS_Queue.h" -#include "CommandParserInterface.h" -#include "PluginInterface2.h" -#include "Export.h" - -namespace RakNet -{ -/// Forward declarations -class BitStream; -class RakPeerInterface; -class RakNetTransport; - -/// \defgroup RAKNET_TRANSPORT_GROUP RakNetTransport -/// \brief UDP based transport implementation for the ConsoleServer -/// \details -/// \ingroup PLUGINS_GROUP - -/// \brief Use RakNetTransport if you need a secure connection between the client and the console server. -/// \details RakNetTransport automatically initializes security for the system. Use the project CommandConsoleClient to connect -/// To the ConsoleServer if you use RakNetTransport -/// \ingroup RAKNET_TRANSPORT_GROUP -class RAK_DLL_EXPORT RakNetTransport2 : public TransportInterface, public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(RakNetTransport2) - - RakNetTransport2(); - virtual ~RakNetTransport2(); - - /// Start the transport provider on the indicated port. - /// \param[in] port The port to start the transport provider on - /// \param[in] serverMode If true, you should allow incoming connections (I don't actually use this anywhere) - /// \return Return true on success, false on failure. - bool Start(unsigned short port, bool serverMode); - - /// Stop the transport provider. You can clear memory and shutdown threads here. - void Stop(void); - - /// Send a null-terminated string to \a systemAddress - /// If your transport method requires particular formatting of the outgoing data (e.g. you don't just send strings) you can do it here - /// and parse it out in Receive(). - /// \param[in] systemAddress The player to send the string to - /// \param[in] data format specifier - same as RAKNET_DEBUG_PRINTF - /// \param[in] ... format specification arguments - same as RAKNET_DEBUG_PRINTF - void Send( SystemAddress systemAddress, const char *data, ... ); - - /// Disconnect \a systemAddress . The binary address and port defines the SystemAddress structure. - /// \param[in] systemAddress The player/address to disconnect - void CloseConnection( SystemAddress systemAddress ); - - /// Return a string. The string should be allocated and written to Packet::data . - /// The byte length should be written to Packet::length . The player/address should be written to Packet::systemAddress - /// If your transport protocol adds special formatting to the data stream you should parse it out before returning it in the packet - /// and thus only return a string in Packet::data - /// \return The packet structure containing the result of Receive, or 0 if no data is available - Packet* Receive( void ); - - /// Deallocate the Packet structure returned by Receive - /// \param[in] The packet to deallocate - void DeallocatePacket( Packet *packet ); - - /// If a new system connects to you, you should queue that event and return the systemAddress/address of that player in this function. - /// \return The SystemAddress/address of the system - SystemAddress HasNewIncomingConnection(void); - - /// If a system loses the connection, you should queue that event and return the systemAddress/address of that player in this function. - /// \return The SystemAddress/address of the system - SystemAddress HasLostConnection(void); - - virtual CommandParserInterface* GetCommandParser(void) {return 0;} - - /// \internal - virtual PluginReceiveResult OnReceive(Packet *packet); - /// \internal - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - /// \internal - virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); -protected: - DataStructures::Queue newConnections, lostConnections; - DataStructures::Queue packetQueue; -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Contains RakNetTransportCommandParser and RakNetTransport used to provide a secure console connection. +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_TelnetTransport==1 + +#pragma once + +#include "TransportInterface.h" +#include "DS_Queue.h" +#include "CommandParserInterface.h" +#include "PluginInterface2.h" +#include "Export.h" + +namespace RakNet +{ +/// Forward declarations +class BitStream; +class RakPeerInterface; +class RakNetTransport; + +/// \defgroup RAKNET_TRANSPORT_GROUP RakNetTransport +/// \brief UDP based transport implementation for the ConsoleServer +/// \details +/// \ingroup PLUGINS_GROUP + +/// \brief Use RakNetTransport if you need a secure connection between the client and the console server. +/// \details RakNetTransport automatically initializes security for the system. Use the project CommandConsoleClient to connect +/// To the ConsoleServer if you use RakNetTransport +/// \ingroup RAKNET_TRANSPORT_GROUP +class RAK_DLL_EXPORT RakNetTransport2 : public TransportInterface, public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(RakNetTransport2) + + RakNetTransport2(); + virtual ~RakNetTransport2(); + + /// Start the transport provider on the indicated port. + /// \param[in] port The port to start the transport provider on + /// \param[in] serverMode If true, you should allow incoming connections (I don't actually use this anywhere) + /// \return Return true on success, false on failure. + bool Start(unsigned short port, bool serverMode); + + /// Stop the transport provider. You can clear memory and shutdown threads here. + void Stop(void); + + /// Send a null-terminated string to \a systemAddress + /// If your transport method requires particular formatting of the outgoing data (e.g. you don't just send strings) you can do it here + /// and parse it out in Receive(). + /// \param[in] systemAddress The player to send the string to + /// \param[in] data format specifier - same as RAKNET_DEBUG_PRINTF + /// \param[in] ... format specification arguments - same as RAKNET_DEBUG_PRINTF + void Send( SystemAddress systemAddress, const char *data, ... ); + + /// Disconnect \a systemAddress . The binary address and port defines the SystemAddress structure. + /// \param[in] systemAddress The player/address to disconnect + void CloseConnection( SystemAddress systemAddress ); + + /// Return a string. The string should be allocated and written to Packet::data . + /// The byte length should be written to Packet::length . The player/address should be written to Packet::systemAddress + /// If your transport protocol adds special formatting to the data stream you should parse it out before returning it in the packet + /// and thus only return a string in Packet::data + /// \return The packet structure containing the result of Receive, or 0 if no data is available + Packet* Receive( void ); + + /// Deallocate the Packet structure returned by Receive + /// \param[in] The packet to deallocate + void DeallocatePacket( Packet *packet ); + + /// If a new system connects to you, you should queue that event and return the systemAddress/address of that player in this function. + /// \return The SystemAddress/address of the system + SystemAddress HasNewIncomingConnection(void); + + /// If a system loses the connection, you should queue that event and return the systemAddress/address of that player in this function. + /// \return The SystemAddress/address of the system + SystemAddress HasLostConnection(void); + + virtual CommandParserInterface* GetCommandParser(void) {return 0;} + + /// \internal + virtual PluginReceiveResult OnReceive(Packet *packet); + /// \internal + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + /// \internal + virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); +protected: + DataStructures::Queue newConnections, lostConnections; + DataStructures::Queue packetQueue; +}; + +} // namespace RakNet + +#endif + diff --git a/Source/RakNetTypes.cpp b/Source/RakNetTypes.cpp index 4ccd9ac95..7feb26865 100644 --- a/Source/RakNetTypes.cpp +++ b/Source/RakNetTypes.cpp @@ -1,812 +1,815 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// - - -#include "RakNetTypes.h" -#include "RakAssert.h" -#include -#include -#include "WindowsIncludes.h" -#include "WSAStartupSingleton.h" -#include "SocketDefines.h" -#include "RakNetSocket2.h" - - -#if defined(_WIN32) -// extern __int64 _strtoui64(const char*, char**, int); // needed for Code::Blocks. Does not compile on Visual Studio 2010 -// IP_DONTFRAGMENT is different between winsock 1 and winsock 2. Therefore, Winsock2.h must be linked againt Ws2_32.lib -// winsock.h must be linked against WSock32.lib. If these two are mixed up the flag won't work correctly -#include "WindowsIncludes.h" - -#else -#include -#include -#include -#endif - -#include // strncasecmp -#include "Itoa.h" -#include "SocketLayer.h" -#include "SuperFastHash.h" -#include - -using namespace RakNet; - -const char *IPV6_LOOPBACK="::1"; -const char *IPV4_LOOPBACK="127.0.0.1"; - -AddressOrGUID::AddressOrGUID( Packet *packet ) -{ - rakNetGuid=packet->guid; - systemAddress=packet->systemAddress; -} - -unsigned long AddressOrGUID::ToInteger( const AddressOrGUID &aog ) -{ - if (aog.rakNetGuid!=UNASSIGNED_RAKNET_GUID) - return RakNetGUID::ToUint32(aog.rakNetGuid); - return SystemAddress::ToInteger(aog.systemAddress); -} -const char *AddressOrGUID::ToString(bool writePort) const -{ - if (rakNetGuid!=UNASSIGNED_RAKNET_GUID) - return rakNetGuid.ToString(); - return systemAddress.ToString(writePort); -} -void AddressOrGUID::ToString(bool writePort, char *dest) const -{ - if (rakNetGuid!=UNASSIGNED_RAKNET_GUID) - return rakNetGuid.ToString(dest); - return systemAddress.ToString(writePort,dest); -} -bool RakNet::NonNumericHostString( const char *host ) -{ - // Return false if IP address. Return true if domain - unsigned int i=0; - while (host[i]) - { - // IPV4: natpunch.jenkinssoftware.com - // IPV6: fe80::7c:31f7:fec4:27de%14 - if ((host[i]>='g' && host[i]<='z') || - (host[i]>='A' && host[i]<='Z')) - return true; - ++i; - } - return false; -} - -SocketDescriptor::SocketDescriptor() { -#ifdef __native_client__ - blockingSocket=false; -#else - blockingSocket=true; -#endif - port=0; hostAddress[0]=0; remotePortRakNetWasStartedOn_PS3_PSP2=0; extraSocketOptions=0; socketFamily=AF_INET;} -SocketDescriptor::SocketDescriptor(unsigned short _port, const char *_hostAddress) -{ - #ifdef __native_client__ - blockingSocket=false; - #else - blockingSocket=true; - #endif - remotePortRakNetWasStartedOn_PS3_PSP2=0; - port=_port; - if (_hostAddress) - strcpy(hostAddress, _hostAddress); - else - hostAddress[0]=0; - extraSocketOptions=0; - socketFamily=AF_INET; -} - -// Defaults to not in peer to peer mode for NetworkIDs. This only sends the localSystemAddress portion in the BitStream class -// This is what you want for client/server, where the server assigns all NetworkIDs and it is unnecessary to transmit the full structure. -// For peer to peer, this will transmit the systemAddress of the system that created the object in addition to localSystemAddress. This allows -// Any system to create unique ids locally. -// All systems must use the same value for this variable. -//bool RAK_DLL_EXPORT NetworkID::peerToPeerMode=false; - -SystemAddress& SystemAddress::operator = ( const SystemAddress& input ) -{ - memcpy(&address, &input.address, sizeof(address)); - systemIndex = input.systemIndex; - debugPort = input.debugPort; - return *this; -} -bool SystemAddress::EqualsExcludingPort( const SystemAddress& right ) const -{ - return (address.addr4.sin_family==AF_INET && address.addr4.sin_addr.s_addr==right.address.addr4.sin_addr.s_addr) -#if RAKNET_SUPPORT_IPV6==1 - || (address.addr4.sin_family==AF_INET6 && memcmp(address.addr6.sin6_addr.s6_addr, right.address.addr6.sin6_addr.s6_addr, sizeof(address.addr6.sin6_addr.s6_addr))==0) -#endif - ; -} -unsigned short SystemAddress::GetPort(void) const -{ - return ntohs(address.addr4.sin_port); -} -unsigned short SystemAddress::GetPortNetworkOrder(void) const -{ - return address.addr4.sin_port; -} -void SystemAddress::SetPortHostOrder(unsigned short s) -{ - address.addr4.sin_port=htons(s); - debugPort=s; -} -void SystemAddress::SetPortNetworkOrder(unsigned short s) -{ - address.addr4.sin_port=s; - debugPort=ntohs(s); -} -bool SystemAddress::operator==( const SystemAddress& right ) const -{ - return address.addr4.sin_port == right.address.addr4.sin_port && EqualsExcludingPort(right); -} - -bool SystemAddress::operator!=( const SystemAddress& right ) const -{ - return (*this==right)==false; -} - -bool SystemAddress::operator>( const SystemAddress& right ) const -{ - if (address.addr4.sin_port == right.address.addr4.sin_port) - { -#if RAKNET_SUPPORT_IPV6==1 - if (address.addr4.sin_family==AF_INET) - return address.addr4.sin_addr.s_addr>right.address.addr4.sin_addr.s_addr; - return memcmp(address.addr6.sin6_addr.s6_addr, right.address.addr6.sin6_addr.s6_addr, sizeof(address.addr6.sin6_addr.s6_addr))>0; -#else - return address.addr4.sin_addr.s_addr>right.address.addr4.sin_addr.s_addr; -#endif - } - return address.addr4.sin_port>right.address.addr4.sin_port; -} - -bool SystemAddress::operator<( const SystemAddress& right ) const -{ - if (address.addr4.sin_port == right.address.addr4.sin_port) - { -#if RAKNET_SUPPORT_IPV6==1 - if (address.addr4.sin_family==AF_INET) - return address.addr4.sin_addr.s_addr0; -#else - return address.addr4.sin_addr.s_addr> 24) == 10 || (address.addr4.sin_addr.s_addr >> 24) == 192; -#endif -} -bool SystemAddress::SetBinaryAddress(const char *str, char portDelineator) -{ - if ( NonNumericHostString( str ) ) - { - -#if defined(_WIN32) - if (_strnicmp(str,"localhost", 9)==0) -#else - if (strncasecmp(str,"localhost", 9)==0) -#endif - { - - - - - - address.addr4.sin_addr.s_addr=inet_addr__("127.0.0.1"); - - if (str[9]) - { - SetPortHostOrder((unsigned short) atoi(str+9)); - } - return true; - } - - //const char *ip = ( char* ) SocketLayer::DomainNameToIP( str ); - char ip[65]; - ip[0]=0; - RakNetSocket2::DomainNameToIP(str, ip); - if (ip[0]) - { - - - - - - address.addr4.sin_addr.s_addr=inet_addr__(ip); - - } - else - { - *this = UNASSIGNED_SYSTEM_ADDRESS; - return false; - } - } - else - { - //#ifdef _XBOX - // binaryAddress=UNASSIGNED_SYSTEM_ADDRESS.binaryAddress; - //#else - // Split the string into the first part, and the : part - int index, portIndex; - char IPPart[22]; - char portPart[10]; - // Only write the valid parts, don't change existing if invalid - // binaryAddress=UNASSIGNED_SYSTEM_ADDRESS.binaryAddress; - // port=UNASSIGNED_SYSTEM_ADDRESS.port; - for (index=0; str[index] && str[index]!=portDelineator && index<22; index++) - { - if (str[index]!='.' && (str[index]<'0' || str[index]>'9')) - break; - IPPart[index]=str[index]; - } - IPPart[index]=0; - portPart[0]=0; - if (str[index] && str[index+1]) - { - index++; - for (portIndex=0; portIndex<10 && str[index] && index < 22+10; index++, portIndex++) - { - if (str[index]<'0' || str[index]>'9') - break; - - portPart[portIndex]=str[index]; - } - portPart[portIndex]=0; - } - - - - - - - - - - - - - - if (IPPart[0]) - { - - - - - - address.addr4.sin_addr.s_addr=inet_addr__(IPPart); - - } - - - if (portPart[0]) - { - address.addr4.sin_port=htons((unsigned short) atoi(portPart)); - debugPort=ntohs(address.addr4.sin_port); - } - //#endif - } - return true; -} - -#ifdef _MSC_VER -#pragma warning( disable : 4702 ) // warning C4702: unreachable code -#endif -bool SystemAddress::FromString(const char *str, char portDelineator, int ipVersion) -{ -#if RAKNET_SUPPORT_IPV6!=1 - (void) ipVersion; - return SetBinaryAddress(str,portDelineator); -#else - if (str==0) - { - memset(&address,0,sizeof(address)); - address.addr4.sin_family=AF_INET; - return true; - } -#if RAKNET_SUPPORT_IPV6==1 - char ipPart[INET6_ADDRSTRLEN]; -#else - char ipPart[INET_ADDRSTRLEN]; -#endif - char portPart[32]; - int i=0,j; - - // TODO - what about 255.255.255.255? - if (ipVersion==4 && strcmp(str, IPV6_LOOPBACK)==0) - { - strcpy(ipPart,IPV4_LOOPBACK); - } - else if (ipVersion==6 && strcmp(str, IPV4_LOOPBACK)==0) - { - address.addr4.sin_family=AF_INET6; - strcpy(ipPart,IPV6_LOOPBACK); - } - else if (NonNumericHostString(str)==false) - { - for (; i < sizeof(ipPart) && str[i]!=0 && str[i]!=portDelineator; i++) - { - if ((str[i]<'0' || str[i]>'9') && (str[i]<'a' || str[i]>'f') && (str[i]<'A' || str[i]>'F') && str[i]!='.' && str[i]!=':' && str[i]!='%' && str[i]!='-' && str[i]!='/') - break; - - ipPart[i]=str[i]; - } - ipPart[i]=0; - } - else - { - strncpy(ipPart,str,sizeof(ipPart)); - ipPart[sizeof(ipPart)-1]=0; - } - - j=0; - if (str[i]==portDelineator && portDelineator!=0) - { - i++; - for (; j < sizeof(portPart) && str[i]!=0; i++, j++) - { - portPart[j]=str[i]; - } - } - portPart[j]=0; - - - - - - - - - - - // needed for getaddrinfo - WSAStartupSingleton::AddRef(); - - // This could be a domain, or a printable address such as "192.0.2.1" or "2001:db8:63b3:1::3490" - // I want to convert it to its binary representation - addrinfo hints, *servinfo=0; - memset(&hints, 0, sizeof hints); - hints.ai_socktype = SOCK_DGRAM; - if (ipVersion==6) - hints.ai_family = AF_INET6; - else if (ipVersion==4) - hints.ai_family = AF_INET; - else - hints.ai_family = AF_UNSPEC; - getaddrinfo(ipPart, "", &hints, &servinfo); - if (servinfo==0) - { - if (ipVersion==6) - { - ipVersion=4; - hints.ai_family = AF_UNSPEC; - getaddrinfo(ipPart, "", &hints, &servinfo); - if (servinfo==0) - return false; - } - else - return false; - } - RakAssert(servinfo); - - unsigned short oldPort = address.addr4.sin_port; -#if RAKNET_SUPPORT_IPV6==1 - if (servinfo->ai_family == AF_INET) - { -// if (ipVersion==6) -// { -// address.addr4.sin_family=AF_INET6; -// memset(&address.addr6,0,sizeof(address.addr6)); -// memcpy(address.addr6.sin6_addr.s6_addr+12,&((struct sockaddr_in *)servinfo->ai_addr)->sin_addr.s_addr,sizeof(unsigned long)); -// } -// else -// { - address.addr4.sin_family=AF_INET; - memcpy(&address.addr4, (struct sockaddr_in *)servinfo->ai_addr,sizeof(struct sockaddr_in)); -// } - } - else - { - address.addr4.sin_family=AF_INET6; - memcpy(&address.addr6, (struct sockaddr_in6 *)servinfo->ai_addr,sizeof(struct sockaddr_in6)); - } -#else - address.addr4.sin_family=AF_INET4; - memcpy(&address.addr4, (struct sockaddr_in *)servinfo->ai_addr,sizeof(struct sockaddr_in)); -#endif - - freeaddrinfo(servinfo); // free the linked list - - // needed for getaddrinfo - WSAStartupSingleton::Deref(); - - // PORT - if (portPart[0]) - { - address.addr4.sin_port=htons((unsigned short) atoi(portPart)); - debugPort=ntohs(address.addr4.sin_port); - } - else - { - address.addr4.sin_port=oldPort; - } -#endif // #if RAKNET_SUPPORT_IPV6!=1 - - return true; -} -bool SystemAddress::FromStringExplicitPort(const char *str, unsigned short port, int ipVersion) -{ - bool b = FromString(str,(char) 0,ipVersion); - if (b==false) - { - *this=UNASSIGNED_SYSTEM_ADDRESS; - return false; - } - address.addr4.sin_port=htons(port); - debugPort=ntohs(address.addr4.sin_port); - return true; -} -void SystemAddress::CopyPort( const SystemAddress& right ) -{ - address.addr4.sin_port=right.address.addr4.sin_port; - debugPort=right.debugPort; -} -RakNetGUID::RakNetGUID() -{ - systemIndex=(SystemIndex)-1; - *this=UNASSIGNED_RAKNET_GUID; -} -bool RakNetGUID::operator==( const RakNetGUID& right ) const -{ - return g==right.g; -} -bool RakNetGUID::operator!=( const RakNetGUID& right ) const -{ - return g!=right.g; -} -bool RakNetGUID::operator > ( const RakNetGUID& right ) const -{ - return g > right.g; -} -bool RakNetGUID::operator < ( const RakNetGUID& right ) const -{ - return g < right.g; -} -const char *RakNetGUID::ToString(void) const -{ - static unsigned char strIndex=0; - static char str[8][64]; - - unsigned char lastStrIndex=strIndex; - strIndex++; - ToString(str[lastStrIndex&7]); - return (char*) str[lastStrIndex&7]; -} -void RakNetGUID::ToString(char *dest) const -{ - if (*this==UNASSIGNED_RAKNET_GUID) - strcpy(dest, "UNASSIGNED_RAKNET_GUID"); - else - //sprintf(dest, "%u.%u.%u.%u.%u.%u", g[0], g[1], g[2], g[3], g[4], g[5]); - sprintf(dest, "%" PRINTF_64_BIT_MODIFIER "u", (long long unsigned int) g); - // sprintf(dest, "%u.%u.%u.%u.%u.%u", g[0], g[1], g[2], g[3], g[4], g[5]); -} -bool RakNetGUID::FromString(const char *source) -{ - if (source==0) - return false; - - - -#if defined(WIN32) - g=_strtoui64(source, NULL, 10); - - -#else - // Changed from g=strtoull(source,0,10); for android - g=strtoull(source, (char **)NULL, 10); -#endif - return true; - -} -unsigned long RakNetGUID::ToUint32( const RakNetGUID &g ) -{ - return ((unsigned long) (g.g >> 32)) ^ ((unsigned long) (g.g & 0xFFFFFFFF)); -} +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// + + +#include "RakNetTypes.h" +#include "RakAssert.h" +#include +#include +#include "WindowsIncludes.h" +#include "WSAStartupSingleton.h" +#include "SocketDefines.h" +#include "RakNetSocket2.h" + + +#if defined(_WIN32) +// extern __int64 _strtoui64(const char*, char**, int); // needed for Code::Blocks. Does not compile on Visual Studio 2010 +// IP_DONTFRAGMENT is different between winsock 1 and winsock 2. Therefore, Winsock2.h must be linked againt Ws2_32.lib +// winsock.h must be linked against WSock32.lib. If these two are mixed up the flag won't work correctly +#include "WindowsIncludes.h" + +#else +#include +#include +#include +#endif + +#include // strncasecmp +#include "Itoa.h" +#include "SocketLayer.h" +#include "SuperFastHash.h" +#include + +using namespace RakNet; + +const char *IPV6_LOOPBACK="::1"; +const char *IPV4_LOOPBACK="127.0.0.1"; + +AddressOrGUID::AddressOrGUID( Packet *packet ) +{ + rakNetGuid=packet->guid; + systemAddress=packet->systemAddress; +} + +unsigned long AddressOrGUID::ToInteger( const AddressOrGUID &aog ) +{ + if (aog.rakNetGuid!=UNASSIGNED_RAKNET_GUID) + return RakNetGUID::ToUint32(aog.rakNetGuid); + return SystemAddress::ToInteger(aog.systemAddress); +} +const char *AddressOrGUID::ToString(bool writePort) const +{ + if (rakNetGuid!=UNASSIGNED_RAKNET_GUID) + return rakNetGuid.ToString(); + return systemAddress.ToString(writePort); +} +void AddressOrGUID::ToString(bool writePort, char *dest) const +{ + if (rakNetGuid!=UNASSIGNED_RAKNET_GUID) + return rakNetGuid.ToString(dest); + return systemAddress.ToString(writePort,dest); +} +bool RakNet::NonNumericHostString( const char *host ) +{ + // Return false if IP address. Return true if domain + unsigned int i=0; + while (host[i]) + { + // IPV4: natpunch.jenkinssoftware.com + // IPV6: fe80::7c:31f7:fec4:27de%14 + if ((host[i]>='g' && host[i]<='z') || + (host[i]>='A' && host[i]<='Z')) + return true; + ++i; + } + return false; +} + +SocketDescriptor::SocketDescriptor() { +#ifdef __native_client__ + blockingSocket=false; +#else + blockingSocket=true; +#endif + port=0; hostAddress[0]=0; remotePortRakNetWasStartedOn_PS3_PSP2=0; extraSocketOptions=0; socketFamily=AF_INET;} +SocketDescriptor::SocketDescriptor(unsigned short _port, const char *_hostAddress) +{ + #ifdef __native_client__ + blockingSocket=false; + #else + blockingSocket=true; + #endif + remotePortRakNetWasStartedOn_PS3_PSP2=0; + port=_port; + if (_hostAddress) + { + strncpy(hostAddress, _hostAddress, sizeof(hostAddress) - 1); + hostAddress[sizeof(hostAddress) - 1] = '\0'; + } + else + hostAddress[0]=0; + extraSocketOptions=0; + socketFamily=AF_INET; +} + +// Defaults to not in peer to peer mode for NetworkIDs. This only sends the localSystemAddress portion in the BitStream class +// This is what you want for client/server, where the server assigns all NetworkIDs and it is unnecessary to transmit the full structure. +// For peer to peer, this will transmit the systemAddress of the system that created the object in addition to localSystemAddress. This allows +// Any system to create unique ids locally. +// All systems must use the same value for this variable. +//bool RAK_DLL_EXPORT NetworkID::peerToPeerMode=false; + +SystemAddress& SystemAddress::operator = ( const SystemAddress& input ) +{ + memcpy(&address, &input.address, sizeof(address)); + systemIndex = input.systemIndex; + debugPort = input.debugPort; + return *this; +} +bool SystemAddress::EqualsExcludingPort( const SystemAddress& right ) const +{ + return (address.addr4.sin_family==AF_INET && address.addr4.sin_addr.s_addr==right.address.addr4.sin_addr.s_addr) +#if RAKNET_SUPPORT_IPV6==1 + || (address.addr4.sin_family==AF_INET6 && memcmp(address.addr6.sin6_addr.s6_addr, right.address.addr6.sin6_addr.s6_addr, sizeof(address.addr6.sin6_addr.s6_addr))==0) +#endif + ; +} +unsigned short SystemAddress::GetPort(void) const +{ + return ntohs(address.addr4.sin_port); +} +unsigned short SystemAddress::GetPortNetworkOrder(void) const +{ + return address.addr4.sin_port; +} +void SystemAddress::SetPortHostOrder(unsigned short s) +{ + address.addr4.sin_port=htons(s); + debugPort=s; +} +void SystemAddress::SetPortNetworkOrder(unsigned short s) +{ + address.addr4.sin_port=s; + debugPort=ntohs(s); +} +bool SystemAddress::operator==( const SystemAddress& right ) const +{ + return address.addr4.sin_port == right.address.addr4.sin_port && EqualsExcludingPort(right); +} + +bool SystemAddress::operator!=( const SystemAddress& right ) const +{ + return (*this==right)==false; +} + +bool SystemAddress::operator>( const SystemAddress& right ) const +{ + if (address.addr4.sin_port == right.address.addr4.sin_port) + { +#if RAKNET_SUPPORT_IPV6==1 + if (address.addr4.sin_family==AF_INET) + return address.addr4.sin_addr.s_addr>right.address.addr4.sin_addr.s_addr; + return memcmp(address.addr6.sin6_addr.s6_addr, right.address.addr6.sin6_addr.s6_addr, sizeof(address.addr6.sin6_addr.s6_addr))>0; +#else + return address.addr4.sin_addr.s_addr>right.address.addr4.sin_addr.s_addr; +#endif + } + return address.addr4.sin_port>right.address.addr4.sin_port; +} + +bool SystemAddress::operator<( const SystemAddress& right ) const +{ + if (address.addr4.sin_port == right.address.addr4.sin_port) + { +#if RAKNET_SUPPORT_IPV6==1 + if (address.addr4.sin_family==AF_INET) + return address.addr4.sin_addr.s_addr0; +#else + return address.addr4.sin_addr.s_addr> 24) == 10 || (address.addr4.sin_addr.s_addr >> 24) == 192; +#endif +} +bool SystemAddress::SetBinaryAddress(const char *str, char portDelineator) +{ + if ( NonNumericHostString( str ) ) + { + +#if defined(_WIN32) + if (_strnicmp(str,"localhost", 9)==0) +#else + if (strncasecmp(str,"localhost", 9)==0) +#endif + { + + + + + + address.addr4.sin_addr.s_addr=inet_addr__("127.0.0.1"); + + if (str[9]) + { + SetPortHostOrder((unsigned short) atoi(str+9)); + } + return true; + } + + //const char *ip = ( char* ) SocketLayer::DomainNameToIP( str ); + char ip[65]; + ip[0]=0; + RakNetSocket2::DomainNameToIP(str, ip); + if (ip[0]) + { + + + + + + address.addr4.sin_addr.s_addr=inet_addr__(ip); + + } + else + { + *this = UNASSIGNED_SYSTEM_ADDRESS; + return false; + } + } + else + { + //#ifdef _XBOX + // binaryAddress=UNASSIGNED_SYSTEM_ADDRESS.binaryAddress; + //#else + // Split the string into the first part, and the : part + int index, portIndex; + char IPPart[22]; + char portPart[10]; + // Only write the valid parts, don't change existing if invalid + // binaryAddress=UNASSIGNED_SYSTEM_ADDRESS.binaryAddress; + // port=UNASSIGNED_SYSTEM_ADDRESS.port; + for (index=0; str[index] && str[index]!=portDelineator && index<22; index++) + { + if (str[index]!='.' && (str[index]<'0' || str[index]>'9')) + break; + IPPart[index]=str[index]; + } + IPPart[index]=0; + portPart[0]=0; + if (str[index] && str[index+1]) + { + index++; + for (portIndex=0; portIndex<10 && str[index] && index < 22+10; index++, portIndex++) + { + if (str[index]<'0' || str[index]>'9') + break; + + portPart[portIndex]=str[index]; + } + portPart[portIndex]=0; + } + + + + + + + + + + + + + + if (IPPart[0]) + { + + + + + + address.addr4.sin_addr.s_addr=inet_addr__(IPPart); + + } + + + if (portPart[0]) + { + address.addr4.sin_port=htons((unsigned short) atoi(portPart)); + debugPort=ntohs(address.addr4.sin_port); + } + //#endif + } + return true; +} + +#ifdef _MSC_VER +#pragma warning( disable : 4702 ) // warning C4702: unreachable code +#endif +bool SystemAddress::FromString(const char *str, char portDelineator, int ipVersion) +{ +#if RAKNET_SUPPORT_IPV6!=1 + (void) ipVersion; + return SetBinaryAddress(str,portDelineator); +#else + if (str==0) + { + memset(&address,0,sizeof(address)); + address.addr4.sin_family=AF_INET; + return true; + } +#if RAKNET_SUPPORT_IPV6==1 + char ipPart[INET6_ADDRSTRLEN]; +#else + char ipPart[INET_ADDRSTRLEN]; +#endif + char portPart[32]; + int i=0,j; + + // TODO - what about 255.255.255.255? + if (ipVersion==4 && strcmp(str, IPV6_LOOPBACK)==0) + { + strcpy(ipPart,IPV4_LOOPBACK); + } + else if (ipVersion==6 && strcmp(str, IPV4_LOOPBACK)==0) + { + address.addr4.sin_family=AF_INET6; + strcpy(ipPart,IPV6_LOOPBACK); + } + else if (NonNumericHostString(str)==false) + { + for (; i < sizeof(ipPart) && str[i]!=0 && str[i]!=portDelineator; i++) + { + if ((str[i]<'0' || str[i]>'9') && (str[i]<'a' || str[i]>'f') && (str[i]<'A' || str[i]>'F') && str[i]!='.' && str[i]!=':' && str[i]!='%' && str[i]!='-' && str[i]!='/') + break; + + ipPart[i]=str[i]; + } + ipPart[i]=0; + } + else + { + strncpy(ipPart,str,sizeof(ipPart)); + ipPart[sizeof(ipPart)-1]=0; + } + + j=0; + if (str[i]==portDelineator && portDelineator!=0) + { + i++; + for (; j < sizeof(portPart) && str[i]!=0; i++, j++) + { + portPart[j]=str[i]; + } + } + portPart[j]=0; + + + + + + + + + + + // needed for getaddrinfo + WSAStartupSingleton::AddRef(); + + // This could be a domain, or a printable address such as "192.0.2.1" or "2001:db8:63b3:1::3490" + // I want to convert it to its binary representation + addrinfo hints, *servinfo=0; + memset(&hints, 0, sizeof hints); + hints.ai_socktype = SOCK_DGRAM; + if (ipVersion==6) + hints.ai_family = AF_INET6; + else if (ipVersion==4) + hints.ai_family = AF_INET; + else + hints.ai_family = AF_UNSPEC; + getaddrinfo(ipPart, "", &hints, &servinfo); + if (servinfo==0) + { + if (ipVersion==6) + { + ipVersion=4; + hints.ai_family = AF_UNSPEC; + getaddrinfo(ipPart, "", &hints, &servinfo); + if (servinfo==0) + return false; + } + else + return false; + } + RakAssert(servinfo); + + unsigned short oldPort = address.addr4.sin_port; +#if RAKNET_SUPPORT_IPV6==1 + if (servinfo->ai_family == AF_INET) + { +// if (ipVersion==6) +// { +// address.addr4.sin_family=AF_INET6; +// memset(&address.addr6,0,sizeof(address.addr6)); +// memcpy(address.addr6.sin6_addr.s6_addr+12,&((struct sockaddr_in *)servinfo->ai_addr)->sin_addr.s_addr,sizeof(unsigned long)); +// } +// else +// { + address.addr4.sin_family=AF_INET; + memcpy(&address.addr4, (struct sockaddr_in *)servinfo->ai_addr,sizeof(struct sockaddr_in)); +// } + } + else + { + address.addr4.sin_family=AF_INET6; + memcpy(&address.addr6, (struct sockaddr_in6 *)servinfo->ai_addr,sizeof(struct sockaddr_in6)); + } +#else + address.addr4.sin_family=AF_INET4; + memcpy(&address.addr4, (struct sockaddr_in *)servinfo->ai_addr,sizeof(struct sockaddr_in)); +#endif + + freeaddrinfo(servinfo); // free the linked list + + // needed for getaddrinfo + WSAStartupSingleton::Deref(); + + // PORT + if (portPart[0]) + { + address.addr4.sin_port=htons((unsigned short) atoi(portPart)); + debugPort=ntohs(address.addr4.sin_port); + } + else + { + address.addr4.sin_port=oldPort; + } +#endif // #if RAKNET_SUPPORT_IPV6!=1 + + return true; +} +bool SystemAddress::FromStringExplicitPort(const char *str, unsigned short port, int ipVersion) +{ + bool b = FromString(str,(char) 0,ipVersion); + if (b==false) + { + *this=UNASSIGNED_SYSTEM_ADDRESS; + return false; + } + address.addr4.sin_port=htons(port); + debugPort=ntohs(address.addr4.sin_port); + return true; +} +void SystemAddress::CopyPort( const SystemAddress& right ) +{ + address.addr4.sin_port=right.address.addr4.sin_port; + debugPort=right.debugPort; +} +RakNetGUID::RakNetGUID() +{ + systemIndex=(SystemIndex)-1; + *this=UNASSIGNED_RAKNET_GUID; +} +bool RakNetGUID::operator==( const RakNetGUID& right ) const +{ + return g==right.g; +} +bool RakNetGUID::operator!=( const RakNetGUID& right ) const +{ + return g!=right.g; +} +bool RakNetGUID::operator > ( const RakNetGUID& right ) const +{ + return g > right.g; +} +bool RakNetGUID::operator < ( const RakNetGUID& right ) const +{ + return g < right.g; +} +const char *RakNetGUID::ToString(void) const +{ + static unsigned char strIndex=0; + static char str[8][64]; + + unsigned char lastStrIndex=strIndex; + strIndex++; + ToString(str[lastStrIndex&7]); + return (char*) str[lastStrIndex&7]; +} +void RakNetGUID::ToString(char *dest) const +{ + if (*this==UNASSIGNED_RAKNET_GUID) + strcpy(dest, "UNASSIGNED_RAKNET_GUID"); + else + //sprintf(dest, "%u.%u.%u.%u.%u.%u", g[0], g[1], g[2], g[3], g[4], g[5]); + sprintf(dest, "%" PRINTF_64_BIT_MODIFIER "u", (long long unsigned int) g); + // sprintf(dest, "%u.%u.%u.%u.%u.%u", g[0], g[1], g[2], g[3], g[4], g[5]); +} +bool RakNetGUID::FromString(const char *source) +{ + if (source==0) + return false; + + + +#if defined(WIN32) + g=_strtoui64(source, nullptr, 10); + + +#else + // Changed from g=strtoull(source,0,10); for android + g=strtoull(source, (char **)nullptr, 10); +#endif + return true; + +} +unsigned long RakNetGUID::ToUint32( const RakNetGUID &g ) +{ + return ((unsigned long) (g.g >> 32)) ^ ((unsigned long) (g.g & 0xFFFFFFFF)); +} diff --git a/Source/RakNetTypes.h b/Source/RakNetTypes.h index 31a147bc8..8739526a0 100644 --- a/Source/RakNetTypes.h +++ b/Source/RakNetTypes.h @@ -1,507 +1,505 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Types used by RakNet, most of which involve user code. -/// - - -#ifndef __NETWORK_TYPES_H -#define __NETWORK_TYPES_H - - - - - -#include "RakNetDefines.h" -#include "NativeTypes.h" -#include "RakNetTime.h" -#include "Export.h" -#include "WindowsIncludes.h" -#include "XBox360Includes.h" -#include "SocketIncludes.h" - - - - - -namespace RakNet { -/// Forward declarations -class RakPeerInterface; -class BitStream; -struct Packet; - -enum StartupResult -{ - RAKNET_STARTED, - RAKNET_ALREADY_STARTED, - INVALID_SOCKET_DESCRIPTORS, - INVALID_MAX_CONNECTIONS, - SOCKET_FAMILY_NOT_SUPPORTED, - SOCKET_PORT_ALREADY_IN_USE, - SOCKET_FAILED_TO_BIND, - SOCKET_FAILED_TEST_SEND, - PORT_CANNOT_BE_ZERO, - FAILED_TO_CREATE_NETWORK_THREAD, - COULD_NOT_GENERATE_GUID, - STARTUP_OTHER_FAILURE -}; - - -enum ConnectionAttemptResult -{ - CONNECTION_ATTEMPT_STARTED, - INVALID_PARAMETER, - CANNOT_RESOLVE_DOMAIN_NAME, - ALREADY_CONNECTED_TO_ENDPOINT, - CONNECTION_ATTEMPT_ALREADY_IN_PROGRESS, - SECURITY_INITIALIZATION_FAILED -}; - -/// Returned from RakPeerInterface::GetConnectionState() -enum ConnectionState -{ - /// Connect() was called, but the process hasn't started yet - IS_PENDING, - /// Processing the connection attempt - IS_CONNECTING, - /// Is connected and able to communicate - IS_CONNECTED, - /// Was connected, but will disconnect as soon as the remaining messages are delivered - IS_DISCONNECTING, - /// A connection attempt failed and will be aborted - IS_SILENTLY_DISCONNECTING, - /// No longer connected - IS_DISCONNECTED, - /// Was never connected, or else was disconnected long enough ago that the entry has been discarded - IS_NOT_CONNECTED -}; - -/// Given a number of bits, return how many bytes are needed to represent that. -#define BITS_TO_BYTES(x) (((x)+7)>>3) -#define BYTES_TO_BITS(x) ((x)<<3) - -/// \sa NetworkIDObject.h -typedef unsigned char UniqueIDType; -typedef unsigned short SystemIndex; -typedef unsigned char RPCIndex; -const int MAX_RPC_MAP_SIZE=((RPCIndex)-1)-1; -const int UNDEFINED_RPC_INDEX=((RPCIndex)-1); - -/// First byte of a network message -typedef unsigned char MessageID; - -typedef uint32_t BitSize_t; - -#if defined(_MSC_VER) && _MSC_VER > 0 -#define PRINTF_64_BIT_MODIFIER "I64" -#else -#define PRINTF_64_BIT_MODIFIER "ll" -#endif - -/// Used with the PublicKey structure -enum PublicKeyMode -{ - /// The connection is insecure. You can also just pass 0 for the pointer to PublicKey in RakPeerInterface::Connect() - PKM_INSECURE_CONNECTION, - - /// Accept whatever public key the server gives us. This is vulnerable to man in the middle, but does not require - /// distribution of the public key in advance of connecting. - PKM_ACCEPT_ANY_PUBLIC_KEY, - - /// Use a known remote server public key. PublicKey::remoteServerPublicKey must be non-zero. - /// This is the recommended mode for secure connections. - PKM_USE_KNOWN_PUBLIC_KEY, - - /// Use a known remote server public key AND provide a public key for the connecting client. - /// PublicKey::remoteServerPublicKey, myPublicKey and myPrivateKey must be all be non-zero. - /// The server must cooperate for this mode to work. - /// I recommend not using this mode except for server-to-server communication as it significantly increases the CPU requirements during connections for both sides. - /// Furthermore, when it is used, a connection password should be used as well to avoid DoS attacks. - PKM_USE_TWO_WAY_AUTHENTICATION -}; - -/// Passed to RakPeerInterface::Connect() -struct RAK_DLL_EXPORT PublicKey -{ - /// How to interpret the public key, see above - PublicKeyMode publicKeyMode; - - /// Pointer to a public key of length cat::EasyHandshake::PUBLIC_KEY_BYTES. See the Encryption sample. - char *remoteServerPublicKey; - - /// (Optional) Pointer to a public key of length cat::EasyHandshake::PUBLIC_KEY_BYTES - char *myPublicKey; - - /// (Optional) Pointer to a private key of length cat::EasyHandshake::PRIVATE_KEY_BYTES - char *myPrivateKey; -}; - -/// Describes the local socket to use for RakPeer::Startup -struct RAK_DLL_EXPORT SocketDescriptor -{ - SocketDescriptor(); - SocketDescriptor(unsigned short _port, const char *_hostAddress); - - /// The local port to bind to. Pass 0 to have the OS autoassign a port. - unsigned short port; - - /// The local network card address to bind to, such as "127.0.0.1". Pass an empty string to use INADDR_ANY. - char hostAddress[32]; - - /// IP version: For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC. - /// IPV6 is the newer internet protocol. Instead of addresses such as natpunch.jenkinssoftware.com, you may have an address such as fe80::7c:31f7:fec4:27de%14. - /// Encoding takes 16 bytes instead of 4, so IPV6 is less efficient for bandwidth. - /// On the positive side, NAT Punchthrough is not needed and should not be used with IPV6 because there are enough addresses that routers do not need to create address mappings. - /// RakPeer::Startup() will fail if this IP version is not supported. - /// \pre RAKNET_SUPPORT_IPV6 must be set to 1 in RakNetDefines.h for AF_INET6 - short socketFamily; - - - - - - - - - - unsigned short remotePortRakNetWasStartedOn_PS3_PSP2; - - // Required for Google chrome - _PP_Instance_ chromeInstance; - - // Set to true to use a blocking socket (default, do not change unless you have a reason to) - bool blockingSocket; - - /// XBOX only: set IPPROTO_VDP if you want to use VDP. If enabled, this socket does not support broadcast to 255.255.255.255 - unsigned int extraSocketOptions; -}; - -extern bool NonNumericHostString( const char *host ); - -/// \brief Network address for a system -/// \details Corresponds to a network address
-/// This is not necessarily a unique identifier. For example, if a system has both LAN and internet connections, the system may be identified by either one, depending on who is communicating
-/// Therefore, you should not transmit the SystemAddress over the network and expect it to identify a system, or use it to connect to that system, except in the case where that system is not behind a NAT (such as with a dedciated server) -/// Use RakNetGUID for a unique per-instance of RakPeer to identify systems -struct RAK_DLL_EXPORT SystemAddress -{ - /// Constructors - SystemAddress(); - SystemAddress(const char *str); - SystemAddress(const char *str, unsigned short port); - - - - - - - - - - - /// SystemAddress, with RAKNET_SUPPORT_IPV6 defined, holds both an sockaddr_in6 and a sockaddr_in - union// In6OrIn4 - { -#if RAKNET_SUPPORT_IPV6==1 - struct sockaddr_storage sa_stor; - sockaddr_in6 addr6; -#endif - - sockaddr_in addr4; - } address; - - /// This is not used internally, but holds a copy of the port held in the address union, so for debugging it's easier to check what port is being held - unsigned short debugPort; - - /// \internal Return the size to write to a bitStream - static int size(void); - - /// Hash the system address - static unsigned long ToInteger( const SystemAddress &sa ); - - /// Return the IP version, either IPV4 or IPV6 - /// \return Either 4 or 6 - unsigned char GetIPVersion(void) const; - - /// \internal Returns either IPPROTO_IP or IPPROTO_IPV6 - /// \sa GetIPVersion - unsigned int GetIPPROTO(void) const; - - /// Call SetToLoopback(), with whatever IP version is currently held. Defaults to IPV4 - void SetToLoopback(void); - - /// Call SetToLoopback() with a specific IP version - /// \param[in] ipVersion Either 4 for IPV4 or 6 for IPV6 - void SetToLoopback(unsigned char ipVersion); - - /// \return If was set to 127.0.0.1 or ::1 - bool IsLoopback(void) const; - - // Return the systemAddress as a string in the format | - // Returns a static string - // NOT THREADSAFE - // portDelineator should not be '.', ':', '%', '-', '/', a number, or a-f - const char *ToString(bool writePort=true, char portDelineator='|') const; - - // Return the systemAddress as a string in the format | - // dest must be large enough to hold the output - // portDelineator should not be '.', ':', '%', '-', '/', a number, or a-f - // THREADSAFE - void ToString(bool writePort, char *dest, char portDelineator='|') const; - - /// Set the system address from a printable IP string, for example "192.0.2.1" or "2001:db8:63b3:1::3490" - /// You can write the port as well, using the portDelineator, for example "192.0.2.1|1234" - /// \param[in] str A printable IP string, for example "192.0.2.1" or "2001:db8:63b3:1::3490". Pass 0 for \a str to set to UNASSIGNED_SYSTEM_ADDRESS - /// \param[in] portDelineator if \a str contains a port, delineate the port with this character. portDelineator should not be '.', ':', '%', '-', '/', a number, or a-f - /// \param[in] ipVersion Only used if str is a pre-defined address in the wrong format, such as 127.0.0.1 but you want ip version 6, so you can pass 6 here to do the conversion - /// \note The current port is unchanged if a port is not specified in \a str - /// \return True on success, false on ipVersion does not match type of passed string - bool FromString(const char *str, char portDelineator='|', int ipVersion=0); - - /// Same as FromString(), but you explicitly set a port at the same time - bool FromStringExplicitPort(const char *str, unsigned short port, int ipVersion=0); - - /// Copy the port from another SystemAddress structure - void CopyPort( const SystemAddress& right ); - - /// Returns if two system addresses have the same IP (port is not checked) - bool EqualsExcludingPort( const SystemAddress& right ) const; - - /// Returns the port in host order (this is what you normally use) - unsigned short GetPort(void) const; - - /// \internal Returns the port in network order - unsigned short GetPortNetworkOrder(void) const; - - /// Sets the port. The port value should be in host order (this is what you normally use) - /// Renamed from SetPort because of winspool.h http://edn.embarcadero.com/article/21494 - void SetPortHostOrder(unsigned short s); - - /// \internal Sets the port. The port value should already be in network order. - void SetPortNetworkOrder(unsigned short s); - - /// Old version, for crap platforms that don't support newer socket functions - bool SetBinaryAddress(const char *str, char portDelineator=':'); - /// Old version, for crap platforms that don't support newer socket functions - void ToString_Old(bool writePort, char *dest, char portDelineator=':') const; - - /// \internal sockaddr_in6 requires extra data beyond just the IP and port. Copy that extra data from an existing SystemAddress that already has it - void FixForIPVersion(const SystemAddress &boundAddressToSocket); - - bool IsLANAddress(void); - - SystemAddress& operator = ( const SystemAddress& input ); - bool operator==( const SystemAddress& right ) const; - bool operator!=( const SystemAddress& right ) const; - bool operator > ( const SystemAddress& right ) const; - bool operator < ( const SystemAddress& right ) const; - - /// \internal Used internally for fast lookup. Optional (use -1 to do regular lookup). Don't transmit this. - SystemIndex systemIndex; - - private: - -#if RAKNET_SUPPORT_IPV6==1 - void ToString_New(bool writePort, char *dest, char portDelineator) const; -#endif -}; - -/// Uniquely identifies an instance of RakPeer. Use RakPeer::GetGuidFromSystemAddress() and RakPeer::GetSystemAddressFromGuid() to go between SystemAddress and RakNetGUID -/// Use RakPeer::GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS) to get your own GUID -struct RAK_DLL_EXPORT RakNetGUID -{ - RakNetGUID(); - explicit RakNetGUID(uint64_t _g) {g=_g; systemIndex=(SystemIndex)-1;} -// uint32_t g[6]; - uint64_t g; - - // Return the GUID as a string - // Returns a static string - // NOT THREADSAFE - const char *ToString(void) const; - - // Return the GUID as a string - // dest must be large enough to hold the output - // THREADSAFE - void ToString(char *dest) const; - - bool FromString(const char *source); - - static unsigned long ToUint32( const RakNetGUID &g ); - - RakNetGUID& operator = ( const RakNetGUID& input ) - { - g=input.g; - systemIndex=input.systemIndex; - return *this; - } - - // Used internally for fast lookup. Optional (use -1 to do regular lookup). Don't transmit this. - SystemIndex systemIndex; - static int size() {return (int) sizeof(uint64_t);} - - bool operator==( const RakNetGUID& right ) const; - bool operator!=( const RakNetGUID& right ) const; - bool operator > ( const RakNetGUID& right ) const; - bool operator < ( const RakNetGUID& right ) const; -}; - -/// Index of an invalid SystemAddress -//const SystemAddress UNASSIGNED_SYSTEM_ADDRESS = -//{ -// 0xFFFFFFFF, 0xFFFF -//}; -#ifndef SWIG -const SystemAddress UNASSIGNED_SYSTEM_ADDRESS; -const RakNetGUID UNASSIGNED_RAKNET_GUID((uint64_t)-1); -#endif -//{ -// {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF} -// 0xFFFFFFFFFFFFFFFF -//}; - - -struct RAK_DLL_EXPORT AddressOrGUID -{ - RakNetGUID rakNetGuid; - SystemAddress systemAddress; - - SystemIndex GetSystemIndex(void) const {if (rakNetGuid!=UNASSIGNED_RAKNET_GUID) return rakNetGuid.systemIndex; else return systemAddress.systemIndex;} - bool IsUndefined(void) const {return rakNetGuid==UNASSIGNED_RAKNET_GUID && systemAddress==UNASSIGNED_SYSTEM_ADDRESS;} - void SetUndefined(void) {rakNetGuid=UNASSIGNED_RAKNET_GUID; systemAddress=UNASSIGNED_SYSTEM_ADDRESS;} - static unsigned long ToInteger( const AddressOrGUID &aog ); - const char *ToString(bool writePort=true) const; - void ToString(bool writePort, char *dest) const; - - AddressOrGUID() {} - AddressOrGUID( const AddressOrGUID& input ) - { - rakNetGuid=input.rakNetGuid; - systemAddress=input.systemAddress; - } - AddressOrGUID( const SystemAddress& input ) - { - rakNetGuid=UNASSIGNED_RAKNET_GUID; - systemAddress=input; - } - AddressOrGUID( Packet *packet ); - AddressOrGUID( const RakNetGUID& input ) - { - rakNetGuid=input; - systemAddress=UNASSIGNED_SYSTEM_ADDRESS; - } - AddressOrGUID& operator = ( const AddressOrGUID& input ) - { - rakNetGuid=input.rakNetGuid; - systemAddress=input.systemAddress; - return *this; - } - - AddressOrGUID& operator = ( const SystemAddress& input ) - { - rakNetGuid=UNASSIGNED_RAKNET_GUID; - systemAddress=input; - return *this; - } - - AddressOrGUID& operator = ( const RakNetGUID& input ) - { - rakNetGuid=input; - systemAddress=UNASSIGNED_SYSTEM_ADDRESS; - return *this; - } - - inline bool operator==( const AddressOrGUID& right ) const {return (rakNetGuid!=UNASSIGNED_RAKNET_GUID && rakNetGuid==right.rakNetGuid) || (systemAddress!=UNASSIGNED_SYSTEM_ADDRESS && systemAddress==right.systemAddress);} -}; - -typedef uint64_t NetworkID; - -/// This represents a user message from another system. -struct Packet -{ - /// The system that send this packet. - SystemAddress systemAddress; - - /// A unique identifier for the system that sent this packet, regardless of IP address (internal / external / remote system) - /// Only valid once a connection has been established (ID_CONNECTION_REQUEST_ACCEPTED, or ID_NEW_INCOMING_CONNECTION) - /// Until that time, will be UNASSIGNED_RAKNET_GUID - RakNetGUID guid; - - /// The length of the data in bytes - unsigned int length; - - /// The length of the data in bits - BitSize_t bitSize; - - /// The data from the sender - unsigned char* data; - - /// @internal - /// Indicates whether to delete the data, or to simply delete the packet. - bool deleteData; - - /// @internal - /// If true, this message is meant for the user, not for the plugins, so do not process it through plugins - bool wasGeneratedLocally; -}; - -/// Index of an unassigned player -const SystemIndex UNASSIGNED_PLAYER_INDEX = 65535; - -/// Unassigned object ID -const NetworkID UNASSIGNED_NETWORK_ID = (uint64_t) -1; - -const int PING_TIMES_ARRAY_SIZE = 5; - -struct RAK_DLL_EXPORT uint24_t -{ - uint32_t val; - - uint24_t() {} - inline operator uint32_t() { return val; } - inline operator uint32_t() const { return val; } - - inline uint24_t(const uint24_t& a) {val=a.val;} - inline uint24_t operator++() {++val; val&=0x00FFFFFF; return *this;} - inline uint24_t operator--() {--val; val&=0x00FFFFFF; return *this;} - inline uint24_t operator++(int) {uint24_t temp(val); ++val; val&=0x00FFFFFF; return temp;} - inline uint24_t operator--(int) {uint24_t temp(val); --val; val&=0x00FFFFFF; return temp;} - inline uint24_t operator&(const uint24_t& a) {return uint24_t(val&a.val);} - inline uint24_t& operator=(const uint24_t& a) { val=a.val; return *this; } - inline uint24_t& operator+=(const uint24_t& a) { val+=a.val; val&=0x00FFFFFF; return *this; } - inline uint24_t& operator-=(const uint24_t& a) { val-=a.val; val&=0x00FFFFFF; return *this; } - inline bool operator==( const uint24_t& right ) const {return val==right.val;} - inline bool operator!=( const uint24_t& right ) const {return val!=right.val;} - inline bool operator > ( const uint24_t& right ) const {return val>right.val;} - inline bool operator < ( const uint24_t& right ) const {return val ( const uint32_t& right ) const {return val>(right&0x00FFFFFF);} - inline bool operator < ( const uint32_t& right ) const {return val<(right&0x00FFFFFF);} - inline const uint24_t operator+( const uint32_t &other ) const { return uint24_t(val+other); } - inline const uint24_t operator-( const uint32_t &other ) const { return uint24_t(val-other); } - inline const uint24_t operator/( const uint32_t &other ) const { return uint24_t(val/other); } - inline const uint24_t operator*( const uint32_t &other ) const { return uint24_t(val*other); } -}; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Types used by RakNet, most of which involve user code. +/// + + +#pragma once + + + + + +#include "RakNetDefines.h" +#include "NativeTypes.h" +#include "RakNetTime.h" +#include "Export.h" +#include "WindowsIncludes.h" +#include "XBox360Includes.h" +#include "SocketIncludes.h" + + + + + +namespace RakNet { +/// Forward declarations +class RakPeerInterface; +class BitStream; +struct Packet; + +enum StartupResult +{ + RAKNET_STARTED, + RAKNET_ALREADY_STARTED, + INVALID_SOCKET_DESCRIPTORS, + INVALID_MAX_CONNECTIONS, + SOCKET_FAMILY_NOT_SUPPORTED, + SOCKET_PORT_ALREADY_IN_USE, + SOCKET_FAILED_TO_BIND, + SOCKET_FAILED_TEST_SEND, + PORT_CANNOT_BE_ZERO, + FAILED_TO_CREATE_NETWORK_THREAD, + COULD_NOT_GENERATE_GUID, + STARTUP_OTHER_FAILURE +}; + + +enum ConnectionAttemptResult +{ + CONNECTION_ATTEMPT_STARTED, + INVALID_PARAMETER, + CANNOT_RESOLVE_DOMAIN_NAME, + ALREADY_CONNECTED_TO_ENDPOINT, + CONNECTION_ATTEMPT_ALREADY_IN_PROGRESS, + SECURITY_INITIALIZATION_FAILED +}; + +/// Returned from RakPeerInterface::GetConnectionState() +enum ConnectionState +{ + /// Connect() was called, but the process hasn't started yet + IS_PENDING, + /// Processing the connection attempt + IS_CONNECTING, + /// Is connected and able to communicate + IS_CONNECTED, + /// Was connected, but will disconnect as soon as the remaining messages are delivered + IS_DISCONNECTING, + /// A connection attempt failed and will be aborted + IS_SILENTLY_DISCONNECTING, + /// No longer connected + IS_DISCONNECTED, + /// Was never connected, or else was disconnected long enough ago that the entry has been discarded + IS_NOT_CONNECTED +}; + +/// Given a number of bits, return how many bytes are needed to represent that. +#define BITS_TO_BYTES(x) (((x)+7)>>3) +#define BYTES_TO_BITS(x) ((x)<<3) + +/// \sa NetworkIDObject.h +typedef unsigned char UniqueIDType; +typedef unsigned short SystemIndex; +typedef unsigned char RPCIndex; +const int MAX_RPC_MAP_SIZE=((RPCIndex)-1)-1; +const int UNDEFINED_RPC_INDEX=((RPCIndex)-1); + +/// First byte of a network message +typedef unsigned char MessageID; + +typedef uint32_t BitSize_t; + +#if defined(_MSC_VER) && _MSC_VER > 0 +#define PRINTF_64_BIT_MODIFIER "I64" +#else +#define PRINTF_64_BIT_MODIFIER "ll" +#endif + +/// Used with the PublicKey structure +enum PublicKeyMode +{ + /// The connection is insecure. You can also just pass 0 for the pointer to PublicKey in RakPeerInterface::Connect() + PKM_INSECURE_CONNECTION, + + /// Accept whatever public key the server gives us. This is vulnerable to man in the middle, but does not require + /// distribution of the public key in advance of connecting. + PKM_ACCEPT_ANY_PUBLIC_KEY, + + /// Use a known remote server public key. PublicKey::remoteServerPublicKey must be non-zero. + /// This is the recommended mode for secure connections. + PKM_USE_KNOWN_PUBLIC_KEY, + + /// Use a known remote server public key AND provide a public key for the connecting client. + /// PublicKey::remoteServerPublicKey, myPublicKey and myPrivateKey must be all be non-zero. + /// The server must cooperate for this mode to work. + /// I recommend not using this mode except for server-to-server communication as it significantly increases the CPU requirements during connections for both sides. + /// Furthermore, when it is used, a connection password should be used as well to avoid DoS attacks. + PKM_USE_TWO_WAY_AUTHENTICATION +}; + +/// Passed to RakPeerInterface::Connect() +struct RAK_DLL_EXPORT PublicKey +{ + /// How to interpret the public key, see above + PublicKeyMode publicKeyMode; + + /// Pointer to a public key of length cat::EasyHandshake::PUBLIC_KEY_BYTES. See the Encryption sample. + char *remoteServerPublicKey; + + /// (Optional) Pointer to a public key of length cat::EasyHandshake::PUBLIC_KEY_BYTES + char *myPublicKey; + + /// (Optional) Pointer to a private key of length cat::EasyHandshake::PRIVATE_KEY_BYTES + char *myPrivateKey; +}; + +/// Describes the local socket to use for RakPeer::Startup +struct RAK_DLL_EXPORT SocketDescriptor +{ + SocketDescriptor(); + SocketDescriptor(unsigned short _port, const char *_hostAddress); + + /// The local port to bind to. Pass 0 to have the OS autoassign a port. + unsigned short port; + + /// The local network card address to bind to, such as "127.0.0.1". Pass an empty string to use INADDR_ANY. + char hostAddress[32]; + + /// IP version: For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC. + /// IPV6 is the newer internet protocol. Instead of addresses such as natpunch.jenkinssoftware.com, you may have an address such as fe80::7c:31f7:fec4:27de%14. + /// Encoding takes 16 bytes instead of 4, so IPV6 is less efficient for bandwidth. + /// On the positive side, NAT Punchthrough is not needed and should not be used with IPV6 because there are enough addresses that routers do not need to create address mappings. + /// RakPeer::Startup() will fail if this IP version is not supported. + /// \pre RAKNET_SUPPORT_IPV6 must be set to 1 in RakNetDefines.h for AF_INET6 + short socketFamily; + + + + + + + + + + unsigned short remotePortRakNetWasStartedOn_PS3_PSP2; + + // Required for Google chrome + _PP_Instance_ chromeInstance; + + // Set to true to use a blocking socket (default, do not change unless you have a reason to) + bool blockingSocket; + + /// XBOX only: set IPPROTO_VDP if you want to use VDP. If enabled, this socket does not support broadcast to 255.255.255.255 + unsigned int extraSocketOptions; +}; + +extern bool NonNumericHostString( const char *host ); + +/// \brief Network address for a system +/// \details Corresponds to a network address
+/// This is not necessarily a unique identifier. For example, if a system has both LAN and internet connections, the system may be identified by either one, depending on who is communicating
+/// Therefore, you should not transmit the SystemAddress over the network and expect it to identify a system, or use it to connect to that system, except in the case where that system is not behind a NAT (such as with a dedciated server) +/// Use RakNetGUID for a unique per-instance of RakPeer to identify systems +struct RAK_DLL_EXPORT SystemAddress +{ + /// Constructors + SystemAddress(); + SystemAddress(const char *str); + SystemAddress(const char *str, unsigned short port); + + + + + + + + + + + /// SystemAddress, with RAKNET_SUPPORT_IPV6 defined, holds both an sockaddr_in6 and a sockaddr_in + union// In6OrIn4 + { +#if RAKNET_SUPPORT_IPV6==1 + struct sockaddr_storage sa_stor; + sockaddr_in6 addr6; +#endif + + sockaddr_in addr4; + } address; + + /// This is not used internally, but holds a copy of the port held in the address union, so for debugging it's easier to check what port is being held + unsigned short debugPort; + + /// \internal Return the size to write to a bitStream + static int size(void); + + /// Hash the system address + static unsigned long ToInteger( const SystemAddress &sa ); + + /// Return the IP version, either IPV4 or IPV6 + /// \return Either 4 or 6 + unsigned char GetIPVersion(void) const; + + /// \internal Returns either IPPROTO_IP or IPPROTO_IPV6 + /// \sa GetIPVersion + unsigned int GetIPPROTO(void) const; + + /// Call SetToLoopback(), with whatever IP version is currently held. Defaults to IPV4 + void SetToLoopback(void); + + /// Call SetToLoopback() with a specific IP version + /// \param[in] ipVersion Either 4 for IPV4 or 6 for IPV6 + void SetToLoopback(unsigned char ipVersion); + + /// \return If was set to 127.0.0.1 or ::1 + bool IsLoopback(void) const; + + // Return the systemAddress as a string in the format | + // Returns a static string + // NOT THREADSAFE + // portDelineator should not be '.', ':', '%', '-', '/', a number, or a-f + const char *ToString(bool writePort=true, char portDelineator='|') const; + + // Return the systemAddress as a string in the format | + // dest must be large enough to hold the output + // portDelineator should not be '.', ':', '%', '-', '/', a number, or a-f + // THREADSAFE + void ToString(bool writePort, char *dest, char portDelineator='|') const; + + /// Set the system address from a printable IP string, for example "192.0.2.1" or "2001:db8:63b3:1::3490" + /// You can write the port as well, using the portDelineator, for example "192.0.2.1|1234" + /// \param[in] str A printable IP string, for example "192.0.2.1" or "2001:db8:63b3:1::3490". Pass 0 for \a str to set to UNASSIGNED_SYSTEM_ADDRESS + /// \param[in] portDelineator if \a str contains a port, delineate the port with this character. portDelineator should not be '.', ':', '%', '-', '/', a number, or a-f + /// \param[in] ipVersion Only used if str is a pre-defined address in the wrong format, such as 127.0.0.1 but you want ip version 6, so you can pass 6 here to do the conversion + /// \note The current port is unchanged if a port is not specified in \a str + /// \return True on success, false on ipVersion does not match type of passed string + bool FromString(const char *str, char portDelineator='|', int ipVersion=0); + + /// Same as FromString(), but you explicitly set a port at the same time + bool FromStringExplicitPort(const char *str, unsigned short port, int ipVersion=0); + + /// Copy the port from another SystemAddress structure + void CopyPort( const SystemAddress& right ); + + /// Returns if two system addresses have the same IP (port is not checked) + bool EqualsExcludingPort( const SystemAddress& right ) const; + + /// Returns the port in host order (this is what you normally use) + unsigned short GetPort(void) const; + + /// \internal Returns the port in network order + unsigned short GetPortNetworkOrder(void) const; + + /// Sets the port. The port value should be in host order (this is what you normally use) + /// Renamed from SetPort because of winspool.h http://edn.embarcadero.com/article/21494 + void SetPortHostOrder(unsigned short s); + + /// \internal Sets the port. The port value should already be in network order. + void SetPortNetworkOrder(unsigned short s); + + /// Old version, for crap platforms that don't support newer socket functions + bool SetBinaryAddress(const char *str, char portDelineator=':'); + /// Old version, for crap platforms that don't support newer socket functions + void ToString_Old(bool writePort, char *dest, char portDelineator=':') const; + + /// \internal sockaddr_in6 requires extra data beyond just the IP and port. Copy that extra data from an existing SystemAddress that already has it + void FixForIPVersion(const SystemAddress &boundAddressToSocket); + + bool IsLANAddress(void); + + SystemAddress& operator = ( const SystemAddress& input ); + bool operator==( const SystemAddress& right ) const; + bool operator!=( const SystemAddress& right ) const; + bool operator > ( const SystemAddress& right ) const; + bool operator < ( const SystemAddress& right ) const; + + /// \internal Used internally for fast lookup. Optional (use -1 to do regular lookup). Don't transmit this. + SystemIndex systemIndex; + + private: + +#if RAKNET_SUPPORT_IPV6==1 + void ToString_New(bool writePort, char *dest, char portDelineator) const; +#endif +}; + +/// Uniquely identifies an instance of RakPeer. Use RakPeer::GetGuidFromSystemAddress() and RakPeer::GetSystemAddressFromGuid() to go between SystemAddress and RakNetGUID +/// Use RakPeer::GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS) to get your own GUID +struct RAK_DLL_EXPORT RakNetGUID +{ + RakNetGUID(); + explicit RakNetGUID(uint64_t _g) {g=_g; systemIndex=(SystemIndex)-1;} +// uint32_t g[6]; + uint64_t g; + + // Return the GUID as a string + // Returns a static string + // NOT THREADSAFE + const char *ToString(void) const; + + // Return the GUID as a string + // dest must be large enough to hold the output + // THREADSAFE + void ToString(char *dest) const; + + bool FromString(const char *source); + + static unsigned long ToUint32( const RakNetGUID &g ); + + RakNetGUID& operator = ( const RakNetGUID& input ) + { + g=input.g; + systemIndex=input.systemIndex; + return *this; + } + + // Used internally for fast lookup. Optional (use -1 to do regular lookup). Don't transmit this. + SystemIndex systemIndex; + static int size() {return (int) sizeof(uint64_t);} + + bool operator==( const RakNetGUID& right ) const; + bool operator!=( const RakNetGUID& right ) const; + bool operator > ( const RakNetGUID& right ) const; + bool operator < ( const RakNetGUID& right ) const; +}; + +/// Index of an invalid SystemAddress +//const SystemAddress UNASSIGNED_SYSTEM_ADDRESS = +//{ +// 0xFFFFFFFF, 0xFFFF +//}; +#ifndef SWIG +const SystemAddress UNASSIGNED_SYSTEM_ADDRESS; +const RakNetGUID UNASSIGNED_RAKNET_GUID((uint64_t)-1); +#endif +//{ +// {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF} +// 0xFFFFFFFFFFFFFFFF +//}; + + +struct RAK_DLL_EXPORT AddressOrGUID +{ + RakNetGUID rakNetGuid; + SystemAddress systemAddress; + + SystemIndex GetSystemIndex(void) const {if (rakNetGuid!=UNASSIGNED_RAKNET_GUID) return rakNetGuid.systemIndex; else return systemAddress.systemIndex;} + bool IsUndefined(void) const {return rakNetGuid==UNASSIGNED_RAKNET_GUID && systemAddress==UNASSIGNED_SYSTEM_ADDRESS;} + void SetUndefined(void) {rakNetGuid=UNASSIGNED_RAKNET_GUID; systemAddress=UNASSIGNED_SYSTEM_ADDRESS;} + static unsigned long ToInteger( const AddressOrGUID &aog ); + const char *ToString(bool writePort=true) const; + void ToString(bool writePort, char *dest) const; + + AddressOrGUID() {} + AddressOrGUID( const AddressOrGUID& input ) + { + rakNetGuid=input.rakNetGuid; + systemAddress=input.systemAddress; + } + AddressOrGUID( const SystemAddress& input ) + { + rakNetGuid=UNASSIGNED_RAKNET_GUID; + systemAddress=input; + } + AddressOrGUID( Packet *packet ); + AddressOrGUID( const RakNetGUID& input ) + { + rakNetGuid=input; + systemAddress=UNASSIGNED_SYSTEM_ADDRESS; + } + AddressOrGUID& operator = ( const AddressOrGUID& input ) + { + rakNetGuid=input.rakNetGuid; + systemAddress=input.systemAddress; + return *this; + } + + AddressOrGUID& operator = ( const SystemAddress& input ) + { + rakNetGuid=UNASSIGNED_RAKNET_GUID; + systemAddress=input; + return *this; + } + + AddressOrGUID& operator = ( const RakNetGUID& input ) + { + rakNetGuid=input; + systemAddress=UNASSIGNED_SYSTEM_ADDRESS; + return *this; + } + + inline bool operator==( const AddressOrGUID& right ) const {return (rakNetGuid!=UNASSIGNED_RAKNET_GUID && rakNetGuid==right.rakNetGuid) || (systemAddress!=UNASSIGNED_SYSTEM_ADDRESS && systemAddress==right.systemAddress);} +}; + +typedef uint64_t NetworkID; + +/// This represents a user message from another system. +struct Packet +{ + /// The system that send this packet. + SystemAddress systemAddress; + + /// A unique identifier for the system that sent this packet, regardless of IP address (internal / external / remote system) + /// Only valid once a connection has been established (ID_CONNECTION_REQUEST_ACCEPTED, or ID_NEW_INCOMING_CONNECTION) + /// Until that time, will be UNASSIGNED_RAKNET_GUID + RakNetGUID guid; + + /// The length of the data in bytes + unsigned int length; + + /// The length of the data in bits + BitSize_t bitSize; + + /// The data from the sender + unsigned char* data; + + /// @internal + /// Indicates whether to delete the data, or to simply delete the packet. + bool deleteData; + + /// @internal + /// If true, this message is meant for the user, not for the plugins, so do not process it through plugins + bool wasGeneratedLocally; +}; + +/// Index of an unassigned player +const SystemIndex UNASSIGNED_PLAYER_INDEX = 65535; + +/// Unassigned object ID +const NetworkID UNASSIGNED_NETWORK_ID = (uint64_t) -1; + +const int PING_TIMES_ARRAY_SIZE = 5; + +struct RAK_DLL_EXPORT uint24_t +{ + uint32_t val; + + uint24_t() {} + inline operator uint32_t() { return val; } + inline operator uint32_t() const { return val; } + + inline uint24_t(const uint24_t& a) {val=a.val;} + inline uint24_t operator++() {++val; val&=0x00FFFFFF; return *this;} + inline uint24_t operator--() {--val; val&=0x00FFFFFF; return *this;} + inline uint24_t operator++(int) {uint24_t temp(val); ++val; val&=0x00FFFFFF; return temp;} + inline uint24_t operator--(int) {uint24_t temp(val); --val; val&=0x00FFFFFF; return temp;} + inline uint24_t operator&(const uint24_t& a) {return uint24_t(val&a.val);} + inline uint24_t& operator=(const uint24_t& a) { val=a.val; return *this; } + inline uint24_t& operator+=(const uint24_t& a) { val+=a.val; val&=0x00FFFFFF; return *this; } + inline uint24_t& operator-=(const uint24_t& a) { val-=a.val; val&=0x00FFFFFF; return *this; } + inline bool operator==( const uint24_t& right ) const {return val==right.val;} + inline bool operator!=( const uint24_t& right ) const {return val!=right.val;} + inline bool operator > ( const uint24_t& right ) const {return val>right.val;} + inline bool operator < ( const uint24_t& right ) const {return val ( const uint32_t& right ) const {return val>(right&0x00FFFFFF);} + inline bool operator < ( const uint32_t& right ) const {return val<(right&0x00FFFFFF);} + inline const uint24_t operator+( const uint32_t &other ) const { return uint24_t(val+other); } + inline const uint24_t operator-( const uint32_t &other ) const { return uint24_t(val-other); } + inline const uint24_t operator/( const uint32_t &other ) const { return uint24_t(val/other); } + inline const uint24_t operator*( const uint32_t &other ) const { return uint24_t(val*other); } +}; + +} // namespace RakNet + diff --git a/Source/RakNetVersion.h b/Source/RakNetVersion.h index 68259fbb4..0aa5b8361 100644 --- a/Source/RakNetVersion.h +++ b/Source/RakNetVersion.h @@ -1,19 +1,22 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#define RAKNET_VERSION "4.081" -#define RAKNET_VERSION_NUMBER 4.081 -#define RAKNET_VERSION_NUMBER_INT 4081 - -#define RAKNET_DATE "5/28/2014" - -// What compatible protocol version RakNet is using. When this value changes, it indicates this version of RakNet cannot connection to an older version. -// ID_INCOMPATIBLE_PROTOCOL_VERSION will be returned on connection attempt in this case -#define RAKNET_PROTOCOL_VERSION 6 +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + + +#define RAKNET_VERSION "4.081" +#define RAKNET_VERSION_NUMBER 4.081 +#define RAKNET_VERSION_NUMBER_INT 4081 + +#define RAKNET_DATE "5/28/2014" + +// What compatible protocol version RakNet is using. When this value changes, it indicates this version of RakNet cannot connection to an older version. +// ID_INCOMPATIBLE_PROTOCOL_VERSION will be returned on connection attempt in this case +#define RAKNET_PROTOCOL_VERSION 6 diff --git a/Source/RakPeer.cpp b/Source/RakPeer.cpp index 09718ded0..d6b1285a3 100644 --- a/Source/RakPeer.cpp +++ b/Source/RakPeer.cpp @@ -1,6504 +1,6504 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -// \file -// - - - -#define CAT_NEUTER_EXPORT /* Neuter dllimport for libcat */ - -#include "RakNetDefines.h" -#include "RakPeer.h" -#include "RakNetTypes.h" - -#ifdef _WIN32 - -#else -#include -#endif - -// #if defined(new) -// #pragma push_macro("new") -// #undef new -// #define RMO_NEW_UNDEF_ALLOCATING_QUEUE -// #endif - -#include -#include // toupper -#include -#include "GetTime.h" -#include "MessageIdentifiers.h" -#include "DS_HuffmanEncodingTree.h" -#include "Rand.h" -#include "PluginInterface2.h" -#include "StringCompressor.h" -#include "StringTable.h" -#include "NetworkIDObject.h" -#include "RakNetTypes.h" -#include "DR_SHA1.h" -#include "RakSleep.h" -#include "RakAssert.h" -#include "RakNetVersion.h" -#include "NetworkIDManager.h" -#include "gettimeofday.h" -#include "SignaledEvent.h" -#include "SuperFastHash.h" -#include "RakAlloca.h" -#include "WSAStartupSingleton.h" - -#ifdef USE_THREADED_SEND -#include "SendToThread.h" -#endif - -#ifdef CAT_AUDIT -#define CAT_AUDIT_PRINTF(...) printf(__VA_ARGS__) -#else -#define CAT_AUDIT_PRINTF(...) -#endif - -namespace RakNet -{ -RAK_THREAD_DECLARATION(UpdateNetworkLoop); -RAK_THREAD_DECLARATION(RecvFromLoop); -RAK_THREAD_DECLARATION(UDTConnect); -} -#define REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE 8 - -#if !defined ( __APPLE__ ) && !defined ( __APPLE_CC__ ) -#include // malloc -#endif - - - -#if defined(_WIN32) -// -#else -/* -#include // Console 2 -#include -extern bool _extern_Console2LoadModules(void); -extern int _extern_Console2GetConnectionStatus(void); -extern int _extern_Console2GetLobbyStatus(void); -//extern bool Console2StartupFluff(unsigned int *); -extern void Console2ShutdownFluff(void); -//extern unsigned int Console2ActivateConnection(unsigned int, void *); -//extern bool Console2BlockOnEstablished(void); -extern void Console2GetIPAndPort(unsigned int, char *, unsigned short *, unsigned int ); -//extern void Console2DeactivateConnection(unsigned int, unsigned int); -*/ -#endif - - -static const int NUM_MTU_SIZES=3; - - - -static const int mtuSizes[NUM_MTU_SIZES]={MAXIMUM_MTU_SIZE, 1200, 576}; - - -// Note to self - if I change this it might affect RECIPIENT_OFFLINE_MESSAGE_INTERVAL in Natpunchthrough.cpp -//static const int MAX_OPEN_CONNECTION_REQUESTS=8; -//static const int TIME_BETWEEN_OPEN_CONNECTION_REQUESTS=500; - -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -using namespace RakNet; - -static RakNetRandom rnr; - -/* -struct RakPeerAndIndex -{ - RakNetSocket2 *s; - RakPeer *rakPeer; -}; -*/ - -static const unsigned int MAX_OFFLINE_DATA_LENGTH=400; // I set this because I limit ID_CONNECTION_REQUEST to 512 bytes, and the password is appended to that packet. - -// Used to distinguish between offline messages with data, and messages from the reliability layer -// Should be different than any message that could result from messages from the reliability layer -#if !defined(__GNUC__) -#pragma warning(disable:4309) // 'initializing' : truncation of constant value -#endif -// Make sure highest bit is 0, so isValid in DatagramHeaderFormat is false -static const unsigned char OFFLINE_MESSAGE_DATA_ID[16]={0x00,0xFF,0xFF,0x00,0xFE,0xFE,0xFE,0xFE,0xFD,0xFD,0xFD,0xFD,0x12,0x34,0x56,0x78}; - -struct PacketFollowedByData -{ - Packet p; - unsigned char data[1]; -}; - -Packet *RakPeer::AllocPacket(unsigned dataSize, const char *file, unsigned int line) -{ - // Crashes when dataSize is 4 bytes - not sure why -// unsigned char *data = (unsigned char *) rakMalloc_Ex(sizeof(PacketFollowedByData)+dataSize, file, line); -// Packet *p = &((PacketFollowedByData *)data)->p; -// p->data=((PacketFollowedByData *)data)->data; -// p->length=dataSize; -// p->bitSize=BYTES_TO_BITS(dataSize); -// p->deleteData=false; -// p->guid=UNASSIGNED_RAKNET_GUID; -// return p; - - RakNet::Packet *p; - packetAllocationPoolMutex.Lock(); - p = packetAllocationPool.Allocate(file,line); - packetAllocationPoolMutex.Unlock(); - p = new ((void*)p) Packet; - p->data=(unsigned char*) rakMalloc_Ex(dataSize,file,line); - p->length=dataSize; - p->bitSize=BYTES_TO_BITS(dataSize); - p->deleteData=true; - p->guid=UNASSIGNED_RAKNET_GUID; - p->wasGeneratedLocally=false; - return p; -} - -Packet *RakPeer::AllocPacket(unsigned dataSize, unsigned char *data, const char *file, unsigned int line) -{ - // Packet *p = (Packet *)rakMalloc_Ex(sizeof(Packet), file, line); - RakNet::Packet *p; - packetAllocationPoolMutex.Lock(); - p = packetAllocationPool.Allocate(file,line); - packetAllocationPoolMutex.Unlock(); - p = new ((void*)p) Packet; - RakAssert(p); - p->data=data; - p->length=dataSize; - p->bitSize=BYTES_TO_BITS(dataSize); - p->deleteData=true; - p->guid=UNASSIGNED_RAKNET_GUID; - p->wasGeneratedLocally=false; - return p; -} - -STATIC_FACTORY_DEFINITIONS(RakPeerInterface,RakPeer) - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Constructor -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RakPeer::RakPeer() -{ -#if LIBCAT_SECURITY==1 - // Encryption and security - CAT_AUDIT_PRINTF("AUDIT: Initializing RakPeer security flags: using_security = false, server_handshake = null, cookie_jar = null\n"); - _using_security = false; - _server_handshake = 0; - _cookie_jar = 0; -#endif - - StringCompressor::AddReference(); - RakNet::StringTable::AddReference(); - WSAStartupSingleton::AddRef(); - - defaultMTUSize = mtuSizes[NUM_MTU_SIZES-1]; - trackFrequencyTable = false; - maximumIncomingConnections = 0; - maximumNumberOfPeers = 0; - //remoteSystemListSize=0; - remoteSystemList = 0; - activeSystemList = 0; - activeSystemListSize=0; - remoteSystemLookup=0; - bytesSentPerSecond = bytesReceivedPerSecond = 0; - endThreads = true; - isMainLoopThreadActive = false; - incomingDatagramEventHandler=0; - - - - - - // isRecvfromThreadActive=false; -#if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0 - occasionalPing = true; -#else - occasionalPing = false; -#endif - allowInternalRouting=false; - for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++) - ipList[i]=UNASSIGNED_SYSTEM_ADDRESS; - allowConnectionResponseIPMigration = false; - //incomingPasswordLength=outgoingPasswordLength=0; - incomingPasswordLength=0; - splitMessageProgressInterval=0; - //unreliableTimeout=0; - unreliableTimeout=1000; - maxOutgoingBPS=0; - firstExternalID=UNASSIGNED_SYSTEM_ADDRESS; - myGuid=UNASSIGNED_RAKNET_GUID; - userUpdateThreadPtr=0; - userUpdateThreadData=0; - -#ifdef _DEBUG - // Wait longer to disconnect in debug so I don't get disconnected while tracing - defaultTimeoutTime=30000; -#else - defaultTimeoutTime=10000; -#endif - -#ifdef _DEBUG - _packetloss=0.0; - _minExtraPing=0; - _extraPingVariance=0; -#endif - - bufferedCommands.SetPageSize(sizeof(BufferedCommandStruct)*16); - socketQueryOutput.SetPageSize(sizeof(SocketQueryOutput)*8); - - packetAllocationPoolMutex.Lock(); - packetAllocationPool.SetPageSize(sizeof(DataStructures::MemoryPool::MemoryWithPage)*32); - packetAllocationPoolMutex.Unlock(); - - remoteSystemIndexPool.SetPageSize(sizeof(DataStructures::MemoryPool::MemoryWithPage)*32); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - GenerateGUID(); - - quitAndDataEvents.InitEvent(); - limitConnectionFrequencyFromTheSameIP=false; - ResetSendReceipt(); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Destructor -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RakPeer::~RakPeer() -{ - Shutdown( 0, 0 ); - - // Free the ban list. - ClearBanList(); - - StringCompressor::RemoveReference(); - RakNet::StringTable::RemoveReference(); - WSAStartupSingleton::Deref(); - - quitAndDataEvents.CloseEvent(); - -#if LIBCAT_SECURITY==1 - // Encryption and security - CAT_AUDIT_PRINTF("AUDIT: Deleting RakPeer security objects, handshake = %x, cookie jar = %x\n", _server_handshake, _cookie_jar); - if (_server_handshake) RakNet::OP_DELETE(_server_handshake,_FILE_AND_LINE_); - if (_cookie_jar) RakNet::OP_DELETE(_cookie_jar,_FILE_AND_LINE_); -#endif - - - - - - - - - - - - - - - - -// for (unsigned int i=0; i < pluginListTS.Size(); i++) -// pluginListTS[i]->SetRakPeerInterface(0); -// for (unsigned int i=0; i < pluginListNTS.Size(); i++) -// pluginListNTS[i]->SetRakPeerInterface(0); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// \brief Starts the network threads, opens the listen port. -// You must call this before calling Connect(). -// Multiple calls while already active are ignored. To call this function again with different settings, you must first call Shutdown(). -// \note Call SetMaximumIncomingConnections if you want to accept incoming connections -// \param[in] maxConnections The maximum number of connections between this instance of RakPeer and another instance of RakPeer. Required so the network can preallocate and for thread safety. A pure client would set this to 1. A pure server would set it to the number of allowed clients.- A hybrid would set it to the sum of both types of connections -// \param[in] localPort The port to listen for connections on. -// \param[in] _threadSleepTimer How many ms to Sleep each internal update cycle. With new congestion control, the best results will be obtained by passing 10. -// \param[in] socketDescriptors An array of SocketDescriptor structures to force RakNet to listen on a particular IP address or port (or both). Each SocketDescriptor will represent one unique socket. Do not pass redundant structures. To listen on a specific port, you can pass &socketDescriptor, 1SocketDescriptor(myPort,0); such as for a server. For a client, it is usually OK to just pass SocketDescriptor(); -// \param[in] socketDescriptorCount The size of the \a socketDescriptors array. Pass 1 if you are not sure what to pass. -// \return False on failure (can't create socket or thread), true on success. -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -StartupResult RakPeer::Startup( unsigned int maxConnections, SocketDescriptor *socketDescriptors, unsigned socketDescriptorCount, int threadPriority ) -{ - if (IsActive()) - return RAKNET_ALREADY_STARTED; - - // If getting the guid failed in the constructor, try again - if (myGuid.g==0) - { - GenerateGUID(); - if (myGuid.g==0) - return COULD_NOT_GENERATE_GUID; - } - - if (threadPriority==-99999) - { - - -#if defined(_WIN32) - threadPriority=0; - - - - -#else - threadPriority=1000; -#endif - } - - - FillIPList(); - - if (myGuid==UNASSIGNED_RAKNET_GUID) - { - rnr.SeedMT( GenerateSeedFromGuid() ); - } - - //RakPeerAndIndex rpai[32]; - //RakAssert(socketDescriptorCount<32); - - RakAssert(socketDescriptors && socketDescriptorCount>=1); - - if (socketDescriptors==0 || socketDescriptorCount<1) - return INVALID_SOCKET_DESCRIPTORS; - - //unsigned short localPort; - //localPort=socketDescriptors[0].port; - - RakAssert( maxConnections > 0 ); - - if ( maxConnections <= 0 ) - return INVALID_MAX_CONNECTIONS; - - DerefAllSockets(); - - - int i; - // Go through all socket descriptors and precreate sockets on the specified addresses - for (i=0; iSetUserConnectionSocketIndex(i); - #if defined(__native_client__) - NativeClientBindParameters ncbp; - RNS2_NativeClient * nativeClientSocket = (RNS2_NativeClient*) r2; - ncbp.eventHandler=this; - ncbp.forceHostAddress=(char*) socketDescriptors[i].hostAddress; - ncbp.is_ipv6=socketDescriptors[i].socketFamily==AF_INET6; - ncbp.nativeClientInstance=socketDescriptors[i].chromeInstance; - ncbp.port=socketDescriptors[i].port; - nativeClientSocket->Bind(&ncbp, _FILE_AND_LINE_); - #elif defined(WINDOWS_STORE_RT) - RNS2BindResult br; - ((RNS2_WindowsStore8*) r2)->SetRecvEventHandler(this); - br = ((RNS2_WindowsStore8*) r2)->Bind(ref new Platform::String()); - if (br!=BR_SUCCESS) - { - RakNetSocket2Allocator::DeallocRNS2(r2); - DerefAllSockets(); - return SOCKET_FAILED_TO_BIND; - } - #else - if (r2->IsBerkleySocket()) - { - RNS2_BerkleyBindParameters bbp; - bbp.port=socketDescriptors[i].port; - bbp.hostAddress=(char*) socketDescriptors[i].hostAddress; - bbp.addressFamily=socketDescriptors[i].socketFamily; - bbp.type=SOCK_DGRAM; - bbp.protocol=socketDescriptors[i].extraSocketOptions; - bbp.nonBlockingSocket=false; - bbp.setBroadcast=true; - bbp.setIPHdrIncl=false; - bbp.doNotFragment=false; - bbp.pollingThreadPriority=threadPriority; - bbp.eventHandler=this; - bbp.remotePortRakNetWasStartedOn_PS3_PS4_PSP2=socketDescriptors[i].remotePortRakNetWasStartedOn_PS3_PSP2; - RNS2BindResult br = ((RNS2_Berkley*) r2)->Bind(&bbp, _FILE_AND_LINE_); - - if ( - #if RAKNET_SUPPORT_IPV6==0 - socketDescriptors[i].socketFamily!=AF_INET || - #endif - br==BR_REQUIRES_RAKNET_SUPPORT_IPV6_DEFINE) - { - RakNetSocket2Allocator::DeallocRNS2(r2); - DerefAllSockets(); - return SOCKET_FAMILY_NOT_SUPPORTED; - } - else if (br==BR_FAILED_TO_BIND_SOCKET) - { - RakNetSocket2Allocator::DeallocRNS2(r2); - DerefAllSockets(); - return SOCKET_PORT_ALREADY_IN_USE; - } - else if (br==BR_FAILED_SEND_TEST) - { - RakNetSocket2Allocator::DeallocRNS2(r2); - DerefAllSockets(); - return SOCKET_FAILED_TEST_SEND; - } - else - { - RakAssert(br==BR_SUCCESS); - } - } - else - { - RakAssert("TODO" && 0); - } - #endif -/* - - SystemAddress saOut; - SocketLayer::GetSystemAddress( rns, &saOut ); - rns->SetBoundAddress(saOut); - rns->SetRemotePortRakNetWasStartedOn(socketDescriptors[i].remotePortRakNetWasStartedOn_PS3_PSP2); - rns->SetChromeInstance(socketDescriptors[i].chromeInstance); - rns->SetExtraSocketOptions(socketDescriptors[i].extraSocketOptions); - rns->SetUserConnectionSocketIndex(i); - rns->SetBlockingSocket(socketDescriptors[i].blockingSocket); - -#if RAKNET_SUPPORT_IPV6==0 - if (addrToBind==0) - rns->SetBoundAddressToLoopback(4); -#endif - - // GetBoundAddress is asynch, which isn't supported by this architecture -#if !defined(__native_client__) - int zero=0; - if (SocketLayer::SendTo(rns, (const char*) &zero,4, rns->GetBoundAddress(), _FILE_AND_LINE_)!=0) - { - DerefAllSockets(); - return SOCKET_FAILED_TEST_SEND; - } -#endif - */ - - socketList.Push(r2, _FILE_AND_LINE_ ); - - } - -#if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) - for (i=0; iIsBerkleySocket()) - ((RNS2_Berkley*) socketList[i])->CreateRecvPollingThread(threadPriority); - } -#endif - -// #if !defined(_XBOX) && !defined(_XBOX_720_COMPILE_AS_WINDOWS) && !defined(X360) - for (i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++) - { - if (ipList[i]==UNASSIGNED_SYSTEM_ADDRESS) - break; -#if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) - if (socketList[0]->IsBerkleySocket()) - { - unsigned short port = ((RNS2_Berkley*)socketList[0])->GetBoundAddress().GetPort(); - ipList[i].SetPortHostOrder(port); - - } -#endif -// ipList[i].SetPort(((RNS2_360_720*)socketList[0])->GetBoundAddress().GetPort()); - } -// #endif - - - if ( maximumNumberOfPeers == 0 ) - { - // Don't allow more incoming connections than we have peers. - if ( maximumIncomingConnections > maxConnections ) - maximumIncomingConnections = maxConnections; - - maximumNumberOfPeers = maxConnections; - // 04/19/2006 - Don't overallocate because I'm no longer allowing connected pings. - // The disconnects are not consistently processed and the process was sloppy and complicated. - // Allocate 10% extra to handle new connections from players trying to connect when the server is full - //remoteSystemListSize = maxConnections;// * 11 / 10 + 1; - - // remoteSystemList in Single thread - //remoteSystemList = RakNet::OP_NEW( _FILE_AND_LINE_ ); - remoteSystemList = RakNet::OP_NEW_ARRAY(maximumNumberOfPeers, _FILE_AND_LINE_ ); - - remoteSystemLookup = RakNet::OP_NEW_ARRAY((unsigned int) maximumNumberOfPeers * REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE, _FILE_AND_LINE_ ); - - activeSystemList = RakNet::OP_NEW_ARRAY(maximumNumberOfPeers, _FILE_AND_LINE_ ); - - for ( i = 0; i < maximumNumberOfPeers; i++ ) - //for ( i = 0; i < remoteSystemListSize; i++ ) - { - // remoteSystemList in Single thread - remoteSystemList[ i ].isActive = false; - remoteSystemList[ i ].systemAddress = UNASSIGNED_SYSTEM_ADDRESS; - remoteSystemList[ i ].guid = UNASSIGNED_RAKNET_GUID; - remoteSystemList[ i ].myExternalSystemAddress = UNASSIGNED_SYSTEM_ADDRESS; - remoteSystemList[ i ].connectMode=RemoteSystemStruct::NO_ACTION; - remoteSystemList[ i ].MTUSize = defaultMTUSize; - remoteSystemList[ i ].remoteSystemIndex = (SystemIndex) i; -#ifdef _DEBUG - remoteSystemList[ i ].reliabilityLayer.ApplyNetworkSimulator(_packetloss, _minExtraPing, _extraPingVariance); -#endif - - // All entries in activeSystemList have valid pointers all the time. - activeSystemList[ i ] = &remoteSystemList[ i ]; - } - - for (unsigned int i=0; i < (unsigned int) maximumNumberOfPeers*REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE; i++) - { - remoteSystemLookup[i]=0; - } - } - - // For histogram statistics - // nextReadBytesTime=0; - // lastSentBytes=lastReceivedBytes=0; - - if ( endThreads ) - { - updateCycleIsRunning = false; - endThreads = false; - firstExternalID=UNASSIGNED_SYSTEM_ADDRESS; - - ClearBufferedCommands(); - ClearBufferedPackets(); - ClearSocketQueryOutput(); - - if ( isMainLoopThreadActive == false ) - { -#if RAKPEER_USER_THREADED!=1 - - int errorCode; - - - - - - - - errorCode = RakNet::RakThread::Create(UpdateNetworkLoop, this, threadPriority); - - - if ( errorCode != 0 ) - { - Shutdown( 0, 0 ); - return FAILED_TO_CREATE_NETWORK_THREAD; - } -// RakAssert(isRecvFromLoopThreadActive.GetValue()==0); -#endif // RAKPEER_USER_THREADED!=1 - - /* - for (i=0; i(_FILE_AND_LINE_); - rpai->s=socketList[i]; - rpai->rakPeer=this; - -#if RAKPEER_USER_THREADED!=1 - - #if defined(SN_TARGET_PSP2) - sprintf(threadName, "RecvFromLoop_%p", this); - //errorCode = RakNet::RakThread::Create(RecvFromLoop, rpai, threadPriority, threadName, 1+i, runtime); - errorCode = RakNet::RakThread::Create(RecvFromLoop, rpai, threadPriority, threadName, 1024*1); - #else - errorCode = RakNet::RakThread::Create(RecvFromLoop, rpai, threadPriority); - #endif - - if ( errorCode != 0 ) - { - Shutdown( 0, 0 ); - return FAILED_TO_CREATE_NETWORK_THREAD; - } -#endif // RAKPEER_USER_THREADED!=1 - } - */ - - - /* -#if RAKPEER_USER_THREADED!=1 - - while ( isRecvFromLoopThreadActive.GetValue() < (uint32_t) socketDescriptorCount ) - RakSleep(10); - #endif // RAKPEER_USER_THREADED!=1 - */ - - } - -#if RAKPEER_USER_THREADED!=1 - // Wait for the threads to activate. When they are active they will set these variables to true - while ( isMainLoopThreadActive == false ) - RakSleep(10); -#endif // RAKPEER_USER_THREADED!=1 - } - - for (i=0; i < pluginListTS.Size(); i++) - { - pluginListTS[i]->OnRakPeerStartup(); - } - - for (i=0; i < pluginListNTS.Size(); i++) - { - pluginListNTS[i]->OnRakPeerStartup(); - } - -#ifdef USE_THREADED_SEND - RakNet::SendToThread::AddRef(); -#endif - - return RAKNET_STARTED; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Must be called while offline -// -// If you accept connections, you must call this or else security will not be enabled for incoming connections. -// -// This feature requires more round trips, bandwidth, and CPU time for the connection handshake -// x64 builds require under 25% of the CPU time of other builds -// -// See the Encryption sample for example usage -// -// Parameters: -// publicKey = A pointer to the public key for accepting new connections -// privateKey = A pointer to the private key for accepting new connections -// If the private keys are 0, then a new key will be generated when this function is called -// bRequireClientKey: Should be set to false for most servers. Allows the server to accept a public key from connecting clients as a proof of identity but eats twice as much CPU time as a normal connection -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -bool RakPeer::InitializeSecurity(const char *public_key, const char *private_key, bool bRequireClientKey) -{ -#if LIBCAT_SECURITY==1 - if ( endThreads == false ) - return false; - - // Copy client public key requirement flag - _require_client_public_key = bRequireClientKey; - - if (_server_handshake) - { - CAT_AUDIT_PRINTF("AUDIT: Deleting old server_handshake %x\n", _server_handshake); - RakNet::OP_DELETE(_server_handshake,_FILE_AND_LINE_); - } - if (_cookie_jar) - { - CAT_AUDIT_PRINTF("AUDIT: Deleting old cookie jar %x\n", _cookie_jar); - RakNet::OP_DELETE(_cookie_jar,_FILE_AND_LINE_); - } - - _server_handshake = RakNet::OP_NEW(_FILE_AND_LINE_); - _cookie_jar = RakNet::OP_NEW(_FILE_AND_LINE_); - - CAT_AUDIT_PRINTF("AUDIT: Created new server_handshake %x\n", _server_handshake); - CAT_AUDIT_PRINTF("AUDIT: Created new cookie jar %x\n", _cookie_jar); - CAT_AUDIT_PRINTF("AUDIT: Running _server_handshake->Initialize()\n"); - - if (_server_handshake->Initialize(public_key, private_key)) - { - CAT_AUDIT_PRINTF("AUDIT: Successfully initialized, filling cookie jar with goodies, storing public key and setting using security flag to true\n"); - - _server_handshake->FillCookieJar(_cookie_jar); - - memcpy(my_public_key, public_key, sizeof(my_public_key)); - - _using_security = true; - return true; - } - - CAT_AUDIT_PRINTF("AUDIT: Failure to initialize so deleting server handshake and cookie jar; also setting using_security flag = false\n"); - - RakNet::OP_DELETE(_server_handshake,_FILE_AND_LINE_); - _server_handshake=0; - RakNet::OP_DELETE(_cookie_jar,_FILE_AND_LINE_); - _cookie_jar=0; - _using_security = false; - return false; -#else - (void) public_key; - (void) private_key; - (void) bRequireClientKey; - - return false; -#endif -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description -// Must be called while offline -// Disables security for incoming connections. -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::DisableSecurity( void ) -{ -#if LIBCAT_SECURITY==1 - CAT_AUDIT_PRINTF("AUDIT: DisableSecurity() called, so deleting _server_handshake %x and cookie_jar %x\n", _server_handshake, _cookie_jar); - RakNet::OP_DELETE(_server_handshake,_FILE_AND_LINE_); - _server_handshake=0; - RakNet::OP_DELETE(_cookie_jar,_FILE_AND_LINE_); - _cookie_jar=0; - - _using_security = false; -#endif -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::AddToSecurityExceptionList(const char *ip) -{ - securityExceptionMutex.Lock(); - securityExceptionList.Insert(RakString(ip), _FILE_AND_LINE_); - securityExceptionMutex.Unlock(); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::RemoveFromSecurityExceptionList(const char *ip) -{ - if (securityExceptionList.Size()==0) - return; - - if (ip==0) - { - securityExceptionMutex.Lock(); - securityExceptionList.Clear(false, _FILE_AND_LINE_); - securityExceptionMutex.Unlock(); - } - else - { - unsigned i=0; - securityExceptionMutex.Lock(); - while (i < securityExceptionList.Size()) - { - if (securityExceptionList[i].IPAddressMatch(ip)) - { - securityExceptionList[i]=securityExceptionList[securityExceptionList.Size()-1]; - securityExceptionList.RemoveAtIndex(securityExceptionList.Size()-1); - } - else - i++; - } - securityExceptionMutex.Unlock(); - } -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -bool RakPeer::IsInSecurityExceptionList(const char *ip) -{ - if (securityExceptionList.Size()==0) - return false; - - unsigned i=0; - securityExceptionMutex.Lock(); - for (; i < securityExceptionList.Size(); i++) - { - if (securityExceptionList[i].IPAddressMatch(ip)) - { - securityExceptionMutex.Unlock(); - return true; - } - } - securityExceptionMutex.Unlock(); - return false; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Sets how many incoming connections are allowed. If this is less than the number of players currently connected, no -// more players will be allowed to connect. If this is greater than the maximum number of peers allowed, it will be reduced -// to the maximum number of peers allowed. Defaults to 0. -// -// Parameters: -// numberAllowed - Maximum number of incoming connections allowed. -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::SetMaximumIncomingConnections( unsigned short numberAllowed ) -{ - maximumIncomingConnections = numberAllowed; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Returns the maximum number of incoming connections, which is always <= maxConnections -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -unsigned int RakPeer::GetMaximumIncomingConnections( void ) const -{ - return maximumIncomingConnections; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Returns how many open connections there are at this time -// \return the number of open connections -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -unsigned short RakPeer::NumberOfConnections(void) const -{ - DataStructures::List addresses; - DataStructures::List guids; - GetSystemList(addresses, guids); - return (unsigned short) addresses.Size(); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Sets the password incoming connections must match in the call to Connect (defaults to none) -// Pass 0 to passwordData to specify no password -// -// Parameters: -// passwordData: A data block that incoming connections must match. This can be just a password, or can be a stream of data. -// - Specify 0 for no password data -// passwordDataLength: The length in bytes of passwordData -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::SetIncomingPassword( const char* passwordData, int passwordDataLength ) -{ - //if (passwordDataLength > MAX_OFFLINE_DATA_LENGTH) - // passwordDataLength=MAX_OFFLINE_DATA_LENGTH; - - if (passwordDataLength > 255) - passwordDataLength=255; - - if (passwordData==0) - passwordDataLength=0; - - // Not threadsafe but it's not important enough to lock. Who is going to change the password a lot during runtime? - // It won't overflow at least because incomingPasswordLength is an unsigned char - if (passwordDataLength>0) - memcpy(incomingPassword, passwordData, passwordDataLength); - incomingPasswordLength=(unsigned char)passwordDataLength; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::GetIncomingPassword( char* passwordData, int *passwordDataLength ) -{ - if (passwordData==0) - { - *passwordDataLength=incomingPasswordLength; - return; - } - - if (*passwordDataLength > incomingPasswordLength) - *passwordDataLength=incomingPasswordLength; - - if (*passwordDataLength>0) - memcpy(passwordData, incomingPassword, *passwordDataLength); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Call this to connect to the specified host (ip or domain name) and server port. -// Calling Connect and not calling SetMaximumIncomingConnections acts as a dedicated client. Calling both acts as a true peer. -// This is a non-blocking connection. You know the connection is successful when IsConnected() returns true -// or receive gets a packet with the type identifier ID_CONNECTION_REQUEST_ACCEPTED. If the connection is not -// successful, such as rejected connection or no response then neither of these things will happen. -// Requires that you first call Initialize -// -// Parameters: -// host: Either a dotted IP address or a domain name -// remotePort: Which port to connect to on the remote machine. -// passwordData: A data block that must match the data block on the server. This can be just a password, or can be a stream of data -// passwordDataLength: The length in bytes of passwordData -// -// Returns: -// True on successful initiation. False on incorrect parameters, internal error, or too many existing peers -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -ConnectionAttemptResult RakPeer::Connect( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNet::TimeMS timeoutTime ) -{ - // If endThreads is true here you didn't call Startup() first. - if ( host == 0 || endThreads || connectionSocketIndex>=socketList.Size() ) - return INVALID_PARAMETER; - - RakAssert(remotePort!=0); - - connectionSocketIndex=GetRakNetSocketFromUserConnectionSocketIndex(connectionSocketIndex); - - if (passwordDataLength>255) - passwordDataLength=255; - - if (passwordData==0) - passwordDataLength=0; - - // Not threadsafe but it's not important enough to lock. Who is going to change the password a lot during runtime? - // It won't overflow at least because outgoingPasswordLength is an unsigned char -// if (passwordDataLength>0) -// memcpy(outgoingPassword, passwordData, passwordDataLength); -// outgoingPasswordLength=(unsigned char) passwordDataLength; - - // 04/02/09 - Can't remember why I disabled connecting to self, but it seems to work - // Connecting to ourselves in the same instance of the program? -// if ( ( strcmp( host, "127.0.0.1" ) == 0 || strcmp( host, "0.0.0.0" ) == 0 ) && remotePort == mySystemAddress[0].port ) -// return false; - - return SendConnectionRequest( host, remotePort, passwordData, passwordDataLength, publicKey, connectionSocketIndex, 0, sendConnectionAttemptCount, timeBetweenSendConnectionAttemptsMS, timeoutTime); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - -ConnectionAttemptResult RakPeer::ConnectWithSocket(const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, RakNetSocket2* socket, PublicKey *publicKey, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNet::TimeMS timeoutTime) -{ - if ( host == 0 || endThreads || socket == 0 ) - return INVALID_PARAMETER; - - if (passwordDataLength>255) - passwordDataLength=255; - - if (passwordData==0) - passwordDataLength=0; - - return SendConnectionRequest( host, remotePort, passwordData, passwordDataLength, publicKey, 0, 0, sendConnectionAttemptCount, timeBetweenSendConnectionAttemptsMS, timeoutTime, socket ); - -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Stops the network threads and close all connections. Multiple calls are ok. -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::Shutdown( unsigned int blockDuration, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority ) -{ - unsigned i,j; - bool anyActive; - RakNet::TimeMS startWaitingTime; -// SystemAddress systemAddress; - RakNet::TimeMS time; - //unsigned short systemListSize = remoteSystemListSize; // This is done for threading reasons - unsigned int systemListSize = maximumNumberOfPeers; - - if ( blockDuration > 0 ) - { - for ( i = 0; i < systemListSize; i++ ) - { - // remoteSystemList in user thread - if (remoteSystemList[i].isActive) - NotifyAndFlagForShutdown(remoteSystemList[i].systemAddress, false, orderingChannel, disconnectionNotificationPriority); - } - - time = RakNet::GetTimeMS(); - startWaitingTime = time; - while ( time - startWaitingTime < blockDuration ) - { - anyActive=false; - for (j=0; j < systemListSize; j++) - { - // remoteSystemList in user thread - if (remoteSystemList[j].isActive) - { - anyActive=true; - break; - } - } - - // If this system is out of packets to send, then stop waiting - if ( anyActive==false ) - break; - - // This will probably cause the update thread to run which will probably - // send the disconnection notification - - RakSleep(15); - time = RakNet::GetTimeMS(); - } - } - for (i=0; i < pluginListTS.Size(); i++) - { - pluginListTS[i]->OnRakPeerShutdown(); - } - for (i=0; i < pluginListNTS.Size(); i++) - { - pluginListNTS[i]->OnRakPeerShutdown(); - } - - activeSystemListSize=0; - - quitAndDataEvents.SetEvent(); - - endThreads = true; - -// RakNet::TimeMS timeout; -#if RAKPEER_USER_THREADED!=1 - -#if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) - for (i=0; i < socketList.Size(); i++) - { - if (socketList[i]->IsBerkleySocket()) - { - ((RNS2_Berkley *)socketList[i])->SignalStopRecvPollingThread(); - } - } -#endif - - /* - // Get recvfrom to unblock - for (i=0; i < socketList.Size(); i++) - { - if (SocketLayer::SendTo(socketList[i], (const char*) &i,1,socketList[i]->GetBoundAddress(), _FILE_AND_LINE_)!=0) - break; - } - */ - - while ( isMainLoopThreadActive ) - { - endThreads = true; - RakSleep(15); - } - - /* - timeout = RakNet::GetTimeMS()+1000; - while ( isRecvFromLoopThreadActive.GetValue()>0 && RakNet::GetTimeMS()GetBoundAddress(), _FILE_AND_LINE_); - } - - RakSleep(30); - } - */ - -#if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) - for (i=0; i < socketList.Size(); i++) - { - if (socketList[i]->IsBerkleySocket()) - { - ((RNS2_Berkley *)socketList[i])->BlockOnStopRecvPollingThread(); - } - } -#endif - - -#endif // RAKPEER_USER_THREADED!=1 - -// char c=0; -// unsigned int socketIndex; - // remoteSystemList in Single thread - for ( i = 0; i < systemListSize; i++ ) - { - // Reserve this reliability layer for ourselves - remoteSystemList[ i ].isActive = false; - - // Remove any remaining packets - RakAssert(remoteSystemList[ i ].MTUSize <= MAXIMUM_MTU_SIZE); - remoteSystemList[ i ].reliabilityLayer.Reset(false, remoteSystemList[ i ].MTUSize, false); - remoteSystemList[ i ].rakNetSocket = 0; - } - - - // Setting maximumNumberOfPeers to 0 allows remoteSystemList to be reallocated in Initialize. - // Setting remoteSystemListSize prevents threads from accessing the reliability layer - maximumNumberOfPeers = 0; - //remoteSystemListSize = 0; - - // Free any packets the user didn't deallocate - packetReturnMutex.Lock(); - for (i=0; i < packetReturnQueue.Size(); i++) - DeallocatePacket(packetReturnQueue[i]); - packetReturnQueue.Clear(_FILE_AND_LINE_); - packetReturnMutex.Unlock(); - packetAllocationPoolMutex.Lock(); - packetAllocationPool.Clear(_FILE_AND_LINE_); - packetAllocationPoolMutex.Unlock(); - - /* - if (isRecvFromLoopThreadActive.GetValue()>0) - { - timeout = RakNet::GetTimeMS()+1000; - while ( isRecvFromLoopThreadActive.GetValue()>0 && RakNet::GetTimeMS() addresses; - DataStructures::List guids; - GetSystemList(addresses, guids); - if (remoteSystems) - { - unsigned short i; - for (i=0; i < *numberOfSystems && i < addresses.Size(); i++) - remoteSystems[i]=addresses[i]; - *numberOfSystems=i; - } - else - { - *numberOfSystems=(unsigned short) addresses.Size(); - } - return true; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -uint32_t RakPeer::GetNextSendReceipt(void) -{ - sendReceiptSerialMutex.Lock(); - uint32_t retVal = sendReceiptSerial; - sendReceiptSerialMutex.Unlock(); - return retVal; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -uint32_t RakPeer::IncrementNextSendReceipt(void) -{ - sendReceiptSerialMutex.Lock(); - uint32_t returned = sendReceiptSerial; - if (++sendReceiptSerial==0) - sendReceiptSerial=1; - sendReceiptSerialMutex.Unlock(); - return returned; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Sends a block of data to the specified system that you are connected to. -// This function only works while the client is connected (Use the Connect function). -// The first byte should be a message identifier starting at ID_USER_PACKET_ENUM -// -// Parameters: -// data: The block of data to send -// length: The size in bytes of the data to send -// bitStream: The bitstream to send -// priority: What priority level to send on. -// reliability: How reliability to send this data -// orderingChannel: When using ordered or sequenced packets, what channel to order these on. -// - Packets are only ordered relative to other packets on the same stream -// systemAddress: Who to send this packet to, or in the case of broadcasting who not to send it to. Use UNASSIGNED_SYSTEM_ADDRESS to specify none -// broadcast: True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. -// Returns: -// \return 0 on bad input. Otherwise a number that identifies this message. If \a reliability is a type that returns a receipt, on a later call to Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS with bytes 1-4 inclusive containing this number -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -uint32_t RakPeer::Send( const char *data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber ) -{ -#ifdef _DEBUG - RakAssert( data && length > 0 ); -#endif - RakAssert( !( reliability >= NUMBER_OF_RELIABILITIES || reliability < 0 ) ); - RakAssert( !( priority > NUMBER_OF_PRIORITIES || priority < 0 ) ); - RakAssert( !( orderingChannel >= NUMBER_OF_ORDERED_STREAMS ) ); - - if ( data == 0 || length < 0 ) - return 0; - - if ( remoteSystemList == 0 || endThreads == true ) - return 0; - - if ( broadcast == false && systemIdentifier.IsUndefined()) - return 0; - - uint32_t usedSendReceipt; - if (forceReceiptNumber!=0) - usedSendReceipt=forceReceiptNumber; - else - usedSendReceipt=IncrementNextSendReceipt(); - - if (broadcast==false && IsLoopbackAddress(systemIdentifier,true)) - { - SendLoopback(data,length); - - if (reliability>=UNRELIABLE_WITH_ACK_RECEIPT) - { - char buff[5]; - buff[0]=ID_SND_RECEIPT_ACKED; - sendReceiptSerialMutex.Lock(); - memcpy(buff+1, &sendReceiptSerial, 4); - sendReceiptSerialMutex.Unlock(); - SendLoopback( buff, 5 ); - } - - return usedSendReceipt; - } - - SendBuffered(data, length*8, priority, reliability, orderingChannel, systemIdentifier, broadcast, RemoteSystemStruct::NO_ACTION, usedSendReceipt); - - return usedSendReceipt; -} - -void RakPeer::SendLoopback( const char *data, const int length ) -{ - if ( data == 0 || length < 0 ) - return; - - Packet *packet = AllocPacket(length, _FILE_AND_LINE_); - memcpy(packet->data, data, length); - packet->systemAddress = GetLoopbackAddress(); - packet->guid=myGuid; - PushBackPacket(packet, false); -} - -uint32_t RakPeer::Send( const RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber ) -{ -#ifdef _DEBUG - RakAssert( bitStream->GetNumberOfBytesUsed() > 0 ); -#endif - - RakAssert( !( reliability >= NUMBER_OF_RELIABILITIES || reliability < 0 ) ); - RakAssert( !( priority > NUMBER_OF_PRIORITIES || priority < 0 ) ); - RakAssert( !( orderingChannel >= NUMBER_OF_ORDERED_STREAMS ) ); - - if ( bitStream->GetNumberOfBytesUsed() == 0 ) - return 0; - - if ( remoteSystemList == 0 || endThreads == true ) - return 0; - - if ( broadcast == false && systemIdentifier.IsUndefined() ) - return 0; - - uint32_t usedSendReceipt; - if (forceReceiptNumber!=0) - usedSendReceipt=forceReceiptNumber; - else - usedSendReceipt=IncrementNextSendReceipt(); - - if (broadcast==false && IsLoopbackAddress(systemIdentifier,true)) - { - SendLoopback((const char*) bitStream->GetData(),bitStream->GetNumberOfBytesUsed()); - if (reliability>=UNRELIABLE_WITH_ACK_RECEIPT) - { - char buff[5]; - buff[0]=ID_SND_RECEIPT_ACKED; - sendReceiptSerialMutex.Lock(); - memcpy(buff+1, &sendReceiptSerial,4); - sendReceiptSerialMutex.Unlock(); - SendLoopback( buff, 5 ); - } - return usedSendReceipt; - } - - // Sends need to be buffered and processed in the update thread because the systemAddress associated with the reliability layer can change, - // from that thread, resulting in a send to the wrong player! While I could mutex the systemAddress, that is much slower than doing this - SendBuffered((const char*)bitStream->GetData(), bitStream->GetNumberOfBitsUsed(), priority, reliability, orderingChannel, systemIdentifier, broadcast, RemoteSystemStruct::NO_ACTION, usedSendReceipt); - - - return usedSendReceipt; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Sends multiple blocks of data, concatenating them automatically. -// -// This is equivalent to: -// RakNet::BitStream bs; -// bs.WriteAlignedBytes(block1, blockLength1); -// bs.WriteAlignedBytes(block2, blockLength2); -// bs.WriteAlignedBytes(block3, blockLength3); -// Send(&bs, ...) -// -// This function only works while connected -// \param[in] data An array of pointers to blocks of data -// \param[in] lengths An array of integers indicating the length of each block of data -// \param[in] numParameters Length of the arrays data and lengths -// \param[in] priority What priority level to send on. See PacketPriority.h -// \param[in] reliability How reliability to send this data. See PacketPriority.h -// \param[in] orderingChannel When using ordered or sequenced messages, what channel to order these on. Messages are only ordered relative to other messages on the same stream -// \param[in] systemIdentifier Who to send this packet to, or in the case of broadcasting who not to send it to. Pass either a SystemAddress structure or a RakNetGUID structure. Use UNASSIGNED_SYSTEM_ADDRESS or to specify none -// \param[in] broadcast True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. -// \return False if we are not connected to the specified recipient. True otherwise -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -uint32_t RakPeer::SendList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber ) -{ -#ifdef _DEBUG - RakAssert( data ); -#endif - - if ( data == 0 || lengths == 0 ) - return 0; - - if ( remoteSystemList == 0 || endThreads == true ) - return 0; - - if (numParameters==0) - return 0; - - if (lengths==0) - return 0; - - if ( broadcast == false && systemIdentifier.IsUndefined() ) - return 0; - - uint32_t usedSendReceipt; - if (forceReceiptNumber!=0) - usedSendReceipt=forceReceiptNumber; - else - usedSendReceipt=IncrementNextSendReceipt(); - - SendBufferedList(data, lengths, numParameters, priority, reliability, orderingChannel, systemIdentifier, broadcast, RemoteSystemStruct::NO_ACTION, usedSendReceipt); - - return usedSendReceipt; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Gets a packet from the incoming packet queue. Use DeallocatePacket to deallocate the packet after you are done with it. -// Check the Packet struct at the top of CoreNetworkStructures.h for the format of the struct -// -// Returns: -// 0 if no packets are waiting to be handled, otherwise an allocated packet -// If the client is not active this will also return 0, as all waiting packets are flushed when the client is Disconnected -// This also updates all memory blocks associated with synchronized memory and distributed objects -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -#ifdef _MSC_VER -#pragma warning( disable : 4701 ) // warning C4701: local variable may be used without having been initialized -#endif -Packet* RakPeer::Receive( void ) -{ - if ( !( IsActive() ) ) - return 0; - - RakNet::Packet *packet; -// Packet **threadPacket; - PluginReceiveResult pluginResult; - - int offset; - unsigned int i; - - // User should call RunUpdateCycle and RunRecvFromOnce to do this commented code - /* -#if RAKPEER_SINGLE_THREADED==1 - RakPeer::RecvFromStruct *recvFromStruct; - for (i=0; i < socketList.Size(); i++) - { - while (1) - { - recvFromStruct=bufferedPackets.Allocate( _FILE_AND_LINE_ ); - recvFromStruct->s=socketList[i]->s; - recvFromStruct->remotePortRakNetWasStartedOn_PS3=socketList[i]->remotePortRakNetWasStartedOn_PS3_PSP2; - recvFromStruct->extraSocketOptions=socketList[i]->extraSocketOptions; - SocketLayer::RecvFromBlocking( - recvFromStruct->s, this, recvFromStruct->remotePortRakNetWasStartedOn_PS3, - recvFromStruct->extraSocketOptions, recvFromStruct->data, &recvFromStruct->bytesRead, &recvFromStruct->systemAddress, &recvFromStruct->timeRead); - if (recvFromStruct->bytesRead<=0) - { - bufferedPackets.Deallocate(recvFromStruct, _FILE_AND_LINE_); - break; - } - else - { - RakAssert(recvFromStruct->systemAddress.GetPort()); - bufferedPackets.Push(recvFromStruct); - } - } - } - - BitStream updateBitStream( MAXIMUM_MTU_SIZE -#if LIBCAT_SECURITY==1 - + cat::AuthenticatedEncryption::OVERHEAD_BYTES -#endif - ); - RunUpdateCycle(0, 0, updateBitStream); -#endif - */ - - for (i=0; i < pluginListTS.Size(); i++) - { - pluginListTS[i]->Update(); - } - for (i=0; i < pluginListNTS.Size(); i++) - { - pluginListNTS[i]->Update(); - } - - do - { - packetReturnMutex.Lock(); - if (packetReturnQueue.IsEmpty()) - packet=0; - else - packet = packetReturnQueue.Pop(); - packetReturnMutex.Unlock(); - if (packet==0) - return 0; - -// unsigned char msgId; - if ( ( packet->length >= sizeof(unsigned char) + sizeof( RakNet::Time ) ) && - ( (unsigned char) packet->data[ 0 ] == ID_TIMESTAMP ) ) - { - offset = sizeof(unsigned char); - ShiftIncomingTimestamp( packet->data + offset, packet->systemAddress ); -// msgId=packet->data[sizeof(unsigned char) + sizeof( RakNet::Time )]; - } -// else - // msgId=packet->data[0]; - - // Some locally generated packets need to be processed by plugins, for example ID_FCM2_NEW_HOST - // The plugin itself should intercept these messages generated remotely -// if (packet->wasGeneratedLocally) -// return packet; - - - CallPluginCallbacks(pluginListTS, packet); - CallPluginCallbacks(pluginListNTS, packet); - - for (i=0; i < pluginListTS.Size(); i++) - { - pluginResult=pluginListTS[i]->OnReceive(packet); - if (pluginResult==RR_STOP_PROCESSING_AND_DEALLOCATE) - { - DeallocatePacket( packet ); - packet=0; // Will do the loop again and get another packet - break; // break out of the enclosing for - } - else if (pluginResult==RR_STOP_PROCESSING) - { - packet=0; - break; - } - } - - for (i=0; i < pluginListNTS.Size(); i++) - { - pluginResult=pluginListNTS[i]->OnReceive(packet); - if (pluginResult==RR_STOP_PROCESSING_AND_DEALLOCATE) - { - DeallocatePacket( packet ); - packet=0; // Will do the loop again and get another packet - break; // break out of the enclosing for - } - else if (pluginResult==RR_STOP_PROCESSING) - { - packet=0; - break; - } - } - - } while(packet==0); - -#ifdef _DEBUG - RakAssert( packet->data ); -#endif - - return packet; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Call this to deallocate a packet returned by Receive -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::DeallocatePacket( Packet *packet ) -{ - if ( packet == 0 ) - return; - - if (packet->deleteData) - { - rakFree_Ex(packet->data, _FILE_AND_LINE_ ); - packet->~Packet(); - packetAllocationPoolMutex.Lock(); - packetAllocationPool.Release(packet,_FILE_AND_LINE_); - packetAllocationPoolMutex.Unlock(); - } - else - { - rakFree_Ex(packet, _FILE_AND_LINE_ ); - } -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Return the total number of connections we are allowed -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -unsigned int RakPeer::GetMaximumNumberOfPeers( void ) const -{ - return maximumNumberOfPeers; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Close the connection to another host (if we initiated the connection it will disconnect, if they did it will kick them out). -// -// Parameters: -// target: Which connection to close -// sendDisconnectionNotification: True to send ID_DISCONNECTION_NOTIFICATION to the recipient. False to close it silently. -// channel: If blockDuration > 0, the disconnect packet will be sent on this channel -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::CloseConnection( const AddressOrGUID target, bool sendDisconnectionNotification, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority ) -{ - /* - // This only be called from the user thread, for the user shutting down. - // From the network thread, this should occur because of ID_DISCONNECTION_NOTIFICATION and ID_CONNECTION_LOST - unsigned j; - for (j=0; j < messageHandlerList.Size(); j++) - { - messageHandlerList[j]->OnClosedConnection( - target.systemAddress==UNASSIGNED_SYSTEM_ADDRESS ? GetSystemAddressFromGuid(target.rakNetGuid) : target.systemAddress, - target.rakNetGuid==UNASSIGNED_RAKNET_GUID ? GetGuidFromSystemAddress(target.systemAddress) : target.rakNetGuid, - LCR_CLOSED_BY_USER); - } - */ - - CloseConnectionInternal(target, sendDisconnectionNotification, false, orderingChannel, disconnectionNotificationPriority); - - // 12/14/09 Return ID_CONNECTION_LOST when calling CloseConnection with sendDisconnectionNotification==false, elsewise it is never returned - if (sendDisconnectionNotification==false && GetConnectionState(target)==IS_CONNECTED) - { - Packet *packet=AllocPacket(sizeof( char ), _FILE_AND_LINE_); - packet->data[ 0 ] = ID_CONNECTION_LOST; // DeadConnection - packet->guid = target.rakNetGuid==UNASSIGNED_RAKNET_GUID ? GetGuidFromSystemAddress(target.systemAddress) : target.rakNetGuid; - packet->systemAddress = target.systemAddress==UNASSIGNED_SYSTEM_ADDRESS ? GetSystemAddressFromGuid(target.rakNetGuid) : target.systemAddress; - packet->systemAddress.systemIndex = (SystemIndex) GetIndexFromSystemAddress(packet->systemAddress); - packet->guid.systemIndex=packet->systemAddress.systemIndex; - packet->wasGeneratedLocally=true; // else processed twice - AddPacketToProducer(packet); - } -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Cancel a pending connection attempt -// If we are already connected, the connection stays open -// \param[in] target Which system to cancel -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::CancelConnectionAttempt( const SystemAddress target ) -{ - unsigned int i; - - // Cancel pending connection attempt, if there is one - i=0; - requestedConnectionQueueMutex.Lock(); - while (i < requestedConnectionQueue.Size()) - { - if (requestedConnectionQueue[i]->systemAddress==target) - { -#if LIBCAT_SECURITY==1 - CAT_AUDIT_PRINTF("AUDIT: Deleting requestedConnectionQueue %i client_handshake %x\n", i, requestedConnectionQueue[ i ]->client_handshake); - RakNet::OP_DELETE(requestedConnectionQueue[i]->client_handshake, _FILE_AND_LINE_ ); -#endif - RakNet::OP_DELETE(requestedConnectionQueue[i], _FILE_AND_LINE_ ); - requestedConnectionQueue.RemoveAtIndex(i); - break; - } - else - i++; - } - requestedConnectionQueueMutex.Unlock(); - -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - -#ifdef _MSC_VER -#pragma warning( disable : 4702 ) // warning C4702: unreachable code -#endif -ConnectionState RakPeer::GetConnectionState(const AddressOrGUID systemIdentifier) -{ - if (systemIdentifier.systemAddress!=UNASSIGNED_SYSTEM_ADDRESS) - { - unsigned int i=0; - requestedConnectionQueueMutex.Lock(); - for (; i < requestedConnectionQueue.Size(); i++) - { - if (requestedConnectionQueue[i]->systemAddress==systemIdentifier.systemAddress) - { - requestedConnectionQueueMutex.Unlock(); - return IS_PENDING; - } - } - requestedConnectionQueueMutex.Unlock(); - } - - int index; - if (systemIdentifier.systemAddress!=UNASSIGNED_SYSTEM_ADDRESS) - { - index = GetIndexFromSystemAddress(systemIdentifier.systemAddress, false); - } - else - { - index = GetIndexFromGuid(systemIdentifier.rakNetGuid); - } - - if (index==-1) - return IS_NOT_CONNECTED; - - if (remoteSystemList[index].isActive==false) - return IS_DISCONNECTED; - - switch (remoteSystemList[index].connectMode) - { - case RemoteSystemStruct::DISCONNECT_ASAP: - return IS_DISCONNECTING; - case RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY: - return IS_SILENTLY_DISCONNECTING; - case RemoteSystemStruct::DISCONNECT_ON_NO_ACK: - return IS_DISCONNECTING; - case RemoteSystemStruct::REQUESTED_CONNECTION: - return IS_CONNECTING; - case RemoteSystemStruct::HANDLING_CONNECTION_REQUEST: - return IS_CONNECTING; - case RemoteSystemStruct::UNVERIFIED_SENDER: - return IS_CONNECTING; - case RemoteSystemStruct::CONNECTED: - return IS_CONNECTED; - default: - return IS_NOT_CONNECTED; - } - - return IS_NOT_CONNECTED; -} - - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Given a systemAddress, returns an index from 0 to the maximum number of players allowed - 1. -// -// Parameters -// systemAddress - The systemAddress to search for -// -// Returns -// An integer from 0 to the maximum number of peers -1, or -1 if that player is not found -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -int RakPeer::GetIndexFromSystemAddress( const SystemAddress systemAddress ) const -{ - return GetIndexFromSystemAddress(systemAddress, false); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// This function is only useful for looping through all players. -// -// Parameters -// index - an integer between 0 and the maximum number of players allowed - 1. -// -// Returns -// A valid systemAddress or UNASSIGNED_SYSTEM_ADDRESS if no such player at that index -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -SystemAddress RakPeer::GetSystemAddressFromIndex( unsigned int index ) -{ - // remoteSystemList in user thread - //if ( index >= 0 && index < remoteSystemListSize ) - if ( index < maximumNumberOfPeers ) - if (remoteSystemList[index].isActive && remoteSystemList[ index ].connectMode==RakPeer::RemoteSystemStruct::CONNECTED) // Don't give the user players that aren't fully connected, since sends will fail - return remoteSystemList[ index ].systemAddress; - - return UNASSIGNED_SYSTEM_ADDRESS; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Same as GetSystemAddressFromIndex but returns RakNetGUID -// \param[in] index Index should range between 0 and the maximum number of players allowed - 1. -// \return The RakNetGUID -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RakNetGUID RakPeer::GetGUIDFromIndex( unsigned int index ) -{ - // remoteSystemList in user thread - //if ( index >= 0 && index < remoteSystemListSize ) - if ( index < maximumNumberOfPeers ) - if (remoteSystemList[index].isActive && remoteSystemList[ index ].connectMode==RakPeer::RemoteSystemStruct::CONNECTED) // Don't give the user players that aren't fully connected, since sends will fail - return remoteSystemList[ index ].guid; - - return UNASSIGNED_RAKNET_GUID; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Same as calling GetSystemAddressFromIndex and GetGUIDFromIndex for all systems, but more efficient -// Indices match each other, so \a addresses[0] and \a guids[0] refer to the same system -// \param[out] addresses All system addresses. Size of the list is the number of connections. Size of the list will match the size of the \a guids list. -// \param[out] guids All guids. Size of the list is the number of connections. Size of the list will match the size of the \a addresses list. -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::GetSystemList(DataStructures::List &addresses, DataStructures::List &guids) const -{ - addresses.Clear(false, _FILE_AND_LINE_); - guids.Clear(false, _FILE_AND_LINE_); - - if ( remoteSystemList == 0 || endThreads == true ) - return; - - unsigned int i; - for (i=0; i < activeSystemListSize; i++) - { - if ((activeSystemList[i])->isActive && - (activeSystemList[i])->connectMode==RakPeer::RemoteSystemStruct::CONNECTED) - { - addresses.Push((activeSystemList[i])->systemAddress, _FILE_AND_LINE_ ); - guids.Push((activeSystemList[i])->guid, _FILE_AND_LINE_ ); - } - } -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Bans an IP from connecting. Banned IPs persist between connections. -// -// Parameters -// IP - Dotted IP address. Can use * as a wildcard, such as 128.0.0.* will ban -// All IP addresses starting with 128.0.0 -// milliseconds - how many ms for a temporary ban. Use 0 for a permanent ban -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::AddToBanList( const char *IP, RakNet::TimeMS milliseconds ) -{ - unsigned index; - RakNet::TimeMS time = RakNet::GetTimeMS(); - - if ( IP == 0 || IP[ 0 ] == 0 || strlen( IP ) > 15 ) - return ; - - // If this guy is already in the ban list, do nothing - index = 0; - - banListMutex.Lock(); - - for ( ; index < banList.Size(); index++ ) - { - if ( strcmp( IP, banList[ index ]->IP ) == 0 ) - { - // Already in the ban list. Just update the time - if (milliseconds==0) - banList[ index ]->timeout=0; // Infinite - else - banList[ index ]->timeout=time+milliseconds; - banListMutex.Unlock(); - return; - } - } - - banListMutex.Unlock(); - - BanStruct *banStruct = RakNet::OP_NEW( _FILE_AND_LINE_ ); - banStruct->IP = (char*) rakMalloc_Ex( 16, _FILE_AND_LINE_ ); - if (milliseconds==0) - banStruct->timeout=0; // Infinite - else - banStruct->timeout=time+milliseconds; - strcpy( banStruct->IP, IP ); - banListMutex.Lock(); - banList.Insert( banStruct, _FILE_AND_LINE_ ); - banListMutex.Unlock(); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Allows a previously banned IP to connect. -// -// Parameters -// IP - Dotted IP address. Can use * as a wildcard, such as 128.0.0.* will ban -// All IP addresses starting with 128.0.0 -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::RemoveFromBanList( const char *IP ) -{ - unsigned index; - BanStruct *temp; - - if ( IP == 0 || IP[ 0 ] == 0 || strlen( IP ) > 15 ) - return ; - - index = 0; - temp=0; - - banListMutex.Lock(); - - for ( ; index < banList.Size(); index++ ) - { - if ( strcmp( IP, banList[ index ]->IP ) == 0 ) - { - temp = banList[ index ]; - banList[ index ] = banList[ banList.Size() - 1 ]; - banList.RemoveAtIndex( banList.Size() - 1 ); - break; - } - } - - banListMutex.Unlock(); - - if (temp) - { - rakFree_Ex(temp->IP, _FILE_AND_LINE_ ); - RakNet::OP_DELETE(temp, _FILE_AND_LINE_); - } - -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Allows all previously banned IPs to connect. -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::ClearBanList( void ) -{ - unsigned index; - index = 0; - banListMutex.Lock(); - - for ( ; index < banList.Size(); index++ ) - { - rakFree_Ex(banList[ index ]->IP, _FILE_AND_LINE_ ); - RakNet::OP_DELETE(banList[ index ], _FILE_AND_LINE_); - } - - banList.Clear(false, _FILE_AND_LINE_); - - banListMutex.Unlock(); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::SetLimitIPConnectionFrequency(bool b) -{ - limitConnectionFrequencyFromTheSameIP=b; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Determines if a particular IP is banned. -// -// Parameters -// IP - Complete dotted IP address -// -// Returns -// True if IP matches any IPs in the ban list, accounting for any wildcards. -// False otherwise. -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -bool RakPeer::IsBanned( const char *IP ) -{ - unsigned banListIndex, characterIndex; - RakNet::TimeMS time; - BanStruct *temp; - - if ( IP == 0 || IP[ 0 ] == 0 || strlen( IP ) > 15 ) - return false; - - banListIndex = 0; - - if ( banList.Size() == 0 ) - return false; // Skip the mutex if possible - - time = RakNet::GetTimeMS(); - - banListMutex.Lock(); - - while ( banListIndex < banList.Size() ) - { - if (banList[ banListIndex ]->timeout>0 && banList[ banListIndex ]->timeoutIP, _FILE_AND_LINE_ ); - RakNet::OP_DELETE(temp, _FILE_AND_LINE_); - } - else - { - characterIndex = 0; - -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - while ( true ) - { - if ( banList[ banListIndex ]->IP[ characterIndex ] == IP[ characterIndex ] ) - { - // Equal characters - - if ( IP[ characterIndex ] == 0 ) - { - banListMutex.Unlock(); - // End of the string and the strings match - - return true; - } - - characterIndex++; - } - - else - { - if ( banList[ banListIndex ]->IP[ characterIndex ] == 0 || IP[ characterIndex ] == 0 ) - { - // End of one of the strings - break; - } - - // Characters do not match - if ( banList[ banListIndex ]->IP[ characterIndex ] == '*' ) - { - banListMutex.Unlock(); - - // Domain is banned. - return true; - } - - // Characters do not match and it is not a * - break; - } - } - - banListIndex++; - } - } - - banListMutex.Unlock(); - - // No match found. - return false; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Send a ping to the specified connected system. -// -// Parameters: -// target - who to ping -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::Ping( const SystemAddress target ) -{ - PingInternal(target, false, UNRELIABLE); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Send a ping to the specified unconnected system. -// The remote system, if it is Initialized, will respond with ID_UNCONNECTED_PONG. -// The final ping time will be encoded in the following sizeof(RakNet::TimeMS) bytes. (Default is 4 bytes - See __GET_TIME_64BIT in RakNetTypes.h -// -// Parameters: -// host: Either a dotted IP address or a domain name. Can be 255.255.255.255 for LAN broadcast. -// remotePort: Which port to connect to on the remote machine. -// onlyReplyOnAcceptingConnections: Only request a reply if the remote system has open connections -// connectionSocketIndex Index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to send on. -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -bool RakPeer::Ping( const char* host, unsigned short remotePort, bool onlyReplyOnAcceptingConnections, unsigned connectionSocketIndex ) -{ - if ( host == 0 ) - return false; - - // If this assert hits then Startup wasn't called or the call failed. - RakAssert(connectionSocketIndex < socketList.Size()); - -// if ( IsActive() == false ) -// return; - - RakNet::BitStream bitStream( sizeof(unsigned char) + sizeof(RakNet::Time) ); - if ( onlyReplyOnAcceptingConnections ) - bitStream.Write((MessageID)ID_UNCONNECTED_PING_OPEN_CONNECTIONS); - else - bitStream.Write((MessageID)ID_UNCONNECTED_PING); - - bitStream.Write(RakNet::GetTime()); - - bitStream.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); - - bitStream.Write(GetMyGUID()); - - // No timestamp for 255.255.255.255 - unsigned int realIndex = GetRakNetSocketFromUserConnectionSocketIndex(connectionSocketIndex); - /* - - SystemAddress systemAddress; - systemAddress.FromStringExplicitPort(host,remotePort, socketList[realIndex]->GetBoundAddress().GetIPVersion()); - systemAddress.FixForIPVersion(socketList[realIndex]->GetBoundAddress()); - - unsigned i; - for (i=0; i < pluginListNTS.Size(); i++) - pluginListNTS[i]->OnDirectSocketSend((const char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), systemAddress); - SocketLayer::SendTo( socketList[realIndex], (const char*)bitStream.GetData(), (int) bitStream.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); - */ - - RNS2_SendParameters bsp; - bsp.data = (char*) bitStream.GetData() ; - bsp.length = bitStream.GetNumberOfBytesUsed(); - bsp.systemAddress.FromStringExplicitPort(host,remotePort, socketList[realIndex]->GetBoundAddress().GetIPVersion()); - if (bsp.systemAddress==UNASSIGNED_SYSTEM_ADDRESS) - return false; - bsp.systemAddress.FixForIPVersion(socketList[realIndex]->GetBoundAddress()); - unsigned i; - for (i=0; i < pluginListNTS.Size(); i++) - pluginListNTS[i]->OnDirectSocketSend((const char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), bsp.systemAddress); - socketList[realIndex]->Send(&bsp, _FILE_AND_LINE_); - - return true; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Returns the average of all ping times read for a specified target -// -// Parameters: -// target - whose time to read -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -int RakPeer::GetAveragePing( const AddressOrGUID systemIdentifier ) -{ - int sum, quantity; - RemoteSystemStruct *remoteSystem = GetRemoteSystem( systemIdentifier, false, false ); - - if ( remoteSystem == 0 ) - return -1; - - for ( sum = 0, quantity = 0; quantity < PING_TIMES_ARRAY_SIZE; quantity++ ) - { - if ( remoteSystem->pingAndClockDifferential[ quantity ].pingTime == 65535 ) - break; - else - sum += remoteSystem->pingAndClockDifferential[ quantity ].pingTime; - } - - if ( quantity > 0 ) - return sum / quantity; - else - return -1; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Returns the last ping time read for the specific player or -1 if none read yet -// -// Parameters: -// target - whose time to read -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -int RakPeer::GetLastPing( const AddressOrGUID systemIdentifier ) const -{ - RemoteSystemStruct * remoteSystem = GetRemoteSystem( systemIdentifier, false, false ); - - if ( remoteSystem == 0 ) - return -1; - -// return (int)(remoteSystem->reliabilityLayer.GetAckPing()/(RakNet::TimeUS)1000); - - if ( remoteSystem->pingAndClockDifferentialWriteIndex == 0 ) - return remoteSystem->pingAndClockDifferential[ PING_TIMES_ARRAY_SIZE - 1 ].pingTime; - else - return remoteSystem->pingAndClockDifferential[ remoteSystem->pingAndClockDifferentialWriteIndex - 1 ].pingTime; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Returns the lowest ping time read or -1 if none read yet -// -// Parameters: -// target - whose time to read -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -int RakPeer::GetLowestPing( const AddressOrGUID systemIdentifier ) const -{ - RemoteSystemStruct * remoteSystem = GetRemoteSystem( systemIdentifier, false, false ); - - if ( remoteSystem == 0 ) - return -1; - - return remoteSystem->lowestPing; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Ping the remote systems every so often. This is off by default -// This will work anytime -// -// Parameters: -// doPing - True to start occasional pings. False to stop them. -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::SetOccasionalPing( bool doPing ) -{ - occasionalPing = doPing; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -/// Return the clock difference between your system and the specified system -/// Subtract the time from a time returned by the remote system to get that time relative to your own system -/// Returns 0 if the system is unknown -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RakNet::Time RakPeer::GetClockDifferential( const AddressOrGUID systemIdentifier ) -{ - RemoteSystemStruct *remoteSystem = GetRemoteSystem(systemIdentifier, false, false); - if (remoteSystem == 0) - return 0; - return GetClockDifferentialInt(remoteSystem); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - -RakNet::Time RakPeer::GetClockDifferentialInt(RemoteSystemStruct *remoteSystem) const -{ - int counter, lowestPingSoFar; - RakNet::Time clockDifferential; - - lowestPingSoFar = 65535; - - clockDifferential = 0; - - for ( counter = 0; counter < PING_TIMES_ARRAY_SIZE; counter++ ) - { - if ( remoteSystem->pingAndClockDifferential[ counter ].pingTime == 65535 ) - break; - - if ( remoteSystem->pingAndClockDifferential[ counter ].pingTime < lowestPingSoFar ) - { - clockDifferential = remoteSystem->pingAndClockDifferential[ counter ].clockDifferential; - lowestPingSoFar = remoteSystem->pingAndClockDifferential[ counter ].pingTime; - } - } - - return clockDifferential; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Length should be under 400 bytes, as a security measure against flood attacks -// Sets the data to send with an (LAN server discovery) /(offline ping) response -// See the Ping sample project for how this is used. -// data: a block of data to store, or 0 for none -// length: The length of data in bytes, or 0 for none -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::SetOfflinePingResponse( const char *data, const unsigned int length ) -{ - RakAssert(length < 400); - - rakPeerMutexes[ offlinePingResponse_Mutex ].Lock(); - offlinePingResponse.Reset(); - - if ( data && length > 0 ) - offlinePingResponse.Write( data, length ); - - rakPeerMutexes[ offlinePingResponse_Mutex ].Unlock(); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Returns pointers to a copy of the data passed to SetOfflinePingResponse -// \param[out] data A pointer to a copy of the data passed to \a SetOfflinePingResponse() -// \param[out] length A pointer filled in with the length parameter passed to SetOfflinePingResponse() -// \sa SetOfflinePingResponse -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::GetOfflinePingResponse( char **data, unsigned int *length ) -{ - rakPeerMutexes[ offlinePingResponse_Mutex ].Lock(); - *data = (char*) offlinePingResponse.GetData(); - *length = (int) offlinePingResponse.GetNumberOfBytesUsed(); - rakPeerMutexes[ offlinePingResponse_Mutex ].Unlock(); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Return the unique SystemAddress that represents you on the the network -// Note that unlike in previous versions, this is a struct and is not sequential -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -SystemAddress RakPeer::GetInternalID( const SystemAddress systemAddress, const int index ) const -{ - if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS) - { - return ipList[index]; - } - else - { - -// SystemAddress returnValue; - RemoteSystemStruct * remoteSystem = GetRemoteSystemFromSystemAddress( systemAddress, false, true ); - if (remoteSystem==0) - return UNASSIGNED_SYSTEM_ADDRESS; - - return remoteSystem->theirInternalSystemAddress[index]; - /* - sockaddr_in sa; - socklen_t len = sizeof(sa); - if (getsockname__(connectionSockets[remoteSystem->connectionSocketIndex], (sockaddr*)&sa, &len)!=0) - return UNASSIGNED_SYSTEM_ADDRESS; - returnValue.port=ntohs(sa.sin_port); - returnValue.binaryAddress=sa.sin_addr.s_addr; - return returnValue; -*/ - - - - } -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -/// \brief Sets your internal IP address, for platforms that do not support reading it, or to override a value -/// \param[in] systemAddress. The address to set. Use SystemAddress::FromString() if you want to use a dotted string -/// \param[in] index When you have multiple internal IDs, which index to set? -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::SetInternalID(SystemAddress systemAddress, int index) -{ - RakAssert(index >=0 && index < MAXIMUM_NUMBER_OF_INTERNAL_IDS); - ipList[index]=systemAddress; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Return the unique address identifier that represents you on the the network and is based on your external -// IP / port (the IP / port the specified player uses to communicate with you) -// Note that unlike in previous versions, this is a struct and is not sequential -// -// Parameters: -// target: Which remote system you are referring to for your external ID -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -SystemAddress RakPeer::GetExternalID( const SystemAddress target ) const -{ - unsigned i; - SystemAddress inactiveExternalId; - - inactiveExternalId=UNASSIGNED_SYSTEM_ADDRESS; - - if (target==UNASSIGNED_SYSTEM_ADDRESS) - return firstExternalID; - - // First check for active connection with this systemAddress - for ( i = 0; i < maximumNumberOfPeers; i++ ) - { - if (remoteSystemList[ i ].systemAddress == target ) - { - if ( remoteSystemList[ i ].isActive ) - return remoteSystemList[ i ].myExternalSystemAddress; - else if (remoteSystemList[ i ].myExternalSystemAddress!=UNASSIGNED_SYSTEM_ADDRESS) - inactiveExternalId=remoteSystemList[ i ].myExternalSystemAddress; - } - } - - return inactiveExternalId; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - -const RakNetGUID RakPeer::GetMyGUID(void) const -{ - return myGuid; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -SystemAddress RakPeer::GetMyBoundAddress(const int socketIndex) -{ - DataStructures::List sockets; - GetSockets( sockets ); - if (sockets.Size()>0) - return sockets[socketIndex]->GetBoundAddress(); - else - return UNASSIGNED_SYSTEM_ADDRESS; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - -const RakNetGUID& RakPeer::GetGuidFromSystemAddress( const SystemAddress input ) const -{ - if (input==UNASSIGNED_SYSTEM_ADDRESS) - return myGuid; - - if (input.systemIndex!=(SystemIndex)-1 && input.systemIndexreliabilityLayer.SetTimeoutTime(timeMS); - } -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - -RakNet::TimeMS RakPeer::GetTimeoutTime( const SystemAddress target ) -{ - if (target==UNASSIGNED_SYSTEM_ADDRESS) - { - return defaultTimeoutTime; - } - else - { - RemoteSystemStruct * remoteSystem = GetRemoteSystemFromSystemAddress( target, false, true ); - - if ( remoteSystem != 0 ) - remoteSystem->reliabilityLayer.GetTimeoutTime(); - } - return defaultTimeoutTime; -} - - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Returns the current MTU size -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -int RakPeer::GetMTUSize( const SystemAddress target ) const -{ - if (target!=UNASSIGNED_SYSTEM_ADDRESS) - { - RemoteSystemStruct *rss=GetRemoteSystemFromSystemAddress(target, false, true); - if (rss) - return rss->MTUSize; - } - return defaultMTUSize; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Returns the number of IP addresses we have -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -unsigned int RakPeer::GetNumberOfAddresses( void ) -{ - - if (IsActive()==false) - { - FillIPList(); - } - - int i = 0; - - while ( ipList[ i ]!=UNASSIGNED_SYSTEM_ADDRESS ) - i++; - - return i; - - - - -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Returns an IP address at index 0 to GetNumberOfAddresses-1 -// \param[in] index index into the list of IP addresses -// \return The local IP address at this index -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -const char* RakPeer::GetLocalIP( unsigned int index ) -{ - if (IsActive()==false) - { - // Fill out ipList structure - - FillIPList(); - - } - - - static char str[128]; - ipList[index].ToString(false,str); - return str; - - - - -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Is this a local IP? -// \param[in] An IP address to check -// \return True if this is one of the IP addresses returned by GetLocalIP -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -bool RakPeer::IsLocalIP( const char *ip ) -{ - if (ip==0 || ip[0]==0) - return false; - - - if (strcmp(ip, "127.0.0.1")==0 || strcmp(ip, "localhost")==0) - return true; - - int num = GetNumberOfAddresses(); - int i; - for (i=0; i < num; i++) - { - if (strcmp(ip, GetLocalIP(i))==0) - return true; - } - - - - - return false; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Allow or disallow connection responses from any IP. Normally this should be false, but may be necessary -// when connection to servers with multiple IP addresses -// -// Parameters: -// allow - True to allow this behavior, false to not allow. Defaults to false. Value persists between connections -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::AllowConnectionResponseIPMigration( bool allow ) -{ - allowConnectionResponseIPMigration = allow; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Description: -// Sends a message ID_ADVERTISE_SYSTEM to the remote unconnected system. -// This will tell the remote system our external IP outside the LAN, and can be used for NAT punch through -// -// Requires: -// The sender and recipient must already be started via a successful call to Initialize -// -// host: Either a dotted IP address or a domain name -// remotePort: Which port to connect to on the remote machine. -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -bool RakPeer::AdvertiseSystem( const char *host, unsigned short remotePort, const char *data, int dataLength, unsigned connectionSocketIndex ) -{ - RakNet::BitStream bs; - bs.Write((MessageID)ID_ADVERTISE_SYSTEM); - bs.WriteAlignedBytes((const unsigned char*) data,dataLength); - return SendOutOfBand(host, remotePort, (const char*) bs.GetData(), bs.GetNumberOfBytesUsed(), connectionSocketIndex ); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Controls how often to return ID_DOWNLOAD_PROGRESS for large message downloads. -// ID_DOWNLOAD_PROGRESS is returned to indicate a new partial message chunk, roughly the MTU size, has arrived -// As it can be slow or cumbersome to get this notification for every chunk, you can set the interval at which it is returned. -// Defaults to 0 (never return this notification) -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::SetSplitMessageProgressInterval(int interval) -{ - RakAssert(interval>=0); - splitMessageProgressInterval=interval; - for ( unsigned short i = 0; i < maximumNumberOfPeers; i++ ) - remoteSystemList[ i ].reliabilityLayer.SetSplitMessageProgressInterval(splitMessageProgressInterval); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Returns what was passed to SetSplitMessageProgressInterval() -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -int RakPeer::GetSplitMessageProgressInterval(void) const -{ - return splitMessageProgressInterval; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Set how long to wait before giving up on sending an unreliable message -// Useful if the network is clogged up. -// Set to 0 or less to never timeout. Defaults to 0. -// timeoutMS How many ms to wait before simply not sending an unreliable message. -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::SetUnreliableTimeout(RakNet::TimeMS timeoutMS) -{ - unreliableTimeout=timeoutMS; - for ( unsigned short i = 0; i < maximumNumberOfPeers; i++ ) - remoteSystemList[ i ].reliabilityLayer.SetUnreliableTimeout(unreliableTimeout); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Send a message to host, with the IP socket option TTL set to 3 -// This message will not reach the host, but will open the router. -// Used for NAT-Punchthrough -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::SendTTL( const char* host, unsigned short remotePort, int ttl, unsigned connectionSocketIndex ) -{ -#if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) - char fakeData[2]; - fakeData[0]=0; - fakeData[1]=1; - unsigned int realIndex = GetRakNetSocketFromUserConnectionSocketIndex(connectionSocketIndex); - if (socketList[realIndex]->IsBerkleySocket()) - { - RNS2_SendParameters bsp; - bsp.data = (char*) fakeData; - bsp.length = 2; - bsp.systemAddress.FromStringExplicitPort(host,remotePort, socketList[realIndex]->GetBoundAddress().GetIPVersion()); - bsp.systemAddress.FixForIPVersion(socketList[realIndex]->GetBoundAddress()); - bsp.ttl=ttl; - unsigned i; - for (i=0; i < pluginListNTS.Size(); i++) - pluginListNTS[i]->OnDirectSocketSend((const char*)bsp.data, BYTES_TO_BITS(bsp.length), bsp.systemAddress); - socketList[realIndex]->Send(&bsp, _FILE_AND_LINE_); - } -#endif -} - - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Attatches a Plugin interface to run code automatically on message receipt in the Receive call -// -// \param messageHandler Pointer to a plugin to attach -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::AttachPlugin( PluginInterface2 *plugin ) -{ - bool isNotThreadsafe = plugin->UsesReliabilityLayer(); - if (isNotThreadsafe) - { - if (pluginListNTS.GetIndexOf(plugin)==MAX_UNSIGNED_LONG) - { - plugin->SetRakPeerInterface(this); - plugin->OnAttach(); - pluginListNTS.Insert(plugin, _FILE_AND_LINE_); - } - } - else - { - if (pluginListTS.GetIndexOf(plugin)==MAX_UNSIGNED_LONG) - { - plugin->SetRakPeerInterface(this); - plugin->OnAttach(); - pluginListTS.Insert(plugin, _FILE_AND_LINE_); - } - } -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Detaches a Plugin interface to run code automatically on message receipt -// -// \param messageHandler Pointer to a plugin to detach -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::DetachPlugin( PluginInterface2 *plugin ) -{ - if (plugin==0) - return; - - unsigned int index; - - bool isNotThreadsafe = plugin->UsesReliabilityLayer(); - if (isNotThreadsafe) - { - index = pluginListNTS.GetIndexOf(plugin); - if (index!=MAX_UNSIGNED_LONG) - { - // Unordered list so delete from end for speed - pluginListNTS[index]=pluginListNTS[pluginListNTS.Size()-1]; - pluginListNTS.RemoveFromEnd(); - } - } - else - { - index = pluginListTS.GetIndexOf(plugin); - if (index!=MAX_UNSIGNED_LONG) - { - // Unordered list so delete from end for speed - pluginListTS[index]=pluginListTS[pluginListTS.Size()-1]; - pluginListTS.RemoveFromEnd(); - } - } - plugin->OnDetach(); - plugin->SetRakPeerInterface(0); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Put a packet back at the end of the receive queue in case you don't want to deal with it immediately -// -// packet The packet you want to push back. -// pushAtHead True to push the packet so that the next receive call returns it. False to push it at the end of the queue (obviously pushing it at the end makes the packets out of order) -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::PushBackPacket( Packet *packet, bool pushAtHead) -{ - if (packet==0) - return; - - unsigned i; - for (i=0; i < pluginListTS.Size(); i++) - pluginListTS[i]->OnPushBackPacket((const char*) packet->data, packet->bitSize, packet->systemAddress); - for (i=0; i < pluginListNTS.Size(); i++) - pluginListNTS[i]->OnPushBackPacket((const char*) packet->data, packet->bitSize, packet->systemAddress); - - packetReturnMutex.Lock(); - if (pushAtHead) - packetReturnQueue.PushAtHead(packet,0,_FILE_AND_LINE_); - else - packetReturnQueue.Push(packet,_FILE_AND_LINE_); - packetReturnMutex.Unlock(); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::ChangeSystemAddress(RakNetGUID guid, const SystemAddress &systemAddress) -{ - BufferedCommandStruct *bcs; - - bcs=bufferedCommands.Allocate( _FILE_AND_LINE_ ); - bcs->data = 0; - bcs->systemIdentifier.systemAddress=systemAddress; - bcs->systemIdentifier.rakNetGuid=guid; - bcs->command=BufferedCommandStruct::BCS_CHANGE_SYSTEM_ADDRESS; - bufferedCommands.Push(bcs); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -Packet* RakPeer::AllocatePacket(unsigned dataSize) -{ - return AllocPacket(dataSize, _FILE_AND_LINE_); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RakNetSocket2* RakPeer::GetSocket( const SystemAddress target ) -{ - // Send a query to the thread to get the socket, and return when we got it - BufferedCommandStruct *bcs; - bcs=bufferedCommands.Allocate( _FILE_AND_LINE_ ); - bcs->command=BufferedCommandStruct::BCS_GET_SOCKET; - bcs->systemIdentifier=target; - bcs->data=0; - bufferedCommands.Push(bcs); - - // Block up to one second to get the socket, although it should actually take virtually no time - SocketQueryOutput *sqo; - RakNet::TimeMS stopWaiting = RakNet::GetTimeMS()+1000; - DataStructures::List output; - while (RakNet::GetTimeMS() < stopWaiting) - { - if (isMainLoopThreadActive==false) - return 0; - - RakSleep(0); - - sqo = socketQueryOutput.Pop(); - if (sqo) - { - output=sqo->sockets; - sqo->sockets.Clear(false, _FILE_AND_LINE_); - socketQueryOutput.Deallocate(sqo, _FILE_AND_LINE_); - if (output.Size()) - return output[0]; - break; - } - } - return 0; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::GetSockets( DataStructures::List &sockets ) -{ - sockets.Clear(false, _FILE_AND_LINE_); - - // Send a query to the thread to get the socket, and return when we got it - BufferedCommandStruct *bcs; - - bcs=bufferedCommands.Allocate( _FILE_AND_LINE_ ); - bcs->command=BufferedCommandStruct::BCS_GET_SOCKET; - bcs->systemIdentifier=UNASSIGNED_SYSTEM_ADDRESS; - bcs->data=0; - bufferedCommands.Push(bcs); - - // Block up to one second to get the socket, although it should actually take virtually no time - SocketQueryOutput *sqo; -// RakNetSocket2* output; - while (1) - { - if (isMainLoopThreadActive==false) - return; - - RakSleep(0); - - sqo = socketQueryOutput.Pop(); - if (sqo) - { - sockets=sqo->sockets; - sqo->sockets.Clear(false, _FILE_AND_LINE_); - socketQueryOutput.Deallocate(sqo, _FILE_AND_LINE_); - return; - } - } - return; -} -void RakPeer::ReleaseSockets( DataStructures::List &sockets ) -{ - sockets.Clear(false,_FILE_AND_LINE_); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Adds simulated ping and packet loss to the outgoing data flow. -// To simulate bi-directional ping and packet loss, you should call this on both the sender and the recipient, with half the total ping and maxSendBPS value on each. -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::ApplyNetworkSimulator( float packetloss, unsigned short minExtraPing, unsigned short extraPingVariance) -{ -#ifdef _DEBUG - if (remoteSystemList) - { - unsigned short i; - for (i=0; i < maximumNumberOfPeers; i++) - //for (i=0; i < remoteSystemListSize; i++) - remoteSystemList[i].reliabilityLayer.ApplyNetworkSimulator(packetloss, minExtraPing, extraPingVariance); - } - - _packetloss=packetloss; - _minExtraPing=minExtraPing; - _extraPingVariance=extraPingVariance; -#endif -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - -void RakPeer::SetPerConnectionOutgoingBandwidthLimit( unsigned maxBitsPerSecond ) -{ - maxOutgoingBPS=maxBitsPerSecond; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Returns if you previously called ApplyNetworkSimulator -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -bool RakPeer::IsNetworkSimulatorActive( void ) -{ -#ifdef _DEBUG - return _packetloss>0 || _minExtraPing>0 || _extraPingVariance>0; -#else - return false; -#endif -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::WriteOutOfBandHeader(RakNet::BitStream *bitStream) -{ - bitStream->Write((MessageID)ID_OUT_OF_BAND_INTERNAL); - bitStream->Write(myGuid); - bitStream->WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::SetUserUpdateThread(void (*_userUpdateThreadPtr)(RakPeerInterface *, void *), void *_userUpdateThreadData) -{ - userUpdateThreadPtr=_userUpdateThreadPtr; - userUpdateThreadData=_userUpdateThreadData; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::SetIncomingDatagramEventHandler( bool (*_incomingDatagramEventHandler)(RNS2RecvStruct *) ) -{ - incomingDatagramEventHandler=_incomingDatagramEventHandler; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -bool RakPeer::SendOutOfBand(const char *host, unsigned short remotePort, const char *data, BitSize_t dataLength, unsigned connectionSocketIndex ) -{ - if ( IsActive() == false ) - return false; - - if (host==0 || host[0]==0) - return false; - - // If this assert hits then Startup wasn't called or the call failed. - RakAssert(connectionSocketIndex < socketList.Size()); - - // This is a security measure. Don't send data longer than this value - RakAssert(dataLength <= (MAX_OFFLINE_DATA_LENGTH + sizeof(unsigned char)+sizeof(RakNet::Time)+RakNetGUID::size()+sizeof(OFFLINE_MESSAGE_DATA_ID))); - - if (host==0) - return false; - - // 34 bytes - RakNet::BitStream bitStream; - WriteOutOfBandHeader(&bitStream); - - if (dataLength>0) - { - bitStream.Write(data, dataLength); - } - - unsigned int realIndex = GetRakNetSocketFromUserConnectionSocketIndex(connectionSocketIndex); - - /* - SystemAddress systemAddress; - systemAddress.FromStringExplicitPort(host,remotePort, socketList[realIndex]->GetBoundAddress().GetIPVersion()); - systemAddress.FixForIPVersion(socketList[realIndex]->GetBoundAddress()); - - unsigned i; - for (i=0; i < pluginListNTS.Size(); i++) - pluginListNTS[i]->OnDirectSocketSend((const char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), systemAddress); - - SocketLayer::SendTo( socketList[realIndex], (const char*)bitStream.GetData(), (int) bitStream.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); - */ - - RNS2_SendParameters bsp; - bsp.data = (char*) bitStream.GetData(); - bsp.length = bitStream.GetNumberOfBytesUsed(); - bsp.systemAddress.FromStringExplicitPort(host,remotePort, socketList[realIndex]->GetBoundAddress().GetIPVersion()); - bsp.systemAddress.FixForIPVersion(socketList[realIndex]->GetBoundAddress()); - unsigned i; - for (i=0; i < pluginListNTS.Size(); i++) - pluginListNTS[i]->OnDirectSocketSend((const char*)bsp.data, BYTES_TO_BITS(bsp.length), bsp.systemAddress); - socketList[realIndex]->Send(&bsp, _FILE_AND_LINE_); - - return true; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RakNetStatistics * RakPeer::GetStatistics( const SystemAddress systemAddress, RakNetStatistics *rns ) -{ - static RakNetStatistics staticStatistics; - RakNetStatistics *systemStats; - if (rns==0) - systemStats=&staticStatistics; - else - systemStats=rns; - - if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS) - { - bool firstWrite=false; - // Return a crude sum - for ( unsigned short i = 0; i < maximumNumberOfPeers; i++ ) - { - if (remoteSystemList[ i ].isActive) - { - RakNetStatistics rnsTemp; - remoteSystemList[ i ].reliabilityLayer.GetStatistics(&rnsTemp); - - if (firstWrite==false) - { - memcpy(systemStats, &rnsTemp, sizeof(RakNetStatistics)); - firstWrite=true; - } - else - (*systemStats)+=rnsTemp; - } - } - return systemStats; - } - else - { - RemoteSystemStruct * rss; - rss = GetRemoteSystemFromSystemAddress( systemAddress, false, false ); - if ( rss && endThreads==false ) - { - rss->reliabilityLayer.GetStatistics(systemStats); - return systemStats; - } - } - - return 0; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::GetStatisticsList(DataStructures::List &addresses, DataStructures::List &guids, DataStructures::List &statistics) -{ - addresses.Clear(false, _FILE_AND_LINE_); - guids.Clear(false, _FILE_AND_LINE_); - statistics.Clear(false, _FILE_AND_LINE_); - - if ( remoteSystemList == 0 || endThreads == true ) - return; - - unsigned int i; - for (i=0; i < activeSystemListSize; i++) - { - if ((activeSystemList[i])->isActive && - (activeSystemList[i])->connectMode==RakPeer::RemoteSystemStruct::CONNECTED) - { - addresses.Push((activeSystemList[i])->systemAddress, _FILE_AND_LINE_ ); - guids.Push((activeSystemList[i])->guid, _FILE_AND_LINE_ ); - RakNetStatistics rns; - (activeSystemList[i])->reliabilityLayer.GetStatistics(&rns); - statistics.Push(rns, _FILE_AND_LINE_); - } - } -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -bool RakPeer::GetStatistics( const unsigned int index, RakNetStatistics *rns ) -{ - if (index < maximumNumberOfPeers && remoteSystemList[ index ].isActive) - { - remoteSystemList[ index ].reliabilityLayer.GetStatistics(rns); - return true; - } - return false; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -unsigned int RakPeer::GetReceiveBufferSize(void) -{ - unsigned int size; - packetReturnMutex.Lock(); - size=packetReturnQueue.Size(); - packetReturnMutex.Unlock(); - return size; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -int RakPeer::GetIndexFromSystemAddress( const SystemAddress systemAddress, bool calledFromNetworkThread ) const -{ - unsigned i; - - if ( systemAddress == UNASSIGNED_SYSTEM_ADDRESS ) - return -1; - - if (systemAddress.systemIndex!=(SystemIndex)-1 && systemAddress.systemIndex < maximumNumberOfPeers && remoteSystemList[systemAddress.systemIndex].systemAddress==systemAddress && remoteSystemList[ systemAddress.systemIndex ].isActive) - return systemAddress.systemIndex; - - if (calledFromNetworkThread) - { - return GetRemoteSystemIndex(systemAddress); - } - else - { - // remoteSystemList in user and network thread - for ( i = 0; i < maximumNumberOfPeers; i++ ) - if ( remoteSystemList[ i ].isActive && remoteSystemList[ i ].systemAddress == systemAddress ) - return i; - - // If no active results found, try previously active results. - for ( i = 0; i < maximumNumberOfPeers; i++ ) - if ( remoteSystemList[ i ].systemAddress == systemAddress ) - return i; - } - - return -1; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -int RakPeer::GetIndexFromGuid( const RakNetGUID guid ) -{ - unsigned i; - - if ( guid == UNASSIGNED_RAKNET_GUID ) - return -1; - - if (guid.systemIndex!=(SystemIndex)-1 && guid.systemIndex < maximumNumberOfPeers && remoteSystemList[guid.systemIndex].guid==guid && remoteSystemList[ guid.systemIndex ].isActive) - return guid.systemIndex; - - // remoteSystemList in user and network thread - for ( i = 0; i < maximumNumberOfPeers; i++ ) - if ( remoteSystemList[ i ].isActive && remoteSystemList[ i ].guid == guid ) - return i; - - // If no active results found, try previously active results. - for ( i = 0; i < maximumNumberOfPeers; i++ ) - if ( remoteSystemList[ i ].guid == guid ) - return i; - - return -1; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -#if LIBCAT_SECURITY==1 -bool RakPeer::GenerateConnectionRequestChallenge(RequestedConnectionStruct *rcs,PublicKey *publicKey) -{ - CAT_AUDIT_PRINTF("AUDIT: In GenerateConnectionRequestChallenge()\n"); - - rcs->client_handshake = 0; - rcs->publicKeyMode = PKM_INSECURE_CONNECTION; - - if (!publicKey) return true; - - switch (publicKey->publicKeyMode) - { - default: - case PKM_INSECURE_CONNECTION: - break; - - case PKM_ACCEPT_ANY_PUBLIC_KEY: - CAT_OBJCLR(rcs->remote_public_key); - rcs->client_handshake = RakNet::OP_NEW(_FILE_AND_LINE_); - - rcs->publicKeyMode = PKM_ACCEPT_ANY_PUBLIC_KEY; - break; - - case PKM_USE_TWO_WAY_AUTHENTICATION: - if (publicKey->myPublicKey == 0 || publicKey->myPrivateKey == 0 || - publicKey->remoteServerPublicKey == 0) - { - return false; - } - - rcs->client_handshake = RakNet::OP_NEW(_FILE_AND_LINE_); - memcpy(rcs->remote_public_key, publicKey->remoteServerPublicKey, cat::EasyHandshake::PUBLIC_KEY_BYTES); - - if (!rcs->client_handshake->Initialize(publicKey->remoteServerPublicKey) || - !rcs->client_handshake->SetIdentity(publicKey->myPublicKey, publicKey->myPrivateKey) || - !rcs->client_handshake->GenerateChallenge(rcs->handshakeChallenge)) - { - CAT_AUDIT_PRINTF("AUDIT: Failure initializing new client_handshake object with identity for this RequestedConnectionStruct\n"); - RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); - rcs->client_handshake=0; - return false; - } - - CAT_AUDIT_PRINTF("AUDIT: Success initializing new client handshake object with identity for this RequestedConnectionStruct -- pre-generated challenge\n"); - - rcs->publicKeyMode = PKM_USE_TWO_WAY_AUTHENTICATION; - break; - - case PKM_USE_KNOWN_PUBLIC_KEY: - if (publicKey->remoteServerPublicKey == 0) - return false; - - rcs->client_handshake = RakNet::OP_NEW(_FILE_AND_LINE_); - memcpy(rcs->remote_public_key, publicKey->remoteServerPublicKey, cat::EasyHandshake::PUBLIC_KEY_BYTES); - - if (!rcs->client_handshake->Initialize(publicKey->remoteServerPublicKey) || - !rcs->client_handshake->GenerateChallenge(rcs->handshakeChallenge)) - { - CAT_AUDIT_PRINTF("AUDIT: Failure initializing new client_handshake object for this RequestedConnectionStruct\n"); - RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); - rcs->client_handshake=0; - return false; - } - - CAT_AUDIT_PRINTF("AUDIT: Success initializing new client handshake object for this RequestedConnectionStruct -- pre-generated challenge\n"); - - rcs->publicKeyMode = PKM_USE_KNOWN_PUBLIC_KEY; - break; - } - - return true; -} -#endif -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -ConnectionAttemptResult RakPeer::SendConnectionRequest( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned int extraData, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNet::TimeMS timeoutTime ) -{ - RakAssert(passwordDataLength <= 256); - RakAssert(remotePort!=0); - SystemAddress systemAddress; - if (!systemAddress.FromStringExplicitPort(host,remotePort,socketList[connectionSocketIndex]->GetBoundAddress().GetIPVersion())) - return CANNOT_RESOLVE_DOMAIN_NAME; - - // Already connected? - if (GetRemoteSystemFromSystemAddress(systemAddress, false, true)) - return ALREADY_CONNECTED_TO_ENDPOINT; - - //RequestedConnectionStruct *rcs = (RequestedConnectionStruct *) rakMalloc_Ex(sizeof(RequestedConnectionStruct), _FILE_AND_LINE_); - RequestedConnectionStruct *rcs = RakNet::OP_NEW(_FILE_AND_LINE_); - - rcs->systemAddress=systemAddress; - rcs->nextRequestTime=RakNet::GetTimeMS(); - rcs->requestsMade=0; - rcs->data=0; - rcs->socket=0; - rcs->extraData=extraData; - rcs->socketIndex=connectionSocketIndex; - rcs->actionToTake=RequestedConnectionStruct::CONNECT; - rcs->sendConnectionAttemptCount=sendConnectionAttemptCount; - rcs->timeBetweenSendConnectionAttemptsMS=timeBetweenSendConnectionAttemptsMS; - memcpy(rcs->outgoingPassword, passwordData, passwordDataLength); - rcs->outgoingPasswordLength=(unsigned char) passwordDataLength; - rcs->timeoutTime=timeoutTime; - -#if LIBCAT_SECURITY==1 - CAT_AUDIT_PRINTF("AUDIT: In SendConnectionRequest()\n"); - if (!GenerateConnectionRequestChallenge(rcs,publicKey)) - return SECURITY_INITIALIZATION_FAILED; -#else - (void) publicKey; -#endif - - // Return false if already pending, else push on queue - unsigned int i=0; - requestedConnectionQueueMutex.Lock(); - for (; i < requestedConnectionQueue.Size(); i++) - { - if (requestedConnectionQueue[i]->systemAddress==systemAddress) - { - requestedConnectionQueueMutex.Unlock(); - // Not necessary - //RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); - RakNet::OP_DELETE(rcs,_FILE_AND_LINE_); - return CONNECTION_ATTEMPT_ALREADY_IN_PROGRESS; - } - } - requestedConnectionQueue.Push(rcs, _FILE_AND_LINE_ ); - requestedConnectionQueueMutex.Unlock(); - - return CONNECTION_ATTEMPT_STARTED; -} -ConnectionAttemptResult RakPeer::SendConnectionRequest( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned int extraData, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNet::TimeMS timeoutTime, RakNetSocket2* socket ) -{ - RakAssert(passwordDataLength <= 256); - SystemAddress systemAddress; - systemAddress.FromStringExplicitPort(host,remotePort); - - // Already connected? - if (GetRemoteSystemFromSystemAddress(systemAddress, false, true)) - return ALREADY_CONNECTED_TO_ENDPOINT; - - //RequestedConnectionStruct *rcs = (RequestedConnectionStruct *) rakMalloc_Ex(sizeof(RequestedConnectionStruct), _FILE_AND_LINE_); - RequestedConnectionStruct *rcs = RakNet::OP_NEW(_FILE_AND_LINE_); - - rcs->systemAddress=systemAddress; - rcs->nextRequestTime=RakNet::GetTimeMS(); - rcs->requestsMade=0; - rcs->data=0; - rcs->socket=0; - rcs->extraData=extraData; - rcs->socketIndex=connectionSocketIndex; - rcs->actionToTake=RequestedConnectionStruct::CONNECT; - rcs->sendConnectionAttemptCount=sendConnectionAttemptCount; - rcs->timeBetweenSendConnectionAttemptsMS=timeBetweenSendConnectionAttemptsMS; - memcpy(rcs->outgoingPassword, passwordData, passwordDataLength); - rcs->outgoingPasswordLength=(unsigned char) passwordDataLength; - rcs->timeoutTime=timeoutTime; - rcs->socket=socket; - -#if LIBCAT_SECURITY==1 - if (!GenerateConnectionRequestChallenge(rcs,publicKey)) - return SECURITY_INITIALIZATION_FAILED; -#else - (void) publicKey; -#endif - - // Return false if already pending, else push on queue - unsigned int i=0; - requestedConnectionQueueMutex.Lock(); - for (; i < requestedConnectionQueue.Size(); i++) - { - if (requestedConnectionQueue[i]->systemAddress==systemAddress) - { - requestedConnectionQueueMutex.Unlock(); - // Not necessary - //RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); - RakNet::OP_DELETE(rcs,_FILE_AND_LINE_); - return CONNECTION_ATTEMPT_ALREADY_IN_PROGRESS; - } - } - requestedConnectionQueue.Push(rcs, _FILE_AND_LINE_ ); - requestedConnectionQueueMutex.Unlock(); - - return CONNECTION_ATTEMPT_STARTED; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::ValidateRemoteSystemLookup(void) const -{ -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RakPeer::RemoteSystemStruct *RakPeer::GetRemoteSystem( const AddressOrGUID systemIdentifier, bool calledFromNetworkThread, bool onlyActive ) const -{ - if (systemIdentifier.rakNetGuid!=UNASSIGNED_RAKNET_GUID) - return GetRemoteSystemFromGUID(systemIdentifier.rakNetGuid, onlyActive); - else - return GetRemoteSystemFromSystemAddress(systemIdentifier.systemAddress, calledFromNetworkThread, onlyActive); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RakPeer::RemoteSystemStruct *RakPeer::GetRemoteSystemFromSystemAddress( const SystemAddress systemAddress, bool calledFromNetworkThread, bool onlyActive ) const -{ - unsigned i; - - if ( systemAddress == UNASSIGNED_SYSTEM_ADDRESS ) - return 0; - - if (calledFromNetworkThread) - { - unsigned int index = GetRemoteSystemIndex(systemAddress); - if (index!=(unsigned int) -1) - { - if (onlyActive==false || remoteSystemList[ index ].isActive==true ) - { - RakAssert(remoteSystemList[index].systemAddress==systemAddress); - return remoteSystemList + index; - } - } - } - else - { - int deadConnectionIndex=-1; - - // Active connections take priority. But if there are no active connections, return the first systemAddress match found - for ( i = 0; i < maximumNumberOfPeers; i++ ) - { - if (remoteSystemList[ i ].systemAddress == systemAddress) - { - if ( remoteSystemList[ i ].isActive ) - return remoteSystemList + i; - else if (deadConnectionIndex==-1) - deadConnectionIndex=i; - } - } - - if (deadConnectionIndex!=-1 && onlyActive==false) - return remoteSystemList + deadConnectionIndex; - } - - return 0; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RakPeer::RemoteSystemStruct *RakPeer::GetRemoteSystemFromGUID( const RakNetGUID guid, bool onlyActive ) const -{ - if (guid==UNASSIGNED_RAKNET_GUID) - return 0; - - unsigned i; - for ( i = 0; i < maximumNumberOfPeers; i++ ) - { - if (remoteSystemList[ i ].guid == guid && (onlyActive==false || remoteSystemList[ i ].isActive)) - { - return remoteSystemList + i; - } - } - return 0; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::ParseConnectionRequestPacket( RakPeer::RemoteSystemStruct *remoteSystem, const SystemAddress &systemAddress, const char *data, int byteSize ) -{ - RakNet::BitStream bs((unsigned char*) data,byteSize,false); - bs.IgnoreBytes(sizeof(MessageID)); - RakNetGUID guid; - bs.Read(guid); - RakNet::Time incomingTimestamp; - bs.Read(incomingTimestamp); - unsigned char doSecurity; - bs.Read(doSecurity); - -#if LIBCAT_SECURITY==1 - unsigned char doClientKey; - if (_using_security) - { - // Ignore message on bad state - if (doSecurity != 1 || !remoteSystem->reliabilityLayer.GetAuthenticatedEncryption()) - return; - - // Validate client proof of key - unsigned char proof[cat::EasyHandshake::PROOF_BYTES]; - bs.ReadAlignedBytes(proof, sizeof(proof)); - if (!remoteSystem->reliabilityLayer.GetAuthenticatedEncryption()->ValidateProof(proof, sizeof(proof))) - { - remoteSystem->connectMode = RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY; - return; - } - - CAT_OBJCLR(remoteSystem->client_public_key); - - bs.Read(doClientKey); - - // Check if client wants to prove identity - if (doClientKey == 1) - { - // Read identity proof - unsigned char ident[cat::EasyHandshake::IDENTITY_BYTES]; - bs.ReadAlignedBytes(ident, sizeof(ident)); - - // If we are listening to these proofs, - if (_require_client_public_key) - { - // Validate client identity - if (!_server_handshake->VerifyInitiatorIdentity(remoteSystem->answer, ident, remoteSystem->client_public_key)) - { - RakNet::BitStream bitStream; - bitStream.Write((MessageID)ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY); // Report an error since the client is not providing an identity when it is necessary to connect - bitStream.Write((unsigned char)2); // Indicate client identity is invalid - SendImmediate((char*) bitStream.GetData(), bitStream.GetNumberOfBytesUsed(), IMMEDIATE_PRIORITY, RELIABLE, 0, systemAddress, false, false, RakNet::GetTimeUS(), 0); - remoteSystem->connectMode = RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY; - return; - } - } - - // Otherwise ignore the client public key - } - else - { - // If no client key was provided but it is required, - if (_require_client_public_key) - { - RakNet::BitStream bitStream; - bitStream.Write((MessageID)ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY); // Report an error since the client is not providing an identity when it is necessary to connect - bitStream.Write((unsigned char)1); // Indicate client identity is missing - SendImmediate((char*) bitStream.GetData(), bitStream.GetNumberOfBytesUsed(), IMMEDIATE_PRIORITY, RELIABLE, 0, systemAddress, false, false, RakNet::GetTimeUS(), 0); - remoteSystem->connectMode = RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY; - return; - } - } - } -#endif // LIBCAT_SECURITY - - unsigned char *password = bs.GetData()+BITS_TO_BYTES(bs.GetReadOffset()); - int passwordLength = byteSize - BITS_TO_BYTES(bs.GetReadOffset()); - if ( incomingPasswordLength != passwordLength || - memcmp( password, incomingPassword, incomingPasswordLength ) != 0 ) - { - CAT_AUDIT_PRINTF("AUDIT: Invalid password\n"); - // This one we only send once since we don't care if it arrives. - RakNet::BitStream bitStream; - bitStream.Write((MessageID)ID_INVALID_PASSWORD); - bitStream.Write(GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); - SendImmediate((char*) bitStream.GetData(), bitStream.GetNumberOfBytesUsed(), IMMEDIATE_PRIORITY, RELIABLE, 0, systemAddress, false, false, RakNet::GetTimeUS(), 0); - remoteSystem->connectMode=RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY; - return; - } - - // OK - remoteSystem->connectMode=RemoteSystemStruct::HANDLING_CONNECTION_REQUEST; - - OnConnectionRequest( remoteSystem, incomingTimestamp ); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::OnConnectionRequest( RakPeer::RemoteSystemStruct *remoteSystem, RakNet::Time incomingTimestamp ) -{ - RakNet::BitStream bitStream; - bitStream.Write((MessageID)ID_CONNECTION_REQUEST_ACCEPTED); - bitStream.Write(remoteSystem->systemAddress); - SystemIndex systemIndex = (SystemIndex) GetIndexFromSystemAddress( remoteSystem->systemAddress, true ); - RakAssert(systemIndex!=65535); - bitStream.Write(systemIndex); - for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++) - bitStream.Write(ipList[i]); - bitStream.Write(incomingTimestamp); - bitStream.Write(RakNet::GetTime()); - - SendImmediate((char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, RELIABLE_ORDERED, 0, remoteSystem->systemAddress, false, false, RakNet::GetTimeUS(), 0); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::NotifyAndFlagForShutdown( const SystemAddress systemAddress, bool performImmediate, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority ) -{ - RakNet::BitStream temp( sizeof(unsigned char) ); - temp.Write( (MessageID)ID_DISCONNECTION_NOTIFICATION ); - if (performImmediate) - { - SendImmediate((char*)temp.GetData(), temp.GetNumberOfBitsUsed(), disconnectionNotificationPriority, RELIABLE_ORDERED, orderingChannel, systemAddress, false, false, RakNet::GetTimeUS(), 0); - RemoteSystemStruct *rss=GetRemoteSystemFromSystemAddress(systemAddress, true, true); - rss->connectMode=RemoteSystemStruct::DISCONNECT_ASAP; - } - else - { - SendBuffered((const char*)temp.GetData(), temp.GetNumberOfBitsUsed(), disconnectionNotificationPriority, RELIABLE_ORDERED, orderingChannel, systemAddress, false, RemoteSystemStruct::DISCONNECT_ASAP, 0); - } -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -unsigned int RakPeer::GetNumberOfRemoteInitiatedConnections( void ) const -{ - if ( remoteSystemList == 0 || endThreads == true ) - return 0; - - unsigned int numberOfIncomingConnections; - numberOfIncomingConnections = 0; - unsigned int i; - for (i=0; i < activeSystemListSize; i++) - { - if ((activeSystemList[i])->isActive && - (activeSystemList[i])->connectMode==RakPeer::RemoteSystemStruct::CONNECTED && - (activeSystemList[i])->weInitiatedTheConnection==false - ) - { - numberOfIncomingConnections++; - } - } - return numberOfIncomingConnections; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RakPeer::RemoteSystemStruct * RakPeer::AssignSystemAddressToRemoteSystemList( const SystemAddress systemAddress, RemoteSystemStruct::ConnectMode connectionMode, RakNetSocket2* incomingRakNetSocket, bool *thisIPConnectedRecently, SystemAddress bindingAddress, int incomingMTU, RakNetGUID guid, bool useSecurity ) -{ - RemoteSystemStruct * remoteSystem; - unsigned i,j,assignedIndex; - RakNet::TimeMS time = RakNet::GetTimeMS(); -#ifdef _DEBUG - RakAssert(systemAddress!=UNASSIGNED_SYSTEM_ADDRESS); -#endif - - if (limitConnectionFrequencyFromTheSameIP) - { - if (IsLoopbackAddress(systemAddress,false)==false) - { - for ( i = 0; i < maximumNumberOfPeers; i++ ) - { - if ( remoteSystemList[ i ].isActive==true && - remoteSystemList[ i ].systemAddress.EqualsExcludingPort(systemAddress) && - time >= remoteSystemList[ i ].connectionTime && - time - remoteSystemList[ i ].connectionTime < 100 - ) - { - // 4/13/09 Attackers can flood ID_OPEN_CONNECTION_REQUEST and use up all available connection slots - // Ignore connection attempts if this IP address connected within the last 100 milliseconds - *thisIPConnectedRecently=true; - ValidateRemoteSystemLookup(); - return 0; - } - } - } - } - - // Don't use a different port than what we received on - bindingAddress.CopyPort(incomingRakNetSocket->GetBoundAddress()); - - *thisIPConnectedRecently=false; - for ( assignedIndex = 0; assignedIndex < maximumNumberOfPeers; assignedIndex++ ) - { - if ( remoteSystemList[ assignedIndex ].isActive==false ) - { - // printf("--- Address %s has become active\n", systemAddress.ToString()); - - remoteSystem=remoteSystemList+assignedIndex; - ReferenceRemoteSystem(systemAddress, assignedIndex); - remoteSystem->MTUSize=defaultMTUSize; - remoteSystem->guid=guid; - remoteSystem->isActive = true; // This one line causes future incoming packets to go through the reliability layer - // Reserve this reliability layer for ourselves. - if (incomingMTU > remoteSystem->MTUSize) - remoteSystem->MTUSize=incomingMTU; - RakAssert(remoteSystem->MTUSize <= MAXIMUM_MTU_SIZE); - remoteSystem->reliabilityLayer.Reset(true, remoteSystem->MTUSize, useSecurity); - remoteSystem->reliabilityLayer.SetSplitMessageProgressInterval(splitMessageProgressInterval); - remoteSystem->reliabilityLayer.SetUnreliableTimeout(unreliableTimeout); - remoteSystem->reliabilityLayer.SetTimeoutTime(defaultTimeoutTime); - AddToActiveSystemList(assignedIndex); - if (incomingRakNetSocket->GetBoundAddress()==bindingAddress) - { - remoteSystem->rakNetSocket=incomingRakNetSocket; - } - else - { - char str[256]; - bindingAddress.ToString(true,str); - // See if this is an internal IP address. - // If so, force binding on it so we reply on the same IP address as they sent to. - unsigned int ipListIndex, foundIndex=(unsigned int)-1; - - for (ipListIndex=0; ipListIndex < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ipListIndex++) - { - if (ipList[ipListIndex]==UNASSIGNED_SYSTEM_ADDRESS) - break; - - if (bindingAddress.EqualsExcludingPort(ipList[ipListIndex])) - { - foundIndex=ipListIndex; - break; - } - } - - // 06/26/09 Unconfirmed report that Vista firewall blocks the reply if we force a binding - // For now use the incoming socket only - // Originally this code was to force a machine with multiple IP addresses to reply back on the IP - // that the datagram came in on - if (1 || foundIndex==(unsigned int)-1) - { - // Must not be an internal LAN address. Just use whatever socket it came in on - remoteSystem->rakNetSocket=incomingRakNetSocket; - } - else - { - /* - // Force binding - unsigned int socketListIndex; - for (socketListIndex=0; socketListIndex < socketList.Size(); socketListIndex++) - { - if (socketList[socketListIndex]->GetBoundAddress()==bindingAddress) - { - // Force binding with existing socket - remoteSystem->rakNetSocket=socketList[socketListIndex]; - break; - } - } - - if (socketListIndex==socketList.Size()) - { - char ipListFoundIndexStr[128]; - ipList[foundIndex].ToString(false,str); - - // Force binding with new socket - RakNetSocket* rns(RakNet::OP_NEW(_FILE_AND_LINE_)); - if (incomingRakNetSocket->GetRemotePortRakNetWasStartedOn()==0) - rns = SocketLayer::CreateBoundSocket( this, bindingAddress.GetPort(), incomingRakNetSocket->GetBlockingSocket(), ipListFoundIndexStr, 0, incomingRakNetSocket->GetExtraSocketOptions(), incomingRakNetSocket->GetSocketFamily(), incomingRakNetSocket->GetChromeInstance() ); - else - rns = SocketLayer::CreateBoundSocket_PS3Lobby( bindingAddress.GetPort(), incomingRakNetSocket->GetBlockingSocket(), ipListFoundIndexStr, incomingRakNetSocket->GetSocketFamily() ); - - - if (rns==0) - { - // Can't bind. Just use whatever socket it came in on - remoteSystem->rakNetSocket=incomingRakNetSocket; - } - else - { - rns->GetBoundAddress()=bindingAddress; - rns->SetUserConnectionSocketIndex((unsigned int)-1); - socketList.Push(rns, _FILE_AND_LINE_ ); - remoteSystem->rakNetSocket=rns; - - -#ifdef _WIN32 - int highPriority=THREAD_PRIORITY_ABOVE_NORMAL; -#else - int highPriority=-10; -#endif - - highPriority=0; - - - } - } - - */ - } - } - - for ( j = 0; j < (unsigned) PING_TIMES_ARRAY_SIZE; j++ ) - { - remoteSystem->pingAndClockDifferential[ j ].pingTime = 65535; - remoteSystem->pingAndClockDifferential[ j ].clockDifferential = 0; - } - - remoteSystem->connectMode=connectionMode; - remoteSystem->pingAndClockDifferentialWriteIndex = 0; - remoteSystem->lowestPing = 65535; - remoteSystem->nextPingTime = 0; // Ping immediately - remoteSystem->weInitiatedTheConnection = false; - remoteSystem->connectionTime = time; - remoteSystem->myExternalSystemAddress = UNASSIGNED_SYSTEM_ADDRESS; - remoteSystem->lastReliableSend=time; - -#ifdef _DEBUG - int indexLoopupCheck=GetIndexFromSystemAddress( systemAddress, true ); - if ((int) indexLoopupCheck!=(int) assignedIndex) - { - RakAssert((int) indexLoopupCheck==(int) assignedIndex); - } -#endif - - return remoteSystem; - } - } - - return 0; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Adjust the first four bytes (treated as unsigned int) of the pointer -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::ShiftIncomingTimestamp( unsigned char *data, const SystemAddress &systemAddress ) const -{ -#ifdef _DEBUG - RakAssert( IsActive() ); - RakAssert( data ); -#endif - - RakNet::BitStream timeBS( data, sizeof(RakNet::Time), false); - RakNet::Time encodedTimestamp; - timeBS.Read(encodedTimestamp); - - encodedTimestamp = encodedTimestamp - GetBestClockDifferential( systemAddress ); - timeBS.SetWriteOffset(0); - timeBS.Write(encodedTimestamp); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// Thanks to Chris Taylor (cat02e@fsu.edu) for the improved timestamping algorithm -RakNet::Time RakPeer::GetBestClockDifferential( const SystemAddress systemAddress ) const -{ - RemoteSystemStruct *remoteSystem = GetRemoteSystemFromSystemAddress( systemAddress, true, true ); - - if ( remoteSystem == 0 ) - return 0; - - return GetClockDifferentialInt(remoteSystem); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -unsigned int RakPeer::RemoteSystemLookupHashIndex(const SystemAddress &sa) const -{ - return SystemAddress::ToInteger(sa) % ((unsigned int) maximumNumberOfPeers * REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::ReferenceRemoteSystem(const SystemAddress &sa, unsigned int remoteSystemListIndex) -{ -// #ifdef _DEBUG -// for ( int remoteSystemIndex = 0; remoteSystemIndex < maximumNumberOfPeers; ++remoteSystemIndex ) -// { -// if (remoteSystemList[remoteSystemIndex].isActive ) -// { -// unsigned int hashIndex = GetRemoteSystemIndex(remoteSystemList[remoteSystemIndex].systemAddress); -// RakAssert(hashIndex==remoteSystemIndex); -// } -// } -// #endif - - - SystemAddress oldAddress = remoteSystemList[remoteSystemListIndex].systemAddress; - if (oldAddress!=UNASSIGNED_SYSTEM_ADDRESS) - { - // The system might be active if rerouting -// RakAssert(remoteSystemList[remoteSystemListIndex].isActive==false); - - // Remove the reference if the reference is pointing to this inactive system - if (GetRemoteSystem(oldAddress)==&remoteSystemList[remoteSystemListIndex]) - DereferenceRemoteSystem(oldAddress); - } - DereferenceRemoteSystem(sa); - -// #ifdef _DEBUG -// for ( int remoteSystemIndex = 0; remoteSystemIndex < maximumNumberOfPeers; ++remoteSystemIndex ) -// { -// if (remoteSystemList[remoteSystemIndex].isActive ) -// { -// unsigned int hashIndex = GetRemoteSystemIndex(remoteSystemList[remoteSystemIndex].systemAddress); -// if (hashIndex!=remoteSystemIndex) -// { -// RakAssert(hashIndex==remoteSystemIndex); -// } -// } -// } -// #endif - - - remoteSystemList[remoteSystemListIndex].systemAddress=sa; - - unsigned int hashIndex = RemoteSystemLookupHashIndex(sa); - RemoteSystemIndex *rsi; - rsi = remoteSystemIndexPool.Allocate(_FILE_AND_LINE_); - if (remoteSystemLookup[hashIndex]==0) - { - rsi->next=0; - rsi->index=remoteSystemListIndex; - remoteSystemLookup[hashIndex]=rsi; - } - else - { - RemoteSystemIndex *cur = remoteSystemLookup[hashIndex]; - while (cur->next!=0) - { - cur=cur->next; - } - - rsi = remoteSystemIndexPool.Allocate(_FILE_AND_LINE_); - rsi->next=0; - rsi->index=remoteSystemListIndex; - cur->next=rsi; - } - -// #ifdef _DEBUG -// for ( int remoteSystemIndex = 0; remoteSystemIndex < maximumNumberOfPeers; ++remoteSystemIndex ) -// { -// if (remoteSystemList[remoteSystemIndex].isActive ) -// { -// unsigned int hashIndex = GetRemoteSystemIndex(remoteSystemList[remoteSystemIndex].systemAddress); -// RakAssert(hashIndex==remoteSystemIndex); -// } -// } -// #endif - - - RakAssert(GetRemoteSystemIndex(sa)==remoteSystemListIndex); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::DereferenceRemoteSystem(const SystemAddress &sa) -{ - unsigned int hashIndex = RemoteSystemLookupHashIndex(sa); - RemoteSystemIndex *cur = remoteSystemLookup[hashIndex]; - RemoteSystemIndex *last = 0; - while (cur!=0) - { - if (remoteSystemList[cur->index].systemAddress==sa) - { - if (last==0) - { - remoteSystemLookup[hashIndex]=cur->next; - } - else - { - last->next=cur->next; - } - remoteSystemIndexPool.Release(cur,_FILE_AND_LINE_); - break; - } - last=cur; - cur=cur->next; - } -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -unsigned int RakPeer::GetRemoteSystemIndex(const SystemAddress &sa) const -{ - unsigned int hashIndex = RemoteSystemLookupHashIndex(sa); - RemoteSystemIndex *cur = remoteSystemLookup[hashIndex]; - while (cur!=0) - { - if (remoteSystemList[cur->index].systemAddress==sa) - return cur->index; - cur=cur->next; - } - return (unsigned int) -1; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RakPeer::RemoteSystemStruct* RakPeer::GetRemoteSystem(const SystemAddress &sa) const -{ - unsigned int remoteSystemIndex = GetRemoteSystemIndex(sa); - if (remoteSystemIndex==(unsigned int)-1) - return 0; - return remoteSystemList + remoteSystemIndex; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::ClearRemoteSystemLookup(void) -{ - remoteSystemIndexPool.Clear(_FILE_AND_LINE_); - RakNet::OP_DELETE_ARRAY(remoteSystemLookup,_FILE_AND_LINE_); - remoteSystemLookup=0; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::AddToActiveSystemList(unsigned int remoteSystemListIndex) -{ - activeSystemList[activeSystemListSize++]=remoteSystemList+remoteSystemListIndex; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::RemoveFromActiveSystemList(const SystemAddress &sa) -{ - unsigned int i; - for (i=0; i < activeSystemListSize; i++) - { - RemoteSystemStruct *rss=activeSystemList[i]; - if (rss->systemAddress==sa) - { - activeSystemList[i]=activeSystemList[activeSystemListSize-1]; - activeSystemListSize--; - return; - } - } - RakAssert("activeSystemList invalid, entry not found in RemoveFromActiveSystemList. Ensure that AddToActiveSystemList and RemoveFromActiveSystemList are called by the same thread." && 0); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -/* -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -unsigned int RakPeer::LookupIndexUsingHashIndex(const SystemAddress &sa) const -{ - unsigned int scanCount=0; - unsigned int index = RemoteSystemLookupHashIndex(sa); - if (remoteSystemLookup[index].index==(unsigned int)-1) - return (unsigned int) -1; - while (remoteSystemList[remoteSystemLookup[index].index].systemAddress!=sa) - { - if (++index==(unsigned int) maximumNumberOfPeers*REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE) - index=0; - if (++scanCount>(unsigned int) maximumNumberOfPeers*REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE) - return (unsigned int) -1; - if (remoteSystemLookup[index].index==-1) - return (unsigned int) -1; - } - return index; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -unsigned int RakPeer::RemoteSystemListIndexUsingHashIndex(const SystemAddress &sa) const -{ - unsigned int index = LookupIndexUsingHashIndex(sa); - if (index!=(unsigned int) -1) - { - return remoteSystemLookup[index].index; - } - return (unsigned int) -1; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -unsigned int RakPeer::FirstFreeRemoteSystemLookupIndex(const SystemAddress &sa) const -{ -// unsigned int collisionCount=0; - unsigned int index = RemoteSystemLookupHashIndex(sa); - while (remoteSystemLookup[index].index!=(unsigned int)-1) - { - if (++index==(unsigned int) maximumNumberOfPeers*REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE) - index=0; -// collisionCount++; - } -// printf("%i collisions. Using index %i\n", collisionCount, index); - return index; -} -*/ -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -bool RakPeer::IsLoopbackAddress(const AddressOrGUID &systemIdentifier, bool matchPort) const -{ - if (systemIdentifier.rakNetGuid!=UNASSIGNED_RAKNET_GUID) - return systemIdentifier.rakNetGuid==myGuid; - - for (int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS && ipList[i]!=UNASSIGNED_SYSTEM_ADDRESS; i++) - { - if (matchPort) - { - if (ipList[i]==systemIdentifier.systemAddress) - return true; - } - else - { - if (ipList[i].EqualsExcludingPort(systemIdentifier.systemAddress)) - return true; - } - } - - return (matchPort==true && systemIdentifier.systemAddress==firstExternalID) || - (matchPort==false && systemIdentifier.systemAddress.EqualsExcludingPort(firstExternalID)); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -SystemAddress RakPeer::GetLoopbackAddress(void) const -{ - - return ipList[0]; - - - -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -bool RakPeer::AllowIncomingConnections(void) const -{ - return GetNumberOfRemoteInitiatedConnections() < GetMaximumIncomingConnections(); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line) -{ - bufferedPacketsFreePoolMutex.Lock(); - bufferedPacketsFreePool.Push(s, file, line); - bufferedPacketsFreePoolMutex.Unlock(); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RNS2RecvStruct *RakPeer::AllocRNS2RecvStruct(const char *file, unsigned int line) -{ - bufferedPacketsFreePoolMutex.Lock(); - if (bufferedPacketsFreePool.Size()>0) - { - RNS2RecvStruct *s = bufferedPacketsFreePool.Pop(); - bufferedPacketsFreePoolMutex.Unlock(); - return s; - } - else - { - bufferedPacketsFreePoolMutex.Unlock(); - return RakNet::OP_NEW(file,line); - } -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::ClearBufferedPackets(void) -{ - bufferedPacketsFreePoolMutex.Lock(); - while (bufferedPacketsFreePool.Size()>0) - RakNet::OP_DELETE(bufferedPacketsFreePool.Pop(), _FILE_AND_LINE_); - bufferedPacketsFreePoolMutex.Unlock(); - - bufferedPacketsQueueMutex.Lock(); - while (bufferedPacketsQueue.Size()>0) - RakNet::OP_DELETE(bufferedPacketsQueue.Pop(), _FILE_AND_LINE_); - bufferedPacketsQueueMutex.Unlock(); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::SetupBufferedPackets(void) -{ -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::PushBufferedPacket(RNS2RecvStruct * p) -{ - bufferedPacketsQueueMutex.Lock(); - bufferedPacketsQueue.Push(p, _FILE_AND_LINE_); - bufferedPacketsQueueMutex.Unlock(); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RNS2RecvStruct *RakPeer::PopBufferedPacket(void) -{ - bufferedPacketsQueueMutex.Lock(); - if (bufferedPacketsQueue.Size()>0) - { - RNS2RecvStruct *s = bufferedPacketsQueue.Pop(); - bufferedPacketsQueueMutex.Unlock(); - return s; - } - bufferedPacketsQueueMutex.Unlock(); - return 0; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::PingInternal( const SystemAddress target, bool performImmediate, PacketReliability reliability ) -{ - if ( IsActive() == false ) - return ; - - RakNet::BitStream bitStream(sizeof(unsigned char)+sizeof(RakNet::Time)); - bitStream.Write((MessageID)ID_CONNECTED_PING); - bitStream.Write(RakNet::GetTime()); - if (performImmediate) - SendImmediate( (char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, reliability, 0, target, false, false, RakNet::GetTimeUS(), 0 ); - else - Send( &bitStream, IMMEDIATE_PRIORITY, reliability, 0, target, false ); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::CloseConnectionInternal( const AddressOrGUID& systemIdentifier, bool sendDisconnectionNotification, bool performImmediate, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority ) -{ -#ifdef _DEBUG - RakAssert(orderingChannel < 32); -#endif - - if (systemIdentifier.IsUndefined()) - return; - - if ( remoteSystemList == 0 || endThreads == true ) - return; - - SystemAddress target; - if (systemIdentifier.systemAddress!=UNASSIGNED_SYSTEM_ADDRESS) - { - target=systemIdentifier.systemAddress; - } - else - { - target=GetSystemAddressFromGuid(systemIdentifier.rakNetGuid); - } - - if (target!=UNASSIGNED_SYSTEM_ADDRESS && performImmediate) - target.FixForIPVersion(socketList[0]->GetBoundAddress()); - - if (sendDisconnectionNotification) - { - NotifyAndFlagForShutdown(target, performImmediate, orderingChannel, disconnectionNotificationPriority); - } - else - { - if (performImmediate) - { - unsigned int index = GetRemoteSystemIndex(target); - if (index!=(unsigned int) -1) - { - if ( remoteSystemList[index].isActive ) - { - RemoveFromActiveSystemList(target); - - // Found the index to stop - // printf("--- Address %s has become inactive\n", remoteSystemList[index].systemAddress.ToString()); - remoteSystemList[index].isActive = false; - - remoteSystemList[index].guid=UNASSIGNED_RAKNET_GUID; - - // Reserve this reliability layer for ourselves - //remoteSystemList[ remoteSystemLookup[index].index ].systemAddress = UNASSIGNED_SYSTEM_ADDRESS; - - // Clear any remaining messages - RakAssert(remoteSystemList[index].MTUSize <= MAXIMUM_MTU_SIZE); - remoteSystemList[index].reliabilityLayer.Reset(false, remoteSystemList[index].MTUSize, false); - - // Not using this socket - remoteSystemList[index].rakNetSocket = 0; - } - } - } - else - { - BufferedCommandStruct *bcs; - bcs=bufferedCommands.Allocate( _FILE_AND_LINE_ ); - bcs->command=BufferedCommandStruct::BCS_CLOSE_CONNECTION; - bcs->systemIdentifier=target; - bcs->data=0; - bcs->orderingChannel=orderingChannel; - bcs->priority=disconnectionNotificationPriority; - bufferedCommands.Push(bcs); - } - } -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::SendBuffered( const char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt ) -{ - BufferedCommandStruct *bcs; - - bcs=bufferedCommands.Allocate( _FILE_AND_LINE_ ); - bcs->data = (char*) rakMalloc_Ex( (size_t) BITS_TO_BYTES(numberOfBitsToSend), _FILE_AND_LINE_ ); // Making a copy doesn't lose efficiency because I tell the reliability layer to use this allocation for its own copy - if (bcs->data==0) - { - notifyOutOfMemory(_FILE_AND_LINE_); - bufferedCommands.Deallocate(bcs, _FILE_AND_LINE_); - return; - } - - RakAssert( !( reliability >= NUMBER_OF_RELIABILITIES || reliability < 0 ) ); - RakAssert( !( priority > NUMBER_OF_PRIORITIES || priority < 0 ) ); - RakAssert( !( orderingChannel >= NUMBER_OF_ORDERED_STREAMS ) ); - - memcpy(bcs->data, data, (size_t) BITS_TO_BYTES(numberOfBitsToSend)); - bcs->numberOfBitsToSend=numberOfBitsToSend; - bcs->priority=priority; - bcs->reliability=reliability; - bcs->orderingChannel=orderingChannel; - bcs->systemIdentifier=systemIdentifier; - bcs->broadcast=broadcast; - bcs->connectionMode=connectionMode; - bcs->receipt=receipt; - bcs->command=BufferedCommandStruct::BCS_SEND; - bufferedCommands.Push(bcs); - - if (priority==IMMEDIATE_PRIORITY) - { - // Forces pending sends to go out now, rather than waiting to the next update interval - quitAndDataEvents.SetEvent(); - } -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::SendBufferedList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt ) -{ - BufferedCommandStruct *bcs; - unsigned int totalLength=0; - unsigned int lengthOffset; - int i; - for (i=0; i < numParameters; i++) - { - if (lengths[i]>0) - totalLength+=lengths[i]; - } - if (totalLength==0) - return; - - char *dataAggregate; - dataAggregate = (char*) rakMalloc_Ex( (size_t) totalLength, _FILE_AND_LINE_ ); // Making a copy doesn't lose efficiency because I tell the reliability layer to use this allocation for its own copy - if (dataAggregate==0) - { - notifyOutOfMemory(_FILE_AND_LINE_); - return; - } - for (i=0, lengthOffset=0; i < numParameters; i++) - { - if (lengths[i]>0) - { - memcpy(dataAggregate+lengthOffset, data[i], lengths[i]); - lengthOffset+=lengths[i]; - } - } - - if (broadcast==false && IsLoopbackAddress(systemIdentifier,true)) - { - SendLoopback(dataAggregate,totalLength); - rakFree_Ex(dataAggregate,_FILE_AND_LINE_); - return; - } - - RakAssert( !( reliability >= NUMBER_OF_RELIABILITIES || reliability < 0 ) ); - RakAssert( !( priority > NUMBER_OF_PRIORITIES || priority < 0 ) ); - RakAssert( !( orderingChannel >= NUMBER_OF_ORDERED_STREAMS ) ); - - bcs=bufferedCommands.Allocate( _FILE_AND_LINE_ ); - bcs->data = dataAggregate; - bcs->numberOfBitsToSend=BYTES_TO_BITS(totalLength); - bcs->priority=priority; - bcs->reliability=reliability; - bcs->orderingChannel=orderingChannel; - bcs->systemIdentifier=systemIdentifier; - bcs->broadcast=broadcast; - bcs->connectionMode=connectionMode; - bcs->receipt=receipt; - bcs->command=BufferedCommandStruct::BCS_SEND; - bufferedCommands.Push(bcs); - - if (priority==IMMEDIATE_PRIORITY) - { - // Forces pending sends to go out now, rather than waiting to the next update interval - quitAndDataEvents.SetEvent(); - } -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -bool RakPeer::SendImmediate( char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, bool useCallerDataAllocation, RakNet::TimeUS currentTime, uint32_t receipt ) -{ - unsigned *sendList; - unsigned sendListSize; - bool callerDataAllocationUsed; - unsigned int remoteSystemIndex, sendListIndex; // Iterates into the list of remote systems -// unsigned numberOfBytesUsed = (unsigned) BITS_TO_BYTES(numberOfBitsToSend); - callerDataAllocationUsed=false; - - sendListSize=0; - - if (systemIdentifier.systemAddress!=UNASSIGNED_SYSTEM_ADDRESS) - remoteSystemIndex=GetIndexFromSystemAddress( systemIdentifier.systemAddress, true ); - else if (systemIdentifier.rakNetGuid!=UNASSIGNED_RAKNET_GUID) - remoteSystemIndex=GetSystemIndexFromGuid(systemIdentifier.rakNetGuid); - else - remoteSystemIndex=(unsigned int) -1; - - // 03/06/06 - If broadcast is false, use the optimized version of GetIndexFromSystemAddress - if (broadcast==false) - { - if (remoteSystemIndex==(unsigned int) -1) - { -#ifdef _DEBUG -// int debugIndex = GetRemoteSystemIndex(systemIdentifier.systemAddress); -#endif - return false; - } - - #if USE_ALLOCA==1 - sendList=(unsigned *)alloca(sizeof(unsigned)); - #else - sendList = (unsigned *) rakMalloc_Ex(sizeof(unsigned), _FILE_AND_LINE_); - #endif - - if (remoteSystemList[remoteSystemIndex].isActive && - remoteSystemList[remoteSystemIndex].connectMode!=RemoteSystemStruct::DISCONNECT_ASAP && - remoteSystemList[remoteSystemIndex].connectMode!=RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY && - remoteSystemList[remoteSystemIndex].connectMode!=RemoteSystemStruct::DISCONNECT_ON_NO_ACK) - { - sendList[0]=remoteSystemIndex; - sendListSize=1; - } - } - else - { - #if USE_ALLOCA==1 - sendList=(unsigned *)alloca(sizeof(unsigned)*maximumNumberOfPeers); - #else - sendList = (unsigned *) rakMalloc_Ex(sizeof(unsigned)*maximumNumberOfPeers, _FILE_AND_LINE_); - #endif - - // remoteSystemList in network thread - unsigned int idx; - for ( idx = 0; idx < maximumNumberOfPeers; idx++ ) - { - if (remoteSystemIndex!=(unsigned int) -1 && idx==remoteSystemIndex) - continue; - - if ( remoteSystemList[ idx ].isActive && remoteSystemList[ idx ].systemAddress != UNASSIGNED_SYSTEM_ADDRESS ) - sendList[sendListSize++]=idx; - } - } - - if (sendListSize==0) - { - #if !defined(USE_ALLOCA) - rakFree_Ex(sendList, _FILE_AND_LINE_ ); - #endif - - return false; - } - - for (sendListIndex=0; sendListIndex < sendListSize; sendListIndex++) - { - // Send may split the packet and thus deallocate data. Don't assume data is valid if we use the callerAllocationData - bool useData = useCallerDataAllocation && callerDataAllocationUsed==false && sendListIndex+1==sendListSize; - remoteSystemList[sendList[sendListIndex]].reliabilityLayer.Send( data, numberOfBitsToSend, priority, reliability, orderingChannel, useData==false, remoteSystemList[sendList[sendListIndex]].MTUSize, currentTime, receipt ); - if (useData) - callerDataAllocationUsed=true; - - if (reliability==RELIABLE || - reliability==RELIABLE_ORDERED || - reliability==RELIABLE_SEQUENCED || - reliability==RELIABLE_WITH_ACK_RECEIPT || - reliability==RELIABLE_ORDERED_WITH_ACK_RECEIPT -// || -// reliability==RELIABLE_SEQUENCED_WITH_ACK_RECEIPT - ) - remoteSystemList[sendList[sendListIndex]].lastReliableSend=(RakNet::TimeMS)(currentTime/(RakNet::TimeUS)1000); - } - -#if !defined(USE_ALLOCA) - rakFree_Ex(sendList, _FILE_AND_LINE_ ); -#endif - - // Return value only meaningful if true was passed for useCallerDataAllocation. Means the reliability layer used that data copy, so the caller should not deallocate it - return callerDataAllocationUsed; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::ResetSendReceipt(void) -{ - sendReceiptSerialMutex.Lock(); - sendReceiptSerial=1; - sendReceiptSerialMutex.Unlock(); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::OnConnectedPong(RakNet::Time sendPingTime, RakNet::Time sendPongTime, RemoteSystemStruct *remoteSystem) -{ - RakNet::Time ping; -// RakNet::TimeMS lastPing; - RakNet::Time time = RakNet::GetTime(); // Update the time value to be accurate - if (time > sendPingTime) - ping = time - sendPingTime; - else - ping=0; - -// lastPing = remoteSystem->pingAndClockDifferential[ remoteSystem->pingAndClockDifferentialWriteIndex ].pingTime; - - remoteSystem->pingAndClockDifferential[ remoteSystem->pingAndClockDifferentialWriteIndex ].pingTime = ( unsigned short ) ping; - // Thanks to Chris Taylor (cat02e@fsu.edu) for the improved timestamping algorithm - // Divide each integer by 2, rather than the sum by 2, to prevent overflow - remoteSystem->pingAndClockDifferential[ remoteSystem->pingAndClockDifferentialWriteIndex ].clockDifferential = sendPongTime - ( time/2 + sendPingTime/2 ); - - if ( remoteSystem->lowestPing == (unsigned short)-1 || remoteSystem->lowestPing > (int) ping ) - remoteSystem->lowestPing = (unsigned short) ping; - - if ( ++( remoteSystem->pingAndClockDifferentialWriteIndex ) == (RakNet::Time) PING_TIMES_ARRAY_SIZE ) - remoteSystem->pingAndClockDifferentialWriteIndex = 0; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::ClearBufferedCommands(void) -{ - BufferedCommandStruct *bcs; - - while ((bcs=bufferedCommands.Pop())!=0) - { - if (bcs->data) - rakFree_Ex(bcs->data, _FILE_AND_LINE_ ); - - bufferedCommands.Deallocate(bcs, _FILE_AND_LINE_); - } - bufferedCommands.Clear(_FILE_AND_LINE_); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::ClearSocketQueryOutput(void) -{ - socketQueryOutput.Clear(_FILE_AND_LINE_); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::ClearRequestedConnectionList(void) -{ - DataStructures::Queue freeQueue; - requestedConnectionQueueMutex.Lock(); - while (requestedConnectionQueue.Size()) - freeQueue.Push(requestedConnectionQueue.Pop(), _FILE_AND_LINE_ ); - requestedConnectionQueueMutex.Unlock(); - unsigned i; - for (i=0; i < freeQueue.Size(); i++) - { -#if LIBCAT_SECURITY==1 - CAT_AUDIT_PRINTF("AUDIT: In ClearRequestedConnectionList(), Deleting freeQueue index %i client_handshake %x\n", i, freeQueue[i]->client_handshake); - RakNet::OP_DELETE(freeQueue[i]->client_handshake,_FILE_AND_LINE_); -#endif - RakNet::OP_DELETE(freeQueue[i], _FILE_AND_LINE_ ); - } -} -inline void RakPeer::AddPacketToProducer(RakNet::Packet *p) -{ - packetReturnMutex.Lock(); - packetReturnQueue.Push(p,_FILE_AND_LINE_); - packetReturnMutex.Unlock(); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -union Buff6AndBuff8 -{ - unsigned char buff6[6]; - uint64_t buff8; -}; -uint64_t RakPeerInterface::Get64BitUniqueRandomNumber(void) -{ - // Mac address is a poor solution because you can't have multiple connections from the same system - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -#if defined(_WIN32) - uint64_t g=RakNet::GetTimeUS(); - - RakNet::TimeUS lastTime, thisTime; - int j; - // Sleep a small random time, then use the last 4 bits as a source of randomness - for (j=0; j < 8; j++) - { - lastTime = RakNet::GetTimeUS(); - RakSleep(1); - RakSleep(0); - thisTime = RakNet::GetTimeUS(); - RakNet::TimeUS diff = thisTime-lastTime; - unsigned int diff4Bits = (unsigned int) (diff & 15); - diff4Bits <<= 32-4; - diff4Bits >>= j*4; - ((char*)&g)[j] ^= diff4Bits; - } - return g; - -#else - struct timeval tv; - gettimeofday(&tv, NULL); - return tv.tv_usec + tv.tv_sec * 1000000; -#endif -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::GenerateGUID(void) -{ - myGuid.g=Get64BitUniqueRandomNumber(); - -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// void RakNet::ProcessPortUnreachable( SystemAddress systemAddress, RakPeer *rakPeer ) -// { -// (void) binaryAddress; -// (void) port; -// (void) rakPeer; -// -// } -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -namespace RakNet { -bool ProcessOfflineNetworkPacket( SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSocket2* rakNetSocket, bool *isOfflineMessage, RakNet::TimeUS timeRead ) -{ - (void) timeRead; - RakPeer::RemoteSystemStruct *remoteSystem; - RakNet::Packet *packet; - unsigned i; - - - char str1[64]; - systemAddress.ToString(false, str1); - if (rakPeer->IsBanned( str1 )) - { - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress); - - RakNet::BitStream bs; - bs.Write((MessageID)ID_CONNECTION_BANNED); - bs.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); - bs.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); - - - RNS2_SendParameters bsp; - bsp.data = (char*) bs.GetData(); - bsp.length = bs.GetNumberOfBytesUsed(); - bsp.systemAddress = systemAddress; - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketSend((char*) bs.GetData(), bs.GetNumberOfBitsUsed(), systemAddress); - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); - -/* - unsigned i; - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketSend((char*) bs.GetData(), bs.GetNumberOfBitsUsed(), systemAddress); - SocketLayer::SendTo( rakNetSocket, (char*) bs.GetData(), bs.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); - */ - - return true; - } - - - - // The reason for all this is that the reliability layer has no way to tell between offline messages that arrived late for a player that is now connected, - // and a regular encoding. So I insert OFFLINE_MESSAGE_DATA_ID into the stream, the encoding of which is essentially impossible to hit by chance - if (length <=2) - { - *isOfflineMessage=true; - } - else if ( - ((unsigned char)data[0] == ID_UNCONNECTED_PING || - (unsigned char)data[0] == ID_UNCONNECTED_PING_OPEN_CONNECTIONS) && - length >= sizeof(unsigned char) + sizeof(RakNet::Time) + sizeof(OFFLINE_MESSAGE_DATA_ID)) - { - *isOfflineMessage=memcmp(data+sizeof(unsigned char) + sizeof(RakNet::Time), OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0; - } - else if ((unsigned char)data[0] == ID_UNCONNECTED_PONG && (size_t) length >= sizeof(unsigned char) + sizeof(RakNet::TimeMS) + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID)) - { - *isOfflineMessage=memcmp(data+sizeof(unsigned char) + sizeof(RakNet::Time) + RakNetGUID::size(), OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0; - } - else if ( - (unsigned char)data[0] == ID_OUT_OF_BAND_INTERNAL && - (size_t) length >= sizeof(MessageID) + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID)) - { - *isOfflineMessage=memcmp(data+sizeof(MessageID) + RakNetGUID::size(), OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0; - } - else if ( - ( - (unsigned char)data[0] == ID_OPEN_CONNECTION_REPLY_1 || - (unsigned char)data[0] == ID_OPEN_CONNECTION_REPLY_2 || - (unsigned char)data[0] == ID_OPEN_CONNECTION_REQUEST_1 || - (unsigned char)data[0] == ID_OPEN_CONNECTION_REQUEST_2 || - (unsigned char)data[0] == ID_CONNECTION_ATTEMPT_FAILED || - (unsigned char)data[0] == ID_NO_FREE_INCOMING_CONNECTIONS || - (unsigned char)data[0] == ID_CONNECTION_BANNED || - (unsigned char)data[0] == ID_ALREADY_CONNECTED || - (unsigned char)data[0] == ID_IP_RECENTLY_CONNECTED) && - (size_t) length >= sizeof(MessageID) + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID)) - { - *isOfflineMessage=memcmp(data+sizeof(MessageID), OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0; - } - else if (((unsigned char)data[0] == ID_INCOMPATIBLE_PROTOCOL_VERSION&& - (size_t) length == sizeof(MessageID)*2 + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID))) - { - *isOfflineMessage=memcmp(data+sizeof(MessageID)*2, OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0; - } - else - { - *isOfflineMessage=false; - } - - if (*isOfflineMessage) - { - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress); - - // These are all messages from unconnected systems. Messages here can be any size, but are never processed from connected systems. - if ( ( (unsigned char) data[ 0 ] == ID_UNCONNECTED_PING_OPEN_CONNECTIONS - || (unsigned char)(data)[0] == ID_UNCONNECTED_PING) && length >= sizeof(unsigned char)+sizeof(RakNet::Time)+sizeof(OFFLINE_MESSAGE_DATA_ID) ) - { - if ( (unsigned char)(data)[0] == ID_UNCONNECTED_PING || - rakPeer->AllowIncomingConnections() ) // Open connections with players - { - RakNet::BitStream inBitStream( (unsigned char *) data, length, false ); - inBitStream.IgnoreBits(8); - RakNet::Time sendPingTime; - inBitStream.Read(sendPingTime); - inBitStream.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID)); - RakNetGUID remoteGuid=UNASSIGNED_RAKNET_GUID; - inBitStream.Read(remoteGuid); - - RakNet::BitStream outBitStream; - outBitStream.Write((MessageID)ID_UNCONNECTED_PONG); // Should be named ID_UNCONNECTED_PONG eventually - outBitStream.Write(sendPingTime); - outBitStream.Write(rakPeer->myGuid); - outBitStream.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); - - rakPeer->rakPeerMutexes[ RakPeer::offlinePingResponse_Mutex ].Lock(); - // They are connected, so append offline ping data - outBitStream.Write( (char*)rakPeer->offlinePingResponse.GetData(), rakPeer->offlinePingResponse.GetNumberOfBytesUsed() ); - rakPeer->rakPeerMutexes[ RakPeer::offlinePingResponse_Mutex ].Unlock(); - - unsigned i; - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*)outBitStream.GetData(), outBitStream.GetNumberOfBytesUsed(), systemAddress); - - RNS2_SendParameters bsp; - bsp.data = (char*) outBitStream.GetData(); - bsp.length = outBitStream.GetNumberOfBytesUsed(); - bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); - - // SocketLayer::SendTo( rakNetSocket, (const char*)outBitStream.GetData(), (unsigned int) outBitStream.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); - - packet=rakPeer->AllocPacket(sizeof(MessageID), _FILE_AND_LINE_); - packet->data[0]=data[0]; - packet->systemAddress = systemAddress; - packet->guid=remoteGuid; - packet->systemAddress.systemIndex = ( SystemIndex ) rakPeer->GetIndexFromSystemAddress( systemAddress, true ); - packet->guid.systemIndex=packet->systemAddress.systemIndex; - rakPeer->AddPacketToProducer(packet); - } - } - // UNCONNECTED MESSAGE Pong with no data. - else if ((unsigned char) data[ 0 ] == ID_UNCONNECTED_PONG && (size_t) length >= sizeof(unsigned char)+sizeof(RakNet::Time)+RakNetGUID::size()+sizeof(OFFLINE_MESSAGE_DATA_ID) && (size_t) length < sizeof(unsigned char)+sizeof(RakNet::Time)+RakNetGUID::size()+sizeof(OFFLINE_MESSAGE_DATA_ID)+MAX_OFFLINE_DATA_LENGTH) - { - packet=rakPeer->AllocPacket((unsigned int) (length-sizeof(OFFLINE_MESSAGE_DATA_ID)-RakNetGUID::size()-sizeof(RakNet::Time)+sizeof(RakNet::TimeMS)), _FILE_AND_LINE_); - RakNet::BitStream bsIn((unsigned char*) data, length, false); - bsIn.IgnoreBytes(sizeof(unsigned char)); - RakNet::Time ping; - bsIn.Read(ping); - bsIn.Read(packet->guid); - - RakNet::BitStream bsOut((unsigned char*) packet->data, packet->length, false); - bsOut.ResetWritePointer(); - bsOut.Write((unsigned char)ID_UNCONNECTED_PONG); - RakNet::TimeMS pingMS=(RakNet::TimeMS)ping; - bsOut.Write(pingMS); - bsOut.WriteAlignedBytes( - (const unsigned char*)data+sizeof(unsigned char)+sizeof(RakNet::Time)+RakNetGUID::size()+sizeof(OFFLINE_MESSAGE_DATA_ID), - length-sizeof(unsigned char)-sizeof(RakNet::Time)-RakNetGUID::size()-sizeof(OFFLINE_MESSAGE_DATA_ID) - ); - - packet->systemAddress = systemAddress; - packet->systemAddress.systemIndex = ( SystemIndex ) rakPeer->GetIndexFromSystemAddress( systemAddress, true ); - packet->guid.systemIndex=packet->systemAddress.systemIndex; - rakPeer->AddPacketToProducer(packet); - } - else if ((unsigned char) data[ 0 ] == ID_OUT_OF_BAND_INTERNAL && - (size_t) length > sizeof(OFFLINE_MESSAGE_DATA_ID)+sizeof(MessageID)+RakNetGUID::size() && - (size_t) length < MAX_OFFLINE_DATA_LENGTH+sizeof(OFFLINE_MESSAGE_DATA_ID)+sizeof(MessageID)+RakNetGUID::size()) - { - unsigned int dataLength = (unsigned int) (length-sizeof(OFFLINE_MESSAGE_DATA_ID)-RakNetGUID::size()-sizeof(MessageID)); - RakAssert(dataLength<1024); - packet=rakPeer->AllocPacket(dataLength+1, _FILE_AND_LINE_); - RakAssert(packet->length<1024); - - RakNet::BitStream bs2((unsigned char*) data, length, false); - bs2.IgnoreBytes(sizeof(MessageID)); - bs2.Read(packet->guid); - - if (data[sizeof(OFFLINE_MESSAGE_DATA_ID)+sizeof(MessageID) + RakNetGUID::size()]==ID_ADVERTISE_SYSTEM) - { - packet->length--; - packet->bitSize=BYTES_TO_BITS(packet->length); - packet->data[0]=ID_ADVERTISE_SYSTEM; - memcpy(packet->data+1, data+sizeof(OFFLINE_MESSAGE_DATA_ID)+sizeof(MessageID)*2 + RakNetGUID::size(), dataLength-1); - } - else - { - packet->data[0]=ID_OUT_OF_BAND_INTERNAL; - memcpy(packet->data+1, data+sizeof(OFFLINE_MESSAGE_DATA_ID)+sizeof(MessageID) + RakNetGUID::size(), dataLength); - } - - packet->systemAddress = systemAddress; - packet->systemAddress.systemIndex = ( SystemIndex ) rakPeer->GetIndexFromSystemAddress( systemAddress, true ); - packet->guid.systemIndex=packet->systemAddress.systemIndex; - rakPeer->AddPacketToProducer(packet); - } - else if ((unsigned char)(data)[0] == (MessageID)ID_OPEN_CONNECTION_REPLY_1) - { - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress); - - RakNet::BitStream bsIn((unsigned char*) data,length,false); - bsIn.IgnoreBytes(sizeof(MessageID)); - bsIn.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID)); - RakNetGUID serverGuid; - bsIn.Read(serverGuid); - unsigned char serverHasSecurity; - uint32_t cookie; - (void) cookie; - bsIn.Read(serverHasSecurity); - // Even if the server has security, it may not be required of us if we are in the security exception list - if (serverHasSecurity) - { - bsIn.Read(cookie); - } - - RakNet::BitStream bsOut; - bsOut.Write((MessageID)ID_OPEN_CONNECTION_REQUEST_2); - bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); - if (serverHasSecurity) - bsOut.Write(cookie); - - unsigned i; - rakPeer->requestedConnectionQueueMutex.Lock(); - for (i=0; i < rakPeer->requestedConnectionQueue.Size(); i++) - { - RakPeer::RequestedConnectionStruct *rcs; - rcs=rakPeer->requestedConnectionQueue[i]; - if (rcs->systemAddress==systemAddress) - { - if (serverHasSecurity) - { -#if LIBCAT_SECURITY==1 - unsigned char public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES]; - bsIn.ReadAlignedBytes(public_key, sizeof(public_key)); - - if (rcs->publicKeyMode==PKM_ACCEPT_ANY_PUBLIC_KEY) - { - memcpy(rcs->remote_public_key, public_key, cat::EasyHandshake::PUBLIC_KEY_BYTES); - if (!rcs->client_handshake->Initialize(public_key) || - !rcs->client_handshake->GenerateChallenge(rcs->handshakeChallenge)) - { - CAT_AUDIT_PRINTF("AUDIT: Server passed a bad public key with PKM_ACCEPT_ANY_PUBLIC_KEY"); - return true; - } - } - - if (cat::SecureEqual(public_key, - rcs->remote_public_key, - cat::EasyHandshake::PUBLIC_KEY_BYTES)==false) - { - rakPeer->requestedConnectionQueueMutex.Unlock(); - CAT_AUDIT_PRINTF("AUDIT: Expected public key does not match what was sent by server -- Reporting back ID_PUBLIC_KEY_MISMATCH to user\n"); - - packet=rakPeer->AllocPacket(sizeof( char ), _FILE_AND_LINE_); - packet->data[ 0 ] = ID_PUBLIC_KEY_MISMATCH; // Attempted a connection and couldn't - packet->bitSize = ( sizeof( char ) * 8); - packet->systemAddress = rcs->systemAddress; - packet->guid=serverGuid; - rakPeer->AddPacketToProducer(packet); - return true; - } - - if (rcs->client_handshake==0) - { - // Message does not contain a challenge - // We might still pass if we are in the security exception list - bsOut.Write((unsigned char)0); - } - else - { - // Message contains a challenge - bsOut.Write((unsigned char)1); - // challenge - CAT_AUDIT_PRINTF("AUDIT: Sending challenge\n"); - bsOut.WriteAlignedBytes((const unsigned char*) rcs->handshakeChallenge,cat::EasyHandshake::CHALLENGE_BYTES); - } -#else // LIBCAT_SECURITY - // Message does not contain a challenge - bsOut.Write((unsigned char)0); -#endif // LIBCAT_SECURITY - } - else - { - // Server does not need security -#if LIBCAT_SECURITY==1 - if (rcs->client_handshake!=0) - { - rakPeer->requestedConnectionQueueMutex.Unlock(); - CAT_AUDIT_PRINTF("AUDIT: Security disabled by server but we expected security (indicated by client_handshake not null) so failing!\n"); - - packet=rakPeer->AllocPacket(sizeof( char ), _FILE_AND_LINE_); - packet->data[ 0 ] = ID_OUR_SYSTEM_REQUIRES_SECURITY; // Attempted a connection and couldn't - packet->bitSize = ( sizeof( char ) * 8); - packet->systemAddress = rcs->systemAddress; - packet->guid=serverGuid; - rakPeer->AddPacketToProducer(packet); - return true; - } -#endif // LIBCAT_SECURITY - - } - - uint16_t mtu; - bsIn.Read(mtu); - - // Binding address - bsOut.Write(rcs->systemAddress); - rakPeer->requestedConnectionQueueMutex.Unlock(); - // MTU - bsOut.Write(mtu); - // Our guid - bsOut.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); - - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), rcs->systemAddress); - - // SocketLayer::SendTo( rakPeer->socketList[rcs->socketIndex], (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), rcs->systemAddress, _FILE_AND_LINE_ ); - - RNS2_SendParameters bsp; - bsp.data = (char*) bsOut.GetData(); - bsp.length = bsOut.GetNumberOfBytesUsed(); - bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); - - return true; - } - } - rakPeer->requestedConnectionQueueMutex.Unlock(); - } - else if ((unsigned char)(data)[0] == (MessageID)ID_OPEN_CONNECTION_REPLY_2) - { - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress); - - RakNet::BitStream bs((unsigned char*) data,length,false); - bs.IgnoreBytes(sizeof(MessageID)); - bs.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID)); - RakNetGUID guid; - bs.Read(guid); - SystemAddress bindingAddress; - bool b = bs.Read(bindingAddress); - RakAssert(b); - uint16_t mtu; - b=bs.Read(mtu); - RakAssert(b); - bool doSecurity=false; - b=bs.Read(doSecurity); - RakAssert(b); - -#if LIBCAT_SECURITY==1 - char answer[cat::EasyHandshake::ANSWER_BYTES]; - CAT_AUDIT_PRINTF("AUDIT: Got ID_OPEN_CONNECTION_REPLY_2 and given doSecurity=%i\n", (int)doSecurity); - if (doSecurity) - { - CAT_AUDIT_PRINTF("AUDIT: Reading cookie and public key\n"); - bs.ReadAlignedBytes((unsigned char*) answer, sizeof(answer)); - } - cat::ClientEasyHandshake *client_handshake=0; -#endif // LIBCAT_SECURITY - - RakPeer::RequestedConnectionStruct *rcs; - bool unlock=true; - unsigned i; - rakPeer->requestedConnectionQueueMutex.Lock(); - for (i=0; i < rakPeer->requestedConnectionQueue.Size(); i++) - { - rcs=rakPeer->requestedConnectionQueue[i]; - - - if (rcs->systemAddress==systemAddress) - { -#if LIBCAT_SECURITY==1 - CAT_AUDIT_PRINTF("AUDIT: System address matches an entry in the requestedConnectionQueue and doSecurity=%i\n", (int)doSecurity); - if (doSecurity) - { - if (rcs->client_handshake==0) - { - CAT_AUDIT_PRINTF("AUDIT: Server wants security but we didn't set a public key -- Reporting back ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY to user\n"); - rakPeer->requestedConnectionQueueMutex.Unlock(); - - packet=rakPeer->AllocPacket(2, _FILE_AND_LINE_); - packet->data[ 0 ] = ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY; // Attempted a connection and couldn't - packet->data[ 1 ] = 0; // Indicate server public key is missing - packet->bitSize = ( sizeof( char ) * 8); - packet->systemAddress = rcs->systemAddress; - packet->guid=guid; - rakPeer->AddPacketToProducer(packet); - return true; - } - - CAT_AUDIT_PRINTF("AUDIT: Looks good, preparing to send challenge to server! client_handshake = %x\n", client_handshake); - } - -#endif // LIBCAT_SECURITY - - rakPeer->requestedConnectionQueueMutex.Unlock(); - unlock=false; - - RakAssert(rcs->actionToTake==RakPeer::RequestedConnectionStruct::CONNECT); - // You might get this when already connected because of cross-connections - bool thisIPConnectedRecently=false; - remoteSystem=rakPeer->GetRemoteSystemFromSystemAddress( systemAddress, true, true ); - if (remoteSystem==0) - { - if (rcs->socket == 0) - { - remoteSystem=rakPeer->AssignSystemAddressToRemoteSystemList(systemAddress, RakPeer::RemoteSystemStruct::UNVERIFIED_SENDER, rakNetSocket, &thisIPConnectedRecently, bindingAddress, mtu, guid, doSecurity); - } - else - { - remoteSystem=rakPeer->AssignSystemAddressToRemoteSystemList(systemAddress, RakPeer::RemoteSystemStruct::UNVERIFIED_SENDER, rcs->socket, &thisIPConnectedRecently, bindingAddress, mtu, guid, doSecurity); - } - } - - // 4/13/09 Attackers can flood ID_OPEN_CONNECTION_REQUEST and use up all available connection slots - // Ignore connection attempts if this IP address connected within the last 100 milliseconds - if (thisIPConnectedRecently==false) - { - // Don't check GetRemoteSystemFromGUID, server will verify - if (remoteSystem) - { - // Move pointer from RequestedConnectionStruct to RemoteSystemStruct -#if LIBCAT_SECURITY==1 - cat::u8 ident[cat::EasyHandshake::IDENTITY_BYTES]; - bool doIdentity = false; - - if (rcs->client_handshake) - { - CAT_AUDIT_PRINTF("AUDIT: Processing answer\n"); - if (rcs->publicKeyMode == PKM_USE_TWO_WAY_AUTHENTICATION) - { - if (!rcs->client_handshake->ProcessAnswerWithIdentity(answer, ident, remoteSystem->reliabilityLayer.GetAuthenticatedEncryption())) - { - CAT_AUDIT_PRINTF("AUDIT: Processing answer -- Invalid Answer\n"); - rakPeer->requestedConnectionQueueMutex.Unlock(); - - return true; - } - - doIdentity = true; - } - else - { - if (!rcs->client_handshake->ProcessAnswer(answer, remoteSystem->reliabilityLayer.GetAuthenticatedEncryption())) - { - CAT_AUDIT_PRINTF("AUDIT: Processing answer -- Invalid Answer\n"); - rakPeer->requestedConnectionQueueMutex.Unlock(); - - return true; - } - } - CAT_AUDIT_PRINTF("AUDIT: Success!\n"); - - RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); - rcs->client_handshake=0; - } -#endif // LIBCAT_SECURITY - - remoteSystem->weInitiatedTheConnection=true; - remoteSystem->connectMode=RakPeer::RemoteSystemStruct::REQUESTED_CONNECTION; - if (rcs->timeoutTime!=0) - remoteSystem->reliabilityLayer.SetTimeoutTime(rcs->timeoutTime); - - RakNet::BitStream temp; - temp.Write( (MessageID)ID_CONNECTION_REQUEST); - temp.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); - temp.Write(RakNet::GetTime()); - -#if LIBCAT_SECURITY==1 - temp.Write((unsigned char)(doSecurity ? 1 : 0)); - - if (doSecurity) - { - unsigned char proof[32]; - remoteSystem->reliabilityLayer.GetAuthenticatedEncryption()->GenerateProof(proof, sizeof(proof)); - temp.WriteAlignedBytes(proof, sizeof(proof)); - - temp.Write((unsigned char)(doIdentity ? 1 : 0)); - - if (doIdentity) - { - temp.WriteAlignedBytes(ident, sizeof(ident)); - } - } -#else - temp.Write((unsigned char)0); -#endif // LIBCAT_SECURITY - - if ( rcs->outgoingPasswordLength > 0 ) - temp.Write( ( char* ) rcs->outgoingPassword, rcs->outgoingPasswordLength ); - - rakPeer->SendImmediate((char*)temp.GetData(), temp.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, RELIABLE, 0, systemAddress, false, false, timeRead, 0 ); - } - else - { - // Failed, no connections available anymore - packet=rakPeer->AllocPacket(sizeof( char ), _FILE_AND_LINE_); - packet->data[ 0 ] = ID_CONNECTION_ATTEMPT_FAILED; // Attempted a connection and couldn't - packet->bitSize = ( sizeof( char ) * 8); - packet->systemAddress = rcs->systemAddress; - packet->guid=guid; - rakPeer->AddPacketToProducer(packet); - } - } - - rakPeer->requestedConnectionQueueMutex.Lock(); - for (unsigned int k=0; k < rakPeer->requestedConnectionQueue.Size(); k++) - { - if (rakPeer->requestedConnectionQueue[k]->systemAddress==systemAddress) - { - rakPeer->requestedConnectionQueue.RemoveAtIndex(k); - break; - } - } - rakPeer->requestedConnectionQueueMutex.Unlock(); - -#if LIBCAT_SECURITY==1 - CAT_AUDIT_PRINTF("AUDIT: Deleting client_handshake object %x and rcs->client_handshake object %x\n", client_handshake, rcs->client_handshake); - RakNet::OP_DELETE(client_handshake,_FILE_AND_LINE_); - RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); -#endif // LIBCAT_SECURITY - RakNet::OP_DELETE(rcs,_FILE_AND_LINE_); - - break; - } - } - - if (unlock) - rakPeer->requestedConnectionQueueMutex.Unlock(); - - return true; - - } - else if ((unsigned char)(data)[0] == (MessageID)ID_CONNECTION_ATTEMPT_FAILED || - (unsigned char)(data)[0] == (MessageID)ID_NO_FREE_INCOMING_CONNECTIONS || - (unsigned char)(data)[0] == (MessageID)ID_CONNECTION_BANNED || - (unsigned char)(data)[0] == (MessageID)ID_ALREADY_CONNECTED || - (unsigned char)(data)[0] == (MessageID)ID_INVALID_PASSWORD || - (unsigned char)(data)[0] == (MessageID)ID_IP_RECENTLY_CONNECTED || - (unsigned char)(data)[0] == (MessageID)ID_INCOMPATIBLE_PROTOCOL_VERSION) - { - - RakNet::BitStream bs((unsigned char*) data,length,false); - bs.IgnoreBytes(sizeof(MessageID)); - bs.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID)); - if ((unsigned char)(data)[0] == (MessageID)ID_INCOMPATIBLE_PROTOCOL_VERSION) - bs.IgnoreBytes(sizeof(unsigned char)); - - RakNetGUID guid; - bs.Read(guid); - - RakPeer::RequestedConnectionStruct *rcs; - bool connectionAttemptCancelled=false; - unsigned i; - rakPeer->requestedConnectionQueueMutex.Lock(); - for (i=0; i < rakPeer->requestedConnectionQueue.Size(); i++) - { - rcs=rakPeer->requestedConnectionQueue[i]; - if (rcs->actionToTake==RakPeer::RequestedConnectionStruct::CONNECT && rcs->systemAddress==systemAddress) - { - connectionAttemptCancelled=true; - rakPeer->requestedConnectionQueue.RemoveAtIndex(i); - -#if LIBCAT_SECURITY==1 - CAT_AUDIT_PRINTF("AUDIT: Connection attempt canceled so deleting rcs->client_handshake object %x\n", rcs->client_handshake); - RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); -#endif // LIBCAT_SECURITY - RakNet::OP_DELETE(rcs,_FILE_AND_LINE_); - break; - } - } - - rakPeer->requestedConnectionQueueMutex.Unlock(); - - if (connectionAttemptCancelled) - { - // Tell user of connection attempt failed - packet=rakPeer->AllocPacket(sizeof( char ), _FILE_AND_LINE_); - packet->data[ 0 ] = data[0]; // Attempted a connection and couldn't - packet->bitSize = ( sizeof( char ) * 8); - packet->systemAddress = systemAddress; - packet->guid=guid; - rakPeer->AddPacketToProducer(packet); - } - } - else if ((unsigned char)(data)[0] == ID_OPEN_CONNECTION_REQUEST_1 && length > (int) (1+sizeof(OFFLINE_MESSAGE_DATA_ID))) - {/* - static int x = 0; - ++x; - - SystemAddress *addr = (SystemAddress*)&systemAddress; - addr->binaryAddress += x;*/ - - unsigned int i; - //RAKNET_DEBUG_PRINTF("%i:IOCR, ", __LINE__); - char remoteProtocol=data[1+sizeof(OFFLINE_MESSAGE_DATA_ID)]; - if (remoteProtocol!=RAKNET_PROTOCOL_VERSION) - { - RakNet::BitStream bs; - bs.Write((MessageID)ID_INCOMPATIBLE_PROTOCOL_VERSION); - bs.Write((unsigned char)RAKNET_PROTOCOL_VERSION); - bs.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); - bs.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); - - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketSend((char*)bs.GetData(), bs.GetNumberOfBitsUsed(), systemAddress); - - // SocketLayer::SendTo( rakNetSocket, (char*)bs.GetData(), bs.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); - - RNS2_SendParameters bsp; - bsp.data = (char*) bs.GetData(); - bsp.length = bs.GetNumberOfBytesUsed(); - bsp.systemAddress = systemAddress; - - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); - return true; - } - - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress); - - RakNet::BitStream bsOut; - bsOut.Write((MessageID)ID_OPEN_CONNECTION_REPLY_1); - bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); - bsOut.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); -#if LIBCAT_SECURITY==1 - if (rakPeer->_using_security) - { - bsOut.Write((unsigned char) 1); // HasCookie Yes - // Write cookie - uint32_t cookie = rakPeer->_cookie_jar->Generate(&systemAddress.address,sizeof(systemAddress.address)); - CAT_AUDIT_PRINTF("AUDIT: Writing cookie %i to %i:%i\n", cookie, systemAddress); - bsOut.Write(cookie); - // Write my public key - bsOut.WriteAlignedBytes((const unsigned char *) rakPeer->my_public_key,sizeof(rakPeer->my_public_key)); - } - else -#endif // LIBCAT_SECURITY - bsOut.Write((unsigned char) 0); // HasCookie oN - - // MTU. Lower MTU if it is exceeds our own limit - if (length+UDP_HEADER_SIZE > MAXIMUM_MTU_SIZE) - bsOut.WriteCasted(MAXIMUM_MTU_SIZE); - else - bsOut.WriteCasted(length+UDP_HEADER_SIZE); - - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); - // SocketLayer::SendTo( rakNetSocket, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); - - RNS2_SendParameters bsp; - bsp.data = (char*) bsOut.GetData(); - bsp.length = bsOut.GetNumberOfBytesUsed(); - bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); - } - else if ((unsigned char)(data)[0] == ID_OPEN_CONNECTION_REQUEST_2) - { - SystemAddress bindingAddress; - RakNetGUID guid; - RakNet::BitStream bsOut; - RakNet::BitStream bs((unsigned char*) data, length, false); - bs.IgnoreBytes(sizeof(MessageID)); - bs.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID)); - - bool requiresSecurityOfThisClient=false; -#if LIBCAT_SECURITY==1 - char remoteHandshakeChallenge[cat::EasyHandshake::CHALLENGE_BYTES]; - - if (rakPeer->_using_security) - { - char str1[64]; - systemAddress.ToString(false, str1); - requiresSecurityOfThisClient=rakPeer->IsInSecurityExceptionList(str1)==false; - - uint32_t cookie; - bs.Read(cookie); - CAT_AUDIT_PRINTF("AUDIT: Got cookie %i from %i:%i\n", cookie, systemAddress); - if (rakPeer->_cookie_jar->Verify(&systemAddress.address,sizeof(systemAddress.address), cookie)==false) - { - return true; - } - CAT_AUDIT_PRINTF("AUDIT: Cookie good!\n"); - - unsigned char clientWroteChallenge; - bs.Read(clientWroteChallenge); - - if (requiresSecurityOfThisClient==true && clientWroteChallenge==0) - { - // Fail, client doesn't support security, and it is required - return true; - } - - if (clientWroteChallenge) - { - bs.ReadAlignedBytes((unsigned char*) remoteHandshakeChallenge, cat::EasyHandshake::CHALLENGE_BYTES); -#ifdef CAT_AUDIT - printf("AUDIT: RECV CHALLENGE "); - for (int ii = 0; ii < sizeof(remoteHandshakeChallenge); ++ii) - { - printf("%02x", (cat::u8)remoteHandshakeChallenge[ii]); - } - printf("\n"); -#endif - } - } -#endif // LIBCAT_SECURITY - - bs.Read(bindingAddress); - uint16_t mtu; - bs.Read(mtu); - bs.Read(guid); - - RakPeer::RemoteSystemStruct *rssFromSA = rakPeer->GetRemoteSystemFromSystemAddress( systemAddress, true, true ); - bool IPAddrInUse = rssFromSA != 0 && rssFromSA->isActive; - RakPeer::RemoteSystemStruct *rssFromGuid = rakPeer->GetRemoteSystemFromGUID(guid, true); - bool GUIDInUse = rssFromGuid != 0 && rssFromGuid->isActive; - - // IPAddrInUse, GuidInUse, outcome - // TRUE, , TRUE , ID_OPEN_CONNECTION_REPLY if they are the same, else ID_ALREADY_CONNECTED - // FALSE, , TRUE , ID_ALREADY_CONNECTED (someone else took this guid) - // TRUE, , FALSE , ID_ALREADY_CONNECTED (silently disconnected, restarted rakNet) - // FALSE , FALSE , Allow connection - - int outcome; - if (IPAddrInUse & GUIDInUse) - { - if (rssFromSA==rssFromGuid && rssFromSA->connectMode==RakPeer::RemoteSystemStruct::UNVERIFIED_SENDER) - { - // ID_OPEN_CONNECTION_REPLY if they are the same - outcome=1; - - // Note to self: If REQUESTED_CONNECTION, this means two systems attempted to connect to each other at the same time, and one finished first. - // Returns ID)_CONNECTION_REQUEST_ACCEPTED to one system, and ID_ALREADY_CONNECTED followed by ID_NEW_INCOMING_CONNECTION to another - } - else - { - // ID_ALREADY_CONNECTED (restarted raknet, connected again from same ip, plus someone else took this guid) - outcome=2; - } - } - else if (IPAddrInUse==false && GUIDInUse==true) - { - // ID_ALREADY_CONNECTED (someone else took this guid) - outcome=3; - } - else if (IPAddrInUse==true && GUIDInUse==false) - { - // ID_ALREADY_CONNECTED (silently disconnected, restarted rakNet) - outcome=4; - } - else - { - // Allow connection - outcome=0; - } - - RakNet::BitStream bsAnswer; - bsAnswer.Write((MessageID)ID_OPEN_CONNECTION_REPLY_2); - bsAnswer.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); - bsAnswer.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); - bsAnswer.Write(systemAddress); - bsAnswer.Write(mtu); - bsAnswer.Write(requiresSecurityOfThisClient); - - if (outcome==1) - { - // Duplicate connection request packet from packetloss - // Send back the same answer -#if LIBCAT_SECURITY==1 - if (requiresSecurityOfThisClient) - { - CAT_AUDIT_PRINTF("AUDIT: Resending public key and answer from packetloss. Sending ID_OPEN_CONNECTION_REPLY_2\n"); - bsAnswer.WriteAlignedBytes((const unsigned char *) rssFromSA->answer,sizeof(rssFromSA->answer)); - } -#endif // LIBCAT_SECURITY - - unsigned int i; - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*) bsAnswer.GetData(), bsAnswer.GetNumberOfBitsUsed(), systemAddress); - // SocketLayer::SendTo( rakNetSocket, (const char*) bsAnswer.GetData(), bsAnswer.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); - - RNS2_SendParameters bsp; - bsp.data = (char*) bsAnswer.GetData(); - bsp.length = bsAnswer.GetNumberOfBytesUsed(); - bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); - - return true; - } - else if (outcome!=0) - { - bsOut.Write((MessageID)ID_ALREADY_CONNECTED); - bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); - bsOut.Write(rakPeer->myGuid); - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); - // SocketLayer::SendTo( rakNetSocket, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); - RNS2_SendParameters bsp; - bsp.data = (char*) bsOut.GetData(); - bsp.length = bsOut.GetNumberOfBytesUsed(); - bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); - - return true; - } - - if (rakPeer->AllowIncomingConnections()==false) - { - bsOut.Write((MessageID)ID_NO_FREE_INCOMING_CONNECTIONS); - bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); - bsOut.Write(rakPeer->myGuid); - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); - //SocketLayer::SendTo( rakNetSocket, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); - RNS2_SendParameters bsp; - bsp.data = (char*) bsOut.GetData(); - bsp.length = bsOut.GetNumberOfBytesUsed(); - bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); - - return true; - } - - bool thisIPConnectedRecently=false; - rssFromSA = rakPeer->AssignSystemAddressToRemoteSystemList(systemAddress, RakPeer::RemoteSystemStruct::UNVERIFIED_SENDER, rakNetSocket, &thisIPConnectedRecently, bindingAddress, mtu, guid, requiresSecurityOfThisClient); - - if (thisIPConnectedRecently==true) - { - bsOut.Write((MessageID)ID_IP_RECENTLY_CONNECTED); - bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); - bsOut.Write(rakPeer->myGuid); - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); - //SocketLayer::SendTo( rakNetSocket, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); - - RNS2_SendParameters bsp; - bsp.data = (char*) bsOut.GetData(); - bsp.length = bsOut.GetNumberOfBytesUsed(); - bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); - - return true; - } - -#if LIBCAT_SECURITY==1 - if (requiresSecurityOfThisClient) - { - CAT_AUDIT_PRINTF("AUDIT: Writing public key. Sending ID_OPEN_CONNECTION_REPLY_2\n"); - if (rakPeer->_server_handshake->ProcessChallenge(remoteHandshakeChallenge, rssFromSA->answer, rssFromSA->reliabilityLayer.GetAuthenticatedEncryption() )) - { - CAT_AUDIT_PRINTF("AUDIT: Challenge good!\n"); - // Keep going to OK block - } - else - { - CAT_AUDIT_PRINTF("AUDIT: Challenge BAD!\n"); - - // Unassign this remote system - rakPeer->DereferenceRemoteSystem(systemAddress); - return true; - } - - bsAnswer.WriteAlignedBytes((const unsigned char *) rssFromSA->answer,sizeof(rssFromSA->answer)); - } -#endif // LIBCAT_SECURITY - - unsigned int i; - for (i=0; i < rakPeer->pluginListNTS.Size(); i++) - rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*) bsAnswer.GetData(), bsAnswer.GetNumberOfBitsUsed(), systemAddress); - // SocketLayer::SendTo( rakNetSocket, (const char*) bsAnswer.GetData(), bsAnswer.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); - RNS2_SendParameters bsp; - bsp.data = (char*) bsAnswer.GetData(); - bsp.length = bsAnswer.GetNumberOfBytesUsed(); - bsp.systemAddress = systemAddress; - rakNetSocket->Send(&bsp, _FILE_AND_LINE_); - } - return true; - } - - return false; -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void ProcessNetworkPacket( SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNet::TimeUS timeRead, BitStream &updateBitStream ) -{ - ProcessNetworkPacket(systemAddress,data,length,rakPeer,rakPeer->socketList[0],timeRead, updateBitStream); -} -void ProcessNetworkPacket( SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSocket2* rakNetSocket, RakNet::TimeUS timeRead, BitStream &updateBitStream ) -{ -#if LIBCAT_SECURITY==1 -#ifdef CAT_AUDIT - printf("AUDIT: RECV "); - for (int ii = 0; ii < length; ++ii) - { - printf("%02x", (cat::u8)data[ii]); - } - printf("\n"); -#endif -#endif // LIBCAT_SECURITY - - RakAssert(systemAddress.GetPort()); - bool isOfflineMessage; - if (ProcessOfflineNetworkPacket(systemAddress, data, length, rakPeer, rakNetSocket, &isOfflineMessage, timeRead)) - { - return; - } - -// RakNet::Packet *packet; - RakPeer::RemoteSystemStruct *remoteSystem; - - // See if this datagram came from a connected system - remoteSystem = rakPeer->GetRemoteSystemFromSystemAddress( systemAddress, true, true ); - if ( remoteSystem ) - { - // Handle regular incoming data - // HandleSocketReceiveFromConnectedPlayer is only safe to be called from the same thread as Update, which is this thread - if ( isOfflineMessage==false) - { - remoteSystem->reliabilityLayer.HandleSocketReceiveFromConnectedPlayer( - data, length, systemAddress, rakPeer->pluginListNTS, remoteSystem->MTUSize, - rakNetSocket, &rnr, timeRead, updateBitStream); - } - } - else - { - // int a=5; - // printf("--- Packet from unknown system %s\n", systemAddress.ToString()); - } -} - -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -unsigned int RakPeer::GenerateSeedFromGuid(void) -{ - /* - // Construct a random seed based on the initial guid value, and the last digits of the difference to each subsequent number - // This assumes that only the last 3 bits of each guidId integer has a meaningful amount of randomness between it and the prior number - unsigned int t = guid.g[0]; - unsigned int i; - for (i=1; i < sizeof(guid.g) / sizeof(guid.g[0]); i++) - { - unsigned int diff = guid.g[i]-guid.g[i-1]; - unsigned int diff3Bits = diff & 0x0007; - diff3Bits <<= 29; - diff3Bits >>= (i-1)*3; - t ^= diff3Bits; - } - - return t; - */ - return (unsigned int) ((myGuid.g >> 32) ^ myGuid.g); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -void RakPeer::DerefAllSockets(void) -{ - unsigned int i; - for (i=0; i < socketList.Size(); i++) - { - delete socketList[i]; - } - socketList.Clear(false, _FILE_AND_LINE_); -} -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -unsigned int RakPeer::GetRakNetSocketFromUserConnectionSocketIndex(unsigned int userIndex) const -{ - unsigned int i; - for (i=0; i < socketList.Size(); i++) - { - if (socketList[i]->GetUserConnectionSocketIndex()==userIndex) - return i; - } - RakAssert("GetRakNetSocketFromUserConnectionSocketIndex failed" && 0); - return (unsigned int) -1; -} - -/* -// DS_APR -void RakPeer::ProcessChromePacket(RakNetSocket2 *s, const char *buffer, int dataSize, const SystemAddress& recvFromAddress, RakNet::TimeUS timeRead) -{ - RakAssert(buffer); - RakAssert(dataSize > 0); - RakAssert(recvFromAddress.GetPort()); - - RNS2RecvStruct *recvFromStruct; - recvFromStruct=bufferedPackets.Allocate( _FILE_AND_LINE_ ); - RakAssert(dataSize <= (int)sizeof(recvFromStruct->data)); - memcpy(recvFromStruct->data, buffer, dataSize); - recvFromStruct->bytesRead=dataSize; - recvFromStruct->systemAddress=recvFromAddress; - recvFromStruct->timeRead=timeRead; - bufferedPackets.Push(recvFromStruct); -} -*/ - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -/* -bool RakPeer::RunRecvFromOnce( RakNetSocket *s ) -{ - RakPeer::RecvFromStruct *recvFromStruct; - - recvFromStruct=bufferedPackets.Allocate( _FILE_AND_LINE_ ); - if (recvFromStruct != NULL) - { - recvFromStruct->s=s; - SocketLayer::RecvFromBlocking(s, this, recvFromStruct->data, &recvFromStruct->bytesRead, &recvFromStruct->systemAddress, &recvFromStruct->timeRead); - - if (recvFromStruct->bytesRead>0) - { - RakAssert(recvFromStruct->systemAddress.GetPort()); - bufferedPackets.Push(recvFromStruct); - quitAndDataEvents.SetEvent(); - - // Got data - return true; - } - else - { - bufferedPackets.Deallocate(recvFromStruct, _FILE_AND_LINE_); - } - } - // No data - return false; -} -*/ -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -bool RakPeer::RunUpdateCycle(BitStream &updateBitStream ) -{ - RakPeer::RemoteSystemStruct * remoteSystem; - unsigned int activeSystemListIndex; - Packet *packet; - // int currentSentBytes,currentReceivedBytes; -// unsigned numberOfBytesUsed; -// BitSize_t numberOfBitsUsed; - //SystemAddress authoritativeClientSystemAddress; - BitSize_t bitSize; - unsigned int byteSize; - unsigned char *data; - SystemAddress systemAddress; - BufferedCommandStruct *bcs; - bool callerDataAllocationUsed; - RakNetStatistics *rnss; - RakNet::TimeUS timeNS=0; - RakNet::Time timeMS=0; - - // This is here so RecvFromBlocking actually gets data from the same thread - - #if defined(WINDOWS_STORE_RT) - #elif defined(_WIN32) - if (socketList[0]->GetSocketType()==RNS2T_WINDOWS && ((RNS2_Windows*)socketList[0])->GetSocketLayerOverride()) - { - int len; - SystemAddress sender; - char dataOut[ MAXIMUM_MTU_SIZE ]; - do { - len = ((RNS2_Windows*)socketList[0])->GetSocketLayerOverride()->RakNetRecvFrom(dataOut,&sender,true); - if (len>0) - ProcessNetworkPacket( sender, dataOut, len, this, socketList[0], RakNet::GetTimeUS(), updateBitStream ); - } while (len>0); - } - #endif - -// unsigned int socketListIndex; - RNS2RecvStruct *recvFromStruct; - while ((recvFromStruct=PopBufferedPacket())!=0) - { - /* - for (socketListIndex=0; socketListIndex < socketList.Size(); socketListIndex++) - { - if ((RakNetSocket*) socketList[socketListIndex]==recvFromStruct->s) - break; - } - if (socketListIndex!=socketList.Size()) - */ - ProcessNetworkPacket(recvFromStruct->systemAddress, recvFromStruct->data, recvFromStruct->bytesRead, this, recvFromStruct->socket, recvFromStruct->timeRead, updateBitStream); - DeallocRNS2RecvStruct(recvFromStruct, _FILE_AND_LINE_); - } - - while ((bcs=bufferedCommands.PopInaccurate())!=0) - { - if (bcs->command==BufferedCommandStruct::BCS_SEND) - { - // GetTime is a very slow call so do it once and as late as possible - if (timeNS==0) - { - timeNS = RakNet::GetTimeUS(); - timeMS = (RakNet::TimeMS)(timeNS/(RakNet::TimeUS)1000); - } - - callerDataAllocationUsed=SendImmediate((char*)bcs->data, bcs->numberOfBitsToSend, bcs->priority, bcs->reliability, bcs->orderingChannel, bcs->systemIdentifier, bcs->broadcast, true, timeNS, bcs->receipt); - if ( callerDataAllocationUsed==false ) - rakFree_Ex(bcs->data, _FILE_AND_LINE_ ); - - // Set the new connection state AFTER we call sendImmediate in case we are setting it to a disconnection state, which does not allow further sends - if (bcs->connectionMode!=RemoteSystemStruct::NO_ACTION ) - { - remoteSystem=GetRemoteSystem( bcs->systemIdentifier, true, true ); - if (remoteSystem) - remoteSystem->connectMode=bcs->connectionMode; - } - } - else if (bcs->command==BufferedCommandStruct::BCS_CLOSE_CONNECTION) - { - CloseConnectionInternal(bcs->systemIdentifier, false, true, bcs->orderingChannel, bcs->priority); - } - else if (bcs->command==BufferedCommandStruct::BCS_CHANGE_SYSTEM_ADDRESS) - { - // Reroute - RakPeer::RemoteSystemStruct *rssFromGuid = GetRemoteSystem(bcs->systemIdentifier.rakNetGuid,true,true); - if (rssFromGuid!=0) - { - unsigned int existingSystemIndex = GetRemoteSystemIndex(rssFromGuid->systemAddress); - ReferenceRemoteSystem(bcs->systemIdentifier.systemAddress, existingSystemIndex); - } - } - else if (bcs->command==BufferedCommandStruct::BCS_GET_SOCKET) - { - SocketQueryOutput *sqo; - if (bcs->systemIdentifier.IsUndefined()) - { - sqo = socketQueryOutput.Allocate( _FILE_AND_LINE_ ); - sqo->sockets=socketList; - socketQueryOutput.Push(sqo); - } - else - { - remoteSystem=GetRemoteSystem( bcs->systemIdentifier, true, true ); - sqo = socketQueryOutput.Allocate( _FILE_AND_LINE_ ); - - sqo->sockets.Clear(false, _FILE_AND_LINE_); - if (remoteSystem) - { - sqo->sockets.Push(remoteSystem->rakNetSocket, _FILE_AND_LINE_ ); - } - else - { - // Leave empty smart pointer - } - socketQueryOutput.Push(sqo); - } - - } - -#ifdef _DEBUG - bcs->data=0; -#endif - - bufferedCommands.Deallocate(bcs, _FILE_AND_LINE_); - } - - if (requestedConnectionQueue.IsEmpty()==false) - { - if (timeNS==0) - { - timeNS = RakNet::GetTimeUS(); - timeMS = (RakNet::TimeMS)(timeNS/(RakNet::TimeUS)1000); - } - - bool condition1, condition2; - unsigned requestedConnectionQueueIndex=0; - requestedConnectionQueueMutex.Lock(); - while (requestedConnectionQueueIndex < requestedConnectionQueue.Size()) - { - RequestedConnectionStruct *rcs; - rcs = requestedConnectionQueue[requestedConnectionQueueIndex]; - requestedConnectionQueueMutex.Unlock(); - if (rcs->nextRequestTime < timeMS) - { - condition1=rcs->requestsMade==rcs->sendConnectionAttemptCount+1; - condition2=(bool)((rcs->systemAddress==UNASSIGNED_SYSTEM_ADDRESS)==1); - // If too many requests made or a hole then remove this if possible, otherwise invalidate it - if (condition1 || condition2) - { - if (rcs->data) - { - rakFree_Ex(rcs->data, _FILE_AND_LINE_ ); - rcs->data=0; - } - - if (condition1 && !condition2 && rcs->actionToTake==RequestedConnectionStruct::CONNECT) - { - // Tell user of connection attempt failed - packet=AllocPacket(sizeof( char ), _FILE_AND_LINE_); - packet->data[ 0 ] = ID_CONNECTION_ATTEMPT_FAILED; // Attempted a connection and couldn't - packet->bitSize = ( sizeof( char ) * 8); - packet->systemAddress = rcs->systemAddress; - AddPacketToProducer(packet); - } - -#if LIBCAT_SECURITY==1 - CAT_AUDIT_PRINTF("AUDIT: Connection attempt FAILED so deleting rcs->client_handshake object %x\n", rcs->client_handshake); - RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); -#endif - RakNet::OP_DELETE(rcs,_FILE_AND_LINE_); - - requestedConnectionQueueMutex.Lock(); - for (unsigned int k=0; k < requestedConnectionQueue.Size(); k++) - { - if (requestedConnectionQueue[k]==rcs) - { - requestedConnectionQueue.RemoveAtIndex(k); - break; - } - } - requestedConnectionQueueMutex.Unlock(); - } - else - { - - int MTUSizeIndex = rcs->requestsMade / (rcs->sendConnectionAttemptCount/NUM_MTU_SIZES); - if (MTUSizeIndex>=NUM_MTU_SIZES) - MTUSizeIndex=NUM_MTU_SIZES-1; - rcs->requestsMade++; - rcs->nextRequestTime=timeMS+rcs->timeBetweenSendConnectionAttemptsMS; - - RakNet::BitStream bitStream; - //WriteOutOfBandHeader(&bitStream, ID_USER_PACKET_ENUM); - bitStream.Write((MessageID)ID_OPEN_CONNECTION_REQUEST_1); - bitStream.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); - bitStream.Write((MessageID)RAKNET_PROTOCOL_VERSION); - bitStream.PadWithZeroToByteLength(mtuSizes[MTUSizeIndex]-UDP_HEADER_SIZE); - - char str[256]; - rcs->systemAddress.ToString(true,str); - - //RAKNET_DEBUG_PRINTF("%i:IOCR, ", __LINE__); - - unsigned i; - for (i=0; i < pluginListNTS.Size(); i++) - pluginListNTS[i]->OnDirectSocketSend((const char*) bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), rcs->systemAddress); - - RakNetSocket2 *socketToUse; - if (rcs->socket == 0) - socketToUse = socketList[rcs->socketIndex]; - else - socketToUse = rcs->socket; - - rcs->systemAddress.FixForIPVersion(socketToUse->GetBoundAddress()); -#if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) - if (socketToUse->IsBerkleySocket()) - ((RNS2_Berkley*)socketToUse)->SetDoNotFragment(1); -#endif - -// SocketLayer::SetDoNotFragment(socketToUse, 1); - RakNet::Time sendToStart=RakNet::GetTime(); - - RNS2_SendParameters bsp; - bsp.data = (char*) bitStream.GetData(); - bsp.length = bitStream.GetNumberOfBytesUsed(); - bsp.systemAddress = rcs->systemAddress; - if (socketToUse->Send(&bsp, _FILE_AND_LINE_) == 10040) - // if (SocketLayer::SendTo( socketToUse, (const char*) bitStream.GetData(), bitStream.GetNumberOfBytesUsed(), rcs->systemAddress, _FILE_AND_LINE_ )==-10040) - { - // Don't use this MTU size again - rcs->requestsMade = (unsigned char) ((MTUSizeIndex + 1) * (rcs->sendConnectionAttemptCount/NUM_MTU_SIZES)); - rcs->nextRequestTime=timeMS; - } - else - { - RakNet::Time sendToEnd=RakNet::GetTime(); - if (sendToEnd-sendToStart>100) - { - // Drop to lowest MTU - int lowestMtuIndex = rcs->sendConnectionAttemptCount/NUM_MTU_SIZES * (NUM_MTU_SIZES - 1); - if (lowestMtuIndex > rcs->requestsMade) - { - rcs->requestsMade = (unsigned char) lowestMtuIndex; - rcs->nextRequestTime=timeMS; - } - else - rcs->requestsMade=(unsigned char)(rcs->sendConnectionAttemptCount+1); - } - } - // SocketLayer::SetDoNotFragment(socketToUse, 0); -#if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) - if (socketToUse->IsBerkleySocket()) - ((RNS2_Berkley*)socketToUse)->SetDoNotFragment(0); -#endif - - requestedConnectionQueueIndex++; - } - } - else - requestedConnectionQueueIndex++; - - requestedConnectionQueueMutex.Lock(); - } - requestedConnectionQueueMutex.Unlock(); - } - - // remoteSystemList in network thread - for ( activeSystemListIndex = 0; activeSystemListIndex < activeSystemListSize; ++activeSystemListIndex ) - //for ( remoteSystemIndex = 0; remoteSystemIndex < remoteSystemListSize; ++remoteSystemIndex ) - { - // I'm using systemAddress from remoteSystemList but am not locking it because this loop is called very frequently and it doesn't - // matter if we miss or do an extra update. The reliability layers themselves never care which player they are associated with - //systemAddress = remoteSystemList[ remoteSystemIndex ].systemAddress; - // Allow the systemAddress for this remote system list to change. We don't care if it changes now. - // remoteSystemList[ remoteSystemIndex ].allowSystemAddressAssigment=true; - - - // Found an active remote system - remoteSystem = activeSystemList[ activeSystemListIndex ]; - systemAddress = remoteSystem->systemAddress; - RakAssert(systemAddress!=UNASSIGNED_SYSTEM_ADDRESS); - // Update is only safe to call from the same thread that calls HandleSocketReceiveFromConnectedPlayer, - // which is this thread - - if (timeNS==0) - { - timeNS = RakNet::GetTimeUS(); - timeMS = (RakNet::TimeMS)(timeNS/(RakNet::TimeUS)1000); - //RAKNET_DEBUG_PRINTF("timeNS = %I64i timeMS=%i\n", timeNS, timeMS); - } - - - if (timeMS > remoteSystem->lastReliableSend && timeMS-remoteSystem->lastReliableSend > remoteSystem->reliabilityLayer.GetTimeoutTime()/2 && remoteSystem->connectMode==RemoteSystemStruct::CONNECTED) - { - // If no reliable packets are waiting for an ack, do a one byte reliable send so that disconnections are noticed - RakNetStatistics rakNetStatistics; - rnss=remoteSystem->reliabilityLayer.GetStatistics(&rakNetStatistics); - if (rnss->messagesInResendBuffer==0) - { - PingInternal( systemAddress, true, RELIABLE ); - - //remoteSystem->lastReliableSend=timeMS+remoteSystem->reliabilityLayer.GetTimeoutTime(); - remoteSystem->lastReliableSend=timeMS; - } - } - - remoteSystem->reliabilityLayer.Update( remoteSystem->rakNetSocket, systemAddress, remoteSystem->MTUSize, timeNS, maxOutgoingBPS, pluginListNTS, &rnr, updateBitStream ); // systemAddress only used for the internet simulator test - - // Check for failure conditions - if ( remoteSystem->reliabilityLayer.IsDeadConnection() || - ((remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ASAP || remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY) && remoteSystem->reliabilityLayer.IsOutgoingDataWaiting()==false) || - (remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ON_NO_ACK && (remoteSystem->reliabilityLayer.AreAcksWaiting()==false || remoteSystem->reliabilityLayer.AckTimeout(timeMS)==true)) || - (( - (remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION || - remoteSystem->connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST || - remoteSystem->connectMode==RemoteSystemStruct::UNVERIFIED_SENDER) - && timeMS > remoteSystem->connectionTime && timeMS - remoteSystem->connectionTime > 10000)) - ) - { - // RAKNET_DEBUG_PRINTF("timeMS=%i remoteSystem->connectionTime=%i\n", timeMS, remoteSystem->connectionTime ); - - // Failed. Inform the user? - // TODO - RakNet 4.0 - Return a different message identifier for DISCONNECT_ASAP_SILENTLY and DISCONNECT_ASAP than for DISCONNECT_ON_NO_ACK - // The first two mean we called CloseConnection(), the last means the other system sent us ID_DISCONNECTION_NOTIFICATION - if (remoteSystem->connectMode==RemoteSystemStruct::CONNECTED || remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION - || remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ASAP || remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ON_NO_ACK) - { - -// RakNet::BitStream undeliveredMessages; -// remoteSystem->reliabilityLayer.GetUndeliveredMessages(&undeliveredMessages,remoteSystem->MTUSize); - -// packet=AllocPacket(sizeof( char ) + undeliveredMessages.GetNumberOfBytesUsed()); - packet=AllocPacket(sizeof( char ), _FILE_AND_LINE_); - if (remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION) - packet->data[ 0 ] = ID_CONNECTION_ATTEMPT_FAILED; // Attempted a connection and couldn't - else if (remoteSystem->connectMode==RemoteSystemStruct::CONNECTED) - packet->data[ 0 ] = ID_CONNECTION_LOST; // DeadConnection - else - packet->data[ 0 ] = ID_DISCONNECTION_NOTIFICATION; // DeadConnection - -// memcpy(packet->data+1, undeliveredMessages.GetData(), undeliveredMessages.GetNumberOfBytesUsed()); - - packet->guid = remoteSystem->guid; - packet->systemAddress = systemAddress; - packet->systemAddress.systemIndex = remoteSystem->remoteSystemIndex; - packet->guid.systemIndex=packet->systemAddress.systemIndex; - - AddPacketToProducer(packet); - } - // else connection shutting down, don't bother telling the user - -#ifdef _DO_PRINTF - RAKNET_DEBUG_PRINTF("Connection dropped for player %i:%i\n", systemAddress); -#endif - CloseConnectionInternal( systemAddress, false, true, 0, LOW_PRIORITY ); - continue; - } - - // Ping this guy if it is time to do so - if ( remoteSystem->connectMode==RemoteSystemStruct::CONNECTED && timeMS > remoteSystem->nextPingTime && ( occasionalPing || remoteSystem->lowestPing == (unsigned short)-1 ) ) - { - remoteSystem->nextPingTime = timeMS + 5000; - PingInternal( systemAddress, true, UNRELIABLE ); - - // Update again immediately after this tick so the ping goes out right away - quitAndDataEvents.SetEvent(); - } - - // Find whoever has the lowest player ID - //if (systemAddress < authoritativeClientSystemAddress) - // authoritativeClientSystemAddress=systemAddress; - - // Does the reliability layer have any packets waiting for us? - // To be thread safe, this has to be called in the same thread as HandleSocketReceiveFromConnectedPlayer - bitSize = remoteSystem->reliabilityLayer.Receive( &data ); - - while ( bitSize > 0 ) - { - // These types are for internal use and should never arrive from a network packet - if (data[0]==ID_CONNECTION_ATTEMPT_FAILED) - { - RakAssert(0); - bitSize=0; - continue; - } - - // Fast and easy - just use the data that was returned - byteSize = (unsigned int) BITS_TO_BYTES( bitSize ); - - // For unknown senders we only accept a few specific packets - if (remoteSystem->connectMode==RemoteSystemStruct::UNVERIFIED_SENDER) - { - if ( (unsigned char)(data)[0] == ID_CONNECTION_REQUEST ) - { - ParseConnectionRequestPacket(remoteSystem, systemAddress, (const char*)data, byteSize); - rakFree_Ex(data, _FILE_AND_LINE_ ); - } - else - { - CloseConnectionInternal( systemAddress, false, true, 0, LOW_PRIORITY ); -#ifdef _DO_PRINTF - RAKNET_DEBUG_PRINTF("Temporarily banning %i:%i for sending nonsense data\n", systemAddress); -#endif - - char str1[64]; - systemAddress.ToString(false, str1); - AddToBanList(str1, remoteSystem->reliabilityLayer.GetTimeoutTime()); - - - rakFree_Ex(data, _FILE_AND_LINE_ ); - } - } - else - { - // However, if we are connected we still take a connection request in case both systems are trying to connect to each other - // at the same time - if ( (unsigned char)(data)[0] == ID_CONNECTION_REQUEST ) - { - // 04/27/06 This is wrong. With cross connections, we can both have initiated the connection are in state REQUESTED_CONNECTION - // 04/28/06 Downgrading connections from connected will close the connection due to security at ((remoteSystem->connectMode!=RemoteSystemStruct::CONNECTED && time > remoteSystem->connectionTime && time - remoteSystem->connectionTime > 10000)) - if (remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION) - { - ParseConnectionRequestPacket(remoteSystem, systemAddress, (const char*)data, byteSize); - } - else - { - - RakNet::BitStream bs((unsigned char*) data,byteSize,false); - bs.IgnoreBytes(sizeof(MessageID)); - bs.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID)); - bs.IgnoreBytes(RakNetGUID::size()); - RakNet::Time incomingTimestamp; - bs.Read(incomingTimestamp); - - // Got a connection request message from someone we are already connected to. Just reply normally. - // This can happen due to race conditions with the fully connected mesh - OnConnectionRequest( remoteSystem, incomingTimestamp ); - } - rakFree_Ex(data, _FILE_AND_LINE_ ); - } - else if ( (unsigned char) data[ 0 ] == ID_NEW_INCOMING_CONNECTION && byteSize > sizeof(unsigned char)+sizeof(unsigned int)+sizeof(unsigned short)+sizeof(RakNet::Time)*2 ) - { - if (remoteSystem->connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST) - { - remoteSystem->connectMode=RemoteSystemStruct::CONNECTED; - PingInternal( systemAddress, true, UNRELIABLE ); - - // Update again immediately after this tick so the ping goes out right away - quitAndDataEvents.SetEvent(); - - RakNet::BitStream inBitStream((unsigned char *) data, byteSize, false); - SystemAddress bsSystemAddress; - - inBitStream.IgnoreBits(8); - inBitStream.Read(bsSystemAddress); - for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++) - inBitStream.Read(remoteSystem->theirInternalSystemAddress[i]); - - RakNet::Time sendPingTime, sendPongTime; - inBitStream.Read(sendPingTime); - inBitStream.Read(sendPongTime); - OnConnectedPong(sendPingTime,sendPongTime,remoteSystem); - - // Overwrite the data in the packet - // NewIncomingConnectionStruct newIncomingConnectionStruct; - // RakNet::BitStream nICS_BS( data, NewIncomingConnectionStruct_Size, false ); - // newIncomingConnectionStruct.Deserialize( nICS_BS ); - - remoteSystem->myExternalSystemAddress = bsSystemAddress; - - // Bug: If A connects to B through R, A's firstExternalID is set to R. If A tries to send to R, sends to loopback because R==firstExternalID - // Correct fix is to specify in Connect() if target is through a proxy. - // However, in practice you have to connect to something else first anyway to know about the proxy. So setting once only is good enough - if (firstExternalID==UNASSIGNED_SYSTEM_ADDRESS) - { - firstExternalID=bsSystemAddress; - firstExternalID.debugPort=ntohs(firstExternalID.address.addr4.sin_port); - } - - // Send this info down to the game - packet=AllocPacket(byteSize, data, _FILE_AND_LINE_); - packet->bitSize = bitSize; - packet->systemAddress = systemAddress; - packet->systemAddress.systemIndex = remoteSystem->remoteSystemIndex; - packet->guid = remoteSystem->guid; - packet->guid.systemIndex=packet->systemAddress.systemIndex; - AddPacketToProducer(packet); - } - else - { - // Send to game even if already connected. This could happen when connecting to 127.0.0.1 - // Ignore, already connected - // rakFree_Ex(data, _FILE_AND_LINE_ ); - } - } - else if ( (unsigned char) data[ 0 ] == ID_CONNECTED_PONG && byteSize == sizeof(unsigned char)+sizeof(RakNet::Time)*2 ) - { - RakNet::Time sendPingTime, sendPongTime; - - // Copy into the ping times array the current time - the value returned - // First extract the sent ping - RakNet::BitStream inBitStream( (unsigned char *) data, byteSize, false ); - //PingStruct ps; - //ps.Deserialize(psBS); - inBitStream.IgnoreBits(8); - inBitStream.Read(sendPingTime); - inBitStream.Read(sendPongTime); - - OnConnectedPong(sendPingTime,sendPongTime,remoteSystem); - - rakFree_Ex(data, _FILE_AND_LINE_ ); - } - else if ( (unsigned char)data[0] == ID_CONNECTED_PING && byteSize == sizeof(unsigned char)+sizeof(RakNet::Time) ) - { - RakNet::BitStream inBitStream( (unsigned char *) data, byteSize, false ); - inBitStream.IgnoreBits(8); - RakNet::Time sendPingTime; - inBitStream.Read(sendPingTime); - - RakNet::BitStream outBitStream; - outBitStream.Write((MessageID)ID_CONNECTED_PONG); - outBitStream.Write(sendPingTime); - outBitStream.Write(RakNet::GetTime()); - SendImmediate( (char*)outBitStream.GetData(), outBitStream.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, UNRELIABLE, 0, systemAddress, false, false, RakNet::GetTimeUS(), 0 ); - - // Update again immediately after this tick so the ping goes out right away - quitAndDataEvents.SetEvent(); - - rakFree_Ex(data, _FILE_AND_LINE_ ); - } - else if ( (unsigned char) data[ 0 ] == ID_DISCONNECTION_NOTIFICATION ) - { - // We shouldn't close the connection immediately because we need to ack the ID_DISCONNECTION_NOTIFICATION - remoteSystem->connectMode=RemoteSystemStruct::DISCONNECT_ON_NO_ACK; - rakFree_Ex(data, _FILE_AND_LINE_ ); - - // AddPacketToProducer(packet); - } - else if ( (unsigned char)(data)[0] == ID_DETECT_LOST_CONNECTIONS && byteSize == sizeof(unsigned char) ) - { - // Do nothing - rakFree_Ex(data, _FILE_AND_LINE_ ); - } - else if ( (unsigned char)(data)[0] == ID_INVALID_PASSWORD ) - { - if (remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION) - { - packet=AllocPacket(byteSize, data, _FILE_AND_LINE_); - packet->bitSize = bitSize; - packet->systemAddress = systemAddress; - packet->systemAddress.systemIndex = remoteSystem->remoteSystemIndex; - packet->guid = remoteSystem->guid; - packet->guid.systemIndex=packet->systemAddress.systemIndex; - AddPacketToProducer(packet); - - remoteSystem->connectMode=RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY; - } - else - { - rakFree_Ex(data, _FILE_AND_LINE_ ); - } - } - else if ( (unsigned char)(data)[0] == ID_CONNECTION_REQUEST_ACCEPTED ) - { - if (byteSize > sizeof(MessageID)+sizeof(unsigned int)+sizeof(unsigned short)+sizeof(SystemIndex)+sizeof(RakNet::Time)*2) - { - // Make sure this connection accept is from someone we wanted to connect to - bool allowConnection, alreadyConnected; - - if (remoteSystem->connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST || - remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION || - allowConnectionResponseIPMigration) - allowConnection=true; - else - allowConnection=false; - - if (remoteSystem->connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST) - alreadyConnected=true; - else - alreadyConnected=false; - - if ( allowConnection ) - { - SystemAddress externalID; - SystemIndex systemIndex; -// SystemAddress internalID; - - RakNet::BitStream inBitStream((unsigned char *) data, byteSize, false); - inBitStream.IgnoreBits(8); - // inBitStream.Read(remotePort); - inBitStream.Read(externalID); - inBitStream.Read(systemIndex); - for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++) - inBitStream.Read(remoteSystem->theirInternalSystemAddress[i]); - - RakNet::Time sendPingTime, sendPongTime; - inBitStream.Read(sendPingTime); - inBitStream.Read(sendPongTime); - OnConnectedPong(sendPingTime, sendPongTime, remoteSystem); - - // Find a free remote system struct to use - // RakNet::BitStream casBitS(data, byteSize, false); - // ConnectionAcceptStruct cas; - // cas.Deserialize(casBitS); - // systemAddress.GetPort() = remotePort; - - // The remote system told us our external IP, so save it - remoteSystem->myExternalSystemAddress = externalID; - remoteSystem->connectMode=RemoteSystemStruct::CONNECTED; - - // Bug: If A connects to B through R, A's firstExternalID is set to R. If A tries to send to R, sends to loopback because R==firstExternalID - // Correct fix is to specify in Connect() if target is through a proxy. - // However, in practice you have to connect to something else first anyway to know about the proxy. So setting once only is good enough - if (firstExternalID==UNASSIGNED_SYSTEM_ADDRESS) - { - firstExternalID=externalID; - firstExternalID.debugPort=ntohs(firstExternalID.address.addr4.sin_port); - } - - // Send the connection request complete to the game - packet=AllocPacket(byteSize, data, _FILE_AND_LINE_); - packet->bitSize = byteSize * 8; - packet->systemAddress = systemAddress; - packet->systemAddress.systemIndex = ( SystemIndex ) GetIndexFromSystemAddress( systemAddress, true ); - packet->guid = remoteSystem->guid; - packet->guid.systemIndex=packet->systemAddress.systemIndex; - AddPacketToProducer(packet); - - RakNet::BitStream outBitStream; - outBitStream.Write((MessageID)ID_NEW_INCOMING_CONNECTION); - outBitStream.Write(systemAddress); - for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++) - outBitStream.Write(ipList[i]); - outBitStream.Write(sendPongTime); - outBitStream.Write(RakNet::GetTime()); - - SendImmediate( (char*)outBitStream.GetData(), outBitStream.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, RELIABLE_ORDERED, 0, systemAddress, false, false, RakNet::GetTimeUS(), 0 ); - - if (alreadyConnected==false) - { - PingInternal( systemAddress, true, UNRELIABLE ); - } - } - else - { - // Ignore, already connected - rakFree_Ex(data, _FILE_AND_LINE_ ); - } - } - else - { - // Version mismatch error? - RakAssert(0); - rakFree_Ex(data, _FILE_AND_LINE_ ); - } - } - else - { - // What do I do if I get a message from a system, before I am fully connected? - // I can either ignore it or give it to the user - // It seems like giving it to the user is a better option - if ((data[0]>=(MessageID)ID_TIMESTAMP || data[0]==ID_SND_RECEIPT_ACKED || data[0]==ID_SND_RECEIPT_LOSS) && - remoteSystem->isActive - ) - { - packet=AllocPacket(byteSize, data, _FILE_AND_LINE_); - packet->bitSize = bitSize; - packet->systemAddress = systemAddress; - packet->systemAddress.systemIndex = remoteSystem->remoteSystemIndex; - packet->guid = remoteSystem->guid; - packet->guid.systemIndex=packet->systemAddress.systemIndex; - AddPacketToProducer(packet); - } - else - { - rakFree_Ex(data, _FILE_AND_LINE_ ); - } - } - } - - // Does the reliability layer have any more packets waiting for us? - // To be thread safe, this has to be called in the same thread as HandleSocketReceiveFromConnectedPlayer - bitSize = remoteSystem->reliabilityLayer.Receive( &data ); - } - - } - - return true; -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - -void RakPeer::OnRNS2Recv(RNS2RecvStruct *recvStruct) -{ - if (incomingDatagramEventHandler) - { - if (incomingDatagramEventHandler(recvStruct)!=true) - return; - } - - PushBufferedPacket(recvStruct); - quitAndDataEvents.SetEvent(); -} - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - -/* -RAK_THREAD_DECLARATION(RakNet::RecvFromLoop) -{ -#if defined(SN_TARGET_PSP2) - RakPeerAndIndex *rpai = ( RakPeerAndIndex * ) RakThread::GetRealThreadArgument(callGetRealThreadArgument); -#else - RakPeerAndIndex *rpai = ( RakPeerAndIndex * ) arguments; -#endif - RakPeer * rakPeer = rpai->rakPeer; - RakNetSocket *s = rpai->s; - RakNet::OP_DELETE(rpai,_FILE_AND_LINE_); - - rakPeer->isRecvFromLoopThreadActive.Increment(); - - while ( rakPeer->endThreads == false ) - { - if (rakPeer->RunRecvFromOnce(s)==false && - s->GetBlockingSocket()==false) - RakSleep(0); - } - rakPeer->isRecvFromLoopThreadActive.Decrement(); - -#if defined(SN_TARGET_PSP2) - return sceKernelExitDeleteThread(0); -#else - return 0; -#endif -} -*/ - -// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -RAK_THREAD_DECLARATION(RakNet::UpdateNetworkLoop) -{ - - - - RakPeer * rakPeer = ( RakPeer * ) arguments; - - -/* - // 11/15/05 - this is slower than Sleep() -#ifdef _WIN32 -#if (_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400) - // Lets see if these timers give better performance than Sleep - HANDLE timerHandle; - LARGE_INTEGER dueTime; - - if ( rakPeer->threadSleepTimer <= 0 ) - rakPeer->threadSleepTimer = 1; - - // 2nd parameter of false means synchronization timer instead of manual-reset timer - timerHandle = CreateWaitableTimer( NULL, FALSE, 0 ); - - RakAssert( timerHandle ); - - dueTime.QuadPart = -10000 * rakPeer->threadSleepTimer; // 10000 is 1 ms? - - BOOL success = SetWaitableTimer( timerHandle, &dueTime, rakPeer->threadSleepTimer, NULL, NULL, FALSE ); - (void) success; - RakAssert( success ); - -#endif -#endif -*/ - - BitStream updateBitStream( MAXIMUM_MTU_SIZE -#if LIBCAT_SECURITY==1 - + cat::AuthenticatedEncryption::OVERHEAD_BYTES -#endif - ); -// - rakPeer->isMainLoopThreadActive = true; - - while ( rakPeer->endThreads == false ) - { -// #ifdef _DEBUG -// // Sanity check, make sure RunUpdateCycle does not block or not otherwise get called for a long time -// RakNetTime thisCall=RakNet::GetTime(); -// RakAssert(thisCall-lastCall<250); -// lastCall=thisCall; -// #endif - if (rakPeer->userUpdateThreadPtr) - rakPeer->userUpdateThreadPtr(rakPeer, rakPeer->userUpdateThreadData); - - rakPeer->RunUpdateCycle(updateBitStream); - - // Pending sends go out this often, unless quitAndDataEvents is set - rakPeer->quitAndDataEvents.WaitOnEvent(10); - - /* - -// #if ((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)) && -#if defined(USE_WAIT_FOR_MULTIPLE_EVENTS) && defined(_WIN32) - - if (rakPeer->threadSleepTimer>0) - { - WSAEVENT eventArray[256]; - unsigned int i, eventArrayIndex; - for (i=0,eventArrayIndex=0; i < rakPeer->socketList.Size(); i++) - { - if (rakPeer->socketList[i]->recvEvent!=INVALID_HANDLE_VALUE) - { - eventArray[eventArrayIndex]=rakPeer->socketList[i]->recvEvent; - eventArrayIndex++; - if (eventArrayIndex==256) - break; - } - } - WSAWaitForMultipleEvents(eventArrayIndex,(const HANDLE*) &eventArray,FALSE,rakPeer->threadSleepTimer,FALSE); - } - else - { - RakSleep(0); - } - -#else // ((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)) && defined(USE_WAIT_FOR_MULTIPLE_EVENTS) - #pragma message("-- RakNet: Using Sleep(). Uncomment USE_WAIT_FOR_MULTIPLE_EVENTS in RakNetDefines.h if you want to use WaitForSingleObject instead. --") - - RakSleep( rakPeer->threadSleepTimer ); -#endif - */ - } - - rakPeer->isMainLoopThreadActive = false; - - /* -#ifdef _WIN32 -#if (_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400) - CloseHandle(timerHandle); -#endif -#endif - */ - - - - - return 0; - -} - -void RakPeer::CallPluginCallbacks(DataStructures::List &pluginList, Packet *packet) -{ - for (unsigned int i=0; i < pluginList.Size(); i++) - { - switch (packet->data[0]) - { - case ID_DISCONNECTION_NOTIFICATION: - pluginList[i]->OnClosedConnection(packet->systemAddress, packet->guid, LCR_DISCONNECTION_NOTIFICATION); - break; - case ID_CONNECTION_LOST: - pluginList[i]->OnClosedConnection(packet->systemAddress, packet->guid, LCR_CONNECTION_LOST); - break; - case ID_NEW_INCOMING_CONNECTION: - pluginList[i]->OnNewConnection(packet->systemAddress, packet->guid, true); - break; - case ID_CONNECTION_REQUEST_ACCEPTED: - pluginList[i]->OnNewConnection(packet->systemAddress, packet->guid, false); - break; - case ID_CONNECTION_ATTEMPT_FAILED: - pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_CONNECTION_ATTEMPT_FAILED); - break; - case ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY: - pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY); - break; - case ID_OUR_SYSTEM_REQUIRES_SECURITY: - pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_OUR_SYSTEM_REQUIRES_SECURITY); - break; - case ID_PUBLIC_KEY_MISMATCH: - pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_PUBLIC_KEY_MISMATCH); - break; - case ID_ALREADY_CONNECTED: - pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_ALREADY_CONNECTED); - break; - case ID_NO_FREE_INCOMING_CONNECTIONS: - pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_NO_FREE_INCOMING_CONNECTIONS); - break; - case ID_CONNECTION_BANNED: - pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_CONNECTION_BANNED); - break; - case ID_INVALID_PASSWORD: - pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_INVALID_PASSWORD); - break; - case ID_INCOMPATIBLE_PROTOCOL_VERSION: - pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_INCOMPATIBLE_PROTOCOL); - break; - case ID_IP_RECENTLY_CONNECTED: - pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_IP_RECENTLY_CONNECTED); - break; - } - } -} - -void RakPeer::FillIPList(void) -{ - if (ipList[0]!=UNASSIGNED_SYSTEM_ADDRESS) - return; - - // Fill out ipList structure -#if !defined(WINDOWS_STORE_RT) - RakNetSocket2::GetMyIP( ipList ); -#endif - - // Sort the addresses from lowest to highest - int startingIdx = 0; - while (startingIdx < MAXIMUM_NUMBER_OF_INTERNAL_IDS-1 && ipList[startingIdx] != UNASSIGNED_SYSTEM_ADDRESS) - { - int lowestIdx = startingIdx; - for (int curIdx = startingIdx + 1; curIdx < MAXIMUM_NUMBER_OF_INTERNAL_IDS-1 && ipList[curIdx] != UNASSIGNED_SYSTEM_ADDRESS; curIdx++ ) - { - if (ipList[curIdx] < ipList[startingIdx]) - { - lowestIdx = curIdx; - } - } - if (startingIdx != lowestIdx) - { - SystemAddress temp = ipList[startingIdx]; - ipList[startingIdx] = ipList[lowestIdx]; - ipList[lowestIdx] = temp; - } - ++startingIdx; - } -} - - -// #if defined(RMO_NEW_UNDEF_ALLOCATING_QUEUE) -// #pragma pop_macro("new") -// #undef RMO_NEW_UNDEF_ALLOCATING_QUEUE -// #endif - - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +// \file +// + + + +#define CAT_NEUTER_EXPORT /* Neuter dllimport for libcat */ + +#include "RakNetDefines.h" +#include "RakPeer.h" +#include "RakNetTypes.h" + +#ifdef _WIN32 + +#else +#include +#endif + +// #if defined(new) +// #pragma push_macro("new") +// #undef new +// #define RMO_NEW_UNDEF_ALLOCATING_QUEUE +// #endif + +#include +#include // toupper +#include +#include "GetTime.h" +#include "MessageIdentifiers.h" +#include "DS_HuffmanEncodingTree.h" +#include "Rand.h" +#include "PluginInterface2.h" +#include "StringCompressor.h" +#include "StringTable.h" +#include "NetworkIDObject.h" +#include "RakNetTypes.h" +#include "DR_SHA1.h" +#include "RakSleep.h" +#include "RakAssert.h" +#include "RakNetVersion.h" +#include "NetworkIDManager.h" +#include "gettimeofday.h" +#include "SignaledEvent.h" +#include "SuperFastHash.h" +#include "RakAlloca.h" +#include "WSAStartupSingleton.h" + +#ifdef USE_THREADED_SEND +#include "SendToThread.h" +#endif + +#ifdef CAT_AUDIT +#define CAT_AUDIT_PRINTF(...) printf(__VA_ARGS__) +#else +#define CAT_AUDIT_PRINTF(...) +#endif + +namespace RakNet +{ +RAK_THREAD_DECLARATION(UpdateNetworkLoop); +RAK_THREAD_DECLARATION(RecvFromLoop); +RAK_THREAD_DECLARATION(UDTConnect); +} +#define REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE 8 + +#if !defined ( __APPLE__ ) && !defined ( __APPLE_CC__ ) +#include // malloc +#endif + + + +#if defined(_WIN32) +// +#else +/* +#include // Console 2 +#include +extern bool _extern_Console2LoadModules(void); +extern int _extern_Console2GetConnectionStatus(void); +extern int _extern_Console2GetLobbyStatus(void); +//extern bool Console2StartupFluff(unsigned int *); +extern void Console2ShutdownFluff(void); +//extern unsigned int Console2ActivateConnection(unsigned int, void *); +//extern bool Console2BlockOnEstablished(void); +extern void Console2GetIPAndPort(unsigned int, char *, unsigned short *, unsigned int ); +//extern void Console2DeactivateConnection(unsigned int, unsigned int); +*/ +#endif + + +static const int NUM_MTU_SIZES=3; + + + +static const int mtuSizes[NUM_MTU_SIZES]={MAXIMUM_MTU_SIZE, 1200, 576}; + + +// Note to self - if I change this it might affect RECIPIENT_OFFLINE_MESSAGE_INTERVAL in Natpunchthrough.cpp +//static const int MAX_OPEN_CONNECTION_REQUESTS=8; +//static const int TIME_BETWEEN_OPEN_CONNECTION_REQUESTS=500; + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +using namespace RakNet; + +static RakNetRandom rnr; + +/* +struct RakPeerAndIndex +{ + RakNetSocket2 *s; + RakPeer *rakPeer; +}; +*/ + +static const unsigned int MAX_OFFLINE_DATA_LENGTH=400; // I set this because I limit ID_CONNECTION_REQUEST to 512 bytes, and the password is appended to that packet. + +// Used to distinguish between offline messages with data, and messages from the reliability layer +// Should be different than any message that could result from messages from the reliability layer +#if !defined(__GNUC__) +#pragma warning(disable:4309) // 'initializing' : truncation of constant value +#endif +// Make sure highest bit is 0, so isValid in DatagramHeaderFormat is false +static const unsigned char OFFLINE_MESSAGE_DATA_ID[16]={0x00,0xFF,0xFF,0x00,0xFE,0xFE,0xFE,0xFE,0xFD,0xFD,0xFD,0xFD,0x12,0x34,0x56,0x78}; + +struct PacketFollowedByData +{ + Packet p; + unsigned char data[1]; +}; + +Packet *RakPeer::AllocPacket(unsigned dataSize, const char *file, unsigned int line) +{ + // Crashes when dataSize is 4 bytes - not sure why +// unsigned char *data = (unsigned char *) rakMalloc_Ex(sizeof(PacketFollowedByData)+dataSize, file, line); +// Packet *p = &((PacketFollowedByData *)data)->p; +// p->data=((PacketFollowedByData *)data)->data; +// p->length=dataSize; +// p->bitSize=BYTES_TO_BITS(dataSize); +// p->deleteData=false; +// p->guid=UNASSIGNED_RAKNET_GUID; +// return p; + + RakNet::Packet *p; + packetAllocationPoolMutex.Lock(); + p = packetAllocationPool.Allocate(file,line); + packetAllocationPoolMutex.Unlock(); + p = new ((void*)p) Packet; + p->data=(unsigned char*) rakMalloc_Ex(dataSize,file,line); + p->length=dataSize; + p->bitSize=BYTES_TO_BITS(dataSize); + p->deleteData=true; + p->guid=UNASSIGNED_RAKNET_GUID; + p->wasGeneratedLocally=false; + return p; +} + +Packet *RakPeer::AllocPacket(unsigned dataSize, unsigned char *data, const char *file, unsigned int line) +{ + // Packet *p = (Packet *)rakMalloc_Ex(sizeof(Packet), file, line); + RakNet::Packet *p; + packetAllocationPoolMutex.Lock(); + p = packetAllocationPool.Allocate(file,line); + packetAllocationPoolMutex.Unlock(); + p = new ((void*)p) Packet; + RakAssert(p); + p->data=data; + p->length=dataSize; + p->bitSize=BYTES_TO_BITS(dataSize); + p->deleteData=true; + p->guid=UNASSIGNED_RAKNET_GUID; + p->wasGeneratedLocally=false; + return p; +} + +STATIC_FACTORY_DEFINITIONS(RakPeerInterface,RakPeer) + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Constructor +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +RakPeer::RakPeer() +{ +#if LIBCAT_SECURITY==1 + // Encryption and security + CAT_AUDIT_PRINTF("AUDIT: Initializing RakPeer security flags: using_security = false, server_handshake = null, cookie_jar = null\n"); + _using_security = false; + _server_handshake = 0; + _cookie_jar = 0; +#endif + + StringCompressor::AddReference(); + RakNet::StringTable::AddReference(); + WSAStartupSingleton::AddRef(); + + defaultMTUSize = mtuSizes[NUM_MTU_SIZES-1]; + trackFrequencyTable = false; + maximumIncomingConnections = 0; + maximumNumberOfPeers = 0; + //remoteSystemListSize=0; + remoteSystemList = 0; + activeSystemList = 0; + activeSystemListSize=0; + remoteSystemLookup=0; + bytesSentPerSecond = bytesReceivedPerSecond = 0; + endThreads = true; + isMainLoopThreadActive = false; + incomingDatagramEventHandler=0; + + + + + + // isRecvfromThreadActive=false; +#if defined(GET_TIME_SPIKE_LIMIT) && GET_TIME_SPIKE_LIMIT>0 + occasionalPing = true; +#else + occasionalPing = false; +#endif + allowInternalRouting=false; + for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++) + ipList[i]=UNASSIGNED_SYSTEM_ADDRESS; + allowConnectionResponseIPMigration = false; + //incomingPasswordLength=outgoingPasswordLength=0; + incomingPasswordLength=0; + splitMessageProgressInterval=0; + //unreliableTimeout=0; + unreliableTimeout=1000; + maxOutgoingBPS=0; + firstExternalID=UNASSIGNED_SYSTEM_ADDRESS; + myGuid=UNASSIGNED_RAKNET_GUID; + userUpdateThreadPtr=0; + userUpdateThreadData=0; + +#ifdef _DEBUG + // Wait longer to disconnect in debug so I don't get disconnected while tracing + defaultTimeoutTime=30000; +#else + defaultTimeoutTime=10000; +#endif + +#ifdef _DEBUG + _packetloss=0.0; + _minExtraPing=0; + _extraPingVariance=0; +#endif + + bufferedCommands.SetPageSize(sizeof(BufferedCommandStruct)*16); + socketQueryOutput.SetPageSize(sizeof(SocketQueryOutput)*8); + + packetAllocationPoolMutex.Lock(); + packetAllocationPool.SetPageSize(sizeof(DataStructures::MemoryPool::MemoryWithPage)*32); + packetAllocationPoolMutex.Unlock(); + + remoteSystemIndexPool.SetPageSize(sizeof(DataStructures::MemoryPool::MemoryWithPage)*32); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + GenerateGUID(); + + quitAndDataEvents.InitEvent(); + limitConnectionFrequencyFromTheSameIP=false; + ResetSendReceipt(); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Destructor +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +RakPeer::~RakPeer() +{ + Shutdown( 0, 0 ); + + // Free the ban list. + ClearBanList(); + + StringCompressor::RemoveReference(); + RakNet::StringTable::RemoveReference(); + WSAStartupSingleton::Deref(); + + quitAndDataEvents.CloseEvent(); + +#if LIBCAT_SECURITY==1 + // Encryption and security + CAT_AUDIT_PRINTF("AUDIT: Deleting RakPeer security objects, handshake = %x, cookie jar = %x\n", _server_handshake, _cookie_jar); + if (_server_handshake) RakNet::OP_DELETE(_server_handshake,_FILE_AND_LINE_); + if (_cookie_jar) RakNet::OP_DELETE(_cookie_jar,_FILE_AND_LINE_); +#endif + + + + + + + + + + + + + + + + +// for (unsigned int i=0; i < pluginListTS.Size(); i++) +// pluginListTS[i]->SetRakPeerInterface(0); +// for (unsigned int i=0; i < pluginListNTS.Size(); i++) +// pluginListNTS[i]->SetRakPeerInterface(0); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// \brief Starts the network threads, opens the listen port. +// You must call this before calling Connect(). +// Multiple calls while already active are ignored. To call this function again with different settings, you must first call Shutdown(). +// \note Call SetMaximumIncomingConnections if you want to accept incoming connections +// \param[in] maxConnections The maximum number of connections between this instance of RakPeer and another instance of RakPeer. Required so the network can preallocate and for thread safety. A pure client would set this to 1. A pure server would set it to the number of allowed clients.- A hybrid would set it to the sum of both types of connections +// \param[in] localPort The port to listen for connections on. +// \param[in] _threadSleepTimer How many ms to Sleep each internal update cycle. With new congestion control, the best results will be obtained by passing 10. +// \param[in] socketDescriptors An array of SocketDescriptor structures to force RakNet to listen on a particular IP address or port (or both). Each SocketDescriptor will represent one unique socket. Do not pass redundant structures. To listen on a specific port, you can pass &socketDescriptor, 1SocketDescriptor(myPort,0); such as for a server. For a client, it is usually OK to just pass SocketDescriptor(); +// \param[in] socketDescriptorCount The size of the \a socketDescriptors array. Pass 1 if you are not sure what to pass. +// \return False on failure (can't create socket or thread), true on success. +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +StartupResult RakPeer::Startup( unsigned int maxConnections, SocketDescriptor *socketDescriptors, unsigned socketDescriptorCount, int threadPriority ) +{ + if (IsActive()) + return RAKNET_ALREADY_STARTED; + + // If getting the guid failed in the constructor, try again + if (myGuid.g==0) + { + GenerateGUID(); + if (myGuid.g==0) + return COULD_NOT_GENERATE_GUID; + } + + if (threadPriority==-99999) + { + + +#if defined(_WIN32) + threadPriority=0; + + + + +#else + threadPriority=1000; +#endif + } + + + FillIPList(); + + if (myGuid==UNASSIGNED_RAKNET_GUID) + { + rnr.SeedMT( GenerateSeedFromGuid() ); + } + + //RakPeerAndIndex rpai[32]; + //RakAssert(socketDescriptorCount<32); + + RakAssert(socketDescriptors && socketDescriptorCount>=1); + + if (socketDescriptors==0 || socketDescriptorCount<1) + return INVALID_SOCKET_DESCRIPTORS; + + //unsigned short localPort; + //localPort=socketDescriptors[0].port; + + RakAssert( maxConnections > 0 ); + + if ( maxConnections <= 0 ) + return INVALID_MAX_CONNECTIONS; + + DerefAllSockets(); + + + int i; + // Go through all socket descriptors and precreate sockets on the specified addresses + for (i=0; iSetUserConnectionSocketIndex(i); + #if defined(__native_client__) + NativeClientBindParameters ncbp; + RNS2_NativeClient * nativeClientSocket = (RNS2_NativeClient*) r2; + ncbp.eventHandler=this; + ncbp.forceHostAddress=(char*) socketDescriptors[i].hostAddress; + ncbp.is_ipv6=socketDescriptors[i].socketFamily==AF_INET6; + ncbp.nativeClientInstance=socketDescriptors[i].chromeInstance; + ncbp.port=socketDescriptors[i].port; + nativeClientSocket->Bind(&ncbp, _FILE_AND_LINE_); + #elif defined(WINDOWS_STORE_RT) + RNS2BindResult br; + ((RNS2_WindowsStore8*) r2)->SetRecvEventHandler(this); + br = ((RNS2_WindowsStore8*) r2)->Bind(ref new Platform::String()); + if (br!=BR_SUCCESS) + { + RakNetSocket2Allocator::DeallocRNS2(r2); + DerefAllSockets(); + return SOCKET_FAILED_TO_BIND; + } + #else + if (r2->IsBerkleySocket()) + { + RNS2_BerkleyBindParameters bbp; + bbp.port=socketDescriptors[i].port; + bbp.hostAddress=(char*) socketDescriptors[i].hostAddress; + bbp.addressFamily=socketDescriptors[i].socketFamily; + bbp.type=SOCK_DGRAM; + bbp.protocol=socketDescriptors[i].extraSocketOptions; + bbp.nonBlockingSocket=false; + bbp.setBroadcast=true; + bbp.setIPHdrIncl=false; + bbp.doNotFragment=false; + bbp.pollingThreadPriority=threadPriority; + bbp.eventHandler=this; + bbp.remotePortRakNetWasStartedOn_PS3_PS4_PSP2=socketDescriptors[i].remotePortRakNetWasStartedOn_PS3_PSP2; + RNS2BindResult br = ((RNS2_Berkley*) r2)->Bind(&bbp, _FILE_AND_LINE_); + + if ( + #if RAKNET_SUPPORT_IPV6==0 + socketDescriptors[i].socketFamily!=AF_INET || + #endif + br==BR_REQUIRES_RAKNET_SUPPORT_IPV6_DEFINE) + { + RakNetSocket2Allocator::DeallocRNS2(r2); + DerefAllSockets(); + return SOCKET_FAMILY_NOT_SUPPORTED; + } + else if (br==BR_FAILED_TO_BIND_SOCKET) + { + RakNetSocket2Allocator::DeallocRNS2(r2); + DerefAllSockets(); + return SOCKET_PORT_ALREADY_IN_USE; + } + else if (br==BR_FAILED_SEND_TEST) + { + RakNetSocket2Allocator::DeallocRNS2(r2); + DerefAllSockets(); + return SOCKET_FAILED_TEST_SEND; + } + else + { + RakAssert(br==BR_SUCCESS); + } + } + else + { + RakAssert("TODO" && 0); + } + #endif +/* + + SystemAddress saOut; + SocketLayer::GetSystemAddress( rns, &saOut ); + rns->SetBoundAddress(saOut); + rns->SetRemotePortRakNetWasStartedOn(socketDescriptors[i].remotePortRakNetWasStartedOn_PS3_PSP2); + rns->SetChromeInstance(socketDescriptors[i].chromeInstance); + rns->SetExtraSocketOptions(socketDescriptors[i].extraSocketOptions); + rns->SetUserConnectionSocketIndex(i); + rns->SetBlockingSocket(socketDescriptors[i].blockingSocket); + +#if RAKNET_SUPPORT_IPV6==0 + if (addrToBind==0) + rns->SetBoundAddressToLoopback(4); +#endif + + // GetBoundAddress is asynch, which isn't supported by this architecture +#if !defined(__native_client__) + int zero=0; + if (SocketLayer::SendTo(rns, (const char*) &zero,4, rns->GetBoundAddress(), _FILE_AND_LINE_)!=0) + { + DerefAllSockets(); + return SOCKET_FAILED_TEST_SEND; + } +#endif + */ + + socketList.Push(r2, _FILE_AND_LINE_ ); + + } + +#if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) + for (i=0; iIsBerkleySocket()) + ((RNS2_Berkley*) socketList[i])->CreateRecvPollingThread(threadPriority); + } +#endif + +// #if !defined(_XBOX) && !defined(_XBOX_720_COMPILE_AS_WINDOWS) && !defined(X360) + for (i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++) + { + if (ipList[i]==UNASSIGNED_SYSTEM_ADDRESS) + break; +#if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) + if (socketList[0]->IsBerkleySocket()) + { + unsigned short port = ((RNS2_Berkley*)socketList[0])->GetBoundAddress().GetPort(); + ipList[i].SetPortHostOrder(port); + + } +#endif +// ipList[i].SetPort(((RNS2_360_720*)socketList[0])->GetBoundAddress().GetPort()); + } +// #endif + + + if ( maximumNumberOfPeers == 0 ) + { + // Don't allow more incoming connections than we have peers. + if ( maximumIncomingConnections > maxConnections ) + maximumIncomingConnections = maxConnections; + + maximumNumberOfPeers = maxConnections; + // 04/19/2006 - Don't overallocate because I'm no longer allowing connected pings. + // The disconnects are not consistently processed and the process was sloppy and complicated. + // Allocate 10% extra to handle new connections from players trying to connect when the server is full + //remoteSystemListSize = maxConnections;// * 11 / 10 + 1; + + // remoteSystemList in Single thread + //remoteSystemList = RakNet::OP_NEW( _FILE_AND_LINE_ ); + remoteSystemList = RakNet::OP_NEW_ARRAY(maximumNumberOfPeers, _FILE_AND_LINE_ ); + + remoteSystemLookup = RakNet::OP_NEW_ARRAY((unsigned int) maximumNumberOfPeers * REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE, _FILE_AND_LINE_ ); + + activeSystemList = RakNet::OP_NEW_ARRAY(maximumNumberOfPeers, _FILE_AND_LINE_ ); + + for ( i = 0; i < maximumNumberOfPeers; i++ ) + //for ( i = 0; i < remoteSystemListSize; i++ ) + { + // remoteSystemList in Single thread + remoteSystemList[ i ].isActive = false; + remoteSystemList[ i ].systemAddress = UNASSIGNED_SYSTEM_ADDRESS; + remoteSystemList[ i ].guid = UNASSIGNED_RAKNET_GUID; + remoteSystemList[ i ].myExternalSystemAddress = UNASSIGNED_SYSTEM_ADDRESS; + remoteSystemList[ i ].connectMode=RemoteSystemStruct::NO_ACTION; + remoteSystemList[ i ].MTUSize = defaultMTUSize; + remoteSystemList[ i ].remoteSystemIndex = (SystemIndex) i; +#ifdef _DEBUG + remoteSystemList[ i ].reliabilityLayer.ApplyNetworkSimulator(_packetloss, _minExtraPing, _extraPingVariance); +#endif + + // All entries in activeSystemList have valid pointers all the time. + activeSystemList[ i ] = &remoteSystemList[ i ]; + } + + for (unsigned int i=0; i < (unsigned int) maximumNumberOfPeers*REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE; i++) + { + remoteSystemLookup[i]=0; + } + } + + // For histogram statistics + // nextReadBytesTime=0; + // lastSentBytes=lastReceivedBytes=0; + + if ( endThreads ) + { + updateCycleIsRunning = false; + endThreads = false; + firstExternalID=UNASSIGNED_SYSTEM_ADDRESS; + + ClearBufferedCommands(); + ClearBufferedPackets(); + ClearSocketQueryOutput(); + + if ( isMainLoopThreadActive == false ) + { +#if RAKPEER_USER_THREADED!=1 + + int errorCode; + + + + + + + + errorCode = RakNet::RakThread::Create(UpdateNetworkLoop, this, threadPriority); + + + if ( errorCode != 0 ) + { + Shutdown( 0, 0 ); + return FAILED_TO_CREATE_NETWORK_THREAD; + } +// RakAssert(isRecvFromLoopThreadActive.GetValue()==0); +#endif // RAKPEER_USER_THREADED!=1 + + /* + for (i=0; i(_FILE_AND_LINE_); + rpai->s=socketList[i]; + rpai->rakPeer=this; + +#if RAKPEER_USER_THREADED!=1 + + #if defined(SN_TARGET_PSP2) + sprintf(threadName, "RecvFromLoop_%p", this); + //errorCode = RakNet::RakThread::Create(RecvFromLoop, rpai, threadPriority, threadName, 1+i, runtime); + errorCode = RakNet::RakThread::Create(RecvFromLoop, rpai, threadPriority, threadName, 1024*1); + #else + errorCode = RakNet::RakThread::Create(RecvFromLoop, rpai, threadPriority); + #endif + + if ( errorCode != 0 ) + { + Shutdown( 0, 0 ); + return FAILED_TO_CREATE_NETWORK_THREAD; + } +#endif // RAKPEER_USER_THREADED!=1 + } + */ + + + /* +#if RAKPEER_USER_THREADED!=1 + + while ( isRecvFromLoopThreadActive.GetValue() < (uint32_t) socketDescriptorCount ) + RakSleep(10); + #endif // RAKPEER_USER_THREADED!=1 + */ + + } + +#if RAKPEER_USER_THREADED!=1 + // Wait for the threads to activate. When they are active they will set these variables to true + while ( isMainLoopThreadActive == false ) + RakSleep(10); +#endif // RAKPEER_USER_THREADED!=1 + } + + for (i=0; i < pluginListTS.Size(); i++) + { + pluginListTS[i]->OnRakPeerStartup(); + } + + for (i=0; i < pluginListNTS.Size(); i++) + { + pluginListNTS[i]->OnRakPeerStartup(); + } + +#ifdef USE_THREADED_SEND + RakNet::SendToThread::AddRef(); +#endif + + return RAKNET_STARTED; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Must be called while offline +// +// If you accept connections, you must call this or else security will not be enabled for incoming connections. +// +// This feature requires more round trips, bandwidth, and CPU time for the connection handshake +// x64 builds require under 25% of the CPU time of other builds +// +// See the Encryption sample for example usage +// +// Parameters: +// publicKey = A pointer to the public key for accepting new connections +// privateKey = A pointer to the private key for accepting new connections +// If the private keys are 0, then a new key will be generated when this function is called +// bRequireClientKey: Should be set to false for most servers. Allows the server to accept a public key from connecting clients as a proof of identity but eats twice as much CPU time as a normal connection +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +bool RakPeer::InitializeSecurity(const char *public_key, const char *private_key, bool bRequireClientKey) +{ +#if LIBCAT_SECURITY==1 + if ( endThreads == false ) + return false; + + // Copy client public key requirement flag + _require_client_public_key = bRequireClientKey; + + if (_server_handshake) + { + CAT_AUDIT_PRINTF("AUDIT: Deleting old server_handshake %x\n", _server_handshake); + RakNet::OP_DELETE(_server_handshake,_FILE_AND_LINE_); + } + if (_cookie_jar) + { + CAT_AUDIT_PRINTF("AUDIT: Deleting old cookie jar %x\n", _cookie_jar); + RakNet::OP_DELETE(_cookie_jar,_FILE_AND_LINE_); + } + + _server_handshake = RakNet::OP_NEW(_FILE_AND_LINE_); + _cookie_jar = RakNet::OP_NEW(_FILE_AND_LINE_); + + CAT_AUDIT_PRINTF("AUDIT: Created new server_handshake %x\n", _server_handshake); + CAT_AUDIT_PRINTF("AUDIT: Created new cookie jar %x\n", _cookie_jar); + CAT_AUDIT_PRINTF("AUDIT: Running _server_handshake->Initialize()\n"); + + if (_server_handshake->Initialize(public_key, private_key)) + { + CAT_AUDIT_PRINTF("AUDIT: Successfully initialized, filling cookie jar with goodies, storing public key and setting using security flag to true\n"); + + _server_handshake->FillCookieJar(_cookie_jar); + + memcpy(my_public_key, public_key, sizeof(my_public_key)); + + _using_security = true; + return true; + } + + CAT_AUDIT_PRINTF("AUDIT: Failure to initialize so deleting server handshake and cookie jar; also setting using_security flag = false\n"); + + RakNet::OP_DELETE(_server_handshake,_FILE_AND_LINE_); + _server_handshake=0; + RakNet::OP_DELETE(_cookie_jar,_FILE_AND_LINE_); + _cookie_jar=0; + _using_security = false; + return false; +#else + (void) public_key; + (void) private_key; + (void) bRequireClientKey; + + return false; +#endif +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description +// Must be called while offline +// Disables security for incoming connections. +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::DisableSecurity( void ) +{ +#if LIBCAT_SECURITY==1 + CAT_AUDIT_PRINTF("AUDIT: DisableSecurity() called, so deleting _server_handshake %x and cookie_jar %x\n", _server_handshake, _cookie_jar); + RakNet::OP_DELETE(_server_handshake,_FILE_AND_LINE_); + _server_handshake=0; + RakNet::OP_DELETE(_cookie_jar,_FILE_AND_LINE_); + _cookie_jar=0; + + _using_security = false; +#endif +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::AddToSecurityExceptionList(const char *ip) +{ + securityExceptionMutex.Lock(); + securityExceptionList.Insert(RakString(ip), _FILE_AND_LINE_); + securityExceptionMutex.Unlock(); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::RemoveFromSecurityExceptionList(const char *ip) +{ + if (securityExceptionList.Size()==0) + return; + + if (ip==0) + { + securityExceptionMutex.Lock(); + securityExceptionList.Clear(false, _FILE_AND_LINE_); + securityExceptionMutex.Unlock(); + } + else + { + unsigned i=0; + securityExceptionMutex.Lock(); + while (i < securityExceptionList.Size()) + { + if (securityExceptionList[i].IPAddressMatch(ip)) + { + securityExceptionList[i]=securityExceptionList[securityExceptionList.Size()-1]; + securityExceptionList.RemoveAtIndex(securityExceptionList.Size()-1); + } + else + i++; + } + securityExceptionMutex.Unlock(); + } +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +bool RakPeer::IsInSecurityExceptionList(const char *ip) +{ + if (securityExceptionList.Size()==0) + return false; + + unsigned i=0; + securityExceptionMutex.Lock(); + for (; i < securityExceptionList.Size(); i++) + { + if (securityExceptionList[i].IPAddressMatch(ip)) + { + securityExceptionMutex.Unlock(); + return true; + } + } + securityExceptionMutex.Unlock(); + return false; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Sets how many incoming connections are allowed. If this is less than the number of players currently connected, no +// more players will be allowed to connect. If this is greater than the maximum number of peers allowed, it will be reduced +// to the maximum number of peers allowed. Defaults to 0. +// +// Parameters: +// numberAllowed - Maximum number of incoming connections allowed. +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::SetMaximumIncomingConnections( unsigned short numberAllowed ) +{ + maximumIncomingConnections = numberAllowed; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Returns the maximum number of incoming connections, which is always <= maxConnections +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +unsigned int RakPeer::GetMaximumIncomingConnections( void ) const +{ + return maximumIncomingConnections; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Returns how many open connections there are at this time +// \return the number of open connections +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +unsigned short RakPeer::NumberOfConnections(void) const +{ + DataStructures::List addresses; + DataStructures::List guids; + GetSystemList(addresses, guids); + return (unsigned short) addresses.Size(); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Sets the password incoming connections must match in the call to Connect (defaults to none) +// Pass 0 to passwordData to specify no password +// +// Parameters: +// passwordData: A data block that incoming connections must match. This can be just a password, or can be a stream of data. +// - Specify 0 for no password data +// passwordDataLength: The length in bytes of passwordData +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::SetIncomingPassword( const char* passwordData, int passwordDataLength ) +{ + //if (passwordDataLength > MAX_OFFLINE_DATA_LENGTH) + // passwordDataLength=MAX_OFFLINE_DATA_LENGTH; + + if (passwordDataLength > 255) + passwordDataLength=255; + + if (passwordData==0) + passwordDataLength=0; + + // Not threadsafe but it's not important enough to lock. Who is going to change the password a lot during runtime? + // It won't overflow at least because incomingPasswordLength is an unsigned char + if (passwordDataLength>0) + memcpy(incomingPassword, passwordData, passwordDataLength); + incomingPasswordLength=(unsigned char)passwordDataLength; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::GetIncomingPassword( char* passwordData, int *passwordDataLength ) +{ + if (passwordData==0) + { + *passwordDataLength=incomingPasswordLength; + return; + } + + if (*passwordDataLength > incomingPasswordLength) + *passwordDataLength=incomingPasswordLength; + + if (*passwordDataLength>0) + memcpy(passwordData, incomingPassword, *passwordDataLength); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Call this to connect to the specified host (ip or domain name) and server port. +// Calling Connect and not calling SetMaximumIncomingConnections acts as a dedicated client. Calling both acts as a true peer. +// This is a non-blocking connection. You know the connection is successful when IsConnected() returns true +// or receive gets a packet with the type identifier ID_CONNECTION_REQUEST_ACCEPTED. If the connection is not +// successful, such as rejected connection or no response then neither of these things will happen. +// Requires that you first call Initialize +// +// Parameters: +// host: Either a dotted IP address or a domain name +// remotePort: Which port to connect to on the remote machine. +// passwordData: A data block that must match the data block on the server. This can be just a password, or can be a stream of data +// passwordDataLength: The length in bytes of passwordData +// +// Returns: +// True on successful initiation. False on incorrect parameters, internal error, or too many existing peers +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +ConnectionAttemptResult RakPeer::Connect( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNet::TimeMS timeoutTime ) +{ + // If endThreads is true here you didn't call Startup() first. + if ( host == 0 || endThreads || connectionSocketIndex>=socketList.Size() ) + return INVALID_PARAMETER; + + RakAssert(remotePort!=0); + + connectionSocketIndex=GetRakNetSocketFromUserConnectionSocketIndex(connectionSocketIndex); + + if (passwordDataLength>255) + passwordDataLength=255; + + if (passwordData==0) + passwordDataLength=0; + + // Not threadsafe but it's not important enough to lock. Who is going to change the password a lot during runtime? + // It won't overflow at least because outgoingPasswordLength is an unsigned char +// if (passwordDataLength>0) +// memcpy(outgoingPassword, passwordData, passwordDataLength); +// outgoingPasswordLength=(unsigned char) passwordDataLength; + + // 04/02/09 - Can't remember why I disabled connecting to self, but it seems to work + // Connecting to ourselves in the same instance of the program? +// if ( ( strcmp( host, "127.0.0.1" ) == 0 || strcmp( host, "0.0.0.0" ) == 0 ) && remotePort == mySystemAddress[0].port ) +// return false; + + return SendConnectionRequest( host, remotePort, passwordData, passwordDataLength, publicKey, connectionSocketIndex, 0, sendConnectionAttemptCount, timeBetweenSendConnectionAttemptsMS, timeoutTime); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +ConnectionAttemptResult RakPeer::ConnectWithSocket(const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, RakNetSocket2* socket, PublicKey *publicKey, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNet::TimeMS timeoutTime) +{ + if ( host == 0 || endThreads || socket == 0 ) + return INVALID_PARAMETER; + + if (passwordDataLength>255) + passwordDataLength=255; + + if (passwordData==0) + passwordDataLength=0; + + return SendConnectionRequest( host, remotePort, passwordData, passwordDataLength, publicKey, 0, 0, sendConnectionAttemptCount, timeBetweenSendConnectionAttemptsMS, timeoutTime, socket ); + +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Stops the network threads and close all connections. Multiple calls are ok. +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::Shutdown( unsigned int blockDuration, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority ) +{ + unsigned i,j; + bool anyActive; + RakNet::TimeMS startWaitingTime; +// SystemAddress systemAddress; + RakNet::TimeMS time; + //unsigned short systemListSize = remoteSystemListSize; // This is done for threading reasons + unsigned int systemListSize = maximumNumberOfPeers; + + if ( blockDuration > 0 ) + { + for ( i = 0; i < systemListSize; i++ ) + { + // remoteSystemList in user thread + if (remoteSystemList[i].isActive) + NotifyAndFlagForShutdown(remoteSystemList[i].systemAddress, false, orderingChannel, disconnectionNotificationPriority); + } + + time = RakNet::GetTimeMS(); + startWaitingTime = time; + while ( time - startWaitingTime < blockDuration ) + { + anyActive=false; + for (j=0; j < systemListSize; j++) + { + // remoteSystemList in user thread + if (remoteSystemList[j].isActive) + { + anyActive=true; + break; + } + } + + // If this system is out of packets to send, then stop waiting + if ( anyActive==false ) + break; + + // This will probably cause the update thread to run which will probably + // send the disconnection notification + + RakSleep(15); + time = RakNet::GetTimeMS(); + } + } + for (i=0; i < pluginListTS.Size(); i++) + { + pluginListTS[i]->OnRakPeerShutdown(); + } + for (i=0; i < pluginListNTS.Size(); i++) + { + pluginListNTS[i]->OnRakPeerShutdown(); + } + + activeSystemListSize=0; + + quitAndDataEvents.SetEvent(); + + endThreads = true; + +// RakNet::TimeMS timeout; +#if RAKPEER_USER_THREADED!=1 + +#if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) + for (i=0; i < socketList.Size(); i++) + { + if (socketList[i]->IsBerkleySocket()) + { + ((RNS2_Berkley *)socketList[i])->SignalStopRecvPollingThread(); + } + } +#endif + + /* + // Get recvfrom to unblock + for (i=0; i < socketList.Size(); i++) + { + if (SocketLayer::SendTo(socketList[i], (const char*) &i,1,socketList[i]->GetBoundAddress(), _FILE_AND_LINE_)!=0) + break; + } + */ + + while ( isMainLoopThreadActive ) + { + endThreads = true; + RakSleep(15); + } + + /* + timeout = RakNet::GetTimeMS()+1000; + while ( isRecvFromLoopThreadActive.GetValue()>0 && RakNet::GetTimeMS()GetBoundAddress(), _FILE_AND_LINE_); + } + + RakSleep(30); + } + */ + +#if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) + for (i=0; i < socketList.Size(); i++) + { + if (socketList[i]->IsBerkleySocket()) + { + ((RNS2_Berkley *)socketList[i])->BlockOnStopRecvPollingThread(); + } + } +#endif + + +#endif // RAKPEER_USER_THREADED!=1 + +// char c=0; +// unsigned int socketIndex; + // remoteSystemList in Single thread + for ( i = 0; i < systemListSize; i++ ) + { + // Reserve this reliability layer for ourselves + remoteSystemList[ i ].isActive = false; + + // Remove any remaining packets + RakAssert(remoteSystemList[ i ].MTUSize <= MAXIMUM_MTU_SIZE); + remoteSystemList[ i ].reliabilityLayer.Reset(false, remoteSystemList[ i ].MTUSize, false); + remoteSystemList[ i ].rakNetSocket = 0; + } + + + // Setting maximumNumberOfPeers to 0 allows remoteSystemList to be reallocated in Initialize. + // Setting remoteSystemListSize prevents threads from accessing the reliability layer + maximumNumberOfPeers = 0; + //remoteSystemListSize = 0; + + // Free any packets the user didn't deallocate + packetReturnMutex.Lock(); + for (i=0; i < packetReturnQueue.Size(); i++) + DeallocatePacket(packetReturnQueue[i]); + packetReturnQueue.Clear(_FILE_AND_LINE_); + packetReturnMutex.Unlock(); + packetAllocationPoolMutex.Lock(); + packetAllocationPool.Clear(_FILE_AND_LINE_); + packetAllocationPoolMutex.Unlock(); + + /* + if (isRecvFromLoopThreadActive.GetValue()>0) + { + timeout = RakNet::GetTimeMS()+1000; + while ( isRecvFromLoopThreadActive.GetValue()>0 && RakNet::GetTimeMS() addresses; + DataStructures::List guids; + GetSystemList(addresses, guids); + if (remoteSystems) + { + unsigned short i; + for (i=0; i < *numberOfSystems && i < addresses.Size(); i++) + remoteSystems[i]=addresses[i]; + *numberOfSystems=i; + } + else + { + *numberOfSystems=(unsigned short) addresses.Size(); + } + return true; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint32_t RakPeer::GetNextSendReceipt(void) +{ + sendReceiptSerialMutex.Lock(); + uint32_t retVal = sendReceiptSerial; + sendReceiptSerialMutex.Unlock(); + return retVal; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint32_t RakPeer::IncrementNextSendReceipt(void) +{ + sendReceiptSerialMutex.Lock(); + uint32_t returned = sendReceiptSerial; + if (++sendReceiptSerial==0) + sendReceiptSerial=1; + sendReceiptSerialMutex.Unlock(); + return returned; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Sends a block of data to the specified system that you are connected to. +// This function only works while the client is connected (Use the Connect function). +// The first byte should be a message identifier starting at ID_USER_PACKET_ENUM +// +// Parameters: +// data: The block of data to send +// length: The size in bytes of the data to send +// bitStream: The bitstream to send +// priority: What priority level to send on. +// reliability: How reliability to send this data +// orderingChannel: When using ordered or sequenced packets, what channel to order these on. +// - Packets are only ordered relative to other packets on the same stream +// systemAddress: Who to send this packet to, or in the case of broadcasting who not to send it to. Use UNASSIGNED_SYSTEM_ADDRESS to specify none +// broadcast: True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. +// Returns: +// \return 0 on bad input. Otherwise a number that identifies this message. If \a reliability is a type that returns a receipt, on a later call to Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS with bytes 1-4 inclusive containing this number +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint32_t RakPeer::Send( const char *data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber ) +{ +#ifdef _DEBUG + RakAssert( data && length > 0 ); +#endif + RakAssert( !( reliability >= NUMBER_OF_RELIABILITIES || reliability < 0 ) ); + RakAssert( !( priority > NUMBER_OF_PRIORITIES || priority < 0 ) ); + RakAssert( !( orderingChannel >= NUMBER_OF_ORDERED_STREAMS ) ); + + if ( data == 0 || length < 0 ) + return 0; + + if ( remoteSystemList == 0 || endThreads == true ) + return 0; + + if ( broadcast == false && systemIdentifier.IsUndefined()) + return 0; + + uint32_t usedSendReceipt; + if (forceReceiptNumber!=0) + usedSendReceipt=forceReceiptNumber; + else + usedSendReceipt=IncrementNextSendReceipt(); + + if (broadcast==false && IsLoopbackAddress(systemIdentifier,true)) + { + SendLoopback(data,length); + + if (reliability>=UNRELIABLE_WITH_ACK_RECEIPT) + { + char buff[5]; + buff[0]=ID_SND_RECEIPT_ACKED; + sendReceiptSerialMutex.Lock(); + memcpy(buff+1, &sendReceiptSerial, 4); + sendReceiptSerialMutex.Unlock(); + SendLoopback( buff, 5 ); + } + + return usedSendReceipt; + } + + SendBuffered(data, length*8, priority, reliability, orderingChannel, systemIdentifier, broadcast, RemoteSystemStruct::NO_ACTION, usedSendReceipt); + + return usedSendReceipt; +} + +void RakPeer::SendLoopback( const char *data, const int length ) +{ + if ( data == 0 || length < 0 ) + return; + + Packet *packet = AllocPacket(length, _FILE_AND_LINE_); + memcpy(packet->data, data, length); + packet->systemAddress = GetLoopbackAddress(); + packet->guid=myGuid; + PushBackPacket(packet, false); +} + +uint32_t RakPeer::Send( const RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber ) +{ +#ifdef _DEBUG + RakAssert( bitStream->GetNumberOfBytesUsed() > 0 ); +#endif + + RakAssert( !( reliability >= NUMBER_OF_RELIABILITIES || reliability < 0 ) ); + RakAssert( !( priority > NUMBER_OF_PRIORITIES || priority < 0 ) ); + RakAssert( !( orderingChannel >= NUMBER_OF_ORDERED_STREAMS ) ); + + if ( bitStream->GetNumberOfBytesUsed() == 0 ) + return 0; + + if ( remoteSystemList == 0 || endThreads == true ) + return 0; + + if ( broadcast == false && systemIdentifier.IsUndefined() ) + return 0; + + uint32_t usedSendReceipt; + if (forceReceiptNumber!=0) + usedSendReceipt=forceReceiptNumber; + else + usedSendReceipt=IncrementNextSendReceipt(); + + if (broadcast==false && IsLoopbackAddress(systemIdentifier,true)) + { + SendLoopback((const char*) bitStream->GetData(),bitStream->GetNumberOfBytesUsed()); + if (reliability>=UNRELIABLE_WITH_ACK_RECEIPT) + { + char buff[5]; + buff[0]=ID_SND_RECEIPT_ACKED; + sendReceiptSerialMutex.Lock(); + memcpy(buff+1, &sendReceiptSerial,4); + sendReceiptSerialMutex.Unlock(); + SendLoopback( buff, 5 ); + } + return usedSendReceipt; + } + + // Sends need to be buffered and processed in the update thread because the systemAddress associated with the reliability layer can change, + // from that thread, resulting in a send to the wrong player! While I could mutex the systemAddress, that is much slower than doing this + SendBuffered((const char*)bitStream->GetData(), bitStream->GetNumberOfBitsUsed(), priority, reliability, orderingChannel, systemIdentifier, broadcast, RemoteSystemStruct::NO_ACTION, usedSendReceipt); + + + return usedSendReceipt; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Sends multiple blocks of data, concatenating them automatically. +// +// This is equivalent to: +// RakNet::BitStream bs; +// bs.WriteAlignedBytes(block1, blockLength1); +// bs.WriteAlignedBytes(block2, blockLength2); +// bs.WriteAlignedBytes(block3, blockLength3); +// Send(&bs, ...) +// +// This function only works while connected +// \param[in] data An array of pointers to blocks of data +// \param[in] lengths An array of integers indicating the length of each block of data +// \param[in] numParameters Length of the arrays data and lengths +// \param[in] priority What priority level to send on. See PacketPriority.h +// \param[in] reliability How reliability to send this data. See PacketPriority.h +// \param[in] orderingChannel When using ordered or sequenced messages, what channel to order these on. Messages are only ordered relative to other messages on the same stream +// \param[in] systemIdentifier Who to send this packet to, or in the case of broadcasting who not to send it to. Pass either a SystemAddress structure or a RakNetGUID structure. Use UNASSIGNED_SYSTEM_ADDRESS or to specify none +// \param[in] broadcast True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. +// \return False if we are not connected to the specified recipient. True otherwise +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint32_t RakPeer::SendList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber ) +{ +#ifdef _DEBUG + RakAssert( data ); +#endif + + if ( data == 0 || lengths == 0 ) + return 0; + + if ( remoteSystemList == 0 || endThreads == true ) + return 0; + + if (numParameters==0) + return 0; + + if (lengths==0) + return 0; + + if ( broadcast == false && systemIdentifier.IsUndefined() ) + return 0; + + uint32_t usedSendReceipt; + if (forceReceiptNumber!=0) + usedSendReceipt=forceReceiptNumber; + else + usedSendReceipt=IncrementNextSendReceipt(); + + SendBufferedList(data, lengths, numParameters, priority, reliability, orderingChannel, systemIdentifier, broadcast, RemoteSystemStruct::NO_ACTION, usedSendReceipt); + + return usedSendReceipt; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Gets a packet from the incoming packet queue. Use DeallocatePacket to deallocate the packet after you are done with it. +// Check the Packet struct at the top of CoreNetworkStructures.h for the format of the struct +// +// Returns: +// 0 if no packets are waiting to be handled, otherwise an allocated packet +// If the client is not active this will also return 0, as all waiting packets are flushed when the client is Disconnected +// This also updates all memory blocks associated with synchronized memory and distributed objects +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#ifdef _MSC_VER +#pragma warning( disable : 4701 ) // warning C4701: local variable may be used without having been initialized +#endif +Packet* RakPeer::Receive( void ) +{ + if ( !( IsActive() ) ) + return 0; + + RakNet::Packet *packet; +// Packet **threadPacket; + PluginReceiveResult pluginResult; + + int offset; + unsigned int i; + + // User should call RunUpdateCycle and RunRecvFromOnce to do this commented code + /* +#if RAKPEER_SINGLE_THREADED==1 + RakPeer::RecvFromStruct *recvFromStruct; + for (i=0; i < socketList.Size(); i++) + { + while (1) + { + recvFromStruct=bufferedPackets.Allocate( _FILE_AND_LINE_ ); + recvFromStruct->s=socketList[i]->s; + recvFromStruct->remotePortRakNetWasStartedOn_PS3=socketList[i]->remotePortRakNetWasStartedOn_PS3_PSP2; + recvFromStruct->extraSocketOptions=socketList[i]->extraSocketOptions; + SocketLayer::RecvFromBlocking( + recvFromStruct->s, this, recvFromStruct->remotePortRakNetWasStartedOn_PS3, + recvFromStruct->extraSocketOptions, recvFromStruct->data, &recvFromStruct->bytesRead, &recvFromStruct->systemAddress, &recvFromStruct->timeRead); + if (recvFromStruct->bytesRead<=0) + { + bufferedPackets.Deallocate(recvFromStruct, _FILE_AND_LINE_); + break; + } + else + { + RakAssert(recvFromStruct->systemAddress.GetPort()); + bufferedPackets.Push(recvFromStruct); + } + } + } + + BitStream updateBitStream( MAXIMUM_MTU_SIZE +#if LIBCAT_SECURITY==1 + + cat::AuthenticatedEncryption::OVERHEAD_BYTES +#endif + ); + RunUpdateCycle(0, 0, updateBitStream); +#endif + */ + + for (i=0; i < pluginListTS.Size(); i++) + { + pluginListTS[i]->Update(); + } + for (i=0; i < pluginListNTS.Size(); i++) + { + pluginListNTS[i]->Update(); + } + + do + { + packetReturnMutex.Lock(); + if (packetReturnQueue.IsEmpty()) + packet=0; + else + packet = packetReturnQueue.Pop(); + packetReturnMutex.Unlock(); + if (packet==0) + return 0; + +// unsigned char msgId; + if ( ( packet->length >= sizeof(unsigned char) + sizeof( RakNet::Time ) ) && + ( (unsigned char) packet->data[ 0 ] == ID_TIMESTAMP ) ) + { + offset = sizeof(unsigned char); + ShiftIncomingTimestamp( packet->data + offset, packet->systemAddress ); +// msgId=packet->data[sizeof(unsigned char) + sizeof( RakNet::Time )]; + } +// else + // msgId=packet->data[0]; + + // Some locally generated packets need to be processed by plugins, for example ID_FCM2_NEW_HOST + // The plugin itself should intercept these messages generated remotely +// if (packet->wasGeneratedLocally) +// return packet; + + + CallPluginCallbacks(pluginListTS, packet); + CallPluginCallbacks(pluginListNTS, packet); + + for (i=0; i < pluginListTS.Size(); i++) + { + pluginResult=pluginListTS[i]->OnReceive(packet); + if (pluginResult==RR_STOP_PROCESSING_AND_DEALLOCATE) + { + DeallocatePacket( packet ); + packet=0; // Will do the loop again and get another packet + break; // break out of the enclosing for + } + else if (pluginResult==RR_STOP_PROCESSING) + { + packet=0; + break; + } + } + + for (i=0; i < pluginListNTS.Size(); i++) + { + pluginResult=pluginListNTS[i]->OnReceive(packet); + if (pluginResult==RR_STOP_PROCESSING_AND_DEALLOCATE) + { + DeallocatePacket( packet ); + packet=0; // Will do the loop again and get another packet + break; // break out of the enclosing for + } + else if (pluginResult==RR_STOP_PROCESSING) + { + packet=0; + break; + } + } + + } while(packet==0); + +#ifdef _DEBUG + RakAssert( packet->data ); +#endif + + return packet; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Call this to deallocate a packet returned by Receive +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::DeallocatePacket( Packet *packet ) +{ + if ( packet == 0 ) + return; + + if (packet->deleteData) + { + rakFree_Ex(packet->data, _FILE_AND_LINE_ ); + packet->~Packet(); + packetAllocationPoolMutex.Lock(); + packetAllocationPool.Release(packet,_FILE_AND_LINE_); + packetAllocationPoolMutex.Unlock(); + } + else + { + rakFree_Ex(packet, _FILE_AND_LINE_ ); + } +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Return the total number of connections we are allowed +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +unsigned int RakPeer::GetMaximumNumberOfPeers( void ) const +{ + return maximumNumberOfPeers; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Close the connection to another host (if we initiated the connection it will disconnect, if they did it will kick them out). +// +// Parameters: +// target: Which connection to close +// sendDisconnectionNotification: True to send ID_DISCONNECTION_NOTIFICATION to the recipient. False to close it silently. +// channel: If blockDuration > 0, the disconnect packet will be sent on this channel +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::CloseConnection( const AddressOrGUID target, bool sendDisconnectionNotification, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority ) +{ + /* + // This only be called from the user thread, for the user shutting down. + // From the network thread, this should occur because of ID_DISCONNECTION_NOTIFICATION and ID_CONNECTION_LOST + unsigned j; + for (j=0; j < messageHandlerList.Size(); j++) + { + messageHandlerList[j]->OnClosedConnection( + target.systemAddress==UNASSIGNED_SYSTEM_ADDRESS ? GetSystemAddressFromGuid(target.rakNetGuid) : target.systemAddress, + target.rakNetGuid==UNASSIGNED_RAKNET_GUID ? GetGuidFromSystemAddress(target.systemAddress) : target.rakNetGuid, + LCR_CLOSED_BY_USER); + } + */ + + CloseConnectionInternal(target, sendDisconnectionNotification, false, orderingChannel, disconnectionNotificationPriority); + + // 12/14/09 Return ID_CONNECTION_LOST when calling CloseConnection with sendDisconnectionNotification==false, elsewise it is never returned + if (sendDisconnectionNotification==false && GetConnectionState(target)==IS_CONNECTED) + { + Packet *packet=AllocPacket(sizeof( char ), _FILE_AND_LINE_); + packet->data[ 0 ] = ID_CONNECTION_LOST; // DeadConnection + packet->guid = target.rakNetGuid==UNASSIGNED_RAKNET_GUID ? GetGuidFromSystemAddress(target.systemAddress) : target.rakNetGuid; + packet->systemAddress = target.systemAddress==UNASSIGNED_SYSTEM_ADDRESS ? GetSystemAddressFromGuid(target.rakNetGuid) : target.systemAddress; + packet->systemAddress.systemIndex = (SystemIndex) GetIndexFromSystemAddress(packet->systemAddress); + packet->guid.systemIndex=packet->systemAddress.systemIndex; + packet->wasGeneratedLocally=true; // else processed twice + AddPacketToProducer(packet); + } +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Cancel a pending connection attempt +// If we are already connected, the connection stays open +// \param[in] target Which system to cancel +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::CancelConnectionAttempt( const SystemAddress target ) +{ + unsigned int i; + + // Cancel pending connection attempt, if there is one + i=0; + requestedConnectionQueueMutex.Lock(); + while (i < requestedConnectionQueue.Size()) + { + if (requestedConnectionQueue[i]->systemAddress==target) + { +#if LIBCAT_SECURITY==1 + CAT_AUDIT_PRINTF("AUDIT: Deleting requestedConnectionQueue %i client_handshake %x\n", i, requestedConnectionQueue[ i ]->client_handshake); + RakNet::OP_DELETE(requestedConnectionQueue[i]->client_handshake, _FILE_AND_LINE_ ); +#endif + RakNet::OP_DELETE(requestedConnectionQueue[i], _FILE_AND_LINE_ ); + requestedConnectionQueue.RemoveAtIndex(i); + break; + } + else + i++; + } + requestedConnectionQueueMutex.Unlock(); + +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +#ifdef _MSC_VER +#pragma warning( disable : 4702 ) // warning C4702: unreachable code +#endif +ConnectionState RakPeer::GetConnectionState(const AddressOrGUID systemIdentifier) +{ + if (systemIdentifier.systemAddress!=UNASSIGNED_SYSTEM_ADDRESS) + { + unsigned int i=0; + requestedConnectionQueueMutex.Lock(); + for (; i < requestedConnectionQueue.Size(); i++) + { + if (requestedConnectionQueue[i]->systemAddress==systemIdentifier.systemAddress) + { + requestedConnectionQueueMutex.Unlock(); + return IS_PENDING; + } + } + requestedConnectionQueueMutex.Unlock(); + } + + int index; + if (systemIdentifier.systemAddress!=UNASSIGNED_SYSTEM_ADDRESS) + { + index = GetIndexFromSystemAddress(systemIdentifier.systemAddress, false); + } + else + { + index = GetIndexFromGuid(systemIdentifier.rakNetGuid); + } + + if (index==-1) + return IS_NOT_CONNECTED; + + if (remoteSystemList[index].isActive==false) + return IS_DISCONNECTED; + + switch (remoteSystemList[index].connectMode) + { + case RemoteSystemStruct::DISCONNECT_ASAP: + return IS_DISCONNECTING; + case RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY: + return IS_SILENTLY_DISCONNECTING; + case RemoteSystemStruct::DISCONNECT_ON_NO_ACK: + return IS_DISCONNECTING; + case RemoteSystemStruct::REQUESTED_CONNECTION: + return IS_CONNECTING; + case RemoteSystemStruct::HANDLING_CONNECTION_REQUEST: + return IS_CONNECTING; + case RemoteSystemStruct::UNVERIFIED_SENDER: + return IS_CONNECTING; + case RemoteSystemStruct::CONNECTED: + return IS_CONNECTED; + default: + return IS_NOT_CONNECTED; + } + + return IS_NOT_CONNECTED; +} + + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Given a systemAddress, returns an index from 0 to the maximum number of players allowed - 1. +// +// Parameters +// systemAddress - The systemAddress to search for +// +// Returns +// An integer from 0 to the maximum number of peers -1, or -1 if that player is not found +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +int RakPeer::GetIndexFromSystemAddress( const SystemAddress systemAddress ) const +{ + return GetIndexFromSystemAddress(systemAddress, false); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// This function is only useful for looping through all players. +// +// Parameters +// index - an integer between 0 and the maximum number of players allowed - 1. +// +// Returns +// A valid systemAddress or UNASSIGNED_SYSTEM_ADDRESS if no such player at that index +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +SystemAddress RakPeer::GetSystemAddressFromIndex( unsigned int index ) +{ + // remoteSystemList in user thread + //if ( index >= 0 && index < remoteSystemListSize ) + if ( index < maximumNumberOfPeers ) + if (remoteSystemList[index].isActive && remoteSystemList[ index ].connectMode==RakPeer::RemoteSystemStruct::CONNECTED) // Don't give the user players that aren't fully connected, since sends will fail + return remoteSystemList[ index ].systemAddress; + + return UNASSIGNED_SYSTEM_ADDRESS; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Same as GetSystemAddressFromIndex but returns RakNetGUID +// \param[in] index Index should range between 0 and the maximum number of players allowed - 1. +// \return The RakNetGUID +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +RakNetGUID RakPeer::GetGUIDFromIndex( unsigned int index ) +{ + // remoteSystemList in user thread + //if ( index >= 0 && index < remoteSystemListSize ) + if ( index < maximumNumberOfPeers ) + if (remoteSystemList[index].isActive && remoteSystemList[ index ].connectMode==RakPeer::RemoteSystemStruct::CONNECTED) // Don't give the user players that aren't fully connected, since sends will fail + return remoteSystemList[ index ].guid; + + return UNASSIGNED_RAKNET_GUID; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Same as calling GetSystemAddressFromIndex and GetGUIDFromIndex for all systems, but more efficient +// Indices match each other, so \a addresses[0] and \a guids[0] refer to the same system +// \param[out] addresses All system addresses. Size of the list is the number of connections. Size of the list will match the size of the \a guids list. +// \param[out] guids All guids. Size of the list is the number of connections. Size of the list will match the size of the \a addresses list. +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::GetSystemList(DataStructures::List &addresses, DataStructures::List &guids) const +{ + addresses.Clear(false, _FILE_AND_LINE_); + guids.Clear(false, _FILE_AND_LINE_); + + if ( remoteSystemList == 0 || endThreads == true ) + return; + + unsigned int i; + for (i=0; i < activeSystemListSize; i++) + { + if ((activeSystemList[i])->isActive && + (activeSystemList[i])->connectMode==RakPeer::RemoteSystemStruct::CONNECTED) + { + addresses.Push((activeSystemList[i])->systemAddress, _FILE_AND_LINE_ ); + guids.Push((activeSystemList[i])->guid, _FILE_AND_LINE_ ); + } + } +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Bans an IP from connecting. Banned IPs persist between connections. +// +// Parameters +// IP - Dotted IP address. Can use * as a wildcard, such as 128.0.0.* will ban +// All IP addresses starting with 128.0.0 +// milliseconds - how many ms for a temporary ban. Use 0 for a permanent ban +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::AddToBanList( const char *IP, RakNet::TimeMS milliseconds ) +{ + unsigned index; + RakNet::TimeMS time = RakNet::GetTimeMS(); + + if ( IP == 0 || IP[ 0 ] == 0 || strlen( IP ) > 15 ) + return ; + + // If this guy is already in the ban list, do nothing + index = 0; + + banListMutex.Lock(); + + for ( ; index < banList.Size(); index++ ) + { + if ( strcmp( IP, banList[ index ]->IP ) == 0 ) + { + // Already in the ban list. Just update the time + if (milliseconds==0) + banList[ index ]->timeout=0; // Infinite + else + banList[ index ]->timeout=time+milliseconds; + banListMutex.Unlock(); + return; + } + } + + banListMutex.Unlock(); + + BanStruct *banStruct = RakNet::OP_NEW( _FILE_AND_LINE_ ); + banStruct->IP = (char*) rakMalloc_Ex( 16, _FILE_AND_LINE_ ); + if (milliseconds==0) + banStruct->timeout=0; // Infinite + else + banStruct->timeout=time+milliseconds; + strcpy( banStruct->IP, IP ); + banListMutex.Lock(); + banList.Insert( banStruct, _FILE_AND_LINE_ ); + banListMutex.Unlock(); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Allows a previously banned IP to connect. +// +// Parameters +// IP - Dotted IP address. Can use * as a wildcard, such as 128.0.0.* will ban +// All IP addresses starting with 128.0.0 +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::RemoveFromBanList( const char *IP ) +{ + unsigned index; + BanStruct *temp; + + if ( IP == 0 || IP[ 0 ] == 0 || strlen( IP ) > 15 ) + return ; + + index = 0; + temp=0; + + banListMutex.Lock(); + + for ( ; index < banList.Size(); index++ ) + { + if ( strcmp( IP, banList[ index ]->IP ) == 0 ) + { + temp = banList[ index ]; + banList[ index ] = banList[ banList.Size() - 1 ]; + banList.RemoveAtIndex( banList.Size() - 1 ); + break; + } + } + + banListMutex.Unlock(); + + if (temp) + { + rakFree_Ex(temp->IP, _FILE_AND_LINE_ ); + RakNet::OP_DELETE(temp, _FILE_AND_LINE_); + } + +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Allows all previously banned IPs to connect. +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::ClearBanList( void ) +{ + unsigned index; + index = 0; + banListMutex.Lock(); + + for ( ; index < banList.Size(); index++ ) + { + rakFree_Ex(banList[ index ]->IP, _FILE_AND_LINE_ ); + RakNet::OP_DELETE(banList[ index ], _FILE_AND_LINE_); + } + + banList.Clear(false, _FILE_AND_LINE_); + + banListMutex.Unlock(); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::SetLimitIPConnectionFrequency(bool b) +{ + limitConnectionFrequencyFromTheSameIP=b; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Determines if a particular IP is banned. +// +// Parameters +// IP - Complete dotted IP address +// +// Returns +// True if IP matches any IPs in the ban list, accounting for any wildcards. +// False otherwise. +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +bool RakPeer::IsBanned( const char *IP ) +{ + unsigned banListIndex, characterIndex; + RakNet::TimeMS time; + BanStruct *temp; + + if ( IP == 0 || IP[ 0 ] == 0 || strlen( IP ) > 15 ) + return false; + + banListIndex = 0; + + if ( banList.Size() == 0 ) + return false; // Skip the mutex if possible + + time = RakNet::GetTimeMS(); + + banListMutex.Lock(); + + while ( banListIndex < banList.Size() ) + { + if (banList[ banListIndex ]->timeout>0 && banList[ banListIndex ]->timeoutIP, _FILE_AND_LINE_ ); + RakNet::OP_DELETE(temp, _FILE_AND_LINE_); + } + else + { + characterIndex = 0; + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + while ( true ) + { + if ( banList[ banListIndex ]->IP[ characterIndex ] == IP[ characterIndex ] ) + { + // Equal characters + + if ( IP[ characterIndex ] == 0 ) + { + banListMutex.Unlock(); + // End of the string and the strings match + + return true; + } + + characterIndex++; + } + + else + { + if ( banList[ banListIndex ]->IP[ characterIndex ] == 0 || IP[ characterIndex ] == 0 ) + { + // End of one of the strings + break; + } + + // Characters do not match + if ( banList[ banListIndex ]->IP[ characterIndex ] == '*' ) + { + banListMutex.Unlock(); + + // Domain is banned. + return true; + } + + // Characters do not match and it is not a * + break; + } + } + + banListIndex++; + } + } + + banListMutex.Unlock(); + + // No match found. + return false; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Send a ping to the specified connected system. +// +// Parameters: +// target - who to ping +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::Ping( const SystemAddress target ) +{ + PingInternal(target, false, UNRELIABLE); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Send a ping to the specified unconnected system. +// The remote system, if it is Initialized, will respond with ID_UNCONNECTED_PONG. +// The final ping time will be encoded in the following sizeof(RakNet::TimeMS) bytes. (Default is 4 bytes - See __GET_TIME_64BIT in RakNetTypes.h +// +// Parameters: +// host: Either a dotted IP address or a domain name. Can be 255.255.255.255 for LAN broadcast. +// remotePort: Which port to connect to on the remote machine. +// onlyReplyOnAcceptingConnections: Only request a reply if the remote system has open connections +// connectionSocketIndex Index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to send on. +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +bool RakPeer::Ping( const char* host, unsigned short remotePort, bool onlyReplyOnAcceptingConnections, unsigned connectionSocketIndex ) +{ + if ( host == 0 ) + return false; + + // If this assert hits then Startup wasn't called or the call failed. + RakAssert(connectionSocketIndex < socketList.Size()); + +// if ( IsActive() == false ) +// return; + + RakNet::BitStream bitStream( sizeof(unsigned char) + sizeof(RakNet::Time) ); + if ( onlyReplyOnAcceptingConnections ) + bitStream.Write((MessageID)ID_UNCONNECTED_PING_OPEN_CONNECTIONS); + else + bitStream.Write((MessageID)ID_UNCONNECTED_PING); + + bitStream.Write(RakNet::GetTime()); + + bitStream.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); + + bitStream.Write(GetMyGUID()); + + // No timestamp for 255.255.255.255 + unsigned int realIndex = GetRakNetSocketFromUserConnectionSocketIndex(connectionSocketIndex); + /* + + SystemAddress systemAddress; + systemAddress.FromStringExplicitPort(host,remotePort, socketList[realIndex]->GetBoundAddress().GetIPVersion()); + systemAddress.FixForIPVersion(socketList[realIndex]->GetBoundAddress()); + + unsigned i; + for (i=0; i < pluginListNTS.Size(); i++) + pluginListNTS[i]->OnDirectSocketSend((const char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), systemAddress); + SocketLayer::SendTo( socketList[realIndex], (const char*)bitStream.GetData(), (int) bitStream.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); + */ + + RNS2_SendParameters bsp; + bsp.data = (char*) bitStream.GetData() ; + bsp.length = bitStream.GetNumberOfBytesUsed(); + bsp.systemAddress.FromStringExplicitPort(host,remotePort, socketList[realIndex]->GetBoundAddress().GetIPVersion()); + if (bsp.systemAddress==UNASSIGNED_SYSTEM_ADDRESS) + return false; + bsp.systemAddress.FixForIPVersion(socketList[realIndex]->GetBoundAddress()); + unsigned i; + for (i=0; i < pluginListNTS.Size(); i++) + pluginListNTS[i]->OnDirectSocketSend((const char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), bsp.systemAddress); + socketList[realIndex]->Send(&bsp, _FILE_AND_LINE_); + + return true; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Returns the average of all ping times read for a specified target +// +// Parameters: +// target - whose time to read +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +int RakPeer::GetAveragePing( const AddressOrGUID systemIdentifier ) +{ + int sum, quantity; + RemoteSystemStruct *remoteSystem = GetRemoteSystem( systemIdentifier, false, false ); + + if ( remoteSystem == 0 ) + return -1; + + for ( sum = 0, quantity = 0; quantity < PING_TIMES_ARRAY_SIZE; quantity++ ) + { + if ( remoteSystem->pingAndClockDifferential[ quantity ].pingTime == 65535 ) + break; + else + sum += remoteSystem->pingAndClockDifferential[ quantity ].pingTime; + } + + if ( quantity > 0 ) + return sum / quantity; + else + return -1; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Returns the last ping time read for the specific player or -1 if none read yet +// +// Parameters: +// target - whose time to read +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +int RakPeer::GetLastPing( const AddressOrGUID systemIdentifier ) const +{ + RemoteSystemStruct * remoteSystem = GetRemoteSystem( systemIdentifier, false, false ); + + if ( remoteSystem == 0 ) + return -1; + +// return (int)(remoteSystem->reliabilityLayer.GetAckPing()/(RakNet::TimeUS)1000); + + if ( remoteSystem->pingAndClockDifferentialWriteIndex == 0 ) + return remoteSystem->pingAndClockDifferential[ PING_TIMES_ARRAY_SIZE - 1 ].pingTime; + else + return remoteSystem->pingAndClockDifferential[ remoteSystem->pingAndClockDifferentialWriteIndex - 1 ].pingTime; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Returns the lowest ping time read or -1 if none read yet +// +// Parameters: +// target - whose time to read +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +int RakPeer::GetLowestPing( const AddressOrGUID systemIdentifier ) const +{ + RemoteSystemStruct * remoteSystem = GetRemoteSystem( systemIdentifier, false, false ); + + if ( remoteSystem == 0 ) + return -1; + + return remoteSystem->lowestPing; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Ping the remote systems every so often. This is off by default +// This will work anytime +// +// Parameters: +// doPing - True to start occasional pings. False to stop them. +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::SetOccasionalPing( bool doPing ) +{ + occasionalPing = doPing; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +/// Return the clock difference between your system and the specified system +/// Subtract the time from a time returned by the remote system to get that time relative to your own system +/// Returns 0 if the system is unknown +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +RakNet::Time RakPeer::GetClockDifferential( const AddressOrGUID systemIdentifier ) +{ + RemoteSystemStruct *remoteSystem = GetRemoteSystem(systemIdentifier, false, false); + if (remoteSystem == 0) + return 0; + return GetClockDifferentialInt(remoteSystem); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +RakNet::Time RakPeer::GetClockDifferentialInt(RemoteSystemStruct *remoteSystem) const +{ + int counter, lowestPingSoFar; + RakNet::Time clockDifferential; + + lowestPingSoFar = 65535; + + clockDifferential = 0; + + for ( counter = 0; counter < PING_TIMES_ARRAY_SIZE; counter++ ) + { + if ( remoteSystem->pingAndClockDifferential[ counter ].pingTime == 65535 ) + break; + + if ( remoteSystem->pingAndClockDifferential[ counter ].pingTime < lowestPingSoFar ) + { + clockDifferential = remoteSystem->pingAndClockDifferential[ counter ].clockDifferential; + lowestPingSoFar = remoteSystem->pingAndClockDifferential[ counter ].pingTime; + } + } + + return clockDifferential; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Length should be under 400 bytes, as a security measure against flood attacks +// Sets the data to send with an (LAN server discovery) /(offline ping) response +// See the Ping sample project for how this is used. +// data: a block of data to store, or 0 for none +// length: The length of data in bytes, or 0 for none +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::SetOfflinePingResponse( const char *data, const unsigned int length ) +{ + RakAssert(length < 400); + + rakPeerMutexes[ offlinePingResponse_Mutex ].Lock(); + offlinePingResponse.Reset(); + + if ( data && length > 0 ) + offlinePingResponse.Write( data, length ); + + rakPeerMutexes[ offlinePingResponse_Mutex ].Unlock(); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Returns pointers to a copy of the data passed to SetOfflinePingResponse +// \param[out] data A pointer to a copy of the data passed to \a SetOfflinePingResponse() +// \param[out] length A pointer filled in with the length parameter passed to SetOfflinePingResponse() +// \sa SetOfflinePingResponse +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::GetOfflinePingResponse( char **data, unsigned int *length ) +{ + rakPeerMutexes[ offlinePingResponse_Mutex ].Lock(); + *data = (char*) offlinePingResponse.GetData(); + *length = (int) offlinePingResponse.GetNumberOfBytesUsed(); + rakPeerMutexes[ offlinePingResponse_Mutex ].Unlock(); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Return the unique SystemAddress that represents you on the the network +// Note that unlike in previous versions, this is a struct and is not sequential +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +SystemAddress RakPeer::GetInternalID( const SystemAddress systemAddress, const int index ) const +{ + if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS) + { + return ipList[index]; + } + else + { + +// SystemAddress returnValue; + RemoteSystemStruct * remoteSystem = GetRemoteSystemFromSystemAddress( systemAddress, false, true ); + if (remoteSystem==0) + return UNASSIGNED_SYSTEM_ADDRESS; + + return remoteSystem->theirInternalSystemAddress[index]; + /* + sockaddr_in sa; + socklen_t len = sizeof(sa); + if (getsockname__(connectionSockets[remoteSystem->connectionSocketIndex], (sockaddr*)&sa, &len)!=0) + return UNASSIGNED_SYSTEM_ADDRESS; + returnValue.port=ntohs(sa.sin_port); + returnValue.binaryAddress=sa.sin_addr.s_addr; + return returnValue; +*/ + + + + } +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +/// \brief Sets your internal IP address, for platforms that do not support reading it, or to override a value +/// \param[in] systemAddress. The address to set. Use SystemAddress::FromString() if you want to use a dotted string +/// \param[in] index When you have multiple internal IDs, which index to set? +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::SetInternalID(SystemAddress systemAddress, int index) +{ + RakAssert(index >=0 && index < MAXIMUM_NUMBER_OF_INTERNAL_IDS); + ipList[index]=systemAddress; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Return the unique address identifier that represents you on the the network and is based on your external +// IP / port (the IP / port the specified player uses to communicate with you) +// Note that unlike in previous versions, this is a struct and is not sequential +// +// Parameters: +// target: Which remote system you are referring to for your external ID +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +SystemAddress RakPeer::GetExternalID( const SystemAddress target ) const +{ + unsigned i; + SystemAddress inactiveExternalId; + + inactiveExternalId=UNASSIGNED_SYSTEM_ADDRESS; + + if (target==UNASSIGNED_SYSTEM_ADDRESS) + return firstExternalID; + + // First check for active connection with this systemAddress + for ( i = 0; i < maximumNumberOfPeers; i++ ) + { + if (remoteSystemList[ i ].systemAddress == target ) + { + if ( remoteSystemList[ i ].isActive ) + return remoteSystemList[ i ].myExternalSystemAddress; + else if (remoteSystemList[ i ].myExternalSystemAddress!=UNASSIGNED_SYSTEM_ADDRESS) + inactiveExternalId=remoteSystemList[ i ].myExternalSystemAddress; + } + } + + return inactiveExternalId; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +const RakNetGUID RakPeer::GetMyGUID(void) const +{ + return myGuid; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +SystemAddress RakPeer::GetMyBoundAddress(const int socketIndex) +{ + DataStructures::List sockets; + GetSockets( sockets ); + if (sockets.Size()>0) + return sockets[socketIndex]->GetBoundAddress(); + else + return UNASSIGNED_SYSTEM_ADDRESS; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +const RakNetGUID& RakPeer::GetGuidFromSystemAddress( const SystemAddress input ) const +{ + if (input==UNASSIGNED_SYSTEM_ADDRESS) + return myGuid; + + if (input.systemIndex!=(SystemIndex)-1 && input.systemIndexreliabilityLayer.SetTimeoutTime(timeMS); + } +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +RakNet::TimeMS RakPeer::GetTimeoutTime( const SystemAddress target ) +{ + if (target==UNASSIGNED_SYSTEM_ADDRESS) + { + return defaultTimeoutTime; + } + else + { + RemoteSystemStruct * remoteSystem = GetRemoteSystemFromSystemAddress( target, false, true ); + + if ( remoteSystem != 0 ) + remoteSystem->reliabilityLayer.GetTimeoutTime(); + } + return defaultTimeoutTime; +} + + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Returns the current MTU size +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +int RakPeer::GetMTUSize( const SystemAddress target ) const +{ + if (target!=UNASSIGNED_SYSTEM_ADDRESS) + { + RemoteSystemStruct *rss=GetRemoteSystemFromSystemAddress(target, false, true); + if (rss) + return rss->MTUSize; + } + return defaultMTUSize; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Returns the number of IP addresses we have +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +unsigned int RakPeer::GetNumberOfAddresses( void ) +{ + + if (IsActive()==false) + { + FillIPList(); + } + + int i = 0; + + while ( ipList[ i ]!=UNASSIGNED_SYSTEM_ADDRESS ) + i++; + + return i; + + + + +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Returns an IP address at index 0 to GetNumberOfAddresses-1 +// \param[in] index index into the list of IP addresses +// \return The local IP address at this index +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +const char* RakPeer::GetLocalIP( unsigned int index ) +{ + if (IsActive()==false) + { + // Fill out ipList structure + + FillIPList(); + + } + + + static char str[128]; + ipList[index].ToString(false,str); + return str; + + + + +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Is this a local IP? +// \param[in] An IP address to check +// \return True if this is one of the IP addresses returned by GetLocalIP +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +bool RakPeer::IsLocalIP( const char *ip ) +{ + if (ip==0 || ip[0]==0) + return false; + + + if (strcmp(ip, "127.0.0.1")==0 || strcmp(ip, "localhost")==0) + return true; + + int num = GetNumberOfAddresses(); + int i; + for (i=0; i < num; i++) + { + if (strcmp(ip, GetLocalIP(i))==0) + return true; + } + + + + + return false; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Allow or disallow connection responses from any IP. Normally this should be false, but may be necessary +// when connection to servers with multiple IP addresses +// +// Parameters: +// allow - True to allow this behavior, false to not allow. Defaults to false. Value persists between connections +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::AllowConnectionResponseIPMigration( bool allow ) +{ + allowConnectionResponseIPMigration = allow; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Description: +// Sends a message ID_ADVERTISE_SYSTEM to the remote unconnected system. +// This will tell the remote system our external IP outside the LAN, and can be used for NAT punch through +// +// Requires: +// The sender and recipient must already be started via a successful call to Initialize +// +// host: Either a dotted IP address or a domain name +// remotePort: Which port to connect to on the remote machine. +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +bool RakPeer::AdvertiseSystem( const char *host, unsigned short remotePort, const char *data, int dataLength, unsigned connectionSocketIndex ) +{ + RakNet::BitStream bs; + bs.Write((MessageID)ID_ADVERTISE_SYSTEM); + bs.WriteAlignedBytes((const unsigned char*) data,dataLength); + return SendOutOfBand(host, remotePort, (const char*) bs.GetData(), bs.GetNumberOfBytesUsed(), connectionSocketIndex ); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Controls how often to return ID_DOWNLOAD_PROGRESS for large message downloads. +// ID_DOWNLOAD_PROGRESS is returned to indicate a new partial message chunk, roughly the MTU size, has arrived +// As it can be slow or cumbersome to get this notification for every chunk, you can set the interval at which it is returned. +// Defaults to 0 (never return this notification) +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::SetSplitMessageProgressInterval(int interval) +{ + RakAssert(interval>=0); + splitMessageProgressInterval=interval; + for ( unsigned short i = 0; i < maximumNumberOfPeers; i++ ) + remoteSystemList[ i ].reliabilityLayer.SetSplitMessageProgressInterval(splitMessageProgressInterval); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Returns what was passed to SetSplitMessageProgressInterval() +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +int RakPeer::GetSplitMessageProgressInterval(void) const +{ + return splitMessageProgressInterval; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Set how long to wait before giving up on sending an unreliable message +// Useful if the network is clogged up. +// Set to 0 or less to never timeout. Defaults to 0. +// timeoutMS How many ms to wait before simply not sending an unreliable message. +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::SetUnreliableTimeout(RakNet::TimeMS timeoutMS) +{ + unreliableTimeout=timeoutMS; + for ( unsigned short i = 0; i < maximumNumberOfPeers; i++ ) + remoteSystemList[ i ].reliabilityLayer.SetUnreliableTimeout(unreliableTimeout); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Send a message to host, with the IP socket option TTL set to 3 +// This message will not reach the host, but will open the router. +// Used for NAT-Punchthrough +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::SendTTL( const char* host, unsigned short remotePort, int ttl, unsigned connectionSocketIndex ) +{ +#if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) + char fakeData[2]; + fakeData[0]=0; + fakeData[1]=1; + unsigned int realIndex = GetRakNetSocketFromUserConnectionSocketIndex(connectionSocketIndex); + if (socketList[realIndex]->IsBerkleySocket()) + { + RNS2_SendParameters bsp; + bsp.data = (char*) fakeData; + bsp.length = 2; + bsp.systemAddress.FromStringExplicitPort(host,remotePort, socketList[realIndex]->GetBoundAddress().GetIPVersion()); + bsp.systemAddress.FixForIPVersion(socketList[realIndex]->GetBoundAddress()); + bsp.ttl=ttl; + unsigned i; + for (i=0; i < pluginListNTS.Size(); i++) + pluginListNTS[i]->OnDirectSocketSend((const char*)bsp.data, BYTES_TO_BITS(bsp.length), bsp.systemAddress); + socketList[realIndex]->Send(&bsp, _FILE_AND_LINE_); + } +#endif +} + + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Attatches a Plugin interface to run code automatically on message receipt in the Receive call +// +// \param messageHandler Pointer to a plugin to attach +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::AttachPlugin( PluginInterface2 *plugin ) +{ + bool isNotThreadsafe = plugin->UsesReliabilityLayer(); + if (isNotThreadsafe) + { + if (pluginListNTS.GetIndexOf(plugin)==MAX_UNSIGNED_LONG) + { + plugin->SetRakPeerInterface(this); + plugin->OnAttach(); + pluginListNTS.Insert(plugin, _FILE_AND_LINE_); + } + } + else + { + if (pluginListTS.GetIndexOf(plugin)==MAX_UNSIGNED_LONG) + { + plugin->SetRakPeerInterface(this); + plugin->OnAttach(); + pluginListTS.Insert(plugin, _FILE_AND_LINE_); + } + } +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Detaches a Plugin interface to run code automatically on message receipt +// +// \param messageHandler Pointer to a plugin to detach +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::DetachPlugin( PluginInterface2 *plugin ) +{ + if (plugin==0) + return; + + unsigned int index; + + bool isNotThreadsafe = plugin->UsesReliabilityLayer(); + if (isNotThreadsafe) + { + index = pluginListNTS.GetIndexOf(plugin); + if (index!=MAX_UNSIGNED_LONG) + { + // Unordered list so delete from end for speed + pluginListNTS[index]=pluginListNTS[pluginListNTS.Size()-1]; + pluginListNTS.RemoveFromEnd(); + } + } + else + { + index = pluginListTS.GetIndexOf(plugin); + if (index!=MAX_UNSIGNED_LONG) + { + // Unordered list so delete from end for speed + pluginListTS[index]=pluginListTS[pluginListTS.Size()-1]; + pluginListTS.RemoveFromEnd(); + } + } + plugin->OnDetach(); + plugin->SetRakPeerInterface(0); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Put a packet back at the end of the receive queue in case you don't want to deal with it immediately +// +// packet The packet you want to push back. +// pushAtHead True to push the packet so that the next receive call returns it. False to push it at the end of the queue (obviously pushing it at the end makes the packets out of order) +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::PushBackPacket( Packet *packet, bool pushAtHead) +{ + if (packet==0) + return; + + unsigned i; + for (i=0; i < pluginListTS.Size(); i++) + pluginListTS[i]->OnPushBackPacket((const char*) packet->data, packet->bitSize, packet->systemAddress); + for (i=0; i < pluginListNTS.Size(); i++) + pluginListNTS[i]->OnPushBackPacket((const char*) packet->data, packet->bitSize, packet->systemAddress); + + packetReturnMutex.Lock(); + if (pushAtHead) + packetReturnQueue.PushAtHead(packet,0,_FILE_AND_LINE_); + else + packetReturnQueue.Push(packet,_FILE_AND_LINE_); + packetReturnMutex.Unlock(); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::ChangeSystemAddress(RakNetGUID guid, const SystemAddress &systemAddress) +{ + BufferedCommandStruct *bcs; + + bcs=bufferedCommands.Allocate( _FILE_AND_LINE_ ); + bcs->data = 0; + bcs->systemIdentifier.systemAddress=systemAddress; + bcs->systemIdentifier.rakNetGuid=guid; + bcs->command=BufferedCommandStruct::BCS_CHANGE_SYSTEM_ADDRESS; + bufferedCommands.Push(bcs); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Packet* RakPeer::AllocatePacket(unsigned dataSize) +{ + return AllocPacket(dataSize, _FILE_AND_LINE_); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +RakNetSocket2* RakPeer::GetSocket( const SystemAddress target ) +{ + // Send a query to the thread to get the socket, and return when we got it + BufferedCommandStruct *bcs; + bcs=bufferedCommands.Allocate( _FILE_AND_LINE_ ); + bcs->command=BufferedCommandStruct::BCS_GET_SOCKET; + bcs->systemIdentifier=target; + bcs->data=0; + bufferedCommands.Push(bcs); + + // Block up to one second to get the socket, although it should actually take virtually no time + SocketQueryOutput *sqo; + RakNet::TimeMS stopWaiting = RakNet::GetTimeMS()+1000; + DataStructures::List output; + while (RakNet::GetTimeMS() < stopWaiting) + { + if (isMainLoopThreadActive==false) + return 0; + + RakSleep(0); + + sqo = socketQueryOutput.Pop(); + if (sqo) + { + output=sqo->sockets; + sqo->sockets.Clear(false, _FILE_AND_LINE_); + socketQueryOutput.Deallocate(sqo, _FILE_AND_LINE_); + if (output.Size()) + return output[0]; + break; + } + } + return 0; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::GetSockets( DataStructures::List &sockets ) +{ + sockets.Clear(false, _FILE_AND_LINE_); + + // Send a query to the thread to get the socket, and return when we got it + BufferedCommandStruct *bcs; + + bcs=bufferedCommands.Allocate( _FILE_AND_LINE_ ); + bcs->command=BufferedCommandStruct::BCS_GET_SOCKET; + bcs->systemIdentifier=UNASSIGNED_SYSTEM_ADDRESS; + bcs->data=0; + bufferedCommands.Push(bcs); + + // Block up to one second to get the socket, although it should actually take virtually no time + SocketQueryOutput *sqo; +// RakNetSocket2* output; + while (1) + { + if (isMainLoopThreadActive==false) + return; + + RakSleep(0); + + sqo = socketQueryOutput.Pop(); + if (sqo) + { + sockets=sqo->sockets; + sqo->sockets.Clear(false, _FILE_AND_LINE_); + socketQueryOutput.Deallocate(sqo, _FILE_AND_LINE_); + return; + } + } + return; +} +void RakPeer::ReleaseSockets( DataStructures::List &sockets ) +{ + sockets.Clear(false,_FILE_AND_LINE_); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Adds simulated ping and packet loss to the outgoing data flow. +// To simulate bi-directional ping and packet loss, you should call this on both the sender and the recipient, with half the total ping and maxSendBPS value on each. +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::ApplyNetworkSimulator( float packetloss, unsigned short minExtraPing, unsigned short extraPingVariance) +{ +#ifdef _DEBUG + if (remoteSystemList) + { + unsigned short i; + for (i=0; i < maximumNumberOfPeers; i++) + //for (i=0; i < remoteSystemListSize; i++) + remoteSystemList[i].reliabilityLayer.ApplyNetworkSimulator(packetloss, minExtraPing, extraPingVariance); + } + + _packetloss=packetloss; + _minExtraPing=minExtraPing; + _extraPingVariance=extraPingVariance; +#endif +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +void RakPeer::SetPerConnectionOutgoingBandwidthLimit( unsigned maxBitsPerSecond ) +{ + maxOutgoingBPS=maxBitsPerSecond; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Returns if you previously called ApplyNetworkSimulator +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +bool RakPeer::IsNetworkSimulatorActive( void ) +{ +#ifdef _DEBUG + return _packetloss>0 || _minExtraPing>0 || _extraPingVariance>0; +#else + return false; +#endif +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::WriteOutOfBandHeader(RakNet::BitStream *bitStream) +{ + bitStream->Write((MessageID)ID_OUT_OF_BAND_INTERNAL); + bitStream->Write(myGuid); + bitStream->WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::SetUserUpdateThread(void (*_userUpdateThreadPtr)(RakPeerInterface *, void *), void *_userUpdateThreadData) +{ + userUpdateThreadPtr=_userUpdateThreadPtr; + userUpdateThreadData=_userUpdateThreadData; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::SetIncomingDatagramEventHandler( bool (*_incomingDatagramEventHandler)(RNS2RecvStruct *) ) +{ + incomingDatagramEventHandler=_incomingDatagramEventHandler; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +bool RakPeer::SendOutOfBand(const char *host, unsigned short remotePort, const char *data, BitSize_t dataLength, unsigned connectionSocketIndex ) +{ + if ( IsActive() == false ) + return false; + + if (host==0 || host[0]==0) + return false; + + // If this assert hits then Startup wasn't called or the call failed. + RakAssert(connectionSocketIndex < socketList.Size()); + + // This is a security measure. Don't send data longer than this value + RakAssert(dataLength <= (MAX_OFFLINE_DATA_LENGTH + sizeof(unsigned char)+sizeof(RakNet::Time)+RakNetGUID::size()+sizeof(OFFLINE_MESSAGE_DATA_ID))); + + if (host==0) + return false; + + // 34 bytes + RakNet::BitStream bitStream; + WriteOutOfBandHeader(&bitStream); + + if (dataLength>0) + { + bitStream.Write(data, dataLength); + } + + unsigned int realIndex = GetRakNetSocketFromUserConnectionSocketIndex(connectionSocketIndex); + + /* + SystemAddress systemAddress; + systemAddress.FromStringExplicitPort(host,remotePort, socketList[realIndex]->GetBoundAddress().GetIPVersion()); + systemAddress.FixForIPVersion(socketList[realIndex]->GetBoundAddress()); + + unsigned i; + for (i=0; i < pluginListNTS.Size(); i++) + pluginListNTS[i]->OnDirectSocketSend((const char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), systemAddress); + + SocketLayer::SendTo( socketList[realIndex], (const char*)bitStream.GetData(), (int) bitStream.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); + */ + + RNS2_SendParameters bsp; + bsp.data = (char*) bitStream.GetData(); + bsp.length = bitStream.GetNumberOfBytesUsed(); + bsp.systemAddress.FromStringExplicitPort(host,remotePort, socketList[realIndex]->GetBoundAddress().GetIPVersion()); + bsp.systemAddress.FixForIPVersion(socketList[realIndex]->GetBoundAddress()); + unsigned i; + for (i=0; i < pluginListNTS.Size(); i++) + pluginListNTS[i]->OnDirectSocketSend((const char*)bsp.data, BYTES_TO_BITS(bsp.length), bsp.systemAddress); + socketList[realIndex]->Send(&bsp, _FILE_AND_LINE_); + + return true; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +RakNetStatistics * RakPeer::GetStatistics( const SystemAddress systemAddress, RakNetStatistics *rns ) +{ + static RakNetStatistics staticStatistics; + RakNetStatistics *systemStats; + if (rns==0) + systemStats=&staticStatistics; + else + systemStats=rns; + + if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS) + { + bool firstWrite=false; + // Return a crude sum + for ( unsigned short i = 0; i < maximumNumberOfPeers; i++ ) + { + if (remoteSystemList[ i ].isActive) + { + RakNetStatistics rnsTemp; + remoteSystemList[ i ].reliabilityLayer.GetStatistics(&rnsTemp); + + if (firstWrite==false) + { + memcpy(systemStats, &rnsTemp, sizeof(RakNetStatistics)); + firstWrite=true; + } + else + (*systemStats)+=rnsTemp; + } + } + return systemStats; + } + else + { + RemoteSystemStruct * rss; + rss = GetRemoteSystemFromSystemAddress( systemAddress, false, false ); + if ( rss && endThreads==false ) + { + rss->reliabilityLayer.GetStatistics(systemStats); + return systemStats; + } + } + + return 0; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::GetStatisticsList(DataStructures::List &addresses, DataStructures::List &guids, DataStructures::List &statistics) +{ + addresses.Clear(false, _FILE_AND_LINE_); + guids.Clear(false, _FILE_AND_LINE_); + statistics.Clear(false, _FILE_AND_LINE_); + + if ( remoteSystemList == 0 || endThreads == true ) + return; + + unsigned int i; + for (i=0; i < activeSystemListSize; i++) + { + if ((activeSystemList[i])->isActive && + (activeSystemList[i])->connectMode==RakPeer::RemoteSystemStruct::CONNECTED) + { + addresses.Push((activeSystemList[i])->systemAddress, _FILE_AND_LINE_ ); + guids.Push((activeSystemList[i])->guid, _FILE_AND_LINE_ ); + RakNetStatistics rns; + (activeSystemList[i])->reliabilityLayer.GetStatistics(&rns); + statistics.Push(rns, _FILE_AND_LINE_); + } + } +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +bool RakPeer::GetStatistics( const unsigned int index, RakNetStatistics *rns ) +{ + if (index < maximumNumberOfPeers && remoteSystemList[ index ].isActive) + { + remoteSystemList[ index ].reliabilityLayer.GetStatistics(rns); + return true; + } + return false; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +unsigned int RakPeer::GetReceiveBufferSize(void) +{ + unsigned int size; + packetReturnMutex.Lock(); + size=packetReturnQueue.Size(); + packetReturnMutex.Unlock(); + return size; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +int RakPeer::GetIndexFromSystemAddress( const SystemAddress systemAddress, bool calledFromNetworkThread ) const +{ + unsigned i; + + if ( systemAddress == UNASSIGNED_SYSTEM_ADDRESS ) + return -1; + + if (systemAddress.systemIndex!=(SystemIndex)-1 && systemAddress.systemIndex < maximumNumberOfPeers && remoteSystemList[systemAddress.systemIndex].systemAddress==systemAddress && remoteSystemList[ systemAddress.systemIndex ].isActive) + return systemAddress.systemIndex; + + if (calledFromNetworkThread) + { + return GetRemoteSystemIndex(systemAddress); + } + else + { + // remoteSystemList in user and network thread + for ( i = 0; i < maximumNumberOfPeers; i++ ) + if ( remoteSystemList[ i ].isActive && remoteSystemList[ i ].systemAddress == systemAddress ) + return i; + + // If no active results found, try previously active results. + for ( i = 0; i < maximumNumberOfPeers; i++ ) + if ( remoteSystemList[ i ].systemAddress == systemAddress ) + return i; + } + + return -1; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +int RakPeer::GetIndexFromGuid( const RakNetGUID guid ) +{ + unsigned i; + + if ( guid == UNASSIGNED_RAKNET_GUID ) + return -1; + + if (guid.systemIndex!=(SystemIndex)-1 && guid.systemIndex < maximumNumberOfPeers && remoteSystemList[guid.systemIndex].guid==guid && remoteSystemList[ guid.systemIndex ].isActive) + return guid.systemIndex; + + // remoteSystemList in user and network thread + for ( i = 0; i < maximumNumberOfPeers; i++ ) + if ( remoteSystemList[ i ].isActive && remoteSystemList[ i ].guid == guid ) + return i; + + // If no active results found, try previously active results. + for ( i = 0; i < maximumNumberOfPeers; i++ ) + if ( remoteSystemList[ i ].guid == guid ) + return i; + + return -1; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#if LIBCAT_SECURITY==1 +bool RakPeer::GenerateConnectionRequestChallenge(RequestedConnectionStruct *rcs,PublicKey *publicKey) +{ + CAT_AUDIT_PRINTF("AUDIT: In GenerateConnectionRequestChallenge()\n"); + + rcs->client_handshake = 0; + rcs->publicKeyMode = PKM_INSECURE_CONNECTION; + + if (!publicKey) return true; + + switch (publicKey->publicKeyMode) + { + default: + case PKM_INSECURE_CONNECTION: + break; + + case PKM_ACCEPT_ANY_PUBLIC_KEY: + CAT_OBJCLR(rcs->remote_public_key); + rcs->client_handshake = RakNet::OP_NEW(_FILE_AND_LINE_); + + rcs->publicKeyMode = PKM_ACCEPT_ANY_PUBLIC_KEY; + break; + + case PKM_USE_TWO_WAY_AUTHENTICATION: + if (publicKey->myPublicKey == 0 || publicKey->myPrivateKey == 0 || + publicKey->remoteServerPublicKey == 0) + { + return false; + } + + rcs->client_handshake = RakNet::OP_NEW(_FILE_AND_LINE_); + memcpy(rcs->remote_public_key, publicKey->remoteServerPublicKey, cat::EasyHandshake::PUBLIC_KEY_BYTES); + + if (!rcs->client_handshake->Initialize(publicKey->remoteServerPublicKey) || + !rcs->client_handshake->SetIdentity(publicKey->myPublicKey, publicKey->myPrivateKey) || + !rcs->client_handshake->GenerateChallenge(rcs->handshakeChallenge)) + { + CAT_AUDIT_PRINTF("AUDIT: Failure initializing new client_handshake object with identity for this RequestedConnectionStruct\n"); + RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); + rcs->client_handshake=0; + return false; + } + + CAT_AUDIT_PRINTF("AUDIT: Success initializing new client handshake object with identity for this RequestedConnectionStruct -- pre-generated challenge\n"); + + rcs->publicKeyMode = PKM_USE_TWO_WAY_AUTHENTICATION; + break; + + case PKM_USE_KNOWN_PUBLIC_KEY: + if (publicKey->remoteServerPublicKey == 0) + return false; + + rcs->client_handshake = RakNet::OP_NEW(_FILE_AND_LINE_); + memcpy(rcs->remote_public_key, publicKey->remoteServerPublicKey, cat::EasyHandshake::PUBLIC_KEY_BYTES); + + if (!rcs->client_handshake->Initialize(publicKey->remoteServerPublicKey) || + !rcs->client_handshake->GenerateChallenge(rcs->handshakeChallenge)) + { + CAT_AUDIT_PRINTF("AUDIT: Failure initializing new client_handshake object for this RequestedConnectionStruct\n"); + RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); + rcs->client_handshake=0; + return false; + } + + CAT_AUDIT_PRINTF("AUDIT: Success initializing new client handshake object for this RequestedConnectionStruct -- pre-generated challenge\n"); + + rcs->publicKeyMode = PKM_USE_KNOWN_PUBLIC_KEY; + break; + } + + return true; +} +#endif +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +ConnectionAttemptResult RakPeer::SendConnectionRequest( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned int extraData, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNet::TimeMS timeoutTime ) +{ + RakAssert(passwordDataLength <= 256); + RakAssert(remotePort!=0); + SystemAddress systemAddress; + if (!systemAddress.FromStringExplicitPort(host,remotePort,socketList[connectionSocketIndex]->GetBoundAddress().GetIPVersion())) + return CANNOT_RESOLVE_DOMAIN_NAME; + + // Already connected? + if (GetRemoteSystemFromSystemAddress(systemAddress, false, true)) + return ALREADY_CONNECTED_TO_ENDPOINT; + + //RequestedConnectionStruct *rcs = (RequestedConnectionStruct *) rakMalloc_Ex(sizeof(RequestedConnectionStruct), _FILE_AND_LINE_); + RequestedConnectionStruct *rcs = RakNet::OP_NEW(_FILE_AND_LINE_); + + rcs->systemAddress=systemAddress; + rcs->nextRequestTime=RakNet::GetTimeMS(); + rcs->requestsMade=0; + rcs->data=0; + rcs->socket=0; + rcs->extraData=extraData; + rcs->socketIndex=connectionSocketIndex; + rcs->actionToTake=RequestedConnectionStruct::CONNECT; + rcs->sendConnectionAttemptCount=sendConnectionAttemptCount; + rcs->timeBetweenSendConnectionAttemptsMS=timeBetweenSendConnectionAttemptsMS; + memcpy(rcs->outgoingPassword, passwordData, passwordDataLength); + rcs->outgoingPasswordLength=(unsigned char) passwordDataLength; + rcs->timeoutTime=timeoutTime; + +#if LIBCAT_SECURITY==1 + CAT_AUDIT_PRINTF("AUDIT: In SendConnectionRequest()\n"); + if (!GenerateConnectionRequestChallenge(rcs,publicKey)) + return SECURITY_INITIALIZATION_FAILED; +#else + (void) publicKey; +#endif + + // Return false if already pending, else push on queue + unsigned int i=0; + requestedConnectionQueueMutex.Lock(); + for (; i < requestedConnectionQueue.Size(); i++) + { + if (requestedConnectionQueue[i]->systemAddress==systemAddress) + { + requestedConnectionQueueMutex.Unlock(); + // Not necessary + //RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); + RakNet::OP_DELETE(rcs,_FILE_AND_LINE_); + return CONNECTION_ATTEMPT_ALREADY_IN_PROGRESS; + } + } + requestedConnectionQueue.Push(rcs, _FILE_AND_LINE_ ); + requestedConnectionQueueMutex.Unlock(); + + return CONNECTION_ATTEMPT_STARTED; +} +ConnectionAttemptResult RakPeer::SendConnectionRequest( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned int extraData, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNet::TimeMS timeoutTime, RakNetSocket2* socket ) +{ + RakAssert(passwordDataLength <= 256); + SystemAddress systemAddress; + systemAddress.FromStringExplicitPort(host,remotePort); + + // Already connected? + if (GetRemoteSystemFromSystemAddress(systemAddress, false, true)) + return ALREADY_CONNECTED_TO_ENDPOINT; + + //RequestedConnectionStruct *rcs = (RequestedConnectionStruct *) rakMalloc_Ex(sizeof(RequestedConnectionStruct), _FILE_AND_LINE_); + RequestedConnectionStruct *rcs = RakNet::OP_NEW(_FILE_AND_LINE_); + + rcs->systemAddress=systemAddress; + rcs->nextRequestTime=RakNet::GetTimeMS(); + rcs->requestsMade=0; + rcs->data=0; + rcs->socket=0; + rcs->extraData=extraData; + rcs->socketIndex=connectionSocketIndex; + rcs->actionToTake=RequestedConnectionStruct::CONNECT; + rcs->sendConnectionAttemptCount=sendConnectionAttemptCount; + rcs->timeBetweenSendConnectionAttemptsMS=timeBetweenSendConnectionAttemptsMS; + memcpy(rcs->outgoingPassword, passwordData, passwordDataLength); + rcs->outgoingPasswordLength=(unsigned char) passwordDataLength; + rcs->timeoutTime=timeoutTime; + rcs->socket=socket; + +#if LIBCAT_SECURITY==1 + if (!GenerateConnectionRequestChallenge(rcs,publicKey)) + return SECURITY_INITIALIZATION_FAILED; +#else + (void) publicKey; +#endif + + // Return false if already pending, else push on queue + unsigned int i=0; + requestedConnectionQueueMutex.Lock(); + for (; i < requestedConnectionQueue.Size(); i++) + { + if (requestedConnectionQueue[i]->systemAddress==systemAddress) + { + requestedConnectionQueueMutex.Unlock(); + // Not necessary + //RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); + RakNet::OP_DELETE(rcs,_FILE_AND_LINE_); + return CONNECTION_ATTEMPT_ALREADY_IN_PROGRESS; + } + } + requestedConnectionQueue.Push(rcs, _FILE_AND_LINE_ ); + requestedConnectionQueueMutex.Unlock(); + + return CONNECTION_ATTEMPT_STARTED; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::ValidateRemoteSystemLookup(void) const +{ +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +RakPeer::RemoteSystemStruct *RakPeer::GetRemoteSystem( const AddressOrGUID systemIdentifier, bool calledFromNetworkThread, bool onlyActive ) const +{ + if (systemIdentifier.rakNetGuid!=UNASSIGNED_RAKNET_GUID) + return GetRemoteSystemFromGUID(systemIdentifier.rakNetGuid, onlyActive); + else + return GetRemoteSystemFromSystemAddress(systemIdentifier.systemAddress, calledFromNetworkThread, onlyActive); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +RakPeer::RemoteSystemStruct *RakPeer::GetRemoteSystemFromSystemAddress( const SystemAddress systemAddress, bool calledFromNetworkThread, bool onlyActive ) const +{ + unsigned i; + + if ( systemAddress == UNASSIGNED_SYSTEM_ADDRESS ) + return 0; + + if (calledFromNetworkThread) + { + unsigned int index = GetRemoteSystemIndex(systemAddress); + if (index!=(unsigned int) -1) + { + if (onlyActive==false || remoteSystemList[ index ].isActive==true ) + { + RakAssert(remoteSystemList[index].systemAddress==systemAddress); + return remoteSystemList + index; + } + } + } + else + { + int deadConnectionIndex=-1; + + // Active connections take priority. But if there are no active connections, return the first systemAddress match found + for ( i = 0; i < maximumNumberOfPeers; i++ ) + { + if (remoteSystemList[ i ].systemAddress == systemAddress) + { + if ( remoteSystemList[ i ].isActive ) + return remoteSystemList + i; + else if (deadConnectionIndex==-1) + deadConnectionIndex=i; + } + } + + if (deadConnectionIndex!=-1 && onlyActive==false) + return remoteSystemList + deadConnectionIndex; + } + + return 0; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +RakPeer::RemoteSystemStruct *RakPeer::GetRemoteSystemFromGUID( const RakNetGUID guid, bool onlyActive ) const +{ + if (guid==UNASSIGNED_RAKNET_GUID) + return 0; + + unsigned i; + for ( i = 0; i < maximumNumberOfPeers; i++ ) + { + if (remoteSystemList[ i ].guid == guid && (onlyActive==false || remoteSystemList[ i ].isActive)) + { + return remoteSystemList + i; + } + } + return 0; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::ParseConnectionRequestPacket( RakPeer::RemoteSystemStruct *remoteSystem, const SystemAddress &systemAddress, const char *data, int byteSize ) +{ + RakNet::BitStream bs((unsigned char*) data,byteSize,false); + bs.IgnoreBytes(sizeof(MessageID)); + RakNetGUID guid; + bs.Read(guid); + RakNet::Time incomingTimestamp; + bs.Read(incomingTimestamp); + unsigned char doSecurity; + bs.Read(doSecurity); + +#if LIBCAT_SECURITY==1 + unsigned char doClientKey; + if (_using_security) + { + // Ignore message on bad state + if (doSecurity != 1 || !remoteSystem->reliabilityLayer.GetAuthenticatedEncryption()) + return; + + // Validate client proof of key + unsigned char proof[cat::EasyHandshake::PROOF_BYTES]; + bs.ReadAlignedBytes(proof, sizeof(proof)); + if (!remoteSystem->reliabilityLayer.GetAuthenticatedEncryption()->ValidateProof(proof, sizeof(proof))) + { + remoteSystem->connectMode = RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY; + return; + } + + CAT_OBJCLR(remoteSystem->client_public_key); + + bs.Read(doClientKey); + + // Check if client wants to prove identity + if (doClientKey == 1) + { + // Read identity proof + unsigned char ident[cat::EasyHandshake::IDENTITY_BYTES]; + bs.ReadAlignedBytes(ident, sizeof(ident)); + + // If we are listening to these proofs, + if (_require_client_public_key) + { + // Validate client identity + if (!_server_handshake->VerifyInitiatorIdentity(remoteSystem->answer, ident, remoteSystem->client_public_key)) + { + RakNet::BitStream bitStream; + bitStream.Write((MessageID)ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY); // Report an error since the client is not providing an identity when it is necessary to connect + bitStream.Write((unsigned char)2); // Indicate client identity is invalid + SendImmediate((char*) bitStream.GetData(), bitStream.GetNumberOfBytesUsed(), IMMEDIATE_PRIORITY, RELIABLE, 0, systemAddress, false, false, RakNet::GetTimeUS(), 0); + remoteSystem->connectMode = RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY; + return; + } + } + + // Otherwise ignore the client public key + } + else + { + // If no client key was provided but it is required, + if (_require_client_public_key) + { + RakNet::BitStream bitStream; + bitStream.Write((MessageID)ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY); // Report an error since the client is not providing an identity when it is necessary to connect + bitStream.Write((unsigned char)1); // Indicate client identity is missing + SendImmediate((char*) bitStream.GetData(), bitStream.GetNumberOfBytesUsed(), IMMEDIATE_PRIORITY, RELIABLE, 0, systemAddress, false, false, RakNet::GetTimeUS(), 0); + remoteSystem->connectMode = RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY; + return; + } + } + } +#endif // LIBCAT_SECURITY + + unsigned char *password = bs.GetData()+BITS_TO_BYTES(bs.GetReadOffset()); + int passwordLength = byteSize - BITS_TO_BYTES(bs.GetReadOffset()); + if ( incomingPasswordLength != passwordLength || + memcmp( password, incomingPassword, incomingPasswordLength ) != 0 ) + { + CAT_AUDIT_PRINTF("AUDIT: Invalid password\n"); + // This one we only send once since we don't care if it arrives. + RakNet::BitStream bitStream; + bitStream.Write((MessageID)ID_INVALID_PASSWORD); + bitStream.Write(GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); + SendImmediate((char*) bitStream.GetData(), bitStream.GetNumberOfBytesUsed(), IMMEDIATE_PRIORITY, RELIABLE, 0, systemAddress, false, false, RakNet::GetTimeUS(), 0); + remoteSystem->connectMode=RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY; + return; + } + + // OK + remoteSystem->connectMode=RemoteSystemStruct::HANDLING_CONNECTION_REQUEST; + + OnConnectionRequest( remoteSystem, incomingTimestamp ); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::OnConnectionRequest( RakPeer::RemoteSystemStruct *remoteSystem, RakNet::Time incomingTimestamp ) +{ + RakNet::BitStream bitStream; + bitStream.Write((MessageID)ID_CONNECTION_REQUEST_ACCEPTED); + bitStream.Write(remoteSystem->systemAddress); + SystemIndex systemIndex = (SystemIndex) GetIndexFromSystemAddress( remoteSystem->systemAddress, true ); + RakAssert(systemIndex!=65535); + bitStream.Write(systemIndex); + for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++) + bitStream.Write(ipList[i]); + bitStream.Write(incomingTimestamp); + bitStream.Write(RakNet::GetTime()); + + SendImmediate((char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, RELIABLE_ORDERED, 0, remoteSystem->systemAddress, false, false, RakNet::GetTimeUS(), 0); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::NotifyAndFlagForShutdown( const SystemAddress systemAddress, bool performImmediate, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority ) +{ + RakNet::BitStream temp( sizeof(unsigned char) ); + temp.Write( (MessageID)ID_DISCONNECTION_NOTIFICATION ); + if (performImmediate) + { + SendImmediate((char*)temp.GetData(), temp.GetNumberOfBitsUsed(), disconnectionNotificationPriority, RELIABLE_ORDERED, orderingChannel, systemAddress, false, false, RakNet::GetTimeUS(), 0); + RemoteSystemStruct *rss=GetRemoteSystemFromSystemAddress(systemAddress, true, true); + rss->connectMode=RemoteSystemStruct::DISCONNECT_ASAP; + } + else + { + SendBuffered((const char*)temp.GetData(), temp.GetNumberOfBitsUsed(), disconnectionNotificationPriority, RELIABLE_ORDERED, orderingChannel, systemAddress, false, RemoteSystemStruct::DISCONNECT_ASAP, 0); + } +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +unsigned int RakPeer::GetNumberOfRemoteInitiatedConnections( void ) const +{ + if ( remoteSystemList == 0 || endThreads == true ) + return 0; + + unsigned int numberOfIncomingConnections; + numberOfIncomingConnections = 0; + unsigned int i; + for (i=0; i < activeSystemListSize; i++) + { + if ((activeSystemList[i])->isActive && + (activeSystemList[i])->connectMode==RakPeer::RemoteSystemStruct::CONNECTED && + (activeSystemList[i])->weInitiatedTheConnection==false + ) + { + numberOfIncomingConnections++; + } + } + return numberOfIncomingConnections; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +RakPeer::RemoteSystemStruct * RakPeer::AssignSystemAddressToRemoteSystemList( const SystemAddress systemAddress, RemoteSystemStruct::ConnectMode connectionMode, RakNetSocket2* incomingRakNetSocket, bool *thisIPConnectedRecently, SystemAddress bindingAddress, int incomingMTU, RakNetGUID guid, bool useSecurity ) +{ + RemoteSystemStruct * remoteSystem; + unsigned i,j,assignedIndex; + RakNet::TimeMS time = RakNet::GetTimeMS(); +#ifdef _DEBUG + RakAssert(systemAddress!=UNASSIGNED_SYSTEM_ADDRESS); +#endif + + if (limitConnectionFrequencyFromTheSameIP) + { + if (IsLoopbackAddress(systemAddress,false)==false) + { + for ( i = 0; i < maximumNumberOfPeers; i++ ) + { + if ( remoteSystemList[ i ].isActive==true && + remoteSystemList[ i ].systemAddress.EqualsExcludingPort(systemAddress) && + time >= remoteSystemList[ i ].connectionTime && + time - remoteSystemList[ i ].connectionTime < 100 + ) + { + // 4/13/09 Attackers can flood ID_OPEN_CONNECTION_REQUEST and use up all available connection slots + // Ignore connection attempts if this IP address connected within the last 100 milliseconds + *thisIPConnectedRecently=true; + ValidateRemoteSystemLookup(); + return 0; + } + } + } + } + + // Don't use a different port than what we received on + bindingAddress.CopyPort(incomingRakNetSocket->GetBoundAddress()); + + *thisIPConnectedRecently=false; + for ( assignedIndex = 0; assignedIndex < maximumNumberOfPeers; assignedIndex++ ) + { + if ( remoteSystemList[ assignedIndex ].isActive==false ) + { + // printf("--- Address %s has become active\n", systemAddress.ToString()); + + remoteSystem=remoteSystemList+assignedIndex; + ReferenceRemoteSystem(systemAddress, assignedIndex); + remoteSystem->MTUSize=defaultMTUSize; + remoteSystem->guid=guid; + remoteSystem->isActive = true; // This one line causes future incoming packets to go through the reliability layer + // Reserve this reliability layer for ourselves. + if (incomingMTU > remoteSystem->MTUSize) + remoteSystem->MTUSize=incomingMTU; + RakAssert(remoteSystem->MTUSize <= MAXIMUM_MTU_SIZE); + remoteSystem->reliabilityLayer.Reset(true, remoteSystem->MTUSize, useSecurity); + remoteSystem->reliabilityLayer.SetSplitMessageProgressInterval(splitMessageProgressInterval); + remoteSystem->reliabilityLayer.SetUnreliableTimeout(unreliableTimeout); + remoteSystem->reliabilityLayer.SetTimeoutTime(defaultTimeoutTime); + AddToActiveSystemList(assignedIndex); + if (incomingRakNetSocket->GetBoundAddress()==bindingAddress) + { + remoteSystem->rakNetSocket=incomingRakNetSocket; + } + else + { + char str[256]; + bindingAddress.ToString(true,str); + // See if this is an internal IP address. + // If so, force binding on it so we reply on the same IP address as they sent to. + unsigned int ipListIndex, foundIndex=(unsigned int)-1; + + for (ipListIndex=0; ipListIndex < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ipListIndex++) + { + if (ipList[ipListIndex]==UNASSIGNED_SYSTEM_ADDRESS) + break; + + if (bindingAddress.EqualsExcludingPort(ipList[ipListIndex])) + { + foundIndex=ipListIndex; + break; + } + } + + // 06/26/09 Unconfirmed report that Vista firewall blocks the reply if we force a binding + // For now use the incoming socket only + // Originally this code was to force a machine with multiple IP addresses to reply back on the IP + // that the datagram came in on + if (1 || foundIndex==(unsigned int)-1) + { + // Must not be an internal LAN address. Just use whatever socket it came in on + remoteSystem->rakNetSocket=incomingRakNetSocket; + } + else + { + /* + // Force binding + unsigned int socketListIndex; + for (socketListIndex=0; socketListIndex < socketList.Size(); socketListIndex++) + { + if (socketList[socketListIndex]->GetBoundAddress()==bindingAddress) + { + // Force binding with existing socket + remoteSystem->rakNetSocket=socketList[socketListIndex]; + break; + } + } + + if (socketListIndex==socketList.Size()) + { + char ipListFoundIndexStr[128]; + ipList[foundIndex].ToString(false,str); + + // Force binding with new socket + RakNetSocket* rns(RakNet::OP_NEW(_FILE_AND_LINE_)); + if (incomingRakNetSocket->GetRemotePortRakNetWasStartedOn()==0) + rns = SocketLayer::CreateBoundSocket( this, bindingAddress.GetPort(), incomingRakNetSocket->GetBlockingSocket(), ipListFoundIndexStr, 0, incomingRakNetSocket->GetExtraSocketOptions(), incomingRakNetSocket->GetSocketFamily(), incomingRakNetSocket->GetChromeInstance() ); + else + rns = SocketLayer::CreateBoundSocket_PS3Lobby( bindingAddress.GetPort(), incomingRakNetSocket->GetBlockingSocket(), ipListFoundIndexStr, incomingRakNetSocket->GetSocketFamily() ); + + + if (rns==0) + { + // Can't bind. Just use whatever socket it came in on + remoteSystem->rakNetSocket=incomingRakNetSocket; + } + else + { + rns->GetBoundAddress()=bindingAddress; + rns->SetUserConnectionSocketIndex((unsigned int)-1); + socketList.Push(rns, _FILE_AND_LINE_ ); + remoteSystem->rakNetSocket=rns; + + +#ifdef _WIN32 + int highPriority=THREAD_PRIORITY_ABOVE_NORMAL; +#else + int highPriority=-10; +#endif + + highPriority=0; + + + } + } + + */ + } + } + + for ( j = 0; j < (unsigned) PING_TIMES_ARRAY_SIZE; j++ ) + { + remoteSystem->pingAndClockDifferential[ j ].pingTime = 65535; + remoteSystem->pingAndClockDifferential[ j ].clockDifferential = 0; + } + + remoteSystem->connectMode=connectionMode; + remoteSystem->pingAndClockDifferentialWriteIndex = 0; + remoteSystem->lowestPing = 65535; + remoteSystem->nextPingTime = 0; // Ping immediately + remoteSystem->weInitiatedTheConnection = false; + remoteSystem->connectionTime = time; + remoteSystem->myExternalSystemAddress = UNASSIGNED_SYSTEM_ADDRESS; + remoteSystem->lastReliableSend=time; + +#ifdef _DEBUG + int indexLoopupCheck=GetIndexFromSystemAddress( systemAddress, true ); + if ((int) indexLoopupCheck!=(int) assignedIndex) + { + RakAssert((int) indexLoopupCheck==(int) assignedIndex); + } +#endif + + return remoteSystem; + } + } + + return 0; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Adjust the first four bytes (treated as unsigned int) of the pointer +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::ShiftIncomingTimestamp( unsigned char *data, const SystemAddress &systemAddress ) const +{ +#ifdef _DEBUG + RakAssert( IsActive() ); + RakAssert( data ); +#endif + + RakNet::BitStream timeBS( data, sizeof(RakNet::Time), false); + RakNet::Time encodedTimestamp; + timeBS.Read(encodedTimestamp); + + encodedTimestamp = encodedTimestamp - GetBestClockDifferential( systemAddress ); + timeBS.SetWriteOffset(0); + timeBS.Write(encodedTimestamp); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Thanks to Chris Taylor (cat02e@fsu.edu) for the improved timestamping algorithm +RakNet::Time RakPeer::GetBestClockDifferential( const SystemAddress systemAddress ) const +{ + RemoteSystemStruct *remoteSystem = GetRemoteSystemFromSystemAddress( systemAddress, true, true ); + + if ( remoteSystem == 0 ) + return 0; + + return GetClockDifferentialInt(remoteSystem); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +unsigned int RakPeer::RemoteSystemLookupHashIndex(const SystemAddress &sa) const +{ + return SystemAddress::ToInteger(sa) % ((unsigned int) maximumNumberOfPeers * REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::ReferenceRemoteSystem(const SystemAddress &sa, unsigned int remoteSystemListIndex) +{ +// #ifdef _DEBUG +// for ( int remoteSystemIndex = 0; remoteSystemIndex < maximumNumberOfPeers; ++remoteSystemIndex ) +// { +// if (remoteSystemList[remoteSystemIndex].isActive ) +// { +// unsigned int hashIndex = GetRemoteSystemIndex(remoteSystemList[remoteSystemIndex].systemAddress); +// RakAssert(hashIndex==remoteSystemIndex); +// } +// } +// #endif + + + SystemAddress oldAddress = remoteSystemList[remoteSystemListIndex].systemAddress; + if (oldAddress!=UNASSIGNED_SYSTEM_ADDRESS) + { + // The system might be active if rerouting +// RakAssert(remoteSystemList[remoteSystemListIndex].isActive==false); + + // Remove the reference if the reference is pointing to this inactive system + if (GetRemoteSystem(oldAddress)==&remoteSystemList[remoteSystemListIndex]) + DereferenceRemoteSystem(oldAddress); + } + DereferenceRemoteSystem(sa); + +// #ifdef _DEBUG +// for ( int remoteSystemIndex = 0; remoteSystemIndex < maximumNumberOfPeers; ++remoteSystemIndex ) +// { +// if (remoteSystemList[remoteSystemIndex].isActive ) +// { +// unsigned int hashIndex = GetRemoteSystemIndex(remoteSystemList[remoteSystemIndex].systemAddress); +// if (hashIndex!=remoteSystemIndex) +// { +// RakAssert(hashIndex==remoteSystemIndex); +// } +// } +// } +// #endif + + + remoteSystemList[remoteSystemListIndex].systemAddress=sa; + + unsigned int hashIndex = RemoteSystemLookupHashIndex(sa); + RemoteSystemIndex *rsi; + rsi = remoteSystemIndexPool.Allocate(_FILE_AND_LINE_); + if (remoteSystemLookup[hashIndex]==0) + { + rsi->next=0; + rsi->index=remoteSystemListIndex; + remoteSystemLookup[hashIndex]=rsi; + } + else + { + RemoteSystemIndex *cur = remoteSystemLookup[hashIndex]; + while (cur->next!=0) + { + cur=cur->next; + } + + rsi = remoteSystemIndexPool.Allocate(_FILE_AND_LINE_); + rsi->next=0; + rsi->index=remoteSystemListIndex; + cur->next=rsi; + } + +// #ifdef _DEBUG +// for ( int remoteSystemIndex = 0; remoteSystemIndex < maximumNumberOfPeers; ++remoteSystemIndex ) +// { +// if (remoteSystemList[remoteSystemIndex].isActive ) +// { +// unsigned int hashIndex = GetRemoteSystemIndex(remoteSystemList[remoteSystemIndex].systemAddress); +// RakAssert(hashIndex==remoteSystemIndex); +// } +// } +// #endif + + + RakAssert(GetRemoteSystemIndex(sa)==remoteSystemListIndex); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::DereferenceRemoteSystem(const SystemAddress &sa) +{ + unsigned int hashIndex = RemoteSystemLookupHashIndex(sa); + RemoteSystemIndex *cur = remoteSystemLookup[hashIndex]; + RemoteSystemIndex *last = 0; + while (cur!=0) + { + if (remoteSystemList[cur->index].systemAddress==sa) + { + if (last==0) + { + remoteSystemLookup[hashIndex]=cur->next; + } + else + { + last->next=cur->next; + } + remoteSystemIndexPool.Release(cur,_FILE_AND_LINE_); + break; + } + last=cur; + cur=cur->next; + } +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +unsigned int RakPeer::GetRemoteSystemIndex(const SystemAddress &sa) const +{ + unsigned int hashIndex = RemoteSystemLookupHashIndex(sa); + RemoteSystemIndex *cur = remoteSystemLookup[hashIndex]; + while (cur!=0) + { + if (remoteSystemList[cur->index].systemAddress==sa) + return cur->index; + cur=cur->next; + } + return (unsigned int) -1; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +RakPeer::RemoteSystemStruct* RakPeer::GetRemoteSystem(const SystemAddress &sa) const +{ + unsigned int remoteSystemIndex = GetRemoteSystemIndex(sa); + if (remoteSystemIndex==(unsigned int)-1) + return 0; + return remoteSystemList + remoteSystemIndex; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::ClearRemoteSystemLookup(void) +{ + remoteSystemIndexPool.Clear(_FILE_AND_LINE_); + RakNet::OP_DELETE_ARRAY(remoteSystemLookup,_FILE_AND_LINE_); + remoteSystemLookup=0; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::AddToActiveSystemList(unsigned int remoteSystemListIndex) +{ + activeSystemList[activeSystemListSize++]=remoteSystemList+remoteSystemListIndex; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::RemoveFromActiveSystemList(const SystemAddress &sa) +{ + unsigned int i; + for (i=0; i < activeSystemListSize; i++) + { + RemoteSystemStruct *rss=activeSystemList[i]; + if (rss->systemAddress==sa) + { + activeSystemList[i]=activeSystemList[activeSystemListSize-1]; + activeSystemListSize--; + return; + } + } + RakAssert("activeSystemList invalid, entry not found in RemoveFromActiveSystemList. Ensure that AddToActiveSystemList and RemoveFromActiveSystemList are called by the same thread." && 0); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +/* +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +unsigned int RakPeer::LookupIndexUsingHashIndex(const SystemAddress &sa) const +{ + unsigned int scanCount=0; + unsigned int index = RemoteSystemLookupHashIndex(sa); + if (remoteSystemLookup[index].index==(unsigned int)-1) + return (unsigned int) -1; + while (remoteSystemList[remoteSystemLookup[index].index].systemAddress!=sa) + { + if (++index==(unsigned int) maximumNumberOfPeers*REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE) + index=0; + if (++scanCount>(unsigned int) maximumNumberOfPeers*REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE) + return (unsigned int) -1; + if (remoteSystemLookup[index].index==-1) + return (unsigned int) -1; + } + return index; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +unsigned int RakPeer::RemoteSystemListIndexUsingHashIndex(const SystemAddress &sa) const +{ + unsigned int index = LookupIndexUsingHashIndex(sa); + if (index!=(unsigned int) -1) + { + return remoteSystemLookup[index].index; + } + return (unsigned int) -1; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +unsigned int RakPeer::FirstFreeRemoteSystemLookupIndex(const SystemAddress &sa) const +{ +// unsigned int collisionCount=0; + unsigned int index = RemoteSystemLookupHashIndex(sa); + while (remoteSystemLookup[index].index!=(unsigned int)-1) + { + if (++index==(unsigned int) maximumNumberOfPeers*REMOTE_SYSTEM_LOOKUP_HASH_MULTIPLE) + index=0; +// collisionCount++; + } +// printf("%i collisions. Using index %i\n", collisionCount, index); + return index; +} +*/ +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +bool RakPeer::IsLoopbackAddress(const AddressOrGUID &systemIdentifier, bool matchPort) const +{ + if (systemIdentifier.rakNetGuid!=UNASSIGNED_RAKNET_GUID) + return systemIdentifier.rakNetGuid==myGuid; + + for (int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS && ipList[i]!=UNASSIGNED_SYSTEM_ADDRESS; i++) + { + if (matchPort) + { + if (ipList[i]==systemIdentifier.systemAddress) + return true; + } + else + { + if (ipList[i].EqualsExcludingPort(systemIdentifier.systemAddress)) + return true; + } + } + + return (matchPort==true && systemIdentifier.systemAddress==firstExternalID) || + (matchPort==false && systemIdentifier.systemAddress.EqualsExcludingPort(firstExternalID)); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +SystemAddress RakPeer::GetLoopbackAddress(void) const +{ + + return ipList[0]; + + + +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +bool RakPeer::AllowIncomingConnections(void) const +{ + return GetNumberOfRemoteInitiatedConnections() < GetMaximumIncomingConnections(); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line) +{ + bufferedPacketsFreePoolMutex.Lock(); + bufferedPacketsFreePool.Push(s, file, line); + bufferedPacketsFreePoolMutex.Unlock(); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +RNS2RecvStruct *RakPeer::AllocRNS2RecvStruct(const char *file, unsigned int line) +{ + bufferedPacketsFreePoolMutex.Lock(); + if (bufferedPacketsFreePool.Size()>0) + { + RNS2RecvStruct *s = bufferedPacketsFreePool.Pop(); + bufferedPacketsFreePoolMutex.Unlock(); + return s; + } + else + { + bufferedPacketsFreePoolMutex.Unlock(); + return RakNet::OP_NEW(file,line); + } +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::ClearBufferedPackets(void) +{ + bufferedPacketsFreePoolMutex.Lock(); + while (bufferedPacketsFreePool.Size()>0) + RakNet::OP_DELETE(bufferedPacketsFreePool.Pop(), _FILE_AND_LINE_); + bufferedPacketsFreePoolMutex.Unlock(); + + bufferedPacketsQueueMutex.Lock(); + while (bufferedPacketsQueue.Size()>0) + RakNet::OP_DELETE(bufferedPacketsQueue.Pop(), _FILE_AND_LINE_); + bufferedPacketsQueueMutex.Unlock(); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::SetupBufferedPackets(void) +{ +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::PushBufferedPacket(RNS2RecvStruct * p) +{ + bufferedPacketsQueueMutex.Lock(); + bufferedPacketsQueue.Push(p, _FILE_AND_LINE_); + bufferedPacketsQueueMutex.Unlock(); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +RNS2RecvStruct *RakPeer::PopBufferedPacket(void) +{ + bufferedPacketsQueueMutex.Lock(); + if (bufferedPacketsQueue.Size()>0) + { + RNS2RecvStruct *s = bufferedPacketsQueue.Pop(); + bufferedPacketsQueueMutex.Unlock(); + return s; + } + bufferedPacketsQueueMutex.Unlock(); + return 0; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::PingInternal( const SystemAddress target, bool performImmediate, PacketReliability reliability ) +{ + if ( IsActive() == false ) + return ; + + RakNet::BitStream bitStream(sizeof(unsigned char)+sizeof(RakNet::Time)); + bitStream.Write((MessageID)ID_CONNECTED_PING); + bitStream.Write(RakNet::GetTime()); + if (performImmediate) + SendImmediate( (char*)bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, reliability, 0, target, false, false, RakNet::GetTimeUS(), 0 ); + else + Send( &bitStream, IMMEDIATE_PRIORITY, reliability, 0, target, false ); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::CloseConnectionInternal( const AddressOrGUID& systemIdentifier, bool sendDisconnectionNotification, bool performImmediate, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority ) +{ +#ifdef _DEBUG + RakAssert(orderingChannel < 32); +#endif + + if (systemIdentifier.IsUndefined()) + return; + + if ( remoteSystemList == 0 || endThreads == true ) + return; + + SystemAddress target; + if (systemIdentifier.systemAddress!=UNASSIGNED_SYSTEM_ADDRESS) + { + target=systemIdentifier.systemAddress; + } + else + { + target=GetSystemAddressFromGuid(systemIdentifier.rakNetGuid); + } + + if (target!=UNASSIGNED_SYSTEM_ADDRESS && performImmediate) + target.FixForIPVersion(socketList[0]->GetBoundAddress()); + + if (sendDisconnectionNotification) + { + NotifyAndFlagForShutdown(target, performImmediate, orderingChannel, disconnectionNotificationPriority); + } + else + { + if (performImmediate) + { + unsigned int index = GetRemoteSystemIndex(target); + if (index!=(unsigned int) -1) + { + if ( remoteSystemList[index].isActive ) + { + RemoveFromActiveSystemList(target); + + // Found the index to stop + // printf("--- Address %s has become inactive\n", remoteSystemList[index].systemAddress.ToString()); + remoteSystemList[index].isActive = false; + + remoteSystemList[index].guid=UNASSIGNED_RAKNET_GUID; + + // Reserve this reliability layer for ourselves + //remoteSystemList[ remoteSystemLookup[index].index ].systemAddress = UNASSIGNED_SYSTEM_ADDRESS; + + // Clear any remaining messages + RakAssert(remoteSystemList[index].MTUSize <= MAXIMUM_MTU_SIZE); + remoteSystemList[index].reliabilityLayer.Reset(false, remoteSystemList[index].MTUSize, false); + + // Not using this socket + remoteSystemList[index].rakNetSocket = 0; + } + } + } + else + { + BufferedCommandStruct *bcs; + bcs=bufferedCommands.Allocate( _FILE_AND_LINE_ ); + bcs->command=BufferedCommandStruct::BCS_CLOSE_CONNECTION; + bcs->systemIdentifier=target; + bcs->data=0; + bcs->orderingChannel=orderingChannel; + bcs->priority=disconnectionNotificationPriority; + bufferedCommands.Push(bcs); + } + } +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::SendBuffered( const char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt ) +{ + BufferedCommandStruct *bcs; + + bcs=bufferedCommands.Allocate( _FILE_AND_LINE_ ); + bcs->data = (char*) rakMalloc_Ex( (size_t) BITS_TO_BYTES(numberOfBitsToSend), _FILE_AND_LINE_ ); // Making a copy doesn't lose efficiency because I tell the reliability layer to use this allocation for its own copy + if (bcs->data==0) + { + notifyOutOfMemory(_FILE_AND_LINE_); + bufferedCommands.Deallocate(bcs, _FILE_AND_LINE_); + return; + } + + RakAssert( !( reliability >= NUMBER_OF_RELIABILITIES || reliability < 0 ) ); + RakAssert( !( priority > NUMBER_OF_PRIORITIES || priority < 0 ) ); + RakAssert( !( orderingChannel >= NUMBER_OF_ORDERED_STREAMS ) ); + + memcpy(bcs->data, data, (size_t) BITS_TO_BYTES(numberOfBitsToSend)); + bcs->numberOfBitsToSend=numberOfBitsToSend; + bcs->priority=priority; + bcs->reliability=reliability; + bcs->orderingChannel=orderingChannel; + bcs->systemIdentifier=systemIdentifier; + bcs->broadcast=broadcast; + bcs->connectionMode=connectionMode; + bcs->receipt=receipt; + bcs->command=BufferedCommandStruct::BCS_SEND; + bufferedCommands.Push(bcs); + + if (priority==IMMEDIATE_PRIORITY) + { + // Forces pending sends to go out now, rather than waiting to the next update interval + quitAndDataEvents.SetEvent(); + } +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::SendBufferedList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt ) +{ + BufferedCommandStruct *bcs; + unsigned int totalLength=0; + unsigned int lengthOffset; + int i; + for (i=0; i < numParameters; i++) + { + if (lengths[i]>0) + totalLength+=lengths[i]; + } + if (totalLength==0) + return; + + char *dataAggregate; + dataAggregate = (char*) rakMalloc_Ex( (size_t) totalLength, _FILE_AND_LINE_ ); // Making a copy doesn't lose efficiency because I tell the reliability layer to use this allocation for its own copy + if (dataAggregate==0) + { + notifyOutOfMemory(_FILE_AND_LINE_); + return; + } + for (i=0, lengthOffset=0; i < numParameters; i++) + { + if (lengths[i]>0) + { + memcpy(dataAggregate+lengthOffset, data[i], lengths[i]); + lengthOffset+=lengths[i]; + } + } + + if (broadcast==false && IsLoopbackAddress(systemIdentifier,true)) + { + SendLoopback(dataAggregate,totalLength); + rakFree_Ex(dataAggregate,_FILE_AND_LINE_); + return; + } + + RakAssert( !( reliability >= NUMBER_OF_RELIABILITIES || reliability < 0 ) ); + RakAssert( !( priority > NUMBER_OF_PRIORITIES || priority < 0 ) ); + RakAssert( !( orderingChannel >= NUMBER_OF_ORDERED_STREAMS ) ); + + bcs=bufferedCommands.Allocate( _FILE_AND_LINE_ ); + bcs->data = dataAggregate; + bcs->numberOfBitsToSend=BYTES_TO_BITS(totalLength); + bcs->priority=priority; + bcs->reliability=reliability; + bcs->orderingChannel=orderingChannel; + bcs->systemIdentifier=systemIdentifier; + bcs->broadcast=broadcast; + bcs->connectionMode=connectionMode; + bcs->receipt=receipt; + bcs->command=BufferedCommandStruct::BCS_SEND; + bufferedCommands.Push(bcs); + + if (priority==IMMEDIATE_PRIORITY) + { + // Forces pending sends to go out now, rather than waiting to the next update interval + quitAndDataEvents.SetEvent(); + } +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +bool RakPeer::SendImmediate( char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, bool useCallerDataAllocation, RakNet::TimeUS currentTime, uint32_t receipt ) +{ + unsigned *sendList; + unsigned sendListSize; + bool callerDataAllocationUsed; + unsigned int remoteSystemIndex, sendListIndex; // Iterates into the list of remote systems +// unsigned numberOfBytesUsed = (unsigned) BITS_TO_BYTES(numberOfBitsToSend); + callerDataAllocationUsed=false; + + sendListSize=0; + + if (systemIdentifier.systemAddress!=UNASSIGNED_SYSTEM_ADDRESS) + remoteSystemIndex=GetIndexFromSystemAddress( systemIdentifier.systemAddress, true ); + else if (systemIdentifier.rakNetGuid!=UNASSIGNED_RAKNET_GUID) + remoteSystemIndex=GetSystemIndexFromGuid(systemIdentifier.rakNetGuid); + else + remoteSystemIndex=(unsigned int) -1; + + // 03/06/06 - If broadcast is false, use the optimized version of GetIndexFromSystemAddress + if (broadcast==false) + { + if (remoteSystemIndex==(unsigned int) -1) + { +#ifdef _DEBUG +// int debugIndex = GetRemoteSystemIndex(systemIdentifier.systemAddress); +#endif + return false; + } + + #if USE_ALLOCA==1 + sendList=(unsigned *)alloca(sizeof(unsigned)); + #else + sendList = (unsigned *) rakMalloc_Ex(sizeof(unsigned), _FILE_AND_LINE_); + #endif + + if (remoteSystemList[remoteSystemIndex].isActive && + remoteSystemList[remoteSystemIndex].connectMode!=RemoteSystemStruct::DISCONNECT_ASAP && + remoteSystemList[remoteSystemIndex].connectMode!=RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY && + remoteSystemList[remoteSystemIndex].connectMode!=RemoteSystemStruct::DISCONNECT_ON_NO_ACK) + { + sendList[0]=remoteSystemIndex; + sendListSize=1; + } + } + else + { + #if USE_ALLOCA==1 + sendList=(unsigned *)alloca(sizeof(unsigned)*maximumNumberOfPeers); + #else + sendList = (unsigned *) rakMalloc_Ex(sizeof(unsigned)*maximumNumberOfPeers, _FILE_AND_LINE_); + #endif + + // remoteSystemList in network thread + unsigned int idx; + for ( idx = 0; idx < maximumNumberOfPeers; idx++ ) + { + if (remoteSystemIndex!=(unsigned int) -1 && idx==remoteSystemIndex) + continue; + + if ( remoteSystemList[ idx ].isActive && remoteSystemList[ idx ].systemAddress != UNASSIGNED_SYSTEM_ADDRESS ) + sendList[sendListSize++]=idx; + } + } + + if (sendListSize==0) + { + #if !defined(USE_ALLOCA) + rakFree_Ex(sendList, _FILE_AND_LINE_ ); + #endif + + return false; + } + + for (sendListIndex=0; sendListIndex < sendListSize; sendListIndex++) + { + // Send may split the packet and thus deallocate data. Don't assume data is valid if we use the callerAllocationData + bool useData = useCallerDataAllocation && callerDataAllocationUsed==false && sendListIndex+1==sendListSize; + remoteSystemList[sendList[sendListIndex]].reliabilityLayer.Send( data, numberOfBitsToSend, priority, reliability, orderingChannel, useData==false, remoteSystemList[sendList[sendListIndex]].MTUSize, currentTime, receipt ); + if (useData) + callerDataAllocationUsed=true; + + if (reliability==RELIABLE || + reliability==RELIABLE_ORDERED || + reliability==RELIABLE_SEQUENCED || + reliability==RELIABLE_WITH_ACK_RECEIPT || + reliability==RELIABLE_ORDERED_WITH_ACK_RECEIPT +// || +// reliability==RELIABLE_SEQUENCED_WITH_ACK_RECEIPT + ) + remoteSystemList[sendList[sendListIndex]].lastReliableSend=(RakNet::TimeMS)(currentTime/(RakNet::TimeUS)1000); + } + +#if !defined(USE_ALLOCA) + rakFree_Ex(sendList, _FILE_AND_LINE_ ); +#endif + + // Return value only meaningful if true was passed for useCallerDataAllocation. Means the reliability layer used that data copy, so the caller should not deallocate it + return callerDataAllocationUsed; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::ResetSendReceipt(void) +{ + sendReceiptSerialMutex.Lock(); + sendReceiptSerial=1; + sendReceiptSerialMutex.Unlock(); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::OnConnectedPong(RakNet::Time sendPingTime, RakNet::Time sendPongTime, RemoteSystemStruct *remoteSystem) +{ + RakNet::Time ping; +// RakNet::TimeMS lastPing; + RakNet::Time time = RakNet::GetTime(); // Update the time value to be accurate + if (time > sendPingTime) + ping = time - sendPingTime; + else + ping=0; + +// lastPing = remoteSystem->pingAndClockDifferential[ remoteSystem->pingAndClockDifferentialWriteIndex ].pingTime; + + remoteSystem->pingAndClockDifferential[ remoteSystem->pingAndClockDifferentialWriteIndex ].pingTime = ( unsigned short ) ping; + // Thanks to Chris Taylor (cat02e@fsu.edu) for the improved timestamping algorithm + // Divide each integer by 2, rather than the sum by 2, to prevent overflow + remoteSystem->pingAndClockDifferential[ remoteSystem->pingAndClockDifferentialWriteIndex ].clockDifferential = sendPongTime - ( time/2 + sendPingTime/2 ); + + if ( remoteSystem->lowestPing == (unsigned short)-1 || remoteSystem->lowestPing > (int) ping ) + remoteSystem->lowestPing = (unsigned short) ping; + + if ( ++( remoteSystem->pingAndClockDifferentialWriteIndex ) == (RakNet::Time) PING_TIMES_ARRAY_SIZE ) + remoteSystem->pingAndClockDifferentialWriteIndex = 0; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::ClearBufferedCommands(void) +{ + BufferedCommandStruct *bcs; + + while ((bcs=bufferedCommands.Pop())!=0) + { + if (bcs->data) + rakFree_Ex(bcs->data, _FILE_AND_LINE_ ); + + bufferedCommands.Deallocate(bcs, _FILE_AND_LINE_); + } + bufferedCommands.Clear(_FILE_AND_LINE_); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::ClearSocketQueryOutput(void) +{ + socketQueryOutput.Clear(_FILE_AND_LINE_); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::ClearRequestedConnectionList(void) +{ + DataStructures::Queue freeQueue; + requestedConnectionQueueMutex.Lock(); + while (requestedConnectionQueue.Size()) + freeQueue.Push(requestedConnectionQueue.Pop(), _FILE_AND_LINE_ ); + requestedConnectionQueueMutex.Unlock(); + unsigned i; + for (i=0; i < freeQueue.Size(); i++) + { +#if LIBCAT_SECURITY==1 + CAT_AUDIT_PRINTF("AUDIT: In ClearRequestedConnectionList(), Deleting freeQueue index %i client_handshake %x\n", i, freeQueue[i]->client_handshake); + RakNet::OP_DELETE(freeQueue[i]->client_handshake,_FILE_AND_LINE_); +#endif + RakNet::OP_DELETE(freeQueue[i], _FILE_AND_LINE_ ); + } +} +inline void RakPeer::AddPacketToProducer(RakNet::Packet *p) +{ + packetReturnMutex.Lock(); + packetReturnQueue.Push(p,_FILE_AND_LINE_); + packetReturnMutex.Unlock(); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +union Buff6AndBuff8 +{ + unsigned char buff6[6]; + uint64_t buff8; +}; +uint64_t RakPeerInterface::Get64BitUniqueRandomNumber(void) +{ + // Mac address is a poor solution because you can't have multiple connections from the same system + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#if defined(_WIN32) + uint64_t g=RakNet::GetTimeUS(); + + RakNet::TimeUS lastTime, thisTime; + int j; + // Sleep a small random time, then use the last 4 bits as a source of randomness + for (j=0; j < 8; j++) + { + lastTime = RakNet::GetTimeUS(); + RakSleep(1); + RakSleep(0); + thisTime = RakNet::GetTimeUS(); + RakNet::TimeUS diff = thisTime-lastTime; + unsigned int diff4Bits = (unsigned int) (diff & 15); + diff4Bits <<= 32-4; + diff4Bits >>= j*4; + ((char*)&g)[j] ^= diff4Bits; + } + return g; + +#else + struct timeval tv; + gettimeofday(&tv, nullptr); + return tv.tv_usec + tv.tv_sec * 1000000; +#endif +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::GenerateGUID(void) +{ + myGuid.g=Get64BitUniqueRandomNumber(); + +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// void RakNet::ProcessPortUnreachable( SystemAddress systemAddress, RakPeer *rakPeer ) +// { +// (void) binaryAddress; +// (void) port; +// (void) rakPeer; +// +// } +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +namespace RakNet { +bool ProcessOfflineNetworkPacket( SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSocket2* rakNetSocket, bool *isOfflineMessage, RakNet::TimeUS timeRead ) +{ + (void) timeRead; + RakPeer::RemoteSystemStruct *remoteSystem; + RakNet::Packet *packet; + unsigned i; + + + char str1[64]; + systemAddress.ToString(false, str1); + if (rakPeer->IsBanned( str1 )) + { + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress); + + RakNet::BitStream bs; + bs.Write((MessageID)ID_CONNECTION_BANNED); + bs.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); + bs.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); + + + RNS2_SendParameters bsp; + bsp.data = (char*) bs.GetData(); + bsp.length = bs.GetNumberOfBytesUsed(); + bsp.systemAddress = systemAddress; + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketSend((char*) bs.GetData(), bs.GetNumberOfBitsUsed(), systemAddress); + rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + +/* + unsigned i; + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketSend((char*) bs.GetData(), bs.GetNumberOfBitsUsed(), systemAddress); + SocketLayer::SendTo( rakNetSocket, (char*) bs.GetData(), bs.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); + */ + + return true; + } + + + + // The reason for all this is that the reliability layer has no way to tell between offline messages that arrived late for a player that is now connected, + // and a regular encoding. So I insert OFFLINE_MESSAGE_DATA_ID into the stream, the encoding of which is essentially impossible to hit by chance + if (length <=2) + { + *isOfflineMessage=true; + } + else if ( + ((unsigned char)data[0] == ID_UNCONNECTED_PING || + (unsigned char)data[0] == ID_UNCONNECTED_PING_OPEN_CONNECTIONS) && + length >= sizeof(unsigned char) + sizeof(RakNet::Time) + sizeof(OFFLINE_MESSAGE_DATA_ID)) + { + *isOfflineMessage=memcmp(data+sizeof(unsigned char) + sizeof(RakNet::Time), OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0; + } + else if ((unsigned char)data[0] == ID_UNCONNECTED_PONG && (size_t) length >= sizeof(unsigned char) + sizeof(RakNet::TimeMS) + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID)) + { + *isOfflineMessage=memcmp(data+sizeof(unsigned char) + sizeof(RakNet::Time) + RakNetGUID::size(), OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0; + } + else if ( + (unsigned char)data[0] == ID_OUT_OF_BAND_INTERNAL && + (size_t) length >= sizeof(MessageID) + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID)) + { + *isOfflineMessage=memcmp(data+sizeof(MessageID) + RakNetGUID::size(), OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0; + } + else if ( + ( + (unsigned char)data[0] == ID_OPEN_CONNECTION_REPLY_1 || + (unsigned char)data[0] == ID_OPEN_CONNECTION_REPLY_2 || + (unsigned char)data[0] == ID_OPEN_CONNECTION_REQUEST_1 || + (unsigned char)data[0] == ID_OPEN_CONNECTION_REQUEST_2 || + (unsigned char)data[0] == ID_CONNECTION_ATTEMPT_FAILED || + (unsigned char)data[0] == ID_NO_FREE_INCOMING_CONNECTIONS || + (unsigned char)data[0] == ID_CONNECTION_BANNED || + (unsigned char)data[0] == ID_ALREADY_CONNECTED || + (unsigned char)data[0] == ID_IP_RECENTLY_CONNECTED) && + (size_t) length >= sizeof(MessageID) + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID)) + { + *isOfflineMessage=memcmp(data+sizeof(MessageID), OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0; + } + else if (((unsigned char)data[0] == ID_INCOMPATIBLE_PROTOCOL_VERSION&& + (size_t) length == sizeof(MessageID)*2 + RakNetGUID::size() + sizeof(OFFLINE_MESSAGE_DATA_ID))) + { + *isOfflineMessage=memcmp(data+sizeof(MessageID)*2, OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID))==0; + } + else + { + *isOfflineMessage=false; + } + + if (*isOfflineMessage) + { + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress); + + // These are all messages from unconnected systems. Messages here can be any size, but are never processed from connected systems. + if ( ( (unsigned char) data[ 0 ] == ID_UNCONNECTED_PING_OPEN_CONNECTIONS + || (unsigned char)(data)[0] == ID_UNCONNECTED_PING) && length >= sizeof(unsigned char)+sizeof(RakNet::Time)+sizeof(OFFLINE_MESSAGE_DATA_ID) ) + { + if ( (unsigned char)(data)[0] == ID_UNCONNECTED_PING || + rakPeer->AllowIncomingConnections() ) // Open connections with players + { + RakNet::BitStream inBitStream( (unsigned char *) data, length, false ); + inBitStream.IgnoreBits(8); + RakNet::Time sendPingTime; + inBitStream.Read(sendPingTime); + inBitStream.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID)); + RakNetGUID remoteGuid=UNASSIGNED_RAKNET_GUID; + inBitStream.Read(remoteGuid); + + RakNet::BitStream outBitStream; + outBitStream.Write((MessageID)ID_UNCONNECTED_PONG); // Should be named ID_UNCONNECTED_PONG eventually + outBitStream.Write(sendPingTime); + outBitStream.Write(rakPeer->myGuid); + outBitStream.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); + + rakPeer->rakPeerMutexes[ RakPeer::offlinePingResponse_Mutex ].Lock(); + // They are connected, so append offline ping data + outBitStream.Write( (char*)rakPeer->offlinePingResponse.GetData(), rakPeer->offlinePingResponse.GetNumberOfBytesUsed() ); + rakPeer->rakPeerMutexes[ RakPeer::offlinePingResponse_Mutex ].Unlock(); + + unsigned i; + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*)outBitStream.GetData(), outBitStream.GetNumberOfBytesUsed(), systemAddress); + + RNS2_SendParameters bsp; + bsp.data = (char*) outBitStream.GetData(); + bsp.length = outBitStream.GetNumberOfBytesUsed(); + bsp.systemAddress = systemAddress; + rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + + // SocketLayer::SendTo( rakNetSocket, (const char*)outBitStream.GetData(), (unsigned int) outBitStream.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); + + packet=rakPeer->AllocPacket(sizeof(MessageID), _FILE_AND_LINE_); + packet->data[0]=data[0]; + packet->systemAddress = systemAddress; + packet->guid=remoteGuid; + packet->systemAddress.systemIndex = ( SystemIndex ) rakPeer->GetIndexFromSystemAddress( systemAddress, true ); + packet->guid.systemIndex=packet->systemAddress.systemIndex; + rakPeer->AddPacketToProducer(packet); + } + } + // UNCONNECTED MESSAGE Pong with no data. + else if ((unsigned char) data[ 0 ] == ID_UNCONNECTED_PONG && (size_t) length >= sizeof(unsigned char)+sizeof(RakNet::Time)+RakNetGUID::size()+sizeof(OFFLINE_MESSAGE_DATA_ID) && (size_t) length < sizeof(unsigned char)+sizeof(RakNet::Time)+RakNetGUID::size()+sizeof(OFFLINE_MESSAGE_DATA_ID)+MAX_OFFLINE_DATA_LENGTH) + { + packet=rakPeer->AllocPacket((unsigned int) (length-sizeof(OFFLINE_MESSAGE_DATA_ID)-RakNetGUID::size()-sizeof(RakNet::Time)+sizeof(RakNet::TimeMS)), _FILE_AND_LINE_); + RakNet::BitStream bsIn((unsigned char*) data, length, false); + bsIn.IgnoreBytes(sizeof(unsigned char)); + RakNet::Time ping; + bsIn.Read(ping); + bsIn.Read(packet->guid); + + RakNet::BitStream bsOut((unsigned char*) packet->data, packet->length, false); + bsOut.ResetWritePointer(); + bsOut.Write((unsigned char)ID_UNCONNECTED_PONG); + RakNet::TimeMS pingMS=(RakNet::TimeMS)ping; + bsOut.Write(pingMS); + bsOut.WriteAlignedBytes( + (const unsigned char*)data+sizeof(unsigned char)+sizeof(RakNet::Time)+RakNetGUID::size()+sizeof(OFFLINE_MESSAGE_DATA_ID), + length-sizeof(unsigned char)-sizeof(RakNet::Time)-RakNetGUID::size()-sizeof(OFFLINE_MESSAGE_DATA_ID) + ); + + packet->systemAddress = systemAddress; + packet->systemAddress.systemIndex = ( SystemIndex ) rakPeer->GetIndexFromSystemAddress( systemAddress, true ); + packet->guid.systemIndex=packet->systemAddress.systemIndex; + rakPeer->AddPacketToProducer(packet); + } + else if ((unsigned char) data[ 0 ] == ID_OUT_OF_BAND_INTERNAL && + (size_t) length > sizeof(OFFLINE_MESSAGE_DATA_ID)+sizeof(MessageID)+RakNetGUID::size() && + (size_t) length < MAX_OFFLINE_DATA_LENGTH+sizeof(OFFLINE_MESSAGE_DATA_ID)+sizeof(MessageID)+RakNetGUID::size()) + { + unsigned int dataLength = (unsigned int) (length-sizeof(OFFLINE_MESSAGE_DATA_ID)-RakNetGUID::size()-sizeof(MessageID)); + RakAssert(dataLength<1024); + packet=rakPeer->AllocPacket(dataLength+1, _FILE_AND_LINE_); + RakAssert(packet->length<1024); + + RakNet::BitStream bs2((unsigned char*) data, length, false); + bs2.IgnoreBytes(sizeof(MessageID)); + bs2.Read(packet->guid); + + if (data[sizeof(OFFLINE_MESSAGE_DATA_ID)+sizeof(MessageID) + RakNetGUID::size()]==ID_ADVERTISE_SYSTEM) + { + packet->length--; + packet->bitSize=BYTES_TO_BITS(packet->length); + packet->data[0]=ID_ADVERTISE_SYSTEM; + memcpy(packet->data+1, data+sizeof(OFFLINE_MESSAGE_DATA_ID)+sizeof(MessageID)*2 + RakNetGUID::size(), dataLength-1); + } + else + { + packet->data[0]=ID_OUT_OF_BAND_INTERNAL; + memcpy(packet->data+1, data+sizeof(OFFLINE_MESSAGE_DATA_ID)+sizeof(MessageID) + RakNetGUID::size(), dataLength); + } + + packet->systemAddress = systemAddress; + packet->systemAddress.systemIndex = ( SystemIndex ) rakPeer->GetIndexFromSystemAddress( systemAddress, true ); + packet->guid.systemIndex=packet->systemAddress.systemIndex; + rakPeer->AddPacketToProducer(packet); + } + else if ((unsigned char)(data)[0] == (MessageID)ID_OPEN_CONNECTION_REPLY_1) + { + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress); + + RakNet::BitStream bsIn((unsigned char*) data,length,false); + bsIn.IgnoreBytes(sizeof(MessageID)); + bsIn.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID)); + RakNetGUID serverGuid; + bsIn.Read(serverGuid); + unsigned char serverHasSecurity; + uint32_t cookie; + (void) cookie; + bsIn.Read(serverHasSecurity); + // Even if the server has security, it may not be required of us if we are in the security exception list + if (serverHasSecurity) + { + bsIn.Read(cookie); + } + + RakNet::BitStream bsOut; + bsOut.Write((MessageID)ID_OPEN_CONNECTION_REQUEST_2); + bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); + if (serverHasSecurity) + bsOut.Write(cookie); + + unsigned i; + rakPeer->requestedConnectionQueueMutex.Lock(); + for (i=0; i < rakPeer->requestedConnectionQueue.Size(); i++) + { + RakPeer::RequestedConnectionStruct *rcs; + rcs=rakPeer->requestedConnectionQueue[i]; + if (rcs->systemAddress==systemAddress) + { + if (serverHasSecurity) + { +#if LIBCAT_SECURITY==1 + unsigned char public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES]; + bsIn.ReadAlignedBytes(public_key, sizeof(public_key)); + + if (rcs->publicKeyMode==PKM_ACCEPT_ANY_PUBLIC_KEY) + { + memcpy(rcs->remote_public_key, public_key, cat::EasyHandshake::PUBLIC_KEY_BYTES); + if (!rcs->client_handshake->Initialize(public_key) || + !rcs->client_handshake->GenerateChallenge(rcs->handshakeChallenge)) + { + CAT_AUDIT_PRINTF("AUDIT: Server passed a bad public key with PKM_ACCEPT_ANY_PUBLIC_KEY"); + return true; + } + } + + if (cat::SecureEqual(public_key, + rcs->remote_public_key, + cat::EasyHandshake::PUBLIC_KEY_BYTES)==false) + { + rakPeer->requestedConnectionQueueMutex.Unlock(); + CAT_AUDIT_PRINTF("AUDIT: Expected public key does not match what was sent by server -- Reporting back ID_PUBLIC_KEY_MISMATCH to user\n"); + + packet=rakPeer->AllocPacket(sizeof( char ), _FILE_AND_LINE_); + packet->data[ 0 ] = ID_PUBLIC_KEY_MISMATCH; // Attempted a connection and couldn't + packet->bitSize = ( sizeof( char ) * 8); + packet->systemAddress = rcs->systemAddress; + packet->guid=serverGuid; + rakPeer->AddPacketToProducer(packet); + return true; + } + + if (rcs->client_handshake==0) + { + // Message does not contain a challenge + // We might still pass if we are in the security exception list + bsOut.Write((unsigned char)0); + } + else + { + // Message contains a challenge + bsOut.Write((unsigned char)1); + // challenge + CAT_AUDIT_PRINTF("AUDIT: Sending challenge\n"); + bsOut.WriteAlignedBytes((const unsigned char*) rcs->handshakeChallenge,cat::EasyHandshake::CHALLENGE_BYTES); + } +#else // LIBCAT_SECURITY + // Message does not contain a challenge + bsOut.Write((unsigned char)0); +#endif // LIBCAT_SECURITY + } + else + { + // Server does not need security +#if LIBCAT_SECURITY==1 + if (rcs->client_handshake!=0) + { + rakPeer->requestedConnectionQueueMutex.Unlock(); + CAT_AUDIT_PRINTF("AUDIT: Security disabled by server but we expected security (indicated by client_handshake not null) so failing!\n"); + + packet=rakPeer->AllocPacket(sizeof( char ), _FILE_AND_LINE_); + packet->data[ 0 ] = ID_OUR_SYSTEM_REQUIRES_SECURITY; // Attempted a connection and couldn't + packet->bitSize = ( sizeof( char ) * 8); + packet->systemAddress = rcs->systemAddress; + packet->guid=serverGuid; + rakPeer->AddPacketToProducer(packet); + return true; + } +#endif // LIBCAT_SECURITY + + } + + uint16_t mtu; + bsIn.Read(mtu); + + // Binding address + bsOut.Write(rcs->systemAddress); + rakPeer->requestedConnectionQueueMutex.Unlock(); + // MTU + bsOut.Write(mtu); + // Our guid + bsOut.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); + + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), rcs->systemAddress); + + // SocketLayer::SendTo( rakPeer->socketList[rcs->socketIndex], (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), rcs->systemAddress, _FILE_AND_LINE_ ); + + RNS2_SendParameters bsp; + bsp.data = (char*) bsOut.GetData(); + bsp.length = bsOut.GetNumberOfBytesUsed(); + bsp.systemAddress = systemAddress; + rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + + return true; + } + } + rakPeer->requestedConnectionQueueMutex.Unlock(); + } + else if ((unsigned char)(data)[0] == (MessageID)ID_OPEN_CONNECTION_REPLY_2) + { + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress); + + RakNet::BitStream bs((unsigned char*) data,length,false); + bs.IgnoreBytes(sizeof(MessageID)); + bs.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID)); + RakNetGUID guid; + bs.Read(guid); + SystemAddress bindingAddress; + bool b = bs.Read(bindingAddress); + RakAssert(b); + uint16_t mtu; + b=bs.Read(mtu); + RakAssert(b); + bool doSecurity=false; + b=bs.Read(doSecurity); + RakAssert(b); + +#if LIBCAT_SECURITY==1 + char answer[cat::EasyHandshake::ANSWER_BYTES]; + CAT_AUDIT_PRINTF("AUDIT: Got ID_OPEN_CONNECTION_REPLY_2 and given doSecurity=%i\n", (int)doSecurity); + if (doSecurity) + { + CAT_AUDIT_PRINTF("AUDIT: Reading cookie and public key\n"); + bs.ReadAlignedBytes((unsigned char*) answer, sizeof(answer)); + } + cat::ClientEasyHandshake *client_handshake=0; +#endif // LIBCAT_SECURITY + + RakPeer::RequestedConnectionStruct *rcs; + bool unlock=true; + unsigned i; + rakPeer->requestedConnectionQueueMutex.Lock(); + for (i=0; i < rakPeer->requestedConnectionQueue.Size(); i++) + { + rcs=rakPeer->requestedConnectionQueue[i]; + + + if (rcs->systemAddress==systemAddress) + { +#if LIBCAT_SECURITY==1 + CAT_AUDIT_PRINTF("AUDIT: System address matches an entry in the requestedConnectionQueue and doSecurity=%i\n", (int)doSecurity); + if (doSecurity) + { + if (rcs->client_handshake==0) + { + CAT_AUDIT_PRINTF("AUDIT: Server wants security but we didn't set a public key -- Reporting back ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY to user\n"); + rakPeer->requestedConnectionQueueMutex.Unlock(); + + packet=rakPeer->AllocPacket(2, _FILE_AND_LINE_); + packet->data[ 0 ] = ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY; // Attempted a connection and couldn't + packet->data[ 1 ] = 0; // Indicate server public key is missing + packet->bitSize = ( sizeof( char ) * 8); + packet->systemAddress = rcs->systemAddress; + packet->guid=guid; + rakPeer->AddPacketToProducer(packet); + return true; + } + + CAT_AUDIT_PRINTF("AUDIT: Looks good, preparing to send challenge to server! client_handshake = %x\n", client_handshake); + } + +#endif // LIBCAT_SECURITY + + rakPeer->requestedConnectionQueueMutex.Unlock(); + unlock=false; + + RakAssert(rcs->actionToTake==RakPeer::RequestedConnectionStruct::CONNECT); + // You might get this when already connected because of cross-connections + bool thisIPConnectedRecently=false; + remoteSystem=rakPeer->GetRemoteSystemFromSystemAddress( systemAddress, true, true ); + if (remoteSystem==0) + { + if (rcs->socket == 0) + { + remoteSystem=rakPeer->AssignSystemAddressToRemoteSystemList(systemAddress, RakPeer::RemoteSystemStruct::UNVERIFIED_SENDER, rakNetSocket, &thisIPConnectedRecently, bindingAddress, mtu, guid, doSecurity); + } + else + { + remoteSystem=rakPeer->AssignSystemAddressToRemoteSystemList(systemAddress, RakPeer::RemoteSystemStruct::UNVERIFIED_SENDER, rcs->socket, &thisIPConnectedRecently, bindingAddress, mtu, guid, doSecurity); + } + } + + // 4/13/09 Attackers can flood ID_OPEN_CONNECTION_REQUEST and use up all available connection slots + // Ignore connection attempts if this IP address connected within the last 100 milliseconds + if (thisIPConnectedRecently==false) + { + // Don't check GetRemoteSystemFromGUID, server will verify + if (remoteSystem) + { + // Move pointer from RequestedConnectionStruct to RemoteSystemStruct +#if LIBCAT_SECURITY==1 + cat::u8 ident[cat::EasyHandshake::IDENTITY_BYTES]; + bool doIdentity = false; + + if (rcs->client_handshake) + { + CAT_AUDIT_PRINTF("AUDIT: Processing answer\n"); + if (rcs->publicKeyMode == PKM_USE_TWO_WAY_AUTHENTICATION) + { + if (!rcs->client_handshake->ProcessAnswerWithIdentity(answer, ident, remoteSystem->reliabilityLayer.GetAuthenticatedEncryption())) + { + CAT_AUDIT_PRINTF("AUDIT: Processing answer -- Invalid Answer\n"); + rakPeer->requestedConnectionQueueMutex.Unlock(); + + return true; + } + + doIdentity = true; + } + else + { + if (!rcs->client_handshake->ProcessAnswer(answer, remoteSystem->reliabilityLayer.GetAuthenticatedEncryption())) + { + CAT_AUDIT_PRINTF("AUDIT: Processing answer -- Invalid Answer\n"); + rakPeer->requestedConnectionQueueMutex.Unlock(); + + return true; + } + } + CAT_AUDIT_PRINTF("AUDIT: Success!\n"); + + RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); + rcs->client_handshake=0; + } +#endif // LIBCAT_SECURITY + + remoteSystem->weInitiatedTheConnection=true; + remoteSystem->connectMode=RakPeer::RemoteSystemStruct::REQUESTED_CONNECTION; + if (rcs->timeoutTime!=0) + remoteSystem->reliabilityLayer.SetTimeoutTime(rcs->timeoutTime); + + RakNet::BitStream temp; + temp.Write( (MessageID)ID_CONNECTION_REQUEST); + temp.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); + temp.Write(RakNet::GetTime()); + +#if LIBCAT_SECURITY==1 + temp.Write((unsigned char)(doSecurity ? 1 : 0)); + + if (doSecurity) + { + unsigned char proof[32]; + remoteSystem->reliabilityLayer.GetAuthenticatedEncryption()->GenerateProof(proof, sizeof(proof)); + temp.WriteAlignedBytes(proof, sizeof(proof)); + + temp.Write((unsigned char)(doIdentity ? 1 : 0)); + + if (doIdentity) + { + temp.WriteAlignedBytes(ident, sizeof(ident)); + } + } +#else + temp.Write((unsigned char)0); +#endif // LIBCAT_SECURITY + + if ( rcs->outgoingPasswordLength > 0 ) + temp.Write( ( char* ) rcs->outgoingPassword, rcs->outgoingPasswordLength ); + + rakPeer->SendImmediate((char*)temp.GetData(), temp.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, RELIABLE, 0, systemAddress, false, false, timeRead, 0 ); + } + else + { + // Failed, no connections available anymore + packet=rakPeer->AllocPacket(sizeof( char ), _FILE_AND_LINE_); + packet->data[ 0 ] = ID_CONNECTION_ATTEMPT_FAILED; // Attempted a connection and couldn't + packet->bitSize = ( sizeof( char ) * 8); + packet->systemAddress = rcs->systemAddress; + packet->guid=guid; + rakPeer->AddPacketToProducer(packet); + } + } + + rakPeer->requestedConnectionQueueMutex.Lock(); + for (unsigned int k=0; k < rakPeer->requestedConnectionQueue.Size(); k++) + { + if (rakPeer->requestedConnectionQueue[k]->systemAddress==systemAddress) + { + rakPeer->requestedConnectionQueue.RemoveAtIndex(k); + break; + } + } + rakPeer->requestedConnectionQueueMutex.Unlock(); + +#if LIBCAT_SECURITY==1 + CAT_AUDIT_PRINTF("AUDIT: Deleting client_handshake object %x and rcs->client_handshake object %x\n", client_handshake, rcs->client_handshake); + RakNet::OP_DELETE(client_handshake,_FILE_AND_LINE_); + RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); +#endif // LIBCAT_SECURITY + RakNet::OP_DELETE(rcs,_FILE_AND_LINE_); + + break; + } + } + + if (unlock) + rakPeer->requestedConnectionQueueMutex.Unlock(); + + return true; + + } + else if ((unsigned char)(data)[0] == (MessageID)ID_CONNECTION_ATTEMPT_FAILED || + (unsigned char)(data)[0] == (MessageID)ID_NO_FREE_INCOMING_CONNECTIONS || + (unsigned char)(data)[0] == (MessageID)ID_CONNECTION_BANNED || + (unsigned char)(data)[0] == (MessageID)ID_ALREADY_CONNECTED || + (unsigned char)(data)[0] == (MessageID)ID_INVALID_PASSWORD || + (unsigned char)(data)[0] == (MessageID)ID_IP_RECENTLY_CONNECTED || + (unsigned char)(data)[0] == (MessageID)ID_INCOMPATIBLE_PROTOCOL_VERSION) + { + + RakNet::BitStream bs((unsigned char*) data,length,false); + bs.IgnoreBytes(sizeof(MessageID)); + bs.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID)); + if ((unsigned char)(data)[0] == (MessageID)ID_INCOMPATIBLE_PROTOCOL_VERSION) + bs.IgnoreBytes(sizeof(unsigned char)); + + RakNetGUID guid; + bs.Read(guid); + + RakPeer::RequestedConnectionStruct *rcs; + bool connectionAttemptCancelled=false; + unsigned i; + rakPeer->requestedConnectionQueueMutex.Lock(); + for (i=0; i < rakPeer->requestedConnectionQueue.Size(); i++) + { + rcs=rakPeer->requestedConnectionQueue[i]; + if (rcs->actionToTake==RakPeer::RequestedConnectionStruct::CONNECT && rcs->systemAddress==systemAddress) + { + connectionAttemptCancelled=true; + rakPeer->requestedConnectionQueue.RemoveAtIndex(i); + +#if LIBCAT_SECURITY==1 + CAT_AUDIT_PRINTF("AUDIT: Connection attempt canceled so deleting rcs->client_handshake object %x\n", rcs->client_handshake); + RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); +#endif // LIBCAT_SECURITY + RakNet::OP_DELETE(rcs,_FILE_AND_LINE_); + break; + } + } + + rakPeer->requestedConnectionQueueMutex.Unlock(); + + if (connectionAttemptCancelled) + { + // Tell user of connection attempt failed + packet=rakPeer->AllocPacket(sizeof( char ), _FILE_AND_LINE_); + packet->data[ 0 ] = data[0]; // Attempted a connection and couldn't + packet->bitSize = ( sizeof( char ) * 8); + packet->systemAddress = systemAddress; + packet->guid=guid; + rakPeer->AddPacketToProducer(packet); + } + } + else if ((unsigned char)(data)[0] == ID_OPEN_CONNECTION_REQUEST_1 && length > (int) (1+sizeof(OFFLINE_MESSAGE_DATA_ID))) + {/* + static int x = 0; + ++x; + + SystemAddress *addr = (SystemAddress*)&systemAddress; + addr->binaryAddress += x;*/ + + unsigned int i; + //RAKNET_DEBUG_PRINTF("%i:IOCR, ", __LINE__); + char remoteProtocol=data[1+sizeof(OFFLINE_MESSAGE_DATA_ID)]; + if (remoteProtocol!=RAKNET_PROTOCOL_VERSION) + { + RakNet::BitStream bs; + bs.Write((MessageID)ID_INCOMPATIBLE_PROTOCOL_VERSION); + bs.Write((unsigned char)RAKNET_PROTOCOL_VERSION); + bs.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); + bs.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); + + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketSend((char*)bs.GetData(), bs.GetNumberOfBitsUsed(), systemAddress); + + // SocketLayer::SendTo( rakNetSocket, (char*)bs.GetData(), bs.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); + + RNS2_SendParameters bsp; + bsp.data = (char*) bs.GetData(); + bsp.length = bs.GetNumberOfBytesUsed(); + bsp.systemAddress = systemAddress; + + rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + return true; + } + + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketReceive(data, length*8, systemAddress); + + RakNet::BitStream bsOut; + bsOut.Write((MessageID)ID_OPEN_CONNECTION_REPLY_1); + bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); + bsOut.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); +#if LIBCAT_SECURITY==1 + if (rakPeer->_using_security) + { + bsOut.Write((unsigned char) 1); // HasCookie Yes + // Write cookie + uint32_t cookie = rakPeer->_cookie_jar->Generate(&systemAddress.address,sizeof(systemAddress.address)); + CAT_AUDIT_PRINTF("AUDIT: Writing cookie %i to %i:%i\n", cookie, systemAddress); + bsOut.Write(cookie); + // Write my public key + bsOut.WriteAlignedBytes((const unsigned char *) rakPeer->my_public_key,sizeof(rakPeer->my_public_key)); + } + else +#endif // LIBCAT_SECURITY + bsOut.Write((unsigned char) 0); // HasCookie oN + + // MTU. Lower MTU if it is exceeds our own limit + if (length+UDP_HEADER_SIZE > MAXIMUM_MTU_SIZE) + bsOut.WriteCasted(MAXIMUM_MTU_SIZE); + else + bsOut.WriteCasted(length+UDP_HEADER_SIZE); + + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); + // SocketLayer::SendTo( rakNetSocket, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); + + RNS2_SendParameters bsp; + bsp.data = (char*) bsOut.GetData(); + bsp.length = bsOut.GetNumberOfBytesUsed(); + bsp.systemAddress = systemAddress; + rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + } + else if ((unsigned char)(data)[0] == ID_OPEN_CONNECTION_REQUEST_2) + { + SystemAddress bindingAddress; + RakNetGUID guid; + RakNet::BitStream bsOut; + RakNet::BitStream bs((unsigned char*) data, length, false); + bs.IgnoreBytes(sizeof(MessageID)); + bs.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID)); + + bool requiresSecurityOfThisClient=false; +#if LIBCAT_SECURITY==1 + char remoteHandshakeChallenge[cat::EasyHandshake::CHALLENGE_BYTES]; + + if (rakPeer->_using_security) + { + char str1[64]; + systemAddress.ToString(false, str1); + requiresSecurityOfThisClient=rakPeer->IsInSecurityExceptionList(str1)==false; + + uint32_t cookie; + bs.Read(cookie); + CAT_AUDIT_PRINTF("AUDIT: Got cookie %i from %i:%i\n", cookie, systemAddress); + if (rakPeer->_cookie_jar->Verify(&systemAddress.address,sizeof(systemAddress.address), cookie)==false) + { + return true; + } + CAT_AUDIT_PRINTF("AUDIT: Cookie good!\n"); + + unsigned char clientWroteChallenge; + bs.Read(clientWroteChallenge); + + if (requiresSecurityOfThisClient==true && clientWroteChallenge==0) + { + // Fail, client doesn't support security, and it is required + return true; + } + + if (clientWroteChallenge) + { + bs.ReadAlignedBytes((unsigned char*) remoteHandshakeChallenge, cat::EasyHandshake::CHALLENGE_BYTES); +#ifdef CAT_AUDIT + printf("AUDIT: RECV CHALLENGE "); + for (int ii = 0; ii < sizeof(remoteHandshakeChallenge); ++ii) + { + printf("%02x", (cat::u8)remoteHandshakeChallenge[ii]); + } + printf("\n"); +#endif + } + } +#endif // LIBCAT_SECURITY + + bs.Read(bindingAddress); + uint16_t mtu; + bs.Read(mtu); + bs.Read(guid); + + RakPeer::RemoteSystemStruct *rssFromSA = rakPeer->GetRemoteSystemFromSystemAddress( systemAddress, true, true ); + bool IPAddrInUse = rssFromSA != 0 && rssFromSA->isActive; + RakPeer::RemoteSystemStruct *rssFromGuid = rakPeer->GetRemoteSystemFromGUID(guid, true); + bool GUIDInUse = rssFromGuid != 0 && rssFromGuid->isActive; + + // IPAddrInUse, GuidInUse, outcome + // TRUE, , TRUE , ID_OPEN_CONNECTION_REPLY if they are the same, else ID_ALREADY_CONNECTED + // FALSE, , TRUE , ID_ALREADY_CONNECTED (someone else took this guid) + // TRUE, , FALSE , ID_ALREADY_CONNECTED (silently disconnected, restarted rakNet) + // FALSE , FALSE , Allow connection + + int outcome; + if (IPAddrInUse & GUIDInUse) + { + if (rssFromSA==rssFromGuid && rssFromSA->connectMode==RakPeer::RemoteSystemStruct::UNVERIFIED_SENDER) + { + // ID_OPEN_CONNECTION_REPLY if they are the same + outcome=1; + + // Note to self: If REQUESTED_CONNECTION, this means two systems attempted to connect to each other at the same time, and one finished first. + // Returns ID)_CONNECTION_REQUEST_ACCEPTED to one system, and ID_ALREADY_CONNECTED followed by ID_NEW_INCOMING_CONNECTION to another + } + else + { + // ID_ALREADY_CONNECTED (restarted raknet, connected again from same ip, plus someone else took this guid) + outcome=2; + } + } + else if (IPAddrInUse==false && GUIDInUse==true) + { + // ID_ALREADY_CONNECTED (someone else took this guid) + outcome=3; + } + else if (IPAddrInUse==true && GUIDInUse==false) + { + // ID_ALREADY_CONNECTED (silently disconnected, restarted rakNet) + outcome=4; + } + else + { + // Allow connection + outcome=0; + } + + RakNet::BitStream bsAnswer; + bsAnswer.Write((MessageID)ID_OPEN_CONNECTION_REPLY_2); + bsAnswer.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); + bsAnswer.Write(rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS)); + bsAnswer.Write(systemAddress); + bsAnswer.Write(mtu); + bsAnswer.Write(requiresSecurityOfThisClient); + + if (outcome==1) + { + // Duplicate connection request packet from packetloss + // Send back the same answer +#if LIBCAT_SECURITY==1 + if (requiresSecurityOfThisClient) + { + CAT_AUDIT_PRINTF("AUDIT: Resending public key and answer from packetloss. Sending ID_OPEN_CONNECTION_REPLY_2\n"); + bsAnswer.WriteAlignedBytes((const unsigned char *) rssFromSA->answer,sizeof(rssFromSA->answer)); + } +#endif // LIBCAT_SECURITY + + unsigned int i; + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*) bsAnswer.GetData(), bsAnswer.GetNumberOfBitsUsed(), systemAddress); + // SocketLayer::SendTo( rakNetSocket, (const char*) bsAnswer.GetData(), bsAnswer.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); + + RNS2_SendParameters bsp; + bsp.data = (char*) bsAnswer.GetData(); + bsp.length = bsAnswer.GetNumberOfBytesUsed(); + bsp.systemAddress = systemAddress; + rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + + return true; + } + else if (outcome!=0) + { + bsOut.Write((MessageID)ID_ALREADY_CONNECTED); + bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); + bsOut.Write(rakPeer->myGuid); + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); + // SocketLayer::SendTo( rakNetSocket, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); + RNS2_SendParameters bsp; + bsp.data = (char*) bsOut.GetData(); + bsp.length = bsOut.GetNumberOfBytesUsed(); + bsp.systemAddress = systemAddress; + rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + + return true; + } + + if (rakPeer->AllowIncomingConnections()==false) + { + bsOut.Write((MessageID)ID_NO_FREE_INCOMING_CONNECTIONS); + bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); + bsOut.Write(rakPeer->myGuid); + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); + //SocketLayer::SendTo( rakNetSocket, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); + RNS2_SendParameters bsp; + bsp.data = (char*) bsOut.GetData(); + bsp.length = bsOut.GetNumberOfBytesUsed(); + bsp.systemAddress = systemAddress; + rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + + return true; + } + + bool thisIPConnectedRecently=false; + rssFromSA = rakPeer->AssignSystemAddressToRemoteSystemList(systemAddress, RakPeer::RemoteSystemStruct::UNVERIFIED_SENDER, rakNetSocket, &thisIPConnectedRecently, bindingAddress, mtu, guid, requiresSecurityOfThisClient); + + if (thisIPConnectedRecently==true) + { + bsOut.Write((MessageID)ID_IP_RECENTLY_CONNECTED); + bsOut.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); + bsOut.Write(rakPeer->myGuid); + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*) bsOut.GetData(), bsOut.GetNumberOfBitsUsed(), systemAddress); + //SocketLayer::SendTo( rakNetSocket, (const char*) bsOut.GetData(), bsOut.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); + + RNS2_SendParameters bsp; + bsp.data = (char*) bsOut.GetData(); + bsp.length = bsOut.GetNumberOfBytesUsed(); + bsp.systemAddress = systemAddress; + rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + + return true; + } + +#if LIBCAT_SECURITY==1 + if (requiresSecurityOfThisClient) + { + CAT_AUDIT_PRINTF("AUDIT: Writing public key. Sending ID_OPEN_CONNECTION_REPLY_2\n"); + if (rakPeer->_server_handshake->ProcessChallenge(remoteHandshakeChallenge, rssFromSA->answer, rssFromSA->reliabilityLayer.GetAuthenticatedEncryption() )) + { + CAT_AUDIT_PRINTF("AUDIT: Challenge good!\n"); + // Keep going to OK block + } + else + { + CAT_AUDIT_PRINTF("AUDIT: Challenge BAD!\n"); + + // Unassign this remote system + rakPeer->DereferenceRemoteSystem(systemAddress); + return true; + } + + bsAnswer.WriteAlignedBytes((const unsigned char *) rssFromSA->answer,sizeof(rssFromSA->answer)); + } +#endif // LIBCAT_SECURITY + + unsigned int i; + for (i=0; i < rakPeer->pluginListNTS.Size(); i++) + rakPeer->pluginListNTS[i]->OnDirectSocketSend((const char*) bsAnswer.GetData(), bsAnswer.GetNumberOfBitsUsed(), systemAddress); + // SocketLayer::SendTo( rakNetSocket, (const char*) bsAnswer.GetData(), bsAnswer.GetNumberOfBytesUsed(), systemAddress, _FILE_AND_LINE_ ); + RNS2_SendParameters bsp; + bsp.data = (char*) bsAnswer.GetData(); + bsp.length = bsAnswer.GetNumberOfBytesUsed(); + bsp.systemAddress = systemAddress; + rakNetSocket->Send(&bsp, _FILE_AND_LINE_); + } + return true; + } + + return false; +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void ProcessNetworkPacket( SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNet::TimeUS timeRead, BitStream &updateBitStream ) +{ + ProcessNetworkPacket(systemAddress,data,length,rakPeer,rakPeer->socketList[0],timeRead, updateBitStream); +} +void ProcessNetworkPacket( SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSocket2* rakNetSocket, RakNet::TimeUS timeRead, BitStream &updateBitStream ) +{ +#if LIBCAT_SECURITY==1 +#ifdef CAT_AUDIT + printf("AUDIT: RECV "); + for (int ii = 0; ii < length; ++ii) + { + printf("%02x", (cat::u8)data[ii]); + } + printf("\n"); +#endif +#endif // LIBCAT_SECURITY + + RakAssert(systemAddress.GetPort()); + bool isOfflineMessage; + if (ProcessOfflineNetworkPacket(systemAddress, data, length, rakPeer, rakNetSocket, &isOfflineMessage, timeRead)) + { + return; + } + +// RakNet::Packet *packet; + RakPeer::RemoteSystemStruct *remoteSystem; + + // See if this datagram came from a connected system + remoteSystem = rakPeer->GetRemoteSystemFromSystemAddress( systemAddress, true, true ); + if ( remoteSystem ) + { + // Handle regular incoming data + // HandleSocketReceiveFromConnectedPlayer is only safe to be called from the same thread as Update, which is this thread + if ( isOfflineMessage==false) + { + remoteSystem->reliabilityLayer.HandleSocketReceiveFromConnectedPlayer( + data, length, systemAddress, rakPeer->pluginListNTS, remoteSystem->MTUSize, + rakNetSocket, &rnr, timeRead, updateBitStream); + } + } + else + { + // int a=5; + // printf("--- Packet from unknown system %s\n", systemAddress.ToString()); + } +} + +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +unsigned int RakPeer::GenerateSeedFromGuid(void) +{ + /* + // Construct a random seed based on the initial guid value, and the last digits of the difference to each subsequent number + // This assumes that only the last 3 bits of each guidId integer has a meaningful amount of randomness between it and the prior number + unsigned int t = guid.g[0]; + unsigned int i; + for (i=1; i < sizeof(guid.g) / sizeof(guid.g[0]); i++) + { + unsigned int diff = guid.g[i]-guid.g[i-1]; + unsigned int diff3Bits = diff & 0x0007; + diff3Bits <<= 29; + diff3Bits >>= (i-1)*3; + t ^= diff3Bits; + } + + return t; + */ + return (unsigned int) ((myGuid.g >> 32) ^ myGuid.g); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void RakPeer::DerefAllSockets(void) +{ + unsigned int i; + for (i=0; i < socketList.Size(); i++) + { + delete socketList[i]; + } + socketList.Clear(false, _FILE_AND_LINE_); +} +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +unsigned int RakPeer::GetRakNetSocketFromUserConnectionSocketIndex(unsigned int userIndex) const +{ + unsigned int i; + for (i=0; i < socketList.Size(); i++) + { + if (socketList[i]->GetUserConnectionSocketIndex()==userIndex) + return i; + } + RakAssert("GetRakNetSocketFromUserConnectionSocketIndex failed" && 0); + return (unsigned int) -1; +} + +/* +// DS_APR +void RakPeer::ProcessChromePacket(RakNetSocket2 *s, const char *buffer, int dataSize, const SystemAddress& recvFromAddress, RakNet::TimeUS timeRead) +{ + RakAssert(buffer); + RakAssert(dataSize > 0); + RakAssert(recvFromAddress.GetPort()); + + RNS2RecvStruct *recvFromStruct; + recvFromStruct=bufferedPackets.Allocate( _FILE_AND_LINE_ ); + RakAssert(dataSize <= (int)sizeof(recvFromStruct->data)); + memcpy(recvFromStruct->data, buffer, dataSize); + recvFromStruct->bytesRead=dataSize; + recvFromStruct->systemAddress=recvFromAddress; + recvFromStruct->timeRead=timeRead; + bufferedPackets.Push(recvFromStruct); +} +*/ + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +/* +bool RakPeer::RunRecvFromOnce( RakNetSocket *s ) +{ + RakPeer::RecvFromStruct *recvFromStruct; + + recvFromStruct=bufferedPackets.Allocate( _FILE_AND_LINE_ ); + if (recvFromStruct != nullptr) + { + recvFromStruct->s=s; + SocketLayer::RecvFromBlocking(s, this, recvFromStruct->data, &recvFromStruct->bytesRead, &recvFromStruct->systemAddress, &recvFromStruct->timeRead); + + if (recvFromStruct->bytesRead>0) + { + RakAssert(recvFromStruct->systemAddress.GetPort()); + bufferedPackets.Push(recvFromStruct); + quitAndDataEvents.SetEvent(); + + // Got data + return true; + } + else + { + bufferedPackets.Deallocate(recvFromStruct, _FILE_AND_LINE_); + } + } + // No data + return false; +} +*/ +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +bool RakPeer::RunUpdateCycle(BitStream &updateBitStream ) +{ + RakPeer::RemoteSystemStruct * remoteSystem; + unsigned int activeSystemListIndex; + Packet *packet; + // int currentSentBytes,currentReceivedBytes; +// unsigned numberOfBytesUsed; +// BitSize_t numberOfBitsUsed; + //SystemAddress authoritativeClientSystemAddress; + BitSize_t bitSize; + unsigned int byteSize; + unsigned char *data; + SystemAddress systemAddress; + BufferedCommandStruct *bcs; + bool callerDataAllocationUsed; + RakNetStatistics *rnss; + RakNet::TimeUS timeNS=0; + RakNet::Time timeMS=0; + + // This is here so RecvFromBlocking actually gets data from the same thread + + #if defined(WINDOWS_STORE_RT) + #elif defined(_WIN32) + if (socketList[0]->GetSocketType()==RNS2T_WINDOWS && ((RNS2_Windows*)socketList[0])->GetSocketLayerOverride()) + { + int len; + SystemAddress sender; + char dataOut[ MAXIMUM_MTU_SIZE ]; + do { + len = ((RNS2_Windows*)socketList[0])->GetSocketLayerOverride()->RakNetRecvFrom(dataOut,&sender,true); + if (len>0) + ProcessNetworkPacket( sender, dataOut, len, this, socketList[0], RakNet::GetTimeUS(), updateBitStream ); + } while (len>0); + } + #endif + +// unsigned int socketListIndex; + RNS2RecvStruct *recvFromStruct; + while ((recvFromStruct=PopBufferedPacket())!=0) + { + /* + for (socketListIndex=0; socketListIndex < socketList.Size(); socketListIndex++) + { + if ((RakNetSocket*) socketList[socketListIndex]==recvFromStruct->s) + break; + } + if (socketListIndex!=socketList.Size()) + */ + ProcessNetworkPacket(recvFromStruct->systemAddress, recvFromStruct->data, recvFromStruct->bytesRead, this, recvFromStruct->socket, recvFromStruct->timeRead, updateBitStream); + DeallocRNS2RecvStruct(recvFromStruct, _FILE_AND_LINE_); + } + + while ((bcs=bufferedCommands.PopInaccurate())!=0) + { + if (bcs->command==BufferedCommandStruct::BCS_SEND) + { + // GetTime is a very slow call so do it once and as late as possible + if (timeNS==0) + { + timeNS = RakNet::GetTimeUS(); + timeMS = (RakNet::TimeMS)(timeNS/(RakNet::TimeUS)1000); + } + + callerDataAllocationUsed=SendImmediate((char*)bcs->data, bcs->numberOfBitsToSend, bcs->priority, bcs->reliability, bcs->orderingChannel, bcs->systemIdentifier, bcs->broadcast, true, timeNS, bcs->receipt); + if ( callerDataAllocationUsed==false ) + rakFree_Ex(bcs->data, _FILE_AND_LINE_ ); + + // Set the new connection state AFTER we call sendImmediate in case we are setting it to a disconnection state, which does not allow further sends + if (bcs->connectionMode!=RemoteSystemStruct::NO_ACTION ) + { + remoteSystem=GetRemoteSystem( bcs->systemIdentifier, true, true ); + if (remoteSystem) + remoteSystem->connectMode=bcs->connectionMode; + } + } + else if (bcs->command==BufferedCommandStruct::BCS_CLOSE_CONNECTION) + { + CloseConnectionInternal(bcs->systemIdentifier, false, true, bcs->orderingChannel, bcs->priority); + } + else if (bcs->command==BufferedCommandStruct::BCS_CHANGE_SYSTEM_ADDRESS) + { + // Reroute + RakPeer::RemoteSystemStruct *rssFromGuid = GetRemoteSystem(bcs->systemIdentifier.rakNetGuid,true,true); + if (rssFromGuid!=0) + { + unsigned int existingSystemIndex = GetRemoteSystemIndex(rssFromGuid->systemAddress); + ReferenceRemoteSystem(bcs->systemIdentifier.systemAddress, existingSystemIndex); + } + } + else if (bcs->command==BufferedCommandStruct::BCS_GET_SOCKET) + { + SocketQueryOutput *sqo; + if (bcs->systemIdentifier.IsUndefined()) + { + sqo = socketQueryOutput.Allocate( _FILE_AND_LINE_ ); + sqo->sockets=socketList; + socketQueryOutput.Push(sqo); + } + else + { + remoteSystem=GetRemoteSystem( bcs->systemIdentifier, true, true ); + sqo = socketQueryOutput.Allocate( _FILE_AND_LINE_ ); + + sqo->sockets.Clear(false, _FILE_AND_LINE_); + if (remoteSystem) + { + sqo->sockets.Push(remoteSystem->rakNetSocket, _FILE_AND_LINE_ ); + } + else + { + // Leave empty smart pointer + } + socketQueryOutput.Push(sqo); + } + + } + +#ifdef _DEBUG + bcs->data=0; +#endif + + bufferedCommands.Deallocate(bcs, _FILE_AND_LINE_); + } + + if (requestedConnectionQueue.IsEmpty()==false) + { + if (timeNS==0) + { + timeNS = RakNet::GetTimeUS(); + timeMS = (RakNet::TimeMS)(timeNS/(RakNet::TimeUS)1000); + } + + bool condition1, condition2; + unsigned requestedConnectionQueueIndex=0; + requestedConnectionQueueMutex.Lock(); + while (requestedConnectionQueueIndex < requestedConnectionQueue.Size()) + { + RequestedConnectionStruct *rcs; + rcs = requestedConnectionQueue[requestedConnectionQueueIndex]; + requestedConnectionQueueMutex.Unlock(); + if (rcs->nextRequestTime < timeMS) + { + condition1=rcs->requestsMade==rcs->sendConnectionAttemptCount+1; + condition2=(bool)((rcs->systemAddress==UNASSIGNED_SYSTEM_ADDRESS)==1); + // If too many requests made or a hole then remove this if possible, otherwise invalidate it + if (condition1 || condition2) + { + if (rcs->data) + { + rakFree_Ex(rcs->data, _FILE_AND_LINE_ ); + rcs->data=0; + } + + if (condition1 && !condition2 && rcs->actionToTake==RequestedConnectionStruct::CONNECT) + { + // Tell user of connection attempt failed + packet=AllocPacket(sizeof( char ), _FILE_AND_LINE_); + packet->data[ 0 ] = ID_CONNECTION_ATTEMPT_FAILED; // Attempted a connection and couldn't + packet->bitSize = ( sizeof( char ) * 8); + packet->systemAddress = rcs->systemAddress; + AddPacketToProducer(packet); + } + +#if LIBCAT_SECURITY==1 + CAT_AUDIT_PRINTF("AUDIT: Connection attempt FAILED so deleting rcs->client_handshake object %x\n", rcs->client_handshake); + RakNet::OP_DELETE(rcs->client_handshake,_FILE_AND_LINE_); +#endif + RakNet::OP_DELETE(rcs,_FILE_AND_LINE_); + + requestedConnectionQueueMutex.Lock(); + for (unsigned int k=0; k < requestedConnectionQueue.Size(); k++) + { + if (requestedConnectionQueue[k]==rcs) + { + requestedConnectionQueue.RemoveAtIndex(k); + break; + } + } + requestedConnectionQueueMutex.Unlock(); + } + else + { + + int MTUSizeIndex = rcs->requestsMade / (rcs->sendConnectionAttemptCount/NUM_MTU_SIZES); + if (MTUSizeIndex>=NUM_MTU_SIZES) + MTUSizeIndex=NUM_MTU_SIZES-1; + rcs->requestsMade++; + rcs->nextRequestTime=timeMS+rcs->timeBetweenSendConnectionAttemptsMS; + + RakNet::BitStream bitStream; + //WriteOutOfBandHeader(&bitStream, ID_USER_PACKET_ENUM); + bitStream.Write((MessageID)ID_OPEN_CONNECTION_REQUEST_1); + bitStream.WriteAlignedBytes((const unsigned char*) OFFLINE_MESSAGE_DATA_ID, sizeof(OFFLINE_MESSAGE_DATA_ID)); + bitStream.Write((MessageID)RAKNET_PROTOCOL_VERSION); + bitStream.PadWithZeroToByteLength(mtuSizes[MTUSizeIndex]-UDP_HEADER_SIZE); + + char str[256]; + rcs->systemAddress.ToString(true,str); + + //RAKNET_DEBUG_PRINTF("%i:IOCR, ", __LINE__); + + unsigned i; + for (i=0; i < pluginListNTS.Size(); i++) + pluginListNTS[i]->OnDirectSocketSend((const char*) bitStream.GetData(), bitStream.GetNumberOfBitsUsed(), rcs->systemAddress); + + RakNetSocket2 *socketToUse; + if (rcs->socket == 0) + socketToUse = socketList[rcs->socketIndex]; + else + socketToUse = rcs->socket; + + rcs->systemAddress.FixForIPVersion(socketToUse->GetBoundAddress()); +#if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) + if (socketToUse->IsBerkleySocket()) + ((RNS2_Berkley*)socketToUse)->SetDoNotFragment(1); +#endif + +// SocketLayer::SetDoNotFragment(socketToUse, 1); + RakNet::Time sendToStart=RakNet::GetTime(); + + RNS2_SendParameters bsp; + bsp.data = (char*) bitStream.GetData(); + bsp.length = bitStream.GetNumberOfBytesUsed(); + bsp.systemAddress = rcs->systemAddress; + if (socketToUse->Send(&bsp, _FILE_AND_LINE_) == 10040) + // if (SocketLayer::SendTo( socketToUse, (const char*) bitStream.GetData(), bitStream.GetNumberOfBytesUsed(), rcs->systemAddress, _FILE_AND_LINE_ )==-10040) + { + // Don't use this MTU size again + rcs->requestsMade = (unsigned char) ((MTUSizeIndex + 1) * (rcs->sendConnectionAttemptCount/NUM_MTU_SIZES)); + rcs->nextRequestTime=timeMS; + } + else + { + RakNet::Time sendToEnd=RakNet::GetTime(); + if (sendToEnd-sendToStart>100) + { + // Drop to lowest MTU + int lowestMtuIndex = rcs->sendConnectionAttemptCount/NUM_MTU_SIZES * (NUM_MTU_SIZES - 1); + if (lowestMtuIndex > rcs->requestsMade) + { + rcs->requestsMade = (unsigned char) lowestMtuIndex; + rcs->nextRequestTime=timeMS; + } + else + rcs->requestsMade=(unsigned char)(rcs->sendConnectionAttemptCount+1); + } + } + // SocketLayer::SetDoNotFragment(socketToUse, 0); +#if !defined(__native_client__) && !defined(WINDOWS_STORE_RT) + if (socketToUse->IsBerkleySocket()) + ((RNS2_Berkley*)socketToUse)->SetDoNotFragment(0); +#endif + + requestedConnectionQueueIndex++; + } + } + else + requestedConnectionQueueIndex++; + + requestedConnectionQueueMutex.Lock(); + } + requestedConnectionQueueMutex.Unlock(); + } + + // remoteSystemList in network thread + for ( activeSystemListIndex = 0; activeSystemListIndex < activeSystemListSize; ++activeSystemListIndex ) + //for ( remoteSystemIndex = 0; remoteSystemIndex < remoteSystemListSize; ++remoteSystemIndex ) + { + // I'm using systemAddress from remoteSystemList but am not locking it because this loop is called very frequently and it doesn't + // matter if we miss or do an extra update. The reliability layers themselves never care which player they are associated with + //systemAddress = remoteSystemList[ remoteSystemIndex ].systemAddress; + // Allow the systemAddress for this remote system list to change. We don't care if it changes now. + // remoteSystemList[ remoteSystemIndex ].allowSystemAddressAssigment=true; + + + // Found an active remote system + remoteSystem = activeSystemList[ activeSystemListIndex ]; + systemAddress = remoteSystem->systemAddress; + RakAssert(systemAddress!=UNASSIGNED_SYSTEM_ADDRESS); + // Update is only safe to call from the same thread that calls HandleSocketReceiveFromConnectedPlayer, + // which is this thread + + if (timeNS==0) + { + timeNS = RakNet::GetTimeUS(); + timeMS = (RakNet::TimeMS)(timeNS/(RakNet::TimeUS)1000); + //RAKNET_DEBUG_PRINTF("timeNS = %I64i timeMS=%i\n", timeNS, timeMS); + } + + + if (timeMS > remoteSystem->lastReliableSend && timeMS-remoteSystem->lastReliableSend > remoteSystem->reliabilityLayer.GetTimeoutTime()/2 && remoteSystem->connectMode==RemoteSystemStruct::CONNECTED) + { + // If no reliable packets are waiting for an ack, do a one byte reliable send so that disconnections are noticed + RakNetStatistics rakNetStatistics; + rnss=remoteSystem->reliabilityLayer.GetStatistics(&rakNetStatistics); + if (rnss->messagesInResendBuffer==0) + { + PingInternal( systemAddress, true, RELIABLE ); + + //remoteSystem->lastReliableSend=timeMS+remoteSystem->reliabilityLayer.GetTimeoutTime(); + remoteSystem->lastReliableSend=timeMS; + } + } + + remoteSystem->reliabilityLayer.Update( remoteSystem->rakNetSocket, systemAddress, remoteSystem->MTUSize, timeNS, maxOutgoingBPS, pluginListNTS, &rnr, updateBitStream ); // systemAddress only used for the internet simulator test + + // Check for failure conditions + if ( remoteSystem->reliabilityLayer.IsDeadConnection() || + ((remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ASAP || remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY) && remoteSystem->reliabilityLayer.IsOutgoingDataWaiting()==false) || + (remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ON_NO_ACK && (remoteSystem->reliabilityLayer.AreAcksWaiting()==false || remoteSystem->reliabilityLayer.AckTimeout(timeMS)==true)) || + (( + (remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION || + remoteSystem->connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST || + remoteSystem->connectMode==RemoteSystemStruct::UNVERIFIED_SENDER) + && timeMS > remoteSystem->connectionTime && timeMS - remoteSystem->connectionTime > 10000)) + ) + { + // RAKNET_DEBUG_PRINTF("timeMS=%i remoteSystem->connectionTime=%i\n", timeMS, remoteSystem->connectionTime ); + + // Failed. Inform the user? + // TODO - RakNet 4.0 - Return a different message identifier for DISCONNECT_ASAP_SILENTLY and DISCONNECT_ASAP than for DISCONNECT_ON_NO_ACK + // The first two mean we called CloseConnection(), the last means the other system sent us ID_DISCONNECTION_NOTIFICATION + if (remoteSystem->connectMode==RemoteSystemStruct::CONNECTED || remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION + || remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ASAP || remoteSystem->connectMode==RemoteSystemStruct::DISCONNECT_ON_NO_ACK) + { + +// RakNet::BitStream undeliveredMessages; +// remoteSystem->reliabilityLayer.GetUndeliveredMessages(&undeliveredMessages,remoteSystem->MTUSize); + +// packet=AllocPacket(sizeof( char ) + undeliveredMessages.GetNumberOfBytesUsed()); + packet=AllocPacket(sizeof( char ), _FILE_AND_LINE_); + if (remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION) + packet->data[ 0 ] = ID_CONNECTION_ATTEMPT_FAILED; // Attempted a connection and couldn't + else if (remoteSystem->connectMode==RemoteSystemStruct::CONNECTED) + packet->data[ 0 ] = ID_CONNECTION_LOST; // DeadConnection + else + packet->data[ 0 ] = ID_DISCONNECTION_NOTIFICATION; // DeadConnection + +// memcpy(packet->data+1, undeliveredMessages.GetData(), undeliveredMessages.GetNumberOfBytesUsed()); + + packet->guid = remoteSystem->guid; + packet->systemAddress = systemAddress; + packet->systemAddress.systemIndex = remoteSystem->remoteSystemIndex; + packet->guid.systemIndex=packet->systemAddress.systemIndex; + + AddPacketToProducer(packet); + } + // else connection shutting down, don't bother telling the user + +#ifdef _DO_PRINTF + RAKNET_DEBUG_PRINTF("Connection dropped for player %i:%i\n", systemAddress); +#endif + CloseConnectionInternal( systemAddress, false, true, 0, LOW_PRIORITY ); + continue; + } + + // Ping this guy if it is time to do so + if ( remoteSystem->connectMode==RemoteSystemStruct::CONNECTED && timeMS > remoteSystem->nextPingTime && ( occasionalPing || remoteSystem->lowestPing == (unsigned short)-1 ) ) + { + remoteSystem->nextPingTime = timeMS + 5000; + PingInternal( systemAddress, true, UNRELIABLE ); + + // Update again immediately after this tick so the ping goes out right away + quitAndDataEvents.SetEvent(); + } + + // Find whoever has the lowest player ID + //if (systemAddress < authoritativeClientSystemAddress) + // authoritativeClientSystemAddress=systemAddress; + + // Does the reliability layer have any packets waiting for us? + // To be thread safe, this has to be called in the same thread as HandleSocketReceiveFromConnectedPlayer + bitSize = remoteSystem->reliabilityLayer.Receive( &data ); + + while ( bitSize > 0 ) + { + // These types are for internal use and should never arrive from a network packet + if (data[0]==ID_CONNECTION_ATTEMPT_FAILED) + { + RakAssert(0); + bitSize=0; + continue; + } + + // Fast and easy - just use the data that was returned + byteSize = (unsigned int) BITS_TO_BYTES( bitSize ); + + // For unknown senders we only accept a few specific packets + if (remoteSystem->connectMode==RemoteSystemStruct::UNVERIFIED_SENDER) + { + if ( (unsigned char)(data)[0] == ID_CONNECTION_REQUEST ) + { + ParseConnectionRequestPacket(remoteSystem, systemAddress, (const char*)data, byteSize); + rakFree_Ex(data, _FILE_AND_LINE_ ); + } + else + { + CloseConnectionInternal( systemAddress, false, true, 0, LOW_PRIORITY ); +#ifdef _DO_PRINTF + RAKNET_DEBUG_PRINTF("Temporarily banning %i:%i for sending nonsense data\n", systemAddress); +#endif + + char str1[64]; + systemAddress.ToString(false, str1); + AddToBanList(str1, remoteSystem->reliabilityLayer.GetTimeoutTime()); + + + rakFree_Ex(data, _FILE_AND_LINE_ ); + } + } + else + { + // However, if we are connected we still take a connection request in case both systems are trying to connect to each other + // at the same time + if ( (unsigned char)(data)[0] == ID_CONNECTION_REQUEST ) + { + // 04/27/06 This is wrong. With cross connections, we can both have initiated the connection are in state REQUESTED_CONNECTION + // 04/28/06 Downgrading connections from connected will close the connection due to security at ((remoteSystem->connectMode!=RemoteSystemStruct::CONNECTED && time > remoteSystem->connectionTime && time - remoteSystem->connectionTime > 10000)) + if (remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION) + { + ParseConnectionRequestPacket(remoteSystem, systemAddress, (const char*)data, byteSize); + } + else + { + + RakNet::BitStream bs((unsigned char*) data,byteSize,false); + bs.IgnoreBytes(sizeof(MessageID)); + bs.IgnoreBytes(sizeof(OFFLINE_MESSAGE_DATA_ID)); + bs.IgnoreBytes(RakNetGUID::size()); + RakNet::Time incomingTimestamp; + bs.Read(incomingTimestamp); + + // Got a connection request message from someone we are already connected to. Just reply normally. + // This can happen due to race conditions with the fully connected mesh + OnConnectionRequest( remoteSystem, incomingTimestamp ); + } + rakFree_Ex(data, _FILE_AND_LINE_ ); + } + else if ( (unsigned char) data[ 0 ] == ID_NEW_INCOMING_CONNECTION && byteSize > sizeof(unsigned char)+sizeof(unsigned int)+sizeof(unsigned short)+sizeof(RakNet::Time)*2 ) + { + if (remoteSystem->connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST) + { + remoteSystem->connectMode=RemoteSystemStruct::CONNECTED; + PingInternal( systemAddress, true, UNRELIABLE ); + + // Update again immediately after this tick so the ping goes out right away + quitAndDataEvents.SetEvent(); + + RakNet::BitStream inBitStream((unsigned char *) data, byteSize, false); + SystemAddress bsSystemAddress; + + inBitStream.IgnoreBits(8); + inBitStream.Read(bsSystemAddress); + for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++) + inBitStream.Read(remoteSystem->theirInternalSystemAddress[i]); + + RakNet::Time sendPingTime, sendPongTime; + inBitStream.Read(sendPingTime); + inBitStream.Read(sendPongTime); + OnConnectedPong(sendPingTime,sendPongTime,remoteSystem); + + // Overwrite the data in the packet + // NewIncomingConnectionStruct newIncomingConnectionStruct; + // RakNet::BitStream nICS_BS( data, NewIncomingConnectionStruct_Size, false ); + // newIncomingConnectionStruct.Deserialize( nICS_BS ); + + remoteSystem->myExternalSystemAddress = bsSystemAddress; + + // Bug: If A connects to B through R, A's firstExternalID is set to R. If A tries to send to R, sends to loopback because R==firstExternalID + // Correct fix is to specify in Connect() if target is through a proxy. + // However, in practice you have to connect to something else first anyway to know about the proxy. So setting once only is good enough + if (firstExternalID==UNASSIGNED_SYSTEM_ADDRESS) + { + firstExternalID=bsSystemAddress; + firstExternalID.debugPort=ntohs(firstExternalID.address.addr4.sin_port); + } + + // Send this info down to the game + packet=AllocPacket(byteSize, data, _FILE_AND_LINE_); + packet->bitSize = bitSize; + packet->systemAddress = systemAddress; + packet->systemAddress.systemIndex = remoteSystem->remoteSystemIndex; + packet->guid = remoteSystem->guid; + packet->guid.systemIndex=packet->systemAddress.systemIndex; + AddPacketToProducer(packet); + } + else + { + // Send to game even if already connected. This could happen when connecting to 127.0.0.1 + // Ignore, already connected + // rakFree_Ex(data, _FILE_AND_LINE_ ); + } + } + else if ( (unsigned char) data[ 0 ] == ID_CONNECTED_PONG && byteSize == sizeof(unsigned char)+sizeof(RakNet::Time)*2 ) + { + RakNet::Time sendPingTime, sendPongTime; + + // Copy into the ping times array the current time - the value returned + // First extract the sent ping + RakNet::BitStream inBitStream( (unsigned char *) data, byteSize, false ); + //PingStruct ps; + //ps.Deserialize(psBS); + inBitStream.IgnoreBits(8); + inBitStream.Read(sendPingTime); + inBitStream.Read(sendPongTime); + + OnConnectedPong(sendPingTime,sendPongTime,remoteSystem); + + rakFree_Ex(data, _FILE_AND_LINE_ ); + } + else if ( (unsigned char)data[0] == ID_CONNECTED_PING && byteSize == sizeof(unsigned char)+sizeof(RakNet::Time) ) + { + RakNet::BitStream inBitStream( (unsigned char *) data, byteSize, false ); + inBitStream.IgnoreBits(8); + RakNet::Time sendPingTime; + inBitStream.Read(sendPingTime); + + RakNet::BitStream outBitStream; + outBitStream.Write((MessageID)ID_CONNECTED_PONG); + outBitStream.Write(sendPingTime); + outBitStream.Write(RakNet::GetTime()); + SendImmediate( (char*)outBitStream.GetData(), outBitStream.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, UNRELIABLE, 0, systemAddress, false, false, RakNet::GetTimeUS(), 0 ); + + // Update again immediately after this tick so the ping goes out right away + quitAndDataEvents.SetEvent(); + + rakFree_Ex(data, _FILE_AND_LINE_ ); + } + else if ( (unsigned char) data[ 0 ] == ID_DISCONNECTION_NOTIFICATION ) + { + // We shouldn't close the connection immediately because we need to ack the ID_DISCONNECTION_NOTIFICATION + remoteSystem->connectMode=RemoteSystemStruct::DISCONNECT_ON_NO_ACK; + rakFree_Ex(data, _FILE_AND_LINE_ ); + + // AddPacketToProducer(packet); + } + else if ( (unsigned char)(data)[0] == ID_DETECT_LOST_CONNECTIONS && byteSize == sizeof(unsigned char) ) + { + // Do nothing + rakFree_Ex(data, _FILE_AND_LINE_ ); + } + else if ( (unsigned char)(data)[0] == ID_INVALID_PASSWORD ) + { + if (remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION) + { + packet=AllocPacket(byteSize, data, _FILE_AND_LINE_); + packet->bitSize = bitSize; + packet->systemAddress = systemAddress; + packet->systemAddress.systemIndex = remoteSystem->remoteSystemIndex; + packet->guid = remoteSystem->guid; + packet->guid.systemIndex=packet->systemAddress.systemIndex; + AddPacketToProducer(packet); + + remoteSystem->connectMode=RemoteSystemStruct::DISCONNECT_ASAP_SILENTLY; + } + else + { + rakFree_Ex(data, _FILE_AND_LINE_ ); + } + } + else if ( (unsigned char)(data)[0] == ID_CONNECTION_REQUEST_ACCEPTED ) + { + if (byteSize > sizeof(MessageID)+sizeof(unsigned int)+sizeof(unsigned short)+sizeof(SystemIndex)+sizeof(RakNet::Time)*2) + { + // Make sure this connection accept is from someone we wanted to connect to + bool allowConnection, alreadyConnected; + + if (remoteSystem->connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST || + remoteSystem->connectMode==RemoteSystemStruct::REQUESTED_CONNECTION || + allowConnectionResponseIPMigration) + allowConnection=true; + else + allowConnection=false; + + if (remoteSystem->connectMode==RemoteSystemStruct::HANDLING_CONNECTION_REQUEST) + alreadyConnected=true; + else + alreadyConnected=false; + + if ( allowConnection ) + { + SystemAddress externalID; + SystemIndex systemIndex; +// SystemAddress internalID; + + RakNet::BitStream inBitStream((unsigned char *) data, byteSize, false); + inBitStream.IgnoreBits(8); + // inBitStream.Read(remotePort); + inBitStream.Read(externalID); + inBitStream.Read(systemIndex); + for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++) + inBitStream.Read(remoteSystem->theirInternalSystemAddress[i]); + + RakNet::Time sendPingTime, sendPongTime; + inBitStream.Read(sendPingTime); + inBitStream.Read(sendPongTime); + OnConnectedPong(sendPingTime, sendPongTime, remoteSystem); + + // Find a free remote system struct to use + // RakNet::BitStream casBitS(data, byteSize, false); + // ConnectionAcceptStruct cas; + // cas.Deserialize(casBitS); + // systemAddress.GetPort() = remotePort; + + // The remote system told us our external IP, so save it + remoteSystem->myExternalSystemAddress = externalID; + remoteSystem->connectMode=RemoteSystemStruct::CONNECTED; + + // Bug: If A connects to B through R, A's firstExternalID is set to R. If A tries to send to R, sends to loopback because R==firstExternalID + // Correct fix is to specify in Connect() if target is through a proxy. + // However, in practice you have to connect to something else first anyway to know about the proxy. So setting once only is good enough + if (firstExternalID==UNASSIGNED_SYSTEM_ADDRESS) + { + firstExternalID=externalID; + firstExternalID.debugPort=ntohs(firstExternalID.address.addr4.sin_port); + } + + // Send the connection request complete to the game + packet=AllocPacket(byteSize, data, _FILE_AND_LINE_); + packet->bitSize = byteSize * 8; + packet->systemAddress = systemAddress; + packet->systemAddress.systemIndex = ( SystemIndex ) GetIndexFromSystemAddress( systemAddress, true ); + packet->guid = remoteSystem->guid; + packet->guid.systemIndex=packet->systemAddress.systemIndex; + AddPacketToProducer(packet); + + RakNet::BitStream outBitStream; + outBitStream.Write((MessageID)ID_NEW_INCOMING_CONNECTION); + outBitStream.Write(systemAddress); + for (unsigned int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++) + outBitStream.Write(ipList[i]); + outBitStream.Write(sendPongTime); + outBitStream.Write(RakNet::GetTime()); + + SendImmediate( (char*)outBitStream.GetData(), outBitStream.GetNumberOfBitsUsed(), IMMEDIATE_PRIORITY, RELIABLE_ORDERED, 0, systemAddress, false, false, RakNet::GetTimeUS(), 0 ); + + if (alreadyConnected==false) + { + PingInternal( systemAddress, true, UNRELIABLE ); + } + } + else + { + // Ignore, already connected + rakFree_Ex(data, _FILE_AND_LINE_ ); + } + } + else + { + // Version mismatch error? + RakAssert(0); + rakFree_Ex(data, _FILE_AND_LINE_ ); + } + } + else + { + // What do I do if I get a message from a system, before I am fully connected? + // I can either ignore it or give it to the user + // It seems like giving it to the user is a better option + if ((data[0]>=(MessageID)ID_TIMESTAMP || data[0]==ID_SND_RECEIPT_ACKED || data[0]==ID_SND_RECEIPT_LOSS) && + remoteSystem->isActive + ) + { + packet=AllocPacket(byteSize, data, _FILE_AND_LINE_); + packet->bitSize = bitSize; + packet->systemAddress = systemAddress; + packet->systemAddress.systemIndex = remoteSystem->remoteSystemIndex; + packet->guid = remoteSystem->guid; + packet->guid.systemIndex=packet->systemAddress.systemIndex; + AddPacketToProducer(packet); + } + else + { + rakFree_Ex(data, _FILE_AND_LINE_ ); + } + } + } + + // Does the reliability layer have any more packets waiting for us? + // To be thread safe, this has to be called in the same thread as HandleSocketReceiveFromConnectedPlayer + bitSize = remoteSystem->reliabilityLayer.Receive( &data ); + } + + } + + return true; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +void RakPeer::OnRNS2Recv(RNS2RecvStruct *recvStruct) +{ + if (incomingDatagramEventHandler) + { + if (incomingDatagramEventHandler(recvStruct)!=true) + return; + } + + PushBufferedPacket(recvStruct); + quitAndDataEvents.SetEvent(); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +/* +RAK_THREAD_DECLARATION(RakNet::RecvFromLoop) +{ +#if defined(SN_TARGET_PSP2) + RakPeerAndIndex *rpai = ( RakPeerAndIndex * ) RakThread::GetRealThreadArgument(callGetRealThreadArgument); +#else + RakPeerAndIndex *rpai = ( RakPeerAndIndex * ) arguments; +#endif + RakPeer * rakPeer = rpai->rakPeer; + RakNetSocket *s = rpai->s; + RakNet::OP_DELETE(rpai,_FILE_AND_LINE_); + + rakPeer->isRecvFromLoopThreadActive.Increment(); + + while ( rakPeer->endThreads == false ) + { + if (rakPeer->RunRecvFromOnce(s)==false && + s->GetBlockingSocket()==false) + RakSleep(0); + } + rakPeer->isRecvFromLoopThreadActive.Decrement(); + +#if defined(SN_TARGET_PSP2) + return sceKernelExitDeleteThread(0); +#else + return 0; +#endif +} +*/ + +// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +RAK_THREAD_DECLARATION(RakNet::UpdateNetworkLoop) +{ + + + + RakPeer * rakPeer = ( RakPeer * ) arguments; + + +/* + // 11/15/05 - this is slower than Sleep() +#ifdef _WIN32 +#if (_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400) + // Lets see if these timers give better performance than Sleep + HANDLE timerHandle; + LARGE_INTEGER dueTime; + + if ( rakPeer->threadSleepTimer <= 0 ) + rakPeer->threadSleepTimer = 1; + + // 2nd parameter of false means synchronization timer instead of manual-reset timer + timerHandle = CreateWaitableTimer( nullptr, FALSE, 0 ); + + RakAssert( timerHandle ); + + dueTime.QuadPart = -10000 * rakPeer->threadSleepTimer; // 10000 is 1 ms? + + BOOL success = SetWaitableTimer( timerHandle, &dueTime, rakPeer->threadSleepTimer, nullptr, nullptr, FALSE ); + (void) success; + RakAssert( success ); + +#endif +#endif +*/ + + BitStream updateBitStream( MAXIMUM_MTU_SIZE +#if LIBCAT_SECURITY==1 + + cat::AuthenticatedEncryption::OVERHEAD_BYTES +#endif + ); +// + rakPeer->isMainLoopThreadActive = true; + + while ( rakPeer->endThreads == false ) + { +// #ifdef _DEBUG +// // Sanity check, make sure RunUpdateCycle does not block or not otherwise get called for a long time +// RakNetTime thisCall=RakNet::GetTime(); +// RakAssert(thisCall-lastCall<250); +// lastCall=thisCall; +// #endif + if (rakPeer->userUpdateThreadPtr) + rakPeer->userUpdateThreadPtr(rakPeer, rakPeer->userUpdateThreadData); + + rakPeer->RunUpdateCycle(updateBitStream); + + // Pending sends go out this often, unless quitAndDataEvents is set + rakPeer->quitAndDataEvents.WaitOnEvent(10); + + /* + +// #if ((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)) && +#if defined(USE_WAIT_FOR_MULTIPLE_EVENTS) && defined(_WIN32) + + if (rakPeer->threadSleepTimer>0) + { + WSAEVENT eventArray[256]; + unsigned int i, eventArrayIndex; + for (i=0,eventArrayIndex=0; i < rakPeer->socketList.Size(); i++) + { + if (rakPeer->socketList[i]->recvEvent!=INVALID_HANDLE_VALUE) + { + eventArray[eventArrayIndex]=rakPeer->socketList[i]->recvEvent; + eventArrayIndex++; + if (eventArrayIndex==256) + break; + } + } + WSAWaitForMultipleEvents(eventArrayIndex,(const HANDLE*) &eventArray,FALSE,rakPeer->threadSleepTimer,FALSE); + } + else + { + RakSleep(0); + } + +#else // ((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)) && defined(USE_WAIT_FOR_MULTIPLE_EVENTS) + #pragma message("-- RakNet: Using Sleep(). Uncomment USE_WAIT_FOR_MULTIPLE_EVENTS in RakNetDefines.h if you want to use WaitForSingleObject instead. --") + + RakSleep( rakPeer->threadSleepTimer ); +#endif + */ + } + + rakPeer->isMainLoopThreadActive = false; + + /* +#ifdef _WIN32 +#if (_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400) + CloseHandle(timerHandle); +#endif +#endif + */ + + + + + return 0; + +} + +void RakPeer::CallPluginCallbacks(DataStructures::List &pluginList, Packet *packet) +{ + for (unsigned int i=0; i < pluginList.Size(); i++) + { + switch (packet->data[0]) + { + case ID_DISCONNECTION_NOTIFICATION: + pluginList[i]->OnClosedConnection(packet->systemAddress, packet->guid, LCR_DISCONNECTION_NOTIFICATION); + break; + case ID_CONNECTION_LOST: + pluginList[i]->OnClosedConnection(packet->systemAddress, packet->guid, LCR_CONNECTION_LOST); + break; + case ID_NEW_INCOMING_CONNECTION: + pluginList[i]->OnNewConnection(packet->systemAddress, packet->guid, true); + break; + case ID_CONNECTION_REQUEST_ACCEPTED: + pluginList[i]->OnNewConnection(packet->systemAddress, packet->guid, false); + break; + case ID_CONNECTION_ATTEMPT_FAILED: + pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_CONNECTION_ATTEMPT_FAILED); + break; + case ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY: + pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY); + break; + case ID_OUR_SYSTEM_REQUIRES_SECURITY: + pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_OUR_SYSTEM_REQUIRES_SECURITY); + break; + case ID_PUBLIC_KEY_MISMATCH: + pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_PUBLIC_KEY_MISMATCH); + break; + case ID_ALREADY_CONNECTED: + pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_ALREADY_CONNECTED); + break; + case ID_NO_FREE_INCOMING_CONNECTIONS: + pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_NO_FREE_INCOMING_CONNECTIONS); + break; + case ID_CONNECTION_BANNED: + pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_CONNECTION_BANNED); + break; + case ID_INVALID_PASSWORD: + pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_INVALID_PASSWORD); + break; + case ID_INCOMPATIBLE_PROTOCOL_VERSION: + pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_INCOMPATIBLE_PROTOCOL); + break; + case ID_IP_RECENTLY_CONNECTED: + pluginList[i]->OnFailedConnectionAttempt(packet, FCAR_IP_RECENTLY_CONNECTED); + break; + } + } +} + +void RakPeer::FillIPList(void) +{ + if (ipList[0]!=UNASSIGNED_SYSTEM_ADDRESS) + return; + + // Fill out ipList structure +#if !defined(WINDOWS_STORE_RT) + RakNetSocket2::GetMyIP( ipList ); +#endif + + // Sort the addresses from lowest to highest + int startingIdx = 0; + while (startingIdx < MAXIMUM_NUMBER_OF_INTERNAL_IDS-1 && ipList[startingIdx] != UNASSIGNED_SYSTEM_ADDRESS) + { + int lowestIdx = startingIdx; + for (int curIdx = startingIdx + 1; curIdx < MAXIMUM_NUMBER_OF_INTERNAL_IDS-1 && ipList[curIdx] != UNASSIGNED_SYSTEM_ADDRESS; curIdx++ ) + { + if (ipList[curIdx] < ipList[startingIdx]) + { + lowestIdx = curIdx; + } + } + if (startingIdx != lowestIdx) + { + SystemAddress temp = ipList[startingIdx]; + ipList[startingIdx] = ipList[lowestIdx]; + ipList[lowestIdx] = temp; + } + ++startingIdx; + } +} + + +// #if defined(RMO_NEW_UNDEF_ALLOCATING_QUEUE) +// #pragma pop_macro("new") +// #undef RMO_NEW_UNDEF_ALLOCATING_QUEUE +// #endif + + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif diff --git a/Source/RakPeer.h b/Source/RakPeer.h index 40ab131fe..151f73d02 100644 --- a/Source/RakPeer.h +++ b/Source/RakPeer.h @@ -1,1034 +1,1032 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Declares RakPeer class. -/// - - -// TODO - RakNet 4 - Add network simulator -// TODO - RakNet 4 - Enable disabling flow control per connections - -#ifndef __RAK_PEER_H -#define __RAK_PEER_H - -#include "ReliabilityLayer.h" -#include "RakPeerInterface.h" -#include "BitStream.h" -#include "SingleProducerConsumer.h" -#include "SimpleMutex.h" -#include "DS_OrderedList.h" -#include "Export.h" -#include "RakString.h" -#include "RakThread.h" -//#include "RakNetSocket.h" -#include "RakNetSmartPtr.h" -#include "DS_ThreadsafeAllocatingQueue.h" -#include "SignaledEvent.h" -#include "NativeFeatureIncludes.h" -#include "SecureHandshake.h" -#include "LocklessTypes.h" -#include "DS_Queue.h" - -namespace RakNet { -/// Forward declarations -class HuffmanEncodingTree; -class PluginInterface2; - -// Sucks but this struct has to be outside the class. Inside and DevCPP won't let you refer to the struct as RakPeer::RemoteSystemIndex while GCC -// forces you to do RakPeer::RemoteSystemIndex -struct RemoteSystemIndex{unsigned index; RemoteSystemIndex *next;}; -//int RAK_DLL_EXPORT SystemAddressAndIndexComp( const SystemAddress &key, const RemoteSystemIndex &data ); // GCC requires RakPeer::RemoteSystemIndex or it won't compile - -///\brief Main interface for network communications. -/// \details It implements most of RakNet's functionality and is the primary interface for RakNet. -/// -/// Inherits RakPeerInterface. -/// -/// See the individual functions for what the class can do. -/// -class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler -{ -public: - ///Constructor - RakPeer(); - - ///Destructor - virtual ~RakPeer(); - - // --------------------------------------------------------------------------------------------Major Low Level Functions - Functions needed by most users-------------------------------------------------------------------------------------------- - /// \brief Starts the network threads and opens the listen port. - /// \details You must call this before calling Connect(). - /// \pre On the PS3, call Startup() after Client_Login() - /// \note Multiple calls while already active are ignored. To call this function again with different settings, you must first call Shutdown(). - /// \note Call SetMaximumIncomingConnections if you want to accept incoming connections. - /// \param[in] maxConnections Maximum number of connections between this instance of RakPeer and another instance of RakPeer. Required so that the network can preallocate and for thread safety. A pure client would set this to 1. A pure server would set it to the number of allowed clients.A hybrid would set it to the sum of both types of connections. - /// \param[in] localPort The port to listen for connections on. On linux the system may be set up so thast ports under 1024 are restricted for everything but the root user. Use a higher port for maximum compatibility. - /// \param[in] socketDescriptors An array of SocketDescriptor structures to force RakNet to listen on a particular IP address or port (or both). Each SocketDescriptor will represent one unique socket. Do not pass redundant structures. To listen on a specific port, you can pass SocketDescriptor(myPort,0); such as for a server. For a client, it is usually OK to just pass SocketDescriptor(); However, on the XBOX be sure to use IPPROTO_VDP - /// \param[in] socketDescriptorCount The size of the \a socketDescriptors array. Pass 1 if you are not sure what to pass. - /// \param[in] threadPriority Passed to the thread creation routine. Use THREAD_PRIORITY_NORMAL for Windows. For Linux based systems, you MUST pass something reasonable based on the thread priorities for your application. - /// \return RAKNET_STARTED on success, otherwise appropriate failure enumeration. - StartupResult Startup( unsigned int maxConnections, SocketDescriptor *socketDescriptors, unsigned socketDescriptorCount, int threadPriority=-99999 ); - - /// If you accept connections, you must call this or else security will not be enabled for incoming connections. - /// This feature requires more round trips, bandwidth, and CPU time for the connection handshake - /// x64 builds require under 25% of the CPU time of other builds - /// See the Encryption sample for example usage - /// \pre Must be called while offline - /// \pre LIBCAT_SECURITY must be defined to 1 in NativeFeatureIncludes.h for this function to have any effect - /// \param[in] publicKey A pointer to the public key for accepting new connections - /// \param[in] privateKey A pointer to the private key for accepting new connections - /// \param[in] bRequireClientKey: Should be set to false for most servers. Allows the server to accept a public key from connecting clients as a proof of identity but eats twice as much CPU time as a normal connection - bool InitializeSecurity( const char *publicKey, const char *privateKey, bool bRequireClientKey = false ); - - /// Disables security for incoming connections. - /// \note Must be called while offline - void DisableSecurity( void ); - - /// \brief This is useful if you have a fixed-address internal server behind a LAN. - /// - /// Secure connections are determined by the recipient of an incoming connection. This has no effect if called on the system attempting to connect. - /// \note If secure connections are on, do not use secure connections for a specific IP address. - /// \param[in] ip IP address to add. * wildcards are supported. - void AddToSecurityExceptionList(const char *ip); - - /// \brief Remove a specific connection previously added via AddToSecurityExceptionList. - /// \param[in] ip IP address to remove. Pass 0 to remove all IP addresses. * wildcards are supported. - void RemoveFromSecurityExceptionList(const char *ip); - - /// \brief Checks to see if a given IP is in the security exception list. - /// \param[in] IP address to check. - /// \return True if the IP address is found in security exception list, else returns false. - bool IsInSecurityExceptionList(const char *ip); - - /// \brief Sets the maximum number of incoming connections allowed. - /// \details If the number of incoming connections is less than the number of players currently connected, - /// no more players will be allowed to connect. If this is greater than the maximum number of peers allowed, - /// it will be reduced to the maximum number of peers allowed. - /// - /// Defaults to 0, meaning by default, nobody can connect to you - /// \param[in] numberAllowed Maximum number of incoming connections allowed. - void SetMaximumIncomingConnections( unsigned short numberAllowed ); - - /// \brief Returns the value passed to SetMaximumIncomingConnections(). - /// \return Maximum number of incoming connections, which is always <= maxConnections - unsigned int GetMaximumIncomingConnections( void ) const; - - /// \brief Returns how many open connections exist at this time. - /// \return Number of open connections. - unsigned short NumberOfConnections(void) const; - - /// \brief Sets the password for the incoming connections. - /// \details The password must match in the call to Connect (defaults to none). - /// Pass 0 to passwordData to specify no password. - /// This is a way to set a low level password for all incoming connections. To selectively reject connections, implement your own scheme using CloseConnection() to remove unwanted connections. - /// \param[in] passwordData A data block that incoming connections must match. This can be just a password, or can be a stream of data. Specify 0 for no password data - /// \param[in] passwordDataLength The length in bytes of passwordData - void SetIncomingPassword( const char* passwordData, int passwordDataLength ); - - /// \brief Gets the password passed to SetIncomingPassword - /// \param[out] passwordData Should point to a block large enough to hold the password data you passed to SetIncomingPassword() - /// \param[in,out] passwordDataLength Maximum size of the passwordData array. Modified to hold the number of bytes actually written. - void GetIncomingPassword( char* passwordData, int *passwordDataLength ); - - /// \brief Connect to the specified host (ip or domain name) and server port. - /// \details Calling Connect and not calling SetMaximumIncomingConnections acts as a dedicated client. - /// Calling both acts as a true peer. - /// - /// This is a non-blocking connection. - /// - /// The connection is successful when GetConnectionState() returns IS_CONNECTED or Receive() gets a message with the type identifier ID_CONNECTION_REQUEST_ACCEPTED. - /// If the connection is not successful, such as a rejected connection or no response then neither of these things will happen. - /// \pre Requires that you first call Startup(). - /// \param[in] host Either a dotted IP address or a domain name. - /// \param[in] remotePort Port to connect to on the remote machine. - /// \param[in] passwordData A data block that must match the data block on the server passed to SetIncomingPassword(). This can be a string or can be a stream of data. Use 0 for no password. - /// \param[in] passwordDataLength The length in bytes of passwordData. - /// \param[in] publicKey The public key the server is using. If 0, the server is not using security. If non-zero, the publicKeyMode member determines how to connect - /// \param[in] connectionSocketIndex Index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to determine the one to send on. - /// \param[in] sendConnectionAttemptCount Number of datagrams to send to the other system to try to connect. - /// \param[in] timeBetweenSendConnectionAttemptsMS Time to elapse before a datagram is sent to the other system to try to connect. After sendConnectionAttemptCount number of attempts, ID_CONNECTION_ATTEMPT_FAILED is returned. Under low bandwidth conditions with multiple simultaneous outgoing connections, this value should be raised to 1000 or higher, or else the MTU detection can overrun the available bandwidth. - /// \param[in] timeoutTime Time to elapse before dropping the connection if a reliable message could not be sent. 0 to use the default value from SetTimeoutTime(UNASSIGNED_SYSTEM_ADDRESS); - /// \return CONNECTION_ATTEMPT_STARTED on successful initiation. Otherwise, an appropriate enumeration indicating failure. - /// \note CONNECTION_ATTEMPT_STARTED does not mean you are already connected! - /// \note It is possible to immediately get back ID_CONNECTION_ATTEMPT_FAILED if you exceed the maxConnections parameter passed to Startup(). This could happen if you call CloseConnection() with sendDisconnectionNotificaiton true, then immediately call Connect() before the connection has closed. - ConnectionAttemptResult Connect( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey=0, unsigned connectionSocketIndex=0, unsigned sendConnectionAttemptCount=6, unsigned timeBetweenSendConnectionAttemptsMS=1000, RakNet::TimeMS timeoutTime=0 ); - - /// \brief Connect to the specified host (ip or domain name) and server port. - /// \param[in] host Either a dotted IP address or a domain name. - /// \param[in] remotePort Which port to connect to on the remote machine. - /// \param[in] passwordData A data block that must match the data block on the server passed to SetIncomingPassword(). This can be a string or can be a stream of data. Use 0 for no password. - /// \param[in] passwordDataLength The length in bytes of passwordData. - /// \param[in] socket A bound socket returned by another instance of RakPeerInterface. - /// \param[in] sendConnectionAttemptCount Number of datagrams to send to the other system to try to connect. - /// \param[in] timeBetweenSendConnectionAttemptsMS Time to elapse before a datagram is sent to the other system to try to connect. After sendConnectionAttemptCount number of attempts, ID_CONNECTION_ATTEMPT_FAILED is returned.. Under low bandwidth conditions with multiple simultaneous outgoing connections, this value should be raised to 1000 or higher, or else the MTU detection can overrun the available bandwidth. - /// \param[in] timeoutTime Time to elapse before dropping the connection if a reliable message could not be sent. 0 to use the default from SetTimeoutTime(UNASSIGNED_SYSTEM_ADDRESS); - /// \return CONNECTION_ATTEMPT_STARTED on successful initiation. Otherwise, an appropriate enumeration indicating failure. - /// \note CONNECTION_ATTEMPT_STARTED does not mean you are already connected! - virtual ConnectionAttemptResult ConnectWithSocket(const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, RakNetSocket2* socket, PublicKey *publicKey=0, unsigned sendConnectionAttemptCount=6, unsigned timeBetweenSendConnectionAttemptsMS=1000, RakNet::TimeMS timeoutTime=0); - - /* /// \brief Connect to the specified network ID (Platform specific console function) - /// \details Does built-in NAT traversal - /// \param[in] networkServiceId Network ID structure for the online service - /// \param[in] passwordData A data block that must match the data block on the server passed to SetIncomingPassword(). This can be a string or can be a stream of data. Use 0 for no password. - /// \param[in] passwordDataLength The length in bytes of passwordData. - //bool Console2LobbyConnect( void *networkServiceId, const char *passwordData, int passwordDataLength );*/ - - /// \brief Stops the network threads and closes all connections. - /// \param[in] blockDuration Wait time(milli seconds) for all remaining messages to go out, including ID_DISCONNECTION_NOTIFICATION. If 0, it doesn't wait at all. - /// \param[in] orderingChannel Channel on which ID_DISCONNECTION_NOTIFICATION will be sent, if blockDuration > 0. - /// \param[in] disconnectionNotificationPriority Priority of sending ID_DISCONNECTION_NOTIFICATION. - /// If set to 0, the disconnection notification won't be sent. - void Shutdown( unsigned int blockDuration, unsigned char orderingChannel=0, PacketPriority disconnectionNotificationPriority=LOW_PRIORITY ); - - /// \brief Returns true if the network thread is running. - /// \return True if the network thread is running, False otherwise - bool IsActive( void ) const; - - /// \brief Fills the array remoteSystems with the SystemAddress of all the systems we are connected to. - /// \param[out] remoteSystems An array of SystemAddress structures, to be filled with the SystemAddresss of the systems we are connected to. Pass 0 to remoteSystems to get the number of systems we are connected to. - /// \param[in, out] numberOfSystems As input, the size of remoteSystems array. As output, the number of elements put into the array. - bool GetConnectionList( SystemAddress *remoteSystems, unsigned short *numberOfSystems ) const; - - /// Returns the next uint32_t that Send() will return - /// \note If using RakPeer from multiple threads, this may not be accurate for your thread. Use IncrementNextSendReceipt() in that case. - /// \return The next uint32_t that Send() or SendList will return - virtual uint32_t GetNextSendReceipt(void); - - /// Returns the next uint32_t that Send() will return, and increments the value by one - /// \note If using RakPeer from multiple threads, pass this to forceReceipt in the send function - /// \return The next uint32_t that Send() or SendList will return - virtual uint32_t IncrementNextSendReceipt(void); - - /// \brief Sends a block of data to the specified system that you are connected to. - /// \note This function only works while connected. - /// \note The first byte should be a message identifier starting at ID_USER_PACKET_ENUM. - /// \param[in] data Block of data to send. - /// \param[in] length Size in bytes of the data to send. - /// \param[in] priority Priority level to send on. See PacketPriority.h - /// \param[in] reliability How reliably to send this data. See PacketPriority.h - /// \param[in] orderingChannel When using ordered or sequenced messages, the channel to order these on. Messages are only ordered relative to other messages on the same stream. - /// \param[in] systemIdentifier Who to send this packet to, or in the case of broadcasting who not to send it to. Pass either a SystemAddress structure or a RakNetGUID structure. Use UNASSIGNED_SYSTEM_ADDRESS or to specify none - /// \param[in] broadcast True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. - /// \param[in] forceReceipt If 0, will automatically determine the receipt number to return. If non-zero, will return what you give it. - /// \return 0 on bad input. Otherwise a number that identifies this message. If \a reliability is a type that returns a receipt, on a later call to Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS with bytes 1-4 inclusive containing this number - uint32_t Send( const char *data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 ); - - /// \brief "Send" to yourself rather than a remote system. - /// \details The message will be processed through the plugins and returned to the game as usual. - /// This function works anytime - /// \note The first byte should be a message identifier starting at ID_USER_PACKET_ENUM - /// \param[in] data Block of data to send. - /// \param[in] length Size in bytes of the data to send. - void SendLoopback( const char *data, const int length ); - - /// \brief Sends a block of data to the specified system that you are connected to. - /// - /// Same as the above version, but takes a BitStream as input. - /// \param[in] bitStream Bitstream to send - /// \param[in] priority Priority level to send on. See PacketPriority.h - /// \param[in] reliability How reliably to send this data. See PacketPriority.h - /// \param[in] orderingChannel Channel to order the messages on, when using ordered or sequenced messages. Messages are only ordered relative to other messages on the same stream. - /// \param[in] systemIdentifier System Address or RakNetGUID to send this packet to, or in the case of broadcasting, the address not to send it to. Use UNASSIGNED_SYSTEM_ADDRESS to specify none. - /// \param[in] broadcast True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. - /// \param[in] forceReceipt If 0, will automatically determine the receipt number to return. If non-zero, will return what you give it. - /// \return 0 on bad input. Otherwise a number that identifies this message. If \a reliability is a type that returns a receipt, on a later call to Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS with bytes 1-4 inclusive containing this number - /// \note COMMON MISTAKE: When writing the first byte, bitStream->Write((unsigned char) ID_MY_TYPE) be sure it is casted to a byte, and you are not writing a 4 byte enumeration. - uint32_t Send( const RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 ); - - /// \brief Sends multiple blocks of data, concatenating them automatically. - /// - /// This is equivalent to: - /// RakNet::BitStream bs; - /// bs.WriteAlignedBytes(block1, blockLength1); - /// bs.WriteAlignedBytes(block2, blockLength2); - /// bs.WriteAlignedBytes(block3, blockLength3); - /// Send(&bs, ...) - /// - /// This function only works when connected. - /// \param[in] data An array of pointers to blocks of data - /// \param[in] lengths An array of integers indicating the length of each block of data - /// \param[in] numParameters Length of the arrays data and lengths - /// \param[in] priority Priority level to send on. See PacketPriority.h - /// \param[in] reliability How reliably to send this data. See PacketPriority.h - /// \param[in] orderingChannel Channel to order the messages on, when using ordered or sequenced messages. Messages are only ordered relative to other messages on the same stream. - /// \param[in] systemIdentifier System Address or RakNetGUID to send this packet to, or in the case of broadcasting, the address not to send it to. Use UNASSIGNED_SYSTEM_ADDRESS to specify none. - /// \param[in] broadcast True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. - /// \param[in] forceReceipt If 0, will automatically determine the receipt number to return. If non-zero, will return what you give it. - /// \return 0 on bad input. Otherwise a number that identifies this message. If \a reliability is a type that returns a receipt, on a later call to Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS with bytes 1-4 inclusive containing this number - uint32_t SendList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 ); - - /// \brief Gets a message from the incoming message queue. - /// \details Use DeallocatePacket() to deallocate the message after you are done with it. - /// User-thread functions, such as RPC calls and the plugin function PluginInterface::Update occur here. - /// \return 0 if no packets are waiting to be handled, otherwise a pointer to a packet. - /// \note COMMON MISTAKE: Be sure to call this in a loop, once per game tick, until it returns 0. If you only process one packet per game tick they will buffer up. - /// \sa RakNetTypes.h contains struct Packet. - Packet* Receive( void ); - - /// \brief Call this to deallocate a message returned by Receive() when you are done handling it. - /// \param[in] packet Message to deallocate. - void DeallocatePacket( Packet *packet ); - - /// \brief Return the total number of connections we are allowed. - /// \return Total number of connections allowed. - unsigned int GetMaximumNumberOfPeers( void ) const; - - // -------------------------------------------------------------------------------------------- Connection Management Functions-------------------------------------------------------------------------------------------- - /// \brief Close the connection to another host (if we initiated the connection it will disconnect, if they did it will kick them out). - /// \details This method closes the connection irrespective of who initiated the connection. - /// \param[in] target Which system to close the connection to. - /// \param[in] sendDisconnectionNotification True to send ID_DISCONNECTION_NOTIFICATION to the recipient. False to close it silently. - /// \param[in] channel Which ordering channel to send the disconnection notification on, if any - /// \param[in] disconnectionNotificationPriority Priority to send ID_DISCONNECTION_NOTIFICATION on. - void CloseConnection( const AddressOrGUID target, bool sendDisconnectionNotification, unsigned char orderingChannel=0, PacketPriority disconnectionNotificationPriority=LOW_PRIORITY ); - - /// \brief Cancel a pending connection attempt. - /// \details If we are already connected, the connection stays open - /// \param[in] target Target system to cancel. - void CancelConnectionAttempt( const SystemAddress target ); - /// Returns if a system is connected, disconnected, connecting in progress, or various other states - /// \param[in] systemIdentifier The system we are referring to - /// \note This locks a mutex, do not call too frequently during connection attempts or the attempt will take longer and possibly even timeout - /// \return What state the remote system is in - ConnectionState GetConnectionState(const AddressOrGUID systemIdentifier); - - /// \brief Given \a systemAddress, returns its index into remoteSystemList. - /// \details Values range from 0 to the maximum number of players allowed - 1. - /// This includes systems which were formerly connected, but are now not connected. - /// \param[in] systemAddress The SystemAddress we are referring to - /// \return The index of this SystemAddress or -1 on system not found. - int GetIndexFromSystemAddress( const SystemAddress systemAddress ) const; - - /// \brief Given \a index into remoteSystemList, will return a SystemAddress. - /// This function is only useful for looping through all systems. - /// - /// \param[in] index Index should range between 0 and the maximum number of players allowed - 1. - /// \return The SystemAddress structure corresponding to \a index in remoteSystemList. - SystemAddress GetSystemAddressFromIndex( unsigned int index ); - - /// \brief Same as GetSystemAddressFromIndex but returns RakNetGUID - /// \param[in] index Index should range between 0 and the maximum number of players allowed - 1. - /// \return The RakNetGUID - RakNetGUID GetGUIDFromIndex( unsigned int index ); - - /// \brief Same as calling GetSystemAddressFromIndex and GetGUIDFromIndex for all systems, but more efficient - /// Indices match each other, so \a addresses[0] and \a guids[0] refer to the same system - /// \param[out] addresses All system addresses. Size of the list is the number of connections. Size of the \a addresses list will match the size of the \a guids list. - /// \param[out] guids All guids. Size of the list is the number of connections. Size of the list will match the size of the \a addresses list. - void GetSystemList(DataStructures::List &addresses, DataStructures::List &guids) const; - - /// \brief Bans an IP from connecting. - /// \details Banned IPs persist between connections but are not saved on shutdown nor loaded on startup. - /// \param[in] IP Dotted IP address. You can use * for a wildcard address, such as 128.0.0. * will ban all IP addresses starting with 128.0.0. - /// \param[in] milliseconds Gives time in milli seconds for a temporary ban of the IP address. Use 0 for a permanent ban. - void AddToBanList( const char *IP, RakNet::TimeMS milliseconds=0 ); - - /// \brief Allows a previously banned IP to connect. - /// param[in] Dotted IP address. You can use * as a wildcard. An IP such as 128.0.0.* will ban all IP addresses starting with 128.0.0. - void RemoveFromBanList( const char *IP ); - - /// \brief Allows all previously banned IPs to connect. - void ClearBanList( void ); - - /// \brief Returns true or false indicating if a particular IP is banned. - /// \param[in] IP Dotted IP address. - /// \return True if IP matches any IPs in the ban list, accounting for any wildcards. False otherwise. - bool IsBanned( const char *IP ); - - /// \brief Enable or disable allowing frequent connections from the same IP adderss - /// \details This is a security measure which is disabled by default, but can be set to true to prevent attackers from using up all connection slots. - /// \param[in] b True to limit connections from the same ip to at most 1 per 100 milliseconds. - void SetLimitIPConnectionFrequency(bool b); - - // --------------------------------------------------------------------------------------------Pinging Functions - Functions dealing with the automatic ping mechanism-------------------------------------------------------------------------------------------- - /// Send a ping to the specified connected system. - /// \pre The sender and recipient must already be started via a successful call to Startup() - /// \param[in] target Which system to ping - void Ping( const SystemAddress target ); - - /// \brief Send a ping to the specified unconnected system. - /// \details The remote system, if it is Initialized, will respond with ID_PONG followed by sizeof(RakNet::TimeMS) containing the system time the ping was sent. Default is 4 bytes - See __GET_TIME_64BIT in RakNetTypes.h - /// System should reply with ID_PONG if it is active - /// \param[in] host Either a dotted IP address or a domain name. Can be 255.255.255.255 for LAN broadcast. - /// \param[in] remotePort Which port to connect to on the remote machine. - /// \param[in] onlyReplyOnAcceptingConnections Only request a reply if the remote system is accepting connections - /// \param[in] connectionSocketIndex Index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to send on. - /// \return true on success, false on failure (unknown hostname) - bool Ping( const char* host, unsigned short remotePort, bool onlyReplyOnAcceptingConnections, unsigned connectionSocketIndex=0 ); - - /// \brief Returns the average of all ping times read for the specific system or -1 if none read yet - /// \param[in] systemAddress Which system we are referring to - /// \return The ping time for this system, or -1 - int GetAveragePing( const AddressOrGUID systemIdentifier ); - - /// \brief Returns the last ping time read for the specific system or -1 if none read yet. - /// \param[in] systemAddress Which system we are referring to - /// \return The last ping time for this system, or -1. - int GetLastPing( const AddressOrGUID systemIdentifier ) const; - - /// \brief Returns the lowest ping time read or -1 if none read yet. - /// \param[in] systemIdentifier Which system we are referring to - /// \return The lowest ping time for this system, or -1. - int GetLowestPing( const AddressOrGUID systemIdentifier ) const; - - /// Ping the remote systems every so often, or not. Can be called anytime. - /// By default this is true. Recommended to leave on, because congestion control uses it to determine how often to resend lost packets. - /// It would be true by default to prevent timestamp drift, since in the event of a clock spike, the timestamp deltas would no longer be accurate - /// \param[in] doPing True to start occasional pings. False to stop them. - void SetOccasionalPing( bool doPing ); - - /// Return the clock difference between your system and the specified system - /// Subtract GetClockDifferential() from a time returned by the remote system to get that time relative to your own system - /// Returns 0 if the system is unknown - /// \param[in] systemIdentifier Which system we are referring to - RakNet::Time GetClockDifferential( const AddressOrGUID systemIdentifier ); - - // --------------------------------------------------------------------------------------------Static Data Functions - Functions dealing with API defined synchronized memory-------------------------------------------------------------------------------------------- - /// \brief Sets the data to send along with a LAN server discovery or offline ping reply. - /// \param[in] data Block of data to send, or 0 for none - /// \param[in] length Length of the data in bytes, or 0 for none - /// \note \a length should be under 400 bytes, as a security measure against flood attacks - /// \sa Ping.cpp - void SetOfflinePingResponse( const char *data, const unsigned int length ); - - /// \brief Returns pointers to a copy of the \a data passed to SetOfflinePingResponse. - /// \param[out] data A pointer to a copy of the data passed to SetOfflinePingResponse() - /// \param[out] length A pointer filled in with the length parameter passed to SetOfflinePingResponse() - /// \sa SetOfflinePingResponse - void GetOfflinePingResponse( char **data, unsigned int *length ); - - //--------------------------------------------------------------------------------------------Network Functions - Functions dealing with the network in general-------------------------------------------------------------------------------------------- - /// \brief Returns the unique address identifier that represents you or another system on the the network - /// \note Not supported by the XBOX - /// \param[in] systemAddress Use UNASSIGNED_SYSTEM_ADDRESS to get your behind-LAN address. Use a connected system to get their behind-LAN address. This does not return the port. - /// \param[in] index When you have multiple internal IDs, which index to return? Currently limited to MAXIMUM_NUMBER_OF_INTERNAL_IDS (so the maximum value of this variable is MAXIMUM_NUMBER_OF_INTERNAL_IDS-1) - /// \return Identifier of your system internally, which may not be how other systems see if you if you are behind a NAT or proxy. - SystemAddress GetInternalID( const SystemAddress systemAddress=UNASSIGNED_SYSTEM_ADDRESS, const int index=0 ) const; - - /// \brief Sets your internal IP address, for platforms that do not support reading it, or to override a value - /// \param[in] systemAddress. The address to set. Use SystemAddress::FromString() if you want to use a dotted string - /// \param[in] index When you have multiple internal IDs, which index to set? - void SetInternalID(SystemAddress systemAddress, int index=0); - - /// \brief Returns the unique address identifier that represents the target on the the network and is based on the target's external IP / port. - /// \param[in] target The SystemAddress of the remote system. Usually the same for all systems, unless you have two or more network cards. - SystemAddress GetExternalID( const SystemAddress target ) const; - - /// Return my own GUID - const RakNetGUID GetMyGUID(void) const; - - /// Return the address bound to a socket at the specified index - SystemAddress GetMyBoundAddress(const int socketIndex=0); - - /// \brief Given a connected system address, this method gives the unique GUID representing that instance of RakPeer. - /// This will be the same on all systems connected to that instance of RakPeer, even if the external system addresses are different. - /// Complexity is O(log2(n)). - /// If \a input is UNASSIGNED_SYSTEM_ADDRESS, will return your own GUID - /// \pre Call Startup() first, or the function will return UNASSIGNED_RAKNET_GUID - /// \param[in] input The system address of the target system we are connected to. - const RakNetGUID& GetGuidFromSystemAddress( const SystemAddress input ) const; - - /// \brief Gives the system address of a connected system, given its GUID. - /// The GUID will be the same on all systems connected to that instance of RakPeer, even if the external system addresses are different. - /// Currently O(log(n)), but this may be improved in the future - /// If \a input is UNASSIGNED_RAKNET_GUID, UNASSIGNED_SYSTEM_ADDRESS is returned. - /// \param[in] input The RakNetGUID of the target system. - SystemAddress GetSystemAddressFromGuid( const RakNetGUID input ) const; - - /// Given the SystemAddress of a connected system, get the public key they provided as an identity - /// Returns false if system address was not found or client public key is not known - /// \param[in] input The RakNetGUID of the system - /// \param[in] client_public_key The connected client's public key is copied to this address. Buffer must be cat::EasyHandshake::PUBLIC_KEY_BYTES bytes in length. - bool GetClientPublicKeyFromSystemAddress( const SystemAddress input, char *client_public_key ) const; - - /// \brief Set the time, in MS, to use before considering ourselves disconnected after not being able to deliver a reliable message. - - /// Set the time, in MS, to use before considering ourselves disconnected after not being able to deliver a reliable message. - /// Default time is 10,000 or 10 seconds in release and 30,000 or 30 seconds in debug. - /// Do not set different values for different computers that are connected to each other, or you won't be able to reconnect after ID_CONNECTION_LOST - /// \param[in] timeMS Time, in MS - /// \param[in] target SystemAddress structure of the target system. Pass UNASSIGNED_SYSTEM_ADDRESS for all systems. - void SetTimeoutTime( RakNet::TimeMS timeMS, const SystemAddress target ); - - /// \brief Returns the Timeout time for the given system. - /// \param[in] target Target system to get the TimeoutTime for. Pass UNASSIGNED_SYSTEM_ADDRESS to get the default value. - /// \return Timeout time for a given system. - RakNet::TimeMS GetTimeoutTime( const SystemAddress target ); - - /// \brief Returns the current MTU size - /// \param[in] target Which system to get MTU for. UNASSIGNED_SYSTEM_ADDRESS to get the default - /// \return The current MTU size of the target system. - int GetMTUSize( const SystemAddress target ) const; - - /// \brief Returns the number of IP addresses this system has internally. - /// \details Get the actual addresses from GetLocalIP() - unsigned GetNumberOfAddresses( void ); - - /// Returns an IP address at index 0 to GetNumberOfAddresses-1 in ipList array. - /// \param[in] index index into the list of IP addresses - /// \return The local IP address at this index - const char* GetLocalIP( unsigned int index ); - - /// Is this a local IP? - /// Checks if this ip is in the ipList array. - /// \param[in] An IP address to check, excluding the port. - /// \return True if this is one of the IP addresses returned by GetLocalIP - bool IsLocalIP( const char *ip ); - - /// \brief Allow or disallow connection responses from any IP. - /// \details Normally this should be false, but may be necessary when connecting to servers with multiple IP addresses. - /// \param[in] allow - True to allow this behavior, false to not allow. Defaults to false. Value persists between connections. - void AllowConnectionResponseIPMigration( bool allow ); - - /// \brief Sends a one byte message ID_ADVERTISE_SYSTEM to the remote unconnected system. - /// This will send our external IP outside the LAN along with some user data to the remote system. - /// \pre The sender and recipient must already be started via a successful call to Initialize - /// \param[in] host Either a dotted IP address or a domain name - /// \param[in] remotePort Which port to connect to on the remote machine. - /// \param[in] data Optional data to append to the packet. - /// \param[in] dataLength Length of data in bytes. Use 0 if no data. - /// \param[in] connectionSocketIndex Index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to send on. - /// \return False if IsActive()==false or the host is unresolvable. True otherwise. - bool AdvertiseSystem( const char *host, unsigned short remotePort, const char *data, int dataLength, unsigned connectionSocketIndex=0 ); - - /// \brief Controls how often to return ID_DOWNLOAD_PROGRESS for large message downloads. - /// \details ID_DOWNLOAD_PROGRESS is returned to indicate a new partial message chunk, roughly the MTU size, has arrived. - /// As it can be slow or cumbersome to get this notification for every chunk, you can set the interval at which it is returned. - /// Defaults to 0 (never return this notification). - /// \param[in] interval How many messages to use as an interval before a download progress notification is returned. - void SetSplitMessageProgressInterval(int interval); - - /// \brief Returns what was passed to SetSplitMessageProgressInterval(). - /// \return Number of messages to be recieved before a download progress notification is returned. Default to 0. - int GetSplitMessageProgressInterval(void) const; - - /// \brief Set how long to wait before giving up on sending an unreliable message. - /// Useful if the network is clogged up. - /// Set to 0 or less to never timeout. Defaults to 0. - /// \param[in] timeoutMS How many ms to wait before simply not sending an unreliable message. - void SetUnreliableTimeout(RakNet::TimeMS timeoutMS); - - /// \brief Send a message to a host, with the IP socket option TTL set to 3. - /// \details This message will not reach the host, but will open the router. - /// \param[in] host The address of the remote host in dotted notation. - /// \param[in] remotePort The port number to send to. - /// \param[in] ttl Max hops of datagram, set to 3 - /// \param[in] connectionSocketIndex userConnectionSocketIndex. - /// \remarks Used for NAT-Punchthrough - void SendTTL( const char* host, unsigned short remotePort, int ttl, unsigned connectionSocketIndex=0 ); - - // -------------------------------------------------------------------------------------------- Plugin Functions-------------------------------------------------------------------------------------------- - /// \brief Attaches a Plugin interface to an instance of the base class (RakPeer or PacketizedTCP) to run code automatically on message receipt in the Receive call. - /// If the plugin returns false from PluginInterface::UsesReliabilityLayer(), which is the case for all plugins except PacketLogger, you can call AttachPlugin() and DetachPlugin() for this plugin while RakPeer is active. - /// \param[in] messageHandler Pointer to the plugin to attach. - void AttachPlugin( PluginInterface2 *plugin ); - - /// \brief Detaches a Plugin interface from the instance of the base class (RakPeer or PacketizedTCP) it is attached to. - /// \details This method disables the plugin code from running automatically on base class's updates or message receipt. - /// If the plugin returns false from PluginInterface::UsesReliabilityLayer(), which is the case for all plugins except PacketLogger, you can call AttachPlugin() and DetachPlugin() for this plugin while RakPeer is active. - /// \param[in] messageHandler Pointer to a plugin to detach. - void DetachPlugin( PluginInterface2 *messageHandler ); - - // --------------------------------------------------------------------------------------------Miscellaneous Functions-------------------------------------------------------------------------------------------- - /// \brief Puts a message back in the receive queue in case you don't want to deal with it immediately. - /// \param[in] packet The pointer to the packet you want to push back. - /// \param[in] pushAtHead True to push the packet at the start of the queue so that the next receive call returns it. False to push it at the end of the queue. - /// \note Setting pushAtHead to false end makes the packets out of order. - void PushBackPacket( Packet *packet, bool pushAtHead ); - - /// \internal - /// \brief For a given system identified by \a guid, change the SystemAddress to send to. - /// \param[in] guid The connection we are referring to - /// \param[in] systemAddress The new address to send to - void ChangeSystemAddress(RakNetGUID guid, const SystemAddress &systemAddress); - - /// \brief Returns a packet for you to write to if you want to create a Packet for some reason. - /// You can add it to the receive buffer with PushBackPacket - /// \param[in] dataSize How many bytes to allocate for the buffer - /// \return A packet. - Packet* AllocatePacket(unsigned dataSize); - - /// \brief Get the socket used with a particular active connection. - /// The smart pointer reference counts the RakNetSocket object, so the socket will remain active as long as the smart pointer does, even if RakNet were to shutdown or close the connection. - /// \note This sends a query to the thread and blocks on the return value for up to one second. In practice it should only take a millisecond or so. - /// \param[in] target Which system. - /// \return A smart pointer object containing the socket information about the target. Be sure to check IsNull() which is returned if the update thread is unresponsive, shutting down, or if this system is not connected. - virtual RakNetSocket2* GetSocket( const SystemAddress target ); - - /// \brief Gets all sockets in use. - /// \note This sends a query to the thread and blocks on the return value for up to one second. In practice it should only take a millisecond or so. - /// \param[out] sockets List of RakNetSocket structures in use. - virtual void GetSockets( DataStructures::List &sockets ); - virtual void ReleaseSockets( DataStructures::List &sockets ); - - /// \internal - virtual void WriteOutOfBandHeader(RakNet::BitStream *bitStream); - - /// If you need code to run in the same thread as RakNet's update thread, this function can be used for that - /// \param[in] _userUpdateThreadPtr C callback function - /// \param[in] _userUpdateThreadData Passed to C callback function - virtual void SetUserUpdateThread(void (*_userUpdateThreadPtr)(RakPeerInterface *, void *), void *_userUpdateThreadData); - - /// Set a C callback to be called whenever a datagram arrives - /// Return true from the callback to have RakPeer handle the datagram. Return false and RakPeer will ignore the datagram. - /// This can be used to filter incoming datagrams by system, or to share a recvfrom socket with RakPeer - /// RNS2RecvStruct will only remain valid for the duration of the call - virtual void SetIncomingDatagramEventHandler( bool (*_incomingDatagramEventHandler)(RNS2RecvStruct *) ); - - // --------------------------------------------------------------------------------------------Network Simulator Functions-------------------------------------------------------------------------------------------- - /// Adds simulated ping and packet loss to the outgoing data flow. - /// To simulate bi-directional ping and packet loss, you should call this on both the sender and the recipient, with half the total ping and packetloss value on each. - /// You can exclude network simulator code with the _RELEASE #define to decrease code size - /// \deprecated Use http://www.jenkinssoftware.com/forum/index.php?topic=1671.0 instead. - /// \note Doesn't work past version 3.6201 - /// \param[in] packetloss Chance to lose a packet. Ranges from 0 to 1. - /// \param[in] minExtraPing The minimum time to delay sends. - /// \param[in] extraPingVariance The additional random time to delay sends. - virtual void ApplyNetworkSimulator( float packetloss, unsigned short minExtraPing, unsigned short extraPingVariance); - - /// Limits how much outgoing bandwidth can be sent per-connection. - /// This limit does not apply to the sum of all connections! - /// Exceeding the limit queues up outgoing traffic - /// \param[in] maxBitsPerSecond Maximum bits per second to send. Use 0 for unlimited (default). Once set, it takes effect immedately and persists until called again. - virtual void SetPerConnectionOutgoingBandwidthLimit( unsigned maxBitsPerSecond ); - - /// Returns if you previously called ApplyNetworkSimulator - /// \return If you previously called ApplyNetworkSimulator - virtual bool IsNetworkSimulatorActive( void ); - - // --------------------------------------------------------------------------------------------Statistical Functions - Functions dealing with API performance-------------------------------------------------------------------------------------------- - - /// \brief Returns a structure containing a large set of network statistics for the specified system. - /// You can map this data to a string using the C style StatisticsToString() function - /// \param[in] systemAddress Which connected system to get statistics for. - /// \param[in] rns If you supply this structure,the network statistics will be written to it. Otherwise the method uses a static struct to write the data, which is not threadsafe. - /// \return 0 if the specified system can't be found. Otherwise a pointer to the struct containing the specified system's network statistics. - /// \sa RakNetStatistics.h - RakNetStatistics * GetStatistics( const SystemAddress systemAddress, RakNetStatistics *rns=0 ); - /// \brief Returns the network statistics of the system at the given index in the remoteSystemList. - /// \return True if the index is less than the maximum number of peers allowed and the system is active. False otherwise. - bool GetStatistics( const unsigned int index, RakNetStatistics *rns ); - /// \brief Returns the list of systems, and statistics for each of those systems - /// Each system has one entry in each of the lists, in the same order - /// \param[out] addresses SystemAddress for each connected system - /// \param[out] guids RakNetGUID for each connected system - /// \param[out] statistics Calculated RakNetStatistics for each connected system - virtual void GetStatisticsList(DataStructures::List &addresses, DataStructures::List &guids, DataStructures::List &statistics); - - /// \Returns how many messages are waiting when you call Receive() - virtual unsigned int GetReceiveBufferSize(void); - - // --------------------------------------------------------------------------------------------EVERYTHING AFTER THIS COMMENT IS FOR INTERNAL USE ONLY-------------------------------------------------------------------------------------------- - - - /// \internal - // Call manually if RAKPEER_USER_THREADED==1 at least every 30 milliseconds. - // updateBitStream should be: - // BitStream updateBitStream( MAXIMUM_MTU_SIZE - // #if LIBCAT_SECURITY==1 - // + cat::AuthenticatedEncryption::OVERHEAD_BYTES - // #endif - // ); - bool RunUpdateCycle( BitStream &updateBitStream ); - - /// \internal - // Call manually if RAKPEER_USER_THREADED==1 at least every 30 milliseconds. - // Call in a loop until returns false if the socket is non-blocking - // remotePortRakNetWasStartedOn_PS3 and extraSocketOptions are from SocketDescriptor when the socket was created - // bool RunRecvFromOnce( RakNetSocket *s ); - - /// \internal - bool SendOutOfBand(const char *host, unsigned short remotePort, const char *data, BitSize_t dataLength, unsigned connectionSocketIndex=0 ); - - // static Packet *AllocPacket(unsigned dataSize, const char *file, unsigned int line); - - /// \internal - /// \brief Holds the clock differences between systems, along with the ping - struct PingAndClockDifferential - { - unsigned short pingTime; - RakNet::Time clockDifferential; - }; - - /// \internal - /// \brief All the information representing a connected system - struct RemoteSystemStruct - { - bool isActive; // Is this structure in use? - SystemAddress systemAddress; /// Their external IP on the internet - SystemAddress myExternalSystemAddress; /// Your external IP on the internet, from their perspective - SystemAddress theirInternalSystemAddress[MAXIMUM_NUMBER_OF_INTERNAL_IDS]; /// Their internal IP, behind the LAN - ReliabilityLayer reliabilityLayer; /// The reliability layer associated with this player - bool weInitiatedTheConnection; /// True if we started this connection via Connect. False if someone else connected to us. - PingAndClockDifferential pingAndClockDifferential[ PING_TIMES_ARRAY_SIZE ]; /// last x ping times and calculated clock differentials with it - RakNet::Time pingAndClockDifferentialWriteIndex; /// The index we are writing into the pingAndClockDifferential circular buffer - unsigned short lowestPing; ///The lowest ping value encountered - RakNet::Time nextPingTime; /// When to next ping this player - RakNet::Time lastReliableSend; /// When did the last reliable send occur. Reliable sends must occur at least once every timeoutTime/2 units to notice disconnects - RakNet::Time connectionTime; /// connection time, if active. -// int connectionSocketIndex; // index into connectionSockets to send back on. - RakNetGUID guid; - int MTUSize; - // Reference counted socket to send back on - RakNetSocket2* rakNetSocket; - SystemIndex remoteSystemIndex; - -#if LIBCAT_SECURITY==1 - // Cached answer used internally by RakPeer to prevent DoS attacks based on the connexion handshake - char answer[cat::EasyHandshake::ANSWER_BYTES]; - - // If the server has bRequireClientKey = true, then this is set to the validated public key of the connected client - // Valid after connectMode reaches HANDLING_CONNECTION_REQUEST - char client_public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES]; -#endif - - enum ConnectMode {NO_ACTION, DISCONNECT_ASAP, DISCONNECT_ASAP_SILENTLY, DISCONNECT_ON_NO_ACK, REQUESTED_CONNECTION, HANDLING_CONNECTION_REQUEST, UNVERIFIED_SENDER, CONNECTED} connectMode; - }; - - // DS_APR - //void ProcessChromePacket(RakNetSocket2 *s, const char *buffer, int dataSize, const SystemAddress& recvFromAddress, RakNet::TimeUS timeRead); - // /DS_APR -protected: - - friend RAK_THREAD_DECLARATION(UpdateNetworkLoop); - //friend RAK_THREAD_DECLARATION(RecvFromLoop); - friend RAK_THREAD_DECLARATION(UDTConnect); - - friend bool ProcessOfflineNetworkPacket( SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSocket2* rakNetSocket, bool *isOfflineMessage, RakNet::TimeUS timeRead ); - friend void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNet::TimeUS timeRead, BitStream &updateBitStream ); - friend void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSocket2* rakNetSocket, RakNet::TimeUS timeRead, BitStream &updateBitStream ); - - int GetIndexFromSystemAddress( const SystemAddress systemAddress, bool calledFromNetworkThread ) const; - int GetIndexFromGuid( const RakNetGUID guid ); - - //void RemoveFromRequestedConnectionsList( const SystemAddress systemAddress ); - // Two versions needed because some buggy compilers strip the last parameter if unused, and crashes - ConnectionAttemptResult SendConnectionRequest( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned int extraData, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNet::TimeMS timeoutTime, RakNetSocket2* socket ); - ConnectionAttemptResult SendConnectionRequest( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned int extraData, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNet::TimeMS timeoutTime ); - ///Get the reliability layer associated with a systemAddress. - /// \param[in] systemAddress The player identifier - /// \return 0 if none - RemoteSystemStruct *GetRemoteSystemFromSystemAddress( const SystemAddress systemAddress, bool calledFromNetworkThread, bool onlyActive ) const; - RakPeer::RemoteSystemStruct *GetRemoteSystem( const AddressOrGUID systemIdentifier, bool calledFromNetworkThread, bool onlyActive ) const; - void ValidateRemoteSystemLookup(void) const; - RemoteSystemStruct *GetRemoteSystemFromGUID( const RakNetGUID guid, bool onlyActive ) const; - ///Parse out a connection request packet - void ParseConnectionRequestPacket( RakPeer::RemoteSystemStruct *remoteSystem, const SystemAddress &systemAddress, const char *data, int byteSize); - void OnConnectionRequest( RakPeer::RemoteSystemStruct *remoteSystem, RakNet::Time incomingTimestamp ); - ///Send a reliable disconnect packet to this player and disconnect them when it is delivered - void NotifyAndFlagForShutdown( const SystemAddress systemAddress, bool performImmediate, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority ); - ///Returns how many remote systems initiated a connection to us - unsigned int GetNumberOfRemoteInitiatedConnections( void ) const; - /// \brief Get a free remote system from the list and assign our systemAddress to it. - /// \note Should only be called from the update thread - not the user thread. - /// \param[in] systemAddress systemAddress to be assigned - /// \param[in] connectionMode connection mode of the RemoteSystem. - /// \param[in] rakNetSocket - /// \param[in] thisIPConnectedRecently Is this IP connected recently? set to False; - /// \param[in] bindingAddress Address to be binded with the remote system - /// \param[in] incomingMTU MTU for the remote system - RemoteSystemStruct * AssignSystemAddressToRemoteSystemList( const SystemAddress systemAddress, RemoteSystemStruct::ConnectMode connectionMode, RakNetSocket2* incomingRakNetSocket, bool *thisIPConnectedRecently, SystemAddress bindingAddress, int incomingMTU, RakNetGUID guid, bool useSecurity ); - /// \brief Adjust the timestamp of the incoming packet to be relative to this system. - /// \param[in] data Data in the incoming packet. - /// \param[in] systemAddress Sender of the incoming packet. - void ShiftIncomingTimestamp( unsigned char *data, const SystemAddress &systemAddress ) const; - /// Get the most accurate clock differential for a certain player. - /// \param[in] systemAddress The player with whose clock the time difference is calculated. - /// \returns The clock differential for a certain player. - RakNet::Time GetBestClockDifferential( const SystemAddress systemAddress ) const; - - bool IsLoopbackAddress(const AddressOrGUID &systemIdentifier, bool matchPort) const; - SystemAddress GetLoopbackAddress(void) const; - - ///Set this to true to terminate the Peer thread execution - volatile bool endThreads; - ///true if the peer thread is active. - volatile bool isMainLoopThreadActive; - - // RakNet::LocklessUint32_t isRecvFromLoopThreadActive; - - - bool occasionalPing; /// Do we occasionally ping the other systems?*/ - ///Store the maximum number of peers allowed to connect - unsigned int maximumNumberOfPeers; - //05/02/06 Just using maximumNumberOfPeers instead - ///Store the maximum number of peers able to connect, including reserved connection slots for pings, etc. - //unsigned short remoteSystemListSize; - ///Store the maximum incoming connection allowed - unsigned int maximumIncomingConnections; - RakNet::BitStream offlinePingResponse; - ///Local Player ID - // SystemAddress mySystemAddress[MAXIMUM_NUMBER_OF_INTERNAL_IDS]; - char incomingPassword[256]; - unsigned char incomingPasswordLength; - - /// This is an array of pointers to RemoteSystemStruct - /// This allows us to preallocate the list when starting, so we don't have to allocate or delete at runtime. - /// Another benefit is that is lets us add and remove active players simply by setting systemAddress - /// and moving elements in the list by copying pointers variables without affecting running threads, even if they are in the reliability layer - RemoteSystemStruct* remoteSystemList; - /// activeSystemList holds a list of pointers and is preallocated to be the same size as remoteSystemList. It is updated only by the network thread, but read by both threads - /// When the isActive member of RemoteSystemStruct is set to true or false, that system is added to this list of pointers - /// Threadsafe because RemoteSystemStruct is preallocated, and the list is only added to, not removed from - RemoteSystemStruct** activeSystemList; - unsigned int activeSystemListSize; - - // Use a hash, with binaryAddress plus port mod length as the index - RemoteSystemIndex **remoteSystemLookup; - unsigned int RemoteSystemLookupHashIndex(const SystemAddress &sa) const; - void ReferenceRemoteSystem(const SystemAddress &sa, unsigned int remoteSystemListIndex); - void DereferenceRemoteSystem(const SystemAddress &sa); - RemoteSystemStruct* GetRemoteSystem(const SystemAddress &sa) const; - unsigned int GetRemoteSystemIndex(const SystemAddress &sa) const; - void ClearRemoteSystemLookup(void); - DataStructures::MemoryPool remoteSystemIndexPool; - - void AddToActiveSystemList(unsigned int remoteSystemListIndex); - void RemoveFromActiveSystemList(const SystemAddress &sa); - -// unsigned int LookupIndexUsingHashIndex(const SystemAddress &sa) const; -// unsigned int RemoteSystemListIndexUsingHashIndex(const SystemAddress &sa) const; -// unsigned int FirstFreeRemoteSystemLookupIndex(const SystemAddress &sa) const; - - enum - { - // Only put these mutexes in user thread functions! - requestedConnectionList_Mutex, - offlinePingResponse_Mutex, - NUMBER_OF_RAKPEER_MUTEXES - }; - SimpleMutex rakPeerMutexes[ NUMBER_OF_RAKPEER_MUTEXES ]; - ///RunUpdateCycle is not thread safe but we don't need to mutex calls. Just skip calls if it is running already - - bool updateCycleIsRunning; - ///The list of people we have tried to connect to recently - - //DataStructures::Queue requestedConnectionsList; - ///Data that both the client and the server needs - - unsigned int bytesSentPerSecond, bytesReceivedPerSecond; - // bool isSocketLayerBlocking; - // bool continualPing,isRecvfromThreadActive,isMainLoopThreadActive, endThreads, isSocketLayerBlocking; - unsigned int validationInteger; - SimpleMutex incomingQueueMutex, banListMutex; //,synchronizedMemoryQueueMutex, automaticVariableSynchronizationMutex; - //DataStructures::Queue incomingpacketSingleProducerConsumer; //, synchronizedMemorypacketSingleProducerConsumer; - // BitStream enumerationData; - - struct BanStruct - { - char *IP; - RakNet::TimeMS timeout; // 0 for none - }; - - struct RequestedConnectionStruct - { - SystemAddress systemAddress; - RakNet::Time nextRequestTime; - unsigned char requestsMade; - char *data; - unsigned short dataLength; - char outgoingPassword[256]; - unsigned char outgoingPasswordLength; - unsigned socketIndex; - unsigned int extraData; - unsigned sendConnectionAttemptCount; - unsigned timeBetweenSendConnectionAttemptsMS; - RakNet::TimeMS timeoutTime; - PublicKeyMode publicKeyMode; - RakNetSocket2* socket; - enum {CONNECT=1, /*PING=2, PING_OPEN_CONNECTIONS=4,*/ /*ADVERTISE_SYSTEM=2*/} actionToTake; - -#if LIBCAT_SECURITY==1 - char handshakeChallenge[cat::EasyHandshake::CHALLENGE_BYTES]; - cat::ClientEasyHandshake *client_handshake; - char remote_public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES]; -// char remote_challenge[cat::EasyHandshake::CHALLENGE_BYTES]; - // char random[16]; -#endif - }; -#if LIBCAT_SECURITY==1 - bool GenerateConnectionRequestChallenge(RequestedConnectionStruct *rcs,PublicKey *publicKey); -#endif - - //DataStructures::List* > automaticVariableSynchronizationList; - DataStructures::List banList; - // Threadsafe, and not thread safe - DataStructures::List pluginListTS, pluginListNTS; - - DataStructures::Queue requestedConnectionQueue; - SimpleMutex requestedConnectionQueueMutex; - - // void RunMutexedUpdateCycle(void); - - struct BufferedCommandStruct - { - BitSize_t numberOfBitsToSend; - PacketPriority priority; - PacketReliability reliability; - char orderingChannel; - AddressOrGUID systemIdentifier; - bool broadcast; - RemoteSystemStruct::ConnectMode connectionMode; - NetworkID networkID; - bool blockingCommand; // Only used for RPC - char *data; - bool haveRakNetCloseSocket; - unsigned connectionSocketIndex; - unsigned short remotePortRakNetWasStartedOn_PS3; - unsigned int extraSocketOptions; - RakNetSocket2* socket; - unsigned short port; - uint32_t receipt; - enum {BCS_SEND, BCS_CLOSE_CONNECTION, BCS_GET_SOCKET, BCS_CHANGE_SYSTEM_ADDRESS,/* BCS_USE_USER_SOCKET, BCS_REBIND_SOCKET_ADDRESS, BCS_RPC, BCS_RPC_SHIFT,*/ BCS_DO_NOTHING} command; - }; - - // Single producer single consumer queue using a linked list - //BufferedCommandStruct* bufferedCommandReadIndex, bufferedCommandWriteIndex; - - DataStructures::ThreadsafeAllocatingQueue bufferedCommands; - - - // DataStructures::ThreadsafeAllocatingQueue bufferedPackets; - - DataStructures::Queue bufferedPacketsFreePool; - RakNet::SimpleMutex bufferedPacketsFreePoolMutex; - DataStructures::Queue bufferedPacketsQueue; - RakNet::SimpleMutex bufferedPacketsQueueMutex; - - virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line); - virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line); - void SetupBufferedPackets(void); - void PushBufferedPacket(RNS2RecvStruct * p); - RNS2RecvStruct *PopBufferedPacket(void); - - struct SocketQueryOutput - { - SocketQueryOutput() {} - ~SocketQueryOutput() {} - DataStructures::List sockets; - }; - - DataStructures::ThreadsafeAllocatingQueue socketQueryOutput; - - - bool AllowIncomingConnections(void) const; - - void PingInternal( const SystemAddress target, bool performImmediate, PacketReliability reliability ); - // This stores the user send calls to be handled by the update thread. This way we don't have thread contention over systemAddresss - void CloseConnectionInternal( const AddressOrGUID& systemIdentifier, bool sendDisconnectionNotification, bool performImmediate, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority ); - void SendBuffered( const char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt ); - void SendBufferedList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt ); - bool SendImmediate( char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, bool useCallerDataAllocation, RakNet::TimeUS currentTime, uint32_t receipt ); - //bool HandleBufferedRPC(BufferedCommandStruct *bcs, RakNet::TimeMS time); - void ClearBufferedCommands(void); - void ClearBufferedPackets(void); - void ClearSocketQueryOutput(void); - void ClearRequestedConnectionList(void); - void AddPacketToProducer(RakNet::Packet *p); - unsigned int GenerateSeedFromGuid(void); - RakNet::Time GetClockDifferentialInt(RemoteSystemStruct *remoteSystem) const; - SimpleMutex securityExceptionMutex; - - //DataStructures::AVLBalancedBinarySearchTree rpcTree; - int defaultMTUSize; - bool trackFrequencyTable; - - // Smart pointer so I can return the object to the user - DataStructures::List socketList; - void DerefAllSockets(void); - unsigned int GetRakNetSocketFromUserConnectionSocketIndex(unsigned int userIndex) const; - // Used for RPC replies - RakNet::BitStream *replyFromTargetBS; - SystemAddress replyFromTargetPlayer; - bool replyFromTargetBroadcast; - - RakNet::TimeMS defaultTimeoutTime; - - // Generate and store a unique GUID - void GenerateGUID(void); - unsigned int GetSystemIndexFromGuid( const RakNetGUID input ) const; - RakNetGUID myGuid; - - unsigned maxOutgoingBPS; - - // Nobody would use the internet simulator in a final build. -#ifdef _DEBUG - double _packetloss; - unsigned short _minExtraPing, _extraPingVariance; -#endif - - ///How long it has been since things were updated by a call to receiveUpdate thread uses this to determine how long to sleep for - //unsigned int lastUserUpdateCycle; - /// True to allow connection accepted packets from anyone. False to only allow these packets from servers we requested a connection to. - bool allowConnectionResponseIPMigration; - - SystemAddress firstExternalID; - int splitMessageProgressInterval; - RakNet::TimeMS unreliableTimeout; - - bool (*incomingDatagramEventHandler)(RNS2RecvStruct *); - - // Systems in this list will not go through the secure connection process, even when secure connections are turned on. Wildcards are accepted. - DataStructures::List securityExceptionList; - - SystemAddress ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ]; - - bool allowInternalRouting; - - void (*userUpdateThreadPtr)(RakPeerInterface *, void *); - void *userUpdateThreadData; - - - SignaledEvent quitAndDataEvents; - bool limitConnectionFrequencyFromTheSameIP; - - SimpleMutex packetAllocationPoolMutex; - DataStructures::MemoryPool packetAllocationPool; - - SimpleMutex packetReturnMutex; - DataStructures::Queue packetReturnQueue; - Packet *AllocPacket(unsigned dataSize, const char *file, unsigned int line); - Packet *AllocPacket(unsigned dataSize, unsigned char *data, const char *file, unsigned int line); - - /// This is used to return a number to the user when they call Send identifying the message - /// This number will be returned back with ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS and is only returned - /// with the reliability types that contain RECEIPT in the name - SimpleMutex sendReceiptSerialMutex; - uint32_t sendReceiptSerial; - void ResetSendReceipt(void); - void OnConnectedPong(RakNet::Time sendPingTime, RakNet::Time sendPongTime, RemoteSystemStruct *remoteSystem); - void CallPluginCallbacks(DataStructures::List &pluginList, Packet *packet); - -#if LIBCAT_SECURITY==1 - // Encryption and security - bool _using_security, _require_client_public_key; - char my_public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES]; - cat::ServerEasyHandshake *_server_handshake; - cat::CookieJar *_cookie_jar; - bool InitializeClientSecurity(RequestedConnectionStruct *rcs, const char *public_key); -#endif - - - - - - - virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct); - void FillIPList(void); -} -// #if defined(SN_TARGET_PSP2) -// __attribute__((aligned(8))) -// #endif -; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Declares RakPeer class. +/// + + +// TODO - RakNet 4 - Add network simulator +// TODO - RakNet 4 - Enable disabling flow control per connections + +#pragma once + +#include "ReliabilityLayer.h" +#include "RakPeerInterface.h" +#include "BitStream.h" +#include "SingleProducerConsumer.h" +#include "SimpleMutex.h" +#include "DS_OrderedList.h" +#include "Export.h" +#include "RakString.h" +#include "RakThread.h" +//#include "RakNetSocket.h" +#include "RakNetSmartPtr.h" +#include "DS_ThreadsafeAllocatingQueue.h" +#include "SignaledEvent.h" +#include "NativeFeatureIncludes.h" +#include "SecureHandshake.h" +#include "LocklessTypes.h" +#include "DS_Queue.h" + +namespace RakNet { +/// Forward declarations +class HuffmanEncodingTree; +class PluginInterface2; + +// Sucks but this struct has to be outside the class. Inside and DevCPP won't let you refer to the struct as RakPeer::RemoteSystemIndex while GCC +// forces you to do RakPeer::RemoteSystemIndex +struct RemoteSystemIndex{unsigned index; RemoteSystemIndex *next;}; +//int RAK_DLL_EXPORT SystemAddressAndIndexComp( const SystemAddress &key, const RemoteSystemIndex &data ); // GCC requires RakPeer::RemoteSystemIndex or it won't compile + +///\brief Main interface for network communications. +/// \details It implements most of RakNet's functionality and is the primary interface for RakNet. +/// +/// Inherits RakPeerInterface. +/// +/// See the individual functions for what the class can do. +/// +class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler +{ +public: + ///Constructor + RakPeer(); + + ///Destructor + virtual ~RakPeer(); + + // --------------------------------------------------------------------------------------------Major Low Level Functions - Functions needed by most users-------------------------------------------------------------------------------------------- + /// \brief Starts the network threads and opens the listen port. + /// \details You must call this before calling Connect(). + /// \pre On the PS3, call Startup() after Client_Login() + /// \note Multiple calls while already active are ignored. To call this function again with different settings, you must first call Shutdown(). + /// \note Call SetMaximumIncomingConnections if you want to accept incoming connections. + /// \param[in] maxConnections Maximum number of connections between this instance of RakPeer and another instance of RakPeer. Required so that the network can preallocate and for thread safety. A pure client would set this to 1. A pure server would set it to the number of allowed clients.A hybrid would set it to the sum of both types of connections. + /// \param[in] localPort The port to listen for connections on. On linux the system may be set up so thast ports under 1024 are restricted for everything but the root user. Use a higher port for maximum compatibility. + /// \param[in] socketDescriptors An array of SocketDescriptor structures to force RakNet to listen on a particular IP address or port (or both). Each SocketDescriptor will represent one unique socket. Do not pass redundant structures. To listen on a specific port, you can pass SocketDescriptor(myPort,0); such as for a server. For a client, it is usually OK to just pass SocketDescriptor(); However, on the XBOX be sure to use IPPROTO_VDP + /// \param[in] socketDescriptorCount The size of the \a socketDescriptors array. Pass 1 if you are not sure what to pass. + /// \param[in] threadPriority Passed to the thread creation routine. Use THREAD_PRIORITY_NORMAL for Windows. For Linux based systems, you MUST pass something reasonable based on the thread priorities for your application. + /// \return RAKNET_STARTED on success, otherwise appropriate failure enumeration. + StartupResult Startup( unsigned int maxConnections, SocketDescriptor *socketDescriptors, unsigned socketDescriptorCount, int threadPriority=-99999 ); + + /// If you accept connections, you must call this or else security will not be enabled for incoming connections. + /// This feature requires more round trips, bandwidth, and CPU time for the connection handshake + /// x64 builds require under 25% of the CPU time of other builds + /// See the Encryption sample for example usage + /// \pre Must be called while offline + /// \pre LIBCAT_SECURITY must be defined to 1 in NativeFeatureIncludes.h for this function to have any effect + /// \param[in] publicKey A pointer to the public key for accepting new connections + /// \param[in] privateKey A pointer to the private key for accepting new connections + /// \param[in] bRequireClientKey: Should be set to false for most servers. Allows the server to accept a public key from connecting clients as a proof of identity but eats twice as much CPU time as a normal connection + bool InitializeSecurity( const char *publicKey, const char *privateKey, bool bRequireClientKey = false ); + + /// Disables security for incoming connections. + /// \note Must be called while offline + void DisableSecurity( void ); + + /// \brief This is useful if you have a fixed-address internal server behind a LAN. + /// + /// Secure connections are determined by the recipient of an incoming connection. This has no effect if called on the system attempting to connect. + /// \note If secure connections are on, do not use secure connections for a specific IP address. + /// \param[in] ip IP address to add. * wildcards are supported. + void AddToSecurityExceptionList(const char *ip); + + /// \brief Remove a specific connection previously added via AddToSecurityExceptionList. + /// \param[in] ip IP address to remove. Pass 0 to remove all IP addresses. * wildcards are supported. + void RemoveFromSecurityExceptionList(const char *ip); + + /// \brief Checks to see if a given IP is in the security exception list. + /// \param[in] IP address to check. + /// \return True if the IP address is found in security exception list, else returns false. + bool IsInSecurityExceptionList(const char *ip); + + /// \brief Sets the maximum number of incoming connections allowed. + /// \details If the number of incoming connections is less than the number of players currently connected, + /// no more players will be allowed to connect. If this is greater than the maximum number of peers allowed, + /// it will be reduced to the maximum number of peers allowed. + /// + /// Defaults to 0, meaning by default, nobody can connect to you + /// \param[in] numberAllowed Maximum number of incoming connections allowed. + void SetMaximumIncomingConnections( unsigned short numberAllowed ); + + /// \brief Returns the value passed to SetMaximumIncomingConnections(). + /// \return Maximum number of incoming connections, which is always <= maxConnections + unsigned int GetMaximumIncomingConnections( void ) const; + + /// \brief Returns how many open connections exist at this time. + /// \return Number of open connections. + unsigned short NumberOfConnections(void) const; + + /// \brief Sets the password for the incoming connections. + /// \details The password must match in the call to Connect (defaults to none). + /// Pass 0 to passwordData to specify no password. + /// This is a way to set a low level password for all incoming connections. To selectively reject connections, implement your own scheme using CloseConnection() to remove unwanted connections. + /// \param[in] passwordData A data block that incoming connections must match. This can be just a password, or can be a stream of data. Specify 0 for no password data + /// \param[in] passwordDataLength The length in bytes of passwordData + void SetIncomingPassword( const char* passwordData, int passwordDataLength ); + + /// \brief Gets the password passed to SetIncomingPassword + /// \param[out] passwordData Should point to a block large enough to hold the password data you passed to SetIncomingPassword() + /// \param[in,out] passwordDataLength Maximum size of the passwordData array. Modified to hold the number of bytes actually written. + void GetIncomingPassword( char* passwordData, int *passwordDataLength ); + + /// \brief Connect to the specified host (ip or domain name) and server port. + /// \details Calling Connect and not calling SetMaximumIncomingConnections acts as a dedicated client. + /// Calling both acts as a true peer. + /// + /// This is a non-blocking connection. + /// + /// The connection is successful when GetConnectionState() returns IS_CONNECTED or Receive() gets a message with the type identifier ID_CONNECTION_REQUEST_ACCEPTED. + /// If the connection is not successful, such as a rejected connection or no response then neither of these things will happen. + /// \pre Requires that you first call Startup(). + /// \param[in] host Either a dotted IP address or a domain name. + /// \param[in] remotePort Port to connect to on the remote machine. + /// \param[in] passwordData A data block that must match the data block on the server passed to SetIncomingPassword(). This can be a string or can be a stream of data. Use 0 for no password. + /// \param[in] passwordDataLength The length in bytes of passwordData. + /// \param[in] publicKey The public key the server is using. If 0, the server is not using security. If non-zero, the publicKeyMode member determines how to connect + /// \param[in] connectionSocketIndex Index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to determine the one to send on. + /// \param[in] sendConnectionAttemptCount Number of datagrams to send to the other system to try to connect. + /// \param[in] timeBetweenSendConnectionAttemptsMS Time to elapse before a datagram is sent to the other system to try to connect. After sendConnectionAttemptCount number of attempts, ID_CONNECTION_ATTEMPT_FAILED is returned. Under low bandwidth conditions with multiple simultaneous outgoing connections, this value should be raised to 1000 or higher, or else the MTU detection can overrun the available bandwidth. + /// \param[in] timeoutTime Time to elapse before dropping the connection if a reliable message could not be sent. 0 to use the default value from SetTimeoutTime(UNASSIGNED_SYSTEM_ADDRESS); + /// \return CONNECTION_ATTEMPT_STARTED on successful initiation. Otherwise, an appropriate enumeration indicating failure. + /// \note CONNECTION_ATTEMPT_STARTED does not mean you are already connected! + /// \note It is possible to immediately get back ID_CONNECTION_ATTEMPT_FAILED if you exceed the maxConnections parameter passed to Startup(). This could happen if you call CloseConnection() with sendDisconnectionNotificaiton true, then immediately call Connect() before the connection has closed. + ConnectionAttemptResult Connect( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey=0, unsigned connectionSocketIndex=0, unsigned sendConnectionAttemptCount=6, unsigned timeBetweenSendConnectionAttemptsMS=1000, RakNet::TimeMS timeoutTime=0 ); + + /// \brief Connect to the specified host (ip or domain name) and server port. + /// \param[in] host Either a dotted IP address or a domain name. + /// \param[in] remotePort Which port to connect to on the remote machine. + /// \param[in] passwordData A data block that must match the data block on the server passed to SetIncomingPassword(). This can be a string or can be a stream of data. Use 0 for no password. + /// \param[in] passwordDataLength The length in bytes of passwordData. + /// \param[in] socket A bound socket returned by another instance of RakPeerInterface. + /// \param[in] sendConnectionAttemptCount Number of datagrams to send to the other system to try to connect. + /// \param[in] timeBetweenSendConnectionAttemptsMS Time to elapse before a datagram is sent to the other system to try to connect. After sendConnectionAttemptCount number of attempts, ID_CONNECTION_ATTEMPT_FAILED is returned.. Under low bandwidth conditions with multiple simultaneous outgoing connections, this value should be raised to 1000 or higher, or else the MTU detection can overrun the available bandwidth. + /// \param[in] timeoutTime Time to elapse before dropping the connection if a reliable message could not be sent. 0 to use the default from SetTimeoutTime(UNASSIGNED_SYSTEM_ADDRESS); + /// \return CONNECTION_ATTEMPT_STARTED on successful initiation. Otherwise, an appropriate enumeration indicating failure. + /// \note CONNECTION_ATTEMPT_STARTED does not mean you are already connected! + virtual ConnectionAttemptResult ConnectWithSocket(const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, RakNetSocket2* socket, PublicKey *publicKey=0, unsigned sendConnectionAttemptCount=6, unsigned timeBetweenSendConnectionAttemptsMS=1000, RakNet::TimeMS timeoutTime=0); + + /* /// \brief Connect to the specified network ID (Platform specific console function) + /// \details Does built-in NAT traversal + /// \param[in] networkServiceId Network ID structure for the online service + /// \param[in] passwordData A data block that must match the data block on the server passed to SetIncomingPassword(). This can be a string or can be a stream of data. Use 0 for no password. + /// \param[in] passwordDataLength The length in bytes of passwordData. + //bool Console2LobbyConnect( void *networkServiceId, const char *passwordData, int passwordDataLength );*/ + + /// \brief Stops the network threads and closes all connections. + /// \param[in] blockDuration Wait time(milli seconds) for all remaining messages to go out, including ID_DISCONNECTION_NOTIFICATION. If 0, it doesn't wait at all. + /// \param[in] orderingChannel Channel on which ID_DISCONNECTION_NOTIFICATION will be sent, if blockDuration > 0. + /// \param[in] disconnectionNotificationPriority Priority of sending ID_DISCONNECTION_NOTIFICATION. + /// If set to 0, the disconnection notification won't be sent. + void Shutdown( unsigned int blockDuration, unsigned char orderingChannel=0, PacketPriority disconnectionNotificationPriority=LOW_PRIORITY ); + + /// \brief Returns true if the network thread is running. + /// \return True if the network thread is running, False otherwise + bool IsActive( void ) const; + + /// \brief Fills the array remoteSystems with the SystemAddress of all the systems we are connected to. + /// \param[out] remoteSystems An array of SystemAddress structures, to be filled with the SystemAddresss of the systems we are connected to. Pass 0 to remoteSystems to get the number of systems we are connected to. + /// \param[in, out] numberOfSystems As input, the size of remoteSystems array. As output, the number of elements put into the array. + bool GetConnectionList( SystemAddress *remoteSystems, unsigned short *numberOfSystems ) const; + + /// Returns the next uint32_t that Send() will return + /// \note If using RakPeer from multiple threads, this may not be accurate for your thread. Use IncrementNextSendReceipt() in that case. + /// \return The next uint32_t that Send() or SendList will return + virtual uint32_t GetNextSendReceipt(void); + + /// Returns the next uint32_t that Send() will return, and increments the value by one + /// \note If using RakPeer from multiple threads, pass this to forceReceipt in the send function + /// \return The next uint32_t that Send() or SendList will return + virtual uint32_t IncrementNextSendReceipt(void); + + /// \brief Sends a block of data to the specified system that you are connected to. + /// \note This function only works while connected. + /// \note The first byte should be a message identifier starting at ID_USER_PACKET_ENUM. + /// \param[in] data Block of data to send. + /// \param[in] length Size in bytes of the data to send. + /// \param[in] priority Priority level to send on. See PacketPriority.h + /// \param[in] reliability How reliably to send this data. See PacketPriority.h + /// \param[in] orderingChannel When using ordered or sequenced messages, the channel to order these on. Messages are only ordered relative to other messages on the same stream. + /// \param[in] systemIdentifier Who to send this packet to, or in the case of broadcasting who not to send it to. Pass either a SystemAddress structure or a RakNetGUID structure. Use UNASSIGNED_SYSTEM_ADDRESS or to specify none + /// \param[in] broadcast True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. + /// \param[in] forceReceipt If 0, will automatically determine the receipt number to return. If non-zero, will return what you give it. + /// \return 0 on bad input. Otherwise a number that identifies this message. If \a reliability is a type that returns a receipt, on a later call to Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS with bytes 1-4 inclusive containing this number + uint32_t Send( const char *data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 ); + + /// \brief "Send" to yourself rather than a remote system. + /// \details The message will be processed through the plugins and returned to the game as usual. + /// This function works anytime + /// \note The first byte should be a message identifier starting at ID_USER_PACKET_ENUM + /// \param[in] data Block of data to send. + /// \param[in] length Size in bytes of the data to send. + void SendLoopback( const char *data, const int length ); + + /// \brief Sends a block of data to the specified system that you are connected to. + /// + /// Same as the above version, but takes a BitStream as input. + /// \param[in] bitStream Bitstream to send + /// \param[in] priority Priority level to send on. See PacketPriority.h + /// \param[in] reliability How reliably to send this data. See PacketPriority.h + /// \param[in] orderingChannel Channel to order the messages on, when using ordered or sequenced messages. Messages are only ordered relative to other messages on the same stream. + /// \param[in] systemIdentifier System Address or RakNetGUID to send this packet to, or in the case of broadcasting, the address not to send it to. Use UNASSIGNED_SYSTEM_ADDRESS to specify none. + /// \param[in] broadcast True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. + /// \param[in] forceReceipt If 0, will automatically determine the receipt number to return. If non-zero, will return what you give it. + /// \return 0 on bad input. Otherwise a number that identifies this message. If \a reliability is a type that returns a receipt, on a later call to Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS with bytes 1-4 inclusive containing this number + /// \note COMMON MISTAKE: When writing the first byte, bitStream->Write((unsigned char) ID_MY_TYPE) be sure it is casted to a byte, and you are not writing a 4 byte enumeration. + uint32_t Send( const RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 ); + + /// \brief Sends multiple blocks of data, concatenating them automatically. + /// + /// This is equivalent to: + /// RakNet::BitStream bs; + /// bs.WriteAlignedBytes(block1, blockLength1); + /// bs.WriteAlignedBytes(block2, blockLength2); + /// bs.WriteAlignedBytes(block3, blockLength3); + /// Send(&bs, ...) + /// + /// This function only works when connected. + /// \param[in] data An array of pointers to blocks of data + /// \param[in] lengths An array of integers indicating the length of each block of data + /// \param[in] numParameters Length of the arrays data and lengths + /// \param[in] priority Priority level to send on. See PacketPriority.h + /// \param[in] reliability How reliably to send this data. See PacketPriority.h + /// \param[in] orderingChannel Channel to order the messages on, when using ordered or sequenced messages. Messages are only ordered relative to other messages on the same stream. + /// \param[in] systemIdentifier System Address or RakNetGUID to send this packet to, or in the case of broadcasting, the address not to send it to. Use UNASSIGNED_SYSTEM_ADDRESS to specify none. + /// \param[in] broadcast True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. + /// \param[in] forceReceipt If 0, will automatically determine the receipt number to return. If non-zero, will return what you give it. + /// \return 0 on bad input. Otherwise a number that identifies this message. If \a reliability is a type that returns a receipt, on a later call to Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS with bytes 1-4 inclusive containing this number + uint32_t SendList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 ); + + /// \brief Gets a message from the incoming message queue. + /// \details Use DeallocatePacket() to deallocate the message after you are done with it. + /// User-thread functions, such as RPC calls and the plugin function PluginInterface::Update occur here. + /// \return 0 if no packets are waiting to be handled, otherwise a pointer to a packet. + /// \note COMMON MISTAKE: Be sure to call this in a loop, once per game tick, until it returns 0. If you only process one packet per game tick they will buffer up. + /// \sa RakNetTypes.h contains struct Packet. + Packet* Receive( void ); + + /// \brief Call this to deallocate a message returned by Receive() when you are done handling it. + /// \param[in] packet Message to deallocate. + void DeallocatePacket( Packet *packet ); + + /// \brief Return the total number of connections we are allowed. + /// \return Total number of connections allowed. + unsigned int GetMaximumNumberOfPeers( void ) const; + + // -------------------------------------------------------------------------------------------- Connection Management Functions-------------------------------------------------------------------------------------------- + /// \brief Close the connection to another host (if we initiated the connection it will disconnect, if they did it will kick them out). + /// \details This method closes the connection irrespective of who initiated the connection. + /// \param[in] target Which system to close the connection to. + /// \param[in] sendDisconnectionNotification True to send ID_DISCONNECTION_NOTIFICATION to the recipient. False to close it silently. + /// \param[in] channel Which ordering channel to send the disconnection notification on, if any + /// \param[in] disconnectionNotificationPriority Priority to send ID_DISCONNECTION_NOTIFICATION on. + void CloseConnection( const AddressOrGUID target, bool sendDisconnectionNotification, unsigned char orderingChannel=0, PacketPriority disconnectionNotificationPriority=LOW_PRIORITY ); + + /// \brief Cancel a pending connection attempt. + /// \details If we are already connected, the connection stays open + /// \param[in] target Target system to cancel. + void CancelConnectionAttempt( const SystemAddress target ); + /// Returns if a system is connected, disconnected, connecting in progress, or various other states + /// \param[in] systemIdentifier The system we are referring to + /// \note This locks a mutex, do not call too frequently during connection attempts or the attempt will take longer and possibly even timeout + /// \return What state the remote system is in + ConnectionState GetConnectionState(const AddressOrGUID systemIdentifier); + + /// \brief Given \a systemAddress, returns its index into remoteSystemList. + /// \details Values range from 0 to the maximum number of players allowed - 1. + /// This includes systems which were formerly connected, but are now not connected. + /// \param[in] systemAddress The SystemAddress we are referring to + /// \return The index of this SystemAddress or -1 on system not found. + int GetIndexFromSystemAddress( const SystemAddress systemAddress ) const; + + /// \brief Given \a index into remoteSystemList, will return a SystemAddress. + /// This function is only useful for looping through all systems. + /// + /// \param[in] index Index should range between 0 and the maximum number of players allowed - 1. + /// \return The SystemAddress structure corresponding to \a index in remoteSystemList. + SystemAddress GetSystemAddressFromIndex( unsigned int index ); + + /// \brief Same as GetSystemAddressFromIndex but returns RakNetGUID + /// \param[in] index Index should range between 0 and the maximum number of players allowed - 1. + /// \return The RakNetGUID + RakNetGUID GetGUIDFromIndex( unsigned int index ); + + /// \brief Same as calling GetSystemAddressFromIndex and GetGUIDFromIndex for all systems, but more efficient + /// Indices match each other, so \a addresses[0] and \a guids[0] refer to the same system + /// \param[out] addresses All system addresses. Size of the list is the number of connections. Size of the \a addresses list will match the size of the \a guids list. + /// \param[out] guids All guids. Size of the list is the number of connections. Size of the list will match the size of the \a addresses list. + void GetSystemList(DataStructures::List &addresses, DataStructures::List &guids) const; + + /// \brief Bans an IP from connecting. + /// \details Banned IPs persist between connections but are not saved on shutdown nor loaded on startup. + /// \param[in] IP Dotted IP address. You can use * for a wildcard address, such as 128.0.0. * will ban all IP addresses starting with 128.0.0. + /// \param[in] milliseconds Gives time in milli seconds for a temporary ban of the IP address. Use 0 for a permanent ban. + void AddToBanList( const char *IP, RakNet::TimeMS milliseconds=0 ); + + /// \brief Allows a previously banned IP to connect. + /// param[in] Dotted IP address. You can use * as a wildcard. An IP such as 128.0.0.* will ban all IP addresses starting with 128.0.0. + void RemoveFromBanList( const char *IP ); + + /// \brief Allows all previously banned IPs to connect. + void ClearBanList( void ); + + /// \brief Returns true or false indicating if a particular IP is banned. + /// \param[in] IP Dotted IP address. + /// \return True if IP matches any IPs in the ban list, accounting for any wildcards. False otherwise. + bool IsBanned( const char *IP ); + + /// \brief Enable or disable allowing frequent connections from the same IP adderss + /// \details This is a security measure which is disabled by default, but can be set to true to prevent attackers from using up all connection slots. + /// \param[in] b True to limit connections from the same ip to at most 1 per 100 milliseconds. + void SetLimitIPConnectionFrequency(bool b); + + // --------------------------------------------------------------------------------------------Pinging Functions - Functions dealing with the automatic ping mechanism-------------------------------------------------------------------------------------------- + /// Send a ping to the specified connected system. + /// \pre The sender and recipient must already be started via a successful call to Startup() + /// \param[in] target Which system to ping + void Ping( const SystemAddress target ); + + /// \brief Send a ping to the specified unconnected system. + /// \details The remote system, if it is Initialized, will respond with ID_PONG followed by sizeof(RakNet::TimeMS) containing the system time the ping was sent. Default is 4 bytes - See __GET_TIME_64BIT in RakNetTypes.h + /// System should reply with ID_PONG if it is active + /// \param[in] host Either a dotted IP address or a domain name. Can be 255.255.255.255 for LAN broadcast. + /// \param[in] remotePort Which port to connect to on the remote machine. + /// \param[in] onlyReplyOnAcceptingConnections Only request a reply if the remote system is accepting connections + /// \param[in] connectionSocketIndex Index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to send on. + /// \return true on success, false on failure (unknown hostname) + bool Ping( const char* host, unsigned short remotePort, bool onlyReplyOnAcceptingConnections, unsigned connectionSocketIndex=0 ); + + /// \brief Returns the average of all ping times read for the specific system or -1 if none read yet + /// \param[in] systemAddress Which system we are referring to + /// \return The ping time for this system, or -1 + int GetAveragePing( const AddressOrGUID systemIdentifier ); + + /// \brief Returns the last ping time read for the specific system or -1 if none read yet. + /// \param[in] systemAddress Which system we are referring to + /// \return The last ping time for this system, or -1. + int GetLastPing( const AddressOrGUID systemIdentifier ) const; + + /// \brief Returns the lowest ping time read or -1 if none read yet. + /// \param[in] systemIdentifier Which system we are referring to + /// \return The lowest ping time for this system, or -1. + int GetLowestPing( const AddressOrGUID systemIdentifier ) const; + + /// Ping the remote systems every so often, or not. Can be called anytime. + /// By default this is true. Recommended to leave on, because congestion control uses it to determine how often to resend lost packets. + /// It would be true by default to prevent timestamp drift, since in the event of a clock spike, the timestamp deltas would no longer be accurate + /// \param[in] doPing True to start occasional pings. False to stop them. + void SetOccasionalPing( bool doPing ); + + /// Return the clock difference between your system and the specified system + /// Subtract GetClockDifferential() from a time returned by the remote system to get that time relative to your own system + /// Returns 0 if the system is unknown + /// \param[in] systemIdentifier Which system we are referring to + RakNet::Time GetClockDifferential( const AddressOrGUID systemIdentifier ); + + // --------------------------------------------------------------------------------------------Static Data Functions - Functions dealing with API defined synchronized memory-------------------------------------------------------------------------------------------- + /// \brief Sets the data to send along with a LAN server discovery or offline ping reply. + /// \param[in] data Block of data to send, or 0 for none + /// \param[in] length Length of the data in bytes, or 0 for none + /// \note \a length should be under 400 bytes, as a security measure against flood attacks + /// \sa Ping.cpp + void SetOfflinePingResponse( const char *data, const unsigned int length ); + + /// \brief Returns pointers to a copy of the \a data passed to SetOfflinePingResponse. + /// \param[out] data A pointer to a copy of the data passed to SetOfflinePingResponse() + /// \param[out] length A pointer filled in with the length parameter passed to SetOfflinePingResponse() + /// \sa SetOfflinePingResponse + void GetOfflinePingResponse( char **data, unsigned int *length ); + + //--------------------------------------------------------------------------------------------Network Functions - Functions dealing with the network in general-------------------------------------------------------------------------------------------- + /// \brief Returns the unique address identifier that represents you or another system on the the network + /// \note Not supported by the XBOX + /// \param[in] systemAddress Use UNASSIGNED_SYSTEM_ADDRESS to get your behind-LAN address. Use a connected system to get their behind-LAN address. This does not return the port. + /// \param[in] index When you have multiple internal IDs, which index to return? Currently limited to MAXIMUM_NUMBER_OF_INTERNAL_IDS (so the maximum value of this variable is MAXIMUM_NUMBER_OF_INTERNAL_IDS-1) + /// \return Identifier of your system internally, which may not be how other systems see if you if you are behind a NAT or proxy. + SystemAddress GetInternalID( const SystemAddress systemAddress=UNASSIGNED_SYSTEM_ADDRESS, const int index=0 ) const; + + /// \brief Sets your internal IP address, for platforms that do not support reading it, or to override a value + /// \param[in] systemAddress. The address to set. Use SystemAddress::FromString() if you want to use a dotted string + /// \param[in] index When you have multiple internal IDs, which index to set? + void SetInternalID(SystemAddress systemAddress, int index=0); + + /// \brief Returns the unique address identifier that represents the target on the the network and is based on the target's external IP / port. + /// \param[in] target The SystemAddress of the remote system. Usually the same for all systems, unless you have two or more network cards. + SystemAddress GetExternalID( const SystemAddress target ) const; + + /// Return my own GUID + const RakNetGUID GetMyGUID(void) const; + + /// Return the address bound to a socket at the specified index + SystemAddress GetMyBoundAddress(const int socketIndex=0); + + /// \brief Given a connected system address, this method gives the unique GUID representing that instance of RakPeer. + /// This will be the same on all systems connected to that instance of RakPeer, even if the external system addresses are different. + /// Complexity is O(log2(n)). + /// If \a input is UNASSIGNED_SYSTEM_ADDRESS, will return your own GUID + /// \pre Call Startup() first, or the function will return UNASSIGNED_RAKNET_GUID + /// \param[in] input The system address of the target system we are connected to. + const RakNetGUID& GetGuidFromSystemAddress( const SystemAddress input ) const; + + /// \brief Gives the system address of a connected system, given its GUID. + /// The GUID will be the same on all systems connected to that instance of RakPeer, even if the external system addresses are different. + /// Currently O(log(n)), but this may be improved in the future + /// If \a input is UNASSIGNED_RAKNET_GUID, UNASSIGNED_SYSTEM_ADDRESS is returned. + /// \param[in] input The RakNetGUID of the target system. + SystemAddress GetSystemAddressFromGuid( const RakNetGUID input ) const; + + /// Given the SystemAddress of a connected system, get the public key they provided as an identity + /// Returns false if system address was not found or client public key is not known + /// \param[in] input The RakNetGUID of the system + /// \param[in] client_public_key The connected client's public key is copied to this address. Buffer must be cat::EasyHandshake::PUBLIC_KEY_BYTES bytes in length. + bool GetClientPublicKeyFromSystemAddress( const SystemAddress input, char *client_public_key ) const; + + /// \brief Set the time, in MS, to use before considering ourselves disconnected after not being able to deliver a reliable message. + + /// Set the time, in MS, to use before considering ourselves disconnected after not being able to deliver a reliable message. + /// Default time is 10,000 or 10 seconds in release and 30,000 or 30 seconds in debug. + /// Do not set different values for different computers that are connected to each other, or you won't be able to reconnect after ID_CONNECTION_LOST + /// \param[in] timeMS Time, in MS + /// \param[in] target SystemAddress structure of the target system. Pass UNASSIGNED_SYSTEM_ADDRESS for all systems. + void SetTimeoutTime( RakNet::TimeMS timeMS, const SystemAddress target ); + + /// \brief Returns the Timeout time for the given system. + /// \param[in] target Target system to get the TimeoutTime for. Pass UNASSIGNED_SYSTEM_ADDRESS to get the default value. + /// \return Timeout time for a given system. + RakNet::TimeMS GetTimeoutTime( const SystemAddress target ); + + /// \brief Returns the current MTU size + /// \param[in] target Which system to get MTU for. UNASSIGNED_SYSTEM_ADDRESS to get the default + /// \return The current MTU size of the target system. + int GetMTUSize( const SystemAddress target ) const; + + /// \brief Returns the number of IP addresses this system has internally. + /// \details Get the actual addresses from GetLocalIP() + unsigned GetNumberOfAddresses( void ); + + /// Returns an IP address at index 0 to GetNumberOfAddresses-1 in ipList array. + /// \param[in] index index into the list of IP addresses + /// \return The local IP address at this index + const char* GetLocalIP( unsigned int index ); + + /// Is this a local IP? + /// Checks if this ip is in the ipList array. + /// \param[in] An IP address to check, excluding the port. + /// \return True if this is one of the IP addresses returned by GetLocalIP + bool IsLocalIP( const char *ip ); + + /// \brief Allow or disallow connection responses from any IP. + /// \details Normally this should be false, but may be necessary when connecting to servers with multiple IP addresses. + /// \param[in] allow - True to allow this behavior, false to not allow. Defaults to false. Value persists between connections. + void AllowConnectionResponseIPMigration( bool allow ); + + /// \brief Sends a one byte message ID_ADVERTISE_SYSTEM to the remote unconnected system. + /// This will send our external IP outside the LAN along with some user data to the remote system. + /// \pre The sender and recipient must already be started via a successful call to Initialize + /// \param[in] host Either a dotted IP address or a domain name + /// \param[in] remotePort Which port to connect to on the remote machine. + /// \param[in] data Optional data to append to the packet. + /// \param[in] dataLength Length of data in bytes. Use 0 if no data. + /// \param[in] connectionSocketIndex Index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to send on. + /// \return False if IsActive()==false or the host is unresolvable. True otherwise. + bool AdvertiseSystem( const char *host, unsigned short remotePort, const char *data, int dataLength, unsigned connectionSocketIndex=0 ); + + /// \brief Controls how often to return ID_DOWNLOAD_PROGRESS for large message downloads. + /// \details ID_DOWNLOAD_PROGRESS is returned to indicate a new partial message chunk, roughly the MTU size, has arrived. + /// As it can be slow or cumbersome to get this notification for every chunk, you can set the interval at which it is returned. + /// Defaults to 0 (never return this notification). + /// \param[in] interval How many messages to use as an interval before a download progress notification is returned. + void SetSplitMessageProgressInterval(int interval); + + /// \brief Returns what was passed to SetSplitMessageProgressInterval(). + /// \return Number of messages to be recieved before a download progress notification is returned. Default to 0. + int GetSplitMessageProgressInterval(void) const; + + /// \brief Set how long to wait before giving up on sending an unreliable message. + /// Useful if the network is clogged up. + /// Set to 0 or less to never timeout. Defaults to 0. + /// \param[in] timeoutMS How many ms to wait before simply not sending an unreliable message. + void SetUnreliableTimeout(RakNet::TimeMS timeoutMS); + + /// \brief Send a message to a host, with the IP socket option TTL set to 3. + /// \details This message will not reach the host, but will open the router. + /// \param[in] host The address of the remote host in dotted notation. + /// \param[in] remotePort The port number to send to. + /// \param[in] ttl Max hops of datagram, set to 3 + /// \param[in] connectionSocketIndex userConnectionSocketIndex. + /// \remarks Used for NAT-Punchthrough + void SendTTL( const char* host, unsigned short remotePort, int ttl, unsigned connectionSocketIndex=0 ); + + // -------------------------------------------------------------------------------------------- Plugin Functions-------------------------------------------------------------------------------------------- + /// \brief Attaches a Plugin interface to an instance of the base class (RakPeer or PacketizedTCP) to run code automatically on message receipt in the Receive call. + /// If the plugin returns false from PluginInterface::UsesReliabilityLayer(), which is the case for all plugins except PacketLogger, you can call AttachPlugin() and DetachPlugin() for this plugin while RakPeer is active. + /// \param[in] messageHandler Pointer to the plugin to attach. + void AttachPlugin( PluginInterface2 *plugin ); + + /// \brief Detaches a Plugin interface from the instance of the base class (RakPeer or PacketizedTCP) it is attached to. + /// \details This method disables the plugin code from running automatically on base class's updates or message receipt. + /// If the plugin returns false from PluginInterface::UsesReliabilityLayer(), which is the case for all plugins except PacketLogger, you can call AttachPlugin() and DetachPlugin() for this plugin while RakPeer is active. + /// \param[in] messageHandler Pointer to a plugin to detach. + void DetachPlugin( PluginInterface2 *messageHandler ); + + // --------------------------------------------------------------------------------------------Miscellaneous Functions-------------------------------------------------------------------------------------------- + /// \brief Puts a message back in the receive queue in case you don't want to deal with it immediately. + /// \param[in] packet The pointer to the packet you want to push back. + /// \param[in] pushAtHead True to push the packet at the start of the queue so that the next receive call returns it. False to push it at the end of the queue. + /// \note Setting pushAtHead to false end makes the packets out of order. + void PushBackPacket( Packet *packet, bool pushAtHead ); + + /// \internal + /// \brief For a given system identified by \a guid, change the SystemAddress to send to. + /// \param[in] guid The connection we are referring to + /// \param[in] systemAddress The new address to send to + void ChangeSystemAddress(RakNetGUID guid, const SystemAddress &systemAddress); + + /// \brief Returns a packet for you to write to if you want to create a Packet for some reason. + /// You can add it to the receive buffer with PushBackPacket + /// \param[in] dataSize How many bytes to allocate for the buffer + /// \return A packet. + Packet* AllocatePacket(unsigned dataSize); + + /// \brief Get the socket used with a particular active connection. + /// The smart pointer reference counts the RakNetSocket object, so the socket will remain active as long as the smart pointer does, even if RakNet were to shutdown or close the connection. + /// \note This sends a query to the thread and blocks on the return value for up to one second. In practice it should only take a millisecond or so. + /// \param[in] target Which system. + /// \return A smart pointer object containing the socket information about the target. Be sure to check IsNull() which is returned if the update thread is unresponsive, shutting down, or if this system is not connected. + virtual RakNetSocket2* GetSocket( const SystemAddress target ); + + /// \brief Gets all sockets in use. + /// \note This sends a query to the thread and blocks on the return value for up to one second. In practice it should only take a millisecond or so. + /// \param[out] sockets List of RakNetSocket structures in use. + virtual void GetSockets( DataStructures::List &sockets ); + virtual void ReleaseSockets( DataStructures::List &sockets ); + + /// \internal + virtual void WriteOutOfBandHeader(RakNet::BitStream *bitStream); + + /// If you need code to run in the same thread as RakNet's update thread, this function can be used for that + /// \param[in] _userUpdateThreadPtr C callback function + /// \param[in] _userUpdateThreadData Passed to C callback function + virtual void SetUserUpdateThread(void (*_userUpdateThreadPtr)(RakPeerInterface *, void *), void *_userUpdateThreadData); + + /// Set a C callback to be called whenever a datagram arrives + /// Return true from the callback to have RakPeer handle the datagram. Return false and RakPeer will ignore the datagram. + /// This can be used to filter incoming datagrams by system, or to share a recvfrom socket with RakPeer + /// RNS2RecvStruct will only remain valid for the duration of the call + virtual void SetIncomingDatagramEventHandler( bool (*_incomingDatagramEventHandler)(RNS2RecvStruct *) ); + + // --------------------------------------------------------------------------------------------Network Simulator Functions-------------------------------------------------------------------------------------------- + /// Adds simulated ping and packet loss to the outgoing data flow. + /// To simulate bi-directional ping and packet loss, you should call this on both the sender and the recipient, with half the total ping and packetloss value on each. + /// You can exclude network simulator code with the _RELEASE #define to decrease code size + /// \deprecated Use http://www.jenkinssoftware.com/forum/index.php?topic=1671.0 instead. + /// \note Doesn't work past version 3.6201 + /// \param[in] packetloss Chance to lose a packet. Ranges from 0 to 1. + /// \param[in] minExtraPing The minimum time to delay sends. + /// \param[in] extraPingVariance The additional random time to delay sends. + virtual void ApplyNetworkSimulator( float packetloss, unsigned short minExtraPing, unsigned short extraPingVariance); + + /// Limits how much outgoing bandwidth can be sent per-connection. + /// This limit does not apply to the sum of all connections! + /// Exceeding the limit queues up outgoing traffic + /// \param[in] maxBitsPerSecond Maximum bits per second to send. Use 0 for unlimited (default). Once set, it takes effect immedately and persists until called again. + virtual void SetPerConnectionOutgoingBandwidthLimit( unsigned maxBitsPerSecond ); + + /// Returns if you previously called ApplyNetworkSimulator + /// \return If you previously called ApplyNetworkSimulator + virtual bool IsNetworkSimulatorActive( void ); + + // --------------------------------------------------------------------------------------------Statistical Functions - Functions dealing with API performance-------------------------------------------------------------------------------------------- + + /// \brief Returns a structure containing a large set of network statistics for the specified system. + /// You can map this data to a string using the C style StatisticsToString() function + /// \param[in] systemAddress Which connected system to get statistics for. + /// \param[in] rns If you supply this structure,the network statistics will be written to it. Otherwise the method uses a static struct to write the data, which is not threadsafe. + /// \return 0 if the specified system can't be found. Otherwise a pointer to the struct containing the specified system's network statistics. + /// \sa RakNetStatistics.h + RakNetStatistics * GetStatistics( const SystemAddress systemAddress, RakNetStatistics *rns=0 ); + /// \brief Returns the network statistics of the system at the given index in the remoteSystemList. + /// \return True if the index is less than the maximum number of peers allowed and the system is active. False otherwise. + bool GetStatistics( const unsigned int index, RakNetStatistics *rns ); + /// \brief Returns the list of systems, and statistics for each of those systems + /// Each system has one entry in each of the lists, in the same order + /// \param[out] addresses SystemAddress for each connected system + /// \param[out] guids RakNetGUID for each connected system + /// \param[out] statistics Calculated RakNetStatistics for each connected system + virtual void GetStatisticsList(DataStructures::List &addresses, DataStructures::List &guids, DataStructures::List &statistics); + + /// \Returns how many messages are waiting when you call Receive() + virtual unsigned int GetReceiveBufferSize(void); + + // --------------------------------------------------------------------------------------------EVERYTHING AFTER THIS COMMENT IS FOR INTERNAL USE ONLY-------------------------------------------------------------------------------------------- + + + /// \internal + // Call manually if RAKPEER_USER_THREADED==1 at least every 30 milliseconds. + // updateBitStream should be: + // BitStream updateBitStream( MAXIMUM_MTU_SIZE + // #if LIBCAT_SECURITY==1 + // + cat::AuthenticatedEncryption::OVERHEAD_BYTES + // #endif + // ); + bool RunUpdateCycle( BitStream &updateBitStream ); + + /// \internal + // Call manually if RAKPEER_USER_THREADED==1 at least every 30 milliseconds. + // Call in a loop until returns false if the socket is non-blocking + // remotePortRakNetWasStartedOn_PS3 and extraSocketOptions are from SocketDescriptor when the socket was created + // bool RunRecvFromOnce( RakNetSocket *s ); + + /// \internal + bool SendOutOfBand(const char *host, unsigned short remotePort, const char *data, BitSize_t dataLength, unsigned connectionSocketIndex=0 ); + + // static Packet *AllocPacket(unsigned dataSize, const char *file, unsigned int line); + + /// \internal + /// \brief Holds the clock differences between systems, along with the ping + struct PingAndClockDifferential + { + unsigned short pingTime; + RakNet::Time clockDifferential; + }; + + /// \internal + /// \brief All the information representing a connected system + struct RemoteSystemStruct + { + bool isActive; // Is this structure in use? + SystemAddress systemAddress; /// Their external IP on the internet + SystemAddress myExternalSystemAddress; /// Your external IP on the internet, from their perspective + SystemAddress theirInternalSystemAddress[MAXIMUM_NUMBER_OF_INTERNAL_IDS]; /// Their internal IP, behind the LAN + ReliabilityLayer reliabilityLayer; /// The reliability layer associated with this player + bool weInitiatedTheConnection; /// True if we started this connection via Connect. False if someone else connected to us. + PingAndClockDifferential pingAndClockDifferential[ PING_TIMES_ARRAY_SIZE ]; /// last x ping times and calculated clock differentials with it + RakNet::Time pingAndClockDifferentialWriteIndex; /// The index we are writing into the pingAndClockDifferential circular buffer + unsigned short lowestPing; ///The lowest ping value encountered + RakNet::Time nextPingTime; /// When to next ping this player + RakNet::Time lastReliableSend; /// When did the last reliable send occur. Reliable sends must occur at least once every timeoutTime/2 units to notice disconnects + RakNet::Time connectionTime; /// connection time, if active. +// int connectionSocketIndex; // index into connectionSockets to send back on. + RakNetGUID guid; + int MTUSize; + // Reference counted socket to send back on + RakNetSocket2* rakNetSocket; + SystemIndex remoteSystemIndex; + +#if LIBCAT_SECURITY==1 + // Cached answer used internally by RakPeer to prevent DoS attacks based on the connexion handshake + char answer[cat::EasyHandshake::ANSWER_BYTES]; + + // If the server has bRequireClientKey = true, then this is set to the validated public key of the connected client + // Valid after connectMode reaches HANDLING_CONNECTION_REQUEST + char client_public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES]; +#endif + + enum ConnectMode {NO_ACTION, DISCONNECT_ASAP, DISCONNECT_ASAP_SILENTLY, DISCONNECT_ON_NO_ACK, REQUESTED_CONNECTION, HANDLING_CONNECTION_REQUEST, UNVERIFIED_SENDER, CONNECTED} connectMode; + }; + + // DS_APR + //void ProcessChromePacket(RakNetSocket2 *s, const char *buffer, int dataSize, const SystemAddress& recvFromAddress, RakNet::TimeUS timeRead); + // /DS_APR +protected: + + friend RAK_THREAD_DECLARATION(UpdateNetworkLoop); + //friend RAK_THREAD_DECLARATION(RecvFromLoop); + friend RAK_THREAD_DECLARATION(UDTConnect); + + friend bool ProcessOfflineNetworkPacket( SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSocket2* rakNetSocket, bool *isOfflineMessage, RakNet::TimeUS timeRead ); + friend void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNet::TimeUS timeRead, BitStream &updateBitStream ); + friend void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSocket2* rakNetSocket, RakNet::TimeUS timeRead, BitStream &updateBitStream ); + + int GetIndexFromSystemAddress( const SystemAddress systemAddress, bool calledFromNetworkThread ) const; + int GetIndexFromGuid( const RakNetGUID guid ); + + //void RemoveFromRequestedConnectionsList( const SystemAddress systemAddress ); + // Two versions needed because some buggy compilers strip the last parameter if unused, and crashes + ConnectionAttemptResult SendConnectionRequest( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned int extraData, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNet::TimeMS timeoutTime, RakNetSocket2* socket ); + ConnectionAttemptResult SendConnectionRequest( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned int extraData, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNet::TimeMS timeoutTime ); + ///Get the reliability layer associated with a systemAddress. + /// \param[in] systemAddress The player identifier + /// \return 0 if none + RemoteSystemStruct *GetRemoteSystemFromSystemAddress( const SystemAddress systemAddress, bool calledFromNetworkThread, bool onlyActive ) const; + RakPeer::RemoteSystemStruct *GetRemoteSystem( const AddressOrGUID systemIdentifier, bool calledFromNetworkThread, bool onlyActive ) const; + void ValidateRemoteSystemLookup(void) const; + RemoteSystemStruct *GetRemoteSystemFromGUID( const RakNetGUID guid, bool onlyActive ) const; + ///Parse out a connection request packet + void ParseConnectionRequestPacket( RakPeer::RemoteSystemStruct *remoteSystem, const SystemAddress &systemAddress, const char *data, int byteSize); + void OnConnectionRequest( RakPeer::RemoteSystemStruct *remoteSystem, RakNet::Time incomingTimestamp ); + ///Send a reliable disconnect packet to this player and disconnect them when it is delivered + void NotifyAndFlagForShutdown( const SystemAddress systemAddress, bool performImmediate, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority ); + ///Returns how many remote systems initiated a connection to us + unsigned int GetNumberOfRemoteInitiatedConnections( void ) const; + /// \brief Get a free remote system from the list and assign our systemAddress to it. + /// \note Should only be called from the update thread - not the user thread. + /// \param[in] systemAddress systemAddress to be assigned + /// \param[in] connectionMode connection mode of the RemoteSystem. + /// \param[in] rakNetSocket + /// \param[in] thisIPConnectedRecently Is this IP connected recently? set to False; + /// \param[in] bindingAddress Address to be binded with the remote system + /// \param[in] incomingMTU MTU for the remote system + RemoteSystemStruct * AssignSystemAddressToRemoteSystemList( const SystemAddress systemAddress, RemoteSystemStruct::ConnectMode connectionMode, RakNetSocket2* incomingRakNetSocket, bool *thisIPConnectedRecently, SystemAddress bindingAddress, int incomingMTU, RakNetGUID guid, bool useSecurity ); + /// \brief Adjust the timestamp of the incoming packet to be relative to this system. + /// \param[in] data Data in the incoming packet. + /// \param[in] systemAddress Sender of the incoming packet. + void ShiftIncomingTimestamp( unsigned char *data, const SystemAddress &systemAddress ) const; + /// Get the most accurate clock differential for a certain player. + /// \param[in] systemAddress The player with whose clock the time difference is calculated. + /// \returns The clock differential for a certain player. + RakNet::Time GetBestClockDifferential( const SystemAddress systemAddress ) const; + + bool IsLoopbackAddress(const AddressOrGUID &systemIdentifier, bool matchPort) const; + SystemAddress GetLoopbackAddress(void) const; + + ///Set this to true to terminate the Peer thread execution + volatile bool endThreads; + ///true if the peer thread is active. + volatile bool isMainLoopThreadActive; + + // RakNet::LocklessUint32_t isRecvFromLoopThreadActive; + + + bool occasionalPing; /// Do we occasionally ping the other systems?*/ + ///Store the maximum number of peers allowed to connect + unsigned int maximumNumberOfPeers; + //05/02/06 Just using maximumNumberOfPeers instead + ///Store the maximum number of peers able to connect, including reserved connection slots for pings, etc. + //unsigned short remoteSystemListSize; + ///Store the maximum incoming connection allowed + unsigned int maximumIncomingConnections; + RakNet::BitStream offlinePingResponse; + ///Local Player ID + // SystemAddress mySystemAddress[MAXIMUM_NUMBER_OF_INTERNAL_IDS]; + char incomingPassword[256]; + unsigned char incomingPasswordLength; + + /// This is an array of pointers to RemoteSystemStruct + /// This allows us to preallocate the list when starting, so we don't have to allocate or delete at runtime. + /// Another benefit is that is lets us add and remove active players simply by setting systemAddress + /// and moving elements in the list by copying pointers variables without affecting running threads, even if they are in the reliability layer + RemoteSystemStruct* remoteSystemList; + /// activeSystemList holds a list of pointers and is preallocated to be the same size as remoteSystemList. It is updated only by the network thread, but read by both threads + /// When the isActive member of RemoteSystemStruct is set to true or false, that system is added to this list of pointers + /// Threadsafe because RemoteSystemStruct is preallocated, and the list is only added to, not removed from + RemoteSystemStruct** activeSystemList; + unsigned int activeSystemListSize; + + // Use a hash, with binaryAddress plus port mod length as the index + RemoteSystemIndex **remoteSystemLookup; + unsigned int RemoteSystemLookupHashIndex(const SystemAddress &sa) const; + void ReferenceRemoteSystem(const SystemAddress &sa, unsigned int remoteSystemListIndex); + void DereferenceRemoteSystem(const SystemAddress &sa); + RemoteSystemStruct* GetRemoteSystem(const SystemAddress &sa) const; + unsigned int GetRemoteSystemIndex(const SystemAddress &sa) const; + void ClearRemoteSystemLookup(void); + DataStructures::MemoryPool remoteSystemIndexPool; + + void AddToActiveSystemList(unsigned int remoteSystemListIndex); + void RemoveFromActiveSystemList(const SystemAddress &sa); + +// unsigned int LookupIndexUsingHashIndex(const SystemAddress &sa) const; +// unsigned int RemoteSystemListIndexUsingHashIndex(const SystemAddress &sa) const; +// unsigned int FirstFreeRemoteSystemLookupIndex(const SystemAddress &sa) const; + + enum + { + // Only put these mutexes in user thread functions! + requestedConnectionList_Mutex, + offlinePingResponse_Mutex, + NUMBER_OF_RAKPEER_MUTEXES + }; + SimpleMutex rakPeerMutexes[ NUMBER_OF_RAKPEER_MUTEXES ]; + ///RunUpdateCycle is not thread safe but we don't need to mutex calls. Just skip calls if it is running already + + bool updateCycleIsRunning; + ///The list of people we have tried to connect to recently + + //DataStructures::Queue requestedConnectionsList; + ///Data that both the client and the server needs + + unsigned int bytesSentPerSecond, bytesReceivedPerSecond; + // bool isSocketLayerBlocking; + // bool continualPing,isRecvfromThreadActive,isMainLoopThreadActive, endThreads, isSocketLayerBlocking; + unsigned int validationInteger; + SimpleMutex incomingQueueMutex, banListMutex; //,synchronizedMemoryQueueMutex, automaticVariableSynchronizationMutex; + //DataStructures::Queue incomingpacketSingleProducerConsumer; //, synchronizedMemorypacketSingleProducerConsumer; + // BitStream enumerationData; + + struct BanStruct + { + char *IP; + RakNet::TimeMS timeout; // 0 for none + }; + + struct RequestedConnectionStruct + { + SystemAddress systemAddress; + RakNet::Time nextRequestTime; + unsigned char requestsMade; + char *data; + unsigned short dataLength; + char outgoingPassword[256]; + unsigned char outgoingPasswordLength; + unsigned socketIndex; + unsigned int extraData; + unsigned sendConnectionAttemptCount; + unsigned timeBetweenSendConnectionAttemptsMS; + RakNet::TimeMS timeoutTime; + PublicKeyMode publicKeyMode; + RakNetSocket2* socket; + enum {CONNECT=1, /*PING=2, PING_OPEN_CONNECTIONS=4,*/ /*ADVERTISE_SYSTEM=2*/} actionToTake; + +#if LIBCAT_SECURITY==1 + char handshakeChallenge[cat::EasyHandshake::CHALLENGE_BYTES]; + cat::ClientEasyHandshake *client_handshake; + char remote_public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES]; +// char remote_challenge[cat::EasyHandshake::CHALLENGE_BYTES]; + // char random[16]; +#endif + }; +#if LIBCAT_SECURITY==1 + bool GenerateConnectionRequestChallenge(RequestedConnectionStruct *rcs,PublicKey *publicKey); +#endif + + //DataStructures::List* > automaticVariableSynchronizationList; + DataStructures::List banList; + // Threadsafe, and not thread safe + DataStructures::List pluginListTS, pluginListNTS; + + DataStructures::Queue requestedConnectionQueue; + SimpleMutex requestedConnectionQueueMutex; + + // void RunMutexedUpdateCycle(void); + + struct BufferedCommandStruct + { + BitSize_t numberOfBitsToSend; + PacketPriority priority; + PacketReliability reliability; + char orderingChannel; + AddressOrGUID systemIdentifier; + bool broadcast; + RemoteSystemStruct::ConnectMode connectionMode; + NetworkID networkID; + bool blockingCommand; // Only used for RPC + char *data; + bool haveRakNetCloseSocket; + unsigned connectionSocketIndex; + unsigned short remotePortRakNetWasStartedOn_PS3; + unsigned int extraSocketOptions; + RakNetSocket2* socket; + unsigned short port; + uint32_t receipt; + enum {BCS_SEND, BCS_CLOSE_CONNECTION, BCS_GET_SOCKET, BCS_CHANGE_SYSTEM_ADDRESS,/* BCS_USE_USER_SOCKET, BCS_REBIND_SOCKET_ADDRESS, BCS_RPC, BCS_RPC_SHIFT,*/ BCS_DO_NOTHING} command; + }; + + // Single producer single consumer queue using a linked list + //BufferedCommandStruct* bufferedCommandReadIndex, bufferedCommandWriteIndex; + + DataStructures::ThreadsafeAllocatingQueue bufferedCommands; + + + // DataStructures::ThreadsafeAllocatingQueue bufferedPackets; + + DataStructures::Queue bufferedPacketsFreePool; + RakNet::SimpleMutex bufferedPacketsFreePoolMutex; + DataStructures::Queue bufferedPacketsQueue; + RakNet::SimpleMutex bufferedPacketsQueueMutex; + + virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line); + virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line); + void SetupBufferedPackets(void); + void PushBufferedPacket(RNS2RecvStruct * p); + RNS2RecvStruct *PopBufferedPacket(void); + + struct SocketQueryOutput + { + SocketQueryOutput() {} + ~SocketQueryOutput() {} + DataStructures::List sockets; + }; + + DataStructures::ThreadsafeAllocatingQueue socketQueryOutput; + + + bool AllowIncomingConnections(void) const; + + void PingInternal( const SystemAddress target, bool performImmediate, PacketReliability reliability ); + // This stores the user send calls to be handled by the update thread. This way we don't have thread contention over systemAddresss + void CloseConnectionInternal( const AddressOrGUID& systemIdentifier, bool sendDisconnectionNotification, bool performImmediate, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority ); + void SendBuffered( const char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt ); + void SendBufferedList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt ); + bool SendImmediate( char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, bool useCallerDataAllocation, RakNet::TimeUS currentTime, uint32_t receipt ); + //bool HandleBufferedRPC(BufferedCommandStruct *bcs, RakNet::TimeMS time); + void ClearBufferedCommands(void); + void ClearBufferedPackets(void); + void ClearSocketQueryOutput(void); + void ClearRequestedConnectionList(void); + void AddPacketToProducer(RakNet::Packet *p); + unsigned int GenerateSeedFromGuid(void); + RakNet::Time GetClockDifferentialInt(RemoteSystemStruct *remoteSystem) const; + SimpleMutex securityExceptionMutex; + + //DataStructures::AVLBalancedBinarySearchTree rpcTree; + int defaultMTUSize; + bool trackFrequencyTable; + + // Smart pointer so I can return the object to the user + DataStructures::List socketList; + void DerefAllSockets(void); + unsigned int GetRakNetSocketFromUserConnectionSocketIndex(unsigned int userIndex) const; + // Used for RPC replies + RakNet::BitStream *replyFromTargetBS; + SystemAddress replyFromTargetPlayer; + bool replyFromTargetBroadcast; + + RakNet::TimeMS defaultTimeoutTime; + + // Generate and store a unique GUID + void GenerateGUID(void); + unsigned int GetSystemIndexFromGuid( const RakNetGUID input ) const; + RakNetGUID myGuid; + + unsigned maxOutgoingBPS; + + // Nobody would use the internet simulator in a final build. +#ifdef _DEBUG + double _packetloss; + unsigned short _minExtraPing, _extraPingVariance; +#endif + + ///How long it has been since things were updated by a call to receiveUpdate thread uses this to determine how long to sleep for + //unsigned int lastUserUpdateCycle; + /// True to allow connection accepted packets from anyone. False to only allow these packets from servers we requested a connection to. + bool allowConnectionResponseIPMigration; + + SystemAddress firstExternalID; + int splitMessageProgressInterval; + RakNet::TimeMS unreliableTimeout; + + bool (*incomingDatagramEventHandler)(RNS2RecvStruct *); + + // Systems in this list will not go through the secure connection process, even when secure connections are turned on. Wildcards are accepted. + DataStructures::List securityExceptionList; + + SystemAddress ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ]; + + bool allowInternalRouting; + + void (*userUpdateThreadPtr)(RakPeerInterface *, void *); + void *userUpdateThreadData; + + + SignaledEvent quitAndDataEvents; + bool limitConnectionFrequencyFromTheSameIP; + + SimpleMutex packetAllocationPoolMutex; + DataStructures::MemoryPool packetAllocationPool; + + SimpleMutex packetReturnMutex; + DataStructures::Queue packetReturnQueue; + Packet *AllocPacket(unsigned dataSize, const char *file, unsigned int line); + Packet *AllocPacket(unsigned dataSize, unsigned char *data, const char *file, unsigned int line); + + /// This is used to return a number to the user when they call Send identifying the message + /// This number will be returned back with ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS and is only returned + /// with the reliability types that contain RECEIPT in the name + SimpleMutex sendReceiptSerialMutex; + uint32_t sendReceiptSerial; + void ResetSendReceipt(void); + void OnConnectedPong(RakNet::Time sendPingTime, RakNet::Time sendPongTime, RemoteSystemStruct *remoteSystem); + void CallPluginCallbacks(DataStructures::List &pluginList, Packet *packet); + +#if LIBCAT_SECURITY==1 + // Encryption and security + bool _using_security, _require_client_public_key; + char my_public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES]; + cat::ServerEasyHandshake *_server_handshake; + cat::CookieJar *_cookie_jar; + bool InitializeClientSecurity(RequestedConnectionStruct *rcs, const char *public_key); +#endif + + + + + + + virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct); + void FillIPList(void); +} +// #if defined(SN_TARGET_PSP2) +// __attribute__((aligned(8))) +// #endif +; + +} // namespace RakNet + diff --git a/Source/RakPeerInterface.h b/Source/RakPeerInterface.h index b654fc945..c382168b5 100644 --- a/Source/RakPeerInterface.h +++ b/Source/RakPeerInterface.h @@ -1,616 +1,614 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief An interface for RakPeer. Simply contains all user functions as pure virtuals. -/// - - - -#ifndef __RAK_PEER_INTERFACE_H -#define __RAK_PEER_INTERFACE_H - -#include "PacketPriority.h" -#include "RakNetTypes.h" -#include "RakMemoryOverride.h" -#include "Export.h" -#include "DS_List.h" -#include "RakNetSmartPtr.h" -#include "RakNetSocket2.h" - -namespace RakNet -{ -// Forward declarations -class BitStream; -class PluginInterface2; -struct RPCMap; -struct RakNetStatistics; -struct RakNetBandwidth; -class RouterInterface; -class NetworkIDManager; - -/// The primary interface for RakNet, RakPeer contains all major functions for the library. -/// See the individual functions for what the class can do. -/// \brief The main interface for network communications -class RAK_DLL_EXPORT RakPeerInterface -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(RakPeerInterface) - - ///Destructor - virtual ~RakPeerInterface() {} - - // --------------------------------------------------------------------------------------------Major Low Level Functions - Functions needed by most users-------------------------------------------------------------------------------------------- - /// \brief Starts the network threads, opens the listen port. - /// \details You must call this before calling Connect(). - /// \pre On the PS3, call Startup() after Client_Login() - /// \pre On Android, add the necessary permission to your application's androidmanifest.xml: - /// Multiple calls while already active are ignored. To call this function again with different settings, you must first call Shutdown(). - /// \note Call SetMaximumIncomingConnections if you want to accept incoming connections - /// \param[in] maxConnections The maximum number of connections between this instance of RakPeer and another instance of RakPeer. Required so the network can preallocate and for thread safety. A pure client would set this to 1. A pure server would set it to the number of allowed clients.- A hybrid would set it to the sum of both types of connections - /// \param[in] localPort The port to listen for connections on. On linux the system may be set up so thast ports under 1024 are restricted for everything but the root user. Use a higher port for maximum compatibility. - /// \param[in] socketDescriptors An array of SocketDescriptor structures to force RakNet to listen on a particular IP address or port (or both). Each SocketDescriptor will represent one unique socket. Do not pass redundant structures. To listen on a specific port, you can pass SocketDescriptor(myPort,0); such as for a server. For a client, it is usually OK to just pass SocketDescriptor(); However, on the XBOX be sure to use IPPROTO_VDP - /// \param[in] socketDescriptorCount The size of the \a socketDescriptors array. Pass 1 if you are not sure what to pass. - /// \param[in] threadPriority Passed to the thread creation routine. Use THREAD_PRIORITY_NORMAL for Windows. For Linux based systems, you MUST pass something reasonable based on the thread priorities for your application. - /// \return RAKNET_STARTED on success, otherwise appropriate failure enumeration. - virtual StartupResult Startup( unsigned int maxConnections, SocketDescriptor *socketDescriptors, unsigned socketDescriptorCount, int threadPriority=-99999 )=0; - - /// If you accept connections, you must call this or else security will not be enabled for incoming connections. - /// This feature requires more round trips, bandwidth, and CPU time for the connection handshake - /// x64 builds require under 25% of the CPU time of other builds - /// See the Encryption sample for example usage - /// \pre Must be called while offline - /// \pre LIBCAT_SECURITY must be defined to 1 in NativeFeatureIncludes.h for this function to have any effect - /// \param[in] publicKey A pointer to the public key for accepting new connections - /// \param[in] privateKey A pointer to the private key for accepting new connections - /// \param[in] bRequireClientKey: Should be set to false for most servers. Allows the server to accept a public key from connecting clients as a proof of identity but eats twice as much CPU time as a normal connection - virtual bool InitializeSecurity( const char *publicKey, const char *privateKey, bool bRequireClientKey = false )=0; - - /// Disables security for incoming connections. - /// \note Must be called while offline - virtual void DisableSecurity( void )=0; - - /// If secure connections are on, do not use secure connections for a specific IP address. - /// This is useful if you have a fixed-address internal server behind a LAN. - /// \note Secure connections are determined by the recipient of an incoming connection. This has no effect if called on the system attempting to connect. - /// \param[in] ip IP address to add. * wildcards are supported. - virtual void AddToSecurityExceptionList(const char *ip)=0; - - /// Remove a specific connection previously added via AddToSecurityExceptionList - /// \param[in] ip IP address to remove. Pass 0 to remove all IP addresses. * wildcards are supported. - virtual void RemoveFromSecurityExceptionList(const char *ip)=0; - - /// Checks to see if a given IP is in the security exception list - /// \param[in] IP address to check. - virtual bool IsInSecurityExceptionList(const char *ip)=0; - - /// Sets how many incoming connections are allowed. If this is less than the number of players currently connected, - /// no more players will be allowed to connect. If this is greater than the maximum number of peers allowed, - /// it will be reduced to the maximum number of peers allowed. - /// Defaults to 0, meaning by default, nobody can connect to you - /// \param[in] numberAllowed Maximum number of incoming connections allowed. - virtual void SetMaximumIncomingConnections( unsigned short numberAllowed )=0; - - /// Returns the value passed to SetMaximumIncomingConnections() - /// \return the maximum number of incoming connections, which is always <= maxConnections - virtual unsigned int GetMaximumIncomingConnections( void ) const=0; - - /// Returns how many open connections there are at this time - /// \return the number of open connections - virtual unsigned short NumberOfConnections(void) const=0; - - /// Sets the password incoming connections must match in the call to Connect (defaults to none). Pass 0 to passwordData to specify no password - /// This is a way to set a low level password for all incoming connections. To selectively reject connections, implement your own scheme using CloseConnection() to remove unwanted connections - /// \param[in] passwordData A data block that incoming connections must match. This can be just a password, or can be a stream of data. Specify 0 for no password data - /// \param[in] passwordDataLength The length in bytes of passwordData - virtual void SetIncomingPassword( const char* passwordData, int passwordDataLength )=0; - - /// Gets the password passed to SetIncomingPassword - /// \param[out] passwordData Should point to a block large enough to hold the password data you passed to SetIncomingPassword() - /// \param[in,out] passwordDataLength Maximum size of the array passwordData. Modified to hold the number of bytes actually written - virtual void GetIncomingPassword( char* passwordData, int *passwordDataLength )=0; - - /// \brief Connect to the specified host (ip or domain name) and server port. - /// Calling Connect and not calling SetMaximumIncomingConnections acts as a dedicated client. - /// Calling both acts as a true peer. This is a non-blocking connection. - /// You know the connection is successful when GetConnectionState() returns IS_CONNECTED or Receive() gets a message with the type identifier ID_CONNECTION_REQUEST_ACCEPTED. - /// If the connection is not successful, such as a rejected connection or no response then neither of these things will happen. - /// \pre Requires that you first call Startup() - /// \param[in] host Either a dotted IP address or a domain name - /// \param[in] remotePort Which port to connect to on the remote machine. - /// \param[in] passwordData A data block that must match the data block on the server passed to SetIncomingPassword. This can be a string or can be a stream of data. Use 0 for no password. - /// \param[in] passwordDataLength The length in bytes of passwordData - /// \param[in] publicKey The public key the server is using. If 0, the server is not using security. If non-zero, the publicKeyMode member determines how to connect - /// \param[in] connectionSocketIndex Index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to send on. - /// \param[in] sendConnectionAttemptCount How many datagrams to send to the other system to try to connect. - /// \param[in] timeBetweenSendConnectionAttemptsMS Time to elapse before a datagram is sent to the other system to try to connect. After sendConnectionAttemptCount number of attempts, ID_CONNECTION_ATTEMPT_FAILED is returned. Under low bandwidth conditions with multiple simultaneous outgoing connections, this value should be raised to 1000 or higher, or else the MTU detection can overrun the available bandwidth. - /// \param[in] timeoutTime How long to keep the connection alive before dropping it on unable to send a reliable message. 0 to use the default from SetTimeoutTime(UNASSIGNED_SYSTEM_ADDRESS); - /// \return CONNECTION_ATTEMPT_STARTED on successful initiation. Otherwise, an appropriate enumeration indicating failure. - /// \note CONNECTION_ATTEMPT_STARTED does not mean you are already connected! - /// \note It is possible to immediately get back ID_CONNECTION_ATTEMPT_FAILED if you exceed the maxConnections parameter passed to Startup(). This could happen if you call CloseConnection() with sendDisconnectionNotificaiton true, then immediately call Connect() before the connection has closed. - virtual ConnectionAttemptResult Connect( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey=0, unsigned connectionSocketIndex=0, unsigned sendConnectionAttemptCount=12, unsigned timeBetweenSendConnectionAttemptsMS=500, RakNet::TimeMS timeoutTime=0 )=0; - - /// \brief Connect to the specified host (ip or domain name) and server port, using a shared socket from another instance of RakNet - /// \param[in] host Either a dotted IP address or a domain name - /// \param[in] remotePort Which port to connect to on the remote machine. - /// \param[in] passwordData A data block that must match the data block on the server passed to SetIncomingPassword. This can be a string or can be a stream of data. Use 0 for no password. - /// \param[in] passwordDataLength The length in bytes of passwordData - /// \param[in] socket A bound socket returned by another instance of RakPeerInterface - /// \param[in] sendConnectionAttemptCount How many datagrams to send to the other system to try to connect. - /// \param[in] timeBetweenSendConnectionAttemptsMS Time to elapse before a datagram is sent to the other system to try to connect. After sendConnectionAttemptCount number of attempts, ID_CONNECTION_ATTEMPT_FAILED is returned. Under low bandwidth conditions with multiple simultaneous outgoing connections, this value should be raised to 1000 or higher, or else the MTU detection can overrun the available bandwidth. - /// \param[in] timeoutTime How long to keep the connection alive before dropping it on unable to send a reliable message. 0 to use the default from SetTimeoutTime(UNASSIGNED_SYSTEM_ADDRESS); - /// \return CONNECTION_ATTEMPT_STARTED on successful initiation. Otherwise, an appropriate enumeration indicating failure. - /// \note CONNECTION_ATTEMPT_STARTED does not mean you are already connected! - virtual ConnectionAttemptResult ConnectWithSocket(const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, RakNetSocket2* socket, PublicKey *publicKey=0, unsigned sendConnectionAttemptCount=12, unsigned timeBetweenSendConnectionAttemptsMS=500, RakNet::TimeMS timeoutTime=0)=0; - - /// \brief Connect to the specified network ID (Platform specific console function) - /// \details Does built-in NAt traversal - /// \param[in] passwordData A data block that must match the data block on the server passed to SetIncomingPassword. This can be a string or can be a stream of data. Use 0 for no password. - /// \param[in] passwordDataLength The length in bytes of passwordData - //virtual bool Console2LobbyConnect( void *networkServiceId, const char *passwordData, int passwordDataLength )=0; - - /// \brief Stops the network threads and closes all connections. - /// \param[in] blockDuration How long, in milliseconds, you should wait for all remaining messages to go out, including ID_DISCONNECTION_NOTIFICATION. If 0, it doesn't wait at all. - /// \param[in] orderingChannel If blockDuration > 0, ID_DISCONNECTION_NOTIFICATION will be sent on this channel - /// \param[in] disconnectionNotificationPriority Priority to send ID_DISCONNECTION_NOTIFICATION on. - /// If you set it to 0 then the disconnection notification won't be sent - virtual void Shutdown( unsigned int blockDuration, unsigned char orderingChannel=0, PacketPriority disconnectionNotificationPriority=LOW_PRIORITY )=0; - - /// Returns if the network thread is running - /// \return true if the network thread is running, false otherwise - virtual bool IsActive( void ) const=0; - - /// Fills the array remoteSystems with the SystemAddress of all the systems we are connected to - /// \param[out] remoteSystems An array of SystemAddress structures to be filled with the SystemAddresss of the systems we are connected to. Pass 0 to remoteSystems to only get the number of systems we are connected to - /// \param[in, out] numberOfSystems As input, the size of remoteSystems array. As output, the number of elements put into the array - virtual bool GetConnectionList( SystemAddress *remoteSystems, unsigned short *numberOfSystems ) const=0; - - /// Returns the next uint32_t that Send() will return - /// \note If using RakPeer from multiple threads, this may not be accurate for your thread. Use IncrementNextSendReceipt() in that case. - /// \return The next uint32_t that Send() or SendList will return - virtual uint32_t GetNextSendReceipt(void)=0; - - /// Returns the next uint32_t that Send() will return, and increments the value by one - /// \note If using RakPeer from multiple threads, pass this to forceReceipt in the send function - /// \return The next uint32_t that Send() or SendList will return - virtual uint32_t IncrementNextSendReceipt(void)=0; - - /// Sends a block of data to the specified system that you are connected to. - /// This function only works while connected - /// The first byte should be a message identifier starting at ID_USER_PACKET_ENUM - /// \param[in] data The block of data to send - /// \param[in] length The size in bytes of the data to send - /// \param[in] priority What priority level to send on. See PacketPriority.h - /// \param[in] reliability How reliability to send this data. See PacketPriority.h - /// \param[in] orderingChannel When using ordered or sequenced messages, what channel to order these on. Messages are only ordered relative to other messages on the same stream - /// \param[in] systemIdentifier Who to send this packet to, or in the case of broadcasting who not to send it to. Pass either a SystemAddress structure or a RakNetGUID structure. Use UNASSIGNED_SYSTEM_ADDRESS or to specify none - /// \param[in] broadcast True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. - /// \param[in] forceReceipt If 0, will automatically determine the receipt number to return. If non-zero, will return what you give it. - /// \return 0 on bad input. Otherwise a number that identifies this message. If \a reliability is a type that returns a receipt, on a later call to Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS with bytes 1-4 inclusive containing this number - virtual uint32_t Send( const char *data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 )=0; - - /// "Send" to yourself rather than a remote system. The message will be processed through the plugins and returned to the game as usual - /// This function works anytime - /// The first byte should be a message identifier starting at ID_USER_PACKET_ENUM - /// \param[in] data The block of data to send - /// \param[in] length The size in bytes of the data to send - virtual void SendLoopback( const char *data, const int length )=0; - - /// Sends a block of data to the specified system that you are connected to. Same as the above version, but takes a BitStream as input. - /// \param[in] bitStream The bitstream to send - /// \param[in] priority What priority level to send on. See PacketPriority.h - /// \param[in] reliability How reliability to send this data. See PacketPriority.h - /// \param[in] orderingChannel When using ordered or sequenced messages, what channel to order these on. Messages are only ordered relative to other messages on the same stream - /// \param[in] systemIdentifier Who to send this packet to, or in the case of broadcasting who not to send it to. Pass either a SystemAddress structure or a RakNetGUID structure. Use UNASSIGNED_SYSTEM_ADDRESS or to specify none - /// \param[in] broadcast True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. - /// \param[in] forceReceipt If 0, will automatically determine the receipt number to return. If non-zero, will return what you give it. - /// \return 0 on bad input. Otherwise a number that identifies this message. If \a reliability is a type that returns a receipt, on a later call to Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS with bytes 1-4 inclusive containing this number - /// \note COMMON MISTAKE: When writing the first byte, bitStream->Write((unsigned char) ID_MY_TYPE) be sure it is casted to a byte, and you are not writing a 4 byte enumeration. - virtual uint32_t Send( const RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 )=0; - - /// Sends multiple blocks of data, concatenating them automatically. - /// - /// This is equivalent to: - /// RakNet::BitStream bs; - /// bs.WriteAlignedBytes(block1, blockLength1); - /// bs.WriteAlignedBytes(block2, blockLength2); - /// bs.WriteAlignedBytes(block3, blockLength3); - /// Send(&bs, ...) - /// - /// This function only works while connected - /// \param[in] data An array of pointers to blocks of data - /// \param[in] lengths An array of integers indicating the length of each block of data - /// \param[in] numParameters Length of the arrays data and lengths - /// \param[in] priority What priority level to send on. See PacketPriority.h - /// \param[in] reliability How reliability to send this data. See PacketPriority.h - /// \param[in] orderingChannel When using ordered or sequenced messages, what channel to order these on. Messages are only ordered relative to other messages on the same stream - /// \param[in] systemIdentifier Who to send this packet to, or in the case of broadcasting who not to send it to. Pass either a SystemAddress structure or a RakNetGUID structure. Use UNASSIGNED_SYSTEM_ADDRESS or to specify none - /// \param[in] broadcast True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. - /// \param[in] forceReceipt If 0, will automatically determine the receipt number to return. If non-zero, will return what you give it. - /// \return 0 on bad input. Otherwise a number that identifies this message. If \a reliability is a type that returns a receipt, on a later call to Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS with bytes 1-4 inclusive containing this number - virtual uint32_t SendList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 )=0; - - /// Gets a message from the incoming message queue. - /// Use DeallocatePacket() to deallocate the message after you are done with it. - /// User-thread functions, such as RPC calls and the plugin function PluginInterface::Update occur here. - /// \return 0 if no packets are waiting to be handled, otherwise a pointer to a packet. - /// \note COMMON MISTAKE: Be sure to call this in a loop, once per game tick, until it returns 0. If you only process one packet per game tick they will buffer up. - /// sa RakNetTypes.h contains struct Packet - virtual Packet* Receive( void )=0; - - /// Call this to deallocate a message returned by Receive() when you are done handling it. - /// \param[in] packet The message to deallocate. - virtual void DeallocatePacket( Packet *packet )=0; - - /// Return the total number of connections we are allowed - virtual unsigned int GetMaximumNumberOfPeers( void ) const=0; - - // -------------------------------------------------------------------------------------------- Connection Management Functions-------------------------------------------------------------------------------------------- - /// Close the connection to another host (if we initiated the connection it will disconnect, if they did it will kick them out). - /// \param[in] target Which system to close the connection to. - /// \param[in] sendDisconnectionNotification True to send ID_DISCONNECTION_NOTIFICATION to the recipient. False to close it silently. - /// \param[in] channel Which ordering channel to send the disconnection notification on, if any - /// \param[in] disconnectionNotificationPriority Priority to send ID_DISCONNECTION_NOTIFICATION on. - virtual void CloseConnection( const AddressOrGUID target, bool sendDisconnectionNotification, unsigned char orderingChannel=0, PacketPriority disconnectionNotificationPriority=LOW_PRIORITY )=0; - - /// Returns if a system is connected, disconnected, connecting in progress, or various other states - /// \param[in] systemIdentifier The system we are referring to - /// \note This locks a mutex, do not call too frequently during connection attempts or the attempt will take longer and possibly even timeout - /// \return What state the remote system is in - virtual ConnectionState GetConnectionState(const AddressOrGUID systemIdentifier)=0; - - /// Cancel a pending connection attempt - /// If we are already connected, the connection stays open - /// \param[in] target Which system to cancel - virtual void CancelConnectionAttempt( const SystemAddress target )=0; - - /// Given a systemAddress, returns an index from 0 to the maximum number of players allowed - 1. - /// \param[in] systemAddress The SystemAddress we are referring to - /// \return The index of this SystemAddress or -1 on system not found. - virtual int GetIndexFromSystemAddress( const SystemAddress systemAddress ) const=0; - - /// This function is only useful for looping through all systems - /// Given an index, will return a SystemAddress. - /// \param[in] index Index should range between 0 and the maximum number of players allowed - 1. - /// \return The SystemAddress - virtual SystemAddress GetSystemAddressFromIndex( unsigned int index )=0; - - /// Same as GetSystemAddressFromIndex but returns RakNetGUID - /// \param[in] index Index should range between 0 and the maximum number of players allowed - 1. - /// \return The RakNetGUID - virtual RakNetGUID GetGUIDFromIndex( unsigned int index )=0; - - /// Same as calling GetSystemAddressFromIndex and GetGUIDFromIndex for all systems, but more efficient - /// Indices match each other, so \a addresses[0] and \a guids[0] refer to the same system - /// \param[out] addresses All system addresses. Size of the list is the number of connections. Size of the list will match the size of the \a guids list. - /// \param[out] guids All guids. Size of the list is the number of connections. Size of the list will match the size of the \a addresses list. - virtual void GetSystemList(DataStructures::List &addresses, DataStructures::List &guids) const=0; - - /// Bans an IP from connecting. Banned IPs persist between connections but are not saved on shutdown nor loaded on startup. - /// param[in] IP Dotted IP address. Can use * as a wildcard, such as 128.0.0.* will ban all IP addresses starting with 128.0.0 - /// \param[in] milliseconds how many ms for a temporary ban. Use 0 for a permanent ban - virtual void AddToBanList( const char *IP, RakNet::TimeMS milliseconds=0 )=0; - - /// Allows a previously banned IP to connect. - /// param[in] Dotted IP address. Can use * as a wildcard, such as 128.0.0.* will banAll IP addresses starting with 128.0.0 - virtual void RemoveFromBanList( const char *IP )=0; - - /// Allows all previously banned IPs to connect. - virtual void ClearBanList( void )=0; - - /// Returns true or false indicating if a particular IP is banned. - /// \param[in] IP - Dotted IP address. - /// \return true if IP matches any IPs in the ban list, accounting for any wildcards. False otherwise. - virtual bool IsBanned( const char *IP )=0; - - /// Enable or disable allowing frequent connections from the same IP adderss - /// This is a security measure which is disabled by default, but can be set to true to prevent attackers from using up all connection slots - /// \param[in] b True to limit connections from the same ip to at most 1 per 100 milliseconds. - virtual void SetLimitIPConnectionFrequency(bool b)=0; - - // --------------------------------------------------------------------------------------------Pinging Functions - Functions dealing with the automatic ping mechanism-------------------------------------------------------------------------------------------- - /// Send a ping to the specified connected system. - /// \pre The sender and recipient must already be started via a successful call to Startup() - /// \param[in] target Which system to ping - virtual void Ping( const SystemAddress target )=0; - - /// Send a ping to the specified unconnected system. The remote system, if it is Initialized, will respond with ID_PONG followed by sizeof(RakNet::TimeMS) containing the system time the ping was sent.(Default is 4 bytes - See __GET_TIME_64BIT in RakNetTypes.h - /// System should reply with ID_PONG if it is active - /// \param[in] host Either a dotted IP address or a domain name. Can be 255.255.255.255 for LAN broadcast. - /// \param[in] remotePort Which port to connect to on the remote machine. - /// \param[in] onlyReplyOnAcceptingConnections Only request a reply if the remote system is accepting connections - /// \param[in] connectionSocketIndex Index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to send on. - /// \return true on success, false on failure (unknown hostname) - virtual bool Ping( const char* host, unsigned short remotePort, bool onlyReplyOnAcceptingConnections, unsigned connectionSocketIndex=0 )=0; - - /// Returns the average of all ping times read for the specific system or -1 if none read yet - /// \param[in] systemAddress Which system we are referring to - /// \return The ping time for this system, or -1 - virtual int GetAveragePing( const AddressOrGUID systemIdentifier )=0; - - /// Returns the last ping time read for the specific system or -1 if none read yet - /// \param[in] systemAddress Which system we are referring to - /// \return The last ping time for this system, or -1 - virtual int GetLastPing( const AddressOrGUID systemIdentifier ) const=0; - - /// Returns the lowest ping time read or -1 if none read yet - /// \param[in] systemAddress Which system we are referring to - /// \return The lowest ping time for this system, or -1 - virtual int GetLowestPing( const AddressOrGUID systemIdentifier ) const=0; - - /// Ping the remote systems every so often, or not. Can be called anytime. - /// By default this is true. Recommended to leave on, because congestion control uses it to determine how often to resend lost packets. - /// It would be true by default to prevent timestamp drift, since in the event of a clock spike, the timestamp deltas would no longer be accurate - /// \param[in] doPing True to start occasional pings. False to stop them. - virtual void SetOccasionalPing( bool doPing )=0; - - /// Return the clock difference between your system and the specified system - /// Subtract GetClockDifferential() from a time returned by the remote system to get that time relative to your own system - /// Returns 0 if the system is unknown - /// \param[in] systemIdentifier Which system we are referring to - virtual RakNet::Time GetClockDifferential( const AddressOrGUID systemIdentifier )=0; - - // --------------------------------------------------------------------------------------------Static Data Functions - Functions dealing with API defined synchronized memory-------------------------------------------------------------------------------------------- - /// Sets the data to send along with a LAN server discovery or offline ping reply. - /// \a length should be under 400 bytes, as a security measure against flood attacks - /// \param[in] data a block of data to store, or 0 for none - /// \param[in] length The length of data in bytes, or 0 for none - /// \sa Ping.cpp - virtual void SetOfflinePingResponse( const char *data, const unsigned int length )=0; - - /// Returns pointers to a copy of the data passed to SetOfflinePingResponse - /// \param[out] data A pointer to a copy of the data passed to \a SetOfflinePingResponse() - /// \param[out] length A pointer filled in with the length parameter passed to SetOfflinePingResponse() - /// \sa SetOfflinePingResponse - virtual void GetOfflinePingResponse( char **data, unsigned int *length )=0; - - //--------------------------------------------------------------------------------------------Network Functions - Functions dealing with the network in general-------------------------------------------------------------------------------------------- - /// Return the unique address identifier that represents you or another system on the the network and is based on your local IP / port. - /// \note Not supported by the XBOX - /// \param[in] systemAddress Use UNASSIGNED_SYSTEM_ADDRESS to get your behind-LAN address. Use a connected system to get their behind-LAN address - /// \param[in] index When you have multiple internal IDs, which index to return? Currently limited to MAXIMUM_NUMBER_OF_INTERNAL_IDS (so the maximum value of this variable is MAXIMUM_NUMBER_OF_INTERNAL_IDS-1) - /// \return the identifier of your system internally, which may not be how other systems see if you if you are behind a NAT or proxy - virtual SystemAddress GetInternalID( const SystemAddress systemAddress=UNASSIGNED_SYSTEM_ADDRESS, const int index=0 ) const=0; - - /// \brief Sets your internal IP address, for platforms that do not support reading it, or to override a value - /// \param[in] systemAddress. The address to set. Use SystemAddress::FromString() if you want to use a dotted string - /// \param[in] index When you have multiple internal IDs, which index to set? - virtual void SetInternalID(SystemAddress systemAddress, int index=0)=0; - - /// Return the unique address identifier that represents you on the the network and is based on your externalIP / port - /// (the IP / port the specified player uses to communicate with you) - /// \param[in] target Which remote system you are referring to for your external ID. Usually the same for all systems, unless you have two or more network cards. - virtual SystemAddress GetExternalID( const SystemAddress target ) const=0; - - /// Return my own GUID - virtual const RakNetGUID GetMyGUID(void) const=0; - - /// Return the address bound to a socket at the specified index - virtual SystemAddress GetMyBoundAddress(const int socketIndex=0)=0; - - /// Get a random number (to generate a GUID) - static uint64_t Get64BitUniqueRandomNumber(void); - - /// Given a connected system, give us the unique GUID representing that instance of RakPeer. - /// This will be the same on all systems connected to that instance of RakPeer, even if the external system addresses are different - /// Currently O(log(n)), but this may be improved in the future. If you use this frequently, you may want to cache the value as it won't change. - /// Returns UNASSIGNED_RAKNET_GUID if system address can't be found. - /// If \a input is UNASSIGNED_SYSTEM_ADDRESS, will return your own GUID - /// \pre Call Startup() first, or the function will return UNASSIGNED_RAKNET_GUID - /// \param[in] input The system address of the system we are connected to - virtual const RakNetGUID& GetGuidFromSystemAddress( const SystemAddress input ) const=0; - - /// Given the GUID of a connected system, give us the system address of that system. - /// The GUID will be the same on all systems connected to that instance of RakPeer, even if the external system addresses are different - /// Currently O(log(n)), but this may be improved in the future. If you use this frequently, you may want to cache the value as it won't change. - /// If \a input is UNASSIGNED_RAKNET_GUID, will return UNASSIGNED_SYSTEM_ADDRESS - /// \param[in] input The RakNetGUID of the system we are checking to see if we are connected to - virtual SystemAddress GetSystemAddressFromGuid( const RakNetGUID input ) const=0; - - /// Given the SystemAddress of a connected system, get the public key they provided as an identity - /// Returns false if system address was not found or client public key is not known - /// \param[in] input The RakNetGUID of the system - /// \param[in] client_public_key The connected client's public key is copied to this address. Buffer must be cat::EasyHandshake::PUBLIC_KEY_BYTES bytes in length. - virtual bool GetClientPublicKeyFromSystemAddress( const SystemAddress input, char *client_public_key ) const=0; - - /// Set the time, in MS, to use before considering ourselves disconnected after not being able to deliver a reliable message. - /// Default time is 10,000 or 10 seconds in release and 30,000 or 30 seconds in debug. - /// Do not set different values for different computers that are connected to each other, or you won't be able to reconnect after ID_CONNECTION_LOST - /// \param[in] timeMS Time, in MS - /// \param[in] target Which system to do this for. Pass UNASSIGNED_SYSTEM_ADDRESS for all systems. - virtual void SetTimeoutTime( RakNet::TimeMS timeMS, const SystemAddress target )=0; - - /// \param[in] target Which system to do this for. Pass UNASSIGNED_SYSTEM_ADDRESS to get the default value - /// \return timeoutTime for a given system. - virtual RakNet::TimeMS GetTimeoutTime( const SystemAddress target )=0; - - /// Returns the current MTU size - /// \param[in] target Which system to get this for. UNASSIGNED_SYSTEM_ADDRESS to get the default - /// \return The current MTU size - virtual int GetMTUSize( const SystemAddress target ) const=0; - - /// Returns the number of IP addresses this system has internally. Get the actual addresses from GetLocalIP() - virtual unsigned GetNumberOfAddresses( void )=0; - - /// Returns an IP address at index 0 to GetNumberOfAddresses-1 - /// \param[in] index index into the list of IP addresses - /// \return The local IP address at this index - virtual const char* GetLocalIP( unsigned int index )=0; - - /// Is this a local IP? - /// \param[in] An IP address to check, excluding the port - /// \return True if this is one of the IP addresses returned by GetLocalIP - virtual bool IsLocalIP( const char *ip )=0; - - /// Allow or disallow connection responses from any IP. Normally this should be false, but may be necessary - /// when connecting to servers with multiple IP addresses. - /// \param[in] allow - True to allow this behavior, false to not allow. Defaults to false. Value persists between connections - virtual void AllowConnectionResponseIPMigration( bool allow )=0; - - /// Sends a one byte message ID_ADVERTISE_SYSTEM to the remote unconnected system. - /// This will tell the remote system our external IP outside the LAN along with some user data. - /// \pre The sender and recipient must already be started via a successful call to Initialize - /// \param[in] host Either a dotted IP address or a domain name - /// \param[in] remotePort Which port to connect to on the remote machine. - /// \param[in] data Optional data to append to the packet. - /// \param[in] dataLength length of data in bytes. Use 0 if no data. - /// \param[in] connectionSocketIndex Index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to send on. - /// \return false if IsActive()==false or the host is unresolvable. True otherwise - virtual bool AdvertiseSystem( const char *host, unsigned short remotePort, const char *data, int dataLength, unsigned connectionSocketIndex=0 )=0; - - /// Controls how often to return ID_DOWNLOAD_PROGRESS for large message downloads. - /// ID_DOWNLOAD_PROGRESS is returned to indicate a new partial message chunk, roughly the MTU size, has arrived - /// As it can be slow or cumbersome to get this notification for every chunk, you can set the interval at which it is returned. - /// Defaults to 0 (never return this notification) - /// \param[in] interval How many messages to use as an interval - virtual void SetSplitMessageProgressInterval(int interval)=0; - - /// Returns what was passed to SetSplitMessageProgressInterval() - /// \return What was passed to SetSplitMessageProgressInterval(). Default to 0. - virtual int GetSplitMessageProgressInterval(void) const=0; - - /// Set how long to wait before giving up on sending an unreliable message - /// Useful if the network is clogged up. - /// Set to 0 or less to never timeout. Defaults to 0. - /// \param[in] timeoutMS How many ms to wait before simply not sending an unreliable message. - virtual void SetUnreliableTimeout(RakNet::TimeMS timeoutMS)=0; - - /// Send a message to host, with the IP socket option TTL set to 3 - /// This message will not reach the host, but will open the router. - /// Used for NAT-Punchthrough - virtual void SendTTL( const char* host, unsigned short remotePort, int ttl, unsigned connectionSocketIndex=0 )=0; - - // -------------------------------------------------------------------------------------------- Plugin Functions-------------------------------------------------------------------------------------------- - /// \brief Attaches a Plugin interface to an instance of the base class (RakPeer or PacketizedTCP) to run code automatically on message receipt in the Receive call. - /// If the plugin returns false from PluginInterface::UsesReliabilityLayer(), which is the case for all plugins except PacketLogger, you can call AttachPlugin() and DetachPlugin() for this plugin while RakPeer is active. - /// \param[in] messageHandler Pointer to the plugin to attach. - virtual void AttachPlugin( PluginInterface2 *plugin )=0; - - /// \brief Detaches a Plugin interface from the instance of the base class (RakPeer or PacketizedTCP) it is attached to. - /// \details This method disables the plugin code from running automatically on base class's updates or message receipt. - /// If the plugin returns false from PluginInterface::UsesReliabilityLayer(), which is the case for all plugins except PacketLogger, you can call AttachPlugin() and DetachPlugin() for this plugin while RakPeer is active. - /// \param[in] messageHandler Pointer to a plugin to detach. - virtual void DetachPlugin( PluginInterface2 *messageHandler )=0; - - // --------------------------------------------------------------------------------------------Miscellaneous Functions-------------------------------------------------------------------------------------------- - /// Put a message back at the end of the receive queue in case you don't want to deal with it immediately - /// \param[in] packet The packet you want to push back. - /// \param[in] pushAtHead True to push the packet so that the next receive call returns it. False to push it at the end of the queue (obviously pushing it at the end makes the packets out of order) - virtual void PushBackPacket( Packet *packet, bool pushAtHead )=0; - - /// \internal - /// \brief For a given system identified by \a guid, change the SystemAddress to send to. - /// \param[in] guid The connection we are referring to - /// \param[in] systemAddress The new address to send to - virtual void ChangeSystemAddress(RakNetGUID guid, const SystemAddress &systemAddress)=0; - - /// \returns a packet for you to write to if you want to create a Packet for some reason. - /// You can add it to the receive buffer with PushBackPacket - /// \param[in] dataSize How many bytes to allocate for the buffer - /// \return A packet you can write to - virtual Packet* AllocatePacket(unsigned dataSize)=0; - - /// Get the socket used with a particular active connection - /// The smart pointer reference counts the RakNetSocket2 object, so the socket will remain active as long as the smart pointer does, even if RakNet were to shutdown or close the connection. - /// \note This sends a query to the thread and blocks on the return value for up to one second. In practice it should only take a millisecond or so. - /// \param[in] target Which system - /// \return A smart pointer object containing the socket information about the socket. Be sure to check IsNull() which is returned if the update thread is unresponsive, shutting down, or if this system is not connected - virtual RakNetSocket2* GetSocket( const SystemAddress target )=0; - - /// Get all sockets in use - /// \note This sends a query to the thread and blocks on the return value for up to one second. In practice it should only take a millisecond or so. - /// \param[out] sockets List of RakNetSocket2 structures in use. Sockets will not be closed until \a sockets goes out of scope - virtual void GetSockets( DataStructures::List &sockets )=0; - virtual void ReleaseSockets( DataStructures::List &sockets )=0; - - virtual void WriteOutOfBandHeader(RakNet::BitStream *bitStream)=0; - - /// If you need code to run in the same thread as RakNet's update thread, this function can be used for that - /// \param[in] _userUpdateThreadPtr C callback function - /// \param[in] _userUpdateThreadData Passed to C callback function - virtual void SetUserUpdateThread(void (*_userUpdateThreadPtr)(RakPeerInterface *, void *), void *_userUpdateThreadData)=0; - - /// Set a C callback to be called whenever a datagram arrives - /// Return true from the callback to have RakPeer handle the datagram. Return false and RakPeer will ignore the datagram. - /// This can be used to filter incoming datagrams by system, or to share a recvfrom socket with RakPeer - /// RNS2RecvStruct will only remain valid for the duration of the call - /// If the incoming datagram is not from your game at all, it is a RakNet packet. - /// If the incoming datagram has an IP address that matches a known address from your game, then check the first byte of data. - /// For RakNet connected systems, the first bit is always 1. So for your own game packets, make sure the first bit is always 0. - virtual void SetIncomingDatagramEventHandler( bool (*_incomingDatagramEventHandler)(RNS2RecvStruct *) )=0; - - // --------------------------------------------------------------------------------------------Network Simulator Functions-------------------------------------------------------------------------------------------- - /// Adds simulated ping and packet loss to the outgoing data flow. - /// To simulate bi-directional ping and packet loss, you should call this on both the sender and the recipient, with half the total ping and packetloss value on each. - /// You can exclude network simulator code with the _RELEASE #define to decrease code size - /// \deprecated Use http://www.jenkinssoftware.com/forum/index.php?topic=1671.0 instead. - /// \note Doesn't work past version 3.6201 - /// \param[in] packetloss Chance to lose a packet. Ranges from 0 to 1. - /// \param[in] minExtraPing The minimum time to delay sends. - /// \param[in] extraPingVariance The additional random time to delay sends. - virtual void ApplyNetworkSimulator( float packetloss, unsigned short minExtraPing, unsigned short extraPingVariance)=0; - - /// Limits how much outgoing bandwidth can be sent per-connection. - /// This limit does not apply to the sum of all connections! - /// Exceeding the limit queues up outgoing traffic - /// \param[in] maxBitsPerSecond Maximum bits per second to send. Use 0 for unlimited (default). Once set, it takes effect immedately and persists until called again. - virtual void SetPerConnectionOutgoingBandwidthLimit( unsigned maxBitsPerSecond )=0; - - /// Returns if you previously called ApplyNetworkSimulator - /// \return If you previously called ApplyNetworkSimulator - virtual bool IsNetworkSimulatorActive( void )=0; - - // --------------------------------------------------------------------------------------------Statistical Functions - Functions dealing with API performance-------------------------------------------------------------------------------------------- - - /// Returns a structure containing a large set of network statistics for the specified system. - /// You can map this data to a string using the C style StatisticsToString() function - /// \param[in] systemAddress: Which connected system to get statistics for - /// \param[in] rns If you supply this structure, it will be written to it. Otherwise it will use a static struct, which is not threadsafe - /// \return 0 on can't find the specified system. A pointer to a set of data otherwise. - /// \sa RakNetStatistics.h - virtual RakNetStatistics * GetStatistics( const SystemAddress systemAddress, RakNetStatistics *rns=0 )=0; - /// \brief Returns the network statistics of the system at the given index in the remoteSystemList. - /// \return True if the index is less than the maximum number of peers allowed and the system is active. False otherwise. - virtual bool GetStatistics( const unsigned int index, RakNetStatistics *rns )=0; - /// \brief Returns the list of systems, and statistics for each of those systems - /// Each system has one entry in each of the lists, in the same order - /// \param[out] addresses SystemAddress for each connected system - /// \param[out] guids RakNetGUID for each connected system - /// \param[out] statistics Calculated RakNetStatistics for each connected system - virtual void GetStatisticsList(DataStructures::List &addresses, DataStructures::List &guids, DataStructures::List &statistics)=0; - - /// \Returns how many messages are waiting when you call Receive() - virtual unsigned int GetReceiveBufferSize(void)=0; - - // --------------------------------------------------------------------------------------------EVERYTHING AFTER THIS COMMENT IS FOR INTERNAL USE ONLY-------------------------------------------------------------------------------------------- - - /// \internal - // Call manually if RAKPEER_USER_THREADED==1 at least every 30 milliseconds. - // updateBitStream should be: - // BitStream updateBitStream( MAXIMUM_MTU_SIZE - // #if LIBCAT_SECURITY==1 - // + cat::AuthenticatedEncryption::OVERHEAD_BYTES - // #endif - // ); - virtual bool RunUpdateCycle( BitStream &updateBitStream )=0; - - /// \internal - virtual bool SendOutOfBand(const char *host, unsigned short remotePort, const char *data, BitSize_t dataLength, unsigned connectionSocketIndex=0 )=0; - -} -// #if defined(SN_TARGET_PSP2) -// __attribute__((aligned(8))) -// #endif -; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief An interface for RakPeer. Simply contains all user functions as pure virtuals. +/// + + + +#pragma once + +#include "PacketPriority.h" +#include "RakNetTypes.h" +#include "RakMemoryOverride.h" +#include "Export.h" +#include "DS_List.h" +#include "RakNetSmartPtr.h" +#include "RakNetSocket2.h" + +namespace RakNet +{ +// Forward declarations +class BitStream; +class PluginInterface2; +struct RPCMap; +struct RakNetStatistics; +struct RakNetBandwidth; +class RouterInterface; +class NetworkIDManager; + +/// The primary interface for RakNet, RakPeer contains all major functions for the library. +/// See the individual functions for what the class can do. +/// \brief The main interface for network communications +class RAK_DLL_EXPORT RakPeerInterface +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(RakPeerInterface) + + ///Destructor + virtual ~RakPeerInterface() {} + + // --------------------------------------------------------------------------------------------Major Low Level Functions - Functions needed by most users-------------------------------------------------------------------------------------------- + /// \brief Starts the network threads, opens the listen port. + /// \details You must call this before calling Connect(). + /// \pre On the PS3, call Startup() after Client_Login() + /// \pre On Android, add the necessary permission to your application's androidmanifest.xml: + /// Multiple calls while already active are ignored. To call this function again with different settings, you must first call Shutdown(). + /// \note Call SetMaximumIncomingConnections if you want to accept incoming connections + /// \param[in] maxConnections The maximum number of connections between this instance of RakPeer and another instance of RakPeer. Required so the network can preallocate and for thread safety. A pure client would set this to 1. A pure server would set it to the number of allowed clients.- A hybrid would set it to the sum of both types of connections + /// \param[in] localPort The port to listen for connections on. On linux the system may be set up so thast ports under 1024 are restricted for everything but the root user. Use a higher port for maximum compatibility. + /// \param[in] socketDescriptors An array of SocketDescriptor structures to force RakNet to listen on a particular IP address or port (or both). Each SocketDescriptor will represent one unique socket. Do not pass redundant structures. To listen on a specific port, you can pass SocketDescriptor(myPort,0); such as for a server. For a client, it is usually OK to just pass SocketDescriptor(); However, on the XBOX be sure to use IPPROTO_VDP + /// \param[in] socketDescriptorCount The size of the \a socketDescriptors array. Pass 1 if you are not sure what to pass. + /// \param[in] threadPriority Passed to the thread creation routine. Use THREAD_PRIORITY_NORMAL for Windows. For Linux based systems, you MUST pass something reasonable based on the thread priorities for your application. + /// \return RAKNET_STARTED on success, otherwise appropriate failure enumeration. + virtual StartupResult Startup( unsigned int maxConnections, SocketDescriptor *socketDescriptors, unsigned socketDescriptorCount, int threadPriority=-99999 )=0; + + /// If you accept connections, you must call this or else security will not be enabled for incoming connections. + /// This feature requires more round trips, bandwidth, and CPU time for the connection handshake + /// x64 builds require under 25% of the CPU time of other builds + /// See the Encryption sample for example usage + /// \pre Must be called while offline + /// \pre LIBCAT_SECURITY must be defined to 1 in NativeFeatureIncludes.h for this function to have any effect + /// \param[in] publicKey A pointer to the public key for accepting new connections + /// \param[in] privateKey A pointer to the private key for accepting new connections + /// \param[in] bRequireClientKey: Should be set to false for most servers. Allows the server to accept a public key from connecting clients as a proof of identity but eats twice as much CPU time as a normal connection + virtual bool InitializeSecurity( const char *publicKey, const char *privateKey, bool bRequireClientKey = false )=0; + + /// Disables security for incoming connections. + /// \note Must be called while offline + virtual void DisableSecurity( void )=0; + + /// If secure connections are on, do not use secure connections for a specific IP address. + /// This is useful if you have a fixed-address internal server behind a LAN. + /// \note Secure connections are determined by the recipient of an incoming connection. This has no effect if called on the system attempting to connect. + /// \param[in] ip IP address to add. * wildcards are supported. + virtual void AddToSecurityExceptionList(const char *ip)=0; + + /// Remove a specific connection previously added via AddToSecurityExceptionList + /// \param[in] ip IP address to remove. Pass 0 to remove all IP addresses. * wildcards are supported. + virtual void RemoveFromSecurityExceptionList(const char *ip)=0; + + /// Checks to see if a given IP is in the security exception list + /// \param[in] IP address to check. + virtual bool IsInSecurityExceptionList(const char *ip)=0; + + /// Sets how many incoming connections are allowed. If this is less than the number of players currently connected, + /// no more players will be allowed to connect. If this is greater than the maximum number of peers allowed, + /// it will be reduced to the maximum number of peers allowed. + /// Defaults to 0, meaning by default, nobody can connect to you + /// \param[in] numberAllowed Maximum number of incoming connections allowed. + virtual void SetMaximumIncomingConnections( unsigned short numberAllowed )=0; + + /// Returns the value passed to SetMaximumIncomingConnections() + /// \return the maximum number of incoming connections, which is always <= maxConnections + virtual unsigned int GetMaximumIncomingConnections( void ) const=0; + + /// Returns how many open connections there are at this time + /// \return the number of open connections + virtual unsigned short NumberOfConnections(void) const=0; + + /// Sets the password incoming connections must match in the call to Connect (defaults to none). Pass 0 to passwordData to specify no password + /// This is a way to set a low level password for all incoming connections. To selectively reject connections, implement your own scheme using CloseConnection() to remove unwanted connections + /// \param[in] passwordData A data block that incoming connections must match. This can be just a password, or can be a stream of data. Specify 0 for no password data + /// \param[in] passwordDataLength The length in bytes of passwordData + virtual void SetIncomingPassword( const char* passwordData, int passwordDataLength )=0; + + /// Gets the password passed to SetIncomingPassword + /// \param[out] passwordData Should point to a block large enough to hold the password data you passed to SetIncomingPassword() + /// \param[in,out] passwordDataLength Maximum size of the array passwordData. Modified to hold the number of bytes actually written + virtual void GetIncomingPassword( char* passwordData, int *passwordDataLength )=0; + + /// \brief Connect to the specified host (ip or domain name) and server port. + /// Calling Connect and not calling SetMaximumIncomingConnections acts as a dedicated client. + /// Calling both acts as a true peer. This is a non-blocking connection. + /// You know the connection is successful when GetConnectionState() returns IS_CONNECTED or Receive() gets a message with the type identifier ID_CONNECTION_REQUEST_ACCEPTED. + /// If the connection is not successful, such as a rejected connection or no response then neither of these things will happen. + /// \pre Requires that you first call Startup() + /// \param[in] host Either a dotted IP address or a domain name + /// \param[in] remotePort Which port to connect to on the remote machine. + /// \param[in] passwordData A data block that must match the data block on the server passed to SetIncomingPassword. This can be a string or can be a stream of data. Use 0 for no password. + /// \param[in] passwordDataLength The length in bytes of passwordData + /// \param[in] publicKey The public key the server is using. If 0, the server is not using security. If non-zero, the publicKeyMode member determines how to connect + /// \param[in] connectionSocketIndex Index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to send on. + /// \param[in] sendConnectionAttemptCount How many datagrams to send to the other system to try to connect. + /// \param[in] timeBetweenSendConnectionAttemptsMS Time to elapse before a datagram is sent to the other system to try to connect. After sendConnectionAttemptCount number of attempts, ID_CONNECTION_ATTEMPT_FAILED is returned. Under low bandwidth conditions with multiple simultaneous outgoing connections, this value should be raised to 1000 or higher, or else the MTU detection can overrun the available bandwidth. + /// \param[in] timeoutTime How long to keep the connection alive before dropping it on unable to send a reliable message. 0 to use the default from SetTimeoutTime(UNASSIGNED_SYSTEM_ADDRESS); + /// \return CONNECTION_ATTEMPT_STARTED on successful initiation. Otherwise, an appropriate enumeration indicating failure. + /// \note CONNECTION_ATTEMPT_STARTED does not mean you are already connected! + /// \note It is possible to immediately get back ID_CONNECTION_ATTEMPT_FAILED if you exceed the maxConnections parameter passed to Startup(). This could happen if you call CloseConnection() with sendDisconnectionNotificaiton true, then immediately call Connect() before the connection has closed. + virtual ConnectionAttemptResult Connect( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey=0, unsigned connectionSocketIndex=0, unsigned sendConnectionAttemptCount=12, unsigned timeBetweenSendConnectionAttemptsMS=500, RakNet::TimeMS timeoutTime=0 )=0; + + /// \brief Connect to the specified host (ip or domain name) and server port, using a shared socket from another instance of RakNet + /// \param[in] host Either a dotted IP address or a domain name + /// \param[in] remotePort Which port to connect to on the remote machine. + /// \param[in] passwordData A data block that must match the data block on the server passed to SetIncomingPassword. This can be a string or can be a stream of data. Use 0 for no password. + /// \param[in] passwordDataLength The length in bytes of passwordData + /// \param[in] socket A bound socket returned by another instance of RakPeerInterface + /// \param[in] sendConnectionAttemptCount How many datagrams to send to the other system to try to connect. + /// \param[in] timeBetweenSendConnectionAttemptsMS Time to elapse before a datagram is sent to the other system to try to connect. After sendConnectionAttemptCount number of attempts, ID_CONNECTION_ATTEMPT_FAILED is returned. Under low bandwidth conditions with multiple simultaneous outgoing connections, this value should be raised to 1000 or higher, or else the MTU detection can overrun the available bandwidth. + /// \param[in] timeoutTime How long to keep the connection alive before dropping it on unable to send a reliable message. 0 to use the default from SetTimeoutTime(UNASSIGNED_SYSTEM_ADDRESS); + /// \return CONNECTION_ATTEMPT_STARTED on successful initiation. Otherwise, an appropriate enumeration indicating failure. + /// \note CONNECTION_ATTEMPT_STARTED does not mean you are already connected! + virtual ConnectionAttemptResult ConnectWithSocket(const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, RakNetSocket2* socket, PublicKey *publicKey=0, unsigned sendConnectionAttemptCount=12, unsigned timeBetweenSendConnectionAttemptsMS=500, RakNet::TimeMS timeoutTime=0)=0; + + /// \brief Connect to the specified network ID (Platform specific console function) + /// \details Does built-in NAt traversal + /// \param[in] passwordData A data block that must match the data block on the server passed to SetIncomingPassword. This can be a string or can be a stream of data. Use 0 for no password. + /// \param[in] passwordDataLength The length in bytes of passwordData + //virtual bool Console2LobbyConnect( void *networkServiceId, const char *passwordData, int passwordDataLength )=0; + + /// \brief Stops the network threads and closes all connections. + /// \param[in] blockDuration How long, in milliseconds, you should wait for all remaining messages to go out, including ID_DISCONNECTION_NOTIFICATION. If 0, it doesn't wait at all. + /// \param[in] orderingChannel If blockDuration > 0, ID_DISCONNECTION_NOTIFICATION will be sent on this channel + /// \param[in] disconnectionNotificationPriority Priority to send ID_DISCONNECTION_NOTIFICATION on. + /// If you set it to 0 then the disconnection notification won't be sent + virtual void Shutdown( unsigned int blockDuration, unsigned char orderingChannel=0, PacketPriority disconnectionNotificationPriority=LOW_PRIORITY )=0; + + /// Returns if the network thread is running + /// \return true if the network thread is running, false otherwise + virtual bool IsActive( void ) const=0; + + /// Fills the array remoteSystems with the SystemAddress of all the systems we are connected to + /// \param[out] remoteSystems An array of SystemAddress structures to be filled with the SystemAddresss of the systems we are connected to. Pass 0 to remoteSystems to only get the number of systems we are connected to + /// \param[in, out] numberOfSystems As input, the size of remoteSystems array. As output, the number of elements put into the array + virtual bool GetConnectionList( SystemAddress *remoteSystems, unsigned short *numberOfSystems ) const=0; + + /// Returns the next uint32_t that Send() will return + /// \note If using RakPeer from multiple threads, this may not be accurate for your thread. Use IncrementNextSendReceipt() in that case. + /// \return The next uint32_t that Send() or SendList will return + virtual uint32_t GetNextSendReceipt(void)=0; + + /// Returns the next uint32_t that Send() will return, and increments the value by one + /// \note If using RakPeer from multiple threads, pass this to forceReceipt in the send function + /// \return The next uint32_t that Send() or SendList will return + virtual uint32_t IncrementNextSendReceipt(void)=0; + + /// Sends a block of data to the specified system that you are connected to. + /// This function only works while connected + /// The first byte should be a message identifier starting at ID_USER_PACKET_ENUM + /// \param[in] data The block of data to send + /// \param[in] length The size in bytes of the data to send + /// \param[in] priority What priority level to send on. See PacketPriority.h + /// \param[in] reliability How reliability to send this data. See PacketPriority.h + /// \param[in] orderingChannel When using ordered or sequenced messages, what channel to order these on. Messages are only ordered relative to other messages on the same stream + /// \param[in] systemIdentifier Who to send this packet to, or in the case of broadcasting who not to send it to. Pass either a SystemAddress structure or a RakNetGUID structure. Use UNASSIGNED_SYSTEM_ADDRESS or to specify none + /// \param[in] broadcast True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. + /// \param[in] forceReceipt If 0, will automatically determine the receipt number to return. If non-zero, will return what you give it. + /// \return 0 on bad input. Otherwise a number that identifies this message. If \a reliability is a type that returns a receipt, on a later call to Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS with bytes 1-4 inclusive containing this number + virtual uint32_t Send( const char *data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 )=0; + + /// "Send" to yourself rather than a remote system. The message will be processed through the plugins and returned to the game as usual + /// This function works anytime + /// The first byte should be a message identifier starting at ID_USER_PACKET_ENUM + /// \param[in] data The block of data to send + /// \param[in] length The size in bytes of the data to send + virtual void SendLoopback( const char *data, const int length )=0; + + /// Sends a block of data to the specified system that you are connected to. Same as the above version, but takes a BitStream as input. + /// \param[in] bitStream The bitstream to send + /// \param[in] priority What priority level to send on. See PacketPriority.h + /// \param[in] reliability How reliability to send this data. See PacketPriority.h + /// \param[in] orderingChannel When using ordered or sequenced messages, what channel to order these on. Messages are only ordered relative to other messages on the same stream + /// \param[in] systemIdentifier Who to send this packet to, or in the case of broadcasting who not to send it to. Pass either a SystemAddress structure or a RakNetGUID structure. Use UNASSIGNED_SYSTEM_ADDRESS or to specify none + /// \param[in] broadcast True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. + /// \param[in] forceReceipt If 0, will automatically determine the receipt number to return. If non-zero, will return what you give it. + /// \return 0 on bad input. Otherwise a number that identifies this message. If \a reliability is a type that returns a receipt, on a later call to Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS with bytes 1-4 inclusive containing this number + /// \note COMMON MISTAKE: When writing the first byte, bitStream->Write((unsigned char) ID_MY_TYPE) be sure it is casted to a byte, and you are not writing a 4 byte enumeration. + virtual uint32_t Send( const RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 )=0; + + /// Sends multiple blocks of data, concatenating them automatically. + /// + /// This is equivalent to: + /// RakNet::BitStream bs; + /// bs.WriteAlignedBytes(block1, blockLength1); + /// bs.WriteAlignedBytes(block2, blockLength2); + /// bs.WriteAlignedBytes(block3, blockLength3); + /// Send(&bs, ...) + /// + /// This function only works while connected + /// \param[in] data An array of pointers to blocks of data + /// \param[in] lengths An array of integers indicating the length of each block of data + /// \param[in] numParameters Length of the arrays data and lengths + /// \param[in] priority What priority level to send on. See PacketPriority.h + /// \param[in] reliability How reliability to send this data. See PacketPriority.h + /// \param[in] orderingChannel When using ordered or sequenced messages, what channel to order these on. Messages are only ordered relative to other messages on the same stream + /// \param[in] systemIdentifier Who to send this packet to, or in the case of broadcasting who not to send it to. Pass either a SystemAddress structure or a RakNetGUID structure. Use UNASSIGNED_SYSTEM_ADDRESS or to specify none + /// \param[in] broadcast True to send this packet to all connected systems. If true, then systemAddress specifies who not to send the packet to. + /// \param[in] forceReceipt If 0, will automatically determine the receipt number to return. If non-zero, will return what you give it. + /// \return 0 on bad input. Otherwise a number that identifies this message. If \a reliability is a type that returns a receipt, on a later call to Receive() you will get ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS with bytes 1-4 inclusive containing this number + virtual uint32_t SendList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 )=0; + + /// Gets a message from the incoming message queue. + /// Use DeallocatePacket() to deallocate the message after you are done with it. + /// User-thread functions, such as RPC calls and the plugin function PluginInterface::Update occur here. + /// \return 0 if no packets are waiting to be handled, otherwise a pointer to a packet. + /// \note COMMON MISTAKE: Be sure to call this in a loop, once per game tick, until it returns 0. If you only process one packet per game tick they will buffer up. + /// sa RakNetTypes.h contains struct Packet + virtual Packet* Receive( void )=0; + + /// Call this to deallocate a message returned by Receive() when you are done handling it. + /// \param[in] packet The message to deallocate. + virtual void DeallocatePacket( Packet *packet )=0; + + /// Return the total number of connections we are allowed + virtual unsigned int GetMaximumNumberOfPeers( void ) const=0; + + // -------------------------------------------------------------------------------------------- Connection Management Functions-------------------------------------------------------------------------------------------- + /// Close the connection to another host (if we initiated the connection it will disconnect, if they did it will kick them out). + /// \param[in] target Which system to close the connection to. + /// \param[in] sendDisconnectionNotification True to send ID_DISCONNECTION_NOTIFICATION to the recipient. False to close it silently. + /// \param[in] channel Which ordering channel to send the disconnection notification on, if any + /// \param[in] disconnectionNotificationPriority Priority to send ID_DISCONNECTION_NOTIFICATION on. + virtual void CloseConnection( const AddressOrGUID target, bool sendDisconnectionNotification, unsigned char orderingChannel=0, PacketPriority disconnectionNotificationPriority=LOW_PRIORITY )=0; + + /// Returns if a system is connected, disconnected, connecting in progress, or various other states + /// \param[in] systemIdentifier The system we are referring to + /// \note This locks a mutex, do not call too frequently during connection attempts or the attempt will take longer and possibly even timeout + /// \return What state the remote system is in + virtual ConnectionState GetConnectionState(const AddressOrGUID systemIdentifier)=0; + + /// Cancel a pending connection attempt + /// If we are already connected, the connection stays open + /// \param[in] target Which system to cancel + virtual void CancelConnectionAttempt( const SystemAddress target )=0; + + /// Given a systemAddress, returns an index from 0 to the maximum number of players allowed - 1. + /// \param[in] systemAddress The SystemAddress we are referring to + /// \return The index of this SystemAddress or -1 on system not found. + virtual int GetIndexFromSystemAddress( const SystemAddress systemAddress ) const=0; + + /// This function is only useful for looping through all systems + /// Given an index, will return a SystemAddress. + /// \param[in] index Index should range between 0 and the maximum number of players allowed - 1. + /// \return The SystemAddress + virtual SystemAddress GetSystemAddressFromIndex( unsigned int index )=0; + + /// Same as GetSystemAddressFromIndex but returns RakNetGUID + /// \param[in] index Index should range between 0 and the maximum number of players allowed - 1. + /// \return The RakNetGUID + virtual RakNetGUID GetGUIDFromIndex( unsigned int index )=0; + + /// Same as calling GetSystemAddressFromIndex and GetGUIDFromIndex for all systems, but more efficient + /// Indices match each other, so \a addresses[0] and \a guids[0] refer to the same system + /// \param[out] addresses All system addresses. Size of the list is the number of connections. Size of the list will match the size of the \a guids list. + /// \param[out] guids All guids. Size of the list is the number of connections. Size of the list will match the size of the \a addresses list. + virtual void GetSystemList(DataStructures::List &addresses, DataStructures::List &guids) const=0; + + /// Bans an IP from connecting. Banned IPs persist between connections but are not saved on shutdown nor loaded on startup. + /// param[in] IP Dotted IP address. Can use * as a wildcard, such as 128.0.0.* will ban all IP addresses starting with 128.0.0 + /// \param[in] milliseconds how many ms for a temporary ban. Use 0 for a permanent ban + virtual void AddToBanList( const char *IP, RakNet::TimeMS milliseconds=0 )=0; + + /// Allows a previously banned IP to connect. + /// param[in] Dotted IP address. Can use * as a wildcard, such as 128.0.0.* will banAll IP addresses starting with 128.0.0 + virtual void RemoveFromBanList( const char *IP )=0; + + /// Allows all previously banned IPs to connect. + virtual void ClearBanList( void )=0; + + /// Returns true or false indicating if a particular IP is banned. + /// \param[in] IP - Dotted IP address. + /// \return true if IP matches any IPs in the ban list, accounting for any wildcards. False otherwise. + virtual bool IsBanned( const char *IP )=0; + + /// Enable or disable allowing frequent connections from the same IP adderss + /// This is a security measure which is disabled by default, but can be set to true to prevent attackers from using up all connection slots + /// \param[in] b True to limit connections from the same ip to at most 1 per 100 milliseconds. + virtual void SetLimitIPConnectionFrequency(bool b)=0; + + // --------------------------------------------------------------------------------------------Pinging Functions - Functions dealing with the automatic ping mechanism-------------------------------------------------------------------------------------------- + /// Send a ping to the specified connected system. + /// \pre The sender and recipient must already be started via a successful call to Startup() + /// \param[in] target Which system to ping + virtual void Ping( const SystemAddress target )=0; + + /// Send a ping to the specified unconnected system. The remote system, if it is Initialized, will respond with ID_PONG followed by sizeof(RakNet::TimeMS) containing the system time the ping was sent.(Default is 4 bytes - See __GET_TIME_64BIT in RakNetTypes.h + /// System should reply with ID_PONG if it is active + /// \param[in] host Either a dotted IP address or a domain name. Can be 255.255.255.255 for LAN broadcast. + /// \param[in] remotePort Which port to connect to on the remote machine. + /// \param[in] onlyReplyOnAcceptingConnections Only request a reply if the remote system is accepting connections + /// \param[in] connectionSocketIndex Index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to send on. + /// \return true on success, false on failure (unknown hostname) + virtual bool Ping( const char* host, unsigned short remotePort, bool onlyReplyOnAcceptingConnections, unsigned connectionSocketIndex=0 )=0; + + /// Returns the average of all ping times read for the specific system or -1 if none read yet + /// \param[in] systemAddress Which system we are referring to + /// \return The ping time for this system, or -1 + virtual int GetAveragePing( const AddressOrGUID systemIdentifier )=0; + + /// Returns the last ping time read for the specific system or -1 if none read yet + /// \param[in] systemAddress Which system we are referring to + /// \return The last ping time for this system, or -1 + virtual int GetLastPing( const AddressOrGUID systemIdentifier ) const=0; + + /// Returns the lowest ping time read or -1 if none read yet + /// \param[in] systemAddress Which system we are referring to + /// \return The lowest ping time for this system, or -1 + virtual int GetLowestPing( const AddressOrGUID systemIdentifier ) const=0; + + /// Ping the remote systems every so often, or not. Can be called anytime. + /// By default this is true. Recommended to leave on, because congestion control uses it to determine how often to resend lost packets. + /// It would be true by default to prevent timestamp drift, since in the event of a clock spike, the timestamp deltas would no longer be accurate + /// \param[in] doPing True to start occasional pings. False to stop them. + virtual void SetOccasionalPing( bool doPing )=0; + + /// Return the clock difference between your system and the specified system + /// Subtract GetClockDifferential() from a time returned by the remote system to get that time relative to your own system + /// Returns 0 if the system is unknown + /// \param[in] systemIdentifier Which system we are referring to + virtual RakNet::Time GetClockDifferential( const AddressOrGUID systemIdentifier )=0; + + // --------------------------------------------------------------------------------------------Static Data Functions - Functions dealing with API defined synchronized memory-------------------------------------------------------------------------------------------- + /// Sets the data to send along with a LAN server discovery or offline ping reply. + /// \a length should be under 400 bytes, as a security measure against flood attacks + /// \param[in] data a block of data to store, or 0 for none + /// \param[in] length The length of data in bytes, or 0 for none + /// \sa Ping.cpp + virtual void SetOfflinePingResponse( const char *data, const unsigned int length )=0; + + /// Returns pointers to a copy of the data passed to SetOfflinePingResponse + /// \param[out] data A pointer to a copy of the data passed to \a SetOfflinePingResponse() + /// \param[out] length A pointer filled in with the length parameter passed to SetOfflinePingResponse() + /// \sa SetOfflinePingResponse + virtual void GetOfflinePingResponse( char **data, unsigned int *length )=0; + + //--------------------------------------------------------------------------------------------Network Functions - Functions dealing with the network in general-------------------------------------------------------------------------------------------- + /// Return the unique address identifier that represents you or another system on the the network and is based on your local IP / port. + /// \note Not supported by the XBOX + /// \param[in] systemAddress Use UNASSIGNED_SYSTEM_ADDRESS to get your behind-LAN address. Use a connected system to get their behind-LAN address + /// \param[in] index When you have multiple internal IDs, which index to return? Currently limited to MAXIMUM_NUMBER_OF_INTERNAL_IDS (so the maximum value of this variable is MAXIMUM_NUMBER_OF_INTERNAL_IDS-1) + /// \return the identifier of your system internally, which may not be how other systems see if you if you are behind a NAT or proxy + virtual SystemAddress GetInternalID( const SystemAddress systemAddress=UNASSIGNED_SYSTEM_ADDRESS, const int index=0 ) const=0; + + /// \brief Sets your internal IP address, for platforms that do not support reading it, or to override a value + /// \param[in] systemAddress. The address to set. Use SystemAddress::FromString() if you want to use a dotted string + /// \param[in] index When you have multiple internal IDs, which index to set? + virtual void SetInternalID(SystemAddress systemAddress, int index=0)=0; + + /// Return the unique address identifier that represents you on the the network and is based on your externalIP / port + /// (the IP / port the specified player uses to communicate with you) + /// \param[in] target Which remote system you are referring to for your external ID. Usually the same for all systems, unless you have two or more network cards. + virtual SystemAddress GetExternalID( const SystemAddress target ) const=0; + + /// Return my own GUID + virtual const RakNetGUID GetMyGUID(void) const=0; + + /// Return the address bound to a socket at the specified index + virtual SystemAddress GetMyBoundAddress(const int socketIndex=0)=0; + + /// Get a random number (to generate a GUID) + static uint64_t Get64BitUniqueRandomNumber(void); + + /// Given a connected system, give us the unique GUID representing that instance of RakPeer. + /// This will be the same on all systems connected to that instance of RakPeer, even if the external system addresses are different + /// Currently O(log(n)), but this may be improved in the future. If you use this frequently, you may want to cache the value as it won't change. + /// Returns UNASSIGNED_RAKNET_GUID if system address can't be found. + /// If \a input is UNASSIGNED_SYSTEM_ADDRESS, will return your own GUID + /// \pre Call Startup() first, or the function will return UNASSIGNED_RAKNET_GUID + /// \param[in] input The system address of the system we are connected to + virtual const RakNetGUID& GetGuidFromSystemAddress( const SystemAddress input ) const=0; + + /// Given the GUID of a connected system, give us the system address of that system. + /// The GUID will be the same on all systems connected to that instance of RakPeer, even if the external system addresses are different + /// Currently O(log(n)), but this may be improved in the future. If you use this frequently, you may want to cache the value as it won't change. + /// If \a input is UNASSIGNED_RAKNET_GUID, will return UNASSIGNED_SYSTEM_ADDRESS + /// \param[in] input The RakNetGUID of the system we are checking to see if we are connected to + virtual SystemAddress GetSystemAddressFromGuid( const RakNetGUID input ) const=0; + + /// Given the SystemAddress of a connected system, get the public key they provided as an identity + /// Returns false if system address was not found or client public key is not known + /// \param[in] input The RakNetGUID of the system + /// \param[in] client_public_key The connected client's public key is copied to this address. Buffer must be cat::EasyHandshake::PUBLIC_KEY_BYTES bytes in length. + virtual bool GetClientPublicKeyFromSystemAddress( const SystemAddress input, char *client_public_key ) const=0; + + /// Set the time, in MS, to use before considering ourselves disconnected after not being able to deliver a reliable message. + /// Default time is 10,000 or 10 seconds in release and 30,000 or 30 seconds in debug. + /// Do not set different values for different computers that are connected to each other, or you won't be able to reconnect after ID_CONNECTION_LOST + /// \param[in] timeMS Time, in MS + /// \param[in] target Which system to do this for. Pass UNASSIGNED_SYSTEM_ADDRESS for all systems. + virtual void SetTimeoutTime( RakNet::TimeMS timeMS, const SystemAddress target )=0; + + /// \param[in] target Which system to do this for. Pass UNASSIGNED_SYSTEM_ADDRESS to get the default value + /// \return timeoutTime for a given system. + virtual RakNet::TimeMS GetTimeoutTime( const SystemAddress target )=0; + + /// Returns the current MTU size + /// \param[in] target Which system to get this for. UNASSIGNED_SYSTEM_ADDRESS to get the default + /// \return The current MTU size + virtual int GetMTUSize( const SystemAddress target ) const=0; + + /// Returns the number of IP addresses this system has internally. Get the actual addresses from GetLocalIP() + virtual unsigned GetNumberOfAddresses( void )=0; + + /// Returns an IP address at index 0 to GetNumberOfAddresses-1 + /// \param[in] index index into the list of IP addresses + /// \return The local IP address at this index + virtual const char* GetLocalIP( unsigned int index )=0; + + /// Is this a local IP? + /// \param[in] An IP address to check, excluding the port + /// \return True if this is one of the IP addresses returned by GetLocalIP + virtual bool IsLocalIP( const char *ip )=0; + + /// Allow or disallow connection responses from any IP. Normally this should be false, but may be necessary + /// when connecting to servers with multiple IP addresses. + /// \param[in] allow - True to allow this behavior, false to not allow. Defaults to false. Value persists between connections + virtual void AllowConnectionResponseIPMigration( bool allow )=0; + + /// Sends a one byte message ID_ADVERTISE_SYSTEM to the remote unconnected system. + /// This will tell the remote system our external IP outside the LAN along with some user data. + /// \pre The sender and recipient must already be started via a successful call to Initialize + /// \param[in] host Either a dotted IP address or a domain name + /// \param[in] remotePort Which port to connect to on the remote machine. + /// \param[in] data Optional data to append to the packet. + /// \param[in] dataLength length of data in bytes. Use 0 if no data. + /// \param[in] connectionSocketIndex Index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to send on. + /// \return false if IsActive()==false or the host is unresolvable. True otherwise + virtual bool AdvertiseSystem( const char *host, unsigned short remotePort, const char *data, int dataLength, unsigned connectionSocketIndex=0 )=0; + + /// Controls how often to return ID_DOWNLOAD_PROGRESS for large message downloads. + /// ID_DOWNLOAD_PROGRESS is returned to indicate a new partial message chunk, roughly the MTU size, has arrived + /// As it can be slow or cumbersome to get this notification for every chunk, you can set the interval at which it is returned. + /// Defaults to 0 (never return this notification) + /// \param[in] interval How many messages to use as an interval + virtual void SetSplitMessageProgressInterval(int interval)=0; + + /// Returns what was passed to SetSplitMessageProgressInterval() + /// \return What was passed to SetSplitMessageProgressInterval(). Default to 0. + virtual int GetSplitMessageProgressInterval(void) const=0; + + /// Set how long to wait before giving up on sending an unreliable message + /// Useful if the network is clogged up. + /// Set to 0 or less to never timeout. Defaults to 0. + /// \param[in] timeoutMS How many ms to wait before simply not sending an unreliable message. + virtual void SetUnreliableTimeout(RakNet::TimeMS timeoutMS)=0; + + /// Send a message to host, with the IP socket option TTL set to 3 + /// This message will not reach the host, but will open the router. + /// Used for NAT-Punchthrough + virtual void SendTTL( const char* host, unsigned short remotePort, int ttl, unsigned connectionSocketIndex=0 )=0; + + // -------------------------------------------------------------------------------------------- Plugin Functions-------------------------------------------------------------------------------------------- + /// \brief Attaches a Plugin interface to an instance of the base class (RakPeer or PacketizedTCP) to run code automatically on message receipt in the Receive call. + /// If the plugin returns false from PluginInterface::UsesReliabilityLayer(), which is the case for all plugins except PacketLogger, you can call AttachPlugin() and DetachPlugin() for this plugin while RakPeer is active. + /// \param[in] messageHandler Pointer to the plugin to attach. + virtual void AttachPlugin( PluginInterface2 *plugin )=0; + + /// \brief Detaches a Plugin interface from the instance of the base class (RakPeer or PacketizedTCP) it is attached to. + /// \details This method disables the plugin code from running automatically on base class's updates or message receipt. + /// If the plugin returns false from PluginInterface::UsesReliabilityLayer(), which is the case for all plugins except PacketLogger, you can call AttachPlugin() and DetachPlugin() for this plugin while RakPeer is active. + /// \param[in] messageHandler Pointer to a plugin to detach. + virtual void DetachPlugin( PluginInterface2 *messageHandler )=0; + + // --------------------------------------------------------------------------------------------Miscellaneous Functions-------------------------------------------------------------------------------------------- + /// Put a message back at the end of the receive queue in case you don't want to deal with it immediately + /// \param[in] packet The packet you want to push back. + /// \param[in] pushAtHead True to push the packet so that the next receive call returns it. False to push it at the end of the queue (obviously pushing it at the end makes the packets out of order) + virtual void PushBackPacket( Packet *packet, bool pushAtHead )=0; + + /// \internal + /// \brief For a given system identified by \a guid, change the SystemAddress to send to. + /// \param[in] guid The connection we are referring to + /// \param[in] systemAddress The new address to send to + virtual void ChangeSystemAddress(RakNetGUID guid, const SystemAddress &systemAddress)=0; + + /// \returns a packet for you to write to if you want to create a Packet for some reason. + /// You can add it to the receive buffer with PushBackPacket + /// \param[in] dataSize How many bytes to allocate for the buffer + /// \return A packet you can write to + virtual Packet* AllocatePacket(unsigned dataSize)=0; + + /// Get the socket used with a particular active connection + /// The smart pointer reference counts the RakNetSocket2 object, so the socket will remain active as long as the smart pointer does, even if RakNet were to shutdown or close the connection. + /// \note This sends a query to the thread and blocks on the return value for up to one second. In practice it should only take a millisecond or so. + /// \param[in] target Which system + /// \return A smart pointer object containing the socket information about the socket. Be sure to check IsNull() which is returned if the update thread is unresponsive, shutting down, or if this system is not connected + virtual RakNetSocket2* GetSocket( const SystemAddress target )=0; + + /// Get all sockets in use + /// \note This sends a query to the thread and blocks on the return value for up to one second. In practice it should only take a millisecond or so. + /// \param[out] sockets List of RakNetSocket2 structures in use. Sockets will not be closed until \a sockets goes out of scope + virtual void GetSockets( DataStructures::List &sockets )=0; + virtual void ReleaseSockets( DataStructures::List &sockets )=0; + + virtual void WriteOutOfBandHeader(RakNet::BitStream *bitStream)=0; + + /// If you need code to run in the same thread as RakNet's update thread, this function can be used for that + /// \param[in] _userUpdateThreadPtr C callback function + /// \param[in] _userUpdateThreadData Passed to C callback function + virtual void SetUserUpdateThread(void (*_userUpdateThreadPtr)(RakPeerInterface *, void *), void *_userUpdateThreadData)=0; + + /// Set a C callback to be called whenever a datagram arrives + /// Return true from the callback to have RakPeer handle the datagram. Return false and RakPeer will ignore the datagram. + /// This can be used to filter incoming datagrams by system, or to share a recvfrom socket with RakPeer + /// RNS2RecvStruct will only remain valid for the duration of the call + /// If the incoming datagram is not from your game at all, it is a RakNet packet. + /// If the incoming datagram has an IP address that matches a known address from your game, then check the first byte of data. + /// For RakNet connected systems, the first bit is always 1. So for your own game packets, make sure the first bit is always 0. + virtual void SetIncomingDatagramEventHandler( bool (*_incomingDatagramEventHandler)(RNS2RecvStruct *) )=0; + + // --------------------------------------------------------------------------------------------Network Simulator Functions-------------------------------------------------------------------------------------------- + /// Adds simulated ping and packet loss to the outgoing data flow. + /// To simulate bi-directional ping and packet loss, you should call this on both the sender and the recipient, with half the total ping and packetloss value on each. + /// You can exclude network simulator code with the _RELEASE #define to decrease code size + /// \deprecated Use http://www.jenkinssoftware.com/forum/index.php?topic=1671.0 instead. + /// \note Doesn't work past version 3.6201 + /// \param[in] packetloss Chance to lose a packet. Ranges from 0 to 1. + /// \param[in] minExtraPing The minimum time to delay sends. + /// \param[in] extraPingVariance The additional random time to delay sends. + virtual void ApplyNetworkSimulator( float packetloss, unsigned short minExtraPing, unsigned short extraPingVariance)=0; + + /// Limits how much outgoing bandwidth can be sent per-connection. + /// This limit does not apply to the sum of all connections! + /// Exceeding the limit queues up outgoing traffic + /// \param[in] maxBitsPerSecond Maximum bits per second to send. Use 0 for unlimited (default). Once set, it takes effect immedately and persists until called again. + virtual void SetPerConnectionOutgoingBandwidthLimit( unsigned maxBitsPerSecond )=0; + + /// Returns if you previously called ApplyNetworkSimulator + /// \return If you previously called ApplyNetworkSimulator + virtual bool IsNetworkSimulatorActive( void )=0; + + // --------------------------------------------------------------------------------------------Statistical Functions - Functions dealing with API performance-------------------------------------------------------------------------------------------- + + /// Returns a structure containing a large set of network statistics for the specified system. + /// You can map this data to a string using the C style StatisticsToString() function + /// \param[in] systemAddress: Which connected system to get statistics for + /// \param[in] rns If you supply this structure, it will be written to it. Otherwise it will use a static struct, which is not threadsafe + /// \return 0 on can't find the specified system. A pointer to a set of data otherwise. + /// \sa RakNetStatistics.h + virtual RakNetStatistics * GetStatistics( const SystemAddress systemAddress, RakNetStatistics *rns=0 )=0; + /// \brief Returns the network statistics of the system at the given index in the remoteSystemList. + /// \return True if the index is less than the maximum number of peers allowed and the system is active. False otherwise. + virtual bool GetStatistics( const unsigned int index, RakNetStatistics *rns )=0; + /// \brief Returns the list of systems, and statistics for each of those systems + /// Each system has one entry in each of the lists, in the same order + /// \param[out] addresses SystemAddress for each connected system + /// \param[out] guids RakNetGUID for each connected system + /// \param[out] statistics Calculated RakNetStatistics for each connected system + virtual void GetStatisticsList(DataStructures::List &addresses, DataStructures::List &guids, DataStructures::List &statistics)=0; + + /// \Returns how many messages are waiting when you call Receive() + virtual unsigned int GetReceiveBufferSize(void)=0; + + // --------------------------------------------------------------------------------------------EVERYTHING AFTER THIS COMMENT IS FOR INTERNAL USE ONLY-------------------------------------------------------------------------------------------- + + /// \internal + // Call manually if RAKPEER_USER_THREADED==1 at least every 30 milliseconds. + // updateBitStream should be: + // BitStream updateBitStream( MAXIMUM_MTU_SIZE + // #if LIBCAT_SECURITY==1 + // + cat::AuthenticatedEncryption::OVERHEAD_BYTES + // #endif + // ); + virtual bool RunUpdateCycle( BitStream &updateBitStream )=0; + + /// \internal + virtual bool SendOutOfBand(const char *host, unsigned short remotePort, const char *data, BitSize_t dataLength, unsigned connectionSocketIndex=0 )=0; + +} +// #if defined(SN_TARGET_PSP2) +// __attribute__((aligned(8))) +// #endif +; + +} // namespace RakNet + diff --git a/Source/RakSleep.cpp b/Source/RakSleep.cpp index 560ba6584..8e75689c0 100644 --- a/Source/RakSleep.cpp +++ b/Source/RakSleep.cpp @@ -1,71 +1,71 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#if defined(_WIN32) -#include "WindowsIncludes.h" // Sleep - - - - - - - -#else -#include -#include -#include -pthread_mutex_t fakeMutex = PTHREAD_MUTEX_INITIALIZER; -pthread_cond_t fakeCond = PTHREAD_COND_INITIALIZER; -#endif - -#include "RakSleep.h" - - -#if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) -#include "ThreadEmulation.h" -using namespace ThreadEmulation; -#endif - -void RakSleep(unsigned int ms) -{ -#ifdef _WIN32 - Sleep(ms); - - - - - - - -#else - //Single thread sleep code thanks to Furquan Shaikh, http://somethingswhichidintknow.blogspot.com/2009/09/sleep-in-pthread.html - //Modified slightly from the original - struct timespec timeToWait; - struct timeval now; - int rt; - - gettimeofday(&now,NULL); - - long seconds = ms/1000; - long nanoseconds = (ms - seconds * 1000) * 1000000; - timeToWait.tv_sec = now.tv_sec + seconds; - timeToWait.tv_nsec = now.tv_usec*1000 + nanoseconds; - - if (timeToWait.tv_nsec >= 1000000000) - { - timeToWait.tv_nsec -= 1000000000; - timeToWait.tv_sec++; - } - - pthread_mutex_lock(&fakeMutex); - rt = pthread_cond_timedwait(&fakeCond, &fakeMutex, &timeToWait); - pthread_mutex_unlock(&fakeMutex); -#endif -} +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#if defined(_WIN32) +#include "WindowsIncludes.h" // Sleep + + + + + + + +#else +#include +#include +#include +pthread_mutex_t fakeMutex = PTHREAD_MUTEX_INITIALIZER; +pthread_cond_t fakeCond = PTHREAD_COND_INITIALIZER; +#endif + +#include "RakSleep.h" + + +#if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) +#include "ThreadEmulation.h" +using namespace ThreadEmulation; +#endif + +void RakSleep(unsigned int ms) +{ +#ifdef _WIN32 + Sleep(ms); + + + + + + + +#else + //Single thread sleep code thanks to Furquan Shaikh, http://somethingswhichidintknow.blogspot.com/2009/09/sleep-in-pthread.html + //Modified slightly from the original + struct timespec timeToWait; + struct timeval now; + int rt; + + gettimeofday(&now,nullptr); + + long seconds = ms/1000; + long nanoseconds = (ms - seconds * 1000) * 1000000; + timeToWait.tv_sec = now.tv_sec + seconds; + timeToWait.tv_nsec = now.tv_usec*1000 + nanoseconds; + + if (timeToWait.tv_nsec >= 1000000000) + { + timeToWait.tv_nsec -= 1000000000; + timeToWait.tv_sec++; + } + + pthread_mutex_lock(&fakeMutex); + rt = pthread_cond_timedwait(&fakeCond, &fakeMutex, &timeToWait); + pthread_mutex_unlock(&fakeMutex); +#endif +} diff --git a/Source/RakSleep.h b/Source/RakSleep.h index 8cf1d0c35..e82e0e077 100644 --- a/Source/RakSleep.h +++ b/Source/RakSleep.h @@ -1,18 +1,16 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __RAK_SLEEP_H -#define __RAK_SLEEP_H - -#include "Export.h" - -void RAK_DLL_EXPORT RakSleep(unsigned int ms); - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include "Export.h" + +void RAK_DLL_EXPORT RakSleep(unsigned int ms); + diff --git a/Source/RakString.cpp b/Source/RakString.cpp index 5d8583ecd..35444b88d 100644 --- a/Source/RakString.cpp +++ b/Source/RakString.cpp @@ -1,1687 +1,1691 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "RakString.h" -#include "RakAssert.h" -#include "RakMemoryOverride.h" -#include "BitStream.h" -#include -#include -#include "LinuxStrings.h" -#include "StringCompressor.h" -#include "SimpleMutex.h" -#include -#include "Itoa.h" - -using namespace RakNet; - -//DataStructures::MemoryPool RakString::pool; -RakString::SharedString RakString::emptyString={0,0,0,(char*) "",(char*) ""}; -//RakString::SharedString *RakString::sharedStringFreeList=0; -//unsigned int RakString::sharedStringFreeListAllocationCount=0; -DataStructures::List RakString::freeList; - -class RakStringCleanup -{ -public: - ~RakStringCleanup() - { - RakNet::RakString::FreeMemoryNoMutex(); - } -}; - -static RakStringCleanup cleanup; - -SimpleMutex& GetPoolMutex(void) -{ - static SimpleMutex poolMutex; - return poolMutex; -} - -int RakNet::RakString::RakStringComp( RakString const &key, RakString const &data ) -{ - return key.StrCmp(data); -} - -RakString::RakString() -{ - sharedString=&emptyString; -} -RakString::RakString( RakString::SharedString *_sharedString ) -{ - sharedString=_sharedString; -} -RakString::RakString(char input) -{ - char str[2]; - str[0]=input; - str[1]=0; - Assign(str); -} -RakString::RakString(unsigned char input) -{ - char str[2]; - str[0]=(char) input; - str[1]=0; - Assign(str); -} -RakString::RakString(const unsigned char *format, ...){ - va_list ap; - va_start(ap, format); - Assign((const char*) format,ap); -} -RakString::RakString(const char *format, ...){ - va_list ap; - va_start(ap, format); - Assign(format,ap); -} -RakString::RakString( const RakString & rhs) -{ - if (rhs.sharedString==&emptyString) - { - sharedString=&emptyString; - return; - } - - rhs.sharedString->refCountMutex->Lock(); - if (rhs.sharedString->refCount==0) - { - sharedString=&emptyString; - } - else - { - rhs.sharedString->refCount++; - sharedString=rhs.sharedString; - } - rhs.sharedString->refCountMutex->Unlock(); -} -RakString::~RakString() -{ - Free(); -} -RakString& RakString::operator = ( const RakString& rhs ) -{ - Free(); - if (rhs.sharedString==&emptyString) - return *this; - - rhs.sharedString->refCountMutex->Lock(); - if (rhs.sharedString->refCount==0) - { - sharedString=&emptyString; - } - else - { - sharedString=rhs.sharedString; - sharedString->refCount++; - } - rhs.sharedString->refCountMutex->Unlock(); - return *this; -} -RakString& RakString::operator = ( const char *str ) -{ - Free(); - Assign(str); - return *this; -} -RakString& RakString::operator = ( char *str ) -{ - return operator = ((const char*)str); -} -RakString& RakString::operator = ( const unsigned char *str ) -{ - return operator = ((const char*)str); -} -RakString& RakString::operator = ( char unsigned *str ) -{ - return operator = ((const char*)str); -} -RakString& RakString::operator = ( const char c ) -{ - char buff[2]; - buff[0]=c; - buff[1]=0; - return operator = ((const char*)buff); -} -void RakString::Realloc(SharedString *sharedString, size_t bytes) -{ - if (bytes<=sharedString->bytesUsed) - return; - - RakAssert(bytes>0); - size_t oldBytes = sharedString->bytesUsed; - size_t newBytes; - const size_t smallStringSize = 128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2; - newBytes = GetSizeToAllocate(bytes); - if (oldBytes <=(size_t) smallStringSize && newBytes > (size_t) smallStringSize) - { - sharedString->bigString=(char*) rakMalloc_Ex(newBytes, _FILE_AND_LINE_); - strcpy(sharedString->bigString, sharedString->smallString); - sharedString->c_str=sharedString->bigString; - } - else if (oldBytes > smallStringSize) - { - sharedString->bigString=(char*) rakRealloc_Ex(sharedString->bigString,newBytes, _FILE_AND_LINE_); - sharedString->c_str=sharedString->bigString; - } - sharedString->bytesUsed=newBytes; -} -RakString& RakString::operator +=( const RakString& rhs) -{ - if (rhs.IsEmpty()) - return *this; - - if (IsEmpty()) - { - return operator=(rhs); - } - else - { - Clone(); - size_t strLen=rhs.GetLength()+GetLength()+1; - Realloc(sharedString, strLen+GetLength()); - strcat(sharedString->c_str,rhs.C_String()); - } - return *this; -} -RakString& RakString::operator +=( const char *str ) -{ - if (str==0 || str[0]==0) - return *this; - - if (IsEmpty()) - { - Assign(str); - } - else - { - Clone(); - size_t strLen=strlen(str)+GetLength()+1; - Realloc(sharedString, strLen); - strcat(sharedString->c_str,str); - } - return *this; -} -RakString& RakString::operator +=( char *str ) -{ - return operator += ((const char*)str); -} -RakString& RakString::operator +=( const unsigned char *str ) -{ - return operator += ((const char*)str); -} -RakString& RakString::operator +=( unsigned char *str ) -{ - return operator += ((const char*)str); -} -RakString& RakString::operator +=( const char c ) -{ - char buff[2]; - buff[0]=c; - buff[1]=0; - return operator += ((const char*)buff); -} -unsigned char RakString::operator[] ( const unsigned int position ) const -{ - RakAssert(positionc_str[position]; -} -bool RakString::operator==(const RakString &rhs) const -{ - return strcmp(sharedString->c_str,rhs.sharedString->c_str)==0; -} -bool RakString::operator==(const char *str) const -{ - return strcmp(sharedString->c_str,str)==0; -} -bool RakString::operator==(char *str) const -{ - return strcmp(sharedString->c_str,str)==0; -} -bool RakString::operator < ( const RakString& right ) const -{ - return strcmp(sharedString->c_str,right.C_String()) < 0; -} -bool RakString::operator <= ( const RakString& right ) const -{ - return strcmp(sharedString->c_str,right.C_String()) <= 0; -} -bool RakString::operator > ( const RakString& right ) const -{ - return strcmp(sharedString->c_str,right.C_String()) > 0; -} -bool RakString::operator >= ( const RakString& right ) const -{ - return strcmp(sharedString->c_str,right.C_String()) >= 0; -} -bool RakString::operator!=(const RakString &rhs) const -{ - return strcmp(sharedString->c_str,rhs.sharedString->c_str)!=0; -} -bool RakString::operator!=(const char *str) const -{ - return strcmp(sharedString->c_str,str)!=0; -} -bool RakString::operator!=(char *str) const -{ - return strcmp(sharedString->c_str,str)!=0; -} -const RakNet::RakString operator+(const RakNet::RakString &lhs, const RakNet::RakString &rhs) -{ - if (lhs.IsEmpty() && rhs.IsEmpty()) - { - return RakString(&RakString::emptyString); - } - if (lhs.IsEmpty()) - { - rhs.sharedString->refCountMutex->Lock(); - if (rhs.sharedString->refCount==0) - { - rhs.sharedString->refCountMutex->Unlock(); - lhs.sharedString->refCountMutex->Lock(); - lhs.sharedString->refCount++; - lhs.sharedString->refCountMutex->Unlock(); - return RakString(lhs.sharedString); - } - else - { - rhs.sharedString->refCount++; - rhs.sharedString->refCountMutex->Unlock(); - return RakString(rhs.sharedString); - } - // rhs.sharedString->refCountMutex->Unlock(); - } - if (rhs.IsEmpty()) - { - lhs.sharedString->refCountMutex->Lock(); - lhs.sharedString->refCount++; - lhs.sharedString->refCountMutex->Unlock(); - return RakString(lhs.sharedString); - } - - size_t len1 = lhs.GetLength(); - size_t len2 = rhs.GetLength(); - size_t allocatedBytes = len1 + len2 + 1; - allocatedBytes = RakString::GetSizeToAllocate(allocatedBytes); - RakString::SharedString *sharedString; - - RakString::LockMutex(); - // sharedString = RakString::pool.Allocate( _FILE_AND_LINE_ ); - if (RakString::freeList.Size()==0) - { - //RakString::sharedStringFreeList=(RakString::SharedString*) rakRealloc_Ex(RakString::sharedStringFreeList,(RakString::sharedStringFreeListAllocationCount+1024)*sizeof(RakString::SharedString), _FILE_AND_LINE_); - unsigned i; - for (i=0; i < 128; i++) - { - // RakString::freeList.Insert(RakString::sharedStringFreeList+i+RakString::sharedStringFreeListAllocationCount); - RakString::SharedString *ss; - ss = (RakString::SharedString*) rakMalloc_Ex(sizeof(RakString::SharedString), _FILE_AND_LINE_); - ss->refCountMutex=RakNet::OP_NEW(_FILE_AND_LINE_); - RakString::freeList.Insert(ss, _FILE_AND_LINE_); - - } - //RakString::sharedStringFreeListAllocationCount+=1024; - } - sharedString = RakString::freeList[RakString::freeList.Size()-1]; - RakString::freeList.RemoveAtIndex(RakString::freeList.Size()-1); - RakString::UnlockMutex(); - - const int smallStringSize = 128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2; - sharedString->bytesUsed=allocatedBytes; - sharedString->refCount=1; - if (allocatedBytes <= (size_t) smallStringSize) - { - sharedString->c_str=sharedString->smallString; - } - else - { - sharedString->bigString=(char*)rakMalloc_Ex(sharedString->bytesUsed, _FILE_AND_LINE_); - sharedString->c_str=sharedString->bigString; - } - - strcpy(sharedString->c_str, lhs); - strcat(sharedString->c_str, rhs); - - return RakString(sharedString); -} -const char * RakString::ToLower(void) -{ - Clone(); - - size_t strLen = strlen(sharedString->c_str); - unsigned i; - for (i=0; i < strLen; i++) - sharedString->c_str[i]=ToLower(sharedString->c_str[i]); - return sharedString->c_str; -} -const char * RakString::ToUpper(void) -{ - Clone(); - - size_t strLen = strlen(sharedString->c_str); - unsigned i; - for (i=0; i < strLen; i++) - sharedString->c_str[i]=ToUpper(sharedString->c_str[i]); - return sharedString->c_str; -} -void RakString::Set(const char *format, ...) -{ - va_list ap; - va_start(ap, format); - Clear(); - Assign(format,ap); -} -bool RakString::IsEmpty(void) const -{ - return sharedString==&emptyString; -} -size_t RakString::GetLength(void) const -{ - return strlen(sharedString->c_str); -} -// http://porg.es/blog/counting-characters-in-utf-8-strings-is-faster -int porges_strlen2(char *s) -{ - int i = 0; - int iBefore = 0; - int count = 0; - - while (s[i] > 0) -ascii: i++; - - count += i-iBefore; - while (s[i]) - { - if (s[i] > 0) - { - iBefore = i; - goto ascii; - } - else - switch (0xF0 & s[i]) - { - case 0xE0: i += 3; break; - case 0xF0: i += 4; break; - default: i += 2; break; - } - ++count; - } - return count; -} -size_t RakString::GetLengthUTF8(void) const -{ - return porges_strlen2(sharedString->c_str); -} -void RakString::Replace(unsigned index, unsigned count, unsigned char c) -{ - RakAssert(index+count < GetLength()); - Clone(); - unsigned countIndex=0; - while (countIndexc_str[index]=c; - index++; - countIndex++; - } - -} -void RakString::SetChar( unsigned index, unsigned char c ) -{ - RakAssert(index < GetLength()); - Clone(); - sharedString->c_str[index]=c; -} -void RakString::SetChar( unsigned index, RakNet::RakString s ) -{ - RakAssert(index < GetLength()); - Clone(); - RakNet::RakString firstHalf = SubStr(0, index); - RakNet::RakString secondHalf = SubStr(index+1, (unsigned int)-1); - *this = firstHalf; - *this += s; - *this += secondHalf; -} - -#ifdef _WIN32 -WCHAR * RakString::ToWideChar(void) -{ - // - // Special case of NULL or empty input string - // - if ( (sharedString->c_str == NULL) || (*sharedString->c_str == '\0') ) - { - // Return empty string - return L""; - } - - // - // Get size of destination UTF-16 buffer, in WCHAR's - // - int cchUTF16 = ::MultiByteToWideChar( - CP_UTF8, // convert from UTF-8 - 0, // Flags - sharedString->c_str, // source UTF-8 string - GetLength()+1, // total length of source UTF-8 string, - // in CHAR's (= bytes), including end-of-string \0 - NULL, // unused - no conversion done in this step - 0 // request size of destination buffer, in WCHAR's - ); - - if ( cchUTF16 == 0 ) - { - RakAssert("RakString::ToWideChar exception from cchUTF16==0" && 0); - return 0; - } - - // - // Allocate destination buffer to store UTF-16 string - // - WCHAR * pszUTF16 = RakNet::OP_NEW_ARRAY(cchUTF16,__FILE__,__LINE__); - - // - // Do the conversion from UTF-8 to UTF-16 - // - int result = ::MultiByteToWideChar( - CP_UTF8, // convert from UTF-8 - 0, // Buffer - sharedString->c_str, // source UTF-8 string - GetLength()+1, // total length of source UTF-8 string, - // in CHAR's (= bytes), including end-of-string \0 - pszUTF16, // destination buffer - cchUTF16 // size of destination buffer, in WCHAR's - ); - - if ( result == 0 ) - { - RakAssert("RakString::ToWideChar exception from MultiByteToWideChar" && 0); - return 0; - } - - return pszUTF16; -} -void RakString::DeallocWideChar(WCHAR * w) -{ - RakNet::OP_DELETE_ARRAY(w,__FILE__,__LINE__); -} -void RakString::FromWideChar(const wchar_t *source) -{ - Clear(); - int bufSize = wcslen(source)*4; - Allocate(bufSize); - WideCharToMultiByte ( CP_ACP, // ANSI code page - - - - WC_COMPOSITECHECK, // Check for accented characters - - source, // Source Unicode string - -1, // -1 means string is zero-terminated - sharedString->c_str, // Destination char string - bufSize, // Size of buffer - NULL, // No default character - NULL ); // Don't care about this flag - - -} -RakNet::RakString RakString::FromWideChar_S(const wchar_t *source) -{ - RakNet::RakString rs; - rs.FromWideChar(source); - return rs; -} -#endif -size_t RakString::Find(const char *stringToFind,size_t pos) -{ - size_t len=GetLength(); - if (pos>=len || stringToFind==0 || stringToFind[0]==0) - { - return (size_t) -1; - } - size_t matchLen= strlen(stringToFind); - size_t matchPos=0; - size_t iStart=0; - - for (size_t i=pos;ic_str[i]) - { - if(matchPos==0) - { - iStart=i; - } - matchPos++; - } - else - { - matchPos=0; - } - - if (matchPos>=matchLen) - { - return iStart; - } - } - - return (size_t) -1; -} - -void RakString::TruncateUTF8(unsigned int length) -{ - int i = 0; - unsigned int count = 0; - - while (sharedString->c_str[i]!=0) - { - if (count==length) - { - sharedString->c_str[i]=0; - return; - } - else if (sharedString->c_str[i]>0) - { - i++; - } - else - { - switch (0xF0 & sharedString->c_str[i]) - { - case 0xE0: i += 3; break; - case 0xF0: i += 4; break; - default: i += 2; break; - } - } - - count++; - } -} - -void RakString::Truncate(unsigned int length) -{ - if (length < GetLength()) - { - SetChar(length, 0); - } -} - -RakString RakString::SubStr(unsigned int index, unsigned int count) const -{ - size_t length = GetLength(); - if (index >= length || count==0) - return RakString(); - RakString copy; - size_t numBytes = length-index; - if (count < numBytes) - numBytes=count; - copy.Allocate(numBytes+1); - size_t i; - for (i=0; i < numBytes; i++) - copy.sharedString->c_str[i]=sharedString->c_str[index+i]; - copy.sharedString->c_str[i]=0; - return copy; -} -void RakString::Erase(unsigned int index, unsigned int count) -{ - size_t len = GetLength(); - RakAssert(index+count <= len); - - Clone(); - unsigned i; - for (i=index; i < len-count; i++) - { - sharedString->c_str[i]=sharedString->c_str[i+count]; - } - sharedString->c_str[i]=0; -} -void RakString::TerminateAtLastCharacter(char c) -{ - int i, len=(int) GetLength(); - for (i=len-1; i >= 0; i--) - { - if (sharedString->c_str[i]==c) - { - Clone(); - sharedString->c_str[i]=0; - return; - } - } -} -void RakString::StartAfterLastCharacter(char c) -{ - int i, len=(int) GetLength(); - for (i=len-1; i >= 0; i--) - { - if (sharedString->c_str[i]==c) - { - ++i; - if (i < len) - { - *this = SubStr(i,GetLength()-i); - } - return; - } - } -} -void RakString::TerminateAtFirstCharacter(char c) -{ - unsigned int i, len=(unsigned int) GetLength(); - for (i=0; i < len; i++) - { - if (sharedString->c_str[i]==c) - { - if (i > 0) - { - Clone(); - sharedString->c_str[i]=0; - } - } - } -} -void RakString::StartAfterFirstCharacter(char c) -{ - unsigned int i, len=(unsigned int) GetLength(); - for (i=0; i < len; i++) - { - if (sharedString->c_str[i]==c) - { - ++i; - if (i < len) - { - *this = SubStr(i,GetLength()-i); - } - return; - } - } -} -int RakString::GetCharacterCount(char c) -{ - int count=0; - unsigned int i, len=(unsigned int) GetLength(); - for (i=0; i < len; i++) - { - if (sharedString->c_str[i]==c) - { - ++count; - } - } - return count; -} -void RakString::RemoveCharacter(char c) -{ - if (c==0) - return; - - unsigned int readIndex, writeIndex=0; - for (readIndex=0; sharedString->c_str[readIndex]; readIndex++) - { - if (sharedString->c_str[readIndex]!=c) - sharedString->c_str[writeIndex++]=sharedString->c_str[readIndex]; - else - Clone(); - } - sharedString->c_str[writeIndex]=0; - if (writeIndex==0) - Clear(); -} -int RakString::StrCmp(const RakString &rhs) const -{ - return strcmp(sharedString->c_str, rhs.C_String()); -} -int RakString::StrNCmp(const RakString &rhs, size_t num) const -{ - return strncmp(sharedString->c_str, rhs.C_String(), num); -} -int RakString::StrICmp(const RakString &rhs) const -{ - return _stricmp(sharedString->c_str, rhs.C_String()); -} -void RakString::Printf(void) -{ - RAKNET_DEBUG_PRINTF("%s", sharedString->c_str); -} -void RakString::FPrintf(FILE *fp) -{ - fprintf(fp,"%s", sharedString->c_str); -} -bool RakString::IPAddressMatch(const char *IP) -{ - unsigned characterIndex; - - if ( IP == 0 || IP[ 0 ] == 0 || strlen( IP ) > 15 ) - return false; - - characterIndex = 0; - -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - while ( true ) - { - if (sharedString->c_str[ characterIndex ] == IP[ characterIndex ] ) - { - // Equal characters - if ( IP[ characterIndex ] == 0 ) - { - // End of the string and the strings match - - return true; - } - - characterIndex++; - } - - else - { - if ( sharedString->c_str[ characterIndex ] == 0 || IP[ characterIndex ] == 0 ) - { - // End of one of the strings - break; - } - - // Characters do not match - if ( sharedString->c_str[ characterIndex ] == '*' ) - { - // Domain is banned. - return true; - } - - // Characters do not match and it is not a * - break; - } - } - - - // No match found. - return false; -} -bool RakString::ContainsNonprintableExceptSpaces(void) const -{ - size_t strLen = strlen(sharedString->c_str); - unsigned i; - for (i=0; i < strLen; i++) - { - if (sharedString->c_str[i] < ' ' || sharedString->c_str[i] >126) - return true; - } - return false; -} -bool RakString::IsEmailAddress(void) const -{ - if (IsEmpty()) - return false; - size_t strLen = strlen(sharedString->c_str); - if (strLen < 6) // a@b.de - return false; - if (sharedString->c_str[strLen-4]!='.' && sharedString->c_str[strLen-3]!='.') // .com, .net., .org, .de - return false; - unsigned i; - // Has non-printable? - for (i=0; i < strLen; i++) - { - if (sharedString->c_str[i] <= ' ' || sharedString->c_str[i] >126) - return false; - } - int atCount=0; - for (i=0; i < strLen; i++) - { - if (sharedString->c_str[i]=='@') - { - atCount++; - } - } - if (atCount!=1) - return false; - int dotCount=0; - for (i=0; i < strLen; i++) - { - if (sharedString->c_str[i]=='.') - { - dotCount++; - } - } - if (dotCount==0) - return false; - - // There's more I could check, but this is good enough - return true; -} -RakNet::RakString& RakString::URLEncode(void) -{ - RakString result; - size_t strLen = strlen(sharedString->c_str); - result.Allocate(strLen*3); - char *output=result.sharedString->c_str; - unsigned int outputIndex=0; - unsigned i; - unsigned char c; - for (i=0; i < strLen; i++) - { - c=sharedString->c_str[i]; - if ( - (c<=47) || - (c>=58 && c<=64) || - (c>=91 && c<=96) || - (c>=123) - ) - { - char buff[3]; - Itoa(c, buff, 16); - output[outputIndex++]='%'; - output[outputIndex++]=buff[0]; - output[outputIndex++]=buff[1]; - } - else - { - output[outputIndex++]=c; - } - } - - output[outputIndex]=0; - - *this = result; - return *this; -} -RakNet::RakString& RakString::URLDecode(void) -{ - RakString result; - size_t strLen = strlen(sharedString->c_str); - result.Allocate(strLen); - char *output=result.sharedString->c_str; - unsigned int outputIndex=0; - char c; - char hexDigits[2]; - char hexValues[2]; - unsigned int i; - for (i=0; i < strLen; i++) - { - c=sharedString->c_str[i]; - if (c=='%') - { - hexDigits[0]=sharedString->c_str[++i]; - hexDigits[1]=sharedString->c_str[++i]; - - if (hexDigits[0]==' ') - hexValues[0]=0; - - if (hexDigits[0]>='A' && hexDigits[0]<='F') - hexValues[0]=hexDigits[0]-'A'+10; - if (hexDigits[0]>='a' && hexDigits[0]<='f') - hexValues[0]=hexDigits[0]-'a'+10; - else - hexValues[0]=hexDigits[0]-'0'; - - if (hexDigits[1]>='A' && hexDigits[1]<='F') - hexValues[1]=hexDigits[1]-'A'+10; - if (hexDigits[1]>='a' && hexDigits[1]<='f') - hexValues[1]=hexDigits[1]-'a'+10; - else - hexValues[1]=hexDigits[1]-'0'; - - output[outputIndex++]=hexValues[0]*16+hexValues[1]; - } - else - { - output[outputIndex++]=c; - } - } - - output[outputIndex]=0; - - *this = result; - return *this; -} -void RakString::SplitURI(RakNet::RakString &header, RakNet::RakString &domain, RakNet::RakString &path) -{ - header.Clear(); - domain.Clear(); - path.Clear(); - - size_t strLen = strlen(sharedString->c_str); - - char c; - unsigned int i=0; - if (strncmp(sharedString->c_str, "http://", 7)==0) - i+=(unsigned int) strlen("http://"); - else if (strncmp(sharedString->c_str, "https://", 8)==0) - i+=(unsigned int) strlen("https://"); - - if (strncmp(sharedString->c_str, "www.", 4)==0) - i+=(unsigned int) strlen("www."); - - if (i!=0) - { - header.Allocate(i+1); - strncpy(header.sharedString->c_str, sharedString->c_str, i); - header.sharedString->c_str[i]=0; - } - - - domain.Allocate(strLen-i+1); - char *domainOutput=domain.sharedString->c_str; - unsigned int outputIndex=0; - for (; i < strLen; i++) - { - c=sharedString->c_str[i]; - if (c=='/') - { - break; - } - else - { - domainOutput[outputIndex++]=sharedString->c_str[i]; - } - } - - domainOutput[outputIndex]=0; - - path.Allocate(strLen-header.GetLength()-outputIndex+1); - outputIndex=0; - char *pathOutput=path.sharedString->c_str; - for (; i < strLen; i++) - { - pathOutput[outputIndex++]=sharedString->c_str[i]; - } - pathOutput[outputIndex]=0; -} -RakNet::RakString& RakString::SQLEscape(void) -{ - int strLen=(int)GetLength(); - int escapedCharacterCount=0; - int index; - for (index=0; index < strLen; index++) - { - if (sharedString->c_str[index]=='\'' || - sharedString->c_str[index]=='"' || - sharedString->c_str[index]=='\\') - escapedCharacterCount++; - } - if (escapedCharacterCount==0) - return *this; - - Clone(); - Realloc(sharedString, strLen+escapedCharacterCount); - int writeIndex, readIndex; - writeIndex = strLen+escapedCharacterCount; - readIndex=strLen; - while (readIndex>=0) - { - if (sharedString->c_str[readIndex]=='\'' || - sharedString->c_str[readIndex]=='"' || - sharedString->c_str[readIndex]=='\\') - { - sharedString->c_str[writeIndex--]=sharedString->c_str[readIndex--]; - sharedString->c_str[writeIndex--]='\\'; - } - else - { - sharedString->c_str[writeIndex--]=sharedString->c_str[readIndex--]; - } - } - return *this; -} -RakNet::RakString RakString::FormatForPUTOrPost(const char* type, const char* uri, const char* contentType, const char* body, const char* extraHeaders) -{ - RakString out; - RakString host; - RakString remotePath; - RakNet::RakString header; - RakString uriRs; - uriRs = uri; - uriRs.SplitURI(header, host, remotePath); - - if (host.IsEmpty() || remotePath.IsEmpty()) - return out; - -// RakString bodyEncoded = body; -// bodyEncoded.URLEncode(); - - if (extraHeaders!=0 && extraHeaders[0]) - { - out.Set("%s %s HTTP/1.1\r\n" - "%s\r\n" - "Host: %s\r\n" - "Content-Type: %s\r\n" - "Content-Length: %u\r\n" - "\r\n" - "%s", - type, - remotePath.C_String(), - extraHeaders, - host.C_String(), - contentType, - //bodyEncoded.GetLength(), - //bodyEncoded.C_String()); - strlen(body), - body); - } - else - { - out.Set("%s %s HTTP/1.1\r\n" - "Host: %s\r\n" - "Content-Type: %s\r\n" - "Content-Length: %u\r\n" - "\r\n" - "%s", - type, - remotePath.C_String(), - host.C_String(), - contentType, - //bodyEncoded.GetLength(), - //bodyEncoded.C_String()); - strlen(body), - body); - } - - return out; -} -RakString RakString::FormatForPOST(const char* uri, const char* contentType, const char* body, const char* extraHeaders) -{ - return FormatForPUTOrPost("POST", uri, contentType, body, extraHeaders); -} -RakString RakString::FormatForPUT(const char* uri, const char* contentType, const char* body, const char* extraHeaders) -{ - return FormatForPUTOrPost("PUT", uri, contentType, body, extraHeaders); -} -RakString RakString::FormatForGET(const char* uri, const char* extraHeaders) -{ - RakString out; - RakString host; - RakString remotePath; - RakNet::RakString header; - RakNet::RakString uriRs; - uriRs = uri; - - uriRs.SplitURI(header, host, remotePath); - if (host.IsEmpty() || remotePath.IsEmpty()) - return out; - - if (extraHeaders && extraHeaders[0]) - { - out.Set("GET %s HTTP/1.1\r\n" - "%s\r\n" - "Host: %s\r\n" - "\r\n", - remotePath.C_String(), - extraHeaders, - host.C_String()); - } - else - { - out.Set("GET %s HTTP/1.1\r\n" - "Host: %s\r\n" - "\r\n", - remotePath.C_String(), - host.C_String()); - - } - - - return out; -} -RakString RakString::FormatForDELETE(const char* uri, const char* extraHeaders) -{ - RakString out; - RakString host; - RakString remotePath; - RakNet::RakString header; - RakNet::RakString uriRs; - uriRs = uri; - - uriRs.SplitURI(header, host, remotePath); - if (host.IsEmpty() || remotePath.IsEmpty()) - return out; - - if (extraHeaders && extraHeaders[0]) - { - out.Set("DELETE %s HTTP/1.1\r\n" - "%s\r\n" - "Content-Length: 0\r\n" - "Host: %s\r\n" - "Connection: close\r\n" - "\r\n", - remotePath.C_String(), - extraHeaders, - host.C_String()); - } - else - { - out.Set("DELETE %s HTTP/1.1\r\n" - "Content-Length: 0\r\n" - "Host: %s\r\n" - "Connection: close\r\n" - "\r\n", - remotePath.C_String(), - host.C_String()); - } - - return out; -} -RakNet::RakString& RakString::MakeFilePath(void) -{ - if (IsEmpty()) - return *this; - - RakNet::RakString fixedString = *this; - fixedString.Clone(); - for (int i=0; fixedString.sharedString->c_str[i]; i++) - { -#ifdef _WIN32 - if (fixedString.sharedString->c_str[i]=='/') - fixedString.sharedString->c_str[i]='\\'; -#else - if (fixedString.sharedString->c_str[i]=='\\') - fixedString.sharedString->c_str[i]='/'; -#endif - } - -#ifdef _WIN32 - if (fixedString.sharedString->c_str[strlen(fixedString.sharedString->c_str)-1]!='\\') - { - fixedString+='\\'; - } -#else - if (fixedString.sharedString->c_str[strlen(fixedString.sharedString->c_str)-1]!='/') - { - fixedString+='/'; - } -#endif - - if (fixedString!=*this) - *this = fixedString; - return *this; -} -void RakString::FreeMemory(void) -{ - LockMutex(); - FreeMemoryNoMutex(); - UnlockMutex(); -} -void RakString::FreeMemoryNoMutex(void) -{ - for (unsigned int i=0; i < freeList.Size(); i++) - { - RakNet::OP_DELETE(freeList[i]->refCountMutex,_FILE_AND_LINE_); - rakFree_Ex(freeList[i], _FILE_AND_LINE_ ); - } - freeList.Clear(false, _FILE_AND_LINE_); -} -void RakString::Serialize(BitStream *bs) const -{ - Serialize(sharedString->c_str, bs); -} -void RakString::Serialize(const char *str, BitStream *bs) -{ - unsigned short l = (unsigned short) strlen(str); - bs->Write(l); - bs->WriteAlignedBytes((const unsigned char*) str, (const unsigned int) l); -} -void RakString::SerializeCompressed(BitStream *bs, uint8_t languageId, bool writeLanguageId) const -{ - SerializeCompressed(C_String(), bs, languageId, writeLanguageId); -} -void RakString::SerializeCompressed(const char *str, BitStream *bs, uint8_t languageId, bool writeLanguageId) -{ - if (writeLanguageId) - bs->WriteCompressed(languageId); - StringCompressor::Instance()->EncodeString(str,0xFFFF,bs,languageId); -} -bool RakString::Deserialize(BitStream *bs) -{ - Clear(); - - bool b; - unsigned short l; - b=bs->Read(l); - if (l>0) - { - Allocate(((unsigned int) l)+1); - b=bs->ReadAlignedBytes((unsigned char*) sharedString->c_str, l); - if (b) - sharedString->c_str[l]=0; - else - Clear(); - } - else - bs->AlignReadToByteBoundary(); - return b; -} -bool RakString::Deserialize(char *str, BitStream *bs) -{ - bool b; - unsigned short l; - b=bs->Read(l); - if (b && l>0) - b=bs->ReadAlignedBytes((unsigned char*) str, l); - - if (b==false) - str[0]=0; - - str[l]=0; - return b; -} -bool RakString::DeserializeCompressed(BitStream *bs, bool readLanguageId) -{ - uint8_t languageId; - if (readLanguageId) - bs->ReadCompressed(languageId); - else - languageId=0; - return StringCompressor::Instance()->DecodeString(this,0xFFFF,bs,languageId); -} -bool RakString::DeserializeCompressed(char *str, BitStream *bs, bool readLanguageId) -{ - uint8_t languageId; - if (readLanguageId) - bs->ReadCompressed(languageId); - else - languageId=0; - return StringCompressor::Instance()->DecodeString(str,0xFFFF,bs,languageId); -} -const char *RakString::ToString(int64_t i) -{ - static int index=0; - static char buff[64][64]; -#if defined(_WIN32) - sprintf(buff[index], "%I64d", i); -#else - sprintf(buff[index], "%lld", (long long unsigned int) i); -#endif - int lastIndex=index; - if (++index==64) - index=0; - return buff[lastIndex]; -} -const char *RakString::ToString(uint64_t i) -{ - static int index=0; - static char buff[64][64]; -#if defined(_WIN32) - sprintf(buff[index], "%I64u", i); -#else - sprintf(buff[index], "%llu", (long long unsigned int) i); -#endif - int lastIndex=index; - if (++index==64) - index=0; - return buff[lastIndex]; -} -void RakString::Clear(void) -{ - Free(); -} -void RakString::Allocate(size_t len) -{ - RakString::LockMutex(); - // sharedString = RakString::pool.Allocate( _FILE_AND_LINE_ ); - if (RakString::freeList.Size()==0) - { - //RakString::sharedStringFreeList=(RakString::SharedString*) rakRealloc_Ex(RakString::sharedStringFreeList,(RakString::sharedStringFreeListAllocationCount+1024)*sizeof(RakString::SharedString), _FILE_AND_LINE_); - unsigned i; - for (i=0; i < 128; i++) - { - // RakString::freeList.Insert(RakString::sharedStringFreeList+i+RakString::sharedStringFreeListAllocationCount); - // RakString::freeList.Insert((RakString::SharedString*)rakMalloc_Ex(sizeof(RakString::SharedString), _FILE_AND_LINE_), _FILE_AND_LINE_); - - RakString::SharedString *ss; - ss = (RakString::SharedString*) rakMalloc_Ex(sizeof(RakString::SharedString), _FILE_AND_LINE_); - ss->refCountMutex=RakNet::OP_NEW(_FILE_AND_LINE_); - RakString::freeList.Insert(ss, _FILE_AND_LINE_); - } - //RakString::sharedStringFreeListAllocationCount+=1024; - } - sharedString = RakString::freeList[RakString::freeList.Size()-1]; - RakString::freeList.RemoveAtIndex(RakString::freeList.Size()-1); - RakString::UnlockMutex(); - - const size_t smallStringSize = 128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2; - sharedString->refCount=1; - if (len <= smallStringSize) - { - sharedString->bytesUsed=smallStringSize; - sharedString->c_str=sharedString->smallString; - } - else - { - sharedString->bytesUsed=len<<1; - sharedString->bigString=(char*)rakMalloc_Ex(sharedString->bytesUsed, _FILE_AND_LINE_); - sharedString->c_str=sharedString->bigString; - } -} -void RakString::Assign(const char *str) -{ - if (str==0 || str[0]==0) - { - sharedString=&emptyString; - return; - } - - size_t len = strlen(str)+1; - Allocate(len); - memcpy(sharedString->c_str, str, len); -} -void RakString::Assign(const char *str, va_list ap) -{ - if (str==0 || str[0]==0) - { - sharedString=&emptyString; - return; - } - - char stackBuff[512]; - if (_vsnprintf(stackBuff, 512, str, ap)!=-1 -#ifndef _WIN32 - // Here Windows will return -1 if the string is too long; Linux just truncates the string. - && strlen(str) <511 -#endif - ) - { - Assign(stackBuff); - return; - } - char *buff=0, *newBuff; - size_t buffSize=8096; - while (1) - { - newBuff = (char*) rakRealloc_Ex(buff, buffSize,__FILE__,__LINE__); - if (newBuff==0) - { - notifyOutOfMemory(_FILE_AND_LINE_); - if (buff!=0) - { - Assign(buff); - rakFree_Ex(buff,__FILE__,__LINE__); - } - else - { - Assign(stackBuff); - } - return; - } - buff=newBuff; - if (_vsnprintf(buff, buffSize, str, ap)!=-1) - { - Assign(buff); - rakFree_Ex(buff,__FILE__,__LINE__); - return; - } - buffSize*=2; - } -} -RakNet::RakString RakString::Assign(const char *str,size_t pos, size_t n ) -{ - size_t incomingLen=strlen(str); - - Clone(); - - if (str==0 || str[0]==0||pos>=incomingLen) - { - sharedString=&emptyString; - return (*this); - } - - if (pos+n>=incomingLen) - { - n=incomingLen-pos; - - } - const char * tmpStr=&(str[pos]); - - size_t len = n+1; - Allocate(len); - memcpy(sharedString->c_str, tmpStr, len); - sharedString->c_str[n]=0; - - return (*this); -} - -RakNet::RakString RakString::NonVariadic(const char *str) -{ - RakNet::RakString rs; - rs=str; - return rs; -} -unsigned long RakString::ToInteger(const char *str) -{ - unsigned long hash = 0; - int c; - - while ((c = *str++)) - hash = c + (hash << 6) + (hash << 16) - hash; - - return hash; -} -unsigned long RakString::ToInteger(const RakString &rs) -{ - return RakString::ToInteger(rs.C_String()); -} -int RakString::ReadIntFromSubstring(const char *str, size_t pos, size_t n) -{ - char tmp[32]; - if (n >= 32) - return 0; - for (size_t i=0; i < n; i++) - tmp[i]=str[i+pos]; - return atoi(tmp); -} -void RakString::AppendBytes(const char *bytes, unsigned int count) -{ - if (IsEmpty()) - { - Allocate(count); - memcpy(sharedString->c_str, bytes, count+1); - sharedString->c_str[count]=0; - } - else - { - Clone(); - unsigned int length=(unsigned int) GetLength(); - Realloc(sharedString, count+length+1); - memcpy(sharedString->c_str+length, bytes, count); - sharedString->c_str[length+count]=0; - } - - -} -void RakString::Clone(void) -{ - RakAssert(sharedString!=&emptyString); - if (sharedString==&emptyString) - { - return; - } - - // Empty or solo then no point to cloning - sharedString->refCountMutex->Lock(); - if (sharedString->refCount==1) - { - sharedString->refCountMutex->Unlock(); - return; - } - - sharedString->refCount--; - sharedString->refCountMutex->Unlock(); - Assign(sharedString->c_str); -} -void RakString::Free(void) -{ - if (sharedString==&emptyString) - return; - sharedString->refCountMutex->Lock(); - sharedString->refCount--; - if (sharedString->refCount==0) - { - sharedString->refCountMutex->Unlock(); - const size_t smallStringSize = 128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2; - if (sharedString->bytesUsed>smallStringSize) - rakFree_Ex(sharedString->bigString, _FILE_AND_LINE_ ); - /* - poolMutex->Lock(); - pool.Release(sharedString); - poolMutex->Unlock(); - */ - - RakString::LockMutex(); - RakString::freeList.Insert(sharedString, _FILE_AND_LINE_); - RakString::UnlockMutex(); - - sharedString=&emptyString; - } - else - { - sharedString->refCountMutex->Unlock(); - } - sharedString=&emptyString; -} -unsigned char RakString::ToLower(unsigned char c) -{ - if (c >= 'A' && c <= 'Z') - return c-'A'+'a'; - return c; -} -unsigned char RakString::ToUpper(unsigned char c) -{ - if (c >= 'a' && c <= 'z') - return c-'a'+'A'; - return c; -} -void RakString::LockMutex(void) -{ - GetPoolMutex().Lock(); -} -void RakString::UnlockMutex(void) -{ - GetPoolMutex().Unlock(); -} - -/* -#include "RakString.h" -#include -#include "GetTime.h" - -using namespace RakNet; - -int main(void) -{ - RakString s3("Hello world"); - RakString s5=s3; - - RakString s1; - RakString s2('a'); - - RakString s4("%i %f", 5, 6.0); - - RakString s6=s3; - RakString s7=s6; - RakString s8=s6; - RakString s9; - s9=s9; - RakString s10(s3); - RakString s11=s10 + s4 + s9 + s2; - s11+=RakString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); - RakString s12("Test"); - s12+=s11; - bool b1 = s12==s12; - s11=s5; - s12.ToUpper(); - s12.ToLower(); - RakString s13; - bool b3 = s13.IsEmpty(); - s13.Set("blah %s", s12.C_String()); - bool b4 = s13.IsEmpty(); - size_t i1=s13.GetLength(); - s3.Clear(_FILE_AND_LINE_); - s4.Clear(_FILE_AND_LINE_); - s5.Clear(_FILE_AND_LINE_); - s5.Clear(_FILE_AND_LINE_); - s6.Printf(); - s7.Printf(); - RAKNET_DEBUG_PRINTF("\n"); - - static const int repeatCount=750; - DataStructures::List rakStringList; - DataStructures::List stdStringList; - DataStructures::List referenceStringList; - char *c; - unsigned i; - RakNet::TimeMS beforeReferenceList, beforeRakString, beforeStdString, afterStdString; - - unsigned loop; - for (loop=0; loop<2; loop++) - { - beforeReferenceList=RakNet::GetTimeMS(); - for (i=0; i < repeatCount; i++) - { - c = RakNet::OP_NEW_ARRAY(56,_FILE_AND_LINE_ ); - strcpy(c, "Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd"); - referenceStringList.Insert(c); - } - beforeRakString=RakNet::GetTimeMS(); - for (i=0; i < repeatCount; i++) - rakStringList.Insert("Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd"); - beforeStdString=RakNet::GetTimeMS(); - - for (i=0; i < repeatCount; i++) - stdStringList.Insert("Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd"); - afterStdString=RakNet::GetTimeMS(); - RAKNET_DEBUG_PRINTF("Insertion 1 Ref=%i Rak=%i, Std=%i\n", beforeRakString-beforeReferenceList, beforeStdString-beforeRakString, afterStdString-beforeStdString); - - beforeReferenceList=RakNet::GetTimeMS(); - for (i=0; i < repeatCount; i++) - { - RakNet::OP_DELETE_ARRAY(referenceStringList[0], _FILE_AND_LINE_); - referenceStringList.RemoveAtIndex(0); - } - beforeRakString=RakNet::GetTimeMS(); - for (i=0; i < repeatCount; i++) - rakStringList.RemoveAtIndex(0); - beforeStdString=RakNet::GetTimeMS(); - for (i=0; i < repeatCount; i++) - stdStringList.RemoveAtIndex(0); - afterStdString=RakNet::GetTimeMS(); - RAKNET_DEBUG_PRINTF("RemoveHead Ref=%i Rak=%i, Std=%i\n", beforeRakString-beforeReferenceList, beforeStdString-beforeRakString, afterStdString-beforeStdString); - - beforeReferenceList=RakNet::GetTimeMS(); - for (i=0; i < repeatCount; i++) - { - c = RakNet::OP_NEW_ARRAY(56, _FILE_AND_LINE_ ); - strcpy(c, "Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd"); - referenceStringList.Insert(0); - } - beforeRakString=RakNet::GetTimeMS(); - for (i=0; i < repeatCount; i++) - rakStringList.Insert("Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd"); - beforeStdString=RakNet::GetTimeMS(); - for (i=0; i < repeatCount; i++) - stdStringList.Insert("Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd"); - afterStdString=RakNet::GetTimeMS(); - RAKNET_DEBUG_PRINTF("Insertion 2 Ref=%i Rak=%i, Std=%i\n", beforeRakString-beforeReferenceList, beforeStdString-beforeRakString, afterStdString-beforeStdString); - - beforeReferenceList=RakNet::GetTimeMS(); - for (i=0; i < repeatCount; i++) - { - RakNet::OP_DELETE_ARRAY(referenceStringList[referenceStringList.Size()-1], _FILE_AND_LINE_); - referenceStringList.RemoveAtIndex(referenceStringList.Size()-1); - } - beforeRakString=RakNet::GetTimeMS(); - for (i=0; i < repeatCount; i++) - rakStringList.RemoveAtIndex(rakStringList.Size()-1); - beforeStdString=RakNet::GetTimeMS(); - for (i=0; i < repeatCount; i++) - stdStringList.RemoveAtIndex(stdStringList.Size()-1); - afterStdString=RakNet::GetTimeMS(); - RAKNET_DEBUG_PRINTF("RemoveTail Ref=%i Rak=%i, Std=%i\n", beforeRakString-beforeReferenceList, beforeStdString-beforeRakString, afterStdString-beforeStdString); - - } - - printf("Done."); - char str[128]; - Gets(str, sizeof(str)); - return 1; -} -*/ +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "RakString.h" +#include "RakAssert.h" +#include "RakMemoryOverride.h" +#include "BitStream.h" +#include +#include +#include "LinuxStrings.h" +#include "StringCompressor.h" +#include "SimpleMutex.h" +#include +#include "Itoa.h" + +using namespace RakNet; + +//DataStructures::MemoryPool RakString::pool; +RakString::SharedString RakString::emptyString={0,0,0,(char*) "",(char*) ""}; +//RakString::SharedString *RakString::sharedStringFreeList=0; +//unsigned int RakString::sharedStringFreeListAllocationCount=0; +DataStructures::List RakString::freeList; + +class RakStringCleanup +{ +public: + ~RakStringCleanup() + { + RakNet::RakString::FreeMemoryNoMutex(); + } +}; + +static RakStringCleanup cleanup; + +SimpleMutex& GetPoolMutex(void) +{ + static SimpleMutex poolMutex; + return poolMutex; +} + +int RakNet::RakString::RakStringComp( RakString const &key, RakString const &data ) +{ + return key.StrCmp(data); +} + +RakString::RakString() +{ + sharedString=&emptyString; +} +RakString::RakString( RakString::SharedString *_sharedString ) +{ + sharedString=_sharedString; +} +RakString::RakString(char input) +{ + char str[2]; + str[0]=input; + str[1]=0; + Assign(str); +} +RakString::RakString(unsigned char input) +{ + char str[2]; + str[0]=(char) input; + str[1]=0; + Assign(str); +} +RakString::RakString(const unsigned char *format, ...){ + va_list ap; + va_start(ap, format); + Assign((const char*) format,ap); +} +RakString::RakString(const char *format, ...){ + va_list ap; + va_start(ap, format); + Assign(format,ap); +} +RakString::RakString( const RakString & rhs) +{ + if (rhs.sharedString==&emptyString) + { + sharedString=&emptyString; + return; + } + + rhs.sharedString->refCountMutex->Lock(); + if (rhs.sharedString->refCount==0) + { + sharedString=&emptyString; + } + else + { + rhs.sharedString->refCount++; + sharedString=rhs.sharedString; + } + rhs.sharedString->refCountMutex->Unlock(); +} +RakString::~RakString() +{ + Free(); +} +RakString& RakString::operator = ( const RakString& rhs ) +{ + Free(); + if (rhs.sharedString==&emptyString) + return *this; + + rhs.sharedString->refCountMutex->Lock(); + if (rhs.sharedString->refCount==0) + { + sharedString=&emptyString; + } + else + { + sharedString=rhs.sharedString; + sharedString->refCount++; + } + rhs.sharedString->refCountMutex->Unlock(); + return *this; +} +RakString& RakString::operator = ( const char *str ) +{ + Free(); + Assign(str); + return *this; +} +RakString& RakString::operator = ( char *str ) +{ + return operator = ((const char*)str); +} +RakString& RakString::operator = ( const unsigned char *str ) +{ + return operator = ((const char*)str); +} +RakString& RakString::operator = ( char unsigned *str ) +{ + return operator = ((const char*)str); +} +RakString& RakString::operator = ( const char c ) +{ + char buff[2]; + buff[0]=c; + buff[1]=0; + return operator = ((const char*)buff); +} +void RakString::Realloc(SharedString *sharedString, size_t bytes) +{ + if (bytes<=sharedString->bytesUsed) + return; + + RakAssert(bytes>0); + size_t oldBytes = sharedString->bytesUsed; + size_t newBytes; + const size_t smallStringSize = 128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2; + newBytes = GetSizeToAllocate(bytes); + if (oldBytes <=(size_t) smallStringSize && newBytes > (size_t) smallStringSize) + { + sharedString->bigString=(char*) rakMalloc_Ex(newBytes, _FILE_AND_LINE_); + strcpy(sharedString->bigString, sharedString->smallString); + sharedString->c_str=sharedString->bigString; + } + else if (oldBytes > smallStringSize) + { + sharedString->bigString=(char*) rakRealloc_Ex(sharedString->bigString,newBytes, _FILE_AND_LINE_); + sharedString->c_str=sharedString->bigString; + } + sharedString->bytesUsed=newBytes; +} +RakString& RakString::operator +=( const RakString& rhs) +{ + if (rhs.IsEmpty()) + return *this; + + if (IsEmpty()) + { + return operator=(rhs); + } + else + { + Clone(); + size_t strLen=rhs.GetLength()+GetLength()+1; + Realloc(sharedString, strLen+GetLength()); + strcat(sharedString->c_str,rhs.C_String()); + } + return *this; +} +RakString& RakString::operator +=( const char *str ) +{ + if (str==0 || str[0]==0) + return *this; + + if (IsEmpty()) + { + Assign(str); + } + else + { + Clone(); + size_t strLen=strlen(str)+GetLength()+1; + Realloc(sharedString, strLen); + strcat(sharedString->c_str,str); + } + return *this; +} +RakString& RakString::operator +=( char *str ) +{ + return operator += ((const char*)str); +} +RakString& RakString::operator +=( const unsigned char *str ) +{ + return operator += ((const char*)str); +} +RakString& RakString::operator +=( unsigned char *str ) +{ + return operator += ((const char*)str); +} +RakString& RakString::operator +=( const char c ) +{ + char buff[2]; + buff[0]=c; + buff[1]=0; + return operator += ((const char*)buff); +} +unsigned char RakString::operator[] ( const unsigned int position ) const +{ + RakAssert(positionc_str[position]; +} +bool RakString::operator==(const RakString &rhs) const +{ + return strcmp(sharedString->c_str,rhs.sharedString->c_str)==0; +} +bool RakString::operator==(const char *str) const +{ + return strcmp(sharedString->c_str,str)==0; +} +bool RakString::operator==(char *str) const +{ + return strcmp(sharedString->c_str,str)==0; +} +bool RakString::operator < ( const RakString& right ) const +{ + return strcmp(sharedString->c_str,right.C_String()) < 0; +} +bool RakString::operator <= ( const RakString& right ) const +{ + return strcmp(sharedString->c_str,right.C_String()) <= 0; +} +bool RakString::operator > ( const RakString& right ) const +{ + return strcmp(sharedString->c_str,right.C_String()) > 0; +} +bool RakString::operator >= ( const RakString& right ) const +{ + return strcmp(sharedString->c_str,right.C_String()) >= 0; +} +bool RakString::operator!=(const RakString &rhs) const +{ + return strcmp(sharedString->c_str,rhs.sharedString->c_str)!=0; +} +bool RakString::operator!=(const char *str) const +{ + return strcmp(sharedString->c_str,str)!=0; +} +bool RakString::operator!=(char *str) const +{ + return strcmp(sharedString->c_str,str)!=0; +} +const RakNet::RakString operator+(const RakNet::RakString &lhs, const RakNet::RakString &rhs) +{ + if (lhs.IsEmpty() && rhs.IsEmpty()) + { + return RakString(&RakString::emptyString); + } + if (lhs.IsEmpty()) + { + rhs.sharedString->refCountMutex->Lock(); + if (rhs.sharedString->refCount==0) + { + rhs.sharedString->refCountMutex->Unlock(); + lhs.sharedString->refCountMutex->Lock(); + lhs.sharedString->refCount++; + lhs.sharedString->refCountMutex->Unlock(); + return RakString(lhs.sharedString); + } + else + { + rhs.sharedString->refCount++; + rhs.sharedString->refCountMutex->Unlock(); + return RakString(rhs.sharedString); + } + // rhs.sharedString->refCountMutex->Unlock(); + } + if (rhs.IsEmpty()) + { + lhs.sharedString->refCountMutex->Lock(); + lhs.sharedString->refCount++; + lhs.sharedString->refCountMutex->Unlock(); + return RakString(lhs.sharedString); + } + + size_t len1 = lhs.GetLength(); + size_t len2 = rhs.GetLength(); + size_t allocatedBytes = len1 + len2 + 1; + allocatedBytes = RakString::GetSizeToAllocate(allocatedBytes); + RakString::SharedString *sharedString; + + RakString::LockMutex(); + // sharedString = RakString::pool.Allocate( _FILE_AND_LINE_ ); + if (RakString::freeList.Size()==0) + { + //RakString::sharedStringFreeList=(RakString::SharedString*) rakRealloc_Ex(RakString::sharedStringFreeList,(RakString::sharedStringFreeListAllocationCount+1024)*sizeof(RakString::SharedString), _FILE_AND_LINE_); + unsigned i; + for (i=0; i < 128; i++) + { + // RakString::freeList.Insert(RakString::sharedStringFreeList+i+RakString::sharedStringFreeListAllocationCount); + RakString::SharedString *ss; + ss = (RakString::SharedString*) rakMalloc_Ex(sizeof(RakString::SharedString), _FILE_AND_LINE_); + ss->refCountMutex=RakNet::OP_NEW(_FILE_AND_LINE_); + RakString::freeList.Insert(ss, _FILE_AND_LINE_); + + } + //RakString::sharedStringFreeListAllocationCount+=1024; + } + sharedString = RakString::freeList[RakString::freeList.Size()-1]; + RakString::freeList.RemoveAtIndex(RakString::freeList.Size()-1); + RakString::UnlockMutex(); + + const int smallStringSize = 128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2; + sharedString->bytesUsed=allocatedBytes; + sharedString->refCount=1; + if (allocatedBytes <= (size_t) smallStringSize) + { + sharedString->c_str=sharedString->smallString; + } + else + { + sharedString->bigString=(char*)rakMalloc_Ex(sharedString->bytesUsed, _FILE_AND_LINE_); + sharedString->c_str=sharedString->bigString; + } + + strcpy(sharedString->c_str, lhs); + strcat(sharedString->c_str, rhs); + + return RakString(sharedString); +} +const char * RakString::ToLower(void) +{ + Clone(); + + size_t strLen = strlen(sharedString->c_str); + unsigned i; + for (i=0; i < strLen; i++) + sharedString->c_str[i]=ToLower(sharedString->c_str[i]); + return sharedString->c_str; +} +const char * RakString::ToUpper(void) +{ + Clone(); + + size_t strLen = strlen(sharedString->c_str); + unsigned i; + for (i=0; i < strLen; i++) + sharedString->c_str[i]=ToUpper(sharedString->c_str[i]); + return sharedString->c_str; +} +void RakString::Set(const char *format, ...) +{ + va_list ap; + va_start(ap, format); + Clear(); + Assign(format,ap); +} +bool RakString::IsEmpty(void) const +{ + return sharedString==&emptyString; +} +size_t RakString::GetLength(void) const +{ + return strlen(sharedString->c_str); +} +// http://porg.es/blog/counting-characters-in-utf-8-strings-is-faster +int porges_strlen2(char *s) +{ + int i = 0; + int iBefore = 0; + int count = 0; + + while (s[i] > 0) +ascii: i++; + + count += i-iBefore; + while (s[i]) + { + if (s[i] > 0) + { + iBefore = i; + goto ascii; + } + else + switch (0xF0 & s[i]) + { + case 0xE0: i += 3; break; + case 0xF0: i += 4; break; + default: i += 2; break; + } + ++count; + } + return count; +} +size_t RakString::GetLengthUTF8(void) const +{ + return porges_strlen2(sharedString->c_str); +} +void RakString::Replace(unsigned index, unsigned count, unsigned char c) +{ + RakAssert(index+count < GetLength()); + Clone(); + unsigned countIndex=0; + while (countIndexc_str[index]=c; + index++; + countIndex++; + } + +} +void RakString::SetChar( unsigned index, unsigned char c ) +{ + RakAssert(index < GetLength()); + Clone(); + sharedString->c_str[index]=c; +} +void RakString::SetChar( unsigned index, RakNet::RakString s ) +{ + RakAssert(index < GetLength()); + Clone(); + RakNet::RakString firstHalf = SubStr(0, index); + RakNet::RakString secondHalf = SubStr(index+1, (unsigned int)-1); + *this = firstHalf; + *this += s; + *this += secondHalf; +} + +#ifdef _WIN32 +WCHAR * RakString::ToWideChar(void) +{ + // + // Special case of nullptr or empty input string + // + if ( (sharedString->c_str == nullptr) || (*sharedString->c_str == '\0') ) + { + // Return empty string + return L""; + } + + // + // Get size of destination UTF-16 buffer, in WCHAR's + // + int cchUTF16 = ::MultiByteToWideChar( + CP_UTF8, // convert from UTF-8 + 0, // Flags + sharedString->c_str, // source UTF-8 string + GetLength()+1, // total length of source UTF-8 string, + // in CHAR's (= bytes), including end-of-string \0 + nullptr, // unused - no conversion done in this step + 0 // request size of destination buffer, in WCHAR's + ); + + if ( cchUTF16 == 0 ) + { + RakAssert("RakString::ToWideChar exception from cchUTF16==0" && 0); + return 0; + } + + // + // Allocate destination buffer to store UTF-16 string + // + WCHAR * pszUTF16 = RakNet::OP_NEW_ARRAY(cchUTF16,__FILE__,__LINE__); + + // + // Do the conversion from UTF-8 to UTF-16 + // + int result = ::MultiByteToWideChar( + CP_UTF8, // convert from UTF-8 + 0, // Buffer + sharedString->c_str, // source UTF-8 string + GetLength()+1, // total length of source UTF-8 string, + // in CHAR's (= bytes), including end-of-string \0 + pszUTF16, // destination buffer + cchUTF16 // size of destination buffer, in WCHAR's + ); + + if ( result == 0 ) + { + RakAssert("RakString::ToWideChar exception from MultiByteToWideChar" && 0); + return 0; + } + + return pszUTF16; +} +void RakString::DeallocWideChar(WCHAR * w) +{ + RakNet::OP_DELETE_ARRAY(w,__FILE__,__LINE__); +} +void RakString::FromWideChar(const wchar_t *source) +{ + Clear(); + int bufSize = wcslen(source)*4; + Allocate(bufSize); + WideCharToMultiByte ( CP_ACP, // ANSI code page + + + + WC_COMPOSITECHECK, // Check for accented characters + + source, // Source Unicode string + -1, // -1 means string is zero-terminated + sharedString->c_str, // Destination char string + bufSize, // Size of buffer + nullptr, // No default character + nullptr ); // Don't care about this flag + + +} +RakNet::RakString RakString::FromWideChar_S(const wchar_t *source) +{ + RakNet::RakString rs; + rs.FromWideChar(source); + return rs; +} +#endif +size_t RakString::Find(const char *stringToFind,size_t pos) +{ + size_t len=GetLength(); + if (pos>=len || stringToFind==0 || stringToFind[0]==0) + { + return (size_t) -1; + } + size_t matchLen= strlen(stringToFind); + size_t matchPos=0; + size_t iStart=0; + + for (size_t i=pos;ic_str[i]) + { + if(matchPos==0) + { + iStart=i; + } + matchPos++; + } + else + { + matchPos=0; + } + + if (matchPos>=matchLen) + { + return iStart; + } + } + + return (size_t) -1; +} + +void RakString::TruncateUTF8(unsigned int length) +{ + int i = 0; + unsigned int count = 0; + + while (sharedString->c_str[i]!=0) + { + if (count==length) + { + sharedString->c_str[i]=0; + return; + } + else if (sharedString->c_str[i]>0) + { + i++; + } + else + { + switch (0xF0 & sharedString->c_str[i]) + { + case 0xE0: i += 3; break; + case 0xF0: i += 4; break; + default: i += 2; break; + } + } + + count++; + } +} + +void RakString::Truncate(unsigned int length) +{ + if (length < GetLength()) + { + SetChar(length, 0); + } +} + +RakString RakString::SubStr(unsigned int index, unsigned int count) const +{ + size_t length = GetLength(); + if (index >= length || count==0) + return RakString(); + RakString copy; + size_t numBytes = length-index; + if (count < numBytes) + numBytes=count; + copy.Allocate(numBytes+1); + size_t i; + for (i=0; i < numBytes; i++) + copy.sharedString->c_str[i]=sharedString->c_str[index+i]; + copy.sharedString->c_str[i]=0; + return copy; +} +void RakString::Erase(unsigned int index, unsigned int count) +{ + size_t len = GetLength(); + RakAssert(index+count <= len); + + Clone(); + unsigned i; + for (i=index; i < len-count; i++) + { + sharedString->c_str[i]=sharedString->c_str[i+count]; + } + sharedString->c_str[i]=0; +} +void RakString::TerminateAtLastCharacter(char c) +{ + int i, len=(int) GetLength(); + for (i=len-1; i >= 0; i--) + { + if (sharedString->c_str[i]==c) + { + Clone(); + sharedString->c_str[i]=0; + return; + } + } +} +void RakString::StartAfterLastCharacter(char c) +{ + int i, len=(int) GetLength(); + for (i=len-1; i >= 0; i--) + { + if (sharedString->c_str[i]==c) + { + ++i; + if (i < len) + { + *this = SubStr(i,GetLength()-i); + } + return; + } + } +} +void RakString::TerminateAtFirstCharacter(char c) +{ + unsigned int i, len=(unsigned int) GetLength(); + for (i=0; i < len; i++) + { + if (sharedString->c_str[i]==c) + { + if (i > 0) + { + Clone(); + sharedString->c_str[i]=0; + } + } + } +} +void RakString::StartAfterFirstCharacter(char c) +{ + unsigned int i, len=(unsigned int) GetLength(); + for (i=0; i < len; i++) + { + if (sharedString->c_str[i]==c) + { + ++i; + if (i < len) + { + *this = SubStr(i,GetLength()-i); + } + return; + } + } +} +int RakString::GetCharacterCount(char c) +{ + int count=0; + unsigned int i, len=(unsigned int) GetLength(); + for (i=0; i < len; i++) + { + if (sharedString->c_str[i]==c) + { + ++count; + } + } + return count; +} +void RakString::RemoveCharacter(char c) +{ + if (c==0) + return; + + unsigned int readIndex, writeIndex=0; + for (readIndex=0; sharedString->c_str[readIndex]; readIndex++) + { + if (sharedString->c_str[readIndex]!=c) + sharedString->c_str[writeIndex++]=sharedString->c_str[readIndex]; + else + Clone(); + } + sharedString->c_str[writeIndex]=0; + if (writeIndex==0) + Clear(); +} +int RakString::StrCmp(const RakString &rhs) const +{ + return strcmp(sharedString->c_str, rhs.C_String()); +} +int RakString::StrNCmp(const RakString &rhs, size_t num) const +{ + return strncmp(sharedString->c_str, rhs.C_String(), num); +} +int RakString::StrICmp(const RakString &rhs) const +{ + return _stricmp(sharedString->c_str, rhs.C_String()); +} +void RakString::Printf(void) +{ + RAKNET_DEBUG_PRINTF("%s", sharedString->c_str); +} +void RakString::FPrintf(FILE *fp) +{ + fprintf(fp,"%s", sharedString->c_str); +} +bool RakString::IPAddressMatch(const char *IP) +{ + unsigned characterIndex; + + if ( IP == 0 || IP[ 0 ] == 0 || strlen( IP ) > 15 ) + return false; + + characterIndex = 0; + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + while ( true ) + { + if (sharedString->c_str[ characterIndex ] == IP[ characterIndex ] ) + { + // Equal characters + if ( IP[ characterIndex ] == 0 ) + { + // End of the string and the strings match + + return true; + } + + characterIndex++; + } + + else + { + if ( sharedString->c_str[ characterIndex ] == 0 || IP[ characterIndex ] == 0 ) + { + // End of one of the strings + break; + } + + // Characters do not match + if ( sharedString->c_str[ characterIndex ] == '*' ) + { + // Domain is banned. + return true; + } + + // Characters do not match and it is not a * + break; + } + } + + + // No match found. + return false; +} +bool RakString::ContainsNonprintableExceptSpaces(void) const +{ + size_t strLen = strlen(sharedString->c_str); + unsigned i; + for (i=0; i < strLen; i++) + { + if (sharedString->c_str[i] < ' ' || sharedString->c_str[i] >126) + return true; + } + return false; +} +bool RakString::IsEmailAddress(void) const +{ + if (IsEmpty()) + return false; + size_t strLen = strlen(sharedString->c_str); + if (strLen < 6) // a@b.de + return false; + if (sharedString->c_str[strLen-4]!='.' && sharedString->c_str[strLen-3]!='.') // .com, .net., .org, .de + return false; + unsigned i; + // Has non-printable? + for (i=0; i < strLen; i++) + { + if (sharedString->c_str[i] <= ' ' || sharedString->c_str[i] >126) + return false; + } + int atCount=0; + for (i=0; i < strLen; i++) + { + if (sharedString->c_str[i]=='@') + { + atCount++; + } + } + if (atCount!=1) + return false; + int dotCount=0; + for (i=0; i < strLen; i++) + { + if (sharedString->c_str[i]=='.') + { + dotCount++; + } + } + if (dotCount==0) + return false; + + // There's more I could check, but this is good enough + return true; +} +RakNet::RakString& RakString::URLEncode(void) +{ + RakString result; + size_t strLen = strlen(sharedString->c_str); + result.Allocate(strLen*3); + char *output=result.sharedString->c_str; + unsigned int outputIndex=0; + unsigned i; + unsigned char c; + for (i=0; i < strLen; i++) + { + c=sharedString->c_str[i]; + if ( + (c<=47) || + (c>=58 && c<=64) || + (c>=91 && c<=96) || + (c>=123) + ) + { + char buff[3]; + Itoa(c, buff, 16); + output[outputIndex++]='%'; + output[outputIndex++]=buff[0]; + output[outputIndex++]=buff[1]; + } + else + { + output[outputIndex++]=c; + } + } + + output[outputIndex]=0; + + *this = result; + return *this; +} +RakNet::RakString& RakString::URLDecode(void) +{ + RakString result; + size_t strLen = strlen(sharedString->c_str); + result.Allocate(strLen); + char *output=result.sharedString->c_str; + unsigned int outputIndex=0; + char c; + char hexDigits[2]; + char hexValues[2]; + unsigned int i; + for (i=0; i < strLen; i++) + { + c=sharedString->c_str[i]; + if (c=='%') + { + hexDigits[0]=sharedString->c_str[++i]; + hexDigits[1]=sharedString->c_str[++i]; + + if (hexDigits[0]==' ') + hexValues[0]=0; + + if (hexDigits[0]>='A' && hexDigits[0]<='F') + hexValues[0]=hexDigits[0]-'A'+10; + if (hexDigits[0]>='a' && hexDigits[0]<='f') + hexValues[0]=hexDigits[0]-'a'+10; + else + hexValues[0]=hexDigits[0]-'0'; + + if (hexDigits[1]>='A' && hexDigits[1]<='F') + hexValues[1]=hexDigits[1]-'A'+10; + if (hexDigits[1]>='a' && hexDigits[1]<='f') + hexValues[1]=hexDigits[1]-'a'+10; + else + hexValues[1]=hexDigits[1]-'0'; + + output[outputIndex++]=hexValues[0]*16+hexValues[1]; + } + else + { + output[outputIndex++]=c; + } + } + + output[outputIndex]=0; + + *this = result; + return *this; +} +void RakString::SplitURI(RakNet::RakString &header, RakNet::RakString &domain, RakNet::RakString &path) +{ + header.Clear(); + domain.Clear(); + path.Clear(); + + size_t strLen = strlen(sharedString->c_str); + + char c; + unsigned int i=0; + if (strncmp(sharedString->c_str, "http://", 7)==0) + i+=(unsigned int) strlen("http://"); + else if (strncmp(sharedString->c_str, "https://", 8)==0) + i+=(unsigned int) strlen("https://"); + + if (strncmp(sharedString->c_str, "www.", 4)==0) + i+=(unsigned int) strlen("www."); + + if (i!=0) + { + header.Allocate(i+1); + strncpy(header.sharedString->c_str, sharedString->c_str, i); + header.sharedString->c_str[i]=0; + } + + + domain.Allocate(strLen-i+1); + char *domainOutput=domain.sharedString->c_str; + unsigned int outputIndex=0; + for (; i < strLen; i++) + { + c=sharedString->c_str[i]; + if (c=='/') + { + break; + } + else + { + domainOutput[outputIndex++]=sharedString->c_str[i]; + } + } + + domainOutput[outputIndex]=0; + + path.Allocate(strLen-header.GetLength()-outputIndex+1); + outputIndex=0; + char *pathOutput=path.sharedString->c_str; + for (; i < strLen; i++) + { + pathOutput[outputIndex++]=sharedString->c_str[i]; + } + pathOutput[outputIndex]=0; +} +RakNet::RakString& RakString::SQLEscape(void) +{ + int strLen=(int)GetLength(); + int escapedCharacterCount=0; + int index; + for (index=0; index < strLen; index++) + { + if (sharedString->c_str[index]=='\'' || + sharedString->c_str[index]=='"' || + sharedString->c_str[index]=='\\') + escapedCharacterCount++; + } + if (escapedCharacterCount==0) + return *this; + + Clone(); + Realloc(sharedString, strLen+escapedCharacterCount); + int writeIndex, readIndex; + writeIndex = strLen+escapedCharacterCount; + readIndex=strLen; + while (readIndex>=0) + { + if (sharedString->c_str[readIndex]=='\'' || + sharedString->c_str[readIndex]=='"' || + sharedString->c_str[readIndex]=='\\') + { + sharedString->c_str[writeIndex--]=sharedString->c_str[readIndex--]; + sharedString->c_str[writeIndex--]='\\'; + } + else + { + sharedString->c_str[writeIndex--]=sharedString->c_str[readIndex--]; + } + } + return *this; +} +RakNet::RakString RakString::FormatForPUTOrPost(const char* type, const char* uri, const char* contentType, const char* body, const char* extraHeaders) +{ + RakString out; + RakString host; + RakString remotePath; + RakNet::RakString header; + RakString uriRs; + uriRs = uri; + uriRs.SplitURI(header, host, remotePath); + + if (host.IsEmpty() || remotePath.IsEmpty()) + return out; + +// RakString bodyEncoded = body; +// bodyEncoded.URLEncode(); + + if (extraHeaders!=0 && extraHeaders[0]) + { + out.Set("%s %s HTTP/1.1\r\n" + "%s\r\n" + "Host: %s\r\n" + "Content-Type: %s\r\n" + "Content-Length: %u\r\n" + "\r\n" + "%s", + type, + remotePath.C_String(), + extraHeaders, + host.C_String(), + contentType, + //bodyEncoded.GetLength(), + //bodyEncoded.C_String()); + strlen(body), + body); + } + else + { + out.Set("%s %s HTTP/1.1\r\n" + "Host: %s\r\n" + "Content-Type: %s\r\n" + "Content-Length: %u\r\n" + "\r\n" + "%s", + type, + remotePath.C_String(), + host.C_String(), + contentType, + //bodyEncoded.GetLength(), + //bodyEncoded.C_String()); + strlen(body), + body); + } + + return out; +} +RakString RakString::FormatForPOST(const char* uri, const char* contentType, const char* body, const char* extraHeaders) +{ + return FormatForPUTOrPost("POST", uri, contentType, body, extraHeaders); +} +RakString RakString::FormatForPUT(const char* uri, const char* contentType, const char* body, const char* extraHeaders) +{ + return FormatForPUTOrPost("PUT", uri, contentType, body, extraHeaders); +} +RakString RakString::FormatForGET(const char* uri, const char* extraHeaders) +{ + RakString out; + RakString host; + RakString remotePath; + RakNet::RakString header; + RakNet::RakString uriRs; + uriRs = uri; + + uriRs.SplitURI(header, host, remotePath); + if (host.IsEmpty() || remotePath.IsEmpty()) + return out; + + if (extraHeaders && extraHeaders[0]) + { + out.Set("GET %s HTTP/1.1\r\n" + "%s\r\n" + "Host: %s\r\n" + "\r\n", + remotePath.C_String(), + extraHeaders, + host.C_String()); + } + else + { + out.Set("GET %s HTTP/1.1\r\n" + "Host: %s\r\n" + "\r\n", + remotePath.C_String(), + host.C_String()); + + } + + + return out; +} +RakString RakString::FormatForDELETE(const char* uri, const char* extraHeaders) +{ + RakString out; + RakString host; + RakString remotePath; + RakNet::RakString header; + RakNet::RakString uriRs; + uriRs = uri; + + uriRs.SplitURI(header, host, remotePath); + if (host.IsEmpty() || remotePath.IsEmpty()) + return out; + + if (extraHeaders && extraHeaders[0]) + { + out.Set("DELETE %s HTTP/1.1\r\n" + "%s\r\n" + "Content-Length: 0\r\n" + "Host: %s\r\n" + "Connection: close\r\n" + "\r\n", + remotePath.C_String(), + extraHeaders, + host.C_String()); + } + else + { + out.Set("DELETE %s HTTP/1.1\r\n" + "Content-Length: 0\r\n" + "Host: %s\r\n" + "Connection: close\r\n" + "\r\n", + remotePath.C_String(), + host.C_String()); + } + + return out; +} +RakNet::RakString& RakString::MakeFilePath(void) +{ + if (IsEmpty()) + return *this; + + RakNet::RakString fixedString = *this; + fixedString.Clone(); + for (int i=0; fixedString.sharedString->c_str[i]; i++) + { +#ifdef _WIN32 + if (fixedString.sharedString->c_str[i]=='/') + fixedString.sharedString->c_str[i]='\\'; +#else + if (fixedString.sharedString->c_str[i]=='\\') + fixedString.sharedString->c_str[i]='/'; +#endif + } + +#ifdef _WIN32 + if (fixedString.sharedString->c_str[strlen(fixedString.sharedString->c_str)-1]!='\\') + { + fixedString+='\\'; + } +#else + if (fixedString.sharedString->c_str[strlen(fixedString.sharedString->c_str)-1]!='/') + { + fixedString+='/'; + } +#endif + + if (fixedString!=*this) + *this = fixedString; + return *this; +} +void RakString::FreeMemory(void) +{ + LockMutex(); + FreeMemoryNoMutex(); + UnlockMutex(); +} +void RakString::FreeMemoryNoMutex(void) +{ + for (unsigned int i=0; i < freeList.Size(); i++) + { + RakNet::OP_DELETE(freeList[i]->refCountMutex,_FILE_AND_LINE_); + rakFree_Ex(freeList[i], _FILE_AND_LINE_ ); + } + freeList.Clear(false, _FILE_AND_LINE_); +} +void RakString::Serialize(BitStream *bs) const +{ + Serialize(sharedString->c_str, bs); +} +void RakString::Serialize(const char *str, BitStream *bs) +{ + unsigned short l = (unsigned short) strlen(str); + bs->Write(l); + bs->WriteAlignedBytes((const unsigned char*) str, (const unsigned int) l); +} +void RakString::SerializeCompressed(BitStream *bs, uint8_t languageId, bool writeLanguageId) const +{ + SerializeCompressed(C_String(), bs, languageId, writeLanguageId); +} +void RakString::SerializeCompressed(const char *str, BitStream *bs, uint8_t languageId, bool writeLanguageId) +{ + if (writeLanguageId) + bs->WriteCompressed(languageId); + StringCompressor::Instance()->EncodeString(str,0xFFFF,bs,languageId); +} +bool RakString::Deserialize(BitStream *bs) +{ + Clear(); + + bool b; + unsigned short l; + b=bs->Read(l); + if (l>0) + { + Allocate(((unsigned int) l)+1); + b=bs->ReadAlignedBytes((unsigned char*) sharedString->c_str, l); + if (b) + sharedString->c_str[l]=0; + else + Clear(); + } + else + bs->AlignReadToByteBoundary(); + return b; +} +bool RakString::Deserialize(char *str, BitStream *bs) +{ + bool b; + unsigned short l; + b=bs->Read(l); + if (b && l>0) + b=bs->ReadAlignedBytes((unsigned char*) str, l); + + if (b==false) + str[0]=0; + + str[l]=0; + return b; +} +bool RakString::DeserializeCompressed(BitStream *bs, bool readLanguageId) +{ + uint8_t languageId; + if (readLanguageId) + bs->ReadCompressed(languageId); + else + languageId=0; + return StringCompressor::Instance()->DecodeString(this,0xFFFF,bs,languageId); +} +bool RakString::DeserializeCompressed(char *str, BitStream *bs, bool readLanguageId) +{ + uint8_t languageId; + if (readLanguageId) + bs->ReadCompressed(languageId); + else + languageId=0; + return StringCompressor::Instance()->DecodeString(str,0xFFFF,bs,languageId); +} +const char *RakString::ToString(int64_t i) +{ + static int index=0; + static char buff[64][64]; +#if defined(_WIN32) + sprintf(buff[index], "%I64d", i); +#else + sprintf(buff[index], "%lld", (long long unsigned int) i); +#endif + int lastIndex=index; + if (++index==64) + index=0; + return buff[lastIndex]; +} +const char *RakString::ToString(uint64_t i) +{ + static int index=0; + static char buff[64][64]; +#if defined(_WIN32) + sprintf(buff[index], "%I64u", i); +#else + sprintf(buff[index], "%llu", (long long unsigned int) i); +#endif + int lastIndex=index; + if (++index==64) + index=0; + return buff[lastIndex]; +} +void RakString::Clear(void) +{ + Free(); +} +void RakString::Allocate(size_t len) +{ + RakString::LockMutex(); + // sharedString = RakString::pool.Allocate( _FILE_AND_LINE_ ); + if (RakString::freeList.Size()==0) + { + //RakString::sharedStringFreeList=(RakString::SharedString*) rakRealloc_Ex(RakString::sharedStringFreeList,(RakString::sharedStringFreeListAllocationCount+1024)*sizeof(RakString::SharedString), _FILE_AND_LINE_); + unsigned i; + for (i=0; i < 128; i++) + { + // RakString::freeList.Insert(RakString::sharedStringFreeList+i+RakString::sharedStringFreeListAllocationCount); + // RakString::freeList.Insert((RakString::SharedString*)rakMalloc_Ex(sizeof(RakString::SharedString), _FILE_AND_LINE_), _FILE_AND_LINE_); + + RakString::SharedString *ss; + ss = (RakString::SharedString*) rakMalloc_Ex(sizeof(RakString::SharedString), _FILE_AND_LINE_); + ss->refCountMutex=RakNet::OP_NEW(_FILE_AND_LINE_); + RakString::freeList.Insert(ss, _FILE_AND_LINE_); + } + //RakString::sharedStringFreeListAllocationCount+=1024; + } + sharedString = RakString::freeList[RakString::freeList.Size()-1]; + RakString::freeList.RemoveAtIndex(RakString::freeList.Size()-1); + RakString::UnlockMutex(); + + const size_t smallStringSize = 128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2; + sharedString->refCount=1; + if (len <= smallStringSize) + { + sharedString->bytesUsed=smallStringSize; + sharedString->c_str=sharedString->smallString; + } + else + { + sharedString->bytesUsed=len<<1; + sharedString->bigString=(char*)rakMalloc_Ex(sharedString->bytesUsed, _FILE_AND_LINE_); + sharedString->c_str=sharedString->bigString; + } +} +void RakString::Assign(const char *str) +{ + if (str==0 || str[0]==0) + { + sharedString=&emptyString; + return; + } + + size_t len = strlen(str)+1; + Allocate(len); + memcpy(sharedString->c_str, str, len); +} +void RakString::Assign(const char *str, va_list ap) +{ + if (str==0 || str[0]==0) + { + sharedString=&emptyString; + return; + } + + char stackBuff[512]; + if (_vsnprintf(stackBuff, 512, str, ap)!=-1 +#ifndef _WIN32 + // Here Windows will return -1 if the string is too long; Linux just truncates the string. + && strlen(str) <511 +#endif + ) + { + Assign(stackBuff); + return; + } + char *buff=0, *newBuff; + size_t buffSize=8096; + while (1) + { + newBuff = (char*) rakRealloc_Ex(buff, buffSize,__FILE__,__LINE__); + if (newBuff==0) + { + notifyOutOfMemory(_FILE_AND_LINE_); + if (buff!=0) + { + Assign(buff); + rakFree_Ex(buff,__FILE__,__LINE__); + } + else + { + Assign(stackBuff); + } + return; + } + buff=newBuff; + if (_vsnprintf(buff, buffSize, str, ap)!=-1) + { + Assign(buff); + rakFree_Ex(buff,__FILE__,__LINE__); + return; + } + buffSize*=2; + } +} +RakNet::RakString RakString::Assign(const char *str,size_t pos, size_t n ) +{ + size_t incomingLen=strlen(str); + + Clone(); + + if (str==0 || str[0]==0||pos>=incomingLen) + { + sharedString=&emptyString; + return (*this); + } + + if (pos+n>=incomingLen) + { + n=incomingLen-pos; + + } + const char * tmpStr=&(str[pos]); + + size_t len = n+1; + Allocate(len); + memcpy(sharedString->c_str, tmpStr, len); + sharedString->c_str[n]=0; + + return (*this); +} + +RakNet::RakString RakString::NonVariadic(const char *str) +{ + RakNet::RakString rs; + rs=str; + return rs; +} +unsigned long RakString::ToInteger(const char *str) +{ + unsigned long hash = 0; + int c; + + while ((c = *str++)) + hash = c + (hash << 6) + (hash << 16) - hash; + + return hash; +} +unsigned long RakString::ToInteger(const RakString &rs) +{ + return RakString::ToInteger(rs.C_String()); +} +int RakString::ReadIntFromSubstring(const char *str, size_t pos, size_t n) +{ + char tmp[32]; + if (n >= 32) + return 0; + for (size_t i=0; i < n; i++) + tmp[i]=str[i+pos]; + return atoi(tmp); +} +void RakString::AppendBytes(const char *bytes, unsigned int count) +{ + if (IsEmpty()) + { + Allocate(count); + // Only copy `count` bytes; the null terminator is set explicitly + // on the next line. Copying count+1 would read one byte past the + // caller's buffer and could overflow the internal smallString buffer + // when count == smallStringSize. + memcpy(sharedString->c_str, bytes, count); + sharedString->c_str[count]=0; + } + else + { + Clone(); + unsigned int length=(unsigned int) GetLength(); + Realloc(sharedString, count+length+1); + memcpy(sharedString->c_str+length, bytes, count); + sharedString->c_str[length+count]=0; + } + + +} +void RakString::Clone(void) +{ + RakAssert(sharedString!=&emptyString); + if (sharedString==&emptyString) + { + return; + } + + // Empty or solo then no point to cloning + sharedString->refCountMutex->Lock(); + if (sharedString->refCount==1) + { + sharedString->refCountMutex->Unlock(); + return; + } + + sharedString->refCount--; + sharedString->refCountMutex->Unlock(); + Assign(sharedString->c_str); +} +void RakString::Free(void) +{ + if (sharedString==&emptyString) + return; + sharedString->refCountMutex->Lock(); + sharedString->refCount--; + if (sharedString->refCount==0) + { + sharedString->refCountMutex->Unlock(); + const size_t smallStringSize = 128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2; + if (sharedString->bytesUsed>smallStringSize) + rakFree_Ex(sharedString->bigString, _FILE_AND_LINE_ ); + /* + poolMutex->Lock(); + pool.Release(sharedString); + poolMutex->Unlock(); + */ + + RakString::LockMutex(); + RakString::freeList.Insert(sharedString, _FILE_AND_LINE_); + RakString::UnlockMutex(); + + sharedString=&emptyString; + } + else + { + sharedString->refCountMutex->Unlock(); + } + sharedString=&emptyString; +} +unsigned char RakString::ToLower(unsigned char c) +{ + if (c >= 'A' && c <= 'Z') + return c-'A'+'a'; + return c; +} +unsigned char RakString::ToUpper(unsigned char c) +{ + if (c >= 'a' && c <= 'z') + return c-'a'+'A'; + return c; +} +void RakString::LockMutex(void) +{ + GetPoolMutex().Lock(); +} +void RakString::UnlockMutex(void) +{ + GetPoolMutex().Unlock(); +} + +/* +#include "RakString.h" +#include +#include "GetTime.h" + +using namespace RakNet; + +int main(void) +{ + RakString s3("Hello world"); + RakString s5=s3; + + RakString s1; + RakString s2('a'); + + RakString s4("%i %f", 5, 6.0); + + RakString s6=s3; + RakString s7=s6; + RakString s8=s6; + RakString s9; + s9=s9; + RakString s10(s3); + RakString s11=s10 + s4 + s9 + s2; + s11+=RakString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + RakString s12("Test"); + s12+=s11; + bool b1 = s12==s12; + s11=s5; + s12.ToUpper(); + s12.ToLower(); + RakString s13; + bool b3 = s13.IsEmpty(); + s13.Set("blah %s", s12.C_String()); + bool b4 = s13.IsEmpty(); + size_t i1=s13.GetLength(); + s3.Clear(_FILE_AND_LINE_); + s4.Clear(_FILE_AND_LINE_); + s5.Clear(_FILE_AND_LINE_); + s5.Clear(_FILE_AND_LINE_); + s6.Printf(); + s7.Printf(); + RAKNET_DEBUG_PRINTF("\n"); + + static const int repeatCount=750; + DataStructures::List rakStringList; + DataStructures::List stdStringList; + DataStructures::List referenceStringList; + char *c; + unsigned i; + RakNet::TimeMS beforeReferenceList, beforeRakString, beforeStdString, afterStdString; + + unsigned loop; + for (loop=0; loop<2; loop++) + { + beforeReferenceList=RakNet::GetTimeMS(); + for (i=0; i < repeatCount; i++) + { + c = RakNet::OP_NEW_ARRAY(56,_FILE_AND_LINE_ ); + strcpy(c, "Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd"); + referenceStringList.Insert(c); + } + beforeRakString=RakNet::GetTimeMS(); + for (i=0; i < repeatCount; i++) + rakStringList.Insert("Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd"); + beforeStdString=RakNet::GetTimeMS(); + + for (i=0; i < repeatCount; i++) + stdStringList.Insert("Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd"); + afterStdString=RakNet::GetTimeMS(); + RAKNET_DEBUG_PRINTF("Insertion 1 Ref=%i Rak=%i, Std=%i\n", beforeRakString-beforeReferenceList, beforeStdString-beforeRakString, afterStdString-beforeStdString); + + beforeReferenceList=RakNet::GetTimeMS(); + for (i=0; i < repeatCount; i++) + { + RakNet::OP_DELETE_ARRAY(referenceStringList[0], _FILE_AND_LINE_); + referenceStringList.RemoveAtIndex(0); + } + beforeRakString=RakNet::GetTimeMS(); + for (i=0; i < repeatCount; i++) + rakStringList.RemoveAtIndex(0); + beforeStdString=RakNet::GetTimeMS(); + for (i=0; i < repeatCount; i++) + stdStringList.RemoveAtIndex(0); + afterStdString=RakNet::GetTimeMS(); + RAKNET_DEBUG_PRINTF("RemoveHead Ref=%i Rak=%i, Std=%i\n", beforeRakString-beforeReferenceList, beforeStdString-beforeRakString, afterStdString-beforeStdString); + + beforeReferenceList=RakNet::GetTimeMS(); + for (i=0; i < repeatCount; i++) + { + c = RakNet::OP_NEW_ARRAY(56, _FILE_AND_LINE_ ); + strcpy(c, "Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd"); + referenceStringList.Insert(0); + } + beforeRakString=RakNet::GetTimeMS(); + for (i=0; i < repeatCount; i++) + rakStringList.Insert("Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd"); + beforeStdString=RakNet::GetTimeMS(); + for (i=0; i < repeatCount; i++) + stdStringList.Insert("Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd"); + afterStdString=RakNet::GetTimeMS(); + RAKNET_DEBUG_PRINTF("Insertion 2 Ref=%i Rak=%i, Std=%i\n", beforeRakString-beforeReferenceList, beforeStdString-beforeRakString, afterStdString-beforeStdString); + + beforeReferenceList=RakNet::GetTimeMS(); + for (i=0; i < repeatCount; i++) + { + RakNet::OP_DELETE_ARRAY(referenceStringList[referenceStringList.Size()-1], _FILE_AND_LINE_); + referenceStringList.RemoveAtIndex(referenceStringList.Size()-1); + } + beforeRakString=RakNet::GetTimeMS(); + for (i=0; i < repeatCount; i++) + rakStringList.RemoveAtIndex(rakStringList.Size()-1); + beforeStdString=RakNet::GetTimeMS(); + for (i=0; i < repeatCount; i++) + stdStringList.RemoveAtIndex(stdStringList.Size()-1); + afterStdString=RakNet::GetTimeMS(); + RAKNET_DEBUG_PRINTF("RemoveTail Ref=%i Rak=%i, Std=%i\n", beforeRakString-beforeReferenceList, beforeStdString-beforeRakString, afterStdString-beforeStdString); + + } + + printf("Done."); + char str[128]; + Gets(str, sizeof(str)); + return 1; +} +*/ diff --git a/Source/RakString.h b/Source/RakString.h index 3e4e9398f..104a528a1 100644 --- a/Source/RakString.h +++ b/Source/RakString.h @@ -1,354 +1,352 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __RAK_STRING_H -#define __RAK_STRING_H - -#include "Export.h" -#include "DS_List.h" -#include "RakNetTypes.h" // int64_t -#include -#include "stdarg.h" - - -#ifdef _WIN32 - - - -#include "WindowsIncludes.h" -#endif - -namespace RakNet -{ -/// Forward declarations -class SimpleMutex; -class BitStream; - -/// \brief String class -/// \details Has the following improvements over std::string -/// -Reference counting: Suitable to store in lists -/// -Variadic assignment operator -/// -Doesn't cause linker errors -class RAK_DLL_EXPORT RakString -{ -public: - // Constructors - RakString(); - RakString(char input); - RakString(unsigned char input); - RakString(const unsigned char *format, ...); - RakString(const char *format, ...); - ~RakString(); - RakString( const RakString & rhs); - - /// Implicit return of const char* - operator const char* () const {return sharedString->c_str;} - - /// Same as std::string::c_str - const char *C_String(void) const {return sharedString->c_str;} - - // Lets you modify the string. Do not make the string longer - however, you can make it shorter, or change the contents. - // Pointer is only valid in the scope of RakString itself - char *C_StringUnsafe(void) {Clone(); return sharedString->c_str;} - - /// Assigment operators - RakString& operator = ( const RakString& rhs ); - RakString& operator = ( const char *str ); - RakString& operator = ( char *str ); - RakString& operator = ( const unsigned char *str ); - RakString& operator = ( char unsigned *str ); - RakString& operator = ( const char c ); - - /// Concatenation - RakString& operator +=( const RakString& rhs); - RakString& operator += ( const char *str ); - RakString& operator += ( char *str ); - RakString& operator += ( const unsigned char *str ); - RakString& operator += ( char unsigned *str ); - RakString& operator += ( const char c ); - - /// Character index. Do not use to change the string however. - unsigned char operator[] ( const unsigned int position ) const; - -#ifdef _WIN32 - // Return as Wide char - // Deallocate with DeallocWideChar - WCHAR * ToWideChar(void); - void DeallocWideChar(WCHAR * w); - - void FromWideChar(const wchar_t *source); - static RakNet::RakString FromWideChar_S(const wchar_t *source); -#endif - - /// String class find replacement - /// Searches the string for the content specified in stringToFind and returns the position of the first occurrence in the string. - /// Search only includes characters on or after position pos, ignoring any possible occurrences in previous locations. - /// \param[in] stringToFind The string to find inside of this object's string - /// \param[in] pos The position in the string to start the search - /// \return Returns the position of the first occurrence in the string. - size_t Find(const char *stringToFind,size_t pos = 0 ); - - /// Equality - bool operator==(const RakString &rhs) const; - bool operator==(const char *str) const; - bool operator==(char *str) const; - - // Comparison - bool operator < ( const RakString& right ) const; - bool operator <= ( const RakString& right ) const; - bool operator > ( const RakString& right ) const; - bool operator >= ( const RakString& right ) const; - - /// Inequality - bool operator!=(const RakString &rhs) const; - bool operator!=(const char *str) const; - bool operator!=(char *str) const; - - /// Change all characters to lowercase - const char * ToLower(void); - - /// Change all characters to uppercase - const char * ToUpper(void); - - /// Set the value of the string - void Set(const char *format, ...); - - /// Sets a copy of a substring of str as the new content. The substring is the portion of str - /// that begins at the character position pos and takes up to n characters - /// (it takes less than n if the end of str is reached before). - /// \param[in] str The string to copy in - /// \param[in] pos The position on str to start the copy - /// \param[in] n How many chars to copy - /// \return Returns the string, note that the current string is set to that value as well - RakString Assign(const char *str,size_t pos, size_t n ); - - /// Returns if the string is empty. Also, C_String() would return "" - bool IsEmpty(void) const; - - /// Returns the length of the string - size_t GetLength(void) const; - size_t GetLengthUTF8(void) const; - - /// Replace character(s) in starting at index, for count, with c - void Replace(unsigned index, unsigned count, unsigned char c); - - /// Replace character at index with c - void SetChar( unsigned index, unsigned char c ); - - /// Replace character at index with string s - void SetChar( unsigned index, RakNet::RakString s ); - - /// Make sure string is no longer than \a length - void Truncate(unsigned int length); - void TruncateUTF8(unsigned int length); - - // Gets the substring starting at index for count characters - RakString SubStr(unsigned int index, unsigned int count) const; - - /// Erase characters out of the string at index for count - void Erase(unsigned int index, unsigned int count); - - /// Set the first instance of c with a NULL terminator - void TerminateAtFirstCharacter(char c); - /// Set the last instance of c with a NULL terminator - void TerminateAtLastCharacter(char c); - - void StartAfterFirstCharacter(char c); - void StartAfterLastCharacter(char c); - - /// Returns how many occurances there are of \a c in the string - int GetCharacterCount(char c); - - /// Remove all instances of c - void RemoveCharacter(char c); - - /// Create a RakString with a value, without doing printf style parsing - /// Equivalent to assignment operator - static RakNet::RakString NonVariadic(const char *str); - - /// Hash the string into an unsigned int - static unsigned long ToInteger(const char *str); - static unsigned long ToInteger(const RakString &rs); - - /// \brief Read an integer out of a substring - /// \param[in] str The string - /// \param[in] pos The position on str where the integer starts - /// \param[in] n How many chars to copy - static int ReadIntFromSubstring(const char *str, size_t pos, size_t n); - - // Like strncat, but for a fixed length - void AppendBytes(const char *bytes, unsigned int count); - - /// Compare strings (case sensitive) - int StrCmp(const RakString &rhs) const; - - /// Compare strings (case sensitive), up to num characters - int StrNCmp(const RakString &rhs, size_t num) const; - - /// Compare strings (not case sensitive) - int StrICmp(const RakString &rhs) const; - - /// Clear the string - void Clear(void); - - /// Print the string to the screen - void Printf(void); - - /// Print the string to a file - void FPrintf(FILE *fp); - - /// Does the given IP address match the IP address encoded into this string, accounting for wildcards? - bool IPAddressMatch(const char *IP); - - /// Does the string contain non-printable characters other than spaces? - bool ContainsNonprintableExceptSpaces(void) const; - - /// Is this a valid email address? - bool IsEmailAddress(void) const; - - /// URL Encode the string. See http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4029/ - RakNet::RakString& URLEncode(void); - - /// URL decode the string - RakNet::RakString& URLDecode(void); - - /// https://servers.api.rackspacecloud.com/v1.0 to https://, servers.api.rackspacecloud.com, /v1.0 - void SplitURI(RakNet::RakString &header, RakNet::RakString &domain, RakNet::RakString &path); - - /// Scan for quote, double quote, and backslash and prepend with backslash - RakNet::RakString& SQLEscape(void); - - /// Format as a POST command that can be sent to a webserver - /// \param[in] uri For example, masterserver2.raknet.com/testServer - /// \param[in] contentType For example, text/plain; charset=UTF-8 - /// \param[in] body Body of the post - /// \return Formatted string - static RakNet::RakString FormatForPOST(const char* uri, const char* contentType, const char* body, const char* extraHeaders=""); - static RakNet::RakString FormatForPUT(const char* uri, const char* contentType, const char* body, const char* extraHeaders=""); - - /// Format as a GET command that can be sent to a webserver - /// \param[in] uri For example, masterserver2.raknet.com/testServer?__gameId=comprehensivePCGame - /// \return Formatted string - static RakNet::RakString FormatForGET(const char* uri, const char* extraHeaders=""); - - /// Format as a DELETE command that can be sent to a webserver - /// \param[in] uri For example, masterserver2.raknet.com/testServer?__gameId=comprehensivePCGame&__rowId=1 - /// \return Formatted string - static RakNet::RakString FormatForDELETE(const char* uri, const char* extraHeaders=""); - - /// Fix to be a file path, ending with / - RakNet::RakString& MakeFilePath(void); - - /// RakString uses a freeList of old no-longer used strings - /// Call this function to clear this memory on shutdown - static void FreeMemory(void); - /// \internal - static void FreeMemoryNoMutex(void); - - /// Serialize to a bitstream, uncompressed (slightly faster) - /// \param[out] bs Bitstream to serialize to - void Serialize(BitStream *bs) const; - - /// Static version of the Serialize function - static void Serialize(const char *str, BitStream *bs); - - /// Serialize to a bitstream, compressed (better bandwidth usage) - /// \param[out] bs Bitstream to serialize to - /// \param[in] languageId languageId to pass to the StringCompressor class - /// \param[in] writeLanguageId encode the languageId variable in the stream. If false, 0 is assumed, and DeserializeCompressed will not look for this variable in the stream (saves bandwidth) - /// \pre StringCompressor::AddReference must have been called to instantiate the class (Happens automatically from RakPeer::Startup()) - void SerializeCompressed(BitStream *bs, uint8_t languageId=0, bool writeLanguageId=false) const; - - /// Static version of the SerializeCompressed function - static void SerializeCompressed(const char *str, BitStream *bs, uint8_t languageId=0, bool writeLanguageId=false); - - /// Deserialize what was written by Serialize - /// \param[in] bs Bitstream to serialize from - /// \return true if the deserialization was successful - bool Deserialize(BitStream *bs); - - /// Static version of the Deserialize() function - static bool Deserialize(char *str, BitStream *bs); - - /// Deserialize compressed string, written by SerializeCompressed - /// \param[in] bs Bitstream to serialize from - /// \param[in] readLanguageId If true, looks for the variable langaugeId in the data stream. Must match what was passed to SerializeCompressed - /// \return true if the deserialization was successful - /// \pre StringCompressor::AddReference must have been called to instantiate the class (Happens automatically from RakPeer::Startup()) - bool DeserializeCompressed(BitStream *bs, bool readLanguageId=false); - - /// Static version of the DeserializeCompressed() function - static bool DeserializeCompressed(char *str, BitStream *bs, bool readLanguageId=false); - - static const char *ToString(int64_t i); - static const char *ToString(uint64_t i); - - /// \internal - static size_t GetSizeToAllocate(size_t bytes) - { - const size_t smallStringSize = 128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2; - if (bytes<=smallStringSize) - return smallStringSize; - else - return bytes*2; - } - - /// \internal - struct SharedString - { - SimpleMutex *refCountMutex; - unsigned int refCount; - size_t bytesUsed; - char *bigString; - char *c_str; - char smallString[128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2]; - }; - - /// \internal - RakString( SharedString *_sharedString ); - - /// \internal - SharedString *sharedString; - -// static SimpleMutex poolMutex; -// static DataStructures::MemoryPool pool; - /// \internal - static SharedString emptyString; - - //static SharedString *sharedStringFreeList; - //static unsigned int sharedStringFreeListAllocationCount; - /// \internal - /// List of free objects to reduce memory reallocations - static DataStructures::List freeList; - - static int RakStringComp( RakString const &key, RakString const &data ); - - static void LockMutex(void); - static void UnlockMutex(void); - -protected: - static RakNet::RakString FormatForPUTOrPost(const char* type, const char* uri, const char* contentType, const char* body, const char* extraHeaders); - void Allocate(size_t len); - void Assign(const char *str); - void Assign(const char *str, va_list ap); - - void Clone(void); - void Free(void); - unsigned char ToLower(unsigned char c); - unsigned char ToUpper(unsigned char c); - void Realloc(SharedString *sharedString, size_t bytes); -}; - -} - -const RakNet::RakString RAK_DLL_EXPORT operator+(const RakNet::RakString &lhs, const RakNet::RakString &rhs); - - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include "Export.h" +#include "DS_List.h" +#include "RakNetTypes.h" // int64_t +#include +#include "stdarg.h" + + +#ifdef _WIN32 + + + +#include "WindowsIncludes.h" +#endif + +namespace RakNet +{ +/// Forward declarations +class SimpleMutex; +class BitStream; + +/// \brief String class +/// \details Has the following improvements over std::string +/// -Reference counting: Suitable to store in lists +/// -Variadic assignment operator +/// -Doesn't cause linker errors +class RAK_DLL_EXPORT RakString +{ +public: + // Constructors + RakString(); + RakString(char input); + RakString(unsigned char input); + RakString(const unsigned char *format, ...); + RakString(const char *format, ...); + ~RakString(); + RakString( const RakString & rhs); + + /// Implicit return of const char* + operator const char* () const {return sharedString->c_str;} + + /// Same as std::string::c_str + const char *C_String(void) const {return sharedString->c_str;} + + // Lets you modify the string. Do not make the string longer - however, you can make it shorter, or change the contents. + // Pointer is only valid in the scope of RakString itself + char *C_StringUnsafe(void) {Clone(); return sharedString->c_str;} + + /// Assigment operators + RakString& operator = ( const RakString& rhs ); + RakString& operator = ( const char *str ); + RakString& operator = ( char *str ); + RakString& operator = ( const unsigned char *str ); + RakString& operator = ( char unsigned *str ); + RakString& operator = ( const char c ); + + /// Concatenation + RakString& operator +=( const RakString& rhs); + RakString& operator += ( const char *str ); + RakString& operator += ( char *str ); + RakString& operator += ( const unsigned char *str ); + RakString& operator += ( char unsigned *str ); + RakString& operator += ( const char c ); + + /// Character index. Do not use to change the string however. + unsigned char operator[] ( const unsigned int position ) const; + +#ifdef _WIN32 + // Return as Wide char + // Deallocate with DeallocWideChar + WCHAR * ToWideChar(void); + void DeallocWideChar(WCHAR * w); + + void FromWideChar(const wchar_t *source); + static RakNet::RakString FromWideChar_S(const wchar_t *source); +#endif + + /// String class find replacement + /// Searches the string for the content specified in stringToFind and returns the position of the first occurrence in the string. + /// Search only includes characters on or after position pos, ignoring any possible occurrences in previous locations. + /// \param[in] stringToFind The string to find inside of this object's string + /// \param[in] pos The position in the string to start the search + /// \return Returns the position of the first occurrence in the string. + size_t Find(const char *stringToFind,size_t pos = 0 ); + + /// Equality + bool operator==(const RakString &rhs) const; + bool operator==(const char *str) const; + bool operator==(char *str) const; + + // Comparison + bool operator < ( const RakString& right ) const; + bool operator <= ( const RakString& right ) const; + bool operator > ( const RakString& right ) const; + bool operator >= ( const RakString& right ) const; + + /// Inequality + bool operator!=(const RakString &rhs) const; + bool operator!=(const char *str) const; + bool operator!=(char *str) const; + + /// Change all characters to lowercase + const char * ToLower(void); + + /// Change all characters to uppercase + const char * ToUpper(void); + + /// Set the value of the string + void Set(const char *format, ...); + + /// Sets a copy of a substring of str as the new content. The substring is the portion of str + /// that begins at the character position pos and takes up to n characters + /// (it takes less than n if the end of str is reached before). + /// \param[in] str The string to copy in + /// \param[in] pos The position on str to start the copy + /// \param[in] n How many chars to copy + /// \return Returns the string, note that the current string is set to that value as well + RakString Assign(const char *str,size_t pos, size_t n ); + + /// Returns if the string is empty. Also, C_String() would return "" + bool IsEmpty(void) const; + + /// Returns the length of the string + size_t GetLength(void) const; + size_t GetLengthUTF8(void) const; + + /// Replace character(s) in starting at index, for count, with c + void Replace(unsigned index, unsigned count, unsigned char c); + + /// Replace character at index with c + void SetChar( unsigned index, unsigned char c ); + + /// Replace character at index with string s + void SetChar( unsigned index, RakNet::RakString s ); + + /// Make sure string is no longer than \a length + void Truncate(unsigned int length); + void TruncateUTF8(unsigned int length); + + // Gets the substring starting at index for count characters + RakString SubStr(unsigned int index, unsigned int count) const; + + /// Erase characters out of the string at index for count + void Erase(unsigned int index, unsigned int count); + + /// Set the first instance of c with a nullptr terminator + void TerminateAtFirstCharacter(char c); + /// Set the last instance of c with a nullptr terminator + void TerminateAtLastCharacter(char c); + + void StartAfterFirstCharacter(char c); + void StartAfterLastCharacter(char c); + + /// Returns how many occurances there are of \a c in the string + int GetCharacterCount(char c); + + /// Remove all instances of c + void RemoveCharacter(char c); + + /// Create a RakString with a value, without doing printf style parsing + /// Equivalent to assignment operator + static RakNet::RakString NonVariadic(const char *str); + + /// Hash the string into an unsigned int + static unsigned long ToInteger(const char *str); + static unsigned long ToInteger(const RakString &rs); + + /// \brief Read an integer out of a substring + /// \param[in] str The string + /// \param[in] pos The position on str where the integer starts + /// \param[in] n How many chars to copy + static int ReadIntFromSubstring(const char *str, size_t pos, size_t n); + + // Like strncat, but for a fixed length + void AppendBytes(const char *bytes, unsigned int count); + + /// Compare strings (case sensitive) + int StrCmp(const RakString &rhs) const; + + /// Compare strings (case sensitive), up to num characters + int StrNCmp(const RakString &rhs, size_t num) const; + + /// Compare strings (not case sensitive) + int StrICmp(const RakString &rhs) const; + + /// Clear the string + void Clear(void); + + /// Print the string to the screen + void Printf(void); + + /// Print the string to a file + void FPrintf(FILE *fp); + + /// Does the given IP address match the IP address encoded into this string, accounting for wildcards? + bool IPAddressMatch(const char *IP); + + /// Does the string contain non-printable characters other than spaces? + bool ContainsNonprintableExceptSpaces(void) const; + + /// Is this a valid email address? + bool IsEmailAddress(void) const; + + /// URL Encode the string. See http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4029/ + RakNet::RakString& URLEncode(void); + + /// URL decode the string + RakNet::RakString& URLDecode(void); + + /// https://servers.api.rackspacecloud.com/v1.0 to https://, servers.api.rackspacecloud.com, /v1.0 + void SplitURI(RakNet::RakString &header, RakNet::RakString &domain, RakNet::RakString &path); + + /// Scan for quote, double quote, and backslash and prepend with backslash + RakNet::RakString& SQLEscape(void); + + /// Format as a POST command that can be sent to a webserver + /// \param[in] uri For example, masterserver2.raknet.com/testServer + /// \param[in] contentType For example, text/plain; charset=UTF-8 + /// \param[in] body Body of the post + /// \return Formatted string + static RakNet::RakString FormatForPOST(const char* uri, const char* contentType, const char* body, const char* extraHeaders=""); + static RakNet::RakString FormatForPUT(const char* uri, const char* contentType, const char* body, const char* extraHeaders=""); + + /// Format as a GET command that can be sent to a webserver + /// \param[in] uri For example, masterserver2.raknet.com/testServer?__gameId=comprehensivePCGame + /// \return Formatted string + static RakNet::RakString FormatForGET(const char* uri, const char* extraHeaders=""); + + /// Format as a DELETE command that can be sent to a webserver + /// \param[in] uri For example, masterserver2.raknet.com/testServer?__gameId=comprehensivePCGame&__rowId=1 + /// \return Formatted string + static RakNet::RakString FormatForDELETE(const char* uri, const char* extraHeaders=""); + + /// Fix to be a file path, ending with / + RakNet::RakString& MakeFilePath(void); + + /// RakString uses a freeList of old no-longer used strings + /// Call this function to clear this memory on shutdown + static void FreeMemory(void); + /// \internal + static void FreeMemoryNoMutex(void); + + /// Serialize to a bitstream, uncompressed (slightly faster) + /// \param[out] bs Bitstream to serialize to + void Serialize(BitStream *bs) const; + + /// Static version of the Serialize function + static void Serialize(const char *str, BitStream *bs); + + /// Serialize to a bitstream, compressed (better bandwidth usage) + /// \param[out] bs Bitstream to serialize to + /// \param[in] languageId languageId to pass to the StringCompressor class + /// \param[in] writeLanguageId encode the languageId variable in the stream. If false, 0 is assumed, and DeserializeCompressed will not look for this variable in the stream (saves bandwidth) + /// \pre StringCompressor::AddReference must have been called to instantiate the class (Happens automatically from RakPeer::Startup()) + void SerializeCompressed(BitStream *bs, uint8_t languageId=0, bool writeLanguageId=false) const; + + /// Static version of the SerializeCompressed function + static void SerializeCompressed(const char *str, BitStream *bs, uint8_t languageId=0, bool writeLanguageId=false); + + /// Deserialize what was written by Serialize + /// \param[in] bs Bitstream to serialize from + /// \return true if the deserialization was successful + bool Deserialize(BitStream *bs); + + /// Static version of the Deserialize() function + static bool Deserialize(char *str, BitStream *bs); + + /// Deserialize compressed string, written by SerializeCompressed + /// \param[in] bs Bitstream to serialize from + /// \param[in] readLanguageId If true, looks for the variable langaugeId in the data stream. Must match what was passed to SerializeCompressed + /// \return true if the deserialization was successful + /// \pre StringCompressor::AddReference must have been called to instantiate the class (Happens automatically from RakPeer::Startup()) + bool DeserializeCompressed(BitStream *bs, bool readLanguageId=false); + + /// Static version of the DeserializeCompressed() function + static bool DeserializeCompressed(char *str, BitStream *bs, bool readLanguageId=false); + + static const char *ToString(int64_t i); + static const char *ToString(uint64_t i); + + /// \internal + static size_t GetSizeToAllocate(size_t bytes) + { + const size_t smallStringSize = 128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2; + if (bytes<=smallStringSize) + return smallStringSize; + else + return bytes*2; + } + + /// \internal + struct SharedString + { + SimpleMutex *refCountMutex; + unsigned int refCount; + size_t bytesUsed; + char *bigString; + char *c_str; + char smallString[128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2]; + }; + + /// \internal + RakString( SharedString *_sharedString ); + + /// \internal + SharedString *sharedString; + +// static SimpleMutex poolMutex; +// static DataStructures::MemoryPool pool; + /// \internal + static SharedString emptyString; + + //static SharedString *sharedStringFreeList; + //static unsigned int sharedStringFreeListAllocationCount; + /// \internal + /// List of free objects to reduce memory reallocations + static DataStructures::List freeList; + + static int RakStringComp( RakString const &key, RakString const &data ); + + static void LockMutex(void); + static void UnlockMutex(void); + +protected: + static RakNet::RakString FormatForPUTOrPost(const char* type, const char* uri, const char* contentType, const char* body, const char* extraHeaders); + void Allocate(size_t len); + void Assign(const char *str); + void Assign(const char *str, va_list ap); + + void Clone(void); + void Free(void); + unsigned char ToLower(unsigned char c); + unsigned char ToUpper(unsigned char c); + void Realloc(SharedString *sharedString, size_t bytes); +}; + +} + +const RakNet::RakString RAK_DLL_EXPORT operator+(const RakNet::RakString &lhs, const RakNet::RakString &rhs); + + diff --git a/Source/RakThread.cpp b/Source/RakThread.cpp index 12f0e6bdf..7b87eb005 100644 --- a/Source/RakThread.cpp +++ b/Source/RakThread.cpp @@ -1,178 +1,178 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "RakThread.h" -#include "RakAssert.h" -#include "RakNetDefines.h" -#include "RakSleep.h" -#include "RakMemoryOverride.h" - -using namespace RakNet; - - - - -#if defined(_WIN32) - #include "WindowsIncludes.h" - #include - #if !defined(_WIN32_WCE) - #include - #endif - - - - -#else -#include -#endif - -#if defined(_WIN32_WCE) || defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) -int RakThread::Create( LPTHREAD_START_ROUTINE start_address, void *arglist, int priority) -#elif defined(_WIN32) -int RakThread::Create( unsigned __stdcall start_address( void* ), void *arglist, int priority) - - - -#else -int RakThread::Create( void* start_address( void* ), void *arglist, int priority) -#endif -{ -#ifdef _WIN32 - HANDLE threadHandle; - unsigned threadID = 0; - - -#if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) - threadHandle = CreateThread(NULL,0,start_address,arglist,CREATE_SUSPENDED, 0); -#elif defined _WIN32_WCE - threadHandle = CreateThread(NULL,MAX_ALLOCA_STACK_ALLOCATION*2,start_address,arglist,0,(DWORD*)&threadID); -#else - threadHandle = (HANDLE) _beginthreadex( NULL, MAX_ALLOCA_STACK_ALLOCATION*2, start_address, arglist, 0, &threadID ); -#endif - - SetThreadPriority(threadHandle, priority); - -#if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) - ResumeThread(threadHandle); -#endif - - if (threadHandle==0) - { - return 1; - } - else - { - CloseHandle( threadHandle ); - return 0; - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -#else - pthread_t threadHandle; - // Create thread linux - pthread_attr_t attr; - sched_param param; - param.sched_priority=priority; - pthread_attr_init( &attr ); - pthread_attr_setschedparam(&attr, ¶m); - - - - - - pthread_attr_setstacksize(&attr, MAX_ALLOCA_STACK_ALLOCATION*2); - - pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED ); - int res = pthread_create( &threadHandle, &attr, start_address, arglist ); - RakAssert(res==0 && "pthread_create in RakThread.cpp failed.") - return res; -#endif -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "RakThread.h" +#include "RakAssert.h" +#include "RakNetDefines.h" +#include "RakSleep.h" +#include "RakMemoryOverride.h" + +using namespace RakNet; + + + + +#if defined(_WIN32) + #include "WindowsIncludes.h" + #include + #if !defined(_WIN32_WCE) + #include + #endif + + + + +#else +#include +#endif + +#if defined(_WIN32_WCE) || defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) +int RakThread::Create( LPTHREAD_START_ROUTINE start_address, void *arglist, int priority) +#elif defined(_WIN32) +int RakThread::Create( unsigned __stdcall start_address( void* ), void *arglist, int priority) + + + +#else +int RakThread::Create( void* start_address( void* ), void *arglist, int priority) +#endif +{ +#ifdef _WIN32 + HANDLE threadHandle; + unsigned threadID = 0; + + +#if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) + threadHandle = CreateThread(nullptr,0,start_address,arglist,CREATE_SUSPENDED, 0); +#elif defined _WIN32_WCE + threadHandle = CreateThread(nullptr,MAX_ALLOCA_STACK_ALLOCATION*2,start_address,arglist,0,(DWORD*)&threadID); +#else + threadHandle = (HANDLE) _beginthreadex( nullptr, MAX_ALLOCA_STACK_ALLOCATION*2, start_address, arglist, 0, &threadID ); +#endif + + SetThreadPriority(threadHandle, priority); + +#if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) + ResumeThread(threadHandle); +#endif + + if (threadHandle==0) + { + return 1; + } + else + { + CloseHandle( threadHandle ); + return 0; + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#else + pthread_t threadHandle; + // Create thread linux + pthread_attr_t attr; + sched_param param; + param.sched_priority=priority; + pthread_attr_init( &attr ); + pthread_attr_setschedparam(&attr, ¶m); + + + + + + pthread_attr_setstacksize(&attr, MAX_ALLOCA_STACK_ALLOCATION*2); + + pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED ); + int res = pthread_create( &threadHandle, &attr, start_address, arglist ); + RakAssert(res==0 && "pthread_create in RakThread.cpp failed.") + return res; +#endif +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Source/RakThread.h b/Source/RakThread.h index b52361883..9dacc1160 100644 --- a/Source/RakThread.h +++ b/Source/RakThread.h @@ -1,106 +1,104 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __RAK_THREAD_H -#define __RAK_THREAD_H - -#if defined(_WIN32_WCE) -#include "WindowsIncludes.h" -#endif - - - - - -#include "Export.h" - - - - - - -#if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) -#include "../DependentExtensions/WinPhone8/ThreadEmulation.h" -using namespace ThreadEmulation; -#endif - -namespace RakNet -{ -/// To define a thread, use RAK_THREAD_DECLARATION(functionName); -#if defined(_WIN32_WCE) || defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) -#define RAK_THREAD_DECLARATION(functionName) DWORD WINAPI functionName(LPVOID arguments) - - -#elif defined(_WIN32) -#define RAK_THREAD_DECLARATION(functionName) unsigned __stdcall functionName( void* arguments ) - - -#else -#define RAK_THREAD_DECLARATION(functionName) void* functionName( void* arguments ) -#endif - -class RAK_DLL_EXPORT RakThread -{ -public: - - - - - /// Create a thread, simplified to be cross platform without all the extra junk - /// To then start that thread, call RakCreateThread(functionName, arguments); - /// \param[in] start_address Function you want to call - /// \param[in] arglist Arguments to pass to the function - /// \return 0=success. >0 = error code - - /* - nice value Win32 Priority - -20 to -16 THREAD_PRIORITY_HIGHEST - -15 to -6 THREAD_PRIORITY_ABOVE_NORMAL - -5 to +4 THREAD_PRIORITY_NORMAL - +5 to +14 THREAD_PRIORITY_BELOW_NORMAL - +15 to +19 THREAD_PRIORITY_LOWEST - */ -#if defined(_WIN32_WCE) || defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) - static int Create( LPTHREAD_START_ROUTINE start_address, void *arglist, int priority=0); - - -#elif defined(_WIN32) - static int Create( unsigned __stdcall start_address( void* ), void *arglist, int priority=0); - - - -#else - static int Create( void* start_address( void* ), void *arglist, int priority=0); -#endif - - - - - - - - - - - - - - - - - - - - -}; - -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#if defined(_WIN32_WCE) +#include "WindowsIncludes.h" +#endif + + + + + +#include "Export.h" + + + + + + +#if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) +#include "../DependentExtensions/WinPhone8/ThreadEmulation.h" +using namespace ThreadEmulation; +#endif + +namespace RakNet +{ +/// To define a thread, use RAK_THREAD_DECLARATION(functionName); +#if defined(_WIN32_WCE) || defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) +#define RAK_THREAD_DECLARATION(functionName) DWORD WINAPI functionName(LPVOID arguments) + + +#elif defined(_WIN32) +#define RAK_THREAD_DECLARATION(functionName) unsigned __stdcall functionName( void* arguments ) + + +#else +#define RAK_THREAD_DECLARATION(functionName) void* functionName( void* arguments ) +#endif + +class RAK_DLL_EXPORT RakThread +{ +public: + + + + + /// Create a thread, simplified to be cross platform without all the extra junk + /// To then start that thread, call RakCreateThread(functionName, arguments); + /// \param[in] start_address Function you want to call + /// \param[in] arglist Arguments to pass to the function + /// \return 0=success. >0 = error code + + /* + nice value Win32 Priority + -20 to -16 THREAD_PRIORITY_HIGHEST + -15 to -6 THREAD_PRIORITY_ABOVE_NORMAL + -5 to +4 THREAD_PRIORITY_NORMAL + +5 to +14 THREAD_PRIORITY_BELOW_NORMAL + +15 to +19 THREAD_PRIORITY_LOWEST + */ +#if defined(_WIN32_WCE) || defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) + static int Create( LPTHREAD_START_ROUTINE start_address, void *arglist, int priority=0); + + +#elif defined(_WIN32) + static int Create( unsigned __stdcall start_address( void* ), void *arglist, int priority=0); + + + +#else + static int Create( void* start_address( void* ), void *arglist, int priority=0); +#endif + + + + + + + + + + + + + + + + + + + + +}; + +} + diff --git a/Source/RakWString.cpp b/Source/RakWString.cpp index 263eeca42..f8bf22518 100644 --- a/Source/RakWString.cpp +++ b/Source/RakWString.cpp @@ -1,412 +1,412 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "RakWString.h" -#include "BitStream.h" -#include -#include -#include - -using namespace RakNet; - -// From http://www.joelonsoftware.com/articles/Unicode.html -// Only code points 128 and above are stored using 2, 3, in fact, up to 6 bytes. -#define MAX_BYTES_PER_UNICODE_CHAR sizeof(wchar_t) - -RakWString::RakWString() -{ - c_str=0; - c_strCharLength=0; -} -RakWString::RakWString( const RakString &right ) -{ - c_str=0; - c_strCharLength=0; - *this=right; -} -RakWString::RakWString( const char *input ) -{ - c_str=0; - c_strCharLength=0; - *this = input; -} -RakWString::RakWString( const wchar_t *input ) -{ - c_str=0; - c_strCharLength=0; - *this = input; -} -RakWString::RakWString( const RakWString & right) -{ - c_str=0; - c_strCharLength=0; - *this = right; -} -RakWString::~RakWString() -{ - rakFree_Ex(c_str,_FILE_AND_LINE_); -} -RakWString& RakWString::operator = ( const RakWString& right ) -{ - Clear(); - if (right.IsEmpty()) - return *this; - c_str = (wchar_t *) rakMalloc_Ex( (right.GetLength() + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); - if (!c_str) - { - c_strCharLength=0; - notifyOutOfMemory(_FILE_AND_LINE_); - return *this; - } - c_strCharLength = right.GetLength(); - memcpy(c_str,right.C_String(),(right.GetLength() + 1) * MAX_BYTES_PER_UNICODE_CHAR); - - return *this; -} -RakWString& RakWString::operator = ( const RakString& right ) -{ - return *this = right.C_String(); -} -RakWString& RakWString::operator = ( const wchar_t * const str ) -{ - Clear(); - if (str==0) - return *this; - c_strCharLength = wcslen(str); - if (c_strCharLength==0) - return *this; - c_str = (wchar_t *) rakMalloc_Ex( (c_strCharLength + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); - if (!c_str) - { - c_strCharLength=0; - notifyOutOfMemory(_FILE_AND_LINE_); - return *this; - } - wcscpy(c_str,str); - - return *this; -} -RakWString& RakWString::operator = ( wchar_t *str ) -{ - *this = ( const wchar_t * const) str; - return *this; -} -RakWString& RakWString::operator = ( const char * const str ) -{ - Clear(); - -// Not supported on android -#if !defined(ANDROID) - if (str==0) - return *this; - if (str[0]==0) - return *this; - - c_strCharLength = mbstowcs(NULL, str, 0); - c_str = (wchar_t *) rakMalloc_Ex( (c_strCharLength + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); - if (!c_str) - { - c_strCharLength=0; - notifyOutOfMemory(_FILE_AND_LINE_); - return *this; - } - - c_strCharLength = mbstowcs(c_str, str, c_strCharLength+1); - if (c_strCharLength == (size_t) (-1)) - { - RAKNET_DEBUG_PRINTF("Couldn't convert string--invalid multibyte character.\n"); - Clear(); - return *this; - } -#else - // mbstowcs not supported on android - RakAssert("mbstowcs not supported on Android" && 0); -#endif // defined(ANDROID) - - return *this; -} -RakWString& RakWString::operator = ( char *str ) -{ - *this = ( const char * const) str; - return *this; -} -RakWString& RakWString::operator +=( const RakWString& right) -{ - if (right.IsEmpty()) - return *this; - size_t newCharLength = c_strCharLength + right.GetLength(); - wchar_t *newCStr; - bool isEmpty = IsEmpty(); - if (isEmpty) - newCStr = (wchar_t *) rakMalloc_Ex( (newCharLength + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); - else - newCStr = (wchar_t *) rakRealloc_Ex( c_str, (newCharLength + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); - if (!newCStr) - { - notifyOutOfMemory(_FILE_AND_LINE_); - return *this; - } - c_str = newCStr; - c_strCharLength = newCharLength; - if (isEmpty) - { - memcpy(newCStr,right.C_String(),(right.GetLength() + 1) * MAX_BYTES_PER_UNICODE_CHAR); - } - else - { - wcscat(c_str, right.C_String()); - } - - return *this; -} -RakWString& RakWString::operator += ( const wchar_t * const right ) -{ - if (right==0) - return *this; - size_t rightLength = wcslen(right); - size_t newCharLength = c_strCharLength + rightLength; - wchar_t *newCStr; - bool isEmpty = IsEmpty(); - if (isEmpty) - newCStr = (wchar_t *) rakMalloc_Ex( (newCharLength + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); - else - newCStr = (wchar_t *) rakRealloc_Ex( c_str, (newCharLength + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); - if (!newCStr) - { - notifyOutOfMemory(_FILE_AND_LINE_); - return *this; - } - c_str = newCStr; - c_strCharLength = newCharLength; - if (isEmpty) - { - memcpy(newCStr,right,(rightLength + 1) * MAX_BYTES_PER_UNICODE_CHAR); - } - else - { - wcscat(c_str, right); - } - - return *this; -} -RakWString& RakWString::operator += ( wchar_t *right ) -{ - return *this += (const wchar_t * const) right; -} -bool RakWString::operator==(const RakWString &right) const -{ - if (GetLength()!=right.GetLength()) - return false; - return wcscmp(C_String(),right.C_String())==0; -} -bool RakWString::operator < ( const RakWString& right ) const -{ - return wcscmp(C_String(),right.C_String())<0; -} -bool RakWString::operator <= ( const RakWString& right ) const -{ - return wcscmp(C_String(),right.C_String())<=0; -} -bool RakWString::operator > ( const RakWString& right ) const -{ - return wcscmp(C_String(),right.C_String())>0; -} -bool RakWString::operator >= ( const RakWString& right ) const -{ - return wcscmp(C_String(),right.C_String())>=0; -} -bool RakWString::operator!=(const RakWString &right) const -{ - if (GetLength()!=right.GetLength()) - return true; - return wcscmp(C_String(),right.C_String())!=0; -} -void RakWString::Set( wchar_t *str ) -{ - *this = str; -} -bool RakWString::IsEmpty(void) const -{ - return GetLength()==0; -} -size_t RakWString::GetLength(void) const -{ - return c_strCharLength; -} -unsigned long RakWString::ToInteger(const RakWString &rs) -{ - unsigned long hash = 0; - int c; - - const char *str = (const char *)rs.C_String(); - size_t i; - for (i=0; i < rs.GetLength()*MAX_BYTES_PER_UNICODE_CHAR*sizeof(wchar_t); i++) - { - c = *str++; - hash = c + (hash << 6) + (hash << 16) - hash; - } - - return hash; -} -int RakWString::StrCmp(const RakWString &right) const -{ - return wcscmp(C_String(), right.C_String()); -} -int RakWString::StrICmp(const RakWString &right) const -{ -#ifdef _WIN32 - return _wcsicmp(C_String(), right.C_String()); -#else - // Not supported - return wcscmp(C_String(), right.C_String()); -#endif -} -void RakWString::Clear(void) -{ - rakFree_Ex(c_str,_FILE_AND_LINE_); - c_str=0; - c_strCharLength=0; -} -void RakWString::Printf(void) -{ - printf("%ls", C_String()); -} -void RakWString::FPrintf(FILE *fp) -{ - fprintf(fp,"%ls", C_String()); -} -void RakWString::Serialize(BitStream *bs) const -{ - Serialize(C_String(), bs); -} -void RakWString::Serialize(const wchar_t * const str, BitStream *bs) -{ -#if 0 - char *multiByteBuffer; - size_t allocated = wcslen(str)*MAX_BYTES_PER_UNICODE_CHAR; - multiByteBuffer = (char*) rakMalloc_Ex(allocated, _FILE_AND_LINE_); - size_t used = wcstombs(multiByteBuffer, str, allocated); - bs->WriteCasted(used); - bs->WriteAlignedBytes((const unsigned char*) multiByteBuffer,(const unsigned int) used); - rakFree_Ex(multiByteBuffer, _FILE_AND_LINE_); -#else - size_t mbByteLength = wcslen(str); - bs->WriteCasted(mbByteLength); - for (unsigned int i=0; i < mbByteLength; i++) - { - uint16_t t; - t = (uint16_t) str[i]; - // Force endian swapping, and write to 16 bits - bs->Write(t); - } -#endif -} -bool RakWString::Deserialize(BitStream *bs) -{ - Clear(); - - size_t mbByteLength; - bs->ReadCasted(mbByteLength); - if (mbByteLength>0) - { -#if 0 - char *multiByteBuffer; - multiByteBuffer = (char*) rakMalloc_Ex(mbByteLength+1, _FILE_AND_LINE_); - bool result = bs->ReadAlignedBytes((unsigned char*) multiByteBuffer,(const unsigned int) mbByteLength); - if (result==false) - { - rakFree_Ex(multiByteBuffer, _FILE_AND_LINE_); - return false; - } - multiByteBuffer[mbByteLength]=0; - c_str = (wchar_t *) rakMalloc_Ex( (mbByteLength + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); - c_strCharLength = mbstowcs(c_str, multiByteBuffer, mbByteLength); - rakFree_Ex(multiByteBuffer, _FILE_AND_LINE_); - c_str[c_strCharLength]=0; -#else - c_str = (wchar_t*) rakMalloc_Ex((mbByteLength+1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); - c_strCharLength = mbByteLength; - for (unsigned int i=0; i < mbByteLength; i++) - { - uint16_t t; - // Force endian swapping, and read 16 bits - bs->Read(t); - c_str[i]=t; - } - c_str[mbByteLength]=0; -#endif - return true; - } - else - { - return true; - } -} -bool RakWString::Deserialize(wchar_t *str, BitStream *bs) -{ - size_t mbByteLength; - bs->ReadCasted(mbByteLength); - if (mbByteLength>0) - { -#if 0 - char *multiByteBuffer; - multiByteBuffer = (char*) rakMalloc_Ex(mbByteLength+1, _FILE_AND_LINE_); - bool result = bs->ReadAlignedBytes((unsigned char*) multiByteBuffer,(const unsigned int) mbByteLength); - if (result==false) - { - rakFree_Ex(multiByteBuffer, _FILE_AND_LINE_); - return false; - } - multiByteBuffer[mbByteLength]=0; - size_t c_strCharLength = mbstowcs(str, multiByteBuffer, mbByteLength); - rakFree_Ex(multiByteBuffer, _FILE_AND_LINE_); - str[c_strCharLength]=0; -#else - for (unsigned int i=0; i < mbByteLength; i++) - { - uint16_t t; - // Force endian swapping, and read 16 bits - bs->Read(t); - str[i]=t; - } - str[mbByteLength]=0; -#endif - return true; - } - else - { - wcscpy(str,L""); - } - return true; -} - -/* -RakNet::BitStream bsTest; -RakNet::RakWString testString("cat"), testString2; -testString = "Hllo"; -testString = L"Hello"; -testString += L" world"; -testString2 += testString2; -RakNet::RakWString ts3(L" from here"); -testString2+=ts3; -RakNet::RakWString ts4(L" 222"); -testString2=ts4; -RakNet::RakString rs("rakstring"); -testString2+=rs; -testString2=rs; -bsTest.Write(L"one"); -bsTest.Write(testString2); -bsTest.SetReadOffset(0); -RakNet::RakWString ts5, ts6; -wchar_t buff[99]; -wchar_t *wptr = (wchar_t*)buff; -bsTest.Read(wptr); -bsTest.Read(ts5); -*/ +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "RakWString.h" +#include "BitStream.h" +#include +#include +#include + +using namespace RakNet; + +// From http://www.joelonsoftware.com/articles/Unicode.html +// Only code points 128 and above are stored using 2, 3, in fact, up to 6 bytes. +#define MAX_BYTES_PER_UNICODE_CHAR sizeof(wchar_t) + +RakWString::RakWString() +{ + c_str=0; + c_strCharLength=0; +} +RakWString::RakWString( const RakString &right ) +{ + c_str=0; + c_strCharLength=0; + *this=right; +} +RakWString::RakWString( const char *input ) +{ + c_str=0; + c_strCharLength=0; + *this = input; +} +RakWString::RakWString( const wchar_t *input ) +{ + c_str=0; + c_strCharLength=0; + *this = input; +} +RakWString::RakWString( const RakWString & right) +{ + c_str=0; + c_strCharLength=0; + *this = right; +} +RakWString::~RakWString() +{ + rakFree_Ex(c_str,_FILE_AND_LINE_); +} +RakWString& RakWString::operator = ( const RakWString& right ) +{ + Clear(); + if (right.IsEmpty()) + return *this; + c_str = (wchar_t *) rakMalloc_Ex( (right.GetLength() + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); + if (!c_str) + { + c_strCharLength=0; + notifyOutOfMemory(_FILE_AND_LINE_); + return *this; + } + c_strCharLength = right.GetLength(); + memcpy(c_str,right.C_String(),(right.GetLength() + 1) * MAX_BYTES_PER_UNICODE_CHAR); + + return *this; +} +RakWString& RakWString::operator = ( const RakString& right ) +{ + return *this = right.C_String(); +} +RakWString& RakWString::operator = ( const wchar_t * const str ) +{ + Clear(); + if (str==0) + return *this; + c_strCharLength = wcslen(str); + if (c_strCharLength==0) + return *this; + c_str = (wchar_t *) rakMalloc_Ex( (c_strCharLength + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); + if (!c_str) + { + c_strCharLength=0; + notifyOutOfMemory(_FILE_AND_LINE_); + return *this; + } + wcscpy(c_str,str); + + return *this; +} +RakWString& RakWString::operator = ( wchar_t *str ) +{ + *this = ( const wchar_t * const) str; + return *this; +} +RakWString& RakWString::operator = ( const char * const str ) +{ + Clear(); + +// Not supported on android +#if !defined(ANDROID) + if (str==0) + return *this; + if (str[0]==0) + return *this; + + c_strCharLength = mbstowcs(nullptr, str, 0); + c_str = (wchar_t *) rakMalloc_Ex( (c_strCharLength + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); + if (!c_str) + { + c_strCharLength=0; + notifyOutOfMemory(_FILE_AND_LINE_); + return *this; + } + + c_strCharLength = mbstowcs(c_str, str, c_strCharLength+1); + if (c_strCharLength == (size_t) (-1)) + { + RAKNET_DEBUG_PRINTF("Couldn't convert string--invalid multibyte character.\n"); + Clear(); + return *this; + } +#else + // mbstowcs not supported on android + RakAssert("mbstowcs not supported on Android" && 0); +#endif // defined(ANDROID) + + return *this; +} +RakWString& RakWString::operator = ( char *str ) +{ + *this = ( const char * const) str; + return *this; +} +RakWString& RakWString::operator +=( const RakWString& right) +{ + if (right.IsEmpty()) + return *this; + size_t newCharLength = c_strCharLength + right.GetLength(); + wchar_t *newCStr; + bool isEmpty = IsEmpty(); + if (isEmpty) + newCStr = (wchar_t *) rakMalloc_Ex( (newCharLength + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); + else + newCStr = (wchar_t *) rakRealloc_Ex( c_str, (newCharLength + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); + if (!newCStr) + { + notifyOutOfMemory(_FILE_AND_LINE_); + return *this; + } + c_str = newCStr; + c_strCharLength = newCharLength; + if (isEmpty) + { + memcpy(newCStr,right.C_String(),(right.GetLength() + 1) * MAX_BYTES_PER_UNICODE_CHAR); + } + else + { + wcscat(c_str, right.C_String()); + } + + return *this; +} +RakWString& RakWString::operator += ( const wchar_t * const right ) +{ + if (right==0) + return *this; + size_t rightLength = wcslen(right); + size_t newCharLength = c_strCharLength + rightLength; + wchar_t *newCStr; + bool isEmpty = IsEmpty(); + if (isEmpty) + newCStr = (wchar_t *) rakMalloc_Ex( (newCharLength + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); + else + newCStr = (wchar_t *) rakRealloc_Ex( c_str, (newCharLength + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); + if (!newCStr) + { + notifyOutOfMemory(_FILE_AND_LINE_); + return *this; + } + c_str = newCStr; + c_strCharLength = newCharLength; + if (isEmpty) + { + memcpy(newCStr,right,(rightLength + 1) * MAX_BYTES_PER_UNICODE_CHAR); + } + else + { + wcscat(c_str, right); + } + + return *this; +} +RakWString& RakWString::operator += ( wchar_t *right ) +{ + return *this += (const wchar_t * const) right; +} +bool RakWString::operator==(const RakWString &right) const +{ + if (GetLength()!=right.GetLength()) + return false; + return wcscmp(C_String(),right.C_String())==0; +} +bool RakWString::operator < ( const RakWString& right ) const +{ + return wcscmp(C_String(),right.C_String())<0; +} +bool RakWString::operator <= ( const RakWString& right ) const +{ + return wcscmp(C_String(),right.C_String())<=0; +} +bool RakWString::operator > ( const RakWString& right ) const +{ + return wcscmp(C_String(),right.C_String())>0; +} +bool RakWString::operator >= ( const RakWString& right ) const +{ + return wcscmp(C_String(),right.C_String())>=0; +} +bool RakWString::operator!=(const RakWString &right) const +{ + if (GetLength()!=right.GetLength()) + return true; + return wcscmp(C_String(),right.C_String())!=0; +} +void RakWString::Set( wchar_t *str ) +{ + *this = str; +} +bool RakWString::IsEmpty(void) const +{ + return GetLength()==0; +} +size_t RakWString::GetLength(void) const +{ + return c_strCharLength; +} +unsigned long RakWString::ToInteger(const RakWString &rs) +{ + unsigned long hash = 0; + int c; + + const char *str = (const char *)rs.C_String(); + size_t i; + for (i=0; i < rs.GetLength()*MAX_BYTES_PER_UNICODE_CHAR*sizeof(wchar_t); i++) + { + c = *str++; + hash = c + (hash << 6) + (hash << 16) - hash; + } + + return hash; +} +int RakWString::StrCmp(const RakWString &right) const +{ + return wcscmp(C_String(), right.C_String()); +} +int RakWString::StrICmp(const RakWString &right) const +{ +#ifdef _WIN32 + return _wcsicmp(C_String(), right.C_String()); +#else + // Not supported + return wcscmp(C_String(), right.C_String()); +#endif +} +void RakWString::Clear(void) +{ + rakFree_Ex(c_str,_FILE_AND_LINE_); + c_str=0; + c_strCharLength=0; +} +void RakWString::Printf(void) +{ + printf("%ls", C_String()); +} +void RakWString::FPrintf(FILE *fp) +{ + fprintf(fp,"%ls", C_String()); +} +void RakWString::Serialize(BitStream *bs) const +{ + Serialize(C_String(), bs); +} +void RakWString::Serialize(const wchar_t * const str, BitStream *bs) +{ +#if 0 + char *multiByteBuffer; + size_t allocated = wcslen(str)*MAX_BYTES_PER_UNICODE_CHAR; + multiByteBuffer = (char*) rakMalloc_Ex(allocated, _FILE_AND_LINE_); + size_t used = wcstombs(multiByteBuffer, str, allocated); + bs->WriteCasted(used); + bs->WriteAlignedBytes((const unsigned char*) multiByteBuffer,(const unsigned int) used); + rakFree_Ex(multiByteBuffer, _FILE_AND_LINE_); +#else + size_t mbByteLength = wcslen(str); + bs->WriteCasted(mbByteLength); + for (unsigned int i=0; i < mbByteLength; i++) + { + uint16_t t; + t = (uint16_t) str[i]; + // Force endian swapping, and write to 16 bits + bs->Write(t); + } +#endif +} +bool RakWString::Deserialize(BitStream *bs) +{ + Clear(); + + size_t mbByteLength; + bs->ReadCasted(mbByteLength); + if (mbByteLength>0) + { +#if 0 + char *multiByteBuffer; + multiByteBuffer = (char*) rakMalloc_Ex(mbByteLength+1, _FILE_AND_LINE_); + bool result = bs->ReadAlignedBytes((unsigned char*) multiByteBuffer,(const unsigned int) mbByteLength); + if (result==false) + { + rakFree_Ex(multiByteBuffer, _FILE_AND_LINE_); + return false; + } + multiByteBuffer[mbByteLength]=0; + c_str = (wchar_t *) rakMalloc_Ex( (mbByteLength + 1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); + c_strCharLength = mbstowcs(c_str, multiByteBuffer, mbByteLength); + rakFree_Ex(multiByteBuffer, _FILE_AND_LINE_); + c_str[c_strCharLength]=0; +#else + c_str = (wchar_t*) rakMalloc_Ex((mbByteLength+1) * MAX_BYTES_PER_UNICODE_CHAR, _FILE_AND_LINE_); + c_strCharLength = mbByteLength; + for (unsigned int i=0; i < mbByteLength; i++) + { + uint16_t t; + // Force endian swapping, and read 16 bits + bs->Read(t); + c_str[i]=t; + } + c_str[mbByteLength]=0; +#endif + return true; + } + else + { + return true; + } +} +bool RakWString::Deserialize(wchar_t *str, BitStream *bs) +{ + size_t mbByteLength; + bs->ReadCasted(mbByteLength); + if (mbByteLength>0) + { +#if 0 + char *multiByteBuffer; + multiByteBuffer = (char*) rakMalloc_Ex(mbByteLength+1, _FILE_AND_LINE_); + bool result = bs->ReadAlignedBytes((unsigned char*) multiByteBuffer,(const unsigned int) mbByteLength); + if (result==false) + { + rakFree_Ex(multiByteBuffer, _FILE_AND_LINE_); + return false; + } + multiByteBuffer[mbByteLength]=0; + size_t c_strCharLength = mbstowcs(str, multiByteBuffer, mbByteLength); + rakFree_Ex(multiByteBuffer, _FILE_AND_LINE_); + str[c_strCharLength]=0; +#else + for (unsigned int i=0; i < mbByteLength; i++) + { + uint16_t t; + // Force endian swapping, and read 16 bits + bs->Read(t); + str[i]=t; + } + str[mbByteLength]=0; +#endif + return true; + } + else + { + wcscpy(str,L""); + } + return true; +} + +/* +RakNet::BitStream bsTest; +RakNet::RakWString testString("cat"), testString2; +testString = "Hllo"; +testString = L"Hello"; +testString += L" world"; +testString2 += testString2; +RakNet::RakWString ts3(L" from here"); +testString2+=ts3; +RakNet::RakWString ts4(L" 222"); +testString2=ts4; +RakNet::RakString rs("rakstring"); +testString2+=rs; +testString2=rs; +bsTest.Write(L"one"); +bsTest.Write(testString2); +bsTest.SetReadOffset(0); +RakNet::RakWString ts5, ts6; +wchar_t buff[99]; +wchar_t *wptr = (wchar_t*)buff; +bsTest.Read(wptr); +bsTest.Read(ts5); +*/ diff --git a/Source/RakWString.h b/Source/RakWString.h index d4c374fac..ee63627d2 100644 --- a/Source/RakWString.h +++ b/Source/RakWString.h @@ -1,123 +1,121 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __RAK_W_STRING_H -#define __RAK_W_STRING_H - -#include "Export.h" -#include "RakNetTypes.h" // int64_t -#include "RakString.h" - -#ifdef _WIN32 - - - -#include "WindowsIncludes.h" -#endif - -namespace RakNet -{ - /// \brief String class for Unicode - class RAK_DLL_EXPORT RakWString - { - public: - // Constructors - RakWString(); - RakWString( const RakString &right ); - RakWString( const wchar_t *input ); - RakWString( const RakWString & right); - RakWString( const char *input ); - ~RakWString(); - - /// Implicit return of wchar_t* - operator wchar_t* () const {if (c_str) return c_str; return (wchar_t*) L"";} - - /// Same as std::string::c_str - const wchar_t* C_String(void) const {if (c_str) return c_str; return (const wchar_t*) L"";} - - /// Assignment operators - RakWString& operator = ( const RakWString& right ); - RakWString& operator = ( const RakString& right ); - RakWString& operator = ( const wchar_t * const str ); - RakWString& operator = ( wchar_t *str ); - RakWString& operator = ( const char * const str ); - RakWString& operator = ( char *str ); - - /// Concatenation - RakWString& operator +=( const RakWString& right); - RakWString& operator += ( const wchar_t * const right ); - RakWString& operator += ( wchar_t *right ); - - /// Equality - bool operator==(const RakWString &right) const; - - // Comparison - bool operator < ( const RakWString& right ) const; - bool operator <= ( const RakWString& right ) const; - bool operator > ( const RakWString& right ) const; - bool operator >= ( const RakWString& right ) const; - - /// Inequality - bool operator!=(const RakWString &right) const; - - /// Set the value of the string - void Set( wchar_t *str ); - - /// Returns if the string is empty. Also, C_String() would return "" - bool IsEmpty(void) const; - - /// Returns the length of the string - size_t GetLength(void) const; - - /// Has the string into an unsigned int - static unsigned long ToInteger(const RakWString &rs); - - /// Compare strings (case sensitive) - int StrCmp(const RakWString &right) const; - - /// Compare strings (not case sensitive) - int StrICmp(const RakWString &right) const; - - /// Clear the string - void Clear(void); - - /// Print the string to the screen - void Printf(void); - - /// Print the string to a file - void FPrintf(FILE *fp); - - /// Serialize to a bitstream, uncompressed (slightly faster) - /// \param[out] bs Bitstream to serialize to - void Serialize(BitStream *bs) const; - - /// Static version of the Serialize function - static void Serialize(const wchar_t * const str, BitStream *bs); - - /// Deserialize what was written by Serialize - /// \param[in] bs Bitstream to serialize from - /// \return true if the deserialization was successful - bool Deserialize(BitStream *bs); - - /// Static version of the Deserialize() function - static bool Deserialize(wchar_t *str, BitStream *bs); - - - protected: - wchar_t* c_str; - size_t c_strCharLength; - }; - -} - -const RakNet::RakWString RAK_DLL_EXPORT operator+(const RakNet::RakWString &lhs, const RakNet::RakWString &rhs); - - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include "Export.h" +#include "RakNetTypes.h" // int64_t +#include "RakString.h" + +#ifdef _WIN32 + + + +#include "WindowsIncludes.h" +#endif + +namespace RakNet +{ + /// \brief String class for Unicode + class RAK_DLL_EXPORT RakWString + { + public: + // Constructors + RakWString(); + RakWString( const RakString &right ); + RakWString( const wchar_t *input ); + RakWString( const RakWString & right); + RakWString( const char *input ); + ~RakWString(); + + /// Implicit return of wchar_t* + operator wchar_t* () const {if (c_str) return c_str; return (wchar_t*) L"";} + + /// Same as std::string::c_str + const wchar_t* C_String(void) const {if (c_str) return c_str; return (const wchar_t*) L"";} + + /// Assignment operators + RakWString& operator = ( const RakWString& right ); + RakWString& operator = ( const RakString& right ); + RakWString& operator = ( const wchar_t * const str ); + RakWString& operator = ( wchar_t *str ); + RakWString& operator = ( const char * const str ); + RakWString& operator = ( char *str ); + + /// Concatenation + RakWString& operator +=( const RakWString& right); + RakWString& operator += ( const wchar_t * const right ); + RakWString& operator += ( wchar_t *right ); + + /// Equality + bool operator==(const RakWString &right) const; + + // Comparison + bool operator < ( const RakWString& right ) const; + bool operator <= ( const RakWString& right ) const; + bool operator > ( const RakWString& right ) const; + bool operator >= ( const RakWString& right ) const; + + /// Inequality + bool operator!=(const RakWString &right) const; + + /// Set the value of the string + void Set( wchar_t *str ); + + /// Returns if the string is empty. Also, C_String() would return "" + bool IsEmpty(void) const; + + /// Returns the length of the string + size_t GetLength(void) const; + + /// Has the string into an unsigned int + static unsigned long ToInteger(const RakWString &rs); + + /// Compare strings (case sensitive) + int StrCmp(const RakWString &right) const; + + /// Compare strings (not case sensitive) + int StrICmp(const RakWString &right) const; + + /// Clear the string + void Clear(void); + + /// Print the string to the screen + void Printf(void); + + /// Print the string to a file + void FPrintf(FILE *fp); + + /// Serialize to a bitstream, uncompressed (slightly faster) + /// \param[out] bs Bitstream to serialize to + void Serialize(BitStream *bs) const; + + /// Static version of the Serialize function + static void Serialize(const wchar_t * const str, BitStream *bs); + + /// Deserialize what was written by Serialize + /// \param[in] bs Bitstream to serialize from + /// \return true if the deserialization was successful + bool Deserialize(BitStream *bs); + + /// Static version of the Deserialize() function + static bool Deserialize(wchar_t *str, BitStream *bs); + + + protected: + wchar_t* c_str; + size_t c_strCharLength; + }; + +} + +const RakNet::RakWString RAK_DLL_EXPORT operator+(const RakNet::RakWString &lhs, const RakNet::RakWString &rhs); + + diff --git a/Source/Rand.cpp b/Source/Rand.cpp index c3d84138c..b01d1c65b 100644 --- a/Source/Rand.cpp +++ b/Source/Rand.cpp @@ -142,8 +142,8 @@ void seedMT( unsigned int seed, unsigned int *state, unsigned int *&next, int &l // so-- that's why the only change I made is to restrict to odd seeds. // - register unsigned int x = ( seed | 1U ) & 0xFFFFFFFFU, *s = state; - register int j; + unsigned int x = ( seed | 1U ) & 0xFFFFFFFFU, *s = state; + int j; for ( left = 0, *s++ = x, j = N; --j; *s++ = ( x *= 69069U ) & 0xFFFFFFFFU ) @@ -154,8 +154,8 @@ void seedMT( unsigned int seed, unsigned int *state, unsigned int *&next, int &l unsigned int reloadMT( unsigned int *state, unsigned int *&next, int &left ) { - register unsigned int * p0 = state, *p2 = state + 2, *pM = state + M, s0, s1; - register int j; + unsigned int * p0 = state, *p2 = state + 2, *pM = state + M, s0, s1; + int j; if ( left < -1 ) seedMT( 4357U ); diff --git a/Source/Rand.h b/Source/Rand.h index 145c253d1..0ef0c4ee9 100644 --- a/Source/Rand.h +++ b/Source/Rand.h @@ -1,67 +1,65 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief \b [Internal] Random number generator -/// - - - -#ifndef __RAND_H -#define __RAND_H - -#include "Export.h" - -/// Initialise seed for Random Generator -/// \note not threadSafe, use an instance of RakNetRandom if necessary per thread -/// \param[in] seed The seed value for the random number generator. -extern void RAK_DLL_EXPORT seedMT( unsigned int seed ); - -/// \internal -/// \note not threadSafe, use an instance of RakNetRandom if necessary per thread -extern unsigned int RAK_DLL_EXPORT reloadMT( void ); - -/// Gets a random unsigned int -/// \note not threadSafe, use an instance of RakNetRandom if necessary per thread -/// \return an integer random value. -extern unsigned int RAK_DLL_EXPORT randomMT( void ); - -/// Gets a random float -/// \note not threadSafe, use an instance of RakNetRandom if necessary per thread -/// \return 0 to 1.0f, inclusive -extern float RAK_DLL_EXPORT frandomMT( void ); - -/// Randomizes a buffer -/// \note not threadSafe, use an instance of RakNetRandom if necessary per thread -extern void RAK_DLL_EXPORT fillBufferMT( void *buffer, unsigned int bytes ); - -namespace RakNet { - -// Same thing as above functions, but not global -class RAK_DLL_EXPORT RakNetRandom -{ -public: - RakNetRandom(); - ~RakNetRandom(); - void SeedMT( unsigned int seed ); - unsigned int ReloadMT( void ); - unsigned int RandomMT( void ); - float FrandomMT( void ); - void FillBufferMT( void *buffer, unsigned int bytes ); - -protected: - unsigned int state[ 624 + 1 ]; - unsigned int *next; - int left; -}; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief \b [Internal] Random number generator +/// + + + +#pragma once + +#include "Export.h" + +/// Initialise seed for Random Generator +/// \note not threadSafe, use an instance of RakNetRandom if necessary per thread +/// \param[in] seed The seed value for the random number generator. +extern void RAK_DLL_EXPORT seedMT( unsigned int seed ); + +/// \internal +/// \note not threadSafe, use an instance of RakNetRandom if necessary per thread +extern unsigned int RAK_DLL_EXPORT reloadMT( void ); + +/// Gets a random unsigned int +/// \note not threadSafe, use an instance of RakNetRandom if necessary per thread +/// \return an integer random value. +extern unsigned int RAK_DLL_EXPORT randomMT( void ); + +/// Gets a random float +/// \note not threadSafe, use an instance of RakNetRandom if necessary per thread +/// \return 0 to 1.0f, inclusive +extern float RAK_DLL_EXPORT frandomMT( void ); + +/// Randomizes a buffer +/// \note not threadSafe, use an instance of RakNetRandom if necessary per thread +extern void RAK_DLL_EXPORT fillBufferMT( void *buffer, unsigned int bytes ); + +namespace RakNet { + +// Same thing as above functions, but not global +class RAK_DLL_EXPORT RakNetRandom +{ +public: + RakNetRandom(); + ~RakNetRandom(); + void SeedMT( unsigned int seed ); + unsigned int ReloadMT( void ); + unsigned int RandomMT( void ); + float FrandomMT( void ); + void FillBufferMT( void *buffer, unsigned int bytes ); + +protected: + unsigned int state[ 624 + 1 ]; + unsigned int *next; + int left; +}; + +} // namespace RakNet + diff --git a/Source/RandSync.h b/Source/RandSync.h index 647c384e4..2b1da3118 100644 --- a/Source/RandSync.h +++ b/Source/RandSync.h @@ -1,58 +1,56 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief \b [Internal] Random number generator -/// - - - -#ifndef __RAND_SYNC_H -#define __RAND_SYNC_H - -#include "Export.h" -#include "Rand.h" -#include "DS_Queue.h" -#include "NativeTypes.h" - -namespace RakNet { - -class BitStream; - -class RAK_DLL_EXPORT RakNetRandomSync -{ -public: - RakNetRandomSync(); - virtual ~RakNetRandomSync(); - void SeedMT( uint32_t _seed ); - void SeedMT( uint32_t _seed, uint32_t skipValues ); - float FrandomMT( void ); - unsigned int RandomMT( void ); - uint32_t GetSeed( void ) const; - uint32_t GetCallCount( void ) const; - void SetCallCount( uint32_t i ); - - virtual void SerializeConstruction(RakNet::BitStream *constructionBitstream); - virtual bool DeserializeConstruction(RakNet::BitStream *constructionBitstream); - virtual void Serialize(RakNet::BitStream *outputBitstream); - virtual void Deserialize(RakNet::BitStream *outputBitstream); - -protected: - void Skip( uint32_t count ); - DataStructures::Queue usedValues; - uint32_t seed; - uint32_t callCount; - uint32_t usedValueBufferCount; - RakNetRandom rnr; -}; -} // namespace RakNet - - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief \b [Internal] Random number generator +/// + + + +#pragma once + +#include "Export.h" +#include "Rand.h" +#include "DS_Queue.h" +#include "NativeTypes.h" + +namespace RakNet { + +class BitStream; + +class RAK_DLL_EXPORT RakNetRandomSync +{ +public: + RakNetRandomSync(); + virtual ~RakNetRandomSync(); + void SeedMT( uint32_t _seed ); + void SeedMT( uint32_t _seed, uint32_t skipValues ); + float FrandomMT( void ); + unsigned int RandomMT( void ); + uint32_t GetSeed( void ) const; + uint32_t GetCallCount( void ) const; + void SetCallCount( uint32_t i ); + + virtual void SerializeConstruction(RakNet::BitStream *constructionBitstream); + virtual bool DeserializeConstruction(RakNet::BitStream *constructionBitstream); + virtual void Serialize(RakNet::BitStream *outputBitstream); + virtual void Deserialize(RakNet::BitStream *outputBitstream); + +protected: + void Skip( uint32_t count ); + DataStructures::Queue usedValues; + uint32_t seed; + uint32_t callCount; + uint32_t usedValueBufferCount; + RakNetRandom rnr; +}; +} // namespace RakNet + + diff --git a/Source/ReadyEvent.h b/Source/ReadyEvent.h index eaa2f7c7e..add987716 100644 --- a/Source/ReadyEvent.h +++ b/Source/ReadyEvent.h @@ -1,242 +1,240 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Ready event plugin. This enables a set of systems to create a signal event, set this signal as ready or unready, and to trigger the event when all systems are ready -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_ReadyEvent==1 - -#ifndef __READY_EVENT_H -#define __READY_EVENT_H - -#include "PluginInterface2.h" -#include "DS_OrderedList.h" - -namespace RakNet { - -class RakPeerInterface; - -/// \defgroup READY_EVENT_GROUP ReadyEvent -/// \brief Peer to peer synchronized ready and unready events -/// \details -/// \ingroup PLUGINS_GROUP - -/// \ingroup READY_EVENT_GROUP -/// Returns the status of a remote system when querying with ReadyEvent::GetReadyStatus -enum ReadyEventSystemStatus -{ - /// ----------- Normal states --------------- - /// The remote system is not in the wait list, and we have never gotten a ready or complete message from it. - /// This is the default state for valid events - RES_NOT_WAITING, - /// We are waiting for this remote system to call SetEvent(thisEvent,true). - RES_WAITING, - /// The remote system called SetEvent(thisEvent,true), but it still waiting for other systems before completing the ReadyEvent. - RES_READY, - /// The remote system called SetEvent(thisEvent,true), and is no longer waiting for any other systems. - /// This remote system has completed the ReadyEvent - RES_ALL_READY, - - /// Error code, we couldn't look up the system because the event was unknown - RES_UNKNOWN_EVENT, -}; - -/// \brief Peer to peer synchronized ready and unready events -/// \details For peer to peer networks in a fully connected mesh.
-/// Solves the problem of how to tell if all peers, relative to all other peers, are in a certain ready state.
-/// For example, if A is connected to B and C, A may see that B and C are ready, but does not know if B is ready to C, or vice-versa.
-/// This plugin uses two stages to solve that problem, first, everyone I know about is ready. Second, everyone I know about is ready to everyone they know about.
-/// The user will get ID_READY_EVENT_SET and ID_READY_EVENT_UNSET as the signal flag is set or unset
-/// The user will get ID_READY_EVENT_ALL_SET when all systems are done waiting for all other systems, in which case the event is considered complete, and no longer tracked.
-/// \sa FullyConnectedMesh2 -/// \ingroup READY_EVENT_GROUP -class ReadyEvent : public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(ReadyEvent) - - // Constructor - ReadyEvent(); - - // Destructor - virtual ~ReadyEvent(); - - // -------------------------------------------------------------------------------------------- - // User functions - // -------------------------------------------------------------------------------------------- - /// Sets or updates the initial ready state for our local system. - /// If eventId is an unknown event the event is created. - /// If eventId was previously used and you want to reuse it, call DeleteEvent first, or else you will keep the same event signals from before - /// Systems previously or later added through AddToWaitList() with the same \a eventId when isReady=true will get ID_READY_EVENT_SET - /// Systems previously added through AddToWaitList with the same \a eventId will get ID_READY_EVENT_UNSET - /// For both ID_READY_EVENT_SET and ID_READY_EVENT_UNSET, eventId is encoded in bytes 1 through 1+sizeof(int) - /// \param[in] eventId A user-defined identifier to wait on. This can be a sequence counter, an event identifier, or anything else you want. - /// \param[in] isReady True to signal we are ready to proceed with this event, false to unsignal - /// \return False if event status is ID_READY_EVENT_FORCE_ALL_SET, or if we are setting to a status we are already in (no change). Otherwise true - bool SetEvent(int eventId, bool isReady); - - /// When systems can call SetEvent() with isReady==false, it is possible for one system to return true from IsEventCompleted() while the other systems return false - /// This can occur if a system SetEvent() with isReady==false while the completion message is still being transmitted. - /// If your game has the situation where some action should be taken on all systems when IsEventCompleted() is true for any system, then call ForceCompletion() when the action begins. - /// This will force all systems to return true from IsEventCompleted(). - /// \param[in] eventId A user-defined identifier to immediately set as completed - void ForceCompletion(int eventId); - - /// Deletes an event. We will no longer wait for this event, and any systems that we know have set the event will be forgotten. - /// Call this to clear memory when events are completed and you know you will never need them again. - /// \param[in] eventId A user-defined identifier - /// \return True on success. False (failure) on unknown eventId - bool DeleteEvent(int eventId); - - /// Returns what was passed to SetEvent() - /// \return The value of isReady passed to SetEvent(). Also returns false on unknown event. - bool IsEventSet(int eventId); - - /// Returns if the event is about to be ready and we are negotiating the final packets. - /// This will usually only be true for a very short time, after which IsEventCompleted should return true. - /// While this is true you cannot add to the wait list, or SetEvent() isReady to false anymore. - /// \param[in] eventId A user-defined identifier - /// \return True if any other system has completed processing. Will always be true if IsEventCompleted() is true - bool IsEventCompletionProcessing(int eventId) const; - - /// Returns if the wait list is a subset of the completion list. - /// Call this after all systems you want to wait for have been added with AddToWaitList - /// If you are waiting for a specific number of systems (such as players later connecting), also check GetRemoteWaitListSize(eventId) to be equal to 1 less than the total number of participants. - /// \param[in] eventId A user-defined identifier - /// \return True on completion. False (failure) on unknown eventId, or the set is not completed. - bool IsEventCompleted(int eventId) const; - - /// Returns if this is a known event. - /// Events may be known even if we never ourselves referenced them with SetEvent, because other systems created them via ID_READY_EVENT_SET. - /// \param[in] eventId A user-defined identifier - /// \return true if we have this event, false otherwise - bool HasEvent(int eventId); - - /// Returns the total number of events stored in the system. - /// \return The total number of events stored in the system. - unsigned GetEventListSize(void) const; - - /// Returns the event ID stored at a particular index. EventIDs are stored sorted from least to greatest. - /// \param[in] index Index into the array, from 0 to GetEventListSize() - /// \return The event ID stored at a particular index - int GetEventAtIndex(unsigned index) const; - - /// Adds a system to wait for to signal an event before considering the event complete and returning ID_READY_EVENT_ALL_SET. - /// As we add systems, if this event was previously set to true with SetEvent, these systems will get ID_READY_EVENT_SET. - /// As these systems disconnect (directly or indirectly through the router) they are removed. - /// \note If the event completion process has already started, you cannot add more systems, as this would cause the completion process to fail - /// \param[in] eventId A user-defined number previously passed to SetEvent that has not yet completed - /// \param[in] guid An address to wait for event replies from. Pass UNASSIGNED_SYSTEM_ADDRESS for all currently connected systems. Until all systems in this list have called SetEvent with this ID and true, and have this system in the list, we won't get ID_READY_EVENT_COMPLETE - /// \return True on success, false on unknown eventId (this should be considered an error) - bool AddToWaitList(int eventId, RakNetGUID guid); - - /// Removes systems from the wait list, which should have been previously added with AddToWaitList - /// \note Systems that directly or indirectly disconnect from us are automatically removed from the wait list - /// \param[in] guid The system to remove from the wait list. Pass UNASSIGNED_RAKNET_GUID for all currently connected systems. - /// \return True on success, false on unknown eventId (this should be considered an error) - bool RemoveFromWaitList(int eventId, RakNetGUID guid); - - /// Returns if a particular system is waiting on a particular event. - /// \param[in] eventId A user-defined identifier - /// \param[in] guid The system we are checking up on - /// \return True if this system is waiting on this event, false otherwise. - bool IsInWaitList(int eventId, RakNetGUID guid); - - /// Returns the total number of systems we are waiting on for this event. - /// Does not include yourself - /// \param[in] eventId A user-defined identifier - /// \return The total number of systems we are waiting on for this event. - unsigned GetRemoteWaitListSize(int eventId) const; - - /// Returns the system address of a system at a particular index, for this event. - /// \param[in] eventId A user-defined identifier - /// \param[in] index Index into the array, from 0 to GetWaitListSize() - /// \return The system address of a system at a particular index, for this event. - RakNetGUID GetFromWaitListAtIndex(int eventId, unsigned index) const; - - /// For a remote system, find out what their ready status is (waiting, signaled, complete). - /// \param[in] eventId A user-defined identifier - /// \param[in] guid Which system we are checking up on - /// \return The status of this system, for this particular event. \sa ReadyEventSystemStatus - ReadyEventSystemStatus GetReadyStatus(int eventId, RakNetGUID guid); - - /// This channel will be used for all RakPeer::Send calls - /// \param[in] newChannel The channel to use for internal RakPeer::Send calls from this system. Defaults to 0. - void SetSendChannel(unsigned char newChannel); - - // ---------------------------- ALL INTERNAL AFTER HERE ---------------------------- - /// \internal - /// Status of a remote system - struct RemoteSystem - { - MessageID lastSentStatus, lastReceivedStatus; - RakNetGUID rakNetGuid; - }; - static int RemoteSystemCompByGuid( const RakNetGUID &key, const RemoteSystem &data ); - /// \internal - /// An event, with a set of systems we are waiting for, a set of systems that are signaled, and a set of systems with completed events - struct ReadyEventNode - { - int eventId; // Sorted on this - MessageID eventStatus; - DataStructures::OrderedList systemList; - }; - static int ReadyEventNodeComp( const int &key, ReadyEvent::ReadyEventNode * const &data ); - - -protected: - // -------------------------------------------------------------------------------------------- - // Packet handling functions - // -------------------------------------------------------------------------------------------- - virtual PluginReceiveResult OnReceive(Packet *packet); - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - virtual void OnRakPeerShutdown(void); - - void Clear(void); - /* - bool AnyWaitersCompleted(unsigned eventIndex) const; - bool AllWaitersCompleted(unsigned eventIndex) const; - bool AllWaitersReady(unsigned eventIndex) const; - void SendAllReady(unsigned eventId, RakNetGUID guid); - void BroadcastAllReady(unsigned eventIndex); - void SendReadyStateQuery(unsigned eventId, RakNetGUID guid); - void BroadcastReadyUpdate(unsigned eventIndex); - bool AddToWaitListInternal(unsigned eventIndex, RakNetGUID guid); - bool IsLocked(unsigned eventIndex) const; - bool IsAllReadyByIndex(unsigned eventIndex) const; - */ - - void SendReadyStateQuery(unsigned eventId, RakNetGUID guid); - void SendReadyUpdate(unsigned eventIndex, unsigned systemIndex, bool forceIfNotDefault); - void BroadcastReadyUpdate(unsigned eventIndex, bool forceIfNotDefault); - void RemoveFromAllLists(RakNetGUID guid); - void OnReadyEventQuery(Packet *packet); - void PushCompletionPacket(unsigned eventId); - bool AddToWaitListInternal(unsigned eventIndex, RakNetGUID guid); - void OnReadyEventForceAllSet(Packet *packet); - void OnReadyEventPacketUpdate(Packet *packet); - void UpdateReadyStatus(unsigned eventIndex); - bool IsEventCompletedByIndex(unsigned eventIndex) const; - unsigned CreateNewEvent(int eventId, bool isReady); - bool SetEventByIndex(int eventIndex, bool isReady); - - DataStructures::OrderedList readyEventNodeList; - unsigned char channel; -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Ready event plugin. This enables a set of systems to create a signal event, set this signal as ready or unready, and to trigger the event when all systems are ready +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_ReadyEvent==1 + +#pragma once + +#include "PluginInterface2.h" +#include "DS_OrderedList.h" + +namespace RakNet { + +class RakPeerInterface; + +/// \defgroup READY_EVENT_GROUP ReadyEvent +/// \brief Peer to peer synchronized ready and unready events +/// \details +/// \ingroup PLUGINS_GROUP + +/// \ingroup READY_EVENT_GROUP +/// Returns the status of a remote system when querying with ReadyEvent::GetReadyStatus +enum ReadyEventSystemStatus +{ + /// ----------- Normal states --------------- + /// The remote system is not in the wait list, and we have never gotten a ready or complete message from it. + /// This is the default state for valid events + RES_NOT_WAITING, + /// We are waiting for this remote system to call SetEvent(thisEvent,true). + RES_WAITING, + /// The remote system called SetEvent(thisEvent,true), but it still waiting for other systems before completing the ReadyEvent. + RES_READY, + /// The remote system called SetEvent(thisEvent,true), and is no longer waiting for any other systems. + /// This remote system has completed the ReadyEvent + RES_ALL_READY, + + /// Error code, we couldn't look up the system because the event was unknown + RES_UNKNOWN_EVENT, +}; + +/// \brief Peer to peer synchronized ready and unready events +/// \details For peer to peer networks in a fully connected mesh.
+/// Solves the problem of how to tell if all peers, relative to all other peers, are in a certain ready state.
+/// For example, if A is connected to B and C, A may see that B and C are ready, but does not know if B is ready to C, or vice-versa.
+/// This plugin uses two stages to solve that problem, first, everyone I know about is ready. Second, everyone I know about is ready to everyone they know about.
+/// The user will get ID_READY_EVENT_SET and ID_READY_EVENT_UNSET as the signal flag is set or unset
+/// The user will get ID_READY_EVENT_ALL_SET when all systems are done waiting for all other systems, in which case the event is considered complete, and no longer tracked.
+/// \sa FullyConnectedMesh2 +/// \ingroup READY_EVENT_GROUP +class ReadyEvent : public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(ReadyEvent) + + // Constructor + ReadyEvent(); + + // Destructor + virtual ~ReadyEvent(); + + // -------------------------------------------------------------------------------------------- + // User functions + // -------------------------------------------------------------------------------------------- + /// Sets or updates the initial ready state for our local system. + /// If eventId is an unknown event the event is created. + /// If eventId was previously used and you want to reuse it, call DeleteEvent first, or else you will keep the same event signals from before + /// Systems previously or later added through AddToWaitList() with the same \a eventId when isReady=true will get ID_READY_EVENT_SET + /// Systems previously added through AddToWaitList with the same \a eventId will get ID_READY_EVENT_UNSET + /// For both ID_READY_EVENT_SET and ID_READY_EVENT_UNSET, eventId is encoded in bytes 1 through 1+sizeof(int) + /// \param[in] eventId A user-defined identifier to wait on. This can be a sequence counter, an event identifier, or anything else you want. + /// \param[in] isReady True to signal we are ready to proceed with this event, false to unsignal + /// \return False if event status is ID_READY_EVENT_FORCE_ALL_SET, or if we are setting to a status we are already in (no change). Otherwise true + bool SetEvent(int eventId, bool isReady); + + /// When systems can call SetEvent() with isReady==false, it is possible for one system to return true from IsEventCompleted() while the other systems return false + /// This can occur if a system SetEvent() with isReady==false while the completion message is still being transmitted. + /// If your game has the situation where some action should be taken on all systems when IsEventCompleted() is true for any system, then call ForceCompletion() when the action begins. + /// This will force all systems to return true from IsEventCompleted(). + /// \param[in] eventId A user-defined identifier to immediately set as completed + void ForceCompletion(int eventId); + + /// Deletes an event. We will no longer wait for this event, and any systems that we know have set the event will be forgotten. + /// Call this to clear memory when events are completed and you know you will never need them again. + /// \param[in] eventId A user-defined identifier + /// \return True on success. False (failure) on unknown eventId + bool DeleteEvent(int eventId); + + /// Returns what was passed to SetEvent() + /// \return The value of isReady passed to SetEvent(). Also returns false on unknown event. + bool IsEventSet(int eventId); + + /// Returns if the event is about to be ready and we are negotiating the final packets. + /// This will usually only be true for a very short time, after which IsEventCompleted should return true. + /// While this is true you cannot add to the wait list, or SetEvent() isReady to false anymore. + /// \param[in] eventId A user-defined identifier + /// \return True if any other system has completed processing. Will always be true if IsEventCompleted() is true + bool IsEventCompletionProcessing(int eventId) const; + + /// Returns if the wait list is a subset of the completion list. + /// Call this after all systems you want to wait for have been added with AddToWaitList + /// If you are waiting for a specific number of systems (such as players later connecting), also check GetRemoteWaitListSize(eventId) to be equal to 1 less than the total number of participants. + /// \param[in] eventId A user-defined identifier + /// \return True on completion. False (failure) on unknown eventId, or the set is not completed. + bool IsEventCompleted(int eventId) const; + + /// Returns if this is a known event. + /// Events may be known even if we never ourselves referenced them with SetEvent, because other systems created them via ID_READY_EVENT_SET. + /// \param[in] eventId A user-defined identifier + /// \return true if we have this event, false otherwise + bool HasEvent(int eventId); + + /// Returns the total number of events stored in the system. + /// \return The total number of events stored in the system. + unsigned GetEventListSize(void) const; + + /// Returns the event ID stored at a particular index. EventIDs are stored sorted from least to greatest. + /// \param[in] index Index into the array, from 0 to GetEventListSize() + /// \return The event ID stored at a particular index + int GetEventAtIndex(unsigned index) const; + + /// Adds a system to wait for to signal an event before considering the event complete and returning ID_READY_EVENT_ALL_SET. + /// As we add systems, if this event was previously set to true with SetEvent, these systems will get ID_READY_EVENT_SET. + /// As these systems disconnect (directly or indirectly through the router) they are removed. + /// \note If the event completion process has already started, you cannot add more systems, as this would cause the completion process to fail + /// \param[in] eventId A user-defined number previously passed to SetEvent that has not yet completed + /// \param[in] guid An address to wait for event replies from. Pass UNASSIGNED_SYSTEM_ADDRESS for all currently connected systems. Until all systems in this list have called SetEvent with this ID and true, and have this system in the list, we won't get ID_READY_EVENT_COMPLETE + /// \return True on success, false on unknown eventId (this should be considered an error) + bool AddToWaitList(int eventId, RakNetGUID guid); + + /// Removes systems from the wait list, which should have been previously added with AddToWaitList + /// \note Systems that directly or indirectly disconnect from us are automatically removed from the wait list + /// \param[in] guid The system to remove from the wait list. Pass UNASSIGNED_RAKNET_GUID for all currently connected systems. + /// \return True on success, false on unknown eventId (this should be considered an error) + bool RemoveFromWaitList(int eventId, RakNetGUID guid); + + /// Returns if a particular system is waiting on a particular event. + /// \param[in] eventId A user-defined identifier + /// \param[in] guid The system we are checking up on + /// \return True if this system is waiting on this event, false otherwise. + bool IsInWaitList(int eventId, RakNetGUID guid); + + /// Returns the total number of systems we are waiting on for this event. + /// Does not include yourself + /// \param[in] eventId A user-defined identifier + /// \return The total number of systems we are waiting on for this event. + unsigned GetRemoteWaitListSize(int eventId) const; + + /// Returns the system address of a system at a particular index, for this event. + /// \param[in] eventId A user-defined identifier + /// \param[in] index Index into the array, from 0 to GetWaitListSize() + /// \return The system address of a system at a particular index, for this event. + RakNetGUID GetFromWaitListAtIndex(int eventId, unsigned index) const; + + /// For a remote system, find out what their ready status is (waiting, signaled, complete). + /// \param[in] eventId A user-defined identifier + /// \param[in] guid Which system we are checking up on + /// \return The status of this system, for this particular event. \sa ReadyEventSystemStatus + ReadyEventSystemStatus GetReadyStatus(int eventId, RakNetGUID guid); + + /// This channel will be used for all RakPeer::Send calls + /// \param[in] newChannel The channel to use for internal RakPeer::Send calls from this system. Defaults to 0. + void SetSendChannel(unsigned char newChannel); + + // ---------------------------- ALL INTERNAL AFTER HERE ---------------------------- + /// \internal + /// Status of a remote system + struct RemoteSystem + { + MessageID lastSentStatus, lastReceivedStatus; + RakNetGUID rakNetGuid; + }; + static int RemoteSystemCompByGuid( const RakNetGUID &key, const RemoteSystem &data ); + /// \internal + /// An event, with a set of systems we are waiting for, a set of systems that are signaled, and a set of systems with completed events + struct ReadyEventNode + { + int eventId; // Sorted on this + MessageID eventStatus; + DataStructures::OrderedList systemList; + }; + static int ReadyEventNodeComp( const int &key, ReadyEvent::ReadyEventNode * const &data ); + + +protected: + // -------------------------------------------------------------------------------------------- + // Packet handling functions + // -------------------------------------------------------------------------------------------- + virtual PluginReceiveResult OnReceive(Packet *packet); + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + virtual void OnRakPeerShutdown(void); + + void Clear(void); + /* + bool AnyWaitersCompleted(unsigned eventIndex) const; + bool AllWaitersCompleted(unsigned eventIndex) const; + bool AllWaitersReady(unsigned eventIndex) const; + void SendAllReady(unsigned eventId, RakNetGUID guid); + void BroadcastAllReady(unsigned eventIndex); + void SendReadyStateQuery(unsigned eventId, RakNetGUID guid); + void BroadcastReadyUpdate(unsigned eventIndex); + bool AddToWaitListInternal(unsigned eventIndex, RakNetGUID guid); + bool IsLocked(unsigned eventIndex) const; + bool IsAllReadyByIndex(unsigned eventIndex) const; + */ + + void SendReadyStateQuery(unsigned eventId, RakNetGUID guid); + void SendReadyUpdate(unsigned eventIndex, unsigned systemIndex, bool forceIfNotDefault); + void BroadcastReadyUpdate(unsigned eventIndex, bool forceIfNotDefault); + void RemoveFromAllLists(RakNetGUID guid); + void OnReadyEventQuery(Packet *packet); + void PushCompletionPacket(unsigned eventId); + bool AddToWaitListInternal(unsigned eventIndex, RakNetGUID guid); + void OnReadyEventForceAllSet(Packet *packet); + void OnReadyEventPacketUpdate(Packet *packet); + void UpdateReadyStatus(unsigned eventIndex); + bool IsEventCompletedByIndex(unsigned eventIndex) const; + unsigned CreateNewEvent(int eventId, bool isReady); + bool SetEventByIndex(int eventIndex, bool isReady); + + DataStructures::OrderedList readyEventNodeList; + unsigned char channel; +}; + +} // namespace RakNet + +#endif + diff --git a/Source/RefCountedObj.h b/Source/RefCountedObj.h index 0708b6967..0d5eebaf7 100644 --- a/Source/RefCountedObj.h +++ b/Source/RefCountedObj.h @@ -1,33 +1,31 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief \b Reference counted object. Very simple class for quick and dirty uses. -/// - - - -#ifndef __REF_COUNTED_OBJ_H -#define __REF_COUNTED_OBJ_H - -#include "RakMemoryOverride.h" - -/// World's simplest class :) -class RefCountedObj -{ - public: - RefCountedObj() {refCount=1;} - virtual ~RefCountedObj() {} - void AddRef(void) {refCount++;} - void Deref(void) {if (--refCount==0) RakNet::OP_DELETE(this, _FILE_AND_LINE_);} - int refCount; -}; - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief \b Reference counted object. Very simple class for quick and dirty uses. +/// + + + +#pragma once + +#include "RakMemoryOverride.h" + +/// World's simplest class :) +class RefCountedObj +{ + public: + RefCountedObj() {refCount=1;} + virtual ~RefCountedObj() {} + void AddRef(void) {refCount++;} + void Deref(void) {if (--refCount==0) RakNet::OP_DELETE(this, _FILE_AND_LINE_);} + int refCount; +}; + diff --git a/Source/RelayPlugin.h b/Source/RelayPlugin.h index 5c24b7d7d..29f90bf4c 100644 --- a/Source/RelayPlugin.h +++ b/Source/RelayPlugin.h @@ -1,166 +1,164 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Contains the class RelayPlugin -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_RelayPlugin==1 - -#ifndef __RELAY_PLUGIN_H -#define __RELAY_PLUGIN_H - -#include "PluginInterface2.h" -#include "RakString.h" -#include "DS_Hash.h" - -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -/// \defgroup RELAY_PLUGIN_GROUP RelayPlugin -/// \brief A simple class to relay messages from one system to another through an intermediary -/// \ingroup PLUGINS_GROUP - -namespace RakNet -{ - -/// Forward declarations -class RakPeerInterface; - -enum RelayPluginEnums -{ - // Server handled messages - RPE_MESSAGE_TO_SERVER_FROM_CLIENT, - RPE_ADD_CLIENT_REQUEST_FROM_CLIENT, - RPE_REMOVE_CLIENT_REQUEST_FROM_CLIENT, - RPE_GROUP_MESSAGE_FROM_CLIENT, - RPE_JOIN_GROUP_REQUEST_FROM_CLIENT, - RPE_LEAVE_GROUP_REQUEST_FROM_CLIENT, - RPE_GET_GROUP_LIST_REQUEST_FROM_CLIENT, - // Client handled messages - RPE_MESSAGE_TO_CLIENT_FROM_SERVER, - RPE_ADD_CLIENT_NOT_ALLOWED, - RPE_ADD_CLIENT_TARGET_NOT_CONNECTED, - RPE_ADD_CLIENT_NAME_ALREADY_IN_USE, - RPE_ADD_CLIENT_SUCCESS, - RPE_USER_ENTERED_ROOM, - RPE_USER_LEFT_ROOM, - RPE_GROUP_MSG_FROM_SERVER, - RPE_GET_GROUP_LIST_REPLY_FROM_SERVER, - RPE_JOIN_GROUP_SUCCESS, - RPE_JOIN_GROUP_FAILURE, -}; - -/// \brief A simple class to relay messages from one system to another, identifying remote systems by a string. -/// \ingroup RELAY_PLUGIN_GROUP -class RAK_DLL_EXPORT RelayPlugin : public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(RelayPlugin) - - /// Constructor - RelayPlugin(); - - /// Destructor - virtual ~RelayPlugin(); - - /// \brief Forward messages from any system, to the system specified by the combination of key and guid. The sending system only needs to know the key. - /// \param[in] key A string to identify the target's RakNetGUID. This is so the sending system does not need to know the RakNetGUID of the target system. The key should be unique among all guids added. If the key is not unique, only one system will be sent to (at random). - /// \param[in] guid The RakNetGuid of the system to send to. If this system disconnects, it is removed from the internal hash - /// \return RPE_ADD_CLIENT_TARGET_NOT_CONNECTED, RPE_ADD_CLIENT_NAME_ALREADY_IN_USE, or RPE_ADD_CLIENT_OK - RelayPluginEnums AddParticipantOnServer(const RakString &key, const RakNetGUID &guid); - - /// \brief Remove a chat participant - void RemoveParticipantOnServer(const RakNetGUID &guid); - - /// \brief If true, then if the client calls AddParticipantRequestFromClient(), the server will call AddParticipantOnServer() automatically - /// Defaults to false - /// \param[in] accept true to accept, false to not. - void SetAcceptAddParticipantRequests(bool accept); - - /// \brief Request from the client for the server to call AddParticipantOnServer() - /// \pre The server must have called SetAcceptAddParticipantRequests(true) or the request will be ignored - /// \param[in] key A string to identify out system. Passed to \a key on AddParticipantOnServer() - /// \param[in] relayPluginServerGuid the RakNetGUID of the system running RelayPlugin - void AddParticipantRequestFromClient(const RakString &key, const RakNetGUID &relayPluginServerGuid); - - /// \brief Remove yourself as a participant - void RemoveParticipantRequestFromClient(const RakNetGUID &relayPluginServerGuid); - - /// \brief Request that the server relay \a bitStream to the system designated by \a key - /// \param[in] relayPluginServerGuid the RakNetGUID of the system running RelayPlugin - /// \param[in] destinationGuid The key value passed to AddParticipant() earlier on the server. If this was not done, the server will not relay the message (it will be silently discarded). - /// \param[in] bitStream The data to relay - /// \param[in] priority See the parameter of the same name in RakPeerInterface::Send() - /// \param[in] reliability See the parameter of the same name in RakPeerInterface::Send() - /// \param[in] orderingChannel See the parameter of the same name in RakPeerInterface::Send() - void SendToParticipant(const RakNetGUID &relayPluginServerGuid, const RakString &destinationGuid, BitStream *bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel); - - void SendGroupMessage(const RakNetGUID &relayPluginServerGuid, BitStream *bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel); - void JoinGroupRequest(const RakNetGUID &relayPluginServerGuid, RakString groupName); - void LeaveGroup(const RakNetGUID &relayPluginServerGuid); - void GetGroupList(const RakNetGUID &relayPluginServerGuid); - - /// \internal - virtual PluginReceiveResult OnReceive(Packet *packet); - /// \internal - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - - struct StrAndGuidAndRoom - { - RakString str; - RakNetGUID guid; - RakString currentRoom; - }; - - struct StrAndGuid - { - RakString str; - RakNetGUID guid; - }; - - struct RP_Group - { - RakString roomName; - DataStructures::List usersInRoom; - }; - -protected: - - RelayPlugin::RP_Group* JoinGroup(RakNetGUID userGuid, RakString roomName); - RelayPlugin::RP_Group* JoinGroup(RP_Group* room, StrAndGuidAndRoom **strAndGuidSender); - void LeaveGroup(StrAndGuidAndRoom **strAndGuidSender); - void NotifyUsersInRoom(RP_Group *room, int msg, const RakString& message); - void SendMessageToRoom(StrAndGuidAndRoom **strAndGuidSender, BitStream* message); - void SendChatRoomsList(RakNetGUID target); - void OnGroupMessageFromClient(Packet *packet); - void OnJoinGroupRequestFromClient(Packet *packet); - void OnLeaveGroupRequestFromClient(Packet *packet); - - DataStructures::Hash strToGuidHash; - DataStructures::Hash guidToStrHash; - DataStructures::List chatRooms; - bool acceptAddParticipantRequests; - -}; - -} // End namespace - -#endif - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Contains the class RelayPlugin +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_RelayPlugin==1 + +#pragma once + +#include "PluginInterface2.h" +#include "RakString.h" +#include "DS_Hash.h" + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +/// \defgroup RELAY_PLUGIN_GROUP RelayPlugin +/// \brief A simple class to relay messages from one system to another through an intermediary +/// \ingroup PLUGINS_GROUP + +namespace RakNet +{ + +/// Forward declarations +class RakPeerInterface; + +enum RelayPluginEnums +{ + // Server handled messages + RPE_MESSAGE_TO_SERVER_FROM_CLIENT, + RPE_ADD_CLIENT_REQUEST_FROM_CLIENT, + RPE_REMOVE_CLIENT_REQUEST_FROM_CLIENT, + RPE_GROUP_MESSAGE_FROM_CLIENT, + RPE_JOIN_GROUP_REQUEST_FROM_CLIENT, + RPE_LEAVE_GROUP_REQUEST_FROM_CLIENT, + RPE_GET_GROUP_LIST_REQUEST_FROM_CLIENT, + // Client handled messages + RPE_MESSAGE_TO_CLIENT_FROM_SERVER, + RPE_ADD_CLIENT_NOT_ALLOWED, + RPE_ADD_CLIENT_TARGET_NOT_CONNECTED, + RPE_ADD_CLIENT_NAME_ALREADY_IN_USE, + RPE_ADD_CLIENT_SUCCESS, + RPE_USER_ENTERED_ROOM, + RPE_USER_LEFT_ROOM, + RPE_GROUP_MSG_FROM_SERVER, + RPE_GET_GROUP_LIST_REPLY_FROM_SERVER, + RPE_JOIN_GROUP_SUCCESS, + RPE_JOIN_GROUP_FAILURE, +}; + +/// \brief A simple class to relay messages from one system to another, identifying remote systems by a string. +/// \ingroup RELAY_PLUGIN_GROUP +class RAK_DLL_EXPORT RelayPlugin : public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(RelayPlugin) + + /// Constructor + RelayPlugin(); + + /// Destructor + virtual ~RelayPlugin(); + + /// \brief Forward messages from any system, to the system specified by the combination of key and guid. The sending system only needs to know the key. + /// \param[in] key A string to identify the target's RakNetGUID. This is so the sending system does not need to know the RakNetGUID of the target system. The key should be unique among all guids added. If the key is not unique, only one system will be sent to (at random). + /// \param[in] guid The RakNetGuid of the system to send to. If this system disconnects, it is removed from the internal hash + /// \return RPE_ADD_CLIENT_TARGET_NOT_CONNECTED, RPE_ADD_CLIENT_NAME_ALREADY_IN_USE, or RPE_ADD_CLIENT_OK + RelayPluginEnums AddParticipantOnServer(const RakString &key, const RakNetGUID &guid); + + /// \brief Remove a chat participant + void RemoveParticipantOnServer(const RakNetGUID &guid); + + /// \brief If true, then if the client calls AddParticipantRequestFromClient(), the server will call AddParticipantOnServer() automatically + /// Defaults to false + /// \param[in] accept true to accept, false to not. + void SetAcceptAddParticipantRequests(bool accept); + + /// \brief Request from the client for the server to call AddParticipantOnServer() + /// \pre The server must have called SetAcceptAddParticipantRequests(true) or the request will be ignored + /// \param[in] key A string to identify out system. Passed to \a key on AddParticipantOnServer() + /// \param[in] relayPluginServerGuid the RakNetGUID of the system running RelayPlugin + void AddParticipantRequestFromClient(const RakString &key, const RakNetGUID &relayPluginServerGuid); + + /// \brief Remove yourself as a participant + void RemoveParticipantRequestFromClient(const RakNetGUID &relayPluginServerGuid); + + /// \brief Request that the server relay \a bitStream to the system designated by \a key + /// \param[in] relayPluginServerGuid the RakNetGUID of the system running RelayPlugin + /// \param[in] destinationGuid The key value passed to AddParticipant() earlier on the server. If this was not done, the server will not relay the message (it will be silently discarded). + /// \param[in] bitStream The data to relay + /// \param[in] priority See the parameter of the same name in RakPeerInterface::Send() + /// \param[in] reliability See the parameter of the same name in RakPeerInterface::Send() + /// \param[in] orderingChannel See the parameter of the same name in RakPeerInterface::Send() + void SendToParticipant(const RakNetGUID &relayPluginServerGuid, const RakString &destinationGuid, BitStream *bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel); + + void SendGroupMessage(const RakNetGUID &relayPluginServerGuid, BitStream *bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel); + void JoinGroupRequest(const RakNetGUID &relayPluginServerGuid, RakString groupName); + void LeaveGroup(const RakNetGUID &relayPluginServerGuid); + void GetGroupList(const RakNetGUID &relayPluginServerGuid); + + /// \internal + virtual PluginReceiveResult OnReceive(Packet *packet); + /// \internal + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + + struct StrAndGuidAndRoom + { + RakString str; + RakNetGUID guid; + RakString currentRoom; + }; + + struct StrAndGuid + { + RakString str; + RakNetGUID guid; + }; + + struct RP_Group + { + RakString roomName; + DataStructures::List usersInRoom; + }; + +protected: + + RelayPlugin::RP_Group* JoinGroup(RakNetGUID userGuid, RakString roomName); + RelayPlugin::RP_Group* JoinGroup(RP_Group* room, StrAndGuidAndRoom **strAndGuidSender); + void LeaveGroup(StrAndGuidAndRoom **strAndGuidSender); + void NotifyUsersInRoom(RP_Group *room, int msg, const RakString& message); + void SendMessageToRoom(StrAndGuidAndRoom **strAndGuidSender, BitStream* message); + void SendChatRoomsList(RakNetGUID target); + void OnGroupMessageFromClient(Packet *packet); + void OnJoinGroupRequestFromClient(Packet *packet); + void OnLeaveGroupRequestFromClient(Packet *packet); + + DataStructures::Hash strToGuidHash; + DataStructures::Hash guidToStrHash; + DataStructures::List chatRooms; + bool acceptAddParticipantRequests; + +}; + +} // End namespace + +#endif + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif + diff --git a/Source/ReliabilityLayer.h b/Source/ReliabilityLayer.h index 14fbd8ff0..9ae147ce2 100644 --- a/Source/ReliabilityLayer.h +++ b/Source/ReliabilityLayer.h @@ -1,596 +1,594 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief \b [Internal] Datagram reliable, ordered, unordered and sequenced sends. Flow control. Message splitting, reassembly, and coalescence. -/// - - -#ifndef __RELIABILITY_LAYER_H -#define __RELIABILITY_LAYER_H - -#include "RakMemoryOverride.h" -#include "MTUSize.h" -#include "DS_LinkedList.h" -#include "DS_List.h" -#include "SocketLayer.h" -#include "PacketPriority.h" -#include "DS_Queue.h" -#include "BitStream.h" -#include "InternalPacket.h" -#include "RakNetStatistics.h" -#include "DR_SHA1.h" -#include "DS_OrderedList.h" -#include "DS_RangeList.h" -#include "DS_BPlusTree.h" -#include "DS_MemoryPool.h" -#include "RakNetDefines.h" -#include "DS_Heap.h" -#include "BitStream.h" -#include "NativeFeatureIncludes.h" -#include "SecureHandshake.h" -#include "PluginInterface2.h" -#include "Rand.h" -#include "RakNetSocket2.h" - -#if USE_SLIDING_WINDOW_CONGESTION_CONTROL!=1 -#include "CCRakNetUDT.h" -#define INCLUDE_TIMESTAMP_WITH_DATAGRAMS 1 -#else -#include "CCRakNetSlidingWindow.h" -#define INCLUDE_TIMESTAMP_WITH_DATAGRAMS 0 -#endif - -/// Number of ordered streams available. You can use up to 32 ordered streams -#define NUMBER_OF_ORDERED_STREAMS 32 // 2^5 - -#define RESEND_TREE_ORDER 32 - -namespace RakNet { - - /// Forward declarations -class PluginInterface2; -class RakNetRandom; -typedef uint64_t reliabilityHeapWeightType; - -// int SplitPacketIndexComp( SplitPacketIndexType const &key, InternalPacket* const &data ); -struct SplitPacketChannel// -{ - CCTimeType lastUpdateTime; - - DataStructures::List splitPacketList; - -#if PREALLOCATE_LARGE_MESSAGES==1 - InternalPacket *returnedPacket; - bool gotFirstPacket; - unsigned int stride; - unsigned int splitPacketsArrived; -#else - // This is here for progress notifications, since progress notifications return the first packet data, if available - InternalPacket *firstPacket; -#endif - -}; -int RAK_DLL_EXPORT SplitPacketChannelComp( SplitPacketIdType const &key, SplitPacketChannel* const &data ); - -// Helper class -struct BPSTracker -{ - BPSTracker(); - ~BPSTracker(); - void Reset(const char *file, unsigned int line); - inline void Push1(CCTimeType time, uint64_t value1) {dataQueue.Push(TimeAndValue2(time,value1),_FILE_AND_LINE_); total1+=value1; lastSec1+=value1;} -// void Push2(RakNet::TimeUS time, uint64_t value1, uint64_t value2); - inline uint64_t GetBPS1(CCTimeType time) {(void) time; return lastSec1;} - inline uint64_t GetBPS1Threadsafe(CCTimeType time) {(void) time; return lastSec1;} -// uint64_t GetBPS2(RakNetTimeUS time); -// void GetBPS1And2(RakNetTimeUS time, uint64_t &out1, uint64_t &out2); - uint64_t GetTotal1(void) const; -// uint64_t GetTotal2(void) const; - - struct TimeAndValue2 - { - TimeAndValue2(); - ~TimeAndValue2(); - TimeAndValue2(CCTimeType t, uint64_t v1); - // TimeAndValue2(RakNet::TimeUS t, uint64_t v1, uint64_t v2); - // uint64_t value1, value2; - uint64_t value1; - CCTimeType time; - }; - - uint64_t total1, lastSec1; -// uint64_t total2, lastSec2; - DataStructures::Queue dataQueue; - void ClearExpired1(CCTimeType time); -// void ClearExpired2(RakNet::TimeUS time); -}; - -/// Datagram reliable, ordered, unordered and sequenced sends. Flow control. Message splitting, reassembly, and coalescence. -class ReliabilityLayer// -{ -public: - - // Constructor - ReliabilityLayer(); - - // Destructor - ~ReliabilityLayer(); - - /// Resets the layer for reuse - void Reset( bool resetVariables, int MTUSize, bool _useSecurity ); - - /// Set the time, in MS, to use before considering ourselves disconnected after not being able to deliver a reliable packet - /// Default time is 10,000 or 10 seconds in release and 30,000 or 30 seconds in debug. - /// \param[in] time Time, in MS - void SetTimeoutTime( RakNet::TimeMS time ); - - /// Returns the value passed to SetTimeoutTime. or the default if it was never called - /// \param[out] the value passed to SetTimeoutTime - RakNet::TimeMS GetTimeoutTime(void); - - /// Packets are read directly from the socket layer and skip the reliability layer because unconnected players do not use the reliability layer - /// This function takes packet data after a player has been confirmed as connected. - /// \param[in] buffer The socket data - /// \param[in] length The length of the socket data - /// \param[in] systemAddress The player that this data is from - /// \param[in] messageHandlerList A list of registered plugins - /// \param[in] MTUSize maximum datagram size - /// \retval true Success - /// \retval false Modified packet - bool HandleSocketReceiveFromConnectedPlayer( - const char *buffer, unsigned int length, SystemAddress &systemAddress, DataStructures::List &messageHandlerList, int MTUSize, - RakNetSocket2 *s, RakNetRandom *rnr, CCTimeType timeRead, BitStream &updateBitStream); - - /// This allocates bytes and writes a user-level message to those bytes. - /// \param[out] data The message - /// \return Returns number of BITS put into the buffer - BitSize_t Receive( unsigned char**data ); - - /// Puts data on the send queue - /// \param[in] data The data to send - /// \param[in] numberOfBitsToSend The length of \a data in bits - /// \param[in] priority The priority level for the send - /// \param[in] reliability The reliability type for the send - /// \param[in] orderingChannel 0 to 31. Specifies what channel to use, for relational ordering and sequencing of packets. - /// \param[in] makeDataCopy If true \a data will be copied. Otherwise, only a pointer will be stored. - /// \param[in] MTUSize maximum datagram size - /// \param[in] currentTime Current time, as per RakNet::GetTimeMS() - /// \param[in] receipt This number will be returned back with ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS and is only returned with the reliability types that contain RECEIPT in the name - /// \return True or false for success or failure. - bool Send( char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, unsigned char orderingChannel, bool makeDataCopy, int MTUSize, CCTimeType currentTime, uint32_t receipt ); - - /// Call once per game cycle. Handles internal lists and actually does the send. - /// \param[in] s the communication end point - /// \param[in] systemAddress The Unique Player Identifier who shouldhave sent some packets - /// \param[in] MTUSize maximum datagram size - /// \param[in] time current system time - /// \param[in] maxBitsPerSecond if non-zero, enforces that outgoing bandwidth does not exceed this amount - /// \param[in] messageHandlerList A list of registered plugins - void Update( RakNetSocket2 *s, SystemAddress &systemAddress, int MTUSize, CCTimeType time, - unsigned bitsPerSecondLimit, - DataStructures::List &messageHandlerList, - RakNetRandom *rnr, BitStream &updateBitStream); - - /// Were you ever unable to deliver a packet despite retries? - /// \return true means the connection has been lost. Otherwise not. - bool IsDeadConnection( void ) const; - - /// Causes IsDeadConnection to return true - void KillConnection(void); - - /// Get Statistics - /// \return A pointer to a static struct, filled out with current statistical information. - RakNetStatistics * GetStatistics( RakNetStatistics *rns ); - - ///Are we waiting for any data to be sent out or be processed by the player? - bool IsOutgoingDataWaiting(void); - bool AreAcksWaiting(void); - - // Set outgoing lag and packet loss properties - void ApplyNetworkSimulator( double _maxSendBPS, RakNet::TimeMS _minExtraPing, RakNet::TimeMS _extraPingVariance ); - - /// Returns if you previously called ApplyNetworkSimulator - /// \return If you previously called ApplyNetworkSimulator - bool IsNetworkSimulatorActive( void ); - - void SetSplitMessageProgressInterval(int interval); - void SetUnreliableTimeout(RakNet::TimeMS timeoutMS); - /// Has a lot of time passed since the last ack - bool AckTimeout(RakNet::Time curTime); - CCTimeType GetNextSendTime(void) const; - CCTimeType GetTimeBetweenPackets(void) const; -#if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1 - CCTimeType GetAckPing(void) const; -#endif - RakNet::TimeMS GetTimeLastDatagramArrived(void) const {return timeLastDatagramArrived;} - - // If true, will update time between packets quickly based on ping calculations - //void SetDoFastThroughputReactions(bool fast); - - // Encoded as numMessages[unsigned int], message1BitLength[unsigned int], message1Data (aligned), ... - //void GetUndeliveredMessages(RakNet::BitStream *messages, int MTUSize); - -private: - /// Send the contents of a bitstream to the socket - /// \param[in] s The socket used for sending data - /// \param[in] systemAddress The address and port to send to - /// \param[in] bitStream The data to send. - void SendBitStream( RakNetSocket2 *s, SystemAddress &systemAddress, RakNet::BitStream *bitStream, RakNetRandom *rnr, CCTimeType currentTime); - - ///Parse an internalPacket and create a bitstream to represent this data - /// \return Returns number of bits used - BitSize_t WriteToBitStreamFromInternalPacket( RakNet::BitStream *bitStream, const InternalPacket *const internalPacket, CCTimeType curTime ); - - - /// Parse a bitstream and create an internal packet to represent this data - InternalPacket* CreateInternalPacketFromBitStream( RakNet::BitStream *bitStream, CCTimeType time ); - - /// Does what the function name says - unsigned RemovePacketFromResendListAndDeleteOlderReliableSequenced( const MessageNumberType messageNumber, CCTimeType time, DataStructures::List &messageHandlerList, const SystemAddress &systemAddress ); - - /// Acknowledge receipt of the packet with the specified messageNumber - void SendAcknowledgementPacket( const DatagramSequenceNumberType messageNumber, CCTimeType time); - - /// This will return true if we should not send at this time - bool IsSendThrottled( int MTUSize ); - - /// We lost a packet - void UpdateWindowFromPacketloss( CCTimeType time ); - - /// Increase the window size - void UpdateWindowFromAck( CCTimeType time ); - - /// Parse an internalPacket and figure out how many header bits would be written. Returns that number - BitSize_t GetMaxMessageHeaderLengthBits( void ); - BitSize_t GetMessageHeaderLengthBits( const InternalPacket *const internalPacket ); - - /// Get the SHA1 code - void GetSHA1( unsigned char * const buffer, unsigned int nbytes, char code[ SHA1_LENGTH ] ); - - /// Check the SHA1 code - bool CheckSHA1( char code[ SHA1_LENGTH ], unsigned char * const buffer, unsigned int nbytes ); - - /// Search the specified list for sequenced packets on the specified ordering channel, optionally skipping those with splitPacketId, and delete them -// void DeleteSequencedPacketsInList( unsigned char orderingChannel, DataStructures::List&theList, int splitPacketId = -1 ); - - /// Search the specified list for sequenced packets with a value less than orderingIndex and delete them -// void DeleteSequencedPacketsInList( unsigned char orderingChannel, DataStructures::Queue&theList ); - - /// Returns true if newPacketOrderingIndex is older than the waitingForPacketOrderingIndex - bool IsOlderOrderedPacket( OrderingIndexType newPacketOrderingIndex, OrderingIndexType waitingForPacketOrderingIndex ); - - /// Split the passed packet into chunks under MTU_SIZE bytes (including headers) and save those new chunks - void SplitPacket( InternalPacket *internalPacket ); - - /// Insert a packet into the split packet list - void InsertIntoSplitPacketList( InternalPacket * internalPacket, CCTimeType time ); - - /// Take all split chunks with the specified splitPacketId and try to reconstruct a packet. If we can, allocate and return it. Otherwise return 0 - InternalPacket * BuildPacketFromSplitPacketList( SplitPacketIdType splitPacketId, CCTimeType time, - RakNetSocket2 *s, SystemAddress &systemAddress, RakNetRandom *rnr, BitStream &updateBitStream); - InternalPacket * BuildPacketFromSplitPacketList( SplitPacketChannel *splitPacketChannel, CCTimeType time ); - - /// Delete any unreliable split packets that have long since expired - //void DeleteOldUnreliableSplitPackets( CCTimeType time ); - - /// Creates a copy of the specified internal packet with data copied from the original starting at dataByteOffset for dataByteLength bytes. - /// Does not copy any split data parameters as that information is always generated does not have any reason to be copied - InternalPacket * CreateInternalPacketCopy( InternalPacket *original, int dataByteOffset, int dataByteLength, CCTimeType time ); - - /// Get the specified ordering list - // DataStructures::LinkedList *GetOrderingListAtOrderingStream( unsigned char orderingChannel ); - - /// Add the internal packet to the ordering list in order based on order index - // void AddToOrderingList( InternalPacket * internalPacket ); - - /// Inserts a packet into the resend list in order - void InsertPacketIntoResendList( InternalPacket *internalPacket, CCTimeType time, bool firstResend, bool modifyUnacknowledgedBytes ); - - /// Memory handling - void FreeMemory( bool freeAllImmediately ); - - /// Memory handling - void FreeThreadSafeMemory( void ); - - // Initialize the variables - void InitializeVariables( void ); - - /// Given the current time, is this time so old that we should consider it a timeout? - bool IsExpiredTime(unsigned int input, CCTimeType currentTime) const; - - // Make it so we don't do resends within a minimum threshold of time - void UpdateNextActionTime(void); - - - /// Does this packet number represent a packet that was skipped (out of order?) - //unsigned int IsReceivedPacketHole(unsigned int input, RakNet::TimeMS currentTime) const; - - /// Skip an element in the received packets list - //unsigned int MakeReceivedPacketHole(unsigned int input) const; - - /// How many elements are waiting to be resent? - unsigned int GetResendListDataSize(void) const; - - /// Update all memory which is not threadsafe - void UpdateThreadedMemory(void); - - void CalculateHistogramAckSize(void); - - // Used ONLY for RELIABLE_ORDERED - // RELIABLE_SEQUENCED just returns the newest one - // DataStructures::List*> orderingList; - DataStructures::Queue outputQueue; - int splitMessageProgressInterval; - CCTimeType unreliableTimeout; - - struct MessageNumberNode - { - DatagramSequenceNumberType messageNumber; - MessageNumberNode *next; - }; - struct DatagramHistoryNode - { - DatagramHistoryNode() {} - DatagramHistoryNode(MessageNumberNode *_head, CCTimeType ts - ) : - head(_head), timeSent(ts) - {} - MessageNumberNode *head; - CCTimeType timeSent; - }; - // Queue length is programmatically restricted to DATAGRAM_MESSAGE_ID_ARRAY_LENGTH - // This is essentially an O(1) lookup to get a DatagramHistoryNode given an index - // datagramHistory holds a linked list of MessageNumberNode. Each MessageNumberNode refers to one element in resendList which can be cleared on an ack. - DataStructures::Queue datagramHistory; - DataStructures::MemoryPool datagramHistoryMessagePool; - - struct UnreliableWithAckReceiptNode - { - UnreliableWithAckReceiptNode() {} - UnreliableWithAckReceiptNode(DatagramSequenceNumberType _datagramNumber, uint32_t _sendReceiptSerial, RakNet::TimeUS _nextActionTime) : - datagramNumber(_datagramNumber), sendReceiptSerial(_sendReceiptSerial), nextActionTime(_nextActionTime) - {} - DatagramSequenceNumberType datagramNumber; - uint32_t sendReceiptSerial; - RakNet::TimeUS nextActionTime; - }; - DataStructures::List unreliableWithAckReceiptHistory; - - void RemoveFromDatagramHistory(DatagramSequenceNumberType index); - MessageNumberNode* GetMessageNumberNodeByDatagramIndex(DatagramSequenceNumberType index, CCTimeType *timeSent); - void AddFirstToDatagramHistory(DatagramSequenceNumberType datagramNumber, CCTimeType timeSent); - MessageNumberNode* AddFirstToDatagramHistory(DatagramSequenceNumberType datagramNumber, DatagramSequenceNumberType messageNumber, CCTimeType timeSent); - MessageNumberNode* AddSubsequentToDatagramHistory(MessageNumberNode *messageNumberNode, DatagramSequenceNumberType messageNumber); - DatagramSequenceNumberType datagramHistoryPopCount; - - DataStructures::MemoryPool internalPacketPool; - // DataStructures::BPlusTree resendTree; - InternalPacket *resendBuffer[RESEND_BUFFER_ARRAY_LENGTH]; - InternalPacket *resendLinkedListHead; - InternalPacket *unreliableLinkedListHead; - void RemoveFromUnreliableLinkedList(InternalPacket *internalPacket); - void AddToUnreliableLinkedList(InternalPacket *internalPacket); -// unsigned int numPacketsOnResendBuffer; - //unsigned int blockWindowIncreaseUntilTime; - // DataStructures::RangeList acknowlegements; - // Resend list is a tree of packets we need to resend - - // Set to the current time when the resend queue is no longer empty - // Set to zero when it becomes empty - // Set to the current time if it is not zero, and we get incoming data - // If the current time - timeResendQueueNonEmpty is greater than a threshold, we are disconnected -// CCTimeType timeResendQueueNonEmpty; - RakNet::TimeMS timeLastDatagramArrived; - - - // If we backoff due to packetloss, don't remeasure until all waiting resends have gone out or else we overcount -// bool packetlossThisSample; -// int backoffThisSample; -// unsigned packetlossThisSampleResendCount; -// CCTimeType lastPacketlossTime; - - //DataStructures::Queue sendPacketSet[ NUMBER_OF_PRIORITIES ]; - DataStructures::Heap outgoingPacketBuffer; - reliabilityHeapWeightType outgoingPacketBufferNextWeights[NUMBER_OF_PRIORITIES]; - void InitHeapWeights(void); - reliabilityHeapWeightType GetNextWeight(int priorityLevel); -// unsigned int messageInSendBuffer[NUMBER_OF_PRIORITIES]; -// double bytesInSendBuffer[NUMBER_OF_PRIORITIES]; - - - DataStructures::OrderedList splitPacketChannelList; - - MessageNumberType sendReliableMessageNumberIndex; - MessageNumberType internalOrderIndex; - //unsigned int windowSize; - //RakNet::BitStream updateBitStream; - bool deadConnection, cheater; - SplitPacketIdType splitPacketId; - RakNet::TimeMS timeoutTime; // How long to wait in MS before timing someone out - //int MAX_AVERAGE_PACKETS_PER_SECOND; // Name says it all -// int RECEIVED_PACKET_LOG_LENGTH, requestedReceivedPacketLogLength; // How big the receivedPackets array is -// unsigned int *receivedPackets; - RakNetStatistics statistics; - - // Algorithm for blending ordered and sequenced on the same channel: - // 1. Each ordered message transmits OrderingIndexType orderedWriteIndex. There are NUMBER_OF_ORDERED_STREAMS independent values of these. The value - // starts at 0. Every time an ordered message is sent, the value increments by 1 - // 2. Each sequenced message contains the current value of orderedWriteIndex for that channel, and additionally OrderingIndexType sequencedWriteIndex. - // sequencedWriteIndex resets to 0 every time orderedWriteIndex increments. It increments by 1 every time a sequenced message is sent. - // 3. The receiver maintains the next expected value for the orderedWriteIndex, stored in orderedReadIndex. - // 4. As messages arrive: - // If a message has the current ordering index, and is sequenced, and is < the current highest sequence value, discard - // If a message has the current ordering index, and is sequenced, and is >= the current highest sequence value, return immediately - // If a message has a greater ordering index, and is sequenced or ordered, buffer it - // If a message has the current ordering index, and is ordered, buffer, then push off messages from buffer - // 5. Pushing off messages from buffer: - // Messages in buffer are put in a minheap. The value of each node is calculated such that messages are returned: - // A. (lowest ordering index, lowest sequence index) - // B. (lowest ordering index, no sequence index) - // Messages are pushed off until the heap is empty, or the next message to be returned does not preserve the ordered index - // For an empty heap, the heap weight should start at the lowest value based on the next expected ordering index, to avoid variable overflow - - // Sender increments this by 1 for every ordered message sent - OrderingIndexType orderedWriteIndex[NUMBER_OF_ORDERED_STREAMS]; - // Sender increments by 1 for every sequenced message sent. Resets to 0 when an ordered message is sent - OrderingIndexType sequencedWriteIndex[NUMBER_OF_ORDERED_STREAMS]; - // Next expected index for ordered messages. - OrderingIndexType orderedReadIndex[NUMBER_OF_ORDERED_STREAMS]; - // Highest value received for sequencedWriteIndex for the current value of orderedReadIndex on the same channel. - OrderingIndexType highestSequencedReadIndex[NUMBER_OF_ORDERED_STREAMS]; - DataStructures::Heap orderingHeaps[NUMBER_OF_ORDERED_STREAMS]; - OrderingIndexType heapIndexOffsets[NUMBER_OF_ORDERED_STREAMS]; - - - - - - - -// CCTimeType histogramStart; -// unsigned histogramBitsSent; - - - /// Memory-efficient receivedPackets algorithm: - /// receivedPacketsBaseIndex is the packet number we are expecting - /// Everything under receivedPacketsBaseIndex is a packet we already got - /// Everything over receivedPacketsBaseIndex is stored in hasReceivedPacketQueue - /// It stores the time to stop waiting for a particular packet number, where the packet number is receivedPacketsBaseIndex + the index into the queue - /// If 0, we got got that packet. Otherwise, the time to give up waiting for that packet. - /// If we get a packet number where (receivedPacketsBaseIndex-packetNumber) is less than half the range of receivedPacketsBaseIndex then it is a duplicate - /// Otherwise, it is a duplicate packet (and ignore it). - // DataStructures::Queue hasReceivedPacketQueue; - DataStructures::Queue hasReceivedPacketQueue; - DatagramSequenceNumberType receivedPacketsBaseIndex; - bool resetReceivedPackets; - - CCTimeType lastUpdateTime; - CCTimeType timeBetweenPackets, nextSendTime; -#if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1 - CCTimeType ackPing; -#endif -// CCTimeType ackPingSamples[ACK_PING_SAMPLES_SIZE]; // Must be range of unsigned char to wrap ackPingIndex properly - CCTimeType ackPingSum; - unsigned char ackPingIndex; - //CCTimeType nextLowestPingReset; - RemoteSystemTimeType remoteSystemTime; -// bool continuousSend; -// CCTimeType lastTimeBetweenPacketsIncrease,lastTimeBetweenPacketsDecrease; - // Limit changes in throughput to once per ping - otherwise even if lag starts we don't know about it - // In the meantime the connection is flooded and overrun. - CCTimeType nextAllowedThroughputSample; - bool bandwidthExceededStatistic; - - // If Update::maxBitsPerSecond > 0, then throughputCapCountdown is used as a timer to prevent sends for some amount of time after each send, depending on - // the amount of data sent - long long throughputCapCountdown; - - unsigned receivePacketCount; - -#ifdef _DEBUG - struct DataAndTime// - { - RakNetSocket2 *s; - char data[ MAXIMUM_MTU_SIZE ]; - unsigned int length; - RakNet::TimeMS sendTime; - // SystemAddress systemAddress; - unsigned short remotePortRakNetWasStartedOn_PS3; - unsigned int extraSocketOptions; - }; - DataStructures::Queue delayList; - - // Internet simulator - double packetloss; - RakNet::TimeMS minExtraPing, extraPingVariance; -#endif - - CCTimeType elapsedTimeSinceLastUpdate; - - CCTimeType nextAckTimeToSend; - - -#if USE_SLIDING_WINDOW_CONGESTION_CONTROL==1 - RakNet::CCRakNetSlidingWindow congestionManager; -#else - RakNet::CCRakNetUDT congestionManager; -#endif - - - uint32_t unacknowledgedBytes; - - bool ResendBufferOverflow(void) const; - void ValidateResendList(void) const; - void ResetPacketsAndDatagrams(void); - void PushPacket(CCTimeType time, InternalPacket *internalPacket, bool isReliable); - void PushDatagram(void); - bool TagMostRecentPushAsSecondOfPacketPair(void); - void ClearPacketsAndDatagrams(void); - void MoveToListHead(InternalPacket *internalPacket); - void RemoveFromList(InternalPacket *internalPacket, bool modifyUnacknowledgedBytes); - void AddToListTail(InternalPacket *internalPacket, bool modifyUnacknowledgedBytes); - void PopListHead(bool modifyUnacknowledgedBytes); - bool IsResendQueueEmpty(void) const; - void SortSplitPacketList(DataStructures::List &data, unsigned int leftEdge, unsigned int rightEdge) const; - void SendACKs(RakNetSocket2 *s, SystemAddress &systemAddress, CCTimeType time, RakNetRandom *rnr, BitStream &updateBitStream); - - DataStructures::List packetsToSendThisUpdate; - DataStructures::List packetsToDeallocThisUpdate; - // boundary is in packetsToSendThisUpdate, inclusive - DataStructures::List packetsToSendThisUpdateDatagramBoundaries; - DataStructures::List datagramsToSendThisUpdateIsPair; - DataStructures::List datagramSizesInBytes; - BitSize_t datagramSizeSoFar; - BitSize_t allDatagramSizesSoFar; - double totalUserDataBytesAcked; - CCTimeType timeOfLastContinualSend; - CCTimeType timeToNextUnreliableCull; - - // This doesn't need to be a member, but I do it to avoid reallocations - DataStructures::RangeList incomingAcks; - - // Every 16 datagrams, we make sure the 17th datagram goes out the same update tick, and is the same size as the 16th - int countdownToNextPacketPair; - InternalPacket* AllocateFromInternalPacketPool(void); - void ReleaseToInternalPacketPool(InternalPacket *ip); - - DataStructures::RangeList acknowlegements; - DataStructures::RangeList NAKs; - bool remoteSystemNeedsBAndAS; - - unsigned int GetMaxDatagramSizeExcludingMessageHeaderBytes(void); - BitSize_t GetMaxDatagramSizeExcludingMessageHeaderBits(void); - - // ourOffset refers to a section within externallyAllocatedPtr. Do not deallocate externallyAllocatedPtr until all references are lost - void AllocInternalPacketData(InternalPacket *internalPacket, InternalPacketRefCountedData **refCounter, unsigned char *externallyAllocatedPtr, unsigned char *ourOffset); - // Set the data pointer to externallyAllocatedPtr, do not allocate - void AllocInternalPacketData(InternalPacket *internalPacket, unsigned char *externallyAllocatedPtr); - // Allocate new - void AllocInternalPacketData(InternalPacket *internalPacket, unsigned int numBytes, bool allowStack, const char *file, unsigned int line); - void FreeInternalPacketData(InternalPacket *internalPacket, const char *file, unsigned int line); - DataStructures::MemoryPool refCountedDataPool; - - BPSTracker bpsMetrics[RNS_PER_SECOND_METRICS_COUNT]; - CCTimeType lastBpsClear; - -#if LIBCAT_SECURITY==1 -public: - cat::AuthenticatedEncryption* GetAuthenticatedEncryption(void) { return &auth_enc; } - -protected: - cat::AuthenticatedEncryption auth_enc; - bool useSecurity; -#endif // LIBCAT_SECURITY -}; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief \b [Internal] Datagram reliable, ordered, unordered and sequenced sends. Flow control. Message splitting, reassembly, and coalescence. +/// + + +#pragma once + +#include "RakMemoryOverride.h" +#include "MTUSize.h" +#include "DS_LinkedList.h" +#include "DS_List.h" +#include "SocketLayer.h" +#include "PacketPriority.h" +#include "DS_Queue.h" +#include "BitStream.h" +#include "InternalPacket.h" +#include "RakNetStatistics.h" +#include "DR_SHA1.h" +#include "DS_OrderedList.h" +#include "DS_RangeList.h" +#include "DS_BPlusTree.h" +#include "DS_MemoryPool.h" +#include "RakNetDefines.h" +#include "DS_Heap.h" +#include "BitStream.h" +#include "NativeFeatureIncludes.h" +#include "SecureHandshake.h" +#include "PluginInterface2.h" +#include "Rand.h" +#include "RakNetSocket2.h" + +#if USE_SLIDING_WINDOW_CONGESTION_CONTROL!=1 +#include "CCRakNetUDT.h" +#define INCLUDE_TIMESTAMP_WITH_DATAGRAMS 1 +#else +#include "CCRakNetSlidingWindow.h" +#define INCLUDE_TIMESTAMP_WITH_DATAGRAMS 0 +#endif + +/// Number of ordered streams available. You can use up to 32 ordered streams +#define NUMBER_OF_ORDERED_STREAMS 32 // 2^5 + +#define RESEND_TREE_ORDER 32 + +namespace RakNet { + + /// Forward declarations +class PluginInterface2; +class RakNetRandom; +typedef uint64_t reliabilityHeapWeightType; + +// int SplitPacketIndexComp( SplitPacketIndexType const &key, InternalPacket* const &data ); +struct SplitPacketChannel// +{ + CCTimeType lastUpdateTime; + + DataStructures::List splitPacketList; + +#if PREALLOCATE_LARGE_MESSAGES==1 + InternalPacket *returnedPacket; + bool gotFirstPacket; + unsigned int stride; + unsigned int splitPacketsArrived; +#else + // This is here for progress notifications, since progress notifications return the first packet data, if available + InternalPacket *firstPacket; +#endif + +}; +int RAK_DLL_EXPORT SplitPacketChannelComp( SplitPacketIdType const &key, SplitPacketChannel* const &data ); + +// Helper class +struct BPSTracker +{ + BPSTracker(); + ~BPSTracker(); + void Reset(const char *file, unsigned int line); + inline void Push1(CCTimeType time, uint64_t value1) {dataQueue.Push(TimeAndValue2(time,value1),_FILE_AND_LINE_); total1+=value1; lastSec1+=value1;} +// void Push2(RakNet::TimeUS time, uint64_t value1, uint64_t value2); + inline uint64_t GetBPS1(CCTimeType time) {(void) time; return lastSec1;} + inline uint64_t GetBPS1Threadsafe(CCTimeType time) {(void) time; return lastSec1;} +// uint64_t GetBPS2(RakNetTimeUS time); +// void GetBPS1And2(RakNetTimeUS time, uint64_t &out1, uint64_t &out2); + uint64_t GetTotal1(void) const; +// uint64_t GetTotal2(void) const; + + struct TimeAndValue2 + { + TimeAndValue2(); + ~TimeAndValue2(); + TimeAndValue2(CCTimeType t, uint64_t v1); + // TimeAndValue2(RakNet::TimeUS t, uint64_t v1, uint64_t v2); + // uint64_t value1, value2; + uint64_t value1; + CCTimeType time; + }; + + uint64_t total1, lastSec1; +// uint64_t total2, lastSec2; + DataStructures::Queue dataQueue; + void ClearExpired1(CCTimeType time); +// void ClearExpired2(RakNet::TimeUS time); +}; + +/// Datagram reliable, ordered, unordered and sequenced sends. Flow control. Message splitting, reassembly, and coalescence. +class ReliabilityLayer// +{ +public: + + // Constructor + ReliabilityLayer(); + + // Destructor + ~ReliabilityLayer(); + + /// Resets the layer for reuse + void Reset( bool resetVariables, int MTUSize, bool _useSecurity ); + + /// Set the time, in MS, to use before considering ourselves disconnected after not being able to deliver a reliable packet + /// Default time is 10,000 or 10 seconds in release and 30,000 or 30 seconds in debug. + /// \param[in] time Time, in MS + void SetTimeoutTime( RakNet::TimeMS time ); + + /// Returns the value passed to SetTimeoutTime. or the default if it was never called + /// \param[out] the value passed to SetTimeoutTime + RakNet::TimeMS GetTimeoutTime(void); + + /// Packets are read directly from the socket layer and skip the reliability layer because unconnected players do not use the reliability layer + /// This function takes packet data after a player has been confirmed as connected. + /// \param[in] buffer The socket data + /// \param[in] length The length of the socket data + /// \param[in] systemAddress The player that this data is from + /// \param[in] messageHandlerList A list of registered plugins + /// \param[in] MTUSize maximum datagram size + /// \retval true Success + /// \retval false Modified packet + bool HandleSocketReceiveFromConnectedPlayer( + const char *buffer, unsigned int length, SystemAddress &systemAddress, DataStructures::List &messageHandlerList, int MTUSize, + RakNetSocket2 *s, RakNetRandom *rnr, CCTimeType timeRead, BitStream &updateBitStream); + + /// This allocates bytes and writes a user-level message to those bytes. + /// \param[out] data The message + /// \return Returns number of BITS put into the buffer + BitSize_t Receive( unsigned char**data ); + + /// Puts data on the send queue + /// \param[in] data The data to send + /// \param[in] numberOfBitsToSend The length of \a data in bits + /// \param[in] priority The priority level for the send + /// \param[in] reliability The reliability type for the send + /// \param[in] orderingChannel 0 to 31. Specifies what channel to use, for relational ordering and sequencing of packets. + /// \param[in] makeDataCopy If true \a data will be copied. Otherwise, only a pointer will be stored. + /// \param[in] MTUSize maximum datagram size + /// \param[in] currentTime Current time, as per RakNet::GetTimeMS() + /// \param[in] receipt This number will be returned back with ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS and is only returned with the reliability types that contain RECEIPT in the name + /// \return True or false for success or failure. + bool Send( char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, unsigned char orderingChannel, bool makeDataCopy, int MTUSize, CCTimeType currentTime, uint32_t receipt ); + + /// Call once per game cycle. Handles internal lists and actually does the send. + /// \param[in] s the communication end point + /// \param[in] systemAddress The Unique Player Identifier who shouldhave sent some packets + /// \param[in] MTUSize maximum datagram size + /// \param[in] time current system time + /// \param[in] maxBitsPerSecond if non-zero, enforces that outgoing bandwidth does not exceed this amount + /// \param[in] messageHandlerList A list of registered plugins + void Update( RakNetSocket2 *s, SystemAddress &systemAddress, int MTUSize, CCTimeType time, + unsigned bitsPerSecondLimit, + DataStructures::List &messageHandlerList, + RakNetRandom *rnr, BitStream &updateBitStream); + + /// Were you ever unable to deliver a packet despite retries? + /// \return true means the connection has been lost. Otherwise not. + bool IsDeadConnection( void ) const; + + /// Causes IsDeadConnection to return true + void KillConnection(void); + + /// Get Statistics + /// \return A pointer to a static struct, filled out with current statistical information. + RakNetStatistics * GetStatistics( RakNetStatistics *rns ); + + ///Are we waiting for any data to be sent out or be processed by the player? + bool IsOutgoingDataWaiting(void); + bool AreAcksWaiting(void); + + // Set outgoing lag and packet loss properties + void ApplyNetworkSimulator( double _maxSendBPS, RakNet::TimeMS _minExtraPing, RakNet::TimeMS _extraPingVariance ); + + /// Returns if you previously called ApplyNetworkSimulator + /// \return If you previously called ApplyNetworkSimulator + bool IsNetworkSimulatorActive( void ); + + void SetSplitMessageProgressInterval(int interval); + void SetUnreliableTimeout(RakNet::TimeMS timeoutMS); + /// Has a lot of time passed since the last ack + bool AckTimeout(RakNet::Time curTime); + CCTimeType GetNextSendTime(void) const; + CCTimeType GetTimeBetweenPackets(void) const; +#if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1 + CCTimeType GetAckPing(void) const; +#endif + RakNet::TimeMS GetTimeLastDatagramArrived(void) const {return timeLastDatagramArrived;} + + // If true, will update time between packets quickly based on ping calculations + //void SetDoFastThroughputReactions(bool fast); + + // Encoded as numMessages[unsigned int], message1BitLength[unsigned int], message1Data (aligned), ... + //void GetUndeliveredMessages(RakNet::BitStream *messages, int MTUSize); + +private: + /// Send the contents of a bitstream to the socket + /// \param[in] s The socket used for sending data + /// \param[in] systemAddress The address and port to send to + /// \param[in] bitStream The data to send. + void SendBitStream( RakNetSocket2 *s, SystemAddress &systemAddress, RakNet::BitStream *bitStream, RakNetRandom *rnr, CCTimeType currentTime); + + ///Parse an internalPacket and create a bitstream to represent this data + /// \return Returns number of bits used + BitSize_t WriteToBitStreamFromInternalPacket( RakNet::BitStream *bitStream, const InternalPacket *const internalPacket, CCTimeType curTime ); + + + /// Parse a bitstream and create an internal packet to represent this data + InternalPacket* CreateInternalPacketFromBitStream( RakNet::BitStream *bitStream, CCTimeType time ); + + /// Does what the function name says + unsigned RemovePacketFromResendListAndDeleteOlderReliableSequenced( const MessageNumberType messageNumber, CCTimeType time, DataStructures::List &messageHandlerList, const SystemAddress &systemAddress ); + + /// Acknowledge receipt of the packet with the specified messageNumber + void SendAcknowledgementPacket( const DatagramSequenceNumberType messageNumber, CCTimeType time); + + /// This will return true if we should not send at this time + bool IsSendThrottled( int MTUSize ); + + /// We lost a packet + void UpdateWindowFromPacketloss( CCTimeType time ); + + /// Increase the window size + void UpdateWindowFromAck( CCTimeType time ); + + /// Parse an internalPacket and figure out how many header bits would be written. Returns that number + BitSize_t GetMaxMessageHeaderLengthBits( void ); + BitSize_t GetMessageHeaderLengthBits( const InternalPacket *const internalPacket ); + + /// Get the SHA1 code + void GetSHA1( unsigned char * const buffer, unsigned int nbytes, char code[ SHA1_LENGTH ] ); + + /// Check the SHA1 code + bool CheckSHA1( char code[ SHA1_LENGTH ], unsigned char * const buffer, unsigned int nbytes ); + + /// Search the specified list for sequenced packets on the specified ordering channel, optionally skipping those with splitPacketId, and delete them +// void DeleteSequencedPacketsInList( unsigned char orderingChannel, DataStructures::List&theList, int splitPacketId = -1 ); + + /// Search the specified list for sequenced packets with a value less than orderingIndex and delete them +// void DeleteSequencedPacketsInList( unsigned char orderingChannel, DataStructures::Queue&theList ); + + /// Returns true if newPacketOrderingIndex is older than the waitingForPacketOrderingIndex + bool IsOlderOrderedPacket( OrderingIndexType newPacketOrderingIndex, OrderingIndexType waitingForPacketOrderingIndex ); + + /// Split the passed packet into chunks under MTU_SIZE bytes (including headers) and save those new chunks + void SplitPacket( InternalPacket *internalPacket ); + + /// Insert a packet into the split packet list + void InsertIntoSplitPacketList( InternalPacket * internalPacket, CCTimeType time ); + + /// Take all split chunks with the specified splitPacketId and try to reconstruct a packet. If we can, allocate and return it. Otherwise return 0 + InternalPacket * BuildPacketFromSplitPacketList( SplitPacketIdType splitPacketId, CCTimeType time, + RakNetSocket2 *s, SystemAddress &systemAddress, RakNetRandom *rnr, BitStream &updateBitStream); + InternalPacket * BuildPacketFromSplitPacketList( SplitPacketChannel *splitPacketChannel, CCTimeType time ); + + /// Delete any unreliable split packets that have long since expired + //void DeleteOldUnreliableSplitPackets( CCTimeType time ); + + /// Creates a copy of the specified internal packet with data copied from the original starting at dataByteOffset for dataByteLength bytes. + /// Does not copy any split data parameters as that information is always generated does not have any reason to be copied + InternalPacket * CreateInternalPacketCopy( InternalPacket *original, int dataByteOffset, int dataByteLength, CCTimeType time ); + + /// Get the specified ordering list + // DataStructures::LinkedList *GetOrderingListAtOrderingStream( unsigned char orderingChannel ); + + /// Add the internal packet to the ordering list in order based on order index + // void AddToOrderingList( InternalPacket * internalPacket ); + + /// Inserts a packet into the resend list in order + void InsertPacketIntoResendList( InternalPacket *internalPacket, CCTimeType time, bool firstResend, bool modifyUnacknowledgedBytes ); + + /// Memory handling + void FreeMemory( bool freeAllImmediately ); + + /// Memory handling + void FreeThreadSafeMemory( void ); + + // Initialize the variables + void InitializeVariables( void ); + + /// Given the current time, is this time so old that we should consider it a timeout? + bool IsExpiredTime(unsigned int input, CCTimeType currentTime) const; + + // Make it so we don't do resends within a minimum threshold of time + void UpdateNextActionTime(void); + + + /// Does this packet number represent a packet that was skipped (out of order?) + //unsigned int IsReceivedPacketHole(unsigned int input, RakNet::TimeMS currentTime) const; + + /// Skip an element in the received packets list + //unsigned int MakeReceivedPacketHole(unsigned int input) const; + + /// How many elements are waiting to be resent? + unsigned int GetResendListDataSize(void) const; + + /// Update all memory which is not threadsafe + void UpdateThreadedMemory(void); + + void CalculateHistogramAckSize(void); + + // Used ONLY for RELIABLE_ORDERED + // RELIABLE_SEQUENCED just returns the newest one + // DataStructures::List*> orderingList; + DataStructures::Queue outputQueue; + int splitMessageProgressInterval; + CCTimeType unreliableTimeout; + + struct MessageNumberNode + { + DatagramSequenceNumberType messageNumber; + MessageNumberNode *next; + }; + struct DatagramHistoryNode + { + DatagramHistoryNode() {} + DatagramHistoryNode(MessageNumberNode *_head, CCTimeType ts + ) : + head(_head), timeSent(ts) + {} + MessageNumberNode *head; + CCTimeType timeSent; + }; + // Queue length is programmatically restricted to DATAGRAM_MESSAGE_ID_ARRAY_LENGTH + // This is essentially an O(1) lookup to get a DatagramHistoryNode given an index + // datagramHistory holds a linked list of MessageNumberNode. Each MessageNumberNode refers to one element in resendList which can be cleared on an ack. + DataStructures::Queue datagramHistory; + DataStructures::MemoryPool datagramHistoryMessagePool; + + struct UnreliableWithAckReceiptNode + { + UnreliableWithAckReceiptNode() {} + UnreliableWithAckReceiptNode(DatagramSequenceNumberType _datagramNumber, uint32_t _sendReceiptSerial, RakNet::TimeUS _nextActionTime) : + datagramNumber(_datagramNumber), sendReceiptSerial(_sendReceiptSerial), nextActionTime(_nextActionTime) + {} + DatagramSequenceNumberType datagramNumber; + uint32_t sendReceiptSerial; + RakNet::TimeUS nextActionTime; + }; + DataStructures::List unreliableWithAckReceiptHistory; + + void RemoveFromDatagramHistory(DatagramSequenceNumberType index); + MessageNumberNode* GetMessageNumberNodeByDatagramIndex(DatagramSequenceNumberType index, CCTimeType *timeSent); + void AddFirstToDatagramHistory(DatagramSequenceNumberType datagramNumber, CCTimeType timeSent); + MessageNumberNode* AddFirstToDatagramHistory(DatagramSequenceNumberType datagramNumber, DatagramSequenceNumberType messageNumber, CCTimeType timeSent); + MessageNumberNode* AddSubsequentToDatagramHistory(MessageNumberNode *messageNumberNode, DatagramSequenceNumberType messageNumber); + DatagramSequenceNumberType datagramHistoryPopCount; + + DataStructures::MemoryPool internalPacketPool; + // DataStructures::BPlusTree resendTree; + InternalPacket *resendBuffer[RESEND_BUFFER_ARRAY_LENGTH]; + InternalPacket *resendLinkedListHead; + InternalPacket *unreliableLinkedListHead; + void RemoveFromUnreliableLinkedList(InternalPacket *internalPacket); + void AddToUnreliableLinkedList(InternalPacket *internalPacket); +// unsigned int numPacketsOnResendBuffer; + //unsigned int blockWindowIncreaseUntilTime; + // DataStructures::RangeList acknowlegements; + // Resend list is a tree of packets we need to resend + + // Set to the current time when the resend queue is no longer empty + // Set to zero when it becomes empty + // Set to the current time if it is not zero, and we get incoming data + // If the current time - timeResendQueueNonEmpty is greater than a threshold, we are disconnected +// CCTimeType timeResendQueueNonEmpty; + RakNet::TimeMS timeLastDatagramArrived; + + + // If we backoff due to packetloss, don't remeasure until all waiting resends have gone out or else we overcount +// bool packetlossThisSample; +// int backoffThisSample; +// unsigned packetlossThisSampleResendCount; +// CCTimeType lastPacketlossTime; + + //DataStructures::Queue sendPacketSet[ NUMBER_OF_PRIORITIES ]; + DataStructures::Heap outgoingPacketBuffer; + reliabilityHeapWeightType outgoingPacketBufferNextWeights[NUMBER_OF_PRIORITIES]; + void InitHeapWeights(void); + reliabilityHeapWeightType GetNextWeight(int priorityLevel); +// unsigned int messageInSendBuffer[NUMBER_OF_PRIORITIES]; +// double bytesInSendBuffer[NUMBER_OF_PRIORITIES]; + + + DataStructures::OrderedList splitPacketChannelList; + + MessageNumberType sendReliableMessageNumberIndex; + MessageNumberType internalOrderIndex; + //unsigned int windowSize; + //RakNet::BitStream updateBitStream; + bool deadConnection, cheater; + SplitPacketIdType splitPacketId; + RakNet::TimeMS timeoutTime; // How long to wait in MS before timing someone out + //int MAX_AVERAGE_PACKETS_PER_SECOND; // Name says it all +// int RECEIVED_PACKET_LOG_LENGTH, requestedReceivedPacketLogLength; // How big the receivedPackets array is +// unsigned int *receivedPackets; + RakNetStatistics statistics; + + // Algorithm for blending ordered and sequenced on the same channel: + // 1. Each ordered message transmits OrderingIndexType orderedWriteIndex. There are NUMBER_OF_ORDERED_STREAMS independent values of these. The value + // starts at 0. Every time an ordered message is sent, the value increments by 1 + // 2. Each sequenced message contains the current value of orderedWriteIndex for that channel, and additionally OrderingIndexType sequencedWriteIndex. + // sequencedWriteIndex resets to 0 every time orderedWriteIndex increments. It increments by 1 every time a sequenced message is sent. + // 3. The receiver maintains the next expected value for the orderedWriteIndex, stored in orderedReadIndex. + // 4. As messages arrive: + // If a message has the current ordering index, and is sequenced, and is < the current highest sequence value, discard + // If a message has the current ordering index, and is sequenced, and is >= the current highest sequence value, return immediately + // If a message has a greater ordering index, and is sequenced or ordered, buffer it + // If a message has the current ordering index, and is ordered, buffer, then push off messages from buffer + // 5. Pushing off messages from buffer: + // Messages in buffer are put in a minheap. The value of each node is calculated such that messages are returned: + // A. (lowest ordering index, lowest sequence index) + // B. (lowest ordering index, no sequence index) + // Messages are pushed off until the heap is empty, or the next message to be returned does not preserve the ordered index + // For an empty heap, the heap weight should start at the lowest value based on the next expected ordering index, to avoid variable overflow + + // Sender increments this by 1 for every ordered message sent + OrderingIndexType orderedWriteIndex[NUMBER_OF_ORDERED_STREAMS]; + // Sender increments by 1 for every sequenced message sent. Resets to 0 when an ordered message is sent + OrderingIndexType sequencedWriteIndex[NUMBER_OF_ORDERED_STREAMS]; + // Next expected index for ordered messages. + OrderingIndexType orderedReadIndex[NUMBER_OF_ORDERED_STREAMS]; + // Highest value received for sequencedWriteIndex for the current value of orderedReadIndex on the same channel. + OrderingIndexType highestSequencedReadIndex[NUMBER_OF_ORDERED_STREAMS]; + DataStructures::Heap orderingHeaps[NUMBER_OF_ORDERED_STREAMS]; + OrderingIndexType heapIndexOffsets[NUMBER_OF_ORDERED_STREAMS]; + + + + + + + +// CCTimeType histogramStart; +// unsigned histogramBitsSent; + + + /// Memory-efficient receivedPackets algorithm: + /// receivedPacketsBaseIndex is the packet number we are expecting + /// Everything under receivedPacketsBaseIndex is a packet we already got + /// Everything over receivedPacketsBaseIndex is stored in hasReceivedPacketQueue + /// It stores the time to stop waiting for a particular packet number, where the packet number is receivedPacketsBaseIndex + the index into the queue + /// If 0, we got got that packet. Otherwise, the time to give up waiting for that packet. + /// If we get a packet number where (receivedPacketsBaseIndex-packetNumber) is less than half the range of receivedPacketsBaseIndex then it is a duplicate + /// Otherwise, it is a duplicate packet (and ignore it). + // DataStructures::Queue hasReceivedPacketQueue; + DataStructures::Queue hasReceivedPacketQueue; + DatagramSequenceNumberType receivedPacketsBaseIndex; + bool resetReceivedPackets; + + CCTimeType lastUpdateTime; + CCTimeType timeBetweenPackets, nextSendTime; +#if INCLUDE_TIMESTAMP_WITH_DATAGRAMS==1 + CCTimeType ackPing; +#endif +// CCTimeType ackPingSamples[ACK_PING_SAMPLES_SIZE]; // Must be range of unsigned char to wrap ackPingIndex properly + CCTimeType ackPingSum; + unsigned char ackPingIndex; + //CCTimeType nextLowestPingReset; + RemoteSystemTimeType remoteSystemTime; +// bool continuousSend; +// CCTimeType lastTimeBetweenPacketsIncrease,lastTimeBetweenPacketsDecrease; + // Limit changes in throughput to once per ping - otherwise even if lag starts we don't know about it + // In the meantime the connection is flooded and overrun. + CCTimeType nextAllowedThroughputSample; + bool bandwidthExceededStatistic; + + // If Update::maxBitsPerSecond > 0, then throughputCapCountdown is used as a timer to prevent sends for some amount of time after each send, depending on + // the amount of data sent + long long throughputCapCountdown; + + unsigned receivePacketCount; + +#ifdef _DEBUG + struct DataAndTime// + { + RakNetSocket2 *s; + char data[ MAXIMUM_MTU_SIZE ]; + unsigned int length; + RakNet::TimeMS sendTime; + // SystemAddress systemAddress; + unsigned short remotePortRakNetWasStartedOn_PS3; + unsigned int extraSocketOptions; + }; + DataStructures::Queue delayList; + + // Internet simulator + double packetloss; + RakNet::TimeMS minExtraPing, extraPingVariance; +#endif + + CCTimeType elapsedTimeSinceLastUpdate; + + CCTimeType nextAckTimeToSend; + + +#if USE_SLIDING_WINDOW_CONGESTION_CONTROL==1 + RakNet::CCRakNetSlidingWindow congestionManager; +#else + RakNet::CCRakNetUDT congestionManager; +#endif + + + uint32_t unacknowledgedBytes; + + bool ResendBufferOverflow(void) const; + void ValidateResendList(void) const; + void ResetPacketsAndDatagrams(void); + void PushPacket(CCTimeType time, InternalPacket *internalPacket, bool isReliable); + void PushDatagram(void); + bool TagMostRecentPushAsSecondOfPacketPair(void); + void ClearPacketsAndDatagrams(void); + void MoveToListHead(InternalPacket *internalPacket); + void RemoveFromList(InternalPacket *internalPacket, bool modifyUnacknowledgedBytes); + void AddToListTail(InternalPacket *internalPacket, bool modifyUnacknowledgedBytes); + void PopListHead(bool modifyUnacknowledgedBytes); + bool IsResendQueueEmpty(void) const; + void SortSplitPacketList(DataStructures::List &data, unsigned int leftEdge, unsigned int rightEdge) const; + void SendACKs(RakNetSocket2 *s, SystemAddress &systemAddress, CCTimeType time, RakNetRandom *rnr, BitStream &updateBitStream); + + DataStructures::List packetsToSendThisUpdate; + DataStructures::List packetsToDeallocThisUpdate; + // boundary is in packetsToSendThisUpdate, inclusive + DataStructures::List packetsToSendThisUpdateDatagramBoundaries; + DataStructures::List datagramsToSendThisUpdateIsPair; + DataStructures::List datagramSizesInBytes; + BitSize_t datagramSizeSoFar; + BitSize_t allDatagramSizesSoFar; + double totalUserDataBytesAcked; + CCTimeType timeOfLastContinualSend; + CCTimeType timeToNextUnreliableCull; + + // This doesn't need to be a member, but I do it to avoid reallocations + DataStructures::RangeList incomingAcks; + + // Every 16 datagrams, we make sure the 17th datagram goes out the same update tick, and is the same size as the 16th + int countdownToNextPacketPair; + InternalPacket* AllocateFromInternalPacketPool(void); + void ReleaseToInternalPacketPool(InternalPacket *ip); + + DataStructures::RangeList acknowlegements; + DataStructures::RangeList NAKs; + bool remoteSystemNeedsBAndAS; + + unsigned int GetMaxDatagramSizeExcludingMessageHeaderBytes(void); + BitSize_t GetMaxDatagramSizeExcludingMessageHeaderBits(void); + + // ourOffset refers to a section within externallyAllocatedPtr. Do not deallocate externallyAllocatedPtr until all references are lost + void AllocInternalPacketData(InternalPacket *internalPacket, InternalPacketRefCountedData **refCounter, unsigned char *externallyAllocatedPtr, unsigned char *ourOffset); + // Set the data pointer to externallyAllocatedPtr, do not allocate + void AllocInternalPacketData(InternalPacket *internalPacket, unsigned char *externallyAllocatedPtr); + // Allocate new + void AllocInternalPacketData(InternalPacket *internalPacket, unsigned int numBytes, bool allowStack, const char *file, unsigned int line); + void FreeInternalPacketData(InternalPacket *internalPacket, const char *file, unsigned int line); + DataStructures::MemoryPool refCountedDataPool; + + BPSTracker bpsMetrics[RNS_PER_SECOND_METRICS_COUNT]; + CCTimeType lastBpsClear; + +#if LIBCAT_SECURITY==1 +public: + cat::AuthenticatedEncryption* GetAuthenticatedEncryption(void) { return &auth_enc; } + +protected: + cat::AuthenticatedEncryption auth_enc; + bool useSecurity; +#endif // LIBCAT_SECURITY +}; + +} // namespace RakNet + diff --git a/Source/ReplicaEnums.h b/Source/ReplicaEnums.h index 6238fdf98..9d6652f26 100644 --- a/Source/ReplicaEnums.h +++ b/Source/ReplicaEnums.h @@ -1,51 +1,49 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Contains enumerations used by the ReplicaManager system. This file is a lightweight header, so you can include it without worrying about linking in lots of other crap -/// - - - -#ifndef __REPLICA_ENUMS_H -#define __REPLICA_ENUMS_H - -/// Replica interface flags, used to enable and disable function calls on the Replica object -/// Passed to ReplicaManager::EnableReplicaInterfaces and ReplicaManager::DisableReplicaInterfaces -enum -{ - REPLICA_RECEIVE_DESTRUCTION=1<<0, - REPLICA_RECEIVE_SERIALIZE=1<<1, - REPLICA_RECEIVE_SCOPE_CHANGE=1<<2, - REPLICA_SEND_CONSTRUCTION=1<<3, - REPLICA_SEND_DESTRUCTION=1<<4, - REPLICA_SEND_SCOPE_CHANGE=1<<5, - REPLICA_SEND_SERIALIZE=1<<6, - REPLICA_SET_ALL = 0xFF // Allow all of the above -}; - -enum ReplicaReturnResult -{ - /// This means call the function again later, with the same parameters - REPLICA_PROCESS_LATER, - /// This means we are done processing (the normal result to return) - REPLICA_PROCESSING_DONE, - /// This means cancel the processing - don't send any network messages and don't change the current state. - REPLICA_CANCEL_PROCESS, - /// Same as REPLICA_PROCESSING_DONE, where a message is sent, but does not clear the send bit. - /// Useful for multi-part sends with different reliability levels. - /// Only currently used by Replica::Serialize - REPLICA_PROCESS_AGAIN, - /// Only returned from the Replica::SendConstruction interface, means act as if the other system had this object but don't actually - /// Send a construction packet. This way you will still send scope and serialize packets to that system - REPLICA_PROCESS_IMPLICIT -}; - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Contains enumerations used by the ReplicaManager system. This file is a lightweight header, so you can include it without worrying about linking in lots of other crap +/// + + + +#pragma once + +/// Replica interface flags, used to enable and disable function calls on the Replica object +/// Passed to ReplicaManager::EnableReplicaInterfaces and ReplicaManager::DisableReplicaInterfaces +enum +{ + REPLICA_RECEIVE_DESTRUCTION=1<<0, + REPLICA_RECEIVE_SERIALIZE=1<<1, + REPLICA_RECEIVE_SCOPE_CHANGE=1<<2, + REPLICA_SEND_CONSTRUCTION=1<<3, + REPLICA_SEND_DESTRUCTION=1<<4, + REPLICA_SEND_SCOPE_CHANGE=1<<5, + REPLICA_SEND_SERIALIZE=1<<6, + REPLICA_SET_ALL = 0xFF // Allow all of the above +}; + +enum ReplicaReturnResult +{ + /// This means call the function again later, with the same parameters + REPLICA_PROCESS_LATER, + /// This means we are done processing (the normal result to return) + REPLICA_PROCESSING_DONE, + /// This means cancel the processing - don't send any network messages and don't change the current state. + REPLICA_CANCEL_PROCESS, + /// Same as REPLICA_PROCESSING_DONE, where a message is sent, but does not clear the send bit. + /// Useful for multi-part sends with different reliability levels. + /// Only currently used by Replica::Serialize + REPLICA_PROCESS_AGAIN, + /// Only returned from the Replica::SendConstruction interface, means act as if the other system had this object but don't actually + /// Send a construction packet. This way you will still send scope and serialize packets to that system + REPLICA_PROCESS_IMPLICIT +}; + diff --git a/Source/ReplicaManager3.cpp b/Source/ReplicaManager3.cpp index ba79c498a..c0c7e6e71 100644 --- a/Source/ReplicaManager3.cpp +++ b/Source/ReplicaManager3.cpp @@ -138,7 +138,7 @@ void ReplicaManager3::AutoCreateConnectionList( { for (unsigned int index=0; index < participantListIn.Size(); index++) { - if (GetConnectionByGUID(participantListIn[index], worldId)==false) + if (GetConnectionByGUID(participantListIn[index], worldId)==nullptr) { Connection_RM3 *connection = AllocConnection(rakPeerInterface->GetSystemAddressFromGuid(participantListIn[index]), participantListIn[index]); if (connection) diff --git a/Source/ReplicaManager3.h b/Source/ReplicaManager3.h index 788c2f052..059d19deb 100644 --- a/Source/ReplicaManager3.h +++ b/Source/ReplicaManager3.h @@ -1,1137 +1,1135 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Contains the third iteration of the ReplicaManager class. -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_ReplicaManager3==1 - -#ifndef __REPLICA_MANAGER_3 -#define __REPLICA_MANAGER_3 - -#include "RakNetTypes.h" -#include "RakNetTime.h" -#include "BitStream.h" -#include "PacketPriority.h" -#include "PluginInterface2.h" -#include "NetworkIDObject.h" -#include "DS_OrderedList.h" -#include "DS_Queue.h" - -/// \defgroup REPLICA_MANAGER_GROUP3 ReplicaManager3 -/// \brief Third implementation of object replication -/// \details -/// \ingroup PLUGINS_GROUP - -namespace RakNet -{ -class Connection_RM3; -class Replica3; - -/// \ingroup REPLICA_MANAGER_GROUP3 -/// Used for multiple worlds. World 0 is created automatically by default -typedef uint8_t WorldId; - - -/// \internal -/// \ingroup REPLICA_MANAGER_GROUP3 -struct PRO -{ - /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultPacketPriority(). - PacketPriority priority; - - /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultPacketReliability(). - PacketReliability reliability; - - /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultOrderingChannel(). - char orderingChannel; - - /// Passed to RakPeerInterface::Send(). Defaults to 0. - uint32_t sendReceipt; - - bool operator==( const PRO& right ) const; - bool operator!=( const PRO& right ) const; -}; - - -/// \brief System to help automate game object construction, destruction, and serialization -/// \details ReplicaManager3 tracks your game objects and automates the networking for replicating them across the network
-/// As objects are created, destroyed, or serialized differently, those changes are pushed out to other systems.
-/// To use:
-///
    -///
  1. Derive from Connection_RM3 and implement Connection_RM3::AllocReplica(). This is a factory function where given a user-supplied identifier for a class (such as name) return an instance of that class. Should be able to return any networked object in your game. -///
  2. Derive from ReplicaManager3 and implement AllocConnection() and DeallocConnection() to return the class you created in step 1. -///
  3. Derive your networked game objects from Replica3. All pure virtuals have to be implemented, however defaults are provided for Replica3::QueryConstruction(), Replica3::QueryRemoteConstruction(), and Replica3::QuerySerialization() depending on your network architecture. -///
  4. When a new game object is created on the local system, pass it to ReplicaManager3::Reference(). -///
  5. When a game object is destroyed on the local system, and you want other systems to know about it, call Replica3::BroadcastDestruction() -///
-///
-/// At this point, all new connections will automatically download, get construction messages, get destruction messages, and update serialization automatically. -/// \ingroup REPLICA_MANAGER_GROUP3 -class RAK_DLL_EXPORT ReplicaManager3 : public PluginInterface2 -{ -public: - ReplicaManager3(); - virtual ~ReplicaManager3(); - - /// \brief Implement to return a game specific derivation of Connection_RM3 - /// \details The connection object represents a remote system connected to you that is using the ReplicaManager3 system.
- /// It has functions to perform operations per-connection.
- /// AllocConnection() and DeallocConnection() are factory functions to create and destroy instances of the connection object.
- /// It is used if autoCreate is true via SetAutoManageConnections() (true by default). Otherwise, the function is not called, and you will have to call PushConnection() manually
- /// \note If you do not want a new network connection to immediately download game objects, SetAutoManageConnections() and PushConnection() are how you do this. - /// \sa SetAutoManageConnections() - /// \param[in] systemAddress Address of the system you are adding - /// \param[in] rakNetGUID GUID of the system you are adding. See Packet::rakNetGUID or RakPeerInterface::GetGUIDFromSystemAddress() - /// \return The new connection instance. - virtual Connection_RM3* AllocConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID) const=0; - - /// \brief Implement to destroy a class instanced returned by AllocConnection() - /// \details Most likely just implement as {delete connection;}
- /// It is used if autoDestroy is true via SetAutoManageConnections() (true by default). Otherwise, the function is not called and you would then be responsible for deleting your own connection objects. - /// \param[in] connection The pointer instance to delete - virtual void DeallocConnection(Connection_RM3 *connection) const=0; - - /// \brief Enable or disable automatically assigning connections to new instances of Connection_RM3 - /// \details ReplicaManager3 can automatically create and/or destroy Connection_RM3 as systems connect or disconnect from RakPeerInterface.
- /// By default this is on, to make the system easier to learn and setup.
- /// If you don't want all connections to take part in the game, or you want to delay when a connection downloads the game, set \a autoCreate to false.
- /// If you want to delay deleting a connection that has dropped, set \a autoDestroy to false. If you do this, then you must call PopConnection() to remove that connection from being internally tracked. You'll also have to delete the connection instance on your own.
- /// \param[in] autoCreate Automatically call ReplicaManager3::AllocConnection() for each new connection. Defaults to true. Also see AutoCreateConnectionList() - /// \param[in] autoDestroy Automatically call ReplicaManager3::DeallocConnection() for each dropped connection. Defaults to true. - void SetAutoManageConnections(bool autoCreate, bool autoDestroy); - - /// \return What was passed to the autoCreate parameter of SetAutoManageConnections() - bool GetAutoCreateConnections(void) const; - - /// \return What was passed to the autoDestroy parameter of SetAutoManageConnections() - bool GetAutoDestroyConnections(void) const; - - /// \brief Call AllocConnection() and PushConnection() for each connection in \a participantList - /// \param[in] participantListIn The list of connections to allocate - /// \param[in] participantListOut The connections allocated, if any - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - void AutoCreateConnectionList( - DataStructures::List &participantListIn, - DataStructures::List &participantListOut, - WorldId worldId=0); - - /// \brief Track a new Connection_RM3 instance - /// \details If \a autoCreate is false for SetAutoManageConnections(), then you need this function to add new instances of Connection_RM3 yourself.
- /// You don't need to track this pointer yourself, you can get it with GetConnectionAtIndex(), GetConnectionByGUID(), or GetConnectionBySystemAddress().
- /// \param[in] newConnection The new connection instance to track. - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - bool PushConnection(RakNet::Connection_RM3 *newConnection, WorldId worldId=0); - - /// \brief Stop tracking a connection - /// \details On call, for each replica returned by GetReplicasCreatedByGuid(), QueryActionOnPopConnection() will be called. Depending on the return value, this may delete the corresponding replica.
- /// If autoDestroy is true in the call to SetAutoManageConnections() (true by default) then this is called automatically when the connection is lost. In that case, the returned connection instance is deleted.
- /// \param[in] guid of the connection to get. Passed to ReplicaManager3::AllocConnection() originally. - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - RakNet::Connection_RM3 * PopConnection(RakNetGUID guid, WorldId worldId=0); - - /// \brief Adds a replicated object to the system. - /// \details Anytime you create a new object that derives from Replica3, and you want ReplicaManager3 to use it, pass it to Reference().
- /// Remote systems already connected will potentially download this object the next time ReplicaManager3::Update() is called, which happens every time you call RakPeerInterface::Receive().
- /// You can also call ReplicaManager3::Update() manually to send referenced objects right away - /// \param[in] replica3 The object to start tracking - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - void Reference(RakNet::Replica3 *replica3, WorldId worldId=0); - - /// \brief Removes a replicated object from the system. - /// \details The object is not deallocated, it is up to the caller to do so.
- /// This is called automatically from the destructor of Replica3, so you don't need to call it manually unless you want to stop tracking an object before it is destroyed. - /// \param[in] replica3 The object to stop tracking - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - void Dereference(RakNet::Replica3 *replica3, WorldId worldId=0); - - /// \brief Removes multiple replicated objects from the system. - /// \details Same as Dereference(), but for a list of objects.
- /// Useful with the lists returned by GetReplicasCreatedByGuid(), GetReplicasCreatedByMe(), or GetReferencedReplicaList().
- /// \param[in] replicaListIn List of objects - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - void DereferenceList(DataStructures::List &replicaListIn, WorldId worldId=0); - - /// \brief Returns all objects originally created by a particular system - /// \details Originally created is defined as the value of Replica3::creatingSystemGUID, which is automatically assigned in ReplicaManager3::Reference().
- /// You do not have to be directly connected to that system to get the objects originally created by that system.
- /// \param[in] guid GUID of the system we are referring to. Originally passed as the \a guid parameter to ReplicaManager3::AllocConnection() - /// \param[out] List of Replica3 instances to be returned - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - void GetReplicasCreatedByGuid(RakNetGUID guid, DataStructures::List &replicaListOut, WorldId worldId=0); - - /// \brief Returns all objects originally created by your system - /// \details Calls GetReplicasCreatedByGuid() for your own system guid. - /// \param[out] List of Replica3 instances to be returned - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - void GetReplicasCreatedByMe(DataStructures::List &replicaListOut, WorldId worldId=0); - - /// \brief Returns the entire list of Replicas that we know about. - /// \details This is all Replica3 instances passed to Reference, as well as instances we downloaded and created via Connection_RM3::AllocReference() - /// \param[out] List of Replica3 instances to be returned - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - void GetReferencedReplicaList(DataStructures::List &replicaListOut, WorldId worldId=0); - - /// \brief Returns the number of replicas known about - /// \details Returns the size of the list that would be returned by GetReferencedReplicaList() - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - /// \return How many replica objects are in the list of replica objects - unsigned GetReplicaCount(WorldId worldId=0) const; - - /// \brief Returns a replica by index - /// \details Returns one of the items in the list that would be returned by GetReferencedReplicaList() - /// \param[in] index An index, from 0 to GetReplicaCount()-1. - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - /// \return A Replica3 instance - Replica3 *GetReplicaAtIndex(unsigned index, WorldId worldId=0); - - /// \brief Returns the number of connections - /// \details Returns the number of connections added with ReplicaManager3::PushConnection(), minus the number removed with ReplicaManager3::PopConnection() - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - /// \return The number of registered connections - unsigned int GetConnectionCount(WorldId worldId=0) const; - - /// \brief Returns a connection pointer previously added with PushConnection() - /// \param[in] index An index, from 0 to GetConnectionCount()-1. - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - /// \return A Connection_RM3 pointer - Connection_RM3* GetConnectionAtIndex(unsigned index, WorldId worldId=0) const; - - /// \brief Returns a connection pointer previously added with PushConnection() - /// \param[in] sa The system address of the connection to return - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - /// \return A Connection_RM3 pointer, or 0 if not found - Connection_RM3* GetConnectionBySystemAddress(const SystemAddress &sa, WorldId worldId=0) const; - - /// \brief Returns a connection pointer previously added with PushConnection.() - /// \param[in] guid The guid of the connection to return - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - /// \return A Connection_RM3 pointer, or 0 if not found - Connection_RM3* GetConnectionByGUID(RakNetGUID guid, WorldId worldId=0) const; - - /// \param[in] Default ordering channel to use for object creation, destruction, and serializations - void SetDefaultOrderingChannel(char def); - - /// \param[in] Default packet priority to use for object creation, destruction, and serializations - void SetDefaultPacketPriority(PacketPriority def); - - /// \param[in] Default packet reliability to use for object creation, destruction, and serializations - void SetDefaultPacketReliability(PacketReliability def); - - /// \details Every \a intervalMS milliseconds, Connection_RM3::OnAutoserializeInterval() will be called.
- /// Defaults to 30.
- /// Pass with <0 to disable. Pass 0 to Serialize() every time RakPeer::Recieve() is called
- /// If you want to control the update interval with more granularity, use the return values from Replica3::Serialize().
- /// \param[in] intervalMS How frequently to autoserialize all objects. This controls the maximum number of game object updates per second. - void SetAutoSerializeInterval(RakNet::Time intervalMS); - - /// \brief Return the connections that we think have an instance of the specified Replica3 instance - /// \details This can be wrong, for example if that system locally deleted the outside the scope of ReplicaManager3, if QueryRemoteConstruction() returned false, or if DeserializeConstruction() returned false. - /// \param[in] replica The replica to check against. - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - /// \param[out] connectionsThatHaveConstructedThisReplica Populated with connection instances that we believe have \a replica allocated - void GetConnectionsThatHaveReplicaConstructed(Replica3 *replica, DataStructures::List &connectionsThatHaveConstructedThisReplica, WorldId worldId=0); - - /// \brief Returns if GetDownloadWasCompleted() returns true for all connections - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - /// \return True when all downloads have been completed - bool GetAllConnectionDownloadsCompleted(WorldId worldId=0) const; - - /// \brief ReplicaManager3 can support multiple worlds, where each world has a separate NetworkIDManager, list of connections, replicas, etc - /// A world with id 0 is created automatically. If you want multiple worlds, use this function, and ReplicaManager3::SetNetworkIDManager() to have a different NetworkIDManager instance per world - /// \param[in] worldId A unique identifier for this world. User-defined - void AddWorld(WorldId worldId); - - /// \brief Deallocate a world added with AddWorld, or the default world with id 0 - /// Deallocating a world will also stop tracking and updating all connections and replicas associated with that world. - /// \param[in] worldId A \a worldId value previously added with AddWorld() - void RemoveWorld(WorldId worldId); - - /// \brief Get one of the WorldId values added with AddWorld() - /// \details WorldId 0 is created by default. Worlds will not necessarily be in the order added with AddWorld(). Edit RemoveWorld() changing RemoveAtIndexFast() to RemoveAtIndex() to preserve order. - /// \param[in] index A value between 0 and GetWorldCount()-1 - /// \return One of the WorldId values added with AddWorld() - WorldId GetWorldIdAtIndex(unsigned int index); - - /// \brief Returns the number of world id specifiers in memory, added with AddWorld() and removed with RemoveWorld() - /// \return The number of worlds added - unsigned int GetWorldCount(void) const; - - /// \details Sets the networkIDManager instance that this plugin relys upon.
- /// Uses whatever instance is attached to RakPeerInterface if unset.
- /// To support multiple worlds, you should set it to a different manager for each instance of the plugin - /// \param[in] _networkIDManager The externally allocated NetworkIDManager instance for this plugin to use. - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - void SetNetworkIDManager(NetworkIDManager *_networkIDManager, WorldId worldId=0); - - /// Returns what was passed to SetNetworkIDManager(), or the instance on RakPeerInterface if unset. - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - NetworkIDManager *GetNetworkIDManager(WorldId worldId=0) const; - - /// \details Send a network command to destroy one or more Replica3 instances - /// Usually you won't need this, but use Replica3::BroadcastDestruction() instead. - /// The objects are unaffected locally - /// \param[in] replicaList List of Replica3 objects to tell other systems to destroy. - /// \param[in] exclusionAddress Which system to not send to. UNASSIGNED_SYSTEM_ADDRESS to send to all. - /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() - void BroadcastDestructionList(DataStructures::List &replicaListSource, const SystemAddress &exclusionAddress, WorldId worldId=0); - - /// \internal - /// \details Tell other systems that have this replica to destroy this replica.
- /// You shouldn't need to call this, as it happens in the Replica3 destructor - void BroadcastDestruction(Replica3 *replica, const SystemAddress &exclusionAddress); - - /// \internal - /// \details Frees internal lists.
- /// \param[in] deleteWorlds True to also delete the worlds added with AddWorld() - /// Externally allocated pointers are not deallocated - void Clear(bool deleteWorlds=false); - - /// \internal - PRO GetDefaultSendParameters(void) const; - - /// Call interfaces, send data - virtual void Update(void); - - /// \internal - struct RM3World - { - RM3World(); - void Clear(ReplicaManager3 *replicaManager3); - - DataStructures::List connectionList; - DataStructures::List userReplicaList; - WorldId worldId; - NetworkIDManager *networkIDManager; - }; -protected: - virtual PluginReceiveResult OnReceive(Packet *packet); - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); - virtual void OnRakPeerShutdown(void); - virtual void OnDetach(void); - - PluginReceiveResult OnConstruction(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset, WorldId worldId); - PluginReceiveResult OnSerialize(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, RakNet::Time timestamp, unsigned char packetDataOffset, WorldId worldId); - PluginReceiveResult OnDownloadStarted(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset, WorldId worldId); - PluginReceiveResult OnDownloadComplete(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset, WorldId worldId); - - void DeallocReplicaNoBroadcastDestruction(RakNet::Connection_RM3 *connection, RakNet::Replica3 *replica3); - RakNet::Connection_RM3 * PopConnection(unsigned int index, WorldId worldId); - Replica3* GetReplicaByNetworkID(NetworkID networkId, WorldId worldId); - unsigned int ReferenceInternal(RakNet::Replica3 *replica3, WorldId worldId); - - PRO defaultSendParameters; - RakNet::Time autoSerializeInterval; - RakNet::Time lastAutoSerializeOccurance; - bool autoCreateConnections, autoDestroyConnections; - Replica3 *currentlyDeallocatingReplica; - // Set on the first call to ReferenceInternal(), and should never be changed after that - // Used to lookup in Replica3LSRComp. I don't want to rely on GetNetworkID() in case it changes at runtime - uint32_t nextReferenceIndex; - - // For O(1) lookup - RM3World *worldsArray[255]; - // For fast traversal - DataStructures::List worldsList; - - friend class Connection_RM3; -}; - -static const int RM3_NUM_OUTPUT_BITSTREAM_CHANNELS=16; - -/// \ingroup REPLICA_MANAGER_GROUP3 -struct LastSerializationResultBS -{ - RakNet::BitStream bitStream[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS]; - bool indicesToSend[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS]; -}; - -/// Represents the serialized data for an object the last time it was sent. Used by Connection_RM3::OnAutoserializeInterval() and Connection_RM3::SendSerializeIfChanged() -/// \ingroup REPLICA_MANAGER_GROUP3 -struct LastSerializationResult -{ - LastSerializationResult(); - ~LastSerializationResult(); - - /// The replica instance we serialized - /// \note replica MUST be the first member of this struct because I cast from replica to LastSerializationResult in Update() - RakNet::Replica3 *replica; - //bool neverSerialize; -// bool isConstructed; - RakNet::Time whenLastSerialized; - - void AllocBS(void); - LastSerializationResultBS* lastSerializationResultBS; -}; - -/// Parameters passed to Replica3::Serialize() -/// \ingroup REPLICA_MANAGER_GROUP3 -struct SerializeParameters -{ - /// Write your output for serialization here - /// If nothing is written, the serialization will not occur - /// Write to any or all of the NUM_OUTPUT_BITSTREAM_CHANNELS channels available. Channels can hold independent data - RakNet::BitStream outputBitstream[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS]; - - /// Last bitstream we sent for this replica to this system. - /// Read, but DO NOT MODIFY - RakNet::BitStream* lastSentBitstream[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS]; - - /// Set to non-zero to transmit a timestamp with this message. - /// Defaults to 0 - /// Use RakNet::GetTime() for this - RakNet::Time messageTimestamp; - - /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultPacketPriority(). - /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultPacketReliability(). - /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultOrderingChannel(). - PRO pro[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS]; - - /// Passed to RakPeerInterface::Send(). - RakNet::Connection_RM3 *destinationConnection; - - /// For prior serializations this tick, for the same connection, how many bits have we written so far? - /// Use this to limit how many objects you send to update per-tick if desired - BitSize_t bitsWrittenSoFar; - - /// When this object was last serialized to the connection - /// 0 means never - RakNet::Time whenLastSerialized; - - /// Current time, in milliseconds. - /// curTime - whenLastSerialized is how long it has been since this object was last sent - RakNet::Time curTime; -}; - -/// \ingroup REPLICA_MANAGER_GROUP3 -struct DeserializeParameters -{ - RakNet::BitStream serializationBitstream[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS]; - bool bitstreamWrittenTo[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS]; - RakNet::Time timeStamp; - RakNet::Connection_RM3 *sourceConnection; -}; - -/// \ingroup REPLICA_MANAGER_GROUP3 -enum SendSerializeIfChangedResult -{ - SSICR_SENT_DATA, - SSICR_DID_NOT_SEND_DATA, - SSICR_NEVER_SERIALIZE, -}; - -/// \brief Each remote system is represented by Connection_RM3. Used to allocate Replica3 and track which instances have been allocated -/// \details Important function: AllocReplica() - must be overridden to create an object given an identifier for that object, which you define for all objects in your game -/// \ingroup REPLICA_MANAGER_GROUP3 -class RAK_DLL_EXPORT Connection_RM3 -{ -public: - - Connection_RM3(const SystemAddress &_systemAddress, RakNetGUID _guid); - virtual ~Connection_RM3(); - - /// \brief Class factory to create a Replica3 instance, given a user-defined identifier - /// \details Identifier is returned by Replica3::WriteAllocationID() for what type of class to create.
- /// This is called when you download a replica from another system.
- /// See Replica3::Dealloc for the corresponding destruction message.
- /// Return 0 if unable to create the intended object. Note, in that case the other system will still think we have the object and will try to serialize object updates to us. Generally, you should not send objects the other system cannot create.
- /// \sa Replica3::WriteAllocationID(). - /// Sample implementation:
- /// {RakNet::RakString typeName; allocationIdBitstream->Read(typeName); if (typeName=="Soldier") return new Soldier; return 0;}
- /// \param[in] allocationIdBitstream user-defined bitstream uniquely identifying a game object type - /// \param[in] replicaManager3 Instance of ReplicaManager3 that controls this connection - /// \return The new replica instance - virtual Replica3 *AllocReplica(RakNet::BitStream *allocationIdBitstream, ReplicaManager3 *replicaManager3)=0; - - /// \brief Get list of all replicas that are constructed for this connection - /// \param[out] objectsTheyDoHave Destination list. Returned in sorted ascending order, sorted on the value of the Replica3 pointer. - virtual void GetConstructedReplicas(DataStructures::List &objectsTheyDoHave); - - /// Returns true if we think this remote connection has this replica constructed - /// \param[in] replica3 Which replica we are querying - /// \return True if constructed, false othewise - bool HasReplicaConstructed(RakNet::Replica3 *replica); - - /// When a new connection connects, before sending any objects, SerializeOnDownloadStarted() is called - /// \param[out] bitStream Passed to DeserializeOnDownloadStarted() - virtual void SerializeOnDownloadStarted(RakNet::BitStream *bitStream) {(void) bitStream;} - - /// Receives whatever was written in SerializeOnDownloadStarted() - /// \param[in] bitStream Written in SerializeOnDownloadStarted() - virtual void DeserializeOnDownloadStarted(RakNet::BitStream *bitStream) {(void) bitStream;} - - /// When a new connection connects, after constructing and serialization all objects, SerializeOnDownloadComplete() is called - /// \param[out] bitStream Passed to DeserializeOnDownloadComplete() - virtual void SerializeOnDownloadComplete(RakNet::BitStream *bitStream) {(void) bitStream;} - - /// Receives whatever was written in DeserializeOnDownloadComplete() - /// \param[in] bitStream Written in SerializeOnDownloadComplete() - virtual void DeserializeOnDownloadComplete(RakNet::BitStream *bitStream) {(void) bitStream;} - - /// \return The system address passed to the constructor of this object - SystemAddress GetSystemAddress(void) const {return systemAddress;} - - /// \return Returns the RakNetGUID passed to the constructor of this object - RakNetGUID GetRakNetGUID(void) const {return guid;} - - /// \return True if ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE arrived for this connection - bool GetDownloadWasCompleted(void) const {return gotDownloadComplete;} - - /// List of enumerations for how to get the list of valid objects for other systems - enum ConstructionMode - { - /// For every object that does not exist on the remote system, call Replica3::QueryConstruction() every tick. - /// Do not call Replica3::QueryDestruction() - /// Do not call Connection_RM3::QueryReplicaList() - QUERY_REPLICA_FOR_CONSTRUCTION, - - /// For every object that does not exist on the remote system, call Replica3::QueryConstruction() every tick. Based on the call, the object may be sent to the other system. - /// For every object that does exist on the remote system, call Replica3::QueryDestruction() every tick. Based on the call, the object may be deleted on the other system. - /// Do not call Connection_RM3::QueryReplicaList() - QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION, - - /// Do not call Replica3::QueryConstruction() or Replica3::QueryDestruction() - /// Call Connection_RM3::QueryReplicaList() to determine which objects exist on remote systems - /// This can be faster than QUERY_REPLICA_FOR_CONSTRUCTION and QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION for large worlds - /// See GridSectorizer.h under /Source for code that can help with this - QUERY_CONNECTION_FOR_REPLICA_LIST - }; - - /// \brief Return whether or not downloads to our system should all be processed the same tick (call to RakPeer::Receive() ) - /// \details Normally the system will send ID_REPLICA_MANAGER_DOWNLOAD_STARTED, ID_REPLICA_MANAGER_CONSTRUCTION for all downloaded objects, - /// ID_REPLICA_MANAGER_SERIALIZE for each downloaded object, and lastly ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE. - /// This enables the application to show a downloading splash screen on ID_REPLICA_MANAGER_DOWNLOAD_STARTED, a progress bar, and to close the splash screen and activate all objects on ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE - /// However, if the application was not set up for this then it would result in incomplete objects spread out over time, and cause problems - /// If you return true from QueryGroupDownloadMessages(), then these messages will be returned all in one tick, returned only when the download is complete - /// \note ID_REPLICA_MANAGER_DOWNLOAD_STARTED calls the callback DeserializeOnDownloadStarted() - /// \note ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE calls the callback DeserializeOnDownloadComplete() - virtual bool QueryGroupDownloadMessages(void) const {return false;} - - /// \brief Queries how to get the list of objects that exist on remote systems - /// \details The default of calling QueryConstruction for every known object is easy to use, but not efficient, especially for large worlds where many objects are outside of the player's circle of influence.
- /// QueryDestruction is also not necessarily useful or efficient, as object destruction tends to happen in known cases, and can be accomplished by calling Replica3::BroadcastDestruction() - /// QueryConstructionMode() allows you to specify more efficient algorithms than the default when overriden. - /// \return How to get the list of objects that exist on the remote system. You should always return the same value for a given connection - virtual ConstructionMode QueryConstructionMode(void) const {return QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION;} - - /// \brief Callback used when QueryConstructionMode() returns QUERY_CONNECTION_FOR_REPLICA_LIST - /// \details This advantage of this callback is if that there are many objects that a particular connection does not have, then we do not have to iterate through those - /// objects calling QueryConstruction() for each of them.
- ///
- /// See GridSectorizer in the Source directory as a method to find all objects within a certain radius in a fast way.
- ///
- /// \param[out] newReplicasToCreate Anything in this list will be created on the remote system - /// \param[out] existingReplicasToDestroy Anything in this list will be destroyed on the remote system - virtual void QueryReplicaList( - DataStructures::List &newReplicasToCreate, - DataStructures::List &existingReplicasToDestroy) {(void) newReplicasToCreate; (void) existingReplicasToDestroy;} - - /// \brief Override which replicas to serialize and in what order for a connection for a ReplicaManager3::Update() cycle - /// \details By default, Connection_RM3 will iterate through queryToSerializeReplicaList and call QuerySerialization() on each Replica in that list - /// queryToSerializeReplicaList is populated in the order in which ReplicaManager3::Reference() is called for those objects. - /// If you write to to \a replicasToSerialize and return true, you can control in what order and for which replicas to call QuerySerialization() - /// Example use case: - /// We have more data to send then the bandwidth supports, so want to prioritize sends. For example enemies shooting are more important than animation effects - /// When QuerySerializationList(), sort objects by priority, and write the list to \a replicasToSerialize, optionally skipping objects with a lower serialization frequency - /// If you hit your bandwidth limit when checking SerializeParameters::bitsWrittenSoFar, you can return RM3SR_DO_NOT_SERIALIZE for all remaining items - /// \note Only replicas written to replicasToSerialize are transmitted. Even if you returned RM3SR_SERIALIZED_ALWAYS a prior ReplicaManager3::Update() cycle, the replica will not be transmitted if it is not in replicasToSerialize - /// \note If you do not know what objects are candidates for serialization, you can use queryToSerializeReplicaList as a source for your filtering or sorting operations - /// \param[in] replicasToSerialize List of replicas to call QuerySerialization() on - /// \return Return true to use replicasToSerialize (replicasToSerialize may be empty if desired). Otherwise return false. - virtual bool QuerySerializationList(DataStructures::List &replicasToSerialize) {(void) replicasToSerialize; return false;} - - /// \internal This is used internally - however, you can also call it manually to send a data update for a remote replica.
- /// \brief Sends over a serialization update for \a replica.
- /// NetworkID::GetNetworkID() is written automatically, serializationData is the object data.
- /// \param[in] replica Which replica to serialize - /// \param[in] serializationData Serialized object data - /// \param[in] timestamp 0 means no timestamp. Otherwise message is prepended with ID_TIMESTAMP - /// \param[in] sendParameters Parameters on how to send - /// \param[in] rakPeer Instance of RakPeerInterface to send on - /// \param[in] worldId Which world, see ReplicaManager3::AddWorld() - /// \param[in] curTime The current time - virtual SendSerializeIfChangedResult SendSerialize(RakNet::Replica3 *replica, bool indicesToSend[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::BitStream serializationData[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::Time timestamp, PRO sendParameters[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::RakPeerInterface *rakPeer, unsigned char worldId, RakNet::Time curTime); - - /// \internal - /// \details Calls Connection_RM3::SendSerialize() if Replica3::Serialize() returns a different result than what is contained in \a lastSerializationResult.
- /// Used by autoserialization in Connection_RM3::OnAutoserializeInterval() - /// \param[in] lsr Item in the queryToSerializeReplicaList - /// \param[in] sp Controlling parameters over the serialization - /// \param[in] rakPeer Instance of RakPeerInterface to send on - /// \param[in] worldId Which world, see ReplicaManager3::AddWorld() - /// \param[in] curTime The current time - virtual SendSerializeIfChangedResult SendSerializeIfChanged(LastSerializationResult *lsr, SerializeParameters *sp, RakNet::RakPeerInterface *rakPeer, unsigned char worldId, ReplicaManager3 *replicaManager, RakNet::Time curTime); - - /// \internal - /// \brief Given a list of objects that were created and destroyed, serialize and send them to another system. - /// \param[in] newObjects Objects to serialize construction - /// \param[in] deletedObjects Objects to serialize destruction - /// \param[in] sendParameters Controlling parameters over the serialization - /// \param[in] rakPeer Instance of RakPeerInterface to send on - /// \param[in] worldId Which world, see ReplicaManager3::AddWorld() - /// \param[in] replicaManager3 ReplicaManager3 instance - virtual void SendConstruction(DataStructures::List &newObjects, DataStructures::List &deletedObjects, PRO sendParameters, RakNet::RakPeerInterface *rakPeer, unsigned char worldId, ReplicaManager3 *replicaManager3); - - /// \internal - void SendValidation(RakNet::RakPeerInterface *rakPeer, WorldId worldId); - - /// \internal - void AutoConstructByQuery(ReplicaManager3 *replicaManager3, WorldId worldId); - - - // Internal - does the other system have this connection too? Validated means we can now use it - bool isValidated; - // Internal - Used to see if we should send download started - bool isFirstConstruction; - - static int Replica3LSRComp( Replica3 * const &replica3, LastSerializationResult * const &data ); - - // Internal - void ClearDownloadGroup(RakPeerInterface *rakPeerInterface); -protected: - - SystemAddress systemAddress; - RakNetGUID guid; - - /* - Operations: - - Locally reference a new replica: - Add to queryToConstructReplicaList for all objects - - Add all objects to queryToConstructReplicaList - - Download: - Add to constructedReplicaList for connection that send the object to us - Add to queryToSerializeReplicaList for connection that send the object to us - Add to queryToConstructReplicaList for all other connections - - Never construct for this connection: - Remove from queryToConstructReplicaList - - Construct to this connection - Remove from queryToConstructReplicaList - Add to constructedReplicaList for this connection - Add to queryToSerializeReplicaList for this connection - - Serialize: - Iterate through queryToSerializeReplicaList - - Never serialize for this connection - Remove from queryToSerializeReplicaList - - Reference (this system has this object already) - Remove from queryToConstructReplicaList - Add to constructedReplicaList for this connection - Add to queryToSerializeReplicaList for this connection - - Downloaded an existing object - if replica is in queryToConstructReplicaList, OnConstructToThisConnection() - else ignore - - Send destruction from query - Remove from queryToDestructReplicaList - Remove from queryToSerializeReplicaList - Remove from constructedReplicaList - Add to queryToConstructReplicaList - - Do not query destruction again - Remove from queryToDestructReplicaList - */ - void OnLocalReference(Replica3* replica3, ReplicaManager3 *replicaManager); - void OnDereference(Replica3* replica3, ReplicaManager3 *replicaManager); - void OnDownloadFromThisSystem(Replica3* replica3, ReplicaManager3 *replicaManager); - void OnDownloadFromOtherSystem(Replica3* replica3, ReplicaManager3 *replicaManager); - void OnNeverConstruct(unsigned int queryToConstructIdx, ReplicaManager3 *replicaManager); - void OnConstructToThisConnection(unsigned int queryToConstructIdx, ReplicaManager3 *replicaManager); - void OnConstructToThisConnection(Replica3 *replica, ReplicaManager3 *replicaManager); - void OnNeverSerialize(LastSerializationResult *lsr, ReplicaManager3 *replicaManager); - void OnReplicaAlreadyExists(unsigned int queryToConstructIdx, ReplicaManager3 *replicaManager); - void OnDownloadExisting(Replica3* replica3, ReplicaManager3 *replicaManager); - void OnSendDestructionFromQuery(unsigned int queryToDestructIdx, ReplicaManager3 *replicaManager); - void OnDoNotQueryDestruction(unsigned int queryToDestructIdx, ReplicaManager3 *replicaManager); - void ValidateLists(ReplicaManager3 *replicaManager) const; - void SendSerializeHeader(RakNet::Replica3 *replica, RakNet::Time timestamp, RakNet::BitStream *bs, WorldId worldId); - - // The list of objects that our local system and this remote system both have - // Either we sent this object to them, or they sent this object to us - // A given Replica can be either in queryToConstructReplicaList or constructedReplicaList but not both at the same time - DataStructures::OrderedList constructedReplicaList; - - // Objects that we have, but this system does not, and we will query each tick to see if it should be sent to them - // If we do send it to them, the replica is moved to constructedReplicaList - // A given Replica can be either in queryToConstructReplicaList or constructedReplicaList but not both at the same time - DataStructures::List queryToConstructReplicaList; - - // Objects that this system has constructed are added at the same time to queryToSerializeReplicaList - // This list is used to serialize all objects that this system has to this connection - DataStructures::List queryToSerializeReplicaList; - - // Objects that are constructed on this system are also queried if they should be destroyed to this system - DataStructures::List queryToDestructReplicaList; - - // Working lists - DataStructures::List constructedReplicasCulled, destroyedReplicasCulled; - - // This is used if QueryGroupDownloadMessages() returns true when ID_REPLICA_MANAGER_DOWNLOAD_STARTED arrives - // Packets will be gathered and not returned until ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE arrives - bool groupConstructionAndSerialize; - DataStructures::Queue downloadGroup; - - // Stores if we got download complete for this connection - bool gotDownloadComplete; - - friend class ReplicaManager3; -private: - Connection_RM3() {}; - - ConstructionMode constructionMode; -}; - -/// \brief Return codes for Connection_RM3::GetConstructionState() and Replica3::QueryConstruction() -/// \details Indicates what state the object should be in for the remote system -/// \ingroup REPLICA_MANAGER_GROUP3 -enum RM3ConstructionState -{ - /// This object should exist on the remote system. Send a construction message if necessary - /// If the NetworkID is already in use, it will not do anything - /// If it is not in use, it will create the object, and then call DeserializeConstruction - RM3CS_SEND_CONSTRUCTION, - - /// This object should exist on the remote system. - /// The other system already has the object, and the object will never be deleted. - /// This is true of objects that are loaded with the level, for example. - /// Treat it as if it existed, without sending a construction message. - /// Will call Serialize() and SerializeConstructionExisting() to the object on the remote system - RM3CS_ALREADY_EXISTS_REMOTELY, - - /// Same as RM3CS_ALREADY_EXISTS_REMOTELY but does not call SerializeConstructionExisting() - RM3CS_ALREADY_EXISTS_REMOTELY_DO_NOT_CONSTRUCT, - - /// This object will never be sent to the target system - /// This object will never be serialized from this system to the target system - RM3CS_NEVER_CONSTRUCT, - - /// Don't do anything this tick. Will query again next tick - RM3CS_NO_ACTION, - - /// Max enum - RM3CS_MAX, -}; - -/// If this object already exists for this system, should it be removed? -/// \ingroup REPLICA_MANAGER_GROUP3 -enum RM3DestructionState -{ - /// This object should not exist on the remote system. Send a destruction message if necessary. - RM3DS_SEND_DESTRUCTION, - - /// This object will never be destroyed by a per-tick query. Don't call again - RM3DS_DO_NOT_QUERY_DESTRUCTION, - - /// Don't do anything this tick. Will query again next tick - RM3DS_NO_ACTION, - - /// Max enum - RM3DS_MAX, -}; - -/// Return codes when constructing an object -/// \ingroup REPLICA_MANAGER_GROUP3 -enum RM3SerializationResult -{ - /// This object serializes identically no matter who we send to - /// We also send it to every connection (broadcast). - /// Efficient for memory, speed, and bandwidth but only if the object is always broadcast identically. - RM3SR_BROADCAST_IDENTICALLY, - - /// Same as RM3SR_BROADCAST_IDENTICALLY, but assume the object needs to be serialized, do not check with a memcmp - /// Assume the object changed, and serialize it - /// Use this if you know exactly when your object needs to change. Can be faster than RM3SR_BROADCAST_IDENTICALLY. - /// An example of this is if every member variable has an accessor, changing a member sets a flag, and you check that flag in Replica3::QuerySerialization() - /// The opposite of this is RM3SR_DO_NOT_SERIALIZE, in case the object did not change - RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION, - - /// Either this object serializes differently depending on who we send to or we send it to some systems and not others. - /// Inefficient for memory and speed, but efficient for bandwidth - /// However, if you don't know what to return, return this - RM3SR_SERIALIZED_UNIQUELY, - - /// Do not compare against last sent value. Just send even if the data is the same as the last tick - /// If the data is always changing anyway, or you want to send unreliably, this is a good method of serialization - /// Can send unique data per connection if desired. If same data is sent to all connections, use RM3SR_SERIALIZED_ALWAYS_IDENTICALLY for even better performance - /// Efficient for memory and speed, but not necessarily bandwidth - RM3SR_SERIALIZED_ALWAYS, - - /// \deprecated, use RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION - RM3SR_SERIALIZED_ALWAYS_IDENTICALLY, - - /// Do not serialize this object this tick, for this connection. Will query again next autoserialize timer - RM3SR_DO_NOT_SERIALIZE, - - /// Never serialize this object for this connection - /// Useful for objects that are downloaded, and never change again - /// Efficient - RM3SR_NEVER_SERIALIZE_FOR_THIS_CONNECTION, - - /// Max enum - RM3SR_MAX, -}; - -/// First pass at topology to see if an object should be serialized -/// \ingroup REPLICA_MANAGER_GROUP3 -enum RM3QuerySerializationResult -{ - /// Call Serialize() to see if this object should be serializable for this connection - RM3QSR_CALL_SERIALIZE, - /// Do not call Serialize() this tick to see if this object should be serializable for this connection - RM3QSR_DO_NOT_CALL_SERIALIZE, - /// Never call Serialize() for this object and connection. This system will not serialize this object for this topology - RM3QSR_NEVER_CALL_SERIALIZE, - /// Max enum - RM3QSR_MAX, -}; - -/// \ingroup REPLICA_MANAGER_GROUP3 -enum RM3ActionOnPopConnection -{ - RM3AOPC_DO_NOTHING, - RM3AOPC_DELETE_REPLICA, - RM3AOPC_DELETE_REPLICA_AND_BROADCAST_DESTRUCTION, - RM3AOPC_MAX, -}; - -/// \ingroup REPLICA_MANAGER_GROUP3 -/// Used for Replica3::QueryConstruction_PeerToPeer() and Replica3::QuerySerialization_PeerToPeer() to describe how the object replicates between hosts -enum Replica3P2PMode -{ - /// The Replica3 instance is constructed and serialized by one system only. - /// Example: Your avatar. No other player serializes or can create your avatar. - R3P2PM_SINGLE_OWNER, - /// The Replica3 instance is constructed and/or serialized by different systems - /// This system is currently in charge of construction and/or serialization - /// Example: A pickup. When an avatar holds it, that avatar controls it. When it is on the ground, the host controls it. - R3P2PM_MULTI_OWNER_CURRENTLY_AUTHORITATIVE, - /// The Replica3 instance is constructed and/or serialized by different systems - /// Another system is in charge of construction and/or serialization, but this system may be in charge at a later time - /// Example: A pickup held by another player. That player sends creation of that object to new connections, and serializes it until it is dropped. - R3P2PM_MULTI_OWNER_NOT_CURRENTLY_AUTHORITATIVE, - /// The Replica3 instance is a static object (already exists on the remote system). - /// This system is currently in charge of construction and/or serialization - R3P2PM_STATIC_OBJECT_CURRENTLY_AUTHORITATIVE, - /// The Replica3 instance is a static object (already exists on the remote system). - /// Another system is in charge of construction and/or serialization, but this system may be in charge at a later time - R3P2PM_STATIC_OBJECT_NOT_CURRENTLY_AUTHORITATIVE, - -}; - -/// \brief Base class for your replicated objects for the ReplicaManager3 system. -/// \details To use, derive your class, or a member of your class, from Replica3.
-/// \ingroup REPLICA_MANAGER_GROUP3 -class RAK_DLL_EXPORT Replica3 : public NetworkIDObject -{ -public: - Replica3(); - - /// Before deleting a local instance of Replica3, call Replica3::BroadcastDestruction() for the deletion notification to go out on the network. - /// It is not necessary to call ReplicaManager3::Dereference(), as this happens automatically in the destructor - virtual ~Replica3(); - - /// \brief Write a unique identifer that can be read on a remote system to create an object of this same class. - /// \details The value written to \a allocationIdBitstream will be passed to Connection_RM3::AllocReplica().
- /// Sample implementation:
- /// {allocationIdBitstream->Write(RakNet::RakString("Soldier");}
- /// \param[out] allocationIdBitstream Bitstream for the user to write to, to identify this class - virtual void WriteAllocationID(RakNet::Connection_RM3 *destinationConnection, RakNet::BitStream *allocationIdBitstream) const=0; - - /// \brief Ask if this object, which does not exist on \a destinationConnection should (now) be sent to that system. - /// \details If ReplicaManager3::QueryConstructionMode() returns QUERY_CONNECTION_FOR_REPLICA_LIST or QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION (default), - /// then QueyrConstruction() is called once per tick from ReplicaManager3::Update() to determine if an object should exist on a given system.
- /// Based on the return value, a network message may be sent to the other system to create the object.
- /// If QueryConstructionMode() is overriden to return QUERY_CONNECTION_FOR_REPLICA_LIST, this function is unused.
- /// \note Defaults are provided: QueryConstruction_PeerToPeer(), QueryConstruction_ServerConstruction(), QueryConstruction_ClientConstruction(). Return one of these functions for a working default for the relevant topology. - /// \param[in] destinationConnection Which system we will send to - /// \param[in] replicaManager3 Plugin instance for this Replica3 - /// \return What action to take - virtual RM3ConstructionState QueryConstruction(RakNet::Connection_RM3 *destinationConnection, ReplicaManager3 *replicaManager3)=0; - - /// \brief Ask if this object, which does exist on \a destinationConnection should be removed from the remote system - /// \details If ReplicaManager3::QueryConstructionMode() returns QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION (default), - /// then QueryDestruction() is called once per tick from ReplicaManager3::Update() to determine if an object that exists on a remote system should be destroyed for a given system.
- /// Based on the return value, a network message may be sent to the other system to destroy the object.
- /// Note that you can also destroy objects with BroadcastDestruction(), so this function is not useful unless you plan to delete objects for only a particular connection.
- /// If QueryConstructionMode() is overriden to return QUERY_CONNECTION_FOR_REPLICA_LIST, this function is unused.
- /// \param[in] destinationConnection Which system we will send to - /// \param[in] replicaManager3 Plugin instance for this Replica3 - /// \return What action to take. Only RM3CS_SEND_DESTRUCTION does anything at this time. - virtual RM3DestructionState QueryDestruction(RakNet::Connection_RM3 *destinationConnection, ReplicaManager3 *replicaManager3) {(void) destinationConnection; (void) replicaManager3; return RM3DS_DO_NOT_QUERY_DESTRUCTION;} - - /// \brief We're about to call DeserializeConstruction() on this Replica3. If QueryRemoteConstruction() returns false, this object is deleted instead. - /// \details By default, QueryRemoteConstruction_ServerConstruction() does not allow clients to create objects. The client will get Replica3::DeserializeConstructionRequestRejected().
- /// If you want the client to be able to potentially create objects for client/server, override accordingly.
- /// Other variants of QueryRemoteConstruction_* just return true. - /// \note Defaults are provided: QueryRemoteConstruction_PeerToPeer(), QueryRemoteConstruction_ServerConstruction(), QueryRemoteConstruction_ClientConstruction(). Return one of these functions for a working default for the relevant topology. - /// \param[in] sourceConnection Which system sent us the object creation request message. - /// \return True to allow the object to pass onto DeserializeConstruction() (where it may also be rejected), false to immediately reject the remote construction request - virtual bool QueryRemoteConstruction(RakNet::Connection_RM3 *sourceConnection)=0; - - /// \brief We got a message from a connection to destroy this replica - /// Return true to automatically relay the destruction message to all our other connections - /// For a client in client/server, it does not matter what this funtion returns - /// For a server in client/server, this should normally return true - /// For a peer in peer to peer, you can normally return false since the original destroying peer would have told all other peers about the destruction - /// If a system gets a destruction command for an object that was already destroyed, the destruction message is ignored - virtual bool QueryRelayDestruction(Connection_RM3 *sourceConnection) const {(void) sourceConnection; return true;} - - /// \brief Write data to be sent only when the object is constructed on a remote system. - /// \details SerializeConstruction is used to write out data that you need to create this object in the context of your game, such as health, score, name. Use it for data you only need to send when the object is created.
- /// After SerializeConstruction() is called, Serialize() will be called immediately thereafter. However, they are sent in different messages, so Serialize() may arrive a later frame than SerializeConstruction() - /// For that reason, the object should be valid after a call to DeserializeConstruction() for at least a short time.
- /// \note The object's NetworkID and allocation id are handled by the system automatically, you do not need to write these values to \a constructionBitstream - /// \param[out] constructionBitstream Destination bitstream to write your data to - /// \param[in] destinationConnection System that will receive this network message. - virtual void SerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection)=0; - - /// \brief Read data written by Replica3::SerializeConstruction() - /// \details Reads whatever data was written to \a constructionBitstream in Replica3::SerializeConstruction() - /// \param[out] constructionBitstream Bitstream written to in Replica3::SerializeConstruction() - /// \param[in] sourceConnection System that sent us this network message. - /// \return true to accept construction of the object. false to reject, in which case the object will be deleted via Replica3::DeallocReplica() - virtual bool DeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection)=0; - - /// Same as SerializeConstruction(), but for an object that already exists on the remote system. - /// Used if you return RM3CS_ALREADY_EXISTS_REMOTELY from QueryConstruction - virtual void SerializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {(void) constructionBitstream; (void) destinationConnection;}; - - /// Same as DeserializeConstruction(), but for an object that already exists on the remote system. - /// Used if you return RM3CS_ALREADY_EXISTS_REMOTELY from QueryConstruction - virtual void DeserializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {(void) constructionBitstream; (void) sourceConnection;}; - - /// \brief Write extra data to send with the object deletion event, if desired - /// \details Replica3::SerializeDestruction() will be called to write any object destruction specific data you want to send with this event. - /// \a destructionBitstream can be read in DeserializeDestruction() - /// \param[out] destructionBitstream Bitstream for you to write to - /// \param[in] destinationConnection System that will receive this network message. - virtual void SerializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *destinationConnection)=0; - - /// \brief Read data written by Replica3::SerializeDestruction() - /// \details Return true to delete the object. BroadcastDestruction() will be called automatically, followed by ReplicaManager3::Dereference.
- /// Return false to not delete it. If you delete it at a later point, you are responsible for calling BroadcastDestruction() yourself. - virtual bool DeserializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *sourceConnection)=0; - - /// \brief The system is asking what to do with this replica when the connection is dropped - /// \details Return QueryActionOnPopConnection_Client, QueryActionOnPopConnection_Server, or QueryActionOnPopConnection_PeerToPeer - virtual RakNet::RM3ActionOnPopConnection QueryActionOnPopConnection(RakNet::Connection_RM3 *droppedConnection) const=0; - - /// Notification called for each of our replicas when a connection is popped - virtual void OnPoppedConnection(RakNet::Connection_RM3 *droppedConnection) {(void) droppedConnection;} - - /// \brief Override with {delete this;} - /// \details - ///
    - ///
  1. Got a remote message to delete this object which passed DeserializeDestruction(), OR - ///
  2. ReplicaManager3::SetAutoManageConnections() was called autoDestroy true (which is the default setting), and a remote system that owns this object disconnected) OR - /// <\OL> - ///
    - /// Override with {delete this;} to actually delete the object (and any other processing you wish).
    - /// If you don't want to delete the object, just do nothing, however, the system will not know this. You may wish to call Dereference() if the object should no longer be networked, but remain in memory. You are responsible for deleting it yoruself later.
    - /// destructionBitstream may be 0 if the object was deleted locally - virtual void DeallocReplica(RakNet::Connection_RM3 *sourceConnection)=0; - - /// \brief Implement with QuerySerialization_ClientSerializable(), QuerySerialization_ServerSerializable(), or QuerySerialization_PeerToPeer() - /// \details QuerySerialization() is a first pass query to check if a given object should serializable to a given system. The intent is that the user implements with one of the defaults for client, server, or peer to peer.
    - /// Without this function, a careless implementation would serialize an object anytime it changed to all systems. This would give you feedback loops as the sender gets the same message back from the recipient it just sent to.
    - /// If more than one system can serialize the same object then you will need to override to return true, and control the serialization result from Replica3::Serialize(). Be careful not to send back the same data to the system that just sent to you! - /// \return True to allow calling Replica3::Serialize() for this connection, false to not call. - virtual RakNet::RM3QuerySerializationResult QuerySerialization(RakNet::Connection_RM3 *destinationConnection)=0; - - /// \brief Called for each replica owned by the user, once per Serialization tick, before Serialize() is called. - /// If you want to do some kind of operation on the Replica objects that you own, just before Serialization(), then overload this function - virtual void OnUserReplicaPreSerializeTick(void) {} - - /// \brief Serialize our class to a bitstream - /// \details User should implement this function to write the contents of this class to SerializationParamters::serializationBitstream.
    - /// If data only needs to be written once, you can write it to SerializeConstruction() instead for efficiency.
    - /// Transmitted over the network if it changed from the last time we called Serialize().
    - /// Called every time the time interval to ReplicaManager3::SetAutoSerializeInterval() elapses and ReplicaManager3::Update is subsequently called. - /// \param[in/out] serializeParameters Parameters controlling the serialization, including destination bitstream to write to - /// \return Whether to serialize, and if so, how to optimize the results - virtual RM3SerializationResult Serialize(RakNet::SerializeParameters *serializeParameters)=0; - - /// \brief Called when the class is actually transmitted via Serialize() - /// \details Use to track how much bandwidth this class it taking - virtual void OnSerializeTransmission(RakNet::BitStream *bitStream, RakNet::Connection_RM3 *destinationConnection, BitSize_t bitsPerChannel[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::Time curTime) {(void) bitStream; (void) destinationConnection; (void) bitsPerChannel; (void) curTime;} - - /// \brief Read what was written in Serialize() - /// \details Reads the contents of the class from SerializationParamters::serializationBitstream.
    - /// Called whenever Serialize() is called with different data from the last send. - /// \param[in] serializationBitstream Bitstream passed to Serialize() - /// \param[in] timeStamp 0 if unused, else contains the time the message originated on the remote system - /// \param[in] sourceConnection Which system sent to us - virtual void Deserialize(RakNet::DeserializeParameters *deserializeParameters)=0; - - /// \brief Called after SerializeConstruction completes for all objects in a given update tick.
    - /// Writes to PostDeserializeConstruction(), which is called after all objects are created for a given Construction tick(). - /// Override to send data to PostDeserializeConstruction(), such as the NetworkID of other objects to resolve pointers to - virtual void PostSerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {(void) constructionBitstream; (void) destinationConnection;} - - /// Called after DeserializeConstruction completes for all objects in a given update tick.
    - /// This is used to resolve dependency chains, where two objects would refer to each other in DeserializeConstruction, yet one had not been constructed yet - /// In PostDeserializeConstruction(), you know that all objects have already been created, so can resolve NetworkIDs to pointers safely. - /// You can also use it to trigger some sort of event when you know the object has completed deserialization. - /// \param[in] constructionBitstream BitStream written in PostSerializeConstruction() - /// \param[in] sourceConnection System that sent us this network message. - virtual void PostDeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {(void) constructionBitstream; (void) sourceConnection;} - - /// Same as PostSerializeConstruction(), but for objects that returned RM3CS_ALREADY_EXISTS_REMOTELY from QueryConstruction - virtual void PostSerializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {(void) constructionBitstream; (void) destinationConnection;} - - /// Same as PostDeserializeConstruction(), but for objects that returned RM3CS_ALREADY_EXISTS_REMOTELY from QueryConstruction - virtual void PostDeserializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {(void) constructionBitstream; (void) sourceConnection;} - - /// Called after DeserializeDestruction completes for the object successfully, but obviously before the object is deleted.
    - /// Override to trigger some sort of event when you know the object has completed destruction. - /// \param[in] sourceConnection System that sent us this network message. - virtual void PreDestruction(RakNet::Connection_RM3 *sourceConnection) {(void) sourceConnection;} - - /// \brief Default call for QueryConstruction(). - /// \details Both the client and the server is allowed to create this object. The network topology is client/server - /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction() - /// \param[in] isThisTheServer True if this system is the server, false if not. - virtual RM3ConstructionState QueryConstruction_ClientConstruction(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer); - - /// Default call for QueryRemoteConstruction(). - /// \details Both the client and the server is allowed to create this object. The network topology is client/server - /// The code means on the client or the server, allow creation of Replica3 instances - /// \param[in] sourceConnection destinationConnection parameter passed to QueryConstruction() - /// \param[in] isThisTheServer True if this system is the server, false if not. - virtual bool QueryRemoteConstruction_ClientConstruction(RakNet::Connection_RM3 *sourceConnection, bool isThisTheServer); - - /// \brief Default call for QueryConstruction(). - /// \details Only the server is allowed to create this object. The network topology is client/server - /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction() - /// \param[in] isThisTheServer True if this system is the server, false if not. - virtual RM3ConstructionState QueryConstruction_ServerConstruction(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer); - - /// \brief Default call for QueryRemoteConstruction(). Allow the server to create this object, but not the client. - /// \details Only the server is allowed to create this object. The network topology is client/server - /// The code means if this is the server, and I got a command to create a Replica3 to ignore it. If this is the client, to allow it. - /// \param[in] sourceConnection destinationConnection parameter passed to QueryConstruction() - /// \param[in] isThisTheServer True if this system is the server, false if not. - virtual bool QueryRemoteConstruction_ServerConstruction(RakNet::Connection_RM3 *sourceConnection, bool isThisTheServer); - - /// \brief Default call for QueryConstruction(). - /// \details All clients are allowed to create all objects. The object is not relayed when remotely created - /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction() - /// \param[in] p2pMode If controlled only by this system ever, pass R3P2PM_SINGLE_OWNER. Otherwise pass R3P2PM_MULTI_OWNER_CURRENTLY_AUTHORITATIVE or R3P2PM_MULTI_OWNER_NOT_CURRENTLY_AUTHORITATIVE - virtual RM3ConstructionState QueryConstruction_PeerToPeer(RakNet::Connection_RM3 *destinationConnection, Replica3P2PMode p2pMode=R3P2PM_SINGLE_OWNER); - /// \brief Default call for QueryRemoteConstruction(). - /// \details All clients are allowed to create all objects. The object is not relayed when remotely created - /// \param[in] sourceConnection destinationConnection parameter passed to QueryConstruction() - virtual bool QueryRemoteConstruction_PeerToPeer(RakNet::Connection_RM3 *sourceConnection); - - /// \brief Default call for QuerySerialization(). - /// \details Use if the values you are serializing are generated by the client that owns the object. The serialization will be relayed through the server to the other clients. - /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction() - /// \param[in] isThisTheServer True if this system is the server, false if not. - virtual RakNet::RM3QuerySerializationResult QuerySerialization_ClientSerializable(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer); - /// \brief Default call for QuerySerialization(). - /// \details Use if the values you are serializing are generated only by the server. The serialization will be sent to all clients, but the clients will not send back to the server. - /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction() - /// \param[in] isThisTheServer True if this system is the server, false if not. - virtual RakNet::RM3QuerySerializationResult QuerySerialization_ServerSerializable(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer); - /// \brief Default call for QuerySerialization(). - /// \details Use if the values you are serializing are on a peer to peer network. The peer that owns the object will send to all. Remote peers will not send. - /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction() - /// \param[in] p2pMode If controlled only by this system ever, pass R3P2PM_SINGLE_OWNER. Otherwise pass R3P2PM_MULTI_OWNER_CURRENTLY_AUTHORITATIVE or R3P2PM_MULTI_OWNER_NOT_CURRENTLY_AUTHORITATIVE - virtual RakNet::RM3QuerySerializationResult QuerySerialization_PeerToPeer(RakNet::Connection_RM3 *destinationConnection, Replica3P2PMode p2pMode=R3P2PM_SINGLE_OWNER); - - /// Default: If we are a client, and the connection is lost, delete the server's objects - virtual RM3ActionOnPopConnection QueryActionOnPopConnection_Client(RakNet::Connection_RM3 *droppedConnection) const; - /// Default: If we are a server, and the connection is lost, delete the client's objects and broadcast the destruction - virtual RM3ActionOnPopConnection QueryActionOnPopConnection_Server(RakNet::Connection_RM3 *droppedConnection) const; - /// Default: If we are a peer, and the connection is lost, delete the peer's objects - virtual RM3ActionOnPopConnection QueryActionOnPopConnection_PeerToPeer(RakNet::Connection_RM3 *droppedConnection) const; - - /// Call to send a network message to delete this object on other systems.
    - /// Call it before deleting the object - virtual void BroadcastDestruction(void); - - /// creatingSystemGUID is set the first time Reference() is called, or if we get the object from another system - /// \return System that originally created this object - RakNetGUID GetCreatingSystemGUID(void) const; - - /// \return If ReplicaManager3::Reference() was called on this object. - bool WasReferenced(void) const {return replicaManager!=0;} - - /// GUID of the system that first called Reference() on this object. - /// Transmitted automatically when the object is constructed - RakNetGUID creatingSystemGUID; - /// GUID of the system that caused the item to send a deletion command over the network - RakNetGUID deletingSystemGUID; - - /// \internal - /// ReplicaManager3 plugin associated with this object - ReplicaManager3 *replicaManager; - - LastSerializationResultBS lastSentSerialization; - bool forceSendUntilNextUpdate; - LastSerializationResult *lsr; - uint32_t referenceIndex; -}; - -/// \brief Use Replica3 through composition instead of inheritance by containing an instance of this templated class -/// Calls to parent class for all functions -/// Parent class must still define and functions though! -/// \pre Parent class must call SetCompositeOwner() on this object -template -class RAK_DLL_EXPORT Replica3Composite : public Replica3 -{ -protected: - parent_type *r3CompositeOwner; -public: - void SetCompositeOwner(parent_type *p) {r3CompositeOwner=p;} - parent_type* GetCompositeOwner(void) const {return r3CompositeOwner;}; - virtual void WriteAllocationID(RakNet::Connection_RM3 *destinationConnection, RakNet::BitStream *allocationIdBitstream) const {r3CompositeOwner->WriteAllocationID(destinationConnection, allocationIdBitstream);} - virtual RakNet::RM3ConstructionState QueryConstruction(RakNet::Connection_RM3 *destinationConnection, RakNet::ReplicaManager3 *replicaManager3) {return r3CompositeOwner->QueryConstruction(destinationConnection, replicaManager3);} - virtual RakNet::RM3DestructionState QueryDestruction(RakNet::Connection_RM3 *destinationConnection, RakNet::ReplicaManager3 *replicaManager3) {return r3CompositeOwner->QueryDestruction(destinationConnection, replicaManager3);} - virtual bool QueryRemoteConstruction(RakNet::Connection_RM3 *sourceConnection) {return r3CompositeOwner->QueryRemoteConstruction(sourceConnection);} - virtual bool QueryRelayDestruction(RakNet::Connection_RM3 *sourceConnection) const {return r3CompositeOwner->QueryRelayDestruction(sourceConnection);} - virtual void SerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->SerializeConstruction(constructionBitstream, destinationConnection);} - virtual bool DeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {return r3CompositeOwner->DeserializeConstruction(constructionBitstream, sourceConnection);} - virtual void SerializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->SerializeConstructionExisting(constructionBitstream, destinationConnection);} - virtual void DeserializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->DeserializeConstructionExisting(constructionBitstream, sourceConnection);} - virtual void SerializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->SerializeDestruction(destructionBitstream, destinationConnection);} - virtual bool DeserializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *sourceConnection) {return r3CompositeOwner->DeserializeDestruction(destructionBitstream, sourceConnection);} - virtual RakNet::RM3ActionOnPopConnection QueryActionOnPopConnection(RakNet::Connection_RM3 *droppedConnection) const {return r3CompositeOwner->QueryActionOnPopConnection(droppedConnection);} - virtual void OnPoppedConnection(RakNet::Connection_RM3 *droppedConnection) {r3CompositeOwner->OnPoppedConnection(droppedConnection);} - virtual void DeallocReplica(RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->DeallocReplica(sourceConnection);} - virtual RakNet::RM3QuerySerializationResult QuerySerialization(RakNet::Connection_RM3 *destinationConnection) {return r3CompositeOwner->QuerySerialization(destinationConnection);} - virtual void OnUserReplicaPreSerializeTick(void) {r3CompositeOwner->OnUserReplicaPreSerializeTick();} - virtual RakNet::RM3SerializationResult Serialize(RakNet::SerializeParameters *serializeParameters) {return r3CompositeOwner->Serialize(serializeParameters);} - virtual void OnSerializeTransmission(RakNet::BitStream *bitStream, RakNet::Connection_RM3 *destinationConnection, RakNet::BitSize_t bitsPerChannel[RakNet::RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::Time curTime) {r3CompositeOwner->OnSerializeTransmission(bitStream, destinationConnection, bitsPerChannel, curTime);} - virtual void Deserialize(RakNet::DeserializeParameters *deserializeParameters) {r3CompositeOwner->Deserialize(deserializeParameters);} - virtual void PostSerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->PostSerializeConstruction(constructionBitstream, destinationConnection);} - virtual void PostDeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->PostDeserializeConstruction(constructionBitstream, sourceConnection);} - virtual void PostSerializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->PostSerializeConstructionExisting(constructionBitstream, destinationConnection);} - virtual void PostDeserializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->PostDeserializeConstructionExisting(constructionBitstream, sourceConnection);} - virtual void PreDestruction(RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->PreDestruction(sourceConnection);} -}; - -} // namespace RakNet - - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Contains the third iteration of the ReplicaManager class. +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_ReplicaManager3==1 + +#pragma once + +#include "RakNetTypes.h" +#include "RakNetTime.h" +#include "BitStream.h" +#include "PacketPriority.h" +#include "PluginInterface2.h" +#include "NetworkIDObject.h" +#include "DS_OrderedList.h" +#include "DS_Queue.h" + +/// \defgroup REPLICA_MANAGER_GROUP3 ReplicaManager3 +/// \brief Third implementation of object replication +/// \details +/// \ingroup PLUGINS_GROUP + +namespace RakNet +{ +class Connection_RM3; +class Replica3; + +/// \ingroup REPLICA_MANAGER_GROUP3 +/// Used for multiple worlds. World 0 is created automatically by default +typedef uint8_t WorldId; + + +/// \internal +/// \ingroup REPLICA_MANAGER_GROUP3 +struct PRO +{ + /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultPacketPriority(). + PacketPriority priority; + + /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultPacketReliability(). + PacketReliability reliability; + + /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultOrderingChannel(). + char orderingChannel; + + /// Passed to RakPeerInterface::Send(). Defaults to 0. + uint32_t sendReceipt; + + bool operator==( const PRO& right ) const; + bool operator!=( const PRO& right ) const; +}; + + +/// \brief System to help automate game object construction, destruction, and serialization +/// \details ReplicaManager3 tracks your game objects and automates the networking for replicating them across the network
    +/// As objects are created, destroyed, or serialized differently, those changes are pushed out to other systems.
    +/// To use:
    +///
      +///
    1. Derive from Connection_RM3 and implement Connection_RM3::AllocReplica(). This is a factory function where given a user-supplied identifier for a class (such as name) return an instance of that class. Should be able to return any networked object in your game. +///
    2. Derive from ReplicaManager3 and implement AllocConnection() and DeallocConnection() to return the class you created in step 1. +///
    3. Derive your networked game objects from Replica3. All pure virtuals have to be implemented, however defaults are provided for Replica3::QueryConstruction(), Replica3::QueryRemoteConstruction(), and Replica3::QuerySerialization() depending on your network architecture. +///
    4. When a new game object is created on the local system, pass it to ReplicaManager3::Reference(). +///
    5. When a game object is destroyed on the local system, and you want other systems to know about it, call Replica3::BroadcastDestruction() +///
    +///
    +/// At this point, all new connections will automatically download, get construction messages, get destruction messages, and update serialization automatically. +/// \ingroup REPLICA_MANAGER_GROUP3 +class RAK_DLL_EXPORT ReplicaManager3 : public PluginInterface2 +{ +public: + ReplicaManager3(); + virtual ~ReplicaManager3(); + + /// \brief Implement to return a game specific derivation of Connection_RM3 + /// \details The connection object represents a remote system connected to you that is using the ReplicaManager3 system.
    + /// It has functions to perform operations per-connection.
    + /// AllocConnection() and DeallocConnection() are factory functions to create and destroy instances of the connection object.
    + /// It is used if autoCreate is true via SetAutoManageConnections() (true by default). Otherwise, the function is not called, and you will have to call PushConnection() manually
    + /// \note If you do not want a new network connection to immediately download game objects, SetAutoManageConnections() and PushConnection() are how you do this. + /// \sa SetAutoManageConnections() + /// \param[in] systemAddress Address of the system you are adding + /// \param[in] rakNetGUID GUID of the system you are adding. See Packet::rakNetGUID or RakPeerInterface::GetGUIDFromSystemAddress() + /// \return The new connection instance. + virtual Connection_RM3* AllocConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID) const=0; + + /// \brief Implement to destroy a class instanced returned by AllocConnection() + /// \details Most likely just implement as {delete connection;}
    + /// It is used if autoDestroy is true via SetAutoManageConnections() (true by default). Otherwise, the function is not called and you would then be responsible for deleting your own connection objects. + /// \param[in] connection The pointer instance to delete + virtual void DeallocConnection(Connection_RM3 *connection) const=0; + + /// \brief Enable or disable automatically assigning connections to new instances of Connection_RM3 + /// \details ReplicaManager3 can automatically create and/or destroy Connection_RM3 as systems connect or disconnect from RakPeerInterface.
    + /// By default this is on, to make the system easier to learn and setup.
    + /// If you don't want all connections to take part in the game, or you want to delay when a connection downloads the game, set \a autoCreate to false.
    + /// If you want to delay deleting a connection that has dropped, set \a autoDestroy to false. If you do this, then you must call PopConnection() to remove that connection from being internally tracked. You'll also have to delete the connection instance on your own.
    + /// \param[in] autoCreate Automatically call ReplicaManager3::AllocConnection() for each new connection. Defaults to true. Also see AutoCreateConnectionList() + /// \param[in] autoDestroy Automatically call ReplicaManager3::DeallocConnection() for each dropped connection. Defaults to true. + void SetAutoManageConnections(bool autoCreate, bool autoDestroy); + + /// \return What was passed to the autoCreate parameter of SetAutoManageConnections() + bool GetAutoCreateConnections(void) const; + + /// \return What was passed to the autoDestroy parameter of SetAutoManageConnections() + bool GetAutoDestroyConnections(void) const; + + /// \brief Call AllocConnection() and PushConnection() for each connection in \a participantList + /// \param[in] participantListIn The list of connections to allocate + /// \param[in] participantListOut The connections allocated, if any + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + void AutoCreateConnectionList( + DataStructures::List &participantListIn, + DataStructures::List &participantListOut, + WorldId worldId=0); + + /// \brief Track a new Connection_RM3 instance + /// \details If \a autoCreate is false for SetAutoManageConnections(), then you need this function to add new instances of Connection_RM3 yourself.
    + /// You don't need to track this pointer yourself, you can get it with GetConnectionAtIndex(), GetConnectionByGUID(), or GetConnectionBySystemAddress().
    + /// \param[in] newConnection The new connection instance to track. + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + bool PushConnection(RakNet::Connection_RM3 *newConnection, WorldId worldId=0); + + /// \brief Stop tracking a connection + /// \details On call, for each replica returned by GetReplicasCreatedByGuid(), QueryActionOnPopConnection() will be called. Depending on the return value, this may delete the corresponding replica.
    + /// If autoDestroy is true in the call to SetAutoManageConnections() (true by default) then this is called automatically when the connection is lost. In that case, the returned connection instance is deleted.
    + /// \param[in] guid of the connection to get. Passed to ReplicaManager3::AllocConnection() originally. + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + RakNet::Connection_RM3 * PopConnection(RakNetGUID guid, WorldId worldId=0); + + /// \brief Adds a replicated object to the system. + /// \details Anytime you create a new object that derives from Replica3, and you want ReplicaManager3 to use it, pass it to Reference().
    + /// Remote systems already connected will potentially download this object the next time ReplicaManager3::Update() is called, which happens every time you call RakPeerInterface::Receive().
    + /// You can also call ReplicaManager3::Update() manually to send referenced objects right away + /// \param[in] replica3 The object to start tracking + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + void Reference(RakNet::Replica3 *replica3, WorldId worldId=0); + + /// \brief Removes a replicated object from the system. + /// \details The object is not deallocated, it is up to the caller to do so.
    + /// This is called automatically from the destructor of Replica3, so you don't need to call it manually unless you want to stop tracking an object before it is destroyed. + /// \param[in] replica3 The object to stop tracking + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + void Dereference(RakNet::Replica3 *replica3, WorldId worldId=0); + + /// \brief Removes multiple replicated objects from the system. + /// \details Same as Dereference(), but for a list of objects.
    + /// Useful with the lists returned by GetReplicasCreatedByGuid(), GetReplicasCreatedByMe(), or GetReferencedReplicaList().
    + /// \param[in] replicaListIn List of objects + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + void DereferenceList(DataStructures::List &replicaListIn, WorldId worldId=0); + + /// \brief Returns all objects originally created by a particular system + /// \details Originally created is defined as the value of Replica3::creatingSystemGUID, which is automatically assigned in ReplicaManager3::Reference().
    + /// You do not have to be directly connected to that system to get the objects originally created by that system.
    + /// \param[in] guid GUID of the system we are referring to. Originally passed as the \a guid parameter to ReplicaManager3::AllocConnection() + /// \param[out] List of Replica3 instances to be returned + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + void GetReplicasCreatedByGuid(RakNetGUID guid, DataStructures::List &replicaListOut, WorldId worldId=0); + + /// \brief Returns all objects originally created by your system + /// \details Calls GetReplicasCreatedByGuid() for your own system guid. + /// \param[out] List of Replica3 instances to be returned + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + void GetReplicasCreatedByMe(DataStructures::List &replicaListOut, WorldId worldId=0); + + /// \brief Returns the entire list of Replicas that we know about. + /// \details This is all Replica3 instances passed to Reference, as well as instances we downloaded and created via Connection_RM3::AllocReference() + /// \param[out] List of Replica3 instances to be returned + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + void GetReferencedReplicaList(DataStructures::List &replicaListOut, WorldId worldId=0); + + /// \brief Returns the number of replicas known about + /// \details Returns the size of the list that would be returned by GetReferencedReplicaList() + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + /// \return How many replica objects are in the list of replica objects + unsigned GetReplicaCount(WorldId worldId=0) const; + + /// \brief Returns a replica by index + /// \details Returns one of the items in the list that would be returned by GetReferencedReplicaList() + /// \param[in] index An index, from 0 to GetReplicaCount()-1. + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + /// \return A Replica3 instance + Replica3 *GetReplicaAtIndex(unsigned index, WorldId worldId=0); + + /// \brief Returns the number of connections + /// \details Returns the number of connections added with ReplicaManager3::PushConnection(), minus the number removed with ReplicaManager3::PopConnection() + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + /// \return The number of registered connections + unsigned int GetConnectionCount(WorldId worldId=0) const; + + /// \brief Returns a connection pointer previously added with PushConnection() + /// \param[in] index An index, from 0 to GetConnectionCount()-1. + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + /// \return A Connection_RM3 pointer + Connection_RM3* GetConnectionAtIndex(unsigned index, WorldId worldId=0) const; + + /// \brief Returns a connection pointer previously added with PushConnection() + /// \param[in] sa The system address of the connection to return + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + /// \return A Connection_RM3 pointer, or 0 if not found + Connection_RM3* GetConnectionBySystemAddress(const SystemAddress &sa, WorldId worldId=0) const; + + /// \brief Returns a connection pointer previously added with PushConnection.() + /// \param[in] guid The guid of the connection to return + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + /// \return A Connection_RM3 pointer, or 0 if not found + Connection_RM3* GetConnectionByGUID(RakNetGUID guid, WorldId worldId=0) const; + + /// \param[in] Default ordering channel to use for object creation, destruction, and serializations + void SetDefaultOrderingChannel(char def); + + /// \param[in] Default packet priority to use for object creation, destruction, and serializations + void SetDefaultPacketPriority(PacketPriority def); + + /// \param[in] Default packet reliability to use for object creation, destruction, and serializations + void SetDefaultPacketReliability(PacketReliability def); + + /// \details Every \a intervalMS milliseconds, Connection_RM3::OnAutoserializeInterval() will be called.
    + /// Defaults to 30.
    + /// Pass with <0 to disable. Pass 0 to Serialize() every time RakPeer::Recieve() is called
    + /// If you want to control the update interval with more granularity, use the return values from Replica3::Serialize().
    + /// \param[in] intervalMS How frequently to autoserialize all objects. This controls the maximum number of game object updates per second. + void SetAutoSerializeInterval(RakNet::Time intervalMS); + + /// \brief Return the connections that we think have an instance of the specified Replica3 instance + /// \details This can be wrong, for example if that system locally deleted the outside the scope of ReplicaManager3, if QueryRemoteConstruction() returned false, or if DeserializeConstruction() returned false. + /// \param[in] replica The replica to check against. + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + /// \param[out] connectionsThatHaveConstructedThisReplica Populated with connection instances that we believe have \a replica allocated + void GetConnectionsThatHaveReplicaConstructed(Replica3 *replica, DataStructures::List &connectionsThatHaveConstructedThisReplica, WorldId worldId=0); + + /// \brief Returns if GetDownloadWasCompleted() returns true for all connections + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + /// \return True when all downloads have been completed + bool GetAllConnectionDownloadsCompleted(WorldId worldId=0) const; + + /// \brief ReplicaManager3 can support multiple worlds, where each world has a separate NetworkIDManager, list of connections, replicas, etc + /// A world with id 0 is created automatically. If you want multiple worlds, use this function, and ReplicaManager3::SetNetworkIDManager() to have a different NetworkIDManager instance per world + /// \param[in] worldId A unique identifier for this world. User-defined + void AddWorld(WorldId worldId); + + /// \brief Deallocate a world added with AddWorld, or the default world with id 0 + /// Deallocating a world will also stop tracking and updating all connections and replicas associated with that world. + /// \param[in] worldId A \a worldId value previously added with AddWorld() + void RemoveWorld(WorldId worldId); + + /// \brief Get one of the WorldId values added with AddWorld() + /// \details WorldId 0 is created by default. Worlds will not necessarily be in the order added with AddWorld(). Edit RemoveWorld() changing RemoveAtIndexFast() to RemoveAtIndex() to preserve order. + /// \param[in] index A value between 0 and GetWorldCount()-1 + /// \return One of the WorldId values added with AddWorld() + WorldId GetWorldIdAtIndex(unsigned int index); + + /// \brief Returns the number of world id specifiers in memory, added with AddWorld() and removed with RemoveWorld() + /// \return The number of worlds added + unsigned int GetWorldCount(void) const; + + /// \details Sets the networkIDManager instance that this plugin relys upon.
    + /// Uses whatever instance is attached to RakPeerInterface if unset.
    + /// To support multiple worlds, you should set it to a different manager for each instance of the plugin + /// \param[in] _networkIDManager The externally allocated NetworkIDManager instance for this plugin to use. + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + void SetNetworkIDManager(NetworkIDManager *_networkIDManager, WorldId worldId=0); + + /// Returns what was passed to SetNetworkIDManager(), or the instance on RakPeerInterface if unset. + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + NetworkIDManager *GetNetworkIDManager(WorldId worldId=0) const; + + /// \details Send a network command to destroy one or more Replica3 instances + /// Usually you won't need this, but use Replica3::BroadcastDestruction() instead. + /// The objects are unaffected locally + /// \param[in] replicaList List of Replica3 objects to tell other systems to destroy. + /// \param[in] exclusionAddress Which system to not send to. UNASSIGNED_SYSTEM_ADDRESS to send to all. + /// \param[in] worldId Used for multiple worlds. World 0 is created automatically by default. See AddWorld() + void BroadcastDestructionList(DataStructures::List &replicaListSource, const SystemAddress &exclusionAddress, WorldId worldId=0); + + /// \internal + /// \details Tell other systems that have this replica to destroy this replica.
    + /// You shouldn't need to call this, as it happens in the Replica3 destructor + void BroadcastDestruction(Replica3 *replica, const SystemAddress &exclusionAddress); + + /// \internal + /// \details Frees internal lists.
    + /// \param[in] deleteWorlds True to also delete the worlds added with AddWorld() + /// Externally allocated pointers are not deallocated + void Clear(bool deleteWorlds=false); + + /// \internal + PRO GetDefaultSendParameters(void) const; + + /// Call interfaces, send data + virtual void Update(void); + + /// \internal + struct RM3World + { + RM3World(); + void Clear(ReplicaManager3 *replicaManager3); + + DataStructures::List connectionList; + DataStructures::List userReplicaList; + WorldId worldId; + NetworkIDManager *networkIDManager; + }; +protected: + virtual PluginReceiveResult OnReceive(Packet *packet); + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); + virtual void OnRakPeerShutdown(void); + virtual void OnDetach(void); + + PluginReceiveResult OnConstruction(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset, WorldId worldId); + PluginReceiveResult OnSerialize(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, RakNet::Time timestamp, unsigned char packetDataOffset, WorldId worldId); + PluginReceiveResult OnDownloadStarted(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset, WorldId worldId); + PluginReceiveResult OnDownloadComplete(Packet *packet, unsigned char *packetData, int packetDataLength, RakNetGUID senderGuid, unsigned char packetDataOffset, WorldId worldId); + + void DeallocReplicaNoBroadcastDestruction(RakNet::Connection_RM3 *connection, RakNet::Replica3 *replica3); + RakNet::Connection_RM3 * PopConnection(unsigned int index, WorldId worldId); + Replica3* GetReplicaByNetworkID(NetworkID networkId, WorldId worldId); + unsigned int ReferenceInternal(RakNet::Replica3 *replica3, WorldId worldId); + + PRO defaultSendParameters; + RakNet::Time autoSerializeInterval; + RakNet::Time lastAutoSerializeOccurance; + bool autoCreateConnections, autoDestroyConnections; + Replica3 *currentlyDeallocatingReplica; + // Set on the first call to ReferenceInternal(), and should never be changed after that + // Used to lookup in Replica3LSRComp. I don't want to rely on GetNetworkID() in case it changes at runtime + uint32_t nextReferenceIndex; + + // For O(1) lookup + RM3World *worldsArray[255]; + // For fast traversal + DataStructures::List worldsList; + + friend class Connection_RM3; +}; + +static const int RM3_NUM_OUTPUT_BITSTREAM_CHANNELS=16; + +/// \ingroup REPLICA_MANAGER_GROUP3 +struct LastSerializationResultBS +{ + RakNet::BitStream bitStream[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS]; + bool indicesToSend[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS]; +}; + +/// Represents the serialized data for an object the last time it was sent. Used by Connection_RM3::OnAutoserializeInterval() and Connection_RM3::SendSerializeIfChanged() +/// \ingroup REPLICA_MANAGER_GROUP3 +struct LastSerializationResult +{ + LastSerializationResult(); + ~LastSerializationResult(); + + /// The replica instance we serialized + /// \note replica MUST be the first member of this struct because I cast from replica to LastSerializationResult in Update() + RakNet::Replica3 *replica; + //bool neverSerialize; +// bool isConstructed; + RakNet::Time whenLastSerialized; + + void AllocBS(void); + LastSerializationResultBS* lastSerializationResultBS; +}; + +/// Parameters passed to Replica3::Serialize() +/// \ingroup REPLICA_MANAGER_GROUP3 +struct SerializeParameters +{ + /// Write your output for serialization here + /// If nothing is written, the serialization will not occur + /// Write to any or all of the NUM_OUTPUT_BITSTREAM_CHANNELS channels available. Channels can hold independent data + RakNet::BitStream outputBitstream[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS]; + + /// Last bitstream we sent for this replica to this system. + /// Read, but DO NOT MODIFY + RakNet::BitStream* lastSentBitstream[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS]; + + /// Set to non-zero to transmit a timestamp with this message. + /// Defaults to 0 + /// Use RakNet::GetTime() for this + RakNet::Time messageTimestamp; + + /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultPacketPriority(). + /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultPacketReliability(). + /// Passed to RakPeerInterface::Send(). Defaults to ReplicaManager3::SetDefaultOrderingChannel(). + PRO pro[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS]; + + /// Passed to RakPeerInterface::Send(). + RakNet::Connection_RM3 *destinationConnection; + + /// For prior serializations this tick, for the same connection, how many bits have we written so far? + /// Use this to limit how many objects you send to update per-tick if desired + BitSize_t bitsWrittenSoFar; + + /// When this object was last serialized to the connection + /// 0 means never + RakNet::Time whenLastSerialized; + + /// Current time, in milliseconds. + /// curTime - whenLastSerialized is how long it has been since this object was last sent + RakNet::Time curTime; +}; + +/// \ingroup REPLICA_MANAGER_GROUP3 +struct DeserializeParameters +{ + RakNet::BitStream serializationBitstream[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS]; + bool bitstreamWrittenTo[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS]; + RakNet::Time timeStamp; + RakNet::Connection_RM3 *sourceConnection; +}; + +/// \ingroup REPLICA_MANAGER_GROUP3 +enum SendSerializeIfChangedResult +{ + SSICR_SENT_DATA, + SSICR_DID_NOT_SEND_DATA, + SSICR_NEVER_SERIALIZE, +}; + +/// \brief Each remote system is represented by Connection_RM3. Used to allocate Replica3 and track which instances have been allocated +/// \details Important function: AllocReplica() - must be overridden to create an object given an identifier for that object, which you define for all objects in your game +/// \ingroup REPLICA_MANAGER_GROUP3 +class RAK_DLL_EXPORT Connection_RM3 +{ +public: + + Connection_RM3(const SystemAddress &_systemAddress, RakNetGUID _guid); + virtual ~Connection_RM3(); + + /// \brief Class factory to create a Replica3 instance, given a user-defined identifier + /// \details Identifier is returned by Replica3::WriteAllocationID() for what type of class to create.
    + /// This is called when you download a replica from another system.
    + /// See Replica3::Dealloc for the corresponding destruction message.
    + /// Return 0 if unable to create the intended object. Note, in that case the other system will still think we have the object and will try to serialize object updates to us. Generally, you should not send objects the other system cannot create.
    + /// \sa Replica3::WriteAllocationID(). + /// Sample implementation:
    + /// {RakNet::RakString typeName; allocationIdBitstream->Read(typeName); if (typeName=="Soldier") return new Soldier; return 0;}
    + /// \param[in] allocationIdBitstream user-defined bitstream uniquely identifying a game object type + /// \param[in] replicaManager3 Instance of ReplicaManager3 that controls this connection + /// \return The new replica instance + virtual Replica3 *AllocReplica(RakNet::BitStream *allocationIdBitstream, ReplicaManager3 *replicaManager3)=0; + + /// \brief Get list of all replicas that are constructed for this connection + /// \param[out] objectsTheyDoHave Destination list. Returned in sorted ascending order, sorted on the value of the Replica3 pointer. + virtual void GetConstructedReplicas(DataStructures::List &objectsTheyDoHave); + + /// Returns true if we think this remote connection has this replica constructed + /// \param[in] replica3 Which replica we are querying + /// \return True if constructed, false othewise + bool HasReplicaConstructed(RakNet::Replica3 *replica); + + /// When a new connection connects, before sending any objects, SerializeOnDownloadStarted() is called + /// \param[out] bitStream Passed to DeserializeOnDownloadStarted() + virtual void SerializeOnDownloadStarted(RakNet::BitStream *bitStream) {(void) bitStream;} + + /// Receives whatever was written in SerializeOnDownloadStarted() + /// \param[in] bitStream Written in SerializeOnDownloadStarted() + virtual void DeserializeOnDownloadStarted(RakNet::BitStream *bitStream) {(void) bitStream;} + + /// When a new connection connects, after constructing and serialization all objects, SerializeOnDownloadComplete() is called + /// \param[out] bitStream Passed to DeserializeOnDownloadComplete() + virtual void SerializeOnDownloadComplete(RakNet::BitStream *bitStream) {(void) bitStream;} + + /// Receives whatever was written in DeserializeOnDownloadComplete() + /// \param[in] bitStream Written in SerializeOnDownloadComplete() + virtual void DeserializeOnDownloadComplete(RakNet::BitStream *bitStream) {(void) bitStream;} + + /// \return The system address passed to the constructor of this object + SystemAddress GetSystemAddress(void) const {return systemAddress;} + + /// \return Returns the RakNetGUID passed to the constructor of this object + RakNetGUID GetRakNetGUID(void) const {return guid;} + + /// \return True if ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE arrived for this connection + bool GetDownloadWasCompleted(void) const {return gotDownloadComplete;} + + /// List of enumerations for how to get the list of valid objects for other systems + enum ConstructionMode + { + /// For every object that does not exist on the remote system, call Replica3::QueryConstruction() every tick. + /// Do not call Replica3::QueryDestruction() + /// Do not call Connection_RM3::QueryReplicaList() + QUERY_REPLICA_FOR_CONSTRUCTION, + + /// For every object that does not exist on the remote system, call Replica3::QueryConstruction() every tick. Based on the call, the object may be sent to the other system. + /// For every object that does exist on the remote system, call Replica3::QueryDestruction() every tick. Based on the call, the object may be deleted on the other system. + /// Do not call Connection_RM3::QueryReplicaList() + QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION, + + /// Do not call Replica3::QueryConstruction() or Replica3::QueryDestruction() + /// Call Connection_RM3::QueryReplicaList() to determine which objects exist on remote systems + /// This can be faster than QUERY_REPLICA_FOR_CONSTRUCTION and QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION for large worlds + /// See GridSectorizer.h under /Source for code that can help with this + QUERY_CONNECTION_FOR_REPLICA_LIST + }; + + /// \brief Return whether or not downloads to our system should all be processed the same tick (call to RakPeer::Receive() ) + /// \details Normally the system will send ID_REPLICA_MANAGER_DOWNLOAD_STARTED, ID_REPLICA_MANAGER_CONSTRUCTION for all downloaded objects, + /// ID_REPLICA_MANAGER_SERIALIZE for each downloaded object, and lastly ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE. + /// This enables the application to show a downloading splash screen on ID_REPLICA_MANAGER_DOWNLOAD_STARTED, a progress bar, and to close the splash screen and activate all objects on ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE + /// However, if the application was not set up for this then it would result in incomplete objects spread out over time, and cause problems + /// If you return true from QueryGroupDownloadMessages(), then these messages will be returned all in one tick, returned only when the download is complete + /// \note ID_REPLICA_MANAGER_DOWNLOAD_STARTED calls the callback DeserializeOnDownloadStarted() + /// \note ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE calls the callback DeserializeOnDownloadComplete() + virtual bool QueryGroupDownloadMessages(void) const {return false;} + + /// \brief Queries how to get the list of objects that exist on remote systems + /// \details The default of calling QueryConstruction for every known object is easy to use, but not efficient, especially for large worlds where many objects are outside of the player's circle of influence.
    + /// QueryDestruction is also not necessarily useful or efficient, as object destruction tends to happen in known cases, and can be accomplished by calling Replica3::BroadcastDestruction() + /// QueryConstructionMode() allows you to specify more efficient algorithms than the default when overriden. + /// \return How to get the list of objects that exist on the remote system. You should always return the same value for a given connection + virtual ConstructionMode QueryConstructionMode(void) const {return QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION;} + + /// \brief Callback used when QueryConstructionMode() returns QUERY_CONNECTION_FOR_REPLICA_LIST + /// \details This advantage of this callback is if that there are many objects that a particular connection does not have, then we do not have to iterate through those + /// objects calling QueryConstruction() for each of them.
    + ///
    + /// See GridSectorizer in the Source directory as a method to find all objects within a certain radius in a fast way.
    + ///
    + /// \param[out] newReplicasToCreate Anything in this list will be created on the remote system + /// \param[out] existingReplicasToDestroy Anything in this list will be destroyed on the remote system + virtual void QueryReplicaList( + DataStructures::List &newReplicasToCreate, + DataStructures::List &existingReplicasToDestroy) {(void) newReplicasToCreate; (void) existingReplicasToDestroy;} + + /// \brief Override which replicas to serialize and in what order for a connection for a ReplicaManager3::Update() cycle + /// \details By default, Connection_RM3 will iterate through queryToSerializeReplicaList and call QuerySerialization() on each Replica in that list + /// queryToSerializeReplicaList is populated in the order in which ReplicaManager3::Reference() is called for those objects. + /// If you write to to \a replicasToSerialize and return true, you can control in what order and for which replicas to call QuerySerialization() + /// Example use case: + /// We have more data to send then the bandwidth supports, so want to prioritize sends. For example enemies shooting are more important than animation effects + /// When QuerySerializationList(), sort objects by priority, and write the list to \a replicasToSerialize, optionally skipping objects with a lower serialization frequency + /// If you hit your bandwidth limit when checking SerializeParameters::bitsWrittenSoFar, you can return RM3SR_DO_NOT_SERIALIZE for all remaining items + /// \note Only replicas written to replicasToSerialize are transmitted. Even if you returned RM3SR_SERIALIZED_ALWAYS a prior ReplicaManager3::Update() cycle, the replica will not be transmitted if it is not in replicasToSerialize + /// \note If you do not know what objects are candidates for serialization, you can use queryToSerializeReplicaList as a source for your filtering or sorting operations + /// \param[in] replicasToSerialize List of replicas to call QuerySerialization() on + /// \return Return true to use replicasToSerialize (replicasToSerialize may be empty if desired). Otherwise return false. + virtual bool QuerySerializationList(DataStructures::List &replicasToSerialize) {(void) replicasToSerialize; return false;} + + /// \internal This is used internally - however, you can also call it manually to send a data update for a remote replica.
    + /// \brief Sends over a serialization update for \a replica.
    + /// NetworkID::GetNetworkID() is written automatically, serializationData is the object data.
    + /// \param[in] replica Which replica to serialize + /// \param[in] serializationData Serialized object data + /// \param[in] timestamp 0 means no timestamp. Otherwise message is prepended with ID_TIMESTAMP + /// \param[in] sendParameters Parameters on how to send + /// \param[in] rakPeer Instance of RakPeerInterface to send on + /// \param[in] worldId Which world, see ReplicaManager3::AddWorld() + /// \param[in] curTime The current time + virtual SendSerializeIfChangedResult SendSerialize(RakNet::Replica3 *replica, bool indicesToSend[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::BitStream serializationData[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::Time timestamp, PRO sendParameters[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::RakPeerInterface *rakPeer, unsigned char worldId, RakNet::Time curTime); + + /// \internal + /// \details Calls Connection_RM3::SendSerialize() if Replica3::Serialize() returns a different result than what is contained in \a lastSerializationResult.
    + /// Used by autoserialization in Connection_RM3::OnAutoserializeInterval() + /// \param[in] lsr Item in the queryToSerializeReplicaList + /// \param[in] sp Controlling parameters over the serialization + /// \param[in] rakPeer Instance of RakPeerInterface to send on + /// \param[in] worldId Which world, see ReplicaManager3::AddWorld() + /// \param[in] curTime The current time + virtual SendSerializeIfChangedResult SendSerializeIfChanged(LastSerializationResult *lsr, SerializeParameters *sp, RakNet::RakPeerInterface *rakPeer, unsigned char worldId, ReplicaManager3 *replicaManager, RakNet::Time curTime); + + /// \internal + /// \brief Given a list of objects that were created and destroyed, serialize and send them to another system. + /// \param[in] newObjects Objects to serialize construction + /// \param[in] deletedObjects Objects to serialize destruction + /// \param[in] sendParameters Controlling parameters over the serialization + /// \param[in] rakPeer Instance of RakPeerInterface to send on + /// \param[in] worldId Which world, see ReplicaManager3::AddWorld() + /// \param[in] replicaManager3 ReplicaManager3 instance + virtual void SendConstruction(DataStructures::List &newObjects, DataStructures::List &deletedObjects, PRO sendParameters, RakNet::RakPeerInterface *rakPeer, unsigned char worldId, ReplicaManager3 *replicaManager3); + + /// \internal + void SendValidation(RakNet::RakPeerInterface *rakPeer, WorldId worldId); + + /// \internal + void AutoConstructByQuery(ReplicaManager3 *replicaManager3, WorldId worldId); + + + // Internal - does the other system have this connection too? Validated means we can now use it + bool isValidated; + // Internal - Used to see if we should send download started + bool isFirstConstruction; + + static int Replica3LSRComp( Replica3 * const &replica3, LastSerializationResult * const &data ); + + // Internal + void ClearDownloadGroup(RakPeerInterface *rakPeerInterface); +protected: + + SystemAddress systemAddress; + RakNetGUID guid; + + /* + Operations: + + Locally reference a new replica: + Add to queryToConstructReplicaList for all objects + + Add all objects to queryToConstructReplicaList + + Download: + Add to constructedReplicaList for connection that send the object to us + Add to queryToSerializeReplicaList for connection that send the object to us + Add to queryToConstructReplicaList for all other connections + + Never construct for this connection: + Remove from queryToConstructReplicaList + + Construct to this connection + Remove from queryToConstructReplicaList + Add to constructedReplicaList for this connection + Add to queryToSerializeReplicaList for this connection + + Serialize: + Iterate through queryToSerializeReplicaList + + Never serialize for this connection + Remove from queryToSerializeReplicaList + + Reference (this system has this object already) + Remove from queryToConstructReplicaList + Add to constructedReplicaList for this connection + Add to queryToSerializeReplicaList for this connection + + Downloaded an existing object + if replica is in queryToConstructReplicaList, OnConstructToThisConnection() + else ignore + + Send destruction from query + Remove from queryToDestructReplicaList + Remove from queryToSerializeReplicaList + Remove from constructedReplicaList + Add to queryToConstructReplicaList + + Do not query destruction again + Remove from queryToDestructReplicaList + */ + void OnLocalReference(Replica3* replica3, ReplicaManager3 *replicaManager); + void OnDereference(Replica3* replica3, ReplicaManager3 *replicaManager); + void OnDownloadFromThisSystem(Replica3* replica3, ReplicaManager3 *replicaManager); + void OnDownloadFromOtherSystem(Replica3* replica3, ReplicaManager3 *replicaManager); + void OnNeverConstruct(unsigned int queryToConstructIdx, ReplicaManager3 *replicaManager); + void OnConstructToThisConnection(unsigned int queryToConstructIdx, ReplicaManager3 *replicaManager); + void OnConstructToThisConnection(Replica3 *replica, ReplicaManager3 *replicaManager); + void OnNeverSerialize(LastSerializationResult *lsr, ReplicaManager3 *replicaManager); + void OnReplicaAlreadyExists(unsigned int queryToConstructIdx, ReplicaManager3 *replicaManager); + void OnDownloadExisting(Replica3* replica3, ReplicaManager3 *replicaManager); + void OnSendDestructionFromQuery(unsigned int queryToDestructIdx, ReplicaManager3 *replicaManager); + void OnDoNotQueryDestruction(unsigned int queryToDestructIdx, ReplicaManager3 *replicaManager); + void ValidateLists(ReplicaManager3 *replicaManager) const; + void SendSerializeHeader(RakNet::Replica3 *replica, RakNet::Time timestamp, RakNet::BitStream *bs, WorldId worldId); + + // The list of objects that our local system and this remote system both have + // Either we sent this object to them, or they sent this object to us + // A given Replica can be either in queryToConstructReplicaList or constructedReplicaList but not both at the same time + DataStructures::OrderedList constructedReplicaList; + + // Objects that we have, but this system does not, and we will query each tick to see if it should be sent to them + // If we do send it to them, the replica is moved to constructedReplicaList + // A given Replica can be either in queryToConstructReplicaList or constructedReplicaList but not both at the same time + DataStructures::List queryToConstructReplicaList; + + // Objects that this system has constructed are added at the same time to queryToSerializeReplicaList + // This list is used to serialize all objects that this system has to this connection + DataStructures::List queryToSerializeReplicaList; + + // Objects that are constructed on this system are also queried if they should be destroyed to this system + DataStructures::List queryToDestructReplicaList; + + // Working lists + DataStructures::List constructedReplicasCulled, destroyedReplicasCulled; + + // This is used if QueryGroupDownloadMessages() returns true when ID_REPLICA_MANAGER_DOWNLOAD_STARTED arrives + // Packets will be gathered and not returned until ID_REPLICA_MANAGER_DOWNLOAD_COMPLETE arrives + bool groupConstructionAndSerialize; + DataStructures::Queue downloadGroup; + + // Stores if we got download complete for this connection + bool gotDownloadComplete; + + friend class ReplicaManager3; +private: + Connection_RM3() {}; + + ConstructionMode constructionMode; +}; + +/// \brief Return codes for Connection_RM3::GetConstructionState() and Replica3::QueryConstruction() +/// \details Indicates what state the object should be in for the remote system +/// \ingroup REPLICA_MANAGER_GROUP3 +enum RM3ConstructionState +{ + /// This object should exist on the remote system. Send a construction message if necessary + /// If the NetworkID is already in use, it will not do anything + /// If it is not in use, it will create the object, and then call DeserializeConstruction + RM3CS_SEND_CONSTRUCTION, + + /// This object should exist on the remote system. + /// The other system already has the object, and the object will never be deleted. + /// This is true of objects that are loaded with the level, for example. + /// Treat it as if it existed, without sending a construction message. + /// Will call Serialize() and SerializeConstructionExisting() to the object on the remote system + RM3CS_ALREADY_EXISTS_REMOTELY, + + /// Same as RM3CS_ALREADY_EXISTS_REMOTELY but does not call SerializeConstructionExisting() + RM3CS_ALREADY_EXISTS_REMOTELY_DO_NOT_CONSTRUCT, + + /// This object will never be sent to the target system + /// This object will never be serialized from this system to the target system + RM3CS_NEVER_CONSTRUCT, + + /// Don't do anything this tick. Will query again next tick + RM3CS_NO_ACTION, + + /// Max enum + RM3CS_MAX, +}; + +/// If this object already exists for this system, should it be removed? +/// \ingroup REPLICA_MANAGER_GROUP3 +enum RM3DestructionState +{ + /// This object should not exist on the remote system. Send a destruction message if necessary. + RM3DS_SEND_DESTRUCTION, + + /// This object will never be destroyed by a per-tick query. Don't call again + RM3DS_DO_NOT_QUERY_DESTRUCTION, + + /// Don't do anything this tick. Will query again next tick + RM3DS_NO_ACTION, + + /// Max enum + RM3DS_MAX, +}; + +/// Return codes when constructing an object +/// \ingroup REPLICA_MANAGER_GROUP3 +enum RM3SerializationResult +{ + /// This object serializes identically no matter who we send to + /// We also send it to every connection (broadcast). + /// Efficient for memory, speed, and bandwidth but only if the object is always broadcast identically. + RM3SR_BROADCAST_IDENTICALLY, + + /// Same as RM3SR_BROADCAST_IDENTICALLY, but assume the object needs to be serialized, do not check with a memcmp + /// Assume the object changed, and serialize it + /// Use this if you know exactly when your object needs to change. Can be faster than RM3SR_BROADCAST_IDENTICALLY. + /// An example of this is if every member variable has an accessor, changing a member sets a flag, and you check that flag in Replica3::QuerySerialization() + /// The opposite of this is RM3SR_DO_NOT_SERIALIZE, in case the object did not change + RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION, + + /// Either this object serializes differently depending on who we send to or we send it to some systems and not others. + /// Inefficient for memory and speed, but efficient for bandwidth + /// However, if you don't know what to return, return this + RM3SR_SERIALIZED_UNIQUELY, + + /// Do not compare against last sent value. Just send even if the data is the same as the last tick + /// If the data is always changing anyway, or you want to send unreliably, this is a good method of serialization + /// Can send unique data per connection if desired. If same data is sent to all connections, use RM3SR_SERIALIZED_ALWAYS_IDENTICALLY for even better performance + /// Efficient for memory and speed, but not necessarily bandwidth + RM3SR_SERIALIZED_ALWAYS, + + /// \deprecated, use RM3SR_BROADCAST_IDENTICALLY_FORCE_SERIALIZATION + RM3SR_SERIALIZED_ALWAYS_IDENTICALLY, + + /// Do not serialize this object this tick, for this connection. Will query again next autoserialize timer + RM3SR_DO_NOT_SERIALIZE, + + /// Never serialize this object for this connection + /// Useful for objects that are downloaded, and never change again + /// Efficient + RM3SR_NEVER_SERIALIZE_FOR_THIS_CONNECTION, + + /// Max enum + RM3SR_MAX, +}; + +/// First pass at topology to see if an object should be serialized +/// \ingroup REPLICA_MANAGER_GROUP3 +enum RM3QuerySerializationResult +{ + /// Call Serialize() to see if this object should be serializable for this connection + RM3QSR_CALL_SERIALIZE, + /// Do not call Serialize() this tick to see if this object should be serializable for this connection + RM3QSR_DO_NOT_CALL_SERIALIZE, + /// Never call Serialize() for this object and connection. This system will not serialize this object for this topology + RM3QSR_NEVER_CALL_SERIALIZE, + /// Max enum + RM3QSR_MAX, +}; + +/// \ingroup REPLICA_MANAGER_GROUP3 +enum RM3ActionOnPopConnection +{ + RM3AOPC_DO_NOTHING, + RM3AOPC_DELETE_REPLICA, + RM3AOPC_DELETE_REPLICA_AND_BROADCAST_DESTRUCTION, + RM3AOPC_MAX, +}; + +/// \ingroup REPLICA_MANAGER_GROUP3 +/// Used for Replica3::QueryConstruction_PeerToPeer() and Replica3::QuerySerialization_PeerToPeer() to describe how the object replicates between hosts +enum Replica3P2PMode +{ + /// The Replica3 instance is constructed and serialized by one system only. + /// Example: Your avatar. No other player serializes or can create your avatar. + R3P2PM_SINGLE_OWNER, + /// The Replica3 instance is constructed and/or serialized by different systems + /// This system is currently in charge of construction and/or serialization + /// Example: A pickup. When an avatar holds it, that avatar controls it. When it is on the ground, the host controls it. + R3P2PM_MULTI_OWNER_CURRENTLY_AUTHORITATIVE, + /// The Replica3 instance is constructed and/or serialized by different systems + /// Another system is in charge of construction and/or serialization, but this system may be in charge at a later time + /// Example: A pickup held by another player. That player sends creation of that object to new connections, and serializes it until it is dropped. + R3P2PM_MULTI_OWNER_NOT_CURRENTLY_AUTHORITATIVE, + /// The Replica3 instance is a static object (already exists on the remote system). + /// This system is currently in charge of construction and/or serialization + R3P2PM_STATIC_OBJECT_CURRENTLY_AUTHORITATIVE, + /// The Replica3 instance is a static object (already exists on the remote system). + /// Another system is in charge of construction and/or serialization, but this system may be in charge at a later time + R3P2PM_STATIC_OBJECT_NOT_CURRENTLY_AUTHORITATIVE, + +}; + +/// \brief Base class for your replicated objects for the ReplicaManager3 system. +/// \details To use, derive your class, or a member of your class, from Replica3.
    +/// \ingroup REPLICA_MANAGER_GROUP3 +class RAK_DLL_EXPORT Replica3 : public NetworkIDObject +{ +public: + Replica3(); + + /// Before deleting a local instance of Replica3, call Replica3::BroadcastDestruction() for the deletion notification to go out on the network. + /// It is not necessary to call ReplicaManager3::Dereference(), as this happens automatically in the destructor + virtual ~Replica3(); + + /// \brief Write a unique identifer that can be read on a remote system to create an object of this same class. + /// \details The value written to \a allocationIdBitstream will be passed to Connection_RM3::AllocReplica().
    + /// Sample implementation:
    + /// {allocationIdBitstream->Write(RakNet::RakString("Soldier");}
    + /// \param[out] allocationIdBitstream Bitstream for the user to write to, to identify this class + virtual void WriteAllocationID(RakNet::Connection_RM3 *destinationConnection, RakNet::BitStream *allocationIdBitstream) const=0; + + /// \brief Ask if this object, which does not exist on \a destinationConnection should (now) be sent to that system. + /// \details If ReplicaManager3::QueryConstructionMode() returns QUERY_CONNECTION_FOR_REPLICA_LIST or QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION (default), + /// then QueyrConstruction() is called once per tick from ReplicaManager3::Update() to determine if an object should exist on a given system.
    + /// Based on the return value, a network message may be sent to the other system to create the object.
    + /// If QueryConstructionMode() is overriden to return QUERY_CONNECTION_FOR_REPLICA_LIST, this function is unused.
    + /// \note Defaults are provided: QueryConstruction_PeerToPeer(), QueryConstruction_ServerConstruction(), QueryConstruction_ClientConstruction(). Return one of these functions for a working default for the relevant topology. + /// \param[in] destinationConnection Which system we will send to + /// \param[in] replicaManager3 Plugin instance for this Replica3 + /// \return What action to take + virtual RM3ConstructionState QueryConstruction(RakNet::Connection_RM3 *destinationConnection, ReplicaManager3 *replicaManager3)=0; + + /// \brief Ask if this object, which does exist on \a destinationConnection should be removed from the remote system + /// \details If ReplicaManager3::QueryConstructionMode() returns QUERY_REPLICA_FOR_CONSTRUCTION_AND_DESTRUCTION (default), + /// then QueryDestruction() is called once per tick from ReplicaManager3::Update() to determine if an object that exists on a remote system should be destroyed for a given system.
    + /// Based on the return value, a network message may be sent to the other system to destroy the object.
    + /// Note that you can also destroy objects with BroadcastDestruction(), so this function is not useful unless you plan to delete objects for only a particular connection.
    + /// If QueryConstructionMode() is overriden to return QUERY_CONNECTION_FOR_REPLICA_LIST, this function is unused.
    + /// \param[in] destinationConnection Which system we will send to + /// \param[in] replicaManager3 Plugin instance for this Replica3 + /// \return What action to take. Only RM3CS_SEND_DESTRUCTION does anything at this time. + virtual RM3DestructionState QueryDestruction(RakNet::Connection_RM3 *destinationConnection, ReplicaManager3 *replicaManager3) {(void) destinationConnection; (void) replicaManager3; return RM3DS_DO_NOT_QUERY_DESTRUCTION;} + + /// \brief We're about to call DeserializeConstruction() on this Replica3. If QueryRemoteConstruction() returns false, this object is deleted instead. + /// \details By default, QueryRemoteConstruction_ServerConstruction() does not allow clients to create objects. The client will get Replica3::DeserializeConstructionRequestRejected().
    + /// If you want the client to be able to potentially create objects for client/server, override accordingly.
    + /// Other variants of QueryRemoteConstruction_* just return true. + /// \note Defaults are provided: QueryRemoteConstruction_PeerToPeer(), QueryRemoteConstruction_ServerConstruction(), QueryRemoteConstruction_ClientConstruction(). Return one of these functions for a working default for the relevant topology. + /// \param[in] sourceConnection Which system sent us the object creation request message. + /// \return True to allow the object to pass onto DeserializeConstruction() (where it may also be rejected), false to immediately reject the remote construction request + virtual bool QueryRemoteConstruction(RakNet::Connection_RM3 *sourceConnection)=0; + + /// \brief We got a message from a connection to destroy this replica + /// Return true to automatically relay the destruction message to all our other connections + /// For a client in client/server, it does not matter what this funtion returns + /// For a server in client/server, this should normally return true + /// For a peer in peer to peer, you can normally return false since the original destroying peer would have told all other peers about the destruction + /// If a system gets a destruction command for an object that was already destroyed, the destruction message is ignored + virtual bool QueryRelayDestruction(Connection_RM3 *sourceConnection) const {(void) sourceConnection; return true;} + + /// \brief Write data to be sent only when the object is constructed on a remote system. + /// \details SerializeConstruction is used to write out data that you need to create this object in the context of your game, such as health, score, name. Use it for data you only need to send when the object is created.
    + /// After SerializeConstruction() is called, Serialize() will be called immediately thereafter. However, they are sent in different messages, so Serialize() may arrive a later frame than SerializeConstruction() + /// For that reason, the object should be valid after a call to DeserializeConstruction() for at least a short time.
    + /// \note The object's NetworkID and allocation id are handled by the system automatically, you do not need to write these values to \a constructionBitstream + /// \param[out] constructionBitstream Destination bitstream to write your data to + /// \param[in] destinationConnection System that will receive this network message. + virtual void SerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection)=0; + + /// \brief Read data written by Replica3::SerializeConstruction() + /// \details Reads whatever data was written to \a constructionBitstream in Replica3::SerializeConstruction() + /// \param[out] constructionBitstream Bitstream written to in Replica3::SerializeConstruction() + /// \param[in] sourceConnection System that sent us this network message. + /// \return true to accept construction of the object. false to reject, in which case the object will be deleted via Replica3::DeallocReplica() + virtual bool DeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection)=0; + + /// Same as SerializeConstruction(), but for an object that already exists on the remote system. + /// Used if you return RM3CS_ALREADY_EXISTS_REMOTELY from QueryConstruction + virtual void SerializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {(void) constructionBitstream; (void) destinationConnection;}; + + /// Same as DeserializeConstruction(), but for an object that already exists on the remote system. + /// Used if you return RM3CS_ALREADY_EXISTS_REMOTELY from QueryConstruction + virtual void DeserializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {(void) constructionBitstream; (void) sourceConnection;}; + + /// \brief Write extra data to send with the object deletion event, if desired + /// \details Replica3::SerializeDestruction() will be called to write any object destruction specific data you want to send with this event. + /// \a destructionBitstream can be read in DeserializeDestruction() + /// \param[out] destructionBitstream Bitstream for you to write to + /// \param[in] destinationConnection System that will receive this network message. + virtual void SerializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *destinationConnection)=0; + + /// \brief Read data written by Replica3::SerializeDestruction() + /// \details Return true to delete the object. BroadcastDestruction() will be called automatically, followed by ReplicaManager3::Dereference.
    + /// Return false to not delete it. If you delete it at a later point, you are responsible for calling BroadcastDestruction() yourself. + virtual bool DeserializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *sourceConnection)=0; + + /// \brief The system is asking what to do with this replica when the connection is dropped + /// \details Return QueryActionOnPopConnection_Client, QueryActionOnPopConnection_Server, or QueryActionOnPopConnection_PeerToPeer + virtual RakNet::RM3ActionOnPopConnection QueryActionOnPopConnection(RakNet::Connection_RM3 *droppedConnection) const=0; + + /// Notification called for each of our replicas when a connection is popped + virtual void OnPoppedConnection(RakNet::Connection_RM3 *droppedConnection) {(void) droppedConnection;} + + /// \brief Override with {delete this;} + /// \details + ///
      + ///
    1. Got a remote message to delete this object which passed DeserializeDestruction(), OR + ///
    2. ReplicaManager3::SetAutoManageConnections() was called autoDestroy true (which is the default setting), and a remote system that owns this object disconnected) OR + /// <\OL> + ///
      + /// Override with {delete this;} to actually delete the object (and any other processing you wish).
      + /// If you don't want to delete the object, just do nothing, however, the system will not know this. You may wish to call Dereference() if the object should no longer be networked, but remain in memory. You are responsible for deleting it yoruself later.
      + /// destructionBitstream may be 0 if the object was deleted locally + virtual void DeallocReplica(RakNet::Connection_RM3 *sourceConnection)=0; + + /// \brief Implement with QuerySerialization_ClientSerializable(), QuerySerialization_ServerSerializable(), or QuerySerialization_PeerToPeer() + /// \details QuerySerialization() is a first pass query to check if a given object should serializable to a given system. The intent is that the user implements with one of the defaults for client, server, or peer to peer.
      + /// Without this function, a careless implementation would serialize an object anytime it changed to all systems. This would give you feedback loops as the sender gets the same message back from the recipient it just sent to.
      + /// If more than one system can serialize the same object then you will need to override to return true, and control the serialization result from Replica3::Serialize(). Be careful not to send back the same data to the system that just sent to you! + /// \return True to allow calling Replica3::Serialize() for this connection, false to not call. + virtual RakNet::RM3QuerySerializationResult QuerySerialization(RakNet::Connection_RM3 *destinationConnection)=0; + + /// \brief Called for each replica owned by the user, once per Serialization tick, before Serialize() is called. + /// If you want to do some kind of operation on the Replica objects that you own, just before Serialization(), then overload this function + virtual void OnUserReplicaPreSerializeTick(void) {} + + /// \brief Serialize our class to a bitstream + /// \details User should implement this function to write the contents of this class to SerializationParamters::serializationBitstream.
      + /// If data only needs to be written once, you can write it to SerializeConstruction() instead for efficiency.
      + /// Transmitted over the network if it changed from the last time we called Serialize().
      + /// Called every time the time interval to ReplicaManager3::SetAutoSerializeInterval() elapses and ReplicaManager3::Update is subsequently called. + /// \param[in/out] serializeParameters Parameters controlling the serialization, including destination bitstream to write to + /// \return Whether to serialize, and if so, how to optimize the results + virtual RM3SerializationResult Serialize(RakNet::SerializeParameters *serializeParameters)=0; + + /// \brief Called when the class is actually transmitted via Serialize() + /// \details Use to track how much bandwidth this class it taking + virtual void OnSerializeTransmission(RakNet::BitStream *bitStream, RakNet::Connection_RM3 *destinationConnection, BitSize_t bitsPerChannel[RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::Time curTime) {(void) bitStream; (void) destinationConnection; (void) bitsPerChannel; (void) curTime;} + + /// \brief Read what was written in Serialize() + /// \details Reads the contents of the class from SerializationParamters::serializationBitstream.
      + /// Called whenever Serialize() is called with different data from the last send. + /// \param[in] serializationBitstream Bitstream passed to Serialize() + /// \param[in] timeStamp 0 if unused, else contains the time the message originated on the remote system + /// \param[in] sourceConnection Which system sent to us + virtual void Deserialize(RakNet::DeserializeParameters *deserializeParameters)=0; + + /// \brief Called after SerializeConstruction completes for all objects in a given update tick.
      + /// Writes to PostDeserializeConstruction(), which is called after all objects are created for a given Construction tick(). + /// Override to send data to PostDeserializeConstruction(), such as the NetworkID of other objects to resolve pointers to + virtual void PostSerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {(void) constructionBitstream; (void) destinationConnection;} + + /// Called after DeserializeConstruction completes for all objects in a given update tick.
      + /// This is used to resolve dependency chains, where two objects would refer to each other in DeserializeConstruction, yet one had not been constructed yet + /// In PostDeserializeConstruction(), you know that all objects have already been created, so can resolve NetworkIDs to pointers safely. + /// You can also use it to trigger some sort of event when you know the object has completed deserialization. + /// \param[in] constructionBitstream BitStream written in PostSerializeConstruction() + /// \param[in] sourceConnection System that sent us this network message. + virtual void PostDeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {(void) constructionBitstream; (void) sourceConnection;} + + /// Same as PostSerializeConstruction(), but for objects that returned RM3CS_ALREADY_EXISTS_REMOTELY from QueryConstruction + virtual void PostSerializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {(void) constructionBitstream; (void) destinationConnection;} + + /// Same as PostDeserializeConstruction(), but for objects that returned RM3CS_ALREADY_EXISTS_REMOTELY from QueryConstruction + virtual void PostDeserializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {(void) constructionBitstream; (void) sourceConnection;} + + /// Called after DeserializeDestruction completes for the object successfully, but obviously before the object is deleted.
      + /// Override to trigger some sort of event when you know the object has completed destruction. + /// \param[in] sourceConnection System that sent us this network message. + virtual void PreDestruction(RakNet::Connection_RM3 *sourceConnection) {(void) sourceConnection;} + + /// \brief Default call for QueryConstruction(). + /// \details Both the client and the server is allowed to create this object. The network topology is client/server + /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction() + /// \param[in] isThisTheServer True if this system is the server, false if not. + virtual RM3ConstructionState QueryConstruction_ClientConstruction(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer); + + /// Default call for QueryRemoteConstruction(). + /// \details Both the client and the server is allowed to create this object. The network topology is client/server + /// The code means on the client or the server, allow creation of Replica3 instances + /// \param[in] sourceConnection destinationConnection parameter passed to QueryConstruction() + /// \param[in] isThisTheServer True if this system is the server, false if not. + virtual bool QueryRemoteConstruction_ClientConstruction(RakNet::Connection_RM3 *sourceConnection, bool isThisTheServer); + + /// \brief Default call for QueryConstruction(). + /// \details Only the server is allowed to create this object. The network topology is client/server + /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction() + /// \param[in] isThisTheServer True if this system is the server, false if not. + virtual RM3ConstructionState QueryConstruction_ServerConstruction(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer); + + /// \brief Default call for QueryRemoteConstruction(). Allow the server to create this object, but not the client. + /// \details Only the server is allowed to create this object. The network topology is client/server + /// The code means if this is the server, and I got a command to create a Replica3 to ignore it. If this is the client, to allow it. + /// \param[in] sourceConnection destinationConnection parameter passed to QueryConstruction() + /// \param[in] isThisTheServer True if this system is the server, false if not. + virtual bool QueryRemoteConstruction_ServerConstruction(RakNet::Connection_RM3 *sourceConnection, bool isThisTheServer); + + /// \brief Default call for QueryConstruction(). + /// \details All clients are allowed to create all objects. The object is not relayed when remotely created + /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction() + /// \param[in] p2pMode If controlled only by this system ever, pass R3P2PM_SINGLE_OWNER. Otherwise pass R3P2PM_MULTI_OWNER_CURRENTLY_AUTHORITATIVE or R3P2PM_MULTI_OWNER_NOT_CURRENTLY_AUTHORITATIVE + virtual RM3ConstructionState QueryConstruction_PeerToPeer(RakNet::Connection_RM3 *destinationConnection, Replica3P2PMode p2pMode=R3P2PM_SINGLE_OWNER); + /// \brief Default call for QueryRemoteConstruction(). + /// \details All clients are allowed to create all objects. The object is not relayed when remotely created + /// \param[in] sourceConnection destinationConnection parameter passed to QueryConstruction() + virtual bool QueryRemoteConstruction_PeerToPeer(RakNet::Connection_RM3 *sourceConnection); + + /// \brief Default call for QuerySerialization(). + /// \details Use if the values you are serializing are generated by the client that owns the object. The serialization will be relayed through the server to the other clients. + /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction() + /// \param[in] isThisTheServer True if this system is the server, false if not. + virtual RakNet::RM3QuerySerializationResult QuerySerialization_ClientSerializable(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer); + /// \brief Default call for QuerySerialization(). + /// \details Use if the values you are serializing are generated only by the server. The serialization will be sent to all clients, but the clients will not send back to the server. + /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction() + /// \param[in] isThisTheServer True if this system is the server, false if not. + virtual RakNet::RM3QuerySerializationResult QuerySerialization_ServerSerializable(RakNet::Connection_RM3 *destinationConnection, bool isThisTheServer); + /// \brief Default call for QuerySerialization(). + /// \details Use if the values you are serializing are on a peer to peer network. The peer that owns the object will send to all. Remote peers will not send. + /// \param[in] destinationConnection destinationConnection parameter passed to QueryConstruction() + /// \param[in] p2pMode If controlled only by this system ever, pass R3P2PM_SINGLE_OWNER. Otherwise pass R3P2PM_MULTI_OWNER_CURRENTLY_AUTHORITATIVE or R3P2PM_MULTI_OWNER_NOT_CURRENTLY_AUTHORITATIVE + virtual RakNet::RM3QuerySerializationResult QuerySerialization_PeerToPeer(RakNet::Connection_RM3 *destinationConnection, Replica3P2PMode p2pMode=R3P2PM_SINGLE_OWNER); + + /// Default: If we are a client, and the connection is lost, delete the server's objects + virtual RM3ActionOnPopConnection QueryActionOnPopConnection_Client(RakNet::Connection_RM3 *droppedConnection) const; + /// Default: If we are a server, and the connection is lost, delete the client's objects and broadcast the destruction + virtual RM3ActionOnPopConnection QueryActionOnPopConnection_Server(RakNet::Connection_RM3 *droppedConnection) const; + /// Default: If we are a peer, and the connection is lost, delete the peer's objects + virtual RM3ActionOnPopConnection QueryActionOnPopConnection_PeerToPeer(RakNet::Connection_RM3 *droppedConnection) const; + + /// Call to send a network message to delete this object on other systems.
      + /// Call it before deleting the object + virtual void BroadcastDestruction(void); + + /// creatingSystemGUID is set the first time Reference() is called, or if we get the object from another system + /// \return System that originally created this object + RakNetGUID GetCreatingSystemGUID(void) const; + + /// \return If ReplicaManager3::Reference() was called on this object. + bool WasReferenced(void) const {return replicaManager!=0;} + + /// GUID of the system that first called Reference() on this object. + /// Transmitted automatically when the object is constructed + RakNetGUID creatingSystemGUID; + /// GUID of the system that caused the item to send a deletion command over the network + RakNetGUID deletingSystemGUID; + + /// \internal + /// ReplicaManager3 plugin associated with this object + ReplicaManager3 *replicaManager; + + LastSerializationResultBS lastSentSerialization; + bool forceSendUntilNextUpdate; + LastSerializationResult *lsr; + uint32_t referenceIndex; +}; + +/// \brief Use Replica3 through composition instead of inheritance by containing an instance of this templated class +/// Calls to parent class for all functions +/// Parent class must still define and functions though! +/// \pre Parent class must call SetCompositeOwner() on this object +template +class RAK_DLL_EXPORT Replica3Composite : public Replica3 +{ +protected: + parent_type *r3CompositeOwner; +public: + void SetCompositeOwner(parent_type *p) {r3CompositeOwner=p;} + parent_type* GetCompositeOwner(void) const {return r3CompositeOwner;}; + virtual void WriteAllocationID(RakNet::Connection_RM3 *destinationConnection, RakNet::BitStream *allocationIdBitstream) const {r3CompositeOwner->WriteAllocationID(destinationConnection, allocationIdBitstream);} + virtual RakNet::RM3ConstructionState QueryConstruction(RakNet::Connection_RM3 *destinationConnection, RakNet::ReplicaManager3 *replicaManager3) {return r3CompositeOwner->QueryConstruction(destinationConnection, replicaManager3);} + virtual RakNet::RM3DestructionState QueryDestruction(RakNet::Connection_RM3 *destinationConnection, RakNet::ReplicaManager3 *replicaManager3) {return r3CompositeOwner->QueryDestruction(destinationConnection, replicaManager3);} + virtual bool QueryRemoteConstruction(RakNet::Connection_RM3 *sourceConnection) {return r3CompositeOwner->QueryRemoteConstruction(sourceConnection);} + virtual bool QueryRelayDestruction(RakNet::Connection_RM3 *sourceConnection) const {return r3CompositeOwner->QueryRelayDestruction(sourceConnection);} + virtual void SerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->SerializeConstruction(constructionBitstream, destinationConnection);} + virtual bool DeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {return r3CompositeOwner->DeserializeConstruction(constructionBitstream, sourceConnection);} + virtual void SerializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->SerializeConstructionExisting(constructionBitstream, destinationConnection);} + virtual void DeserializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->DeserializeConstructionExisting(constructionBitstream, sourceConnection);} + virtual void SerializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->SerializeDestruction(destructionBitstream, destinationConnection);} + virtual bool DeserializeDestruction(RakNet::BitStream *destructionBitstream, RakNet::Connection_RM3 *sourceConnection) {return r3CompositeOwner->DeserializeDestruction(destructionBitstream, sourceConnection);} + virtual RakNet::RM3ActionOnPopConnection QueryActionOnPopConnection(RakNet::Connection_RM3 *droppedConnection) const {return r3CompositeOwner->QueryActionOnPopConnection(droppedConnection);} + virtual void OnPoppedConnection(RakNet::Connection_RM3 *droppedConnection) {r3CompositeOwner->OnPoppedConnection(droppedConnection);} + virtual void DeallocReplica(RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->DeallocReplica(sourceConnection);} + virtual RakNet::RM3QuerySerializationResult QuerySerialization(RakNet::Connection_RM3 *destinationConnection) {return r3CompositeOwner->QuerySerialization(destinationConnection);} + virtual void OnUserReplicaPreSerializeTick(void) {r3CompositeOwner->OnUserReplicaPreSerializeTick();} + virtual RakNet::RM3SerializationResult Serialize(RakNet::SerializeParameters *serializeParameters) {return r3CompositeOwner->Serialize(serializeParameters);} + virtual void OnSerializeTransmission(RakNet::BitStream *bitStream, RakNet::Connection_RM3 *destinationConnection, RakNet::BitSize_t bitsPerChannel[RakNet::RM3_NUM_OUTPUT_BITSTREAM_CHANNELS], RakNet::Time curTime) {r3CompositeOwner->OnSerializeTransmission(bitStream, destinationConnection, bitsPerChannel, curTime);} + virtual void Deserialize(RakNet::DeserializeParameters *deserializeParameters) {r3CompositeOwner->Deserialize(deserializeParameters);} + virtual void PostSerializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->PostSerializeConstruction(constructionBitstream, destinationConnection);} + virtual void PostDeserializeConstruction(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->PostDeserializeConstruction(constructionBitstream, sourceConnection);} + virtual void PostSerializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *destinationConnection) {r3CompositeOwner->PostSerializeConstructionExisting(constructionBitstream, destinationConnection);} + virtual void PostDeserializeConstructionExisting(RakNet::BitStream *constructionBitstream, RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->PostDeserializeConstructionExisting(constructionBitstream, sourceConnection);} + virtual void PreDestruction(RakNet::Connection_RM3 *sourceConnection) {r3CompositeOwner->PreDestruction(sourceConnection);} +}; + +} // namespace RakNet + + +#endif + diff --git a/Source/Router2.h b/Source/Router2.h index 45cbbf3ea..84c011ae1 100644 --- a/Source/Router2.h +++ b/Source/Router2.h @@ -1,203 +1,201 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Router2 plugin. Allows you to connect to a system by routing packets through another system that is connected to both you and the destination. Useful for getting around NATs. -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_Router2==1 && _RAKNET_SUPPORT_UDPForwarder==1 - -#ifndef __ROUTER_2_PLUGIN_H -#define __ROUTER_2_PLUGIN_H - -#include "RakNetTypes.h" -#include "PluginInterface2.h" -#include "PacketPriority.h" -#include "Export.h" -#include "UDPForwarder.h" -#include "MessageIdentifiers.h" -#include "DS_List.h" -#include "SimpleMutex.h" - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; - -struct Router2DebugInterface -{ - Router2DebugInterface() {} - virtual ~Router2DebugInterface() {} - virtual void ShowFailure(const char *message); - virtual void ShowDiagnostic(const char *message); -}; - -/// \defgroup ROUTER_2_GROUP Router2 -/// \brief Part of the NAT punchthrough solution, allowing you to connect to systems by routing through a shared connection. -/// \details Router2 routes datagrams between two systems that are not directly connected by using the bandwidth of a third system, to which the other two systems were connected -/// It is of benefit when a fully connected mesh topology is desired, but could not be completely established due to routers and/or firewalls -/// As the system address of a remote system will be the system address of the intermediary, it is necessary to use the RakNetGUID object to refer to systems, including with other plugins -/// \ingroup PLUGINS_GROUP - -/// \ingroup ROUTER_2_GROUP -/// \brief Class interface for the Router2 system -/// \details -class RAK_DLL_EXPORT Router2 : public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(Router2) - - Router2(); - virtual ~Router2(); - - /// Sets the socket family to use, either IPV4 or IPV6 - /// \param[in] socketFamily For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC. - void SetSocketFamily(unsigned short _socketFamily); - - /// \brief Query all connected systems to connect through them to a third system. - /// System will return ID_ROUTER_2_FORWARDING_NO_PATH if unable to connect. - /// Else you will get ID_ROUTER_2_FORWARDING_ESTABLISHED - /// - /// On ID_ROUTER_2_FORWARDING_ESTABLISHED, EstablishRouting as follows: - /// - /// RakNet::BitStream bs(packet->data, packet->length, false); - /// bs.IgnoreBytes(sizeof(MessageID)); - /// RakNetGUID endpointGuid; - /// bs.Read(endpointGuid); - /// unsigned short sourceToDestPort; - /// bs.Read(sourceToDestPort); - /// char ipAddressString[32]; - /// packet->systemAddress.ToString(false, ipAddressString); - /// rakPeerInterface->EstablishRouting(ipAddressString, sourceToDestPort, 0,0); - /// - /// \note The SystemAddress for a connection should not be used - always use RakNetGuid as the address can change at any time. - /// When the address changes, you will get ID_ROUTER_2_REROUTED - void EstablishRouting(RakNetGUID endpointGuid); - - /// Set the maximum number of bidirectional connections this system will support - /// Defaults to 0 - void SetMaximumForwardingRequests(int max); - - /// For testing and debugging - void SetDebugInterface(Router2DebugInterface *_debugInterface); - - /// Get the pointer passed to SetDebugInterface() - Router2DebugInterface *GetDebugInterface(void) const; - - // -------------------------------------------------------------------------------------------- - // Packet handling functions - // -------------------------------------------------------------------------------------------- - virtual PluginReceiveResult OnReceive(Packet *packet); - virtual void Update(void); - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason); - virtual void OnRakPeerShutdown(void); - - - enum Router2RequestStates - { - R2RS_REQUEST_STATE_QUERY_FORWARDING, - REQUEST_STATE_REQUEST_FORWARDING, - }; - - struct ConnectionRequestSystem - { - RakNetGUID guid; - int pingToEndpoint; - unsigned short usedForwardingEntries; - }; - - struct ConnnectRequest - { - ConnnectRequest(); - ~ConnnectRequest(); - - DataStructures::List connectionRequestSystems; - SimpleMutex connectionRequestSystemsMutex; - Router2RequestStates requestState; - RakNet::TimeMS pingTimeout; - RakNetGUID endpointGuid; - RakNetGUID lastRequestedForwardingSystem; - bool returnConnectionLostOnFailure; - unsigned int GetGuidIndex(RakNetGUID guid); - }; - - unsigned int GetConnectionRequestIndex(RakNetGUID endpointGuid); - - struct MiniPunchRequest - { - RakNetGUID endpointGuid; - SystemAddress endpointAddress; - bool gotReplyFromEndpoint; - RakNetGUID sourceGuid; - SystemAddress sourceAddress; - bool gotReplyFromSource; - RakNet::TimeMS timeout; - RakNet::TimeMS nextAction; - unsigned short forwardingPort; - __UDPSOCKET__ forwardingSocket; - }; - - struct ForwardedConnection - { - RakNetGUID endpointGuid; - RakNetGUID intermediaryGuid; - SystemAddress intermediaryAddress; - bool returnConnectionLostOnFailure; - bool weInitiatedForwarding; - }; - -protected: - - bool UpdateForwarding(ConnnectRequest* connectionRequest); - void RemoveConnectionRequest(unsigned int connectionRequestIndex); - void RequestForwarding(ConnnectRequest* connectionRequest); - void OnQueryForwarding(Packet *packet); - void OnQueryForwardingReply(Packet *packet); - void OnRequestForwarding(Packet *packet); - void OnRerouted(Packet *packet); - void OnMiniPunchReply(Packet *packet); - void OnMiniPunchReplyBounce(Packet *packet); - bool OnForwardingSuccess(Packet *packet); - int GetLargestPingAmongConnectedSystems(void) const; - void ReturnToUser(MessageID messageId, RakNetGUID endpointGuid, const SystemAddress &systemAddress, bool wasGeneratedLocally); - bool ConnectInternal(RakNetGUID endpointGuid, bool returnConnectionLostOnFailure); - - UDPForwarder *udpForwarder; - int maximumForwardingRequests; - SimpleMutex connectionRequestsMutex, miniPunchesInProgressMutex, forwardedConnectionListMutex; - DataStructures::List connectionRequests; - DataStructures::List miniPunchesInProgress; - // Forwarding we have initiated - DataStructures::List forwardedConnectionList; - - void ClearConnectionRequests(void); - void ClearMinipunches(void); - void ClearForwardedConnections(void); - void ClearAll(void); - int ReturnFailureOnCannotForward(RakNetGUID sourceGuid, RakNetGUID endpointGuid); - void SendFailureOnCannotForward(RakNetGUID sourceGuid, RakNetGUID endpointGuid); - void SendForwardingSuccess(MessageID messageId, RakNetGUID sourceGuid, RakNetGUID endpointGuid, unsigned short sourceToDstPort); - void SendOOBFromRakNetPort(OutOfBandIdentifiers oob, BitStream *extraData, SystemAddress sa); - void SendOOBFromSpecifiedSocket(OutOfBandIdentifiers oob, SystemAddress sa, __UDPSOCKET__ socket); - void SendOOBMessages(MiniPunchRequest *mpr); - - Router2DebugInterface *debugInterface; - unsigned short socketFamily; -}; - -} - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Router2 plugin. Allows you to connect to a system by routing packets through another system that is connected to both you and the destination. Useful for getting around NATs. +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_Router2==1 && _RAKNET_SUPPORT_UDPForwarder==1 + +#pragma once + +#include "RakNetTypes.h" +#include "PluginInterface2.h" +#include "PacketPriority.h" +#include "Export.h" +#include "UDPForwarder.h" +#include "MessageIdentifiers.h" +#include "DS_List.h" +#include "SimpleMutex.h" + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; + +struct Router2DebugInterface +{ + Router2DebugInterface() {} + virtual ~Router2DebugInterface() {} + virtual void ShowFailure(const char *message); + virtual void ShowDiagnostic(const char *message); +}; + +/// \defgroup ROUTER_2_GROUP Router2 +/// \brief Part of the NAT punchthrough solution, allowing you to connect to systems by routing through a shared connection. +/// \details Router2 routes datagrams between two systems that are not directly connected by using the bandwidth of a third system, to which the other two systems were connected +/// It is of benefit when a fully connected mesh topology is desired, but could not be completely established due to routers and/or firewalls +/// As the system address of a remote system will be the system address of the intermediary, it is necessary to use the RakNetGUID object to refer to systems, including with other plugins +/// \ingroup PLUGINS_GROUP + +/// \ingroup ROUTER_2_GROUP +/// \brief Class interface for the Router2 system +/// \details +class RAK_DLL_EXPORT Router2 : public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(Router2) + + Router2(); + virtual ~Router2(); + + /// Sets the socket family to use, either IPV4 or IPV6 + /// \param[in] socketFamily For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC. + void SetSocketFamily(unsigned short _socketFamily); + + /// \brief Query all connected systems to connect through them to a third system. + /// System will return ID_ROUTER_2_FORWARDING_NO_PATH if unable to connect. + /// Else you will get ID_ROUTER_2_FORWARDING_ESTABLISHED + /// + /// On ID_ROUTER_2_FORWARDING_ESTABLISHED, EstablishRouting as follows: + /// + /// RakNet::BitStream bs(packet->data, packet->length, false); + /// bs.IgnoreBytes(sizeof(MessageID)); + /// RakNetGUID endpointGuid; + /// bs.Read(endpointGuid); + /// unsigned short sourceToDestPort; + /// bs.Read(sourceToDestPort); + /// char ipAddressString[32]; + /// packet->systemAddress.ToString(false, ipAddressString); + /// rakPeerInterface->EstablishRouting(ipAddressString, sourceToDestPort, 0,0); + /// + /// \note The SystemAddress for a connection should not be used - always use RakNetGuid as the address can change at any time. + /// When the address changes, you will get ID_ROUTER_2_REROUTED + void EstablishRouting(RakNetGUID endpointGuid); + + /// Set the maximum number of bidirectional connections this system will support + /// Defaults to 0 + void SetMaximumForwardingRequests(int max); + + /// For testing and debugging + void SetDebugInterface(Router2DebugInterface *_debugInterface); + + /// Get the pointer passed to SetDebugInterface() + Router2DebugInterface *GetDebugInterface(void) const; + + // -------------------------------------------------------------------------------------------- + // Packet handling functions + // -------------------------------------------------------------------------------------------- + virtual PluginReceiveResult OnReceive(Packet *packet); + virtual void Update(void); + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason); + virtual void OnRakPeerShutdown(void); + + + enum Router2RequestStates + { + R2RS_REQUEST_STATE_QUERY_FORWARDING, + REQUEST_STATE_REQUEST_FORWARDING, + }; + + struct ConnectionRequestSystem + { + RakNetGUID guid; + int pingToEndpoint; + unsigned short usedForwardingEntries; + }; + + struct ConnnectRequest + { + ConnnectRequest(); + ~ConnnectRequest(); + + DataStructures::List connectionRequestSystems; + SimpleMutex connectionRequestSystemsMutex; + Router2RequestStates requestState; + RakNet::TimeMS pingTimeout; + RakNetGUID endpointGuid; + RakNetGUID lastRequestedForwardingSystem; + bool returnConnectionLostOnFailure; + unsigned int GetGuidIndex(RakNetGUID guid); + }; + + unsigned int GetConnectionRequestIndex(RakNetGUID endpointGuid); + + struct MiniPunchRequest + { + RakNetGUID endpointGuid; + SystemAddress endpointAddress; + bool gotReplyFromEndpoint; + RakNetGUID sourceGuid; + SystemAddress sourceAddress; + bool gotReplyFromSource; + RakNet::TimeMS timeout; + RakNet::TimeMS nextAction; + unsigned short forwardingPort; + __UDPSOCKET__ forwardingSocket; + }; + + struct ForwardedConnection + { + RakNetGUID endpointGuid; + RakNetGUID intermediaryGuid; + SystemAddress intermediaryAddress; + bool returnConnectionLostOnFailure; + bool weInitiatedForwarding; + }; + +protected: + + bool UpdateForwarding(ConnnectRequest* connectionRequest); + void RemoveConnectionRequest(unsigned int connectionRequestIndex); + void RequestForwarding(ConnnectRequest* connectionRequest); + void OnQueryForwarding(Packet *packet); + void OnQueryForwardingReply(Packet *packet); + void OnRequestForwarding(Packet *packet); + void OnRerouted(Packet *packet); + void OnMiniPunchReply(Packet *packet); + void OnMiniPunchReplyBounce(Packet *packet); + bool OnForwardingSuccess(Packet *packet); + int GetLargestPingAmongConnectedSystems(void) const; + void ReturnToUser(MessageID messageId, RakNetGUID endpointGuid, const SystemAddress &systemAddress, bool wasGeneratedLocally); + bool ConnectInternal(RakNetGUID endpointGuid, bool returnConnectionLostOnFailure); + + UDPForwarder *udpForwarder; + int maximumForwardingRequests; + SimpleMutex connectionRequestsMutex, miniPunchesInProgressMutex, forwardedConnectionListMutex; + DataStructures::List connectionRequests; + DataStructures::List miniPunchesInProgress; + // Forwarding we have initiated + DataStructures::List forwardedConnectionList; + + void ClearConnectionRequests(void); + void ClearMinipunches(void); + void ClearForwardedConnections(void); + void ClearAll(void); + int ReturnFailureOnCannotForward(RakNetGUID sourceGuid, RakNetGUID endpointGuid); + void SendFailureOnCannotForward(RakNetGUID sourceGuid, RakNetGUID endpointGuid); + void SendForwardingSuccess(MessageID messageId, RakNetGUID sourceGuid, RakNetGUID endpointGuid, unsigned short sourceToDstPort); + void SendOOBFromRakNetPort(OutOfBandIdentifiers oob, BitStream *extraData, SystemAddress sa); + void SendOOBFromSpecifiedSocket(OutOfBandIdentifiers oob, SystemAddress sa, __UDPSOCKET__ socket); + void SendOOBMessages(MiniPunchRequest *mpr); + + Router2DebugInterface *debugInterface; + unsigned short socketFamily; +}; + +} + +#endif + diff --git a/Source/SecureHandshake.h b/Source/SecureHandshake.h index ba2773cdd..5aed03137 100644 --- a/Source/SecureHandshake.h +++ b/Source/SecureHandshake.h @@ -1,34 +1,32 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// - - -#ifndef SECURE_HANDSHAKE_H -#define SECURE_HANDSHAKE_H - -#include "NativeFeatureIncludes.h" - -#if LIBCAT_SECURITY==1 - -// If building a RakNet DLL, be sure to tweak the CAT_EXPORT macro meaning -#if !defined(_RAKNET_LIB) && defined(_RAKNET_DLL) -# define CAT_BUILD_DLL -#else -# define CAT_NEUTER_EXPORT -#endif - -// Include DependentExtensions in your path to include this -#include "cat/AllTunnel.hpp" - -#endif // LIBCAT_SECURITY - -#endif // SECURE_HANDSHAKE_H +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// + + +#pragma once + +#include "NativeFeatureIncludes.h" + +#if LIBCAT_SECURITY==1 + +// If building a RakNet DLL, be sure to tweak the CAT_EXPORT macro meaning +#if !defined(_RAKNET_LIB) && defined(_RAKNET_DLL) +# define CAT_BUILD_DLL +#else +# define CAT_NEUTER_EXPORT +#endif + +// Include DependentExtensions in your path to include this +#include "cat/AllTunnel.hpp" + +#endif // LIBCAT_SECURITY + diff --git a/Source/SendToThread.h b/Source/SendToThread.h index 72d07dd31..29bd254b2 100644 --- a/Source/SendToThread.h +++ b/Source/SendToThread.h @@ -1,57 +1,55 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __SENDTO_THREAD -#define __SENDTO_THREAD - -#include "RakNetDefines.h" - -#ifdef USE_THREADED_SEND - -#include "InternalPacket.h" -#include "SocketLayer.h" -#include "DS_ThreadsafeAllocatingQueue.h" -#include "ThreadPool.h" - -namespace RakNet -{ -class SendToThread -{ -public: - SendToThread(); - ~SendToThread(); - - struct SendToThreadBlock - { - SOCKET s; - SystemAddress systemAddress; - unsigned short remotePortRakNetWasStartedOn_PS3; - unsigned int extraSocketOptions; - char data[MAXIMUM_MTU_SIZE]; - unsigned short dataWriteOffset; - }; - - static SendToThreadBlock* AllocateBlock(void); - static void ProcessBlock(SendToThreadBlock* threadedSend); - - static void AddRef(void); - static void Deref(void); - static DataStructures::ThreadsafeAllocatingQueue objectQueue; -protected: - static int refCount; - static ThreadPool threadPool; - -}; -} - - -#endif - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include "RakNetDefines.h" + +#ifdef USE_THREADED_SEND + +#include "InternalPacket.h" +#include "SocketLayer.h" +#include "DS_ThreadsafeAllocatingQueue.h" +#include "ThreadPool.h" + +namespace RakNet +{ +class SendToThread +{ +public: + SendToThread(); + ~SendToThread(); + + struct SendToThreadBlock + { + SOCKET s; + SystemAddress systemAddress; + unsigned short remotePortRakNetWasStartedOn_PS3; + unsigned int extraSocketOptions; + char data[MAXIMUM_MTU_SIZE]; + unsigned short dataWriteOffset; + }; + + static SendToThreadBlock* AllocateBlock(void); + static void ProcessBlock(SendToThreadBlock* threadedSend); + + static void AddRef(void); + static void Deref(void); + static DataStructures::ThreadsafeAllocatingQueue objectQueue; +protected: + static int refCount; + static ThreadPool threadPool; + +}; +} + + +#endif + diff --git a/Source/SignaledEvent.cpp b/Source/SignaledEvent.cpp index 5b6121e5d..9e4437fe1 100644 --- a/Source/SignaledEvent.cpp +++ b/Source/SignaledEvent.cpp @@ -1,264 +1,264 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "SignaledEvent.h" -#include "RakAssert.h" -#include "RakSleep.h" - -#if defined(__GNUC__) -#include -#include -#endif - -using namespace RakNet; - - - - - -SignaledEvent::SignaledEvent() -{ -#ifdef _WIN32 - eventList=INVALID_HANDLE_VALUE; - - -#else - isSignaled=false; -#endif -} -SignaledEvent::~SignaledEvent() -{ - // Intentionally do not close event, so it doesn't close twice on linux -} - -void SignaledEvent::InitEvent(void) -{ -#if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) - eventList=CreateEventEx(0, 0, 0, 0); -#elif defined(_WIN32) - eventList=CreateEvent(0, false, false, 0); - - - - - - - - - -#else - -#if !defined(ANDROID) - pthread_condattr_init( &condAttr ); - pthread_cond_init(&eventList, &condAttr); -#else - pthread_cond_init(&eventList, 0); -#endif - pthread_mutexattr_init( &mutexAttr ); - pthread_mutex_init(&hMutex, &mutexAttr); -#endif -} - -void SignaledEvent::CloseEvent(void) -{ -#ifdef _WIN32 - if (eventList!=INVALID_HANDLE_VALUE) - { - CloseHandle(eventList); - eventList=INVALID_HANDLE_VALUE; - } - - - - - - - - - -#else - pthread_cond_destroy(&eventList); - pthread_mutex_destroy(&hMutex); -#if !defined(ANDROID) - pthread_condattr_destroy( &condAttr ); -#endif - pthread_mutexattr_destroy( &mutexAttr ); -#endif -} - -void SignaledEvent::SetEvent(void) -{ -#ifdef _WIN32 - ::SetEvent(eventList); - - - - - - - - - - -#else - // Different from SetEvent which stays signaled. - // We have to record manually that the event was signaled - isSignaledMutex.Lock(); - isSignaled=true; - isSignaledMutex.Unlock(); - - // Unblock waiting threads - pthread_cond_broadcast(&eventList); -#endif -} - -void SignaledEvent::WaitOnEvent(int timeoutMs) -{ -#ifdef _WIN32 -// WaitForMultipleObjects( -// 2, -// eventList, -// false, -// timeoutMs); - WaitForSingleObjectEx(eventList,timeoutMs,FALSE); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -#else - - // If was previously set signaled, just unset and return - isSignaledMutex.Lock(); - if (isSignaled==true) - { - isSignaled=false; - isSignaledMutex.Unlock(); - return; - } - isSignaledMutex.Unlock(); - - - - //struct timespec ts; - - // Else wait for SetEvent to be called - - - - - - - - - - - - - - - - - - struct timespec ts; - - int rc; - struct timeval tp; - rc = gettimeofday(&tp, NULL); - ts.tv_sec = tp.tv_sec; - ts.tv_nsec = tp.tv_usec * 1000; -// #endif - - while (timeoutMs > 30) - { - // Wait 30 milliseconds for the signal, then check again. - // This is in case we missed the signal between the top of this function and pthread_cond_timedwait, or after the end of the loop and pthread_cond_timedwait - ts.tv_nsec += 30*1000000; - if (ts.tv_nsec >= 1000000000) - { - ts.tv_nsec -= 1000000000; - ts.tv_sec++; - } - - // [SBC] added mutex lock/unlock around cond_timedwait. - // this prevents airplay from generating a whole much of errors. - // not sure how this works on other platforms since according to - // the docs you are suppost to hold the lock before you wait - // on the cond. - pthread_mutex_lock(&hMutex); - pthread_cond_timedwait(&eventList, &hMutex, &ts); - pthread_mutex_unlock(&hMutex); - - timeoutMs-=30; - - isSignaledMutex.Lock(); - if (isSignaled==true) - { - isSignaled=false; - isSignaledMutex.Unlock(); - return; - } - isSignaledMutex.Unlock(); - } - - // Wait the remaining time, and turn off the signal in case it was set - ts.tv_nsec += timeoutMs*1000000; - if (ts.tv_nsec >= 1000000000) - { - ts.tv_nsec -= 1000000000; - ts.tv_sec++; - } - - pthread_mutex_lock(&hMutex); - pthread_cond_timedwait(&eventList, &hMutex, &ts); - pthread_mutex_unlock(&hMutex); - - isSignaledMutex.Lock(); - isSignaled=false; - isSignaledMutex.Unlock(); - -#endif -} +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "SignaledEvent.h" +#include "RakAssert.h" +#include "RakSleep.h" + +#if defined(__GNUC__) +#include +#include +#endif + +using namespace RakNet; + + + + + +SignaledEvent::SignaledEvent() +{ +#ifdef _WIN32 + eventList=INVALID_HANDLE_VALUE; + + +#else + isSignaled=false; +#endif +} +SignaledEvent::~SignaledEvent() +{ + // Intentionally do not close event, so it doesn't close twice on linux +} + +void SignaledEvent::InitEvent(void) +{ +#if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) + eventList=CreateEventEx(0, 0, 0, 0); +#elif defined(_WIN32) + eventList=CreateEvent(0, false, false, 0); + + + + + + + + + +#else + +#if !defined(ANDROID) + pthread_condattr_init( &condAttr ); + pthread_cond_init(&eventList, &condAttr); +#else + pthread_cond_init(&eventList, 0); +#endif + pthread_mutexattr_init( &mutexAttr ); + pthread_mutex_init(&hMutex, &mutexAttr); +#endif +} + +void SignaledEvent::CloseEvent(void) +{ +#ifdef _WIN32 + if (eventList!=INVALID_HANDLE_VALUE) + { + CloseHandle(eventList); + eventList=INVALID_HANDLE_VALUE; + } + + + + + + + + + +#else + pthread_cond_destroy(&eventList); + pthread_mutex_destroy(&hMutex); +#if !defined(ANDROID) + pthread_condattr_destroy( &condAttr ); +#endif + pthread_mutexattr_destroy( &mutexAttr ); +#endif +} + +void SignaledEvent::SetEvent(void) +{ +#ifdef _WIN32 + ::SetEvent(eventList); + + + + + + + + + + +#else + // Different from SetEvent which stays signaled. + // We have to record manually that the event was signaled + isSignaledMutex.Lock(); + isSignaled=true; + isSignaledMutex.Unlock(); + + // Unblock waiting threads + pthread_cond_broadcast(&eventList); +#endif +} + +void SignaledEvent::WaitOnEvent(int timeoutMs) +{ +#ifdef _WIN32 +// WaitForMultipleObjects( +// 2, +// eventList, +// false, +// timeoutMs); + WaitForSingleObjectEx(eventList,timeoutMs,FALSE); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#else + + // If was previously set signaled, just unset and return + isSignaledMutex.Lock(); + if (isSignaled==true) + { + isSignaled=false; + isSignaledMutex.Unlock(); + return; + } + isSignaledMutex.Unlock(); + + + + //struct timespec ts; + + // Else wait for SetEvent to be called + + + + + + + + + + + + + + + + + + struct timespec ts; + + int rc; + struct timeval tp; + rc = gettimeofday(&tp, nullptr); + ts.tv_sec = tp.tv_sec; + ts.tv_nsec = tp.tv_usec * 1000; +// #endif + + while (timeoutMs > 30) + { + // Wait 30 milliseconds for the signal, then check again. + // This is in case we missed the signal between the top of this function and pthread_cond_timedwait, or after the end of the loop and pthread_cond_timedwait + ts.tv_nsec += 30*1000000; + if (ts.tv_nsec >= 1000000000) + { + ts.tv_nsec -= 1000000000; + ts.tv_sec++; + } + + // [SBC] added mutex lock/unlock around cond_timedwait. + // this prevents airplay from generating a whole much of errors. + // not sure how this works on other platforms since according to + // the docs you are suppost to hold the lock before you wait + // on the cond. + pthread_mutex_lock(&hMutex); + pthread_cond_timedwait(&eventList, &hMutex, &ts); + pthread_mutex_unlock(&hMutex); + + timeoutMs-=30; + + isSignaledMutex.Lock(); + if (isSignaled==true) + { + isSignaled=false; + isSignaledMutex.Unlock(); + return; + } + isSignaledMutex.Unlock(); + } + + // Wait the remaining time, and turn off the signal in case it was set + ts.tv_nsec += timeoutMs*1000000; + if (ts.tv_nsec >= 1000000000) + { + ts.tv_nsec -= 1000000000; + ts.tv_sec++; + } + + pthread_mutex_lock(&hMutex); + pthread_cond_timedwait(&eventList, &hMutex, &ts); + pthread_mutex_unlock(&hMutex); + + isSignaledMutex.Lock(); + isSignaled=false; + isSignaledMutex.Unlock(); + +#endif +} diff --git a/Source/SignaledEvent.h b/Source/SignaledEvent.h index 0cbbc5113..ed8b12eba 100644 --- a/Source/SignaledEvent.h +++ b/Source/SignaledEvent.h @@ -1,69 +1,67 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __SIGNALED_EVENT_H -#define __SIGNALED_EVENT_H - - - -#if defined(_WIN32) -#include "WindowsIncludes.h" - - - -#else - #include - #include - #include "SimpleMutex.h" - - - - -#endif - -#include "Export.h" - -namespace RakNet -{ - -class RAK_DLL_EXPORT SignaledEvent -{ -public: - SignaledEvent(); - ~SignaledEvent(); - - void InitEvent(void); - void CloseEvent(void); - void SetEvent(void); - void WaitOnEvent(int timeoutMs); - -protected: -#ifdef _WIN32 - HANDLE eventList; - - - - - -#else - SimpleMutex isSignaledMutex; - bool isSignaled; -#if !defined(ANDROID) - pthread_condattr_t condAttr; -#endif - pthread_cond_t eventList; - pthread_mutex_t hMutex; - pthread_mutexattr_t mutexAttr; -#endif -}; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + + + +#if defined(_WIN32) +#include "WindowsIncludes.h" + + + +#else + #include + #include + #include "SimpleMutex.h" + + + + +#endif + +#include "Export.h" + +namespace RakNet +{ + +class RAK_DLL_EXPORT SignaledEvent +{ +public: + SignaledEvent(); + ~SignaledEvent(); + + void InitEvent(void); + void CloseEvent(void); + void SetEvent(void); + void WaitOnEvent(int timeoutMs); + +protected: +#ifdef _WIN32 + HANDLE eventList; + + + + + +#else + SimpleMutex isSignaledMutex; + bool isSignaled; +#if !defined(ANDROID) + pthread_condattr_t condAttr; +#endif + pthread_cond_t eventList; + pthread_mutex_t hMutex; + pthread_mutexattr_t mutexAttr; +#endif +}; + +} // namespace RakNet + diff --git a/Source/SimpleMutex.cpp b/Source/SimpleMutex.cpp index 6b1ec07d5..39d624d69 100644 --- a/Source/SimpleMutex.cpp +++ b/Source/SimpleMutex.cpp @@ -1,191 +1,191 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// - - - -#include "SimpleMutex.h" -#include "RakAssert.h" - -using namespace RakNet; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -SimpleMutex::SimpleMutex() //: isInitialized(false) -{ - - - - - - - - // Prior implementation of Initializing in Lock() was not threadsafe - Init(); -} - -SimpleMutex::~SimpleMutex() -{ -// if (isInitialized==false) -// return; -#ifdef _WIN32 - // CloseHandle(hMutex); - DeleteCriticalSection(&criticalSection); - - - - - - -#else - pthread_mutex_destroy(&hMutex); -#endif - - - - - - - -} - -#ifdef _WIN32 -#ifdef _DEBUG -#include -#endif -#endif - -void SimpleMutex::Lock(void) -{ -// if (isInitialized==false) -// Init(); - -#ifdef _WIN32 - /* - DWORD d = WaitForSingleObject(hMutex, INFINITE); - #ifdef _DEBUG - if (d==WAIT_FAILED) - { - LPVOID messageBuffer; - FormatMessage( - FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR) &messageBuffer, - 0, - NULL - ); - // Process any inserts in messageBuffer. - // ... - // Display the string. - //MessageBox( NULL, (LPCTSTR)messageBuffer, "Error", MB_OK | MB_ICONINFORMATION ); - RAKNET_DEBUG_PRINTF("SimpleMutex error: %s", messageBuffer); - // Free the buffer. - LocalFree( messageBuffer ); - - } - - RakAssert(d==WAIT_OBJECT_0); - */ - EnterCriticalSection(&criticalSection); - - - - - - -#else - int error = pthread_mutex_lock(&hMutex); - (void) error; - RakAssert(error==0); -#endif -} - -void SimpleMutex::Unlock(void) -{ -// if (isInitialized==false) -// return; -#ifdef _WIN32 - // ReleaseMutex(hMutex); - LeaveCriticalSection(&criticalSection); - - - - - - -#else - int error = pthread_mutex_unlock(&hMutex); - (void) error; - RakAssert(error==0); -#endif -} - -void SimpleMutex::Init(void) -{ -#if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) - InitializeCriticalSectionEx(&criticalSection,0,CRITICAL_SECTION_NO_DEBUG_INFO); -#elif defined(_WIN32) - // hMutex = CreateMutex(NULL, FALSE, 0); - // RakAssert(hMutex); - InitializeCriticalSection(&criticalSection); - - - - - - - - -#else - int error = pthread_mutex_init(&hMutex, 0); - (void) error; - RakAssert(error==0); -#endif -// isInitialized=true; -} +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// + + + +#include "SimpleMutex.h" +#include "RakAssert.h" + +using namespace RakNet; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +SimpleMutex::SimpleMutex() //: isInitialized(false) +{ + + + + + + + + // Prior implementation of Initializing in Lock() was not threadsafe + Init(); +} + +SimpleMutex::~SimpleMutex() +{ +// if (isInitialized==false) +// return; +#ifdef _WIN32 + // CloseHandle(hMutex); + DeleteCriticalSection(&criticalSection); + + + + + + +#else + pthread_mutex_destroy(&hMutex); +#endif + + + + + + + +} + +#ifdef _WIN32 +#ifdef _DEBUG +#include +#endif +#endif + +void SimpleMutex::Lock(void) +{ +// if (isInitialized==false) +// Init(); + +#ifdef _WIN32 + /* + DWORD d = WaitForSingleObject(hMutex, INFINITE); + #ifdef _DEBUG + if (d==WAIT_FAILED) + { + LPVOID messageBuffer; + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR) &messageBuffer, + 0, + nullptr + ); + // Process any inserts in messageBuffer. + // ... + // Display the string. + //MessageBox( nullptr, (LPCTSTR)messageBuffer, "Error", MB_OK | MB_ICONINFORMATION ); + RAKNET_DEBUG_PRINTF("SimpleMutex error: %s", messageBuffer); + // Free the buffer. + LocalFree( messageBuffer ); + + } + + RakAssert(d==WAIT_OBJECT_0); + */ + EnterCriticalSection(&criticalSection); + + + + + + +#else + int error = pthread_mutex_lock(&hMutex); + (void) error; + RakAssert(error==0); +#endif +} + +void SimpleMutex::Unlock(void) +{ +// if (isInitialized==false) +// return; +#ifdef _WIN32 + // ReleaseMutex(hMutex); + LeaveCriticalSection(&criticalSection); + + + + + + +#else + int error = pthread_mutex_unlock(&hMutex); + (void) error; + RakAssert(error==0); +#endif +} + +void SimpleMutex::Init(void) +{ +#if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) + InitializeCriticalSectionEx(&criticalSection,0,CRITICAL_SECTION_NO_DEBUG_INFO); +#elif defined(_WIN32) + // hMutex = CreateMutex(nullptr, FALSE, 0); + // RakAssert(hMutex); + InitializeCriticalSection(&criticalSection); + + + + + + + + +#else + int error = pthread_mutex_init(&hMutex, 0); + (void) error; + RakAssert(error==0); +#endif +// isInitialized=true; +} diff --git a/Source/SimpleMutex.h b/Source/SimpleMutex.h index bb998e3e8..fb111f03b 100644 --- a/Source/SimpleMutex.h +++ b/Source/SimpleMutex.h @@ -1,78 +1,75 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief \b [Internal] Encapsulates a mutex -/// - - - -#ifndef __SIMPLE_MUTEX_H -#define __SIMPLE_MUTEX_H - -#include "RakMemoryOverride.h" - - -#if defined(_WIN32) -#include "WindowsIncludes.h" - - -#else -#include -#include -#endif -#include "Export.h" - -namespace RakNet -{ - -/// \brief An easy to use mutex. -/// -/// I wrote this because the version that comes with Windows is too complicated and requires too much code to use. -/// @remark Previously I used this everywhere, and in fact for a year or two RakNet was totally threadsafe. While doing profiling, I saw that this function was incredibly slow compared to the blazing performance of everything else, so switched to single producer / consumer everywhere. Now the user thread of RakNet is not threadsafe, but it's 100X faster than before. -class RAK_DLL_EXPORT SimpleMutex -{ -public: - - // Constructor - SimpleMutex(); - - // Destructor - ~SimpleMutex(); - - // Locks the mutex. Slow! - void Lock(void); - - // Unlocks the mutex. - void Unlock(void); - - - - - - - -private: - void Init(void); -#ifdef _WIN32 - CRITICAL_SECTION criticalSection; /// Docs say this is faster than a mutex for single process access - - -#else - pthread_mutex_t hMutex; -#endif - // Not threadsafe - // bool isInitialized; -}; - -} // namespace RakNet - -#endif - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief \b [Internal] Encapsulates a mutex +/// + + + +#pragma once + +#include "RakMemoryOverride.h" + + +#if defined(_WIN32) +#include "WindowsIncludes.h" + + +#else +#include +#include +#endif +#include "Export.h" + +namespace RakNet +{ + +/// \brief An easy to use mutex. +/// +/// I wrote this because the version that comes with Windows is too complicated and requires too much code to use. +/// @remark Previously I used this everywhere, and in fact for a year or two RakNet was totally threadsafe. While doing profiling, I saw that this function was incredibly slow compared to the blazing performance of everything else, so switched to single producer / consumer everywhere. Now the user thread of RakNet is not threadsafe, but it's 100X faster than before. +class RAK_DLL_EXPORT SimpleMutex +{ +public: + + // Constructor + SimpleMutex(); + + // Destructor + ~SimpleMutex(); + + // Locks the mutex. Slow! + void Lock(void); + + // Unlocks the mutex. + void Unlock(void); + + + + + + + +private: + void Init(void); +#ifdef _WIN32 + CRITICAL_SECTION criticalSection; /// Docs say this is faster than a mutex for single process access + + +#else + pthread_mutex_t hMutex; +#endif + // Not threadsafe + // bool isInitialized; +}; + +} // namespace RakNet + diff --git a/Source/SingleProducerConsumer.h b/Source/SingleProducerConsumer.h index d202b8801..451294de4 100644 --- a/Source/SingleProducerConsumer.h +++ b/Source/SingleProducerConsumer.h @@ -1,267 +1,265 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief \b [Internal] Passes queued data between threads using a circular buffer with read and write pointers -/// - - - -#ifndef __SINGLE_PRODUCER_CONSUMER_H -#define __SINGLE_PRODUCER_CONSUMER_H - -#include "RakAssert.h" - -static const int MINIMUM_LIST_SIZE=8; - -#include "RakMemoryOverride.h" -#include "Export.h" - -/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures -/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. -namespace DataStructures -{ - /// \brief A single producer consumer implementation without critical sections. - template - class RAK_DLL_EXPORT SingleProducerConsumer - { - public: - // Constructor - SingleProducerConsumer(); - - // Destructor - ~SingleProducerConsumer(); - - /// WriteLock must be immediately followed by WriteUnlock. These two functions must be called in the same thread. - /// \return A pointer to a block of data you can write to. - SingleProducerConsumerType* WriteLock(void); - - /// Call if you don't want to write to a block of data from WriteLock() after all. - /// Cancelling locks cancels all locks back up to the data passed. So if you lock twice and cancel using the first lock, the second lock is ignored - /// \param[in] cancelToLocation Which WriteLock() to cancel. - void CancelWriteLock(SingleProducerConsumerType* cancelToLocation); - - /// Call when you are done writing to a block of memory returned by WriteLock() - void WriteUnlock(void); - - /// ReadLock must be immediately followed by ReadUnlock. These two functions must be called in the same thread. - /// \retval 0 No data is availble to read - /// \retval Non-zero The data previously written to, in another thread, by WriteLock followed by WriteUnlock. - SingleProducerConsumerType* ReadLock(void); - - // Cancelling locks cancels all locks back up to the data passed. So if you lock twice and cancel using the first lock, the second lock is ignored - /// param[in] Which ReadLock() to cancel. - void CancelReadLock(SingleProducerConsumerType* cancelToLocation); - - /// Signals that we are done reading the the data from the least recent call of ReadLock. - /// At this point that pointer is no longer valid, and should no longer be read. - void ReadUnlock(void); - - /// Clear is not thread-safe and none of the lock or unlock functions should be called while it is running. - void Clear(void); - - /// This function will estimate how many elements are waiting to be read. It's threadsafe enough that the value returned is stable, but not threadsafe enough to give accurate results. - /// \return An ESTIMATE of how many data elements are waiting to be read - int Size(void) const; - - /// Make sure that the pointer we done reading for the call to ReadUnlock is the right pointer. - /// param[in] A previous pointer returned by ReadLock() - bool CheckReadUnlockOrder(const SingleProducerConsumerType* data) const; - - /// Returns if ReadUnlock was called before ReadLock - /// \return If the read is locked - bool ReadIsLocked(void) const; - - private: - struct DataPlusPtr - { - DataPlusPtr () {readyToRead=false;} - SingleProducerConsumerType object; - - // Ready to read is so we can use an equality boolean comparison, in case the writePointer var is trashed while context switching. - volatile bool readyToRead; - volatile DataPlusPtr *next; - }; - volatile DataPlusPtr *readAheadPointer; - volatile DataPlusPtr *writeAheadPointer; - volatile DataPlusPtr *readPointer; - volatile DataPlusPtr *writePointer; - unsigned readCount, writeCount; - }; - - template - SingleProducerConsumer::SingleProducerConsumer() - { - // Preallocate - readPointer = RakNet::OP_NEW( _FILE_AND_LINE_ ); - writePointer=readPointer; - readPointer->next = RakNet::OP_NEW( _FILE_AND_LINE_ ); - int listSize; -#ifdef _DEBUG - RakAssert(MINIMUM_LIST_SIZE>=3); -#endif - for (listSize=2; listSize < MINIMUM_LIST_SIZE; listSize++) - { - readPointer=readPointer->next; - readPointer->next = RakNet::OP_NEW( _FILE_AND_LINE_ ); - } - readPointer->next->next=writePointer; // last to next = start - readPointer=writePointer; - readAheadPointer=readPointer; - writeAheadPointer=writePointer; - readCount=writeCount=0; - } - - template - SingleProducerConsumer::~SingleProducerConsumer() - { - volatile DataPlusPtr *next; - readPointer=writeAheadPointer->next; - while (readPointer!=writeAheadPointer) - { - next=readPointer->next; - RakNet::OP_DELETE((char*) readPointer, _FILE_AND_LINE_); - readPointer=next; - } - RakNet::OP_DELETE((char*) readPointer, _FILE_AND_LINE_); - } - - template - SingleProducerConsumerType* SingleProducerConsumer::WriteLock( void ) - { - if (writeAheadPointer->next==readPointer || - writeAheadPointer->next->readyToRead==true) - { - volatile DataPlusPtr *originalNext=writeAheadPointer->next; - writeAheadPointer->next=RakNet::OP_NEW(_FILE_AND_LINE_); - RakAssert(writeAheadPointer->next); - writeAheadPointer->next->next=originalNext; - } - - volatile DataPlusPtr *last; - last=writeAheadPointer; - writeAheadPointer=writeAheadPointer->next; - - return (SingleProducerConsumerType*) last; - } - - template - void SingleProducerConsumer::CancelWriteLock( SingleProducerConsumerType* cancelToLocation ) - { - writeAheadPointer=(DataPlusPtr *)cancelToLocation; - } - - template - void SingleProducerConsumer::WriteUnlock( void ) - { - // DataPlusPtr *dataContainer = (DataPlusPtr *)structure; - -#ifdef _DEBUG - RakAssert(writePointer->next!=readPointer); - RakAssert(writePointer!=writeAheadPointer); -#endif - - writeCount++; - // User is done with the data, allow send by updating the write pointer - writePointer->readyToRead=true; - writePointer=writePointer->next; - } - - template - SingleProducerConsumerType* SingleProducerConsumer::ReadLock( void ) - { - if (readAheadPointer==writePointer || - readAheadPointer->readyToRead==false) - { - return 0; - } - - volatile DataPlusPtr *last; - last=readAheadPointer; - readAheadPointer=readAheadPointer->next; - return (SingleProducerConsumerType*)last; - } - - template - void SingleProducerConsumer::CancelReadLock( SingleProducerConsumerType* cancelToLocation ) - { -#ifdef _DEBUG - RakAssert(readPointer!=writePointer); -#endif - readAheadPointer=(DataPlusPtr *)cancelToLocation; - } - - template - void SingleProducerConsumer::ReadUnlock( void ) - { -#ifdef _DEBUG - RakAssert(readAheadPointer!=readPointer); // If hits, then called ReadUnlock before ReadLock - RakAssert(readPointer!=writePointer); // If hits, then called ReadUnlock when Read returns 0 -#endif - readCount++; - - // Allow writes to this memory block - readPointer->readyToRead=false; - readPointer=readPointer->next; - } - - template - void SingleProducerConsumer::Clear( void ) - { - // Shrink the list down to MINIMUM_LIST_SIZE elements - volatile DataPlusPtr *next; - writePointer=readPointer->next; - - int listSize=1; - next=readPointer->next; - while (next!=readPointer) - { - listSize++; - next=next->next; - } - - while (listSize-- > MINIMUM_LIST_SIZE) - { - next=writePointer->next; -#ifdef _DEBUG - RakAssert(writePointer!=readPointer); -#endif - RakNet::OP_DELETE((char*) writePointer, _FILE_AND_LINE_); - writePointer=next; - } - - readPointer->next=writePointer; - writePointer=readPointer; - readAheadPointer=readPointer; - writeAheadPointer=writePointer; - readCount=writeCount=0; - } - - template - int SingleProducerConsumer::Size( void ) const - { - return writeCount-readCount; - } - - template - bool SingleProducerConsumer::CheckReadUnlockOrder(const SingleProducerConsumerType* data) const - { - return const_cast(&readPointer->object) == data; - } - - - template - bool SingleProducerConsumer::ReadIsLocked(void) const - { - return readAheadPointer!=readPointer; - } -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief \b [Internal] Passes queued data between threads using a circular buffer with read and write pointers +/// + + + +#pragma once + +#include "RakAssert.h" + +static const int MINIMUM_LIST_SIZE=8; + +#include "RakMemoryOverride.h" +#include "Export.h" + +/// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures +/// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish. +namespace DataStructures +{ + /// \brief A single producer consumer implementation without critical sections. + template + class RAK_DLL_EXPORT SingleProducerConsumer + { + public: + // Constructor + SingleProducerConsumer(); + + // Destructor + ~SingleProducerConsumer(); + + /// WriteLock must be immediately followed by WriteUnlock. These two functions must be called in the same thread. + /// \return A pointer to a block of data you can write to. + SingleProducerConsumerType* WriteLock(void); + + /// Call if you don't want to write to a block of data from WriteLock() after all. + /// Cancelling locks cancels all locks back up to the data passed. So if you lock twice and cancel using the first lock, the second lock is ignored + /// \param[in] cancelToLocation Which WriteLock() to cancel. + void CancelWriteLock(SingleProducerConsumerType* cancelToLocation); + + /// Call when you are done writing to a block of memory returned by WriteLock() + void WriteUnlock(void); + + /// ReadLock must be immediately followed by ReadUnlock. These two functions must be called in the same thread. + /// \retval 0 No data is availble to read + /// \retval Non-zero The data previously written to, in another thread, by WriteLock followed by WriteUnlock. + SingleProducerConsumerType* ReadLock(void); + + // Cancelling locks cancels all locks back up to the data passed. So if you lock twice and cancel using the first lock, the second lock is ignored + /// param[in] Which ReadLock() to cancel. + void CancelReadLock(SingleProducerConsumerType* cancelToLocation); + + /// Signals that we are done reading the the data from the least recent call of ReadLock. + /// At this point that pointer is no longer valid, and should no longer be read. + void ReadUnlock(void); + + /// Clear is not thread-safe and none of the lock or unlock functions should be called while it is running. + void Clear(void); + + /// This function will estimate how many elements are waiting to be read. It's threadsafe enough that the value returned is stable, but not threadsafe enough to give accurate results. + /// \return An ESTIMATE of how many data elements are waiting to be read + int Size(void) const; + + /// Make sure that the pointer we done reading for the call to ReadUnlock is the right pointer. + /// param[in] A previous pointer returned by ReadLock() + bool CheckReadUnlockOrder(const SingleProducerConsumerType* data) const; + + /// Returns if ReadUnlock was called before ReadLock + /// \return If the read is locked + bool ReadIsLocked(void) const; + + private: + struct DataPlusPtr + { + DataPlusPtr () {readyToRead=false;} + SingleProducerConsumerType object; + + // Ready to read is so we can use an equality boolean comparison, in case the writePointer var is trashed while context switching. + volatile bool readyToRead; + volatile DataPlusPtr *next; + }; + volatile DataPlusPtr *readAheadPointer; + volatile DataPlusPtr *writeAheadPointer; + volatile DataPlusPtr *readPointer; + volatile DataPlusPtr *writePointer; + unsigned readCount, writeCount; + }; + + template + SingleProducerConsumer::SingleProducerConsumer() + { + // Preallocate + readPointer = RakNet::OP_NEW( _FILE_AND_LINE_ ); + writePointer=readPointer; + readPointer->next = RakNet::OP_NEW( _FILE_AND_LINE_ ); + int listSize; +#ifdef _DEBUG + RakAssert(MINIMUM_LIST_SIZE>=3); +#endif + for (listSize=2; listSize < MINIMUM_LIST_SIZE; listSize++) + { + readPointer=readPointer->next; + readPointer->next = RakNet::OP_NEW( _FILE_AND_LINE_ ); + } + readPointer->next->next=writePointer; // last to next = start + readPointer=writePointer; + readAheadPointer=readPointer; + writeAheadPointer=writePointer; + readCount=writeCount=0; + } + + template + SingleProducerConsumer::~SingleProducerConsumer() + { + volatile DataPlusPtr *next; + readPointer=writeAheadPointer->next; + while (readPointer!=writeAheadPointer) + { + next=readPointer->next; + RakNet::OP_DELETE((char*) readPointer, _FILE_AND_LINE_); + readPointer=next; + } + RakNet::OP_DELETE((char*) readPointer, _FILE_AND_LINE_); + } + + template + SingleProducerConsumerType* SingleProducerConsumer::WriteLock( void ) + { + if (writeAheadPointer->next==readPointer || + writeAheadPointer->next->readyToRead==true) + { + volatile DataPlusPtr *originalNext=writeAheadPointer->next; + writeAheadPointer->next=RakNet::OP_NEW(_FILE_AND_LINE_); + RakAssert(writeAheadPointer->next); + writeAheadPointer->next->next=originalNext; + } + + volatile DataPlusPtr *last; + last=writeAheadPointer; + writeAheadPointer=writeAheadPointer->next; + + return (SingleProducerConsumerType*) last; + } + + template + void SingleProducerConsumer::CancelWriteLock( SingleProducerConsumerType* cancelToLocation ) + { + writeAheadPointer=(DataPlusPtr *)cancelToLocation; + } + + template + void SingleProducerConsumer::WriteUnlock( void ) + { + // DataPlusPtr *dataContainer = (DataPlusPtr *)structure; + +#ifdef _DEBUG + RakAssert(writePointer->next!=readPointer); + RakAssert(writePointer!=writeAheadPointer); +#endif + + writeCount++; + // User is done with the data, allow send by updating the write pointer + writePointer->readyToRead=true; + writePointer=writePointer->next; + } + + template + SingleProducerConsumerType* SingleProducerConsumer::ReadLock( void ) + { + if (readAheadPointer==writePointer || + readAheadPointer->readyToRead==false) + { + return 0; + } + + volatile DataPlusPtr *last; + last=readAheadPointer; + readAheadPointer=readAheadPointer->next; + return (SingleProducerConsumerType*)last; + } + + template + void SingleProducerConsumer::CancelReadLock( SingleProducerConsumerType* cancelToLocation ) + { +#ifdef _DEBUG + RakAssert(readPointer!=writePointer); +#endif + readAheadPointer=(DataPlusPtr *)cancelToLocation; + } + + template + void SingleProducerConsumer::ReadUnlock( void ) + { +#ifdef _DEBUG + RakAssert(readAheadPointer!=readPointer); // If hits, then called ReadUnlock before ReadLock + RakAssert(readPointer!=writePointer); // If hits, then called ReadUnlock when Read returns 0 +#endif + readCount++; + + // Allow writes to this memory block + readPointer->readyToRead=false; + readPointer=readPointer->next; + } + + template + void SingleProducerConsumer::Clear( void ) + { + // Shrink the list down to MINIMUM_LIST_SIZE elements + volatile DataPlusPtr *next; + writePointer=readPointer->next; + + int listSize=1; + next=readPointer->next; + while (next!=readPointer) + { + listSize++; + next=next->next; + } + + while (listSize-- > MINIMUM_LIST_SIZE) + { + next=writePointer->next; +#ifdef _DEBUG + RakAssert(writePointer!=readPointer); +#endif + RakNet::OP_DELETE((char*) writePointer, _FILE_AND_LINE_); + writePointer=next; + } + + readPointer->next=writePointer; + writePointer=readPointer; + readAheadPointer=readPointer; + writeAheadPointer=writePointer; + readCount=writeCount=0; + } + + template + int SingleProducerConsumer::Size( void ) const + { + return writeCount-readCount; + } + + template + bool SingleProducerConsumer::CheckReadUnlockOrder(const SingleProducerConsumerType* data) const + { + return const_cast(&readPointer->object) == data; + } + + + template + bool SingleProducerConsumer::ReadIsLocked(void) const + { + return readAheadPointer!=readPointer; + } +} + diff --git a/Source/SocketDefines.h b/Source/SocketDefines.h index a9f3cd262..0cfe2ed96 100644 --- a/Source/SocketDefines.h +++ b/Source/SocketDefines.h @@ -1,122 +1,120 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __SOCKET_DEFINES_H -#define __SOCKET_DEFINES_H - -/// Internal - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -#if defined(WINDOWS_STORE_RT) - #include "WinRTSocketAdapter.h" - #define accept__ WinRTAccept - #define connect__ WinRTConnect - #define closesocket__ WinRTClose - #define socket__ WinRTCreateDatagramSocket - #define bind__ WinRTBind - #define getsockname__ RNS2_WindowsStore8::WinRTGetSockName - #define getsockopt__ WinRTGetSockOpt - #define inet_addr__ RNS2_WindowsStore8::WinRTInet_Addr - #define ioctlsocket__ RNS2_WindowsStore8::WinRTIOCTLSocket - #define listen__ WinRTListen - #define recv__ WinRTRecv - #define recvfrom__ WinRTRecvFrom - #define select__ WinRTSelect - #define send__ WinRTSend - #define sendto__ WinRTSendTo - #define setsockopt__ RNS2_WindowsStore8::WinRTSetSockOpt - #define shutdown__ WinRTShutdown - #define WSASendTo__ WinRTSendTo -#else - - - - - - - #if defined(_WIN32) - #define closesocket__ closesocket - #define select__ select - #elif defined(__native_client__) - // namespace RakNet { void CloseSocket(SOCKET s); } - // #define closesocket__ RakNet::CloseSocket - #define select__ select - #else - #define closesocket__ close - #define select__ select - #endif - #define accept__ accept - #define connect__ connect - - - - #define socket__ socket - - #define bind__ bind - #define getsockname__ getsockname - #define getsockopt__ getsockopt - - - - #define inet_addr__ inet_addr - - #define ioctlsocket__ ioctlsocket - #define listen__ listen - #define recv__ recv - #define recvfrom__ recvfrom - - - - #define sendto__ sendto - - #define send__ send - - - - #define setsockopt__ setsockopt - - #define shutdown__ shutdown - #define WSASendTo__ WSASendTo -#endif - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +/// Internal + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#if defined(WINDOWS_STORE_RT) + #include "WinRTSocketAdapter.h" + #define accept__ WinRTAccept + #define connect__ WinRTConnect + #define closesocket__ WinRTClose + #define socket__ WinRTCreateDatagramSocket + #define bind__ WinRTBind + #define getsockname__ RNS2_WindowsStore8::WinRTGetSockName + #define getsockopt__ WinRTGetSockOpt + #define inet_addr__ RNS2_WindowsStore8::WinRTInet_Addr + #define ioctlsocket__ RNS2_WindowsStore8::WinRTIOCTLSocket + #define listen__ WinRTListen + #define recv__ WinRTRecv + #define recvfrom__ WinRTRecvFrom + #define select__ WinRTSelect + #define send__ WinRTSend + #define sendto__ WinRTSendTo + #define setsockopt__ RNS2_WindowsStore8::WinRTSetSockOpt + #define shutdown__ WinRTShutdown + #define WSASendTo__ WinRTSendTo +#else + + + + + + + #if defined(_WIN32) + #define closesocket__ closesocket + #define select__ select + #elif defined(__native_client__) + // namespace RakNet { void CloseSocket(SOCKET s); } + // #define closesocket__ RakNet::CloseSocket + #define select__ select + #else + #define closesocket__ close + #define select__ select + #endif + #define accept__ accept + #define connect__ connect + + + + #define socket__ socket + + #define bind__ bind + #define getsockname__ getsockname + #define getsockopt__ getsockopt + + + + #define inet_addr__ inet_addr + + #define ioctlsocket__ ioctlsocket + #define listen__ listen + #define recv__ recv + #define recvfrom__ recvfrom + + + + #define sendto__ sendto + + #define send__ send + + + + #define setsockopt__ setsockopt + + #define shutdown__ shutdown + #define WSASendTo__ WSASendTo +#endif + diff --git a/Source/SocketIncludes.h b/Source/SocketIncludes.h index b40709081..1b2d407ac 100644 --- a/Source/SocketIncludes.h +++ b/Source/SocketIncludes.h @@ -1,98 +1,96 @@ -#ifndef RAKNET_SOCKETINCLUDES_H -#define RAKNET_SOCKETINCLUDES_H - -// All this crap just to include type SOCKET - -#ifdef __native_client__ -#define _PP_Instance_ PP_Instance -#else -#define _PP_Instance_ int -#endif - - - - - - - - - - - - - - - - - - - - -#if defined(WINDOWS_STORE_RT) - #include - #include "WinRTSockAddr.h" - typedef Windows::Networking::Sockets::DatagramSocket^ __UDPSOCKET__; - typedef Windows::Networking::Sockets::StreamSocket^ __TCPSOCKET__; - typedef unsigned int socklen_t; - #define FORMAT_MESSAGE_ALLOCATE_BUFFER 0 - #define FIONBIO 0 - #define LocalFree(x) - // using Windows.Networking; - // using Windows.Networking.Sockets; - // See http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets.datagramsocketcontrol -#elif defined(_WIN32) - // IP_DONTFRAGMENT is different between winsock 1 and winsock 2. Therefore, Winsock2.h must be linked againt Ws2_32.lib - // winsock.h must be linked against WSock32.lib. If these two are mixed up the flag won't work correctly - // WinRT: http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets - // Sample code: http://stackoverflow.com/questions/10290945/correct-use-of-udp-datagramsocket - #include - typedef SOCKET __UDPSOCKET__; - typedef SOCKET __TCPSOCKET__; - typedef int socklen_t; -#else - #define closesocket close - #include - #include - #include - #include - #include - #include - #include - - #ifdef __native_client__ - #include "ppapi/cpp/private/net_address_private.h" - #include "ppapi/c/pp_bool.h" - #include "ppapi/c/pp_errors.h" - #include "ppapi/cpp/completion_callback.h" - #include "ppapi/cpp/instance_handle.h" - #include "ppapi/cpp/module.h" - #include "ppapi/cpp/module_impl.h" - #include "ppapi/c/pp_errors.h" - #include "ppapi/c/pp_module.h" - #include "ppapi/c/pp_var.h" - #include "ppapi/c/pp_resource.h" - #include "ppapi/c/ppb.h" - #include "ppapi/c/ppb_instance.h" - #include "ppapi/c/ppb_messaging.h" - #include "ppapi/c/ppb_var.h" - #include "ppapi/c/ppp.h" - #include "ppapi/c/ppb_core.h" - #include "ppapi/c/ppp_instance.h" - #include "ppapi/c/ppp_messaging.h" - #include "ppapi/c/pp_input_event.h" - #include "ppapi/c/pp_completion_callback.h" - //UDP specific - the 'private' folder was copied from the chromium src/ppapi/c headers folder - #include "ppapi/c/private/ppb_udp_socket_private.h" - #include "ppapi/cpp/private/net_address_private.h" - typedef PP_Resource __UDPSOCKET__; - typedef PP_Resource __TCPSOCKET__; - #else - //#include "RakMemoryOverride.h" - /// Unix/Linux uses ints for sockets - typedef int __UDPSOCKET__; - typedef int __TCPSOCKET__; -#endif - -#endif - -#endif // RAKNET_SOCKETINCLUDES_H +#pragma once + +// All this crap just to include type SOCKET + +#ifdef __native_client__ +#define _PP_Instance_ PP_Instance +#else +#define _PP_Instance_ int +#endif + + + + + + + + + + + + + + + + + + + + +#if defined(WINDOWS_STORE_RT) + #include + #include "WinRTSockAddr.h" + typedef Windows::Networking::Sockets::DatagramSocket^ __UDPSOCKET__; + typedef Windows::Networking::Sockets::StreamSocket^ __TCPSOCKET__; + typedef unsigned int socklen_t; + #define FORMAT_MESSAGE_ALLOCATE_BUFFER 0 + #define FIONBIO 0 + #define LocalFree(x) + // using Windows.Networking; + // using Windows.Networking.Sockets; + // See http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets.datagramsocketcontrol +#elif defined(_WIN32) + // IP_DONTFRAGMENT is different between winsock 1 and winsock 2. Therefore, Winsock2.h must be linked againt Ws2_32.lib + // winsock.h must be linked against WSock32.lib. If these two are mixed up the flag won't work correctly + // WinRT: http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets + // Sample code: http://stackoverflow.com/questions/10290945/correct-use-of-udp-datagramsocket + #include + typedef SOCKET __UDPSOCKET__; + typedef SOCKET __TCPSOCKET__; + typedef int socklen_t; +#else + #define closesocket close + #include + #include + #include + #include + #include + #include + #include + + #ifdef __native_client__ + #include "ppapi/cpp/private/net_address_private.h" + #include "ppapi/c/pp_bool.h" + #include "ppapi/c/pp_errors.h" + #include "ppapi/cpp/completion_callback.h" + #include "ppapi/cpp/instance_handle.h" + #include "ppapi/cpp/module.h" + #include "ppapi/cpp/module_impl.h" + #include "ppapi/c/pp_errors.h" + #include "ppapi/c/pp_module.h" + #include "ppapi/c/pp_var.h" + #include "ppapi/c/pp_resource.h" + #include "ppapi/c/ppb.h" + #include "ppapi/c/ppb_instance.h" + #include "ppapi/c/ppb_messaging.h" + #include "ppapi/c/ppb_var.h" + #include "ppapi/c/ppp.h" + #include "ppapi/c/ppb_core.h" + #include "ppapi/c/ppp_instance.h" + #include "ppapi/c/ppp_messaging.h" + #include "ppapi/c/pp_input_event.h" + #include "ppapi/c/pp_completion_callback.h" + //UDP specific - the 'private' folder was copied from the chromium src/ppapi/c headers folder + #include "ppapi/c/private/ppb_udp_socket_private.h" + #include "ppapi/cpp/private/net_address_private.h" + typedef PP_Resource __UDPSOCKET__; + typedef PP_Resource __TCPSOCKET__; + #else + //#include "RakMemoryOverride.h" + /// Unix/Linux uses ints for sockets + typedef int __UDPSOCKET__; + typedef int __TCPSOCKET__; +#endif + +#endif + diff --git a/Source/SocketLayer.cpp b/Source/SocketLayer.cpp index d111e5630..52103d234 100644 --- a/Source/SocketLayer.cpp +++ b/Source/SocketLayer.cpp @@ -1,613 +1,613 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief SocketLayer class implementation -/// - - -#include "SocketLayer.h" -#include "RakAssert.h" -#include "RakNetTypes.h" -#include "RakPeer.h" -#include "GetTime.h" -#include "LinuxStrings.h" -#include "SocketDefines.h" -#if (defined(__GNUC__) || defined(__GCCXML__)) && !defined(__WIN32__) -#include -#endif - -using namespace RakNet; - -/* -#if defined(__native_client__) -using namespace pp; -#endif -*/ - -#if USE_SLIDING_WINDOW_CONGESTION_CONTROL!=1 -#include "CCRakNetUDT.h" -#else -#include "CCRakNetSlidingWindow.h" -#endif - -//SocketLayerOverride *SocketLayer::slo=0; - -#ifdef _WIN32 -#else -#include // memcpy -#include -#include -#include -#include // error numbers -#include // RAKNET_DEBUG_PRINTF -#if !defined(ANDROID) -#include -#endif -#include -#include -#include -#include -#include - -#endif - - - - - - - - - - - - - -#if defined(_WIN32) -#include "WSAStartupSingleton.h" -#include "WindowsIncludes.h" - -#else -#include -#endif - -#include "RakSleep.h" -#include -#include "Itoa.h" - -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -namespace RakNet -{ - extern void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNet::TimeUS timeRead ); - //extern void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSocket* rakNetSocket, RakNet::TimeUS timeRead ); -} - -#ifdef _DEBUG -#include -#endif - - - -// http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#ip4to6 -// http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#getaddrinfo - -#if RAKNET_SUPPORT_IPV6==1 -void PrepareAddrInfoHints(addrinfo *hints) -{ - memset(hints, 0, sizeof (addrinfo)); // make sure the struct is empty - hints->ai_socktype = SOCK_DGRAM; // UDP sockets - hints->ai_flags = AI_PASSIVE; // fill in my IP for me -} -#endif - -void SocketLayer::SetSocketOptions( __UDPSOCKET__ listenSocket, bool blockingSocket, bool setBroadcast) -{ -#ifdef __native_client__ - (void) listenSocket; -#else - int sock_opt = 1; - - // This doubles the max throughput rate - sock_opt=1024*256; - setsockopt__(listenSocket, SOL_SOCKET, SO_RCVBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) ); - - // Immediate hard close. Don't linger the socket, or recreating the socket quickly on Vista fails. - // Fail with voice and xbox - - sock_opt=0; - setsockopt__(listenSocket, SOL_SOCKET, SO_LINGER, ( char * ) & sock_opt, sizeof ( sock_opt ) ); - - - - // This doesn't make much difference: 10% maybe - // Not supported on console 2 - sock_opt=1024*16; - setsockopt__(listenSocket, SOL_SOCKET, SO_SNDBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) ); - - - if (blockingSocket==false) - { -#ifdef _WIN32 - unsigned long nonblocking = 1; - ioctlsocket__(listenSocket, FIONBIO, &nonblocking ); - - - -#else - fcntl( listenSocket, F_SETFL, O_NONBLOCK ); -#endif - } - if (setBroadcast) - { - // Note: Fails with VDP but not xbox - // Set broadcast capable - sock_opt=1; - if ( setsockopt__(listenSocket, SOL_SOCKET, SO_BROADCAST, ( char * ) & sock_opt, sizeof( sock_opt ) ) == -1 ) - { -#if defined(_WIN32) && defined(_DEBUG) -#if !defined(WINDOWS_PHONE_8) - DWORD dwIOError = GetLastError(); - // On Vista, can get WSAEACCESS (10013) - // See http://support.microsoft.com/kb/819124 - // http://blogs.msdn.com/wndp/archive/2007/03/19/winsock-so-exclusiveaddruse-on-vista.aspx - // http://msdn.microsoft.com/en-us/library/ms740621(VS.85).aspx - LPVOID messageBuffer; - FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language - ( LPTSTR ) & messageBuffer, 0, NULL ); - // something has gone wrong here... - RAKNET_DEBUG_PRINTF( "setsockopt__(SO_BROADCAST) failed:Error code - %d\n%s", dwIOError, messageBuffer ); - //Free the buffer. - LocalFree( messageBuffer ); -#endif -#endif - - } - - } - -#endif -} - - -RakNet::RakString SocketLayer::GetSubNetForSocketAndIp(__UDPSOCKET__ inSock, RakNet::RakString inIpString) -{ - RakNet::RakString netMaskString; - RakNet::RakString ipString; - - - - - -#if defined(WINDOWS_STORE_RT) - RakAssert("Not yet supported" && 0); - return ""; -#elif defined(_WIN32) - INTERFACE_INFO InterfaceList[20]; - unsigned long nBytesReturned; - if (WSAIoctl(inSock, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList, - sizeof(InterfaceList), &nBytesReturned, 0, 0) == SOCKET_ERROR) { - return ""; - } - - int nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO); - - for (int i = 0; i < nNumInterfaces; ++i) - { - sockaddr_in *pAddress; - pAddress = (sockaddr_in *) & (InterfaceList[i].iiAddress); - ipString=inet_ntoa(pAddress->sin_addr); - - if (inIpString==ipString) - { - pAddress = (sockaddr_in *) & (InterfaceList[i].iiNetmask); - netMaskString=inet_ntoa(pAddress->sin_addr); - return netMaskString; - } - } - return ""; -#else - - int fd,fd2; - fd2 = socket__(AF_INET, SOCK_DGRAM, 0); - - if(fd2 < 0) - { - return ""; - } - - struct ifconf ifc; - char buf[1999]; - ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = buf; - if(ioctl(fd2, SIOCGIFCONF, &ifc) < 0) - { - return ""; - } - - struct ifreq *ifr; - ifr = ifc.ifc_req; - int intNum = ifc.ifc_len / sizeof(struct ifreq); - for(int i = 0; i < intNum; i++) - { - ipString=inet_ntoa(((struct sockaddr_in *)&ifr[i].ifr_addr)->sin_addr); - - if (inIpString==ipString) - { - struct ifreq ifr2; - fd = socket__(AF_INET, SOCK_DGRAM, 0); - if(fd < 0) - { - return ""; - } - ifr2.ifr_addr.sa_family = AF_INET; - - strncpy(ifr2.ifr_name, ifr[i].ifr_name, IFNAMSIZ-1); - - ioctl(fd, SIOCGIFNETMASK, &ifr2); - - close(fd); - close(fd2); - netMaskString=inet_ntoa(((struct sockaddr_in *)&ifr2.ifr_addr)->sin_addr); - - return netMaskString; - } - } - - close(fd2); - return ""; - -#endif - -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -#if defined(WINDOWS_STORE_RT) -void GetMyIP_WinRT( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) -{ - // Perhaps DatagramSocket.BindEndpointAsynch, use localHostName as an empty string, then query what it bound to? - RakAssert("Not yet supported" && 0); -} -#else -void GetMyIP_Win32( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) -{ - int idx=0; - idx=0; - char ac[ 80 ]; - if ( gethostname( ac, sizeof( ac ) ) == -1 ) - { - #if defined(_WIN32) && !defined(WINDOWS_PHONE_8) - DWORD dwIOError = GetLastError(); - LPVOID messageBuffer; - FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language - ( LPTSTR ) & messageBuffer, 0, NULL ); - // something has gone wrong here... - RAKNET_DEBUG_PRINTF( "gethostname failed:Error code - %d\n%s", dwIOError, messageBuffer ); - //Free the buffer. - LocalFree( messageBuffer ); - #endif - return ; - } - - -#if RAKNET_SUPPORT_IPV6==1 - struct addrinfo hints; - struct addrinfo *servinfo=0, *aip; // will point to the results - PrepareAddrInfoHints(&hints); - getaddrinfo(ac, "", &hints, &servinfo); - - for (idx=0, aip = servinfo; aip != NULL && idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; aip = aip->ai_next, idx++) - { - if (aip->ai_family == AF_INET) - { - struct sockaddr_in *ipv4 = (struct sockaddr_in *)aip->ai_addr; - memcpy(&addresses[idx].address.addr4,ipv4,sizeof(sockaddr_in)); - } - else - { - struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)aip->ai_addr; - memcpy(&addresses[idx].address.addr4,ipv6,sizeof(sockaddr_in6)); - } - - } - - freeaddrinfo(servinfo); // free the linked-list -#else - struct hostent *phe = gethostbyname( ac ); - - if ( phe == 0 ) - { - #if defined(_WIN32) && !defined(WINDOWS_PHONE_8) - DWORD dwIOError = GetLastError(); - LPVOID messageBuffer; - FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language - ( LPTSTR ) & messageBuffer, 0, NULL ); - // something has gone wrong here... - RAKNET_DEBUG_PRINTF( "gethostbyname failed:Error code - %d\n%s", dwIOError, messageBuffer ); - - //Free the buffer. - LocalFree( messageBuffer ); - #endif - return ; - } - for ( idx = 0; idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ++idx ) - { - if (phe->h_addr_list[ idx ] == 0) - break; - - memcpy(&addresses[idx].address.addr4.sin_addr,phe->h_addr_list[ idx ],sizeof(struct in_addr)); - - } -#endif // else RAKNET_SUPPORT_IPV6==1 - - while (idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS) - { - addresses[idx]=UNASSIGNED_SYSTEM_ADDRESS; - idx++; - } -} - -#endif - - -void SocketLayer::GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) -{ - - - - - - -#if defined(WINDOWS_STORE_RT) - GetMyIP_WinRT(addresses); -#elif defined(_WIN32) - GetMyIP_Win32(addresses); -#else -// GetMyIP_Linux(addresses); - GetMyIP_Win32(addresses); -#endif -} - - -/* -unsigned short SocketLayer::GetLocalPort(RakNetSocket *s) -{ - SystemAddress sa; - GetSystemAddress(s,&sa); - return sa.GetPort(); -} -*/ -unsigned short SocketLayer::GetLocalPort(__UDPSOCKET__ s) -{ - SystemAddress sa; - GetSystemAddress(s,&sa); - return sa.GetPort(); -} -void SocketLayer::GetSystemAddress_Old ( __UDPSOCKET__ s, SystemAddress *systemAddressOut ) -{ -#if defined(__native_client__) - *systemAddressOut = UNASSIGNED_SYSTEM_ADDRESS; -#else - sockaddr_in sa; - memset(&sa,0,sizeof(sockaddr_in)); - socklen_t len = sizeof(sa); - if (getsockname__(s, (sockaddr*)&sa, &len)!=0) - { -#if defined(_WIN32) && defined(_DEBUG) && !defined(WINDOWS_PHONE_8) - DWORD dwIOError = GetLastError(); - LPVOID messageBuffer; - FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language - ( LPTSTR ) & messageBuffer, 0, NULL ); - // something has gone wrong here... - RAKNET_DEBUG_PRINTF( "getsockname failed:Error code - %d\n%s", dwIOError, messageBuffer ); - - //Free the buffer. - LocalFree( messageBuffer ); -#endif - *systemAddressOut = UNASSIGNED_SYSTEM_ADDRESS; - return; - } - - systemAddressOut->SetPortNetworkOrder(sa.sin_port); - systemAddressOut->address.addr4.sin_addr.s_addr=sa.sin_addr.s_addr; -#endif -} -/* -void SocketLayer::GetSystemAddress_Old ( RakNetSocket *s, SystemAddress *systemAddressOut ) -{ - return GetSystemAddress_Old(s->s, systemAddressOut); -} -*/ -void SocketLayer::GetSystemAddress ( __UDPSOCKET__ s, SystemAddress *systemAddressOut ) -{ -#if RAKNET_SUPPORT_IPV6!=1 - GetSystemAddress_Old(s, systemAddressOut); -#else - socklen_t slen; - sockaddr_storage ss; - slen = sizeof(ss); - - if (getsockname__(s, (struct sockaddr *)&ss, &slen)!=0) - { -#if defined(_WIN32) && defined(_DEBUG) - DWORD dwIOError = GetLastError(); - LPVOID messageBuffer; - FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language - ( LPTSTR ) & messageBuffer, 0, NULL ); - // something has gone wrong here... - RAKNET_DEBUG_PRINTF( "getsockname failed:Error code - %d\n%s", dwIOError, messageBuffer ); - - //Free the buffer. - LocalFree( messageBuffer ); -#endif - systemAddressOut->FromString(0); - return; - } - - if (ss.ss_family==AF_INET) - { - memcpy(&systemAddressOut->address.addr4,(sockaddr_in *)&ss,sizeof(sockaddr_in)); - systemAddressOut->debugPort=ntohs(systemAddressOut->address.addr4.sin_port); - - uint32_t zero = 0; - if (memcmp(&systemAddressOut->address.addr4.sin_addr.s_addr, &zero, sizeof(zero))==0) - systemAddressOut->SetToLoopback(4); - // systemAddressOut->address.addr4.sin_port=ntohs(systemAddressOut->address.addr4.sin_port); - } - else - { - memcpy(&systemAddressOut->address.addr6,(sockaddr_in6 *)&ss,sizeof(sockaddr_in6)); - systemAddressOut->debugPort=ntohs(systemAddressOut->address.addr6.sin6_port); - - char zero[16]; - memset(zero,0,sizeof(zero)); - if (memcmp(&systemAddressOut->address.addr4.sin_addr.s_addr, &zero, sizeof(zero))==0) - systemAddressOut->SetToLoopback(6); - - // systemAddressOut->address.addr6.sin6_port=ntohs(systemAddressOut->address.addr6.sin6_port); - } -#endif // #if RAKNET_SUPPORT_IPV6!=1 -} -/* -void SocketLayer::GetSystemAddress ( RakNetSocket *s, SystemAddress *systemAddressOut ) -{ - return GetSystemAddress(s->s, systemAddressOut); -} -*/ - -// void SocketLayer::SetSocketLayerOverride(SocketLayerOverride *_slo) -// { -// slo=_slo; -// } - -bool SocketLayer::GetFirstBindableIP(char firstBindable[128], int ipProto) -{ - SystemAddress ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ]; - SocketLayer::GetMyIP( ipList ); - - - if (ipProto==AF_UNSPEC) - - { - ipList[0].ToString(false,firstBindable); - return true; - } - - // Find the first valid host address - unsigned int l; - for (l=0; l < MAXIMUM_NUMBER_OF_INTERNAL_IDS; l++) - { - if (ipList[l]==UNASSIGNED_SYSTEM_ADDRESS) - break; - if (ipList[l].GetIPVersion()==4 && ipProto==AF_INET) - break; - if (ipList[l].GetIPVersion()==6 && ipProto==AF_INET6) - break; - } - - if (ipList[l]==UNASSIGNED_SYSTEM_ADDRESS || l==MAXIMUM_NUMBER_OF_INTERNAL_IDS) - return false; -// RAKNET_DEBUG_PRINTF("%i %i %i %i\n", -// ((char*)(&ipList[l].address.addr4.sin_addr.s_addr))[0], -// ((char*)(&ipList[l].address.addr4.sin_addr.s_addr))[1], -// ((char*)(&ipList[l].address.addr4.sin_addr.s_addr))[2], -// ((char*)(&ipList[l].address.addr4.sin_addr.s_addr))[3] -// ); - ipList[l].ToString(false,firstBindable); - return true; - -} - - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief SocketLayer class implementation +/// + + +#include "SocketLayer.h" +#include "RakAssert.h" +#include "RakNetTypes.h" +#include "RakPeer.h" +#include "GetTime.h" +#include "LinuxStrings.h" +#include "SocketDefines.h" +#if (defined(__GNUC__) || defined(__GCCXML__)) && !defined(__WIN32__) +#include +#endif + +using namespace RakNet; + +/* +#if defined(__native_client__) +using namespace pp; +#endif +*/ + +#if USE_SLIDING_WINDOW_CONGESTION_CONTROL!=1 +#include "CCRakNetUDT.h" +#else +#include "CCRakNetSlidingWindow.h" +#endif + +//SocketLayerOverride *SocketLayer::slo=0; + +#ifdef _WIN32 +#else +#include // memcpy +#include +#include +#include +#include // error numbers +#include // RAKNET_DEBUG_PRINTF +#if !defined(ANDROID) +#include +#endif +#include +#include +#include +#include +#include + +#endif + + + + + + + + + + + + + +#if defined(_WIN32) +#include "WSAStartupSingleton.h" +#include "WindowsIncludes.h" + +#else +#include +#endif + +#include "RakSleep.h" +#include +#include "Itoa.h" + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +namespace RakNet +{ + extern void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNet::TimeUS timeRead ); + //extern void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSocket* rakNetSocket, RakNet::TimeUS timeRead ); +} + +#ifdef _DEBUG +#include +#endif + + + +// http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#ip4to6 +// http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#getaddrinfo + +#if RAKNET_SUPPORT_IPV6==1 +void PrepareAddrInfoHints(addrinfo *hints) +{ + memset(hints, 0, sizeof (addrinfo)); // make sure the struct is empty + hints->ai_socktype = SOCK_DGRAM; // UDP sockets + hints->ai_flags = AI_PASSIVE; // fill in my IP for me +} +#endif + +void SocketLayer::SetSocketOptions( __UDPSOCKET__ listenSocket, bool blockingSocket, bool setBroadcast) +{ +#ifdef __native_client__ + (void) listenSocket; +#else + int sock_opt = 1; + + // This doubles the max throughput rate + sock_opt=1024*256; + setsockopt__(listenSocket, SOL_SOCKET, SO_RCVBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) ); + + // Immediate hard close. Don't linger the socket, or recreating the socket quickly on Vista fails. + // Fail with voice and xbox + + sock_opt=0; + setsockopt__(listenSocket, SOL_SOCKET, SO_LINGER, ( char * ) & sock_opt, sizeof ( sock_opt ) ); + + + + // This doesn't make much difference: 10% maybe + // Not supported on console 2 + sock_opt=1024*16; + setsockopt__(listenSocket, SOL_SOCKET, SO_SNDBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) ); + + + if (blockingSocket==false) + { +#ifdef _WIN32 + unsigned long nonblocking = 1; + ioctlsocket__(listenSocket, FIONBIO, &nonblocking ); + + + +#else + fcntl( listenSocket, F_SETFL, O_NONBLOCK ); +#endif + } + if (setBroadcast) + { + // Note: Fails with VDP but not xbox + // Set broadcast capable + sock_opt=1; + if ( setsockopt__(listenSocket, SOL_SOCKET, SO_BROADCAST, ( char * ) & sock_opt, sizeof( sock_opt ) ) == -1 ) + { +#if defined(_WIN32) && defined(_DEBUG) +#if !defined(WINDOWS_PHONE_8) + DWORD dwIOError = GetLastError(); + // On Vista, can get WSAEACCESS (10013) + // See http://support.microsoft.com/kb/819124 + // http://blogs.msdn.com/wndp/archive/2007/03/19/winsock-so-exclusiveaddruse-on-vista.aspx + // http://msdn.microsoft.com/en-us/library/ms740621(VS.85).aspx + LPVOID messageBuffer; + FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language + ( LPTSTR ) & messageBuffer, 0, nullptr ); + // something has gone wrong here... + RAKNET_DEBUG_PRINTF( "setsockopt__(SO_BROADCAST) failed:Error code - %d\n%s", dwIOError, messageBuffer ); + //Free the buffer. + LocalFree( messageBuffer ); +#endif +#endif + + } + + } + +#endif +} + + +RakNet::RakString SocketLayer::GetSubNetForSocketAndIp(__UDPSOCKET__ inSock, RakNet::RakString inIpString) +{ + RakNet::RakString netMaskString; + RakNet::RakString ipString; + + + + + +#if defined(WINDOWS_STORE_RT) + RakAssert("Not yet supported" && 0); + return ""; +#elif defined(_WIN32) + INTERFACE_INFO InterfaceList[20]; + unsigned long nBytesReturned; + if (WSAIoctl(inSock, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList, + sizeof(InterfaceList), &nBytesReturned, 0, 0) == SOCKET_ERROR) { + return ""; + } + + int nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO); + + for (int i = 0; i < nNumInterfaces; ++i) + { + sockaddr_in *pAddress; + pAddress = (sockaddr_in *) & (InterfaceList[i].iiAddress); + ipString=inet_ntoa(pAddress->sin_addr); + + if (inIpString==ipString) + { + pAddress = (sockaddr_in *) & (InterfaceList[i].iiNetmask); + netMaskString=inet_ntoa(pAddress->sin_addr); + return netMaskString; + } + } + return ""; +#else + + int fd,fd2; + fd2 = socket__(AF_INET, SOCK_DGRAM, 0); + + if(fd2 < 0) + { + return ""; + } + + struct ifconf ifc; + char buf[1999]; + ifc.ifc_len = sizeof(buf); + ifc.ifc_buf = buf; + if(ioctl(fd2, SIOCGIFCONF, &ifc) < 0) + { + return ""; + } + + struct ifreq *ifr; + ifr = ifc.ifc_req; + int intNum = ifc.ifc_len / sizeof(struct ifreq); + for(int i = 0; i < intNum; i++) + { + ipString=inet_ntoa(((struct sockaddr_in *)&ifr[i].ifr_addr)->sin_addr); + + if (inIpString==ipString) + { + struct ifreq ifr2; + fd = socket__(AF_INET, SOCK_DGRAM, 0); + if(fd < 0) + { + return ""; + } + ifr2.ifr_addr.sa_family = AF_INET; + + strncpy(ifr2.ifr_name, ifr[i].ifr_name, IFNAMSIZ-1); + + ioctl(fd, SIOCGIFNETMASK, &ifr2); + + close(fd); + close(fd2); + netMaskString=inet_ntoa(((struct sockaddr_in *)&ifr2.ifr_addr)->sin_addr); + + return netMaskString; + } + } + + close(fd2); + return ""; + +#endif + +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#if defined(WINDOWS_STORE_RT) +void GetMyIP_WinRT( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) +{ + // Perhaps DatagramSocket.BindEndpointAsynch, use localHostName as an empty string, then query what it bound to? + RakAssert("Not yet supported" && 0); +} +#else +void GetMyIP_Win32( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) +{ + int idx=0; + idx=0; + char ac[ 80 ]; + if ( gethostname( ac, sizeof( ac ) ) == -1 ) + { + #if defined(_WIN32) && !defined(WINDOWS_PHONE_8) + DWORD dwIOError = GetLastError(); + LPVOID messageBuffer; + FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language + ( LPTSTR ) & messageBuffer, 0, nullptr ); + // something has gone wrong here... + RAKNET_DEBUG_PRINTF( "gethostname failed:Error code - %d\n%s", dwIOError, messageBuffer ); + //Free the buffer. + LocalFree( messageBuffer ); + #endif + return ; + } + + +#if RAKNET_SUPPORT_IPV6==1 + struct addrinfo hints; + struct addrinfo *servinfo=0, *aip; // will point to the results + PrepareAddrInfoHints(&hints); + getaddrinfo(ac, "", &hints, &servinfo); + + for (idx=0, aip = servinfo; aip != nullptr && idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; aip = aip->ai_next, idx++) + { + if (aip->ai_family == AF_INET) + { + struct sockaddr_in *ipv4 = (struct sockaddr_in *)aip->ai_addr; + memcpy(&addresses[idx].address.addr4,ipv4,sizeof(sockaddr_in)); + } + else + { + struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)aip->ai_addr; + memcpy(&addresses[idx].address.addr4,ipv6,sizeof(sockaddr_in6)); + } + + } + + freeaddrinfo(servinfo); // free the linked-list +#else + struct hostent *phe = gethostbyname( ac ); + + if ( phe == 0 ) + { + #if defined(_WIN32) && !defined(WINDOWS_PHONE_8) + DWORD dwIOError = GetLastError(); + LPVOID messageBuffer; + FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language + ( LPTSTR ) & messageBuffer, 0, nullptr ); + // something has gone wrong here... + RAKNET_DEBUG_PRINTF( "gethostbyname failed:Error code - %d\n%s", dwIOError, messageBuffer ); + + //Free the buffer. + LocalFree( messageBuffer ); + #endif + return ; + } + for ( idx = 0; idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ++idx ) + { + if (phe->h_addr_list[ idx ] == 0) + break; + + memcpy(&addresses[idx].address.addr4.sin_addr,phe->h_addr_list[ idx ],sizeof(struct in_addr)); + + } +#endif // else RAKNET_SUPPORT_IPV6==1 + + while (idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS) + { + addresses[idx]=UNASSIGNED_SYSTEM_ADDRESS; + idx++; + } +} + +#endif + + +void SocketLayer::GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ) +{ + + + + + + +#if defined(WINDOWS_STORE_RT) + GetMyIP_WinRT(addresses); +#elif defined(_WIN32) + GetMyIP_Win32(addresses); +#else +// GetMyIP_Linux(addresses); + GetMyIP_Win32(addresses); +#endif +} + + +/* +unsigned short SocketLayer::GetLocalPort(RakNetSocket *s) +{ + SystemAddress sa; + GetSystemAddress(s,&sa); + return sa.GetPort(); +} +*/ +unsigned short SocketLayer::GetLocalPort(__UDPSOCKET__ s) +{ + SystemAddress sa; + GetSystemAddress(s,&sa); + return sa.GetPort(); +} +void SocketLayer::GetSystemAddress_Old ( __UDPSOCKET__ s, SystemAddress *systemAddressOut ) +{ +#if defined(__native_client__) + *systemAddressOut = UNASSIGNED_SYSTEM_ADDRESS; +#else + sockaddr_in sa; + memset(&sa,0,sizeof(sockaddr_in)); + socklen_t len = sizeof(sa); + if (getsockname__(s, (sockaddr*)&sa, &len)!=0) + { +#if defined(_WIN32) && defined(_DEBUG) && !defined(WINDOWS_PHONE_8) + DWORD dwIOError = GetLastError(); + LPVOID messageBuffer; + FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language + ( LPTSTR ) & messageBuffer, 0, nullptr ); + // something has gone wrong here... + RAKNET_DEBUG_PRINTF( "getsockname failed:Error code - %d\n%s", dwIOError, messageBuffer ); + + //Free the buffer. + LocalFree( messageBuffer ); +#endif + *systemAddressOut = UNASSIGNED_SYSTEM_ADDRESS; + return; + } + + systemAddressOut->SetPortNetworkOrder(sa.sin_port); + systemAddressOut->address.addr4.sin_addr.s_addr=sa.sin_addr.s_addr; +#endif +} +/* +void SocketLayer::GetSystemAddress_Old ( RakNetSocket *s, SystemAddress *systemAddressOut ) +{ + return GetSystemAddress_Old(s->s, systemAddressOut); +} +*/ +void SocketLayer::GetSystemAddress ( __UDPSOCKET__ s, SystemAddress *systemAddressOut ) +{ +#if RAKNET_SUPPORT_IPV6!=1 + GetSystemAddress_Old(s, systemAddressOut); +#else + socklen_t slen; + sockaddr_storage ss; + slen = sizeof(ss); + + if (getsockname__(s, (struct sockaddr *)&ss, &slen)!=0) + { +#if defined(_WIN32) && defined(_DEBUG) + DWORD dwIOError = GetLastError(); + LPVOID messageBuffer; + FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language + ( LPTSTR ) & messageBuffer, 0, nullptr ); + // something has gone wrong here... + RAKNET_DEBUG_PRINTF( "getsockname failed:Error code - %d\n%s", dwIOError, messageBuffer ); + + //Free the buffer. + LocalFree( messageBuffer ); +#endif + systemAddressOut->FromString(0); + return; + } + + if (ss.ss_family==AF_INET) + { + memcpy(&systemAddressOut->address.addr4,(sockaddr_in *)&ss,sizeof(sockaddr_in)); + systemAddressOut->debugPort=ntohs(systemAddressOut->address.addr4.sin_port); + + uint32_t zero = 0; + if (memcmp(&systemAddressOut->address.addr4.sin_addr.s_addr, &zero, sizeof(zero))==0) + systemAddressOut->SetToLoopback(4); + // systemAddressOut->address.addr4.sin_port=ntohs(systemAddressOut->address.addr4.sin_port); + } + else + { + memcpy(&systemAddressOut->address.addr6,(sockaddr_in6 *)&ss,sizeof(sockaddr_in6)); + systemAddressOut->debugPort=ntohs(systemAddressOut->address.addr6.sin6_port); + + char zero[16]; + memset(zero,0,sizeof(zero)); + if (memcmp(&systemAddressOut->address.addr4.sin_addr.s_addr, &zero, sizeof(zero))==0) + systemAddressOut->SetToLoopback(6); + + // systemAddressOut->address.addr6.sin6_port=ntohs(systemAddressOut->address.addr6.sin6_port); + } +#endif // #if RAKNET_SUPPORT_IPV6!=1 +} +/* +void SocketLayer::GetSystemAddress ( RakNetSocket *s, SystemAddress *systemAddressOut ) +{ + return GetSystemAddress(s->s, systemAddressOut); +} +*/ + +// void SocketLayer::SetSocketLayerOverride(SocketLayerOverride *_slo) +// { +// slo=_slo; +// } + +bool SocketLayer::GetFirstBindableIP(char firstBindable[128], int ipProto) +{ + SystemAddress ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ]; + SocketLayer::GetMyIP( ipList ); + + + if (ipProto==AF_UNSPEC) + + { + ipList[0].ToString(false,firstBindable); + return true; + } + + // Find the first valid host address + unsigned int l; + for (l=0; l < MAXIMUM_NUMBER_OF_INTERNAL_IDS; l++) + { + if (ipList[l]==UNASSIGNED_SYSTEM_ADDRESS) + break; + if (ipList[l].GetIPVersion()==4 && ipProto==AF_INET) + break; + if (ipList[l].GetIPVersion()==6 && ipProto==AF_INET6) + break; + } + + if (ipList[l]==UNASSIGNED_SYSTEM_ADDRESS || l==MAXIMUM_NUMBER_OF_INTERNAL_IDS) + return false; +// RAKNET_DEBUG_PRINTF("%i %i %i %i\n", +// ((char*)(&ipList[l].address.addr4.sin_addr.s_addr))[0], +// ((char*)(&ipList[l].address.addr4.sin_addr.s_addr))[1], +// ((char*)(&ipList[l].address.addr4.sin_addr.s_addr))[2], +// ((char*)(&ipList[l].address.addr4.sin_addr.s_addr))[3] +// ); + ipList[l].ToString(false,firstBindable); + return true; + +} + + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif diff --git a/Source/SocketLayer.h b/Source/SocketLayer.h index 7979fe357..72ab7c148 100644 --- a/Source/SocketLayer.h +++ b/Source/SocketLayer.h @@ -1,197 +1,195 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief SocketLayer class implementation -/// - - - - -#ifndef __SOCKET_LAYER_H -#define __SOCKET_LAYER_H - -#include "RakMemoryOverride.h" -#include "RakNetTypes.h" -#include "RakNetSmartPtr.h" -//#include "RakNetSocket.h" -#include "Export.h" -#include "MTUSize.h" -#include "RakString.h" - -//#include "ClientContextStruct.h" - -namespace RakNet -{ -/// Forward declarations -class RakPeer; - -/* -class RAK_DLL_EXPORT SocketLayerOverride -{ -public: - SocketLayerOverride() {} - virtual ~SocketLayerOverride() {} - - /// Called when SendTo would otherwise occur. - virtual int RakNetSendTo( const char *data, int length, const SystemAddress &systemAddress )=0; - - /// Called when RecvFrom would otherwise occur. Return number of bytes read. Write data into dataOut - // Return -1 to use RakNet's normal recvfrom, 0 to abort RakNet's normal recvfrom, and positive to return data - virtual int RakNetRecvFrom( char dataOut[ MAXIMUM_MTU_SIZE ], SystemAddress *senderOut, bool calledFromMainThread )=0; -}; -*/ - -// A platform independent implementation of Berkeley sockets, with settings used by RakNet -class RAK_DLL_EXPORT SocketLayer -{ - -public: - - /// Default Constructor - SocketLayer(); - - // Destructor - ~SocketLayer(); - - /* - /// Creates a bound socket to listen for incoming connections on the specified port - /// \param[in] port the port number - /// \param[in] blockingSocket - /// \return A new socket used for accepting clients - static RakNetSocket* CreateBoundSocket( RakPeer *peer, unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned int sleepOn10048, unsigned int extraSocketOptions, unsigned short socketFamily, _PP_Instance_ chromeInstance ); -#if defined(WINDOWS_STORE_RT) - static RakNetSocket* CreateWindowsStore8Socket( RakPeer *peer, unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned int sleepOn10048, unsigned int extraSocketOptions, _PP_Instance_ chromeInstance ); -#endif - static RakNetSocket* CreateBoundSocket_IPV4( RakPeer *peer, unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned int sleepOn10048, unsigned int extraSocketOptions, _PP_Instance_ chromeInstance ); - #if RAKNET_SUPPORT_IPV6==1 - static RakNetSocket* CreateBoundSocket_SupportIPV4And6( RakPeer *peer, unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned int sleepOn10048, unsigned int extraSocketOptions, unsigned short socketFamily, _PP_Instance_ chromeInstance ); - #endif - static RakNetSocket* CreateBoundSocket_PS3Lobby( unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned short socketFamily ); - static RakNetSocket* CreateBoundSocket_PSP2( unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned short socketFamily ); - */ - - /* -#ifndef WINDOWS_STORE_RT - /// Returns if this specified port is in use, for UDP - /// \param[in] port the port number - /// \return If this port is already in use - //static bool IsPortInUse_Old(unsigned short port, const char *hostAddress); - //static bool IsPortInUse(unsigned short port, const char *hostAddress, unsigned short socketFamily ); - static bool IsSocketFamilySupported(const char *hostAddress, unsigned short socketFamily); -#endif - */ - -// static const char* DomainNameToIP_Old( const char *domainName ); -// static const char* DomainNameToIP( const char *domainName ); - - /// Write \a data of length \a length to \a writeSocket - /// \param[in] writeSocket The socket to write to - /// \param[in] data The data to write - /// \param[in] length The length of \a data - // static void Write( RakNetSocket*writeSocket, const char* data, const int length ); - - /// Read data from a socket - /// \param[in] s the socket - /// \param[in] rakPeer The instance of rakPeer containing the recvFrom C callback - /// \param[in] errorCode An error code if an error occured . - /// \param[in] connectionSocketIndex Which of the sockets in RakPeer we are using - /// \return Returns true if you successfully read data, false on error. -// static void RecvFromBlocking_IPV4( RakNetSocket *s, RakPeer *rakPeer, char *dataOut, int *bytesReadOut, SystemAddress *systemAddressOut, RakNet::TimeUS *timeRead ); -// #if RAKNET_SUPPORT_IPV6==1 -// static void RecvFromBlockingIPV4And6( RakNetSocket *s, RakPeer *rakPeer, char *dataOut, int *bytesReadOut, SystemAddress *systemAddressOut, RakNet::TimeUS *timeRead ); -// #endif -// static void RecvFromBlocking( RakNetSocket *s, RakPeer *rakPeer, char *dataOut, int *bytesReadOut, SystemAddress *systemAddressOut, RakNet::TimeUS *timeRead ); -#if defined(WINDOWS_STORE_RT) -// static void RecvFromBlocking_WindowsStore8( RakNetSocket *s, RakPeer *rakPeer, char *dataOut, int *bytesReadOut, SystemAddress *systemAddressOut, RakNet::TimeUS *timeRead ); -#endif - - /// Given a socket and IP, retrieves the subnet mask, on linux the socket is unused - /// \param[in] inSock the socket - /// \param[in] inIpString The ip of the interface you wish to retrieve the subnet mask from - /// \return Returns the ip dotted subnet mask if successful, otherwise returns empty string ("") - static RakNet::RakString GetSubNetForSocketAndIp(__UDPSOCKET__ inSock, RakNet::RakString inIpString); - - - /// Sets the socket flags to nonblocking - /// \param[in] listenSocket the socket to set -// static void SetNonBlocking( RakNetSocket* listenSocket); - - - /// Retrieve all local IP address in a string format. - /// \param[in] s The socket whose port we are referring to - /// \param[in] ipList An array of ip address in dotted notation. - static void GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); - - - /// Call sendto (UDP obviously) - /// \param[in] s the socket - /// \param[in] data The byte buffer to send - /// \param[in] length The length of the \a data in bytes - /// \param[in] ip The address of the remote host in dotted notation. - /// \param[in] port The port number to send to. - /// \return 0 on success, nonzero on failure. -// static int SendTo( UDPSOCKET s, const char *data, int length, const char ip[ 16 ], unsigned short port, unsigned short remotePortRakNetWasStartedOn_PS3, unsigned int extraSocketOptions, const char *file, const long line ); - - /// Call sendto' (UDP obviously) - /// It won't reach the recipient, except on a LAN - /// However, this is good for opening routers / firewalls - /// \param[in] s the socket - /// \param[in] data The byte buffer to send - /// \param[in] length The length of the \a data in bytes - /// \param[in] ip The address of the remote host in dotted notation. - /// \param[in] port The port number to send to. - /// \param[in] ttl Max hops of datagram - /// \return 0 on success, nonzero on failure. -// static int SendToTTL( RakNetSocket *s, const char *data, int length, SystemAddress &systemAddress, int ttl ); - - /// Call sendto (UDP obviously) - /// \param[in] s the socket - /// \param[in] data The byte buffer to send - /// \param[in] length The length of the \a data in bytes - /// \param[in] binaryAddress The address of the remote host in binary format. - /// \param[in] port The port number to send to. - /// \return 0 on success, nonzero on failure. -// static int SendTo( RakNetSocket *s, const char *data, int length, SystemAddress systemAddress, const char *file, const long line ); - -// static unsigned short GetLocalPort(RakNetSocket *s); - static unsigned short GetLocalPort( __UDPSOCKET__ s); -// static void GetSystemAddress_Old ( RakNetSocket *s, SystemAddress *systemAddressOut ); - static void GetSystemAddress_Old ( __UDPSOCKET__ s, SystemAddress *systemAddressOut ); -// static void GetSystemAddress ( RakNetSocket *s, SystemAddress *systemAddressOut ); - static void GetSystemAddress ( __UDPSOCKET__ s, SystemAddress *systemAddressOut ); - -// static void SetSocketLayerOverride(SocketLayerOverride *_slo); -// static SocketLayerOverride* GetSocketLayerOverride(void) {return slo;} - -// static int SendTo_PS3Lobby( RakNetSocket *s, const char *data, int length, const SystemAddress &systemAddress ); -// static int SendTo_PSP2( RakNetSocket *s, const char *data, int length, const SystemAddress &systemAddress ); -// static int SendTo_360( RakNetSocket *s, const char *data, int length, const char *voiceData, int voiceLength, const SystemAddress &systemAddress ); -// static int SendTo_PC( RakNetSocket *s, const char *data, int length, const SystemAddress &systemAddress, const char *file, const long line ); -// #if defined(WINDOWS_STORE_RT) -// static int SendTo_WindowsStore8( RakNetSocket *s, const char *data, int length, const SystemAddress &systemAddress, const char *file, const long line ); -// #endif -// -// static void SetDoNotFragment( RakNetSocket* listenSocket, int opt ); -// static void SetSocketOptions( RakNetSocket* listenSocket, bool blockingSocket, bool setBroadcast); - static void SetSocketOptions( __UDPSOCKET__ listenSocket, bool blockingSocket, bool setBroadcast); - - - // AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC. - static bool GetFirstBindableIP(char firstBindable[128], int ipProto); - -private: - -// static SocketLayerOverride *slo; -}; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief SocketLayer class implementation +/// + + + + +#pragma once + +#include "RakMemoryOverride.h" +#include "RakNetTypes.h" +#include "RakNetSmartPtr.h" +//#include "RakNetSocket.h" +#include "Export.h" +#include "MTUSize.h" +#include "RakString.h" + +//#include "ClientContextStruct.h" + +namespace RakNet +{ +/// Forward declarations +class RakPeer; + +/* +class RAK_DLL_EXPORT SocketLayerOverride +{ +public: + SocketLayerOverride() {} + virtual ~SocketLayerOverride() {} + + /// Called when SendTo would otherwise occur. + virtual int RakNetSendTo( const char *data, int length, const SystemAddress &systemAddress )=0; + + /// Called when RecvFrom would otherwise occur. Return number of bytes read. Write data into dataOut + // Return -1 to use RakNet's normal recvfrom, 0 to abort RakNet's normal recvfrom, and positive to return data + virtual int RakNetRecvFrom( char dataOut[ MAXIMUM_MTU_SIZE ], SystemAddress *senderOut, bool calledFromMainThread )=0; +}; +*/ + +// A platform independent implementation of Berkeley sockets, with settings used by RakNet +class RAK_DLL_EXPORT SocketLayer +{ + +public: + + /// Default Constructor + SocketLayer(); + + // Destructor + ~SocketLayer(); + + /* + /// Creates a bound socket to listen for incoming connections on the specified port + /// \param[in] port the port number + /// \param[in] blockingSocket + /// \return A new socket used for accepting clients + static RakNetSocket* CreateBoundSocket( RakPeer *peer, unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned int sleepOn10048, unsigned int extraSocketOptions, unsigned short socketFamily, _PP_Instance_ chromeInstance ); +#if defined(WINDOWS_STORE_RT) + static RakNetSocket* CreateWindowsStore8Socket( RakPeer *peer, unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned int sleepOn10048, unsigned int extraSocketOptions, _PP_Instance_ chromeInstance ); +#endif + static RakNetSocket* CreateBoundSocket_IPV4( RakPeer *peer, unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned int sleepOn10048, unsigned int extraSocketOptions, _PP_Instance_ chromeInstance ); + #if RAKNET_SUPPORT_IPV6==1 + static RakNetSocket* CreateBoundSocket_SupportIPV4And6( RakPeer *peer, unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned int sleepOn10048, unsigned int extraSocketOptions, unsigned short socketFamily, _PP_Instance_ chromeInstance ); + #endif + static RakNetSocket* CreateBoundSocket_PS3Lobby( unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned short socketFamily ); + static RakNetSocket* CreateBoundSocket_PSP2( unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned short socketFamily ); + */ + + /* +#ifndef WINDOWS_STORE_RT + /// Returns if this specified port is in use, for UDP + /// \param[in] port the port number + /// \return If this port is already in use + //static bool IsPortInUse_Old(unsigned short port, const char *hostAddress); + //static bool IsPortInUse(unsigned short port, const char *hostAddress, unsigned short socketFamily ); + static bool IsSocketFamilySupported(const char *hostAddress, unsigned short socketFamily); +#endif + */ + +// static const char* DomainNameToIP_Old( const char *domainName ); +// static const char* DomainNameToIP( const char *domainName ); + + /// Write \a data of length \a length to \a writeSocket + /// \param[in] writeSocket The socket to write to + /// \param[in] data The data to write + /// \param[in] length The length of \a data + // static void Write( RakNetSocket*writeSocket, const char* data, const int length ); + + /// Read data from a socket + /// \param[in] s the socket + /// \param[in] rakPeer The instance of rakPeer containing the recvFrom C callback + /// \param[in] errorCode An error code if an error occured . + /// \param[in] connectionSocketIndex Which of the sockets in RakPeer we are using + /// \return Returns true if you successfully read data, false on error. +// static void RecvFromBlocking_IPV4( RakNetSocket *s, RakPeer *rakPeer, char *dataOut, int *bytesReadOut, SystemAddress *systemAddressOut, RakNet::TimeUS *timeRead ); +// #if RAKNET_SUPPORT_IPV6==1 +// static void RecvFromBlockingIPV4And6( RakNetSocket *s, RakPeer *rakPeer, char *dataOut, int *bytesReadOut, SystemAddress *systemAddressOut, RakNet::TimeUS *timeRead ); +// #endif +// static void RecvFromBlocking( RakNetSocket *s, RakPeer *rakPeer, char *dataOut, int *bytesReadOut, SystemAddress *systemAddressOut, RakNet::TimeUS *timeRead ); +#if defined(WINDOWS_STORE_RT) +// static void RecvFromBlocking_WindowsStore8( RakNetSocket *s, RakPeer *rakPeer, char *dataOut, int *bytesReadOut, SystemAddress *systemAddressOut, RakNet::TimeUS *timeRead ); +#endif + + /// Given a socket and IP, retrieves the subnet mask, on linux the socket is unused + /// \param[in] inSock the socket + /// \param[in] inIpString The ip of the interface you wish to retrieve the subnet mask from + /// \return Returns the ip dotted subnet mask if successful, otherwise returns empty string ("") + static RakNet::RakString GetSubNetForSocketAndIp(__UDPSOCKET__ inSock, RakNet::RakString inIpString); + + + /// Sets the socket flags to nonblocking + /// \param[in] listenSocket the socket to set +// static void SetNonBlocking( RakNetSocket* listenSocket); + + + /// Retrieve all local IP address in a string format. + /// \param[in] s The socket whose port we are referring to + /// \param[in] ipList An array of ip address in dotted notation. + static void GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] ); + + + /// Call sendto (UDP obviously) + /// \param[in] s the socket + /// \param[in] data The byte buffer to send + /// \param[in] length The length of the \a data in bytes + /// \param[in] ip The address of the remote host in dotted notation. + /// \param[in] port The port number to send to. + /// \return 0 on success, nonzero on failure. +// static int SendTo( UDPSOCKET s, const char *data, int length, const char ip[ 16 ], unsigned short port, unsigned short remotePortRakNetWasStartedOn_PS3, unsigned int extraSocketOptions, const char *file, const long line ); + + /// Call sendto' (UDP obviously) + /// It won't reach the recipient, except on a LAN + /// However, this is good for opening routers / firewalls + /// \param[in] s the socket + /// \param[in] data The byte buffer to send + /// \param[in] length The length of the \a data in bytes + /// \param[in] ip The address of the remote host in dotted notation. + /// \param[in] port The port number to send to. + /// \param[in] ttl Max hops of datagram + /// \return 0 on success, nonzero on failure. +// static int SendToTTL( RakNetSocket *s, const char *data, int length, SystemAddress &systemAddress, int ttl ); + + /// Call sendto (UDP obviously) + /// \param[in] s the socket + /// \param[in] data The byte buffer to send + /// \param[in] length The length of the \a data in bytes + /// \param[in] binaryAddress The address of the remote host in binary format. + /// \param[in] port The port number to send to. + /// \return 0 on success, nonzero on failure. +// static int SendTo( RakNetSocket *s, const char *data, int length, SystemAddress systemAddress, const char *file, const long line ); + +// static unsigned short GetLocalPort(RakNetSocket *s); + static unsigned short GetLocalPort( __UDPSOCKET__ s); +// static void GetSystemAddress_Old ( RakNetSocket *s, SystemAddress *systemAddressOut ); + static void GetSystemAddress_Old ( __UDPSOCKET__ s, SystemAddress *systemAddressOut ); +// static void GetSystemAddress ( RakNetSocket *s, SystemAddress *systemAddressOut ); + static void GetSystemAddress ( __UDPSOCKET__ s, SystemAddress *systemAddressOut ); + +// static void SetSocketLayerOverride(SocketLayerOverride *_slo); +// static SocketLayerOverride* GetSocketLayerOverride(void) {return slo;} + +// static int SendTo_PS3Lobby( RakNetSocket *s, const char *data, int length, const SystemAddress &systemAddress ); +// static int SendTo_PSP2( RakNetSocket *s, const char *data, int length, const SystemAddress &systemAddress ); +// static int SendTo_360( RakNetSocket *s, const char *data, int length, const char *voiceData, int voiceLength, const SystemAddress &systemAddress ); +// static int SendTo_PC( RakNetSocket *s, const char *data, int length, const SystemAddress &systemAddress, const char *file, const long line ); +// #if defined(WINDOWS_STORE_RT) +// static int SendTo_WindowsStore8( RakNetSocket *s, const char *data, int length, const SystemAddress &systemAddress, const char *file, const long line ); +// #endif +// +// static void SetDoNotFragment( RakNetSocket* listenSocket, int opt ); +// static void SetSocketOptions( RakNetSocket* listenSocket, bool blockingSocket, bool setBroadcast); + static void SetSocketOptions( __UDPSOCKET__ listenSocket, bool blockingSocket, bool setBroadcast); + + + // AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC. + static bool GetFirstBindableIP(char firstBindable[128], int ipProto); + +private: + +// static SocketLayerOverride *slo; +}; + +} // namespace RakNet + diff --git a/Source/StatisticsHistory.h b/Source/StatisticsHistory.h index a4b75e4c9..a7d7cdbe5 100644 --- a/Source/StatisticsHistory.h +++ b/Source/StatisticsHistory.h @@ -1,235 +1,233 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file StatisticsHistory.h -/// \brief Input numerical values over time. Get sum, average, highest, lowest, standard deviation on recent or all-time values - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_StatisticsHistory==1 - -#ifndef __STATISTICS_HISTORY_H -#define __STATISTICS_HISTORY_H - -#include "PluginInterface2.h" -#include "RakMemoryOverride.h" -#include "NativeTypes.h" -#include "DS_List.h" -#include "RakNetTypes.h" -#include "DS_OrderedList.h" -#include "RakString.h" -#include "DS_Queue.h" -#include "DS_Hash.h" -#include - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; - -// Type used to track values. If needed, change to double and recompile -typedef double SHValueType; -#define SH_TYPE_MAX DBL_MAX - -/// \brief Input numerical values over time. Get sum, average, highest, lowest, standard deviation on recent or all-time values -class RAK_DLL_EXPORT StatisticsHistory -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(StatisticsHistory) - - enum SHErrorCode - { - SH_OK, - SH_UKNOWN_OBJECT, - SH_UKNOWN_KEY, - SH_INVALID_PARAMETER, - }; - - enum SHSortOperation - { - SH_DO_NOT_SORT, - - SH_SORT_BY_RECENT_SUM_ASCENDING, - SH_SORT_BY_RECENT_SUM_DESCENDING, - SH_SORT_BY_LONG_TERM_SUM_ASCENDING, - SH_SORT_BY_LONG_TERM_SUM_DESCENDING, - SH_SORT_BY_RECENT_SUM_OF_SQUARES_ASCENDING, - SH_SORT_BY_RECENT_SUM_OF_SQUARES_DESCENDING, - SH_SORT_BY_RECENT_AVERAGE_ASCENDING, - SH_SORT_BY_RECENT_AVERAGE_DESCENDING, - SH_SORT_BY_LONG_TERM_AVERAGE_ASCENDING, - SH_SORT_BY_LONG_TERM_AVERAGE_DESCENDING, - SH_SORT_BY_RECENT_HIGHEST_ASCENDING, - SH_SORT_BY_RECENT_HIGHEST_DESCENDING, - SH_SORT_BY_RECENT_LOWEST_ASCENDING, - SH_SORT_BY_RECENT_LOWEST_DESCENDING, - SH_SORT_BY_LONG_TERM_HIGHEST_ASCENDING, - SH_SORT_BY_LONG_TERM_HIGHEST_DESCENDING, - SH_SORT_BY_LONG_TERM_LOWEST_ASCENDING, - SH_SORT_BY_LONG_TERM_LOWEST_DESCENDING, - }; - - enum SHDataCategory - { - /// Insert values from one set into the other set, in time order - /// Values at the same time end up in the final set twice - /// Use when you have additional data points to add to a graph - DC_DISCRETE, - - /// Add values from one set to values from the other set, at corresponding times - /// If value at time t does not exist in the other set, linearly extrapolate value for other set based on nearest two data points - /// longTerm* values are unknown using this method - /// Use to add two graphs together - DC_CONTINUOUS - }; - - struct TimeAndValue; - struct TimeAndValueQueue; - - struct TrackedObjectData - { - TrackedObjectData(); - TrackedObjectData(uint64_t _objectId, int _objectType, void *_userData); - uint64_t objectId; - int objectType; - void *userData; - }; - - StatisticsHistory(); - virtual ~StatisticsHistory(); - void SetDefaultTimeToTrack(Time defaultTimeToTrack); - Time GetDefaultTimeToTrack(void) const; - bool AddObject(TrackedObjectData tod); - bool RemoveObject(uint64_t objectId, void **userData); - void RemoveObjectAtIndex(unsigned int index); - void Clear(void); - unsigned int GetObjectCount(void) const; - StatisticsHistory::TrackedObjectData * GetObjectAtIndex(unsigned int index) const; - unsigned int GetObjectIndex(uint64_t objectId) const; - bool AddValueByObjectID(uint64_t objectId, RakString key, SHValueType val, Time curTime, bool combineEqualTimes); - void AddValueByIndex(unsigned int index, RakString key, SHValueType val, Time curTime, bool combineEqualTimes); - SHErrorCode GetHistoryForKey(uint64_t objectId, RakString key, TimeAndValueQueue **values, Time curTime) const; - bool GetHistorySorted(uint64_t objectId, SHSortOperation sortType, DataStructures::List &values) const; - void MergeAllObjectsOnKey(RakString key, TimeAndValueQueue *tavqOutput, SHDataCategory dataCategory) const; - void GetUniqueKeyList(DataStructures::List &keys); - - struct TimeAndValue - { - Time time; - SHValueType val; - }; - - struct TimeAndValueQueue - { - TimeAndValueQueue(); - ~TimeAndValueQueue(); - - DataStructures::Queue values; - - Time timeToTrackValues; - RakString key; - - SHValueType recentSum; - SHValueType recentSumOfSquares; - SHValueType longTermSum; - SHValueType longTermCount; - SHValueType longTermLowest; - SHValueType longTermHighest; - - void SetTimeToTrackValues(Time t); - Time GetTimeToTrackValues(void) const; - SHValueType GetRecentSum(void) const; - SHValueType GetRecentSumOfSquares(void) const; - SHValueType GetLongTermSum(void) const; - SHValueType GetRecentAverage(void) const; - SHValueType GetRecentLowest(void) const; - SHValueType GetRecentHighest(void) const; - SHValueType GetRecentStandardDeviation(void) const; - SHValueType GetLongTermAverage(void) const; - SHValueType GetLongTermLowest(void) const; - SHValueType GetLongTermHighest(void) const; - SHValueType GetSumSinceTime(Time t) const; - Time GetTimeRange(void) const; - - // Merge two sets to output - static void MergeSets( const TimeAndValueQueue *lhs, SHDataCategory lhsDataCategory, const TimeAndValueQueue *rhs, SHDataCategory rhsDataCategory, TimeAndValueQueue *output ); - - // Shrink or expand a sample set to the approximate number given - // DC_DISCRETE will produce a histogram (sum) while DC_CONTINUOUS will produce an average - void ResizeSampleSet( int approximateSamples, DataStructures::Queue &blendedSamples, SHDataCategory dataCategory, Time timeClipStart=0, Time timeClipEnd=0 ); - - // Clear out all values - void Clear(void); - - TimeAndValueQueue& operator = ( const TimeAndValueQueue& input ); - - /// \internal - void CullExpiredValues(Time curTime); - /// \internal - static SHValueType Interpolate(TimeAndValue t1, TimeAndValue t2, Time time); - /// \internal - SHValueType sortValue; - }; - -protected: - struct TrackedObject; -public: - static int TrackedObjectComp( const uint64_t &key, TrackedObject* const &data ); -protected: - - struct TrackedObject - { - TrackedObject(); - ~TrackedObject(); - TrackedObjectData trackedObjectData; - DataStructures::Hash dataQueues; - }; - - DataStructures::OrderedList objects; - - Time timeToTrack; -}; - -/// \brief Input numerical values over time. Get sum, average, highest, lowest, standard deviation on recent or all-time values -/// \ingroup PLUGINS_GROUP -class RAK_DLL_EXPORT StatisticsHistoryPlugin : public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(StatisticsHistoryPlugin) - - StatisticsHistory statistics; - - StatisticsHistoryPlugin(); - virtual ~StatisticsHistoryPlugin(); - void SetTrackConnections(bool _addNewConnections, int newConnectionsObjectType, bool _removeLostConnections); - -protected: - virtual void Update(void); - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); - - // Too slow -// virtual bool UsesReliabilityLayer(void) const {return true;} -// virtual void OnDirectSocketSend(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress); -// virtual void OnDirectSocketReceive(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress); - - - bool addNewConnections; - bool removeLostConnections; - int newConnectionsObjectType; -}; - -} // namespace RakNet - -#endif // __STATISTICS_HISTORY_H - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file StatisticsHistory.h +/// \brief Input numerical values over time. Get sum, average, highest, lowest, standard deviation on recent or all-time values + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_StatisticsHistory==1 + +#pragma once + +#include "PluginInterface2.h" +#include "RakMemoryOverride.h" +#include "NativeTypes.h" +#include "DS_List.h" +#include "RakNetTypes.h" +#include "DS_OrderedList.h" +#include "RakString.h" +#include "DS_Queue.h" +#include "DS_Hash.h" +#include + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; + +// Type used to track values. If needed, change to double and recompile +typedef double SHValueType; +#define SH_TYPE_MAX DBL_MAX + +/// \brief Input numerical values over time. Get sum, average, highest, lowest, standard deviation on recent or all-time values +class RAK_DLL_EXPORT StatisticsHistory +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(StatisticsHistory) + + enum SHErrorCode + { + SH_OK, + SH_UKNOWN_OBJECT, + SH_UKNOWN_KEY, + SH_INVALID_PARAMETER, + }; + + enum SHSortOperation + { + SH_DO_NOT_SORT, + + SH_SORT_BY_RECENT_SUM_ASCENDING, + SH_SORT_BY_RECENT_SUM_DESCENDING, + SH_SORT_BY_LONG_TERM_SUM_ASCENDING, + SH_SORT_BY_LONG_TERM_SUM_DESCENDING, + SH_SORT_BY_RECENT_SUM_OF_SQUARES_ASCENDING, + SH_SORT_BY_RECENT_SUM_OF_SQUARES_DESCENDING, + SH_SORT_BY_RECENT_AVERAGE_ASCENDING, + SH_SORT_BY_RECENT_AVERAGE_DESCENDING, + SH_SORT_BY_LONG_TERM_AVERAGE_ASCENDING, + SH_SORT_BY_LONG_TERM_AVERAGE_DESCENDING, + SH_SORT_BY_RECENT_HIGHEST_ASCENDING, + SH_SORT_BY_RECENT_HIGHEST_DESCENDING, + SH_SORT_BY_RECENT_LOWEST_ASCENDING, + SH_SORT_BY_RECENT_LOWEST_DESCENDING, + SH_SORT_BY_LONG_TERM_HIGHEST_ASCENDING, + SH_SORT_BY_LONG_TERM_HIGHEST_DESCENDING, + SH_SORT_BY_LONG_TERM_LOWEST_ASCENDING, + SH_SORT_BY_LONG_TERM_LOWEST_DESCENDING, + }; + + enum SHDataCategory + { + /// Insert values from one set into the other set, in time order + /// Values at the same time end up in the final set twice + /// Use when you have additional data points to add to a graph + DC_DISCRETE, + + /// Add values from one set to values from the other set, at corresponding times + /// If value at time t does not exist in the other set, linearly extrapolate value for other set based on nearest two data points + /// longTerm* values are unknown using this method + /// Use to add two graphs together + DC_CONTINUOUS + }; + + struct TimeAndValue; + struct TimeAndValueQueue; + + struct TrackedObjectData + { + TrackedObjectData(); + TrackedObjectData(uint64_t _objectId, int _objectType, void *_userData); + uint64_t objectId; + int objectType; + void *userData; + }; + + StatisticsHistory(); + virtual ~StatisticsHistory(); + void SetDefaultTimeToTrack(Time defaultTimeToTrack); + Time GetDefaultTimeToTrack(void) const; + bool AddObject(TrackedObjectData tod); + bool RemoveObject(uint64_t objectId, void **userData); + void RemoveObjectAtIndex(unsigned int index); + void Clear(void); + unsigned int GetObjectCount(void) const; + StatisticsHistory::TrackedObjectData * GetObjectAtIndex(unsigned int index) const; + unsigned int GetObjectIndex(uint64_t objectId) const; + bool AddValueByObjectID(uint64_t objectId, RakString key, SHValueType val, Time curTime, bool combineEqualTimes); + void AddValueByIndex(unsigned int index, RakString key, SHValueType val, Time curTime, bool combineEqualTimes); + SHErrorCode GetHistoryForKey(uint64_t objectId, RakString key, TimeAndValueQueue **values, Time curTime) const; + bool GetHistorySorted(uint64_t objectId, SHSortOperation sortType, DataStructures::List &values) const; + void MergeAllObjectsOnKey(RakString key, TimeAndValueQueue *tavqOutput, SHDataCategory dataCategory) const; + void GetUniqueKeyList(DataStructures::List &keys); + + struct TimeAndValue + { + Time time; + SHValueType val; + }; + + struct TimeAndValueQueue + { + TimeAndValueQueue(); + ~TimeAndValueQueue(); + + DataStructures::Queue values; + + Time timeToTrackValues; + RakString key; + + SHValueType recentSum; + SHValueType recentSumOfSquares; + SHValueType longTermSum; + SHValueType longTermCount; + SHValueType longTermLowest; + SHValueType longTermHighest; + + void SetTimeToTrackValues(Time t); + Time GetTimeToTrackValues(void) const; + SHValueType GetRecentSum(void) const; + SHValueType GetRecentSumOfSquares(void) const; + SHValueType GetLongTermSum(void) const; + SHValueType GetRecentAverage(void) const; + SHValueType GetRecentLowest(void) const; + SHValueType GetRecentHighest(void) const; + SHValueType GetRecentStandardDeviation(void) const; + SHValueType GetLongTermAverage(void) const; + SHValueType GetLongTermLowest(void) const; + SHValueType GetLongTermHighest(void) const; + SHValueType GetSumSinceTime(Time t) const; + Time GetTimeRange(void) const; + + // Merge two sets to output + static void MergeSets( const TimeAndValueQueue *lhs, SHDataCategory lhsDataCategory, const TimeAndValueQueue *rhs, SHDataCategory rhsDataCategory, TimeAndValueQueue *output ); + + // Shrink or expand a sample set to the approximate number given + // DC_DISCRETE will produce a histogram (sum) while DC_CONTINUOUS will produce an average + void ResizeSampleSet( int approximateSamples, DataStructures::Queue &blendedSamples, SHDataCategory dataCategory, Time timeClipStart=0, Time timeClipEnd=0 ); + + // Clear out all values + void Clear(void); + + TimeAndValueQueue& operator = ( const TimeAndValueQueue& input ); + + /// \internal + void CullExpiredValues(Time curTime); + /// \internal + static SHValueType Interpolate(TimeAndValue t1, TimeAndValue t2, Time time); + /// \internal + SHValueType sortValue; + }; + +protected: + struct TrackedObject; +public: + static int TrackedObjectComp( const uint64_t &key, TrackedObject* const &data ); +protected: + + struct TrackedObject + { + TrackedObject(); + ~TrackedObject(); + TrackedObjectData trackedObjectData; + DataStructures::Hash dataQueues; + }; + + DataStructures::OrderedList objects; + + Time timeToTrack; +}; + +/// \brief Input numerical values over time. Get sum, average, highest, lowest, standard deviation on recent or all-time values +/// \ingroup PLUGINS_GROUP +class RAK_DLL_EXPORT StatisticsHistoryPlugin : public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(StatisticsHistoryPlugin) + + StatisticsHistory statistics; + + StatisticsHistoryPlugin(); + virtual ~StatisticsHistoryPlugin(); + void SetTrackConnections(bool _addNewConnections, int newConnectionsObjectType, bool _removeLostConnections); + +protected: + virtual void Update(void); + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); + + // Too slow +// virtual bool UsesReliabilityLayer(void) const {return true;} +// virtual void OnDirectSocketSend(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress); +// virtual void OnDirectSocketReceive(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress); + + + bool addNewConnections; + bool removeLostConnections; + int newConnectionsObjectType; +}; + +} // namespace RakNet + +#endif // __STATISTICS_HISTORY_H + diff --git a/Source/StringCompressor.h b/Source/StringCompressor.h index e29aea3c6..45c229749 100644 --- a/Source/StringCompressor.h +++ b/Source/StringCompressor.h @@ -1,113 +1,111 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief \b Compresses/Decompresses ASCII strings and writes/reads them to BitStream class instances. You can use this to easily serialize and deserialize your own strings. -/// - - - -#ifndef __STRING_COMPRESSOR_H -#define __STRING_COMPRESSOR_H - -#include "Export.h" -#include "DS_Map.h" -#include "RakMemoryOverride.h" -#include "NativeTypes.h" - -#ifdef _STD_STRING_COMPRESSOR -#include -#endif - -/// Forward declaration -namespace RakNet -{ - class BitStream; - class RakString; -}; - - -namespace RakNet -{ -/// Forward declarations -class HuffmanEncodingTree; - -/// \brief Writes and reads strings to and from bitstreams. -/// -/// Only works with ASCII strings. The default compression is for English. -/// You can call GenerateTreeFromStrings to compress and decompress other languages efficiently as well. -class RAK_DLL_EXPORT StringCompressor -{ -public: - - // Destructor - ~StringCompressor(); - - /// static function because only static functions can access static members - /// The RakPeer constructor adds a reference to this class, so don't call this until an instance of RakPeer exists, or unless you call AddReference yourself. - /// \return the unique instance of the StringCompressor - static StringCompressor* Instance(void); - - /// Given an array of strings, such as a chat log, generate the optimal encoding tree for it. - /// This function is optional and if it is not called a default tree will be used instead. - /// \param[in] input An array of bytes which should point to text. - /// \param[in] inputLength Length of \a input - /// \param[in] languageID An identifier for the language / string table to generate the tree for. English is automatically created with ID 0 in the constructor. - void GenerateTreeFromStrings( unsigned char *input, unsigned inputLength, uint8_t languageId ); - - /// Writes input to output, compressed. Takes care of the null terminator for you. - /// \param[in] input Pointer to an ASCII string - /// \param[in] maxCharsToWrite The max number of bytes to write of \a input. Use 0 to mean no limit. - /// \param[out] output The bitstream to write the compressed string to - /// \param[in] languageID Which language to use - void EncodeString( const char *input, int maxCharsToWrite, RakNet::BitStream *output, uint8_t languageId=0 ); - - /// Writes input to output, uncompressed. Takes care of the null terminator for you. - /// \param[out] output A block of bytes to receive the output - /// \param[in] maxCharsToWrite Size, in bytes, of \a output . A NULL terminator will always be appended to the output string. If the maxCharsToWrite is not large enough, the string will be truncated. - /// \param[in] input The bitstream containing the compressed string - /// \param[in] languageID Which language to use - bool DecodeString( char *output, int maxCharsToWrite, RakNet::BitStream *input, uint8_t languageId=0 ); - -#ifdef _CSTRING_COMPRESSOR - void EncodeString( const CString &input, int maxCharsToWrite, RakNet::BitStream *output, uint8_t languageId=0 ); - bool DecodeString( CString &output, int maxCharsToWrite, RakNet::BitStream *input, uint8_t languageId=0 ); -#endif - -#ifdef _STD_STRING_COMPRESSOR - void EncodeString( const std::string &input, int maxCharsToWrite, RakNet::BitStream *output, uint8_t languageId=0 ); - bool DecodeString( std::string *output, int maxCharsToWrite, RakNet::BitStream *input, uint8_t languageId=0 ); -#endif - - void EncodeString( const RakNet::RakString *input, int maxCharsToWrite, RakNet::BitStream *output, uint8_t languageId=0 ); - bool DecodeString( RakNet::RakString *output, int maxCharsToWrite, RakNet::BitStream *input, uint8_t languageId=0 ); - - /// Used so I can allocate and deallocate this singleton at runtime - static void AddReference(void); - - /// Used so I can allocate and deallocate this singleton at runtime - static void RemoveReference(void); - - StringCompressor(); - -private: - - /// Singleton instance - static StringCompressor *instance; - - /// Pointer to the huffman encoding trees. - DataStructures::Map huffmanEncodingTrees; - - static int referenceCount; -}; - -} // namespace RakNet - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief \b Compresses/Decompresses ASCII strings and writes/reads them to BitStream class instances. You can use this to easily serialize and deserialize your own strings. +/// + + + +#pragma once + +#include "Export.h" +#include "DS_Map.h" +#include "RakMemoryOverride.h" +#include "NativeTypes.h" + +#ifdef _STD_STRING_COMPRESSOR +#include +#endif + +/// Forward declaration +namespace RakNet +{ + class BitStream; + class RakString; +}; + + +namespace RakNet +{ +/// Forward declarations +class HuffmanEncodingTree; + +/// \brief Writes and reads strings to and from bitstreams. +/// +/// Only works with ASCII strings. The default compression is for English. +/// You can call GenerateTreeFromStrings to compress and decompress other languages efficiently as well. +class RAK_DLL_EXPORT StringCompressor +{ +public: + + // Destructor + ~StringCompressor(); + + /// static function because only static functions can access static members + /// The RakPeer constructor adds a reference to this class, so don't call this until an instance of RakPeer exists, or unless you call AddReference yourself. + /// \return the unique instance of the StringCompressor + static StringCompressor* Instance(void); + + /// Given an array of strings, such as a chat log, generate the optimal encoding tree for it. + /// This function is optional and if it is not called a default tree will be used instead. + /// \param[in] input An array of bytes which should point to text. + /// \param[in] inputLength Length of \a input + /// \param[in] languageID An identifier for the language / string table to generate the tree for. English is automatically created with ID 0 in the constructor. + void GenerateTreeFromStrings( unsigned char *input, unsigned inputLength, uint8_t languageId ); + + /// Writes input to output, compressed. Takes care of the null terminator for you. + /// \param[in] input Pointer to an ASCII string + /// \param[in] maxCharsToWrite The max number of bytes to write of \a input. Use 0 to mean no limit. + /// \param[out] output The bitstream to write the compressed string to + /// \param[in] languageID Which language to use + void EncodeString( const char *input, int maxCharsToWrite, RakNet::BitStream *output, uint8_t languageId=0 ); + + /// Writes input to output, uncompressed. Takes care of the null terminator for you. + /// \param[out] output A block of bytes to receive the output + /// \param[in] maxCharsToWrite Size, in bytes, of \a output . A nullptr terminator will always be appended to the output string. If the maxCharsToWrite is not large enough, the string will be truncated. + /// \param[in] input The bitstream containing the compressed string + /// \param[in] languageID Which language to use + bool DecodeString( char *output, int maxCharsToWrite, RakNet::BitStream *input, uint8_t languageId=0 ); + +#ifdef _CSTRING_COMPRESSOR + void EncodeString( const CString &input, int maxCharsToWrite, RakNet::BitStream *output, uint8_t languageId=0 ); + bool DecodeString( CString &output, int maxCharsToWrite, RakNet::BitStream *input, uint8_t languageId=0 ); +#endif + +#ifdef _STD_STRING_COMPRESSOR + void EncodeString( const std::string &input, int maxCharsToWrite, RakNet::BitStream *output, uint8_t languageId=0 ); + bool DecodeString( std::string *output, int maxCharsToWrite, RakNet::BitStream *input, uint8_t languageId=0 ); +#endif + + void EncodeString( const RakNet::RakString *input, int maxCharsToWrite, RakNet::BitStream *output, uint8_t languageId=0 ); + bool DecodeString( RakNet::RakString *output, int maxCharsToWrite, RakNet::BitStream *input, uint8_t languageId=0 ); + + /// Used so I can allocate and deallocate this singleton at runtime + static void AddReference(void); + + /// Used so I can allocate and deallocate this singleton at runtime + static void RemoveReference(void); + + StringCompressor(); + +private: + + /// Singleton instance + static StringCompressor *instance; + + /// Pointer to the huffman encoding trees. + DataStructures::Map huffmanEncodingTrees; + + static int referenceCount; +}; + +} // namespace RakNet + diff --git a/Source/StringTable.h b/Source/StringTable.h index c495093b9..7e03d3df8 100644 --- a/Source/StringTable.h +++ b/Source/StringTable.h @@ -1,104 +1,102 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief A simple class to encode and decode known strings based on a lookup table. Similar to the StringCompressor class. -/// - - - -#ifndef __STRING_TABLE_H -#define __STRING_TABLE_H - -#include "DS_OrderedList.h" -#include "Export.h" -#include "RakMemoryOverride.h" - -/// Forward declaration -namespace RakNet -{ - class BitStream; -}; - -/// StringTableType should be the smallest type possible, or else it defeats the purpose of the StringTable class, which is to save bandwidth. -typedef unsigned char StringTableType; - -/// The string plus a bool telling us if this string was copied or not. -struct StrAndBool -{ - char *str; - bool b; -}; - -namespace RakNet -{ - int RAK_DLL_EXPORT StrAndBoolComp( char *const &key, const StrAndBool &data ); - - /// \details This is an even more efficient alternative to StringCompressor in that it writes a single byte from a lookup table and only does compression.
      - /// if the string does not already exist in the table.
      - /// All string tables must match on all systems - hence you must add all the strings in the same order on all systems.
      - /// Furthermore, this must be done before sending packets that use this class, since the strings are ordered for fast lookup. Adding after that time would mess up all the indices so don't do it.
      - /// Don't use this class to write strings which were not previously registered with AddString, since you just waste bandwidth then. Use StringCompressor instead. - /// \brief Writes a string index, instead of the whole string - class RAK_DLL_EXPORT StringTable - { - public: - - // Destructor - ~StringTable(); - - /// static function because only static functions can access static members - /// The RakPeer constructor adds a reference to this class, so don't call this until an instance of RakPeer exists, or unless you call AddReference yourself. - /// \return the unique instance of the StringTable - static StringTable* Instance(void); - - /// Add a string to the string table. - /// \param[in] str The string to add to the string table - /// \param[in] copyString true to make a copy of the passed string (takes more memory), false to not do so (if your string is in static memory). - void AddString(const char *str, bool copyString); - - /// Writes input to output, compressed. Takes care of the null terminator for you. - /// Relies on the StringCompressor class, which is automatically reference counted in the constructor and destructor in RakPeer. You can call the reference counting functions yourself if you wish too. - /// \param[in] input Pointer to an ASCII string - /// \param[in] maxCharsToWrite The size of \a input - /// \param[out] output The bitstream to write the compressed string to - void EncodeString( const char *input, int maxCharsToWrite, RakNet::BitStream *output ); - - /// Writes input to output, uncompressed. Takes care of the null terminator for you. - /// Relies on the StringCompressor class, which is automatically reference counted in the constructor and destructor in RakPeer. You can call the reference counting functions yourself if you wish too. - /// \param[out] output A block of bytes to receive the output - /// \param[in] maxCharsToWrite Size, in bytes, of \a output . A NULL terminator will always be appended to the output string. If the maxCharsToWrite is not large enough, the string will be truncated. - /// \param[in] input The bitstream containing the compressed string - bool DecodeString( char *output, int maxCharsToWrite, RakNet::BitStream *input ); - - /// Used so I can allocate and deallocate this singleton at runtime - static void AddReference(void); - - /// Used so I can allocate and deallocate this singleton at runtime - static void RemoveReference(void); - - /// Private Constructor - StringTable(); - - protected: - /// Called when you mess up and send a string using this class that was not registered with AddString - /// \param[in] maxCharsToWrite Size, in bytes, of \a output . A NULL terminator will always be appended to the output string. If the maxCharsToWrite is not large enough, the string will be truncated. - void LogStringNotFound(const char *strName); - - /// Singleton instance - static StringTable *instance; - static int referenceCount; - - DataStructures::OrderedList orderedStringList; - }; -} - - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief A simple class to encode and decode known strings based on a lookup table. Similar to the StringCompressor class. +/// + + + +#pragma once + +#include "DS_OrderedList.h" +#include "Export.h" +#include "RakMemoryOverride.h" + +/// Forward declaration +namespace RakNet +{ + class BitStream; +}; + +/// StringTableType should be the smallest type possible, or else it defeats the purpose of the StringTable class, which is to save bandwidth. +typedef unsigned char StringTableType; + +/// The string plus a bool telling us if this string was copied or not. +struct StrAndBool +{ + char *str; + bool b; +}; + +namespace RakNet +{ + int RAK_DLL_EXPORT StrAndBoolComp( char *const &key, const StrAndBool &data ); + + /// \details This is an even more efficient alternative to StringCompressor in that it writes a single byte from a lookup table and only does compression.
      + /// if the string does not already exist in the table.
      + /// All string tables must match on all systems - hence you must add all the strings in the same order on all systems.
      + /// Furthermore, this must be done before sending packets that use this class, since the strings are ordered for fast lookup. Adding after that time would mess up all the indices so don't do it.
      + /// Don't use this class to write strings which were not previously registered with AddString, since you just waste bandwidth then. Use StringCompressor instead. + /// \brief Writes a string index, instead of the whole string + class RAK_DLL_EXPORT StringTable + { + public: + + // Destructor + ~StringTable(); + + /// static function because only static functions can access static members + /// The RakPeer constructor adds a reference to this class, so don't call this until an instance of RakPeer exists, or unless you call AddReference yourself. + /// \return the unique instance of the StringTable + static StringTable* Instance(void); + + /// Add a string to the string table. + /// \param[in] str The string to add to the string table + /// \param[in] copyString true to make a copy of the passed string (takes more memory), false to not do so (if your string is in static memory). + void AddString(const char *str, bool copyString); + + /// Writes input to output, compressed. Takes care of the null terminator for you. + /// Relies on the StringCompressor class, which is automatically reference counted in the constructor and destructor in RakPeer. You can call the reference counting functions yourself if you wish too. + /// \param[in] input Pointer to an ASCII string + /// \param[in] maxCharsToWrite The size of \a input + /// \param[out] output The bitstream to write the compressed string to + void EncodeString( const char *input, int maxCharsToWrite, RakNet::BitStream *output ); + + /// Writes input to output, uncompressed. Takes care of the null terminator for you. + /// Relies on the StringCompressor class, which is automatically reference counted in the constructor and destructor in RakPeer. You can call the reference counting functions yourself if you wish too. + /// \param[out] output A block of bytes to receive the output + /// \param[in] maxCharsToWrite Size, in bytes, of \a output . A nullptr terminator will always be appended to the output string. If the maxCharsToWrite is not large enough, the string will be truncated. + /// \param[in] input The bitstream containing the compressed string + bool DecodeString( char *output, int maxCharsToWrite, RakNet::BitStream *input ); + + /// Used so I can allocate and deallocate this singleton at runtime + static void AddReference(void); + + /// Used so I can allocate and deallocate this singleton at runtime + static void RemoveReference(void); + + /// Private Constructor + StringTable(); + + protected: + /// Called when you mess up and send a string using this class that was not registered with AddString + /// \param[in] maxCharsToWrite Size, in bytes, of \a output . A nullptr terminator will always be appended to the output string. If the maxCharsToWrite is not large enough, the string will be truncated. + void LogStringNotFound(const char *strName); + + /// Singleton instance + static StringTable *instance; + static int referenceCount; + + DataStructures::OrderedList orderedStringList; + }; +} + + diff --git a/Source/SuperFastHash.cpp b/Source/SuperFastHash.cpp index 375347eda..2749ce2f5 100644 --- a/Source/SuperFastHash.cpp +++ b/Source/SuperFastHash.cpp @@ -1,129 +1,129 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "SuperFastHash.h" -#include "NativeTypes.h" -#include - -#if !defined(_WIN32) -#include -#endif - -#undef get16bits - -#if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \ - || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__) -#define get16bits(d) (*((const uint16_t *) (d))) -#else -#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\ - +(uint32_t)(((const uint8_t *)(d))[0]) ) -#endif - -static const int INCREMENTAL_READ_BLOCK=65536; - -uint32_t SuperFastHash (const char * data, int length) -{ - // All this is necessary or the hash does not match SuperFastHashIncremental - int bytesRemaining=length; - unsigned int lastHash = length; - int offset=0; - while (bytesRemaining>=INCREMENTAL_READ_BLOCK) - { - lastHash=SuperFastHashIncremental (data+offset, INCREMENTAL_READ_BLOCK, lastHash ); - bytesRemaining-=INCREMENTAL_READ_BLOCK; - offset+=INCREMENTAL_READ_BLOCK; - } - if (bytesRemaining>0) - { - lastHash=SuperFastHashIncremental (data+offset, bytesRemaining, lastHash ); - } - return lastHash; - -// return SuperFastHashIncremental(data,len,len); -} -uint32_t SuperFastHashIncremental (const char * data, int len, unsigned int lastHash ) -{ - uint32_t hash = (uint32_t) lastHash; - uint32_t tmp; - int rem; - - if (len <= 0 || data == NULL) return 0; - - rem = len & 3; - len >>= 2; - - /* Main loop */ - for (;len > 0; len--) { - hash += get16bits (data); - tmp = (get16bits (data+2) << 11) ^ hash; - hash = (hash << 16) ^ tmp; - data += 2*sizeof (uint16_t); - hash += hash >> 11; - } - - /* Handle end cases */ - switch (rem) { - case 3: hash += get16bits (data); - hash ^= hash << 16; - hash ^= data[sizeof (uint16_t)] << 18; - hash += hash >> 11; - break; - case 2: hash += get16bits (data); - hash ^= hash << 11; - hash += hash >> 17; - break; - case 1: hash += *data; - hash ^= hash << 10; - hash += hash >> 1; - } - - /* Force "avalanching" of final 127 bits */ - hash ^= hash << 3; - hash += hash >> 5; - hash ^= hash << 4; - hash += hash >> 17; - hash ^= hash << 25; - hash += hash >> 6; - - return (uint32_t) hash; - -} - -uint32_t SuperFastHashFile (const char * filename) -{ - FILE *fp = fopen(filename, "rb"); - if (fp==0) - return 0; - uint32_t hash = SuperFastHashFilePtr(fp); - fclose(fp); - return hash; -} - -uint32_t SuperFastHashFilePtr (FILE *fp) -{ - fseek(fp, 0, SEEK_END); - int length = ftell(fp); - fseek(fp, 0, SEEK_SET); - int bytesRemaining=length; - unsigned int lastHash = length; - char readBlock[INCREMENTAL_READ_BLOCK]; - while (bytesRemaining>=(int) sizeof(readBlock)) - { - fread(readBlock, sizeof(readBlock), 1, fp); - lastHash=SuperFastHashIncremental (readBlock, (int) sizeof(readBlock), lastHash ); - bytesRemaining-=(int) sizeof(readBlock); - } - if (bytesRemaining>0) - { - fread(readBlock, bytesRemaining, 1, fp); - lastHash=SuperFastHashIncremental (readBlock, bytesRemaining, lastHash ); - } - return lastHash; -} +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "SuperFastHash.h" +#include "NativeTypes.h" +#include + +#if !defined(_WIN32) +#include +#endif + +#undef get16bits + +#if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \ + || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__) +#define get16bits(d) (*((const uint16_t *) (d))) +#else +#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\ + +(uint32_t)(((const uint8_t *)(d))[0]) ) +#endif + +static const int INCREMENTAL_READ_BLOCK=65536; + +uint32_t SuperFastHash (const char * data, int length) +{ + // All this is necessary or the hash does not match SuperFastHashIncremental + int bytesRemaining=length; + unsigned int lastHash = length; + int offset=0; + while (bytesRemaining>=INCREMENTAL_READ_BLOCK) + { + lastHash=SuperFastHashIncremental (data+offset, INCREMENTAL_READ_BLOCK, lastHash ); + bytesRemaining-=INCREMENTAL_READ_BLOCK; + offset+=INCREMENTAL_READ_BLOCK; + } + if (bytesRemaining>0) + { + lastHash=SuperFastHashIncremental (data+offset, bytesRemaining, lastHash ); + } + return lastHash; + +// return SuperFastHashIncremental(data,len,len); +} +uint32_t SuperFastHashIncremental (const char * data, int len, unsigned int lastHash ) +{ + uint32_t hash = (uint32_t) lastHash; + uint32_t tmp; + int rem; + + if (len <= 0 || data == nullptr) return 0; + + rem = len & 3; + len >>= 2; + + /* Main loop */ + for (;len > 0; len--) { + hash += get16bits (data); + tmp = (get16bits (data+2) << 11) ^ hash; + hash = (hash << 16) ^ tmp; + data += 2*sizeof (uint16_t); + hash += hash >> 11; + } + + /* Handle end cases */ + switch (rem) { + case 3: hash += get16bits (data); + hash ^= hash << 16; + hash ^= data[sizeof (uint16_t)] << 18; + hash += hash >> 11; + break; + case 2: hash += get16bits (data); + hash ^= hash << 11; + hash += hash >> 17; + break; + case 1: hash += *data; + hash ^= hash << 10; + hash += hash >> 1; + } + + /* Force "avalanching" of final 127 bits */ + hash ^= hash << 3; + hash += hash >> 5; + hash ^= hash << 4; + hash += hash >> 17; + hash ^= hash << 25; + hash += hash >> 6; + + return (uint32_t) hash; + +} + +uint32_t SuperFastHashFile (const char * filename) +{ + FILE *fp = fopen(filename, "rb"); + if (fp==0) + return 0; + uint32_t hash = SuperFastHashFilePtr(fp); + fclose(fp); + return hash; +} + +uint32_t SuperFastHashFilePtr (FILE *fp) +{ + fseek(fp, 0, SEEK_END); + int length = ftell(fp); + fseek(fp, 0, SEEK_SET); + int bytesRemaining=length; + unsigned int lastHash = length; + char readBlock[INCREMENTAL_READ_BLOCK]; + while (bytesRemaining>=(int) sizeof(readBlock)) + { + fread(readBlock, sizeof(readBlock), 1, fp); + lastHash=SuperFastHashIncremental (readBlock, (int) sizeof(readBlock), lastHash ); + bytesRemaining-=(int) sizeof(readBlock); + } + if (bytesRemaining>0) + { + fread(readBlock, bytesRemaining, 1, fp); + lastHash=SuperFastHashIncremental (readBlock, bytesRemaining, lastHash ); + } + return lastHash; +} diff --git a/Source/SuperFastHash.h b/Source/SuperFastHash.h index 3d09caf56..5c1664d17 100644 --- a/Source/SuperFastHash.h +++ b/Source/SuperFastHash.h @@ -1,27 +1,25 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __SUPER_FAST_HASH_H -#define __SUPER_FAST_HASH_H - -#include -#include "NativeTypes.h" - -// From http://www.azillionmonkeys.com/qed/hash.html -// Author of main code is Paul Hsieh -// I just added some convenience functions -// Also note http://burtleburtle.net/bob/hash/doobs.html, which shows that this is 20% faster than the one on that page but has more collisions - -uint32_t SuperFastHash (const char * data, int length); -uint32_t SuperFastHashIncremental (const char * data, int len, unsigned int lastHash ); -uint32_t SuperFastHashFile (const char * filename); -uint32_t SuperFastHashFilePtr (FILE *fp); - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include +#include "NativeTypes.h" + +// From http://www.azillionmonkeys.com/qed/hash.html +// Author of main code is Paul Hsieh +// I just added some convenience functions +// Also note http://burtleburtle.net/bob/hash/doobs.html, which shows that this is 20% faster than the one on that page but has more collisions + +uint32_t SuperFastHash (const char * data, int length); +uint32_t SuperFastHashIncremental (const char * data, int len, unsigned int lastHash ); +uint32_t SuperFastHashFile (const char * filename); +uint32_t SuperFastHashFilePtr (FILE *fp); + diff --git a/Source/TCPInterface.cpp b/Source/TCPInterface.cpp index 71f8fdfd3..ea0e5e264 100644 --- a/Source/TCPInterface.cpp +++ b/Source/TCPInterface.cpp @@ -1,1439 +1,1439 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_TCPInterface==1 - -/// \file -/// \brief A simple TCP based server allowing sends and receives. Can be connected to by a telnet client. -/// - - - -#include "TCPInterface.h" -#ifdef _WIN32 - #if !defined (WINDOWS_STORE_RT) - typedef int socklen_t; - #endif - - -#else -#include -#include -#include -#endif -#include -#include "RakAssert.h" -#include -#include "RakAssert.h" -#include "RakSleep.h" -#include "StringCompressor.h" -#include "StringTable.h" -#include "Itoa.h" -#include "SocketLayer.h" -#include "SocketDefines.h" -#if (defined(__GNUC__) || defined(__GCCXML__)) && !defined(__WIN32__) -#include -#endif - -#ifdef _DO_PRINTF -#endif - -#ifdef _WIN32 -#include "WSAStartupSingleton.h" -#endif -namespace RakNet -{ -RAK_THREAD_DECLARATION(UpdateTCPInterfaceLoop); -RAK_THREAD_DECLARATION(ConnectionAttemptLoop); -} -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -using namespace RakNet; - -STATIC_FACTORY_DEFINITIONS(TCPInterface,TCPInterface); - -TCPInterface::TCPInterface() -{ -#if !defined(WINDOWS_STORE_RT) - listenSocket=0; -#endif - remoteClients=0; - remoteClientsLength=0; - - StringCompressor::AddReference(); - RakNet::StringTable::AddReference(); - -#if OPEN_SSL_CLIENT_SUPPORT==1 - ctx=0; - meth=0; -#endif - -#ifdef _WIN32 - WSAStartupSingleton::AddRef(); -#endif -} -TCPInterface::~TCPInterface() -{ - Stop(); -#ifdef _WIN32 - WSAStartupSingleton::Deref(); -#endif - - RakNet::OP_DELETE_ARRAY(remoteClients,_FILE_AND_LINE_); - - StringCompressor::RemoveReference(); - RakNet::StringTable::RemoveReference(); -} -#if !defined(WINDOWS_STORE_RT) -bool TCPInterface::CreateListenSocket(unsigned short port, unsigned short maxIncomingConnections, unsigned short socketFamily, const char *bindAddress) -{ - (void) maxIncomingConnections; - (void) socketFamily; -#if RAKNET_SUPPORT_IPV6!=1 - listenSocket = socket__(AF_INET, SOCK_STREAM, 0); - if ((int)listenSocket ==-1) - return false; - - struct sockaddr_in serverAddress; - memset(&serverAddress,0,sizeof(sockaddr_in)); - serverAddress.sin_family = AF_INET; - if ( bindAddress && bindAddress[0] ) - { - - - - - - serverAddress.sin_addr.s_addr = inet_addr__(bindAddress ); - - } - else - serverAddress.sin_addr.s_addr = INADDR_ANY; - - serverAddress.sin_port = htons(port); - - SocketLayer::SetSocketOptions(listenSocket, false, false); - - if (bind__(listenSocket,(struct sockaddr *) &serverAddress,sizeof(serverAddress)) < 0) - return false; - - listen__(listenSocket, maxIncomingConnections); -#else - struct addrinfo hints; - memset(&hints, 0, sizeof (addrinfo)); // make sure the struct is empty - hints.ai_family = socketFamily; // don't care IPv4 or IPv6 - hints.ai_socktype = SOCK_STREAM; // TCP sockets - hints.ai_flags = AI_PASSIVE; // fill in my IP for me - struct addrinfo *servinfo=0, *aip; // will point to the results - char portStr[32]; - Itoa(port,portStr,10); - - getaddrinfo(0, portStr, &hints, &servinfo); - for (aip = servinfo; aip != NULL; aip = aip->ai_next) - { - // Open socket. The address type depends on what - // getaddrinfo() gave us. - listenSocket = socket__(aip->ai_family, aip->ai_socktype, aip->ai_protocol); - if (listenSocket != 0) - { - int ret = bind__( listenSocket, aip->ai_addr, (int) aip->ai_addrlen ); - if (ret>=0) - { - break; - } - else - { - closesocket__(listenSocket); - listenSocket=0; - } - } - } - - if (listenSocket==0) - return false; - - SocketLayer::SetSocketOptions(listenSocket, false, false); - - listen__(listenSocket, maxIncomingConnections); -#endif // #if RAKNET_SUPPORT_IPV6!=1 - - return true; -} -#endif - -#if defined(WINDOWS_STORE_RT) -bool TCPInterface::CreateListenSocket_WinStore8(unsigned short port, unsigned short maxIncomingConnections, unsigned short socketFamily, const char *bindAddress) -{ - listenSocket = WinRTCreateStreamSocket(AF_INET, SOCK_STREAM, 0); - return true; -} -#endif -bool TCPInterface::Start(unsigned short port, unsigned short maxIncomingConnections, unsigned short maxConnections, int _threadPriority, unsigned short socketFamily, const char *bindAddress) -{ -#ifdef __native_client__ - return false; -#else - (void) socketFamily; - - if (isStarted.GetValue()>0) - return false; - - threadPriority=_threadPriority; - - if (threadPriority==-99999) - { - - -#if defined(_WIN32) - threadPriority=0; - - -#else - threadPriority=1000; -#endif - } - - isStarted.Increment(); - if (maxConnections==0) - maxConnections=maxIncomingConnections; - if (maxConnections==0) - maxConnections=1; - remoteClientsLength=maxConnections; - remoteClients=RakNet::OP_NEW_ARRAY(maxConnections,_FILE_AND_LINE_); - - - listenSocket=0; - if (maxIncomingConnections>0) - { -#if defined(WINDOWS_STORE_RT) - CreateListenSocket_WinStore8(port, maxIncomingConnections, socketFamily, bindAddress); -#else - CreateListenSocket(port, maxIncomingConnections, socketFamily, bindAddress); -#endif - } - - - // Start the update thread - int errorCode; - - - - - - errorCode = RakNet::RakThread::Create(UpdateTCPInterfaceLoop, this, threadPriority); - - - if (errorCode!=0) - return false; - - while (threadRunning.GetValue()==0) - RakSleep(0); - - unsigned int i; - for (i=0; i < messageHandlerList.Size(); i++) - messageHandlerList[i]->OnRakPeerStartup(); - - return true; -#endif // __native_client__ -} -void TCPInterface::Stop(void) -{ - unsigned int i; - for (i=0; i < messageHandlerList.Size(); i++) - messageHandlerList[i]->OnRakPeerShutdown(); - -#ifndef __native_client__ - if (isStarted.GetValue()==0) - return; - -#if OPEN_SSL_CLIENT_SUPPORT==1 - for (i=0; i < remoteClientsLength; i++) - remoteClients[i].DisconnectSSL(); -#endif - - isStarted.Decrement(); - -#if !defined(WINDOWS_STORE_RT) - if (listenSocket!=0) -#endif - { -#ifdef _WIN32 - shutdown__(listenSocket, SD_BOTH); - -#else - shutdown__(listenSocket, SHUT_RDWR); -#endif - closesocket__(listenSocket); - } - - // Abort waiting connect calls - blockingSocketListMutex.Lock(); - for (i=0; i < blockingSocketList.Size(); i++) - { - closesocket__(blockingSocketList[i]); - } - blockingSocketListMutex.Unlock(); - - // Wait for the thread to stop - while ( threadRunning.GetValue()>0 ) - RakSleep(15); - - RakSleep(100); - - #if !defined(WINDOWS_STORE_RT) - listenSocket=0; - #endif - - // Stuff from here on to the end of the function is not threadsafe - for (i=0; i < (unsigned int) remoteClientsLength; i++) - { - closesocket__(remoteClients[i].socket); -#if OPEN_SSL_CLIENT_SUPPORT==1 - remoteClients[i].FreeSSL(); -#endif - } - remoteClientsLength=0; - RakNet::OP_DELETE_ARRAY(remoteClients,_FILE_AND_LINE_); - remoteClients=0; - - incomingMessages.Clear(_FILE_AND_LINE_); - newIncomingConnections.Clear(_FILE_AND_LINE_); - newRemoteClients.Clear(_FILE_AND_LINE_); - lostConnections.Clear(_FILE_AND_LINE_); - requestedCloseConnections.Clear(_FILE_AND_LINE_); - failedConnectionAttempts.Clear(_FILE_AND_LINE_); - completedConnectionAttempts.Clear(_FILE_AND_LINE_); - failedConnectionAttempts.Clear(_FILE_AND_LINE_); - for (i=0; i < headPush.Size(); i++) - DeallocatePacket(headPush[i]); - headPush.Clear(_FILE_AND_LINE_); - for (i=0; i < tailPush.Size(); i++) - DeallocatePacket(tailPush[i]); - tailPush.Clear(_FILE_AND_LINE_); - -#if OPEN_SSL_CLIENT_SUPPORT==1 - SSL_CTX_free (ctx); - startSSL.Clear(_FILE_AND_LINE_); - activeSSLConnections.Clear(false, _FILE_AND_LINE_); -#endif - - - - - -#endif // __native_client__ -} -SystemAddress TCPInterface::Connect(const char* host, unsigned short remotePort, bool block, unsigned short socketFamily, const char *bindAddress) -{ - if (threadRunning.GetValue()==0) - return UNASSIGNED_SYSTEM_ADDRESS; - - int newRemoteClientIndex=-1; - for (newRemoteClientIndex=0; newRemoteClientIndex < remoteClientsLength; newRemoteClientIndex++) - { - remoteClients[newRemoteClientIndex].isActiveMutex.Lock(); - if (remoteClients[newRemoteClientIndex].isActive==false) - { - remoteClients[newRemoteClientIndex].SetActive(true); - remoteClients[newRemoteClientIndex].isActiveMutex.Unlock(); - break; - } - remoteClients[newRemoteClientIndex].isActiveMutex.Unlock(); - } - if (newRemoteClientIndex==-1) - return UNASSIGNED_SYSTEM_ADDRESS; - - if (block) - { - SystemAddress systemAddress; - systemAddress.FromString(host); - systemAddress.SetPortHostOrder(remotePort); - systemAddress.systemIndex=(SystemIndex) newRemoteClientIndex; - char buffout[128]; - systemAddress.ToString(false,buffout); - - __TCPSOCKET__ sockfd = SocketConnect(buffout, remotePort, socketFamily, bindAddress); - // Windows RT TODO -#if !defined(WINDOWS_STORE_RT) - if (sockfd==0) -#endif - { - remoteClients[newRemoteClientIndex].isActiveMutex.Lock(); - remoteClients[newRemoteClientIndex].SetActive(false); - remoteClients[newRemoteClientIndex].isActiveMutex.Unlock(); - - failedConnectionAttemptMutex.Lock(); - failedConnectionAttempts.Push(systemAddress, _FILE_AND_LINE_ ); - failedConnectionAttemptMutex.Unlock(); - - return UNASSIGNED_SYSTEM_ADDRESS; - } - - remoteClients[newRemoteClientIndex].socket=sockfd; - remoteClients[newRemoteClientIndex].systemAddress=systemAddress; - - completedConnectionAttemptMutex.Lock(); - completedConnectionAttempts.Push(remoteClients[newRemoteClientIndex].systemAddress, _FILE_AND_LINE_ ); - completedConnectionAttemptMutex.Unlock(); - - return remoteClients[newRemoteClientIndex].systemAddress; - } - else - { - ThisPtrPlusSysAddr *s = RakNet::OP_NEW( _FILE_AND_LINE_ ); - s->systemAddress.FromStringExplicitPort(host,remotePort); - s->systemAddress.systemIndex=(SystemIndex) newRemoteClientIndex; - if (bindAddress) - strcpy(s->bindAddress, bindAddress); - else - s->bindAddress[0]=0; - s->tcpInterface=this; - s->socketFamily=socketFamily; - - // Start the connection thread - int errorCode; - - - - - errorCode = RakNet::RakThread::Create(ConnectionAttemptLoop, s, threadPriority); - - if (errorCode!=0) - { - RakNet::OP_DELETE(s, _FILE_AND_LINE_); - failedConnectionAttempts.Push(s->systemAddress, _FILE_AND_LINE_ ); - } - return UNASSIGNED_SYSTEM_ADDRESS; - } -} -#if OPEN_SSL_CLIENT_SUPPORT==1 -void TCPInterface::StartSSLClient(SystemAddress systemAddress) -{ - if (ctx==0) - { - sharedSslMutex.Lock(); - SSLeay_add_ssl_algorithms(); - meth = (SSL_METHOD*) SSLv23_client_method(); - SSL_load_error_strings(); - ctx = SSL_CTX_new (meth); - RakAssert(ctx!=0); - sharedSslMutex.Unlock(); - } - - SystemAddress *id = startSSL.Allocate( _FILE_AND_LINE_ ); - *id=systemAddress; - startSSL.Push(id); - unsigned index = activeSSLConnections.GetIndexOf(systemAddress); - if (index==(unsigned)-1) - activeSSLConnections.Insert(systemAddress,_FILE_AND_LINE_); -} -bool TCPInterface::IsSSLActive(SystemAddress systemAddress) -{ - return activeSSLConnections.GetIndexOf(systemAddress)!=-1; -} -#endif -void TCPInterface::Send( const char *data, unsigned length, const SystemAddress &systemAddress, bool broadcast ) -{ - SendList( &data, &length, 1, systemAddress,broadcast ); -} -bool TCPInterface::SendList( const char **data, const unsigned int *lengths, const int numParameters, const SystemAddress &systemAddress, bool broadcast ) -{ - if (isStarted.GetValue()==0) - return false; - if (data==0) - return false; - if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS && broadcast==false) - return false; - unsigned int totalLength=0; - int i; - for (i=0; i < numParameters; i++) - { - if (lengths[i]>0) - totalLength+=lengths[i]; - } - if (totalLength==0) - return false; - - if (broadcast) - { - // Send to all, possible exception system - for (i=0; i < remoteClientsLength; i++) - { - if (remoteClients[i].systemAddress!=systemAddress) - { - remoteClients[i].SendOrBuffer(data, lengths, numParameters); - } - } - } - else - { - // Send to this player - if (systemAddress.systemIndexUpdate(); - - Packet* outgoingPacket = ReceiveInt(); - - if (outgoingPacket) - { - PluginReceiveResult pluginResult; - for (i=0; i < messageHandlerList.Size(); i++) - { - pluginResult=messageHandlerList[i]->OnReceive(outgoingPacket); - if (pluginResult==RR_STOP_PROCESSING_AND_DEALLOCATE) - { - DeallocatePacket( outgoingPacket ); - outgoingPacket=0; // Will do the loop again and get another packet - break; // break out of the enclosing for - } - else if (pluginResult==RR_STOP_PROCESSING) - { - outgoingPacket=0; - break; - } - } - } - - - return outgoingPacket; -} -Packet* TCPInterface::ReceiveInt( void ) -{ - if (isStarted.GetValue()==0) - return 0; - if (headPush.IsEmpty()==false) - return headPush.Pop(); - Packet *p = incomingMessages.PopInaccurate(); - if (p) - return p; - if (tailPush.IsEmpty()==false) - return tailPush.Pop(); - return 0; -} - - -void TCPInterface::AttachPlugin( PluginInterface2 *plugin ) -{ - if (messageHandlerList.GetIndexOf(plugin)==MAX_UNSIGNED_LONG) - { - messageHandlerList.Insert(plugin, _FILE_AND_LINE_); - plugin->SetTCPInterface(this); - plugin->OnAttach(); - } -} -void TCPInterface::DetachPlugin( PluginInterface2 *plugin ) -{ - if (plugin==0) - return; - - unsigned int index; - index = messageHandlerList.GetIndexOf(plugin); - if (index!=MAX_UNSIGNED_LONG) - { - messageHandlerList[index]->OnDetach(); - // Unordered list so delete from end for speed - messageHandlerList[index]=messageHandlerList[messageHandlerList.Size()-1]; - messageHandlerList.RemoveFromEnd(); - plugin->SetTCPInterface(0); - } -} -void TCPInterface::CloseConnection( SystemAddress systemAddress ) -{ - if (isStarted.GetValue()==0) - return; - if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS) - return; - - unsigned int i; - for (i=0; i < messageHandlerList.Size(); i++) - messageHandlerList[i]->OnClosedConnection(systemAddress, UNASSIGNED_RAKNET_GUID, LCR_CLOSED_BY_USER); - - if (systemAddress.systemIndexdeleteData) - { - rakFree_Ex(packet->data, _FILE_AND_LINE_ ); - incomingMessages.Deallocate(packet, _FILE_AND_LINE_); - } - else - { - // Came from userspace AllocatePacket - rakFree_Ex(packet->data, _FILE_AND_LINE_ ); - RakNet::OP_DELETE(packet, _FILE_AND_LINE_); - } -} -Packet* TCPInterface::AllocatePacket(unsigned dataSize) -{ - Packet*p = RakNet::OP_NEW(_FILE_AND_LINE_); - p->data=(unsigned char*) rakMalloc_Ex(dataSize,_FILE_AND_LINE_); - p->length=dataSize; - p->bitSize=BYTES_TO_BITS(dataSize); - p->deleteData=false; - p->guid=UNASSIGNED_RAKNET_GUID; - p->systemAddress=UNASSIGNED_SYSTEM_ADDRESS; - p->systemAddress.systemIndex=(SystemIndex)-1; - return p; -} -void TCPInterface::PushBackPacket( Packet *packet, bool pushAtHead ) -{ - if (pushAtHead) - headPush.Push(packet, _FILE_AND_LINE_ ); - else - tailPush.Push(packet, _FILE_AND_LINE_ ); -} -bool TCPInterface::WasStarted(void) const -{ - return threadRunning.GetValue()>0; -} -SystemAddress TCPInterface::HasCompletedConnectionAttempt(void) -{ - SystemAddress sysAddr=UNASSIGNED_SYSTEM_ADDRESS; - completedConnectionAttemptMutex.Lock(); - if (completedConnectionAttempts.IsEmpty()==false) - sysAddr=completedConnectionAttempts.Pop(); - completedConnectionAttemptMutex.Unlock(); - - if (sysAddr!=UNASSIGNED_SYSTEM_ADDRESS) - { - unsigned int i; - for (i=0; i < messageHandlerList.Size(); i++) - messageHandlerList[i]->OnNewConnection(sysAddr, UNASSIGNED_RAKNET_GUID, true); - } - - return sysAddr; -} -SystemAddress TCPInterface::HasFailedConnectionAttempt(void) -{ - SystemAddress sysAddr=UNASSIGNED_SYSTEM_ADDRESS; - failedConnectionAttemptMutex.Lock(); - if (failedConnectionAttempts.IsEmpty()==false) - sysAddr=failedConnectionAttempts.Pop(); - failedConnectionAttemptMutex.Unlock(); - - if (sysAddr!=UNASSIGNED_SYSTEM_ADDRESS) - { - unsigned int i; - for (i=0; i < messageHandlerList.Size(); i++) - { - Packet p; - p.systemAddress=sysAddr; - p.data=0; - p.length=0; - p.bitSize=0; - messageHandlerList[i]->OnFailedConnectionAttempt(&p, FCAR_CONNECTION_ATTEMPT_FAILED); - } - } - - return sysAddr; -} -SystemAddress TCPInterface::HasNewIncomingConnection(void) -{ - SystemAddress *out, out2; - out = newIncomingConnections.PopInaccurate(); - if (out) - { - out2=*out; - newIncomingConnections.Deallocate(out, _FILE_AND_LINE_); - - unsigned int i; - for (i=0; i < messageHandlerList.Size(); i++) - messageHandlerList[i]->OnNewConnection(out2, UNASSIGNED_RAKNET_GUID, true); - - return *out; - } - else - { - return UNASSIGNED_SYSTEM_ADDRESS; - } -} -SystemAddress TCPInterface::HasLostConnection(void) -{ - SystemAddress *out, out2; - out = lostConnections.PopInaccurate(); - if (out) - { - out2=*out; - lostConnections.Deallocate(out, _FILE_AND_LINE_); - - unsigned int i; - for (i=0; i < messageHandlerList.Size(); i++) - messageHandlerList[i]->OnClosedConnection(out2, UNASSIGNED_RAKNET_GUID, LCR_DISCONNECTION_NOTIFICATION); - - return *out; - } - else - { - return UNASSIGNED_SYSTEM_ADDRESS; - } -} -void TCPInterface::GetConnectionList( SystemAddress *remoteSystems, unsigned short *numberOfSystems ) const -{ - unsigned short systemCount=0; - unsigned short maxToWrite=*numberOfSystems; - for (int i=0; i < remoteClientsLength; i++) - { - if (remoteClients[i].isActive) - { - if (systemCount < maxToWrite) - remoteSystems[systemCount]=remoteClients[i].systemAddress; - systemCount++; - } - } - *numberOfSystems=systemCount; -} -unsigned short TCPInterface::GetConnectionCount(void) const -{ - unsigned short systemCount=0; - for (int i=0; i < remoteClientsLength; i++) - { - if (remoteClients[i].isActive) - systemCount++; - } - return systemCount; -} - -unsigned int TCPInterface::GetOutgoingDataBufferSize(SystemAddress systemAddress) const -{ - unsigned bytesWritten=0; - if (systemAddress.systemIndexh_addr, server->h_length); - - - - - - - - - - - blockingSocketListMutex.Lock(); - blockingSocketList.Insert(sockfd, _FILE_AND_LINE_); - blockingSocketListMutex.Unlock(); - - // This is blocking - connectResult = connect__( sockfd, ( struct sockaddr * ) &serverAddress, sizeof( struct sockaddr ) ); - -#else - - - struct addrinfo hints, *res; - int sockfd; - memset(&hints, 0, sizeof hints); - hints.ai_family = socketFamily; - hints.ai_socktype = SOCK_STREAM; - char portStr[32]; - Itoa(remotePort,portStr,10); - getaddrinfo(host, portStr, &hints, &res); - sockfd = socket__(res->ai_family, res->ai_socktype, res->ai_protocol); - blockingSocketListMutex.Lock(); - blockingSocketList.Insert(sockfd, _FILE_AND_LINE_); - blockingSocketListMutex.Unlock(); - connectResult=connect__(sockfd, res->ai_addr, res->ai_addrlen); - freeaddrinfo(res); // free the linked-list - -#endif // #if RAKNET_SUPPORT_IPV6!=1 - - if (connectResult==-1) - { - unsigned sockfdIndex; - blockingSocketListMutex.Lock(); - sockfdIndex=blockingSocketList.GetIndexOf(sockfd); - if (sockfdIndex!=(unsigned)-1) - blockingSocketList.RemoveAtIndexFast(sockfdIndex); - blockingSocketListMutex.Unlock(); - - closesocket__(sockfd); - return 0; - } - - return sockfd; -#endif // __native_client__ -} - -RAK_THREAD_DECLARATION(RakNet::ConnectionAttemptLoop) -{ - - - - TCPInterface::ThisPtrPlusSysAddr *s = (TCPInterface::ThisPtrPlusSysAddr *) arguments; - - - - SystemAddress systemAddress = s->systemAddress; - TCPInterface *tcpInterface = s->tcpInterface; - int newRemoteClientIndex=systemAddress.systemIndex; - unsigned short socketFamily = s->socketFamily; - RakNet::OP_DELETE(s, _FILE_AND_LINE_); - - char str1[64]; - systemAddress.ToString(false, str1); - __TCPSOCKET__ sockfd = tcpInterface->SocketConnect(str1, systemAddress.GetPort(), socketFamily, s->bindAddress); - if (sockfd==0) - { - tcpInterface->remoteClients[newRemoteClientIndex].isActiveMutex.Lock(); - tcpInterface->remoteClients[newRemoteClientIndex].SetActive(false); - tcpInterface->remoteClients[newRemoteClientIndex].isActiveMutex.Unlock(); - - tcpInterface->failedConnectionAttemptMutex.Lock(); - tcpInterface->failedConnectionAttempts.Push(systemAddress, _FILE_AND_LINE_ ); - tcpInterface->failedConnectionAttemptMutex.Unlock(); - return 0; - } - - tcpInterface->remoteClients[newRemoteClientIndex].socket=sockfd; - tcpInterface->remoteClients[newRemoteClientIndex].systemAddress=systemAddress; - - // Notify user that the connection attempt has completed. - if (tcpInterface->threadRunning.GetValue()>0) - { - tcpInterface->completedConnectionAttemptMutex.Lock(); - tcpInterface->completedConnectionAttempts.Push(systemAddress, _FILE_AND_LINE_ ); - tcpInterface->completedConnectionAttemptMutex.Unlock(); - } - - - - - return 0; - -} - -RAK_THREAD_DECLARATION(RakNet::UpdateTCPInterfaceLoop) -{ - - - - TCPInterface * sts = ( TCPInterface * ) arguments; - - -// const int BUFF_SIZE=8096; - const unsigned int BUFF_SIZE=1048576; - //char data[ BUFF_SIZE ]; - char * data = (char*) rakMalloc_Ex(BUFF_SIZE,_FILE_AND_LINE_); - Packet *incomingMessage; - fd_set readFD, exceptionFD, writeFD; - sts->threadRunning.Increment(); - -#if RAKNET_SUPPORT_IPV6!=1 - sockaddr_in sockAddr; - int sockAddrSize = sizeof(sockAddr); -#else - struct sockaddr_storage sockAddr; - socklen_t sockAddrSize = sizeof(sockAddr); -#endif - - int len; - __TCPSOCKET__ newSock; - int selectResult; - - - timeval tv; - tv.tv_sec=0; - tv.tv_usec=30000; - - - while (sts->isStarted.GetValue()>0) - { -#if OPEN_SSL_CLIENT_SUPPORT==1 - SystemAddress *sslSystemAddress; - sslSystemAddress = sts->startSSL.PopInaccurate(); - if (sslSystemAddress) - { - if (sslSystemAddress->systemIndex>=0 && - sslSystemAddress->systemIndexremoteClientsLength && - sts->remoteClients[sslSystemAddress->systemIndex].systemAddress==*sslSystemAddress) - { - sts->remoteClients[sslSystemAddress->systemIndex].InitSSL(sts->ctx,sts->meth); - } - else - { - for (int i=0; i < sts->remoteClientsLength; i++) - { - sts->remoteClients[i].isActiveMutex.Lock(); - if (sts->remoteClients[i].isActive && sts->remoteClients[i].systemAddress==*sslSystemAddress) - { - if (sts->remoteClients[i].ssl==0) - sts->remoteClients[i].InitSSL(sts->ctx,sts->meth); - } - sts->remoteClients[i].isActiveMutex.Unlock(); - } - } - sts->startSSL.Deallocate(sslSystemAddress,_FILE_AND_LINE_); - } -#endif - - - __TCPSOCKET__ largestDescriptor=0; // see select__()'s first parameter's documentation under linux - - - // Linux' select__() implementation changes the timeout - - tv.tv_sec=0; - tv.tv_usec=30000; - - -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - while (1) - { - // Reset readFD, writeFD, and exceptionFD since select seems to clear it - FD_ZERO(&readFD); - FD_ZERO(&exceptionFD); - FD_ZERO(&writeFD); - largestDescriptor=0; - if (sts->listenSocket!=0) - { - FD_SET(sts->listenSocket, &readFD); - FD_SET(sts->listenSocket, &exceptionFD); - largestDescriptor = sts->listenSocket; // @see largestDescriptor def - } - - unsigned i; - for (i=0; i < (unsigned int) sts->remoteClientsLength; i++) - { - sts->remoteClients[i].isActiveMutex.Lock(); - if (sts->remoteClients[i].isActive) - { - // calling FD_ISSET with -1 as socket (that’s what 0 is set to) produces a bus error under Linux 64-Bit - __TCPSOCKET__ socketCopy = sts->remoteClients[i].socket; - if (socketCopy != 0) - { - FD_SET(socketCopy, &readFD); - FD_SET(socketCopy, &exceptionFD); - if (sts->remoteClients[i].outgoingData.GetBytesWritten()>0) - FD_SET(socketCopy, &writeFD); - if(socketCopy > largestDescriptor) // @see largestDescriptorDef - largestDescriptor = socketCopy; - } - } - sts->remoteClients[i].isActiveMutex.Unlock(); - } - -#ifdef _MSC_VER -#pragma warning( disable : 4244 ) // warning C4127: conditional expression is constant -#endif - - - selectResult=(int) select__(largestDescriptor+1, &readFD, &writeFD, &exceptionFD, &tv); - - - - - if (selectResult<=0) - break; - - if (sts->listenSocket!=0 && FD_ISSET(sts->listenSocket, &readFD)) - { - newSock = accept__(sts->listenSocket, (sockaddr*)&sockAddr, (socklen_t*)&sockAddrSize); - - if (newSock != 0) - { - int newRemoteClientIndex=-1; - for (newRemoteClientIndex=0; newRemoteClientIndex < sts->remoteClientsLength; newRemoteClientIndex++) - { - sts->remoteClients[newRemoteClientIndex].isActiveMutex.Lock(); - if (sts->remoteClients[newRemoteClientIndex].isActive==false) - { - sts->remoteClients[newRemoteClientIndex].socket=newSock; - -#if RAKNET_SUPPORT_IPV6!=1 - sts->remoteClients[newRemoteClientIndex].systemAddress.address.addr4.sin_addr.s_addr=sockAddr.sin_addr.s_addr; - sts->remoteClients[newRemoteClientIndex].systemAddress.SetPortNetworkOrder( sockAddr.sin_port); - sts->remoteClients[newRemoteClientIndex].systemAddress.systemIndex=newRemoteClientIndex; -#else - if (sockAddr.ss_family==AF_INET) - { - memcpy(&sts->remoteClients[newRemoteClientIndex].systemAddress.address.addr4,(sockaddr_in *)&sockAddr,sizeof(sockaddr_in)); - // sts->remoteClients[newRemoteClientIndex].systemAddress.address.addr4.sin_port=ntohs( sts->remoteClients[newRemoteClientIndex].systemAddress.address.addr4.sin_port ); - } - else - { - memcpy(&sts->remoteClients[newRemoteClientIndex].systemAddress.address.addr6,(sockaddr_in6 *)&sockAddr,sizeof(sockaddr_in6)); - // sts->remoteClients[newRemoteClientIndex].systemAddress.address.addr6.sin6_port=ntohs( sts->remoteClients[newRemoteClientIndex].systemAddress.address.addr6.sin6_port ); - } - -#endif // #if RAKNET_SUPPORT_IPV6!=1 - sts->remoteClients[newRemoteClientIndex].SetActive(true); - sts->remoteClients[newRemoteClientIndex].isActiveMutex.Unlock(); - - - SystemAddress *newConnectionSystemAddress=sts->newIncomingConnections.Allocate( _FILE_AND_LINE_ ); - *newConnectionSystemAddress=sts->remoteClients[newRemoteClientIndex].systemAddress; - sts->newIncomingConnections.Push(newConnectionSystemAddress); - - break; - } - sts->remoteClients[newRemoteClientIndex].isActiveMutex.Unlock(); - } - if (newRemoteClientIndex==-1) - { - closesocket__(sts->listenSocket); - } - } - else - { -#ifdef _DO_PRINTF - RAKNET_DEBUG_PRINTF("Error: connection failed\n"); -#endif - } - } - else if (sts->listenSocket!=0 && FD_ISSET(sts->listenSocket, &exceptionFD)) - { -#ifdef _DO_PRINTF - int err; - int errlen = sizeof(err); - getsockopt__(sts->listenSocket, SOL_SOCKET, SO_ERROR,(char*)&err, &errlen); - RAKNET_DEBUG_PRINTF("Socket error %s on listening socket\n", err); -#endif - } - - { - i=0; - while (i < (unsigned int) sts->remoteClientsLength) - { - if (sts->remoteClients[i].isActive==false) - { - i++; - continue; - } - // calling FD_ISSET with -1 as socket (that’s what 0 is set to) produces a bus error under Linux 64-Bit - __TCPSOCKET__ socketCopy = sts->remoteClients[i].socket; - if (socketCopy == 0) - { - i++; - continue; - } - - if (FD_ISSET(socketCopy, &exceptionFD)) - { -// #ifdef _DO_PRINTF -// if (sts->listenSocket!=-1) -// { -// int err; -// int errlen = sizeof(err); -// getsockopt__(sts->listenSocket, SOL_SOCKET, SO_ERROR,(char*)&err, &errlen); -// in_addr in; -// in.s_addr = sts->remoteClients[i].systemAddress.binaryAddress; -// RAKNET_DEBUG_PRINTF("Socket error %i on %s:%i\n", err,inet_ntoa( in ), sts->remoteClients[i].systemAddress.GetPort() ); -// } -// -// #endif - // Connection lost abruptly - SystemAddress *lostConnectionSystemAddress=sts->lostConnections.Allocate( _FILE_AND_LINE_ ); - *lostConnectionSystemAddress=sts->remoteClients[i].systemAddress; - sts->lostConnections.Push(lostConnectionSystemAddress); - sts->remoteClients[i].isActiveMutex.Lock(); - sts->remoteClients[i].SetActive(false); - sts->remoteClients[i].isActiveMutex.Unlock(); - } - else - { - if (FD_ISSET(socketCopy, &readFD)) - { - // if recv returns 0 this was a graceful close - len = sts->remoteClients[i].Recv(data,BUFF_SIZE); - - - // removeme -// data[len]=0; -// printf(data); - - if (len>0) - { - incomingMessage=sts->incomingMessages.Allocate( _FILE_AND_LINE_ ); - incomingMessage->data = (unsigned char*) rakMalloc_Ex( len+1, _FILE_AND_LINE_ ); - memcpy(incomingMessage->data, data, len); - incomingMessage->data[len]=0; // Null terminate this so we can print it out as regular strings. This is different from RakNet which does not do this. - // printf("RECV: %s\n",incomingMessage->data); - /* - if (1) - { - static FILE *fp=0; - if (fp==0) - { - fp = fopen("tcpRcv.txt", "wb"); - } - fwrite(data,1,len,fp); - } - */ - incomingMessage->length=len; - incomingMessage->deleteData=true; // actually means came from SPSC, rather than AllocatePacket - incomingMessage->systemAddress=sts->remoteClients[i].systemAddress; - sts->incomingMessages.Push(incomingMessage); - } - else - { - // Connection lost gracefully - SystemAddress *lostConnectionSystemAddress=sts->lostConnections.Allocate( _FILE_AND_LINE_ ); - *lostConnectionSystemAddress=sts->remoteClients[i].systemAddress; - sts->lostConnections.Push(lostConnectionSystemAddress); - sts->remoteClients[i].isActiveMutex.Lock(); - sts->remoteClients[i].SetActive(false); - sts->remoteClients[i].isActiveMutex.Unlock(); - continue; - } - } - if (FD_ISSET(socketCopy, &writeFD)) - { - RemoteClient *rc = &sts->remoteClients[i]; - unsigned int bytesInBuffer; - int bytesAvailable; - int bytesSent; - rc->outgoingDataMutex.Lock(); - bytesInBuffer=rc->outgoingData.GetBytesWritten(); - if (bytesInBuffer>0) - { - unsigned int contiguousLength; - char* contiguousBytesPointer = rc->outgoingData.PeekContiguousBytes(&contiguousLength); - if (contiguousLength < (unsigned int) BUFF_SIZE && contiguousLength BUFF_SIZE) - bytesAvailable=BUFF_SIZE; - else - bytesAvailable=bytesInBuffer; - rc->outgoingData.ReadBytes(data,bytesAvailable,true); - bytesSent=rc->Send(data,bytesAvailable); - } - else - { - bytesSent=rc->Send(contiguousBytesPointer,contiguousLength); - } - - if (bytesSent>0) - rc->outgoingData.IncrementReadOffset(bytesSent); - bytesInBuffer=rc->outgoingData.GetBytesWritten(); - } - rc->outgoingDataMutex.Unlock(); - } - - i++; // Nothing deleted so increment the index - } - } - } - } - - // Sleep 0 on Linux monopolizes the CPU - RakSleep(30); - } - sts->threadRunning.Decrement(); - - rakFree_Ex(data,_FILE_AND_LINE_); - - - - - return 0; - -} - -void RemoteClient::SetActive(bool a) -{ - if (isActive != a) - { - isActive=a; - Reset(); - if (isActive==false && socket!=0) - { - closesocket__(socket); - socket=0; - } - } -} -void RemoteClient::SendOrBuffer(const char **data, const unsigned int *lengths, const int numParameters) -{ - // True can save memory and buffer copies, but gives worse performance overall - // Do not use true for the XBOX, as it just locks up - const bool ALLOW_SEND_FROM_USER_THREAD=false; - - int parameterIndex; - if (isActive==false) - return; - parameterIndex=0; - for (; parameterIndex < numParameters; parameterIndex++) - { - outgoingDataMutex.Lock(); - if (ALLOW_SEND_FROM_USER_THREAD && outgoingData.GetBytesWritten()==0) - { - outgoingDataMutex.Unlock(); - int bytesSent = Send(data[parameterIndex],lengths[parameterIndex]); - if (bytesSent<(int) lengths[parameterIndex]) - { - // Push remainder - outgoingDataMutex.Lock(); - outgoingData.WriteBytes(data[parameterIndex]+bytesSent,lengths[parameterIndex]-bytesSent,_FILE_AND_LINE_); - outgoingDataMutex.Unlock(); - } - } - else - { - outgoingData.WriteBytes(data[parameterIndex],lengths[parameterIndex],_FILE_AND_LINE_); - outgoingDataMutex.Unlock(); - } - } -} -#if OPEN_SSL_CLIENT_SUPPORT==1 -bool RemoteClient::InitSSL(SSL_CTX* ctx, SSL_METHOD *meth) -{ - (void) meth; - - ssl = SSL_new (ctx); - RakAssert(ssl); - int res; - res = SSL_set_fd (ssl, socket); - if (res!=1) - { - printf("SSL_set_fd error: %s\n", ERR_reason_error_string(ERR_get_error())); - SSL_free(ssl); - ssl=0; - return false; - } - RakAssert(res==1); - res = SSL_connect (ssl); - if (res<0) - { - unsigned long err = ERR_get_error(); - printf("SSL_connect error: %s\n", ERR_reason_error_string(err)); - SSL_free(ssl); - ssl=0; - return false; - } - else if (res==0) - { - // The TLS/SSL handshake was not successful but was shut down controlled and by the specifications of the TLS/SSL protocol. Call SSL_get_error() with the return value ret to find out the reason. - int err = SSL_get_error(ssl, res); - switch (err) - { - case SSL_ERROR_NONE: - printf("SSL_ERROR_NONE\n"); - break; - case SSL_ERROR_ZERO_RETURN: - printf("SSL_ERROR_ZERO_RETURN\n"); - break; - case SSL_ERROR_WANT_READ: - printf("SSL_ERROR_WANT_READ\n"); - break; - case SSL_ERROR_WANT_WRITE: - printf("SSL_ERROR_WANT_WRITE\n"); - break; - case SSL_ERROR_WANT_CONNECT: - printf("SSL_ERROR_WANT_CONNECT\n"); - break; - case SSL_ERROR_WANT_ACCEPT: - printf("SSL_ERROR_WANT_ACCEPT\n"); - break; - case SSL_ERROR_WANT_X509_LOOKUP: - printf("SSL_ERROR_WANT_X509_LOOKUP\n"); - break; - case SSL_ERROR_SYSCALL: - { - // http://www.openssl.org/docs/ssl/SSL_get_error.html - char buff[1024]; - unsigned long ege = ERR_get_error(); - if (ege==0 && res==0) - printf("SSL_ERROR_SYSCALL EOF in violation of the protocol\n"); - else if (ege==0 && res==-1) - printf("SSL_ERROR_SYSCALL %s\n", strerror(errno)); - else - printf("SSL_ERROR_SYSCALL %s\n", ERR_error_string(ege, buff)); - } - break; - case SSL_ERROR_SSL: - printf("SSL_ERROR_SSL\n"); - break; - } - - } - - if (res!=1) - { - SSL_free(ssl); - ssl=0; - return false; - } - return true; -} -void RemoteClient::DisconnectSSL(void) -{ - if (ssl) - SSL_shutdown (ssl); /* send SSL/TLS close_notify */ -} -void RemoteClient::FreeSSL(void) -{ - if (ssl) - SSL_free (ssl); -} -int RemoteClient::Send(const char *data, unsigned int length) -{ - if (ssl) - return SSL_write (ssl, data, length); - else - return send__(socket, data, length, 0); -} -int RemoteClient::Recv(char *data, const int dataSize) -{ - if (ssl) - return SSL_read (ssl, data, dataSize); - else - return recv__(socket, data, dataSize, 0); -} -#else -int RemoteClient::Send(const char *data, unsigned int length) -{ -#ifdef __native_client__ - return -1; -#else - return send__(socket, data, length, 0); -#endif -} -int RemoteClient::Recv(char *data, const int dataSize) -{ -#ifdef __native_client__ - return -1; -#else - return recv__(socket, data, dataSize, 0); -#endif -} -#endif - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_TCPInterface==1 + +/// \file +/// \brief A simple TCP based server allowing sends and receives. Can be connected to by a telnet client. +/// + + + +#include "TCPInterface.h" +#ifdef _WIN32 + #if !defined (WINDOWS_STORE_RT) + typedef int socklen_t; + #endif + + +#else +#include +#include +#include +#endif +#include +#include "RakAssert.h" +#include +#include "RakAssert.h" +#include "RakSleep.h" +#include "StringCompressor.h" +#include "StringTable.h" +#include "Itoa.h" +#include "SocketLayer.h" +#include "SocketDefines.h" +#if (defined(__GNUC__) || defined(__GCCXML__)) && !defined(__WIN32__) +#include +#endif + +#ifdef _DO_PRINTF +#endif + +#ifdef _WIN32 +#include "WSAStartupSingleton.h" +#endif +namespace RakNet +{ +RAK_THREAD_DECLARATION(UpdateTCPInterfaceLoop); +RAK_THREAD_DECLARATION(ConnectionAttemptLoop); +} +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +using namespace RakNet; + +STATIC_FACTORY_DEFINITIONS(TCPInterface,TCPInterface); + +TCPInterface::TCPInterface() +{ +#if !defined(WINDOWS_STORE_RT) + listenSocket=0; +#endif + remoteClients=0; + remoteClientsLength=0; + + StringCompressor::AddReference(); + RakNet::StringTable::AddReference(); + +#if OPEN_SSL_CLIENT_SUPPORT==1 + ctx=0; + meth=0; +#endif + +#ifdef _WIN32 + WSAStartupSingleton::AddRef(); +#endif +} +TCPInterface::~TCPInterface() +{ + Stop(); +#ifdef _WIN32 + WSAStartupSingleton::Deref(); +#endif + + RakNet::OP_DELETE_ARRAY(remoteClients,_FILE_AND_LINE_); + + StringCompressor::RemoveReference(); + RakNet::StringTable::RemoveReference(); +} +#if !defined(WINDOWS_STORE_RT) +bool TCPInterface::CreateListenSocket(unsigned short port, unsigned short maxIncomingConnections, unsigned short socketFamily, const char *bindAddress) +{ + (void) maxIncomingConnections; + (void) socketFamily; +#if RAKNET_SUPPORT_IPV6!=1 + listenSocket = socket__(AF_INET, SOCK_STREAM, 0); + if ((int)listenSocket ==-1) + return false; + + struct sockaddr_in serverAddress; + memset(&serverAddress,0,sizeof(sockaddr_in)); + serverAddress.sin_family = AF_INET; + if ( bindAddress && bindAddress[0] ) + { + + + + + + serverAddress.sin_addr.s_addr = inet_addr__(bindAddress ); + + } + else + serverAddress.sin_addr.s_addr = INADDR_ANY; + + serverAddress.sin_port = htons(port); + + SocketLayer::SetSocketOptions(listenSocket, false, false); + + if (bind__(listenSocket,(struct sockaddr *) &serverAddress,sizeof(serverAddress)) < 0) + return false; + + listen__(listenSocket, maxIncomingConnections); +#else + struct addrinfo hints; + memset(&hints, 0, sizeof (addrinfo)); // make sure the struct is empty + hints.ai_family = socketFamily; // don't care IPv4 or IPv6 + hints.ai_socktype = SOCK_STREAM; // TCP sockets + hints.ai_flags = AI_PASSIVE; // fill in my IP for me + struct addrinfo *servinfo=0, *aip; // will point to the results + char portStr[32]; + Itoa(port,portStr,10); + + getaddrinfo(0, portStr, &hints, &servinfo); + for (aip = servinfo; aip != nullptr; aip = aip->ai_next) + { + // Open socket. The address type depends on what + // getaddrinfo() gave us. + listenSocket = socket__(aip->ai_family, aip->ai_socktype, aip->ai_protocol); + if (listenSocket != 0) + { + int ret = bind__( listenSocket, aip->ai_addr, (int) aip->ai_addrlen ); + if (ret>=0) + { + break; + } + else + { + closesocket__(listenSocket); + listenSocket=0; + } + } + } + + if (listenSocket==0) + return false; + + SocketLayer::SetSocketOptions(listenSocket, false, false); + + listen__(listenSocket, maxIncomingConnections); +#endif // #if RAKNET_SUPPORT_IPV6!=1 + + return true; +} +#endif + +#if defined(WINDOWS_STORE_RT) +bool TCPInterface::CreateListenSocket_WinStore8(unsigned short port, unsigned short maxIncomingConnections, unsigned short socketFamily, const char *bindAddress) +{ + listenSocket = WinRTCreateStreamSocket(AF_INET, SOCK_STREAM, 0); + return true; +} +#endif +bool TCPInterface::Start(unsigned short port, unsigned short maxIncomingConnections, unsigned short maxConnections, int _threadPriority, unsigned short socketFamily, const char *bindAddress) +{ +#ifdef __native_client__ + return false; +#else + (void) socketFamily; + + if (isStarted.GetValue()>0) + return false; + + threadPriority=_threadPriority; + + if (threadPriority==-99999) + { + + +#if defined(_WIN32) + threadPriority=0; + + +#else + threadPriority=1000; +#endif + } + + isStarted.Increment(); + if (maxConnections==0) + maxConnections=maxIncomingConnections; + if (maxConnections==0) + maxConnections=1; + remoteClientsLength=maxConnections; + remoteClients=RakNet::OP_NEW_ARRAY(maxConnections,_FILE_AND_LINE_); + + + listenSocket=0; + if (maxIncomingConnections>0) + { +#if defined(WINDOWS_STORE_RT) + CreateListenSocket_WinStore8(port, maxIncomingConnections, socketFamily, bindAddress); +#else + CreateListenSocket(port, maxIncomingConnections, socketFamily, bindAddress); +#endif + } + + + // Start the update thread + int errorCode; + + + + + + errorCode = RakNet::RakThread::Create(UpdateTCPInterfaceLoop, this, threadPriority); + + + if (errorCode!=0) + return false; + + while (threadRunning.GetValue()==0) + RakSleep(0); + + unsigned int i; + for (i=0; i < messageHandlerList.Size(); i++) + messageHandlerList[i]->OnRakPeerStartup(); + + return true; +#endif // __native_client__ +} +void TCPInterface::Stop(void) +{ + unsigned int i; + for (i=0; i < messageHandlerList.Size(); i++) + messageHandlerList[i]->OnRakPeerShutdown(); + +#ifndef __native_client__ + if (isStarted.GetValue()==0) + return; + +#if OPEN_SSL_CLIENT_SUPPORT==1 + for (i=0; i < remoteClientsLength; i++) + remoteClients[i].DisconnectSSL(); +#endif + + isStarted.Decrement(); + +#if !defined(WINDOWS_STORE_RT) + if (listenSocket!=0) +#endif + { +#ifdef _WIN32 + shutdown__(listenSocket, SD_BOTH); + +#else + shutdown__(listenSocket, SHUT_RDWR); +#endif + closesocket__(listenSocket); + } + + // Abort waiting connect calls + blockingSocketListMutex.Lock(); + for (i=0; i < blockingSocketList.Size(); i++) + { + closesocket__(blockingSocketList[i]); + } + blockingSocketListMutex.Unlock(); + + // Wait for the thread to stop + while ( threadRunning.GetValue()>0 ) + RakSleep(15); + + RakSleep(100); + + #if !defined(WINDOWS_STORE_RT) + listenSocket=0; + #endif + + // Stuff from here on to the end of the function is not threadsafe + for (i=0; i < (unsigned int) remoteClientsLength; i++) + { + closesocket__(remoteClients[i].socket); +#if OPEN_SSL_CLIENT_SUPPORT==1 + remoteClients[i].FreeSSL(); +#endif + } + remoteClientsLength=0; + RakNet::OP_DELETE_ARRAY(remoteClients,_FILE_AND_LINE_); + remoteClients=0; + + incomingMessages.Clear(_FILE_AND_LINE_); + newIncomingConnections.Clear(_FILE_AND_LINE_); + newRemoteClients.Clear(_FILE_AND_LINE_); + lostConnections.Clear(_FILE_AND_LINE_); + requestedCloseConnections.Clear(_FILE_AND_LINE_); + failedConnectionAttempts.Clear(_FILE_AND_LINE_); + completedConnectionAttempts.Clear(_FILE_AND_LINE_); + failedConnectionAttempts.Clear(_FILE_AND_LINE_); + for (i=0; i < headPush.Size(); i++) + DeallocatePacket(headPush[i]); + headPush.Clear(_FILE_AND_LINE_); + for (i=0; i < tailPush.Size(); i++) + DeallocatePacket(tailPush[i]); + tailPush.Clear(_FILE_AND_LINE_); + +#if OPEN_SSL_CLIENT_SUPPORT==1 + SSL_CTX_free (ctx); + startSSL.Clear(_FILE_AND_LINE_); + activeSSLConnections.Clear(false, _FILE_AND_LINE_); +#endif + + + + + +#endif // __native_client__ +} +SystemAddress TCPInterface::Connect(const char* host, unsigned short remotePort, bool block, unsigned short socketFamily, const char *bindAddress) +{ + if (threadRunning.GetValue()==0) + return UNASSIGNED_SYSTEM_ADDRESS; + + int newRemoteClientIndex=-1; + for (newRemoteClientIndex=0; newRemoteClientIndex < remoteClientsLength; newRemoteClientIndex++) + { + remoteClients[newRemoteClientIndex].isActiveMutex.Lock(); + if (remoteClients[newRemoteClientIndex].isActive==false) + { + remoteClients[newRemoteClientIndex].SetActive(true); + remoteClients[newRemoteClientIndex].isActiveMutex.Unlock(); + break; + } + remoteClients[newRemoteClientIndex].isActiveMutex.Unlock(); + } + if (newRemoteClientIndex==-1) + return UNASSIGNED_SYSTEM_ADDRESS; + + if (block) + { + SystemAddress systemAddress; + systemAddress.FromString(host); + systemAddress.SetPortHostOrder(remotePort); + systemAddress.systemIndex=(SystemIndex) newRemoteClientIndex; + char buffout[128]; + systemAddress.ToString(false,buffout); + + __TCPSOCKET__ sockfd = SocketConnect(buffout, remotePort, socketFamily, bindAddress); + // Windows RT TODO +#if !defined(WINDOWS_STORE_RT) + if (sockfd==0) +#endif + { + remoteClients[newRemoteClientIndex].isActiveMutex.Lock(); + remoteClients[newRemoteClientIndex].SetActive(false); + remoteClients[newRemoteClientIndex].isActiveMutex.Unlock(); + + failedConnectionAttemptMutex.Lock(); + failedConnectionAttempts.Push(systemAddress, _FILE_AND_LINE_ ); + failedConnectionAttemptMutex.Unlock(); + + return UNASSIGNED_SYSTEM_ADDRESS; + } + + remoteClients[newRemoteClientIndex].socket=sockfd; + remoteClients[newRemoteClientIndex].systemAddress=systemAddress; + + completedConnectionAttemptMutex.Lock(); + completedConnectionAttempts.Push(remoteClients[newRemoteClientIndex].systemAddress, _FILE_AND_LINE_ ); + completedConnectionAttemptMutex.Unlock(); + + return remoteClients[newRemoteClientIndex].systemAddress; + } + else + { + ThisPtrPlusSysAddr *s = RakNet::OP_NEW( _FILE_AND_LINE_ ); + s->systemAddress.FromStringExplicitPort(host,remotePort); + s->systemAddress.systemIndex=(SystemIndex) newRemoteClientIndex; + if (bindAddress) + strcpy(s->bindAddress, bindAddress); + else + s->bindAddress[0]=0; + s->tcpInterface=this; + s->socketFamily=socketFamily; + + // Start the connection thread + int errorCode; + + + + + errorCode = RakNet::RakThread::Create(ConnectionAttemptLoop, s, threadPriority); + + if (errorCode!=0) + { + RakNet::OP_DELETE(s, _FILE_AND_LINE_); + failedConnectionAttempts.Push(s->systemAddress, _FILE_AND_LINE_ ); + } + return UNASSIGNED_SYSTEM_ADDRESS; + } +} +#if OPEN_SSL_CLIENT_SUPPORT==1 +void TCPInterface::StartSSLClient(SystemAddress systemAddress) +{ + if (ctx==0) + { + sharedSslMutex.Lock(); + SSLeay_add_ssl_algorithms(); + meth = (SSL_METHOD*) SSLv23_client_method(); + SSL_load_error_strings(); + ctx = SSL_CTX_new (meth); + RakAssert(ctx!=0); + sharedSslMutex.Unlock(); + } + + SystemAddress *id = startSSL.Allocate( _FILE_AND_LINE_ ); + *id=systemAddress; + startSSL.Push(id); + unsigned index = activeSSLConnections.GetIndexOf(systemAddress); + if (index==(unsigned)-1) + activeSSLConnections.Insert(systemAddress,_FILE_AND_LINE_); +} +bool TCPInterface::IsSSLActive(SystemAddress systemAddress) +{ + return activeSSLConnections.GetIndexOf(systemAddress)!=-1; +} +#endif +void TCPInterface::Send( const char *data, unsigned length, const SystemAddress &systemAddress, bool broadcast ) +{ + SendList( &data, &length, 1, systemAddress,broadcast ); +} +bool TCPInterface::SendList( const char **data, const unsigned int *lengths, const int numParameters, const SystemAddress &systemAddress, bool broadcast ) +{ + if (isStarted.GetValue()==0) + return false; + if (data==0) + return false; + if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS && broadcast==false) + return false; + unsigned int totalLength=0; + int i; + for (i=0; i < numParameters; i++) + { + if (lengths[i]>0) + totalLength+=lengths[i]; + } + if (totalLength==0) + return false; + + if (broadcast) + { + // Send to all, possible exception system + for (i=0; i < remoteClientsLength; i++) + { + if (remoteClients[i].systemAddress!=systemAddress) + { + remoteClients[i].SendOrBuffer(data, lengths, numParameters); + } + } + } + else + { + // Send to this player + if (systemAddress.systemIndexUpdate(); + + Packet* outgoingPacket = ReceiveInt(); + + if (outgoingPacket) + { + PluginReceiveResult pluginResult; + for (i=0; i < messageHandlerList.Size(); i++) + { + pluginResult=messageHandlerList[i]->OnReceive(outgoingPacket); + if (pluginResult==RR_STOP_PROCESSING_AND_DEALLOCATE) + { + DeallocatePacket( outgoingPacket ); + outgoingPacket=0; // Will do the loop again and get another packet + break; // break out of the enclosing for + } + else if (pluginResult==RR_STOP_PROCESSING) + { + outgoingPacket=0; + break; + } + } + } + + + return outgoingPacket; +} +Packet* TCPInterface::ReceiveInt( void ) +{ + if (isStarted.GetValue()==0) + return 0; + if (headPush.IsEmpty()==false) + return headPush.Pop(); + Packet *p = incomingMessages.PopInaccurate(); + if (p) + return p; + if (tailPush.IsEmpty()==false) + return tailPush.Pop(); + return 0; +} + + +void TCPInterface::AttachPlugin( PluginInterface2 *plugin ) +{ + if (messageHandlerList.GetIndexOf(plugin)==MAX_UNSIGNED_LONG) + { + messageHandlerList.Insert(plugin, _FILE_AND_LINE_); + plugin->SetTCPInterface(this); + plugin->OnAttach(); + } +} +void TCPInterface::DetachPlugin( PluginInterface2 *plugin ) +{ + if (plugin==0) + return; + + unsigned int index; + index = messageHandlerList.GetIndexOf(plugin); + if (index!=MAX_UNSIGNED_LONG) + { + messageHandlerList[index]->OnDetach(); + // Unordered list so delete from end for speed + messageHandlerList[index]=messageHandlerList[messageHandlerList.Size()-1]; + messageHandlerList.RemoveFromEnd(); + plugin->SetTCPInterface(0); + } +} +void TCPInterface::CloseConnection( SystemAddress systemAddress ) +{ + if (isStarted.GetValue()==0) + return; + if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS) + return; + + unsigned int i; + for (i=0; i < messageHandlerList.Size(); i++) + messageHandlerList[i]->OnClosedConnection(systemAddress, UNASSIGNED_RAKNET_GUID, LCR_CLOSED_BY_USER); + + if (systemAddress.systemIndexdeleteData) + { + rakFree_Ex(packet->data, _FILE_AND_LINE_ ); + incomingMessages.Deallocate(packet, _FILE_AND_LINE_); + } + else + { + // Came from userspace AllocatePacket + rakFree_Ex(packet->data, _FILE_AND_LINE_ ); + RakNet::OP_DELETE(packet, _FILE_AND_LINE_); + } +} +Packet* TCPInterface::AllocatePacket(unsigned dataSize) +{ + Packet*p = RakNet::OP_NEW(_FILE_AND_LINE_); + p->data=(unsigned char*) rakMalloc_Ex(dataSize,_FILE_AND_LINE_); + p->length=dataSize; + p->bitSize=BYTES_TO_BITS(dataSize); + p->deleteData=false; + p->guid=UNASSIGNED_RAKNET_GUID; + p->systemAddress=UNASSIGNED_SYSTEM_ADDRESS; + p->systemAddress.systemIndex=(SystemIndex)-1; + return p; +} +void TCPInterface::PushBackPacket( Packet *packet, bool pushAtHead ) +{ + if (pushAtHead) + headPush.Push(packet, _FILE_AND_LINE_ ); + else + tailPush.Push(packet, _FILE_AND_LINE_ ); +} +bool TCPInterface::WasStarted(void) const +{ + return threadRunning.GetValue()>0; +} +SystemAddress TCPInterface::HasCompletedConnectionAttempt(void) +{ + SystemAddress sysAddr=UNASSIGNED_SYSTEM_ADDRESS; + completedConnectionAttemptMutex.Lock(); + if (completedConnectionAttempts.IsEmpty()==false) + sysAddr=completedConnectionAttempts.Pop(); + completedConnectionAttemptMutex.Unlock(); + + if (sysAddr!=UNASSIGNED_SYSTEM_ADDRESS) + { + unsigned int i; + for (i=0; i < messageHandlerList.Size(); i++) + messageHandlerList[i]->OnNewConnection(sysAddr, UNASSIGNED_RAKNET_GUID, true); + } + + return sysAddr; +} +SystemAddress TCPInterface::HasFailedConnectionAttempt(void) +{ + SystemAddress sysAddr=UNASSIGNED_SYSTEM_ADDRESS; + failedConnectionAttemptMutex.Lock(); + if (failedConnectionAttempts.IsEmpty()==false) + sysAddr=failedConnectionAttempts.Pop(); + failedConnectionAttemptMutex.Unlock(); + + if (sysAddr!=UNASSIGNED_SYSTEM_ADDRESS) + { + unsigned int i; + for (i=0; i < messageHandlerList.Size(); i++) + { + Packet p; + p.systemAddress=sysAddr; + p.data=0; + p.length=0; + p.bitSize=0; + messageHandlerList[i]->OnFailedConnectionAttempt(&p, FCAR_CONNECTION_ATTEMPT_FAILED); + } + } + + return sysAddr; +} +SystemAddress TCPInterface::HasNewIncomingConnection(void) +{ + SystemAddress *out, out2; + out = newIncomingConnections.PopInaccurate(); + if (out) + { + out2=*out; + newIncomingConnections.Deallocate(out, _FILE_AND_LINE_); + + unsigned int i; + for (i=0; i < messageHandlerList.Size(); i++) + messageHandlerList[i]->OnNewConnection(out2, UNASSIGNED_RAKNET_GUID, true); + + return *out; + } + else + { + return UNASSIGNED_SYSTEM_ADDRESS; + } +} +SystemAddress TCPInterface::HasLostConnection(void) +{ + SystemAddress *out, out2; + out = lostConnections.PopInaccurate(); + if (out) + { + out2=*out; + lostConnections.Deallocate(out, _FILE_AND_LINE_); + + unsigned int i; + for (i=0; i < messageHandlerList.Size(); i++) + messageHandlerList[i]->OnClosedConnection(out2, UNASSIGNED_RAKNET_GUID, LCR_DISCONNECTION_NOTIFICATION); + + return *out; + } + else + { + return UNASSIGNED_SYSTEM_ADDRESS; + } +} +void TCPInterface::GetConnectionList( SystemAddress *remoteSystems, unsigned short *numberOfSystems ) const +{ + unsigned short systemCount=0; + unsigned short maxToWrite=*numberOfSystems; + for (int i=0; i < remoteClientsLength; i++) + { + if (remoteClients[i].isActive) + { + if (systemCount < maxToWrite) + remoteSystems[systemCount]=remoteClients[i].systemAddress; + systemCount++; + } + } + *numberOfSystems=systemCount; +} +unsigned short TCPInterface::GetConnectionCount(void) const +{ + unsigned short systemCount=0; + for (int i=0; i < remoteClientsLength; i++) + { + if (remoteClients[i].isActive) + systemCount++; + } + return systemCount; +} + +unsigned int TCPInterface::GetOutgoingDataBufferSize(SystemAddress systemAddress) const +{ + unsigned bytesWritten=0; + if (systemAddress.systemIndexh_addr, server->h_length); + + + + + + + + + + + blockingSocketListMutex.Lock(); + blockingSocketList.Insert(sockfd, _FILE_AND_LINE_); + blockingSocketListMutex.Unlock(); + + // This is blocking + connectResult = connect__( sockfd, ( struct sockaddr * ) &serverAddress, sizeof( struct sockaddr ) ); + +#else + + + struct addrinfo hints, *res; + int sockfd; + memset(&hints, 0, sizeof hints); + hints.ai_family = socketFamily; + hints.ai_socktype = SOCK_STREAM; + char portStr[32]; + Itoa(remotePort,portStr,10); + getaddrinfo(host, portStr, &hints, &res); + sockfd = socket__(res->ai_family, res->ai_socktype, res->ai_protocol); + blockingSocketListMutex.Lock(); + blockingSocketList.Insert(sockfd, _FILE_AND_LINE_); + blockingSocketListMutex.Unlock(); + connectResult=connect__(sockfd, res->ai_addr, res->ai_addrlen); + freeaddrinfo(res); // free the linked-list + +#endif // #if RAKNET_SUPPORT_IPV6!=1 + + if (connectResult==-1) + { + unsigned sockfdIndex; + blockingSocketListMutex.Lock(); + sockfdIndex=blockingSocketList.GetIndexOf(sockfd); + if (sockfdIndex!=(unsigned)-1) + blockingSocketList.RemoveAtIndexFast(sockfdIndex); + blockingSocketListMutex.Unlock(); + + closesocket__(sockfd); + return 0; + } + + return sockfd; +#endif // __native_client__ +} + +RAK_THREAD_DECLARATION(RakNet::ConnectionAttemptLoop) +{ + + + + TCPInterface::ThisPtrPlusSysAddr *s = (TCPInterface::ThisPtrPlusSysAddr *) arguments; + + + + SystemAddress systemAddress = s->systemAddress; + TCPInterface *tcpInterface = s->tcpInterface; + int newRemoteClientIndex=systemAddress.systemIndex; + unsigned short socketFamily = s->socketFamily; + RakNet::OP_DELETE(s, _FILE_AND_LINE_); + + char str1[64]; + systemAddress.ToString(false, str1); + __TCPSOCKET__ sockfd = tcpInterface->SocketConnect(str1, systemAddress.GetPort(), socketFamily, s->bindAddress); + if (sockfd==0) + { + tcpInterface->remoteClients[newRemoteClientIndex].isActiveMutex.Lock(); + tcpInterface->remoteClients[newRemoteClientIndex].SetActive(false); + tcpInterface->remoteClients[newRemoteClientIndex].isActiveMutex.Unlock(); + + tcpInterface->failedConnectionAttemptMutex.Lock(); + tcpInterface->failedConnectionAttempts.Push(systemAddress, _FILE_AND_LINE_ ); + tcpInterface->failedConnectionAttemptMutex.Unlock(); + return 0; + } + + tcpInterface->remoteClients[newRemoteClientIndex].socket=sockfd; + tcpInterface->remoteClients[newRemoteClientIndex].systemAddress=systemAddress; + + // Notify user that the connection attempt has completed. + if (tcpInterface->threadRunning.GetValue()>0) + { + tcpInterface->completedConnectionAttemptMutex.Lock(); + tcpInterface->completedConnectionAttempts.Push(systemAddress, _FILE_AND_LINE_ ); + tcpInterface->completedConnectionAttemptMutex.Unlock(); + } + + + + + return 0; + +} + +RAK_THREAD_DECLARATION(RakNet::UpdateTCPInterfaceLoop) +{ + + + + TCPInterface * sts = ( TCPInterface * ) arguments; + + +// const int BUFF_SIZE=8096; + const unsigned int BUFF_SIZE=1048576; + //char data[ BUFF_SIZE ]; + char * data = (char*) rakMalloc_Ex(BUFF_SIZE,_FILE_AND_LINE_); + Packet *incomingMessage; + fd_set readFD, exceptionFD, writeFD; + sts->threadRunning.Increment(); + +#if RAKNET_SUPPORT_IPV6!=1 + sockaddr_in sockAddr; + int sockAddrSize = sizeof(sockAddr); +#else + struct sockaddr_storage sockAddr; + socklen_t sockAddrSize = sizeof(sockAddr); +#endif + + int len; + __TCPSOCKET__ newSock; + int selectResult; + + + timeval tv; + tv.tv_sec=0; + tv.tv_usec=30000; + + + while (sts->isStarted.GetValue()>0) + { +#if OPEN_SSL_CLIENT_SUPPORT==1 + SystemAddress *sslSystemAddress; + sslSystemAddress = sts->startSSL.PopInaccurate(); + if (sslSystemAddress) + { + if (sslSystemAddress->systemIndex>=0 && + sslSystemAddress->systemIndexremoteClientsLength && + sts->remoteClients[sslSystemAddress->systemIndex].systemAddress==*sslSystemAddress) + { + sts->remoteClients[sslSystemAddress->systemIndex].InitSSL(sts->ctx,sts->meth); + } + else + { + for (int i=0; i < sts->remoteClientsLength; i++) + { + sts->remoteClients[i].isActiveMutex.Lock(); + if (sts->remoteClients[i].isActive && sts->remoteClients[i].systemAddress==*sslSystemAddress) + { + if (sts->remoteClients[i].ssl==0) + sts->remoteClients[i].InitSSL(sts->ctx,sts->meth); + } + sts->remoteClients[i].isActiveMutex.Unlock(); + } + } + sts->startSSL.Deallocate(sslSystemAddress,_FILE_AND_LINE_); + } +#endif + + + __TCPSOCKET__ largestDescriptor=0; // see select__()'s first parameter's documentation under linux + + + // Linux' select__() implementation changes the timeout + + tv.tv_sec=0; + tv.tv_usec=30000; + + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + while (1) + { + // Reset readFD, writeFD, and exceptionFD since select seems to clear it + FD_ZERO(&readFD); + FD_ZERO(&exceptionFD); + FD_ZERO(&writeFD); + largestDescriptor=0; + if (sts->listenSocket!=0) + { + FD_SET(sts->listenSocket, &readFD); + FD_SET(sts->listenSocket, &exceptionFD); + largestDescriptor = sts->listenSocket; // @see largestDescriptor def + } + + unsigned i; + for (i=0; i < (unsigned int) sts->remoteClientsLength; i++) + { + sts->remoteClients[i].isActiveMutex.Lock(); + if (sts->remoteClients[i].isActive) + { + // calling FD_ISSET with -1 as socket (that�s what 0 is set to) produces a bus error under Linux 64-Bit + __TCPSOCKET__ socketCopy = sts->remoteClients[i].socket; + if (socketCopy != 0) + { + FD_SET(socketCopy, &readFD); + FD_SET(socketCopy, &exceptionFD); + if (sts->remoteClients[i].outgoingData.GetBytesWritten()>0) + FD_SET(socketCopy, &writeFD); + if(socketCopy > largestDescriptor) // @see largestDescriptorDef + largestDescriptor = socketCopy; + } + } + sts->remoteClients[i].isActiveMutex.Unlock(); + } + +#ifdef _MSC_VER +#pragma warning( disable : 4244 ) // warning C4127: conditional expression is constant +#endif + + + selectResult=(int) select__(largestDescriptor+1, &readFD, &writeFD, &exceptionFD, &tv); + + + + + if (selectResult<=0) + break; + + if (sts->listenSocket!=0 && FD_ISSET(sts->listenSocket, &readFD)) + { + newSock = accept__(sts->listenSocket, (sockaddr*)&sockAddr, (socklen_t*)&sockAddrSize); + + if (newSock != 0) + { + int newRemoteClientIndex=-1; + for (newRemoteClientIndex=0; newRemoteClientIndex < sts->remoteClientsLength; newRemoteClientIndex++) + { + sts->remoteClients[newRemoteClientIndex].isActiveMutex.Lock(); + if (sts->remoteClients[newRemoteClientIndex].isActive==false) + { + sts->remoteClients[newRemoteClientIndex].socket=newSock; + +#if RAKNET_SUPPORT_IPV6!=1 + sts->remoteClients[newRemoteClientIndex].systemAddress.address.addr4.sin_addr.s_addr=sockAddr.sin_addr.s_addr; + sts->remoteClients[newRemoteClientIndex].systemAddress.SetPortNetworkOrder( sockAddr.sin_port); + sts->remoteClients[newRemoteClientIndex].systemAddress.systemIndex=newRemoteClientIndex; +#else + if (sockAddr.ss_family==AF_INET) + { + memcpy(&sts->remoteClients[newRemoteClientIndex].systemAddress.address.addr4,(sockaddr_in *)&sockAddr,sizeof(sockaddr_in)); + // sts->remoteClients[newRemoteClientIndex].systemAddress.address.addr4.sin_port=ntohs( sts->remoteClients[newRemoteClientIndex].systemAddress.address.addr4.sin_port ); + } + else + { + memcpy(&sts->remoteClients[newRemoteClientIndex].systemAddress.address.addr6,(sockaddr_in6 *)&sockAddr,sizeof(sockaddr_in6)); + // sts->remoteClients[newRemoteClientIndex].systemAddress.address.addr6.sin6_port=ntohs( sts->remoteClients[newRemoteClientIndex].systemAddress.address.addr6.sin6_port ); + } + +#endif // #if RAKNET_SUPPORT_IPV6!=1 + sts->remoteClients[newRemoteClientIndex].SetActive(true); + sts->remoteClients[newRemoteClientIndex].isActiveMutex.Unlock(); + + + SystemAddress *newConnectionSystemAddress=sts->newIncomingConnections.Allocate( _FILE_AND_LINE_ ); + *newConnectionSystemAddress=sts->remoteClients[newRemoteClientIndex].systemAddress; + sts->newIncomingConnections.Push(newConnectionSystemAddress); + + break; + } + sts->remoteClients[newRemoteClientIndex].isActiveMutex.Unlock(); + } + if (newRemoteClientIndex==-1) + { + closesocket__(sts->listenSocket); + } + } + else + { +#ifdef _DO_PRINTF + RAKNET_DEBUG_PRINTF("Error: connection failed\n"); +#endif + } + } + else if (sts->listenSocket!=0 && FD_ISSET(sts->listenSocket, &exceptionFD)) + { +#ifdef _DO_PRINTF + int err; + int errlen = sizeof(err); + getsockopt__(sts->listenSocket, SOL_SOCKET, SO_ERROR,(char*)&err, &errlen); + RAKNET_DEBUG_PRINTF("Socket error %s on listening socket\n", err); +#endif + } + + { + i=0; + while (i < (unsigned int) sts->remoteClientsLength) + { + if (sts->remoteClients[i].isActive==false) + { + i++; + continue; + } + // calling FD_ISSET with -1 as socket (that�s what 0 is set to) produces a bus error under Linux 64-Bit + __TCPSOCKET__ socketCopy = sts->remoteClients[i].socket; + if (socketCopy == 0) + { + i++; + continue; + } + + if (FD_ISSET(socketCopy, &exceptionFD)) + { +// #ifdef _DO_PRINTF +// if (sts->listenSocket!=-1) +// { +// int err; +// int errlen = sizeof(err); +// getsockopt__(sts->listenSocket, SOL_SOCKET, SO_ERROR,(char*)&err, &errlen); +// in_addr in; +// in.s_addr = sts->remoteClients[i].systemAddress.binaryAddress; +// RAKNET_DEBUG_PRINTF("Socket error %i on %s:%i\n", err,inet_ntoa( in ), sts->remoteClients[i].systemAddress.GetPort() ); +// } +// +// #endif + // Connection lost abruptly + SystemAddress *lostConnectionSystemAddress=sts->lostConnections.Allocate( _FILE_AND_LINE_ ); + *lostConnectionSystemAddress=sts->remoteClients[i].systemAddress; + sts->lostConnections.Push(lostConnectionSystemAddress); + sts->remoteClients[i].isActiveMutex.Lock(); + sts->remoteClients[i].SetActive(false); + sts->remoteClients[i].isActiveMutex.Unlock(); + } + else + { + if (FD_ISSET(socketCopy, &readFD)) + { + // if recv returns 0 this was a graceful close + len = sts->remoteClients[i].Recv(data,BUFF_SIZE); + + + // removeme +// data[len]=0; +// printf(data); + + if (len>0) + { + incomingMessage=sts->incomingMessages.Allocate( _FILE_AND_LINE_ ); + incomingMessage->data = (unsigned char*) rakMalloc_Ex( len+1, _FILE_AND_LINE_ ); + memcpy(incomingMessage->data, data, len); + incomingMessage->data[len]=0; // Null terminate this so we can print it out as regular strings. This is different from RakNet which does not do this. + // printf("RECV: %s\n",incomingMessage->data); + /* + if (1) + { + static FILE *fp=0; + if (fp==0) + { + fp = fopen("tcpRcv.txt", "wb"); + } + fwrite(data,1,len,fp); + } + */ + incomingMessage->length=len; + incomingMessage->deleteData=true; // actually means came from SPSC, rather than AllocatePacket + incomingMessage->systemAddress=sts->remoteClients[i].systemAddress; + sts->incomingMessages.Push(incomingMessage); + } + else + { + // Connection lost gracefully + SystemAddress *lostConnectionSystemAddress=sts->lostConnections.Allocate( _FILE_AND_LINE_ ); + *lostConnectionSystemAddress=sts->remoteClients[i].systemAddress; + sts->lostConnections.Push(lostConnectionSystemAddress); + sts->remoteClients[i].isActiveMutex.Lock(); + sts->remoteClients[i].SetActive(false); + sts->remoteClients[i].isActiveMutex.Unlock(); + continue; + } + } + if (FD_ISSET(socketCopy, &writeFD)) + { + RemoteClient *rc = &sts->remoteClients[i]; + unsigned int bytesInBuffer; + int bytesAvailable; + int bytesSent; + rc->outgoingDataMutex.Lock(); + bytesInBuffer=rc->outgoingData.GetBytesWritten(); + if (bytesInBuffer>0) + { + unsigned int contiguousLength; + char* contiguousBytesPointer = rc->outgoingData.PeekContiguousBytes(&contiguousLength); + if (contiguousLength < (unsigned int) BUFF_SIZE && contiguousLength BUFF_SIZE) + bytesAvailable=BUFF_SIZE; + else + bytesAvailable=bytesInBuffer; + rc->outgoingData.ReadBytes(data,bytesAvailable,true); + bytesSent=rc->Send(data,bytesAvailable); + } + else + { + bytesSent=rc->Send(contiguousBytesPointer,contiguousLength); + } + + if (bytesSent>0) + rc->outgoingData.IncrementReadOffset(bytesSent); + bytesInBuffer=rc->outgoingData.GetBytesWritten(); + } + rc->outgoingDataMutex.Unlock(); + } + + i++; // Nothing deleted so increment the index + } + } + } + } + + // Sleep 0 on Linux monopolizes the CPU + RakSleep(30); + } + sts->threadRunning.Decrement(); + + rakFree_Ex(data,_FILE_AND_LINE_); + + + + + return 0; + +} + +void RemoteClient::SetActive(bool a) +{ + if (isActive != a) + { + isActive=a; + Reset(); + if (isActive==false && socket!=0) + { + closesocket__(socket); + socket=0; + } + } +} +void RemoteClient::SendOrBuffer(const char **data, const unsigned int *lengths, const int numParameters) +{ + // True can save memory and buffer copies, but gives worse performance overall + // Do not use true for the XBOX, as it just locks up + const bool ALLOW_SEND_FROM_USER_THREAD=false; + + int parameterIndex; + if (isActive==false) + return; + parameterIndex=0; + for (; parameterIndex < numParameters; parameterIndex++) + { + outgoingDataMutex.Lock(); + if (ALLOW_SEND_FROM_USER_THREAD && outgoingData.GetBytesWritten()==0) + { + outgoingDataMutex.Unlock(); + int bytesSent = Send(data[parameterIndex],lengths[parameterIndex]); + if (bytesSent<(int) lengths[parameterIndex]) + { + // Push remainder + outgoingDataMutex.Lock(); + outgoingData.WriteBytes(data[parameterIndex]+bytesSent,lengths[parameterIndex]-bytesSent,_FILE_AND_LINE_); + outgoingDataMutex.Unlock(); + } + } + else + { + outgoingData.WriteBytes(data[parameterIndex],lengths[parameterIndex],_FILE_AND_LINE_); + outgoingDataMutex.Unlock(); + } + } +} +#if OPEN_SSL_CLIENT_SUPPORT==1 +bool RemoteClient::InitSSL(SSL_CTX* ctx, SSL_METHOD *meth) +{ + (void) meth; + + ssl = SSL_new (ctx); + RakAssert(ssl); + int res; + res = SSL_set_fd (ssl, socket); + if (res!=1) + { + printf("SSL_set_fd error: %s\n", ERR_reason_error_string(ERR_get_error())); + SSL_free(ssl); + ssl=0; + return false; + } + RakAssert(res==1); + res = SSL_connect (ssl); + if (res<0) + { + unsigned long err = ERR_get_error(); + printf("SSL_connect error: %s\n", ERR_reason_error_string(err)); + SSL_free(ssl); + ssl=0; + return false; + } + else if (res==0) + { + // The TLS/SSL handshake was not successful but was shut down controlled and by the specifications of the TLS/SSL protocol. Call SSL_get_error() with the return value ret to find out the reason. + int err = SSL_get_error(ssl, res); + switch (err) + { + case SSL_ERROR_NONE: + printf("SSL_ERROR_NONE\n"); + break; + case SSL_ERROR_ZERO_RETURN: + printf("SSL_ERROR_ZERO_RETURN\n"); + break; + case SSL_ERROR_WANT_READ: + printf("SSL_ERROR_WANT_READ\n"); + break; + case SSL_ERROR_WANT_WRITE: + printf("SSL_ERROR_WANT_WRITE\n"); + break; + case SSL_ERROR_WANT_CONNECT: + printf("SSL_ERROR_WANT_CONNECT\n"); + break; + case SSL_ERROR_WANT_ACCEPT: + printf("SSL_ERROR_WANT_ACCEPT\n"); + break; + case SSL_ERROR_WANT_X509_LOOKUP: + printf("SSL_ERROR_WANT_X509_LOOKUP\n"); + break; + case SSL_ERROR_SYSCALL: + { + // http://www.openssl.org/docs/ssl/SSL_get_error.html + char buff[1024]; + unsigned long ege = ERR_get_error(); + if (ege==0 && res==0) + printf("SSL_ERROR_SYSCALL EOF in violation of the protocol\n"); + else if (ege==0 && res==-1) + printf("SSL_ERROR_SYSCALL %s\n", strerror(errno)); + else + printf("SSL_ERROR_SYSCALL %s\n", ERR_error_string(ege, buff)); + } + break; + case SSL_ERROR_SSL: + printf("SSL_ERROR_SSL\n"); + break; + } + + } + + if (res!=1) + { + SSL_free(ssl); + ssl=0; + return false; + } + return true; +} +void RemoteClient::DisconnectSSL(void) +{ + if (ssl) + SSL_shutdown (ssl); /* send SSL/TLS close_notify */ +} +void RemoteClient::FreeSSL(void) +{ + if (ssl) + SSL_free (ssl); +} +int RemoteClient::Send(const char *data, unsigned int length) +{ + if (ssl) + return SSL_write (ssl, data, length); + else + return send__(socket, data, length, 0); +} +int RemoteClient::Recv(char *data, const int dataSize) +{ + if (ssl) + return SSL_read (ssl, data, dataSize); + else + return recv__(socket, data, dataSize, 0); +} +#else +int RemoteClient::Send(const char *data, unsigned int length) +{ +#ifdef __native_client__ + return -1; +#else + return send__(socket, data, length, 0); +#endif +} +int RemoteClient::Recv(char *data, const int dataSize) +{ +#ifdef __native_client__ + return -1; +#else + return recv__(socket, data, dataSize, 0); +#endif +} +#endif + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif + +#endif // _RAKNET_SUPPORT_* diff --git a/Source/TCPInterface.h b/Source/TCPInterface.h index d259e383a..c7641b453 100644 --- a/Source/TCPInterface.h +++ b/Source/TCPInterface.h @@ -1,263 +1,260 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief A simple TCP based server allowing sends and receives. Can be connected by any TCP client, including telnet. -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_TCPInterface==1 - -#ifndef __SIMPLE_TCP_SERVER -#define __SIMPLE_TCP_SERVER - -#include "RakMemoryOverride.h" -#include "DS_List.h" -#include "RakNetTypes.h" -#include "Export.h" -#include "RakThread.h" -#include "DS_Queue.h" -#include "SimpleMutex.h" -#include "RakNetDefines.h" -#include "SocketIncludes.h" -#include "DS_ByteQueue.h" -#include "DS_ThreadsafeAllocatingQueue.h" -#include "LocklessTypes.h" -#include "PluginInterface2.h" - -#if OPEN_SSL_CLIENT_SUPPORT==1 -#include -#include -#include -#include -#include -#endif - -namespace RakNet -{ -/// Forward declarations -struct RemoteClient; - -/// \internal -/// \brief As the name says, a simple multithreaded TCP server. Used by TelnetTransport -class RAK_DLL_EXPORT TCPInterface -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(TCPInterface) - - TCPInterface(); - virtual ~TCPInterface(); - - // TODO - add socketdescriptor - /// Starts the TCP server on the indicated port - /// \param[in] port Which port to listen on. - /// \param[in] maxIncomingConnections Max incoming connections we will accept - /// \param[in] maxConnections Max total connections, which should be >= maxIncomingConnections - /// \param[in] threadPriority Passed to the thread creation routine. Use THREAD_PRIORITY_NORMAL for Windows. For Linux based systems, you MUST pass something reasonable based on the thread priorities for your application. - /// \param[in] socketFamily IP version: For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC. - bool Start(unsigned short port, unsigned short maxIncomingConnections, unsigned short maxConnections=0, int _threadPriority=-99999, unsigned short socketFamily=AF_INET, const char *bindAddress=0); - - /// Stops the TCP server - void Stop(void); - - /// Connect to the specified host on the specified port - SystemAddress Connect(const char* host, unsigned short remotePort, bool block=true, unsigned short socketFamily=AF_INET, const char *bindAddress=0); - -#if OPEN_SSL_CLIENT_SUPPORT==1 - /// Start SSL on an existing connection, notified with HasCompletedConnectionAttempt - void StartSSLClient(SystemAddress systemAddress); - - /// Was SSL started on this socket? - bool IsSSLActive(SystemAddress systemAddress); -#endif - - /// Sends a byte stream - virtual void Send( const char *data, unsigned int length, const SystemAddress &systemAddress, bool broadcast ); - - // Sends a concatenated list of byte streams - virtual bool SendList( const char **data, const unsigned int *lengths, const int numParameters, const SystemAddress &systemAddress, bool broadcast ); - - // Get how many bytes are waiting to be sent. If too many, you may want to skip sending - unsigned int GetOutgoingDataBufferSize(SystemAddress systemAddress) const; - - /// Returns if Receive() will return data - /// Do not use on PacketizedTCP - virtual bool ReceiveHasPackets( void ); - - /// Returns data received - virtual Packet* Receive( void ); - - /// Disconnects a player/address - void CloseConnection( SystemAddress systemAddress ); - - /// Deallocates a packet returned by Receive - void DeallocatePacket( Packet *packet ); - - /// Fills the array remoteSystems with the SystemAddress of all the systems we are connected to - /// \param[out] remoteSystems An array of SystemAddress structures to be filled with the SystemAddresss of the systems we are connected to. Pass 0 to remoteSystems to only get the number of systems we are connected to - /// \param[in, out] numberOfSystems As input, the size of remoteSystems array. As output, the number of elements put into the array - void GetConnectionList( SystemAddress *remoteSystems, unsigned short *numberOfSystems ) const; - - /// Returns just the number of connections we have - unsigned short GetConnectionCount(void) const; - - /// Has a previous call to connect succeeded? - /// \return UNASSIGNED_SYSTEM_ADDRESS = no. Anything else means yes. - SystemAddress HasCompletedConnectionAttempt(void); - - /// Has a previous call to connect failed? - /// \return UNASSIGNED_SYSTEM_ADDRESS = no. Anything else means yes. - SystemAddress HasFailedConnectionAttempt(void); - - /// Queued events of new incoming connections - SystemAddress HasNewIncomingConnection(void); - - /// Queued events of lost connections - SystemAddress HasLostConnection(void); - - /// Return an allocated but empty packet, for custom use - Packet* AllocatePacket(unsigned dataSize); - - // Push a packet back to the queue - virtual void PushBackPacket( Packet *packet, bool pushAtHead ); - - /// Returns if Start() was called successfully - bool WasStarted(void) const; - - void AttachPlugin( PluginInterface2 *plugin ); - void DetachPlugin( PluginInterface2 *plugin ); -protected: - - Packet* ReceiveInt( void ); - -#if defined(WINDOWS_STORE_RT) - bool CreateListenSocket_WinStore8(unsigned short port, unsigned short maxIncomingConnections, unsigned short socketFamily, const char *hostAddress); -#else - bool CreateListenSocket(unsigned short port, unsigned short maxIncomingConnections, unsigned short socketFamily, const char *hostAddress); -#endif - - // Plugins - DataStructures::List messageHandlerList; - - RakNet::LocklessUint32_t isStarted, threadRunning; - __TCPSOCKET__ listenSocket; - - DataStructures::Queue headPush, tailPush; - RemoteClient* remoteClients; - int remoteClientsLength; - - // Assuming remoteClients is only used by one thread! - // DataStructures::List remoteClients; - // Use this thread-safe queue to add to remoteClients - // DataStructures::Queue remoteClientsInsertionQueue; - // SimpleMutex remoteClientsInsertionQueueMutex; - - /* - struct OutgoingMessage - { - unsigned char* data; - SystemAddress systemAddress; - bool broadcast; - unsigned int length; - }; - */ -// DataStructures::SingleProducerConsumer outgoingMessages; -// DataStructures::SingleProducerConsumer incomingMessages; -// DataStructures::SingleProducerConsumer newIncomingConnections, lostConnections, requestedCloseConnections; -// DataStructures::SingleProducerConsumer newRemoteClients; -// DataStructures::ThreadsafeAllocatingQueue outgoingMessages; - DataStructures::ThreadsafeAllocatingQueue incomingMessages; - DataStructures::ThreadsafeAllocatingQueue newIncomingConnections, lostConnections, requestedCloseConnections; - DataStructures::ThreadsafeAllocatingQueue newRemoteClients; - SimpleMutex completedConnectionAttemptMutex, failedConnectionAttemptMutex; - DataStructures::Queue completedConnectionAttempts, failedConnectionAttempts; - - int threadPriority; - - DataStructures::List<__TCPSOCKET__> blockingSocketList; - SimpleMutex blockingSocketListMutex; - - - - - - friend RAK_THREAD_DECLARATION(UpdateTCPInterfaceLoop); - friend RAK_THREAD_DECLARATION(ConnectionAttemptLoop); - -// void DeleteRemoteClient(RemoteClient *remoteClient, fd_set *exceptionFD); -// void InsertRemoteClient(RemoteClient* remoteClient); - __TCPSOCKET__ SocketConnect(const char* host, unsigned short remotePort, unsigned short socketFamily, const char *bindAddress); - - struct ThisPtrPlusSysAddr - { - TCPInterface *tcpInterface; - SystemAddress systemAddress; - bool useSSL; - char bindAddress[64]; - unsigned short socketFamily; - }; - -#if OPEN_SSL_CLIENT_SUPPORT==1 - SSL_CTX* ctx; - SSL_METHOD *meth; - DataStructures::ThreadsafeAllocatingQueue startSSL; - DataStructures::List activeSSLConnections; - SimpleMutex sharedSslMutex; -#endif -}; - -/// Stores information about a remote client. -struct RemoteClient -{ - RemoteClient() { -#if OPEN_SSL_CLIENT_SUPPORT==1 - ssl=0; -#endif - isActive=false; -#if !defined(WINDOWS_STORE_RT) - socket=0; -#endif - } - __TCPSOCKET__ socket; - SystemAddress systemAddress; - DataStructures::ByteQueue outgoingData; - bool isActive; - SimpleMutex outgoingDataMutex; - SimpleMutex isActiveMutex; - -#if OPEN_SSL_CLIENT_SUPPORT==1 - SSL* ssl; - bool InitSSL(SSL_CTX* ctx, SSL_METHOD *meth); - void DisconnectSSL(void); - void FreeSSL(void); - int Send(const char *data, unsigned int length); - int Recv(char *data, const int dataSize); -#else - int Send(const char *data, unsigned int length); - int Recv(char *data, const int dataSize); -#endif - void Reset(void) - { - outgoingDataMutex.Lock(); - outgoingData.Clear(_FILE_AND_LINE_); - outgoingDataMutex.Unlock(); - } - void SetActive(bool a); - void SendOrBuffer(const char **data, const unsigned int *lengths, const int numParameters); -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief A simple TCP based server allowing sends and receives. Can be connected by any TCP client, including telnet. +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_TCPInterface==1 + +#pragma once + +#include "RakMemoryOverride.h" +#include "DS_List.h" +#include "RakNetTypes.h" +#include "Export.h" +#include "RakThread.h" +#include "DS_Queue.h" +#include "SimpleMutex.h" +#include "RakNetDefines.h" +#include "SocketIncludes.h" +#include "DS_ByteQueue.h" +#include "DS_ThreadsafeAllocatingQueue.h" +#include "LocklessTypes.h" +#include "PluginInterface2.h" + +#if OPEN_SSL_CLIENT_SUPPORT==1 +#include +#include +#include +#include +#include +#endif + +namespace RakNet +{ +/// Forward declarations +struct RemoteClient; + +/// \internal +/// \brief As the name says, a simple multithreaded TCP server. Used by TelnetTransport +class RAK_DLL_EXPORT TCPInterface +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(TCPInterface) + + TCPInterface(); + virtual ~TCPInterface(); + + // TODO - add socketdescriptor + /// Starts the TCP server on the indicated port + /// \param[in] port Which port to listen on. + /// \param[in] maxIncomingConnections Max incoming connections we will accept + /// \param[in] maxConnections Max total connections, which should be >= maxIncomingConnections + /// \param[in] threadPriority Passed to the thread creation routine. Use THREAD_PRIORITY_NORMAL for Windows. For Linux based systems, you MUST pass something reasonable based on the thread priorities for your application. + /// \param[in] socketFamily IP version: For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC. + bool Start(unsigned short port, unsigned short maxIncomingConnections, unsigned short maxConnections=0, int _threadPriority=-99999, unsigned short socketFamily=AF_INET, const char *bindAddress=0); + + /// Stops the TCP server + void Stop(void); + + /// Connect to the specified host on the specified port + SystemAddress Connect(const char* host, unsigned short remotePort, bool block=true, unsigned short socketFamily=AF_INET, const char *bindAddress=0); + +#if OPEN_SSL_CLIENT_SUPPORT==1 + /// Start SSL on an existing connection, notified with HasCompletedConnectionAttempt + void StartSSLClient(SystemAddress systemAddress); + + /// Was SSL started on this socket? + bool IsSSLActive(SystemAddress systemAddress); +#endif + + /// Sends a byte stream + virtual void Send( const char *data, unsigned int length, const SystemAddress &systemAddress, bool broadcast ); + + // Sends a concatenated list of byte streams + virtual bool SendList( const char **data, const unsigned int *lengths, const int numParameters, const SystemAddress &systemAddress, bool broadcast ); + + // Get how many bytes are waiting to be sent. If too many, you may want to skip sending + unsigned int GetOutgoingDataBufferSize(SystemAddress systemAddress) const; + + /// Returns if Receive() will return data + /// Do not use on PacketizedTCP + virtual bool ReceiveHasPackets( void ); + + /// Returns data received + virtual Packet* Receive( void ); + + /// Disconnects a player/address + void CloseConnection( SystemAddress systemAddress ); + + /// Deallocates a packet returned by Receive + void DeallocatePacket( Packet *packet ); + + /// Fills the array remoteSystems with the SystemAddress of all the systems we are connected to + /// \param[out] remoteSystems An array of SystemAddress structures to be filled with the SystemAddresss of the systems we are connected to. Pass 0 to remoteSystems to only get the number of systems we are connected to + /// \param[in, out] numberOfSystems As input, the size of remoteSystems array. As output, the number of elements put into the array + void GetConnectionList( SystemAddress *remoteSystems, unsigned short *numberOfSystems ) const; + + /// Returns just the number of connections we have + unsigned short GetConnectionCount(void) const; + + /// Has a previous call to connect succeeded? + /// \return UNASSIGNED_SYSTEM_ADDRESS = no. Anything else means yes. + SystemAddress HasCompletedConnectionAttempt(void); + + /// Has a previous call to connect failed? + /// \return UNASSIGNED_SYSTEM_ADDRESS = no. Anything else means yes. + SystemAddress HasFailedConnectionAttempt(void); + + /// Queued events of new incoming connections + SystemAddress HasNewIncomingConnection(void); + + /// Queued events of lost connections + SystemAddress HasLostConnection(void); + + /// Return an allocated but empty packet, for custom use + Packet* AllocatePacket(unsigned dataSize); + + // Push a packet back to the queue + virtual void PushBackPacket( Packet *packet, bool pushAtHead ); + + /// Returns if Start() was called successfully + bool WasStarted(void) const; + + void AttachPlugin( PluginInterface2 *plugin ); + void DetachPlugin( PluginInterface2 *plugin ); +protected: + + Packet* ReceiveInt( void ); + +#if defined(WINDOWS_STORE_RT) + bool CreateListenSocket_WinStore8(unsigned short port, unsigned short maxIncomingConnections, unsigned short socketFamily, const char *hostAddress); +#else + bool CreateListenSocket(unsigned short port, unsigned short maxIncomingConnections, unsigned short socketFamily, const char *hostAddress); +#endif + + // Plugins + DataStructures::List messageHandlerList; + + RakNet::LocklessUint32_t isStarted, threadRunning; + __TCPSOCKET__ listenSocket; + + DataStructures::Queue headPush, tailPush; + RemoteClient* remoteClients; + int remoteClientsLength; + + // Assuming remoteClients is only used by one thread! + // DataStructures::List remoteClients; + // Use this thread-safe queue to add to remoteClients + // DataStructures::Queue remoteClientsInsertionQueue; + // SimpleMutex remoteClientsInsertionQueueMutex; + + /* + struct OutgoingMessage + { + unsigned char* data; + SystemAddress systemAddress; + bool broadcast; + unsigned int length; + }; + */ +// DataStructures::SingleProducerConsumer outgoingMessages; +// DataStructures::SingleProducerConsumer incomingMessages; +// DataStructures::SingleProducerConsumer newIncomingConnections, lostConnections, requestedCloseConnections; +// DataStructures::SingleProducerConsumer newRemoteClients; +// DataStructures::ThreadsafeAllocatingQueue outgoingMessages; + DataStructures::ThreadsafeAllocatingQueue incomingMessages; + DataStructures::ThreadsafeAllocatingQueue newIncomingConnections, lostConnections, requestedCloseConnections; + DataStructures::ThreadsafeAllocatingQueue newRemoteClients; + SimpleMutex completedConnectionAttemptMutex, failedConnectionAttemptMutex; + DataStructures::Queue completedConnectionAttempts, failedConnectionAttempts; + + int threadPriority; + + DataStructures::List<__TCPSOCKET__> blockingSocketList; + SimpleMutex blockingSocketListMutex; + + + + + + friend RAK_THREAD_DECLARATION(UpdateTCPInterfaceLoop); + friend RAK_THREAD_DECLARATION(ConnectionAttemptLoop); + +// void DeleteRemoteClient(RemoteClient *remoteClient, fd_set *exceptionFD); +// void InsertRemoteClient(RemoteClient* remoteClient); + __TCPSOCKET__ SocketConnect(const char* host, unsigned short remotePort, unsigned short socketFamily, const char *bindAddress); + + struct ThisPtrPlusSysAddr + { + TCPInterface *tcpInterface; + SystemAddress systemAddress; + bool useSSL; + char bindAddress[64]; + unsigned short socketFamily; + }; + +#if OPEN_SSL_CLIENT_SUPPORT==1 + SSL_CTX* ctx; + SSL_METHOD *meth; + DataStructures::ThreadsafeAllocatingQueue startSSL; + DataStructures::List activeSSLConnections; + SimpleMutex sharedSslMutex; +#endif +}; + +/// Stores information about a remote client. +struct RemoteClient +{ + RemoteClient() { +#if OPEN_SSL_CLIENT_SUPPORT==1 + ssl=0; +#endif + isActive=false; +#if !defined(WINDOWS_STORE_RT) + socket=0; +#endif + } + __TCPSOCKET__ socket; + SystemAddress systemAddress; + DataStructures::ByteQueue outgoingData; + bool isActive; + SimpleMutex outgoingDataMutex; + SimpleMutex isActiveMutex; + +#if OPEN_SSL_CLIENT_SUPPORT==1 + SSL* ssl; + bool InitSSL(SSL_CTX* ctx, SSL_METHOD *meth); + void DisconnectSSL(void); + void FreeSSL(void); + int Send(const char *data, unsigned int length); + int Recv(char *data, const int dataSize); +#else + int Send(const char *data, unsigned int length); + int Recv(char *data, const int dataSize); +#endif + void Reset(void) + { + outgoingDataMutex.Lock(); + outgoingData.Clear(_FILE_AND_LINE_); + outgoingDataMutex.Unlock(); + } + void SetActive(bool a); + void SendOrBuffer(const char **data, const unsigned int *lengths, const int numParameters); +}; + +} // namespace RakNet + +#endif + diff --git a/Source/TableSerializer.h b/Source/TableSerializer.h index 1c6d99a9e..3628ce195 100644 --- a/Source/TableSerializer.h +++ b/Source/TableSerializer.h @@ -1,218 +1,216 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __TABLE_SERIALIZER_H -#define __TABLE_SERIALIZER_H - -#include "RakMemoryOverride.h" -#include "DS_Table.h" -#include "Export.h" - -namespace RakNet -{ - class BitStream; -} - -namespace RakNet -{ - -class RAK_DLL_EXPORT TableSerializer -{ -public: - static void SerializeTable(DataStructures::Table *in, RakNet::BitStream *out); - static bool DeserializeTable(unsigned char *serializedTable, unsigned int dataLength, DataStructures::Table *out); - static bool DeserializeTable(RakNet::BitStream *in, DataStructures::Table *out); - static void SerializeColumns(DataStructures::Table *in, RakNet::BitStream *out); - static void SerializeColumns(DataStructures::Table *in, RakNet::BitStream *out, DataStructures::List &skipColumnIndices); - static bool DeserializeColumns(RakNet::BitStream *in, DataStructures::Table *out); - static void SerializeRow(DataStructures::Table::Row *in, unsigned keyIn, const DataStructures::List &columns, RakNet::BitStream *out); - static void SerializeRow(DataStructures::Table::Row *in, unsigned keyIn, const DataStructures::List &columns, RakNet::BitStream *out, DataStructures::List &skipColumnIndices); - static bool DeserializeRow(RakNet::BitStream *in, DataStructures::Table *out); - static void SerializeCell(RakNet::BitStream *out, DataStructures::Table::Cell *cell, DataStructures::Table::ColumnType columnType); - static bool DeserializeCell(RakNet::BitStream *in, DataStructures::Table::Cell *cell, DataStructures::Table::ColumnType columnType); - static void SerializeFilterQuery(RakNet::BitStream *in, DataStructures::Table::FilterQuery *query); - // Note that this allocates query->cell->c! - static bool DeserializeFilterQuery(RakNet::BitStream *out, DataStructures::Table::FilterQuery *query); - static void SerializeFilterQueryList(RakNet::BitStream *in, DataStructures::Table::FilterQuery *query, unsigned int numQueries, unsigned int maxQueries); - // Note that this allocates queries, cells, and query->cell->c!. Use DeallocateQueryList to free. - static bool DeserializeFilterQueryList(RakNet::BitStream *out, DataStructures::Table::FilterQuery **query, unsigned int *numQueries, unsigned int maxQueries, int allocateExtraQueries=0); - static void DeallocateQueryList(DataStructures::Table::FilterQuery *query, unsigned int numQueries); -}; - -} // namespace RakNet - -#endif - -// Test code for the table -/* -#include "LightweightDatabaseServer.h" -#include "LightweightDatabaseClient.h" -#include "TableSerializer.h" -#include "BitStream.h" -#include "StringCompressor.h" -#include "DS_Table.h" -void main(void) -{ - DataStructures::Table table; - DataStructures::Table::Row *row; - unsigned int dummydata=12345; - - // Add columns Name (string), IP (binary), score (int), and players (int). - table.AddColumn("Name", DataStructures::Table::STRING); - table.AddColumn("IP", DataStructures::Table::BINARY); - table.AddColumn("Score", DataStructures::Table::NUMERIC); - table.AddColumn("Players", DataStructures::Table::NUMERIC); - table.AddColumn("Empty Test Column", DataStructures::Table::STRING); - RakAssert(table.GetColumnCount()==5); - row=table.AddRow(0); - RakAssert(row); - row->UpdateCell(0,"Kevin Jenkins"); - row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata); - row->UpdateCell(2,5); - row->UpdateCell(3,10); - //row->UpdateCell(4,"should be unique"); - - row=table.AddRow(1); - row->UpdateCell(0,"Kevin Jenkins"); - row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata); - row->UpdateCell(2,5); - row->UpdateCell(3,15); - - row=table.AddRow(2); - row->UpdateCell(0,"Kevin Jenkins"); - row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata); - row->UpdateCell(2,5); - row->UpdateCell(3,20); - - row=table.AddRow(3); - RakAssert(row); - row->UpdateCell(0,"Kevin Jenkins"); - row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata); - row->UpdateCell(2,15); - row->UpdateCell(3,5); - row->UpdateCell(4,"col index 4"); - - row=table.AddRow(4); - RakAssert(row); - row->UpdateCell(0,"Kevin Jenkins"); - row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata); - //row->UpdateCell(2,25); - row->UpdateCell(3,30); - //row->UpdateCell(4,"should be unique"); - - row=table.AddRow(5); - RakAssert(row); - row->UpdateCell(0,"Kevin Jenkins"); - row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata); - //row->UpdateCell(2,25); - row->UpdateCell(3,5); - //row->UpdateCell(4,"should be unique"); - - row=table.AddRow(6); - RakAssert(row); - row->UpdateCell(0,"Kevin Jenkins"); - row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata); - row->UpdateCell(2,35); - //row->UpdateCell(3,40); - //row->UpdateCell(4,"should be unique"); - - row=table.AddRow(7); - RakAssert(row); - row->UpdateCell(0,"Bob Jenkins"); - - row=table.AddRow(8); - RakAssert(row); - row->UpdateCell(0,"Zack Jenkins"); - - // Test multi-column sorting - DataStructures::Table::Row *rows[30]; - DataStructures::Table::SortQuery queries[4]; - queries[0].columnIndex=0; - queries[0].operation=DataStructures::Table::QS_INCREASING_ORDER; - queries[1].columnIndex=1; - queries[1].operation=DataStructures::Table::QS_INCREASING_ORDER; - queries[2].columnIndex=2; - queries[2].operation=DataStructures::Table::QS_INCREASING_ORDER; - queries[3].columnIndex=3; - queries[3].operation=DataStructures::Table::QS_DECREASING_ORDER; - table.SortTable(queries, 4, rows); - unsigned i; - char out[256]; - RAKNET_DEBUG_PRINTF("Sort: Ascending except for column index 3\n"); - for (i=0; i < table.GetRowCount(); i++) - { - table.PrintRow(out,256,',',true, rows[i]); - RAKNET_DEBUG_PRINTF("%s\n", out); - } - - // Test query: - // Don't return column 3, and swap columns 0 and 2 - unsigned columnsToReturn[4]; - columnsToReturn[0]=2; - columnsToReturn[1]=1; - columnsToReturn[2]=0; - columnsToReturn[3]=4; - DataStructures::Table resultsTable; - table.QueryTable(columnsToReturn,4,0,0,&resultsTable); - RAKNET_DEBUG_PRINTF("Query: Don't return column 3, and swap columns 0 and 2:\n"); - for (i=0; i < resultsTable.GetRowCount(); i++) - { - resultsTable.PrintRow(out,256,',',true, resultsTable.GetRowByIndex(i)); - RAKNET_DEBUG_PRINTF("%s\n", out); - } - - // Test filter: - // Only return rows with column index 4 empty - DataStructures::Table::FilterQuery inclusionFilters[3]; - inclusionFilters[0].columnIndex=4; - inclusionFilters[0].operation=DataStructures::Table::QF_IS_EMPTY; - // inclusionFilters[0].cellValue; // Unused for IS_EMPTY - table.QueryTable(0,0,inclusionFilters,1,&resultsTable); - RAKNET_DEBUG_PRINTF("Filter: Only return rows with column index 4 empty:\n"); - for (i=0; i < resultsTable.GetRowCount(); i++) - { - resultsTable.PrintRow(out,256,',',true, resultsTable.GetRowByIndex(i)); - RAKNET_DEBUG_PRINTF("%s\n", out); - } - - // Column 5 empty and column 0 == Kevin Jenkins - inclusionFilters[0].columnIndex=4; - inclusionFilters[0].operation=DataStructures::Table::QF_IS_EMPTY; - inclusionFilters[1].columnIndex=0; - inclusionFilters[1].operation=DataStructures::Table::QF_EQUAL; - inclusionFilters[1].cellValue.Set("Kevin Jenkins"); - table.QueryTable(0,0,inclusionFilters,2,&resultsTable); - RAKNET_DEBUG_PRINTF("Filter: Column 5 empty and column 0 == Kevin Jenkins:\n"); - for (i=0; i < resultsTable.GetRowCount(); i++) - { - resultsTable.PrintRow(out,256,',',true, resultsTable.GetRowByIndex(i)); - RAKNET_DEBUG_PRINTF("%s\n", out); - } - - RakNet::BitStream bs; - RAKNET_DEBUG_PRINTF("PreSerialize:\n"); - for (i=0; i < table.GetRowCount(); i++) - { - table.PrintRow(out,256,',',true, table.GetRowByIndex(i)); - RAKNET_DEBUG_PRINTF("%s\n", out); - } - StringCompressor::AddReference(); - TableSerializer::Serialize(&table, &bs); - TableSerializer::Deserialize(&bs, &table); - StringCompressor::RemoveReference(); - RAKNET_DEBUG_PRINTF("PostDeserialize:\n"); - for (i=0; i < table.GetRowCount(); i++) - { - table.PrintRow(out,256,',',true, table.GetRowByIndex(i)); - RAKNET_DEBUG_PRINTF("%s\n", out); - } - int a=5; -} -*/ +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include "RakMemoryOverride.h" +#include "DS_Table.h" +#include "Export.h" + +namespace RakNet +{ + class BitStream; +} + +namespace RakNet +{ + +class RAK_DLL_EXPORT TableSerializer +{ +public: + static void SerializeTable(DataStructures::Table *in, RakNet::BitStream *out); + static bool DeserializeTable(unsigned char *serializedTable, unsigned int dataLength, DataStructures::Table *out); + static bool DeserializeTable(RakNet::BitStream *in, DataStructures::Table *out); + static void SerializeColumns(DataStructures::Table *in, RakNet::BitStream *out); + static void SerializeColumns(DataStructures::Table *in, RakNet::BitStream *out, DataStructures::List &skipColumnIndices); + static bool DeserializeColumns(RakNet::BitStream *in, DataStructures::Table *out); + static void SerializeRow(DataStructures::Table::Row *in, unsigned keyIn, const DataStructures::List &columns, RakNet::BitStream *out); + static void SerializeRow(DataStructures::Table::Row *in, unsigned keyIn, const DataStructures::List &columns, RakNet::BitStream *out, DataStructures::List &skipColumnIndices); + static bool DeserializeRow(RakNet::BitStream *in, DataStructures::Table *out); + static void SerializeCell(RakNet::BitStream *out, DataStructures::Table::Cell *cell, DataStructures::Table::ColumnType columnType); + static bool DeserializeCell(RakNet::BitStream *in, DataStructures::Table::Cell *cell, DataStructures::Table::ColumnType columnType); + static void SerializeFilterQuery(RakNet::BitStream *in, DataStructures::Table::FilterQuery *query); + // Note that this allocates query->cell->c! + static bool DeserializeFilterQuery(RakNet::BitStream *out, DataStructures::Table::FilterQuery *query); + static void SerializeFilterQueryList(RakNet::BitStream *in, DataStructures::Table::FilterQuery *query, unsigned int numQueries, unsigned int maxQueries); + // Note that this allocates queries, cells, and query->cell->c!. Use DeallocateQueryList to free. + static bool DeserializeFilterQueryList(RakNet::BitStream *out, DataStructures::Table::FilterQuery **query, unsigned int *numQueries, unsigned int maxQueries, int allocateExtraQueries=0); + static void DeallocateQueryList(DataStructures::Table::FilterQuery *query, unsigned int numQueries); +}; + +} // namespace RakNet + + +// Test code for the table +/* +#include "LightweightDatabaseServer.h" +#include "LightweightDatabaseClient.h" +#include "TableSerializer.h" +#include "BitStream.h" +#include "StringCompressor.h" +#include "DS_Table.h" +void main(void) +{ + DataStructures::Table table; + DataStructures::Table::Row *row; + unsigned int dummydata=12345; + + // Add columns Name (string), IP (binary), score (int), and players (int). + table.AddColumn("Name", DataStructures::Table::STRING); + table.AddColumn("IP", DataStructures::Table::BINARY); + table.AddColumn("Score", DataStructures::Table::NUMERIC); + table.AddColumn("Players", DataStructures::Table::NUMERIC); + table.AddColumn("Empty Test Column", DataStructures::Table::STRING); + RakAssert(table.GetColumnCount()==5); + row=table.AddRow(0); + RakAssert(row); + row->UpdateCell(0,"Kevin Jenkins"); + row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata); + row->UpdateCell(2,5); + row->UpdateCell(3,10); + //row->UpdateCell(4,"should be unique"); + + row=table.AddRow(1); + row->UpdateCell(0,"Kevin Jenkins"); + row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata); + row->UpdateCell(2,5); + row->UpdateCell(3,15); + + row=table.AddRow(2); + row->UpdateCell(0,"Kevin Jenkins"); + row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata); + row->UpdateCell(2,5); + row->UpdateCell(3,20); + + row=table.AddRow(3); + RakAssert(row); + row->UpdateCell(0,"Kevin Jenkins"); + row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata); + row->UpdateCell(2,15); + row->UpdateCell(3,5); + row->UpdateCell(4,"col index 4"); + + row=table.AddRow(4); + RakAssert(row); + row->UpdateCell(0,"Kevin Jenkins"); + row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata); + //row->UpdateCell(2,25); + row->UpdateCell(3,30); + //row->UpdateCell(4,"should be unique"); + + row=table.AddRow(5); + RakAssert(row); + row->UpdateCell(0,"Kevin Jenkins"); + row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata); + //row->UpdateCell(2,25); + row->UpdateCell(3,5); + //row->UpdateCell(4,"should be unique"); + + row=table.AddRow(6); + RakAssert(row); + row->UpdateCell(0,"Kevin Jenkins"); + row->UpdateCell(1,sizeof(dummydata), (char*)&dummydata); + row->UpdateCell(2,35); + //row->UpdateCell(3,40); + //row->UpdateCell(4,"should be unique"); + + row=table.AddRow(7); + RakAssert(row); + row->UpdateCell(0,"Bob Jenkins"); + + row=table.AddRow(8); + RakAssert(row); + row->UpdateCell(0,"Zack Jenkins"); + + // Test multi-column sorting + DataStructures::Table::Row *rows[30]; + DataStructures::Table::SortQuery queries[4]; + queries[0].columnIndex=0; + queries[0].operation=DataStructures::Table::QS_INCREASING_ORDER; + queries[1].columnIndex=1; + queries[1].operation=DataStructures::Table::QS_INCREASING_ORDER; + queries[2].columnIndex=2; + queries[2].operation=DataStructures::Table::QS_INCREASING_ORDER; + queries[3].columnIndex=3; + queries[3].operation=DataStructures::Table::QS_DECREASING_ORDER; + table.SortTable(queries, 4, rows); + unsigned i; + char out[256]; + RAKNET_DEBUG_PRINTF("Sort: Ascending except for column index 3\n"); + for (i=0; i < table.GetRowCount(); i++) + { + table.PrintRow(out,256,',',true, rows[i]); + RAKNET_DEBUG_PRINTF("%s\n", out); + } + + // Test query: + // Don't return column 3, and swap columns 0 and 2 + unsigned columnsToReturn[4]; + columnsToReturn[0]=2; + columnsToReturn[1]=1; + columnsToReturn[2]=0; + columnsToReturn[3]=4; + DataStructures::Table resultsTable; + table.QueryTable(columnsToReturn,4,0,0,&resultsTable); + RAKNET_DEBUG_PRINTF("Query: Don't return column 3, and swap columns 0 and 2:\n"); + for (i=0; i < resultsTable.GetRowCount(); i++) + { + resultsTable.PrintRow(out,256,',',true, resultsTable.GetRowByIndex(i)); + RAKNET_DEBUG_PRINTF("%s\n", out); + } + + // Test filter: + // Only return rows with column index 4 empty + DataStructures::Table::FilterQuery inclusionFilters[3]; + inclusionFilters[0].columnIndex=4; + inclusionFilters[0].operation=DataStructures::Table::QF_IS_EMPTY; + // inclusionFilters[0].cellValue; // Unused for IS_EMPTY + table.QueryTable(0,0,inclusionFilters,1,&resultsTable); + RAKNET_DEBUG_PRINTF("Filter: Only return rows with column index 4 empty:\n"); + for (i=0; i < resultsTable.GetRowCount(); i++) + { + resultsTable.PrintRow(out,256,',',true, resultsTable.GetRowByIndex(i)); + RAKNET_DEBUG_PRINTF("%s\n", out); + } + + // Column 5 empty and column 0 == Kevin Jenkins + inclusionFilters[0].columnIndex=4; + inclusionFilters[0].operation=DataStructures::Table::QF_IS_EMPTY; + inclusionFilters[1].columnIndex=0; + inclusionFilters[1].operation=DataStructures::Table::QF_EQUAL; + inclusionFilters[1].cellValue.Set("Kevin Jenkins"); + table.QueryTable(0,0,inclusionFilters,2,&resultsTable); + RAKNET_DEBUG_PRINTF("Filter: Column 5 empty and column 0 == Kevin Jenkins:\n"); + for (i=0; i < resultsTable.GetRowCount(); i++) + { + resultsTable.PrintRow(out,256,',',true, resultsTable.GetRowByIndex(i)); + RAKNET_DEBUG_PRINTF("%s\n", out); + } + + RakNet::BitStream bs; + RAKNET_DEBUG_PRINTF("PreSerialize:\n"); + for (i=0; i < table.GetRowCount(); i++) + { + table.PrintRow(out,256,',',true, table.GetRowByIndex(i)); + RAKNET_DEBUG_PRINTF("%s\n", out); + } + StringCompressor::AddReference(); + TableSerializer::Serialize(&table, &bs); + TableSerializer::Deserialize(&bs, &table); + StringCompressor::RemoveReference(); + RAKNET_DEBUG_PRINTF("PostDeserialize:\n"); + for (i=0; i < table.GetRowCount(); i++) + { + table.PrintRow(out,256,',',true, table.GetRowByIndex(i)); + RAKNET_DEBUG_PRINTF("%s\n", out); + } + int a=5; +} +*/ diff --git a/Source/TeamBalancer.h b/Source/TeamBalancer.h index 49591e00f..289e7172e 100644 --- a/Source/TeamBalancer.h +++ b/Source/TeamBalancer.h @@ -1,206 +1,204 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file TeamBalancer.h -/// \brief Set and network team selection (supports peer to peer or client/server) -/// \details Automatically handles transmission and resolution of team selection, including team switching and balancing -/// \deprecated Use TeamManager intead -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_TeamBalancer==1 - -#ifndef __TEAM_BALANCER_H -#define __TEAM_BALANCER_H - -#include "PluginInterface2.h" -#include "RakMemoryOverride.h" -#include "NativeTypes.h" -#include "DS_List.h" -#include "RakString.h" - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; - -/// \defgroup TEAM_BALANCER_GROUP TeamBalancer -/// \brief Set and network team selection (supports peer to peer or client/server) -/// \details Automatically handles transmission and resolution of team selection, including team switching and balancing -/// \deprecated Use TeamManager intead -/// \ingroup PLUGINS_GROUP - -/// 0...254 for your team number identifiers. 255 is reserved as undefined. -/// \deprecated Use TeamManager intead -/// \ingroup TEAM_BALANCER_GROUP -typedef unsigned char TeamId; - -#define UNASSIGNED_TEAM_ID 255 - -/// \brief Set and network team selection (supports peer to peer or client/server) -/// \details Automatically handles transmission and resolution of team selection, including team switching and balancing.
      -/// Usage: TODO -/// \deprecated Use TeamManager intead -/// \ingroup TEAM_BALANCER_GROUP -class RAK_DLL_EXPORT TeamBalancer : public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(TeamBalancer) - - TeamBalancer(); - virtual ~TeamBalancer(); - - /// \brief Set the limit to the number of players on the specified team - /// \details SetTeamSizeLimit() must be called on the host, so the host can enforce the maximum number of players on each team. - /// SetTeamSizeLimit() can be called on all systems if desired - for example, in a P2P environment you may wish to call it on all systems in advanced in case you become host. - /// \param[in] team Which team to set the limit for - /// \param[in] limit The maximum number of people on this team - void SetTeamSizeLimit(TeamId team, unsigned short limit); - - enum DefaultAssigmentAlgorithm - { - /// Among all the teams, join the team with the smallest number of players - SMALLEST_TEAM, - /// Join the team with the lowest index that has open slots. - FILL_IN_ORDER - }; - /// \brief Determine how players' teams will be set when they call RequestAnyTeam() - /// \details Based on the specified enumeration, a player will join a team automatically - /// Defaults to SMALLEST_TEAM - /// This function is only used by the host - /// \param[in] daa Enumeration describing the algorithm to use - void SetDefaultAssignmentAlgorithm(DefaultAssigmentAlgorithm daa); - - /// \brief By default, teams can be unbalanced up to the team size limit defined by SetTeamSizeLimits() - /// \details If SetForceEvenTeams(true) is called on the host, then teams cannot be unbalanced by more than 1 player - /// If teams are uneven at the time that SetForceEvenTeams(true) is called, players at randomly will be switched, and will be notified of ID_TEAM_BALANCER_TEAM_ASSIGNED - /// If players disconnect from the host such that teams would not be even, and teams are not locked, then a player from the largest team is randomly moved to even the teams. - /// Defaults to false - /// \note SetLockTeams(true) takes priority over SetForceEvenTeams(), so if teams are currently locked, this function will have no effect until teams become unlocked. - /// \param[in] force True to force even teams. False to allow teams to not be evenly matched - void SetForceEvenTeams(bool force); - - /// \brief If set, calls to RequestSpecificTeam() and RequestAnyTeam() will return the team you are currently on. - /// \details However, if those functions are called and you do not have a team, then you will be assigned to a default team according to SetDefaultAssignmentAlgorithm() and possibly SetForceEvenTeams(true) - /// If \a lock is false, and SetForceEvenTeams() was called with \a force as true, and teams are currently uneven, they will be made even, and those players randomly moved will get ID_TEAM_BALANCER_TEAM_ASSIGNED - /// Defaults to false - /// \param[in] lock True to lock teams, false to unlock - void SetLockTeams(bool lock); - - /// Set your requested team. UNASSIGNED_TEAM_ID means no team. - /// After enough time for network communication, ID_TEAM_BALANCER_SET_TEAM will be returned with your current team, or - /// If team switch is not possible, ID_TEAM_BALANCER_REQUESTED_TEAM_CHANGE_PENDING or ID_TEAM_BALANCER_TEAMS_LOCKED will be returned. - /// In the case of ID_TEAM_BALANCER_REQUESTED_TEAM_CHANGE_PENDING the request will stay in memory. ID_TEAM_BALANCER_SET_TEAM will be returned when someone on the desired team leaves or wants to switch to your team. - /// If SetLockTeams(true) is called while you have a request pending, you will get ID_TEAM_BALANCER_TEAMS_LOCKED - /// \pre Call SetTeamSizeLimits() on the host and call SetHostGuid() on this system. If the host is not running the TeamBalancer plugin or did not have SetTeamSizeLimits() called, then you will not get any response. - /// \param[in] memberId If there is more than one player per computer, this number identifies that player. Use any consistent value, such as UNASSIGNED_NETWORK_ID if there is only one player. - /// \param[in] desiredTeam An index representing your team number. The index should range from 0 to one less than the size of the list passed to SetTeamSizeLimits() on the host. You can also pass UNASSIGNED_TEAM_ID to not be on any team (such as if spectating) - void RequestSpecificTeam(NetworkID memberId, TeamId desiredTeam); - - /// If ID_TEAM_BALANCER_REQUESTED_TEAM_CHANGE_PENDING is returned after a call to RequestSpecificTeam(), the request will stay in memory on the host and execute when available, or until the teams become locked. - /// You can cancel the request by calling CancelRequestSpecificTeam(), in which case you will stay on your existing team. - /// \note Due to latency, even after calling CancelRequestSpecificTeam() you may still get ID_TEAM_BALANCER_SET_TEAM if the packet was already in transmission. - /// \param[in] memberId If there is more than one player per computer, this number identifies that player. Use any consistent value, such as UNASSIGNED_NETWORK_ID if there is only one player. - void CancelRequestSpecificTeam(NetworkID memberId); - - /// Allow host to pick your team, based on whatever algorithm it uses for default team assignments. - /// This only has an effect if you are not currently on a team (GetMyTeam() returns UNASSIGNED_TEAM_ID) - /// \pre Call SetTeamSizeLimits() on the host and call SetHostGuid() on this system - /// \param[in] memberId If there is more than one player per computer, this number identifies that player. Use any consistent value, such as UNASSIGNED_NETWORK_ID if there is only one player. - void RequestAnyTeam(NetworkID memberId); - - /// Returns your team. - /// As your team changes, you are notified through the ID_TEAM_BALANCER_TEAM_ASSIGNED packet in byte 1. - /// Returns UNASSIGNED_TEAM_ID initially - /// \pre For this to return anything other than UNASSIGNED_TEAM_ID, connect to a properly initialized host and RequestSpecificTeam() or RequestAnyTeam() first - /// \param[in] memberId If there is more than one player per computer, this number identifies that player. Use any consistent value, such as UNASSIGNED_NETWORK_ID if there is only one player. - /// \return UNASSIGNED_TEAM_ID for no team. Otherwise, the index should range from 0 to one less than the size of the list passed to SetTeamSizeLimits() on the host - TeamId GetMyTeam(NetworkID memberId) const; - - /// If you called RequestSpecificTeam() or RequestAnyTeam() with a value for \a memberId that - /// Has since been deleted, call DeleteMember(). to notify this plugin of that event. - /// Not necessary with only one team member per system - /// \param[in] memberId If there is more than one player per computer, this number identifies that player. Use any consistent value, such as UNASSIGNED_NETWORK_ID if there is only one player. - void DeleteMember(NetworkID memberId); - - struct TeamMember - { - RakNetGUID memberGuid; - NetworkID memberId; - TeamId currentTeam; - TeamId requestedTeam; - }; - struct MyTeamMembers - { - NetworkID memberId; - TeamId currentTeam; - TeamId requestedTeam; - }; - -protected: - - /// \internal - virtual PluginReceiveResult OnReceive(Packet *packet); - /// \internal - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - /// \internal - void OnAttach(void); - - void OnStatusUpdateToNewHost(Packet *packet); - void OnCancelTeamRequest(Packet *packet); - void OnRequestAnyTeam(Packet *packet); - void OnRequestSpecificTeam(Packet *packet); - - RakNetGUID hostGuid; - DefaultAssigmentAlgorithm defaultAssigmentAlgorithm; - bool forceTeamsToBeEven; - bool lockTeams; - // So if we lose the connection while processing, we request the same info of the new host - DataStructures::List myTeamMembers; - - DataStructures::List teamLimits; - DataStructures::List teamMemberCounts; - DataStructures::List teamMembers; - unsigned int GetMemberIndex(NetworkID memberId, RakNetGUID guid) const; - unsigned int AddTeamMember(const TeamMember &tm); // Returns index of new member - void RemoveTeamMember(unsigned int index); - void EvenTeams(void); - unsigned int GetMemberIndexToSwitchTeams(const DataStructures::List &sourceTeamNumbers, TeamId targetTeamNumber); - void GetOverpopulatedTeams(DataStructures::List &overpopulatedTeams, int maxTeamSize); - void SwitchMemberTeam(unsigned int teamMemberIndex, TeamId destinationTeam); - void NotifyTeamAssigment(unsigned int teamMemberIndex); - bool WeAreHost(void) const; - PluginReceiveResult OnTeamAssigned(Packet *packet); - PluginReceiveResult OnRequestedTeamChangePending(Packet *packet); - PluginReceiveResult OnTeamsLocked(Packet *packet); - void GetMinMaxTeamMembers(int &minMembersOnASingleTeam, int &maxMembersOnASingleTeam); - TeamId GetNextDefaultTeam(void); // Accounting for team balancing and team limits, get the team a player should be placed on - bool TeamWouldBeOverpopulatedOnAddition(TeamId teamId, unsigned int teamMemberSize); // Accounting for team balancing and team limits, would this team be overpopulated if a member was added to it? - bool TeamWouldBeUnderpopulatedOnLeave(TeamId teamId, unsigned int teamMemberSize); - TeamId GetSmallestNonFullTeam(void) const; - TeamId GetFirstNonFullTeam(void) const; - void MoveMemberThatWantsToJoinTeam(TeamId teamId); - TeamId MoveMemberThatWantsToJoinTeamInternal(TeamId teamId); - void NotifyTeamsLocked(RakNetGUID target, TeamId requestedTeam); - void NotifyTeamSwitchPending(RakNetGUID target, TeamId requestedTeam, NetworkID memberId); - void NotifyNoTeam(NetworkID memberId, RakNetGUID target); - void SwapTeamMembersByRequest(unsigned int memberIndex1, unsigned int memberIndex2); - void RemoveByGuid(RakNetGUID rakNetGUID); - bool TeamsWouldBeEvenOnSwitch(TeamId t1, TeamId t2); - -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file TeamBalancer.h +/// \brief Set and network team selection (supports peer to peer or client/server) +/// \details Automatically handles transmission and resolution of team selection, including team switching and balancing +/// \deprecated Use TeamManager intead +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_TeamBalancer==1 + +#pragma once + +#include "PluginInterface2.h" +#include "RakMemoryOverride.h" +#include "NativeTypes.h" +#include "DS_List.h" +#include "RakString.h" + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; + +/// \defgroup TEAM_BALANCER_GROUP TeamBalancer +/// \brief Set and network team selection (supports peer to peer or client/server) +/// \details Automatically handles transmission and resolution of team selection, including team switching and balancing +/// \deprecated Use TeamManager intead +/// \ingroup PLUGINS_GROUP + +/// 0...254 for your team number identifiers. 255 is reserved as undefined. +/// \deprecated Use TeamManager intead +/// \ingroup TEAM_BALANCER_GROUP +typedef unsigned char TeamId; + +#define UNASSIGNED_TEAM_ID 255 + +/// \brief Set and network team selection (supports peer to peer or client/server) +/// \details Automatically handles transmission and resolution of team selection, including team switching and balancing.
      +/// Usage: TODO +/// \deprecated Use TeamManager intead +/// \ingroup TEAM_BALANCER_GROUP +class RAK_DLL_EXPORT TeamBalancer : public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(TeamBalancer) + + TeamBalancer(); + virtual ~TeamBalancer(); + + /// \brief Set the limit to the number of players on the specified team + /// \details SetTeamSizeLimit() must be called on the host, so the host can enforce the maximum number of players on each team. + /// SetTeamSizeLimit() can be called on all systems if desired - for example, in a P2P environment you may wish to call it on all systems in advanced in case you become host. + /// \param[in] team Which team to set the limit for + /// \param[in] limit The maximum number of people on this team + void SetTeamSizeLimit(TeamId team, unsigned short limit); + + enum DefaultAssigmentAlgorithm + { + /// Among all the teams, join the team with the smallest number of players + SMALLEST_TEAM, + /// Join the team with the lowest index that has open slots. + FILL_IN_ORDER + }; + /// \brief Determine how players' teams will be set when they call RequestAnyTeam() + /// \details Based on the specified enumeration, a player will join a team automatically + /// Defaults to SMALLEST_TEAM + /// This function is only used by the host + /// \param[in] daa Enumeration describing the algorithm to use + void SetDefaultAssignmentAlgorithm(DefaultAssigmentAlgorithm daa); + + /// \brief By default, teams can be unbalanced up to the team size limit defined by SetTeamSizeLimits() + /// \details If SetForceEvenTeams(true) is called on the host, then teams cannot be unbalanced by more than 1 player + /// If teams are uneven at the time that SetForceEvenTeams(true) is called, players at randomly will be switched, and will be notified of ID_TEAM_BALANCER_TEAM_ASSIGNED + /// If players disconnect from the host such that teams would not be even, and teams are not locked, then a player from the largest team is randomly moved to even the teams. + /// Defaults to false + /// \note SetLockTeams(true) takes priority over SetForceEvenTeams(), so if teams are currently locked, this function will have no effect until teams become unlocked. + /// \param[in] force True to force even teams. False to allow teams to not be evenly matched + void SetForceEvenTeams(bool force); + + /// \brief If set, calls to RequestSpecificTeam() and RequestAnyTeam() will return the team you are currently on. + /// \details However, if those functions are called and you do not have a team, then you will be assigned to a default team according to SetDefaultAssignmentAlgorithm() and possibly SetForceEvenTeams(true) + /// If \a lock is false, and SetForceEvenTeams() was called with \a force as true, and teams are currently uneven, they will be made even, and those players randomly moved will get ID_TEAM_BALANCER_TEAM_ASSIGNED + /// Defaults to false + /// \param[in] lock True to lock teams, false to unlock + void SetLockTeams(bool lock); + + /// Set your requested team. UNASSIGNED_TEAM_ID means no team. + /// After enough time for network communication, ID_TEAM_BALANCER_SET_TEAM will be returned with your current team, or + /// If team switch is not possible, ID_TEAM_BALANCER_REQUESTED_TEAM_CHANGE_PENDING or ID_TEAM_BALANCER_TEAMS_LOCKED will be returned. + /// In the case of ID_TEAM_BALANCER_REQUESTED_TEAM_CHANGE_PENDING the request will stay in memory. ID_TEAM_BALANCER_SET_TEAM will be returned when someone on the desired team leaves or wants to switch to your team. + /// If SetLockTeams(true) is called while you have a request pending, you will get ID_TEAM_BALANCER_TEAMS_LOCKED + /// \pre Call SetTeamSizeLimits() on the host and call SetHostGuid() on this system. If the host is not running the TeamBalancer plugin or did not have SetTeamSizeLimits() called, then you will not get any response. + /// \param[in] memberId If there is more than one player per computer, this number identifies that player. Use any consistent value, such as UNASSIGNED_NETWORK_ID if there is only one player. + /// \param[in] desiredTeam An index representing your team number. The index should range from 0 to one less than the size of the list passed to SetTeamSizeLimits() on the host. You can also pass UNASSIGNED_TEAM_ID to not be on any team (such as if spectating) + void RequestSpecificTeam(NetworkID memberId, TeamId desiredTeam); + + /// If ID_TEAM_BALANCER_REQUESTED_TEAM_CHANGE_PENDING is returned after a call to RequestSpecificTeam(), the request will stay in memory on the host and execute when available, or until the teams become locked. + /// You can cancel the request by calling CancelRequestSpecificTeam(), in which case you will stay on your existing team. + /// \note Due to latency, even after calling CancelRequestSpecificTeam() you may still get ID_TEAM_BALANCER_SET_TEAM if the packet was already in transmission. + /// \param[in] memberId If there is more than one player per computer, this number identifies that player. Use any consistent value, such as UNASSIGNED_NETWORK_ID if there is only one player. + void CancelRequestSpecificTeam(NetworkID memberId); + + /// Allow host to pick your team, based on whatever algorithm it uses for default team assignments. + /// This only has an effect if you are not currently on a team (GetMyTeam() returns UNASSIGNED_TEAM_ID) + /// \pre Call SetTeamSizeLimits() on the host and call SetHostGuid() on this system + /// \param[in] memberId If there is more than one player per computer, this number identifies that player. Use any consistent value, such as UNASSIGNED_NETWORK_ID if there is only one player. + void RequestAnyTeam(NetworkID memberId); + + /// Returns your team. + /// As your team changes, you are notified through the ID_TEAM_BALANCER_TEAM_ASSIGNED packet in byte 1. + /// Returns UNASSIGNED_TEAM_ID initially + /// \pre For this to return anything other than UNASSIGNED_TEAM_ID, connect to a properly initialized host and RequestSpecificTeam() or RequestAnyTeam() first + /// \param[in] memberId If there is more than one player per computer, this number identifies that player. Use any consistent value, such as UNASSIGNED_NETWORK_ID if there is only one player. + /// \return UNASSIGNED_TEAM_ID for no team. Otherwise, the index should range from 0 to one less than the size of the list passed to SetTeamSizeLimits() on the host + TeamId GetMyTeam(NetworkID memberId) const; + + /// If you called RequestSpecificTeam() or RequestAnyTeam() with a value for \a memberId that + /// Has since been deleted, call DeleteMember(). to notify this plugin of that event. + /// Not necessary with only one team member per system + /// \param[in] memberId If there is more than one player per computer, this number identifies that player. Use any consistent value, such as UNASSIGNED_NETWORK_ID if there is only one player. + void DeleteMember(NetworkID memberId); + + struct TeamMember + { + RakNetGUID memberGuid; + NetworkID memberId; + TeamId currentTeam; + TeamId requestedTeam; + }; + struct MyTeamMembers + { + NetworkID memberId; + TeamId currentTeam; + TeamId requestedTeam; + }; + +protected: + + /// \internal + virtual PluginReceiveResult OnReceive(Packet *packet); + /// \internal + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + /// \internal + void OnAttach(void); + + void OnStatusUpdateToNewHost(Packet *packet); + void OnCancelTeamRequest(Packet *packet); + void OnRequestAnyTeam(Packet *packet); + void OnRequestSpecificTeam(Packet *packet); + + RakNetGUID hostGuid; + DefaultAssigmentAlgorithm defaultAssigmentAlgorithm; + bool forceTeamsToBeEven; + bool lockTeams; + // So if we lose the connection while processing, we request the same info of the new host + DataStructures::List myTeamMembers; + + DataStructures::List teamLimits; + DataStructures::List teamMemberCounts; + DataStructures::List teamMembers; + unsigned int GetMemberIndex(NetworkID memberId, RakNetGUID guid) const; + unsigned int AddTeamMember(const TeamMember &tm); // Returns index of new member + void RemoveTeamMember(unsigned int index); + void EvenTeams(void); + unsigned int GetMemberIndexToSwitchTeams(const DataStructures::List &sourceTeamNumbers, TeamId targetTeamNumber); + void GetOverpopulatedTeams(DataStructures::List &overpopulatedTeams, int maxTeamSize); + void SwitchMemberTeam(unsigned int teamMemberIndex, TeamId destinationTeam); + void NotifyTeamAssigment(unsigned int teamMemberIndex); + bool WeAreHost(void) const; + PluginReceiveResult OnTeamAssigned(Packet *packet); + PluginReceiveResult OnRequestedTeamChangePending(Packet *packet); + PluginReceiveResult OnTeamsLocked(Packet *packet); + void GetMinMaxTeamMembers(int &minMembersOnASingleTeam, int &maxMembersOnASingleTeam); + TeamId GetNextDefaultTeam(void); // Accounting for team balancing and team limits, get the team a player should be placed on + bool TeamWouldBeOverpopulatedOnAddition(TeamId teamId, unsigned int teamMemberSize); // Accounting for team balancing and team limits, would this team be overpopulated if a member was added to it? + bool TeamWouldBeUnderpopulatedOnLeave(TeamId teamId, unsigned int teamMemberSize); + TeamId GetSmallestNonFullTeam(void) const; + TeamId GetFirstNonFullTeam(void) const; + void MoveMemberThatWantsToJoinTeam(TeamId teamId); + TeamId MoveMemberThatWantsToJoinTeamInternal(TeamId teamId); + void NotifyTeamsLocked(RakNetGUID target, TeamId requestedTeam); + void NotifyTeamSwitchPending(RakNetGUID target, TeamId requestedTeam, NetworkID memberId); + void NotifyNoTeam(NetworkID memberId, RakNetGUID target); + void SwapTeamMembersByRequest(unsigned int memberIndex1, unsigned int memberIndex2); + void RemoveByGuid(RakNetGUID rakNetGUID); + bool TeamsWouldBeEvenOnSwitch(TeamId t1, TeamId t2); + +}; + +} // namespace RakNet + +#endif + diff --git a/Source/TeamManager.h b/Source/TeamManager.h index 3df091810..e0349f21a 100644 --- a/Source/TeamManager.h +++ b/Source/TeamManager.h @@ -1,757 +1,754 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -// TODO: optimize the list of teams and team members to be O(1). Store in hashes, use linked lists to get ordered traversal - -/// \file TeamManager.h -/// \brief Automates networking and list management for teams -/// \details TeamManager provides support for teams. A team is a list of team members. -/// Teams contain properties including the number of team members per team, whether or not tagged teams must have equal numbers of members, and if a team is locked or not to certain entry conditions -/// Team members contain properties including which teams they are on and which teams they want to join if a team is not immediately joinable -/// Advanced functionality includes the ability for a team member to be on multiple teams simultaneously, the ability to swap teams with other members, and the ability to resize the number of members supported per team -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_TeamManager==1 - -#ifndef __TEAM_MANAGER_H -#define __TEAM_MANAGER_H - -#include "PluginInterface2.h" -#include "RakMemoryOverride.h" -#include "NativeTypes.h" -#include "DS_List.h" -#include "RakNetTypes.h" -#include "DS_Hash.h" -#include "DS_OrderedList.h" - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; - -/// \defgroup TEAM_MANAGER_GROUP TeamManager -/// \brief Automates networking and list management for teams -/// \details When used with ReplicaManager3 and FullyConnectedMesh2, provides a complete solution to managing a distributed list of teams and team member objects with support for host migration. -/// \ingroup PLUGINS_GROUP - -/// \ingroup TEAM_MANAGER_GROUP -/// \brief A subcategory of not being on a team. For example, 0 may mean no team for a player, while 1 may mean no team for a spectator. Defined by the user. -typedef unsigned char NoTeamId; - -/// \ingroup TEAM_MANAGER_GROUP -/// Used for multiple worlds. -typedef uint8_t WorldId; - -/// \ingroup TEAM_MANAGER_GROUP -/// Maximum number of members on one team. Use 65535 for unlimited. -typedef uint16_t TeamMemberLimit; - -/// Allow members to join this team when they specify TeamSelection::JOIN_ANY_AVAILABLE_TEAM -#define ALLOW_JOIN_ANY_AVAILABLE_TEAM (1<<0) -/// Allow members to join this team when they specify TeamSelection::JOIN_SPECIFIC_TEAM -#define ALLOW_JOIN_SPECIFIC_TEAM (1<<1) -/// Allow the host to put members on this team when rebalancing with TM_World::SetBalanceTeams() -#define ALLOW_JOIN_REBALANCING (1<<2) - -// Bitwise combination of ALLOW_JOIN_ANY_AVAILABLE_TEAM, ALLOW_JOIN_SPECIFIC_TEAM, ALLOW_JOIN_REBALANCING -typedef uint8_t JoinPermissions; - -// Forward declarations -class TM_Team; -class TM_TeamMember; -class TM_World; -class TeamManager; - -/// \ingroup TEAM_MANAGER_GROUP -enum JoinTeamType -{ - /// Attempt to join the first available team. - JOIN_ANY_AVAILABLE_TEAM, - /// Attempt to join a specific team, previously added with TM_World::ReferenceTeam() - JOIN_SPECIFIC_TEAM, - /// No team. Always succeeds. - JOIN_NO_TEAM -}; - -/// \ingroup TEAM_MANAGER_GROUP -enum TMTopology -{ - // Each system will send all messages to all participants - TM_PEER_TO_PEER, - - // The host will relay incoming messages to all participants - TM_CLIENT_SERVER, -}; - -/// \brief Parameter to TM_World::ReferenceTeamMember() -/// \details Use TeamSelection::AnyAvailable(), TeamSelection::SpecificTeam(), or TeamSelection::NoTeam() -/// \ingroup TEAM_MANAGER_GROUP -struct TeamSelection -{ - TeamSelection(); - TeamSelection(JoinTeamType itt); - TeamSelection(JoinTeamType itt, TM_Team *param); - TeamSelection(JoinTeamType itt, NoTeamId param); - JoinTeamType joinTeamType; - - union - { - TM_Team *specificTeamToJoin; - NoTeamId noTeamSubcategory; - } teamParameter; - - /// \brief Join any team that has available slots and is tagged with ALLOW_JOIN_ANY_AVAILABLE_TEAM - /// \details ID_TEAM_BALANCER_TEAM_ASSIGNED, ID_TEAM_BALANCER_REQUESTED_TEAM_FULL, or ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED will be returned to all systems. - static TeamSelection AnyAvailable(void); - /// \brief Join a specific team if it has available slots, and is tagged with JOIN_SPECIFIC_TEAMS - /// \details ID_TEAM_BALANCER_TEAM_ASSIGNED, ID_TEAM_BALANCER_REQUESTED_TEAM_FULL, or ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED will be returned to all systems. - /// \param[in] specificTeamToJoin Which team to attempt to join. - static TeamSelection SpecificTeam(TM_Team *specificTeamToJoin); - /// \brief Do not join a team, or leave all current teams. - /// \details This always succeeds. ID_TEAM_BALANCER_TEAM_ASSIGNED will be returned to all systems. - /// \param[in] noTeamSubcategory Even when not on a team, you can internally identify a subcategory of not being on a team, such as AI or spectator. - static TeamSelection NoTeam(NoTeamId noTeamSubcategory); -}; - -/// \brief A member of one or more teams. -/// \details Contains data and operations on data to manage which team your game's team members are on. -/// Best used as a composite member of your "User" or "Player" class(es). -/// When using with ReplicaManager3, call TM_TeamMember::ReferenceTeamMember() in Replica3::DeserializeConstruction() and TM_TeamMember::DeserializeConstruction() in Replica3::PostDeserializeConstruction() -/// There is otherwise no need to manually serialize the class, as operations are networked internally. -/// \ingroup TEAM_MANAGER_GROUP -class RAK_DLL_EXPORT TM_TeamMember -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(TM_TeamMember) - - TM_TeamMember(); - virtual ~TM_TeamMember(); - - /// \brief Request to join any team, a specific team, or to leave all teams - /// \details Function will return false on invalid operations, such as joining a team you are already on. - /// Will also fail with TeamSelection::JOIN_ANY_AVAILABLE_TEAM if you are currently on a team. - /// On success, every system will get ID_TEAM_BALANCER_TEAM_ASSIGNED. Use TeamManager::DecomposeTeamAssigned() to get details of which team member the message refers to. - /// On failure, all systems will get ID_TEAM_BALANCER_REQUESTED_TEAM_FULL or ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED. Use TeamManager::DecomposeTeamFull() and TeamManager::DecomposeTeamLocked() to get details of which team member the message refers to. - /// \note Joining a specific team with this function may result in being on more than one team at once, even if you call the function while locally only on one team. If your game depends on only being on one team at a team, use RequestTeamSwitch() instead with the parameter teamToLeave set to 0 - /// \param[in] TeamSelection::AnyAvailable(), TeamSelection::SpecificTeam(), or TeamSelection::NoTeam() - /// \return false On invalid or unnecessary operation. Otherwise returns true - bool RequestTeam(TeamSelection teamSelection); - - /// \brief Similar to RequestTeam with TeamSelection::SpecificTeam(), but leave a team simultaneously when the desired team is joinable - /// \param[in] teamToJoin Which team to join - /// \param[in] teamToLeave If 0, means leave all current teams. Otherwise, leave the specified team. - /// \return false On invalid or unnecessary operation. Otherwise returns true - bool RequestTeamSwitch(TM_Team *teamToJoin, TM_Team *teamToLeave); - - /// \brief Returns the first requested team in the list of requested teams, if you have a requested team at all. - /// \return TeamSelection::SpecificTeam(), TeamSelection::NoTeam(), or TeamSelection::AnyAvailable() - TeamSelection GetRequestedTeam(void) const; - - /// \brief Returns pending calls to RequestTeam() when using TeamSelection::JOIN_SPECIFIC_TEAM - /// \param[out] All pending requested teams - void GetRequestedSpecificTeams(DataStructures::List &requestedTeams) const; - - /// \brief Returns if the specified team is in the list of pending requested teams - /// \param[in] The team we are checking - /// \return Did we request to join this specific team? - bool HasRequestedTeam(TM_Team *team) const; - - /// \brief Returns the index of \a team in the requested teams list - /// \param[in] The team we are checking - /// \return -1 if we did not requested to join this team. Otherwise the index. - unsigned int GetRequestedTeamIndex(TM_Team *team) const; - - /// \return The number of teams that would be returned by a call to GetRequestedSpecificTeams() - unsigned int GetRequestedTeamCount(void) const; - - /// \brief Cancels a request to join a specific team. - /// \details Useful if you got ID_TEAM_BALANCER_REQUESTED_TEAM_FULL or ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED and changed your mind about joining the team. - /// \note This is not guaranteed to work due to latency. To clarify, If the host switches your team at the same time you call CancelRequestTeam() you may still get ID_TEAM_BALANCER_TEAM_ASSIGNED for the team you tried to cancel. - /// \param[in] specificTeamToCancel Which team to no longer join. Use 0 for all. - /// \return false On invalid or unnecessary operation. Otherwise returns true - bool CancelTeamRequest(TM_Team *specificTeamToCancel); - - /// \brief Leave a team - /// \details Leaves a team that you are on. Always succeeds provided you are on that team - /// Generates ID_TEAM_BALANCER_TEAM_ASSIGNED on all systems on success. - /// If you leave the last team you are on, \a noTeamSubcategory is set as well. - /// \param[in] team Which team to leave - /// \param[in] _noTeamSubcategory If the team member has been removed from all teams, which subcategory of NoTeamId to set them to - /// \return false On invalid or unnecessary operation. Otherwise returns true - bool LeaveTeam(TM_Team* team, NoTeamId _noTeamSubcategory); - - /// \brief Leave all teams - /// \Details Leaves all teams you are on, and sets \a noTeamSubcategory - /// \note This is the same as and just calls RequestTeam(TeamSelection::NoTeam(noTeamSubcategory)); - /// \return false On invalid or unnecessary operation. Otherwise returns true - bool LeaveAllTeams(NoTeamId noTeamSubcategory); - - /// \return Get the first team we are on, or 0 if we are not on a team. - TM_Team* GetCurrentTeam(void) const; - - /// \return How many teams we are on - unsigned int GetCurrentTeamCount(void) const; - - /// \return Returns one of the teams in the current team list, up to GetCurrentTeamCount() - TM_Team* GetCurrentTeamByIndex(unsigned int index); - - /// \param[out] Get all teams we are on, as a list - void GetCurrentTeams(DataStructures::List &_teams) const; - - /// For each team member, when you get ID_TEAM_BALANCER_TEAM_ASSIGNED for that member, the team list is saved. - /// Use this function to get that list, for example to determine which teams we just left or joined - /// \param[out] _teams The previous list of teams we were on - void GetLastTeams(DataStructures::List &_teams) const; - - /// \param[in] The team we are checking - /// \return Are we on this team? - bool IsOnTeam(TM_Team *team) const; - - /// \return The teamMemberID parameter passed to TM_World::ReferenceTeamMember() - NetworkID GetNetworkID(void) const; - - /// \return The TM_World instance that was used when calling TM_World::ReferenceTeamMember() - TM_World* GetTM_World(void) const; - - /// \brief Serializes the current state of this object - /// \details To replicate a TM_TeamMember on another system, first instantiate the object using your own code, or a system such as ReplicaManager3. - /// Next, call SerializeConstruction() from whichever system owns the team member - /// Last, call DeserializeConstruction() on the newly created TM_TeamMember - /// \note You must instantiate and deserialize all TM_Team instances that the team member refers to before calling DesrializeConstruction(). ReplicaManager3::PostSerializeConstruction() and ReplicaManager3::PostDeserializeConstruction() will ensure this. - /// \param[out] constructionBitstream This object serialized to a BitStream - void SerializeConstruction(BitStream *constructionBitstream); - - /// \brief Deserializes the current state of this object - /// \details See SerializeConstruction for more details() - /// \note DeserializeConstruction also calls ReferenceTeamMember on the passed \a teamManager instance, there is no need to do so yourself - /// \param[in] teamManager TeamManager instance - /// \param[in] constructionBitstream This object serialized to a BitStream - bool DeserializeConstruction(TeamManager *teamManager, BitStream *constructionBitstream); - - /// \param[in] o Stores a void* for your own use. If using composition, this is useful to store a pointer to the containing object. - void SetOwner(void *o); - - /// \return Whatever was passed to SetOwner() - void *GetOwner(void) const; - - /// \return If not on a team, returns the current NoTeamId value - NoTeamId GetNoTeamId(void) const; - - /// Return world->GetTeamMemberIndex(this) - unsigned int GetWorldIndex(void) const; - - /// \internal - static unsigned long ToUint32( const NetworkID &g ); - - /// \internal - struct RequestedTeam - { - RakNet::Time whenRequested; - unsigned int requestIndex; - TM_Team *requested; - bool isTeamSwitch; - TM_Team *teamToLeave; - }; - -protected: - NetworkID networkId; - TM_World* world; - // Teams we are a member of. We can be on more than one team, but not on the same team more than once - DataStructures::List teams; - // If teams is empty, which subcategory of noTeam we are on - NoTeamId noTeamSubcategory; - // Teams we have requested to join. Mutually exclusive with teams we are already on. Cannot request the same team more than once. - DataStructures::List teamsRequested; - // If teamsRequested is not empty, we want to join a specific team - // If teamsRequested is empty, then joinTeamType is either JOIN_NO_TEAM or JOIN_ANY_AVAILABLE_TEAM - JoinTeamType joinTeamType; - // Set by StoreLastTeams() - DataStructures::List lastTeams; - RakNet::Time whenJoinAnyRequested; - unsigned int joinAnyRequestIndex; - void *owner; - - // Remove from all requested and current teams. - void UpdateListsToNoTeam(NoTeamId nti); - bool JoinAnyTeamCheck(void) const; - bool JoinSpecificTeamCheck(TM_Team *specificTeamToJoin, bool ignoreRequested) const; - bool SwitchSpecificTeamCheck(TM_Team *teamToJoin, TM_Team *teamToLeave, bool ignoreRequested) const; - bool LeaveTeamCheck(TM_Team *team) const; - void UpdateTeamsRequestedToAny(void); - void UpdateTeamsRequestedToNone(void); - void AddToRequestedTeams(TM_Team *teamToJoin); - void AddToRequestedTeams(TM_Team *teamToJoin, TM_Team *teamToLeave); - bool RemoveFromRequestedTeams(TM_Team *team); - void AddToTeamList(TM_Team *team); - void RemoveFromSpecificTeamInternal(TM_Team *team); - void RemoveFromAllTeamsInternal(void); - void StoreLastTeams(void); - - friend class TM_World; - friend class TM_Team; - friend class TeamManager; -}; - -/// \brief A team, containing a list of TM_TeamMember instances -/// \details Contains lists of TM_TeamMember instances -/// Best used as a composite member of your "Team" or "PlayerList" class(es). -/// When using with ReplicaManager3, call TM_Team::ReferenceTeam() in Replica3::DeserializeConstruction() and TM_Team::DeserializeConstruction() in Replica3::PostDeserializeConstruction() -/// There is otherwise no need to manually serialize the class, as operations are networked internally. -/// \ingroup TEAM_MANAGER_GROUP -class RAK_DLL_EXPORT TM_Team -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(TM_Team) - - TM_Team(); - virtual ~TM_Team(); - - /// \brief Set the maximum number of members that can join this team. - /// Defaults to 65535 - /// Setting the limit lower than the existing number of members kicks members out, and assigns noTeamSubcategory to them if they have no other team to go to - /// Setting the limit higher allows members to join in. If a member has a pending request to join this team, they join automatically and ID_TEAM_BALANCER_TEAM_ASSIGNED will be returned for those members. - /// \param[in] _teamMemberLimit The new limit - /// \param[in] noTeamSubcategory Which noTeamSubcategory to assign to members that now have no team. - /// \return false On invalid or unnecessary operation. Otherwise returns true - bool SetMemberLimit(TeamMemberLimit _teamMemberLimit, NoTeamId noTeamSubcategory); - - /// \return If team balancing is on, the most members that can be on this team that would not either unbalance it or exceed the value passed to SetMemberLimit(). If team balancing is off, the same as GetMemberLimitSetting() - TeamMemberLimit GetMemberLimit(void) const; - - /// \return What was passed to SetMemberLimit() or the default - TeamMemberLimit GetMemberLimitSetting(void) const; - - /// \brief Who can join this team under what conditions, while the team is not full - /// To not allow new joins, pass 0 - /// To allow all new joins under any circumstances, bitwise-OR all permission defines. - /// For an invite-only team, use ALLOW_JOIN_SPECIFIC_TEAM only and only allow the requester to call TM_TeamMember::RequestTeam() upon invitiation through your game code. - /// Defaults to allow all - /// \param[in] _joinPermissions Bitwise combination of ALLOW_JOIN_ANY_AVAILABLE_TEAM, ALLOW_JOIN_SPECIFIC_TEAM, ALLOW_JOIN_REBALANCING - /// \return false On invalid or unnecessary operation. Otherwise returns true - bool SetJoinPermissions(JoinPermissions _joinPermissions); - - /// \return Whatever was passed to SetJoinPermissions(), or the default. - JoinPermissions GetJoinPermissions(void) const; - - /// \brief Removes a member from a team he or she is on - /// \details Identical to teamMember->LeaveTeam(this, noTeamSubcategory); See TeamMember::LeaveTeam() for details. - /// \param[in] teamMember Which team member to remove - /// \param[in] noTeamSubcategory If the team member has been removed from all teams, which subcategory of NoTeamId to set them to - void LeaveTeam(TM_TeamMember* teamMember, NoTeamId noTeamSubcategory); - - /// \return What was passed as the \a applyBalancing parameter TM_World::ReferenceTeam() when this team was added. - bool GetBalancingApplies(void) const; - - /// \param[out] All team members of this team - void GetTeamMembers(DataStructures::List &_teamMembers) const; - - /// \return The number of team members on this team - unsigned int GetTeamMembersCount(void) const; - - /// \return A team member on this team. Members are stored in the order they are added - /// \param[in] index A value between 0 and GetTeamMembersCount() - TM_TeamMember *GetTeamMemberByIndex(unsigned int index) const; - - /// \return The teamID parameter passed to TM_World::ReferenceTeam() - NetworkID GetNetworkID(void) const; - - /// \return The TM_World instance that was used when calling TM_World::ReferenceTeamMember() - TM_World* GetTM_World(void) const; - - /// \brief Used by the host to serialize the initial state of this object to a new system - /// \details On the host, when sending existing objects to a new system, call SerializeConstruction() on each of those objects to serialize creation state. - /// Creating the actual Team and TeamMember objects should be handled by your game code, or a system such as ReplicaManager3 - void SerializeConstruction(BitStream *constructionBitstream); - - /// \brief Used by non-host systems to read the bitStream written by SerializeConstruction() - /// \details On non-host systems, after creating existing objects, call DeserializeConstruction() to read and setup that object - /// Creating the actual Team and TeamMember objects should be handled by your game code, or a system such as ReplicaManager3 - bool DeserializeConstruction(TeamManager *teamManager, BitStream *constructionBitstream); - - /// \param[in] o Stores a void* for your own use. If using composition, this is useful to store a pointer to the containing object. - void SetOwner(void *o); - - /// \return Whatever was passed to SetOwner() - void *GetOwner(void) const; - - /// Return world->GetTeamIndex(this) - unsigned int GetWorldIndex(void) const; - - /// \internal - static unsigned long ToUint32( const NetworkID &g ); - -protected: - NetworkID ID; - TM_World* world; - // Which members are on this team. The same member cannot be on the same team more than once - DataStructures::List teamMembers; - // Permissions on who can join this team - JoinPermissions joinPermissions; - // Whether or not to consider this team when balancing teams - bool balancingApplies; - TeamMemberLimit teamMemberLimit; - void *owner; - - // Remove input from list teamMembers - void RemoveFromTeamMemberList(TM_TeamMember *teamMember); - - // Find the member index that wants to join the indicated team, is only on one team, and wants to leave that team - unsigned int GetMemberWithRequestedSingleTeamSwitch(TM_Team *team); - - - friend class TM_World; - friend class TM_TeamMember; - friend class TeamManager; -}; - -/// \brief Stores a list of teams which may be enforcing a balanced number of members -/// \details Each TM_World instance is independent of other TM_World world instances. This enables you to host multiple games on a single computer. -/// Not currently supported to have the same TM_Team or TM_TeamMember in more than one world at a time, but easily added on request. -/// \ingroup TEAM_MANAGER_GROUP -class TM_World -{ -public: - TM_World(); - virtual ~TM_World(); - - /// \return Returns the plugin that created this TM_World instance - TeamManager *GetTeamManager(void) const; - - /// \brief Add a new system to send team and team member updates to. - /// \param[in] rakNetGUID GUID of the system you are adding. See Packet::rakNetGUID or RakPeerInterface::GetGUIDFromSystemAddress() - void AddParticipant(RakNetGUID rakNetGUID); - - /// \brief Remove a system that was previously added with AddParticipant() - /// \details Systems that disconnect are removed automatically - /// \param[in] rakNetGUID GUID of the system you are removing. See Packet::rakNetGUID or RakPeerInterface::GetGUIDFromSystemAddress() - void RemoveParticipant(RakNetGUID rakNetGUID); - - /// \brief If true, all new connections are added to this world using AddParticipant() - /// \details Defaults to true - /// \param[in] autoAdd Setting to set - void SetAutoManageConnections(bool autoAdd); - - /// Get the participants added with AddParticipant() - /// \param[out] participantList Participants added with AddParticipant(); - void GetParticipantList(DataStructures::List &participantList); - - /// \brief Register a TM_Team object with this system. - /// \details Your game should contain instances of TM_Team, for example by using composition with your game's Team or PlayerList class - /// Tell TeamManager about these instances using ReferenceTeam(). - /// \note The destrutor of TM_Team calls DereferenceTeam() automatically. - /// \param[in] team The instance you are registering - /// \param[in] networkId Identifies this instance. This value is independent of values used by NetworkIDManager. You can use the same value as the object that contains this instance. - /// \param[in] applyBalancing Whether or not to include this team for balancing when calling SetBalanceTeams(). - void ReferenceTeam(TM_Team *team, NetworkID networkId, bool applyBalancing); - - /// \brief Unregisters the associated TM_Team object with this system. - /// Call when a TM_Team instance is no longer needed - /// \param[in] team Which team instance to unregister - /// \param[in] noTeamSubcategory All players on this team are kicked off. If these players then have no team, they are set to this no team category. - void DereferenceTeam(TM_Team *team, NoTeamId noTeamSubcategory); - - /// \return Number of teams uniquely added with ReferenceTeam() - unsigned int GetTeamCount(void) const; - - /// \param[in] index A value between 0 and GetTeamCount() - /// \return Returns whatever was passed to \a team in the function ReferenceTeam() in the order it was called. - TM_Team *GetTeamByIndex(unsigned int index) const; - - /// \param[in] teamId Value passed to ReferenceTeam() - /// \return Returns whatever was passed to \a team in the function ReferenceTeam() with this NetworkID. - TM_Team *GetTeamByNetworkID(NetworkID teamId); - - /// \brief Inverse of GetTeamByIndex() - /// \param[in] team Which taem - /// \return The index of the specified team, or -1 if not found - unsigned int GetTeamIndex(const TM_Team *team) const; - - /// \brief Register a TM_TeamMember object with this system. - /// \details Your game should contain instances of TM_TeamMember, for example by using composition with your game's User or Player classes - /// Tell TeamManager about these instances using ReferenceTeamMember(). - /// \note The destrutor of TM_TeamMember calls DereferenceTeamMember() automatically. - /// \param[in] teamMember The instance you are registering - /// \param[in] networkId Identifies this instance. This value is independent of values used by NetworkIDManager. You can use the same value as the object that contains this instance - void ReferenceTeamMember(TM_TeamMember *teamMember, NetworkID networkId); - - /// \brief Unregisters the associated TM_TeamMember object with this system. - /// Call when a TM_TeamMember instance is no longer needed - /// \note This is called by the destructor of TM_TeamMember automatically, so you do not normally need to call this function - void DereferenceTeamMember(TM_TeamMember *teamMember); - - /// \return Number of team members uniquely added with ReferenceTeamMember() - unsigned int GetTeamMemberCount(void) const; - - /// \param[in] index A value between 0 and GetTeamMemberCount() - /// \return Returns whatever was passed to \a team in the function ReferenceTeamMember() in the order it was called. - TM_TeamMember *GetTeamMemberByIndex(unsigned int index) const; - - /// \param[in] index A value between 0 and GetTeamMemberCount() - /// \return Returns whatever was passed to \a teamMemberID in the function ReferenceTeamMember() in the order it was called. - NetworkID GetTeamMemberIDByIndex(unsigned int index) const; - - /// \param[in] teamId Value passed to ReferenceTeamMember() - /// \return Returns Returns whatever was passed to \a team in the function ReferenceTeamMember() with this NetworkID - TM_TeamMember *GetTeamMemberByNetworkID(NetworkID teamMemberId); - - /// \brief Inverse of GetTeamMemberByIndex() - /// \param[in] team Which team member - /// \return The index of the specified team member, or -1 if not found - unsigned int GetTeamMemberIndex(const TM_TeamMember *teamMember) const; - - /// \brief Force or stop forcing teams to be balanced. - /// \details For each team added with ReferenceTeam() and \a applyBalancing set to true, players on unbalanced teams will be redistributed - /// While active, players can only join balanced teams if doing so would not cause that team to become unbalanced. - /// If a player on the desired team also wants to switch, then both players will switch simultaneously. Otherwise, ID_TEAM_BALANCER_REQUESTED_TEAM_FULL will be returned to the requester and switching will occur when possible. - /// If balanceTeams is true and later set to false, players waiting on ID_TEAM_BALANCER_REQUESTED_TEAM_FULL will be able to join the desired team immediately provided it is not full. - /// \param[in] balanceTeams Whether to activate or deactivate team balancing. - /// \param[in] noTeamSubcategory If a player is kicked off a team and is no longer on any team, his or her noTeamSubcategory is set to this value - bool SetBalanceTeams(bool balanceTeams, NoTeamId noTeamSubcategory); - - /// \return \a balanceTeams parameter of SetBalanceTeams(), or the default - bool GetBalanceTeams(void) const; - - /// \brief Set the host that will perform balancing calculations and send notifications - /// \details Operations that can cause conflicts due to latency, such as joining teams, are operated on by the host. The result is sent to all systems added with AddParticipant() - /// For a client/server game, call SetHost() with the server's RakNetGUID value on all systems (including the server itself). If you call TeamManager::SetTopology(TM_CLIENT_SERVER), the server will also relay messages between participants. - /// For a peer to peer game, call SetHost() on the same peer when host migration occurs. Use TeamManager::SetTopology(TM_PEER_TO_PEER) in this case. - /// \note If using FullyConnectedMesh2, SetHost() is called automatically when ID_FCM2_NEW_HOST is returned. - /// \param[in] _hostGuid The host, which is the system that will serialize and resolve team disputes and calculate team balancing. - void SetHost(RakNetGUID _hostGuid); - - /// \return Returns the current host, or UNASSIGNED_RAKNET_GUID if unknown - RakNetGUID GetHost(void) const; - - /// \return The \a worldId passed to TeamManagr::AddWorld() - WorldId GetWorldId(void) const; - - /// \brief Clear all memory and reset everything. - /// \details It is up to the user to deallocate pointers passed to ReferenceTeamMember() or ReferenceTeam(), if so desired. - void Clear(void); - - /// \internal - struct JoinRequestHelper - { - RakNet::Time whenRequestMade; - unsigned int teamMemberIndex; - unsigned int indexIntoTeamsRequested; - unsigned int requestIndex; - }; - /// \internal - static int JoinRequestHelperComp(const TM_World::JoinRequestHelper &key, const TM_World::JoinRequestHelper &data); - -protected: - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); - - // Teams with too many members have those members go to other teams. - void EnforceTeamBalance(NoTeamId noTeamSubcategory); - void KickExcessMembers(NoTeamId noTeamSubcategory); - void FillRequestedSlots(void); - unsigned int GetAvailableTeamIndexWithFewestMembers(TeamMemberLimit secondaryLimit, JoinPermissions joinPermissions); - - void GetSortedJoinRequests(DataStructures::OrderedList &joinRequests); - - - // Send a message to all participants - void BroadcastToParticipants(RakNet::BitStream *bsOut, RakNetGUID exclusionGuid); - void BroadcastToParticipants(unsigned char *data, const int length, RakNetGUID exclusionGuid); - - // 1. If can join a team: - // A. teamMember->UpdateTeamsRequestedToNone(); - // B. teamMember->AddToTeamList() - // C. Return new team - // 2. Else return 0 - TM_Team* JoinAnyTeam(TM_TeamMember *teamMember, int *resultCode); - - int JoinSpecificTeam(TM_TeamMember *teamMember, TM_Team *team, bool isTeamSwitch, TM_Team *teamToLeave, DataStructures::List &teamsWeAreLeaving); - - TeamMemberLimit GetBalancedTeamLimit(void) const; - - // For fast lookup. Shares pointers with list teams - DataStructures::Hash teamsHash; - // For fast lookup. Shares pointers with list teamMembers - DataStructures::Hash teamMembersHash; - - TeamManager *teamManager; - DataStructures::List participants; - DataStructures::List teams; - DataStructures::List teamMembers; - bool balanceTeamsIsActive; - RakNetGUID hostGuid; - WorldId worldId; - bool autoAddParticipants; - int teamRequestIndex; - - friend class TeamManager; - friend class TM_TeamMember; - friend class TM_Team; -}; - -/// \brief Automates networking and list management for teams -/// \details TeamManager provides support for teams. A team is a list of team members. -/// Teams contain properties including the number of team members per team, whether or not tagged teams must have equal numbers of members, and if a team is locked or not to certain entry conditions -/// Team members contain properties including which teams they are on and which teams they want to join if a team is not immediately joinable -/// Advanced functionality includes the ability for a team member to be on multiple teams simultaneously, the ability to swap teams with other members, and the ability to resize the number of members supported per team -/// The architecture is designed for easy integration with ReplicaManager3 -/// -/// Usage:
      -/// 1. Define your game classes to represent teams and team members. Your game classes should hold game-specific information such as team name and color.
      -/// 2. Have those game classes contain a corresponding TM_Team or TM_TeamMember instance. Operations on teams will be performed by those instances. Use SetOwner() to refer to the parent object when using composition.
      -/// 3. Call TeamManager::SetTopology() for client/server or peer to peer.
      -/// 4. Call AddWorld() to instantiate a TM_World object which will contain references to your TM_TeamMember and TM_Team instances.
      -/// 5. When you instantiate a TM_TeamMember or TM_Team object, call ReferenceTeam() and ReferenceTeamMember() for each corresponding object
      -/// 6. When sending world state to a new connection, for example in ReplicaManager3::SerializeConstruction(), call TM_SerializeConstruction() on the corresponding TM_TeamMember and TM_Team objects. TM_Team instances on the new connection must be created before TM_TeamMember instances.
      -/// 7. Call TM_DeserializeConstruction() on your new corresponding TM_TeamMember and TM_Team instances.
      -/// 8. Execute team operations. ID_TEAM_BALANCER_REQUESTED_TEAM_FULL, ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED, ID_TEAM_BALANCER_TEAM_REQUESTED_CANCELLED, and ID_TEAM_BALANCER_TEAM_ASSIGNED are returned to all systems when the corresponding event occurs for a team member.
      -/// 9. As the peer to peer session host changes, call SetHost() (Not necessary if using FullyConnectedMesh2). If using client/server, you must set the host
      -/// \note This replaces TeamBalancer. You cannot use TeamBalancer and TeamManager at the same time. -/// \ingroup TEAM_MANAGER_GROUP -class RAK_DLL_EXPORT TeamManager : public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(TeamManager) - - TeamManager(); - virtual ~TeamManager(); - - /// \brief Allocate a world to hold a list of teams and players for that team. - /// Use the returned TM_World object for actual team functionality. - /// \note The world is tracked by TeamManager and deallocated by calling Clear() - /// \param[in] worldId Arbitrary user-defined id of the world to create. Each world instance must have a unique id. - TM_World* AddWorld(WorldId worldId); - - /// \brief Deallocate a world created with AddWorld() - /// \param[in] worldId The world to deallocate - void RemoveWorld(WorldId worldId); - - /// \return Returns the number of worlds created with AddWorld() - unsigned int GetWorldCount(void) const; - - /// \param[in] index A value beteween 0 and GetWorldCount()-1 inclusive. - /// \return Returns a world created with AddWorld() - TM_World* GetWorldAtIndex(unsigned int index) const; - - /// \param[in] worldId \a worldId value passed to AddWorld() - /// \return Returns a world created with AddWorld(), or 0 if no such \a worldId - TM_World* GetWorldWithId(WorldId worldId) const; - - /// \brief When auto managing connections, call TM_World::AddParticipant() on all worlds for all new connections automatically - /// Defaults to true - /// \note You probably want this set to false if using multiple worlds - /// \param[in] autoAdd Automatically call TM_World::AddParticipant() all worlds each new connection. Defaults to true. - void SetAutoManageConnections(bool autoAdd); - - /// \brief If \a _topology is set to TM_CLIENT_SERVER, the host will relay messages to participants. - /// \details If topology is set to TM_PEER_TO_PEER, the host assumes the original message source was connected to all other participants and does not relay messages. - /// \note If TM_PEER_TO_PEER, this plugin will listen for ID_FCM2_NEW_HOST and call SetHost() on all worlds automatically - /// \note Defaults to TM_PEER_TO_PEER - /// \param[in] _topology Topology to use - void SetTopology(TMTopology _topology); - - /// \brief When you get ID_TEAM_BALANCER_REQUESTED_TEAM_FULL, pass the packet to this function to read out parameters - /// \param[in] A packet where packet->data[0]==ID_TEAM_BALANCER_REQUESTED_TEAM_FULL - /// \return true on success, false on read error - void DecomposeTeamFull(Packet *packet, - TM_World **world, TM_TeamMember **teamMember, TM_Team **team, - uint16_t ¤tMembers, uint16_t &memberLimitIncludingBalancing, bool &balancingIsActive, JoinPermissions &joinPermissions); - - /// \brief When you get ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED, pass the packet to this function to read out parameters - /// \param[in] A packet where packet->data[0]==ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED - /// \return true on success, false on read error - void DecomposeTeamLocked(Packet *packet, - TM_World **world, TM_TeamMember **teamMember, TM_Team **team, - uint16_t ¤tMembers, uint16_t &memberLimitIncludingBalancing, bool &balancingIsActive, JoinPermissions &joinPermissions); - - /// \brief Clear all memory and reset everything. - /// \details Deallocates TM_World instances. It is up to the user to deallocate pointers passed to ReferenceTeamMember() or ReferenceTeam(), if so desired. - void Clear(void); - - /// \brief Reads out the world and teamMember from ID_TEAM_BALANCER_TEAM_ASSIGNED - /// \note You can get the current and prior team list from the teamMember itself - /// \param[in] A packet where packet->data[0]==ID_TEAM_BALANCER_TEAM_ASSIGNED - /// \param[out] world Set to the world this \a teamMember is on. 0 on bad lookup. - /// \param[out] teamMember Set to the teamMember affected. 0 on bad lookup. - void DecodeTeamAssigned(Packet *packet, TM_World **world, TM_TeamMember **teamMember); - - // \brief Reads out the world and teamMember from ID_TEAM_BALANCER_TEAM_REQUESTED_CANCELLED - /// \note You can get the requested team list from the teamMember itself - /// \param[in] A packet where packet->data[0]==ID_TEAM_BALANCER_TEAM_REQUESTED_CANCELLED - /// \param[out] world Set to the world this \a teamMember is on. 0 on bad lookup. - /// \param[out] teamMember Set to the teamMember affected. 0 on bad lookup. - /// \param[out] teamCancelled Set to the team that was cancelled. 0 for all teams. - void DecodeTeamCancelled(Packet *packet, TM_World **world, TM_TeamMember **teamMember, TM_Team **teamCancelled); - -protected: - - virtual void Update(void); - virtual PluginReceiveResult OnReceive(Packet *packet); - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); - void Send( const RakNet::BitStream * bitStream, const AddressOrGUID systemIdentifier, bool broadcast ); - - void EncodeTeamFullOrLocked(RakNet::BitStream *bitStream, TM_TeamMember *teamMember, TM_Team *team); - void DecomposeTeamFullOrLocked(RakNet::BitStream *bsIn, TM_World **world, TM_TeamMember **teamMember, TM_Team **team, - uint16_t ¤tMembers, uint16_t &memberLimitIncludingBalancing, bool &balancingIsActive, JoinPermissions &joinPermissions); - void ProcessTeamAssigned(RakNet::BitStream *bsIn); - - void EncodeTeamAssigned(RakNet::BitStream *bitStream, TM_TeamMember *teamMember); - void RemoveFromTeamsRequestedAndAddTeam(TM_TeamMember *teamMember, TM_Team *team, bool isTeamSwitch, TM_Team *teamToLeave); - - void PushTeamAssigned(TM_TeamMember *teamMember); - void PushBitStream(RakNet::BitStream *bitStream); - void OnUpdateListsToNoTeam(Packet *packet, TM_World *world); - void OnUpdateTeamsRequestedToAny(Packet *packet, TM_World *world); - void OnJoinAnyTeam(Packet *packet, TM_World *world); - void OnJoinRequestedTeam(Packet *packet, TM_World *world); - void OnUpdateTeamsRequestedToNoneAndAddTeam(Packet *packet, TM_World *world); - void OnRemoveFromTeamsRequestedAndAddTeam(Packet *packet, TM_World *world); - void OnAddToRequestedTeams(Packet *packet, TM_World *world); - bool OnRemoveFromRequestedTeams(Packet *packet, TM_World *world); - void OnLeaveTeam(Packet *packet, TM_World *world); - void OnSetMemberLimit(Packet *packet, TM_World *world); - void OnSetJoinPermissions(Packet *packet, TM_World *world); - void OnSetBalanceTeams(Packet *packet, TM_World *world); - void OnSetBalanceTeamsInitial(Packet *packet, TM_World *world); - - - void EncodeTeamFull(RakNet::BitStream *bitStream, TM_TeamMember *teamMember, TM_Team *team); - void EncodeTeamLocked(RakNet::BitStream *bitStream, TM_TeamMember *teamMember, TM_Team *team); - - /// \brief When you get ID_TEAM_BALANCER_TEAM_ASSIGNED, pass the packet to this function to read out parameters - /// \param[in] A packet where packet->data[0]==ID_TEAM_BALANCER_TEAM_ASSIGNED - /// \return true on success, false on read error - void DecodeTeamAssigned(RakNet::BitStream *bsIn, TM_World **world, TM_TeamMember **teamMember, NoTeamId &noTeamSubcategory, - JoinTeamType &joinTeamType, DataStructures::List &newTeam, - DataStructures::List &teamsLeft, DataStructures::List &teamsJoined); - - // O(1) lookup for a given world. If I need more worlds, change this to a hash or ordered list - TM_World *worldsArray[255]; - // All allocated worlds for linear traversal - DataStructures::List worldsList; - bool autoAddParticipants; - TMTopology topology; - - friend class TM_TeamMember; - friend class TM_World; - friend class TM_Team; -}; - -} // namespace RakNet - -#endif // __TEAM_MANAGER_H - -#endif // _RAKNET_SUPPORT_* - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +// TODO: optimize the list of teams and team members to be O(1). Store in hashes, use linked lists to get ordered traversal + +/// \file TeamManager.h +/// \brief Automates networking and list management for teams +/// \details TeamManager provides support for teams. A team is a list of team members. +/// Teams contain properties including the number of team members per team, whether or not tagged teams must have equal numbers of members, and if a team is locked or not to certain entry conditions +/// Team members contain properties including which teams they are on and which teams they want to join if a team is not immediately joinable +/// Advanced functionality includes the ability for a team member to be on multiple teams simultaneously, the ability to swap teams with other members, and the ability to resize the number of members supported per team +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_TeamManager==1 + +#pragma once + +#include "PluginInterface2.h" +#include "RakMemoryOverride.h" +#include "NativeTypes.h" +#include "DS_List.h" +#include "RakNetTypes.h" +#include "DS_Hash.h" +#include "DS_OrderedList.h" + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; + +/// \defgroup TEAM_MANAGER_GROUP TeamManager +/// \brief Automates networking and list management for teams +/// \details When used with ReplicaManager3 and FullyConnectedMesh2, provides a complete solution to managing a distributed list of teams and team member objects with support for host migration. +/// \ingroup PLUGINS_GROUP + +/// \ingroup TEAM_MANAGER_GROUP +/// \brief A subcategory of not being on a team. For example, 0 may mean no team for a player, while 1 may mean no team for a spectator. Defined by the user. +typedef unsigned char NoTeamId; + +/// \ingroup TEAM_MANAGER_GROUP +/// Used for multiple worlds. +typedef uint8_t WorldId; + +/// \ingroup TEAM_MANAGER_GROUP +/// Maximum number of members on one team. Use 65535 for unlimited. +typedef uint16_t TeamMemberLimit; + +/// Allow members to join this team when they specify TeamSelection::JOIN_ANY_AVAILABLE_TEAM +#define ALLOW_JOIN_ANY_AVAILABLE_TEAM (1<<0) +/// Allow members to join this team when they specify TeamSelection::JOIN_SPECIFIC_TEAM +#define ALLOW_JOIN_SPECIFIC_TEAM (1<<1) +/// Allow the host to put members on this team when rebalancing with TM_World::SetBalanceTeams() +#define ALLOW_JOIN_REBALANCING (1<<2) + +// Bitwise combination of ALLOW_JOIN_ANY_AVAILABLE_TEAM, ALLOW_JOIN_SPECIFIC_TEAM, ALLOW_JOIN_REBALANCING +typedef uint8_t JoinPermissions; + +// Forward declarations +class TM_Team; +class TM_TeamMember; +class TM_World; +class TeamManager; + +/// \ingroup TEAM_MANAGER_GROUP +enum JoinTeamType +{ + /// Attempt to join the first available team. + JOIN_ANY_AVAILABLE_TEAM, + /// Attempt to join a specific team, previously added with TM_World::ReferenceTeam() + JOIN_SPECIFIC_TEAM, + /// No team. Always succeeds. + JOIN_NO_TEAM +}; + +/// \ingroup TEAM_MANAGER_GROUP +enum TMTopology +{ + // Each system will send all messages to all participants + TM_PEER_TO_PEER, + + // The host will relay incoming messages to all participants + TM_CLIENT_SERVER, +}; + +/// \brief Parameter to TM_World::ReferenceTeamMember() +/// \details Use TeamSelection::AnyAvailable(), TeamSelection::SpecificTeam(), or TeamSelection::NoTeam() +/// \ingroup TEAM_MANAGER_GROUP +struct TeamSelection +{ + TeamSelection(); + TeamSelection(JoinTeamType itt); + TeamSelection(JoinTeamType itt, TM_Team *param); + TeamSelection(JoinTeamType itt, NoTeamId param); + JoinTeamType joinTeamType; + + union + { + TM_Team *specificTeamToJoin; + NoTeamId noTeamSubcategory; + } teamParameter; + + /// \brief Join any team that has available slots and is tagged with ALLOW_JOIN_ANY_AVAILABLE_TEAM + /// \details ID_TEAM_BALANCER_TEAM_ASSIGNED, ID_TEAM_BALANCER_REQUESTED_TEAM_FULL, or ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED will be returned to all systems. + static TeamSelection AnyAvailable(void); + /// \brief Join a specific team if it has available slots, and is tagged with JOIN_SPECIFIC_TEAMS + /// \details ID_TEAM_BALANCER_TEAM_ASSIGNED, ID_TEAM_BALANCER_REQUESTED_TEAM_FULL, or ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED will be returned to all systems. + /// \param[in] specificTeamToJoin Which team to attempt to join. + static TeamSelection SpecificTeam(TM_Team *specificTeamToJoin); + /// \brief Do not join a team, or leave all current teams. + /// \details This always succeeds. ID_TEAM_BALANCER_TEAM_ASSIGNED will be returned to all systems. + /// \param[in] noTeamSubcategory Even when not on a team, you can internally identify a subcategory of not being on a team, such as AI or spectator. + static TeamSelection NoTeam(NoTeamId noTeamSubcategory); +}; + +/// \brief A member of one or more teams. +/// \details Contains data and operations on data to manage which team your game's team members are on. +/// Best used as a composite member of your "User" or "Player" class(es). +/// When using with ReplicaManager3, call TM_TeamMember::ReferenceTeamMember() in Replica3::DeserializeConstruction() and TM_TeamMember::DeserializeConstruction() in Replica3::PostDeserializeConstruction() +/// There is otherwise no need to manually serialize the class, as operations are networked internally. +/// \ingroup TEAM_MANAGER_GROUP +class RAK_DLL_EXPORT TM_TeamMember +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(TM_TeamMember) + + TM_TeamMember(); + virtual ~TM_TeamMember(); + + /// \brief Request to join any team, a specific team, or to leave all teams + /// \details Function will return false on invalid operations, such as joining a team you are already on. + /// Will also fail with TeamSelection::JOIN_ANY_AVAILABLE_TEAM if you are currently on a team. + /// On success, every system will get ID_TEAM_BALANCER_TEAM_ASSIGNED. Use TeamManager::DecomposeTeamAssigned() to get details of which team member the message refers to. + /// On failure, all systems will get ID_TEAM_BALANCER_REQUESTED_TEAM_FULL or ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED. Use TeamManager::DecomposeTeamFull() and TeamManager::DecomposeTeamLocked() to get details of which team member the message refers to. + /// \note Joining a specific team with this function may result in being on more than one team at once, even if you call the function while locally only on one team. If your game depends on only being on one team at a team, use RequestTeamSwitch() instead with the parameter teamToLeave set to 0 + /// \param[in] TeamSelection::AnyAvailable(), TeamSelection::SpecificTeam(), or TeamSelection::NoTeam() + /// \return false On invalid or unnecessary operation. Otherwise returns true + bool RequestTeam(TeamSelection teamSelection); + + /// \brief Similar to RequestTeam with TeamSelection::SpecificTeam(), but leave a team simultaneously when the desired team is joinable + /// \param[in] teamToJoin Which team to join + /// \param[in] teamToLeave If 0, means leave all current teams. Otherwise, leave the specified team. + /// \return false On invalid or unnecessary operation. Otherwise returns true + bool RequestTeamSwitch(TM_Team *teamToJoin, TM_Team *teamToLeave); + + /// \brief Returns the first requested team in the list of requested teams, if you have a requested team at all. + /// \return TeamSelection::SpecificTeam(), TeamSelection::NoTeam(), or TeamSelection::AnyAvailable() + TeamSelection GetRequestedTeam(void) const; + + /// \brief Returns pending calls to RequestTeam() when using TeamSelection::JOIN_SPECIFIC_TEAM + /// \param[out] All pending requested teams + void GetRequestedSpecificTeams(DataStructures::List &requestedTeams) const; + + /// \brief Returns if the specified team is in the list of pending requested teams + /// \param[in] The team we are checking + /// \return Did we request to join this specific team? + bool HasRequestedTeam(TM_Team *team) const; + + /// \brief Returns the index of \a team in the requested teams list + /// \param[in] The team we are checking + /// \return -1 if we did not requested to join this team. Otherwise the index. + unsigned int GetRequestedTeamIndex(TM_Team *team) const; + + /// \return The number of teams that would be returned by a call to GetRequestedSpecificTeams() + unsigned int GetRequestedTeamCount(void) const; + + /// \brief Cancels a request to join a specific team. + /// \details Useful if you got ID_TEAM_BALANCER_REQUESTED_TEAM_FULL or ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED and changed your mind about joining the team. + /// \note This is not guaranteed to work due to latency. To clarify, If the host switches your team at the same time you call CancelRequestTeam() you may still get ID_TEAM_BALANCER_TEAM_ASSIGNED for the team you tried to cancel. + /// \param[in] specificTeamToCancel Which team to no longer join. Use 0 for all. + /// \return false On invalid or unnecessary operation. Otherwise returns true + bool CancelTeamRequest(TM_Team *specificTeamToCancel); + + /// \brief Leave a team + /// \details Leaves a team that you are on. Always succeeds provided you are on that team + /// Generates ID_TEAM_BALANCER_TEAM_ASSIGNED on all systems on success. + /// If you leave the last team you are on, \a noTeamSubcategory is set as well. + /// \param[in] team Which team to leave + /// \param[in] _noTeamSubcategory If the team member has been removed from all teams, which subcategory of NoTeamId to set them to + /// \return false On invalid or unnecessary operation. Otherwise returns true + bool LeaveTeam(TM_Team* team, NoTeamId _noTeamSubcategory); + + /// \brief Leave all teams + /// \Details Leaves all teams you are on, and sets \a noTeamSubcategory + /// \note This is the same as and just calls RequestTeam(TeamSelection::NoTeam(noTeamSubcategory)); + /// \return false On invalid or unnecessary operation. Otherwise returns true + bool LeaveAllTeams(NoTeamId noTeamSubcategory); + + /// \return Get the first team we are on, or 0 if we are not on a team. + TM_Team* GetCurrentTeam(void) const; + + /// \return How many teams we are on + unsigned int GetCurrentTeamCount(void) const; + + /// \return Returns one of the teams in the current team list, up to GetCurrentTeamCount() + TM_Team* GetCurrentTeamByIndex(unsigned int index); + + /// \param[out] Get all teams we are on, as a list + void GetCurrentTeams(DataStructures::List &_teams) const; + + /// For each team member, when you get ID_TEAM_BALANCER_TEAM_ASSIGNED for that member, the team list is saved. + /// Use this function to get that list, for example to determine which teams we just left or joined + /// \param[out] _teams The previous list of teams we were on + void GetLastTeams(DataStructures::List &_teams) const; + + /// \param[in] The team we are checking + /// \return Are we on this team? + bool IsOnTeam(TM_Team *team) const; + + /// \return The teamMemberID parameter passed to TM_World::ReferenceTeamMember() + NetworkID GetNetworkID(void) const; + + /// \return The TM_World instance that was used when calling TM_World::ReferenceTeamMember() + TM_World* GetTM_World(void) const; + + /// \brief Serializes the current state of this object + /// \details To replicate a TM_TeamMember on another system, first instantiate the object using your own code, or a system such as ReplicaManager3. + /// Next, call SerializeConstruction() from whichever system owns the team member + /// Last, call DeserializeConstruction() on the newly created TM_TeamMember + /// \note You must instantiate and deserialize all TM_Team instances that the team member refers to before calling DesrializeConstruction(). ReplicaManager3::PostSerializeConstruction() and ReplicaManager3::PostDeserializeConstruction() will ensure this. + /// \param[out] constructionBitstream This object serialized to a BitStream + void SerializeConstruction(BitStream *constructionBitstream); + + /// \brief Deserializes the current state of this object + /// \details See SerializeConstruction for more details() + /// \note DeserializeConstruction also calls ReferenceTeamMember on the passed \a teamManager instance, there is no need to do so yourself + /// \param[in] teamManager TeamManager instance + /// \param[in] constructionBitstream This object serialized to a BitStream + bool DeserializeConstruction(TeamManager *teamManager, BitStream *constructionBitstream); + + /// \param[in] o Stores a void* for your own use. If using composition, this is useful to store a pointer to the containing object. + void SetOwner(void *o); + + /// \return Whatever was passed to SetOwner() + void *GetOwner(void) const; + + /// \return If not on a team, returns the current NoTeamId value + NoTeamId GetNoTeamId(void) const; + + /// Return world->GetTeamMemberIndex(this) + unsigned int GetWorldIndex(void) const; + + /// \internal + static unsigned long ToUint32( const NetworkID &g ); + + /// \internal + struct RequestedTeam + { + RakNet::Time whenRequested; + unsigned int requestIndex; + TM_Team *requested; + bool isTeamSwitch; + TM_Team *teamToLeave; + }; + +protected: + NetworkID networkId; + TM_World* world; + // Teams we are a member of. We can be on more than one team, but not on the same team more than once + DataStructures::List teams; + // If teams is empty, which subcategory of noTeam we are on + NoTeamId noTeamSubcategory; + // Teams we have requested to join. Mutually exclusive with teams we are already on. Cannot request the same team more than once. + DataStructures::List teamsRequested; + // If teamsRequested is not empty, we want to join a specific team + // If teamsRequested is empty, then joinTeamType is either JOIN_NO_TEAM or JOIN_ANY_AVAILABLE_TEAM + JoinTeamType joinTeamType; + // Set by StoreLastTeams() + DataStructures::List lastTeams; + RakNet::Time whenJoinAnyRequested; + unsigned int joinAnyRequestIndex; + void *owner; + + // Remove from all requested and current teams. + void UpdateListsToNoTeam(NoTeamId nti); + bool JoinAnyTeamCheck(void) const; + bool JoinSpecificTeamCheck(TM_Team *specificTeamToJoin, bool ignoreRequested) const; + bool SwitchSpecificTeamCheck(TM_Team *teamToJoin, TM_Team *teamToLeave, bool ignoreRequested) const; + bool LeaveTeamCheck(TM_Team *team) const; + void UpdateTeamsRequestedToAny(void); + void UpdateTeamsRequestedToNone(void); + void AddToRequestedTeams(TM_Team *teamToJoin); + void AddToRequestedTeams(TM_Team *teamToJoin, TM_Team *teamToLeave); + bool RemoveFromRequestedTeams(TM_Team *team); + void AddToTeamList(TM_Team *team); + void RemoveFromSpecificTeamInternal(TM_Team *team); + void RemoveFromAllTeamsInternal(void); + void StoreLastTeams(void); + + friend class TM_World; + friend class TM_Team; + friend class TeamManager; +}; + +/// \brief A team, containing a list of TM_TeamMember instances +/// \details Contains lists of TM_TeamMember instances +/// Best used as a composite member of your "Team" or "PlayerList" class(es). +/// When using with ReplicaManager3, call TM_Team::ReferenceTeam() in Replica3::DeserializeConstruction() and TM_Team::DeserializeConstruction() in Replica3::PostDeserializeConstruction() +/// There is otherwise no need to manually serialize the class, as operations are networked internally. +/// \ingroup TEAM_MANAGER_GROUP +class RAK_DLL_EXPORT TM_Team +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(TM_Team) + + TM_Team(); + virtual ~TM_Team(); + + /// \brief Set the maximum number of members that can join this team. + /// Defaults to 65535 + /// Setting the limit lower than the existing number of members kicks members out, and assigns noTeamSubcategory to them if they have no other team to go to + /// Setting the limit higher allows members to join in. If a member has a pending request to join this team, they join automatically and ID_TEAM_BALANCER_TEAM_ASSIGNED will be returned for those members. + /// \param[in] _teamMemberLimit The new limit + /// \param[in] noTeamSubcategory Which noTeamSubcategory to assign to members that now have no team. + /// \return false On invalid or unnecessary operation. Otherwise returns true + bool SetMemberLimit(TeamMemberLimit _teamMemberLimit, NoTeamId noTeamSubcategory); + + /// \return If team balancing is on, the most members that can be on this team that would not either unbalance it or exceed the value passed to SetMemberLimit(). If team balancing is off, the same as GetMemberLimitSetting() + TeamMemberLimit GetMemberLimit(void) const; + + /// \return What was passed to SetMemberLimit() or the default + TeamMemberLimit GetMemberLimitSetting(void) const; + + /// \brief Who can join this team under what conditions, while the team is not full + /// To not allow new joins, pass 0 + /// To allow all new joins under any circumstances, bitwise-OR all permission defines. + /// For an invite-only team, use ALLOW_JOIN_SPECIFIC_TEAM only and only allow the requester to call TM_TeamMember::RequestTeam() upon invitiation through your game code. + /// Defaults to allow all + /// \param[in] _joinPermissions Bitwise combination of ALLOW_JOIN_ANY_AVAILABLE_TEAM, ALLOW_JOIN_SPECIFIC_TEAM, ALLOW_JOIN_REBALANCING + /// \return false On invalid or unnecessary operation. Otherwise returns true + bool SetJoinPermissions(JoinPermissions _joinPermissions); + + /// \return Whatever was passed to SetJoinPermissions(), or the default. + JoinPermissions GetJoinPermissions(void) const; + + /// \brief Removes a member from a team he or she is on + /// \details Identical to teamMember->LeaveTeam(this, noTeamSubcategory); See TeamMember::LeaveTeam() for details. + /// \param[in] teamMember Which team member to remove + /// \param[in] noTeamSubcategory If the team member has been removed from all teams, which subcategory of NoTeamId to set them to + void LeaveTeam(TM_TeamMember* teamMember, NoTeamId noTeamSubcategory); + + /// \return What was passed as the \a applyBalancing parameter TM_World::ReferenceTeam() when this team was added. + bool GetBalancingApplies(void) const; + + /// \param[out] All team members of this team + void GetTeamMembers(DataStructures::List &_teamMembers) const; + + /// \return The number of team members on this team + unsigned int GetTeamMembersCount(void) const; + + /// \return A team member on this team. Members are stored in the order they are added + /// \param[in] index A value between 0 and GetTeamMembersCount() + TM_TeamMember *GetTeamMemberByIndex(unsigned int index) const; + + /// \return The teamID parameter passed to TM_World::ReferenceTeam() + NetworkID GetNetworkID(void) const; + + /// \return The TM_World instance that was used when calling TM_World::ReferenceTeamMember() + TM_World* GetTM_World(void) const; + + /// \brief Used by the host to serialize the initial state of this object to a new system + /// \details On the host, when sending existing objects to a new system, call SerializeConstruction() on each of those objects to serialize creation state. + /// Creating the actual Team and TeamMember objects should be handled by your game code, or a system such as ReplicaManager3 + void SerializeConstruction(BitStream *constructionBitstream); + + /// \brief Used by non-host systems to read the bitStream written by SerializeConstruction() + /// \details On non-host systems, after creating existing objects, call DeserializeConstruction() to read and setup that object + /// Creating the actual Team and TeamMember objects should be handled by your game code, or a system such as ReplicaManager3 + bool DeserializeConstruction(TeamManager *teamManager, BitStream *constructionBitstream); + + /// \param[in] o Stores a void* for your own use. If using composition, this is useful to store a pointer to the containing object. + void SetOwner(void *o); + + /// \return Whatever was passed to SetOwner() + void *GetOwner(void) const; + + /// Return world->GetTeamIndex(this) + unsigned int GetWorldIndex(void) const; + + /// \internal + static unsigned long ToUint32( const NetworkID &g ); + +protected: + NetworkID ID; + TM_World* world; + // Which members are on this team. The same member cannot be on the same team more than once + DataStructures::List teamMembers; + // Permissions on who can join this team + JoinPermissions joinPermissions; + // Whether or not to consider this team when balancing teams + bool balancingApplies; + TeamMemberLimit teamMemberLimit; + void *owner; + + // Remove input from list teamMembers + void RemoveFromTeamMemberList(TM_TeamMember *teamMember); + + // Find the member index that wants to join the indicated team, is only on one team, and wants to leave that team + unsigned int GetMemberWithRequestedSingleTeamSwitch(TM_Team *team); + + + friend class TM_World; + friend class TM_TeamMember; + friend class TeamManager; +}; + +/// \brief Stores a list of teams which may be enforcing a balanced number of members +/// \details Each TM_World instance is independent of other TM_World world instances. This enables you to host multiple games on a single computer. +/// Not currently supported to have the same TM_Team or TM_TeamMember in more than one world at a time, but easily added on request. +/// \ingroup TEAM_MANAGER_GROUP +class TM_World +{ +public: + TM_World(); + virtual ~TM_World(); + + /// \return Returns the plugin that created this TM_World instance + TeamManager *GetTeamManager(void) const; + + /// \brief Add a new system to send team and team member updates to. + /// \param[in] rakNetGUID GUID of the system you are adding. See Packet::rakNetGUID or RakPeerInterface::GetGUIDFromSystemAddress() + void AddParticipant(RakNetGUID rakNetGUID); + + /// \brief Remove a system that was previously added with AddParticipant() + /// \details Systems that disconnect are removed automatically + /// \param[in] rakNetGUID GUID of the system you are removing. See Packet::rakNetGUID or RakPeerInterface::GetGUIDFromSystemAddress() + void RemoveParticipant(RakNetGUID rakNetGUID); + + /// \brief If true, all new connections are added to this world using AddParticipant() + /// \details Defaults to true + /// \param[in] autoAdd Setting to set + void SetAutoManageConnections(bool autoAdd); + + /// Get the participants added with AddParticipant() + /// \param[out] participantList Participants added with AddParticipant(); + void GetParticipantList(DataStructures::List &participantList); + + /// \brief Register a TM_Team object with this system. + /// \details Your game should contain instances of TM_Team, for example by using composition with your game's Team or PlayerList class + /// Tell TeamManager about these instances using ReferenceTeam(). + /// \note The destrutor of TM_Team calls DereferenceTeam() automatically. + /// \param[in] team The instance you are registering + /// \param[in] networkId Identifies this instance. This value is independent of values used by NetworkIDManager. You can use the same value as the object that contains this instance. + /// \param[in] applyBalancing Whether or not to include this team for balancing when calling SetBalanceTeams(). + void ReferenceTeam(TM_Team *team, NetworkID networkId, bool applyBalancing); + + /// \brief Unregisters the associated TM_Team object with this system. + /// Call when a TM_Team instance is no longer needed + /// \param[in] team Which team instance to unregister + /// \param[in] noTeamSubcategory All players on this team are kicked off. If these players then have no team, they are set to this no team category. + void DereferenceTeam(TM_Team *team, NoTeamId noTeamSubcategory); + + /// \return Number of teams uniquely added with ReferenceTeam() + unsigned int GetTeamCount(void) const; + + /// \param[in] index A value between 0 and GetTeamCount() + /// \return Returns whatever was passed to \a team in the function ReferenceTeam() in the order it was called. + TM_Team *GetTeamByIndex(unsigned int index) const; + + /// \param[in] teamId Value passed to ReferenceTeam() + /// \return Returns whatever was passed to \a team in the function ReferenceTeam() with this NetworkID. + TM_Team *GetTeamByNetworkID(NetworkID teamId); + + /// \brief Inverse of GetTeamByIndex() + /// \param[in] team Which taem + /// \return The index of the specified team, or -1 if not found + unsigned int GetTeamIndex(const TM_Team *team) const; + + /// \brief Register a TM_TeamMember object with this system. + /// \details Your game should contain instances of TM_TeamMember, for example by using composition with your game's User or Player classes + /// Tell TeamManager about these instances using ReferenceTeamMember(). + /// \note The destrutor of TM_TeamMember calls DereferenceTeamMember() automatically. + /// \param[in] teamMember The instance you are registering + /// \param[in] networkId Identifies this instance. This value is independent of values used by NetworkIDManager. You can use the same value as the object that contains this instance + void ReferenceTeamMember(TM_TeamMember *teamMember, NetworkID networkId); + + /// \brief Unregisters the associated TM_TeamMember object with this system. + /// Call when a TM_TeamMember instance is no longer needed + /// \note This is called by the destructor of TM_TeamMember automatically, so you do not normally need to call this function + void DereferenceTeamMember(TM_TeamMember *teamMember); + + /// \return Number of team members uniquely added with ReferenceTeamMember() + unsigned int GetTeamMemberCount(void) const; + + /// \param[in] index A value between 0 and GetTeamMemberCount() + /// \return Returns whatever was passed to \a team in the function ReferenceTeamMember() in the order it was called. + TM_TeamMember *GetTeamMemberByIndex(unsigned int index) const; + + /// \param[in] index A value between 0 and GetTeamMemberCount() + /// \return Returns whatever was passed to \a teamMemberID in the function ReferenceTeamMember() in the order it was called. + NetworkID GetTeamMemberIDByIndex(unsigned int index) const; + + /// \param[in] teamId Value passed to ReferenceTeamMember() + /// \return Returns Returns whatever was passed to \a team in the function ReferenceTeamMember() with this NetworkID + TM_TeamMember *GetTeamMemberByNetworkID(NetworkID teamMemberId); + + /// \brief Inverse of GetTeamMemberByIndex() + /// \param[in] team Which team member + /// \return The index of the specified team member, or -1 if not found + unsigned int GetTeamMemberIndex(const TM_TeamMember *teamMember) const; + + /// \brief Force or stop forcing teams to be balanced. + /// \details For each team added with ReferenceTeam() and \a applyBalancing set to true, players on unbalanced teams will be redistributed + /// While active, players can only join balanced teams if doing so would not cause that team to become unbalanced. + /// If a player on the desired team also wants to switch, then both players will switch simultaneously. Otherwise, ID_TEAM_BALANCER_REQUESTED_TEAM_FULL will be returned to the requester and switching will occur when possible. + /// If balanceTeams is true and later set to false, players waiting on ID_TEAM_BALANCER_REQUESTED_TEAM_FULL will be able to join the desired team immediately provided it is not full. + /// \param[in] balanceTeams Whether to activate or deactivate team balancing. + /// \param[in] noTeamSubcategory If a player is kicked off a team and is no longer on any team, his or her noTeamSubcategory is set to this value + bool SetBalanceTeams(bool balanceTeams, NoTeamId noTeamSubcategory); + + /// \return \a balanceTeams parameter of SetBalanceTeams(), or the default + bool GetBalanceTeams(void) const; + + /// \brief Set the host that will perform balancing calculations and send notifications + /// \details Operations that can cause conflicts due to latency, such as joining teams, are operated on by the host. The result is sent to all systems added with AddParticipant() + /// For a client/server game, call SetHost() with the server's RakNetGUID value on all systems (including the server itself). If you call TeamManager::SetTopology(TM_CLIENT_SERVER), the server will also relay messages between participants. + /// For a peer to peer game, call SetHost() on the same peer when host migration occurs. Use TeamManager::SetTopology(TM_PEER_TO_PEER) in this case. + /// \note If using FullyConnectedMesh2, SetHost() is called automatically when ID_FCM2_NEW_HOST is returned. + /// \param[in] _hostGuid The host, which is the system that will serialize and resolve team disputes and calculate team balancing. + void SetHost(RakNetGUID _hostGuid); + + /// \return Returns the current host, or UNASSIGNED_RAKNET_GUID if unknown + RakNetGUID GetHost(void) const; + + /// \return The \a worldId passed to TeamManagr::AddWorld() + WorldId GetWorldId(void) const; + + /// \brief Clear all memory and reset everything. + /// \details It is up to the user to deallocate pointers passed to ReferenceTeamMember() or ReferenceTeam(), if so desired. + void Clear(void); + + /// \internal + struct JoinRequestHelper + { + RakNet::Time whenRequestMade; + unsigned int teamMemberIndex; + unsigned int indexIntoTeamsRequested; + unsigned int requestIndex; + }; + /// \internal + static int JoinRequestHelperComp(const TM_World::JoinRequestHelper &key, const TM_World::JoinRequestHelper &data); + +protected: + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); + + // Teams with too many members have those members go to other teams. + void EnforceTeamBalance(NoTeamId noTeamSubcategory); + void KickExcessMembers(NoTeamId noTeamSubcategory); + void FillRequestedSlots(void); + unsigned int GetAvailableTeamIndexWithFewestMembers(TeamMemberLimit secondaryLimit, JoinPermissions joinPermissions); + + void GetSortedJoinRequests(DataStructures::OrderedList &joinRequests); + + + // Send a message to all participants + void BroadcastToParticipants(RakNet::BitStream *bsOut, RakNetGUID exclusionGuid); + void BroadcastToParticipants(unsigned char *data, const int length, RakNetGUID exclusionGuid); + + // 1. If can join a team: + // A. teamMember->UpdateTeamsRequestedToNone(); + // B. teamMember->AddToTeamList() + // C. Return new team + // 2. Else return 0 + TM_Team* JoinAnyTeam(TM_TeamMember *teamMember, int *resultCode); + + int JoinSpecificTeam(TM_TeamMember *teamMember, TM_Team *team, bool isTeamSwitch, TM_Team *teamToLeave, DataStructures::List &teamsWeAreLeaving); + + TeamMemberLimit GetBalancedTeamLimit(void) const; + + // For fast lookup. Shares pointers with list teams + DataStructures::Hash teamsHash; + // For fast lookup. Shares pointers with list teamMembers + DataStructures::Hash teamMembersHash; + + TeamManager *teamManager; + DataStructures::List participants; + DataStructures::List teams; + DataStructures::List teamMembers; + bool balanceTeamsIsActive; + RakNetGUID hostGuid; + WorldId worldId; + bool autoAddParticipants; + int teamRequestIndex; + + friend class TeamManager; + friend class TM_TeamMember; + friend class TM_Team; +}; + +/// \brief Automates networking and list management for teams +/// \details TeamManager provides support for teams. A team is a list of team members. +/// Teams contain properties including the number of team members per team, whether or not tagged teams must have equal numbers of members, and if a team is locked or not to certain entry conditions +/// Team members contain properties including which teams they are on and which teams they want to join if a team is not immediately joinable +/// Advanced functionality includes the ability for a team member to be on multiple teams simultaneously, the ability to swap teams with other members, and the ability to resize the number of members supported per team +/// The architecture is designed for easy integration with ReplicaManager3 +/// +/// Usage:
      +/// 1. Define your game classes to represent teams and team members. Your game classes should hold game-specific information such as team name and color.
      +/// 2. Have those game classes contain a corresponding TM_Team or TM_TeamMember instance. Operations on teams will be performed by those instances. Use SetOwner() to refer to the parent object when using composition.
      +/// 3. Call TeamManager::SetTopology() for client/server or peer to peer.
      +/// 4. Call AddWorld() to instantiate a TM_World object which will contain references to your TM_TeamMember and TM_Team instances.
      +/// 5. When you instantiate a TM_TeamMember or TM_Team object, call ReferenceTeam() and ReferenceTeamMember() for each corresponding object
      +/// 6. When sending world state to a new connection, for example in ReplicaManager3::SerializeConstruction(), call TM_SerializeConstruction() on the corresponding TM_TeamMember and TM_Team objects. TM_Team instances on the new connection must be created before TM_TeamMember instances.
      +/// 7. Call TM_DeserializeConstruction() on your new corresponding TM_TeamMember and TM_Team instances.
      +/// 8. Execute team operations. ID_TEAM_BALANCER_REQUESTED_TEAM_FULL, ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED, ID_TEAM_BALANCER_TEAM_REQUESTED_CANCELLED, and ID_TEAM_BALANCER_TEAM_ASSIGNED are returned to all systems when the corresponding event occurs for a team member.
      +/// 9. As the peer to peer session host changes, call SetHost() (Not necessary if using FullyConnectedMesh2). If using client/server, you must set the host
      +/// \note This replaces TeamBalancer. You cannot use TeamBalancer and TeamManager at the same time. +/// \ingroup TEAM_MANAGER_GROUP +class RAK_DLL_EXPORT TeamManager : public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(TeamManager) + + TeamManager(); + virtual ~TeamManager(); + + /// \brief Allocate a world to hold a list of teams and players for that team. + /// Use the returned TM_World object for actual team functionality. + /// \note The world is tracked by TeamManager and deallocated by calling Clear() + /// \param[in] worldId Arbitrary user-defined id of the world to create. Each world instance must have a unique id. + TM_World* AddWorld(WorldId worldId); + + /// \brief Deallocate a world created with AddWorld() + /// \param[in] worldId The world to deallocate + void RemoveWorld(WorldId worldId); + + /// \return Returns the number of worlds created with AddWorld() + unsigned int GetWorldCount(void) const; + + /// \param[in] index A value beteween 0 and GetWorldCount()-1 inclusive. + /// \return Returns a world created with AddWorld() + TM_World* GetWorldAtIndex(unsigned int index) const; + + /// \param[in] worldId \a worldId value passed to AddWorld() + /// \return Returns a world created with AddWorld(), or 0 if no such \a worldId + TM_World* GetWorldWithId(WorldId worldId) const; + + /// \brief When auto managing connections, call TM_World::AddParticipant() on all worlds for all new connections automatically + /// Defaults to true + /// \note You probably want this set to false if using multiple worlds + /// \param[in] autoAdd Automatically call TM_World::AddParticipant() all worlds each new connection. Defaults to true. + void SetAutoManageConnections(bool autoAdd); + + /// \brief If \a _topology is set to TM_CLIENT_SERVER, the host will relay messages to participants. + /// \details If topology is set to TM_PEER_TO_PEER, the host assumes the original message source was connected to all other participants and does not relay messages. + /// \note If TM_PEER_TO_PEER, this plugin will listen for ID_FCM2_NEW_HOST and call SetHost() on all worlds automatically + /// \note Defaults to TM_PEER_TO_PEER + /// \param[in] _topology Topology to use + void SetTopology(TMTopology _topology); + + /// \brief When you get ID_TEAM_BALANCER_REQUESTED_TEAM_FULL, pass the packet to this function to read out parameters + /// \param[in] A packet where packet->data[0]==ID_TEAM_BALANCER_REQUESTED_TEAM_FULL + /// \return true on success, false on read error + void DecomposeTeamFull(Packet *packet, + TM_World **world, TM_TeamMember **teamMember, TM_Team **team, + uint16_t ¤tMembers, uint16_t &memberLimitIncludingBalancing, bool &balancingIsActive, JoinPermissions &joinPermissions); + + /// \brief When you get ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED, pass the packet to this function to read out parameters + /// \param[in] A packet where packet->data[0]==ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED + /// \return true on success, false on read error + void DecomposeTeamLocked(Packet *packet, + TM_World **world, TM_TeamMember **teamMember, TM_Team **team, + uint16_t ¤tMembers, uint16_t &memberLimitIncludingBalancing, bool &balancingIsActive, JoinPermissions &joinPermissions); + + /// \brief Clear all memory and reset everything. + /// \details Deallocates TM_World instances. It is up to the user to deallocate pointers passed to ReferenceTeamMember() or ReferenceTeam(), if so desired. + void Clear(void); + + /// \brief Reads out the world and teamMember from ID_TEAM_BALANCER_TEAM_ASSIGNED + /// \note You can get the current and prior team list from the teamMember itself + /// \param[in] A packet where packet->data[0]==ID_TEAM_BALANCER_TEAM_ASSIGNED + /// \param[out] world Set to the world this \a teamMember is on. 0 on bad lookup. + /// \param[out] teamMember Set to the teamMember affected. 0 on bad lookup. + void DecodeTeamAssigned(Packet *packet, TM_World **world, TM_TeamMember **teamMember); + + // \brief Reads out the world and teamMember from ID_TEAM_BALANCER_TEAM_REQUESTED_CANCELLED + /// \note You can get the requested team list from the teamMember itself + /// \param[in] A packet where packet->data[0]==ID_TEAM_BALANCER_TEAM_REQUESTED_CANCELLED + /// \param[out] world Set to the world this \a teamMember is on. 0 on bad lookup. + /// \param[out] teamMember Set to the teamMember affected. 0 on bad lookup. + /// \param[out] teamCancelled Set to the team that was cancelled. 0 for all teams. + void DecodeTeamCancelled(Packet *packet, TM_World **world, TM_TeamMember **teamMember, TM_Team **teamCancelled); + +protected: + + virtual void Update(void); + virtual PluginReceiveResult OnReceive(Packet *packet); + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming); + void Send( const RakNet::BitStream * bitStream, const AddressOrGUID systemIdentifier, bool broadcast ); + + void EncodeTeamFullOrLocked(RakNet::BitStream *bitStream, TM_TeamMember *teamMember, TM_Team *team); + void DecomposeTeamFullOrLocked(RakNet::BitStream *bsIn, TM_World **world, TM_TeamMember **teamMember, TM_Team **team, + uint16_t ¤tMembers, uint16_t &memberLimitIncludingBalancing, bool &balancingIsActive, JoinPermissions &joinPermissions); + void ProcessTeamAssigned(RakNet::BitStream *bsIn); + + void EncodeTeamAssigned(RakNet::BitStream *bitStream, TM_TeamMember *teamMember); + void RemoveFromTeamsRequestedAndAddTeam(TM_TeamMember *teamMember, TM_Team *team, bool isTeamSwitch, TM_Team *teamToLeave); + + void PushTeamAssigned(TM_TeamMember *teamMember); + void PushBitStream(RakNet::BitStream *bitStream); + void OnUpdateListsToNoTeam(Packet *packet, TM_World *world); + void OnUpdateTeamsRequestedToAny(Packet *packet, TM_World *world); + void OnJoinAnyTeam(Packet *packet, TM_World *world); + void OnJoinRequestedTeam(Packet *packet, TM_World *world); + void OnUpdateTeamsRequestedToNoneAndAddTeam(Packet *packet, TM_World *world); + void OnRemoveFromTeamsRequestedAndAddTeam(Packet *packet, TM_World *world); + void OnAddToRequestedTeams(Packet *packet, TM_World *world); + bool OnRemoveFromRequestedTeams(Packet *packet, TM_World *world); + void OnLeaveTeam(Packet *packet, TM_World *world); + void OnSetMemberLimit(Packet *packet, TM_World *world); + void OnSetJoinPermissions(Packet *packet, TM_World *world); + void OnSetBalanceTeams(Packet *packet, TM_World *world); + void OnSetBalanceTeamsInitial(Packet *packet, TM_World *world); + + + void EncodeTeamFull(RakNet::BitStream *bitStream, TM_TeamMember *teamMember, TM_Team *team); + void EncodeTeamLocked(RakNet::BitStream *bitStream, TM_TeamMember *teamMember, TM_Team *team); + + /// \brief When you get ID_TEAM_BALANCER_TEAM_ASSIGNED, pass the packet to this function to read out parameters + /// \param[in] A packet where packet->data[0]==ID_TEAM_BALANCER_TEAM_ASSIGNED + /// \return true on success, false on read error + void DecodeTeamAssigned(RakNet::BitStream *bsIn, TM_World **world, TM_TeamMember **teamMember, NoTeamId &noTeamSubcategory, + JoinTeamType &joinTeamType, DataStructures::List &newTeam, + DataStructures::List &teamsLeft, DataStructures::List &teamsJoined); + + // O(1) lookup for a given world. If I need more worlds, change this to a hash or ordered list + TM_World *worldsArray[255]; + // All allocated worlds for linear traversal + DataStructures::List worldsList; + bool autoAddParticipants; + TMTopology topology; + + friend class TM_TeamMember; + friend class TM_World; + friend class TM_Team; +}; + +} // namespace RakNet + +#endif // __TEAM_MANAGER_H + diff --git a/Source/TelnetTransport.h b/Source/TelnetTransport.h index ea19e763f..740782980 100644 --- a/Source/TelnetTransport.h +++ b/Source/TelnetTransport.h @@ -1,80 +1,78 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Contains TelnetTransport , used to supports the telnet transport protocol. Insecure -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_TelnetTransport==1 && _RAKNET_SUPPORT_TCPInterface==1 - -#ifndef __TELNET_TRANSPORT -#define __TELNET_TRANSPORT - -#include "TransportInterface.h" -#include "DS_List.h" -#include "Export.h" - -namespace RakNet -{ -/// Forward declarations -class TCPInterface; -struct TelnetClient; - -/// \brief Use TelnetTransport to easily allow windows telnet to connect to your ConsoleServer -/// \details To run Windows telnet, go to your start menu, click run, and in the edit box type "telnet " where is the ip address.
      -/// of your ConsoleServer (most likely the same IP as your game).
      -/// This implementation always echos commands. -class RAK_DLL_EXPORT TelnetTransport : public TransportInterface -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(TelnetTransport) - - TelnetTransport(); - virtual ~TelnetTransport(); - bool Start(unsigned short port, bool serverMode); - void Stop(void); - void Send( SystemAddress systemAddress, const char *data, ... ); - void CloseConnection( SystemAddress systemAddress ); - Packet* Receive( void ); - void DeallocatePacket( Packet *packet ); - SystemAddress HasNewIncomingConnection(void); - SystemAddress HasLostConnection(void); - CommandParserInterface* GetCommandParser(void); - void SetSendSuffix(const char *suffix); - void SetSendPrefix(const char *prefix); -protected: - - struct TelnetClient - { - SystemAddress systemAddress; - char textInput[REMOTE_MAX_TEXT_INPUT]; - char lastSentTextInput[REMOTE_MAX_TEXT_INPUT]; - unsigned cursorPosition; - }; - - TCPInterface *tcpInterface; - void AutoAllocate(void); - bool ReassembleLine(TelnetTransport::TelnetClient* telnetClient, unsigned char c); - - // Crap this sucks but because windows telnet won't send line at a time, I have to reconstruct the lines at the server per player - DataStructures::List remoteClients; - - char *sendSuffix, *sendPrefix; - -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Contains TelnetTransport , used to supports the telnet transport protocol. Insecure +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_TelnetTransport==1 && _RAKNET_SUPPORT_TCPInterface==1 + +#pragma once + +#include "TransportInterface.h" +#include "DS_List.h" +#include "Export.h" + +namespace RakNet +{ +/// Forward declarations +class TCPInterface; +struct TelnetClient; + +/// \brief Use TelnetTransport to easily allow windows telnet to connect to your ConsoleServer +/// \details To run Windows telnet, go to your start menu, click run, and in the edit box type "telnet " where is the ip address.
      +/// of your ConsoleServer (most likely the same IP as your game).
      +/// This implementation always echos commands. +class RAK_DLL_EXPORT TelnetTransport : public TransportInterface +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(TelnetTransport) + + TelnetTransport(); + virtual ~TelnetTransport(); + bool Start(unsigned short port, bool serverMode); + void Stop(void); + void Send( SystemAddress systemAddress, const char *data, ... ); + void CloseConnection( SystemAddress systemAddress ); + Packet* Receive( void ); + void DeallocatePacket( Packet *packet ); + SystemAddress HasNewIncomingConnection(void); + SystemAddress HasLostConnection(void); + CommandParserInterface* GetCommandParser(void); + void SetSendSuffix(const char *suffix); + void SetSendPrefix(const char *prefix); +protected: + + struct TelnetClient + { + SystemAddress systemAddress; + char textInput[REMOTE_MAX_TEXT_INPUT]; + char lastSentTextInput[REMOTE_MAX_TEXT_INPUT]; + unsigned cursorPosition; + }; + + TCPInterface *tcpInterface; + void AutoAllocate(void); + bool ReassembleLine(TelnetTransport::TelnetClient* telnetClient, unsigned char c); + + // Crap this sucks but because windows telnet won't send line at a time, I have to reconstruct the lines at the server per player + DataStructures::List remoteClients; + + char *sendSuffix, *sendPrefix; + +}; + +} // namespace RakNet + +#endif + diff --git a/Source/ThreadPool.h b/Source/ThreadPool.h index 2f94a9d98..5d606a368 100644 --- a/Source/ThreadPool.h +++ b/Source/ThreadPool.h @@ -1,633 +1,630 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __THREAD_POOL_H -#define __THREAD_POOL_H - -#include "RakMemoryOverride.h" -#include "DS_Queue.h" -#include "SimpleMutex.h" -#include "Export.h" -#include "RakThread.h" -#include "SignaledEvent.h" - -#ifdef _MSC_VER -#pragma warning( push ) -#endif - -class ThreadDataInterface -{ -public: - ThreadDataInterface() {} - virtual ~ThreadDataInterface() {} - - virtual void* PerThreadFactory(void *context)=0; - virtual void PerThreadDestructor(void* factoryResult, void *context)=0; -}; -/// A simple class to create worker threads that processes a queue of functions with data. -/// This class does not allocate or deallocate memory. It is up to the user to handle memory management. -/// InputType and OutputType are stored directly in a queue. For large structures, if you plan to delete from the middle of the queue, -/// you might wish to store pointers rather than the structures themselves so the array can shift efficiently. -template -struct RAK_DLL_EXPORT ThreadPool -{ - ThreadPool(); - ~ThreadPool(); - - /// Start the specified number of threads. - /// \param[in] numThreads The number of threads to start - /// \param[in] stackSize 0 for default (except on consoles). - /// \param[in] _perThreadInit User callback to return data stored per thread. Pass 0 if not needed. - /// \param[in] _perThreadDeinit User callback to destroy data stored per thread, created by _perThreadInit. Pass 0 if not needed. - /// \return True on success, false on failure. - bool StartThreads(int numThreads, int stackSize, void* (*_perThreadInit)()=0, void (*_perThreadDeinit)(void*)=0); - - // Alternate form of _perThreadDataFactory, _perThreadDataDestructor - void SetThreadDataInterface(ThreadDataInterface *tdi, void *context); - - /// Stops all threads - void StopThreads(void); - - /// Adds a function to a queue with data to pass to that function. This function will be called from the thread - /// Memory management is your responsibility! This class does not allocate or deallocate memory. - /// The best way to deallocate \a inputData is in userCallback. If you call EndThreads such that callbacks were not called, you - /// can iterate through the inputQueue and deallocate all pending input data there - /// The best way to deallocate output is as it is returned to you from GetOutput. Similarly, if you end the threads such that - /// not all output was returned, you can iterate through outputQueue and deallocate it there. - /// \param[in] workerThreadCallback The function to call from the thread - /// \param[in] inputData The parameter to pass to \a userCallback - void AddInput(OutputType (*workerThreadCallback)(InputType, bool *returnOutput, void* perThreadData), InputType inputData); - - /// Adds to the output queue - /// Use it if you want to inject output into the same queue that the system uses. Normally you would not use this. Consider it a convenience function. - /// \param[in] outputData The output to inject - void AddOutput(OutputType outputData); - - /// Returns true if output from GetOutput is waiting. - /// \return true if output is waiting, false otherwise - bool HasOutput(void); - - /// Inaccurate but fast version of HasOutput. If this returns true, you should still check HasOutput for the real value. - /// \return true if output is probably waiting, false otherwise - bool HasOutputFast(void); - - /// Returns true if input from GetInput is waiting. - /// \return true if input is waiting, false otherwise - bool HasInput(void); - - /// Inaccurate but fast version of HasInput. If this returns true, you should still check HasInput for the real value. - /// \return true if input is probably waiting, false otherwise - bool HasInputFast(void); - - /// Gets the output of a call to \a userCallback - /// HasOutput must return true before you call this function. Otherwise it will assert. - /// \return The output of \a userCallback. If you have different output signatures, it is up to you to encode the data to indicate this - OutputType GetOutput(void); - - /// Clears internal buffers - void Clear(void); - - /// Lock the input buffer before calling the functions InputSize, InputAtIndex, and RemoveInputAtIndex - /// It is only necessary to lock the input or output while the threads are running - void LockInput(void); - - /// Unlock the input buffer after you are done with the functions InputSize, GetInputAtIndex, and RemoveInputAtIndex - void UnlockInput(void); - - /// Length of the input queue - unsigned InputSize(void); - - /// Get the input at a specified index - InputType GetInputAtIndex(unsigned index); - - /// Remove input from a specific index. This does NOT do memory deallocation - it only removes the item from the queue - void RemoveInputAtIndex(unsigned index); - - /// Lock the output buffer before calling the functions OutputSize, OutputAtIndex, and RemoveOutputAtIndex - /// It is only necessary to lock the input or output while the threads are running - void LockOutput(void); - - /// Unlock the output buffer after you are done with the functions OutputSize, GetOutputAtIndex, and RemoveOutputAtIndex - void UnlockOutput(void); - - /// Length of the output queue - unsigned OutputSize(void); - - /// Get the output at a specified index - OutputType GetOutputAtIndex(unsigned index); - - /// Remove output from a specific index. This does NOT do memory deallocation - it only removes the item from the queue - void RemoveOutputAtIndex(unsigned index); - - /// Removes all items from the input queue - void ClearInput(void); - - /// Removes all items from the output queue - void ClearOutput(void); - - /// Are any of the threads working, or is input or output available? - bool IsWorking(void); - - /// The number of currently active threads. - int NumThreadsWorking(void); - - /// Did we call Start? - bool WasStarted(void); - - // Block until all threads are stopped. - bool Pause(void); - - // Continue running - void Resume(void); - -protected: - // It is valid to cancel input before it is processed. To do so, lock the inputQueue with inputQueueMutex, - // Scan the list, and remove the item you don't want. - RakNet::SimpleMutex inputQueueMutex, outputQueueMutex, workingThreadCountMutex, runThreadsMutex; - - void* (*perThreadDataFactory)(); - void (*perThreadDataDestructor)(void*); - - // inputFunctionQueue & inputQueue are paired arrays so if you delete from one at a particular index you must delete from the other - // at the same index - DataStructures::Queue inputFunctionQueue; - DataStructures::Queue inputQueue; - DataStructures::Queue outputQueue; - - ThreadDataInterface *threadDataInterface; - void *tdiContext; - - - template - friend RAK_THREAD_DECLARATION(WorkerThread); - - /* -#ifdef _WIN32 - friend unsigned __stdcall WorkerThread( LPVOID arguments ); -#else - friend void* WorkerThread( void* arguments ); -#endif - */ - - /// \internal - bool runThreads; - /// \internal - int numThreadsRunning; - /// \internal - int numThreadsWorking; - /// \internal - RakNet::SimpleMutex numThreadsRunningMutex; - - RakNet::SignaledEvent quitAndIncomingDataEvents; - -// #if defined(SN_TARGET_PSP2) -// RakNet::RakThread::UltUlThreadRuntime *runtime; -// #endif -}; - -#include "ThreadPool.h" -#include "RakSleep.h" -#ifdef _WIN32 - -#else -#include -#endif - -#ifdef _MSC_VER -#pragma warning(disable:4127) -#pragma warning( disable : 4701 ) // potentially uninitialized local variable 'inputData' used -#endif - -template -RAK_THREAD_DECLARATION(WorkerThread) -/* -#ifdef _WIN32 -unsigned __stdcall WorkerThread( LPVOID arguments ) -#else -void* WorkerThread( void* arguments ) -#endif -*/ -{ - - - - ThreadPool *threadPool = (ThreadPool*) arguments; - - - bool returnOutput; - ThreadOutputType (*userCallback)(ThreadInputType, bool *, void*); - ThreadInputType inputData; - ThreadOutputType callbackOutput; - - userCallback=0; - - void *perThreadData; - if (threadPool->perThreadDataFactory) - perThreadData=threadPool->perThreadDataFactory(); - else if (threadPool->threadDataInterface) - perThreadData=threadPool->threadDataInterface->PerThreadFactory(threadPool->tdiContext); - else - perThreadData=0; - - // Increase numThreadsRunning - threadPool->numThreadsRunningMutex.Lock(); - ++threadPool->numThreadsRunning; - threadPool->numThreadsRunningMutex.Unlock(); - - while (1) - { -//#ifdef _WIN32 - if (userCallback==0) - { - threadPool->quitAndIncomingDataEvents.WaitOnEvent(1000); - } -// #else -// if (userCallback==0) -// RakSleep(30); -// #endif - - threadPool->runThreadsMutex.Lock(); - if (threadPool->runThreads==false) - { - threadPool->runThreadsMutex.Unlock(); - break; - } - threadPool->runThreadsMutex.Unlock(); - - threadPool->workingThreadCountMutex.Lock(); - ++threadPool->numThreadsWorking; - threadPool->workingThreadCountMutex.Unlock(); - - // Read input data - userCallback=0; - threadPool->inputQueueMutex.Lock(); - if (threadPool->inputFunctionQueue.Size()) - { - userCallback=threadPool->inputFunctionQueue.Pop(); - inputData=threadPool->inputQueue.Pop(); - } - threadPool->inputQueueMutex.Unlock(); - - if (userCallback) - { - callbackOutput=userCallback(inputData, &returnOutput,perThreadData); - if (returnOutput) - { - threadPool->outputQueueMutex.Lock(); - threadPool->outputQueue.Push(callbackOutput, _FILE_AND_LINE_ ); - threadPool->outputQueueMutex.Unlock(); - } - } - - threadPool->workingThreadCountMutex.Lock(); - --threadPool->numThreadsWorking; - threadPool->workingThreadCountMutex.Unlock(); - } - - // Decrease numThreadsRunning - threadPool->numThreadsRunningMutex.Lock(); - --threadPool->numThreadsRunning; - threadPool->numThreadsRunningMutex.Unlock(); - - if (threadPool->perThreadDataDestructor) - threadPool->perThreadDataDestructor(perThreadData); - else if (threadPool->threadDataInterface) - threadPool->threadDataInterface->PerThreadDestructor(perThreadData, threadPool->tdiContext); - - - - - return 0; - -} -template -ThreadPool::ThreadPool() -{ - runThreads=false; - numThreadsRunning=0; - threadDataInterface=0; - tdiContext=0; - numThreadsWorking=0; - -} -template -ThreadPool::~ThreadPool() -{ - StopThreads(); - Clear(); -} -template -bool ThreadPool::StartThreads(int numThreads, int stackSize, void* (*_perThreadDataFactory)(), void (*_perThreadDataDestructor)(void *)) -{ - (void) stackSize; - -// #if defined(SN_TARGET_PSP2) -// runtime = RakNet::RakThread::AllocRuntime(numThreads); -// #endif - - runThreadsMutex.Lock(); - if (runThreads==true) - { - // Already running - runThreadsMutex.Unlock(); - return false; - } - runThreadsMutex.Unlock(); - - quitAndIncomingDataEvents.InitEvent(); - - perThreadDataFactory=_perThreadDataFactory; - perThreadDataDestructor=_perThreadDataDestructor; - - runThreadsMutex.Lock(); - runThreads=true; - runThreadsMutex.Unlock(); - - numThreadsWorking=0; - unsigned threadId = 0; - (void) threadId; - int i; - for (i=0; i < numThreads; i++) - { - int errorCode; - - - - - errorCode = RakNet::RakThread::Create(WorkerThread, this); - - if (errorCode!=0) - { - StopThreads(); - return false; - } - } - // Wait for number of threads running to increase to numThreads - bool done=false; - while (done==false) - { - RakSleep(50); - numThreadsRunningMutex.Lock(); - if (numThreadsRunning==numThreads) - done=true; - numThreadsRunningMutex.Unlock(); - } - - return true; -} -template -void ThreadPool::SetThreadDataInterface(ThreadDataInterface *tdi, void *context) -{ - threadDataInterface=tdi; - tdiContext=context; -} -template -void ThreadPool::StopThreads(void) -{ - runThreadsMutex.Lock(); - if (runThreads==false) - { - runThreadsMutex.Unlock(); - return; - } - - runThreads=false; - runThreadsMutex.Unlock(); - - // Wait for number of threads running to decrease to 0 - bool done=false; - while (done==false) - { - quitAndIncomingDataEvents.SetEvent(); - - RakSleep(50); - numThreadsRunningMutex.Lock(); - if (numThreadsRunning==0) - done=true; - numThreadsRunningMutex.Unlock(); - } - - quitAndIncomingDataEvents.CloseEvent(); - -// #if defined(SN_TARGET_PSP2) -// RakNet::RakThread::DeallocRuntime(runtime); -// runtime=0; -// #endif - -} -template -void ThreadPool::AddInput(OutputType (*workerThreadCallback)(InputType, bool *returnOutput, void* perThreadData), InputType inputData) -{ - inputQueueMutex.Lock(); - inputQueue.Push(inputData, _FILE_AND_LINE_ ); - inputFunctionQueue.Push(workerThreadCallback, _FILE_AND_LINE_ ); - inputQueueMutex.Unlock(); - - quitAndIncomingDataEvents.SetEvent(); -} -template -void ThreadPool::AddOutput(OutputType outputData) -{ - outputQueueMutex.Lock(); - outputQueue.Push(outputData, _FILE_AND_LINE_ ); - outputQueueMutex.Unlock(); -} -template -bool ThreadPool::HasOutputFast(void) -{ - return outputQueue.IsEmpty()==false; -} -template -bool ThreadPool::HasOutput(void) -{ - bool res; - outputQueueMutex.Lock(); - res=outputQueue.IsEmpty()==false; - outputQueueMutex.Unlock(); - return res; -} -template -bool ThreadPool::HasInputFast(void) -{ - return inputQueue.IsEmpty()==false; -} -template -bool ThreadPool::HasInput(void) -{ - bool res; - inputQueueMutex.Lock(); - res=inputQueue.IsEmpty()==false; - inputQueueMutex.Unlock(); - return res; -} -template -OutputType ThreadPool::GetOutput(void) -{ - // Real output check - OutputType output; - outputQueueMutex.Lock(); - output=outputQueue.Pop(); - outputQueueMutex.Unlock(); - return output; -} -template -void ThreadPool::Clear(void) -{ - runThreadsMutex.Lock(); - if (runThreads) - { - runThreadsMutex.Unlock(); - inputQueueMutex.Lock(); - inputFunctionQueue.Clear(_FILE_AND_LINE_); - inputQueue.Clear(_FILE_AND_LINE_); - inputQueueMutex.Unlock(); - - outputQueueMutex.Lock(); - outputQueue.Clear(_FILE_AND_LINE_); - outputQueueMutex.Unlock(); - } - else - { - inputFunctionQueue.Clear(_FILE_AND_LINE_); - inputQueue.Clear(_FILE_AND_LINE_); - outputQueue.Clear(_FILE_AND_LINE_); - } -} -template -void ThreadPool::LockInput(void) -{ - inputQueueMutex.Lock(); -} -template -void ThreadPool::UnlockInput(void) -{ - inputQueueMutex.Unlock(); -} -template -unsigned ThreadPool::InputSize(void) -{ - return inputQueue.Size(); -} -template -InputType ThreadPool::GetInputAtIndex(unsigned index) -{ - return inputQueue[index]; -} -template -void ThreadPool::RemoveInputAtIndex(unsigned index) -{ - inputQueue.RemoveAtIndex(index); - inputFunctionQueue.RemoveAtIndex(index); -} -template -void ThreadPool::LockOutput(void) -{ - outputQueueMutex.Lock(); -} -template -void ThreadPool::UnlockOutput(void) -{ - outputQueueMutex.Unlock(); -} -template -unsigned ThreadPool::OutputSize(void) -{ - return outputQueue.Size(); -} -template -OutputType ThreadPool::GetOutputAtIndex(unsigned index) -{ - return outputQueue[index]; -} -template -void ThreadPool::RemoveOutputAtIndex(unsigned index) -{ - outputQueue.RemoveAtIndex(index); -} -template -void ThreadPool::ClearInput(void) -{ - inputQueue.Clear(_FILE_AND_LINE_); - inputFunctionQueue.Clear(_FILE_AND_LINE_); -} - -template -void ThreadPool::ClearOutput(void) -{ - outputQueue.Clear(_FILE_AND_LINE_); -} -template -bool ThreadPool::IsWorking(void) -{ - bool isWorking; -// workingThreadCountMutex.Lock(); -// isWorking=numThreadsWorking!=0; -// workingThreadCountMutex.Unlock(); - -// if (isWorking) -// return true; - - // Bug fix: Originally the order of these two was reversed. - // It's possible with the thread timing that working could have been false, then it picks up the data in the other thread, then it checks - // here and sees there is no data. So it thinks the thread is not working when it was. - if (HasOutputFast() && HasOutput()) - return true; - - if (HasInputFast() && HasInput()) - return true; - - // Need to check is working again, in case the thread was between the first and second checks - workingThreadCountMutex.Lock(); - isWorking=numThreadsWorking!=0; - workingThreadCountMutex.Unlock(); - - return isWorking; -} - -template -int ThreadPool::NumThreadsWorking(void) -{ - return numThreadsWorking; -} - -template -bool ThreadPool::WasStarted(void) -{ - bool b; - runThreadsMutex.Lock(); - b = runThreads; - runThreadsMutex.Unlock(); - return b; -} -template -bool ThreadPool::Pause(void) -{ - if (WasStarted()==false) - return false; - - workingThreadCountMutex.Lock(); - while (numThreadsWorking>0) - { - RakSleep(30); - } - return true; -} -template -void ThreadPool::Resume(void) -{ - workingThreadCountMutex.Unlock(); -} - -#ifdef _MSC_VER -#pragma warning( pop ) -#endif - -#endif - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include "RakMemoryOverride.h" +#include "DS_Queue.h" +#include "SimpleMutex.h" +#include "Export.h" +#include "RakThread.h" +#include "SignaledEvent.h" + +#ifdef _MSC_VER +#pragma warning( push ) +#endif + +class ThreadDataInterface +{ +public: + ThreadDataInterface() {} + virtual ~ThreadDataInterface() {} + + virtual void* PerThreadFactory(void *context)=0; + virtual void PerThreadDestructor(void* factoryResult, void *context)=0; +}; +/// A simple class to create worker threads that processes a queue of functions with data. +/// This class does not allocate or deallocate memory. It is up to the user to handle memory management. +/// InputType and OutputType are stored directly in a queue. For large structures, if you plan to delete from the middle of the queue, +/// you might wish to store pointers rather than the structures themselves so the array can shift efficiently. +template +struct RAK_DLL_EXPORT ThreadPool +{ + ThreadPool(); + ~ThreadPool(); + + /// Start the specified number of threads. + /// \param[in] numThreads The number of threads to start + /// \param[in] stackSize 0 for default (except on consoles). + /// \param[in] _perThreadInit User callback to return data stored per thread. Pass 0 if not needed. + /// \param[in] _perThreadDeinit User callback to destroy data stored per thread, created by _perThreadInit. Pass 0 if not needed. + /// \return True on success, false on failure. + bool StartThreads(int numThreads, int stackSize, void* (*_perThreadInit)()=0, void (*_perThreadDeinit)(void*)=0); + + // Alternate form of _perThreadDataFactory, _perThreadDataDestructor + void SetThreadDataInterface(ThreadDataInterface *tdi, void *context); + + /// Stops all threads + void StopThreads(void); + + /// Adds a function to a queue with data to pass to that function. This function will be called from the thread + /// Memory management is your responsibility! This class does not allocate or deallocate memory. + /// The best way to deallocate \a inputData is in userCallback. If you call EndThreads such that callbacks were not called, you + /// can iterate through the inputQueue and deallocate all pending input data there + /// The best way to deallocate output is as it is returned to you from GetOutput. Similarly, if you end the threads such that + /// not all output was returned, you can iterate through outputQueue and deallocate it there. + /// \param[in] workerThreadCallback The function to call from the thread + /// \param[in] inputData The parameter to pass to \a userCallback + void AddInput(OutputType (*workerThreadCallback)(InputType, bool *returnOutput, void* perThreadData), InputType inputData); + + /// Adds to the output queue + /// Use it if you want to inject output into the same queue that the system uses. Normally you would not use this. Consider it a convenience function. + /// \param[in] outputData The output to inject + void AddOutput(OutputType outputData); + + /// Returns true if output from GetOutput is waiting. + /// \return true if output is waiting, false otherwise + bool HasOutput(void); + + /// Inaccurate but fast version of HasOutput. If this returns true, you should still check HasOutput for the real value. + /// \return true if output is probably waiting, false otherwise + bool HasOutputFast(void); + + /// Returns true if input from GetInput is waiting. + /// \return true if input is waiting, false otherwise + bool HasInput(void); + + /// Inaccurate but fast version of HasInput. If this returns true, you should still check HasInput for the real value. + /// \return true if input is probably waiting, false otherwise + bool HasInputFast(void); + + /// Gets the output of a call to \a userCallback + /// HasOutput must return true before you call this function. Otherwise it will assert. + /// \return The output of \a userCallback. If you have different output signatures, it is up to you to encode the data to indicate this + OutputType GetOutput(void); + + /// Clears internal buffers + void Clear(void); + + /// Lock the input buffer before calling the functions InputSize, InputAtIndex, and RemoveInputAtIndex + /// It is only necessary to lock the input or output while the threads are running + void LockInput(void); + + /// Unlock the input buffer after you are done with the functions InputSize, GetInputAtIndex, and RemoveInputAtIndex + void UnlockInput(void); + + /// Length of the input queue + unsigned InputSize(void); + + /// Get the input at a specified index + InputType GetInputAtIndex(unsigned index); + + /// Remove input from a specific index. This does NOT do memory deallocation - it only removes the item from the queue + void RemoveInputAtIndex(unsigned index); + + /// Lock the output buffer before calling the functions OutputSize, OutputAtIndex, and RemoveOutputAtIndex + /// It is only necessary to lock the input or output while the threads are running + void LockOutput(void); + + /// Unlock the output buffer after you are done with the functions OutputSize, GetOutputAtIndex, and RemoveOutputAtIndex + void UnlockOutput(void); + + /// Length of the output queue + unsigned OutputSize(void); + + /// Get the output at a specified index + OutputType GetOutputAtIndex(unsigned index); + + /// Remove output from a specific index. This does NOT do memory deallocation - it only removes the item from the queue + void RemoveOutputAtIndex(unsigned index); + + /// Removes all items from the input queue + void ClearInput(void); + + /// Removes all items from the output queue + void ClearOutput(void); + + /// Are any of the threads working, or is input or output available? + bool IsWorking(void); + + /// The number of currently active threads. + int NumThreadsWorking(void); + + /// Did we call Start? + bool WasStarted(void); + + // Block until all threads are stopped. + bool Pause(void); + + // Continue running + void Resume(void); + +protected: + // It is valid to cancel input before it is processed. To do so, lock the inputQueue with inputQueueMutex, + // Scan the list, and remove the item you don't want. + RakNet::SimpleMutex inputQueueMutex, outputQueueMutex, workingThreadCountMutex, runThreadsMutex; + + void* (*perThreadDataFactory)(); + void (*perThreadDataDestructor)(void*); + + // inputFunctionQueue & inputQueue are paired arrays so if you delete from one at a particular index you must delete from the other + // at the same index + DataStructures::Queue inputFunctionQueue; + DataStructures::Queue inputQueue; + DataStructures::Queue outputQueue; + + ThreadDataInterface *threadDataInterface; + void *tdiContext; + + + template + friend RAK_THREAD_DECLARATION(WorkerThread); + + /* +#ifdef _WIN32 + friend unsigned __stdcall WorkerThread( LPVOID arguments ); +#else + friend void* WorkerThread( void* arguments ); +#endif + */ + + /// \internal + bool runThreads; + /// \internal + int numThreadsRunning; + /// \internal + int numThreadsWorking; + /// \internal + RakNet::SimpleMutex numThreadsRunningMutex; + + RakNet::SignaledEvent quitAndIncomingDataEvents; + +// #if defined(SN_TARGET_PSP2) +// RakNet::RakThread::UltUlThreadRuntime *runtime; +// #endif +}; + +#include "ThreadPool.h" +#include "RakSleep.h" +#ifdef _WIN32 + +#else +#include +#endif + +#ifdef _MSC_VER +#pragma warning(disable:4127) +#pragma warning( disable : 4701 ) // potentially uninitialized local variable 'inputData' used +#endif + +template +RAK_THREAD_DECLARATION(WorkerThread) +/* +#ifdef _WIN32 +unsigned __stdcall WorkerThread( LPVOID arguments ) +#else +void* WorkerThread( void* arguments ) +#endif +*/ +{ + + + + ThreadPool *threadPool = (ThreadPool*) arguments; + + + bool returnOutput; + ThreadOutputType (*userCallback)(ThreadInputType, bool *, void*); + ThreadInputType inputData; + ThreadOutputType callbackOutput; + + userCallback=0; + + void *perThreadData; + if (threadPool->perThreadDataFactory) + perThreadData=threadPool->perThreadDataFactory(); + else if (threadPool->threadDataInterface) + perThreadData=threadPool->threadDataInterface->PerThreadFactory(threadPool->tdiContext); + else + perThreadData=0; + + // Increase numThreadsRunning + threadPool->numThreadsRunningMutex.Lock(); + ++threadPool->numThreadsRunning; + threadPool->numThreadsRunningMutex.Unlock(); + + while (1) + { +//#ifdef _WIN32 + if (userCallback==0) + { + threadPool->quitAndIncomingDataEvents.WaitOnEvent(1000); + } +// #else +// if (userCallback==0) +// RakSleep(30); +// #endif + + threadPool->runThreadsMutex.Lock(); + if (threadPool->runThreads==false) + { + threadPool->runThreadsMutex.Unlock(); + break; + } + threadPool->runThreadsMutex.Unlock(); + + threadPool->workingThreadCountMutex.Lock(); + ++threadPool->numThreadsWorking; + threadPool->workingThreadCountMutex.Unlock(); + + // Read input data + userCallback=0; + threadPool->inputQueueMutex.Lock(); + if (threadPool->inputFunctionQueue.Size()) + { + userCallback=threadPool->inputFunctionQueue.Pop(); + inputData=threadPool->inputQueue.Pop(); + } + threadPool->inputQueueMutex.Unlock(); + + if (userCallback) + { + callbackOutput=userCallback(inputData, &returnOutput,perThreadData); + if (returnOutput) + { + threadPool->outputQueueMutex.Lock(); + threadPool->outputQueue.Push(callbackOutput, _FILE_AND_LINE_ ); + threadPool->outputQueueMutex.Unlock(); + } + } + + threadPool->workingThreadCountMutex.Lock(); + --threadPool->numThreadsWorking; + threadPool->workingThreadCountMutex.Unlock(); + } + + // Decrease numThreadsRunning + threadPool->numThreadsRunningMutex.Lock(); + --threadPool->numThreadsRunning; + threadPool->numThreadsRunningMutex.Unlock(); + + if (threadPool->perThreadDataDestructor) + threadPool->perThreadDataDestructor(perThreadData); + else if (threadPool->threadDataInterface) + threadPool->threadDataInterface->PerThreadDestructor(perThreadData, threadPool->tdiContext); + + + + + return 0; + +} +template +ThreadPool::ThreadPool() +{ + runThreads=false; + numThreadsRunning=0; + threadDataInterface=0; + tdiContext=0; + numThreadsWorking=0; + +} +template +ThreadPool::~ThreadPool() +{ + StopThreads(); + Clear(); +} +template +bool ThreadPool::StartThreads(int numThreads, int stackSize, void* (*_perThreadDataFactory)(), void (*_perThreadDataDestructor)(void *)) +{ + (void) stackSize; + +// #if defined(SN_TARGET_PSP2) +// runtime = RakNet::RakThread::AllocRuntime(numThreads); +// #endif + + runThreadsMutex.Lock(); + if (runThreads==true) + { + // Already running + runThreadsMutex.Unlock(); + return false; + } + runThreadsMutex.Unlock(); + + quitAndIncomingDataEvents.InitEvent(); + + perThreadDataFactory=_perThreadDataFactory; + perThreadDataDestructor=_perThreadDataDestructor; + + runThreadsMutex.Lock(); + runThreads=true; + runThreadsMutex.Unlock(); + + numThreadsWorking=0; + unsigned threadId = 0; + (void) threadId; + int i; + for (i=0; i < numThreads; i++) + { + int errorCode; + + + + + errorCode = RakNet::RakThread::Create(WorkerThread, this); + + if (errorCode!=0) + { + StopThreads(); + return false; + } + } + // Wait for number of threads running to increase to numThreads + bool done=false; + while (done==false) + { + RakSleep(50); + numThreadsRunningMutex.Lock(); + if (numThreadsRunning==numThreads) + done=true; + numThreadsRunningMutex.Unlock(); + } + + return true; +} +template +void ThreadPool::SetThreadDataInterface(ThreadDataInterface *tdi, void *context) +{ + threadDataInterface=tdi; + tdiContext=context; +} +template +void ThreadPool::StopThreads(void) +{ + runThreadsMutex.Lock(); + if (runThreads==false) + { + runThreadsMutex.Unlock(); + return; + } + + runThreads=false; + runThreadsMutex.Unlock(); + + // Wait for number of threads running to decrease to 0 + bool done=false; + while (done==false) + { + quitAndIncomingDataEvents.SetEvent(); + + RakSleep(50); + numThreadsRunningMutex.Lock(); + if (numThreadsRunning==0) + done=true; + numThreadsRunningMutex.Unlock(); + } + + quitAndIncomingDataEvents.CloseEvent(); + +// #if defined(SN_TARGET_PSP2) +// RakNet::RakThread::DeallocRuntime(runtime); +// runtime=0; +// #endif + +} +template +void ThreadPool::AddInput(OutputType (*workerThreadCallback)(InputType, bool *returnOutput, void* perThreadData), InputType inputData) +{ + inputQueueMutex.Lock(); + inputQueue.Push(inputData, _FILE_AND_LINE_ ); + inputFunctionQueue.Push(workerThreadCallback, _FILE_AND_LINE_ ); + inputQueueMutex.Unlock(); + + quitAndIncomingDataEvents.SetEvent(); +} +template +void ThreadPool::AddOutput(OutputType outputData) +{ + outputQueueMutex.Lock(); + outputQueue.Push(outputData, _FILE_AND_LINE_ ); + outputQueueMutex.Unlock(); +} +template +bool ThreadPool::HasOutputFast(void) +{ + return outputQueue.IsEmpty()==false; +} +template +bool ThreadPool::HasOutput(void) +{ + bool res; + outputQueueMutex.Lock(); + res=outputQueue.IsEmpty()==false; + outputQueueMutex.Unlock(); + return res; +} +template +bool ThreadPool::HasInputFast(void) +{ + return inputQueue.IsEmpty()==false; +} +template +bool ThreadPool::HasInput(void) +{ + bool res; + inputQueueMutex.Lock(); + res=inputQueue.IsEmpty()==false; + inputQueueMutex.Unlock(); + return res; +} +template +OutputType ThreadPool::GetOutput(void) +{ + // Real output check + OutputType output; + outputQueueMutex.Lock(); + output=outputQueue.Pop(); + outputQueueMutex.Unlock(); + return output; +} +template +void ThreadPool::Clear(void) +{ + runThreadsMutex.Lock(); + if (runThreads) + { + runThreadsMutex.Unlock(); + inputQueueMutex.Lock(); + inputFunctionQueue.Clear(_FILE_AND_LINE_); + inputQueue.Clear(_FILE_AND_LINE_); + inputQueueMutex.Unlock(); + + outputQueueMutex.Lock(); + outputQueue.Clear(_FILE_AND_LINE_); + outputQueueMutex.Unlock(); + } + else + { + inputFunctionQueue.Clear(_FILE_AND_LINE_); + inputQueue.Clear(_FILE_AND_LINE_); + outputQueue.Clear(_FILE_AND_LINE_); + } +} +template +void ThreadPool::LockInput(void) +{ + inputQueueMutex.Lock(); +} +template +void ThreadPool::UnlockInput(void) +{ + inputQueueMutex.Unlock(); +} +template +unsigned ThreadPool::InputSize(void) +{ + return inputQueue.Size(); +} +template +InputType ThreadPool::GetInputAtIndex(unsigned index) +{ + return inputQueue[index]; +} +template +void ThreadPool::RemoveInputAtIndex(unsigned index) +{ + inputQueue.RemoveAtIndex(index); + inputFunctionQueue.RemoveAtIndex(index); +} +template +void ThreadPool::LockOutput(void) +{ + outputQueueMutex.Lock(); +} +template +void ThreadPool::UnlockOutput(void) +{ + outputQueueMutex.Unlock(); +} +template +unsigned ThreadPool::OutputSize(void) +{ + return outputQueue.Size(); +} +template +OutputType ThreadPool::GetOutputAtIndex(unsigned index) +{ + return outputQueue[index]; +} +template +void ThreadPool::RemoveOutputAtIndex(unsigned index) +{ + outputQueue.RemoveAtIndex(index); +} +template +void ThreadPool::ClearInput(void) +{ + inputQueue.Clear(_FILE_AND_LINE_); + inputFunctionQueue.Clear(_FILE_AND_LINE_); +} + +template +void ThreadPool::ClearOutput(void) +{ + outputQueue.Clear(_FILE_AND_LINE_); +} +template +bool ThreadPool::IsWorking(void) +{ + bool isWorking; +// workingThreadCountMutex.Lock(); +// isWorking=numThreadsWorking!=0; +// workingThreadCountMutex.Unlock(); + +// if (isWorking) +// return true; + + // Bug fix: Originally the order of these two was reversed. + // It's possible with the thread timing that working could have been false, then it picks up the data in the other thread, then it checks + // here and sees there is no data. So it thinks the thread is not working when it was. + if (HasOutputFast() && HasOutput()) + return true; + + if (HasInputFast() && HasInput()) + return true; + + // Need to check is working again, in case the thread was between the first and second checks + workingThreadCountMutex.Lock(); + isWorking=numThreadsWorking!=0; + workingThreadCountMutex.Unlock(); + + return isWorking; +} + +template +int ThreadPool::NumThreadsWorking(void) +{ + return numThreadsWorking; +} + +template +bool ThreadPool::WasStarted(void) +{ + bool b; + runThreadsMutex.Lock(); + b = runThreads; + runThreadsMutex.Unlock(); + return b; +} +template +bool ThreadPool::Pause(void) +{ + if (WasStarted()==false) + return false; + + workingThreadCountMutex.Lock(); + while (numThreadsWorking>0) + { + RakSleep(30); + } + return true; +} +template +void ThreadPool::Resume(void) +{ + workingThreadCountMutex.Unlock(); +} + +#ifdef _MSC_VER +#pragma warning( pop ) +#endif + diff --git a/Source/ThreadsafePacketLogger.h b/Source/ThreadsafePacketLogger.h index 61fe3074e..de4e58100 100644 --- a/Source/ThreadsafePacketLogger.h +++ b/Source/ThreadsafePacketLogger.h @@ -1,48 +1,46 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Derivation of the packet logger to defer the call to WriteLog until the user thread. -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_PacketLogger==1 - -#ifndef __THREADSAFE_PACKET_LOGGER_H -#define __THREADSAFE_PACKET_LOGGER_H - -#include "PacketLogger.h" -#include "SingleProducerConsumer.h" - -namespace RakNet -{ - -/// \ingroup PACKETLOGGER_GROUP -/// \brief Same as PacketLogger, but writes output in the user thread. -class RAK_DLL_EXPORT ThreadsafePacketLogger : public PacketLogger -{ -public: - ThreadsafePacketLogger(); - virtual ~ThreadsafePacketLogger(); - - virtual void Update(void); - -protected: - virtual void AddToLog(const char *str); - - DataStructures::SingleProducerConsumer logMessages; -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Derivation of the packet logger to defer the call to WriteLog until the user thread. +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_PacketLogger==1 + +#pragma once + +#include "PacketLogger.h" +#include "SingleProducerConsumer.h" + +namespace RakNet +{ + +/// \ingroup PACKETLOGGER_GROUP +/// \brief Same as PacketLogger, but writes output in the user thread. +class RAK_DLL_EXPORT ThreadsafePacketLogger : public PacketLogger +{ +public: + ThreadsafePacketLogger(); + virtual ~ThreadsafePacketLogger(); + + virtual void Update(void); + +protected: + virtual void AddToLog(const char *str); + + DataStructures::SingleProducerConsumer logMessages; +}; + +} // namespace RakNet + +#endif + diff --git a/Source/TransportInterface.h b/Source/TransportInterface.h index b1893afff..4a3632253 100644 --- a/Source/TransportInterface.h +++ b/Source/TransportInterface.h @@ -1,91 +1,88 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Contains TransportInterface from which you can derive custom transport providers for ConsoleServer. -/// - - - -#ifndef __TRANSPORT_INTERFACE_H -#define __TRANSPORT_INTERFACE_H - -#include "RakNetTypes.h" -#include "Export.h" -#include "RakMemoryOverride.h" - -#define REMOTE_MAX_TEXT_INPUT 2048 - -namespace RakNet -{ - -class CommandParserInterface; - - -/// \brief Defines an interface that is used to send and receive null-terminated strings. -/// \details In practice this is only used by the CommandParser system for for servers. -class RAK_DLL_EXPORT TransportInterface -{ -public: - TransportInterface() {} - virtual ~TransportInterface() {} - - /// Start the transport provider on the indicated port. - /// \param[in] port The port to start the transport provider on - /// \param[in] serverMode If true, you should allow incoming connections (I don't actually use this anywhere) - /// \return Return true on success, false on failure. - virtual bool Start(unsigned short port, bool serverMode)=0; - - /// Stop the transport provider. You can clear memory and shutdown threads here. - virtual void Stop(void)=0; - - /// Send a null-terminated string to \a systemAddress - /// If your transport method requires particular formatting of the outgoing data (e.g. you don't just send strings) you can do it here - /// and parse it out in Receive(). - /// \param[in] systemAddress The player to send the string to - /// \param[in] data format specifier - same as RAKNET_DEBUG_PRINTF - /// \param[in] ... format specification arguments - same as RAKNET_DEBUG_PRINTF - virtual void Send( SystemAddress systemAddress, const char *data, ... )=0; - - /// Disconnect \a systemAddress . The binary address and port defines the SystemAddress structure. - /// \param[in] systemAddress The player/address to disconnect - virtual void CloseConnection( SystemAddress systemAddress )=0; - - /// Return a string. The string should be allocated and written to Packet::data . - /// The byte length should be written to Packet::length . The player/address should be written to Packet::systemAddress - /// If your transport protocol adds special formatting to the data stream you should parse it out before returning it in the packet - /// and thus only return a string in Packet::data - /// \return The packet structure containing the result of Receive, or 0 if no data is available - virtual Packet* Receive( void )=0; - - /// Deallocate the Packet structure returned by Receive - /// \param[in] The packet to deallocate - virtual void DeallocatePacket( Packet *packet )=0; - - /// If a new system connects to you, you should queue that event and return the systemAddress/address of that player in this function. - /// \return The SystemAddress/address of the system - virtual SystemAddress HasNewIncomingConnection(void)=0; - - /// If a system loses the connection, you should queue that event and return the systemAddress/address of that player in this function. - /// \return The SystemAddress/address of the system - virtual SystemAddress HasLostConnection(void)=0; - - /// Your transport provider can itself have command parsers if the transport layer has user-modifiable features - /// For example, your transport layer may have a password which you want remote users to be able to set or you may want - /// to allow remote users to turn on or off command echo - /// \return 0 if you do not need a command parser - otherwise the desired derivation of CommandParserInterface - virtual CommandParserInterface* GetCommandParser(void)=0; -protected: -}; - -} // namespace RakNet - -#endif - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Contains TransportInterface from which you can derive custom transport providers for ConsoleServer. +/// + + + +#pragma once + +#include "RakNetTypes.h" +#include "Export.h" +#include "RakMemoryOverride.h" + +#define REMOTE_MAX_TEXT_INPUT 2048 + +namespace RakNet +{ + +class CommandParserInterface; + + +/// \brief Defines an interface that is used to send and receive null-terminated strings. +/// \details In practice this is only used by the CommandParser system for for servers. +class RAK_DLL_EXPORT TransportInterface +{ +public: + TransportInterface() {} + virtual ~TransportInterface() {} + + /// Start the transport provider on the indicated port. + /// \param[in] port The port to start the transport provider on + /// \param[in] serverMode If true, you should allow incoming connections (I don't actually use this anywhere) + /// \return Return true on success, false on failure. + virtual bool Start(unsigned short port, bool serverMode)=0; + + /// Stop the transport provider. You can clear memory and shutdown threads here. + virtual void Stop(void)=0; + + /// Send a null-terminated string to \a systemAddress + /// If your transport method requires particular formatting of the outgoing data (e.g. you don't just send strings) you can do it here + /// and parse it out in Receive(). + /// \param[in] systemAddress The player to send the string to + /// \param[in] data format specifier - same as RAKNET_DEBUG_PRINTF + /// \param[in] ... format specification arguments - same as RAKNET_DEBUG_PRINTF + virtual void Send( SystemAddress systemAddress, const char *data, ... )=0; + + /// Disconnect \a systemAddress . The binary address and port defines the SystemAddress structure. + /// \param[in] systemAddress The player/address to disconnect + virtual void CloseConnection( SystemAddress systemAddress )=0; + + /// Return a string. The string should be allocated and written to Packet::data . + /// The byte length should be written to Packet::length . The player/address should be written to Packet::systemAddress + /// If your transport protocol adds special formatting to the data stream you should parse it out before returning it in the packet + /// and thus only return a string in Packet::data + /// \return The packet structure containing the result of Receive, or 0 if no data is available + virtual Packet* Receive( void )=0; + + /// Deallocate the Packet structure returned by Receive + /// \param[in] The packet to deallocate + virtual void DeallocatePacket( Packet *packet )=0; + + /// If a new system connects to you, you should queue that event and return the systemAddress/address of that player in this function. + /// \return The SystemAddress/address of the system + virtual SystemAddress HasNewIncomingConnection(void)=0; + + /// If a system loses the connection, you should queue that event and return the systemAddress/address of that player in this function. + /// \return The SystemAddress/address of the system + virtual SystemAddress HasLostConnection(void)=0; + + /// Your transport provider can itself have command parsers if the transport layer has user-modifiable features + /// For example, your transport layer may have a password which you want remote users to be able to set or you may want + /// to allow remote users to turn on or off command echo + /// \return 0 if you do not need a command parser - otherwise the desired derivation of CommandParserInterface + virtual CommandParserInterface* GetCommandParser(void)=0; +protected: +}; + +} // namespace RakNet + diff --git a/Source/TwoWayAuthentication.h b/Source/TwoWayAuthentication.h index bf080d2d0..e69c21c94 100644 --- a/Source/TwoWayAuthentication.h +++ b/Source/TwoWayAuthentication.h @@ -1,154 +1,152 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file TwoWayAuthentication.h -/// \brief Implements two way authentication -/// \details Given two systems, each of whom known a common password, verify the password without transmitting it -/// This can be used to determine what permissions are should be allowed to the other system -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_TwoWayAuthentication==1 - -#ifndef __TWO_WAY_AUTHENTICATION_H -#define __TWO_WAY_AUTHENTICATION_H - -// How often to change the nonce. -#define NONCE_TIMEOUT_MS 10000 -// How often to check for ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_TIMEOUT, and the minimum timeout time. Maximum is double this value. -#define CHALLENGE_MINIMUM_TIMEOUT 3000 - -#if LIBCAT_SECURITY==1 -// From CPP FILE: -// static const int HASH_BITS = 256; -// static const int HASH_BYTES = HASH_BITS / 8; -// static const int STRENGTHENING_FACTOR = 1000; -#define TWO_WAY_AUTHENTICATION_NONCE_LENGTH 32 -#define HASHED_NONCE_AND_PW_LENGTH 32 -#else -#include "DR_SHA1.h" -#define TWO_WAY_AUTHENTICATION_NONCE_LENGTH 20 -#define HASHED_NONCE_AND_PW_LENGTH SHA1_LENGTH -#endif - -#include "PluginInterface2.h" -#include "RakMemoryOverride.h" -#include "NativeTypes.h" -#include "RakString.h" -#include "DS_Hash.h" -#include "DS_Queue.h" - -typedef int64_t FCM2Guid; - -namespace RakNet -{ -/// Forward declarations -class RakPeerInterface; - -/// \brief Implements two way authentication -/// \details Given two systems, each of whom known a common password / identifier pair, verify the password without transmitting it -/// This can be used to determine what permissions are should be allowed to the other system -/// If the other system should not send any data until authentication passes, you can use the MessageFilter plugin for this. Call MessageFilter::SetAllowMessageID() including ID_TWO_WAY_AUTHENTICATION_NEGOTIATION when doing so. Also attach MessageFilter first in the list of plugins -/// \note If other systems challenges us, and fails, you will get ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_FAILED. -/// \ingroup PLUGINS_GROUP -class RAK_DLL_EXPORT TwoWayAuthentication : public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(TwoWayAuthentication) - - TwoWayAuthentication(); - virtual ~TwoWayAuthentication(); - - /// \brief Adds a password to the list of passwords the system will accept - /// \details Each password, which is secret and not transmitted, is identified by \a identifier. - /// \a identifier is transmitted in plaintext with the request. It is only needed because the system supports multiple password. - /// It is used to only hash against once password on the remote system, rather than having to hash against every known password. - /// \param[in] identifier A unique identifier representing this password. This is transmitted in plaintext and should be considered insecure - /// \param[in] password The password to add - /// \return True on success, false on identifier==password, either identifier or password is blank, or identifier is already in use - bool AddPassword(RakNet::RakString identifier, RakNet::RakString password); - - /// \brief Challenge another system for the specified identifier - /// \details After calling Challenge, you will get back ID_TWO_WAY_AUTHENTICATION_SUCCESS, ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_TIMEOUT, or ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_FAILED - /// ID_TWO_WAY_AUTHENTICATION_SUCCESS will be returned if and only if the other system has called AddPassword() with the same identifier\password pair as this system. - /// \param[in] identifier A unique identifier representing this password. This is transmitted in plaintext and should be considered insecure - /// \return True on success, false on remote system not connected, or identifier not previously added with AddPassword() - bool Challenge(RakNet::RakString identifier, AddressOrGUID remoteSystem); - - /// \brief Free all memory - void Clear(void); - - /// \internal - virtual void Update(void); - /// \internal - virtual PluginReceiveResult OnReceive(Packet *packet); - /// \internal - virtual void OnRakPeerShutdown(void); - /// \internal - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - - /// \internal - struct PendingChallenge - { - RakNet::RakString identifier; - AddressOrGUID remoteSystem; - RakNet::Time time; - bool sentHash; - }; - - DataStructures::Queue outgoingChallenges; - - /// \internal - struct NonceAndRemoteSystemRequest - { - char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH]; - RakNet::AddressOrGUID remoteSystem; - unsigned short requestId; - RakNet::Time whenGenerated; - }; - /// \internal - struct RAK_DLL_EXPORT NonceGenerator - { - NonceGenerator(); - ~NonceGenerator(); - void GetNonce(char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH], unsigned short *requestId, RakNet::AddressOrGUID remoteSystem); - void GenerateNonce(char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH]); - bool GetNonceById(char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH], unsigned short requestId, RakNet::AddressOrGUID remoteSystem, bool popIfFound); - void Clear(void); - void ClearByAddress(RakNet::AddressOrGUID remoteSystem); - void Update(RakNet::Time curTime); - - DataStructures::List generatedNonces; - unsigned short nextRequestId; - }; - -protected: - void PushToUser(MessageID messageId, RakNet::RakString password, RakNet::AddressOrGUID remoteSystem); - // Key is identifier, data is password - DataStructures::Hash passwords; - - RakNet::Time whenLastTimeoutCheck; - - NonceGenerator nonceGenerator; - - void OnNonceRequest(Packet *packet); - void OnNonceReply(Packet *packet); - PluginReceiveResult OnHashedNonceAndPassword(Packet *packet); - void OnPasswordResult(Packet *packet); - void Hash(char thierNonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH], RakNet::RakString password, char out[HASHED_NONCE_AND_PW_LENGTH]); -}; - -} // namespace RakNet - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file TwoWayAuthentication.h +/// \brief Implements two way authentication +/// \details Given two systems, each of whom known a common password, verify the password without transmitting it +/// This can be used to determine what permissions are should be allowed to the other system +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_TwoWayAuthentication==1 + +#pragma once + +// How often to change the nonce. +#define NONCE_TIMEOUT_MS 10000 +// How often to check for ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_TIMEOUT, and the minimum timeout time. Maximum is double this value. +#define CHALLENGE_MINIMUM_TIMEOUT 3000 + +#if LIBCAT_SECURITY==1 +// From CPP FILE: +// static const int HASH_BITS = 256; +// static const int HASH_BYTES = HASH_BITS / 8; +// static const int STRENGTHENING_FACTOR = 1000; +#define TWO_WAY_AUTHENTICATION_NONCE_LENGTH 32 +#define HASHED_NONCE_AND_PW_LENGTH 32 +#else +#include "DR_SHA1.h" +#define TWO_WAY_AUTHENTICATION_NONCE_LENGTH 20 +#define HASHED_NONCE_AND_PW_LENGTH SHA1_LENGTH +#endif + +#include "PluginInterface2.h" +#include "RakMemoryOverride.h" +#include "NativeTypes.h" +#include "RakString.h" +#include "DS_Hash.h" +#include "DS_Queue.h" + +typedef int64_t FCM2Guid; + +namespace RakNet +{ +/// Forward declarations +class RakPeerInterface; + +/// \brief Implements two way authentication +/// \details Given two systems, each of whom known a common password / identifier pair, verify the password without transmitting it +/// This can be used to determine what permissions are should be allowed to the other system +/// If the other system should not send any data until authentication passes, you can use the MessageFilter plugin for this. Call MessageFilter::SetAllowMessageID() including ID_TWO_WAY_AUTHENTICATION_NEGOTIATION when doing so. Also attach MessageFilter first in the list of plugins +/// \note If other systems challenges us, and fails, you will get ID_TWO_WAY_AUTHENTICATION_INCOMING_CHALLENGE_FAILED. +/// \ingroup PLUGINS_GROUP +class RAK_DLL_EXPORT TwoWayAuthentication : public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(TwoWayAuthentication) + + TwoWayAuthentication(); + virtual ~TwoWayAuthentication(); + + /// \brief Adds a password to the list of passwords the system will accept + /// \details Each password, which is secret and not transmitted, is identified by \a identifier. + /// \a identifier is transmitted in plaintext with the request. It is only needed because the system supports multiple password. + /// It is used to only hash against once password on the remote system, rather than having to hash against every known password. + /// \param[in] identifier A unique identifier representing this password. This is transmitted in plaintext and should be considered insecure + /// \param[in] password The password to add + /// \return True on success, false on identifier==password, either identifier or password is blank, or identifier is already in use + bool AddPassword(RakNet::RakString identifier, RakNet::RakString password); + + /// \brief Challenge another system for the specified identifier + /// \details After calling Challenge, you will get back ID_TWO_WAY_AUTHENTICATION_SUCCESS, ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_TIMEOUT, or ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_FAILED + /// ID_TWO_WAY_AUTHENTICATION_SUCCESS will be returned if and only if the other system has called AddPassword() with the same identifier\password pair as this system. + /// \param[in] identifier A unique identifier representing this password. This is transmitted in plaintext and should be considered insecure + /// \return True on success, false on remote system not connected, or identifier not previously added with AddPassword() + bool Challenge(RakNet::RakString identifier, AddressOrGUID remoteSystem); + + /// \brief Free all memory + void Clear(void); + + /// \internal + virtual void Update(void); + /// \internal + virtual PluginReceiveResult OnReceive(Packet *packet); + /// \internal + virtual void OnRakPeerShutdown(void); + /// \internal + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + + /// \internal + struct PendingChallenge + { + RakNet::RakString identifier; + AddressOrGUID remoteSystem; + RakNet::Time time; + bool sentHash; + }; + + DataStructures::Queue outgoingChallenges; + + /// \internal + struct NonceAndRemoteSystemRequest + { + char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH]; + RakNet::AddressOrGUID remoteSystem; + unsigned short requestId; + RakNet::Time whenGenerated; + }; + /// \internal + struct RAK_DLL_EXPORT NonceGenerator + { + NonceGenerator(); + ~NonceGenerator(); + void GetNonce(char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH], unsigned short *requestId, RakNet::AddressOrGUID remoteSystem); + void GenerateNonce(char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH]); + bool GetNonceById(char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH], unsigned short requestId, RakNet::AddressOrGUID remoteSystem, bool popIfFound); + void Clear(void); + void ClearByAddress(RakNet::AddressOrGUID remoteSystem); + void Update(RakNet::Time curTime); + + DataStructures::List generatedNonces; + unsigned short nextRequestId; + }; + +protected: + void PushToUser(MessageID messageId, RakNet::RakString password, RakNet::AddressOrGUID remoteSystem); + // Key is identifier, data is password + DataStructures::Hash passwords; + + RakNet::Time whenLastTimeoutCheck; + + NonceGenerator nonceGenerator; + + void OnNonceRequest(Packet *packet); + void OnNonceReply(Packet *packet); + PluginReceiveResult OnHashedNonceAndPassword(Packet *packet); + void OnPasswordResult(Packet *packet); + void Hash(char thierNonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH], RakNet::RakString password, char out[HASHED_NONCE_AND_PW_LENGTH]); +}; + +} // namespace RakNet + +#endif + diff --git a/Source/UDPForwarder.cpp b/Source/UDPForwarder.cpp index 9ee2377bd..a6a352b60 100644 --- a/Source/UDPForwarder.cpp +++ b/Source/UDPForwarder.cpp @@ -1,643 +1,643 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "UDPForwarder.h" - -#if _RAKNET_SUPPORT_UDPForwarder==1 - -#include "GetTime.h" -#include "MTUSize.h" -#include "SocketLayer.h" -#include "WSAStartupSingleton.h" -#include "RakSleep.h" -#include "DS_OrderedList.h" -#include "LinuxStrings.h" -#include "SocketDefines.h" -#include "VitaIncludes.h" -#include "errno.h" - -#ifndef INVALID_SOCKET -#define INVALID_SOCKET -1 -#endif - -using namespace RakNet; -static const unsigned short DEFAULT_MAX_FORWARD_ENTRIES=64; - -namespace RakNet -{ - RAK_THREAD_DECLARATION(UpdateUDPForwarderGlobal); -} - -UDPForwarder::ForwardEntry::ForwardEntry() -{ - socket=INVALID_SOCKET; - timeLastDatagramForwarded=RakNet::GetTimeMS(); - addr1Confirmed=UNASSIGNED_SYSTEM_ADDRESS; - addr2Confirmed=UNASSIGNED_SYSTEM_ADDRESS; -} -UDPForwarder::ForwardEntry::~ForwardEntry() { - if (socket!=INVALID_SOCKET) - closesocket__(socket); -} - -UDPForwarder::UDPForwarder() -{ -#ifdef _WIN32 - WSAStartupSingleton::AddRef(); -#endif - - maxForwardEntries=DEFAULT_MAX_FORWARD_ENTRIES; - nextInputId=0; - startForwardingInput.SetPageSize(sizeof(StartForwardingInputStruct)*16); - stopForwardingCommands.SetPageSize(sizeof(StopForwardingStruct)*16); -} -UDPForwarder::~UDPForwarder() -{ - Shutdown(); - -#ifdef _WIN32 - WSAStartupSingleton::Deref(); -#endif -} -void UDPForwarder::Startup(void) -{ - if (isRunning.GetValue()>0) - return; - - isRunning.Increment(); - - int errorCode; - - - - errorCode = RakNet::RakThread::Create(UpdateUDPForwarderGlobal, this); - - if ( errorCode != 0 ) - { - RakAssert(0); - return; - } - - while (threadRunning.GetValue()==0) - RakSleep(30); -} -void UDPForwarder::Shutdown(void) -{ - if (isRunning.GetValue()==0) - return; - isRunning.Decrement(); - - while (threadRunning.GetValue()>0) - RakSleep(30); - - unsigned int j; - for (j=0; j < forwardListNotUpdated.Size(); j++) - RakNet::OP_DELETE(forwardListNotUpdated[j],_FILE_AND_LINE_); - forwardListNotUpdated.Clear(false, _FILE_AND_LINE_); -} -void UDPForwarder::SetMaxForwardEntries(unsigned short maxEntries) -{ - RakAssert(maxEntries>0 && maxEntries<65535/2); - maxForwardEntries=maxEntries; -} -int UDPForwarder::GetMaxForwardEntries(void) const -{ - return maxForwardEntries; -} -int UDPForwarder::GetUsedForwardEntries(void) const -{ - return (int) forwardListNotUpdated.Size(); -} -UDPForwarderResult UDPForwarder::StartForwarding(SystemAddress source, SystemAddress destination, RakNet::TimeMS timeoutOnNoDataMS, const char *forceHostAddress, unsigned short socketFamily, - unsigned short *forwardingPort, __UDPSOCKET__ *forwardingSocket) -{ - // Invalid parameters? - if (timeoutOnNoDataMS == 0 || timeoutOnNoDataMS > UDP_FORWARDER_MAXIMUM_TIMEOUT || source==UNASSIGNED_SYSTEM_ADDRESS || destination==UNASSIGNED_SYSTEM_ADDRESS) - return UDPFORWARDER_INVALID_PARAMETERS; - - if (isRunning.GetValue()==0) - return UDPFORWARDER_NOT_RUNNING; - - (void) socketFamily; - - unsigned int inputId = nextInputId++; - - StartForwardingInputStruct *sfis; - sfis = startForwardingInput.Allocate(_FILE_AND_LINE_); - sfis->source=source; - sfis->destination=destination; - sfis->timeoutOnNoDataMS=timeoutOnNoDataMS; - RakAssert(timeoutOnNoDataMS!=0); - if (forceHostAddress && forceHostAddress[0]) - sfis->forceHostAddress=forceHostAddress; - sfis->socketFamily=socketFamily; - sfis->inputId=inputId; - startForwardingInput.Push(sfis); - -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - while (1) - { - RakSleep(0); - startForwardingOutputMutex.Lock(); - for (unsigned int i=0; i < startForwardingOutput.Size(); i++) - { - if (startForwardingOutput[i].inputId==inputId) - { - if (startForwardingOutput[i].result==UDPFORWARDER_SUCCESS) - { - if (forwardingPort) - *forwardingPort = startForwardingOutput[i].forwardingPort; - if (forwardingSocket) - *forwardingSocket = startForwardingOutput[i].forwardingSocket; - } - UDPForwarderResult res = startForwardingOutput[i].result; - startForwardingOutput.RemoveAtIndex(i); - startForwardingOutputMutex.Unlock(); - return res; - } - } - startForwardingOutputMutex.Unlock(); - } - - return UDPFORWARDER_RESULT_COUNT; -} -void UDPForwarder::StopForwarding(SystemAddress source, SystemAddress destination) -{ - StopForwardingStruct *sfs; - sfs = stopForwardingCommands.Allocate(_FILE_AND_LINE_); - sfs->destination=destination; - sfs->source=source; - stopForwardingCommands.Push(sfs); -} -void UDPForwarder::RecvFrom(RakNet::TimeMS curTime, ForwardEntry *forwardEntry) -{ -#ifndef __native_client__ - char data[ MAXIMUM_MTU_SIZE ]; - -#if RAKNET_SUPPORT_IPV6==1 - sockaddr_storage their_addr; - memset(&their_addr,0,sizeof(their_addr)); - sockaddr* sockAddrPtr; - socklen_t sockLen; - socklen_t* socketlenPtr=(socklen_t*) &sockLen; - sockaddr_in *sockAddrIn; - sockaddr_in6 *sockAddrIn6; - sockLen=sizeof(their_addr); - sockAddrPtr=(sockaddr*) &their_addr; -#else - sockaddr_in sockAddrIn; - memset(&sockAddrIn,0,sizeof(sockaddr_in)); - socklen_t len2; - len2 = sizeof( sockAddrIn ); - sockAddrIn.sin_family = AF_INET; -#endif - -#if defined(__GNUC__) - #if defined(MSG_DONTWAIT) - const int flag=MSG_DONTWAIT; - #else - const int flag=0x40; - #endif -#else - const int flag=0; -#endif - - int receivedDataLen, len=0; - //unsigned short portnum=0; - -#if RAKNET_SUPPORT_IPV6==1 - receivedDataLen = recvfrom__( forwardEntry->socket, data, MAXIMUM_MTU_SIZE, flag, sockAddrPtr, socketlenPtr ); -#else - receivedDataLen = recvfrom__( forwardEntry->socket, data, MAXIMUM_MTU_SIZE, flag, ( sockaddr* ) & sockAddrIn, ( socklen_t* ) & len2 ); -#endif - - if (receivedDataLen<0) - { -#if defined(_WIN32) && defined(_DEBUG) && !defined(WINDOWS_PHONE_8) && !defined(WINDOWS_STORE_RT) - DWORD dwIOError = WSAGetLastError(); - - if (dwIOError!=WSAECONNRESET && dwIOError!=WSAEINTR && dwIOError!=WSAETIMEDOUT && dwIOError!=WSAEWOULDBLOCK) - { - LPVOID messageBuffer; - FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language - ( LPTSTR ) & messageBuffer, 0, NULL ); - // something has gone wrong here... - RAKNET_DEBUG_PRINTF( "recvfrom failed:Error code - %d\n%s", dwIOError, messageBuffer ); - - //Free the buffer. - LocalFree( messageBuffer ); - } -#else - if (errno!=EAGAIN - && errno!=0 -#if defined(__GNUC__) - && errno!=EWOULDBLOCK -#endif - ) - { - printf("errno=%i\n", errno); - } -#endif - - - } - - if (receivedDataLen<=0) - return; - - SystemAddress receivedAddr; -#if RAKNET_SUPPORT_IPV6==1 - if (their_addr.ss_family==AF_INET) - { - sockAddrIn=(sockaddr_in *)&their_addr; - sockAddrIn6=0; - memcpy(&receivedAddr.address.addr4,sockAddrIn,sizeof(sockaddr_in)); - } - else - { - sockAddrIn=0; - sockAddrIn6=(sockaddr_in6 *)&their_addr; - memcpy(&receivedAddr.address.addr6,sockAddrIn6,sizeof(sockaddr_in6)); - } -#else - memcpy(&receivedAddr.address.addr4,&sockAddrIn,sizeof(sockaddr_in)); -#endif - //portnum=receivedAddr.GetPort(); - - SystemAddress forwardTarget; - - bool confirmed1 = forwardEntry->addr1Confirmed!=UNASSIGNED_SYSTEM_ADDRESS; - bool confirmed2 = forwardEntry->addr2Confirmed!=UNASSIGNED_SYSTEM_ADDRESS; - bool matchConfirmed1 = - confirmed1 && - forwardEntry->addr1Confirmed==receivedAddr; - bool matchConfirmed2 = - confirmed2 && - forwardEntry->addr2Confirmed==receivedAddr; - bool matchUnconfirmed1 = forwardEntry->addr1Unconfirmed.EqualsExcludingPort(receivedAddr); - bool matchUnconfirmed2 = forwardEntry->addr2Unconfirmed.EqualsExcludingPort(receivedAddr); - - if (matchConfirmed1==true || (matchConfirmed2==false && confirmed1==false && matchUnconfirmed1==true)) - { - // Forward to addr2 - if (forwardEntry->addr1Confirmed==UNASSIGNED_SYSTEM_ADDRESS) - { - forwardEntry->addr1Confirmed=receivedAddr; - } - if (forwardEntry->addr2Confirmed!=UNASSIGNED_SYSTEM_ADDRESS) - forwardTarget=forwardEntry->addr2Confirmed; - else - forwardTarget=forwardEntry->addr2Unconfirmed; - } - else if (matchConfirmed2==true || (confirmed2==false && matchUnconfirmed2==true)) - { - // Forward to addr1 - if (forwardEntry->addr2Confirmed==UNASSIGNED_SYSTEM_ADDRESS) - { - forwardEntry->addr2Confirmed=receivedAddr; - } - if (forwardEntry->addr1Confirmed!=UNASSIGNED_SYSTEM_ADDRESS) - forwardTarget=forwardEntry->addr1Confirmed; - else - forwardTarget=forwardEntry->addr1Unconfirmed; - } - else - { - return; - } - - // Forward to dest - len=0; -// sockaddr_in saOut; -// saOut.sin_port = forwardTarget.GetPortNetworkOrder(); // User port -// saOut.sin_addr.s_addr = forwardTarget.address.addr4.sin_addr.s_addr; -// saOut.sin_family = AF_INET; - do - { - - - -#if RAKNET_SUPPORT_IPV6==1 - if (forwardTarget.address.addr4.sin_family==AF_INET) - { - do - { - len = sendto__( forwardEntry->socket, data, receivedDataLen, 0, ( const sockaddr* ) & forwardTarget.address.addr4, sizeof( sockaddr_in ) ); - } - while ( len == 0 ); - } - else - { - do - { - len = sendto__( forwardEntry->socket, data, receivedDataLen, 0, ( const sockaddr* ) & forwardTarget.address.addr6, sizeof( sockaddr_in6 ) ); - } - while ( len == 0 ); - } -#else - do - { - len = sendto__( forwardEntry->socket, data, receivedDataLen, 0, ( const sockaddr* ) & forwardTarget.address.addr4, sizeof( sockaddr_in ) ); - } - while ( len == 0 ); -#endif - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - } - while ( len == 0 ); - - forwardEntry->timeLastDatagramForwarded=curTime; -#endif // __native_client__ -} -void UDPForwarder::UpdateUDPForwarder(void) -{ - /* -#if !defined(SN_TARGET_PSP2) - timeval tv; - tv.tv_sec=0; - tv.tv_usec=0; -#endif - */ - - RakNet::TimeMS curTime = RakNet::GetTimeMS(); - - StartForwardingInputStruct *sfis; - StartForwardingOutputStruct sfos; - sfos.forwardingSocket=INVALID_SOCKET; - sfos.forwardingPort=0; - sfos.inputId=0; - sfos.result=UDPFORWARDER_RESULT_COUNT; - -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - while (1) - { - sfis = startForwardingInput.Pop(); - if (sfis==0) - break; - - if (GetUsedForwardEntries()>maxForwardEntries) - { - sfos.result=UDPFORWARDER_NO_SOCKETS; - } - else - { - sfos.result=UDPFORWARDER_RESULT_COUNT; - - for (unsigned int i=0; i < forwardListNotUpdated.Size(); i++) - { - if ( - (forwardListNotUpdated[i]->addr1Unconfirmed==sfis->source && - forwardListNotUpdated[i]->addr2Unconfirmed==sfis->destination) - || - (forwardListNotUpdated[i]->addr1Unconfirmed==sfis->destination && - forwardListNotUpdated[i]->addr2Unconfirmed==sfis->source) - ) - { - ForwardEntry *fe = forwardListNotUpdated[i]; - sfos.forwardingPort = SocketLayer::GetLocalPort ( fe->socket ); - sfos.forwardingSocket=fe->socket; - sfos.result=UDPFORWARDER_FORWARDING_ALREADY_EXISTS; - break; - } - } - - if (sfos.result==UDPFORWARDER_RESULT_COUNT) - { - int sock_opt; - sockaddr_in listenerSocketAddress; - listenerSocketAddress.sin_port = 0; - ForwardEntry *fe = RakNet::OP_NEW(_FILE_AND_LINE_); - fe->addr1Unconfirmed=sfis->source; - fe->addr2Unconfirmed=sfis->destination; - fe->timeoutOnNoDataMS=sfis->timeoutOnNoDataMS; - -#if RAKNET_SUPPORT_IPV6!=1 - fe->socket = socket__( AF_INET, SOCK_DGRAM, 0 ); - listenerSocketAddress.sin_family = AF_INET; - if (sfis->forceHostAddress.IsEmpty()==false) - { - - - - - - listenerSocketAddress.sin_addr.s_addr = inet_addr__( sfis->forceHostAddress.C_String() ); - - } - else - { - listenerSocketAddress.sin_addr.s_addr = INADDR_ANY; - } - int ret = bind__( fe->socket, ( struct sockaddr * ) & listenerSocketAddress, sizeof( listenerSocketAddress ) ); - if (ret==-1) - { - RakNet::OP_DELETE(fe,_FILE_AND_LINE_); - sfos.result=UDPFORWARDER_BIND_FAILED; - } - else - { - sfos.result=UDPFORWARDER_SUCCESS; - } - -#else // RAKNET_SUPPORT_IPV6==1 - struct addrinfo hints; - memset(&hints, 0, sizeof (addrinfo)); // make sure the struct is empty - hints.ai_family = sfis->socketFamily; - hints.ai_socktype = SOCK_DGRAM; // UDP sockets - hints.ai_flags = AI_PASSIVE; // fill in my IP for me - struct addrinfo *servinfo=0, *aip; // will point to the results - - if (sfis->forceHostAddress.IsEmpty() || sfis->forceHostAddress=="UNASSIGNED_SYSTEM_ADDRESS") - getaddrinfo(0, "0", &hints, &servinfo); - else - getaddrinfo(sfis->forceHostAddress.C_String(), "0", &hints, &servinfo); - - for (aip = servinfo; aip != NULL; aip = aip->ai_next) - { - // Open socket. The address type depends on what - // getaddrinfo() gave us. - fe->socket = socket__(aip->ai_family, aip->ai_socktype, aip->ai_protocol); - if (fe->socket != INVALID_SOCKET) - { - int ret = bind__( fe->socket, aip->ai_addr, (int) aip->ai_addrlen ); - if (ret>=0) - { - break; - } - else - { - closesocket__(fe->socket); - fe->socket=INVALID_SOCKET; - } - } - } - - if (fe->socket==INVALID_SOCKET) - sfos.result=UDPFORWARDER_BIND_FAILED; - else - sfos.result=UDPFORWARDER_SUCCESS; -#endif // RAKNET_SUPPORT_IPV6==1 - - if (sfos.result==UDPFORWARDER_SUCCESS) - { - sfos.forwardingPort = SocketLayer::GetLocalPort ( fe->socket ); - sfos.forwardingSocket=fe->socket; - - sock_opt=1024*256; - setsockopt__(fe->socket, SOL_SOCKET, SO_RCVBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) ); - sock_opt=0; - setsockopt__(fe->socket, SOL_SOCKET, SO_LINGER, ( char * ) & sock_opt, sizeof ( sock_opt ) ); -#ifdef _WIN32 - unsigned long nonblocking = 1; - ioctlsocket__( fe->socket, FIONBIO, &nonblocking ); - - - -#else - fcntl( fe->socket, F_SETFL, O_NONBLOCK ); -#endif - - forwardListNotUpdated.Insert(fe,_FILE_AND_LINE_); - } - } - } - - // Push result - sfos.inputId=sfis->inputId; - startForwardingOutputMutex.Lock(); - startForwardingOutput.Push(sfos,_FILE_AND_LINE_); - startForwardingOutputMutex.Unlock(); - - startForwardingInput.Deallocate(sfis, _FILE_AND_LINE_); - } - - StopForwardingStruct *sfs; - -#ifdef _MSC_VER -#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant -#endif - while (1) - { - sfs = stopForwardingCommands.Pop(); - if (sfs==0) - break; - - ForwardEntry *fe; - for (unsigned int i=0; i < forwardListNotUpdated.Size(); i++) - { - if ( - (forwardListNotUpdated[i]->addr1Unconfirmed==sfs->source && - forwardListNotUpdated[i]->addr2Unconfirmed==sfs->destination) - || - (forwardListNotUpdated[i]->addr1Unconfirmed==sfs->destination && - forwardListNotUpdated[i]->addr2Unconfirmed==sfs->source) - ) - { - fe = forwardListNotUpdated[i]; - forwardListNotUpdated.RemoveAtIndexFast(i); - RakNet::OP_DELETE(fe, _FILE_AND_LINE_); - break; - } - } - - stopForwardingCommands.Deallocate(sfs, _FILE_AND_LINE_); - } - - unsigned int i; - - i=0; - while (i < forwardListNotUpdated.Size()) - { - if (curTime > forwardListNotUpdated[i]->timeLastDatagramForwarded && // Account for timestamp wrap - curTime > forwardListNotUpdated[i]->timeLastDatagramForwarded+forwardListNotUpdated[i]->timeoutOnNoDataMS) - { - RakNet::OP_DELETE(forwardListNotUpdated[i],_FILE_AND_LINE_); - forwardListNotUpdated.RemoveAtIndex(i); - } - else - i++; - } - - ForwardEntry *forwardEntry; - for (i=0; i < forwardListNotUpdated.Size(); i++) - { - forwardEntry = forwardListNotUpdated[i]; - RecvFrom(curTime, forwardEntry); - } -} - -namespace RakNet { -RAK_THREAD_DECLARATION(UpdateUDPForwarderGlobal) -{ - - - - UDPForwarder * udpForwarder = ( UDPForwarder * ) arguments; - - - udpForwarder->threadRunning.Increment(); - while (udpForwarder->isRunning.GetValue()>0) - { - udpForwarder->UpdateUDPForwarder(); - - // 12/1/2010 Do not change from 0 - // See http://www.jenkinssoftware.com/forum/index.php?topic=4033.0;topicseen - // Avoid 100% reported CPU usage - if (udpForwarder->forwardListNotUpdated.Size()==0) - RakSleep(30); - else - RakSleep(0); - } - udpForwarder->threadRunning.Decrement(); - - - - - return 0; - - -} - -} // namespace RakNet - -#endif // #if _RAKNET_SUPPORT_FileOperations==1 +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "UDPForwarder.h" + +#if _RAKNET_SUPPORT_UDPForwarder==1 + +#include "GetTime.h" +#include "MTUSize.h" +#include "SocketLayer.h" +#include "WSAStartupSingleton.h" +#include "RakSleep.h" +#include "DS_OrderedList.h" +#include "LinuxStrings.h" +#include "SocketDefines.h" +#include "VitaIncludes.h" +#include "errno.h" + +#ifndef INVALID_SOCKET +#define INVALID_SOCKET -1 +#endif + +using namespace RakNet; +static const unsigned short DEFAULT_MAX_FORWARD_ENTRIES=64; + +namespace RakNet +{ + RAK_THREAD_DECLARATION(UpdateUDPForwarderGlobal); +} + +UDPForwarder::ForwardEntry::ForwardEntry() +{ + socket=INVALID_SOCKET; + timeLastDatagramForwarded=RakNet::GetTimeMS(); + addr1Confirmed=UNASSIGNED_SYSTEM_ADDRESS; + addr2Confirmed=UNASSIGNED_SYSTEM_ADDRESS; +} +UDPForwarder::ForwardEntry::~ForwardEntry() { + if (socket!=INVALID_SOCKET) + closesocket__(socket); +} + +UDPForwarder::UDPForwarder() +{ +#ifdef _WIN32 + WSAStartupSingleton::AddRef(); +#endif + + maxForwardEntries=DEFAULT_MAX_FORWARD_ENTRIES; + nextInputId=0; + startForwardingInput.SetPageSize(sizeof(StartForwardingInputStruct)*16); + stopForwardingCommands.SetPageSize(sizeof(StopForwardingStruct)*16); +} +UDPForwarder::~UDPForwarder() +{ + Shutdown(); + +#ifdef _WIN32 + WSAStartupSingleton::Deref(); +#endif +} +void UDPForwarder::Startup(void) +{ + if (isRunning.GetValue()>0) + return; + + isRunning.Increment(); + + int errorCode; + + + + errorCode = RakNet::RakThread::Create(UpdateUDPForwarderGlobal, this); + + if ( errorCode != 0 ) + { + RakAssert(0); + return; + } + + while (threadRunning.GetValue()==0) + RakSleep(30); +} +void UDPForwarder::Shutdown(void) +{ + if (isRunning.GetValue()==0) + return; + isRunning.Decrement(); + + while (threadRunning.GetValue()>0) + RakSleep(30); + + unsigned int j; + for (j=0; j < forwardListNotUpdated.Size(); j++) + RakNet::OP_DELETE(forwardListNotUpdated[j],_FILE_AND_LINE_); + forwardListNotUpdated.Clear(false, _FILE_AND_LINE_); +} +void UDPForwarder::SetMaxForwardEntries(unsigned short maxEntries) +{ + RakAssert(maxEntries>0 && maxEntries<65535/2); + maxForwardEntries=maxEntries; +} +int UDPForwarder::GetMaxForwardEntries(void) const +{ + return maxForwardEntries; +} +int UDPForwarder::GetUsedForwardEntries(void) const +{ + return (int) forwardListNotUpdated.Size(); +} +UDPForwarderResult UDPForwarder::StartForwarding(SystemAddress source, SystemAddress destination, RakNet::TimeMS timeoutOnNoDataMS, const char *forceHostAddress, unsigned short socketFamily, + unsigned short *forwardingPort, __UDPSOCKET__ *forwardingSocket) +{ + // Invalid parameters? + if (timeoutOnNoDataMS == 0 || timeoutOnNoDataMS > UDP_FORWARDER_MAXIMUM_TIMEOUT || source==UNASSIGNED_SYSTEM_ADDRESS || destination==UNASSIGNED_SYSTEM_ADDRESS) + return UDPFORWARDER_INVALID_PARAMETERS; + + if (isRunning.GetValue()==0) + return UDPFORWARDER_NOT_RUNNING; + + (void) socketFamily; + + unsigned int inputId = nextInputId++; + + StartForwardingInputStruct *sfis; + sfis = startForwardingInput.Allocate(_FILE_AND_LINE_); + sfis->source=source; + sfis->destination=destination; + sfis->timeoutOnNoDataMS=timeoutOnNoDataMS; + RakAssert(timeoutOnNoDataMS!=0); + if (forceHostAddress && forceHostAddress[0]) + sfis->forceHostAddress=forceHostAddress; + sfis->socketFamily=socketFamily; + sfis->inputId=inputId; + startForwardingInput.Push(sfis); + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + while (1) + { + RakSleep(0); + startForwardingOutputMutex.Lock(); + for (unsigned int i=0; i < startForwardingOutput.Size(); i++) + { + if (startForwardingOutput[i].inputId==inputId) + { + if (startForwardingOutput[i].result==UDPFORWARDER_SUCCESS) + { + if (forwardingPort) + *forwardingPort = startForwardingOutput[i].forwardingPort; + if (forwardingSocket) + *forwardingSocket = startForwardingOutput[i].forwardingSocket; + } + UDPForwarderResult res = startForwardingOutput[i].result; + startForwardingOutput.RemoveAtIndex(i); + startForwardingOutputMutex.Unlock(); + return res; + } + } + startForwardingOutputMutex.Unlock(); + } + + return UDPFORWARDER_RESULT_COUNT; +} +void UDPForwarder::StopForwarding(SystemAddress source, SystemAddress destination) +{ + StopForwardingStruct *sfs; + sfs = stopForwardingCommands.Allocate(_FILE_AND_LINE_); + sfs->destination=destination; + sfs->source=source; + stopForwardingCommands.Push(sfs); +} +void UDPForwarder::RecvFrom(RakNet::TimeMS curTime, ForwardEntry *forwardEntry) +{ +#ifndef __native_client__ + char data[ MAXIMUM_MTU_SIZE ]; + +#if RAKNET_SUPPORT_IPV6==1 + sockaddr_storage their_addr; + memset(&their_addr,0,sizeof(their_addr)); + sockaddr* sockAddrPtr; + socklen_t sockLen; + socklen_t* socketlenPtr=(socklen_t*) &sockLen; + sockaddr_in *sockAddrIn; + sockaddr_in6 *sockAddrIn6; + sockLen=sizeof(their_addr); + sockAddrPtr=(sockaddr*) &their_addr; +#else + sockaddr_in sockAddrIn; + memset(&sockAddrIn,0,sizeof(sockaddr_in)); + socklen_t len2; + len2 = sizeof( sockAddrIn ); + sockAddrIn.sin_family = AF_INET; +#endif + +#if defined(__GNUC__) + #if defined(MSG_DONTWAIT) + const int flag=MSG_DONTWAIT; + #else + const int flag=0x40; + #endif +#else + const int flag=0; +#endif + + int receivedDataLen, len=0; + //unsigned short portnum=0; + +#if RAKNET_SUPPORT_IPV6==1 + receivedDataLen = recvfrom__( forwardEntry->socket, data, MAXIMUM_MTU_SIZE, flag, sockAddrPtr, socketlenPtr ); +#else + receivedDataLen = recvfrom__( forwardEntry->socket, data, MAXIMUM_MTU_SIZE, flag, ( sockaddr* ) & sockAddrIn, ( socklen_t* ) & len2 ); +#endif + + if (receivedDataLen<0) + { +#if defined(_WIN32) && defined(_DEBUG) && !defined(WINDOWS_PHONE_8) && !defined(WINDOWS_STORE_RT) + DWORD dwIOError = WSAGetLastError(); + + if (dwIOError!=WSAECONNRESET && dwIOError!=WSAEINTR && dwIOError!=WSAETIMEDOUT && dwIOError!=WSAEWOULDBLOCK) + { + LPVOID messageBuffer; + FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language + ( LPTSTR ) & messageBuffer, 0, nullptr ); + // something has gone wrong here... + RAKNET_DEBUG_PRINTF( "recvfrom failed:Error code - %d\n%s", dwIOError, messageBuffer ); + + //Free the buffer. + LocalFree( messageBuffer ); + } +#else + if (errno!=EAGAIN + && errno!=0 +#if defined(__GNUC__) + && errno!=EWOULDBLOCK +#endif + ) + { + printf("errno=%i\n", errno); + } +#endif + + + } + + if (receivedDataLen<=0) + return; + + SystemAddress receivedAddr; +#if RAKNET_SUPPORT_IPV6==1 + if (their_addr.ss_family==AF_INET) + { + sockAddrIn=(sockaddr_in *)&their_addr; + sockAddrIn6=0; + memcpy(&receivedAddr.address.addr4,sockAddrIn,sizeof(sockaddr_in)); + } + else + { + sockAddrIn=0; + sockAddrIn6=(sockaddr_in6 *)&their_addr; + memcpy(&receivedAddr.address.addr6,sockAddrIn6,sizeof(sockaddr_in6)); + } +#else + memcpy(&receivedAddr.address.addr4,&sockAddrIn,sizeof(sockaddr_in)); +#endif + //portnum=receivedAddr.GetPort(); + + SystemAddress forwardTarget; + + bool confirmed1 = forwardEntry->addr1Confirmed!=UNASSIGNED_SYSTEM_ADDRESS; + bool confirmed2 = forwardEntry->addr2Confirmed!=UNASSIGNED_SYSTEM_ADDRESS; + bool matchConfirmed1 = + confirmed1 && + forwardEntry->addr1Confirmed==receivedAddr; + bool matchConfirmed2 = + confirmed2 && + forwardEntry->addr2Confirmed==receivedAddr; + bool matchUnconfirmed1 = forwardEntry->addr1Unconfirmed.EqualsExcludingPort(receivedAddr); + bool matchUnconfirmed2 = forwardEntry->addr2Unconfirmed.EqualsExcludingPort(receivedAddr); + + if (matchConfirmed1==true || (matchConfirmed2==false && confirmed1==false && matchUnconfirmed1==true)) + { + // Forward to addr2 + if (forwardEntry->addr1Confirmed==UNASSIGNED_SYSTEM_ADDRESS) + { + forwardEntry->addr1Confirmed=receivedAddr; + } + if (forwardEntry->addr2Confirmed!=UNASSIGNED_SYSTEM_ADDRESS) + forwardTarget=forwardEntry->addr2Confirmed; + else + forwardTarget=forwardEntry->addr2Unconfirmed; + } + else if (matchConfirmed2==true || (confirmed2==false && matchUnconfirmed2==true)) + { + // Forward to addr1 + if (forwardEntry->addr2Confirmed==UNASSIGNED_SYSTEM_ADDRESS) + { + forwardEntry->addr2Confirmed=receivedAddr; + } + if (forwardEntry->addr1Confirmed!=UNASSIGNED_SYSTEM_ADDRESS) + forwardTarget=forwardEntry->addr1Confirmed; + else + forwardTarget=forwardEntry->addr1Unconfirmed; + } + else + { + return; + } + + // Forward to dest + len=0; +// sockaddr_in saOut; +// saOut.sin_port = forwardTarget.GetPortNetworkOrder(); // User port +// saOut.sin_addr.s_addr = forwardTarget.address.addr4.sin_addr.s_addr; +// saOut.sin_family = AF_INET; + do + { + + + +#if RAKNET_SUPPORT_IPV6==1 + if (forwardTarget.address.addr4.sin_family==AF_INET) + { + do + { + len = sendto__( forwardEntry->socket, data, receivedDataLen, 0, ( const sockaddr* ) & forwardTarget.address.addr4, sizeof( sockaddr_in ) ); + } + while ( len == 0 ); + } + else + { + do + { + len = sendto__( forwardEntry->socket, data, receivedDataLen, 0, ( const sockaddr* ) & forwardTarget.address.addr6, sizeof( sockaddr_in6 ) ); + } + while ( len == 0 ); + } +#else + do + { + len = sendto__( forwardEntry->socket, data, receivedDataLen, 0, ( const sockaddr* ) & forwardTarget.address.addr4, sizeof( sockaddr_in ) ); + } + while ( len == 0 ); +#endif + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + } + while ( len == 0 ); + + forwardEntry->timeLastDatagramForwarded=curTime; +#endif // __native_client__ +} +void UDPForwarder::UpdateUDPForwarder(void) +{ + /* +#if !defined(SN_TARGET_PSP2) + timeval tv; + tv.tv_sec=0; + tv.tv_usec=0; +#endif + */ + + RakNet::TimeMS curTime = RakNet::GetTimeMS(); + + StartForwardingInputStruct *sfis; + StartForwardingOutputStruct sfos; + sfos.forwardingSocket=INVALID_SOCKET; + sfos.forwardingPort=0; + sfos.inputId=0; + sfos.result=UDPFORWARDER_RESULT_COUNT; + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + while (1) + { + sfis = startForwardingInput.Pop(); + if (sfis==0) + break; + + if (GetUsedForwardEntries()>maxForwardEntries) + { + sfos.result=UDPFORWARDER_NO_SOCKETS; + } + else + { + sfos.result=UDPFORWARDER_RESULT_COUNT; + + for (unsigned int i=0; i < forwardListNotUpdated.Size(); i++) + { + if ( + (forwardListNotUpdated[i]->addr1Unconfirmed==sfis->source && + forwardListNotUpdated[i]->addr2Unconfirmed==sfis->destination) + || + (forwardListNotUpdated[i]->addr1Unconfirmed==sfis->destination && + forwardListNotUpdated[i]->addr2Unconfirmed==sfis->source) + ) + { + ForwardEntry *fe = forwardListNotUpdated[i]; + sfos.forwardingPort = SocketLayer::GetLocalPort ( fe->socket ); + sfos.forwardingSocket=fe->socket; + sfos.result=UDPFORWARDER_FORWARDING_ALREADY_EXISTS; + break; + } + } + + if (sfos.result==UDPFORWARDER_RESULT_COUNT) + { + int sock_opt; + sockaddr_in listenerSocketAddress; + listenerSocketAddress.sin_port = 0; + ForwardEntry *fe = RakNet::OP_NEW(_FILE_AND_LINE_); + fe->addr1Unconfirmed=sfis->source; + fe->addr2Unconfirmed=sfis->destination; + fe->timeoutOnNoDataMS=sfis->timeoutOnNoDataMS; + +#if RAKNET_SUPPORT_IPV6!=1 + fe->socket = socket__( AF_INET, SOCK_DGRAM, 0 ); + listenerSocketAddress.sin_family = AF_INET; + if (sfis->forceHostAddress.IsEmpty()==false) + { + + + + + + listenerSocketAddress.sin_addr.s_addr = inet_addr__( sfis->forceHostAddress.C_String() ); + + } + else + { + listenerSocketAddress.sin_addr.s_addr = INADDR_ANY; + } + int ret = bind__( fe->socket, ( struct sockaddr * ) & listenerSocketAddress, sizeof( listenerSocketAddress ) ); + if (ret==-1) + { + RakNet::OP_DELETE(fe,_FILE_AND_LINE_); + sfos.result=UDPFORWARDER_BIND_FAILED; + } + else + { + sfos.result=UDPFORWARDER_SUCCESS; + } + +#else // RAKNET_SUPPORT_IPV6==1 + struct addrinfo hints; + memset(&hints, 0, sizeof (addrinfo)); // make sure the struct is empty + hints.ai_family = sfis->socketFamily; + hints.ai_socktype = SOCK_DGRAM; // UDP sockets + hints.ai_flags = AI_PASSIVE; // fill in my IP for me + struct addrinfo *servinfo=0, *aip; // will point to the results + + if (sfis->forceHostAddress.IsEmpty() || sfis->forceHostAddress=="UNASSIGNED_SYSTEM_ADDRESS") + getaddrinfo(0, "0", &hints, &servinfo); + else + getaddrinfo(sfis->forceHostAddress.C_String(), "0", &hints, &servinfo); + + for (aip = servinfo; aip != nullptr; aip = aip->ai_next) + { + // Open socket. The address type depends on what + // getaddrinfo() gave us. + fe->socket = socket__(aip->ai_family, aip->ai_socktype, aip->ai_protocol); + if (fe->socket != INVALID_SOCKET) + { + int ret = bind__( fe->socket, aip->ai_addr, (int) aip->ai_addrlen ); + if (ret>=0) + { + break; + } + else + { + closesocket__(fe->socket); + fe->socket=INVALID_SOCKET; + } + } + } + + if (fe->socket==INVALID_SOCKET) + sfos.result=UDPFORWARDER_BIND_FAILED; + else + sfos.result=UDPFORWARDER_SUCCESS; +#endif // RAKNET_SUPPORT_IPV6==1 + + if (sfos.result==UDPFORWARDER_SUCCESS) + { + sfos.forwardingPort = SocketLayer::GetLocalPort ( fe->socket ); + sfos.forwardingSocket=fe->socket; + + sock_opt=1024*256; + setsockopt__(fe->socket, SOL_SOCKET, SO_RCVBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) ); + sock_opt=0; + setsockopt__(fe->socket, SOL_SOCKET, SO_LINGER, ( char * ) & sock_opt, sizeof ( sock_opt ) ); +#ifdef _WIN32 + unsigned long nonblocking = 1; + ioctlsocket__( fe->socket, FIONBIO, &nonblocking ); + + + +#else + fcntl( fe->socket, F_SETFL, O_NONBLOCK ); +#endif + + forwardListNotUpdated.Insert(fe,_FILE_AND_LINE_); + } + } + } + + // Push result + sfos.inputId=sfis->inputId; + startForwardingOutputMutex.Lock(); + startForwardingOutput.Push(sfos,_FILE_AND_LINE_); + startForwardingOutputMutex.Unlock(); + + startForwardingInput.Deallocate(sfis, _FILE_AND_LINE_); + } + + StopForwardingStruct *sfs; + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant +#endif + while (1) + { + sfs = stopForwardingCommands.Pop(); + if (sfs==0) + break; + + ForwardEntry *fe; + for (unsigned int i=0; i < forwardListNotUpdated.Size(); i++) + { + if ( + (forwardListNotUpdated[i]->addr1Unconfirmed==sfs->source && + forwardListNotUpdated[i]->addr2Unconfirmed==sfs->destination) + || + (forwardListNotUpdated[i]->addr1Unconfirmed==sfs->destination && + forwardListNotUpdated[i]->addr2Unconfirmed==sfs->source) + ) + { + fe = forwardListNotUpdated[i]; + forwardListNotUpdated.RemoveAtIndexFast(i); + RakNet::OP_DELETE(fe, _FILE_AND_LINE_); + break; + } + } + + stopForwardingCommands.Deallocate(sfs, _FILE_AND_LINE_); + } + + unsigned int i; + + i=0; + while (i < forwardListNotUpdated.Size()) + { + if (curTime > forwardListNotUpdated[i]->timeLastDatagramForwarded && // Account for timestamp wrap + curTime > forwardListNotUpdated[i]->timeLastDatagramForwarded+forwardListNotUpdated[i]->timeoutOnNoDataMS) + { + RakNet::OP_DELETE(forwardListNotUpdated[i],_FILE_AND_LINE_); + forwardListNotUpdated.RemoveAtIndex(i); + } + else + i++; + } + + ForwardEntry *forwardEntry; + for (i=0; i < forwardListNotUpdated.Size(); i++) + { + forwardEntry = forwardListNotUpdated[i]; + RecvFrom(curTime, forwardEntry); + } +} + +namespace RakNet { +RAK_THREAD_DECLARATION(UpdateUDPForwarderGlobal) +{ + + + + UDPForwarder * udpForwarder = ( UDPForwarder * ) arguments; + + + udpForwarder->threadRunning.Increment(); + while (udpForwarder->isRunning.GetValue()>0) + { + udpForwarder->UpdateUDPForwarder(); + + // 12/1/2010 Do not change from 0 + // See http://www.jenkinssoftware.com/forum/index.php?topic=4033.0;topicseen + // Avoid 100% reported CPU usage + if (udpForwarder->forwardListNotUpdated.Size()==0) + RakSleep(30); + else + RakSleep(0); + } + udpForwarder->threadRunning.Decrement(); + + + + + return 0; + + +} + +} // namespace RakNet + +#endif // #if _RAKNET_SUPPORT_FileOperations==1 diff --git a/Source/UDPForwarder.h b/Source/UDPForwarder.h index 3bf7cb7e1..45754f9d1 100644 --- a/Source/UDPForwarder.h +++ b/Source/UDPForwarder.h @@ -1,159 +1,157 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Forwards UDP datagrams. Independent of RakNet's protocol. -/// - - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_UDPForwarder==1 - -#ifndef __UDP_FORWARDER_H -#define __UDP_FORWARDER_H - -#include "Export.h" -#include "RakNetTypes.h" -#include "SocketIncludes.h" -#include "UDPProxyCommon.h" -#include "SimpleMutex.h" -#include "RakString.h" -#include "RakThread.h" -#include "DS_Queue.h" -#include "DS_OrderedList.h" -#include "LocklessTypes.h" -#include "DS_ThreadsafeAllocatingQueue.h" - -namespace RakNet -{ - -enum UDPForwarderResult -{ - UDPFORWARDER_FORWARDING_ALREADY_EXISTS, - UDPFORWARDER_NO_SOCKETS, - UDPFORWARDER_BIND_FAILED, - UDPFORWARDER_INVALID_PARAMETERS, - UDPFORWARDER_NOT_RUNNING, - UDPFORWARDER_SUCCESS, - UDPFORWARDER_RESULT_COUNT -}; - -/// \brief Forwards UDP datagrams. Independent of RakNet's protocol. -/// \ingroup NAT_PUNCHTHROUGH_GROUP -class RAK_DLL_EXPORT UDPForwarder -{ -public: - UDPForwarder(); - virtual ~UDPForwarder(); - - /// Starts the system. - /// Required to call before StartForwarding - void Startup(void); - - /// Stops the system, and frees all sockets - void Shutdown(void); - - /// Sets the maximum number of forwarding entries allowed - /// Set according to your available bandwidth and the estimated average bandwidth per forwarded address. - /// \param[in] maxEntries The maximum number of simultaneous forwarding entries. Defaults to 64 (32 connections) - void SetMaxForwardEntries(unsigned short maxEntries); - - /// \return The \a maxEntries parameter passed to SetMaxForwardEntries(), or the default if it was never called - int GetMaxForwardEntries(void) const; - - /// \return How many entries have been used - int GetUsedForwardEntries(void) const; - - /// Forwards datagrams from source to destination, and vice-versa - /// Does nothing if this forward entry already exists via a previous call - /// \pre Call Startup() - /// \note RakNet's protocol will ensure a message is sent at least every 15 seconds, so if routing RakNet messages, it is a reasonable value for timeoutOnNoDataMS, plus an some extra seconds for latency - /// \param[in] source The source IP and port - /// \param[in] destination Where to forward to (and vice-versa) - /// \param[in] timeoutOnNoDataMS If no messages are forwarded for this many MS, then automatically remove this entry. - /// \param[in] forceHostAddress Force binding on a particular address. 0 to use any. - /// \param[in] socketFamily IP version: For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC. - /// \param[out] forwardingPort New opened port for forwarding - /// \param[out] forwardingSocket New opened socket for forwarding - /// \return UDPForwarderResult - UDPForwarderResult StartForwarding( - SystemAddress source, SystemAddress destination, RakNet::TimeMS timeoutOnNoDataMS, - const char *forceHostAddress, unsigned short socketFamily, - unsigned short *forwardingPort, __UDPSOCKET__ *forwardingSocket); - - /// No longer forward datagrams from source to destination - /// \param[in] source The source IP and port - /// \param[in] destination Where to forward to - void StopForwarding(SystemAddress source, SystemAddress destination); - - - struct ForwardEntry - { - ForwardEntry(); - ~ForwardEntry(); - SystemAddress addr1Unconfirmed, addr2Unconfirmed, addr1Confirmed, addr2Confirmed; - RakNet::TimeMS timeLastDatagramForwarded; - __UDPSOCKET__ socket; - RakNet::TimeMS timeoutOnNoDataMS; - short socketFamily; - }; - - -protected: - friend RAK_THREAD_DECLARATION(UpdateUDPForwarderGlobal); - - void UpdateUDPForwarder(void); - void RecvFrom(RakNet::TimeMS curTime, ForwardEntry *forwardEntry); - - struct StartForwardingInputStruct - { - SystemAddress source; - SystemAddress destination; - RakNet::TimeMS timeoutOnNoDataMS; - RakString forceHostAddress; - unsigned short socketFamily; - unsigned int inputId; - }; - - DataStructures::ThreadsafeAllocatingQueue startForwardingInput; - - struct StartForwardingOutputStruct - { - unsigned short forwardingPort; - __UDPSOCKET__ forwardingSocket; - UDPForwarderResult result; - unsigned int inputId; - }; - DataStructures::Queue startForwardingOutput; - SimpleMutex startForwardingOutputMutex; - - struct StopForwardingStruct - { - SystemAddress source; - SystemAddress destination; - }; - DataStructures::ThreadsafeAllocatingQueue stopForwardingCommands; - unsigned int nextInputId; - - // New entries are added to forwardListNotUpdated - DataStructures::List forwardListNotUpdated; -// SimpleMutex forwardListNotUpdatedMutex; - - unsigned short maxForwardEntries; - RakNet::LocklessUint32_t isRunning, threadRunning; - -}; - -} // End namespace - -#endif - -#endif // #if _RAKNET_SUPPORT_UDPForwarder==1 +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Forwards UDP datagrams. Independent of RakNet's protocol. +/// + + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_UDPForwarder==1 + +#pragma once + +#include "Export.h" +#include "RakNetTypes.h" +#include "SocketIncludes.h" +#include "UDPProxyCommon.h" +#include "SimpleMutex.h" +#include "RakString.h" +#include "RakThread.h" +#include "DS_Queue.h" +#include "DS_OrderedList.h" +#include "LocklessTypes.h" +#include "DS_ThreadsafeAllocatingQueue.h" + +namespace RakNet +{ + +enum UDPForwarderResult +{ + UDPFORWARDER_FORWARDING_ALREADY_EXISTS, + UDPFORWARDER_NO_SOCKETS, + UDPFORWARDER_BIND_FAILED, + UDPFORWARDER_INVALID_PARAMETERS, + UDPFORWARDER_NOT_RUNNING, + UDPFORWARDER_SUCCESS, + UDPFORWARDER_RESULT_COUNT +}; + +/// \brief Forwards UDP datagrams. Independent of RakNet's protocol. +/// \ingroup NAT_PUNCHTHROUGH_GROUP +class RAK_DLL_EXPORT UDPForwarder +{ +public: + UDPForwarder(); + virtual ~UDPForwarder(); + + /// Starts the system. + /// Required to call before StartForwarding + void Startup(void); + + /// Stops the system, and frees all sockets + void Shutdown(void); + + /// Sets the maximum number of forwarding entries allowed + /// Set according to your available bandwidth and the estimated average bandwidth per forwarded address. + /// \param[in] maxEntries The maximum number of simultaneous forwarding entries. Defaults to 64 (32 connections) + void SetMaxForwardEntries(unsigned short maxEntries); + + /// \return The \a maxEntries parameter passed to SetMaxForwardEntries(), or the default if it was never called + int GetMaxForwardEntries(void) const; + + /// \return How many entries have been used + int GetUsedForwardEntries(void) const; + + /// Forwards datagrams from source to destination, and vice-versa + /// Does nothing if this forward entry already exists via a previous call + /// \pre Call Startup() + /// \note RakNet's protocol will ensure a message is sent at least every 15 seconds, so if routing RakNet messages, it is a reasonable value for timeoutOnNoDataMS, plus an some extra seconds for latency + /// \param[in] source The source IP and port + /// \param[in] destination Where to forward to (and vice-versa) + /// \param[in] timeoutOnNoDataMS If no messages are forwarded for this many MS, then automatically remove this entry. + /// \param[in] forceHostAddress Force binding on a particular address. 0 to use any. + /// \param[in] socketFamily IP version: For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC. + /// \param[out] forwardingPort New opened port for forwarding + /// \param[out] forwardingSocket New opened socket for forwarding + /// \return UDPForwarderResult + UDPForwarderResult StartForwarding( + SystemAddress source, SystemAddress destination, RakNet::TimeMS timeoutOnNoDataMS, + const char *forceHostAddress, unsigned short socketFamily, + unsigned short *forwardingPort, __UDPSOCKET__ *forwardingSocket); + + /// No longer forward datagrams from source to destination + /// \param[in] source The source IP and port + /// \param[in] destination Where to forward to + void StopForwarding(SystemAddress source, SystemAddress destination); + + + struct ForwardEntry + { + ForwardEntry(); + ~ForwardEntry(); + SystemAddress addr1Unconfirmed, addr2Unconfirmed, addr1Confirmed, addr2Confirmed; + RakNet::TimeMS timeLastDatagramForwarded; + __UDPSOCKET__ socket; + RakNet::TimeMS timeoutOnNoDataMS; + short socketFamily; + }; + + +protected: + friend RAK_THREAD_DECLARATION(UpdateUDPForwarderGlobal); + + void UpdateUDPForwarder(void); + void RecvFrom(RakNet::TimeMS curTime, ForwardEntry *forwardEntry); + + struct StartForwardingInputStruct + { + SystemAddress source; + SystemAddress destination; + RakNet::TimeMS timeoutOnNoDataMS; + RakString forceHostAddress; + unsigned short socketFamily; + unsigned int inputId; + }; + + DataStructures::ThreadsafeAllocatingQueue startForwardingInput; + + struct StartForwardingOutputStruct + { + unsigned short forwardingPort; + __UDPSOCKET__ forwardingSocket; + UDPForwarderResult result; + unsigned int inputId; + }; + DataStructures::Queue startForwardingOutput; + SimpleMutex startForwardingOutputMutex; + + struct StopForwardingStruct + { + SystemAddress source; + SystemAddress destination; + }; + DataStructures::ThreadsafeAllocatingQueue stopForwardingCommands; + unsigned int nextInputId; + + // New entries are added to forwardListNotUpdated + DataStructures::List forwardListNotUpdated; +// SimpleMutex forwardListNotUpdatedMutex; + + unsigned short maxForwardEntries; + RakNet::LocklessUint32_t isRunning, threadRunning; + +}; + +} // End namespace + +#endif + diff --git a/Source/UDPProxyClient.h b/Source/UDPProxyClient.h index eb2de53d6..3bdc736a0 100644 --- a/Source/UDPProxyClient.h +++ b/Source/UDPProxyClient.h @@ -1,181 +1,179 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief A RakNet plugin performing networking to communicate with UDPProxyCoordinator. Ultimately used to tell UDPProxyServer to forward UDP packets. - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_UDPProxyClient==1 - -#ifndef __UDP_PROXY_CLIENT_H -#define __UDP_PROXY_CLIENT_H - -#include "Export.h" -#include "RakNetTypes.h" -#include "PluginInterface2.h" -#include "DS_List.h" - -/// \defgroup UDP_PROXY_GROUP UDPProxy -/// \brief Forwards UDP datagrams from one system to another. Protocol independent -/// \details Used when NatPunchthroughClient fails -/// \ingroup PLUGINS_GROUP - -namespace RakNet -{ -class UDPProxyClient; - -/// Callback to handle results of calling UDPProxyClient::RequestForwarding() -/// \ingroup UDP_PROXY_GROUP -struct UDPProxyClientResultHandler -{ - UDPProxyClientResultHandler() {} - virtual ~UDPProxyClientResultHandler() {} - - /// Called when our forwarding request was completed. We can now connect to \a targetAddress by using \a proxyAddress instead - /// \param[out] proxyIPAddress IP Address of the proxy server, which will forward messages to targetAddress - /// \param[out] proxyPort Remote port to use on the proxy server, which will forward messages to targetAddress - /// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address. - /// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] proxyClient The plugin that is calling this callback - virtual void OnForwardingSuccess(const char *proxyIPAddress, unsigned short proxyPort, - SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0; - - /// Called when another system has setup forwarding, with our system as the target address. - /// Plugin automatically sends a datagram to proxyIPAddress before this callback, to open our router if necessary. - /// \param[out] proxyIPAddress IP Address of the proxy server, which will forward messages to targetAddress - /// \param[out] proxyPort Remote port to use on the proxy server, which will forward messages to targetAddress - /// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. This is originating source IP address of the remote system that will be sending to us. - /// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding. This is our external IP address. - /// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] proxyClient The plugin that is calling this callback - virtual void OnForwardingNotification(const char *proxyIPAddress, unsigned short proxyPort, - SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0; - - /// Called when our forwarding request failed, because no UDPProxyServers are connected to UDPProxyCoordinator - /// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address. - /// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] proxyClient The plugin that is calling this callback - virtual void OnNoServersOnline(SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0; - - /// Called when our forwarding request failed, because no UDPProxyServers are connected to UDPProxyCoordinator - /// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address. - /// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] proxyClient The plugin that is calling this callback - virtual void OnRecipientNotConnected(SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0; - - /// Called when our forwarding request failed, because all UDPProxyServers that are connected to UDPProxyCoordinator are at their capacity - /// Either add more servers, or increase capacity via UDPForwarder::SetMaxForwardEntries() - /// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address. - /// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] proxyClient The plugin that is calling this callback - virtual void OnAllServersBusy(SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0; - - /// Called when our forwarding request is already in progress on the \a proxyCoordinator. - /// This can be ignored, but indicates an unneeded second request - /// \param[out] proxyIPAddress IP Address of the proxy server, which is forwarding messages to targetAddress - /// \param[out] proxyPort Remote port to use on the proxy server, which is forwarding messages to targetAddress - /// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address. - /// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding - /// \param[out] proxyClient The plugin that is calling this callback - virtual void OnForwardingInProgress(const char *proxyIPAddress, unsigned short proxyPort, SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0; -}; - - -/// \brief Communicates with UDPProxyCoordinator, in order to find a UDPProxyServer to forward our datagrams. -/// \details When NAT Punchthrough fails, it is possible to use a non-NAT system to forward messages from us to the recipient, and vice-versa.
      -/// The class to forward messages is UDPForwarder, and it is triggered over the network via the UDPProxyServer plugin.
      -/// The UDPProxyClient connects to UDPProxyCoordinator to get a list of servers running UDPProxyServer, and the coordinator will relay our forwarding request -/// \sa NatPunchthroughServer -/// \sa NatPunchthroughClient -/// \ingroup UDP_PROXY_GROUP -class RAK_DLL_EXPORT UDPProxyClient : public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(UDPProxyClient) - - UDPProxyClient(); - ~UDPProxyClient(); - - /// Receives the results of calling RequestForwarding() - /// Set before calling RequestForwarding or you won't know what happened - /// \param[in] resultHandler - void SetResultHandler(UDPProxyClientResultHandler *rh); - - /// Sends a request to proxyCoordinator to find a server and have that server setup UDPForwarder::StartForwarding() on our address to \a targetAddressAsSeenFromCoordinator - /// The forwarded datagrams can be from any UDP source, not just RakNet - /// \pre Must be connected to \a proxyCoordinator - /// \pre Systems running UDPProxyServer must be connected to \a proxyCoordinator and logged in via UDPProxyCoordinator::LoginServer() or UDPProxyServer::LoginToCoordinator() - /// \note May still fail, if all proxy servers have no open connections. - /// \note RakNet's protocol will ensure a message is sent at least every 5 seconds, so if routing RakNet messages, it is a reasonable value for timeoutOnNoDataMS, plus an extra few seconds for latency. - /// \param[in] proxyCoordinator System we are connected to that is running the UDPProxyCoordinator plugin - /// \param[in] sourceAddress External IP address of the system we want to forward messages from. This does not have to be our own system. To specify our own system, you can pass UNASSIGNED_SYSTEM_ADDRESS which the coordinator will treat as our external IP address. - /// \param[in] targetAddressAsSeenFromCoordinator External IP address of the system we want to forward messages to. If this system is connected to UDPProxyCoordinator at this address using RakNet, that system will ping the server and thus open the router for incoming communication. In any other case, you are responsible for doing your own network communication to have that system ping the server. See also targetGuid in the other version of RequestForwarding(), to avoid the need to know the IP address to the coordinator of the destination. - /// \param[in] timeoutOnNoData If no data is sent by the forwarded systems, how long before removing the forward entry from UDPForwarder? UDP_FORWARDER_MAXIMUM_TIMEOUT is the maximum value. Recommended 10 seconds. - /// \param[in] serverSelectionBitstream If you want to send data to UDPProxyCoordinator::GetBestServer(), write it here - /// \return true if the request was sent, false if we are not connected to proxyCoordinator - bool RequestForwarding(SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddressAsSeenFromCoordinator, RakNet::TimeMS timeoutOnNoDataMS, RakNet::BitStream *serverSelectionBitstream=0); - - /// Same as above, but specify the target with a GUID, in case you don't know what its address is to the coordinator - /// If requesting forwarding to a RakNet enabled system, then it is easier to use targetGuid instead of targetAddressAsSeenFromCoordinator - bool RequestForwarding(SystemAddress proxyCoordinator, SystemAddress sourceAddress, RakNetGUID targetGuid, RakNet::TimeMS timeoutOnNoDataMS, RakNet::BitStream *serverSelectionBitstream=0); - - /// \internal - virtual void Update(void); - virtual PluginReceiveResult OnReceive(Packet *packet); - virtual void OnRakPeerShutdown(void); - - struct ServerWithPing - { - unsigned short ping; - SystemAddress serverAddress; - }; - struct SenderAndTargetAddress - { - SystemAddress senderClientAddress; - SystemAddress targetClientAddress; - }; - struct PingServerGroup - { - SenderAndTargetAddress sata; - RakNet::TimeMS startPingTime; - SystemAddress coordinatorAddressForPings; - //DataStructures::Multilist serversToPing; - DataStructures::List serversToPing; - bool AreAllServersPinged(void) const; - void SendPingedServersToCoordinator(RakPeerInterface *rakPeerInterface); - }; - //DataStructures::Multilist pingServerGroups; - DataStructures::List pingServerGroups; -protected: - - void OnPingServers(Packet *packet); - void Clear(void); - UDPProxyClientResultHandler *resultHandler; - -}; - -} // End namespace - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief A RakNet plugin performing networking to communicate with UDPProxyCoordinator. Ultimately used to tell UDPProxyServer to forward UDP packets. + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_UDPProxyClient==1 + +#pragma once + +#include "Export.h" +#include "RakNetTypes.h" +#include "PluginInterface2.h" +#include "DS_List.h" + +/// \defgroup UDP_PROXY_GROUP UDPProxy +/// \brief Forwards UDP datagrams from one system to another. Protocol independent +/// \details Used when NatPunchthroughClient fails +/// \ingroup PLUGINS_GROUP + +namespace RakNet +{ +class UDPProxyClient; + +/// Callback to handle results of calling UDPProxyClient::RequestForwarding() +/// \ingroup UDP_PROXY_GROUP +struct UDPProxyClientResultHandler +{ + UDPProxyClientResultHandler() {} + virtual ~UDPProxyClientResultHandler() {} + + /// Called when our forwarding request was completed. We can now connect to \a targetAddress by using \a proxyAddress instead + /// \param[out] proxyIPAddress IP Address of the proxy server, which will forward messages to targetAddress + /// \param[out] proxyPort Remote port to use on the proxy server, which will forward messages to targetAddress + /// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address. + /// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] proxyClient The plugin that is calling this callback + virtual void OnForwardingSuccess(const char *proxyIPAddress, unsigned short proxyPort, + SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0; + + /// Called when another system has setup forwarding, with our system as the target address. + /// Plugin automatically sends a datagram to proxyIPAddress before this callback, to open our router if necessary. + /// \param[out] proxyIPAddress IP Address of the proxy server, which will forward messages to targetAddress + /// \param[out] proxyPort Remote port to use on the proxy server, which will forward messages to targetAddress + /// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. This is originating source IP address of the remote system that will be sending to us. + /// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding. This is our external IP address. + /// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] proxyClient The plugin that is calling this callback + virtual void OnForwardingNotification(const char *proxyIPAddress, unsigned short proxyPort, + SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0; + + /// Called when our forwarding request failed, because no UDPProxyServers are connected to UDPProxyCoordinator + /// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address. + /// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] proxyClient The plugin that is calling this callback + virtual void OnNoServersOnline(SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0; + + /// Called when our forwarding request failed, because no UDPProxyServers are connected to UDPProxyCoordinator + /// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address. + /// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] proxyClient The plugin that is calling this callback + virtual void OnRecipientNotConnected(SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0; + + /// Called when our forwarding request failed, because all UDPProxyServers that are connected to UDPProxyCoordinator are at their capacity + /// Either add more servers, or increase capacity via UDPForwarder::SetMaxForwardEntries() + /// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address. + /// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] proxyClient The plugin that is calling this callback + virtual void OnAllServersBusy(SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0; + + /// Called when our forwarding request is already in progress on the \a proxyCoordinator. + /// This can be ignored, but indicates an unneeded second request + /// \param[out] proxyIPAddress IP Address of the proxy server, which is forwarding messages to targetAddress + /// \param[out] proxyPort Remote port to use on the proxy server, which is forwarding messages to targetAddress + /// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address. + /// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding + /// \param[out] proxyClient The plugin that is calling this callback + virtual void OnForwardingInProgress(const char *proxyIPAddress, unsigned short proxyPort, SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0; +}; + + +/// \brief Communicates with UDPProxyCoordinator, in order to find a UDPProxyServer to forward our datagrams. +/// \details When NAT Punchthrough fails, it is possible to use a non-NAT system to forward messages from us to the recipient, and vice-versa.
      +/// The class to forward messages is UDPForwarder, and it is triggered over the network via the UDPProxyServer plugin.
      +/// The UDPProxyClient connects to UDPProxyCoordinator to get a list of servers running UDPProxyServer, and the coordinator will relay our forwarding request +/// \sa NatPunchthroughServer +/// \sa NatPunchthroughClient +/// \ingroup UDP_PROXY_GROUP +class RAK_DLL_EXPORT UDPProxyClient : public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(UDPProxyClient) + + UDPProxyClient(); + ~UDPProxyClient(); + + /// Receives the results of calling RequestForwarding() + /// Set before calling RequestForwarding or you won't know what happened + /// \param[in] resultHandler + void SetResultHandler(UDPProxyClientResultHandler *rh); + + /// Sends a request to proxyCoordinator to find a server and have that server setup UDPForwarder::StartForwarding() on our address to \a targetAddressAsSeenFromCoordinator + /// The forwarded datagrams can be from any UDP source, not just RakNet + /// \pre Must be connected to \a proxyCoordinator + /// \pre Systems running UDPProxyServer must be connected to \a proxyCoordinator and logged in via UDPProxyCoordinator::LoginServer() or UDPProxyServer::LoginToCoordinator() + /// \note May still fail, if all proxy servers have no open connections. + /// \note RakNet's protocol will ensure a message is sent at least every 5 seconds, so if routing RakNet messages, it is a reasonable value for timeoutOnNoDataMS, plus an extra few seconds for latency. + /// \param[in] proxyCoordinator System we are connected to that is running the UDPProxyCoordinator plugin + /// \param[in] sourceAddress External IP address of the system we want to forward messages from. This does not have to be our own system. To specify our own system, you can pass UNASSIGNED_SYSTEM_ADDRESS which the coordinator will treat as our external IP address. + /// \param[in] targetAddressAsSeenFromCoordinator External IP address of the system we want to forward messages to. If this system is connected to UDPProxyCoordinator at this address using RakNet, that system will ping the server and thus open the router for incoming communication. In any other case, you are responsible for doing your own network communication to have that system ping the server. See also targetGuid in the other version of RequestForwarding(), to avoid the need to know the IP address to the coordinator of the destination. + /// \param[in] timeoutOnNoData If no data is sent by the forwarded systems, how long before removing the forward entry from UDPForwarder? UDP_FORWARDER_MAXIMUM_TIMEOUT is the maximum value. Recommended 10 seconds. + /// \param[in] serverSelectionBitstream If you want to send data to UDPProxyCoordinator::GetBestServer(), write it here + /// \return true if the request was sent, false if we are not connected to proxyCoordinator + bool RequestForwarding(SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddressAsSeenFromCoordinator, RakNet::TimeMS timeoutOnNoDataMS, RakNet::BitStream *serverSelectionBitstream=0); + + /// Same as above, but specify the target with a GUID, in case you don't know what its address is to the coordinator + /// If requesting forwarding to a RakNet enabled system, then it is easier to use targetGuid instead of targetAddressAsSeenFromCoordinator + bool RequestForwarding(SystemAddress proxyCoordinator, SystemAddress sourceAddress, RakNetGUID targetGuid, RakNet::TimeMS timeoutOnNoDataMS, RakNet::BitStream *serverSelectionBitstream=0); + + /// \internal + virtual void Update(void); + virtual PluginReceiveResult OnReceive(Packet *packet); + virtual void OnRakPeerShutdown(void); + + struct ServerWithPing + { + unsigned short ping; + SystemAddress serverAddress; + }; + struct SenderAndTargetAddress + { + SystemAddress senderClientAddress; + SystemAddress targetClientAddress; + }; + struct PingServerGroup + { + SenderAndTargetAddress sata; + RakNet::TimeMS startPingTime; + SystemAddress coordinatorAddressForPings; + //DataStructures::Multilist serversToPing; + DataStructures::List serversToPing; + bool AreAllServersPinged(void) const; + void SendPingedServersToCoordinator(RakPeerInterface *rakPeerInterface); + }; + //DataStructures::Multilist pingServerGroups; + DataStructures::List pingServerGroups; +protected: + + void OnPingServers(Packet *packet); + void Clear(void); + UDPProxyClientResultHandler *resultHandler; + +}; + +} // End namespace + +#endif + diff --git a/Source/UDPProxyCommon.h b/Source/UDPProxyCommon.h index f74cdd37e..e9b6da400 100644 --- a/Source/UDPProxyCommon.h +++ b/Source/UDPProxyCommon.h @@ -1,67 +1,65 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __UDP_PROXY_COMMON_H -#define __UDP_PROXY_COMMON_H - -// System flow: -/* -UDPProxyClient: End user -UDPProxyServer: open server, to route messages from end users that can't connect to each other using UDPForwarder class. -UDPProxyCoordinator: Server somewhere, connected to by RakNet, to maintain a list of UDPProxyServer - -UDPProxyServer - On startup, log into UDPProxyCoordinator and register self - -UDPProxyClient - Wish to open route to X - Send message to UDPProxyCoordinator containing X, desired timeout - Wait for success or failure - -UDPProxyCoordinator: -* Get openRouteRequest - If no servers registered, return failure - Add entry to memory - chooseBestUDPProxyServer() (overridable, chooses at random by default) - Query this server to StartForwarding(). Return success or failure - If failure, choose another server from the remaining list. If none remaining, return failure. Else return success. -* Disconnect: - If disconnected system is pending client on openRouteRequest, delete that request - If disconnected system is UDPProxyServer, remove from list. For each pending client for this server, choose from remaining servers. -* Login: - Add to UDPProxyServer list, validating password if set -*/ - -// Stored in the second byte after ID_UDP_PROXY_GENERAL -// Otherwise MessageIdentifiers.h is too cluttered and will hit the limit on enumerations in a single byte -enum UDPProxyMessages -{ - ID_UDP_PROXY_FORWARDING_SUCCEEDED, - ID_UDP_PROXY_FORWARDING_NOTIFICATION, - ID_UDP_PROXY_NO_SERVERS_ONLINE, - ID_UDP_PROXY_RECIPIENT_GUID_NOT_CONNECTED_TO_COORDINATOR, - ID_UDP_PROXY_ALL_SERVERS_BUSY, - ID_UDP_PROXY_IN_PROGRESS, - ID_UDP_PROXY_FORWARDING_REQUEST_FROM_CLIENT_TO_COORDINATOR, - ID_UDP_PROXY_PING_SERVERS_FROM_COORDINATOR_TO_CLIENT, - ID_UDP_PROXY_PING_SERVERS_REPLY_FROM_CLIENT_TO_COORDINATOR, - ID_UDP_PROXY_FORWARDING_REQUEST_FROM_COORDINATOR_TO_SERVER, - ID_UDP_PROXY_FORWARDING_REPLY_FROM_SERVER_TO_COORDINATOR, - ID_UDP_PROXY_LOGIN_REQUEST_FROM_SERVER_TO_COORDINATOR, - ID_UDP_PROXY_LOGIN_SUCCESS_FROM_COORDINATOR_TO_SERVER, - ID_UDP_PROXY_ALREADY_LOGGED_IN_FROM_COORDINATOR_TO_SERVER, - ID_UDP_PROXY_NO_PASSWORD_SET_FROM_COORDINATOR_TO_SERVER, - ID_UDP_PROXY_WRONG_PASSWORD_FROM_COORDINATOR_TO_SERVER -}; - - -#define UDP_FORWARDER_MAXIMUM_TIMEOUT (60000 * 10) - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +// System flow: +/* +UDPProxyClient: End user +UDPProxyServer: open server, to route messages from end users that can't connect to each other using UDPForwarder class. +UDPProxyCoordinator: Server somewhere, connected to by RakNet, to maintain a list of UDPProxyServer + +UDPProxyServer + On startup, log into UDPProxyCoordinator and register self + +UDPProxyClient + Wish to open route to X + Send message to UDPProxyCoordinator containing X, desired timeout + Wait for success or failure + +UDPProxyCoordinator: +* Get openRouteRequest + If no servers registered, return failure + Add entry to memory + chooseBestUDPProxyServer() (overridable, chooses at random by default) + Query this server to StartForwarding(). Return success or failure + If failure, choose another server from the remaining list. If none remaining, return failure. Else return success. +* Disconnect: + If disconnected system is pending client on openRouteRequest, delete that request + If disconnected system is UDPProxyServer, remove from list. For each pending client for this server, choose from remaining servers. +* Login: + Add to UDPProxyServer list, validating password if set +*/ + +// Stored in the second byte after ID_UDP_PROXY_GENERAL +// Otherwise MessageIdentifiers.h is too cluttered and will hit the limit on enumerations in a single byte +enum UDPProxyMessages +{ + ID_UDP_PROXY_FORWARDING_SUCCEEDED, + ID_UDP_PROXY_FORWARDING_NOTIFICATION, + ID_UDP_PROXY_NO_SERVERS_ONLINE, + ID_UDP_PROXY_RECIPIENT_GUID_NOT_CONNECTED_TO_COORDINATOR, + ID_UDP_PROXY_ALL_SERVERS_BUSY, + ID_UDP_PROXY_IN_PROGRESS, + ID_UDP_PROXY_FORWARDING_REQUEST_FROM_CLIENT_TO_COORDINATOR, + ID_UDP_PROXY_PING_SERVERS_FROM_COORDINATOR_TO_CLIENT, + ID_UDP_PROXY_PING_SERVERS_REPLY_FROM_CLIENT_TO_COORDINATOR, + ID_UDP_PROXY_FORWARDING_REQUEST_FROM_COORDINATOR_TO_SERVER, + ID_UDP_PROXY_FORWARDING_REPLY_FROM_SERVER_TO_COORDINATOR, + ID_UDP_PROXY_LOGIN_REQUEST_FROM_SERVER_TO_COORDINATOR, + ID_UDP_PROXY_LOGIN_SUCCESS_FROM_COORDINATOR_TO_SERVER, + ID_UDP_PROXY_ALREADY_LOGGED_IN_FROM_COORDINATOR_TO_SERVER, + ID_UDP_PROXY_NO_PASSWORD_SET_FROM_COORDINATOR_TO_SERVER, + ID_UDP_PROXY_WRONG_PASSWORD_FROM_COORDINATOR_TO_SERVER +}; + + +#define UDP_FORWARDER_MAXIMUM_TIMEOUT (60000 * 10) + diff --git a/Source/UDPProxyCoordinator.h b/Source/UDPProxyCoordinator.h index 650164377..d5c2cf412 100644 --- a/Source/UDPProxyCoordinator.h +++ b/Source/UDPProxyCoordinator.h @@ -1,117 +1,115 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief Essentially maintains a list of servers running UDPProxyServer, and some state management for UDPProxyClient to find a free server to forward datagrams -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_UDPProxyCoordinator==1 && _RAKNET_SUPPORT_UDPForwarder==1 - -#ifndef __UDP_PROXY_COORDINATOR_H -#define __UDP_PROXY_COORDINATOR_H - -#include "Export.h" -#include "RakNetTypes.h" -#include "PluginInterface2.h" -#include "RakString.h" -#include "BitStream.h" -#include "DS_Queue.h" -#include "DS_OrderedList.h" - -namespace RakNet -{ - /// When NAT Punchthrough fails, it is possible to use a non-NAT system to forward messages from us to the recipient, and vice-versa - /// The class to forward messages is UDPForwarder, and it is triggered over the network via the UDPProxyServer plugin. - /// The UDPProxyClient connects to UDPProxyCoordinator to get a list of servers running UDPProxyServer, and the coordinator will relay our forwarding request - /// \brief Middleman between UDPProxyServer and UDPProxyClient, maintaining a list of UDPProxyServer, and managing state for clients to find an available forwarding server. - /// \ingroup NAT_PUNCHTHROUGH_GROUP - class RAK_DLL_EXPORT UDPProxyCoordinator : public PluginInterface2 - { - public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(UDPProxyCoordinator) - - UDPProxyCoordinator(); - virtual ~UDPProxyCoordinator(); - - /// For UDPProxyServers logging in remotely, they must pass a password to UDPProxyServer::LoginToCoordinator(). It must match the password set here. - /// If no password is set, they cannot login remotely. - /// By default, no password is set - void SetRemoteLoginPassword(RakNet::RakString password); - - /// \internal - virtual void Update(void); - virtual PluginReceiveResult OnReceive(Packet *packet); - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - - struct SenderAndTargetAddress - { - SystemAddress senderClientAddress; - RakNetGUID senderClientGuid; - SystemAddress targetClientAddress; - RakNetGUID targetClientGuid; - }; - - struct ServerWithPing - { - unsigned short ping; - SystemAddress serverAddress; - }; - - struct ForwardingRequest - { - RakNet::TimeMS timeoutOnNoDataMS; - RakNet::TimeMS timeoutAfterSuccess; - SenderAndTargetAddress sata; - SystemAddress requestingAddress; // Which system originally sent the network message to start forwarding - SystemAddress currentlyAttemptedServerAddress; - DataStructures::Queue remainingServersToTry; - RakNet::BitStream serverSelectionBitstream; - - DataStructures::List sourceServerPings, targetServerPings; - RakNet::TimeMS timeRequestedPings; - // Order based on sourceServerPings and targetServerPings - void OrderRemainingServersToTry(void); - - }; - protected: - - static int ServerWithPingComp( const unsigned short &key, const UDPProxyCoordinator::ServerWithPing &data ); - static int ForwardingRequestComp( const SenderAndTargetAddress &key, ForwardingRequest* const &data); - - void OnForwardingRequestFromClientToCoordinator(Packet *packet); - void OnLoginRequestFromServerToCoordinator(Packet *packet); - void OnForwardingReplyFromServerToCoordinator(Packet *packet); - void OnPingServersReplyFromClientToCoordinator(Packet *packet); - void TryNextServer(SenderAndTargetAddress sata, ForwardingRequest *fw); - void SendAllBusy(SystemAddress senderClientAddress, SystemAddress targetClientAddress, RakNetGUID targetClientGuid, SystemAddress requestingAddress); - void Clear(void); - - void SendForwardingRequest(SystemAddress sourceAddress, SystemAddress targetAddress, SystemAddress serverAddress, RakNet::TimeMS timeoutOnNoDataMS); - - // Logged in servers - //DataStructures::Multilist serverList; - DataStructures::List serverList; - - // Forwarding requests in progress - //DataStructures::Multilist forwardingRequestList; - DataStructures::OrderedList forwardingRequestList; - - RakNet::RakString remoteLoginPassword; - - }; - -} // End namespace - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief Essentially maintains a list of servers running UDPProxyServer, and some state management for UDPProxyClient to find a free server to forward datagrams +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_UDPProxyCoordinator==1 && _RAKNET_SUPPORT_UDPForwarder==1 + +#pragma once + +#include "Export.h" +#include "RakNetTypes.h" +#include "PluginInterface2.h" +#include "RakString.h" +#include "BitStream.h" +#include "DS_Queue.h" +#include "DS_OrderedList.h" + +namespace RakNet +{ + /// When NAT Punchthrough fails, it is possible to use a non-NAT system to forward messages from us to the recipient, and vice-versa + /// The class to forward messages is UDPForwarder, and it is triggered over the network via the UDPProxyServer plugin. + /// The UDPProxyClient connects to UDPProxyCoordinator to get a list of servers running UDPProxyServer, and the coordinator will relay our forwarding request + /// \brief Middleman between UDPProxyServer and UDPProxyClient, maintaining a list of UDPProxyServer, and managing state for clients to find an available forwarding server. + /// \ingroup NAT_PUNCHTHROUGH_GROUP + class RAK_DLL_EXPORT UDPProxyCoordinator : public PluginInterface2 + { + public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(UDPProxyCoordinator) + + UDPProxyCoordinator(); + virtual ~UDPProxyCoordinator(); + + /// For UDPProxyServers logging in remotely, they must pass a password to UDPProxyServer::LoginToCoordinator(). It must match the password set here. + /// If no password is set, they cannot login remotely. + /// By default, no password is set + void SetRemoteLoginPassword(RakNet::RakString password); + + /// \internal + virtual void Update(void); + virtual PluginReceiveResult OnReceive(Packet *packet); + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + + struct SenderAndTargetAddress + { + SystemAddress senderClientAddress; + RakNetGUID senderClientGuid; + SystemAddress targetClientAddress; + RakNetGUID targetClientGuid; + }; + + struct ServerWithPing + { + unsigned short ping; + SystemAddress serverAddress; + }; + + struct ForwardingRequest + { + RakNet::TimeMS timeoutOnNoDataMS; + RakNet::TimeMS timeoutAfterSuccess; + SenderAndTargetAddress sata; + SystemAddress requestingAddress; // Which system originally sent the network message to start forwarding + SystemAddress currentlyAttemptedServerAddress; + DataStructures::Queue remainingServersToTry; + RakNet::BitStream serverSelectionBitstream; + + DataStructures::List sourceServerPings, targetServerPings; + RakNet::TimeMS timeRequestedPings; + // Order based on sourceServerPings and targetServerPings + void OrderRemainingServersToTry(void); + + }; + protected: + + static int ServerWithPingComp( const unsigned short &key, const UDPProxyCoordinator::ServerWithPing &data ); + static int ForwardingRequestComp( const SenderAndTargetAddress &key, ForwardingRequest* const &data); + + void OnForwardingRequestFromClientToCoordinator(Packet *packet); + void OnLoginRequestFromServerToCoordinator(Packet *packet); + void OnForwardingReplyFromServerToCoordinator(Packet *packet); + void OnPingServersReplyFromClientToCoordinator(Packet *packet); + void TryNextServer(SenderAndTargetAddress sata, ForwardingRequest *fw); + void SendAllBusy(SystemAddress senderClientAddress, SystemAddress targetClientAddress, RakNetGUID targetClientGuid, SystemAddress requestingAddress); + void Clear(void); + + void SendForwardingRequest(SystemAddress sourceAddress, SystemAddress targetAddress, SystemAddress serverAddress, RakNet::TimeMS timeoutOnNoDataMS); + + // Logged in servers + //DataStructures::Multilist serverList; + DataStructures::List serverList; + + // Forwarding requests in progress + //DataStructures::Multilist forwardingRequestList; + DataStructures::OrderedList forwardingRequestList; + + RakNet::RakString remoteLoginPassword; + + }; + +} // End namespace + +#endif + diff --git a/Source/UDPProxyServer.h b/Source/UDPProxyServer.h index bd806ff02..6642f8683 100644 --- a/Source/UDPProxyServer.h +++ b/Source/UDPProxyServer.h @@ -1,126 +1,124 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -/// \file -/// \brief A RakNet plugin performing networking to communicate with UDPProxyServer. It allows UDPProxyServer to control our instance of UDPForwarder. -/// - - -#include "NativeFeatureIncludes.h" -#if _RAKNET_SUPPORT_UDPProxyServer==1 && _RAKNET_SUPPORT_UDPForwarder==1 - -#ifndef __UDP_PROXY_SERVER_H -#define __UDP_PROXY_SERVER_H - -#include "Export.h" -#include "RakNetTypes.h" -#include "PluginInterface2.h" -#include "UDPForwarder.h" -#include "RakString.h" - -namespace RakNet -{ -class UDPProxyServer; - -/// Callback to handle results of calling UDPProxyServer::LoginToCoordinator() -/// \ingroup UDP_PROXY_GROUP -struct UDPProxyServerResultHandler -{ - UDPProxyServerResultHandler() {} - virtual ~UDPProxyServerResultHandler() {} - - /// Called when our login succeeds - /// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator() - /// \param[out] proxyServer The plugin calling this callback - virtual void OnLoginSuccess(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0; - - /// We are already logged in. - /// This login failed, but the system is operational as if it succeeded - /// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator() - /// \param[out] proxyServer The plugin calling this callback - virtual void OnAlreadyLoggedIn(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0; - - /// The coordinator operator forgot to call UDPProxyCoordinator::SetRemoteLoginPassword() - /// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator() - /// \param[out] proxyServer The plugin calling this callback - virtual void OnNoPasswordSet(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0; - - /// The coordinator operator set a different password in UDPProxyCoordinator::SetRemoteLoginPassword() than what we passed - /// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator() - /// \param[out] proxyServer The plugin calling this callback - virtual void OnWrongPassword(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0; -}; - -/// \brief UDPProxyServer to control our instance of UDPForwarder -/// \details When NAT Punchthrough fails, it is possible to use a non-NAT system to forward messages from us to the recipient, and vice-versa.
      -/// The class to forward messages is UDPForwarder, and it is triggered over the network via the UDPProxyServer plugin.
      -/// The UDPProxyServer connects to UDPProxyServer to get a list of servers running UDPProxyServer, and the coordinator will relay our forwarding request. -/// \ingroup UDP_PROXY_GROUP -class RAK_DLL_EXPORT UDPProxyServer : public PluginInterface2 -{ -public: - // GetInstance() and DestroyInstance(instance*) - STATIC_FACTORY_DECLARATIONS(UDPProxyServer) - - UDPProxyServer(); - ~UDPProxyServer(); - - /// Sets the socket family to use, either IPV4 or IPV6 - /// \param[in] socketFamily For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC. - void SetSocketFamily(unsigned short _socketFamily); - - /// Receives the results of calling LoginToCoordinator() - /// Set before calling LoginToCoordinator or you won't know what happened - /// \param[in] resultHandler - void SetResultHandler(UDPProxyServerResultHandler *rh); - - /// Before the coordinator will register the UDPProxyServer, you must login - /// \pre Must be connected to the coordinator - /// \pre Coordinator must have set a password with UDPProxyCoordinator::SetRemoteLoginPassword() - /// \returns false if already logged in, or logging in. Returns true otherwise - bool LoginToCoordinator(RakNet::RakString password, SystemAddress coordinatorAddress); - - /// \brief The server IP reported to the client is the IP address from the server to the coordinator. - /// If the server and coordinator are on the same LAN, you need to call SetServerPublicIP() to tell the client what address to connect to - /// \param[in] ip IP address to report in UDPProxyClientResultHandler::OnForwardingSuccess() and UDPProxyClientResultHandler::OnForwardingNotification() as proxyIPAddress - void SetServerPublicIP(RakString ip); - - /// Operative class that performs the forwarding - /// Exposed so you can call UDPForwarder::SetMaxForwardEntries() if you want to change away from the default - /// UDPForwarder::Startup(), UDPForwarder::Shutdown(), and UDPForwarder::Update() are called automatically by the plugin - UDPForwarder udpForwarder; - - virtual void OnAttach(void); - virtual void OnDetach(void); - - /// \internal - virtual void Update(void); - virtual PluginReceiveResult OnReceive(Packet *packet); - virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); - virtual void OnRakPeerStartup(void); - virtual void OnRakPeerShutdown(void); - -protected: - void OnForwardingRequestFromCoordinatorToServer(Packet *packet); - - DataStructures::OrderedList loggingInCoordinators; - DataStructures::OrderedList loggedInCoordinators; - - UDPProxyServerResultHandler *resultHandler; - unsigned short socketFamily; - RakString serverPublicIp; - -}; - -} // End namespace - -#endif - -#endif // _RAKNET_SUPPORT_* +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +/// \file +/// \brief A RakNet plugin performing networking to communicate with UDPProxyServer. It allows UDPProxyServer to control our instance of UDPForwarder. +/// + + +#include "NativeFeatureIncludes.h" +#if _RAKNET_SUPPORT_UDPProxyServer==1 && _RAKNET_SUPPORT_UDPForwarder==1 + +#pragma once + +#include "Export.h" +#include "RakNetTypes.h" +#include "PluginInterface2.h" +#include "UDPForwarder.h" +#include "RakString.h" + +namespace RakNet +{ +class UDPProxyServer; + +/// Callback to handle results of calling UDPProxyServer::LoginToCoordinator() +/// \ingroup UDP_PROXY_GROUP +struct UDPProxyServerResultHandler +{ + UDPProxyServerResultHandler() {} + virtual ~UDPProxyServerResultHandler() {} + + /// Called when our login succeeds + /// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator() + /// \param[out] proxyServer The plugin calling this callback + virtual void OnLoginSuccess(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0; + + /// We are already logged in. + /// This login failed, but the system is operational as if it succeeded + /// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator() + /// \param[out] proxyServer The plugin calling this callback + virtual void OnAlreadyLoggedIn(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0; + + /// The coordinator operator forgot to call UDPProxyCoordinator::SetRemoteLoginPassword() + /// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator() + /// \param[out] proxyServer The plugin calling this callback + virtual void OnNoPasswordSet(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0; + + /// The coordinator operator set a different password in UDPProxyCoordinator::SetRemoteLoginPassword() than what we passed + /// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator() + /// \param[out] proxyServer The plugin calling this callback + virtual void OnWrongPassword(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0; +}; + +/// \brief UDPProxyServer to control our instance of UDPForwarder +/// \details When NAT Punchthrough fails, it is possible to use a non-NAT system to forward messages from us to the recipient, and vice-versa.
      +/// The class to forward messages is UDPForwarder, and it is triggered over the network via the UDPProxyServer plugin.
      +/// The UDPProxyServer connects to UDPProxyServer to get a list of servers running UDPProxyServer, and the coordinator will relay our forwarding request. +/// \ingroup UDP_PROXY_GROUP +class RAK_DLL_EXPORT UDPProxyServer : public PluginInterface2 +{ +public: + // GetInstance() and DestroyInstance(instance*) + STATIC_FACTORY_DECLARATIONS(UDPProxyServer) + + UDPProxyServer(); + ~UDPProxyServer(); + + /// Sets the socket family to use, either IPV4 or IPV6 + /// \param[in] socketFamily For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC. + void SetSocketFamily(unsigned short _socketFamily); + + /// Receives the results of calling LoginToCoordinator() + /// Set before calling LoginToCoordinator or you won't know what happened + /// \param[in] resultHandler + void SetResultHandler(UDPProxyServerResultHandler *rh); + + /// Before the coordinator will register the UDPProxyServer, you must login + /// \pre Must be connected to the coordinator + /// \pre Coordinator must have set a password with UDPProxyCoordinator::SetRemoteLoginPassword() + /// \returns false if already logged in, or logging in. Returns true otherwise + bool LoginToCoordinator(RakNet::RakString password, SystemAddress coordinatorAddress); + + /// \brief The server IP reported to the client is the IP address from the server to the coordinator. + /// If the server and coordinator are on the same LAN, you need to call SetServerPublicIP() to tell the client what address to connect to + /// \param[in] ip IP address to report in UDPProxyClientResultHandler::OnForwardingSuccess() and UDPProxyClientResultHandler::OnForwardingNotification() as proxyIPAddress + void SetServerPublicIP(RakString ip); + + /// Operative class that performs the forwarding + /// Exposed so you can call UDPForwarder::SetMaxForwardEntries() if you want to change away from the default + /// UDPForwarder::Startup(), UDPForwarder::Shutdown(), and UDPForwarder::Update() are called automatically by the plugin + UDPForwarder udpForwarder; + + virtual void OnAttach(void); + virtual void OnDetach(void); + + /// \internal + virtual void Update(void); + virtual PluginReceiveResult OnReceive(Packet *packet); + virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ); + virtual void OnRakPeerStartup(void); + virtual void OnRakPeerShutdown(void); + +protected: + void OnForwardingRequestFromCoordinatorToServer(Packet *packet); + + DataStructures::OrderedList loggingInCoordinators; + DataStructures::OrderedList loggedInCoordinators; + + UDPProxyServerResultHandler *resultHandler; + unsigned short socketFamily; + RakString serverPublicIp; + +}; + +} // End namespace + +#endif + diff --git a/Source/VariableDeltaSerializer.h b/Source/VariableDeltaSerializer.h index e8dc48c2b..e8e1a98ad 100644 --- a/Source/VariableDeltaSerializer.h +++ b/Source/VariableDeltaSerializer.h @@ -1,267 +1,265 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __VARIABLE_DELTA_SERIALIZER_H -#define __VARIABLE_DELTA_SERIALIZER_H - -#include "VariableListDeltaTracker.h" -#include "DS_MemoryPool.h" -#include "NativeTypes.h" -#include "BitStream.h" -#include "PacketPriority.h" -#include "DS_OrderedList.h" - -namespace RakNet -{ - -/// \brief Class to compare memory values of variables in a current state to a prior state -/// Results of the comparisons will be written to a bitStream, such that only changed variables get written
      -/// Can be used with ReplicaManager3 to Serialize a Replica3 per-variable, rather than comparing the entire object against itself
      -/// Usage:
      -///
      -/// 1. Call BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize(). In the case of Replica3, this would be in the Serialize() call
      -/// 2. For each variable of the type in step 1, call Serialize(). The same variables must be serialized every tick()
      -/// 3. Call EndSerialize()
      -/// 4. Repeat step 1 for each of the other categories of how to send varaibles
      -///
      -/// On the receiver:
      -///
      -/// 1. Call BeginDeserialize(). In the case of Replica3, this would be in the Deserialize() call
      -/// 2. Call DeserializeVariable() for each variable, in the same order as was Serialized()
      -/// 3. Call EndSerialize()
      -/// \sa The ReplicaManager3 sample -class RAK_DLL_EXPORT VariableDeltaSerializer -{ -protected: - struct RemoteSystemVariableHistory; - struct ChangedVariablesList; - -public: - VariableDeltaSerializer(); - ~VariableDeltaSerializer(); - - struct SerializationContext - { - SerializationContext(); - ~SerializationContext(); - - RakNetGUID guid; - BitStream *bitStream; - uint32_t rakPeerSendReceipt; - RemoteSystemVariableHistory *variableHistory; - RemoteSystemVariableHistory *variableHistoryIdentical; - RemoteSystemVariableHistory *variableHistoryUnique; - ChangedVariablesList *changedVariables; - uint32_t sendReceipt; - PacketReliability serializationMode; - bool anyVariablesWritten; - bool newSystemSend; // Force send all, do not record - }; - - struct DeserializationContext - { - BitStream *bitStream; - }; - - /// \brief Call before doing one or more SerializeVariable calls when the data will be sent UNRELIABLE_WITH_ACK_RECEIPT - /// The last value of each variable will be saved per remote system. Additionally, a history of \a _sendReceipts is stored to determine what to resend on packetloss. - /// When variables are lost, they will be flagged dirty and always resent to the system that lost it - /// Disadvantages: Every variable for every remote system is copied internally, in addition to a history list of what variables changed for which \a _sendReceipt. Very memory and CPU intensive for multiple connections. - /// Advantages: When data needs to be resent by RakNet, RakNet can only resend the value it currently has. This allows the application to control the resend, sending the most recent value of the variable. The end result is that bandwidth is used more efficiently because old data is never sent. - /// \pre Upon getting ID_SND_RECEIPT_LOSS or ID_SND_RECEIPT_ACKED call OnMessageReceipt() - /// \pre AddRemoteSystemVariableHistory() and RemoveRemoteSystemVariableHistory() must be called for new and lost connections - /// \param[in] context Holds the context of this group of serialize calls. This can be a stack object just passed to the function. - /// \param[in] _guid Which system we are sending to - /// \param[in] _bitSteam Which bitStream to write to - /// \param[in] _sendReceipt Returned from RakPeer::IncrementNextSendReceipt() and passed to the Send() or SendLists() function. Identifies this update for ID_SND_RECEIPT_LOSS and ID_SND_RECEIPT_ACKED - void BeginUnreliableAckedSerialize(SerializationContext *context, RakNetGUID _guid, BitStream *_bitStream, uint32_t _sendReceipt); - - /// \brief Call before doing one or more SerializeVariable calls for data that may be sent differently to every remote system (such as an invisibility flag that only teammates can see) - /// The last value of each variable will be saved per remote system. - /// Unlike BeginUnreliableAckedSerialize(), send receipts are not necessary - /// Disadvantages: Every variable for every remote system is copied internally. Very memory and CPU intensive for multiple connections. - /// Advantages: When data is sent differently depending on the recipient, this system can make things easier to use and is as efficient as it can be. - /// \pre AddRemoteSystemVariableHistory() and RemoveRemoteSystemVariableHistory() must be called for new and lost connections - /// \param[in] context Holds the context of this group of serialize calls. This can be a stack object just passed to the function. - /// \param[in] _guid Which system we are sending to - /// \param[in] _bitSteam Which bitStream to write to - void BeginUniqueSerialize(SerializationContext *context, RakNetGUID _guid, BitStream *_bitStream); - - /// \brief Call before doing one or more SerializeVariable calls for data that is sent with the same value to every remote system (such as health, position, etc.) - /// This is the most common type of serialization, and also the most efficient - /// Disadvantages: A copy of every variable still needs to be held, although only once - /// Advantages: After the first serialization, the last serialized bitStream will be used for subsequent sends - /// \pre Call OnPreSerializeTick() before doing any calls to BeginIdenticalSerialize() for each of your objects, once per game tick - /// \param[in] context Holds the context of this group of serialize calls. This can be a stack object just passed to the function. - /// \param[in] _isFirstSerializeToThisSystem Pass true if this is the first time ever serializing to this system (the initial download). This way all variables will be written, rather than checking against prior sent values. - /// \param[in] _bitSteam Which bitStream to write to - void BeginIdenticalSerialize(SerializationContext *context, bool _isFirstSerializeToThisSystem, BitStream *_bitStream); - - /// \brief Call after BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize(), then after calling SerializeVariable() one or more times - /// \param[in] context Same context pointer passed to BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize() - void EndSerialize(SerializationContext *context); - - /// \brief Call when you receive the BitStream written by SerializeVariable(), before calling DeserializeVariable() - /// \param[in] context Holds the context of this group of deserialize calls. This can be a stack object just passed to the function. - /// \param[in] _bitStream Pass the bitStream originally passed to and written to by serialize calls - void BeginDeserialize(DeserializationContext *context, BitStream *_bitStream); - - /// \param[in] context Same context pointer passed to BeginDeserialize() - void EndDeserialize(DeserializationContext *context); - - /// BeginUnreliableAckedSerialize() and BeginUniqueSerialize() require knowledge of when connections are added and dropped - /// Call AddRemoteSystemVariableHistory() and RemoveRemoteSystemVariableHistory() to notify the system of these events - /// \param[in] _guid Which system we are sending to - void AddRemoteSystemVariableHistory(RakNetGUID guid); - - /// BeginUnreliableAckedSerialize() and BeginUniqueSerialize() require knowledge of when connections are added and dropped - /// Call AddRemoteSystemVariableHistory() and RemoveRemoteSystemVariableHistory() to notify the system of these events - /// \param[in] _guid Which system we are sending to - void RemoveRemoteSystemVariableHistory(RakNetGUID guid); - - /// BeginIdenticalSerialize() requires knowledge of when serialization has started for an object across multiple systems - /// This way it can setup the flag to do new comparisons against the last sent values, rather than just resending the last sent bitStream - /// For Replica3, overload and call this from Replica3::OnUserReplicaPreSerializeTick() - void OnPreSerializeTick(void); - - /// Call when getting ID_SND_RECEIPT_LOSS or ID_SND_RECEIPT_ACKED for a particular system - /// Example: - /// - /// uint32_t msgNumber; - /// memcpy(&msgNumber, packet->data+1, 4); - /// DataStructures::List replicaListOut; - /// replicaManager.GetReplicasCreatedByMe(replicaListOut); - /// unsigned int idx; - /// for (idx=0; idx < replicaListOut.GetSize(); idx++) - /// { - /// ((SampleReplica*)replicaListOut[idx])->NotifyReplicaOfMessageDeliveryStatus(packet->guid,msgNumber, packet->data[0]==ID_SND_RECEIPT_ACKED); - /// } - /// - /// \param[in] guid Which system we are sending to - /// \param[in] receiptId Encoded in bytes 1-4 inclusive of ID_SND_RECEIPT_LOSS and ID_SND_RECEIPT_ACKED - /// \param[in] messageArrived True for ID_SND_RECEIPT_ACKED, false otherwise - void OnMessageReceipt(RakNetGUID guid, uint32_t receiptId, bool messageArrived); - - /// Call to Serialize a variable - /// Will write to the bitSteam passed to \a context true, variableValue if the variable has changed or has never been written. Otherwise will write false. - /// \pre You have called BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize() - /// \pre Will also require calling OnPreSerializeTick() if using BeginIdenticalSerialize() - /// \note Be sure to call EndSerialize() after finishing all serializations - /// \param[in] context Same context pointer passed to BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize() - /// \param[in] variable A variable to write to the bitStream passed to \a context - template - void SerializeVariable(SerializationContext *context, const VarType &variable) - { - if (context->newSystemSend) - { - if (context->variableHistory->variableListDeltaTracker.IsPastEndOfList()==false) - { - // previously sent data to another system - context->bitStream->Write(true); - context->bitStream->Write(variable); - context->anyVariablesWritten=true; - } - else - { - // never sent data to another system - context->variableHistory->variableListDeltaTracker.WriteVarToBitstream(variable, context->bitStream); - context->anyVariablesWritten=true; - } - } - else if (context->serializationMode==UNRELIABLE_WITH_ACK_RECEIPT) - { - context->anyVariablesWritten|= - context->variableHistory->variableListDeltaTracker.WriteVarToBitstream(variable, context->bitStream, context->changedVariables->bitField, context->changedVariables->bitWriteIndex++); - } - else - { - if (context->variableHistoryIdentical) - { - // Identical serialization to a number of systems - if (didComparisonThisTick==false) - context->anyVariablesWritten|= - context->variableHistory->variableListDeltaTracker.WriteVarToBitstream(variable, context->bitStream); - // Else bitstream is written to at the end - } - else - { - // Per-system serialization - context->anyVariablesWritten|= - context->variableHistory->variableListDeltaTracker.WriteVarToBitstream(variable, context->bitStream); - } - } - } - - /// Call to deserialize into a variable - /// \pre You have called BeginDeserialize() - /// \note Be sure to call EndDeserialize() after finishing all deserializations - /// \param[in] context Same context pointer passed to BeginDeserialize() - /// \param[in] variable A variable to write to the bitStream passed to \a context - template - bool DeserializeVariable(DeserializationContext *context, VarType &variable) - { - return VariableListDeltaTracker::ReadVarFromBitstream(variable, context->bitStream); - } - - - -protected: - - // For a given send receipt from RakPeer::Send() track which variables we updated - // That way if that send does not arrive (ID_SND_RECEIPT_LOSS) we can mark those variables as dirty to resend them with current values - struct ChangedVariablesList - { - uint32_t sendReceipt; - unsigned short bitWriteIndex; - unsigned char bitField[56]; - }; - - // static int Replica2ObjectComp( const uint32_t &key, ChangedVariablesList* const &data ); - - static int UpdatedVariablesListPtrComp( const uint32_t &key, ChangedVariablesList* const &data ); - - // For each remote system, track the last values of variables we sent to them, and the history of what values changed per call to Send() - // Every serialize if a variable changes from its last value, send it out again - // Also if a send does not arrive (ID_SND_RECEIPT_LOSS) we use updatedVariablesHistory to mark those variables as dirty, to resend them unreliably with the current values - struct RemoteSystemVariableHistory - { - RakNetGUID guid; - VariableListDeltaTracker variableListDeltaTracker; - DataStructures::OrderedList updatedVariablesHistory; - }; - /// A list of RemoteSystemVariableHistory indexed by guid, one per connection that we serialize to - /// List is added to when SerializeConstruction is called, and removed from when SerializeDestruction is called, or when a given connection is dropped - DataStructures::List remoteSystemVariableHistoryList; - - // Because the ChangedVariablesList is created every serialize and destroyed every receipt I use a pool to avoid fragmentation - DataStructures::MemoryPool updatedVariablesMemoryPool; - - bool didComparisonThisTick; - RakNet::BitStream identicalSerializationBs; - - void FreeVarsAssociatedWithReceipt(RakNetGUID guid, uint32_t receiptId); - void DirtyAndFreeVarsAssociatedWithReceipt(RakNetGUID guid, uint32_t receiptId); - unsigned int GetVarsWrittenPerRemoteSystemListIndex(RakNetGUID guid); - void RemoveRemoteSystemVariableHistory(void); - - RemoteSystemVariableHistory* GetRemoteSystemVariableHistory(RakNetGUID guid); - - ChangedVariablesList *AllocChangedVariablesList(void); - void FreeChangedVariablesList(ChangedVariablesList *changedVariables); - void StoreChangedVariablesList(RemoteSystemVariableHistory *variableHistory, ChangedVariablesList *changedVariables, uint32_t sendReceipt); - - RemoteSystemVariableHistory *StartVariableHistoryWrite(RakNetGUID guid); - unsigned int GetRemoteSystemHistoryListIndex(RakNetGUID guid); - -}; - -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include "VariableListDeltaTracker.h" +#include "DS_MemoryPool.h" +#include "NativeTypes.h" +#include "BitStream.h" +#include "PacketPriority.h" +#include "DS_OrderedList.h" + +namespace RakNet +{ + +/// \brief Class to compare memory values of variables in a current state to a prior state +/// Results of the comparisons will be written to a bitStream, such that only changed variables get written
      +/// Can be used with ReplicaManager3 to Serialize a Replica3 per-variable, rather than comparing the entire object against itself
      +/// Usage:
      +///
      +/// 1. Call BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize(). In the case of Replica3, this would be in the Serialize() call
      +/// 2. For each variable of the type in step 1, call Serialize(). The same variables must be serialized every tick()
      +/// 3. Call EndSerialize()
      +/// 4. Repeat step 1 for each of the other categories of how to send varaibles
      +///
      +/// On the receiver:
      +///
      +/// 1. Call BeginDeserialize(). In the case of Replica3, this would be in the Deserialize() call
      +/// 2. Call DeserializeVariable() for each variable, in the same order as was Serialized()
      +/// 3. Call EndSerialize()
      +/// \sa The ReplicaManager3 sample +class RAK_DLL_EXPORT VariableDeltaSerializer +{ +protected: + struct RemoteSystemVariableHistory; + struct ChangedVariablesList; + +public: + VariableDeltaSerializer(); + ~VariableDeltaSerializer(); + + struct SerializationContext + { + SerializationContext(); + ~SerializationContext(); + + RakNetGUID guid; + BitStream *bitStream; + uint32_t rakPeerSendReceipt; + RemoteSystemVariableHistory *variableHistory; + RemoteSystemVariableHistory *variableHistoryIdentical; + RemoteSystemVariableHistory *variableHistoryUnique; + ChangedVariablesList *changedVariables; + uint32_t sendReceipt; + PacketReliability serializationMode; + bool anyVariablesWritten; + bool newSystemSend; // Force send all, do not record + }; + + struct DeserializationContext + { + BitStream *bitStream; + }; + + /// \brief Call before doing one or more SerializeVariable calls when the data will be sent UNRELIABLE_WITH_ACK_RECEIPT + /// The last value of each variable will be saved per remote system. Additionally, a history of \a _sendReceipts is stored to determine what to resend on packetloss. + /// When variables are lost, they will be flagged dirty and always resent to the system that lost it + /// Disadvantages: Every variable for every remote system is copied internally, in addition to a history list of what variables changed for which \a _sendReceipt. Very memory and CPU intensive for multiple connections. + /// Advantages: When data needs to be resent by RakNet, RakNet can only resend the value it currently has. This allows the application to control the resend, sending the most recent value of the variable. The end result is that bandwidth is used more efficiently because old data is never sent. + /// \pre Upon getting ID_SND_RECEIPT_LOSS or ID_SND_RECEIPT_ACKED call OnMessageReceipt() + /// \pre AddRemoteSystemVariableHistory() and RemoveRemoteSystemVariableHistory() must be called for new and lost connections + /// \param[in] context Holds the context of this group of serialize calls. This can be a stack object just passed to the function. + /// \param[in] _guid Which system we are sending to + /// \param[in] _bitSteam Which bitStream to write to + /// \param[in] _sendReceipt Returned from RakPeer::IncrementNextSendReceipt() and passed to the Send() or SendLists() function. Identifies this update for ID_SND_RECEIPT_LOSS and ID_SND_RECEIPT_ACKED + void BeginUnreliableAckedSerialize(SerializationContext *context, RakNetGUID _guid, BitStream *_bitStream, uint32_t _sendReceipt); + + /// \brief Call before doing one or more SerializeVariable calls for data that may be sent differently to every remote system (such as an invisibility flag that only teammates can see) + /// The last value of each variable will be saved per remote system. + /// Unlike BeginUnreliableAckedSerialize(), send receipts are not necessary + /// Disadvantages: Every variable for every remote system is copied internally. Very memory and CPU intensive for multiple connections. + /// Advantages: When data is sent differently depending on the recipient, this system can make things easier to use and is as efficient as it can be. + /// \pre AddRemoteSystemVariableHistory() and RemoveRemoteSystemVariableHistory() must be called for new and lost connections + /// \param[in] context Holds the context of this group of serialize calls. This can be a stack object just passed to the function. + /// \param[in] _guid Which system we are sending to + /// \param[in] _bitSteam Which bitStream to write to + void BeginUniqueSerialize(SerializationContext *context, RakNetGUID _guid, BitStream *_bitStream); + + /// \brief Call before doing one or more SerializeVariable calls for data that is sent with the same value to every remote system (such as health, position, etc.) + /// This is the most common type of serialization, and also the most efficient + /// Disadvantages: A copy of every variable still needs to be held, although only once + /// Advantages: After the first serialization, the last serialized bitStream will be used for subsequent sends + /// \pre Call OnPreSerializeTick() before doing any calls to BeginIdenticalSerialize() for each of your objects, once per game tick + /// \param[in] context Holds the context of this group of serialize calls. This can be a stack object just passed to the function. + /// \param[in] _isFirstSerializeToThisSystem Pass true if this is the first time ever serializing to this system (the initial download). This way all variables will be written, rather than checking against prior sent values. + /// \param[in] _bitSteam Which bitStream to write to + void BeginIdenticalSerialize(SerializationContext *context, bool _isFirstSerializeToThisSystem, BitStream *_bitStream); + + /// \brief Call after BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize(), then after calling SerializeVariable() one or more times + /// \param[in] context Same context pointer passed to BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize() + void EndSerialize(SerializationContext *context); + + /// \brief Call when you receive the BitStream written by SerializeVariable(), before calling DeserializeVariable() + /// \param[in] context Holds the context of this group of deserialize calls. This can be a stack object just passed to the function. + /// \param[in] _bitStream Pass the bitStream originally passed to and written to by serialize calls + void BeginDeserialize(DeserializationContext *context, BitStream *_bitStream); + + /// \param[in] context Same context pointer passed to BeginDeserialize() + void EndDeserialize(DeserializationContext *context); + + /// BeginUnreliableAckedSerialize() and BeginUniqueSerialize() require knowledge of when connections are added and dropped + /// Call AddRemoteSystemVariableHistory() and RemoveRemoteSystemVariableHistory() to notify the system of these events + /// \param[in] _guid Which system we are sending to + void AddRemoteSystemVariableHistory(RakNetGUID guid); + + /// BeginUnreliableAckedSerialize() and BeginUniqueSerialize() require knowledge of when connections are added and dropped + /// Call AddRemoteSystemVariableHistory() and RemoveRemoteSystemVariableHistory() to notify the system of these events + /// \param[in] _guid Which system we are sending to + void RemoveRemoteSystemVariableHistory(RakNetGUID guid); + + /// BeginIdenticalSerialize() requires knowledge of when serialization has started for an object across multiple systems + /// This way it can setup the flag to do new comparisons against the last sent values, rather than just resending the last sent bitStream + /// For Replica3, overload and call this from Replica3::OnUserReplicaPreSerializeTick() + void OnPreSerializeTick(void); + + /// Call when getting ID_SND_RECEIPT_LOSS or ID_SND_RECEIPT_ACKED for a particular system + /// Example: + /// + /// uint32_t msgNumber; + /// memcpy(&msgNumber, packet->data+1, 4); + /// DataStructures::List replicaListOut; + /// replicaManager.GetReplicasCreatedByMe(replicaListOut); + /// unsigned int idx; + /// for (idx=0; idx < replicaListOut.GetSize(); idx++) + /// { + /// ((SampleReplica*)replicaListOut[idx])->NotifyReplicaOfMessageDeliveryStatus(packet->guid,msgNumber, packet->data[0]==ID_SND_RECEIPT_ACKED); + /// } + /// + /// \param[in] guid Which system we are sending to + /// \param[in] receiptId Encoded in bytes 1-4 inclusive of ID_SND_RECEIPT_LOSS and ID_SND_RECEIPT_ACKED + /// \param[in] messageArrived True for ID_SND_RECEIPT_ACKED, false otherwise + void OnMessageReceipt(RakNetGUID guid, uint32_t receiptId, bool messageArrived); + + /// Call to Serialize a variable + /// Will write to the bitSteam passed to \a context true, variableValue if the variable has changed or has never been written. Otherwise will write false. + /// \pre You have called BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize() + /// \pre Will also require calling OnPreSerializeTick() if using BeginIdenticalSerialize() + /// \note Be sure to call EndSerialize() after finishing all serializations + /// \param[in] context Same context pointer passed to BeginUnreliableAckedSerialize(), BeginUniqueSerialize(), or BeginIdenticalSerialize() + /// \param[in] variable A variable to write to the bitStream passed to \a context + template + void SerializeVariable(SerializationContext *context, const VarType &variable) + { + if (context->newSystemSend) + { + if (context->variableHistory->variableListDeltaTracker.IsPastEndOfList()==false) + { + // previously sent data to another system + context->bitStream->Write(true); + context->bitStream->Write(variable); + context->anyVariablesWritten=true; + } + else + { + // never sent data to another system + context->variableHistory->variableListDeltaTracker.WriteVarToBitstream(variable, context->bitStream); + context->anyVariablesWritten=true; + } + } + else if (context->serializationMode==UNRELIABLE_WITH_ACK_RECEIPT) + { + context->anyVariablesWritten|= + context->variableHistory->variableListDeltaTracker.WriteVarToBitstream(variable, context->bitStream, context->changedVariables->bitField, context->changedVariables->bitWriteIndex++); + } + else + { + if (context->variableHistoryIdentical) + { + // Identical serialization to a number of systems + if (didComparisonThisTick==false) + context->anyVariablesWritten|= + context->variableHistory->variableListDeltaTracker.WriteVarToBitstream(variable, context->bitStream); + // Else bitstream is written to at the end + } + else + { + // Per-system serialization + context->anyVariablesWritten|= + context->variableHistory->variableListDeltaTracker.WriteVarToBitstream(variable, context->bitStream); + } + } + } + + /// Call to deserialize into a variable + /// \pre You have called BeginDeserialize() + /// \note Be sure to call EndDeserialize() after finishing all deserializations + /// \param[in] context Same context pointer passed to BeginDeserialize() + /// \param[in] variable A variable to write to the bitStream passed to \a context + template + bool DeserializeVariable(DeserializationContext *context, VarType &variable) + { + return VariableListDeltaTracker::ReadVarFromBitstream(variable, context->bitStream); + } + + + +protected: + + // For a given send receipt from RakPeer::Send() track which variables we updated + // That way if that send does not arrive (ID_SND_RECEIPT_LOSS) we can mark those variables as dirty to resend them with current values + struct ChangedVariablesList + { + uint32_t sendReceipt; + unsigned short bitWriteIndex; + unsigned char bitField[56]; + }; + + // static int Replica2ObjectComp( const uint32_t &key, ChangedVariablesList* const &data ); + + static int UpdatedVariablesListPtrComp( const uint32_t &key, ChangedVariablesList* const &data ); + + // For each remote system, track the last values of variables we sent to them, and the history of what values changed per call to Send() + // Every serialize if a variable changes from its last value, send it out again + // Also if a send does not arrive (ID_SND_RECEIPT_LOSS) we use updatedVariablesHistory to mark those variables as dirty, to resend them unreliably with the current values + struct RemoteSystemVariableHistory + { + RakNetGUID guid; + VariableListDeltaTracker variableListDeltaTracker; + DataStructures::OrderedList updatedVariablesHistory; + }; + /// A list of RemoteSystemVariableHistory indexed by guid, one per connection that we serialize to + /// List is added to when SerializeConstruction is called, and removed from when SerializeDestruction is called, or when a given connection is dropped + DataStructures::List remoteSystemVariableHistoryList; + + // Because the ChangedVariablesList is created every serialize and destroyed every receipt I use a pool to avoid fragmentation + DataStructures::MemoryPool updatedVariablesMemoryPool; + + bool didComparisonThisTick; + RakNet::BitStream identicalSerializationBs; + + void FreeVarsAssociatedWithReceipt(RakNetGUID guid, uint32_t receiptId); + void DirtyAndFreeVarsAssociatedWithReceipt(RakNetGUID guid, uint32_t receiptId); + unsigned int GetVarsWrittenPerRemoteSystemListIndex(RakNetGUID guid); + void RemoveRemoteSystemVariableHistory(void); + + RemoteSystemVariableHistory* GetRemoteSystemVariableHistory(RakNetGUID guid); + + ChangedVariablesList *AllocChangedVariablesList(void); + void FreeChangedVariablesList(ChangedVariablesList *changedVariables); + void StoreChangedVariablesList(RemoteSystemVariableHistory *variableHistory, ChangedVariablesList *changedVariables, uint32_t sendReceipt); + + RemoteSystemVariableHistory *StartVariableHistoryWrite(RakNetGUID guid); + unsigned int GetRemoteSystemHistoryListIndex(RakNetGUID guid); + +}; + +} + diff --git a/Source/VariableListDeltaTracker.h b/Source/VariableListDeltaTracker.h index a0625b111..e6bcdbde2 100644 --- a/Source/VariableListDeltaTracker.h +++ b/Source/VariableListDeltaTracker.h @@ -1,146 +1,144 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "NativeTypes.h" -#include "DS_List.h" -#include "RakMemoryOverride.h" -#include "BitStream.h" - -#ifndef __VARIABLE_LIST_DELTA_TRACKER -#define __VARIABLE_LIST_DELTA_TRACKER - -namespace RakNet -{ -/// Class to write a series of variables, copy the contents to memory, and return if the newly written value is different than what was last written -/// Can also encode the reads, writes, and results directly to/from a bitstream -class VariableListDeltaTracker -{ -public: - VariableListDeltaTracker(); - ~VariableListDeltaTracker(); - - // Call before using a series of WriteVar - void StartWrite(void); - - bool IsPastEndOfList(void) const {return nextWriteIndex>=variableList.Size();} - - /// Records the passed value of the variable to memory, and returns true if the value is different from the write before that (or if it is the first write) - /// \pre Call StartWrite() before doing the first of a series of calls to WriteVar or other functions that call WriteVar - /// \note Variables must be of the same type, written in the same order, each time - template - bool WriteVar(const VarType &varData) - { - RakNet::BitStream temp; - temp.Write(varData); - if (nextWriteIndex>=variableList.Size()) - { - variableList.Push(VariableLastValueNode(temp.GetData(),temp.GetNumberOfBytesUsed()),_FILE_AND_LINE_); - nextWriteIndex++; - return true; // Different because it's new - } - - if (temp.GetNumberOfBytesUsed()!=variableList[nextWriteIndex].byteLength) - { - variableList[nextWriteIndex].lastData=(char*) rakRealloc_Ex(variableList[nextWriteIndex].lastData, temp.GetNumberOfBytesUsed(),_FILE_AND_LINE_); - variableList[nextWriteIndex].byteLength=temp.GetNumberOfBytesUsed(); - memcpy(variableList[nextWriteIndex].lastData,temp.GetData(),temp.GetNumberOfBytesUsed()); - nextWriteIndex++; - variableList[nextWriteIndex].isDirty=false; - return true; // Different because the serialized size is different - } - if (variableList[nextWriteIndex].isDirty==false && memcmp(temp.GetData(),variableList[nextWriteIndex].lastData, variableList[nextWriteIndex].byteLength)==0) - { - nextWriteIndex++; - return false; // Same because not dirty and memcmp is the same - } - - variableList[nextWriteIndex].isDirty=false; - memcpy(variableList[nextWriteIndex].lastData,temp.GetData(),temp.GetNumberOfBytesUsed()); - nextWriteIndex++; - return true; // Different because dirty or memcmp was different - } - /// Calls WriteVar. If the variable has changed, writes true, and writes the variable. Otherwise writes false. - template - bool WriteVarToBitstream(const VarType &varData, RakNet::BitStream *bitStream) - { - bool wasDifferent = WriteVar(varData); - bitStream->Write(wasDifferent); - if (wasDifferent) - { - bitStream->Write(varData); - return true; - } - return false; - } - /// Calls WriteVarToBitstream(). Additionally, adds the boolean result of WriteVar() to boolean bit array - template - bool WriteVarToBitstream(const VarType &varData, RakNet::BitStream *bitStream, unsigned char *bArray, unsigned short writeOffset) - { - if (WriteVarToBitstream(varData,bitStream)==true) - { - BitSize_t numberOfBitsMod8 = writeOffset & 7; - - if ( numberOfBitsMod8 == 0 ) - bArray[ writeOffset >> 3 ] = 0x80; - else - bArray[ writeOffset >> 3 ] |= 0x80 >> ( numberOfBitsMod8 ); // Set the bit to 1 - - return true; - } - else - { - if ( ( writeOffset & 7 ) == 0 ) - bArray[ writeOffset >> 3 ] = 0; - - return false; - } - } - - /// Paired with a call to WriteVarToBitstream(), will read a variable if it had changed. Otherwise the values remains the same. - template - static bool ReadVarFromBitstream(VarType &varData, RakNet::BitStream *bitStream) - { - bool wasWritten; - if (bitStream->Read(wasWritten)==false) - return false; - if (wasWritten) - { - if (bitStream->Read(varData)==false) - return false; - } - return wasWritten; - } - - /// Variables flagged dirty will cause WriteVar() to return true, even if the variable had not otherwise changed - /// This updates all the variables in the list, where in each index \a varsWritten is true, so will the variable at the corresponding index be flagged dirty - void FlagDirtyFromBitArray(unsigned char *bArray); - - /// \internal - struct VariableLastValueNode - { - VariableLastValueNode(); - VariableLastValueNode(const unsigned char *data, int _byteLength); - ~VariableLastValueNode(); - char *lastData; - unsigned int byteLength; - bool isDirty; - }; - -protected: - /// \internal - DataStructures::List variableList; - /// \internal - unsigned int nextWriteIndex; -}; - - -} - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "NativeTypes.h" +#include "DS_List.h" +#include "RakMemoryOverride.h" +#include "BitStream.h" + +#pragma once + +namespace RakNet +{ +/// Class to write a series of variables, copy the contents to memory, and return if the newly written value is different than what was last written +/// Can also encode the reads, writes, and results directly to/from a bitstream +class VariableListDeltaTracker +{ +public: + VariableListDeltaTracker(); + ~VariableListDeltaTracker(); + + // Call before using a series of WriteVar + void StartWrite(void); + + bool IsPastEndOfList(void) const {return nextWriteIndex>=variableList.Size();} + + /// Records the passed value of the variable to memory, and returns true if the value is different from the write before that (or if it is the first write) + /// \pre Call StartWrite() before doing the first of a series of calls to WriteVar or other functions that call WriteVar + /// \note Variables must be of the same type, written in the same order, each time + template + bool WriteVar(const VarType &varData) + { + RakNet::BitStream temp; + temp.Write(varData); + if (nextWriteIndex>=variableList.Size()) + { + variableList.Push(VariableLastValueNode(temp.GetData(),temp.GetNumberOfBytesUsed()),_FILE_AND_LINE_); + nextWriteIndex++; + return true; // Different because it's new + } + + if (temp.GetNumberOfBytesUsed()!=variableList[nextWriteIndex].byteLength) + { + variableList[nextWriteIndex].lastData=(char*) rakRealloc_Ex(variableList[nextWriteIndex].lastData, temp.GetNumberOfBytesUsed(),_FILE_AND_LINE_); + variableList[nextWriteIndex].byteLength=temp.GetNumberOfBytesUsed(); + memcpy(variableList[nextWriteIndex].lastData,temp.GetData(),temp.GetNumberOfBytesUsed()); + nextWriteIndex++; + variableList[nextWriteIndex].isDirty=false; + return true; // Different because the serialized size is different + } + if (variableList[nextWriteIndex].isDirty==false && memcmp(temp.GetData(),variableList[nextWriteIndex].lastData, variableList[nextWriteIndex].byteLength)==0) + { + nextWriteIndex++; + return false; // Same because not dirty and memcmp is the same + } + + variableList[nextWriteIndex].isDirty=false; + memcpy(variableList[nextWriteIndex].lastData,temp.GetData(),temp.GetNumberOfBytesUsed()); + nextWriteIndex++; + return true; // Different because dirty or memcmp was different + } + /// Calls WriteVar. If the variable has changed, writes true, and writes the variable. Otherwise writes false. + template + bool WriteVarToBitstream(const VarType &varData, RakNet::BitStream *bitStream) + { + bool wasDifferent = WriteVar(varData); + bitStream->Write(wasDifferent); + if (wasDifferent) + { + bitStream->Write(varData); + return true; + } + return false; + } + /// Calls WriteVarToBitstream(). Additionally, adds the boolean result of WriteVar() to boolean bit array + template + bool WriteVarToBitstream(const VarType &varData, RakNet::BitStream *bitStream, unsigned char *bArray, unsigned short writeOffset) + { + if (WriteVarToBitstream(varData,bitStream)==true) + { + BitSize_t numberOfBitsMod8 = writeOffset & 7; + + if ( numberOfBitsMod8 == 0 ) + bArray[ writeOffset >> 3 ] = 0x80; + else + bArray[ writeOffset >> 3 ] |= 0x80 >> ( numberOfBitsMod8 ); // Set the bit to 1 + + return true; + } + else + { + if ( ( writeOffset & 7 ) == 0 ) + bArray[ writeOffset >> 3 ] = 0; + + return false; + } + } + + /// Paired with a call to WriteVarToBitstream(), will read a variable if it had changed. Otherwise the values remains the same. + template + static bool ReadVarFromBitstream(VarType &varData, RakNet::BitStream *bitStream) + { + bool wasWritten; + if (bitStream->Read(wasWritten)==false) + return false; + if (wasWritten) + { + if (bitStream->Read(varData)==false) + return false; + } + return wasWritten; + } + + /// Variables flagged dirty will cause WriteVar() to return true, even if the variable had not otherwise changed + /// This updates all the variables in the list, where in each index \a varsWritten is true, so will the variable at the corresponding index be flagged dirty + void FlagDirtyFromBitArray(unsigned char *bArray); + + /// \internal + struct VariableLastValueNode + { + VariableLastValueNode(); + VariableLastValueNode(const unsigned char *data, int _byteLength); + ~VariableLastValueNode(); + char *lastData; + unsigned int byteLength; + bool isDirty; + }; + +protected: + /// \internal + DataStructures::List variableList; + /// \internal + unsigned int nextWriteIndex; +}; + + +} + diff --git a/Source/VariadicSQLParser.h b/Source/VariadicSQLParser.h index e39ed3b82..d18e2aba4 100644 --- a/Source/VariadicSQLParser.h +++ b/Source/VariadicSQLParser.h @@ -1,34 +1,32 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __VARIADIC_SQL_PARSER_H -#define __VARIADIC_SQL_PARSER_H - -#include "DS_List.h" - -#include - -namespace VariadicSQLParser -{ - struct IndexAndType - { - unsigned int strIndex; - unsigned int typeMappingIndex; - }; - const char* GetTypeMappingAtIndex(int i); - void GetTypeMappingIndices( const char *format, DataStructures::List &indices ); - // Given an SQL string with variadic arguments, allocate argumentBinary and argumentLengths, and hold the parameters in binary format - // Last 2 parameters are out parameters - void ExtractArguments( va_list argptr, const DataStructures::List &indices, char ***argumentBinary, int **argumentLengths ); - void FreeArguments(const DataStructures::List &indices, char **argumentBinary, int *argumentLengths); -} - - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#include "DS_List.h" + +#include + +namespace VariadicSQLParser +{ + struct IndexAndType + { + unsigned int strIndex; + unsigned int typeMappingIndex; + }; + const char* GetTypeMappingAtIndex(int i); + void GetTypeMappingIndices( const char *format, DataStructures::List &indices ); + // Given an SQL string with variadic arguments, allocate argumentBinary and argumentLengths, and hold the parameters in binary format + // Last 2 parameters are out parameters + void ExtractArguments( va_list argptr, const DataStructures::List &indices, char ***argumentBinary, int **argumentLengths ); + void FreeArguments(const DataStructures::List &indices, char **argumentBinary, int *argumentLengths); +} + + diff --git a/Source/WSAStartupSingleton.cpp b/Source/WSAStartupSingleton.cpp index fd05f3c60..390a10152 100644 --- a/Source/WSAStartupSingleton.cpp +++ b/Source/WSAStartupSingleton.cpp @@ -1,85 +1,85 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#include "WSAStartupSingleton.h" - - - - - -#if defined(_WIN32) && !defined(WINDOWS_STORE_RT) -#include -#include - - - - - -#endif -#include "RakNetDefines.h" -#include - -int WSAStartupSingleton::refCount=0; - -WSAStartupSingleton::WSAStartupSingleton() {} -WSAStartupSingleton::~WSAStartupSingleton() {} -void WSAStartupSingleton::AddRef(void) -{ -#if defined(_WIN32) && !defined(WINDOWS_STORE_RT) - - refCount++; - - if (refCount!=1) - return; - - - - - - WSADATA winsockInfo; - if ( WSAStartup( MAKEWORD( 2, 2 ), &winsockInfo ) != 0 ) - { -#if defined(_DEBUG) && !defined(WINDOWS_PHONE_8) - DWORD dwIOError = GetLastError(); - LPVOID messageBuffer; - FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language - ( LPTSTR ) & messageBuffer, 0, NULL ); - // something has gone wrong here... - RAKNET_DEBUG_PRINTF( "WSAStartup failed:Error code - %d\n%s", dwIOError, messageBuffer ); - //Free the buffer. - LocalFree( messageBuffer ); -#endif - } - -#endif -} -void WSAStartupSingleton::Deref(void) -{ -#if defined(_WIN32) && !defined(WINDOWS_STORE_RT) - if (refCount==0) - return; - - if (refCount>1) - { - refCount--; - return; - } - - WSACleanup(); - - - - - - - refCount=0; -#endif -} +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#include "WSAStartupSingleton.h" + + + + + +#if defined(_WIN32) && !defined(WINDOWS_STORE_RT) +#include +#include + + + + + +#endif +#include "RakNetDefines.h" +#include + +int WSAStartupSingleton::refCount=0; + +WSAStartupSingleton::WSAStartupSingleton() {} +WSAStartupSingleton::~WSAStartupSingleton() {} +void WSAStartupSingleton::AddRef(void) +{ +#if defined(_WIN32) && !defined(WINDOWS_STORE_RT) + + refCount++; + + if (refCount!=1) + return; + + + + + + WSADATA winsockInfo; + if ( WSAStartup( MAKEWORD( 2, 2 ), &winsockInfo ) != 0 ) + { +#if defined(_DEBUG) && !defined(WINDOWS_PHONE_8) + DWORD dwIOError = GetLastError(); + LPVOID messageBuffer; + FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language + ( LPTSTR ) & messageBuffer, 0, nullptr ); + // something has gone wrong here... + RAKNET_DEBUG_PRINTF( "WSAStartup failed:Error code - %d\n%s", dwIOError, messageBuffer ); + //Free the buffer. + LocalFree( messageBuffer ); +#endif + } + +#endif +} +void WSAStartupSingleton::Deref(void) +{ +#if defined(_WIN32) && !defined(WINDOWS_STORE_RT) + if (refCount==0) + return; + + if (refCount>1) + { + refCount--; + return; + } + + WSACleanup(); + + + + + + + refCount=0; +#endif +} diff --git a/Source/WSAStartupSingleton.h b/Source/WSAStartupSingleton.h index a600af1e6..9a34c761a 100644 --- a/Source/WSAStartupSingleton.h +++ b/Source/WSAStartupSingleton.h @@ -1,26 +1,24 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __WSA_STARTUP_SINGLETON_H -#define __WSA_STARTUP_SINGLETON_H - -class WSAStartupSingleton -{ -public: - WSAStartupSingleton(); - ~WSAStartupSingleton(); - static void AddRef(void); - static void Deref(void); - -protected: - static int refCount; -}; - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +class WSAStartupSingleton +{ +public: + WSAStartupSingleton(); + ~WSAStartupSingleton(); + static void AddRef(void); + static void Deref(void); + +protected: + static int refCount; +}; + diff --git a/Source/WindowsIncludes.h b/Source/WindowsIncludes.h index bd3905f83..aae19a700 100644 --- a/Source/WindowsIncludes.h +++ b/Source/WindowsIncludes.h @@ -1,29 +1,32 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#if defined (WINDOWS_STORE_RT) -#include -#include -#elif defined (_WIN32) -#include -#include -#include - -// Must always include Winsock2.h before windows.h -// or else: -// winsock2.h(99) : error C2011: 'fd_set' : 'struct' type redefinition -// winsock2.h(134) : warning C4005: 'FD_SET' : macro redefinition -// winsock.h(83) : see previous definition of 'FD_SET' -// winsock2.h(143) : error C2011: 'timeval' : 'struct' type redefinition -// winsock2.h(199) : error C2011: 'hostent' : 'struct' type redefinition -// winsock2.h(212) : error C2011: 'netent' : 'struct' type redefinition -// winsock2.h(219) : error C2011: 'servent' : 'struct' type redefinition - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + + +#if defined (WINDOWS_STORE_RT) +#include +#include +#elif defined (_WIN32) +#include +#include +#include + +// Must always include Winsock2.h before windows.h +// or else: +// winsock2.h(99) : error C2011: 'fd_set' : 'struct' type redefinition +// winsock2.h(134) : warning C4005: 'FD_SET' : macro redefinition +// winsock.h(83) : see previous definition of 'FD_SET' +// winsock2.h(143) : error C2011: 'timeval' : 'struct' type redefinition +// winsock2.h(199) : error C2011: 'hostent' : 'struct' type redefinition +// winsock2.h(212) : error C2011: 'netent' : 'struct' type redefinition +// winsock2.h(219) : error C2011: 'servent' : 'struct' type redefinition + +#endif diff --git a/Source/_FindFirst.h b/Source/_FindFirst.h index a664222d7..409db4e69 100644 --- a/Source/_FindFirst.h +++ b/Source/_FindFirst.h @@ -1,56 +1,53 @@ -/// -/// Original file by the_viking, fixed by Rômulo Fernandes -/// Should emulate windows finddata structure -/// - -#ifndef GCC_FINDFIRST_H -#define GCC_FINDFIRST_H - -#if (defined(__GNUC__) || defined(__ARMCC_VERSION) || defined(__GCCXML__) || defined(__S3E__) ) && !defined(__WIN32) - -#include - -#include "RakString.h" - -#define _A_NORMAL 0x00 // Normal file -#define _A_RDONLY 0x01 // Read-only file -#define _A_HIDDEN 0x02 // Hidden file -#define _A_SYSTEM 0x04 // System file -#define _A_VOLID 0x08 // Volume ID -#define _A_SUBDIR 0x10 // Subdirectory -#define _A_ARCH 0x20 // File changed since last archive -#define FA_NORMAL 0x00 // Synonym of _A_NORMAL -#define FA_RDONLY 0x01 // Synonym of _A_RDONLY -#define FA_HIDDEN 0x02 // Synonym of _A_HIDDEN -#define FA_SYSTEM 0x04 // Synonym of _A_SYSTEM -#define FA_LABEL 0x08 // Synonym of _A_VOLID -#define FA_DIREC 0x10 // Synonym of _A_SUBDIR -#define FA_ARCH 0x20 // Synonym of _A_ARCH - - -const unsigned STRING_BUFFER_SIZE = 512; - -typedef struct _finddata_t -{ - char name[STRING_BUFFER_SIZE]; - int attrib; - unsigned long size; -} _finddata; - -/** - * Hold information about the current search - */ -typedef struct _findinfo_t -{ - DIR* openedDir; - RakNet::RakString filter; - RakNet::RakString dirName; -} _findinfo; - -long _findfirst(const char *name, _finddata_t *f); -int _findnext(long h, _finddata_t *f); -int _findclose(long h); - -#endif -#endif - +/// +/// Original file by the_viking, fixed by Rômulo Fernandes +/// Should emulate windows finddata structure +/// + +#pragma once + +#if (defined(__GNUC__) || defined(__ARMCC_VERSION) || defined(__GCCXML__) || defined(__S3E__) ) && !defined(__WIN32) + +#include + +#include "RakString.h" + +#define _A_NORMAL 0x00 // Normal file +#define _A_RDONLY 0x01 // Read-only file +#define _A_HIDDEN 0x02 // Hidden file +#define _A_SYSTEM 0x04 // System file +#define _A_VOLID 0x08 // Volume ID +#define _A_SUBDIR 0x10 // Subdirectory +#define _A_ARCH 0x20 // File changed since last archive +#define FA_NORMAL 0x00 // Synonym of _A_NORMAL +#define FA_RDONLY 0x01 // Synonym of _A_RDONLY +#define FA_HIDDEN 0x02 // Synonym of _A_HIDDEN +#define FA_SYSTEM 0x04 // Synonym of _A_SYSTEM +#define FA_LABEL 0x08 // Synonym of _A_VOLID +#define FA_DIREC 0x10 // Synonym of _A_SUBDIR +#define FA_ARCH 0x20 // Synonym of _A_ARCH + + +const unsigned STRING_BUFFER_SIZE = 512; + +typedef struct _finddata_t +{ + char name[STRING_BUFFER_SIZE]; + int attrib; + unsigned long size; +} _finddata; + +/** + * Hold information about the current search + */ +typedef struct _findinfo_t +{ + DIR* openedDir; + RakNet::RakString filter; + RakNet::RakString dirName; +} _findinfo; + +long _findfirst(const char *name, _finddata_t *f); +int _findnext(long h, _finddata_t *f); +int _findclose(long h); + +#endif diff --git a/Source/gettimeofday.cpp b/Source/gettimeofday.cpp index 4081abdbc..2794c85b2 100644 --- a/Source/gettimeofday.cpp +++ b/Source/gettimeofday.cpp @@ -1,69 +1,69 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#if defined(_WIN32) && !defined(__GNUC__) &&!defined(__GCCXML__) - -#include "gettimeofday.h" - -// From http://www.openasthra.com/c-tidbits/gettimeofday-function-for-windows/ - -#include "WindowsIncludes.h" - -#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) - #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 -#else - #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL -#endif - -int gettimeofday(struct timeval *tv, struct timezone *tz) -{ -#if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) - // _tzset not supported - (void) tv; - (void) tz; -#else - - FILETIME ft; - unsigned __int64 tmpres = 0; - static int tzflag; - - if (NULL != tv) - { - GetSystemTimeAsFileTime(&ft); - - tmpres |= ft.dwHighDateTime; - tmpres <<= 32; - tmpres |= ft.dwLowDateTime; - - /*converting file time to unix epoch*/ - tmpres /= 10; /*convert into microseconds*/ - tmpres -= DELTA_EPOCH_IN_MICROSECS; - tv->tv_sec = (long)(tmpres / 1000000UL); - tv->tv_usec = (long)(tmpres % 1000000UL); - } - - if (NULL != tz) - { - if (!tzflag) - { - _tzset(); - tzflag++; - } - tz->tz_minuteswest = _timezone / 60; - tz->tz_dsttime = _daylight; - } - -#endif - - return 0; -} - -#endif - +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#if defined(_WIN32) && !defined(__GNUC__) &&!defined(__GCCXML__) + +#include "gettimeofday.h" + +// From http://www.openasthra.com/c-tidbits/gettimeofday-function-for-windows/ + +#include "WindowsIncludes.h" + +#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#else + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#endif + +int gettimeofday(struct timeval *tv, struct timezone *tz) +{ +#if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT) + // _tzset not supported + (void) tv; + (void) tz; +#else + + FILETIME ft; + unsigned __int64 tmpres = 0; + static int tzflag; + + if (nullptr != tv) + { + GetSystemTimeAsFileTime(&ft); + + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + + /*converting file time to unix epoch*/ + tmpres /= 10; /*convert into microseconds*/ + tmpres -= DELTA_EPOCH_IN_MICROSECS; + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } + + if (nullptr != tz) + { + if (!tzflag) + { + _tzset(); + tzflag++; + } + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; + } + +#endif + + return 0; +} + +#endif + diff --git a/Source/gettimeofday.h b/Source/gettimeofday.h index 7cab89cf0..4a67488f7 100644 --- a/Source/gettimeofday.h +++ b/Source/gettimeofday.h @@ -1,74 +1,72 @@ -/* - * Copyright (c) 2014, Oculus VR, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - */ - -#ifndef __GET_TIME_OF_DAY_H -#define __GET_TIME_OF_DAY_H - -#if defined(_WIN32) && !defined(__GNUC__) &&!defined(__GCCXML__) -#include < time.h > -struct timezone -{ - int tz_minuteswest; /* minutes W of Greenwich */ - int tz_dsttime; /* type of dst correction */ -}; - -#if defined(WINDOWS_STORE_RT) -struct timeval { - long tv_sec; - long tv_usec; -}; -#endif - -int gettimeofday(struct timeval *tv, struct timezone *tz); - - -#else - - - - -#include - -#include - -// Uncomment this if you need to -/* -// http://www.halcode.com/archives/2008/08/26/retrieving-system-time-gettimeofday/ -struct timezone -{ - int tz_minuteswest; - int tz_dsttime; -}; - -#ifdef __cplusplus - -void GetSystemTimeAsFileTime(FILETIME*); - -inline int gettimeofday(struct timeval* p, void* tz ) -{ - union { - long long ns100; // time since 1 Jan 1601 in 100ns units - FILETIME ft; - } now; - - GetSystemTimeAsFileTime( &(now.ft) ); - p->tv_usec=(long)((now.ns100 / 10LL) % 1000000LL ); - p->tv_sec= (long)((now.ns100-(116444736000000000LL))/10000000LL); - return 0; -} - -#else - int gettimeofday(struct timeval* p, void* tz ); -#endif -*/ - -#endif - -#endif +/* + * Copyright (c) 2014, Oculus VR, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ + +#pragma once + +#if defined(_WIN32) && !defined(__GNUC__) &&!defined(__GCCXML__) +#include < time.h > +struct timezone +{ + int tz_minuteswest; /* minutes W of Greenwich */ + int tz_dsttime; /* type of dst correction */ +}; + +#if defined(WINDOWS_STORE_RT) +struct timeval { + long tv_sec; + long tv_usec; +}; +#endif + +int gettimeofday(struct timeval *tv, struct timezone *tz); + + +#else + + + + +#include + +#include + +// Uncomment this if you need to +/* +// http://www.halcode.com/archives/2008/08/26/retrieving-system-time-gettimeofday/ +struct timezone +{ + int tz_minuteswest; + int tz_dsttime; +}; + +#ifdef __cplusplus + +void GetSystemTimeAsFileTime(FILETIME*); + +inline int gettimeofday(struct timeval* p, void* tz ) +{ + union { + long long ns100; // time since 1 Jan 1601 in 100ns units + FILETIME ft; + } now; + + GetSystemTimeAsFileTime( &(now.ft) ); + p->tv_usec=(long)((now.ns100 / 10LL) % 1000000LL ); + p->tv_sec= (long)((now.ns100-(116444736000000000LL))/10000000LL); + return 0; +} + +#else + int gettimeofday(struct timeval* p, void* tz ); +#endif +*/ + +#endif +