├── acc.txt ├── config.json ├── pch.cpp ├── enet ├── include │ ├── utility.h │ ├── types.h │ ├── time2.h │ ├── callbacks.h │ ├── list.h │ ├── unix.h │ ├── win32.h │ ├── protocol.h │ └── enet.h ├── callbacks.c ├── list.c ├── packet.c ├── win32.c ├── unix.c ├── host.c └── compress.c ├── pch.h ├── proton ├── hash.hpp ├── vector.hpp ├── rtparam.hpp └── variant.hpp ├── CPPBot.cpp ├── README.md ├── packet.h ├── userfunc.h ├── utilsfunc.h ├── corefunc.h └── LICENSE /acc.txt: -------------------------------------------------------------------------------- 1 | a -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "bot1": "", 3 | "pass1": "", 4 | "gtversion": "4.20", 5 | "home": "" 6 | } 7 | -------------------------------------------------------------------------------- /pch.cpp: -------------------------------------------------------------------------------- 1 | // pch.cpp: source file corresponding to pre-compiled header; necessary for compilation to succeed 2 | 3 | #include "pch.h" 4 | #include "json.hpp" 5 | // In general, ignore this file, but keep it around if you are using pre-compiled headers. 6 | -------------------------------------------------------------------------------- /enet/include/utility.h: -------------------------------------------------------------------------------- 1 | /** 2 | @file utility.h 3 | @brief ENet utility header 4 | */ 5 | #ifndef __ENET_UTILITY_H__ 6 | #define __ENET_UTILITY_H__ 7 | 8 | #define ENET_MAX(x, y) ((x) > (y) ? (x) : (y)) 9 | #define ENET_MIN(x, y) ((x) < (y) ? (x) : (y)) 10 | 11 | #endif /* __ENET_UTILITY_H__ */ 12 | 13 | -------------------------------------------------------------------------------- /enet/include/types.h: -------------------------------------------------------------------------------- 1 | /** 2 | @file types.h 3 | @brief type definitions for ENet 4 | */ 5 | #ifndef __ENET_TYPES_H__ 6 | #define __ENET_TYPES_H__ 7 | 8 | typedef unsigned char enet_uint8; /**< unsigned 8-bit type */ 9 | typedef unsigned short enet_uint16; /**< unsigned 16-bit type */ 10 | typedef unsigned int enet_uint32; /**< unsigned 32-bit type */ 11 | 12 | #endif /* __ENET_TYPES_H__ */ 13 | 14 | -------------------------------------------------------------------------------- /enet/include/time2.h: -------------------------------------------------------------------------------- 1 | /** 2 | @file time.h 3 | @brief ENet time constants and macros 4 | */ 5 | #ifndef __ENET_TIME_H__ 6 | #define __ENET_TIME_H__ 7 | 8 | #define ENET_TIME_OVERFLOW 86400000 9 | 10 | #define ENET_TIME_LESS(a, b) ((a) - (b) >= ENET_TIME_OVERFLOW) 11 | #define ENET_TIME_GREATER(a, b) ((b) - (a) >= ENET_TIME_OVERFLOW) 12 | #define ENET_TIME_LESS_EQUAL(a, b) (! ENET_TIME_GREATER (a, b)) 13 | #define ENET_TIME_GREATER_EQUAL(a, b) (! ENET_TIME_LESS (a, b)) 14 | 15 | #define ENET_TIME_DIFFERENCE(a, b) ((a) - (b) >= ENET_TIME_OVERFLOW ? (b) - (a) : (a) - (b)) 16 | 17 | #endif /* __ENET_TIME_H__ */ 18 | 19 | -------------------------------------------------------------------------------- /enet/include/callbacks.h: -------------------------------------------------------------------------------- 1 | /** 2 | @file callbacks.h 3 | @brief ENet callbacks 4 | */ 5 | #ifndef __ENET_CALLBACKS_H__ 6 | #define __ENET_CALLBACKS_H__ 7 | 8 | #include 9 | 10 | typedef struct _ENetCallbacks 11 | { 12 | void * (ENET_CALLBACK * malloc) (size_t size); 13 | void (ENET_CALLBACK * free) (void * memory); 14 | void (ENET_CALLBACK * no_memory) (void); 15 | } ENetCallbacks; 16 | 17 | /** @defgroup callbacks ENet internal callbacks 18 | @{ 19 | @ingroup private 20 | */ 21 | extern void * enet_malloc (size_t); 22 | extern void enet_free (void *); 23 | 24 | /** @} */ 25 | 26 | #endif /* __ENET_CALLBACKS_H__ */ 27 | 28 | -------------------------------------------------------------------------------- /pch.h: -------------------------------------------------------------------------------- 1 | // Tips for Getting Started: 2 | // 1. Use the Solution Explorer window to add/manage files 3 | // 2. Use the Team Explorer window to connect to source control 4 | // 3. Use the Output window to see build output and other messages 5 | // 4. Use the Error List window to view errors 6 | // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project 7 | // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file 8 | 9 | #ifndef PCH_H 10 | #define PCH_H 11 | #include "json.hpp" 12 | // TODO: add headers that you want to pre-compile here 13 | 14 | #endif //PCH_H 15 | -------------------------------------------------------------------------------- /proton/hash.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | #pragma warning(disable : 4307) 5 | 6 | #ifdef _WIN32 7 | #define INLINE __forceinline 8 | #else //for gcc/clang 9 | #define INLINE __attribute__((always_inline)) 10 | #endif 11 | 12 | 13 | template 14 | class cexpr { 15 | public: 16 | static constexpr t value = val; 17 | }; 18 | namespace hs { 19 | constexpr uint32_t val_32_const = 0x811c9dc5u; 20 | constexpr uint32_t prime_32_const = 0x1000193u; 21 | constexpr uint64_t val_64_const = 0xcbf29ce484222325u; 22 | constexpr uint64_t prime_64_const = 0x100000001b3u; 23 | INLINE constexpr uint32_t hash32(const char* const str, const uint32_t value = val_32_const) noexcept { 24 | return (str[0] == '\0') ? value : hash32(&str[1], (value ^ uint32_t(str[0])) * prime_32_const); 25 | } 26 | INLINE constexpr uint64_t hash64(const char* const str, const uint64_t value = val_64_const) noexcept { 27 | return (str[0] == '\0') ? value : hash64(&str[1], (value ^ uint64_t(str[0])) * prime_64_const); 28 | } 29 | } // namespace hs 30 | #define fnv32(s) (cexpr::value) 31 | #define fnv64(s) (cexpr::value) 32 | -------------------------------------------------------------------------------- /enet/callbacks.c: -------------------------------------------------------------------------------- 1 | /** 2 | @file callbacks.c 3 | @brief ENet callback functions 4 | */ 5 | #define ENET_BUILDING_LIB 1 6 | #include "include/enet.h" 7 | 8 | static ENetCallbacks callbacks = { malloc, free, abort }; 9 | 10 | int 11 | enet_initialize_with_callbacks (ENetVersion version, const ENetCallbacks * inits) 12 | { 13 | if (version < ENET_VERSION_CREATE (1, 3, 0)) 14 | return -1; 15 | 16 | if (inits -> malloc != NULL || inits -> free != NULL) 17 | { 18 | if (inits -> malloc == NULL || inits -> free == NULL) 19 | return -1; 20 | 21 | callbacks.malloc = inits -> malloc; 22 | callbacks.free = inits -> free; 23 | } 24 | 25 | if (inits -> no_memory != NULL) 26 | callbacks.no_memory = inits -> no_memory; 27 | 28 | return enet_initialize (); 29 | } 30 | 31 | ENetVersion 32 | enet_linked_version (void) 33 | { 34 | return ENET_VERSION; 35 | } 36 | 37 | void * 38 | enet_malloc (size_t size) 39 | { 40 | void * memory = callbacks.malloc (size); 41 | 42 | if (memory == NULL) 43 | callbacks.no_memory (); 44 | 45 | return memory; 46 | } 47 | 48 | void 49 | enet_free (void * memory) 50 | { 51 | callbacks.free (memory); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /enet/include/list.h: -------------------------------------------------------------------------------- 1 | /** 2 | @file list.h 3 | @brief ENet list management 4 | */ 5 | #ifndef __ENET_LIST_H__ 6 | #define __ENET_LIST_H__ 7 | 8 | #include 9 | 10 | typedef struct _ENetListNode 11 | { 12 | struct _ENetListNode * next; 13 | struct _ENetListNode * previous; 14 | } ENetListNode; 15 | 16 | typedef ENetListNode * ENetListIterator; 17 | 18 | typedef struct _ENetList 19 | { 20 | ENetListNode sentinel; 21 | } ENetList; 22 | 23 | extern void enet_list_clear (ENetList *); 24 | 25 | extern ENetListIterator enet_list_insert (ENetListIterator, void *); 26 | extern void * enet_list_remove (ENetListIterator); 27 | extern ENetListIterator enet_list_move (ENetListIterator, void *, void *); 28 | 29 | extern size_t enet_list_size (ENetList *); 30 | 31 | #define enet_list_begin(list) ((list) -> sentinel.next) 32 | #define enet_list_end(list) (& (list) -> sentinel) 33 | 34 | #define enet_list_empty(list) (enet_list_begin (list) == enet_list_end (list)) 35 | 36 | #define enet_list_next(iterator) ((iterator) -> next) 37 | #define enet_list_previous(iterator) ((iterator) -> previous) 38 | 39 | #define enet_list_front(list) ((void *) (list) -> sentinel.next) 40 | #define enet_list_back(list) ((void *) (list) -> sentinel.previous) 41 | 42 | #endif /* __ENET_LIST_H__ */ 43 | 44 | -------------------------------------------------------------------------------- /CPPBot.cpp: -------------------------------------------------------------------------------- 1 | // CPPBot.cpp : This file contains the 'main' function. Program execution begins and ends there. 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "corefunc.h" 9 | #include "userfunc.h" 10 | #include 11 | #include 12 | #include 13 | #include "json.hpp" 14 | //#include 15 | //#include 16 | //#include 17 | 18 | using json = nlohmann::json; 19 | using namespace std; 20 | vector bots; 21 | 22 | GrowtopiaBot bot1 = {"", "", "", -1}; 23 | 24 | GrowtopiaBot makeBot(string user, string pass, string host, int port, string vers, string wname) { 25 | GrowtopiaBot bot = {user, pass, host, port}; 26 | bot.gameVersion = vers; 27 | bot.currentWorld = wname; 28 | bot.userInit(); 29 | bots.push_back(bot); 30 | return bot; 31 | } 32 | void botSetup() { 33 | ifstream i("config.json"); 34 | json j; 35 | i >> j; 36 | init(); 37 | system("clear"); 38 | string user1 = j["bot1"].get(), pass1 = j["pass1"].get(); 39 | string vers = j["gtversion"].get(); 40 | string wn = j["home"].get(); 41 | bot1 = makeBot(user1, pass1,"213.179.209.168", 17198, vers, wn); 42 | while (true) { 43 | bot1.eventLoop(); 44 | } 45 | } 46 | int main() { 47 | botSetup(); 48 | } 49 | -------------------------------------------------------------------------------- /enet/include/unix.h: -------------------------------------------------------------------------------- 1 | /** 2 | @file unix.h 3 | @brief ENet Unix header 4 | */ 5 | #ifndef _WIN32 6 | #ifndef __ENET_UNIX_H__ 7 | #define __ENET_UNIX_H__ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #ifdef MSG_MAXIOVLEN 18 | #define ENET_BUFFER_MAXIMUM MSG_MAXIOVLEN 19 | #endif 20 | 21 | typedef int ENetSocket; 22 | 23 | #define ENET_SOCKET_NULL -1 24 | 25 | #define ENET_HOST_TO_NET_16(value) (htons (value)) /**< macro that converts host to net byte-order of a 16-bit value */ 26 | #define ENET_HOST_TO_NET_32(value) (htonl (value)) /**< macro that converts host to net byte-order of a 32-bit value */ 27 | 28 | #define ENET_NET_TO_HOST_16(value) (ntohs (value)) /**< macro that converts net to host byte-order of a 16-bit value */ 29 | #define ENET_NET_TO_HOST_32(value) (ntohl (value)) /**< macro that converts net to host byte-order of a 32-bit value */ 30 | 31 | typedef struct 32 | { 33 | void * data; 34 | size_t dataLength; 35 | } ENetBuffer; 36 | 37 | #define ENET_CALLBACK 38 | 39 | #define ENET_API extern 40 | 41 | typedef fd_set ENetSocketSet; 42 | 43 | #define ENET_SOCKETSET_EMPTY(sockset) FD_ZERO (& (sockset)) 44 | #define ENET_SOCKETSET_ADD(sockset, socket) FD_SET (socket, & (sockset)) 45 | #define ENET_SOCKETSET_REMOVE(sockset, socket) FD_CLR (socket, & (sockset)) 46 | #define ENET_SOCKETSET_CHECK(sockset, socket) FD_ISSET (socket, & (sockset)) 47 | 48 | #endif /* __ENET_UNIX_H__ */ 49 | #endif -------------------------------------------------------------------------------- /enet/include/win32.h: -------------------------------------------------------------------------------- 1 | /** 2 | @file win32.h 3 | @brief ENet Win32 header 4 | */ 5 | #ifdef _WIN32 6 | #ifndef __ENET_WIN32_H__ 7 | #define __ENET_WIN32_H__ 8 | 9 | #ifdef _MSC_VER 10 | #ifdef ENET_BUILDING_LIB 11 | #pragma warning (disable: 4267) // size_t to int conversion 12 | #pragma warning (disable: 4244) // 64bit to 32bit int 13 | #pragma warning (disable: 4018) // signed/unsigned mismatch 14 | #pragma warning (disable: 4146) // unary minus operator applied to unsigned type 15 | #endif 16 | #endif 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | typedef SOCKET ENetSocket; 23 | 24 | #define ENET_SOCKET_NULL INVALID_SOCKET 25 | 26 | #define ENET_HOST_TO_NET_16(value) (htons (value)) 27 | #define ENET_HOST_TO_NET_32(value) (htonl (value)) 28 | 29 | #define ENET_NET_TO_HOST_16(value) (ntohs (value)) 30 | #define ENET_NET_TO_HOST_32(value) (ntohl (value)) 31 | 32 | typedef struct 33 | { 34 | size_t dataLength; 35 | void * data; 36 | } ENetBuffer; 37 | 38 | #define ENET_CALLBACK __cdecl 39 | 40 | #ifdef ENET_DLL 41 | #ifdef ENET_BUILDING_LIB 42 | #define ENET_API __declspec( dllexport ) 43 | #else 44 | #define ENET_API __declspec( dllimport ) 45 | #endif /* ENET_BUILDING_LIB */ 46 | #else /* !ENET_DLL */ 47 | #define ENET_API extern 48 | #endif /* ENET_DLL */ 49 | 50 | typedef fd_set ENetSocketSet; 51 | 52 | #define ENET_SOCKETSET_EMPTY(sockset) FD_ZERO (& (sockset)) 53 | #define ENET_SOCKETSET_ADD(sockset, socket) FD_SET (socket, & (sockset)) 54 | #define ENET_SOCKETSET_REMOVE(sockset, socket) FD_CLR (socket, & (sockset)) 55 | #define ENET_SOCKETSET_CHECK(sockset, socket) FD_ISSET (socket, & (sockset)) 56 | 57 | #endif /* __ENET_WIN32_H__ */ 58 | #endif -------------------------------------------------------------------------------- /enet/list.c: -------------------------------------------------------------------------------- 1 | /** 2 | @file list.c 3 | @brief ENet linked list functions 4 | */ 5 | #define ENET_BUILDING_LIB 1 6 | #include "include/enet.h" 7 | 8 | /** 9 | @defgroup list ENet linked list utility functions 10 | @ingroup private 11 | @{ 12 | */ 13 | void 14 | enet_list_clear (ENetList * list) 15 | { 16 | list -> sentinel.next = & list -> sentinel; 17 | list -> sentinel.previous = & list -> sentinel; 18 | } 19 | 20 | ENetListIterator 21 | enet_list_insert (ENetListIterator position, void * data) 22 | { 23 | ENetListIterator result = (ENetListIterator) data; 24 | 25 | result -> previous = position -> previous; 26 | result -> next = position; 27 | 28 | result -> previous -> next = result; 29 | position -> previous = result; 30 | 31 | return result; 32 | } 33 | 34 | void * 35 | enet_list_remove (ENetListIterator position) 36 | { 37 | position -> previous -> next = position -> next; 38 | position -> next -> previous = position -> previous; 39 | 40 | return position; 41 | } 42 | 43 | ENetListIterator 44 | enet_list_move (ENetListIterator position, void * dataFirst, void * dataLast) 45 | { 46 | ENetListIterator first = (ENetListIterator) dataFirst, 47 | last = (ENetListIterator) dataLast; 48 | 49 | first -> previous -> next = last -> next; 50 | last -> next -> previous = first -> previous; 51 | 52 | first -> previous = position -> previous; 53 | last -> next = position; 54 | 55 | first -> previous -> next = first; 56 | position -> previous = last; 57 | 58 | return first; 59 | } 60 | 61 | size_t 62 | enet_list_size (ENetList * list) 63 | { 64 | size_t size = 0; 65 | ENetListIterator position; 66 | 67 | for (position = enet_list_begin (list); 68 | position != enet_list_end (list); 69 | position = enet_list_next (position)) 70 | ++ size; 71 | 72 | return size; 73 | } 74 | 75 | /** @} */ 76 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Outdated! 2 | 3 | * Note: This is the outdated version of my CID-Creator script. To use the latest and more easy one just head to > https://github.com/CapciGithub/Growtopia-CID-Creator-v2 4 | 5 | ### How to use CID-Creator? 6 | 7 | * Visit https://console.cloud.google.com/ and click the emoji that in the middle of gift and question mark emoji on right corner. 8 | * After your cloud shell terminal ready paste this > git clone https://github.com/CapciGithub/Growtopia-CID-Creator and press enter. 9 | * Write cd Growtopia-CID-Creator/ and press enter again. 10 | * Now click the Open editor button on the right corner and wait for editor to load. 11 | * After editor loaded click the Growtopia-CID-Creator folder then click corefunc.h and edit Growid_acc = "sakgay31"; Password_acc = "loler1234@"; to whatever you want to create. 12 | * When your editing is done do CTRL + S or click File on the left corner and click Save 13 | * Now, click open terminal and wait for terminal to open. 14 | * After terminal loaded enter those commands: chmod +x build.sh and hit enter then ./build* and wait for it to load. ( Note: It will ask you this: Do you want to continue? [Y/n] write y into terminal and hit enter button ) 15 | * If you can see this message on your terminal: Tool succesfuly builded, you can start creating ID by typing ./createid then type ./createid but, If you can't see that message write ./build* again. 16 | * If you want to stop creating do CTRL+C and to download created accounts write this: cloudshell download acc.txt and click the download button. 17 | * Note: It just creates CID, doesn't skips the tutorial so they will be bot accounts. You can enter them If you want but, If you do you gotta finish the tutorial but If you use they as bots you can do what normal player does. 18 | * Note 2: If it stucks at connected to server/disconnected server after you created accounts probably you're got ip limited you gotta do this: https://media.discordapp.net/attachments/900416037781131305/901503881534664815/Ekran_Alnts.PNG?width=357&height=494 19 | 20 | 21 | ### How to use CID-Creator? ( Tutorial with Video ) 22 | 23 | * English Tutorials: https://streamable.com/k6nhvk 24 | * Indonesian Tutorials: https://www.youtube.com/watch?v=Ywwaw8NVZnw ( MOCH Hildan ) , https://www.youtube.com/watch?v=pB4DP0R9ClE ( Trust Tactic ) 25 | 26 | 27 | * Note: It just creates CID, doesn't skips the tutorial so they will be bot accounts. You can enter them If you want but, If you do you gotta finish the tutorial but If you use they as bots you can do what normal player does. 28 | * Note 2: If it stucks at connected to server/disconnected server after you created accounts probably you're got ip limited you gotta do this: https://media.discordapp.net/attachments/900416037781131305/901503881534664815/Ekran_Alnts.PNG?width=357&height=494 29 | -------------------------------------------------------------------------------- /packet.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | #pragma pack(push, 1) 7 | struct gameupdatepacket_t { 8 | uint8_t m_type; 9 | uint8_t m_netid; 10 | uint8_t m_jump_amount; 11 | uint8_t m_count; 12 | int32_t m_player_flags; 13 | int32_t m_item; 14 | int32_t m_packet_flags; 15 | float m_struct_flags; 16 | int32_t m_int_data; 17 | float m_vec_x; 18 | float m_vec_y; 19 | float m_vec2_x; 20 | float m_vec2_y; 21 | float m_particle_time; 22 | uint32_t m_state1; 23 | uint32_t m_state2; 24 | uint32_t m_data_size; 25 | uint32_t m_data; 26 | }; 27 | typedef struct gametankpacket_t { 28 | int32_t m_type; 29 | char m_data; 30 | } gametextpacket_t; 31 | #pragma pack(pop) 32 | enum { 33 | PACKET_STATE = 0, 34 | PACKET_CALL_FUNCTION, 35 | PACKET_UPDATE_STATUS, 36 | PACKET_TILE_CHANGE_REQUEST, 37 | PACKET_SEND_MAP_DATA, 38 | PACKET_SEND_TILE_UPDATE_DATA, 39 | PACKET_SEND_TILE_UPDATE_DATA_MULTIPLE, 40 | PACKET_TILE_ACTIVATE_REQUEST, 41 | PACKET_TILE_APPLY_DAMAGE, 42 | PACKET_SEND_INVENTORY_STATE, 43 | PACKET_ITEM_ACTIVATE_REQUEST, 44 | PACKET_ITEM_ACTIVATE_OBJECT_REQUEST, 45 | PACKET_SEND_TILE_TREE_STATE, 46 | PACKET_MODIFY_ITEM_INVENTORY, 47 | PACKET_ITEM_CHANGE_OBJECT, 48 | PACKET_SEND_LOCK, 49 | PACKET_SEND_ITEM_DATABASE_DATA, 50 | PACKET_SEND_PARTICLE_EFFECT, 51 | PACKET_SET_ICON_STATE, 52 | PACKET_ITEM_EFFECT, 53 | PACKET_SET_CHARACTER_STATE, 54 | PACKET_PING_REPLY, 55 | PACKET_PING_REQUEST, 56 | PACKET_GOT_PUNCHED, 57 | PACKET_APP_CHECK_RESPONSE, 58 | PACKET_APP_INTEGRITY_FAIL, 59 | PACKET_DISCONNECT, 60 | PACKET_BATTLE_JOIN, 61 | PACKET_BATTLE_EVEN, 62 | PACKET_USE_DOOR, 63 | PACKET_SEND_PARENTAL, 64 | PACKET_GONE_FISHIN, 65 | PACKET_STEAM, 66 | PACKET_PET_BATTLE, 67 | PACKET_NPC, 68 | PACKET_SPECIAL, 69 | PACKET_SEND_PARTICLE_EFFECT_V2, 70 | GAME_ACTIVE_ARROW_TO_ITEM, 71 | GAME_SELECT_TILE_INDEX 72 | }; 73 | enum { 74 | NET_MESSAGE_UNKNOWN = 0, 75 | NET_MESSAGE_SERVER_HELLO, 76 | NET_MESSAGE_GENERIC_TEXT, 77 | NET_MESSAGE_GAME_MESSAGE, 78 | NET_MESSAGE_GAME_PACKET, 79 | NET_MESSAGE_ERROR, 80 | NET_MESSAGE_TRACK, 81 | NET_MESSAGE_CLIENT_LOG_REQUEST, 82 | NET_MESSAGE_CLIENT_LOG_RESPONSE, 83 | }; 84 | vector explode(const string &delimiter, const string &str) 85 | { 86 | vector arr; 87 | int strleng = str.length(); 88 | int delleng = delimiter.length(); 89 | if (delleng == 0)return arr; 90 | int i = 0; 91 | int k = 0; 92 | while (i < strleng) 93 | { 94 | int j = 0; 95 | while (i + j < strleng && j < delleng && str[i + j] == delimiter[j]) 96 | j++; 97 | if (j == delleng)//found delimiter 98 | { 99 | arr.push_back(str.substr(k, i - k)); 100 | i += delleng; 101 | k = i; 102 | } 103 | else 104 | { 105 | i++; 106 | } 107 | } 108 | arr.push_back(str.substr(k, i - k)); 109 | return arr; 110 | } 111 | -------------------------------------------------------------------------------- /proton/vector.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | class vector2_t { 4 | public: 5 | float m_x; 6 | float m_y; 7 | vector2_t() : m_x(0), m_y(0) {} 8 | vector2_t(float x, float y) : m_x(x), m_y(y) {} 9 | bool operator==(vector2_t& rhs) { return m_x == rhs.m_x && m_y == rhs.m_y; } 10 | bool operator==(const vector2_t& rhs) const { return m_x == rhs.m_x && m_y == rhs.m_y; } 11 | vector2_t operator+(const vector2_t& rhs) { return vector2_t(m_x + rhs.m_x, m_y + rhs.m_y); } 12 | vector2_t operator-(const vector2_t& rhs) { return vector2_t(m_x - rhs.m_x, m_y - rhs.m_y); } 13 | float distance(float x, float y) { 14 | float value_x = this->m_x - x; 15 | float value_y = this->m_y - y; 16 | return sqrt(value_x * value_x + value_y * value_y); 17 | } 18 | }; 19 | 20 | class vector2i_t { 21 | public: 22 | int m_x; 23 | int m_y; 24 | vector2i_t() : m_x(0), m_y(0) {} 25 | vector2i_t(int x, int y) : m_x(x), m_y(y) {} 26 | vector2i_t(uint32_t x, uint32_t y) : m_x(x), m_y(y) {} 27 | bool operator==(vector2i_t& rhs) { return m_x == rhs.m_x && m_y == rhs.m_y; } 28 | bool operator!=(vector2i_t& rhs) { 29 | return m_x != rhs.m_x || m_y != rhs.m_y; 30 | } 31 | bool operator==(const vector2i_t& rhs) const { return m_x == rhs.m_x && m_y == rhs.m_y; } 32 | vector2i_t operator+(const vector2i_t& rhs) { return vector2i_t(m_x + rhs.m_x, m_y + rhs.m_y); } 33 | vector2i_t operator-(const vector2i_t& rhs) { return vector2i_t(m_x - rhs.m_x, m_y - rhs.m_y); } 34 | float distance(int x, int y) { 35 | float value_x = float(this->m_x) - x; 36 | float value_y = float(this->m_y) - y; 37 | return sqrt(value_x * value_x + value_y * value_y); 38 | } 39 | }; 40 | 41 | class vector3_t { 42 | public: 43 | float m_x; 44 | float m_y; 45 | float m_z; 46 | vector3_t() : m_x(0), m_y(0), m_z(0) {} 47 | vector3_t(float x, float y, float z) : m_x(x), m_y(y), m_z(z) {} 48 | bool operator==(vector3_t& rhs) { return m_x == rhs.m_x && m_y == rhs.m_y && m_z == rhs.m_z; } 49 | bool operator==(const vector3_t& rhs) const { return m_x == rhs.m_x && m_y == rhs.m_y && m_z == rhs.m_z; } 50 | vector3_t operator+(const vector3_t& rhs) { return vector3_t(m_x + rhs.m_x, m_y + rhs.m_y, m_z + rhs.m_z); } 51 | vector3_t operator-(const vector3_t& rhs) { return vector3_t(m_x - rhs.m_x, m_y - rhs.m_y, m_z - rhs.m_z); } 52 | }; 53 | 54 | class rect_t { 55 | public: 56 | float m_x; 57 | float m_y; 58 | float m_w; 59 | float m_h; 60 | rect_t() : m_x(0), m_y(0), m_w(0), m_h(0) {} 61 | rect_t(float x, float y, float w, float h) : m_x(x), m_y(y), m_w(w), m_h(h) {} 62 | bool operator==(rect_t& rhs) { return m_x == rhs.m_x && m_y == rhs.m_y && m_w == rhs.m_w && m_h == rhs.m_h; } 63 | bool operator==(const rect_t& rhs) const { return m_x == rhs.m_x && m_y == rhs.m_y && m_w == rhs.m_w && m_h == rhs.m_h; } 64 | rect_t operator+(const rect_t& rhs) { return rect_t(m_x + rhs.m_x, m_y + rhs.m_y, m_w + rhs.m_w, m_h + rhs.m_h); } 65 | rect_t operator-(const rect_t& rhs) { return rect_t(m_x - rhs.m_x, m_y - rhs.m_y, m_w - rhs.m_w, m_h - rhs.m_h); } 66 | }; 67 | -------------------------------------------------------------------------------- /enet/packet.c: -------------------------------------------------------------------------------- 1 | /** 2 | @file packet.c 3 | @brief ENet packet management functions 4 | */ 5 | #include 6 | #define ENET_BUILDING_LIB 1 7 | #include "include/enet.h" 8 | 9 | /** @defgroup Packet ENet packet functions 10 | @{ 11 | */ 12 | 13 | /** Creates a packet that may be sent to a peer. 14 | @param data initial contents of the packet's data; the packet's data will remain uninitialized if data is NULL. 15 | @param dataLength size of the data allocated for this packet 16 | @param flags flags for this packet as described for the ENetPacket structure. 17 | @returns the packet on success, NULL on failure 18 | */ 19 | ENetPacket * 20 | enet_packet_create (const void * data, size_t dataLength, enet_uint32 flags) 21 | { 22 | ENetPacket * packet = (ENetPacket *) enet_malloc (sizeof (ENetPacket)); 23 | if (packet == NULL) 24 | return NULL; 25 | 26 | if (flags & ENET_PACKET_FLAG_NO_ALLOCATE) 27 | packet -> data = (enet_uint8 *) data; 28 | else 29 | if (dataLength <= 0) 30 | packet -> data = NULL; 31 | else 32 | { 33 | packet -> data = (enet_uint8 *) enet_malloc (dataLength); 34 | if (packet -> data == NULL) 35 | { 36 | enet_free (packet); 37 | return NULL; 38 | } 39 | 40 | if (data != NULL) 41 | memcpy (packet -> data, data, dataLength); 42 | } 43 | 44 | packet -> referenceCount = 0; 45 | packet -> flags = flags; 46 | packet -> dataLength = dataLength; 47 | packet -> freeCallback = NULL; 48 | packet -> userData = NULL; 49 | 50 | return packet; 51 | } 52 | 53 | /** Destroys the packet and deallocates its data. 54 | @param packet packet to be destroyed 55 | */ 56 | void 57 | enet_packet_destroy (ENetPacket * packet) 58 | { 59 | if (packet == NULL) 60 | return; 61 | 62 | if (packet -> freeCallback != NULL) 63 | (* packet -> freeCallback) (packet); 64 | if (! (packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE) && 65 | packet -> data != NULL) 66 | enet_free (packet -> data); 67 | enet_free (packet); 68 | } 69 | 70 | /** Attempts to resize the data in the packet to length specified in the 71 | dataLength parameter 72 | @param packet packet to resize 73 | @param dataLength new size for the packet data 74 | @returns 0 on success, < 0 on failure 75 | */ 76 | int 77 | enet_packet_resize (ENetPacket * packet, size_t dataLength) 78 | { 79 | enet_uint8 * newData; 80 | 81 | if (dataLength <= packet -> dataLength || (packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE)) 82 | { 83 | packet -> dataLength = dataLength; 84 | 85 | return 0; 86 | } 87 | 88 | newData = (enet_uint8 *) enet_malloc (dataLength); 89 | if (newData == NULL) 90 | return -1; 91 | 92 | memcpy (newData, packet -> data, packet -> dataLength); 93 | enet_free (packet -> data); 94 | 95 | packet -> data = newData; 96 | packet -> dataLength = dataLength; 97 | 98 | return 0; 99 | } 100 | 101 | static int initializedCRC32 = 0; 102 | static enet_uint32 crcTable [256]; 103 | 104 | static enet_uint32 105 | reflect_crc (int val, int bits) 106 | { 107 | int result = 0, bit; 108 | 109 | for (bit = 0; bit < bits; bit ++) 110 | { 111 | if(val & 1) result |= 1 << (bits - 1 - bit); 112 | val >>= 1; 113 | } 114 | 115 | return result; 116 | } 117 | 118 | static void 119 | initialize_crc32 (void) 120 | { 121 | int byte; 122 | 123 | for (byte = 0; byte < 256; ++ byte) 124 | { 125 | enet_uint32 crc = reflect_crc (byte, 8) << 24; 126 | int offset; 127 | 128 | for(offset = 0; offset < 8; ++ offset) 129 | { 130 | if (crc & 0x80000000) 131 | crc = (crc << 1) ^ 0x04c11db7; 132 | else 133 | crc <<= 1; 134 | } 135 | 136 | crcTable [byte] = reflect_crc (crc, 32); 137 | } 138 | 139 | initializedCRC32 = 1; 140 | } 141 | 142 | enet_uint32 143 | enet_crc32 (const ENetBuffer * buffers, size_t bufferCount) 144 | { 145 | enet_uint32 crc = 0xFFFFFFFF; 146 | 147 | if (! initializedCRC32) initialize_crc32 (); 148 | 149 | while (bufferCount -- > 0) 150 | { 151 | const enet_uint8 * data = (const enet_uint8 *) buffers -> data, 152 | * dataEnd = & data [buffers -> dataLength]; 153 | 154 | while (data < dataEnd) 155 | { 156 | crc = (crc >> 8) ^ crcTable [(crc & 0xFF) ^ *data++]; 157 | } 158 | 159 | ++ buffers; 160 | } 161 | 162 | return ENET_HOST_TO_NET_32 (~ crc); 163 | } 164 | 165 | /** @} */ 166 | -------------------------------------------------------------------------------- /userfunc.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Growtopia Bot 4 | // Copyright (C) 2018 Growtopia Noobs 5 | // 6 | // This program is free software: you can redistribute it and/or modify 7 | // it under the terms of the GNU Affero General Public License as published 8 | // by the Free Software Foundation, either version 3 of the License, or 9 | // (at your option) any later version. 10 | // 11 | // This program is distributed in the hope that it will be useful, 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | // GNU Affero General Public License for more details. 15 | // 16 | // You should have received a copy of the GNU Affero General Public License 17 | // along with this program. If not, see . 18 | #include "json.hpp" 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include "utilsfunc.h" 30 | #include "corefunc.h" 31 | #include "enet/include/enet.h" 32 | //#define WORLD_GO 33 | using namespace std; 34 | char hexmap[] = { '0', '1', '2', '3', '4', '5', '6', '7', 35 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; 36 | 37 | std::string hexStr(unsigned char data) 38 | { 39 | std::string s(2, ' '); 40 | s[0] = hexmap[(data & 0xF0) >> 4]; 41 | s[1] = hexmap[data & 0x0F]; 42 | return s; 43 | } 44 | 45 | string generateMeta() 46 | { 47 | string x = to_string(rand() % 255) + "." + to_string(rand() % 255) + "." + to_string(rand() % 255) + "." + to_string(rand() % 255); 48 | return x; 49 | } 50 | 51 | string generateMac() 52 | { 53 | string x; 54 | for (int i = 0; i < 6; i++) 55 | { 56 | x += hexStr(rand()); 57 | if (i != 5) 58 | x += ":"; 59 | } 60 | return x; 61 | } 62 | 63 | string generateRid() 64 | { 65 | string x; 66 | for (int i = 0; i < 16; i++) 67 | { 68 | x += hexStr(rand()); 69 | } 70 | for (auto & c : x) c = toupper(c); 71 | return x; 72 | } 73 | 74 | string stripMessage(string msg) { 75 | regex e("\\x60[a-zA-Z0-9!@#$%^&*()_+\\-=\\[\\]\\{};':\"\\\\|,.<>\\/?]"); 76 | string result = regex_replace(msg, e, ""); 77 | result.erase(std::remove(result.begin(), result.end(), '`'), result.end()); 78 | return result; 79 | } 80 | 81 | void GrowtopiaBot::onLoginRequested() 82 | { 83 | string token; 84 | if (!login_user && !login_token) { 85 | token = ""; 86 | } else { 87 | token = "\nuser|" + std::to_string(login_user) + "\ntoken|" + std::to_string(login_token); 88 | } 89 | string ver = gameVersion; 90 | string hash = std::to_string((unsigned int)rand()); 91 | string hash2 = std::to_string((unsigned int)rand()); 92 | //string packet = "Logging on: " + uname + " Token: " + to_string(login_token) + " UserID: " + to_string(login_user) + "\n"; 93 | //if (login_token != 0 || login_token != -1) //cout << packet; 94 | SendPacket(2, "tankIDName|" + uname + "\ntankIDPass|" + upass + "\nrequestedName|SmellZero\nf|1\nprotocol|127\ngame_version|" + ver + "\nfz|5367464\nlmode|0\ncbits|0\nplayer_age|18\nGDPR|1\nhash2|" + hash2 + "\nmeta|" + generateMeta() + "\nfhash|-716928004\nrid|" + generateRid() + "\nplatformID|0\ndeviceVersion|0\ncountry|us\nhash|" + hash + "\nmac|" + generateMac() + "\nwk|" + generateRid() + "\nzf|-496303939" + token, peer); 95 | //currentWorld = ""; 96 | } 97 | void GrowtopiaBot::packet_type3(string text) 98 | { 99 | if (text.find("action|log\nmsg|") != std::string::npos) { 100 | string t = explode("msg|", text)[1]; 101 | cout << "Message: " << t << endl; 102 | } 103 | //dbgPrint("[" + uname + " ]Some text is here: " + text); 104 | if (text.find("LOGON ATTEMPTS") != string::npos) 105 | { 106 | //cout << "Wrong username / password!. (LOGON ATTEMPTS)"; 107 | } 108 | if (text.find("password is wrong") != string::npos) 109 | { 110 | //cout << "Wrong password!"; 111 | } 112 | //if (text.find("action|log\n") != std::string::npos) cout << uname << " " << text << endl; 113 | if (text.find("action|logon_fail") != string::npos) 114 | { 115 | connectClient(); 116 | objects.clear(); 117 | } 118 | } 119 | 120 | void GrowtopiaBot::packet_type6(string text) 121 | { 122 | //dbgPrint("Some text is here: " + text); 123 | SendPacket(2, "action|enter_game\n", peer); 124 | enet_host_flush(client); 125 | } 126 | 127 | void GrowtopiaBot::packet_unknown(ENetPacket* packet) 128 | { 129 | //dbgPrint("Got unknown packet type: " + std::to_string(GetMessageTypeFromPacket(packet))); 130 | //dbgPrint("Packet size is " + std::to_string(packet->dataLength)); 131 | } 132 | void GrowtopiaBot::WhenConnected() 133 | { 134 | cout << uname << " Connected to server, creating account!" << endl; 135 | } 136 | 137 | void GrowtopiaBot::WhenDisconnected() 138 | { 139 | cout << uname << " Disconnected from server..." << endl; 140 | connectClient(); 141 | } 142 | 143 | void GrowtopiaBot::userInit() { 144 | connectClient(); 145 | //cout << flush; 146 | } 147 | -------------------------------------------------------------------------------- /utilsfunc.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | //#include 5 | #include "corefunc.h" 6 | #include "json.hpp" 7 | using namespace std; 8 | 9 | namespace patch 10 | { 11 | template < typename T > std::string to_string(const T& n) 12 | { 13 | std::ostringstream stm; 14 | stm << n; 15 | return stm.str(); 16 | } 17 | } 18 | 19 | int GetApp() 20 | { 21 | return 0xA2Cu; 22 | } 23 | 24 | unsigned int HashString(const char *str, int len) 25 | { 26 | if (!str) return 0; 27 | 28 | unsigned char *n = (unsigned char *)str; 29 | unsigned int acc = 0x55555555; 30 | 31 | if (len == 0) 32 | { 33 | while (*n) 34 | acc = (acc >> 27) + (acc << 5) + *n++; 35 | } 36 | else 37 | { 38 | for (int i = 0; i < len; i++) 39 | { 40 | acc = (acc >> 27) + (acc << 5) + *n++; 41 | } 42 | } 43 | return acc; 44 | } 45 | 46 | int GetDeviceHash(string deviceId) 47 | { 48 | string v8 = deviceId; 49 | int v3; 50 | v8 += patch::to_string(GetApp() + 2280); 51 | v8 += "RT"; 52 | v3 = HashString(v8.c_str(), 0); 53 | return v3; 54 | } 55 | 56 | 57 | int GetDeviceSecondaryHash(string macAddr) 58 | { 59 | int v1; 60 | string v3 = ""; 61 | for (std::string::size_type i = 0; i < macAddr.length(); ++i) 62 | v3 += tolower(macAddr[i]); 63 | v3 += "RT"; 64 | v1 = HashString(v3.c_str(), 0); 65 | return v1; 66 | } 67 | 68 | string createLoginData(string swearFilter, string protocol, string gameVer, string fz, string lmode, string 69 | 70 | cbits, string hash2, string meta, string fhash, string rid, string platformID, string deviceVersion, string 71 | 72 | country, string hash, string mac, string wk) 73 | { 74 | string loginPacket; 75 | loginPacket = "f|"; 76 | loginPacket += swearFilter; 77 | loginPacket += "\n"; 78 | loginPacket += "protocol|"; 79 | loginPacket += protocol; 80 | loginPacket += "\n"; 81 | loginPacket += "game_version|"; 82 | loginPacket += gameVer; 83 | loginPacket += "\n"; 84 | loginPacket += "fz|"; 85 | loginPacket += fz; 86 | loginPacket += "\n"; 87 | loginPacket += "lmode|"; 88 | loginPacket += lmode; 89 | loginPacket += "\n"; 90 | loginPacket += "cbits|"; 91 | loginPacket += cbits; 92 | loginPacket += "\n"; 93 | loginPacket += "hash2|"; 94 | loginPacket += hash2; 95 | loginPacket += "\n"; 96 | loginPacket += "meta|"; 97 | loginPacket += meta; 98 | loginPacket += "\n"; 99 | loginPacket += "fhash|"; 100 | loginPacket += fhash; 101 | loginPacket += "\n"; 102 | loginPacket += "rid|"; 103 | loginPacket += rid; 104 | loginPacket += "\n"; 105 | loginPacket += "platformID|"; 106 | loginPacket += platformID; 107 | loginPacket += "\n"; 108 | loginPacket += "deviceVersion|"; 109 | loginPacket += deviceVersion; 110 | loginPacket += "\n"; 111 | loginPacket += "country|"; 112 | loginPacket += country; 113 | loginPacket += "\n"; 114 | loginPacket += "hash|"; 115 | loginPacket += hash; 116 | loginPacket += "\n"; 117 | loginPacket += "mac|"; 118 | loginPacket += mac; 119 | loginPacket += "\n"; 120 | loginPacket += "wk|"; 121 | loginPacket += wk; 122 | loginPacket += "\n"; 123 | return loginPacket; 124 | } 125 | 126 | /*void SendPacketRaw(int a1, void *a2, size_t a3, void *a4, ENetPeer* a5, int a6) 127 | { 128 | ENetPacket *v6; 129 | ENetPacket *v7; 130 | 131 | if (a5) 132 | { 133 | if (a1 == 4 && *((BYTE *)a2 + 12) & 8) 134 | { 135 | v7 = enet_packet_create(0, a3 + *((DWORD *)a2 + 13) + 5, a6); 136 | *(DWORD *)v7->data = 4; 137 | memcpy((char *)v7->data + 4, a2, a3); 138 | memcpy((char *)v7->data + a3 + 4, a4, *((DWORD *)a2 + 13)); 139 | enet_peer_send(a5, 0, v7); 140 | } 141 | else 142 | { 143 | v6 = enet_packet_create(0, a3 + 5, a6); 144 | *(DWORD *)v6->data = a1; 145 | memcpy((char *)v6->data + 4, a2, a3); 146 | enet_peer_send(a5, 0, v6); 147 | } 148 | } 149 | }*/ 150 | 151 | 152 | char* CreateGameUpdatePacketWithExtraDataAtEnd(int a1) 153 | { 154 | char* result = new char[a1 + 56]; 155 | 156 | *(DWORD *)result = 0; 157 | *(DWORD *)(result + 4) = 0; 158 | *(DWORD *)(result + 8) = 0; 159 | *(DWORD *)(result + 12) = 8; 160 | *(DWORD *)(result + 16) = 0; 161 | *(DWORD *)(result + 20) = 0; 162 | *(DWORD *)(result + 24) = 0; 163 | *(DWORD *)(result + 28) = 0; 164 | *(DWORD *)(result + 32) = 0; 165 | *(DWORD *)(result + 36) = 0; 166 | *(DWORD *)(result + 40) = 0; 167 | *(DWORD *)(result + 44) = 0; 168 | *(DWORD *)(result + 48) = 0; 169 | *(DWORD *)(result + 52) = a1; 170 | return result; 171 | } 172 | 173 | 174 | 175 | std::string colorstr(string str) 176 | { 177 | string chrs = "0123456789bwpo^$#@!qertas"; 178 | string s; 179 | for (int i = 0; i < str.length(); i++) 180 | { 181 | s += "`"; 182 | char* x; 183 | x = (char*)malloc(2); 184 | x[0] = chrs[rand() % chrs.length()]; 185 | x[1] = 0; 186 | string y = x; 187 | s += y; 188 | free(x); 189 | s += str[i]; 190 | } 191 | return s; 192 | } 193 | 194 | std::string colorstr2(string str) 195 | { 196 | string chrs = "0123456789bwpo^$#@!qertas"; 197 | char* x; 198 | x = (char*)malloc(2); 199 | x[0] = chrs[rand() % chrs.length()]; 200 | x[1] = 0; 201 | string y = x; 202 | free(x); 203 | return "`" + y + str; 204 | } 205 | 206 | 207 | /*string text_encode(char* text) 208 | { 209 | string ret = ""; 210 | while (text[0] != 0) 211 | { 212 | switch (text[0]) 213 | { 214 | case '\n': 215 | ret += "\\n"; 216 | break; 217 | case '\t': 218 | ret += "\\t"; 219 | break; 220 | case '\b': 221 | ret += "\\b"; 222 | break; 223 | case '\\': 224 | ret += "\\\\"; 225 | break; 226 | case '\r': 227 | ret += "\\r"; 228 | break; 229 | default: 230 | ret += text[0]; 231 | break; 232 | } 233 | text++; 234 | } 235 | return ret; 236 | }*/ 237 | -------------------------------------------------------------------------------- /proton/rtparam.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include "../utils.h" 6 | #include 7 | 8 | class rtvar { 9 | public: 10 | class pair { 11 | public: 12 | std::string m_key{}; 13 | std::vector m_values{}; 14 | std::string m_value; 15 | pair() { 16 | } 17 | pair(std::string key, std::initializer_list values) : m_key(key), m_values(values) { 18 | } 19 | bool operator==( const rtvar::pair& rst) { 20 | return m_key == rst.m_key && m_values[0] == rst.m_values[0]; 21 | } 22 | static pair parse(std::string str) { 23 | pair ret{}; 24 | if (!str.length()) { // we dont want to parse empty string so m_pairs vector wont make the app crash 25 | ret.m_value.append("[EMPTY]"); 26 | ret.m_values.push_back("[EMPTY]"); 27 | return ret; 28 | } 29 | std::string token; 30 | std::stringstream ss(str); 31 | bool key = true, first = true; 32 | while (std::getline(ss, token, '|')) { 33 | if (key) { 34 | ret.m_key = token; 35 | key = false; 36 | } else { 37 | if (!first) 38 | ret.m_value.append("|"); 39 | ret.m_value.append(token); 40 | ret.m_values.push_back(token); 41 | first = false; 42 | } 43 | } 44 | return ret; 45 | } 46 | std::string serialize() { 47 | std::string ret{}; 48 | ret.append(m_key); 49 | for (auto& val : m_values) { 50 | ret.append("|"); 51 | ret.append(val); 52 | } 53 | return ret; 54 | } 55 | }; 56 | rtvar() { 57 | } 58 | rtvar(std::initializer_list pairs) : m_pairs(pairs) { 59 | } 60 | static rtvar parse(std::string str) { 61 | rtvar ret{}; 62 | std::stringstream ss(str); 63 | std::string token{}; 64 | while (std::getline(ss, token, '\n')) 65 | ret.append(token); 66 | return ret; 67 | } 68 | pair& append(std::string str) { 69 | pair p = pair::parse(str); 70 | m_pairs.push_back(p); 71 | return m_pairs.back(); 72 | } 73 | pair& get(int i) { 74 | if ((unsigned)i >= m_pairs.size()) 75 | return m_pairs[0]; 76 | return m_pairs[i]; 77 | } 78 | bool valid() { 79 | if (m_pairs.size() < 1) 80 | return false; 81 | 82 | if (m_pairs[0].m_values.size() < 1) 83 | return false; 84 | 85 | return true; 86 | } 87 | pair* find(const std::string& key) { 88 | int inx = 0; 89 | for (auto pair : m_pairs) { 90 | if (pair.m_key == key) //we cant return local pairs addr 91 | return &m_pairs[inx]; 92 | inx++; 93 | } 94 | return nullptr; 95 | } 96 | 97 | std::string get(const std::string& key) { 98 | auto pair = find(key); 99 | if (pair) 100 | return pair->m_value; 101 | return ""; 102 | } 103 | void set(const std::string& key, std::string value) { 104 | auto pair = find(key); 105 | if (pair && pair->m_values.size() >= 1) 106 | pair->m_values[0] = value; 107 | } 108 | std::string serialize() { 109 | std::string ret{}; 110 | for (auto& val : m_pairs) { 111 | ret.append(val.serialize()); 112 | ret.append("\n"); 113 | } 114 | if (ret != "") 115 | ret.erase(ret.end()); 116 | return ret; 117 | } 118 | bool validate_ints(std::vector vals) { 119 | for (auto str : vals) { 120 | auto pair = this->find(str); 121 | if (!pair) 122 | return false; 123 | if (!utils::is_number(pair->m_value)) 124 | return false; 125 | } 126 | return true; 127 | } 128 | bool validate_int(std::string str) { 129 | auto pair = this->find(str); 130 | if (!pair) 131 | return false; 132 | if (!utils::is_number(pair->m_value)) 133 | return false; 134 | return true; 135 | } 136 | inline int get_int(const std::string& key) { //this does not chekc if it exists, it assumes validate_ints has been consulated beforehand 137 | return atoi(find(key)->m_value.c_str()); 138 | } 139 | inline long long get_long(const std::string& key) { //assumes validate_ints 140 | return atoll(find(key)->m_value.c_str()); 141 | } 142 | size_t size() const { 143 | return m_pairs.size(); 144 | } 145 | void remove(const std::string& key) { 146 | auto pair = find(key); 147 | if (pair) { 148 | auto& ref = *pair; 149 | m_pairs.erase(std::remove(m_pairs.begin(), m_pairs.end(), ref), m_pairs.end()); 150 | } 151 | } 152 | 153 | private: 154 | //i could use std::map but for the sake of simplicity i dont, i want the code to be as readable as possible 155 | std::vector m_pairs{}; 156 | }; 157 | class rtvar_opt { //optimized version of rtvars (really nothing more than a container) when only needing to append strings 158 | private: 159 | std::string m_var{}; 160 | 161 | public: 162 | rtvar_opt() { 163 | } 164 | rtvar_opt(std::string start) { 165 | m_var = start; 166 | } 167 | void append(std::string str) { 168 | m_var = m_var.append("\n" + str); 169 | } 170 | std::string get() { 171 | return m_var; 172 | } 173 | }; 174 | -------------------------------------------------------------------------------- /enet/include/protocol.h: -------------------------------------------------------------------------------- 1 | /** 2 | @file protocol.h 3 | @brief ENet protocol 4 | */ 5 | #ifndef __ENET_PROTOCOL_H__ 6 | #define __ENET_PROTOCOL_H__ 7 | 8 | #include "types.h" 9 | 10 | enum 11 | { 12 | ENET_PROTOCOL_MINIMUM_MTU = 576, 13 | ENET_PROTOCOL_MAXIMUM_MTU = 4096, 14 | ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS = 32, 15 | ENET_PROTOCOL_MINIMUM_WINDOW_SIZE = 4096, 16 | ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE = 65536, 17 | ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT = 1, 18 | ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT = 255, 19 | ENET_PROTOCOL_MAXIMUM_PEER_ID = 0xFFF, 20 | ENET_PROTOCOL_MAXIMUM_FRAGMENT_COUNT = 1024 * 1024 21 | }; 22 | 23 | typedef enum _ENetProtocolCommand 24 | { 25 | ENET_PROTOCOL_COMMAND_NONE = 0, 26 | ENET_PROTOCOL_COMMAND_ACKNOWLEDGE = 1, 27 | ENET_PROTOCOL_COMMAND_CONNECT = 2, 28 | ENET_PROTOCOL_COMMAND_VERIFY_CONNECT = 3, 29 | ENET_PROTOCOL_COMMAND_DISCONNECT = 4, 30 | ENET_PROTOCOL_COMMAND_PING = 5, 31 | ENET_PROTOCOL_COMMAND_SEND_RELIABLE = 6, 32 | ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE = 7, 33 | ENET_PROTOCOL_COMMAND_SEND_FRAGMENT = 8, 34 | ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED = 9, 35 | ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT = 10, 36 | ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE = 11, 37 | ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE_FRAGMENT = 12, 38 | ENET_PROTOCOL_COMMAND_COUNT = 13, 39 | 40 | ENET_PROTOCOL_COMMAND_MASK = 0x0F 41 | } ENetProtocolCommand; 42 | 43 | typedef enum _ENetProtocolFlag 44 | { 45 | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE = (1 << 7), 46 | ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED = (1 << 6), 47 | 48 | ENET_PROTOCOL_HEADER_FLAG_COMPRESSED = (1 << 14), 49 | ENET_PROTOCOL_HEADER_FLAG_SENT_TIME = (1 << 15), 50 | ENET_PROTOCOL_HEADER_FLAG_MASK = ENET_PROTOCOL_HEADER_FLAG_COMPRESSED | ENET_PROTOCOL_HEADER_FLAG_SENT_TIME, 51 | 52 | ENET_PROTOCOL_HEADER_SESSION_MASK = (3 << 12), 53 | ENET_PROTOCOL_HEADER_SESSION_SHIFT = 12 54 | } ENetProtocolFlag; 55 | 56 | #ifdef _MSC_VER 57 | #pragma pack(push, 1) 58 | #define ENET_PACKED 59 | #elif defined(__GNUC__) || defined(__clang__) 60 | #define ENET_PACKED __attribute__ ((packed)) 61 | #else 62 | #define ENET_PACKED 63 | #endif 64 | 65 | typedef struct _ENetProtocolHeader 66 | { 67 | enet_uint16 peerID; 68 | enet_uint16 sentTime; 69 | } ENET_PACKED ENetProtocolHeader; 70 | 71 | typedef struct _ENetProtocolHeaderUbisoft { 72 | enet_uint16 integrity[3]; 73 | enet_uint16 peerID; 74 | enet_uint16 sentTime; 75 | } ENET_PACKED ENetProtocolHeaderUbisoft; 76 | 77 | typedef struct _ENetProtocolCommandHeader 78 | { 79 | enet_uint8 command; 80 | enet_uint8 channelID; 81 | enet_uint16 reliableSequenceNumber; 82 | } ENET_PACKED ENetProtocolCommandHeader; 83 | 84 | typedef struct _ENetProtocolAcknowledge 85 | { 86 | ENetProtocolCommandHeader header; 87 | enet_uint16 receivedReliableSequenceNumber; 88 | enet_uint16 receivedSentTime; 89 | } ENET_PACKED ENetProtocolAcknowledge; 90 | 91 | typedef struct _ENetProtocolConnect 92 | { 93 | ENetProtocolCommandHeader header; 94 | enet_uint16 outgoingPeerID; 95 | enet_uint8 incomingSessionID; 96 | enet_uint8 outgoingSessionID; 97 | enet_uint32 mtu; 98 | enet_uint32 windowSize; 99 | enet_uint32 channelCount; 100 | enet_uint32 incomingBandwidth; 101 | enet_uint32 outgoingBandwidth; 102 | enet_uint32 packetThrottleInterval; 103 | enet_uint32 packetThrottleAcceleration; 104 | enet_uint32 packetThrottleDeceleration; 105 | enet_uint32 connectID; 106 | enet_uint32 data; 107 | } ENET_PACKED ENetProtocolConnect; 108 | 109 | typedef struct _ENetProtocolVerifyConnect 110 | { 111 | ENetProtocolCommandHeader header; 112 | enet_uint16 outgoingPeerID; 113 | enet_uint8 incomingSessionID; 114 | enet_uint8 outgoingSessionID; 115 | enet_uint32 mtu; 116 | enet_uint32 windowSize; 117 | enet_uint32 channelCount; 118 | enet_uint32 incomingBandwidth; 119 | enet_uint32 outgoingBandwidth; 120 | enet_uint32 packetThrottleInterval; 121 | enet_uint32 packetThrottleAcceleration; 122 | enet_uint32 packetThrottleDeceleration; 123 | enet_uint32 connectID; 124 | } ENET_PACKED ENetProtocolVerifyConnect; 125 | 126 | typedef struct _ENetProtocolBandwidthLimit 127 | { 128 | ENetProtocolCommandHeader header; 129 | enet_uint32 incomingBandwidth; 130 | enet_uint32 outgoingBandwidth; 131 | } ENET_PACKED ENetProtocolBandwidthLimit; 132 | 133 | typedef struct _ENetProtocolThrottleConfigure 134 | { 135 | ENetProtocolCommandHeader header; 136 | enet_uint32 packetThrottleInterval; 137 | enet_uint32 packetThrottleAcceleration; 138 | enet_uint32 packetThrottleDeceleration; 139 | } ENET_PACKED ENetProtocolThrottleConfigure; 140 | 141 | typedef struct _ENetProtocolDisconnect 142 | { 143 | ENetProtocolCommandHeader header; 144 | enet_uint32 data; 145 | } ENET_PACKED ENetProtocolDisconnect; 146 | 147 | typedef struct _ENetProtocolPing 148 | { 149 | ENetProtocolCommandHeader header; 150 | } ENET_PACKED ENetProtocolPing; 151 | 152 | typedef struct _ENetProtocolSendReliable 153 | { 154 | ENetProtocolCommandHeader header; 155 | enet_uint16 dataLength; 156 | } ENET_PACKED ENetProtocolSendReliable; 157 | 158 | typedef struct _ENetProtocolSendUnreliable 159 | { 160 | ENetProtocolCommandHeader header; 161 | enet_uint16 unreliableSequenceNumber; 162 | enet_uint16 dataLength; 163 | } ENET_PACKED ENetProtocolSendUnreliable; 164 | 165 | typedef struct _ENetProtocolSendUnsequenced 166 | { 167 | ENetProtocolCommandHeader header; 168 | enet_uint16 unsequencedGroup; 169 | enet_uint16 dataLength; 170 | } ENET_PACKED ENetProtocolSendUnsequenced; 171 | 172 | typedef struct _ENetProtocolSendFragment 173 | { 174 | ENetProtocolCommandHeader header; 175 | enet_uint16 startSequenceNumber; 176 | enet_uint16 dataLength; 177 | enet_uint32 fragmentCount; 178 | enet_uint32 fragmentNumber; 179 | enet_uint32 totalLength; 180 | enet_uint32 fragmentOffset; 181 | } ENET_PACKED ENetProtocolSendFragment; 182 | 183 | typedef union _ENetProtocol 184 | { 185 | ENetProtocolCommandHeader header; 186 | ENetProtocolAcknowledge acknowledge; 187 | ENetProtocolConnect connect; 188 | ENetProtocolVerifyConnect verifyConnect; 189 | ENetProtocolDisconnect disconnect; 190 | ENetProtocolPing ping; 191 | ENetProtocolSendReliable sendReliable; 192 | ENetProtocolSendUnreliable sendUnreliable; 193 | ENetProtocolSendUnsequenced sendUnsequenced; 194 | ENetProtocolSendFragment sendFragment; 195 | ENetProtocolBandwidthLimit bandwidthLimit; 196 | ENetProtocolThrottleConfigure throttleConfigure; 197 | } ENET_PACKED ENetProtocol; 198 | 199 | #ifdef _MSC_VER 200 | #pragma pack(pop) 201 | #endif 202 | 203 | #endif /* __ENET_PROTOCOL_H__ */ 204 | 205 | -------------------------------------------------------------------------------- /enet/win32.c: -------------------------------------------------------------------------------- 1 | /** 2 | @file win32.c 3 | @brief ENet Win32 system specific functions 4 | */ 5 | #ifdef _WIN32 6 | 7 | #define ENET_BUILDING_LIB 1 8 | #include "include/enet.h" 9 | #include 10 | #include 11 | 12 | #pragma warning(disable : 4996) 13 | 14 | static enet_uint32 timeBase = 0; 15 | 16 | int 17 | enet_initialize (void) 18 | { 19 | WORD versionRequested = MAKEWORD (1, 1); 20 | WSADATA wsaData; 21 | 22 | if (WSAStartup (versionRequested, & wsaData)) 23 | return -1; 24 | 25 | if (LOBYTE (wsaData.wVersion) != 1|| 26 | HIBYTE (wsaData.wVersion) != 1) 27 | { 28 | WSACleanup (); 29 | 30 | return -1; 31 | } 32 | 33 | timeBeginPeriod (1); 34 | 35 | return 0; 36 | } 37 | 38 | void 39 | enet_deinitialize (void) 40 | { 41 | timeEndPeriod (1); 42 | 43 | WSACleanup (); 44 | } 45 | 46 | enet_uint32 47 | enet_host_random_seed (void) 48 | { 49 | return (enet_uint32) timeGetTime (); 50 | } 51 | 52 | enet_uint32 53 | enet_time_get (void) 54 | { 55 | return (enet_uint32) timeGetTime () - timeBase; 56 | } 57 | 58 | void 59 | enet_time_set (enet_uint32 newTimeBase) 60 | { 61 | timeBase = (enet_uint32) timeGetTime () - newTimeBase; 62 | } 63 | 64 | int 65 | enet_address_set_host_ip (ENetAddress * address, const char * name) 66 | { 67 | enet_uint8 vals [4] = { 0, 0, 0, 0 }; 68 | int i; 69 | 70 | for (i = 0; i < 4; ++ i) 71 | { 72 | const char * next = name + 1; 73 | if (* name != '0') 74 | { 75 | long val = strtol (name, (char **) & next, 10); 76 | if (val < 0 || val > 255 || next == name || next - name > 3) 77 | return -1; 78 | vals [i] = (enet_uint8) val; 79 | } 80 | 81 | if (* next != (i < 3 ? '.' : '\0')) 82 | return -1; 83 | name = next + 1; 84 | } 85 | 86 | memcpy (& address -> host, vals, sizeof (enet_uint32)); 87 | return 0; 88 | } 89 | 90 | int 91 | enet_address_set_host (ENetAddress * address, const char * name) 92 | { 93 | struct hostent * hostEntry; 94 | 95 | hostEntry = gethostbyname (name); 96 | if (hostEntry == NULL || 97 | hostEntry -> h_addrtype != AF_INET) 98 | return enet_address_set_host_ip (address, name); 99 | 100 | address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0]; 101 | 102 | return 0; 103 | } 104 | 105 | int 106 | enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) 107 | { 108 | char * addr = inet_ntoa (* (struct in_addr *) & address -> host); 109 | if (addr == NULL) 110 | return -1; 111 | else 112 | { 113 | size_t addrLen = strlen(addr); 114 | if (addrLen >= nameLength) 115 | return -1; 116 | memcpy (name, addr, addrLen + 1); 117 | } 118 | return 0; 119 | } 120 | 121 | int 122 | enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) 123 | { 124 | struct in_addr in; 125 | struct hostent * hostEntry; 126 | 127 | in.s_addr = address -> host; 128 | 129 | hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET); 130 | if (hostEntry == NULL) 131 | return enet_address_get_host_ip (address, name, nameLength); 132 | else 133 | { 134 | size_t hostLen = strlen (hostEntry -> h_name); 135 | if (hostLen >= nameLength) 136 | return -1; 137 | memcpy (name, hostEntry -> h_name, hostLen + 1); 138 | } 139 | 140 | return 0; 141 | } 142 | 143 | int 144 | enet_socket_bind (ENetSocket socket, const ENetAddress * address) 145 | { 146 | struct sockaddr_in sin; 147 | 148 | memset (& sin, 0, sizeof (struct sockaddr_in)); 149 | 150 | sin.sin_family = AF_INET; 151 | 152 | if (address != NULL) 153 | { 154 | sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); 155 | sin.sin_addr.s_addr = address -> host; 156 | } 157 | else 158 | { 159 | sin.sin_port = 0; 160 | sin.sin_addr.s_addr = INADDR_ANY; 161 | } 162 | 163 | return bind (socket, 164 | (struct sockaddr *) & sin, 165 | sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0; 166 | } 167 | 168 | int 169 | enet_socket_get_address (ENetSocket socket, ENetAddress * address) 170 | { 171 | struct sockaddr_in sin; 172 | int sinLength = sizeof (struct sockaddr_in); 173 | 174 | if (getsockname (socket, (struct sockaddr *) & sin, & sinLength) == -1) 175 | return -1; 176 | 177 | address -> host = (enet_uint32) sin.sin_addr.s_addr; 178 | address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); 179 | 180 | return 0; 181 | } 182 | 183 | int 184 | enet_socket_listen (ENetSocket socket, int backlog) 185 | { 186 | return listen (socket, backlog < 0 ? SOMAXCONN : backlog) == SOCKET_ERROR ? -1 : 0; 187 | } 188 | 189 | ENetSocket 190 | enet_socket_create (ENetSocketType type) 191 | { 192 | return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); 193 | } 194 | 195 | int 196 | enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) 197 | { 198 | int result = SOCKET_ERROR; 199 | switch (option) 200 | { 201 | case ENET_SOCKOPT_NONBLOCK: 202 | { 203 | u_long nonBlocking = (u_long) value; 204 | result = ioctlsocket (socket, FIONBIO, & nonBlocking); 205 | break; 206 | } 207 | 208 | case ENET_SOCKOPT_BROADCAST: 209 | result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int)); 210 | break; 211 | 212 | case ENET_SOCKOPT_REUSEADDR: 213 | result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int)); 214 | break; 215 | 216 | case ENET_SOCKOPT_RCVBUF: 217 | result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int)); 218 | break; 219 | 220 | case ENET_SOCKOPT_SNDBUF: 221 | result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int)); 222 | break; 223 | 224 | case ENET_SOCKOPT_RCVTIMEO: 225 | result = setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & value, sizeof (int)); 226 | break; 227 | 228 | case ENET_SOCKOPT_SNDTIMEO: 229 | result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & value, sizeof (int)); 230 | break; 231 | 232 | case ENET_SOCKOPT_NODELAY: 233 | result = setsockopt (socket, IPPROTO_TCP, TCP_NODELAY, (char *) & value, sizeof (int)); 234 | break; 235 | 236 | default: 237 | break; 238 | } 239 | return result == SOCKET_ERROR ? -1 : 0; 240 | } 241 | 242 | int 243 | enet_socket_get_option (ENetSocket socket, ENetSocketOption option, int * value) 244 | { 245 | int result = SOCKET_ERROR, len; 246 | switch (option) 247 | { 248 | case ENET_SOCKOPT_ERROR: 249 | len = sizeof(int); 250 | result = getsockopt (socket, SOL_SOCKET, SO_ERROR, (char *) value, & len); 251 | break; 252 | 253 | default: 254 | break; 255 | } 256 | return result == SOCKET_ERROR ? -1 : 0; 257 | } 258 | 259 | int 260 | enet_socket_connect (ENetSocket socket, const ENetAddress * address) 261 | { 262 | struct sockaddr_in sin; 263 | int result; 264 | 265 | memset (& sin, 0, sizeof (struct sockaddr_in)); 266 | 267 | sin.sin_family = AF_INET; 268 | sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); 269 | sin.sin_addr.s_addr = address -> host; 270 | 271 | result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in)); 272 | if (result == SOCKET_ERROR && WSAGetLastError () != WSAEWOULDBLOCK) 273 | return -1; 274 | 275 | return 0; 276 | } 277 | 278 | ENetSocket 279 | enet_socket_accept (ENetSocket socket, ENetAddress * address) 280 | { 281 | SOCKET result; 282 | struct sockaddr_in sin; 283 | int sinLength = sizeof (struct sockaddr_in); 284 | 285 | result = accept (socket, 286 | address != NULL ? (struct sockaddr *) & sin : NULL, 287 | address != NULL ? & sinLength : NULL); 288 | 289 | if (result == INVALID_SOCKET) 290 | return ENET_SOCKET_NULL; 291 | 292 | if (address != NULL) 293 | { 294 | address -> host = (enet_uint32) sin.sin_addr.s_addr; 295 | address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); 296 | } 297 | 298 | return result; 299 | } 300 | 301 | int 302 | enet_socket_shutdown (ENetSocket socket, ENetSocketShutdown how) 303 | { 304 | return shutdown (socket, (int) how) == SOCKET_ERROR ? -1 : 0; 305 | } 306 | 307 | void 308 | enet_socket_destroy (ENetSocket socket) 309 | { 310 | if (socket != INVALID_SOCKET) 311 | closesocket (socket); 312 | } 313 | 314 | int 315 | enet_socket_send (ENetSocket socket, 316 | const ENetAddress * address, 317 | const ENetBuffer * buffers, 318 | size_t bufferCount) 319 | { 320 | struct sockaddr_in sin; 321 | DWORD sentLength; 322 | 323 | if (address != NULL) 324 | { 325 | memset (& sin, 0, sizeof (struct sockaddr_in)); 326 | 327 | sin.sin_family = AF_INET; 328 | sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); 329 | sin.sin_addr.s_addr = address -> host; 330 | } 331 | 332 | if (WSASendTo (socket, 333 | (LPWSABUF) buffers, 334 | (DWORD) bufferCount, 335 | & sentLength, 336 | 0, 337 | address != NULL ? (struct sockaddr *) & sin : NULL, 338 | address != NULL ? sizeof (struct sockaddr_in) : 0, 339 | NULL, 340 | NULL) == SOCKET_ERROR) 341 | { 342 | if (WSAGetLastError () == WSAEWOULDBLOCK) 343 | return 0; 344 | 345 | return -1; 346 | } 347 | 348 | return (int) sentLength; 349 | } 350 | 351 | int 352 | enet_socket_receive (ENetSocket socket, 353 | ENetAddress * address, 354 | ENetBuffer * buffers, 355 | size_t bufferCount) 356 | { 357 | INT sinLength = sizeof (struct sockaddr_in); 358 | DWORD flags = 0, 359 | recvLength; 360 | struct sockaddr_in sin; 361 | 362 | if (WSARecvFrom (socket, 363 | (LPWSABUF) buffers, 364 | (DWORD) bufferCount, 365 | & recvLength, 366 | & flags, 367 | address != NULL ? (struct sockaddr *) & sin : NULL, 368 | address != NULL ? & sinLength : NULL, 369 | NULL, 370 | NULL) == SOCKET_ERROR) 371 | { 372 | switch (WSAGetLastError ()) 373 | { 374 | case WSAEWOULDBLOCK: 375 | case WSAECONNRESET: 376 | return 0; 377 | } 378 | 379 | return -1; 380 | } 381 | 382 | if (flags & MSG_PARTIAL) 383 | return -1; 384 | 385 | if (address != NULL) 386 | { 387 | address -> host = (enet_uint32) sin.sin_addr.s_addr; 388 | address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); 389 | } 390 | 391 | return (int) recvLength; 392 | } 393 | 394 | int 395 | enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout) 396 | { 397 | struct timeval timeVal; 398 | 399 | timeVal.tv_sec = timeout / 1000; 400 | timeVal.tv_usec = (timeout % 1000) * 1000; 401 | 402 | return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal); 403 | } 404 | 405 | int 406 | enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout) 407 | { 408 | fd_set readSet, writeSet; 409 | struct timeval timeVal; 410 | int selectCount; 411 | 412 | timeVal.tv_sec = timeout / 1000; 413 | timeVal.tv_usec = (timeout % 1000) * 1000; 414 | 415 | FD_ZERO (& readSet); 416 | FD_ZERO (& writeSet); 417 | 418 | if (* condition & ENET_SOCKET_WAIT_SEND) 419 | FD_SET (socket, & writeSet); 420 | 421 | if (* condition & ENET_SOCKET_WAIT_RECEIVE) 422 | FD_SET (socket, & readSet); 423 | 424 | selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal); 425 | 426 | if (selectCount < 0) 427 | return -1; 428 | 429 | * condition = ENET_SOCKET_WAIT_NONE; 430 | 431 | if (selectCount == 0) 432 | return 0; 433 | 434 | if (FD_ISSET (socket, & writeSet)) 435 | * condition |= ENET_SOCKET_WAIT_SEND; 436 | 437 | if (FD_ISSET (socket, & readSet)) 438 | * condition |= ENET_SOCKET_WAIT_RECEIVE; 439 | 440 | return 0; 441 | } 442 | 443 | #endif 444 | 445 | -------------------------------------------------------------------------------- /enet/unix.c: -------------------------------------------------------------------------------- 1 | /** 2 | @file unix.c 3 | @brief ENet Unix system specific functions 4 | */ 5 | #ifndef _WIN32 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #define ENET_BUILDING_LIB 1 19 | #include "include/enet.h" 20 | 21 | #ifdef __APPLE__ 22 | #ifdef HAS_POLL 23 | #undef HAS_POLL 24 | #endif 25 | #ifndef HAS_FCNTL 26 | #define HAS_FCNTL 1 27 | #endif 28 | #ifndef HAS_INET_PTON 29 | #define HAS_INET_PTON 1 30 | #endif 31 | #ifndef HAS_INET_NTOP 32 | #define HAS_INET_NTOP 1 33 | #endif 34 | #ifndef HAS_MSGHDR_FLAGS 35 | #define HAS_MSGHDR_FLAGS 1 36 | #endif 37 | #ifndef HAS_SOCKLEN_T 38 | #define HAS_SOCKLEN_T 1 39 | #endif 40 | #ifndef HAS_GETADDRINFO 41 | #define HAS_GETADDRINFO 1 42 | #endif 43 | #ifndef HAS_GETNAMEINFO 44 | #define HAS_GETNAMEINFO 1 45 | #endif 46 | #endif 47 | 48 | #ifdef HAS_FCNTL 49 | #include 50 | #endif 51 | 52 | #ifdef HAS_POLL 53 | #include 54 | #endif 55 | 56 | #ifndef MSG_NOSIGNAL 57 | #define MSG_NOSIGNAL 0 58 | #endif 59 | 60 | static enet_uint32 timeBase = 0; 61 | 62 | int 63 | enet_initialize (void) 64 | { 65 | return 0; 66 | } 67 | 68 | void 69 | enet_deinitialize (void) 70 | { 71 | } 72 | 73 | enet_uint32 74 | enet_host_random_seed (void) 75 | { 76 | return (enet_uint32) time (NULL); 77 | } 78 | 79 | enet_uint32 80 | enet_time_get (void) 81 | { 82 | struct timeval timeVal; 83 | 84 | gettimeofday (& timeVal, NULL); 85 | 86 | return timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - timeBase; 87 | } 88 | 89 | void 90 | enet_time_set (enet_uint32 newTimeBase) 91 | { 92 | struct timeval timeVal; 93 | 94 | gettimeofday (& timeVal, NULL); 95 | 96 | timeBase = timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - newTimeBase; 97 | } 98 | 99 | int 100 | enet_address_set_host_ip (ENetAddress * address, const char * name) 101 | { 102 | #ifdef HAS_INET_PTON 103 | if (! inet_pton (AF_INET, name, & address -> host)) 104 | #else 105 | if (! inet_aton (name, (struct in_addr *) & address -> host)) 106 | #endif 107 | return -1; 108 | 109 | return 0; 110 | } 111 | 112 | int 113 | enet_address_set_host (ENetAddress * address, const char * name) 114 | { 115 | #ifdef HAS_GETADDRINFO 116 | struct addrinfo hints, * resultList = NULL, * result = NULL; 117 | 118 | memset (& hints, 0, sizeof (hints)); 119 | hints.ai_family = AF_INET; 120 | 121 | if (getaddrinfo (name, NULL, NULL, & resultList) != 0) 122 | return -1; 123 | 124 | for (result = resultList; result != NULL; result = result -> ai_next) 125 | { 126 | if (result -> ai_family == AF_INET && result -> ai_addr != NULL && result -> ai_addrlen >= sizeof (struct sockaddr_in)) 127 | { 128 | struct sockaddr_in * sin = (struct sockaddr_in *) result -> ai_addr; 129 | 130 | address -> host = sin -> sin_addr.s_addr; 131 | 132 | freeaddrinfo (resultList); 133 | 134 | return 0; 135 | } 136 | } 137 | 138 | if (resultList != NULL) 139 | freeaddrinfo (resultList); 140 | #else 141 | struct hostent * hostEntry = NULL; 142 | #ifdef HAS_GETHOSTBYNAME_R 143 | struct hostent hostData; 144 | char buffer [2048]; 145 | int errnum; 146 | 147 | #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) 148 | gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum); 149 | #else 150 | hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum); 151 | #endif 152 | #else 153 | hostEntry = gethostbyname (name); 154 | #endif 155 | 156 | if (hostEntry != NULL && hostEntry -> h_addrtype == AF_INET) 157 | { 158 | address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0]; 159 | 160 | return 0; 161 | } 162 | #endif 163 | 164 | return enet_address_set_host_ip (address, name); 165 | } 166 | 167 | int 168 | enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) 169 | { 170 | #ifdef HAS_INET_NTOP 171 | if (inet_ntop (AF_INET, & address -> host, name, nameLength) == NULL) 172 | #else 173 | char * addr = inet_ntoa (* (struct in_addr *) & address -> host); 174 | if (addr != NULL) 175 | { 176 | size_t addrLen = strlen(addr); 177 | if (addrLen >= nameLength) 178 | return -1; 179 | memcpy (name, addr, addrLen + 1); 180 | } 181 | else 182 | #endif 183 | return -1; 184 | return 0; 185 | } 186 | 187 | int 188 | enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) 189 | { 190 | #ifdef HAS_GETNAMEINFO 191 | struct sockaddr_in sin; 192 | int err; 193 | 194 | memset (& sin, 0, sizeof (struct sockaddr_in)); 195 | 196 | sin.sin_family = AF_INET; 197 | sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); 198 | sin.sin_addr.s_addr = address -> host; 199 | 200 | err = getnameinfo ((struct sockaddr *) & sin, sizeof (sin), name, nameLength, NULL, 0, NI_NAMEREQD); 201 | if (! err) 202 | { 203 | if (name != NULL && nameLength > 0 && ! memchr (name, '\0', nameLength)) 204 | return -1; 205 | return 0; 206 | } 207 | if (err != EAI_NONAME) 208 | return -1; 209 | #else 210 | struct in_addr in; 211 | struct hostent * hostEntry = NULL; 212 | #ifdef HAS_GETHOSTBYADDR_R 213 | struct hostent hostData; 214 | char buffer [2048]; 215 | int errnum; 216 | 217 | in.s_addr = address -> host; 218 | 219 | #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) 220 | gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum); 221 | #else 222 | hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum); 223 | #endif 224 | #else 225 | in.s_addr = address -> host; 226 | 227 | hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET); 228 | #endif 229 | 230 | if (hostEntry != NULL) 231 | { 232 | size_t hostLen = strlen (hostEntry -> h_name); 233 | if (hostLen >= nameLength) 234 | return -1; 235 | memcpy (name, hostEntry -> h_name, hostLen + 1); 236 | return 0; 237 | } 238 | #endif 239 | 240 | return enet_address_get_host_ip (address, name, nameLength); 241 | } 242 | 243 | int 244 | enet_socket_bind (ENetSocket socket, const ENetAddress * address) 245 | { 246 | struct sockaddr_in sin; 247 | 248 | memset (& sin, 0, sizeof (struct sockaddr_in)); 249 | 250 | sin.sin_family = AF_INET; 251 | 252 | if (address != NULL) 253 | { 254 | sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); 255 | sin.sin_addr.s_addr = address -> host; 256 | } 257 | else 258 | { 259 | sin.sin_port = 0; 260 | sin.sin_addr.s_addr = INADDR_ANY; 261 | } 262 | 263 | return bind (socket, 264 | (struct sockaddr *) & sin, 265 | sizeof (struct sockaddr_in)); 266 | } 267 | 268 | int 269 | enet_socket_get_address (ENetSocket socket, ENetAddress * address) 270 | { 271 | struct sockaddr_in sin; 272 | socklen_t sinLength = sizeof (struct sockaddr_in); 273 | 274 | if (getsockname (socket, (struct sockaddr *) & sin, & sinLength) == -1) 275 | return -1; 276 | 277 | address -> host = (enet_uint32) sin.sin_addr.s_addr; 278 | address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); 279 | 280 | return 0; 281 | } 282 | 283 | int 284 | enet_socket_listen (ENetSocket socket, int backlog) 285 | { 286 | return listen (socket, backlog < 0 ? SOMAXCONN : backlog); 287 | } 288 | 289 | ENetSocket 290 | enet_socket_create (ENetSocketType type) 291 | { 292 | return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); 293 | } 294 | 295 | int 296 | enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) 297 | { 298 | int result = -1; 299 | switch (option) 300 | { 301 | case ENET_SOCKOPT_NONBLOCK: 302 | #ifdef HAS_FCNTL 303 | result = fcntl (socket, F_SETFL, (value ? O_NONBLOCK : 0) | (fcntl (socket, F_GETFL) & ~O_NONBLOCK)); 304 | #else 305 | result = ioctl (socket, FIONBIO, & value); 306 | #endif 307 | break; 308 | 309 | case ENET_SOCKOPT_BROADCAST: 310 | result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int)); 311 | break; 312 | 313 | case ENET_SOCKOPT_REUSEADDR: 314 | result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int)); 315 | break; 316 | 317 | case ENET_SOCKOPT_RCVBUF: 318 | result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int)); 319 | break; 320 | 321 | case ENET_SOCKOPT_SNDBUF: 322 | result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int)); 323 | break; 324 | 325 | case ENET_SOCKOPT_RCVTIMEO: 326 | { 327 | struct timeval timeVal; 328 | timeVal.tv_sec = value / 1000; 329 | timeVal.tv_usec = (value % 1000) * 1000; 330 | result = setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & timeVal, sizeof (struct timeval)); 331 | break; 332 | } 333 | 334 | case ENET_SOCKOPT_SNDTIMEO: 335 | { 336 | struct timeval timeVal; 337 | timeVal.tv_sec = value / 1000; 338 | timeVal.tv_usec = (value % 1000) * 1000; 339 | result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & timeVal, sizeof (struct timeval)); 340 | break; 341 | } 342 | 343 | case ENET_SOCKOPT_NODELAY: 344 | result = setsockopt (socket, IPPROTO_TCP, TCP_NODELAY, (char *) & value, sizeof (int)); 345 | break; 346 | 347 | default: 348 | break; 349 | } 350 | return result == -1 ? -1 : 0; 351 | } 352 | 353 | int 354 | enet_socket_get_option (ENetSocket socket, ENetSocketOption option, int * value) 355 | { 356 | int result = -1; 357 | socklen_t len; 358 | switch (option) 359 | { 360 | case ENET_SOCKOPT_ERROR: 361 | len = sizeof (int); 362 | result = getsockopt (socket, SOL_SOCKET, SO_ERROR, value, & len); 363 | break; 364 | 365 | default: 366 | break; 367 | } 368 | return result == -1 ? -1 : 0; 369 | } 370 | 371 | int 372 | enet_socket_connect (ENetSocket socket, const ENetAddress * address) 373 | { 374 | struct sockaddr_in sin; 375 | int result; 376 | 377 | memset (& sin, 0, sizeof (struct sockaddr_in)); 378 | 379 | sin.sin_family = AF_INET; 380 | sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); 381 | sin.sin_addr.s_addr = address -> host; 382 | 383 | result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in)); 384 | if (result == -1 && errno == EINPROGRESS) 385 | return 0; 386 | 387 | return result; 388 | } 389 | 390 | ENetSocket 391 | enet_socket_accept (ENetSocket socket, ENetAddress * address) 392 | { 393 | int result; 394 | struct sockaddr_in sin; 395 | socklen_t sinLength = sizeof (struct sockaddr_in); 396 | 397 | result = accept (socket, 398 | address != NULL ? (struct sockaddr *) & sin : NULL, 399 | address != NULL ? & sinLength : NULL); 400 | 401 | if (result == -1) 402 | return ENET_SOCKET_NULL; 403 | 404 | if (address != NULL) 405 | { 406 | address -> host = (enet_uint32) sin.sin_addr.s_addr; 407 | address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); 408 | } 409 | 410 | return result; 411 | } 412 | 413 | int 414 | enet_socket_shutdown (ENetSocket socket, ENetSocketShutdown how) 415 | { 416 | return shutdown (socket, (int) how); 417 | } 418 | 419 | void 420 | enet_socket_destroy (ENetSocket socket) 421 | { 422 | if (socket != -1) 423 | close (socket); 424 | } 425 | 426 | int 427 | enet_socket_send (ENetSocket socket, 428 | const ENetAddress * address, 429 | const ENetBuffer * buffers, 430 | size_t bufferCount) 431 | { 432 | struct msghdr msgHdr; 433 | struct sockaddr_in sin; 434 | int sentLength; 435 | 436 | memset (& msgHdr, 0, sizeof (struct msghdr)); 437 | 438 | if (address != NULL) 439 | { 440 | memset (& sin, 0, sizeof (struct sockaddr_in)); 441 | 442 | sin.sin_family = AF_INET; 443 | sin.sin_port = ENET_HOST_TO_NET_16 (address -> port); 444 | sin.sin_addr.s_addr = address -> host; 445 | 446 | msgHdr.msg_name = & sin; 447 | msgHdr.msg_namelen = sizeof (struct sockaddr_in); 448 | } 449 | 450 | msgHdr.msg_iov = (struct iovec *) buffers; 451 | msgHdr.msg_iovlen = bufferCount; 452 | 453 | sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL); 454 | 455 | if (sentLength == -1) 456 | { 457 | if (errno == EWOULDBLOCK) 458 | return 0; 459 | 460 | return -1; 461 | } 462 | 463 | return sentLength; 464 | } 465 | 466 | int 467 | enet_socket_receive (ENetSocket socket, 468 | ENetAddress * address, 469 | ENetBuffer * buffers, 470 | size_t bufferCount) 471 | { 472 | struct msghdr msgHdr; 473 | struct sockaddr_in sin; 474 | int recvLength; 475 | 476 | memset (& msgHdr, 0, sizeof (struct msghdr)); 477 | 478 | if (address != NULL) 479 | { 480 | msgHdr.msg_name = & sin; 481 | msgHdr.msg_namelen = sizeof (struct sockaddr_in); 482 | } 483 | 484 | msgHdr.msg_iov = (struct iovec *) buffers; 485 | msgHdr.msg_iovlen = bufferCount; 486 | 487 | recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL); 488 | 489 | if (recvLength == -1) 490 | { 491 | if (errno == EWOULDBLOCK) 492 | return 0; 493 | 494 | return -1; 495 | } 496 | 497 | #ifdef HAS_MSGHDR_FLAGS 498 | if (msgHdr.msg_flags & MSG_TRUNC) 499 | return -1; 500 | #endif 501 | 502 | if (address != NULL) 503 | { 504 | address -> host = (enet_uint32) sin.sin_addr.s_addr; 505 | address -> port = ENET_NET_TO_HOST_16 (sin.sin_port); 506 | } 507 | 508 | return recvLength; 509 | } 510 | 511 | int 512 | enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout) 513 | { 514 | struct timeval timeVal; 515 | 516 | timeVal.tv_sec = timeout / 1000; 517 | timeVal.tv_usec = (timeout % 1000) * 1000; 518 | 519 | return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal); 520 | } 521 | 522 | int 523 | enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout) 524 | { 525 | #ifdef HAS_POLL 526 | struct pollfd pollSocket; 527 | int pollCount; 528 | 529 | pollSocket.fd = socket; 530 | pollSocket.events = 0; 531 | 532 | if (* condition & ENET_SOCKET_WAIT_SEND) 533 | pollSocket.events |= POLLOUT; 534 | 535 | if (* condition & ENET_SOCKET_WAIT_RECEIVE) 536 | pollSocket.events |= POLLIN; 537 | 538 | pollCount = poll (& pollSocket, 1, timeout); 539 | 540 | if (pollCount < 0) 541 | { 542 | if (errno == EINTR && * condition & ENET_SOCKET_WAIT_INTERRUPT) 543 | { 544 | * condition = ENET_SOCKET_WAIT_INTERRUPT; 545 | 546 | return 0; 547 | } 548 | 549 | return -1; 550 | } 551 | 552 | * condition = ENET_SOCKET_WAIT_NONE; 553 | 554 | if (pollCount == 0) 555 | return 0; 556 | 557 | if (pollSocket.revents & POLLOUT) 558 | * condition |= ENET_SOCKET_WAIT_SEND; 559 | 560 | if (pollSocket.revents & POLLIN) 561 | * condition |= ENET_SOCKET_WAIT_RECEIVE; 562 | 563 | return 0; 564 | #else 565 | fd_set readSet, writeSet; 566 | struct timeval timeVal; 567 | int selectCount; 568 | 569 | timeVal.tv_sec = timeout / 1000; 570 | timeVal.tv_usec = (timeout % 1000) * 1000; 571 | 572 | FD_ZERO (& readSet); 573 | FD_ZERO (& writeSet); 574 | 575 | if (* condition & ENET_SOCKET_WAIT_SEND) 576 | FD_SET (socket, & writeSet); 577 | 578 | if (* condition & ENET_SOCKET_WAIT_RECEIVE) 579 | FD_SET (socket, & readSet); 580 | 581 | selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal); 582 | 583 | if (selectCount < 0) 584 | { 585 | if (errno == EINTR && * condition & ENET_SOCKET_WAIT_INTERRUPT) 586 | { 587 | * condition = ENET_SOCKET_WAIT_INTERRUPT; 588 | 589 | return 0; 590 | } 591 | 592 | return -1; 593 | } 594 | 595 | * condition = ENET_SOCKET_WAIT_NONE; 596 | 597 | if (selectCount == 0) 598 | return 0; 599 | 600 | if (FD_ISSET (socket, & writeSet)) 601 | * condition |= ENET_SOCKET_WAIT_SEND; 602 | 603 | if (FD_ISSET (socket, & readSet)) 604 | * condition |= ENET_SOCKET_WAIT_RECEIVE; 605 | 606 | return 0; 607 | #endif 608 | } 609 | 610 | #endif 611 | 612 | -------------------------------------------------------------------------------- /proton/variant.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "vector.hpp" 7 | 8 | #define C_VAR_SPACE_BYTES 16 9 | class variant_t { 10 | public: 11 | enum class vartype_t { TYPE_UNUSED, TYPE_FLOAT, TYPE_STRING, TYPE_VECTOR2, TYPE_VECTOR3, TYPE_UINT32, TYPE_ENTITY, TYPE_COMPONENT, TYPE_RECT, TYPE_INT32 }; 12 | variant_t() { set_defaults(); } 13 | variant_t(const variant_t& v) { 14 | set_defaults(); 15 | *this = v; 16 | } 17 | variant_t(int32_t var) { 18 | set_defaults(); 19 | set(var); 20 | } 21 | variant_t(uint32_t var) { 22 | set_defaults(); 23 | set(var); 24 | } 25 | variant_t(float var) { 26 | set_defaults(); 27 | set(var); 28 | } 29 | variant_t(float x, float y) { 30 | set_defaults(); 31 | set(vector2_t(x, y)); 32 | } 33 | variant_t(float x, float y, float z) { 34 | set_defaults(); 35 | set(vector3_t(x, y, z)); 36 | } 37 | variant_t(const vector2_t& v2) { 38 | set_defaults(); 39 | set(v2); 40 | } 41 | variant_t(const vector3_t& v3) { 42 | set_defaults(); 43 | set(v3); 44 | } 45 | variant_t(const rect_t& r) { 46 | set_defaults(); 47 | set(r); 48 | } 49 | variant_t(const std::string& var) { 50 | set_defaults(); 51 | set(var); 52 | } 53 | 54 | void reset() { m_type = vartype_t::TYPE_UNUSED; } 55 | void set(const variant_t& v) { 56 | switch (v.get_type()) { 57 | case vartype_t::TYPE_FLOAT: set(v.get_float()); break; 58 | case vartype_t::TYPE_STRING: set(v.get_string()); break; 59 | case vartype_t::TYPE_VECTOR2: set(v.get_vector2()); break; 60 | case vartype_t::TYPE_VECTOR3: set(v.get_vector3()); break; 61 | case vartype_t::TYPE_UINT32: set(v.get_uint32()); break; 62 | case vartype_t::TYPE_INT32: set(v.get_int32()); break; 63 | case vartype_t::TYPE_RECT: set(v.get_rect()); break; 64 | case vartype_t::TYPE_ENTITY: 65 | case vartype_t::TYPE_COMPONENT: 66 | default: break; 67 | } 68 | } 69 | void set(float var) { 70 | m_type = vartype_t::TYPE_FLOAT; 71 | *((float*)m_var) = var; 72 | } 73 | void set(uint32_t var) { 74 | m_type = vartype_t::TYPE_UINT32; 75 | *((uint32_t*)m_var) = var; 76 | } 77 | void set(int32_t var) { 78 | m_type = vartype_t::TYPE_INT32; 79 | *((int32_t*)m_var) = var; 80 | } 81 | void operator=(float var) { 82 | set(var); 83 | } 84 | void operator=(int32_t var) { 85 | set(var); 86 | } 87 | void operator=(uint32_t var) { 88 | set(var); 89 | } 90 | void operator=(std::string const& var) { 91 | set(var); 92 | } 93 | void set(std::string const& var) { 94 | m_type = vartype_t::TYPE_STRING; 95 | m_string = var; 96 | } 97 | void operator=(vector2_t const& var) { 98 | set(var); 99 | } 100 | void set(vector2_t const& var) { 101 | m_type = vartype_t::TYPE_VECTOR2; 102 | *((vector2_t*)m_var) = var; 103 | } 104 | void set(float x, float y) { set(vector2_t(x, y)); } 105 | void operator=(vector3_t const& var) { 106 | set(var); 107 | } 108 | void operator=(rect_t const& var) { 109 | set(var); 110 | } 111 | void set(vector3_t const& var) { 112 | m_type = vartype_t::TYPE_VECTOR3; 113 | *((vector3_t*)m_var) = var; 114 | } 115 | void set(rect_t const& var) { 116 | m_type = vartype_t::TYPE_RECT; 117 | *((rect_t*)m_var) = var; 118 | } 119 | void set(float x, float y, float z) { set(vector3_t(x, y, z)); } 120 | 121 | float& get_float() { 122 | if (m_type == vartype_t::TYPE_UNUSED) 123 | set(float(0)); 124 | return *((float*)m_var); 125 | } 126 | int32_t& get_int32() { 127 | if (m_type == vartype_t::TYPE_UNUSED) 128 | set(int32_t(0)); 129 | return *((int32_t*)m_var); 130 | } 131 | uint32_t& get_uint32() { 132 | if (m_type == vartype_t::TYPE_UNUSED) 133 | set(uint32_t(0)); 134 | return *((uint32_t*)m_var); 135 | } 136 | std::string& get_string() { return m_string; } 137 | vector2_t& get_vector2() { 138 | if (m_type == vartype_t::TYPE_UNUSED) 139 | set(vector2_t(0, 0)); 140 | return *((vector2_t*)m_var); 141 | } 142 | vector3_t& get_vector3() { 143 | if (m_type == vartype_t::TYPE_UNUSED) 144 | set(vector3_t(0, 0, 0)); 145 | return *((vector3_t*)m_var); 146 | } 147 | rect_t& get_rect() { 148 | if (m_type == vartype_t::TYPE_UNUSED) 149 | set(rect_t(0, 0, 0, 0)); 150 | return *((rect_t*)m_var); 151 | } 152 | 153 | const float& get_float() const { return *((float*)m_var); } 154 | const int32_t& get_int32() const { return *((int32_t*)m_var); } 155 | const uint32_t& get_uint32() const { return *((uint32_t*)m_var); } 156 | const std::string& get_string() const { return m_string; } 157 | const vector2_t& get_vector2() const { return *((vector2_t*)m_var); } 158 | const vector3_t& get_vector3() const { return *((vector3_t*)m_var); } 159 | const rect_t& get_rect() const { return *((rect_t*)m_var); } 160 | 161 | vartype_t get_type() const { return m_type; } 162 | 163 | std::string print() { 164 | switch (get_type()) { 165 | case vartype_t::TYPE_FLOAT: return std::to_string(get_float()); 166 | case vartype_t::TYPE_STRING: return get_string(); 167 | case vartype_t::TYPE_VECTOR2: return "x: " + std::to_string(get_vector2().m_x) + " y: " + std::to_string(get_vector2().m_y); 168 | case vartype_t::TYPE_VECTOR3: 169 | return "x: " + std::to_string(get_vector3().m_x) + " y: " + std::to_string(get_vector3().m_y) + " z: " + std::to_string(get_vector3().m_z); 170 | case vartype_t::TYPE_UINT32: return std::to_string(get_uint32()); 171 | case vartype_t::TYPE_INT32: return std::to_string(get_int32()); 172 | case vartype_t::TYPE_RECT: 173 | return "x: " + std::to_string(get_rect().m_x) + " y: " + std::to_string(get_rect().m_y) + " w: " + std::to_string(get_rect().m_w) + 174 | " h: " + std::to_string(get_rect().m_h); 175 | case vartype_t::TYPE_ENTITY: 176 | case vartype_t::TYPE_COMPONENT: 177 | case vartype_t::TYPE_UNUSED: return "unused"; break; 178 | default: break; 179 | } 180 | return "unknown"; 181 | } 182 | variant_t& operator=(const variant_t& rhs) { 183 | m_type = rhs.m_type; 184 | m_pointer = rhs.m_pointer; 185 | memcpy(m_var, rhs.m_var, C_VAR_SPACE_BYTES); 186 | m_string = rhs.m_string; 187 | return *this; 188 | } 189 | variant_t& operator+=(const variant_t& rhs) { 190 | if (get_type() == rhs.get_type()) { 191 | switch (get_type()) { 192 | case vartype_t::TYPE_FLOAT: set(get_float() + rhs.get_float()); break; 193 | case vartype_t::TYPE_STRING: set(get_string() + rhs.get_string()); break; 194 | case vartype_t::TYPE_VECTOR2: set(get_vector2() + rhs.get_vector2()); break; 195 | case vartype_t::TYPE_VECTOR3: set(get_vector3() + rhs.get_vector3()); break; 196 | case vartype_t::TYPE_UINT32: set(get_uint32() + rhs.get_uint32()); break; 197 | case vartype_t::TYPE_INT32: set(get_int32() + rhs.get_int32()); break; 198 | default: break; 199 | } 200 | } 201 | return *this; 202 | } 203 | variant_t& operator-=(const variant_t& rhs) { 204 | if (get_type() == rhs.get_type()) { 205 | switch (get_type()) { 206 | case vartype_t::TYPE_FLOAT: set(get_float() - rhs.get_float()); break; 207 | case vartype_t::TYPE_VECTOR2: set(get_vector2() - rhs.get_vector2()); break; 208 | case vartype_t::TYPE_VECTOR3: set(get_vector3() - rhs.get_vector3()); break; 209 | case vartype_t::TYPE_UINT32: set(get_uint32() - rhs.get_uint32()); break; 210 | case vartype_t::TYPE_INT32: set(get_int32() - rhs.get_int32()); break; 211 | default: break; 212 | } 213 | } 214 | return *this; 215 | } 216 | bool operator==(const variant_t& rhs) const { 217 | if (get_type() != rhs.get_type()) 218 | return false; 219 | switch (get_type()) { 220 | case vartype_t::TYPE_UNUSED: return true; 221 | case vartype_t::TYPE_FLOAT: return get_float() == rhs.get_float(); 222 | case vartype_t::TYPE_STRING: return get_string() == rhs.get_string(); 223 | case vartype_t::TYPE_VECTOR2: return get_vector2() == rhs.get_vector2(); 224 | case vartype_t::TYPE_VECTOR3: return get_vector3() == rhs.get_vector3(); 225 | case vartype_t::TYPE_UINT32: return get_uint32() == rhs.get_uint32(); 226 | case vartype_t::TYPE_RECT: return get_rect() == rhs.get_rect(); 227 | case vartype_t::TYPE_INT32: return get_int32() == rhs.get_int32(); 228 | case vartype_t::TYPE_ENTITY: 229 | case vartype_t::TYPE_COMPONENT: 230 | default: return false; 231 | } 232 | } 233 | bool operator!=(const variant_t& rhs) const { return !operator==(rhs); } 234 | friend class variantlist_t; 235 | 236 | private: 237 | void set_defaults() { m_type = vartype_t::TYPE_UNUSED; } 238 | vartype_t m_type; 239 | void* m_pointer; 240 | union { 241 | uint8_t m_var[C_VAR_SPACE_BYTES]; 242 | float m_as_floats[4]; 243 | uint32_t m_as_uint32s[4]; 244 | int32_t m_as_int32s[4]; 245 | }; 246 | std::string m_string; 247 | }; 248 | inline variant_t operator+(variant_t lhs, const variant_t& rhs) { 249 | lhs += rhs; 250 | return lhs; 251 | } 252 | inline variant_t operator-(variant_t lhs, const variant_t& rhs) { 253 | lhs -= rhs; 254 | return lhs; 255 | } 256 | #define C_MAX_VARIANT_LIST_PARMS 7 257 | class variantlist_t { 258 | int size_of_variant(variant_t::vartype_t type) { 259 | switch (type) { 260 | case variant_t::vartype_t::TYPE_UNUSED: 261 | case variant_t::vartype_t::TYPE_COMPONENT: 262 | case variant_t::vartype_t::TYPE_ENTITY: return 0; 263 | case variant_t::vartype_t::TYPE_UINT32: 264 | case variant_t::vartype_t::TYPE_INT32: 265 | case variant_t::vartype_t::TYPE_FLOAT: return 4; 266 | case variant_t::vartype_t::TYPE_VECTOR2: return sizeof(vector2_t); 267 | case variant_t::vartype_t::TYPE_VECTOR3: return sizeof(vector3_t); 268 | case variant_t::vartype_t::TYPE_RECT: return sizeof(rect_t); 269 | default: return 0; 270 | } 271 | } 272 | 273 | public: 274 | variantlist_t(){}; 275 | variant_t& get(int parmNum) { return m_variant[parmNum]; } 276 | variant_t& operator[](int num) { 277 | return m_variant[num]; 278 | } 279 | variantlist_t(const std::string& string) { 280 | m_variant[0] = variant_t(string); 281 | } 282 | variantlist_t(variant_t v0) { m_variant[0] = v0; } 283 | variantlist_t(variant_t v0, variant_t v1) { 284 | m_variant[0] = v0; 285 | m_variant[1] = v1; 286 | } 287 | variantlist_t(variant_t v0, variant_t v1, variant_t v2) { 288 | m_variant[0] = v0; 289 | m_variant[1] = v1; 290 | m_variant[2] = v2; 291 | } 292 | variantlist_t(variant_t v0, variant_t v1, variant_t v2, variant_t v3) { 293 | m_variant[0] = v0; 294 | m_variant[1] = v1; 295 | m_variant[2] = v2; 296 | m_variant[3] = v3; 297 | } 298 | variantlist_t(variant_t v0, variant_t v1, variant_t v2, variant_t v3, variant_t v4) { 299 | m_variant[0] = v0; 300 | m_variant[1] = v1; 301 | m_variant[2] = v2; 302 | m_variant[3] = v3; 303 | m_variant[4] = v4; 304 | } 305 | variantlist_t(variant_t v0, variant_t v1, variant_t v2, variant_t v3, variant_t v4, variant_t v5) { 306 | m_variant[0] = v0; 307 | m_variant[1] = v1; 308 | m_variant[2] = v2; 309 | m_variant[3] = v3; 310 | m_variant[4] = v4; 311 | m_variant[5] = v5; 312 | } 313 | void reset() { 314 | for (int i = 0; i < C_MAX_VARIANT_LIST_PARMS; i++) m_variant[i].reset(); 315 | } 316 | uint32_t get_mem_needed() { 317 | int vars_used = 0; 318 | int mem_needed = 0; 319 | int var_size; 320 | for (int i = 0; i < C_MAX_VARIANT_LIST_PARMS; i++) { 321 | if (m_variant[i].get_type() == variant_t::vartype_t::TYPE_STRING) 322 | var_size = m_variant[i].get_string().size() + 4; 323 | else 324 | var_size = size_of_variant(m_variant[i].get_type()); 325 | if (var_size > 0) { 326 | vars_used++; 327 | mem_needed += var_size; 328 | } 329 | } 330 | int total = mem_needed + 1 + (vars_used * 2); 331 | return total; 332 | } 333 | uint8_t* serialize_to_mem(uint32_t* size, uint8_t* data) { 334 | int vars_used = 0; 335 | int mem_needed = 0; 336 | int var_size; 337 | for (int i = 0; i < C_MAX_VARIANT_LIST_PARMS; i++) { 338 | if (m_variant[i].get_type() == variant_t::vartype_t::TYPE_STRING) 339 | var_size = m_variant[i].get_string().size() + 4; 340 | else 341 | var_size = size_of_variant(m_variant[i].get_type()); 342 | if (var_size > 0) { 343 | vars_used++; 344 | mem_needed += var_size; 345 | } 346 | } 347 | int total = mem_needed + 1 + (vars_used * 2); 348 | if (!data) 349 | data = new uint8_t[total]; 350 | uint8_t* p = data; 351 | *(p++) = uint8_t(vars_used); 352 | for (int idx = 0; idx < C_MAX_VARIANT_LIST_PARMS; idx++) { 353 | uint8_t type = uint8_t(m_variant[idx].get_type()); 354 | if (m_variant[idx].get_type() == variant_t::vartype_t::TYPE_STRING) { 355 | uint32_t len = m_variant[idx].get_string().size(); 356 | memcpy(p++, &idx, 1); 357 | memcpy(p++, &type, 1); 358 | memcpy(p, &len, 4); 359 | p += 4; 360 | memcpy(p, m_variant[idx].get_string().c_str(), len); 361 | p += len; 362 | } else { 363 | var_size = size_of_variant(m_variant[idx].get_type()); 364 | if (var_size > 0) { 365 | memcpy(p++, &idx, 1); 366 | memcpy(p++, &type, 1); 367 | memcpy(p, m_variant[idx].m_var, var_size); 368 | p += var_size; 369 | } 370 | } 371 | } 372 | if (size) 373 | *size = total; 374 | return data; 375 | } 376 | bool serialize_from_mem(uint8_t* data, int* read = 0) { //robinson way cuz i dont want to refactor this yet 377 | uint8_t* p = data; 378 | uint8_t count = *(p++); 379 | for (int i = 0; i < count; i++) { 380 | uint8_t index = *(p++); 381 | uint8_t type = *(p++); 382 | switch (variant_t::vartype_t(type)) { 383 | case variant_t::vartype_t::TYPE_STRING: { 384 | uint32_t len; 385 | memcpy(&len, p, 4); 386 | p += 4; 387 | std::string v; 388 | v.resize(len); 389 | memcpy(&v[0], p, len); 390 | p += len; 391 | m_variant[index].set(v); 392 | break; 393 | } 394 | case variant_t::vartype_t::TYPE_UINT32: { 395 | uint32_t v; 396 | memcpy(&v, p, sizeof(uint32_t)); 397 | p += sizeof(uint32_t); 398 | m_variant[index].set(v); 399 | break; 400 | } 401 | case variant_t::vartype_t::TYPE_INT32: { 402 | int32_t v; 403 | memcpy(&v, p, sizeof(int32_t)); 404 | p += sizeof(int32_t); 405 | m_variant[index].set(v); 406 | break; 407 | } 408 | case variant_t::vartype_t::TYPE_FLOAT: { 409 | float v; 410 | memcpy(&v, p, sizeof(float)); 411 | p += sizeof(float); 412 | m_variant[index].set(v); 413 | break; 414 | } 415 | case variant_t::vartype_t::TYPE_VECTOR2: { 416 | vector2_t v; 417 | memcpy(&v, p, sizeof(vector2_t)); 418 | p += sizeof(vector2_t); 419 | m_variant[index].set(v); 420 | break; 421 | } 422 | case variant_t::vartype_t::TYPE_VECTOR3: { 423 | vector3_t v; 424 | memcpy(&v, p, sizeof(vector3_t)); 425 | p += sizeof(vector3_t); 426 | m_variant[index].set(v); 427 | break; 428 | } 429 | case variant_t::vartype_t::TYPE_RECT: { 430 | rect_t v; 431 | memcpy(&v, p, sizeof(rect_t)); 432 | p += sizeof(rect_t); 433 | m_variant[index].set(v); 434 | break; 435 | } 436 | default: 437 | if (read) 438 | *read = 0; 439 | return false; 440 | } 441 | } 442 | if (read) 443 | *read = int(p - data); 444 | return true; 445 | } 446 | variant_t m_variant[C_MAX_VARIANT_LIST_PARMS]; 447 | int size() { 448 | int gg = 0; 449 | for (int i = 0; i < C_MAX_VARIANT_LIST_PARMS; i++) { 450 | if (m_variant[i].get_type() == variant_t::vartype_t::TYPE_UNUSED) continue; 451 | gg++; 452 | //ss << "param " << std::to_string(i) << ": " << m_variant[i].print() + "\n"; 453 | } return gg; 454 | } 455 | std::string print() { 456 | std::stringstream ss; 457 | for (int i = 0; i < C_MAX_VARIANT_LIST_PARMS; i++) { 458 | if (m_variant[i].get_type() == variant_t::vartype_t::TYPE_UNUSED) 459 | continue; 460 | ss << "param " << std::to_string(i) << ": " << m_variant[i].print() + "\n"; 461 | } 462 | if (ss.str().empty()) 463 | ss.str("(none)"); 464 | return ss.str(); 465 | } 466 | }; 467 | -------------------------------------------------------------------------------- /enet/host.c: -------------------------------------------------------------------------------- 1 | /** 2 | @file host.c 3 | @brief ENet host management functions 4 | */ 5 | #define ENET_BUILDING_LIB 1 6 | #include 7 | #include "include/enet.h" 8 | 9 | /** @defgroup host ENet host functions 10 | @{ 11 | */ 12 | 13 | /** Creates a host for communicating to peers. 14 | 15 | @param address the address at which other peers may connect to this host. If NULL, then no peers may connect to the host. 16 | @param peerCount the maximum number of peers that should be allocated for the host. 17 | @param channelLimit the maximum number of channels allowed; if 0, then this is equivalent to ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT 18 | @param incomingBandwidth downstream bandwidth of the host in bytes/second; if 0, ENet will assume unlimited bandwidth. 19 | @param outgoingBandwidth upstream bandwidth of the host in bytes/second; if 0, ENet will assume unlimited bandwidth. 20 | 21 | @returns the host on success and NULL on failure 22 | 23 | @remarks ENet will strategically drop packets on specific sides of a connection between hosts 24 | to ensure the host's bandwidth is not overwhelmed. The bandwidth parameters also determine 25 | the window size of a connection which limits the amount of reliable packets that may be in transit 26 | at any given time. 27 | */ 28 | ENetHost * 29 | enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelLimit, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth) 30 | { 31 | ENetHost * host; 32 | ENetPeer * currentPeer; 33 | 34 | if (peerCount > ENET_PROTOCOL_MAXIMUM_PEER_ID) 35 | return NULL; 36 | 37 | host = (ENetHost *) enet_malloc (sizeof (ENetHost)); 38 | if (host == NULL) 39 | return NULL; 40 | memset (host, 0, sizeof (ENetHost)); 41 | 42 | host -> peers = (ENetPeer *) enet_malloc (peerCount * sizeof (ENetPeer)); 43 | if (host -> peers == NULL) 44 | { 45 | enet_free (host); 46 | 47 | return NULL; 48 | } 49 | memset (host -> peers, 0, peerCount * sizeof (ENetPeer)); 50 | 51 | host -> socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM); 52 | if (host -> socket == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket, address) < 0)) 53 | { 54 | if (host -> socket != ENET_SOCKET_NULL) 55 | enet_socket_destroy (host -> socket); 56 | 57 | enet_free (host -> peers); 58 | enet_free (host); 59 | 60 | return NULL; 61 | } 62 | 63 | enet_socket_set_option (host -> socket, ENET_SOCKOPT_NONBLOCK, 1); 64 | enet_socket_set_option (host -> socket, ENET_SOCKOPT_BROADCAST, 1); 65 | enet_socket_set_option (host -> socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE); 66 | enet_socket_set_option (host -> socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE); 67 | 68 | if (address != NULL && enet_socket_get_address (host -> socket, & host -> address) < 0) 69 | host -> address = * address; 70 | 71 | if (! channelLimit || channelLimit > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT) 72 | channelLimit = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT; 73 | else 74 | if (channelLimit < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT) 75 | channelLimit = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT; 76 | 77 | host -> randomSeed = (enet_uint32) (size_t) host; 78 | host -> randomSeed += enet_host_random_seed (); 79 | host -> randomSeed = (host -> randomSeed << 16) | (host -> randomSeed >> 16); 80 | host -> channelLimit = channelLimit; 81 | host -> incomingBandwidth = incomingBandwidth; 82 | host -> outgoingBandwidth = outgoingBandwidth; 83 | host -> bandwidthThrottleEpoch = 0; 84 | host -> recalculateBandwidthLimits = 0; 85 | host -> mtu = ENET_HOST_DEFAULT_MTU; 86 | host -> peerCount = peerCount; 87 | host -> commandCount = 0; 88 | host -> bufferCount = 0; 89 | host -> checksum = NULL; 90 | host -> receivedAddress.host = ENET_HOST_ANY; 91 | host -> receivedAddress.port = 0; 92 | host -> receivedData = NULL; 93 | host -> receivedDataLength = 0; 94 | 95 | host -> totalSentData = 0; 96 | host -> totalSentPackets = 0; 97 | host -> totalReceivedData = 0; 98 | host -> totalReceivedPackets = 0; 99 | 100 | host -> connectedPeers = 0; 101 | host -> bandwidthLimitedPeers = 0; 102 | host -> duplicatePeers = ENET_PROTOCOL_MAXIMUM_PEER_ID; 103 | host -> maximumPacketSize = ENET_HOST_DEFAULT_MAXIMUM_PACKET_SIZE; 104 | host -> maximumWaitingData = ENET_HOST_DEFAULT_MAXIMUM_WAITING_DATA; 105 | 106 | host -> compressor.context = NULL; 107 | host -> compressor.compress = NULL; 108 | host -> compressor.decompress = NULL; 109 | host -> compressor.destroy = NULL; 110 | 111 | host -> intercept = NULL; 112 | 113 | enet_list_clear (& host -> dispatchQueue); 114 | 115 | for (currentPeer = host -> peers; 116 | currentPeer < & host -> peers [host -> peerCount]; 117 | ++ currentPeer) 118 | { 119 | currentPeer -> host = host; 120 | currentPeer -> incomingPeerID = currentPeer - host -> peers; 121 | currentPeer -> outgoingSessionID = currentPeer -> incomingSessionID = 0xFF; 122 | currentPeer -> data = NULL; 123 | 124 | enet_list_clear (& currentPeer -> acknowledgements); 125 | enet_list_clear (& currentPeer -> sentReliableCommands); 126 | enet_list_clear (& currentPeer -> sentUnreliableCommands); 127 | enet_list_clear (& currentPeer -> outgoingReliableCommands); 128 | enet_list_clear (& currentPeer -> outgoingUnreliableCommands); 129 | enet_list_clear (& currentPeer -> dispatchedCommands); 130 | 131 | enet_peer_reset (currentPeer); 132 | } 133 | 134 | return host; 135 | } 136 | 137 | /** Destroys the host and all resources associated with it. 138 | @param host pointer to the host to destroy 139 | */ 140 | void 141 | enet_host_destroy (ENetHost * host) 142 | { 143 | ENetPeer * currentPeer; 144 | 145 | if (host == NULL) 146 | return; 147 | 148 | enet_socket_destroy (host -> socket); 149 | 150 | for (currentPeer = host -> peers; 151 | currentPeer < & host -> peers [host -> peerCount]; 152 | ++ currentPeer) 153 | { 154 | enet_peer_reset (currentPeer); 155 | } 156 | 157 | if (host -> compressor.context != NULL && host -> compressor.destroy) 158 | (* host -> compressor.destroy) (host -> compressor.context); 159 | 160 | enet_free (host -> peers); 161 | enet_free (host); 162 | } 163 | 164 | /** Initiates a connection to a foreign host. 165 | @param host host seeking the connection 166 | @param address destination for the connection 167 | @param channelCount number of channels to allocate 168 | @param data user data supplied to the receiving host 169 | @returns a peer representing the foreign host on success, NULL on failure 170 | @remarks The peer returned will have not completed the connection until enet_host_service() 171 | notifies of an ENET_EVENT_TYPE_CONNECT event for the peer. 172 | */ 173 | ENetPeer * 174 | enet_host_connect (ENetHost * host, const ENetAddress * address, size_t channelCount, enet_uint32 data) 175 | { 176 | ENetPeer * currentPeer; 177 | ENetChannel * channel; 178 | ENetProtocol command; 179 | 180 | if (channelCount < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT) 181 | channelCount = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT; 182 | else 183 | if (channelCount > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT) 184 | channelCount = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT; 185 | 186 | for (currentPeer = host -> peers; 187 | currentPeer < & host -> peers [host -> peerCount]; 188 | ++ currentPeer) 189 | { 190 | if (currentPeer -> state == ENET_PEER_STATE_DISCONNECTED) 191 | break; 192 | } 193 | 194 | if (currentPeer >= & host -> peers [host -> peerCount]) 195 | return NULL; 196 | 197 | currentPeer -> channels = (ENetChannel *) enet_malloc (channelCount * sizeof (ENetChannel)); 198 | if (currentPeer -> channels == NULL) 199 | return NULL; 200 | currentPeer -> channelCount = channelCount; 201 | currentPeer -> state = ENET_PEER_STATE_CONNECTING; 202 | currentPeer -> address = * address; 203 | currentPeer -> connectID = ++ host -> randomSeed; 204 | 205 | if (host -> outgoingBandwidth == 0) 206 | currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE; 207 | else 208 | currentPeer -> windowSize = (host -> outgoingBandwidth / 209 | ENET_PEER_WINDOW_SIZE_SCALE) * 210 | ENET_PROTOCOL_MINIMUM_WINDOW_SIZE; 211 | 212 | if (currentPeer -> windowSize < ENET_PROTOCOL_MINIMUM_WINDOW_SIZE) 213 | currentPeer -> windowSize = ENET_PROTOCOL_MINIMUM_WINDOW_SIZE; 214 | else 215 | if (currentPeer -> windowSize > ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE) 216 | currentPeer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE; 217 | 218 | for (channel = currentPeer -> channels; 219 | channel < & currentPeer -> channels [channelCount]; 220 | ++ channel) 221 | { 222 | channel -> outgoingReliableSequenceNumber = 0; 223 | channel -> outgoingUnreliableSequenceNumber = 0; 224 | channel -> incomingReliableSequenceNumber = 0; 225 | channel -> incomingUnreliableSequenceNumber = 0; 226 | 227 | enet_list_clear (& channel -> incomingReliableCommands); 228 | enet_list_clear (& channel -> incomingUnreliableCommands); 229 | 230 | channel -> usedReliableWindows = 0; 231 | memset (channel -> reliableWindows, 0, sizeof (channel -> reliableWindows)); 232 | } 233 | 234 | command.header.command = ENET_PROTOCOL_COMMAND_CONNECT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE; 235 | command.header.channelID = 0xFF; 236 | command.connect.outgoingPeerID = ENET_HOST_TO_NET_16 (currentPeer -> incomingPeerID); 237 | command.connect.incomingSessionID = currentPeer -> incomingSessionID; 238 | command.connect.outgoingSessionID = currentPeer -> outgoingSessionID; 239 | command.connect.mtu = ENET_HOST_TO_NET_32 (currentPeer -> mtu); 240 | command.connect.windowSize = ENET_HOST_TO_NET_32 (currentPeer -> windowSize); 241 | command.connect.channelCount = ENET_HOST_TO_NET_32 (channelCount); 242 | command.connect.incomingBandwidth = ENET_HOST_TO_NET_32 (host -> incomingBandwidth); 243 | command.connect.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth); 244 | command.connect.packetThrottleInterval = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleInterval); 245 | command.connect.packetThrottleAcceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleAcceleration); 246 | command.connect.packetThrottleDeceleration = ENET_HOST_TO_NET_32 (currentPeer -> packetThrottleDeceleration); 247 | command.connect.connectID = currentPeer -> connectID; 248 | command.connect.data = ENET_HOST_TO_NET_32 (data); 249 | 250 | enet_peer_queue_outgoing_command (currentPeer, & command, NULL, 0, 0); 251 | 252 | return currentPeer; 253 | } 254 | 255 | /** Queues a packet to be sent to all peers associated with the host. 256 | @param host host on which to broadcast the packet 257 | @param channelID channel on which to broadcast 258 | @param packet packet to broadcast 259 | */ 260 | void 261 | enet_host_broadcast (ENetHost * host, enet_uint8 channelID, ENetPacket * packet) 262 | { 263 | ENetPeer * currentPeer; 264 | 265 | for (currentPeer = host -> peers; 266 | currentPeer < & host -> peers [host -> peerCount]; 267 | ++ currentPeer) 268 | { 269 | if (currentPeer -> state != ENET_PEER_STATE_CONNECTED) 270 | continue; 271 | 272 | enet_peer_send (currentPeer, channelID, packet); 273 | } 274 | 275 | if (packet -> referenceCount == 0) 276 | enet_packet_destroy (packet); 277 | } 278 | 279 | /** Sets the packet compressor the host should use to compress and decompress packets. 280 | @param host host to enable or disable compression for 281 | @param compressor callbacks for for the packet compressor; if NULL, then compression is disabled 282 | */ 283 | void 284 | enet_host_compress (ENetHost * host, const ENetCompressor * compressor) 285 | { 286 | if (host -> compressor.context != NULL && host -> compressor.destroy) 287 | (* host -> compressor.destroy) (host -> compressor.context); 288 | 289 | if (compressor) 290 | host -> compressor = * compressor; 291 | else 292 | host -> compressor.context = NULL; 293 | } 294 | 295 | /** Limits the maximum allowed channels of future incoming connections. 296 | @param host host to limit 297 | @param channelLimit the maximum number of channels allowed; if 0, then this is equivalent to ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT 298 | */ 299 | void 300 | enet_host_channel_limit (ENetHost * host, size_t channelLimit) 301 | { 302 | if (! channelLimit || channelLimit > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT) 303 | channelLimit = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT; 304 | else 305 | if (channelLimit < ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT) 306 | channelLimit = ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT; 307 | 308 | host -> channelLimit = channelLimit; 309 | } 310 | 311 | 312 | /** Adjusts the bandwidth limits of a host. 313 | @param host host to adjust 314 | @param incomingBandwidth new incoming bandwidth 315 | @param outgoingBandwidth new outgoing bandwidth 316 | @remarks the incoming and outgoing bandwidth parameters are identical in function to those 317 | specified in enet_host_create(). 318 | */ 319 | void 320 | enet_host_bandwidth_limit (ENetHost * host, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth) 321 | { 322 | host -> incomingBandwidth = incomingBandwidth; 323 | host -> outgoingBandwidth = outgoingBandwidth; 324 | host -> recalculateBandwidthLimits = 1; 325 | } 326 | 327 | void 328 | enet_host_bandwidth_throttle (ENetHost * host) 329 | { 330 | enet_uint32 timeCurrent = enet_time_get (), 331 | elapsedTime = timeCurrent - host -> bandwidthThrottleEpoch, 332 | peersRemaining = (enet_uint32) host -> connectedPeers, 333 | dataTotal = ~0, 334 | bandwidth = ~0, 335 | throttle = 0, 336 | bandwidthLimit = 0; 337 | int needsAdjustment = host -> bandwidthLimitedPeers > 0 ? 1 : 0; 338 | ENetPeer * peer; 339 | ENetProtocol command; 340 | 341 | if (elapsedTime < ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL) 342 | return; 343 | 344 | host -> bandwidthThrottleEpoch = timeCurrent; 345 | 346 | if (peersRemaining == 0) 347 | return; 348 | 349 | if (host -> outgoingBandwidth != 0) 350 | { 351 | dataTotal = 0; 352 | bandwidth = (host -> outgoingBandwidth * elapsedTime) / 1000; 353 | 354 | for (peer = host -> peers; 355 | peer < & host -> peers [host -> peerCount]; 356 | ++ peer) 357 | { 358 | if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER) 359 | continue; 360 | 361 | dataTotal += peer -> outgoingDataTotal; 362 | } 363 | } 364 | 365 | while (peersRemaining > 0 && needsAdjustment != 0) 366 | { 367 | needsAdjustment = 0; 368 | 369 | if (dataTotal <= bandwidth) 370 | throttle = ENET_PEER_PACKET_THROTTLE_SCALE; 371 | else 372 | throttle = (bandwidth * ENET_PEER_PACKET_THROTTLE_SCALE) / dataTotal; 373 | 374 | for (peer = host -> peers; 375 | peer < & host -> peers [host -> peerCount]; 376 | ++ peer) 377 | { 378 | enet_uint32 peerBandwidth; 379 | 380 | if ((peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER) || 381 | peer -> incomingBandwidth == 0 || 382 | peer -> outgoingBandwidthThrottleEpoch == timeCurrent) 383 | continue; 384 | 385 | peerBandwidth = (peer -> incomingBandwidth * elapsedTime) / 1000; 386 | if ((throttle * peer -> outgoingDataTotal) / ENET_PEER_PACKET_THROTTLE_SCALE <= peerBandwidth) 387 | continue; 388 | 389 | peer -> packetThrottleLimit = (peerBandwidth * 390 | ENET_PEER_PACKET_THROTTLE_SCALE) / peer -> outgoingDataTotal; 391 | 392 | if (peer -> packetThrottleLimit == 0) 393 | peer -> packetThrottleLimit = 1; 394 | 395 | if (peer -> packetThrottle > peer -> packetThrottleLimit) 396 | peer -> packetThrottle = peer -> packetThrottleLimit; 397 | 398 | peer -> outgoingBandwidthThrottleEpoch = timeCurrent; 399 | 400 | peer -> incomingDataTotal = 0; 401 | peer -> outgoingDataTotal = 0; 402 | 403 | needsAdjustment = 1; 404 | -- peersRemaining; 405 | bandwidth -= peerBandwidth; 406 | dataTotal -= peerBandwidth; 407 | } 408 | } 409 | 410 | if (peersRemaining > 0) 411 | { 412 | if (dataTotal <= bandwidth) 413 | throttle = ENET_PEER_PACKET_THROTTLE_SCALE; 414 | else 415 | throttle = (bandwidth * ENET_PEER_PACKET_THROTTLE_SCALE) / dataTotal; 416 | 417 | for (peer = host -> peers; 418 | peer < & host -> peers [host -> peerCount]; 419 | ++ peer) 420 | { 421 | if ((peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER) || 422 | peer -> outgoingBandwidthThrottleEpoch == timeCurrent) 423 | continue; 424 | 425 | peer -> packetThrottleLimit = throttle; 426 | 427 | if (peer -> packetThrottle > peer -> packetThrottleLimit) 428 | peer -> packetThrottle = peer -> packetThrottleLimit; 429 | 430 | peer -> incomingDataTotal = 0; 431 | peer -> outgoingDataTotal = 0; 432 | } 433 | } 434 | 435 | if (host -> recalculateBandwidthLimits) 436 | { 437 | host -> recalculateBandwidthLimits = 0; 438 | 439 | peersRemaining = (enet_uint32) host -> connectedPeers; 440 | bandwidth = host -> incomingBandwidth; 441 | needsAdjustment = 1; 442 | 443 | if (bandwidth == 0) 444 | bandwidthLimit = 0; 445 | else 446 | while (peersRemaining > 0 && needsAdjustment != 0) 447 | { 448 | needsAdjustment = 0; 449 | bandwidthLimit = bandwidth / peersRemaining; 450 | 451 | for (peer = host -> peers; 452 | peer < & host -> peers [host -> peerCount]; 453 | ++ peer) 454 | { 455 | if ((peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER) || 456 | peer -> incomingBandwidthThrottleEpoch == timeCurrent) 457 | continue; 458 | 459 | if (peer -> outgoingBandwidth > 0 && 460 | peer -> outgoingBandwidth >= bandwidthLimit) 461 | continue; 462 | 463 | peer -> incomingBandwidthThrottleEpoch = timeCurrent; 464 | 465 | needsAdjustment = 1; 466 | -- peersRemaining; 467 | bandwidth -= peer -> outgoingBandwidth; 468 | } 469 | } 470 | 471 | for (peer = host -> peers; 472 | peer < & host -> peers [host -> peerCount]; 473 | ++ peer) 474 | { 475 | if (peer -> state != ENET_PEER_STATE_CONNECTED && peer -> state != ENET_PEER_STATE_DISCONNECT_LATER) 476 | continue; 477 | 478 | command.header.command = ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT | ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE; 479 | command.header.channelID = 0xFF; 480 | command.bandwidthLimit.outgoingBandwidth = ENET_HOST_TO_NET_32 (host -> outgoingBandwidth); 481 | 482 | if (peer -> incomingBandwidthThrottleEpoch == timeCurrent) 483 | command.bandwidthLimit.incomingBandwidth = ENET_HOST_TO_NET_32 (peer -> outgoingBandwidth); 484 | else 485 | command.bandwidthLimit.incomingBandwidth = ENET_HOST_TO_NET_32 (bandwidthLimit); 486 | 487 | enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0); 488 | } 489 | } 490 | } 491 | 492 | /** @} */ 493 | -------------------------------------------------------------------------------- /corefunc.h: -------------------------------------------------------------------------------- 1 | #include "proton/variant.hpp" 2 | #include "enet/include/enet.h" 3 | #include 4 | //#include 5 | //#include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "json.hpp" 15 | #include "packet.h" 16 | #include 17 | #define byte uint8_t 18 | #define BYTE uint8_t 19 | 20 | 21 | //This Is The Customization Area 22 | 23 | #pragma once 24 | //Customing GrowID 25 | string Growid_acc = "capcitest1"; 26 | 27 | //Customing Password 28 | string Password_acc = "loler1234@"; 29 | 30 | //Customing Gmail ( At least put a random gmail. Dont add @gmail.com ) 31 | string Gmail_acc = "testgmail"; 32 | 33 | //Customing Start From (Account) 34 | int START_ACC = 1; 35 | 36 | //Customing Start From (Gmail) 37 | int START_GMAIL = 1; 38 | 39 | 40 | 41 | typedef unsigned long DWORD; 42 | typedef unsigned long dword; 43 | using namespace std; 44 | using json = nlohmann::json; 45 | 46 | uint8_t* get_extended(gameupdatepacket_t* packet) { 47 | return reinterpret_cast(&packet->m_data_size); 48 | } 49 | class GrowtopiaBot { 50 | public: 51 | ENetPeer *peer; 52 | ENetHost * client; 53 | int login_user = 0; 54 | int login_token = 0; 55 | string currentWorld; 56 | bool connect = false; 57 | int timeFromWorldEnter = 0; // in 10mss... 58 | string gameVersion = "3.65"; 59 | int owner = -1; 60 | string ownerUsername; 61 | int localX = -1; 62 | int localY = -1; 63 | struct ObjectData 64 | { 65 | int netId = -1; // used to interact with stuff in world 66 | int userId = -1; // growtopia id 67 | string name = ""; 68 | string country = ""; 69 | string objectType = ""; // "avatar" is player creature 70 | float x = -1; 71 | float y = -1; 72 | bool isGone = false; // set true if character left 73 | int rectX; // collision stuff 74 | int rectY; // collision stuff 75 | int rectWidth; // collision stuff 76 | int rectHeight; // collision stuff 77 | bool isMod = false; 78 | bool isLocal = false; 79 | }; 80 | 81 | vector objects; 82 | 83 | string uname; 84 | string upass; 85 | string SERVER_HOST; 86 | int SERVER_PORT; 87 | string worldName; // excepted world name 88 | int follownetid = -1; 89 | bool isFollowing = false; 90 | bool backwardWalk = false; 91 | int respawnX; 92 | int respawnY; 93 | int localid = -1; 94 | struct PlayerMoving { 95 | int packetType; 96 | int netID; 97 | float x; 98 | float y; 99 | int characterState; 100 | int plantingTree; 101 | float XSpeed; 102 | float YSpeed; 103 | int punchX; 104 | int punchY; 105 | 106 | }; 107 | //void userLoop(); 108 | void userInit(); 109 | void onLoginRequested(); 110 | void packet_type3(string text); 111 | void packet_type6(string text); 112 | void packet_unknown(ENetPacket* packet); 113 | void WhenConnected(); 114 | void WhenDisconnected(); 115 | void respawn(); 116 | void dbgPrint(string text) 117 | { 118 | cout << "[DBG] " + text << endl; 119 | } 120 | GrowtopiaBot(string username, string password, string ip, int port) 121 | { 122 | this->uname = username; 123 | this->upass = password; 124 | this->SERVER_HOST = ip; 125 | this->SERVER_PORT = port; 126 | } 127 | 128 | /******************* enet core *********************/ 129 | void SendPacket(int a1, string a2, ENetPeer* enetPeer) 130 | { 131 | if (enetPeer) 132 | { 133 | ENetPacket* v3 = enet_packet_create(0, a2.length() + 5, 1); 134 | memcpy(v3->data, &a1, 4); 135 | //*(v3->data) = (DWORD)a1; 136 | memcpy((v3->data) + 4, a2.c_str(), a2.length()); 137 | 138 | //cout << std::hex << (int)(char)v3->data[3] << endl; 139 | enet_peer_send(enetPeer, 0, v3); 140 | } 141 | } 142 | void SendPacketRaw(int a1, void *packetData, size_t packetDataSize, void *a4, ENetPeer* peer, int packetFlag) 143 | { 144 | ENetPacket *p; 145 | if (peer) // check if we have it setup 146 | { 147 | if (a1 == 4 && *((BYTE *)packetData + 12) & 8) 148 | { 149 | p = enet_packet_create(0, packetDataSize + *((DWORD *)packetData + 13) + 5, packetFlag); 150 | int four = 4; 151 | memcpy(p->data, &four, 4); 152 | memcpy((char *)p->data + 4, packetData, packetDataSize); 153 | memcpy((char *)p->data + packetDataSize + 4, a4, *((DWORD *)packetData + 13)); 154 | enet_peer_send(peer, 0, p); 155 | } 156 | else 157 | { 158 | p = enet_packet_create(0, packetDataSize + 5, packetFlag); 159 | memcpy(p->data, &a1, 4); 160 | memcpy((char *)p->data + 4, packetData, packetDataSize); 161 | enet_peer_send(peer, 0, p); 162 | } 163 | } 164 | } 165 | 166 | // Connect with default value 167 | void connectClient() { 168 | connectClient(SERVER_HOST, SERVER_PORT); 169 | } 170 | 171 | void connectClient(string hostName, int port) 172 | { 173 | //cout << "Connecting " << uname << " to " << hostName << ":" << port << endl; 174 | client = enet_host_create(NULL /* create a client host */, 175 | 1 /* only allow 1 outgoing connection */, 176 | 2 /* allow up 2 channels to be used, 0 and 1 */, 177 | 0 /* 56K modem with 56 Kbps downstream bandwidth */, 178 | 0 /* 56K modem with 14 Kbps upstream bandwidth */); 179 | client->usingNewPacket=true; 180 | if (client == NULL) 181 | { 182 | cout << "An error occurred while trying to create an ENet client host.\n"; 183 | 184 | exit(EXIT_FAILURE); 185 | } 186 | ENetAddress address; 187 | 188 | client->checksum = enet_crc32; 189 | enet_host_compress_with_range_coder(client); 190 | enet_address_set_host(&address, hostName.c_str()); 191 | address.port = port; 192 | peer = enet_host_connect(client, &address, 2, 0); 193 | if (peer == NULL) 194 | { 195 | cout << "No available peers for initiating an ENet connection.\n"; 196 | 197 | exit(EXIT_FAILURE); 198 | } 199 | enet_host_flush(client); 200 | } 201 | void gotow(string name) { 202 | SendPacket(3, "action|join_request\nname|" + name, peer); 203 | } 204 | ENetPeer* getPeer() { 205 | return peer; 206 | } 207 | void packetPeer(int type, string pkt) { 208 | SendPacket(type, pkt, peer); 209 | } 210 | void RequestItemActivate(unsigned int item) 211 | { 212 | BYTE* data = new BYTE[56]; 213 | for (int i = 0; i < 56; i++) 214 | { 215 | data[i] = 0; 216 | } 217 | BYTE ten = 10; 218 | memcpy(data + 0, &ten, 1); 219 | memcpy(data + 20, &item, 1); 220 | SendPacketRaw(4, data, 0x38u, 0, peer, 1); 221 | free(data); 222 | } 223 | void SetAndBroadcastIconState(int netID, int state) 224 | { 225 | BYTE* data = new BYTE[56]; 226 | for (int i = 0; i < 56; i++) 227 | { 228 | data[i] = 0; 229 | } 230 | BYTE eighteen = 18; 231 | memcpy(data + 0, &eighteen, 1); 232 | memcpy(data + 4, &netID, 4); // (a1+40) 233 | memcpy(data + 44, &state, 4); 234 | SendPacketRaw(4, data, 0x38u, 0, peer, 1); 235 | free(data); 236 | } 237 | void SendPing() 238 | { 239 | BYTE* data = new BYTE[56]; 240 | for (int i = 0; i < 56; i++) 241 | { 242 | data[i] = 0; 243 | } 244 | BYTE twentytwo = 22; 245 | memcpy(data + 0, &twentytwo, 1); 246 | SendPacketRaw(4, data, 56, 0, peer, 1); 247 | free(data); 248 | } 249 | int GetMessageTypeFromPacket(ENetPacket* packet) 250 | { 251 | int result; 252 | 253 | if (packet->dataLength > 3u) 254 | { 255 | result = *(packet->data); 256 | } 257 | else 258 | { 259 | cout << "Bad packet length, ignoring message" << endl; 260 | result = 0; 261 | } 262 | return result; 263 | } 264 | char* GetTextPointerFromPacket(ENetPacket* packet) 265 | { 266 | char zero = 0; 267 | memcpy(packet->data + packet->dataLength - 1, &zero, 1); 268 | return (char*)(packet->data + 4); 269 | } 270 | void fixCaptcha(string pkt) { 271 | string gg = explode("\nadd_text_input|captcha_answer|Answer:||32|", explode("add_textbox|What will be the sum of the following numbers|left|\n", pkt)[1])[0]; 272 | string gg2 = explode("|", gg)[1]; 273 | string res = to_string(atoi(explode("+", gg2)[0].c_str()) + atoi(explode("+", gg2)[1].c_str())); 274 | cout << "Solved Captcha: " << res << endl; 275 | SendPacket(2, "action|dialog_return\ndialog_name|captcha_submit\ncaptcha_answer|" + res, peer); 276 | SendPacket(2, "action|input\n|text|Solved Captcha: `2" + res, peer); 277 | } 278 | void appendAcc(string text) { 279 | string filename("acc.txt"); 280 | fstream file; 281 | file.open(filename, std::ios_base::app | std::ios_base::in); 282 | if (file.is_open()) file << text << endl; 283 | } 284 | void decPacket(gameupdatepacket_t* packet) { 285 | if (packet) { 286 | variantlist_t varlist{}; 287 | auto extended = get_extended(packet); 288 | if (extended) { 289 | extended += 4; 290 | varlist.serialize_from_mem(extended); 291 | //cout << "varlist: " << varlist.print() << endl; 292 | auto func = varlist[0].get_string(); 293 | if (func == "OnRequestWorldSelectMenu") { 294 | localX = -1;localY=-1;localid=-1;world->name="EXIT"; 295 | } 296 | else if (func == "OnSendToServer") { 297 | auto port = varlist[1].get_uint32();login_user = varlist[3].get_uint32();login_token = varlist[2].get_uint32();auto str = varlist[4].get_string(); auto doorid = str.substr(str.find("|")); 298 | auto address = str.erase(str.find("|")); 299 | connectClient(address, port); 300 | } 301 | else if (func == "onShowCaptcha") { 302 | auto ctx = varlist[1].get_string(); 303 | if (ctx.find("add_label_with_icon|big|`wAre you Human?``|left|206|") != std::string::npos) fixCaptcha(ctx); 304 | } 305 | else if (func == "OnDialogRequest") { 306 | auto ctx = varlist[1].get_string(); 307 | if (ctx.find("add_label_with_icon|big|`wAre you Human?``|left|206|") != std::string::npos) fixCaptcha(ctx); 308 | if (ctx.find("add_label_with_icon|big|`wGet a GrowID``|left|206|") != std::string::npos) { 309 | SendPacket(2, "action|dialog_return\ndialog_name|growid_apply\nlogon|"+ Growid_acc + to_string(START_ACC) + "\npassword|" + Password_acc + "\npassword_verify|" + Password_acc + "\nemail|"+ Gmail_acc + to_string(START_GMAIL) + "@gmail.com", peer); 310 | START_ACC = START_ACC + 1; 311 | START_GMAIL = START_GMAIL + 1; 312 | } 313 | else if (ctx.find("add_label_with_icon|big|`wGrowID GET!``|left|206|") != std::string::npos) { 314 | string growid = explode("`` created. Write it and your password", explode("add_textbox|A `wGrowID`` with the log on of `w", ctx)[1])[0]; 315 | string passwr = ""+ Password_acc; 316 | cout << "Account succesfuly created! GrowID: " << growid << ",Password: " << Password_acc << endl; 317 | appendAcc("User: " + growid + ",Password: " + Password_acc); 318 | enet_peer_disconnect_later(peer, 0); 319 | } 320 | //cout << ctx << endl; 321 | } 322 | else if (func == "OnSpawn") { 323 | auto ctx = varlist[1].get_string(); 324 | if (ctx.find("type|local") != std::string::npos) { 325 | string net = explode("\n", explode("netID|", ctx)[1])[0]; 326 | auto gg = explode("|", explode("\n", explode("posXY|", ctx)[1])[0]);//posXY|320|736 327 | int netid = atoi(net.c_str()); 328 | localid = netid;localX = atoi(gg[0].c_str());localY=atoi(gg[1].c_str()); 329 | //cout << uname << " Local netid: " << localid << " X: " << localX << " Y: " << localY << endl; 330 | } 331 | } 332 | } 333 | } 334 | } 335 | class EntityComponent { 336 | }; 337 | BYTE* GetExtendedDataPointerFromTankPacket(BYTE* a1) 338 | { 339 | return (BYTE*)((intptr_t)(a1 + 56)); 340 | } 341 | struct WorldThingStruct 342 | { 343 | }; 344 | 345 | struct WorldStruct 346 | { 347 | int XSize; 348 | int YSize; 349 | int tileCount; 350 | string name; 351 | __int16_t* foreground; 352 | __int16_t* background; 353 | WorldThingStruct* specials; 354 | }; 355 | BYTE* packPlayerMoving(PlayerMoving* dataStruct) 356 | { 357 | try 358 | { 359 | BYTE* data = new BYTE[56]; 360 | for (int i = 0; i < 56; i++) 361 | { 362 | data[i] = 0; 363 | } 364 | memcpy(data, &dataStruct->packetType, 4); 365 | memcpy(data + 4, &dataStruct->netID, 4); 366 | memcpy(data + 12, &dataStruct->characterState, 4); 367 | memcpy(data + 20, &dataStruct->plantingTree, 4); 368 | memcpy(data + 24, &dataStruct->x, 4); 369 | memcpy(data + 28, &dataStruct->y, 4); 370 | memcpy(data + 32, &dataStruct->XSpeed, 4); 371 | memcpy(data + 36, &dataStruct->YSpeed, 4); 372 | memcpy(data + 44, &dataStruct->punchX, 4); 373 | memcpy(data + 48, &dataStruct->punchY, 4); 374 | return data; 375 | } 376 | catch(exception& e) { 377 | return NULL; 378 | } 379 | } 380 | 381 | PlayerMoving* unpackPlayerMoving(BYTE* data) 382 | { 383 | try 384 | { 385 | PlayerMoving* dataStruct = new PlayerMoving; 386 | memcpy(&dataStruct->packetType, data, 4); 387 | memcpy(&dataStruct->netID, data + 4, 4); 388 | memcpy(&dataStruct->characterState, data + 12, 4); 389 | memcpy(&dataStruct->plantingTree, data + 20, 4); 390 | memcpy(&dataStruct->x, data + 24, 4); 391 | memcpy(&dataStruct->y, data + 28, 4); 392 | memcpy(&dataStruct->XSpeed, data + 32, 4); 393 | memcpy(&dataStruct->YSpeed, data + 36, 4); 394 | memcpy(&dataStruct->punchX, data + 44, 4); 395 | memcpy(&dataStruct->punchY, data + 48, 4); 396 | return dataStruct; 397 | } 398 | catch (...) { 399 | return NULL; 400 | } 401 | } 402 | WorldStruct* world = NULL; 403 | gameupdatepacket_t* get_struct(ENetPacket* packet) { 404 | if (packet->dataLength < sizeof(gameupdatepacket_t) - 4) 405 | return nullptr; 406 | gametankpacket_t* tank = reinterpret_cast(packet->data); 407 | gameupdatepacket_t* gamepacket = reinterpret_cast(packet->data + 4); 408 | if (gamepacket->m_packet_flags & 8) { 409 | if (packet->dataLength < gamepacket->m_data_size + 60) { 410 | printf("got invalid packet. (too small)\n"); 411 | return nullptr; 412 | } 413 | return reinterpret_cast(&tank->m_data); 414 | } else 415 | gamepacket->m_data_size = 0; 416 | return gamepacket; 417 | } 418 | void sendPunch(int x, int y,int x2, int y2, int net) { 419 | PlayerMoving data; 420 | data.packetType = 0x3; 421 | data.x = x2; 422 | data.y = y2; 423 | data.punchX = x; 424 | data.punchY = y; 425 | data.netID = net; 426 | data.plantingTree = 18; 427 | auto datas = packPlayerMoving(&data); 428 | SendPacketRaw(4, datas, 56, 0, peer, ENET_PACKET_FLAG_RELIABLE); 429 | } 430 | void useTile(int x, int y, int netid) { 431 | PlayerMoving data; 432 | data.packetType = 7; 433 | data.x = x*32; 434 | data.y = y*32; 435 | data.punchX = x; 436 | data.punchY = y; 437 | data.netID = netid; 438 | auto datas = packPlayerMoving(&data); 439 | if (peer) SendPacketRaw(4, datas, 56, 0, peer, ENET_PACKET_FLAG_RELIABLE); 440 | } 441 | void ProcessTankUpdatePacket(float someVal, EntityComponent* entityComponent, BYTE* structPointer, ENetPacket* packets) 442 | { 443 | //cout << "Processing tank packet with id of: " << +(*(char*)structPointer) << " Where first byte is " << std::to_string(structPointer[0]) << endl; 444 | auto ptype = *(char*)structPointer; 445 | switch (*(char*)structPointer) 446 | { 447 | case 1: 448 | { 449 | try { 450 | auto packet = get_struct(packets); 451 | decPacket(packet); 452 | //SerializeFromMem(GetExtendedDataPointerFromTankPacket(structPointer), *(int*)(structPointer + 52), 0, *(int*)(structPointer + 4)); 453 | } 454 | catch (int e) 455 | { 456 | 457 | } 458 | break; 459 | } 460 | case 7: { 461 | PlayerMoving* datak = unpackPlayerMoving(structPointer); 462 | cout << "Type 7: " << datak->netID << endl; 463 | break; 464 | } 465 | case 0x10: 466 | { 467 | BYTE* itemsData = GetExtendedDataPointerFromTankPacket(structPointer); 468 | __int16_t val1 = *(__int16_t*)itemsData; 469 | int itemsCount = *(int*)(itemsData + 2); 470 | break; 471 | } 472 | case 0x16: 473 | { 474 | PlayerMoving* datak = unpackPlayerMoving(structPointer); 475 | datak->packetType = 21; 476 | SendPacketRaw(4, packPlayerMoving(datak), 56, 0, peer, ENET_PACKET_FLAG_RELIABLE); 477 | break; 478 | // SendPacketRaw(4, &v205, 0x38u, 0, peer, 1); 479 | } 480 | break; 481 | case 0x12: 482 | { 483 | break; 484 | } 485 | case 0x14: 486 | { 487 | break; 488 | } 489 | case 0xC: 490 | { 491 | string x = std::to_string(*(int*)(structPointer + 44)); 492 | string y = std::to_string(*(int*)(structPointer + 48)); 493 | //SendPacket(2, "action|input\n|text|x: " + x + ", y: " + y, peer); 494 | break; 495 | } 496 | case 0xE: 497 | { 498 | } 499 | case 0x23u: 500 | break; 501 | case 3: 502 | break; 503 | case 4: 504 | { 505 | BYTE* worldPtr = GetExtendedDataPointerFromTankPacket(structPointer); // World::LoadFromMem 506 | world = new WorldStruct; 507 | worldPtr += 6; 508 | __int16_t strlen = *(__int16_t*)worldPtr; 509 | worldPtr += 2; 510 | world->name = ""; 511 | for (int i = 0; i < strlen; i++) 512 | { 513 | world->name += worldPtr[0]; 514 | worldPtr++; 515 | } 516 | world->XSize = *(int*)worldPtr; 517 | worldPtr += 4; 518 | world->YSize = *(int*)worldPtr; 519 | worldPtr += 4; 520 | world->tileCount = *(int*)worldPtr; 521 | worldPtr += 4; 522 | world->foreground = (__int16_t*)malloc(world->tileCount * sizeof(__int16_t)); 523 | world->background = (__int16_t*)malloc(world->tileCount * sizeof(__int16_t)); 524 | 525 | for (int i = 0; i < world->tileCount; i++) 526 | { 527 | world->foreground[i] = *(__int16_t*)worldPtr; 528 | //if (i<100) 529 | //cout << std::hex << *(__int16_t*)worldPtr << " "; 530 | worldPtr += 2; 531 | world->background[i] = *(__int16_t*)worldPtr; 532 | worldPtr += 2; 533 | if (*(BYTE*)worldPtr == 0xae) // locked area 534 | { 535 | worldPtr += 2; //10 // 0 4 6 8 10 536 | } else if (world->foreground[i] == 0xca) // small lock 537 | { 538 | worldPtr += 10; 539 | } 540 | else if (world->foreground[i] == 242) // world lock 541 | { 542 | worldPtr += 30; // 12 543 | } 544 | else if (world->foreground[i] == 4802) // crown lock? 545 | { 546 | worldPtr += 14; 547 | } 548 | else if (world->foreground[i] == 6) // main door 549 | { 550 | worldPtr += (*(__int16_t*)(worldPtr + 5)) + 4; 551 | } 552 | else if (world->foreground[i] == 20) // sign 553 | { 554 | worldPtr += (*(__int16_t*)(worldPtr + 5)) + 4 + 3; 555 | } 556 | else if (world->foreground[i] == 28) // danger sign // TODO 557 | { 558 | worldPtr += (*(__int16_t*)(worldPtr + 5)) + 4 + 3; 559 | } 560 | else if (world->foreground[i] == 1682) // gateway to adventure 561 | { 562 | worldPtr += (*(__int16_t*)(worldPtr + 5)) + 4; 563 | } 564 | else if (world->foreground[i] == 858) // screen door 565 | { 566 | worldPtr += (*(__int16_t*)(worldPtr + 5)) + 4; 567 | } 568 | else if (world->foreground[i] == 382) // time space rupture 569 | { 570 | worldPtr += (*(__int16_t*)(worldPtr + 5)) + 4; 571 | } 572 | else if (world->foreground[i] == 546) worldPtr += (*(__int16_t*)(worldPtr + 5)) + 4; 573 | else if (world->foreground[i] == 12) worldPtr += (*(__int16_t*)(worldPtr + 5)) + 4; 574 | else if (world->foreground[i] == 3808) worldPtr += 1; 575 | else if (world->foreground[i] == 3804) // challenge timer 576 | { 577 | worldPtr += 1; 578 | } 579 | else if (world->foreground[i] == 3806) // challenge start flag 580 | { 581 | worldPtr += 1; 582 | } 583 | else if (world->foreground[i] == 658) // bulletin board 584 | { 585 | worldPtr += 7; 586 | } 587 | else if (world->foreground[i] == 1684) // path marker 588 | { 589 | worldPtr += 7; 590 | } 591 | else if (world->foreground[i] == 3760) // data bedrock 592 | { 593 | worldPtr += 2; 594 | } 595 | else if (world->foreground[i] == 1420) // mannequin 596 | { 597 | worldPtr += 26; 598 | } 599 | else if (world->foreground[i] % 2) { // seeds 600 | worldPtr += 6; 601 | } 602 | else if (0 != *(BYTE*)worldPtr) 603 | { 604 | 605 | } 606 | worldPtr += 4; 607 | } 608 | //cout << "[" << uname << "] World " + std::to_string(world->XSize) + "x" + std::to_string(world->YSize) + " with name " + world->name << endl; 609 | if (world->name.find("TUTORIAL_1") != std::string::npos) SendPacket(2, "action|growid", peer); 610 | //if (world->name != currentWorld) SendPacket(3, "action|join_request\nname|" + currentWorld, peer); 611 | //if (world->name == currentWorld) cout << uname << " has been landed in " << currentWorld << endl; 612 | //currentWorld = world->name; 613 | break; 614 | } 615 | case 0: // AvatarPacketReceiver::LerpState 616 | { 617 | PlayerMoving* datak = unpackPlayerMoving(structPointer); 618 | if (datak->packetType == 0 && datak->netID == follownetid && follownetid != -1) { 619 | if (datak->punchX != -1 && datak->punchY != -1) sendPunch(datak->punchX, datak->punchY, datak->x, datak->y, datak->netID); 620 | SendPacketRaw(4, packPlayerMoving(datak), 56, 0, peer, ENET_PACKET_FLAG_RELIABLE); 621 | } 622 | if (datak->packetType == 0 && datak->netID == localid) { 623 | localX = datak->x; 624 | localY = datak->y; 625 | } 626 | 627 | free(datak); 628 | break; 629 | } 630 | default: 631 | break; 632 | } 633 | 634 | } 635 | 636 | BYTE* GetStructPointerFromTankPacket(ENetPacket* packet) 637 | { 638 | unsigned int packetLenght = packet->dataLength; 639 | BYTE* result = NULL; 640 | if (packetLenght >= 0x3C) 641 | { 642 | BYTE* packetData = packet->data; 643 | result = packetData + 4; 644 | 645 | if (*(BYTE*)(packetData + 16) & 8) 646 | { 647 | if (packetLenght < *(int*)(packetData + 56) + 60) 648 | { 649 | result = 0; 650 | } 651 | } 652 | else 653 | { 654 | int zero = 0; 655 | memcpy(packetData + 56, &zero, 4); 656 | } 657 | } 658 | return result; 659 | } 660 | 661 | void ProcessPacket(ENetEvent* event, ENetPeer* peer) 662 | { 663 | int messageType = GetMessageTypeFromPacket(event->packet); 664 | //cout << "Packet type is " << messageType << endl; 665 | //cout << (event->packet->data+4) << endl; 666 | switch (messageType) { 667 | case 1: 668 | onLoginRequested(); 669 | break; 670 | default: 671 | packet_unknown(event->packet); 672 | break; 673 | case 3: 674 | packet_type3(GetTextPointerFromPacket(event->packet)); 675 | break; 676 | case 4: 677 | { 678 | BYTE* tankUpdatePacket = GetStructPointerFromTankPacket(event->packet); 679 | if (tankUpdatePacket) 680 | { 681 | ProcessTankUpdatePacket(0, NULL, tankUpdatePacket, event->packet); 682 | } 683 | else { 684 | cout << "Got bad tank packet"; 685 | } 686 | } 687 | break; 688 | case 5: 689 | break; 690 | case 6: 691 | packet_type6(GetTextPointerFromPacket(event->packet)); 692 | break; 693 | } 694 | } 695 | 696 | void eventLoop() 697 | { 698 | ENetEvent event; 699 | while (enet_host_service(client, &event, 0) > 0) 700 | { 701 | switch (event.type) 702 | { 703 | case ENET_EVENT_TYPE_NONE: 704 | cout << "No event???" << endl; 705 | break; 706 | case ENET_EVENT_TYPE_CONNECT: 707 | WhenConnected(); 708 | break; 709 | case ENET_EVENT_TYPE_DISCONNECT: 710 | WhenDisconnected(); 711 | break; 712 | case ENET_EVENT_TYPE_RECEIVE: 713 | ProcessPacket(&event, peer); 714 | enet_packet_destroy(event.packet); 715 | break; 716 | default: 717 | cout << "WTF???" << endl; 718 | break; 719 | } 720 | } 721 | //userLoop(); 722 | } 723 | }; 724 | void init() { 725 | if (enet_initialize() != 0) { 726 | fprintf(stderr, "An error occurred while initializing ENet.\n"); 727 | exit(0); 728 | } 729 | atexit(enet_deinitialize); 730 | srand(time(NULL)); 731 | } 732 | -------------------------------------------------------------------------------- /enet/compress.c: -------------------------------------------------------------------------------- 1 | /** 2 | @file compress.c 3 | @brief An adaptive order-2 PPM range coder 4 | */ 5 | #define ENET_BUILDING_LIB 1 6 | #include 7 | #include "include/enet.h" 8 | 9 | typedef struct _ENetSymbol 10 | { 11 | /* binary indexed tree of symbols */ 12 | enet_uint8 value; 13 | enet_uint8 count; 14 | enet_uint16 under; 15 | enet_uint16 left, right; 16 | 17 | /* context defined by this symbol */ 18 | enet_uint16 symbols; 19 | enet_uint16 escapes; 20 | enet_uint16 total; 21 | enet_uint16 parent; 22 | } ENetSymbol; 23 | 24 | /* adaptation constants tuned aggressively for small packet sizes rather than large file compression */ 25 | enum 26 | { 27 | ENET_RANGE_CODER_TOP = 1<<24, 28 | ENET_RANGE_CODER_BOTTOM = 1<<16, 29 | 30 | ENET_CONTEXT_SYMBOL_DELTA = 3, 31 | ENET_CONTEXT_SYMBOL_MINIMUM = 1, 32 | ENET_CONTEXT_ESCAPE_MINIMUM = 1, 33 | 34 | ENET_SUBCONTEXT_ORDER = 2, 35 | ENET_SUBCONTEXT_SYMBOL_DELTA = 2, 36 | ENET_SUBCONTEXT_ESCAPE_DELTA = 5 37 | }; 38 | 39 | /* context exclusion roughly halves compression speed, so disable for now */ 40 | #undef ENET_CONTEXT_EXCLUSION 41 | 42 | typedef struct _ENetRangeCoder 43 | { 44 | /* only allocate enough symbols for reasonable MTUs, would need to be larger for large file compression */ 45 | ENetSymbol symbols[4096]; 46 | } ENetRangeCoder; 47 | 48 | void * 49 | enet_range_coder_create (void) 50 | { 51 | ENetRangeCoder * rangeCoder = (ENetRangeCoder *) enet_malloc (sizeof (ENetRangeCoder)); 52 | if (rangeCoder == NULL) 53 | return NULL; 54 | 55 | return rangeCoder; 56 | } 57 | 58 | void 59 | enet_range_coder_destroy (void * context) 60 | { 61 | ENetRangeCoder * rangeCoder = (ENetRangeCoder *) context; 62 | if (rangeCoder == NULL) 63 | return; 64 | 65 | enet_free (rangeCoder); 66 | } 67 | 68 | #define ENET_SYMBOL_CREATE(symbol, value_, count_) \ 69 | { \ 70 | symbol = & rangeCoder -> symbols [nextSymbol ++]; \ 71 | symbol -> value = value_; \ 72 | symbol -> count = count_; \ 73 | symbol -> under = count_; \ 74 | symbol -> left = 0; \ 75 | symbol -> right = 0; \ 76 | symbol -> symbols = 0; \ 77 | symbol -> escapes = 0; \ 78 | symbol -> total = 0; \ 79 | symbol -> parent = 0; \ 80 | } 81 | 82 | #define ENET_CONTEXT_CREATE(context, escapes_, minimum) \ 83 | { \ 84 | ENET_SYMBOL_CREATE (context, 0, 0); \ 85 | (context) -> escapes = escapes_; \ 86 | (context) -> total = escapes_ + 256*minimum; \ 87 | (context) -> symbols = 0; \ 88 | } 89 | 90 | static enet_uint16 91 | enet_symbol_rescale (ENetSymbol * symbol) 92 | { 93 | enet_uint16 total = 0; 94 | for (;;) 95 | { 96 | symbol -> count -= symbol->count >> 1; 97 | symbol -> under = symbol -> count; 98 | if (symbol -> left) 99 | symbol -> under += enet_symbol_rescale (symbol + symbol -> left); 100 | total += symbol -> under; 101 | if (! symbol -> right) break; 102 | symbol += symbol -> right; 103 | } 104 | return total; 105 | } 106 | 107 | #define ENET_CONTEXT_RESCALE(context, minimum) \ 108 | { \ 109 | (context) -> total = (context) -> symbols ? enet_symbol_rescale ((context) + (context) -> symbols) : 0; \ 110 | (context) -> escapes -= (context) -> escapes >> 1; \ 111 | (context) -> total += (context) -> escapes + 256*minimum; \ 112 | } 113 | 114 | #define ENET_RANGE_CODER_OUTPUT(value) \ 115 | { \ 116 | if (outData >= outEnd) \ 117 | return 0; \ 118 | * outData ++ = value; \ 119 | } 120 | 121 | #define ENET_RANGE_CODER_ENCODE(under, count, total) \ 122 | { \ 123 | encodeRange /= (total); \ 124 | encodeLow += (under) * encodeRange; \ 125 | encodeRange *= (count); \ 126 | for (;;) \ 127 | { \ 128 | if((encodeLow ^ (encodeLow + encodeRange)) >= ENET_RANGE_CODER_TOP) \ 129 | { \ 130 | if(encodeRange >= ENET_RANGE_CODER_BOTTOM) break; \ 131 | encodeRange = -encodeLow & (ENET_RANGE_CODER_BOTTOM - 1); \ 132 | } \ 133 | ENET_RANGE_CODER_OUTPUT (encodeLow >> 24); \ 134 | encodeRange <<= 8; \ 135 | encodeLow <<= 8; \ 136 | } \ 137 | } 138 | 139 | #define ENET_RANGE_CODER_FLUSH \ 140 | { \ 141 | while (encodeLow) \ 142 | { \ 143 | ENET_RANGE_CODER_OUTPUT (encodeLow >> 24); \ 144 | encodeLow <<= 8; \ 145 | } \ 146 | } 147 | 148 | #define ENET_RANGE_CODER_FREE_SYMBOLS \ 149 | { \ 150 | if (nextSymbol >= sizeof (rangeCoder -> symbols) / sizeof (ENetSymbol) - ENET_SUBCONTEXT_ORDER ) \ 151 | { \ 152 | nextSymbol = 0; \ 153 | ENET_CONTEXT_CREATE (root, ENET_CONTEXT_ESCAPE_MINIMUM, ENET_CONTEXT_SYMBOL_MINIMUM); \ 154 | predicted = 0; \ 155 | order = 0; \ 156 | } \ 157 | } 158 | 159 | #define ENET_CONTEXT_ENCODE(context, symbol_, value_, under_, count_, update, minimum) \ 160 | { \ 161 | under_ = value*minimum; \ 162 | count_ = minimum; \ 163 | if (! (context) -> symbols) \ 164 | { \ 165 | ENET_SYMBOL_CREATE (symbol_, value_, update); \ 166 | (context) -> symbols = symbol_ - (context); \ 167 | } \ 168 | else \ 169 | { \ 170 | ENetSymbol * node = (context) + (context) -> symbols; \ 171 | for (;;) \ 172 | { \ 173 | if (value_ < node -> value) \ 174 | { \ 175 | node -> under += update; \ 176 | if (node -> left) { node += node -> left; continue; } \ 177 | ENET_SYMBOL_CREATE (symbol_, value_, update); \ 178 | node -> left = symbol_ - node; \ 179 | } \ 180 | else \ 181 | if (value_ > node -> value) \ 182 | { \ 183 | under_ += node -> under; \ 184 | if (node -> right) { node += node -> right; continue; } \ 185 | ENET_SYMBOL_CREATE (symbol_, value_, update); \ 186 | node -> right = symbol_ - node; \ 187 | } \ 188 | else \ 189 | { \ 190 | count_ += node -> count; \ 191 | under_ += node -> under - node -> count; \ 192 | node -> under += update; \ 193 | node -> count += update; \ 194 | symbol_ = node; \ 195 | } \ 196 | break; \ 197 | } \ 198 | } \ 199 | } 200 | 201 | #ifdef ENET_CONTEXT_EXCLUSION 202 | static const ENetSymbol emptyContext = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 203 | 204 | #define ENET_CONTEXT_WALK(context, body) \ 205 | { \ 206 | const ENetSymbol * node = (context) + (context) -> symbols; \ 207 | const ENetSymbol * stack [256]; \ 208 | size_t stackSize = 0; \ 209 | while (node -> left) \ 210 | { \ 211 | stack [stackSize ++] = node; \ 212 | node += node -> left; \ 213 | } \ 214 | for (;;) \ 215 | { \ 216 | body; \ 217 | if (node -> right) \ 218 | { \ 219 | node += node -> right; \ 220 | while (node -> left) \ 221 | { \ 222 | stack [stackSize ++] = node; \ 223 | node += node -> left; \ 224 | } \ 225 | } \ 226 | else \ 227 | if (stackSize <= 0) \ 228 | break; \ 229 | else \ 230 | node = stack [-- stackSize]; \ 231 | } \ 232 | } 233 | 234 | #define ENET_CONTEXT_ENCODE_EXCLUDE(context, value_, under, total, minimum) \ 235 | ENET_CONTEXT_WALK(context, { \ 236 | if (node -> value != value_) \ 237 | { \ 238 | enet_uint16 parentCount = rangeCoder -> symbols [node -> parent].count + minimum; \ 239 | if (node -> value < value_) \ 240 | under -= parentCount; \ 241 | total -= parentCount; \ 242 | } \ 243 | }) 244 | #endif 245 | 246 | size_t 247 | enet_range_coder_compress (void * context, const ENetBuffer * inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 * outData, size_t outLimit) 248 | { 249 | ENetRangeCoder * rangeCoder = (ENetRangeCoder *) context; 250 | enet_uint8 * outStart = outData, * outEnd = & outData [outLimit]; 251 | const enet_uint8 * inData, * inEnd; 252 | enet_uint32 encodeLow = 0, encodeRange = ~0; 253 | ENetSymbol * root; 254 | enet_uint16 predicted = 0; 255 | size_t order = 0, nextSymbol = 0; 256 | 257 | if (rangeCoder == NULL || inBufferCount <= 0 || inLimit <= 0) 258 | return 0; 259 | 260 | inData = (const enet_uint8 *) inBuffers -> data; 261 | inEnd = & inData [inBuffers -> dataLength]; 262 | inBuffers ++; 263 | inBufferCount --; 264 | 265 | ENET_CONTEXT_CREATE (root, ENET_CONTEXT_ESCAPE_MINIMUM, ENET_CONTEXT_SYMBOL_MINIMUM); 266 | 267 | for (;;) 268 | { 269 | ENetSymbol * subcontext, * symbol; 270 | #ifdef ENET_CONTEXT_EXCLUSION 271 | const ENetSymbol * childContext = & emptyContext; 272 | #endif 273 | enet_uint8 value; 274 | enet_uint16 count, under, * parent = & predicted, total; 275 | if (inData >= inEnd) 276 | { 277 | if (inBufferCount <= 0) 278 | break; 279 | inData = (const enet_uint8 *) inBuffers -> data; 280 | inEnd = & inData [inBuffers -> dataLength]; 281 | inBuffers ++; 282 | inBufferCount --; 283 | } 284 | value = * inData ++; 285 | 286 | for (subcontext = & rangeCoder -> symbols [predicted]; 287 | subcontext != root; 288 | #ifdef ENET_CONTEXT_EXCLUSION 289 | childContext = subcontext, 290 | #endif 291 | subcontext = & rangeCoder -> symbols [subcontext -> parent]) 292 | { 293 | ENET_CONTEXT_ENCODE (subcontext, symbol, value, under, count, ENET_SUBCONTEXT_SYMBOL_DELTA, 0); 294 | * parent = symbol - rangeCoder -> symbols; 295 | parent = & symbol -> parent; 296 | total = subcontext -> total; 297 | #ifdef ENET_CONTEXT_EXCLUSION 298 | if (childContext -> total > ENET_SUBCONTEXT_SYMBOL_DELTA + ENET_SUBCONTEXT_ESCAPE_DELTA) 299 | ENET_CONTEXT_ENCODE_EXCLUDE (childContext, value, under, total, 0); 300 | #endif 301 | if (count > 0) 302 | { 303 | ENET_RANGE_CODER_ENCODE (subcontext -> escapes + under, count, total); 304 | } 305 | else 306 | { 307 | if (subcontext -> escapes > 0 && subcontext -> escapes < total) 308 | ENET_RANGE_CODER_ENCODE (0, subcontext -> escapes, total); 309 | subcontext -> escapes += ENET_SUBCONTEXT_ESCAPE_DELTA; 310 | subcontext -> total += ENET_SUBCONTEXT_ESCAPE_DELTA; 311 | } 312 | subcontext -> total += ENET_SUBCONTEXT_SYMBOL_DELTA; 313 | if (count > 0xFF - 2*ENET_SUBCONTEXT_SYMBOL_DELTA || subcontext -> total > ENET_RANGE_CODER_BOTTOM - 0x100) 314 | ENET_CONTEXT_RESCALE (subcontext, 0); 315 | if (count > 0) goto nextInput; 316 | } 317 | 318 | ENET_CONTEXT_ENCODE (root, symbol, value, under, count, ENET_CONTEXT_SYMBOL_DELTA, ENET_CONTEXT_SYMBOL_MINIMUM); 319 | * parent = symbol - rangeCoder -> symbols; 320 | parent = & symbol -> parent; 321 | total = root -> total; 322 | #ifdef ENET_CONTEXT_EXCLUSION 323 | if (childContext -> total > ENET_SUBCONTEXT_SYMBOL_DELTA + ENET_SUBCONTEXT_ESCAPE_DELTA) 324 | ENET_CONTEXT_ENCODE_EXCLUDE (childContext, value, under, total, ENET_CONTEXT_SYMBOL_MINIMUM); 325 | #endif 326 | ENET_RANGE_CODER_ENCODE (root -> escapes + under, count, total); 327 | root -> total += ENET_CONTEXT_SYMBOL_DELTA; 328 | if (count > 0xFF - 2*ENET_CONTEXT_SYMBOL_DELTA + ENET_CONTEXT_SYMBOL_MINIMUM || root -> total > ENET_RANGE_CODER_BOTTOM - 0x100) 329 | ENET_CONTEXT_RESCALE (root, ENET_CONTEXT_SYMBOL_MINIMUM); 330 | 331 | nextInput: 332 | if (order >= ENET_SUBCONTEXT_ORDER) 333 | predicted = rangeCoder -> symbols [predicted].parent; 334 | else 335 | order ++; 336 | ENET_RANGE_CODER_FREE_SYMBOLS; 337 | } 338 | 339 | ENET_RANGE_CODER_FLUSH; 340 | 341 | return (size_t) (outData - outStart); 342 | } 343 | 344 | #define ENET_RANGE_CODER_SEED \ 345 | { \ 346 | if (inData < inEnd) decodeCode |= * inData ++ << 24; \ 347 | if (inData < inEnd) decodeCode |= * inData ++ << 16; \ 348 | if (inData < inEnd) decodeCode |= * inData ++ << 8; \ 349 | if (inData < inEnd) decodeCode |= * inData ++; \ 350 | } 351 | 352 | #define ENET_RANGE_CODER_READ(total) ((decodeCode - decodeLow) / (decodeRange /= (total))) 353 | 354 | #define ENET_RANGE_CODER_DECODE(under, count, total) \ 355 | { \ 356 | decodeLow += (under) * decodeRange; \ 357 | decodeRange *= (count); \ 358 | for (;;) \ 359 | { \ 360 | if((decodeLow ^ (decodeLow + decodeRange)) >= ENET_RANGE_CODER_TOP) \ 361 | { \ 362 | if(decodeRange >= ENET_RANGE_CODER_BOTTOM) break; \ 363 | decodeRange = -decodeLow & (ENET_RANGE_CODER_BOTTOM - 1); \ 364 | } \ 365 | decodeCode <<= 8; \ 366 | if (inData < inEnd) \ 367 | decodeCode |= * inData ++; \ 368 | decodeRange <<= 8; \ 369 | decodeLow <<= 8; \ 370 | } \ 371 | } 372 | 373 | #define ENET_CONTEXT_DECODE(context, symbol_, code, value_, under_, count_, update, minimum, createRoot, visitNode, createRight, createLeft) \ 374 | { \ 375 | under_ = 0; \ 376 | count_ = minimum; \ 377 | if (! (context) -> symbols) \ 378 | { \ 379 | createRoot; \ 380 | } \ 381 | else \ 382 | { \ 383 | ENetSymbol * node = (context) + (context) -> symbols; \ 384 | for (;;) \ 385 | { \ 386 | enet_uint16 after = under_ + node -> under + (node -> value + 1)*minimum, before = node -> count + minimum; \ 387 | visitNode; \ 388 | if (code >= after) \ 389 | { \ 390 | under_ += node -> under; \ 391 | if (node -> right) { node += node -> right; continue; } \ 392 | createRight; \ 393 | } \ 394 | else \ 395 | if (code < after - before) \ 396 | { \ 397 | node -> under += update; \ 398 | if (node -> left) { node += node -> left; continue; } \ 399 | createLeft; \ 400 | } \ 401 | else \ 402 | { \ 403 | value_ = node -> value; \ 404 | count_ += node -> count; \ 405 | under_ = after - before; \ 406 | node -> under += update; \ 407 | node -> count += update; \ 408 | symbol_ = node; \ 409 | } \ 410 | break; \ 411 | } \ 412 | } \ 413 | } 414 | 415 | #define ENET_CONTEXT_TRY_DECODE(context, symbol_, code, value_, under_, count_, update, minimum, exclude) \ 416 | ENET_CONTEXT_DECODE (context, symbol_, code, value_, under_, count_, update, minimum, return 0, exclude (node -> value, after, before), return 0, return 0) 417 | 418 | #define ENET_CONTEXT_ROOT_DECODE(context, symbol_, code, value_, under_, count_, update, minimum, exclude) \ 419 | ENET_CONTEXT_DECODE (context, symbol_, code, value_, under_, count_, update, minimum, \ 420 | { \ 421 | value_ = code / minimum; \ 422 | under_ = code - code%minimum; \ 423 | ENET_SYMBOL_CREATE (symbol_, value_, update); \ 424 | (context) -> symbols = symbol_ - (context); \ 425 | }, \ 426 | exclude (node -> value, after, before), \ 427 | { \ 428 | value_ = node->value + 1 + (code - after)/minimum; \ 429 | under_ = code - (code - after)%minimum; \ 430 | ENET_SYMBOL_CREATE (symbol_, value_, update); \ 431 | node -> right = symbol_ - node; \ 432 | }, \ 433 | { \ 434 | value_ = node->value - 1 - (after - before - code - 1)/minimum; \ 435 | under_ = code - (after - before - code - 1)%minimum; \ 436 | ENET_SYMBOL_CREATE (symbol_, value_, update); \ 437 | node -> left = symbol_ - node; \ 438 | }) \ 439 | 440 | #ifdef ENET_CONTEXT_EXCLUSION 441 | typedef struct _ENetExclude 442 | { 443 | enet_uint8 value; 444 | enet_uint16 under; 445 | } ENetExclude; 446 | 447 | #define ENET_CONTEXT_DECODE_EXCLUDE(context, total, minimum) \ 448 | { \ 449 | enet_uint16 under = 0; \ 450 | nextExclude = excludes; \ 451 | ENET_CONTEXT_WALK (context, { \ 452 | under += rangeCoder -> symbols [node -> parent].count + minimum; \ 453 | nextExclude -> value = node -> value; \ 454 | nextExclude -> under = under; \ 455 | nextExclude ++; \ 456 | }); \ 457 | total -= under; \ 458 | } 459 | 460 | #define ENET_CONTEXT_EXCLUDED(value_, after, before) \ 461 | { \ 462 | size_t low = 0, high = nextExclude - excludes; \ 463 | for(;;) \ 464 | { \ 465 | size_t mid = (low + high) >> 1; \ 466 | const ENetExclude * exclude = & excludes [mid]; \ 467 | if (value_ < exclude -> value) \ 468 | { \ 469 | if (low + 1 < high) \ 470 | { \ 471 | high = mid; \ 472 | continue; \ 473 | } \ 474 | if (exclude > excludes) \ 475 | after -= exclude [-1].under; \ 476 | } \ 477 | else \ 478 | { \ 479 | if (value_ > exclude -> value) \ 480 | { \ 481 | if (low + 1 < high) \ 482 | { \ 483 | low = mid; \ 484 | continue; \ 485 | } \ 486 | } \ 487 | else \ 488 | before = 0; \ 489 | after -= exclude -> under; \ 490 | } \ 491 | break; \ 492 | } \ 493 | } 494 | #endif 495 | 496 | #define ENET_CONTEXT_NOT_EXCLUDED(value_, after, before) 497 | 498 | size_t 499 | enet_range_coder_decompress (void * context, const enet_uint8 * inData, size_t inLimit, enet_uint8 * outData, size_t outLimit) 500 | { 501 | ENetRangeCoder * rangeCoder = (ENetRangeCoder *) context; 502 | enet_uint8 * outStart = outData, * outEnd = & outData [outLimit]; 503 | const enet_uint8 * inEnd = & inData [inLimit]; 504 | enet_uint32 decodeLow = 0, decodeCode = 0, decodeRange = ~0; 505 | ENetSymbol * root; 506 | enet_uint16 predicted = 0; 507 | size_t order = 0, nextSymbol = 0; 508 | #ifdef ENET_CONTEXT_EXCLUSION 509 | ENetExclude excludes [256]; 510 | ENetExclude * nextExclude = excludes; 511 | #endif 512 | 513 | if (rangeCoder == NULL || inLimit <= 0) 514 | return 0; 515 | 516 | ENET_CONTEXT_CREATE (root, ENET_CONTEXT_ESCAPE_MINIMUM, ENET_CONTEXT_SYMBOL_MINIMUM); 517 | 518 | ENET_RANGE_CODER_SEED; 519 | 520 | for (;;) 521 | { 522 | ENetSymbol * subcontext, * symbol, * patch; 523 | #ifdef ENET_CONTEXT_EXCLUSION 524 | const ENetSymbol * childContext = & emptyContext; 525 | #endif 526 | enet_uint8 value = 0; 527 | enet_uint16 code, under, count, bottom, * parent = & predicted, total; 528 | 529 | for (subcontext = & rangeCoder -> symbols [predicted]; 530 | subcontext != root; 531 | #ifdef ENET_CONTEXT_EXCLUSION 532 | childContext = subcontext, 533 | #endif 534 | subcontext = & rangeCoder -> symbols [subcontext -> parent]) 535 | { 536 | if (subcontext -> escapes <= 0) 537 | continue; 538 | total = subcontext -> total; 539 | #ifdef ENET_CONTEXT_EXCLUSION 540 | if (childContext -> total > 0) 541 | ENET_CONTEXT_DECODE_EXCLUDE (childContext, total, 0); 542 | #endif 543 | if (subcontext -> escapes >= total) 544 | continue; 545 | code = ENET_RANGE_CODER_READ (total); 546 | if (code < subcontext -> escapes) 547 | { 548 | ENET_RANGE_CODER_DECODE (0, subcontext -> escapes, total); 549 | continue; 550 | } 551 | code -= subcontext -> escapes; 552 | #ifdef ENET_CONTEXT_EXCLUSION 553 | if (childContext -> total > 0) 554 | { 555 | ENET_CONTEXT_TRY_DECODE (subcontext, symbol, code, value, under, count, ENET_SUBCONTEXT_SYMBOL_DELTA, 0, ENET_CONTEXT_EXCLUDED); 556 | } 557 | else 558 | #endif 559 | { 560 | ENET_CONTEXT_TRY_DECODE (subcontext, symbol, code, value, under, count, ENET_SUBCONTEXT_SYMBOL_DELTA, 0, ENET_CONTEXT_NOT_EXCLUDED); 561 | } 562 | bottom = symbol - rangeCoder -> symbols; 563 | ENET_RANGE_CODER_DECODE (subcontext -> escapes + under, count, total); 564 | subcontext -> total += ENET_SUBCONTEXT_SYMBOL_DELTA; 565 | if (count > 0xFF - 2*ENET_SUBCONTEXT_SYMBOL_DELTA || subcontext -> total > ENET_RANGE_CODER_BOTTOM - 0x100) 566 | ENET_CONTEXT_RESCALE (subcontext, 0); 567 | goto patchContexts; 568 | } 569 | 570 | total = root -> total; 571 | #ifdef ENET_CONTEXT_EXCLUSION 572 | if (childContext -> total > 0) 573 | ENET_CONTEXT_DECODE_EXCLUDE (childContext, total, ENET_CONTEXT_SYMBOL_MINIMUM); 574 | #endif 575 | code = ENET_RANGE_CODER_READ (total); 576 | if (code < root -> escapes) 577 | { 578 | ENET_RANGE_CODER_DECODE (0, root -> escapes, total); 579 | break; 580 | } 581 | code -= root -> escapes; 582 | #ifdef ENET_CONTEXT_EXCLUSION 583 | if (childContext -> total > 0) 584 | { 585 | ENET_CONTEXT_ROOT_DECODE (root, symbol, code, value, under, count, ENET_CONTEXT_SYMBOL_DELTA, ENET_CONTEXT_SYMBOL_MINIMUM, ENET_CONTEXT_EXCLUDED); 586 | } 587 | else 588 | #endif 589 | { 590 | ENET_CONTEXT_ROOT_DECODE (root, symbol, code, value, under, count, ENET_CONTEXT_SYMBOL_DELTA, ENET_CONTEXT_SYMBOL_MINIMUM, ENET_CONTEXT_NOT_EXCLUDED); 591 | } 592 | bottom = symbol - rangeCoder -> symbols; 593 | ENET_RANGE_CODER_DECODE (root -> escapes + under, count, total); 594 | root -> total += ENET_CONTEXT_SYMBOL_DELTA; 595 | if (count > 0xFF - 2*ENET_CONTEXT_SYMBOL_DELTA + ENET_CONTEXT_SYMBOL_MINIMUM || root -> total > ENET_RANGE_CODER_BOTTOM - 0x100) 596 | ENET_CONTEXT_RESCALE (root, ENET_CONTEXT_SYMBOL_MINIMUM); 597 | 598 | patchContexts: 599 | for (patch = & rangeCoder -> symbols [predicted]; 600 | patch != subcontext; 601 | patch = & rangeCoder -> symbols [patch -> parent]) 602 | { 603 | ENET_CONTEXT_ENCODE (patch, symbol, value, under, count, ENET_SUBCONTEXT_SYMBOL_DELTA, 0); 604 | * parent = symbol - rangeCoder -> symbols; 605 | parent = & symbol -> parent; 606 | if (count <= 0) 607 | { 608 | patch -> escapes += ENET_SUBCONTEXT_ESCAPE_DELTA; 609 | patch -> total += ENET_SUBCONTEXT_ESCAPE_DELTA; 610 | } 611 | patch -> total += ENET_SUBCONTEXT_SYMBOL_DELTA; 612 | if (count > 0xFF - 2*ENET_SUBCONTEXT_SYMBOL_DELTA || patch -> total > ENET_RANGE_CODER_BOTTOM - 0x100) 613 | ENET_CONTEXT_RESCALE (patch, 0); 614 | } 615 | * parent = bottom; 616 | 617 | ENET_RANGE_CODER_OUTPUT (value); 618 | 619 | if (order >= ENET_SUBCONTEXT_ORDER) 620 | predicted = rangeCoder -> symbols [predicted].parent; 621 | else 622 | order ++; 623 | ENET_RANGE_CODER_FREE_SYMBOLS; 624 | } 625 | 626 | return (size_t) (outData - outStart); 627 | } 628 | 629 | /** @defgroup host ENet host functions 630 | @{ 631 | */ 632 | 633 | /** Sets the packet compressor the host should use to the default range coder. 634 | @param host host to enable the range coder for 635 | @returns 0 on success, < 0 on failure 636 | */ 637 | int 638 | enet_host_compress_with_range_coder (ENetHost * host) 639 | { 640 | ENetCompressor compressor; 641 | memset (& compressor, 0, sizeof (compressor)); 642 | compressor.context = enet_range_coder_create(); 643 | if (compressor.context == NULL) 644 | return -1; 645 | compressor.compress = enet_range_coder_compress; 646 | compressor.decompress = enet_range_coder_decompress; 647 | compressor.destroy = enet_range_coder_destroy; 648 | enet_host_compress (host, & compressor); 649 | return 0; 650 | } 651 | 652 | /** @} */ 653 | 654 | 655 | -------------------------------------------------------------------------------- /enet/include/enet.h: -------------------------------------------------------------------------------- 1 | /** 2 | @file enet.h 3 | @brief ENet public header file 4 | */ 5 | #ifndef __ENET_ENET_H__ 6 | #define __ENET_ENET_H__ 7 | 8 | #ifdef __cplusplus 9 | extern "C" 10 | { 11 | #endif 12 | 13 | #include 14 | 15 | #ifdef _WIN32 16 | #include "win32.h" 17 | #else 18 | #include "unix.h" 19 | #endif 20 | 21 | #include "types.h" 22 | #include "protocol.h" 23 | #include "list.h" 24 | #include "callbacks.h" 25 | 26 | #define ENET_VERSION_MAJOR 1 27 | #define ENET_VERSION_MINOR 3 28 | #define ENET_VERSION_PATCH 14 29 | #define ENET_VERSION_CREATE(major, minor, patch) (((major)<<16) | ((minor)<<8) | (patch)) 30 | #define ENET_VERSION_GET_MAJOR(version) (((version)>>16)&0xFF) 31 | #define ENET_VERSION_GET_MINOR(version) (((version)>>8)&0xFF) 32 | #define ENET_VERSION_GET_PATCH(version) ((version)&0xFF) 33 | #define ENET_VERSION ENET_VERSION_CREATE(ENET_VERSION_MAJOR, ENET_VERSION_MINOR, ENET_VERSION_PATCH) 34 | 35 | typedef enet_uint32 ENetVersion; 36 | 37 | struct _ENetHost; 38 | struct _ENetEvent; 39 | struct _ENetPacket; 40 | 41 | typedef enum _ENetSocketType 42 | { 43 | ENET_SOCKET_TYPE_STREAM = 1, 44 | ENET_SOCKET_TYPE_DATAGRAM = 2 45 | } ENetSocketType; 46 | 47 | typedef enum _ENetSocketWait 48 | { 49 | ENET_SOCKET_WAIT_NONE = 0, 50 | ENET_SOCKET_WAIT_SEND = (1 << 0), 51 | ENET_SOCKET_WAIT_RECEIVE = (1 << 1), 52 | ENET_SOCKET_WAIT_INTERRUPT = (1 << 2) 53 | } ENetSocketWait; 54 | 55 | typedef enum _ENetSocketOption 56 | { 57 | ENET_SOCKOPT_NONBLOCK = 1, 58 | ENET_SOCKOPT_BROADCAST = 2, 59 | ENET_SOCKOPT_RCVBUF = 3, 60 | ENET_SOCKOPT_SNDBUF = 4, 61 | ENET_SOCKOPT_REUSEADDR = 5, 62 | ENET_SOCKOPT_RCVTIMEO = 6, 63 | ENET_SOCKOPT_SNDTIMEO = 7, 64 | ENET_SOCKOPT_ERROR = 8, 65 | ENET_SOCKOPT_NODELAY = 9 66 | } ENetSocketOption; 67 | 68 | typedef enum _ENetSocketShutdown 69 | { 70 | ENET_SOCKET_SHUTDOWN_READ = 0, 71 | ENET_SOCKET_SHUTDOWN_WRITE = 1, 72 | ENET_SOCKET_SHUTDOWN_READ_WRITE = 2 73 | } ENetSocketShutdown; 74 | 75 | #define ENET_HOST_ANY 0 76 | #define ENET_HOST_BROADCAST 0xFFFFFFFFU 77 | #define ENET_PORT_ANY 0 78 | 79 | /** 80 | * Portable internet address structure. 81 | * 82 | * The host must be specified in network byte-order, and the port must be in host 83 | * byte-order. The constant ENET_HOST_ANY may be used to specify the default 84 | * server host. The constant ENET_HOST_BROADCAST may be used to specify the 85 | * broadcast address (255.255.255.255). This makes sense for enet_host_connect, 86 | * but not for enet_host_create. Once a server responds to a broadcast, the 87 | * address is updated from ENET_HOST_BROADCAST to the server's actual IP address. 88 | */ 89 | typedef struct _ENetAddress { 90 | enet_uint32 host; 91 | enet_uint16 port; 92 | } ENetAddress; 93 | 94 | /** 95 | * Packet flag bit constants. 96 | * 97 | * The host must be specified in network byte-order, and the port must be in 98 | * host byte-order. The constant ENET_HOST_ANY may be used to specify the 99 | * default server host. 100 | 101 | @sa ENetPacket 102 | */ 103 | typedef enum _ENetPacketFlag 104 | { 105 | /** packet must be received by the target peer and resend attempts should be 106 | * made until the packet is delivered */ 107 | ENET_PACKET_FLAG_RELIABLE = (1 << 0), 108 | /** packet will not be sequenced with other packets 109 | * not supported for reliable packets 110 | */ 111 | ENET_PACKET_FLAG_UNSEQUENCED = (1 << 1), 112 | /** packet will not allocate data, and user must supply it instead */ 113 | ENET_PACKET_FLAG_NO_ALLOCATE = (1 << 2), 114 | /** packet will be fragmented using unreliable (instead of reliable) sends 115 | * if it exceeds the MTU */ 116 | ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT = (1 << 3), 117 | 118 | /** whether the packet has been sent from all queues it has been entered into */ 119 | ENET_PACKET_FLAG_SENT = (1<<8) 120 | } ENetPacketFlag; 121 | 122 | typedef void (ENET_CALLBACK * ENetPacketFreeCallback) (struct _ENetPacket *); 123 | 124 | /** 125 | * ENet packet structure. 126 | * 127 | * An ENet data packet that may be sent to or received from a peer. The shown 128 | * fields should only be read and never modified. The data field contains the 129 | * allocated data for the packet. The dataLength fields specifies the length 130 | * of the allocated data. The flags field is either 0 (specifying no flags), 131 | * or a bitwise-or of any combination of the following flags: 132 | * 133 | * ENET_PACKET_FLAG_RELIABLE - packet must be received by the target peer 134 | * and resend attempts should be made until the packet is delivered 135 | * 136 | * ENET_PACKET_FLAG_UNSEQUENCED - packet will not be sequenced with other packets 137 | * (not supported for reliable packets) 138 | * 139 | * ENET_PACKET_FLAG_NO_ALLOCATE - packet will not allocate data, and user must supply it instead 140 | * 141 | * ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT - packet will be fragmented using unreliable 142 | * (instead of reliable) sends if it exceeds the MTU 143 | * 144 | * ENET_PACKET_FLAG_SENT - whether the packet has been sent from all queues it has been entered into 145 | @sa ENetPacketFlag 146 | */ 147 | typedef struct _ENetPacket 148 | { 149 | size_t referenceCount; /**< internal use only */ 150 | enet_uint32 flags; /**< bitwise-or of ENetPacketFlag constants */ 151 | enet_uint8 * data; /**< allocated data for packet */ 152 | size_t dataLength; /**< length of data */ 153 | ENetPacketFreeCallback freeCallback; /**< function to be called when the packet is no longer in use */ 154 | void * userData; /**< application private data, may be freely modified */ 155 | } ENetPacket; 156 | 157 | typedef struct _ENetAcknowledgement 158 | { 159 | ENetListNode acknowledgementList; 160 | enet_uint32 sentTime; 161 | ENetProtocol command; 162 | } ENetAcknowledgement; 163 | 164 | typedef struct _ENetOutgoingCommand 165 | { 166 | ENetListNode outgoingCommandList; 167 | enet_uint16 reliableSequenceNumber; 168 | enet_uint16 unreliableSequenceNumber; 169 | enet_uint32 sentTime; 170 | enet_uint32 roundTripTimeout; 171 | enet_uint32 roundTripTimeoutLimit; 172 | enet_uint32 fragmentOffset; 173 | enet_uint16 fragmentLength; 174 | enet_uint16 sendAttempts; 175 | ENetProtocol command; 176 | ENetPacket * packet; 177 | } ENetOutgoingCommand; 178 | 179 | typedef struct _ENetIncomingCommand 180 | { 181 | ENetListNode incomingCommandList; 182 | enet_uint16 reliableSequenceNumber; 183 | enet_uint16 unreliableSequenceNumber; 184 | ENetProtocol command; 185 | enet_uint32 fragmentCount; 186 | enet_uint32 fragmentsRemaining; 187 | enet_uint32 * fragments; 188 | ENetPacket * packet; 189 | } ENetIncomingCommand; 190 | 191 | typedef enum _ENetPeerState 192 | { 193 | ENET_PEER_STATE_DISCONNECTED = 0, 194 | ENET_PEER_STATE_CONNECTING = 1, 195 | ENET_PEER_STATE_ACKNOWLEDGING_CONNECT = 2, 196 | ENET_PEER_STATE_CONNECTION_PENDING = 3, 197 | ENET_PEER_STATE_CONNECTION_SUCCEEDED = 4, 198 | ENET_PEER_STATE_CONNECTED = 5, 199 | ENET_PEER_STATE_DISCONNECT_LATER = 6, 200 | ENET_PEER_STATE_DISCONNECTING = 7, 201 | ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT = 8, 202 | ENET_PEER_STATE_ZOMBIE = 9 203 | } ENetPeerState; 204 | 205 | #ifndef ENET_BUFFER_MAXIMUM 206 | #define ENET_BUFFER_MAXIMUM (1 + 2 * ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS) 207 | #endif 208 | 209 | enum 210 | { 211 | ENET_HOST_RECEIVE_BUFFER_SIZE = 256 * 1024, 212 | ENET_HOST_SEND_BUFFER_SIZE = 256 * 1024, 213 | ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL = 1000, 214 | ENET_HOST_DEFAULT_MTU = 1400, 215 | ENET_HOST_DEFAULT_MAXIMUM_PACKET_SIZE = 32 * 1024 * 1024, 216 | ENET_HOST_DEFAULT_MAXIMUM_WAITING_DATA = 32 * 1024 * 1024, 217 | 218 | ENET_PEER_DEFAULT_ROUND_TRIP_TIME = 500, 219 | ENET_PEER_DEFAULT_PACKET_THROTTLE = 32, 220 | ENET_PEER_PACKET_THROTTLE_SCALE = 32, 221 | ENET_PEER_PACKET_THROTTLE_COUNTER = 7, 222 | ENET_PEER_PACKET_THROTTLE_ACCELERATION = 2, 223 | ENET_PEER_PACKET_THROTTLE_DECELERATION = 2, 224 | ENET_PEER_PACKET_THROTTLE_INTERVAL = 5000, 225 | ENET_PEER_PACKET_LOSS_SCALE = (1 << 16), 226 | ENET_PEER_PACKET_LOSS_INTERVAL = 10000, 227 | ENET_PEER_WINDOW_SIZE_SCALE = 64 * 1024, 228 | ENET_PEER_TIMEOUT_LIMIT = 32, 229 | ENET_PEER_TIMEOUT_MINIMUM = 5000, 230 | ENET_PEER_TIMEOUT_MAXIMUM = 30000, 231 | ENET_PEER_PING_INTERVAL = 500, 232 | ENET_PEER_UNSEQUENCED_WINDOWS = 64, 233 | ENET_PEER_UNSEQUENCED_WINDOW_SIZE = 1024, 234 | ENET_PEER_FREE_UNSEQUENCED_WINDOWS = 32, 235 | ENET_PEER_RELIABLE_WINDOWS = 16, 236 | ENET_PEER_RELIABLE_WINDOW_SIZE = 0x1000, 237 | ENET_PEER_FREE_RELIABLE_WINDOWS = 8 238 | }; 239 | 240 | typedef struct _ENetChannel 241 | { 242 | enet_uint16 outgoingReliableSequenceNumber; 243 | enet_uint16 outgoingUnreliableSequenceNumber; 244 | enet_uint16 usedReliableWindows; 245 | enet_uint16 reliableWindows [ENET_PEER_RELIABLE_WINDOWS]; 246 | enet_uint16 incomingReliableSequenceNumber; 247 | enet_uint16 incomingUnreliableSequenceNumber; 248 | ENetList incomingReliableCommands; 249 | ENetList incomingUnreliableCommands; 250 | } ENetChannel; 251 | 252 | /** 253 | * An ENet peer which data packets may be sent or received from. 254 | * 255 | * No fields should be modified unless otherwise specified. 256 | */ 257 | typedef struct _ENetPeer 258 | { 259 | ENetListNode dispatchList; 260 | struct _ENetHost * host; 261 | enet_uint16 outgoingPeerID; 262 | enet_uint16 incomingPeerID; 263 | enet_uint32 connectID; 264 | enet_uint8 outgoingSessionID; 265 | enet_uint8 incomingSessionID; 266 | ENetAddress address; /**< Internet address of the peer */ 267 | void * data; /**< Application private data, may be freely modified */ 268 | ENetPeerState state; 269 | ENetChannel * channels; 270 | size_t channelCount; /**< Number of channels allocated for communication with peer */ 271 | enet_uint32 incomingBandwidth; /**< Downstream bandwidth of the client in bytes/second */ 272 | enet_uint32 outgoingBandwidth; /**< Upstream bandwidth of the client in bytes/second */ 273 | enet_uint32 incomingBandwidthThrottleEpoch; 274 | enet_uint32 outgoingBandwidthThrottleEpoch; 275 | enet_uint32 incomingDataTotal; 276 | enet_uint32 outgoingDataTotal; 277 | enet_uint32 lastSendTime; 278 | enet_uint32 lastReceiveTime; 279 | enet_uint32 nextTimeout; 280 | enet_uint32 earliestTimeout; 281 | enet_uint32 packetLossEpoch; 282 | enet_uint32 packetsSent; 283 | enet_uint32 packetsLost; 284 | enet_uint32 packetLoss; /**< mean packet loss of reliable packets as a ratio with respect to the constant ENET_PEER_PACKET_LOSS_SCALE */ 285 | enet_uint32 packetLossVariance; 286 | enet_uint32 packetThrottle; 287 | enet_uint32 packetThrottleLimit; 288 | enet_uint32 packetThrottleCounter; 289 | enet_uint32 packetThrottleEpoch; 290 | enet_uint32 packetThrottleAcceleration; 291 | enet_uint32 packetThrottleDeceleration; 292 | enet_uint32 packetThrottleInterval; 293 | enet_uint32 pingInterval; 294 | enet_uint32 timeoutLimit; 295 | enet_uint32 timeoutMinimum; 296 | enet_uint32 timeoutMaximum; 297 | enet_uint32 lastRoundTripTime; 298 | enet_uint32 lowestRoundTripTime; 299 | enet_uint32 lastRoundTripTimeVariance; 300 | enet_uint32 highestRoundTripTimeVariance; 301 | enet_uint32 roundTripTime; /**< mean round trip time (RTT), in milliseconds, between sending a reliable packet and receiving its acknowledgement */ 302 | enet_uint32 roundTripTimeVariance; 303 | enet_uint32 mtu; 304 | enet_uint32 windowSize; 305 | enet_uint32 reliableDataInTransit; 306 | enet_uint16 outgoingReliableSequenceNumber; 307 | ENetList acknowledgements; 308 | ENetList sentReliableCommands; 309 | ENetList sentUnreliableCommands; 310 | ENetList outgoingReliableCommands; 311 | ENetList outgoingUnreliableCommands; 312 | ENetList dispatchedCommands; 313 | int needsDispatch; 314 | enet_uint16 incomingUnsequencedGroup; 315 | enet_uint16 outgoingUnsequencedGroup; 316 | enet_uint32 unsequencedWindow [ENET_PEER_UNSEQUENCED_WINDOW_SIZE / 32]; 317 | enet_uint32 eventData; 318 | size_t totalWaitingData; 319 | } ENetPeer; 320 | 321 | /** An ENet packet compressor for compressing UDP packets before socket sends or receives. 322 | */ 323 | typedef struct _ENetCompressor 324 | { 325 | /** Context data for the compressor. Must be non-NULL. */ 326 | void * context; 327 | /** Compresses from inBuffers[0:inBufferCount-1], containing inLimit bytes, to outData, outputting at most outLimit bytes. Should return 0 on failure. */ 328 | size_t (ENET_CALLBACK * compress) (void * context, const ENetBuffer * inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 * outData, size_t outLimit); 329 | /** Decompresses from inData, containing inLimit bytes, to outData, outputting at most outLimit bytes. Should return 0 on failure. */ 330 | size_t (ENET_CALLBACK * decompress) (void * context, const enet_uint8 * inData, size_t inLimit, enet_uint8 * outData, size_t outLimit); 331 | /** Destroys the context when compression is disabled or the host is destroyed. May be NULL. */ 332 | void (ENET_CALLBACK * destroy) (void * context); 333 | } ENetCompressor; 334 | 335 | /** Callback that computes the checksum of the data held in buffers[0:bufferCount-1] */ 336 | typedef enet_uint32 (ENET_CALLBACK * ENetChecksumCallback) (const ENetBuffer * buffers, size_t bufferCount); 337 | 338 | /** Callback for intercepting received raw UDP packets. Should return 1 to intercept, 0 to ignore, or -1 to propagate an error. */ 339 | typedef int (ENET_CALLBACK * ENetInterceptCallback) (struct _ENetHost * host, struct _ENetEvent * event); 340 | 341 | /** An ENet host for communicating with peers. 342 | * 343 | * No fields should be modified unless otherwise stated. 344 | 345 | @sa enet_host_create() 346 | @sa enet_host_destroy() 347 | @sa enet_host_connect() 348 | @sa enet_host_service() 349 | @sa enet_host_flush() 350 | @sa enet_host_broadcast() 351 | @sa enet_host_compress() 352 | @sa enet_host_compress_with_range_coder() 353 | @sa enet_host_channel_limit() 354 | @sa enet_host_bandwidth_limit() 355 | @sa enet_host_bandwidth_throttle() 356 | */ 357 | typedef struct _ENetHost 358 | { 359 | ENetSocket socket; 360 | ENetAddress address; /**< Internet address of the host */ 361 | enet_uint32 incomingBandwidth; /**< downstream bandwidth of the host */ 362 | enet_uint32 outgoingBandwidth; /**< upstream bandwidth of the host */ 363 | enet_uint32 bandwidthThrottleEpoch; 364 | enet_uint32 mtu; 365 | enet_uint32 randomSeed; 366 | int recalculateBandwidthLimits; 367 | ENetPeer * peers; /**< array of peers allocated for this host */ 368 | size_t peerCount; /**< number of peers allocated for this host */ 369 | size_t channelLimit; /**< maximum number of channels allowed for connected peers */ 370 | enet_uint32 serviceTime; 371 | ENetList dispatchQueue; 372 | int continueSending; 373 | size_t packetSize; 374 | enet_uint16 headerFlags; 375 | ENetProtocol commands [ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS]; 376 | size_t commandCount; 377 | ENetBuffer buffers [ENET_BUFFER_MAXIMUM]; 378 | size_t bufferCount; 379 | ENetChecksumCallback checksum; /**< callback the user can set to enable packet checksums for this host */ 380 | ENetCompressor compressor; 381 | enet_uint8 packetData [2][ENET_PROTOCOL_MAXIMUM_MTU]; 382 | ENetAddress receivedAddress; 383 | enet_uint8 * receivedData; 384 | size_t receivedDataLength; 385 | enet_uint32 totalSentData; /**< total data sent, user should reset to 0 as needed to prevent overflow */ 386 | enet_uint32 totalSentPackets; /**< total UDP packets sent, user should reset to 0 as needed to prevent overflow */ 387 | enet_uint32 totalReceivedData; /**< total data received, user should reset to 0 as needed to prevent overflow */ 388 | enet_uint32 totalReceivedPackets; /**< total UDP packets received, user should reset to 0 as needed to prevent overflow */ 389 | ENetInterceptCallback intercept; /**< callback the user can set to intercept received raw UDP packets */ 390 | size_t connectedPeers; 391 | size_t bandwidthLimitedPeers; 392 | size_t duplicatePeers; /**< optional number of allowed peers from duplicate IPs, defaults to ENET_PROTOCOL_MAXIMUM_PEER_ID */ 393 | size_t maximumPacketSize; /**< the maximum allowable packet size that may be sent or received on a peer */ 394 | size_t maximumWaitingData; /**< the maximum aggregate amount of buffer space a peer may use waiting for packets to be delivered */ 395 | size_t usingNewPacket; /**< the New and Improved! */ 396 | } ENetHost; 397 | 398 | /** 399 | * An ENet event type, as specified in @ref ENetEvent. 400 | */ 401 | typedef enum _ENetEventType 402 | { 403 | /** no event occurred within the specified time limit */ 404 | ENET_EVENT_TYPE_NONE = 0, 405 | 406 | /** a connection request initiated by enet_host_connect has completed. 407 | * The peer field contains the peer which successfully connected. 408 | */ 409 | ENET_EVENT_TYPE_CONNECT = 1, 410 | 411 | /** a peer has disconnected. This event is generated on a successful 412 | * completion of a disconnect initiated by enet_peer_disconnect, if 413 | * a peer has timed out, or if a connection request intialized by 414 | * enet_host_connect has timed out. The peer field contains the peer 415 | * which disconnected. The data field contains user supplied data 416 | * describing the disconnection, or 0, if none is available. 417 | */ 418 | ENET_EVENT_TYPE_DISCONNECT = 2, 419 | 420 | /** a packet has been received from a peer. The peer field specifies the 421 | * peer which sent the packet. The channelID field specifies the channel 422 | * number upon which the packet was received. The packet field contains 423 | * the packet that was received; this packet must be destroyed with 424 | * enet_packet_destroy after use. 425 | */ 426 | ENET_EVENT_TYPE_RECEIVE = 3 427 | } ENetEventType; 428 | 429 | /** 430 | * An ENet event as returned by enet_host_service(). 431 | 432 | @sa enet_host_service 433 | */ 434 | typedef struct _ENetEvent 435 | { 436 | ENetEventType type; /**< type of the event */ 437 | ENetPeer * peer; /**< peer that generated a connect, disconnect or receive event */ 438 | enet_uint8 channelID; /**< channel on the peer that generated the event, if appropriate */ 439 | enet_uint32 data; /**< data associated with the event, if appropriate */ 440 | ENetPacket * packet; /**< packet associated with the event, if appropriate */ 441 | } ENetEvent; 442 | 443 | /** @defgroup global ENet global functions 444 | @{ 445 | */ 446 | 447 | /** 448 | Initializes ENet globally. Must be called prior to using any functions in 449 | ENet. 450 | @returns 0 on success, < 0 on failure 451 | */ 452 | ENET_API int enet_initialize (void); 453 | 454 | /** 455 | Initializes ENet globally and supplies user-overridden callbacks. Must be called prior to using any functions in ENet. Do not use enet_initialize() if you use this variant. Make sure the ENetCallbacks structure is zeroed out so that any additional callbacks added in future versions will be properly ignored. 456 | 457 | @param version the constant ENET_VERSION should be supplied so ENet knows which version of ENetCallbacks struct to use 458 | @param inits user-overridden callbacks where any NULL callbacks will use ENet's defaults 459 | @returns 0 on success, < 0 on failure 460 | */ 461 | ENET_API int enet_initialize_with_callbacks (ENetVersion version, const ENetCallbacks * inits); 462 | 463 | /** 464 | Shuts down ENet globally. Should be called when a program that has 465 | initialized ENet exits. 466 | */ 467 | ENET_API void enet_deinitialize (void); 468 | 469 | /** 470 | Gives the linked version of the ENet library. 471 | @returns the version number 472 | */ 473 | ENET_API ENetVersion enet_linked_version (void); 474 | 475 | /** @} */ 476 | 477 | /** @defgroup private ENet private implementation functions */ 478 | 479 | /** 480 | Returns the wall-time in milliseconds. Its initial value is unspecified 481 | unless otherwise set. 482 | */ 483 | ENET_API enet_uint32 enet_time_get (void); 484 | /** 485 | Sets the current wall-time in milliseconds. 486 | */ 487 | ENET_API void enet_time_set (enet_uint32); 488 | 489 | /** @defgroup socket ENet socket functions 490 | @{ 491 | */ 492 | ENET_API ENetSocket enet_socket_create (ENetSocketType); 493 | ENET_API int enet_socket_bind (ENetSocket, const ENetAddress *); 494 | ENET_API int enet_socket_get_address (ENetSocket, ENetAddress *); 495 | ENET_API int enet_socket_listen (ENetSocket, int); 496 | ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress *); 497 | ENET_API int enet_socket_connect (ENetSocket, const ENetAddress *); 498 | ENET_API int enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t); 499 | ENET_API int enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t); 500 | ENET_API int enet_socket_wait (ENetSocket, enet_uint32 *, enet_uint32); 501 | ENET_API int enet_socket_set_option (ENetSocket, ENetSocketOption, int); 502 | ENET_API int enet_socket_get_option (ENetSocket, ENetSocketOption, int *); 503 | ENET_API int enet_socket_shutdown (ENetSocket, ENetSocketShutdown); 504 | ENET_API void enet_socket_destroy (ENetSocket); 505 | ENET_API int enet_socketset_select (ENetSocket, ENetSocketSet *, ENetSocketSet *, enet_uint32); 506 | 507 | /** @} */ 508 | 509 | /** @defgroup Address ENet address functions 510 | @{ 511 | */ 512 | 513 | /** Attempts to parse the printable form of the IP address in the parameter hostName 514 | and sets the host field in the address parameter if successful. 515 | @param address destination to store the parsed IP address 516 | @param hostName IP address to parse 517 | @retval 0 on success 518 | @retval < 0 on failure 519 | @returns the address of the given hostName in address on success 520 | */ 521 | ENET_API int enet_address_set_host_ip (ENetAddress * address, const char * hostName); 522 | 523 | /** Attempts to resolve the host named by the parameter hostName and sets 524 | the host field in the address parameter if successful. 525 | @param address destination to store resolved address 526 | @param hostName host name to lookup 527 | @retval 0 on success 528 | @retval < 0 on failure 529 | @returns the address of the given hostName in address on success 530 | */ 531 | ENET_API int enet_address_set_host (ENetAddress * address, const char * hostName); 532 | 533 | /** Gives the printable form of the IP address specified in the address parameter. 534 | @param address address printed 535 | @param hostName destination for name, must not be NULL 536 | @param nameLength maximum length of hostName. 537 | @returns the null-terminated name of the host in hostName on success 538 | @retval 0 on success 539 | @retval < 0 on failure 540 | */ 541 | ENET_API int enet_address_get_host_ip (const ENetAddress * address, char * hostName, size_t nameLength); 542 | 543 | /** Attempts to do a reverse lookup of the host field in the address parameter. 544 | @param address address used for reverse lookup 545 | @param hostName destination for name, must not be NULL 546 | @param nameLength maximum length of hostName. 547 | @returns the null-terminated name of the host in hostName on success 548 | @retval 0 on success 549 | @retval < 0 on failure 550 | */ 551 | ENET_API int enet_address_get_host (const ENetAddress * address, char * hostName, size_t nameLength); 552 | 553 | /** @} */ 554 | 555 | ENET_API ENetPacket * enet_packet_create (const void *, size_t, enet_uint32); 556 | ENET_API void enet_packet_destroy (ENetPacket *); 557 | ENET_API int enet_packet_resize (ENetPacket *, size_t); 558 | ENET_API enet_uint32 enet_crc32 (const ENetBuffer *, size_t); 559 | 560 | ENET_API ENetHost * enet_host_create (const ENetAddress *, size_t, size_t, enet_uint32, enet_uint32); 561 | ENET_API void enet_host_destroy (ENetHost *); 562 | ENET_API ENetPeer * enet_host_connect (ENetHost *, const ENetAddress *, size_t, enet_uint32); 563 | ENET_API int enet_host_check_events (ENetHost *, ENetEvent *); 564 | ENET_API int enet_host_service (ENetHost *, ENetEvent *, enet_uint32); 565 | ENET_API void enet_host_flush (ENetHost *); 566 | ENET_API void enet_host_broadcast (ENetHost *, enet_uint8, ENetPacket *); 567 | ENET_API void enet_host_compress (ENetHost *, const ENetCompressor *); 568 | ENET_API int enet_host_compress_with_range_coder (ENetHost * host); 569 | ENET_API void enet_host_channel_limit (ENetHost *, size_t); 570 | ENET_API void enet_host_bandwidth_limit (ENetHost *, enet_uint32, enet_uint32); 571 | extern void enet_host_bandwidth_throttle (ENetHost *); 572 | extern enet_uint32 enet_host_random_seed (void); 573 | 574 | ENET_API int enet_peer_send (ENetPeer *, enet_uint8, ENetPacket *); 575 | ENET_API ENetPacket * enet_peer_receive (ENetPeer *, enet_uint8 * channelID); 576 | ENET_API void enet_peer_ping (ENetPeer *); 577 | ENET_API void enet_peer_ping_interval (ENetPeer *, enet_uint32); 578 | ENET_API void enet_peer_timeout (ENetPeer *, enet_uint32, enet_uint32, enet_uint32); 579 | ENET_API void enet_peer_reset (ENetPeer *); 580 | ENET_API void enet_peer_disconnect (ENetPeer *, enet_uint32); 581 | ENET_API void enet_peer_disconnect_now (ENetPeer *, enet_uint32); 582 | ENET_API void enet_peer_disconnect_later (ENetPeer *, enet_uint32); 583 | ENET_API void enet_peer_throttle_configure (ENetPeer *, enet_uint32, enet_uint32, enet_uint32); 584 | extern int enet_peer_throttle (ENetPeer *, enet_uint32); 585 | extern void enet_peer_reset_queues (ENetPeer *); 586 | extern void enet_peer_setup_outgoing_command (ENetPeer *, ENetOutgoingCommand *); 587 | extern ENetOutgoingCommand * enet_peer_queue_outgoing_command (ENetPeer *, const ENetProtocol *, ENetPacket *, enet_uint32, enet_uint16); 588 | extern ENetIncomingCommand * enet_peer_queue_incoming_command (ENetPeer *, const ENetProtocol *, const void *, size_t, enet_uint32, enet_uint32); 589 | extern ENetAcknowledgement * enet_peer_queue_acknowledgement (ENetPeer *, const ENetProtocol *, enet_uint16); 590 | extern void enet_peer_dispatch_incoming_unreliable_commands (ENetPeer *, ENetChannel *); 591 | extern void enet_peer_dispatch_incoming_reliable_commands (ENetPeer *, ENetChannel *); 592 | extern void enet_peer_on_connect (ENetPeer *); 593 | extern void enet_peer_on_disconnect (ENetPeer *); 594 | 595 | ENET_API void * enet_range_coder_create (void); 596 | ENET_API void enet_range_coder_destroy (void *); 597 | ENET_API size_t enet_range_coder_compress (void *, const ENetBuffer *, size_t, size_t, enet_uint8 *, size_t); 598 | ENET_API size_t enet_range_coder_decompress (void *, const enet_uint8 *, size_t, enet_uint8 *, size_t); 599 | 600 | extern size_t enet_protocol_command_size (enet_uint8); 601 | 602 | #ifdef __cplusplus 603 | } 604 | #endif 605 | 606 | #endif /* __ENET_ENET_H__ */ 607 | 608 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------