編輯:關於Android編程
既然選擇Raknet開發,那就深入研究其源碼結構,為以後的應用打下基礎。
文件 描述_FindFirst快速查找AutopatcherPatchContext自動更新、不停AutopatcherRepositoryInterface更新 獲取重要的數據接口Base64Encoderbase64編碼BitStream比特流 流結構CCRakNetSlidingWindow觀測CCRakNetUDT CheckSum校驗CloudClient雲端 客戶端CloudCommon雲端 通用功能CloudServer雲端 服務器CommandParserInterface通用解析接口ConnectionGraph2連接圖ConsoleServer控制服務器DataCompressor數據處理DirectoryDeltaTransfer文件目錄傳輸DR_SHA1哈希值計算DS_BinarySearchTree二叉樹查詢DS_BPlusTree二叉樹DS_BytePool字節池DS_ByteQueue字節隊列DS_Hash哈希DS_Heap堆棧DS_HuffmanEncodingTree哈夫曼編碼樹DS_HuffmanEncodingTreeFactory哈夫曼編碼樹產生DS_HuffmanEncodingTreeNode哈夫曼編碼樹節點DS_LinkedList鏈表DS_List列表DS_Map哈希DS_MemoryPool內存池DS_Multilist多鏈表DS_OrderedChannelHeap順序通道堆DS_OrderedList有序列表DS_Queue隊列DS_QueueLinkedList隊列鏈表DS_RangeList范圍列表DS_Table表DS_ThreadsafeAllocatingQueue線性安全隊列DS_Tree樹DS_WeightedGraph權重圖DynDNS動態域EmailSender郵箱發送EmptyHeader EpochTimeToString時間值轉換Export導出FileList文件列表FileListNodeContext文件列表節點FileListTransfer文件列表傳輸FileListTransferCBInterface文件列表傳輸接口FileOperations文件操作FormatString字符格式化FullyConnectedMesh2飽和鏈接Getche獲取一個字符Gets獲取一組字符GetTime獲取時間gettimeofday獲取一天的時間值GridSectorizer.h網格HTTPConnectionhttp連接類HTTPConnection2http連接插件類IncrementalReadInterface InternalPacket內部包Itoa整形轉換Kbhit單擊LinuxStrings LocklessTypes計數LogCommandParser日志分析MessageFilter.h消息過濾MessageIdentifiers消息idMTUSize定義默認的最大、最小傳輸單元NativeFeatureIncludes定義本地功能NativeFeatureIncludesOverrides NativeTypes本地類型NatPunchthroughClient穿透插件NatPunchthroughServer穿透服務器NatTypeDetectionClient網絡類型匹配NatTypeDetectionCommon網絡類型匹配 通用NatTypeDetectionServer網絡類型匹配服務端NetworkIDManager網絡id管理NetworkIDObject網絡id對象PacketConsoleLogger包控制日志PacketFileLogger包日志記錄PacketizedTCPtcp包分組PacketLogger包記錄PacketOutputWindowLogger包輸出記錄PacketPool PacketPriority枚舉枚舉分組優先級和可靠性PluginInterface2插件接口PS3Includes PS4Includes Rackspace輔助服務器空間RakAlloca內存申請RakAssert RakMemoryOverride內存管理RakNetCommandParser通用解析RakNetDefines定義RakNetDefinesOverrides RakNetSmartPtr引用指針RakNetSocket套接字RakNetSocket2套接字2RakNetStatistics常量RakNetTime時間RakNetTransport2傳輸端口RakNetTypes使用網絡類型RakNetVersion版本RakPeer實例RakPeerInterface RakSleep RakString RakThread RakWString Rand RandSync ReadyEvent RefCountedObj引用計數RelayPlugin延遲插件ReliabilityLayer數據層ReplicaEnums復制管理系統ReplicaManager3復制管理Router2路由器插件RPC4Plugin遠程調用call插件SecureHandshake密鑰握手SendToThread SignaledEvent信號事件SimpleMutex互斥SimpleTCPServer SingleProducerConsumer通過使用一個循環緩沖區隊列中讀寫指針線程之間的數據SocketDefines SocketIncludes套接字包含SocketLayer套接字層StatisticsHistory統計記錄(輸入的數值和時間)StringCompressor字符串壓縮StringTable字符串編碼與解碼SuperFastHash TableSerializer TCPInterfacetcp操作接口TeamBalancer團隊平衡TeamManager團隊管理TelnetTransport傳輸ThreadPool線程池ThreadsafePacketLogger線性安全數據包 日志TransportInterface傳輸接口TwoWayAuthentication雙向認證 UDPForwarderudp數據包UDPProxyClientudp代理客戶端 UDPProxyCommon UDPProxyCoordinator代理協調 UDPProxyServer代理服務器VariableDeltaSerializer變量序列化 VariableListDeltaTracker變量監聽VariadicSQLParser變量sql分析 VitaIncludes WindowsIncludes WSAStartupSingleton XBox360Includes
跟編碼有關的文件如:
Base64Encoder、DR_SHA1、DS_HuffmanEncodingTree、DS_HuffmanEncodingTreeFactory、DS_HuffmanEncodingTreeNode
其中Base64Encoder提供兩個函數
extern "C" {
/// \brief Returns how many bytes were written.
// outputData should be at least the size of inputData * 2 + 6
int Base64Encoding(const unsigned char *inputData, int dataLength, char *outputData);
}
extern "C" {
const char *Base64Map(void);
} SHA1("abc" in ANSI) =
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
SHA1("abc" in Unicode LE) =
9F04F41A 84851416 2050E3D6 8C1A7ABB 441DC2B5
SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
in ANSI) =
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
in Unicode LE) =
51D7D876 9AC72C40 9C5B0E3F 69C60ADC 9A039014
SHA1(A million repetitions of "a" in ANSI) =
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
SHA1(A million repetitions of "a" in Unicode LE) =
C4609560 A108A0C6 26AA7F2B 38A65566 739353C5DS_HuffmanEncodingTree、DS_HuffmanEncodingTreeFactory、DS_HuffmanEncodingTreeNode跟哈夫曼有關。
哈夫曼編碼(Huffman Coding)是一種編碼方式,哈夫曼編碼是可變字長編碼(VLC)的一種。Huffman於1952年提出一種編碼方法,該方法完全依據字符出現概率來構造異字頭的平均長度最短的碼字,有時稱之為最佳編碼,一般就叫做Huffman編碼(有時也稱為霍夫曼編碼)。
struct HuffmanEncodingTreeNode
{
unsigned char value;
unsigned weight;
HuffmanEncodingTreeNode *left;
HuffmanEncodingTreeNode *right;
HuffmanEncodingTreeNode *parent;
};容器可以管理對象的生命周期、對象與對象之間的依賴關系,您可以使用一個配置文件(通常是XML),在上面定義好對象的名稱、如何產生(Prototype 方式或Singleton 方式)、哪個對象產生之後必須設定成為某個對象的屬性等,在啟動容器之後,所有的對象都可以直接取用,不用編寫任何一行程序代碼來產生對象,或是建立對象與對象之間的依賴關系。
容器分同步異步、線程安全非安全之分。
跟容器相關的文件如下:
DS_BinarySearchTree、DS_BPlusTree、DS_Hash、DS_Heap、DS_LinkedList、DS_List、DS_Map、DS_Multilist、DS_OrderedChannelHeap
DS_OrderedList、DS_Queue、DS_QueueLinkedList、DS_RangeList、DS_ThreadsafeAllocatingQueue、DS_Tree
DS_BinarySearchTree :二叉搜索樹:若它的左子樹不空,則左子樹上所有結點的值均小於它的根結點的值; 若它的右子樹不空,則右子樹上所有結點的值均大於它的根結點的值; 它的左、右子樹也分別為二叉排序樹。
* EXAMPLE * @code * BinarySearchTreeA; * A.Add(10); * A.Add(15); * A.Add(5); * int* array = RakNet::OP_NEW (A.Size(), _FILE_AND_LINE_ ); * A.DisplayInorder(array); * array[0]; // returns 5 * array[1]; // returns 10 * array[2]; // returns 15
二叉樹:每個結點最多有兩個子樹的有序樹。
void main(void)
{
DataStructures::BPlusTree btree;
DataStructures::List haveList, removedList;
int temp;
int i, j, index;
int testSize;
bool b;
for (testSize=0; testSize < 514; testSize++)
{
RAKNET_DEBUG_PRINTF("TestSize=%i\n", testSize);
for (i=0; i < testSize; i++)
haveList.Insert(i);
for (i=0; i < testSize; i++)
{
index=i+randomMT()%(testSize-i);
temp=haveList[index];
haveList[index]=haveList[i];
haveList[i]=temp;
}
for (i=0; i
DS_Hash:把任意長度的輸入(預映射)通過散列算法變換成固定長度的輸出,該輸出就是散列值。
//直接取余法:f(x):= x mod maxM ; maxM一般是不太接近 2^t 的一個質數。
//乘法取整法:f(x):=trunc((x/maxX)*maxlongit) mod maxM,主要用於實數。
//平方取中法:f(x):=(x*x div 1000 ) mod 1000000); 平方後取中間的,每位包含信息比較多。
項目中結構體: struct HashIndex
{
unsigned int primaryIndex;
unsigned int secondaryIndex;
bool IsInvalid(void) const {return primaryIndex==(unsigned int) -1;}
void SetInvalid(void) {primaryIndex=(unsigned int) -1; secondaryIndex=(unsigned int) -1;}
};DS_Heap
堆:一種特殊的樹形數據結構,它滿足堆的特性:父節點的值一定大於或等於子節點的值。
struct HeapNode
{
HeapNode() {}
HeapNode(const weight_type &w, const data_type &d) : weight(w), data(d) {}
weight_type weight; // I'm assuming key is a native numerical type - float or int
data_type data;
};DS_OrderedChannelHeap:同上
DS_LinkedList:鏈表:
* EXAMPLE:
* @code
* LinkedList A; // Creates a Linked List of integers called A
* CircularLinkedList B; // Creates a Circular Linked List of
* // integers called B
*
* A.Insert(20); // Adds 20 to A. A: 20 - current is 20
* A.Insert(5); // Adds 5 to A. A: 5 20 - current is 5
* A.Insert(1); // Adds 1 to A. A: 1 5 20 - current is 1
*
* A.IsIn1); // returns true
* A.IsIn200); // returns false
* A.Find(5); // returns true and sets current to 5
* A.Peek(); // returns 5
* A.Find(1); // returns true and sets current to 1
*
* (++A).Peek(); // Returns 5
* A.Peek(); // Returns 5
*
* A.Replace(10); // Replaces 5 with 10.
* A.Peek(); // Returns 10
*
* A.Beginning(); // Current points to the beginning of the list at 1
*
* (++A).Peek(); // Returns 5
* A.Peek(); // Returns 10
*
* A.Del(); // Deletes 10. Current points to the next element, which is 20
* A.Peek(); // Returns 20
*
* A.Beginning(); // Current points to the beginning of the list at 1
*
* (++A).Peek(); // Returns 5
* A.Peek(); // Returns 20
*
* A.Clear(_FILE_AND_LINE_); // Deletes all nodes in A
*
* A.Insert(5); // A: 5 - current is 5
* A.Insert(6); // A: 6 5 - current is 6
* A.Insert(7); // A: 7 6 5 - current is 7
*
* A.Clear(_FILE_AND_LINE_);
* B.Clear(_FILE_AND_LINE_);
*
* B.Add(10);
* B.Add(20);
* B.Add(30);
* B.Add(5);
* B.Add(2);
* B.Add(25);
* // Sorts the numbers in the list and sets the current pointer to the
* // first element
* B.sort();
*
* // Postfix ++ just calls the prefix version and has no functional
* // difference.
* B.Peek(); // Returns 2
* B++;
* B.Peek(); // Returns 5
* B++;
* B.Peek(); // Returns 10
* B++;
* B.Peek(); // Returns 20
* B++;
* B.Peek(); // Returns 25
* B++;
* B.Peek(); // Returns 30
DS_List、DS_Multilist、DS_OrderedList、DS_RangeList、DS_QueueLinkedList:同上
DS_Map:鍵值對
lastSearchIndex=index;
lastSearchKey=key;
lastSearchIndexValid=true;
結構體: struct MapNode
{
MapNode() {}
MapNode(key_type _key, data_type _data) : mapNodeKey(_key), mapNodeData(_data) {}
MapNode& operator = ( const MapNode& input ) {mapNodeKey=input.mapNodeKey; mapNodeData=input.mapNodeData; return *this;}
MapNode( const MapNode & input) {mapNodeKey=input.mapNodeKey; mapNodeData=input.mapNodeData;}
key_type mapNodeKey;
data_type mapNodeData;
};DS_Queue:隊列,先進後出的原則。
DS_ThreadsafeAllocatingQueue:同上,特點是在線程中是安全的,應該添加了鎖。
DS_Tree:樹結構
類如下:
template
class RAK_DLL_EXPORT Tree
{
public:
Tree();
Tree(TreeType &inputData);
~Tree();
void LevelOrderTraversal(DataStructures::List &output);
void AddChild(TreeType &newData);
void DeleteDecendants(void);
TreeType data;
DataStructures::List children;
};
3、字節輔助
字節輔助操作相關的文件有:
DS_BytePool、DS_ByteQueue
DS_BytePool:字節池,有效管理字節,主要是申請內存和釋放。
類如下:
class RAK_DLL_EXPORT BytePool
{
public:
BytePool();
~BytePool();
// Should be at least 8 times bigger than 8192
void SetPageSize(int size);
unsigned char* Allocate(int bytesWanted, const char *file, unsigned int line);
void Release(unsigned char *data, const char *file, unsigned int line);
void Clear(const char *file, unsigned int line);
protected:
MemoryPool pool128;
MemoryPool pool512;
MemoryPool pool2048;
MemoryPool pool8192;
#ifdef _THREADSAFE_BYTE_POOL
SimpleMutex mutex128;
SimpleMutex mutex512;
SimpleMutex mutex2048;
SimpleMutex mutex8192;
#endif
}; DS_ByteQueue:字節隊列,字節寫入和讀取的操作。類如下:
class ByteQueue
{
public:
ByteQueue();
~ByteQueue();
void WriteBytes(const char *in, unsigned length, const char *file, unsigned int line);
bool ReadBytes(char *out, unsigned maxLengthToRead, bool peek);
unsigned GetBytesWritten(void) const;
char* PeekContiguousBytes(unsigned int *outLength) const;
void IncrementReadOffset(unsigned length);
void DecrementReadOffset(unsigned length);
void Clear(const char *file, unsigned int line);
void Print(void);
protected:
char *data;
unsigned readOffset, writeOffset, lengthAllocated;
};4、字符串輔助
字節輔助操作相關的文件有:
FormatString、LinuxStrings、RakString、RakWString、StringCompressor、StringTable、EpochTimeToString
顧名思義,操作字符串的類或函數。
FormatString:
extern "C" { //格式化輸出
char * FormatString(const char *format, ...);
}
// Threadsafe
extern "C" {
char * FormatStringTS(char *output, const char *format, ...);
}LinuxStrings:系統函數#if defined(__native_client__)
#ifndef _stricmp
int _stricmp(const char* s1, const char* s2);
#endif
int _strnicmp(const char* s1, const char* s2, size_t n);
char *_strlwr(char * str );
#define _vsnprintf vsnprintf
#else
#if (defined(__GNUC__) || defined(__GCCXML__) || defined(__S3E__) ) && !defined(_WIN32)
#ifndef _stricmp
int _stricmp(const char* s1, const char* s2);
#endif
int _strnicmp(const char* s1, const char* s2, size_t n);
// http://www.jenkinssoftware.com/forum/index.php?topic=5010.msg20920#msg20920
// #ifndef _vsnprintf
#define _vsnprintf vsnprintf
// #endif
#ifndef __APPLE__
char *_strlwr(char * str ); //this won't compile on OSX for some reason
#endifRakString、RakWString:自定義的字符串,添加很多操作符。StringCompressor:字符串壓縮
把文件流壓縮成字符串或字符串編程文件流等操作。
哈夫曼編碼樹管理
/// Pointer to the huffman encoding trees.
DataStructures::Map huffmanEncodingTrees;
StringTable:字符串編碼與解碼
int RAK_DLL_EXPORT StrAndBoolComp( char *const &key, const StrAndBool &data );
EpochTimeToString:時間值轉換
RAK_DLL_EXPORT char * EpochTimeToString(long long time);
5、內存輔助
RakNetSmartPtr、RakAlloca、RakAssert、RakMemoryOverride
有效管理內存或指針。
RakNetSmartPtr:實現對指針的管理。
關鍵類:
class RAK_DLL_EXPORT ReferenceCounter
{
private:
int refCount;
public:
ReferenceCounter() {refCount=0;}
~ReferenceCounter() {}
void AddRef() {refCount++;}
int Release() {return --refCount;}
int GetRefCount(void) const {return refCount;}
};
RakAlloca、RakAssert、RakMemoryOverride:實現內存的管理RakAlloca、RakAssert :直接引用系統函數。
RakMemoryOverride:對外公開接口,如下:
extern RAK_DLL_EXPORT void * (*rakMalloc) (size_t size);
extern RAK_DLL_EXPORT void * (*rakRealloc) (void *p, size_t size);
extern RAK_DLL_EXPORT void (*rakFree) (void *p);
extern RAK_DLL_EXPORT void * (*rakMalloc_Ex) (size_t size, const char *file, unsigned int line);
extern RAK_DLL_EXPORT void * (*rakRealloc_Ex) (void *p, size_t size, const char *file, unsigned int line);
extern RAK_DLL_EXPORT void (*rakFree_Ex) (void *p, const char *file, unsigned int line);
extern RAK_DLL_EXPORT void (*notifyOutOfMemory) (const char *file, const long line);
extern RAK_DLL_EXPORT void * (*dlMallocMMap) (size_t size);
extern RAK_DLL_EXPORT void * (*dlMallocDirectMMap) (size_t size);
extern RAK_DLL_EXPORT int (*dlMallocMUnmap) (void* ptr, size_t size);
// Change to a user defined allocation function
void RAK_DLL_EXPORT SetMalloc( void* (*userFunction)(size_t size) );
void RAK_DLL_EXPORT SetRealloc( void* (*userFunction)(void *p, size_t size) );
void RAK_DLL_EXPORT SetFree( void (*userFunction)(void *p) );
void RAK_DLL_EXPORT SetMalloc_Ex( void* (*userFunction)(size_t size, const char *file, unsigned int line) );
void RAK_DLL_EXPORT SetRealloc_Ex( void* (*userFunction)(void *p, size_t size, const char *file, unsigned int line) );
void RAK_DLL_EXPORT SetFree_Ex( void (*userFunction)(void *p, const char *file, unsigned int line) );
// Change to a user defined out of memory function
void RAK_DLL_EXPORT SetNotifyOutOfMemory( void (*userFunction)(const char *file, const long line) );
void RAK_DLL_EXPORT SetDLMallocMMap( void* (*userFunction)(size_t size) );
void RAK_DLL_EXPORT SetDLMallocDirectMMap( void* (*userFunction)(size_t size) );
void RAK_DLL_EXPORT SetDLMallocMUnmap( int (*userFunction)(void* ptr, size_t size) );
extern RAK_DLL_EXPORT void * (*GetMalloc()) (size_t size);
extern RAK_DLL_EXPORT void * (*GetRealloc()) (void *p, size_t size);
extern RAK_DLL_EXPORT void (*GetFree()) (void *p);
extern RAK_DLL_EXPORT void * (*GetMalloc_Ex()) (size_t size, const char *file, unsigned int line);
extern RAK_DLL_EXPORT void * (*GetRealloc_Ex()) (void *p, size_t size, const char *file, unsigned int line);
extern RAK_DLL_EXPORT void (*GetFree_Ex()) (void *p, const char *file, unsigned int line);
extern RAK_DLL_EXPORT void *(*GetDLMallocMMap())(size_t size);
extern RAK_DLL_EXPORT void *(*GetDLMallocDirectMMap())(size_t size);
extern RAK_DLL_EXPORT int (*GetDLMallocMUnmap())(void* ptr, size_t size);
6、其它輔助
_FindFirst、CheckSum、RefCountedObj、ThreadPool、DS_Table、Getche、Gets、GetTime、gettimeofday、Itoa
Kbhit、LocklessTypes、RakNetTime、RakThread、
這些都是輔助項目的函數或類。、
_FindFirst:文件快速查找
long _findfirst(const char *name, _finddata_t *f);
int _findnext(long h, _finddata_t *f);
int _findclose(long h);
SuperFastHash:快速哈希
對外的接口如下:
uint32_t SuperFastHash (const char * data, int length);
uint32_t SuperFastHashIncremental (const char * data, int len, unsigned int lastHash );
uint32_t SuperFastHashFile (const char * filename);
uint32_t SuperFastHashFilePtr (FILE *fp);
CheckSum:效驗和類如下:
class CheckSum
{
public:
/// Default constructor
CheckSum()
{
Clear();
}
void Clear()
{
sum = 0;
r = 55665;
c1 = 52845;
c2 = 22719;
}
void Add ( unsigned int w );
void Add ( unsigned short w );
void Add ( unsigned char* b, unsigned int length );
void Add ( unsigned char b );
unsigned int Get ()
{
return sum;
}
protected:
unsigned short r;
unsigned short c1;
unsigned short c2;
unsigned int sum;
};
RefCountedObj、LocklessTypes:引用計數
實現對對象生命周期的管理,類如下:
class RAK_DLL_EXPORT LocklessUint32_t
{
public:
LocklessUint32_t();
explicit LocklessUint32_t(uint32_t initial);
// Returns variable value after changing it
uint32_t Increment(void);
// Returns variable value after changing it
uint32_t Decrement(void);
uint32_t GetValue(void) const {return value;}
protected:
#ifdef _WIN32
volatile LONG value;
#elif defined(ANDROID) || defined(__S3E__) || defined(__APPLE__)
// __sync_fetch_and_add not supported apparently
SimpleMutex mutex;
uint32_t value;
#else
volatile uint32_t value;
#endif
};class RefCountedObj
{
public:
RefCountedObj() {refCount=1;}
virtual ~RefCountedObj() {}
void AddRef(void) {refCount++;}
void Deref(void) {if (--refCount==0) RakNet::OP_DELETE(this, _FILE_AND_LINE_);}
int refCount;
};
ThreadPool、RakThread:實現對線程的管理和封裝
Getche、Gets、GetTime、gettimeofday、Itoa、Kbhit、RakNetTime:系統函數
DS_Table:實現對數據庫表的 操作
7、消息id(內部消息id和用戶自定義消息id)
enum OutOfBandIdentifiers
{
ID_NAT_ESTABLISH_UNIDIRECTIONAL, //單向穿透
ID_NAT_ESTABLISH_BIDIRECTIONAL, //雙向穿透
ID_NAT_TYPE_DETECT, //匹配類型
ID_ROUTER_2_REPLY_TO_SENDER_PORT, //路由器延時發送端口
ID_ROUTER_2_REPLY_TO_SPECIFIED_PORT, //特定端口
ID_ROUTER_2_MINI_PUNCH_REPLY, //回復
ID_ROUTER_2_MINI_PUNCH_REPLY_BOUNCE, //反彈
ID_XBOX_360_VOICE, //聲音
ID_XBOX_360_GET_NETWORK_ROOM, //獲取網絡房間
ID_XBOX_360_RETURN_NETWORK_ROOM, //返回網絡房間
ID_NAT_PING, //ping測試
ID_NAT_PONG, //pong測試
};
枚舉DefaultMessageIDTypes中都是內部消息id,需要添加自定義消息id,則使用如下方法:
enum {
ID_MYPROJECT_MSG_1 = ID_USER_PACKET_ENUM,
ID_MYPROJECT_MSG_2,
...
};8、比特流 BitStream
用一個封裝的動態數組來打包和解包bits,具有四個優勢:1. 動態創建數據報;2. 數據壓縮;3. 寫入Bits;4. 數據字節序轉換。
Bitstream是作為模板類,可以容納任何類型數據。如果這是一個內置的類型(NetwordIDObject),它使用部分模板實現使得類型寫入更加有效。如果是局部類型或一個結構體,它可以寫入單獨的內存數據(比特流、序列化對象)。
struct MyVector //寫數據
{
float x,y,z;
} myVector;
bitStream.Write(myVector);// 沒有字節序交換
#undef __BITSTREAM_NATIVE_END // 帶有字節序交換
bitStream.Write(myVector.x);
bitStream.Write(myVector.y);
bitStream.Write(myVector.z);
// 也可以重寫操作符
namespace RakNet
{
RakNet::BitStream& operator << (RakNet::BitStream& out, MyVector& in)
{
out.WriteNormVector(in.x,in.y,in.z);
return out;
}
RakNet::BitStream& operator >> (RakNet::BitStream& in, MyVector& out)
{
bool success = in.ReadNormVector(out.x,out.y,out.z);
assert(success);
return in;
}
}
myVector << bitStream;// 從bitstream讀取數據
myVector >> bitStream;// 向bitstream寫入數據
可選—其中的一個構造函數是以長度作為參數。如果大概知道數據的大小,在構造Bitstream對象的時候可以將這個參數傳遞給Bitstream的構造函數,可以避免在生成bitstream對象後在動態重新分配內存。讀取數據也是一樣的簡單。創建一個bitstream,在構造函數中賦值給它數據。
// 假設我們接收到一個數據包Packet *
BitStream myBitStream(packet->data, packet->length, false);
struct MyVector
{
float x,y,z;
} myVector;
// 沒有字節序轉換
bitStream.Read(myVector);
// 要轉換字節序(__BITSTREAM_NATIVE_END在RakNetDefines.h中要注釋掉)
#undef __BITSTREAM_NATIVE_END
#include "BitStream.h"
bitStream.Read(myVector.x);
bitStream.Read(myVector.y);
bitStream.Read(myVector.z);序列化數據:需要同時使用相同的函數Read和Write,可以使用BitStream::Serialize()代替Read()和Write()
struct MyVector
{
float x,y,z;
// 如果ToBitstream==true,則是寫入數據, 如果ToBitstream==false,則是讀取數據
void Serialize(bool writeToBitstream, BitStream *bs)
{
bs->Serialize(writeToBitstream, x);
bs->Serialize(writeToBitstream, y);
bs->Serialize(writeToBitstream, z);
}
} myVector;
測試如下: struct EmploymentStruct //自定義結構體
{
int salary;
unsigned char yearsEmployed;
};
void clientRPC(RPCParameters *rpcParameters) //遠程調用rpc
{
BitStream b(rpcParameters->input, BITS_TO_BYTES(rpcParameters->numberOfBitsOfData), false); //構建比特流
char name[200]; //名稱
// printf("GOT RPC:\n");
// b.PrintBits();
unsigned char nameLength;
b.Read(nameLength);
if (b.Read(name, nameLength)==false) // 獲取名稱
{
printf("Name was not null-terminated!\n");
return;
}
name[nameLength]=0; // Name is now null terminated
printf("In clientRPC:\n");
printf("Name is %s\n", name);
unsigned int age;
if (b.ReadCompressed(age)==false)
return;
printf("Age is %i\n", age);
fflush(stdout);
bool wroteEmploymentStruct;
if (b.Read(wroteEmploymentStruct)==false)
{
return;
}
if (wroteEmploymentStruct)
{
printf("We are employed.\n");
EmploymentStruct employmentStruct;
if (b.Read(employmentStruct.salary)==false) return;
if (b.Read(employmentStruct.yearsEmployed)==false) return;
printf("Salary is %i. Years employed is %i\n", employmentStruct.salary, (int)employmentStruct.yearsEmployed);
}
else
printf("We are between jobs :)\n");
quit=true;
}
#if defined(_PS3) || defined(__PS3__)
#endif
int main(void)
{
RakPeerInterface *rakClient=RakNetworkFactory::GetRakPeerInterface();
RakPeerInterface *rakServer=RakNetworkFactory::GetRakPeerInterface();
#ifndef WIN32
#define getch getchar
#endif
#if defined(_PS3) || defined(__PS3__)
#endif
quit=false;
char text[255];
// Defined in RakNetTypes.h.
// You can register a function anytime
REGISTER_STATIC_RPC(rakClient, clientRPC);
//rakServer->InitializeSecurity(0,0,0,0);
SocketDescriptor socketDescriptor(10000,0);
if (rakServer->Startup(1,30,&socketDescriptor, 1)==false)
{
printf("Start call failed!\n");
fflush(stdout);
fgets(text, sizeof(text), stdin);
printf("\n");
return 0;
}
rakServer->SetMaximumIncomingConnections(1);
socketDescriptor.port=0;
rakClient->Startup(1, 30, &socketDescriptor, 1);
if (rakClient->Connect("127.0.0.1", 10000, 0, 0)==false)
{
printf("Connect call failed\n");
fflush(stdout);
fgets(text, sizeof(text), stdin);
printf("\n");
return 0;
}
BitStream outgoingBitstream;
unsigned int age;
printf("A sample on how to use RakNet's bitstream class\n");
printf("Difficulty: Beginner\n\n");
printf("Enter your name.\n");
fflush(stdout);
fgets(text, sizeof(text), stdin);
printf("\n");
if (text[0]==0)
strcpy(text, "Unnamed!");
outgoingBitstream.Write((unsigned char)strlen(text));
outgoingBitstream.Write(text, (int) strlen(text));
printf("Enter your age (numbers only).\n");
fflush(stdout);
fgets(text, sizeof(text), stdin);
printf("\n");
if (text[0]==0)
age=0;
else
age=atoi(text);
outgoingBitstream.WriteCompressed(age);
printf("Are you employed (y/n)?\n");
fflush(stdout);
fgets(text, sizeof(text), stdin);
printf("\n");
if (text[0]=='y')
{
outgoingBitstream.Write(true); // Writing a bool takes 1 bit
// Read some data into a struct
EmploymentStruct employmentStruct;
printf("What is your salary (enter a number only)?\n");
fflush(stdout);
fgets(text, sizeof(text), stdin);
printf("\n");
employmentStruct.salary = atoi(text);
printf("How many years have you been employed (enter a number only)?\n");
fflush(stdout);
fgets(text, sizeof(text), stdin);
printf("\n");
employmentStruct.yearsEmployed = atoi(text);
// We can write structs to a bitstream but this is not portable due to:
// 1. Different-endian CPUs
// 2. Different 'padding' of structs depending on compiler, etc
// The only safe way to send a struct is by using the BitStream
// to write out every single member which you want to send.
outgoingBitstream.Write(employmentStruct.salary);
outgoingBitstream.Write(employmentStruct.yearsEmployed);
// We're done writing to the struct
}
else
{
//printf("Number of bits before [false]: %d\n",
//outgoingBitstream.GetNumberOfBitsUsed() );
outgoingBitstream.Write(false); // Writing a bool takes 1 bit
// We're done writing to the struct. Compare this to the example above - we wrote quite a bit less.
}
printf("Waiting for connection...\n");
while (rakClient->GetSystemAddressFromIndex(0)==UNASSIGNED_SYSTEM_ADDRESS)
RakSleep(30);
printf("Connected.\n");
// printf("SEND RPC:\n");
// outgoingBitstream.PrintBits();
// RPC functions as well as send can take bitstreams directly
bool success = rakServer->RPC("clientRPC",&outgoingBitstream, HIGH_PRIORITY, RELIABLE, 0, UNASSIGNED_SYSTEM_ADDRESS, true, 0, UNASSIGNED_NETWORK_ID, 0); // broadcast to everyone, which happens to be our one client
if (!success)
printf("RPC call failed\n");
while (!quit)
{
rakClient->DeallocatePacket(rakClient->Receive());
rakServer->DeallocatePacket(rakServer->Receive());
RakSleep(30);
}
printf("Press enter to quit\n");
fflush(stdout);
264 fgets(text, sizeof(text), stdin);
printf("\n");
rakClient->Shutdown(100,0);
rakServer->Shutdown(100,0);
// This is not necessary since on shutdown everything is unregistered. This is just here to show usage
UNREGISTER_STATIC_RPC(rakClient, clientRPC);
RakNetworkFactory::DestroyRakPeerInterface(rakClient);
RakNetworkFactory::DestroyRakPeerInterface(rakServer);
return 0;
}9、內部網絡數據包
結構:
struct Packet
{
/// The system that send this packet.
SystemAddress systemAddress; //地址信息
/// A unique identifier for the system that sent this packet, regardless of IP address (internal / external / remote system)
/// Only valid once a connection has been established (ID_CONNECTION_REQUEST_ACCEPTED, or ID_NEW_INCOMING_CONNECTION)
/// Until that time, will be UNASSIGNED_RAKNET_GUID
RakNetGUID guid;
/// The length of the data in bytes
unsigned int length;
/// The length of the data in bits
BitSize_t bitSize;
/// The data from the sender
unsigned char* data;
/// @internal
/// Indicates whether to delete the data, or to simply delete the packet.
bool deleteData;
/// @internal
/// If true, this message is meant for the user, not for the plugins, so do not process it through plugins
bool wasGeneratedLocally;
};
源碼如下:typedef uint16_t SplitPacketIdType; //id標識類型
typedef uint32_t SplitPacketIndexType; //序列類型
/// This is the counter used for holding packet numbers, so we can detect duplicate packets. It should be large enough that if the variables
/// Internally assumed to be 4 bytes, but written as 3 bytes in ReliabilityLayer::WriteToBitStreamFromInternalPacket
typedef uint24_t MessageNumberType; //保持分組數的計數器,可以檢測到重復的數據包。
/// This is the counter used for holding ordered packet numbers, so we can detect out-of-order packets. It should be large enough that if the variables
/// were to wrap, the newly wrapped values would no longer be in use. Warning: Too large of a value wastes bandwidth!
typedef MessageNumberType OrderingIndexType;//用來保持有序的包數的計數器,可以檢測出數據包的順序。
typedef RakNet::TimeUS RemoteSystemTimeType;
struct InternalPacketFixedSizeTransmissionHeader
{
/// A unique numerical identifier given to this user message. Used to identify reliable messages on the network
MessageNumberType reliableMessageNumber; //唯一標識 號碼
///The ID used as identification for ordering messages. Also included in sequenced messages
OrderingIndexType orderingIndex; //排序消息識別
// Used only with sequenced messages
OrderingIndexType sequencingIndex;//用於排序消息
///What ordering channel this packet is on, if the reliability type uses ordering channels
unsigned char orderingChannel; //順序通道
///The ID of the split packet, if we have split packets. This is the maximum number of split messages we can send simultaneously per connection.
SplitPacketIdType splitPacketId; //分包標識
///If this is a split packet, the index into the array of subsplit packets
SplitPacketIndexType splitPacketIndex;//分包序列
///The size of the array of subsplit packets
SplitPacketIndexType splitPacketCount; //分包總數
///How many bits long the data is
BitSize_t dataBitLength; //數據長度
///What type of reliability algorithm to use with this packet
PacketReliability reliability;//可靠性算法
// Not endian safe
// unsigned char priority : 3;
// unsigned char reliability : 5;
};
/// Used in InternalPacket when pointing to sharedDataBlock, rather than allocating itself
struct InternalPacketRefCountedData //引用計數
{
unsigned char *sharedDataBlock;
unsigned int refCount;
};
/// Holds a user message, and related information
/// Don't use a constructor or destructor, due to the memory pool I am using
struct InternalPacket : public InternalPacketFixedSizeTransmissionHeader //內部數據包
{
/// Identifies the order in which this number was sent. Used locally
MessageNumberType messageInternalOrder; //唯一標識
/// Has this message number been assigned yet? We don't assign until the message is actually sent.
/// This fixes a bug where pre-determining message numbers and then sending a message on a different channel creates a huge gap.
/// This causes performance problems and causes those messages to timeout.
bool messageNumberAssigned; //是否被分配標識
/// Was this packet number used this update to track windowing drops or increases? Each packet number is only used once per update.
// bool allowWindowUpdate;
///When this packet was created
RakNet::TimeUS creationTime; //創建時間
///The resendNext time to take action on this packet
RakNet::TimeUS nextActionTime; //下個動作時間
// For debugging
RakNet::TimeUS retransmissionTime; //丟失時間
// Size of the header when encoded into a bitstream
BitSize_t headerLength; //頭長度信息
/// Buffer is a pointer to the actual data, assuming this packet has data at all
unsigned char *data; //數據內容
/// How to alloc and delete the data member
enum AllocationScheme //定義申請內容方式
{
/// Data is allocated using rakMalloc. Just free it
NORMAL, //正常情況
/// data points to a larger block of data, where the larger block is reference counted. internalPacketRefCountedData is used in this case
REF_COUNTED, //引用計數
/// If allocation scheme is STACK, data points to stackData and should not be deallocated
/// This is only used when sending. Received packets are deallocated in RakPeer
STACK //棧模式
} allocationScheme;
InternalPacketRefCountedData *refCountedData; //引用計數
/// How many attempts we made at sending this message
unsigned char timesSent; //發送時間
/// The priority level of this packet
PacketPriority priority; //優先級
/// If the reliability type requires a receipt, then return this number with it
uint32_t sendReceiptSerial; //可靠性統計
// Used for the resend queue
// Linked list implementation so I can remove from the list via a pointer, without finding it in the list
InternalPacket *resendPrev, *resendNext,*unreliablePrev,*unreliableNext; //發送隊列
unsigned char stackData[128]; //堆棧數據
};6、網絡id標識
定義如下:
typedef uint64_t NetworkID;
struct RAK_DLL_EXPORT RakNetGUID
{
RakNetGUID();
explicit RakNetGUID(uint64_t _g) {g=_g; systemIndex=(SystemIndex)-1;}
// uint32_t g[6];
uint64_t g;
// Return the GUID as a string
// Returns a static string
// NOT THREADSAFE
const char *ToString(void) const;
// Return the GUID as a string
// dest must be large enough to hold the output
// THREADSAFE
void ToString(char *dest) const;
bool FromString(const char *source);
static unsigned long ToUint32( const RakNetGUID &g );
RakNetGUID& operator = ( const RakNetGUID& input )
{
g=input.g;
systemIndex=input.systemIndex;
return *this;
}
// Used internally for fast lookup. Optional (use -1 to do regular lookup). Don't transmit this.
SystemIndex systemIndex;
static int size() {return (int) sizeof(uint64_t);}
bool operator==( const RakNetGUID& right ) const;
bool operator!=( const RakNetGUID& right ) const;
bool operator > ( const RakNetGUID& right ) const;
bool operator < ( const RakNetGUID& right ) const;
};class RAK_DLL_EXPORT NetworkIDObject
{
public:
// Constructor. NetworkIDs, if IsNetworkIDAuthority() is true, are created here.
NetworkIDObject();
// Destructor. Used NetworkIDs, if any, are freed here.
virtual ~NetworkIDObject();
/// Sets the manager class from which to request unique network IDs
/// Unlike previous versions, the NetworkIDObject relies on a manager class to provide IDs, rather than using statics,
/// So you can have more than one set of IDs on the same system.
virtual void SetNetworkIDManager( NetworkIDManager *manager); //附屬哪個管理
/// Returns what was passed to SetNetworkIDManager
virtual NetworkIDManager * GetNetworkIDManager( void ) const; //返回管理
/// Returns the NetworkID that you can use to refer to this object over the network.
/// \pre You must first call SetNetworkIDManager before using this function
/// \retval UNASSIGNED_NETWORK_ID UNASSIGNED_NETWORK_ID is returned IsNetworkIDAuthority() is false and SetNetworkID() was not previously called. This is also returned if you call this function in the constructor.
/// \retval 0-65534 Any other value is a valid NetworkID. NetworkIDs start at 0 and go to 65534, wrapping at that point.
virtual NetworkID GetNetworkID( void ); //獲取id
/// Sets the NetworkID for this instance. Usually this is called by the clients and determined from the servers. However, if you save multiplayer games you would likely use
/// This on load as well.
virtual void SetNetworkID( NetworkID id ); //設置id
/// Your class does not have to derive from NetworkIDObject, although that is the easiest way to implement this.
/// If you want this to be a member object of another class, rather than inherit, then call SetParent() with a pointer to the parent class instance.
/// GET_OBJECT_FROM_ID will then return the parent rather than this instance.
virtual void SetParent( void *_parent ); //設置父類
/// Return what was passed to SetParent
/// \return The value passed to SetParent, or 0 if it was never called.
virtual void* GetParent( void ) const; //獲取父類
protected:
/// The network ID of this object
// networkID is assigned when networkIDManager is set.
NetworkID networkID;
NetworkIDManager *networkIDManager;
/// The parent set by SetParent()
void *parent;
/// \internal, used by NetworkIDManager
friend class NetworkIDManager;
NetworkIDObject *nextInstanceForNetworkIDManager;
};10、接口
enum PluginReceiveResult //返回結果
{
/// The plugin used this message and it shouldn't be given to the user.
RR_STOP_PROCESSING_AND_DEALLOCATE=0,
/// This message will be processed by other plugins, and at last by the user.
RR_CONTINUE_PROCESSING,
/// The plugin is going to hold on to this message. Do not deallocate it but do not pass it to other plugins either.
RR_STOP_PROCESSING
};enum PI2_LostConnectionReason //丟失原因
{
/// Called RakPeer::CloseConnection()
LCR_CLOSED_BY_USER,
/// Got ID_DISCONNECTION_NOTIFICATION
LCR_DISCONNECTION_NOTIFICATION,
/// GOT ID_CONNECTION_LOST
LCR_CONNECTION_LOST
};enum PI2_FailedConnectionAttemptReason //失敗原因
{
FCAR_CONNECTION_ATTEMPT_FAILED,
FCAR_ALREADY_CONNECTED,
FCAR_NO_FREE_INCOMING_CONNECTIONS,
FCAR_SECURITY_PUBLIC_KEY_MISMATCH,
FCAR_CONNECTION_BANNED,
FCAR_INVALID_PASSWORD,
FCAR_INCOMPATIBLE_PROTOCOL,
FCAR_IP_RECENTLY_CONNECTED,
FCAR_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY,
FCAR_OUR_SYSTEM_REQUIRES_SECURITY,
FCAR_PUBLIC_KEY_MISMATCH
};接口類:class RAK_DLL_EXPORT PluginInterface2 //插件類
{
public:
PluginInterface2();
virtual ~PluginInterface2();
/// Called when the interface is attached
virtual void OnAttach(void) {} //綁定
/// Called when the interface is detached
virtual void OnDetach(void) {} //斷開
/// Update is called every time a packet is checked for .
virtual void Update(void) {} //更新
/// OnReceive is called for every packet.
/// \param[in] packet the packet that is being returned to the user
/// \return True to allow the game and other plugins to get this message, false to absorb it
virtual PluginReceiveResult OnReceive(Packet *packet) {(void) packet; return RR_CONTINUE_PROCESSING;} //接收到數據,進行處理
/// Called when RakPeer is initialized
virtual void OnRakPeerStartup(void) {} //啟動
/// Called when RakPeer is shutdown
virtual void OnRakPeerShutdown(void) {}//關閉
/// Called when a connection is dropped because the user called RakPeer::CloseConnection() for a particular system
/// \param[in] systemAddress The system whose connection was closed
/// \param[in] rakNetGuid The guid of the specified system
/// \param[in] lostConnectionReason How the connection was closed: manually, connection lost, or notification of disconnection
virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason ){(void) systemAddress; (void) rakNetGUID; (void) lostConnectionReason;}
/// Called when we got a new connection
/// \param[in] systemAddress Address of the new connection
/// \param[in] rakNetGuid The guid of the specified system
/// \param[in] isIncoming If true, this is ID_NEW_INCOMING_CONNECTION, or the equivalent
virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming) {(void) systemAddress; (void) rakNetGUID; (void) isIncoming;}
/// Called when a connection attempt fails
/// \param[in] packet Packet to be returned to the user
/// \param[in] failedConnectionReason Why the connection failed
virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason) {(void) packet; (void) failedConnectionAttemptReason;}
/// Queried when attached to RakPeer
/// Return true to call OnDirectSocketSend(), OnDirectSocketReceive(), OnReliabilityLayerNotification(), OnInternalPacket(), and OnAck()
/// If true, then you cannot call RakPeer::AttachPlugin() or RakPeer::DetachPlugin() for this plugin, while RakPeer is active
virtual bool UsesReliabilityLayer(void) const {return false;}
/// Called on a send to the socket, per datagram, that does not go through the reliability layer
/// \pre To be called, UsesReliabilityLayer() must return true
/// \param[in] data The data being sent
/// \param[in] bitsUsed How many bits long \a data is
/// \param[in] remoteSystemAddress Which system this message is being sent to
virtual void OnDirectSocketSend(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) {(void) data; (void) bitsUsed; (void) remoteSystemAddress;}
/// Called on a receive from the socket, per datagram, that does not go through the reliability layer
/// \pre To be called, UsesReliabilityLayer() must return true
/// \param[in] data The data being sent
/// \param[in] bitsUsed How many bits long \a data is
/// \param[in] remoteSystemAddress Which system this message is being sent to
virtual void OnDirectSocketReceive(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) {(void) data; (void) bitsUsed; (void) remoteSystemAddress;}
/// Called when the reliability layer rejects a send or receive
/// \pre To be called, UsesReliabilityLayer() must return true
/// \param[in] bitsUsed How many bits long \a data is
/// \param[in] remoteSystemAddress Which system this message is being sent to
virtual void OnReliabilityLayerNotification(const char *errorMessage, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress, bool isError) {(void) errorMessage; (void) bitsUsed; (void) remoteSystemAddress; (void) isError;}
/// Called on a send or receive of a message within the reliability layer
/// \pre To be called, UsesReliabilityLayer() must return true
/// \param[in] internalPacket The user message, along with all send data.
/// \param[in] frameNumber The number of frames sent or received so far for this player depending on \a isSend . Indicates the frame of this user message.
/// \param[in] remoteSystemAddress The player we sent or got this packet from
/// \param[in] time The current time as returned by RakNet::GetTimeMS()
/// \param[in] isSend Is this callback representing a send event or receive event?
virtual void OnInternalPacket(InternalPacket *internalPacket, unsigned frameNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time, int isSend) {(void) internalPacket; (void) frameNumber; (void) remoteSystemAddress; (void) time; (void) isSend;}
/// Called when we get an ack for a message we reliably sent
/// \pre To be called, UsesReliabilityLayer() must return true
/// \param[in] messageNumber The numerical identifier for which message this is
/// \param[in] remoteSystemAddress The player we sent or got this packet from
/// \param[in] time The current time as returned by RakNet::GetTimeMS()
virtual void OnAck(unsigned int messageNumber, SystemAddress remoteSystemAddress, RakNet::TimeMS time) {(void) messageNumber; (void) remoteSystemAddress; (void) time;}
/// System called RakPeerInterface::PushBackPacket
/// \param[in] data The data being sent
/// \param[in] bitsUsed How many bits long \a data is
/// \param[in] remoteSystemAddress The player we sent or got this packet from
virtual void OnPushBackPacket(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) {(void) data; (void) bitsUsed; (void) remoteSystemAddress;}
RakPeerInterface *GetRakPeerInterface(void) const {return rakPeerInterface;}
RakNetGUID GetMyGUIDUnified(void) const;
/// \internal
void SetRakPeerInterface( RakPeerInterface *ptr );
#if _RAKNET_SUPPORT_TCPInterface==1
/// \internal
void SetTCPInterface( TCPInterface *ptr );
#endif
protected:
// Send through either rakPeerInterface or tcpInterface, whichever is available
void SendUnified( const RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast );
void SendUnified( const char * data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast );
bool SendListUnified( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast );
Packet *AllocatePacketUnified(unsigned dataSize);
void PushBackPacketUnified(Packet *packet, bool pushAtHead);
void DeallocPacketUnified(Packet *packet);
// Filled automatically in when attached
RakPeerInterface *rakPeerInterface;
#if _RAKNET_SUPPORT_TCPInterface==1
TCPInterface *tcpInterface;
#endif
};11、郵箱發送
直接應用EmailSender,便可實現郵箱發送,類定義如下:
class RAK_DLL_EXPORT EmailSender
{
public:
// GetInstance() and DestroyInstance(instance*)
STATIC_FACTORY_DECLARATIONS(EmailSender)
/// \brief Sends an email.
/// \param[in] hostAddress The address of the email server.
/// \param[in] hostPort The port of the email server (usually 25)
/// \param[in] sender The email address you are sending from.
/// \param[in] recipient The email address you are sending to.
/// \param[in] senderName The email address you claim to be sending from
/// \param[in] recipientName The email address you claim to be sending to
/// \param[in] subject Email subject
/// \param[in] body Email body
/// \param[in] attachedFiles List of files to attach to the email. (Can be 0 to send none).
/// \param[in] doPrintf true to output SMTP info to console(for debugging?)
/// \param[in] password Used if the server uses AUTHENTICATE PLAIN over TLS (such as gmail)
/// \return 0 on success, otherwise a string indicating the error message
const char *Send(const char *hostAddress, unsigned short hostPort, const char *sender, const char *recipient, const char *senderName, const char *recipientName, const char *subject, const char *body, FileList *attachedFiles, bool doPrintf, const char *password);
protected:
const char *GetResponse(TCPInterface *tcpInterface, const SystemAddress &emailServer, bool doPrintf);
RakNetRandom rakNetRandom;
};測試如下:
int main()
{
printf("A C++ class used to send email, such as for servers.\n");
printf("TLS support (such as for Gmail) requires OPEN_SSL_CLIENT_SUPPORT to be defined\nin RakNetDefines.h.\n");
printf("Difficulty: Beginner\n\n");
RakNet::FileList fileList;
RakNet::EmailSender emailSender;
const char *quote = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.";
// const char base64Map[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// char output[1024];
// emailSender.Base64Encoding(quote, strlen(quote), output, base64Map);
// printf("%s", output);
char mailServer[128], senderUsername[128], receiver[128], password[128];
printf("Tests sending email.\n");
printf("Enter mail server: ");
Gets(mailServer,sizeof(mailServer));
if (mailServer[0]==0)
strcpy(mailServer, "smtp.gmail.com");
printf("Enter email account username: ");
Gets(senderUsername,sizeof(senderUsername));
if (senderUsername[0]==0)
strcpy(senderUsername, "subspacegod@gmail.com");
printf("Enter receiver email address: ");
Gets(receiver,sizeof(receiver));
if (receiver[0]==0)
strcpy(receiver, "rakkar@rakkar.org");
printf("Enter password needed to send: ");
Gets(password,sizeof(password));
// http://mail.google.com/support/bin/answer.py?hl=en&answer=13287
unsigned short hostPort;
if (strcmp(mailServer,"smtp.gmail.com")==0)
hostPort=465;
else
hostPort=25;
fileList.AddFile("quote.txt", "quote.txt", quote, (const unsigned int) strlen(quote), (const unsigned int) strlen(quote), FileListNodeContext(0,0,0,0), false);
const char *sendResult=emailSender.Send(mailServer,
hostPort,
senderUsername,
receiver,
senderUsername,
receiver,
"Test subject.",
"Test attachment body :).\n.\n..\n.\n(Should be .,.,..,.)\r\n.\r\n.\r\n..\r\n.\r\n(Should be .,.,..,.)12345\r\n.\r\n",
&fileList,
true,
password);
if (sendResult!=0)
printf("Send Failed! %s", sendResult);
else
printf("Success (probably).\n");
printf("Press enter to quit.\n");
char buff[256];
Gets(buff,sizeof(buff));
return 0;
}12、日志
流程操作的一種記錄。
主要實現類:
class RAK_DLL_EXPORT LogCommandParser : public CommandParserInterface
13、消息過濾插件
結構如下:
struct FilterSet
{
bool banOnFilterTimeExceed;
bool kickOnDisallowedMessage;
bool banOnDisallowedMessage;
RakNet::TimeMS disallowedMessageBanTimeMS;
RakNet::TimeMS timeExceedBanTimeMS;
RakNet::TimeMS maxMemberTimeMS;
void (*invalidMessageCallback)(RakPeerInterface *peer, AddressOrGUID systemAddress, int filterSetID, void *userData, unsigned char messageID);
void *disallowedCallbackUserData;
void (*timeoutCallback)(RakPeerInterface *peer, AddressOrGUID systemAddress, int filterSetID, void *userData);
void *timeoutUserData;
int filterSetID;
bool allowedIDs[MESSAGE_FILTER_MAX_MESSAGE_ID];
DataStructures::OrderedList allowedRPC4;
}; struct FilteredSystem
{
FilterSet *filter;
RakNet::TimeMS timeEnteredThisSet;
};主要實現類:class RAK_DLL_EXPORT MessageFilter : public PluginInterface2
關鍵成員:DataStructures::OrderedList filterList;
// Change to guid
DataStructures::Hash systemList;
14、雙向認證
單項認證:就是比如你有個密碼 用戶名 然後和服務器上的用戶信息進行比對 一致的話你們就可以建立連接.
雙向認證就是:你有個密碼 用戶名 你先發給服務器進行比對,如果一致服務器再把它的密碼用戶名發到你機器上與你機器上保留的用戶信息進行比對 如果還一致則建立鏈接!
實現類:
class RAK_DLL_EXPORT TwoWayAuthentication : public PluginInterface2
內部結構:/// \internal
struct PendingChallenge
{
RakNet::RakString identifier;
AddressOrGUID remoteSystem;
RakNet::Time time;
bool sentHash;
};
DataStructures::Queue outgoingChallenges;
/// \internal
struct NonceAndRemoteSystemRequest
{
char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH];
RakNet::AddressOrGUID remoteSystem;
unsigned short requestId;
RakNet::Time whenGenerated;
};
/// \internal
struct RAK_DLL_EXPORT NonceGenerator
{
NonceGenerator();
~NonceGenerator();
void GetNonce(char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH], unsigned short *requestId, RakNet::AddressOrGUID remoteSystem);
void GenerateNonce(char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH]);
bool GetNonceById(char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH], unsigned short requestId, RakNet::AddressOrGUID remoteSystem, bool popIfFound);
void Clear(void);
void ClearByAddress(RakNet::AddressOrGUID remoteSystem);
void Update(RakNet::Time curTime);
DataStructures::List generatedNonces;
unsigned short nextRequestId;
}; 關鍵成員: // Key is identifier, data is password
DataStructures::Hash passwords;
15、傳輸接口
class RAK_DLL_EXPORT TransportInterface //用於發送和接收的數據
{
public:
TransportInterface() {}
virtual ~TransportInterface() {}
/// Start the transport provider on the indicated port.
/// \param[in] port The port to start the transport provider on
/// \param[in] serverMode If true, you should allow incoming connections (I don't actually use this anywhere)
/// \return Return true on success, false on failure.
virtual bool Start(unsigned short port, bool serverMode)=0;
/// Stop the transport provider. You can clear memory and shutdown threads here.
virtual void Stop(void)=0;
/// Send a null-terminated string to \a systemAddress
/// If your transport method requires particular formatting of the outgoing data (e.g. you don't just send strings) you can do it here
/// and parse it out in Receive().
/// \param[in] systemAddress The player to send the string to
/// \param[in] data format specifier - same as RAKNET_DEBUG_PRINTF
/// \param[in] ... format specification arguments - same as RAKNET_DEBUG_PRINTF
virtual void Send( SystemAddress systemAddress, const char *data, ... )=0;
/// Disconnect \a systemAddress . The binary address and port defines the SystemAddress structure.
/// \param[in] systemAddress The player/address to disconnect
virtual void CloseConnection( SystemAddress systemAddress )=0;
/// Return a string. The string should be allocated and written to Packet::data .
/// The byte length should be written to Packet::length . The player/address should be written to Packet::systemAddress
/// If your transport protocol adds special formatting to the data stream you should parse it out before returning it in the packet
/// and thus only return a string in Packet::data
/// \return The packet structure containing the result of Receive, or 0 if no data is available
virtual Packet* Receive( void )=0;
/// Deallocate the Packet structure returned by Receive
/// \param[in] The packet to deallocate
virtual void DeallocatePacket( Packet *packet )=0;
/// If a new system connects to you, you should queue that event and return the systemAddress/address of that player in this function.
/// \return The SystemAddress/address of the system
virtual SystemAddress HasNewIncomingConnection(void)=0;
/// If a system loses the connection, you should queue that event and return the systemAddress/address of that player in this function.
/// \return The SystemAddress/address of the system
virtual SystemAddress HasLostConnection(void)=0;
/// Your transport provider can itself have command parsers if the transport layer has user-modifiable features
/// For example, your transport layer may have a password which you want remote users to be able to set or you may want
/// to allow remote users to turn on or off command echo
/// \return 0 if you do not need a command parser - otherwise the desired derivation of CommandParserInterface
virtual CommandParserInterface* GetCommandParser(void)=0;
protected:
};16、網絡類型匹配
網絡類型匹配分客戶端和服務端,如下:
/// All possible types of NATs (except NAT_TYPE_COUNT, which is an internal value)
enum NATTypeDetectionResult //匹配結果
{
/// Works with anyone
NAT_TYPE_NONE,
/// Accepts any datagrams to a port that has been previously used. Will accept the first datagram from the remote peer.
NAT_TYPE_FULL_CONE,
/// Accepts datagrams to a port as long as the datagram source IP address is a system we have already sent to. Will accept the first datagram if both systems send simultaneously. Otherwise, will accept the first datagram after we have sent one datagram.
NAT_TYPE_ADDRESS_RESTRICTED,
/// Same as address-restricted cone NAT, but we had to send to both the correct remote IP address and correct remote port. The same source address and port to a different destination uses the same mapping.
NAT_TYPE_PORT_RESTRICTED,
/// A different port is chosen for every remote destination. The same source address and port to a different destination uses a different mapping. Since the port will be different, the first external punchthrough attempt will fail. For this to work it requires port-prediction (MAX_PREDICTIVE_PORT_RANGE>1) and that the router chooses ports sequentially.
NAT_TYPE_SYMMETRIC,
/// Hasn't been determined. NATTypeDetectionClient does not use this, but other plugins might
NAT_TYPE_UNKNOWN,
/// In progress. NATTypeDetectionClient does not use this, but other plugins might
NAT_TYPE_DETECTION_IN_PROGRESS,
/// Didn't bother figuring it out, as we support UPNP, so it is equivalent to NAT_TYPE_NONE. NATTypeDetectionClient does not use this, but other plugins might
NAT_TYPE_SUPPORTS_UPNP,
/// \internal Must be last
NAT_TYPE_COUNT
}; class RAK_DLL_EXPORT NatTypeDetectionClient : public PluginInterface2, public RNS2EventHandler
{ //客戶端
public:
// GetInstance() and DestroyInstance(instance*)
STATIC_FACTORY_DECLARATIONS(NatTypeDetectionClient)
// Constructor
NatTypeDetectionClient();
// Destructor
virtual ~NatTypeDetectionClient();
/// Send the message to the server to detect the nat type
/// Server must be running NatTypeDetectionServer
/// We must already be connected to the server
/// \param[in] serverAddress address of the server
void DetectNATType(SystemAddress _serverAddress);
/// \internal For plugin handling
virtual void Update(void);
/// \internal For plugin handling
virtual PluginReceiveResult OnReceive(Packet *packet);
virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
virtual void OnRakPeerShutdown(void);
virtual void OnDetach(void);
virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct);
virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line);
virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line);
protected:
DataStructures::Queue bufferedPackets;
SimpleMutex bufferedPacketsMutex;
RakNetSocket2* c2;
//unsigned short c2Port;
void Shutdown(void);
void OnCompletion(NATTypeDetectionResult result);
bool IsInProgress(void) const;
void OnTestPortRestricted(Packet *packet);
SystemAddress serverAddress;
}; class RAK_DLL_EXPORT NatTypeDetectionServer : public PluginInterface2, public RNS2EventHandler
{ //服務端
public:
// GetInstance() and DestroyInstance(instance*)
STATIC_FACTORY_DECLARATIONS(NatTypeDetectionServer)
// Constructor
NatTypeDetectionServer();
// Destructor
virtual ~NatTypeDetectionServer();
/// Start the system, binding to 3 external IPs not already in useS
/// \param[in] nonRakNetIP2 First unused external IP
/// \param[in] nonRakNetIP3 Second unused external IP
/// \param[in] nonRakNetIP4 Third unused external IP
void Startup(
const char *nonRakNetIP2,
const char *nonRakNetIP3,
const char *nonRakNetIP4
#ifdef __native_client__
,_PP_Instance_ chromeInstance
#endif
);
// Releases the sockets created in Startup();
void Shutdown(void);
/// \internal For plugin handling
virtual void Update(void);
/// \internal For plugin handling
virtual PluginReceiveResult OnReceive(Packet *packet);
virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
enum NATDetectionState
{
STATE_NONE,
STATE_TESTING_NONE_1,
STATE_TESTING_NONE_2,
STATE_TESTING_FULL_CONE_1,
STATE_TESTING_FULL_CONE_2,
STATE_TESTING_ADDRESS_RESTRICTED_1,
STATE_TESTING_ADDRESS_RESTRICTED_2,
STATE_TESTING_PORT_RESTRICTED_1,
STATE_TESTING_PORT_RESTRICTED_2,
STATE_DONE,
};
struct NATDetectionAttempt
{
SystemAddress systemAddress;
NATDetectionState detectionState;
RakNet::TimeMS nextStateTime;
RakNet::TimeMS timeBetweenAttempts;
unsigned short c2Port;
RakNetGUID guid;
};
virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct);
virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line);
virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line);
protected:
DataStructures::Queue bufferedPackets;
SimpleMutex bufferedPacketsMutex;
void OnDetectionRequest(Packet *packet);
DataStructures::List natDetectionAttempts;
unsigned int GetDetectionAttemptIndex(const SystemAddress &sa);
unsigned int GetDetectionAttemptIndex(RakNetGUID guid);
// s1p1 is rakpeer itself
RakNetSocket2 *s1p2,*s2p3,*s3p4,*s4p5;
//unsigned short s1p2Port, s2p3Port, s3p4Port, s4p5Port;
char s3p4Address[64];
}; 17、TCP接口
struct RemoteClient //存儲有關遠程客戶端的信息
{
RemoteClient() {
#if OPEN_SSL_CLIENT_SUPPORT==1
ssl=0;
#endif
isActive=false;
#if !defined(WINDOWS_STORE_RT)
socket=0;
#endif
}
__TCPSOCKET__ socket;
SystemAddress systemAddress;
DataStructures::ByteQueue outgoingData;
bool isActive;
SimpleMutex outgoingDataMutex;
SimpleMutex isActiveMutex;
#if OPEN_SSL_CLIENT_SUPPORT==1
SSL* ssl;
bool InitSSL(SSL_CTX* ctx, SSL_METHOD *meth);
void DisconnectSSL(void);
void FreeSSL(void);
int Send(const char *data, unsigned int length);
int Recv(char *data, const int dataSize);
#else
int Send(const char *data, unsigned int length);
int Recv(char *data, const int dataSize);
#endif
void Reset(void)
{
outgoingDataMutex.Lock();
outgoingData.Clear(_FILE_AND_LINE_);
outgoingDataMutex.Unlock();
}
void SetActive(bool a);
void SendOrBuffer(const char **data, const unsigned int *lengths, const int numParameters);
};實現類:
class RAK_DLL_EXPORT TCPInterface //簡單的TCP服務器多線程
18、RPC4插件
enum RPCErrorCodes //錯誤碼
{
/// Named function was not registered with RegisterFunction(). Check your spelling.
RPC_ERROR_FUNCTION_NOT_REGISTERED,
};
/// \brief Instantiate this class globally if you want to register a function with RPC4 at the global space
class RAK_DLL_EXPORT RPC4GlobalRegistration //記錄
{
public:
/// \brief Queue a call to RPC4::RegisterFunction() globally. Actual call occurs once RPC4 is attached to an instance of RakPeer or TCPInterface.
RPC4GlobalRegistration(const char* uniqueID, void ( *functionPointer ) ( RakNet::BitStream *userData, Packet *packet ));
/// \brief Queue a call to RPC4::RegisterSlot() globally. Actual call occurs once RPC4 is attached to an instance of RakPeer or TCPInterface.
RPC4GlobalRegistration(const char* uniqueID, void ( *functionPointer ) ( RakNet::BitStream *userData, Packet *packet ), int callPriority);
/// \brief Queue a call to RPC4::RegisterBlockingFunction() globally. Actual call occurs once RPC4 is attached to an instance of RakPeer or TCPInterface.
RPC4GlobalRegistration(const char* uniqueID, void ( *functionPointer ) ( RakNet::BitStream *userData, RakNet::BitStream *returnData, Packet *packet ));
/// \brief Queue a call to RPC4::RegisterLocalCallback() globally. Actual call occurs once RPC4 is attached to an instance of RakPeer or TCPInterface.
RPC4GlobalRegistration(const char* uniqueID, MessageID messageId);
};實現類: class RAK_DLL_EXPORT RPC4 : public PluginInterface2
19、補丁更新
定義結構:
enum PatchContext
{
PC_HASH_1_WITH_PATCH, //hash值
PC_HASH_2_WITH_PATCH,
PC_WRITE_FILE, //寫入文件
PC_ERROR_FILE_WRITE_FAILURE, //寫入文件失敗
PC_ERROR_PATCH_TARGET_MISSING, //目標文件丟失
PC_ERROR_PATCH_APPLICATION_FAILURE, //失敗
PC_ERROR_PATCH_RESULT_CHECKSUM_FAILURE, //檢測失敗
PC_NOTICE_WILL_COPY_ON_RESTART, //通知重新拷貝
PC_NOTICE_FILE_DOWNLOADED, //通知文件下載
PC_NOTICE_FILE_DOWNLOADED_PATCH, //通知文件下載更新
};實現類:class AutopatcherRepositoryInterface : public IncrementalReadInterface
{
public:
/// Get list of files added and deleted since a certain date. This is used by AutopatcherServer and not usually explicitly called.
/// \param[in] applicationName A null terminated string identifying the application
/// \param[out] addedFiles A list of the current versions of filenames with hashes as their data that were created after \a sinceData
/// \param[out] deletedFiles A list of the current versions of filenames that were deleted after \a sinceData
/// \param[in] An input date, in whatever format your repository uses
/// \param[out] currentDate The current server date, in whatever format your repository uses
/// \return True on success, false on failure.
//獲取改變的數據
virtual bool GetChangelistSinceDate(const char *applicationName, FileList *addedOrModifiedFilesWithHashData, FileList *deletedFiles, double sinceDate)=0;
/// Get patches (or files) for every file in input, assuming that input has a hash for each of those files.
/// \param[in] applicationName A null terminated string identifying the application
/// \param[in] input A list of files with SHA1_LENGTH byte hashes to get from the database.
/// \param[out] patchList You should return list of files with either the filedata or the patch. This is a subset of \a input. The context data for each file will be either PC_WRITE_FILE (to just write the file) or PC_HASH_WITH_PATCH (to patch). If PC_HASH_WITH_PATCH, then the file contains a SHA1_LENGTH byte patch followed by the hash. The datalength is patchlength + SHA1_LENGTH
/// \param[out] currentDate The current server date, in whatever format your repository uses
/// \return 1 on success, 0 on database failure, -1 on tried to download original unmodified file
//獲取補丁包
virtual int GetPatches(const char *applicationName, FileList *input, bool allowDownloadOfOriginalUnmodifiedFiles, FileList *patchList)=0;
/// For the most recent update, return files that were patched, added, or deleted. For files that were patched, return both the patch in \a patchedFiles and the current version in \a updatedFiles
/// \param[in,out] applicationName Name of the application to get patches for. If empty, uses the most recently updated application, and the string will be updated to reflect this name.
/// \param[out] patchedFiles A list of patched files with op PC_HASH_2_WITH_PATCH. It has 2 hashes, the priorHash and the currentHash. The currentHash is checked on the client after patching for patch success. The priorHash is checked in AutopatcherServer::OnGetPatch() to see if the client is able to hash with the version they currently have
/// \param[out] patchedFiles A list of new files. It contains the actual data in addition to the filename
/// \param[out] addedOrModifiedFileHashes A list of file hashes that were either modified or new. This is returned to the client when replying to ID_AUTOPATCHER_CREATION_LIST, which tells the client what files have changed on the server since a certain date
/// \param[out] deletedFiles A list of the current versions of filenames that were deleted in the most recent patch
/// \param[out] whenPatched time in seconds since epoch when patched. Use time() function to get this in C
/// \return true on success, false on failure
//獲取補丁
virtual bool GetMostRecentChangelistWithPatches(
RakNet::RakString &applicationName,
FileList *patchedFiles,
FileList *updatedFiles,
FileList *addedOrModifiedFileHashes,
FileList *deletedFiles,
double *priorRowPatchTime,
double *mostRecentRowPatchTime)=0;
/// \return Whatever this function returns is sent from the AutopatcherServer to the AutopatcherClient when one of the above functions returns false.
//獲取最近的錯誤
virtual const char *GetLastError(void) const=0;
/// \return Passed to FileListTransfer::Send() as the _chunkSize parameter.
//獲取增量數據
virtual const int GetIncrementalReadChunkSize(void) const=0;
};20、路由器
內部結構:
enum Router2RequestStates
{
R2RS_REQUEST_STATE_QUERY_FORWARDING,
REQUEST_STATE_REQUEST_FORWARDING,
};
struct ConnectionRequestSystem
{
RakNetGUID guid;
int pingToEndpoint;
unsigned short usedForwardingEntries;
};
struct ConnnectRequest
{
ConnnectRequest();
~ConnnectRequest();
DataStructures::List connectionRequestSystems;
SimpleMutex connectionRequestSystemsMutex;
Router2RequestStates requestState;
RakNet::TimeMS pingTimeout;
RakNetGUID endpointGuid;
RakNetGUID lastRequestedForwardingSystem;
bool returnConnectionLostOnFailure;
unsigned int GetGuidIndex(RakNetGUID guid);
};
struct MiniPunchRequest
{
RakNetGUID endpointGuid;
SystemAddress endpointAddress;
bool gotReplyFromEndpoint;
RakNetGUID sourceGuid;
SystemAddress sourceAddress;
bool gotReplyFromSource;
RakNet::TimeMS timeout;
RakNet::TimeMS nextAction;
unsigned short forwardingPort;
__UDPSOCKET__ forwardingSocket;
};
struct ForwardedConnection
{
RakNetGUID endpointGuid;
RakNetGUID intermediaryGuid;
SystemAddress intermediaryAddress;
bool returnConnectionLostOnFailure;
bool weInitiatedForwarding;
}; class RAK_DLL_EXPORT Router2 : public PluginInterface2 //通過一個共享的連接路由連接系統
{
public:
// GetInstance() and DestroyInstance(instance*)
STATIC_FACTORY_DECLARATIONS(Router2)
Router2();
virtual ~Router2();
/// Sets the socket family to use, either IPV4 or IPV6
/// \param[in] socketFamily For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC.
void SetSocketFamily(unsigned short _socketFamily);
/// \note The SystemAddress for a connection should not be used - always use RakNetGuid as the address can change at any time.
/// When the address changes, you will get ID_ROUTER_2_REROUTED
void EstablishRouting(RakNetGUID endpointGuid);
/// Set the maximum number of bidirectional connections this system will support
/// Defaults to 0
void SetMaximumForwardingRequests(int max);
/// For testing and debugging
void SetDebugInterface(Router2DebugInterface *_debugInterface);
/// Get the pointer passed to SetDebugInterface()
Router2DebugInterface *GetDebugInterface(void) const;
// --------------------------------------------------------------------------------------------
// Packet handling functions
// --------------------------------------------------------------------------------------------
virtual PluginReceiveResult OnReceive(Packet *packet);
virtual void Update(void);
virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason);
virtual void OnRakPeerShutdown(void);
unsigned int GetConnectionRequestIndex(RakNetGUID endpointGuid);
protected:
bool UpdateForwarding(ConnnectRequest* connectionRequest);
void RemoveConnectionRequest(unsigned int connectionRequestIndex);
void RequestForwarding(ConnnectRequest* connectionRequest);
void OnQueryForwarding(Packet *packet);
void OnQueryForwardingReply(Packet *packet);
void OnRequestForwarding(Packet *packet);
void OnRerouted(Packet *packet);
void OnMiniPunchReply(Packet *packet);
void OnMiniPunchReplyBounce(Packet *packet);
bool OnForwardingSuccess(Packet *packet);
int GetLargestPingAmongConnectedSystems(void) const;
void ReturnToUser(MessageID messageId, RakNetGUID endpointGuid, const SystemAddress &systemAddress, bool wasGeneratedLocally);
bool ConnectInternal(RakNetGUID endpointGuid, bool returnConnectionLostOnFailure);
UDPForwarder *udpForwarder;
int maximumForwardingRequests;
SimpleMutex connectionRequestsMutex, miniPunchesInProgressMutex, forwardedConnectionListMutex;
DataStructures::List connectionRequests;
DataStructures::List miniPunchesInProgress;
// Forwarding we have initiated
DataStructures::List forwardedConnectionList;
void ClearConnectionRequests(void);
void ClearMinipunches(void);
void ClearForwardedConnections(void);
void ClearAll(void);
int ReturnFailureOnCannotForward(RakNetGUID sourceGuid, RakNetGUID endpointGuid);
void SendFailureOnCannotForward(RakNetGUID sourceGuid, RakNetGUID endpointGuid);
void SendForwardingSuccess(MessageID messageId, RakNetGUID sourceGuid, RakNetGUID endpointGuid, unsigned short sourceToDstPort);
void SendOOBFromRakNetPort(OutOfBandIdentifiers oob, BitStream *extraData, SystemAddress sa);
void SendOOBFromSpecifiedSocket(OutOfBandIdentifiers oob, SystemAddress sa, __UDPSOCKET__ socket);
void SendOOBMessages(MiniPunchRequest *mpr);
Router2DebugInterface *debugInterface;
unsigned short socketFamily;
}; 實例: /// RakNet::BitStream bs(packet->data, packet->length, false);
/// bs.IgnoreBytes(sizeof(MessageID));
/// RakNetGUID endpointGuid;
/// bs.Read(endpointGuid);
/// unsigned short sourceToDestPort;
/// bs.Read(sourceToDestPort);
/// char ipAddressString[32];
/// packet->systemAddress.ToString(false, ipAddressString);
/// rakPeerInterface->EstablishRouting(ipAddressString, sourceToDestPort, 0,0);
21、延遲插件
enum RelayPluginEnums
{
// Server handled messages
RPE_MESSAGE_TO_SERVER_FROM_CLIENT,
RPE_ADD_CLIENT_REQUEST_FROM_CLIENT,
RPE_REMOVE_CLIENT_REQUEST_FROM_CLIENT,
RPE_GROUP_MESSAGE_FROM_CLIENT,
RPE_JOIN_GROUP_REQUEST_FROM_CLIENT,
RPE_LEAVE_GROUP_REQUEST_FROM_CLIENT,
RPE_GET_GROUP_LIST_REQUEST_FROM_CLIENT,
// Client handled messages
RPE_MESSAGE_TO_CLIENT_FROM_SERVER,
RPE_ADD_CLIENT_NOT_ALLOWED,
RPE_ADD_CLIENT_TARGET_NOT_CONNECTED,
RPE_ADD_CLIENT_NAME_ALREADY_IN_USE,
RPE_ADD_CLIENT_SUCCESS,
RPE_USER_ENTERED_ROOM,
RPE_USER_LEFT_ROOM,
RPE_GROUP_MSG_FROM_SERVER,
RPE_GET_GROUP_LIST_REPLY_FROM_SERVER,
RPE_JOIN_GROUP_SUCCESS,
RPE_JOIN_GROUP_FAILURE,
};內部結構: struct StrAndGuidAndRoom
{
RakString str;
RakNetGUID guid;
RakString currentRoom;
};
struct StrAndGuid
{
RakString str;
RakNetGUID guid;
};
struct RP_Group
{
RakString roomName;
DataStructures::List usersInRoom;
}; 實現類:class RAK_DLL_EXPORT RelayPlugin : public PluginInterface2 //通過一個字符串的識別遠程系統
關鍵成員: DataStructures::Hash strToGuidHash;
DataStructures::Hash guidToStrHash;
DataStructures::List chatRooms;
bool acceptAddParticipantRequests;
22、文件傳輸
class FileListTransferCBInterface //文件傳輸回調接口
{
public:
// Note: If this structure is changed the struct in the swig files need to be changed as well
struct OnFileStruct
{
/// \brief The index into the set of files, from 0 to numberOfFilesInThisSet
unsigned fileIndex;
/// \brief The name of the file
char fileName[512];
/// \brief The data pointed to by the file
char *fileData;
/// \brief The amount of data to be downloaded for this file
BitSize_t byteLengthOfThisFile;
/// \brief How many bytes of this file has been downloaded
BitSize_t bytesDownloadedForThisFile;
/// \brief Files are transmitted in sets, where more than one set of files can be transmitted at the same time.
/// \details This is the identifier for the set, which is returned by FileListTransfer::SetupReceive
unsigned short setID;
/// \brief The number of files that are in this set.
unsigned numberOfFilesInThisSet;
/// \brief The total length of the transmitted files for this set, after being uncompressed
unsigned byteLengthOfThisSet;
/// \brief The total length, in bytes, downloaded for this set.
unsigned bytesDownloadedForThisSet;
/// \brief User data passed to one of the functions in the FileList class.
/// \details However, on error, this is instead changed to one of the enumerations in the PatchContext structure.
FileListNodeContext context;
/// \brief Who sent this file
SystemAddress senderSystemAddress;
/// \brief Who sent this file. Not valid when using TCP, only RakPeer (UDP)
RakNetGUID senderGuid;
};
// Note: If this structure is changed the struct in the swig files need to be changed as well
struct FileProgressStruct
{
/// \param[out] onFileStruct General information about this file, such as the filename and the first \a partLength bytes. You do NOT need to save this data yourself. The complete file will arrive normally.
OnFileStruct *onFileStruct;
/// \param[out] partCount The zero based index into partTotal. The percentage complete done of this file is 100 * (partCount+1)/partTotal
unsigned int partCount;
/// \param[out] partTotal The total number of parts this file was split into. Each part will be roughly the MTU size, minus the UDP header and RakNet headers
unsigned int partTotal;
/// \param[out] dataChunkLength How many bytes long firstDataChunk and iriDataChunk are
unsigned int dataChunkLength;
/// \param[out] firstDataChunk The first \a partLength of the final file. If you store identifying information about the file in the first \a partLength bytes, you can read them while the download is taking place. If this hasn't arrived yet, firstDataChunk will be 0
char *firstDataChunk;
/// \param[out] iriDataChunk If the remote system is sending this file using IncrementalReadInterface, then this is the chunk we just downloaded. It will not exist in memory after this callback. You should either store this to disk, or in memory. If it is 0, then the file is smaller than one chunk, and will be held in memory automatically
char *iriDataChunk;
/// \param[out] iriWriteOffset Offset in bytes from the start of the file for the data pointed to by iriDataChunk
unsigned int iriWriteOffset;
/// \param[out] Who sent this file
SystemAddress senderSystemAddress;
/// \param[out] Who sent this file. Not valid when using TCP, only RakPeer (UDP)
RakNetGUID senderGuid;
/// \param[in] allocateIrIDataChunkAutomatically If true, then RakNet will hold iriDataChunk for you and return it in OnFile. Defaults to true
bool allocateIrIDataChunkAutomatically;
};
struct DownloadCompleteStruct
{
/// \brief Files are transmitted in sets, where more than one set of files can be transmitted at the same time.
/// \details This is the identifier for the set, which is returned by FileListTransfer::SetupReceive
unsigned short setID;
/// \brief The number of files that are in this set.
unsigned numberOfFilesInThisSet;
/// \brief The total length of the transmitted files for this set, after being uncompressed
unsigned byteLengthOfThisSet;
/// \brief Who sent this file
SystemAddress senderSystemAddress;
/// \brief Who sent this file. Not valid when using TCP, only RakPeer (UDP)
RakNetGUID senderGuid;
};
FileListTransferCBInterface() {}
virtual ~FileListTransferCBInterface() {}
/// \brief Got a file.
/// \details This structure is only valid for the duration of this function call.
/// \return Return true to have RakNet delete the memory allocated to hold this file for this function call.
virtual bool OnFile(OnFileStruct *onFileStruct)=0;
/// \brief Got part of a big file internally in RakNet
/// \details This is called in one of two circumstances: Either the transport layer is returning ID_PROGRESS_NOTIFICATION, or you got a block via IncrementalReadInterface
/// If the transport layer is returning ID_PROGRESS_NOTIFICATION (see RakPeer::SetSplitMessageProgressInterval()) then FileProgressStruct::iriDataChunk will be 0.
/// If this is a block via IncrementalReadInterface, then iriDataChunk will point to the block just downloaded.
/// If not using IncrementalReadInterface, then you only care about partCount and partTotal to tell how far the download has progressed. YOu can use firstDataChunk to read the first part of the file if desired. The file is usable when you get the OnFile callback.
/// If using IncrementalReadInterface and you let RakNet buffer the files in memory (default), then it is the same as above. The file is usable when you get the OnFile callback.
/// If using IncrementalReadInterface and you do not let RakNet buffer the files in memory, then set allocateIrIDataChunkAutomatically to false. Write the file to disk whenever you get OnFileProgress and iriDataChunk is not 0, and ignore OnFile.
virtual void OnFileProgress(FileProgressStruct *fps)=0;
/// \brief Called while the handler is active by FileListTransfer
/// \details Return false when you are done with the class.
/// At that point OnDereference will be called and the class will no longer be maintained by the FileListTransfer plugin.
virtual bool Update(void) {return true;}
/// \brief Called when the download is completed.
/// \details If you are finished with this class, return false.
/// At that point OnDereference will be called and the class will no longer be maintained by the FileListTransfer plugin.
/// Otherwise return true, and Update will continue to be called.
virtual bool OnDownloadComplete(DownloadCompleteStruct *dcs) {(void) dcs; return false;}
/// \brief This function is called when this instance is about to be dereferenced by the FileListTransfer plugin.
/// \details Update will no longer be called.
/// It will will be deleted automatically if true was passed to FileListTransfer::SetupReceive::deleteHandler
/// Otherwise it is up to you to delete it yourself.
virtual void OnDereference(void) {}
};內部結構: struct FileToPush
{
FileListNode fileListNode;
PacketPriority packetPriority;
char orderingChannel;
unsigned int currentOffset;
////unsigned short setID;
unsigned int setIndex;
IncrementalReadInterface *incrementalReadInterface;
unsigned int chunkSize;
};
struct FileToPushRecipient
{
unsigned int refCount;
SimpleMutex refCountMutex;
void DeleteThis(void);
void AddRef(void);
void Deref(void);
SystemAddress systemAddress;
unsigned short setId;
//// SimpleMutex filesToPushMutex;
DataStructures::Queue filesToPush;
};
struct ThreadData
{
FileListTransfer *fileListTransfer;
SystemAddress systemAddress;
unsigned short setId;
}; 實現類:class RAK_DLL_EXPORT FileListTransfer : public PluginInterface2
23、控制服務器
class RAK_DLL_EXPORT ConsoleServer //遠程控制台應用程序
{
public:
// GetInstance() and DestroyInstance(instance*)
STATIC_FACTORY_DECLARATIONS(ConsoleServer)
ConsoleServer();
~ConsoleServer();
/// \brief Call this with a derivation of TransportInterface so that the console server can send and receive commands
/// \param[in] transportInterface Your interface to use.
/// \param[in] port The port to host on. Telnet uses port 23 by default. RakNet can use whatever you want.
void SetTransportProvider(TransportInterface *transportInterface, unsigned short port);
/// \brief Add an implementation of CommandParserInterface to the list of command parsers.
/// \param[in] commandParserInterface The command parser referred to
void AddCommandParser(CommandParserInterface *commandParserInterface);
/// \brief Remove an implementation of CommandParserInterface previously added with AddCommandParser().
/// \param[in] commandParserInterface The command parser referred to
void RemoveCommandParser(CommandParserInterface *commandParserInterface);
/// \brief Call update to read packet sent from your TransportInterface.
/// You should do this fairly frequently.
void Update(void);
/// \brief Sets a prompt to show when waiting for user input.
/// \details Pass an empty string to clear the prompt
/// Defaults to no prompt
/// \param[in] _prompt Null-terminated string of the prompt to use. If you want a newline, be sure to use /r/n
void SetPrompt(const char *_prompt);
protected:
void ListParsers(SystemAddress systemAddress);
void ShowPrompt(SystemAddress systemAddress);
TransportInterface *transport;
DataStructures::List commandParserList;
char* password[256];
char *prompt;
}; 24、連接圖
class RAK_DLL_EXPORT ConnectionGraph2 : public PluginInterface2 //跳轉連接圖插件
{
public:
// GetInstance() and DestroyInstance(instance*)
STATIC_FACTORY_DECLARATIONS(ConnectionGraph2)
ConnectionGraph2();
~ConnectionGraph2();
/// \brief Given a remote system identified by RakNetGUID, return the list of SystemAddresses and RakNetGUIDs they are connected to
/// \param[in] remoteSystemGuid Which system we are referring to. This only works for remote systems, not ourselves.
/// \param[out] saOut A preallocated array to hold the output list of SystemAddress. Can be 0 if you don't care.
/// \param[out] guidOut A preallocated array to hold the output list of RakNetGUID. Can be 0 if you don't care.
/// \param[in,out] outLength On input, the size of \a saOut and \a guidOut. On output, modified to reflect the number of elements actually written
/// \return True if \a remoteSystemGuid was found. Otherwise false, and \a saOut, \a guidOut remain unchanged. \a outLength will be set to 0.
bool GetConnectionListForRemoteSystem(RakNetGUID remoteSystemGuid, SystemAddress *saOut, RakNetGUID *guidOut, unsigned int *outLength);
/// Returns if g1 is connected to g2
bool ConnectionExists(RakNetGUID g1, RakNetGUID g2);
/// Returns the average ping between two systems in the connection graph. Returns -1 if no connection exists between those systems
uint16_t GetPingBetweenSystems(RakNetGUID g1, RakNetGUID g2) const;
/// Returns the system with the lowest average ping among all its connections.
/// If you need one system in the peer to peer group to relay data, have the FullyConnectedMesh2 host call this function after host migration, and use that system
RakNetGUID GetLowestAveragePingSystem(void) const;
/// \brief If called with false, then new connections are only added to the connection graph when you call ProcessNewConnection();
/// \details This is useful if you want to perform validation before connecting a system to a mesh, or if you want a submesh (for example a server cloud)
/// \param[in] b True to automatically call ProcessNewConnection() on any new connection, false to not do so. Defaults to true.
void SetAutoProcessNewConnections(bool b);
/// \brief Returns value passed to SetAutoProcessNewConnections()
/// \return Value passed to SetAutoProcessNewConnections(), or the default of true if it was never called
bool GetAutoProcessNewConnections(void) const;
/// \brief If you call SetAutoProcessNewConnections(false);, then you will need to manually call ProcessNewConnection() on new connections
/// \details On ID_NEW_INCOMING_CONNECTION or ID_CONNECTION_REQUEST_ACCEPTED, adds that system to the graph
/// Do not call ProcessNewConnection() manually otherwise
/// \param[in] The packet->SystemAddress member
/// \param[in] The packet->guid member
void AddParticipant(const SystemAddress &systemAddress, RakNetGUID rakNetGUID);
/// Get the participants added with AddParticipant()
/// \param[out] participantList Participants added with AddParticipant();
void GetParticipantList(DataStructures::OrderedList &participantList);
/// \internal
struct SystemAddressAndGuid
{
SystemAddress systemAddress;
RakNetGUID guid;
uint16_t sendersPingToThatSystem;
};
/// \internal
static int SystemAddressAndGuidComp( const SystemAddressAndGuid &key, const SystemAddressAndGuid &data );
/// \internal
struct RemoteSystem
{
DataStructures::OrderedList remoteConnections;
RakNetGUID guid;
};
/// \internal
static int RemoteSystemComp( const RakNetGUID &key, RemoteSystem * const &data );
protected:
/// \internal
virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
/// \internal
virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
/// \internal
virtual PluginReceiveResult OnReceive(Packet *packet);
// List of systems I am connected to, which in turn stores which systems they are connected to
DataStructures::OrderedList remoteSystems;
bool autoProcessNewConnections;
}; 25、雲端
雲端有服務端和客戶端之分。
class RAK_DLL_EXPORT CloudAllocator //遠端生成
{
public:
CloudAllocator() {}
virtual ~CloudAllocator() {}
/// \brief Allocate a row
virtual CloudQueryRow* AllocateCloudQueryRow(void);
/// \brief Free a row
virtual void DeallocateCloudQueryRow(CloudQueryRow *row);
/// \brief Allocate CloudQueryRow::data
virtual unsigned char *AllocateRowData(uint32_t bytesNeededForData);
/// \brief Free CloudQueryRow::data
virtual void DeallocateRowData(void *data);
};struct RAK_DLL_EXPORT CloudKey //雲端密鑰
{
CloudKey() {}
CloudKey(RakNet::RakString _primaryKey, uint32_t _secondaryKey) : primaryKey(_primaryKey), secondaryKey(_secondaryKey) {}
~CloudKey() {}
/// Identifies the primary key. This is intended to be a major category, such as the name of the application
/// Must be non-empty
RakNet::RakString primaryKey;
/// Identifies the secondary key. This is intended to be a subcategory enumeration, such as PLAYER_LIST or RUNNING_SCORES
uint32_t secondaryKey;
/// \internal
void Serialize(bool writeToBitstream, BitStream *bitStream);
};struct RAK_DLL_EXPORT CloudQuery //雲端查詢
{
CloudQuery() {startingRowIndex=0; maxRowsToReturn=0; subscribeToResults=false;}
/// List of keys to query. Must be at least of length 1.
/// This query is run on uploads from all clients, and those that match the combination of primaryKey and secondaryKey are potentially returned
/// If you pass more than one key at a time, the results are concatenated so if you need to differentiate between queries then send two different queries
DataStructures::List keys;
/// If limiting the number of rows to return, this is the starting offset into the list. Has no effect unless maxRowsToReturn is > 0
uint32_t startingRowIndex;
/// Maximum number of rows to return. Actual number may still be less than this. Pass 0 to mean no-limit.
uint32_t maxRowsToReturn;
/// If true, automatically get updates as the results returned to you change. Unsubscribe with CloudMemoryClient::Unsubscribe()
bool subscribeToResults;
/// \internal
void Serialize(bool writeToBitstream, BitStream *bitStream);
}; struct RAK_DLL_EXPORT CloudQueryRow //雲端查詢節點
{
/// Key used to identify this data
CloudKey key;
/// Data uploaded
unsigned char *data;
/// Length of data uploaded
uint32_t length;
/// System address of server that is holding this data, and the client is connected to
SystemAddress serverSystemAddress;
/// System address of client that uploaded this data
SystemAddress clientSystemAddress;
/// RakNetGUID of server that is holding this data, and the client is connected to
RakNetGUID serverGUID;
/// RakNetGUID of client that uploaded this data
RakNetGUID clientGUID;
/// \internal
void Serialize(bool writeToBitstream, BitStream *bitStream, CloudAllocator *allocator);
};struct RAK_DLL_EXPORT CloudQueryResult //雲端查詢結果
{
/// Query originally passed to Download()
CloudQuery cloudQuery;
/// Results returned from query. If there were multiple keys in CloudQuery::keys then see resultKeyIndices
DataStructures::List rowsReturned;
/// If there were multiple keys in CloudQuery::keys, then each key is processed in order and the result concatenated to rowsReturned
/// The starting index of each query is written to resultKeyIndices
/// For example, if CloudQuery::keys had 4 keys, returning 3 rows, 0, rows, 5 rows, and 12 rows then
/// resultKeyIndices would be 0, 3, 3, 8
DataStructures::List resultKeyIndices;
/// Whatever was passed to CloudClient::Get() as CloudQuery::subscribeToResults
bool subscribeToResults;
/// \internal
void Serialize(bool writeToBitstream, BitStream *bitStream, CloudAllocator *allocator);
/// \internal
void SerializeHeader(bool writeToBitstream, BitStream *bitStream);
/// \internal
void SerializeNumRows(bool writeToBitstream, uint32_t &numRows, BitStream *bitStream);
/// \internal
void SerializeCloudQueryRows(bool writeToBitstream, uint32_t &numRows, BitStream *bitStream, CloudAllocator *allocator);
}; 客戶端有回調類:class RAK_DLL_EXPORT CloudClientCallback
{
public:
CloudClientCallback() {}
virtual ~CloudClientCallback() {}
/// \brief Called in response to ID_CLOUD_GET_RESPONSE
/// \param[out] result Contains the original query passed to Get(), and a list of rows returned.
/// \param[out] deallocateRowsAfterReturn CloudQueryResult::rowsReturned will be deallocated after the function returns by default. Set to false to not deallocate these pointers. The pointers are allocated through CloudAllocator.
virtual void OnGet(RakNet::CloudQueryResult *result, bool *deallocateRowsAfterReturn) {(void) result; (void) deallocateRowsAfterReturn;}
/// \brief Called in response to ID_CLOUD_SUBSCRIPTION_NOTIFICATION
/// \param[out] result Contains the row updated
/// \param[out] wasUpdated If true, the row was updated. If false, it was deleted. \a result will contain the last value just before deletion
/// \param[out] deallocateRowAfterReturn \a result will be deallocated after the function returns by default. Set to false to not deallocate these pointers. The pointers are allocated through CloudAllocator.
virtual void OnSubscriptionNotification(RakNet::CloudQueryRow *result, bool wasUpdated, bool *deallocateRowAfterReturn) {(void) result; (void) wasUpdated; (void) deallocateRowAfterReturn;}
};
客戶端實現類:class RAK_DLL_EXPORT CloudClient : public PluginInterface2
服務端有查詢過濾類:class RAK_DLL_EXPORT CloudServerQueryFilter
{
public:
CloudServerQueryFilter() {}
virtual ~CloudServerQueryFilter() {}
/// Called when a local client wants to post data
/// \return true to allow, false to reject
virtual bool OnPostRequest(RakNetGUID clientGuid, SystemAddress clientAddress, CloudKey key, uint32_t dataLength, const char *data)=0;
/// Called when a local client wants to release data that it has previously uploaded
/// \return true to allow, false to reject
virtual bool OnReleaseRequest(RakNetGUID clientGuid, SystemAddress clientAddress, DataStructures::List &cloudKeys)=0;
/// Called when a local client wants to query data
/// If you return false, the client will get no response at all
/// \return true to allow, false to reject
virtual bool OnGetRequest(RakNetGUID clientGuid, SystemAddress clientAddress, CloudQuery &query, DataStructures::List &specificSystems)=0;
/// Called when a local client wants to stop getting updates for data
/// If you return false, the client will keep getting updates for that data
/// \return true to allow, false to reject
virtual bool OnUnsubscribeRequest(RakNetGUID clientGuid, SystemAddress clientAddress, DataStructures::List &cloudKeys, DataStructures::List &specificSystems)=0;
}; 服務端實現類:class RAK_DLL_EXPORT CloudServer : public PluginInterface2, CloudAllocator
26、拷貝管理
enum //復制接口標志,用於啟用和禁用的函數調用的復制對象
{
REPLICA_RECEIVE_DESTRUCTION=1<<0,
REPLICA_RECEIVE_SERIALIZE=1<<1,
REPLICA_RECEIVE_SCOPE_CHANGE=1<<2,
REPLICA_SEND_CONSTRUCTION=1<<3,
REPLICA_SEND_DESTRUCTION=1<<4,
REPLICA_SEND_SCOPE_CHANGE=1<<5,
REPLICA_SEND_SERIALIZE=1<<6,
REPLICA_SET_ALL = 0xFF // Allow all of the above
};
enum ReplicaReturnResult //復制結果
{
/// This means call the function again later, with the same parameters
REPLICA_PROCESS_LATER,
/// This means we are done processing (the normal result to return)
REPLICA_PROCESSING_DONE,
/// This means cancel the processing - don't send any network messages and don't change the current state.
REPLICA_CANCEL_PROCESS,
/// Same as REPLICA_PROCESSING_DONE, where a message is sent, but does not clear the send bit.
/// Useful for multi-part sends with different reliability levels.
/// Only currently used by Replica::Serialize
REPLICA_PROCESS_AGAIN,
/// Only returned from the Replica::SendConstruction interface, means act as if the other system had this object but don't actually
/// Send a construction packet. This way you will still send scope and serialize packets to that system
REPLICA_PROCESS_IMPLICIT
};有關類:class RAK_DLL_EXPORT ReplicaManager3 : public PluginInterface2
class RAK_DLL_EXPORT Connection_RM3
class RAK_DLL_EXPORT Replica3 : public NetworkIDObject
template
class RAK_DLL_EXPORT Replica3Composite : public Replica3
27、飽和連接
內部結構:
/// \internal
struct FCM2Participant
{
FCM2Participant() {}
FCM2Participant(const FCM2Guid &_fcm2Guid, const RakNetGUID &_rakNetGuid) : fcm2Guid(_fcm2Guid), rakNetGuid(_rakNetGuid) {}
// Low half is a random number.
// High half is the order we connected in (totalConnectionCount)
FCM2Guid fcm2Guid;
RakNetGUID rakNetGuid;
// BitStream userContext;
};
enum JoinInProgressState
{
JIPS_PROCESSING,
JIPS_FAILED,
JIPS_CONNECTED,
JIPS_UNNECESSARY,
};
struct VerifiedJoinInProgressMember
{
SystemAddress systemAddress;
RakNetGUID guid;
JoinInProgressState joinInProgressState;
BitStream *userData;
bool workingFlag;
};
/// \internal
struct VerifiedJoinInProgress
{
RakNetGUID requester;
DataStructures::List vjipMembers;
//bool sentResults;
}; 實現類:class RAK_DLL_EXPORT FullyConnectedMesh2 : public PluginInterface2
測試代碼:Startup()
ourFCMGuid=unknown
totalConnectionCount=0
Set startupTime
AddParticipant()
if (sender by guid is a participant)
return;
AddParticipantInternal(guid);
if (ourFCMGuid==unknown)
Send to that system a request for their fcmGuid, totalConnectionCount. Inform startupTime.
else
Send to that system a request for their fcmGuid. Inform total connection count, our fcmGuid
OnRequestGuid()
if (sender by guid is not a participant)
{
// They added us as a participant, but we didn't add them. This can be caused by lag where both participants are not added at the same time.
// It doesn't affect the outcome as long as we still process the data
AddParticipantInternal(guid);
}
if (ourFCMGuid==unknown)
{
if (includedStartupTime)
{
// Nobody has a fcmGuid
if (their startup time is greater than our startup time)
ReplyConnectionCount(1);
else
ReplyConnectionCount(2);
}
else
{
// They have a fcmGuid, we do not
SetMaxTotalConnectionCount(remoteCount);
AssignTheirGuid()
GenerateOurGuid();
SendOurGuid(all);
}
}
else
{
if (includedStartupTime)
{
// We have a fcmGuid they do not
ReplyConnectionCount(totalConnectionCount+1);
SendOurGuid(sender);
}
else
{
// We both have fcmGuids
SetMaxTotalConnectionCount(remoteCount);
AssignTheirGuid();
SendOurGuid(sender);
}
}
OnReplyConnectionCount()
SetMaxTotalConnectionCount(remoteCount);
GenerateOurGuid();
SendOurGuid(allParticipants);
OnReceiveTheirGuid()
AssignTheirGuid()28、Http連接
有兩個Http封裝類:
class RAK_DLL_EXPORT HTTPConnection
{
public:
// GetInstance() and DestroyInstance(instance*)
STATIC_FACTORY_DECLARATIONS(HTTPConnection)
/// Returns a HTTP object associated with this tcp connection
HTTPConnection();
virtual ~HTTPConnection();
/// \pre tcp should already be started
void Init(TCPInterface *_tcp, const char *host, unsigned short port=80);
/// Submit data to the HTTP server
/// HTTP only allows one request at a time per connection
///
/// \pre IsBusy()==false
/// \param path the path on the remote server you want to POST to. For example "index.html"
/// \param data A NULL terminated string to submit to the server
/// \param contentType "Content-Type:" passed to post.
void Post(const char *path, const char *data, const char *_contentType="application/x-www-form-urlencoded");
/// Get a file from a webserver
/// \param path the path on the remote server you want to GET from. For example "index.html"
void Get(const char *path);
/// Is there a Read result ready?
bool HasRead(void) const;
/// Get one result from the server
/// \pre HasResult must return true
RakNet::RakString Read(void);
/// Call periodically to do time-based updates
void Update(void);
/// Returns the address of the server we are connected to
SystemAddress GetServerAddress(void) const;
/// Process an HTTP data packet returned from TCPInterface
/// Returns true when we have gotten all the data from the HTTP server.
/// If this returns true then it's safe to Post() another request
/// Deallocate the packet as usual via TCPInterface
/// \param packet NULL or a packet associated with our host and port
void ProcessTCPPacket(Packet *packet);
/// Results of HTTP requests. Standard response codes are < 999
/// ( define HTTP codes and our internal codes as needed )
enum ResponseCodes { NoBody=1001, OK=200, Deleted=1002 };
HTTPConnection& operator=(const HTTPConnection& rhs){(void) rhs; return *this;}
/// Encapsulates a raw HTTP response and response code
struct BadResponse
{
public:
BadResponse() {code=0;}
BadResponse(const unsigned char *_data, int _code)
: data((const char *)_data), code(_code) {}
BadResponse(const char *_data, int _code)
: data(_data), code(_code) {}
operator int () const { return code; }
RakNet::RakString data;
int code; // ResponseCodes
};
/// Queued events of failed exchanges with the HTTP server
bool HasBadResponse(int *code, RakNet::RakString *data);
/// Returns false if the connection is not doing anything else
bool IsBusy(void) const;
/// \internal
int GetState(void) const;
struct OutgoingCommand
{
RakNet::RakString remotePath;
RakNet::RakString data;
RakNet::RakString contentType;
bool isPost;
};
DataStructures::Queue outgoingCommand;
OutgoingCommand currentProcessingCommand;
private:
SystemAddress server;
TCPInterface *tcp;
RakNet::RakString host;
unsigned short port;
DataStructures::Queue badResponses;
enum ConnectionState
{
CS_NONE,
CS_DISCONNECTING,
CS_CONNECTING,
CS_CONNECTED,
CS_PROCESSING,
} connectionState;
RakNet::RakString incomingData;
DataStructures::Queue results;
void CloseConnection();
/*
enum { RAK_HTTP_INITIAL,
RAK_HTTP_STARTING,
RAK_HTTP_CONNECTING,
RAK_HTTP_ESTABLISHED,
RAK_HTTP_REQUEST_SENT,
RAK_HTTP_IDLE } state;
RakNet::RakString outgoing, incoming, path, contentType;
void Process(Packet *packet); // the workhorse
// this helps check the various status lists in TCPInterface
typedef SystemAddress (TCPInterface::*StatusCheckFunction)(void);
bool InList(StatusCheckFunction func);
*/
}; class RAK_DLL_EXPORT HTTPConnection2 : public PluginInterface2 //使用插件實現
{
public:
// GetInstance() and DestroyInstance(instance*)
STATIC_FACTORY_DECLARATIONS(HTTPConnection2)
HTTPConnection2();
virtual ~HTTPConnection2();
/// \brief Connect to, then transmit a request to a TCP based server
/// \param[in] tcp An instance of TCPInterface that previously had TCPInterface::Start() called
/// \param[in] stringToTransmit What string to transmit. See RakString::FormatForPOST(), RakString::FormatForGET(), RakString::FormatForDELETE()
/// \param[in] host The IP address to connect to
/// \param[in] port The port to connect to
/// \param[in] useSSL If to use SSL to connect. OPEN_SSL_CLIENT_SUPPORT must be defined to 1 in RakNetDefines.h or RakNetDefinesOverrides.h
/// \param[in] ipVersion 4 for IPV4, 6 for IPV6
/// \param[in] useAddress Assume we are connected to this address and send to it, rather than do a lookup
/// \param[in] userData
/// \return false if host is not a valid IP address or domain name
bool TransmitRequest(const char* stringToTransmit, const char* host, unsigned short port=80, bool useSSL=false, int ipVersion=4, SystemAddress useAddress=UNASSIGNED_SYSTEM_ADDRESS, void *userData=0);
/// \brief Check for and return a response from a prior call to TransmitRequest()
/// As TCP is stream based, you may get a webserver reply over several calls to TCPInterface::Receive()
/// HTTPConnection2 will store Packet::data and return the response to you either when the connection to the webserver is lost, or enough data has been received()
/// This will only potentially return true after a call to ProcessTCPPacket() or OnLostConnection()
/// \param[out] stringTransmitted The original string transmitted
/// \param[out] hostTransmitted The parameter of the same name passed to TransmitRequest()
/// \param[out] responseReceived The response, if any
/// \param[out] hostReceived The SystemAddress from ProcessTCPPacket() or OnLostConnection()
/// \param[out] contentOffset The offset from the start of responseReceived to the data body. Equivalent to searching for \r\n\r\n in responseReceived.
/// \param[out] userData Whatever you passed to TransmitRequest
/// \return true if there was a response. false if not.
bool GetResponse( RakString &stringTransmitted, RakString &hostTransmitted, RakString &responseReceived, SystemAddress &hostReceived, int &contentOffset, void **userData );
bool GetResponse( RakString &stringTransmitted, RakString &hostTransmitted, RakString &responseReceived, SystemAddress &hostReceived, int &contentOffset );
/// \brief Return if any requests are pending
bool IsBusy(void) const;
/// \brief Return if any requests are waiting to be read by the user
bool HasResponse(void) const;
struct Request
{
RakString stringToTransmit;
RakString stringReceived;
RakString host;
SystemAddress hostEstimatedAddress;
SystemAddress hostCompletedAddress;
unsigned short port;
bool useSSL;
int contentOffset;
int contentLength;
int ipVersion;
void *userData;
bool chunked;
size_t thisChunkSize;
size_t bytesReadForThisChunk;
};
/// \internal
virtual PluginReceiveResult OnReceive(Packet *packet);
virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason);
protected:
bool IsConnected(SystemAddress sa);
void SendRequest(Request *request);
void RemovePendingRequest(SystemAddress sa);
void SendNextPendingRequest(void);
void SendPendingRequestToConnectedSystem(SystemAddress sa);
DataStructures::Queue pendingRequests;
DataStructures::List sentRequests;
DataStructures::List completedRequests;
SimpleMutex pendingRequestsMutex, sentRequestsMutex, completedRequestsMutex;
}; 29、團隊管理
內部結構:
struct TeamMember
{
RakNetGUID memberGuid;
NetworkID memberId;
TeamId currentTeam;
TeamId requestedTeam;
};
struct MyTeamMembers
{
NetworkID memberId;
TeamId currentTeam;
TeamId requestedTeam;
};實現類:class RAK_DLL_EXPORT TeamBalancer : public PluginInterface2 //設置網絡和團隊的選擇(支持對等或客戶機/服務器),信息自動處理團
管理部分
enum JoinTeamType
{
/// Attempt to join the first available team.
JOIN_ANY_AVAILABLE_TEAM,
/// Attempt to join a specific team, previously added with TM_World::ReferenceTeam()
JOIN_SPECIFIC_TEAM,
/// No team. Always succeeds.
JOIN_NO_TEAM
};
/// \ingroup TEAM_MANAGER_GROUP
enum TMTopology
{
// Each system will send all messages to all participants
TM_PEER_TO_PEER,
// The host will relay incoming messages to all participants
TM_CLIENT_SERVER,
};
/// \brief Parameter to TM_World::ReferenceTeamMember()
/// \details Use TeamSelection::AnyAvailable(), TeamSelection::SpecificTeam(), or TeamSelection::NoTeam()
/// \ingroup TEAM_MANAGER_GROUP
struct TeamSelection
{
TeamSelection();
TeamSelection(JoinTeamType itt);
TeamSelection(JoinTeamType itt, TM_Team *param);
TeamSelection(JoinTeamType itt, NoTeamId param);
JoinTeamType joinTeamType;
union
{
TM_Team *specificTeamToJoin;
NoTeamId noTeamSubcategory;
} teamParameter;
/// \brief Join any team that has available slots and is tagged with ALLOW_JOIN_ANY_AVAILABLE_TEAM
/// \details ID_TEAM_BALANCER_TEAM_ASSIGNED, ID_TEAM_BALANCER_REQUESTED_TEAM_FULL, or ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED will be returned to all systems.
static TeamSelection AnyAvailable(void);
/// \brief Join a specific team if it has available slots, and is tagged with JOIN_SPECIFIC_TEAMS
/// \details ID_TEAM_BALANCER_TEAM_ASSIGNED, ID_TEAM_BALANCER_REQUESTED_TEAM_FULL, or ID_TEAM_BALANCER_REQUESTED_TEAM_LOCKED will be returned to all systems.
/// \param[in] specificTeamToJoin Which team to attempt to join.
static TeamSelection SpecificTeam(TM_Team *specificTeamToJoin);
/// \brief Do not join a team, or leave all current teams.
/// \details This always succeeds. ID_TEAM_BALANCER_TEAM_ASSIGNED will be returned to all systems.
/// \param[in] noTeamSubcategory Even when not on a team, you can internally identify a subcategory of not being on a team, such as AI or spectator.
static TeamSelection NoTeam(NoTeamId noTeamSubcategory);
};class RAK_DLL_EXPORT TM_TeamMember //包含數據和操作(管理團隊、團隊成員、你的游戲)
class RAK_DLL_EXPORT TM_Team //一個團隊
class TM_World //存儲一個團隊列表,負責平衡。
class RAK_DLL_EXPORT TeamManager : public PluginInterface2 //自動化網絡與團隊列表管理
30、UDP代理
UDP代理有服務端和客戶端之分
enum UDPProxyMessages //消息類型
{
ID_UDP_PROXY_FORWARDING_SUCCEEDED,
ID_UDP_PROXY_FORWARDING_NOTIFICATION,
ID_UDP_PROXY_NO_SERVERS_ONLINE,
ID_UDP_PROXY_RECIPIENT_GUID_NOT_CONNECTED_TO_COORDINATOR,
ID_UDP_PROXY_ALL_SERVERS_BUSY,
ID_UDP_PROXY_IN_PROGRESS,
ID_UDP_PROXY_FORWARDING_REQUEST_FROM_CLIENT_TO_COORDINATOR,
ID_UDP_PROXY_PING_SERVERS_FROM_COORDINATOR_TO_CLIENT,
ID_UDP_PROXY_PING_SERVERS_REPLY_FROM_CLIENT_TO_COORDINATOR,
ID_UDP_PROXY_FORWARDING_REQUEST_FROM_COORDINATOR_TO_SERVER,
ID_UDP_PROXY_FORWARDING_REPLY_FROM_SERVER_TO_COORDINATOR,
ID_UDP_PROXY_LOGIN_REQUEST_FROM_SERVER_TO_COORDINATOR,
ID_UDP_PROXY_LOGIN_SUCCESS_FROM_COORDINATOR_TO_SERVER,
ID_UDP_PROXY_ALREADY_LOGGED_IN_FROM_COORDINATOR_TO_SERVER,
ID_UDP_PROXY_NO_PASSWORD_SET_FROM_COORDINATOR_TO_SERVER,
ID_UDP_PROXY_WRONG_PASSWORD_FROM_COORDINATOR_TO_SERVER
}; class RAK_DLL_EXPORT UDPProxyCoordinator : public PluginInterface2 //代理協調類
{
public:
// GetInstance() and DestroyInstance(instance*)
STATIC_FACTORY_DECLARATIONS(UDPProxyCoordinator)
UDPProxyCoordinator();
virtual ~UDPProxyCoordinator();
/// For UDPProxyServers logging in remotely, they must pass a password to UDPProxyServer::LoginToCoordinator(). It must match the password set here.
/// If no password is set, they cannot login remotely.
/// By default, no password is set
void SetRemoteLoginPassword(RakNet::RakString password);
/// \internal
virtual void Update(void);
virtual PluginReceiveResult OnReceive(Packet *packet);
virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
struct SenderAndTargetAddress
{
SystemAddress senderClientAddress;
RakNetGUID senderClientGuid;
SystemAddress targetClientAddress;
RakNetGUID targetClientGuid;
};
struct ServerWithPing
{
unsigned short ping;
SystemAddress serverAddress;
};
struct ForwardingRequest
{
RakNet::TimeMS timeoutOnNoDataMS;
RakNet::TimeMS timeoutAfterSuccess;
SenderAndTargetAddress sata;
SystemAddress requestingAddress; // Which system originally sent the network message to start forwarding
SystemAddress currentlyAttemptedServerAddress;
DataStructures::Queue remainingServersToTry;
RakNet::BitStream serverSelectionBitstream;
DataStructures::List sourceServerPings, targetServerPings;
RakNet::TimeMS timeRequestedPings;
// Order based on sourceServerPings and targetServerPings
void OrderRemainingServersToTry(void);
};
protected:
static int ServerWithPingComp( const unsigned short &key, const UDPProxyCoordinator::ServerWithPing &data );
static int ForwardingRequestComp( const SenderAndTargetAddress &key, ForwardingRequest* const &data);
void OnForwardingRequestFromClientToCoordinator(Packet *packet);
void OnLoginRequestFromServerToCoordinator(Packet *packet);
void OnForwardingReplyFromServerToCoordinator(Packet *packet);
void OnPingServersReplyFromClientToCoordinator(Packet *packet);
void TryNextServer(SenderAndTargetAddress sata, ForwardingRequest *fw);
void SendAllBusy(SystemAddress senderClientAddress, SystemAddress targetClientAddress, RakNetGUID targetClientGuid, SystemAddress requestingAddress);
void Clear(void);
void SendForwardingRequest(SystemAddress sourceAddress, SystemAddress targetAddress, SystemAddress serverAddress, RakNet::TimeMS timeoutOnNoDataMS);
// Logged in servers
//DataStructures::Multilist serverList;
DataStructures::List serverList;
// Forwarding requests in progress
//DataStructures::Multilist forwardingRequestList;
DataStructures::OrderedList forwardingRequestList;
RakNet::RakString remoteLoginPassword;
}; 客戶端:struct UDPProxyClientResultHandler //客戶端返回
{
UDPProxyClientResultHandler() {}
virtual ~UDPProxyClientResultHandler() {}
/// Called when our forwarding request was completed. We can now connect to \a targetAddress by using \a proxyAddress instead
/// \param[out] proxyIPAddress IP Address of the proxy server, which will forward messages to targetAddress
/// \param[out] proxyPort Remote port to use on the proxy server, which will forward messages to targetAddress
/// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address.
/// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] proxyClient The plugin that is calling this callback
virtual void OnForwardingSuccess(const char *proxyIPAddress, unsigned short proxyPort,
SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0;
/// Called when another system has setup forwarding, with our system as the target address.
/// Plugin automatically sends a datagram to proxyIPAddress before this callback, to open our router if necessary.
/// \param[out] proxyIPAddress IP Address of the proxy server, which will forward messages to targetAddress
/// \param[out] proxyPort Remote port to use on the proxy server, which will forward messages to targetAddress
/// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. This is originating source IP address of the remote system that will be sending to us.
/// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding. This is our external IP address.
/// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] proxyClient The plugin that is calling this callback
virtual void OnForwardingNotification(const char *proxyIPAddress, unsigned short proxyPort,
SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0;
/// Called when our forwarding request failed, because no UDPProxyServers are connected to UDPProxyCoordinator
/// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address.
/// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] proxyClient The plugin that is calling this callback
virtual void OnNoServersOnline(SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0;
/// Called when our forwarding request failed, because no UDPProxyServers are connected to UDPProxyCoordinator
/// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address.
/// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] proxyClient The plugin that is calling this callback
virtual void OnRecipientNotConnected(SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0;
/// Called when our forwarding request failed, because all UDPProxyServers that are connected to UDPProxyCoordinator are at their capacity
/// Either add more servers, or increase capacity via UDPForwarder::SetMaxForwardEntries()
/// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address.
/// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] proxyClient The plugin that is calling this callback
virtual void OnAllServersBusy(SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0;
/// Called when our forwarding request is already in progress on the \a proxyCoordinator.
/// This can be ignored, but indicates an unneeded second request
/// \param[out] proxyIPAddress IP Address of the proxy server, which is forwarding messages to targetAddress
/// \param[out] proxyPort Remote port to use on the proxy server, which is forwarding messages to targetAddress
/// \param[out] proxyCoordinator \a proxyCoordinator parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] sourceAddress \a sourceAddress parameter passed to UDPProxyClient::RequestForwarding. If it was UNASSIGNED_SYSTEM_ADDRESS, it is now our external IP address.
/// \param[out] targetAddress \a targetAddress parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] targetGuid \a targetGuid parameter originally passed to UDPProxyClient::RequestForwarding
/// \param[out] proxyClient The plugin that is calling this callback
virtual void OnForwardingInProgress(const char *proxyIPAddress, unsigned short proxyPort, SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddress, RakNetGUID targetGuid, RakNet::UDPProxyClient *proxyClientPlugin)=0;
};class RAK_DLL_EXPORT UDPProxyClient : public PluginInterface2
{
public:
// GetInstance() and DestroyInstance(instance*)
STATIC_FACTORY_DECLARATIONS(UDPProxyClient)
UDPProxyClient();
~UDPProxyClient();
/// Receives the results of calling RequestForwarding()
/// Set before calling RequestForwarding or you won't know what happened
/// \param[in] resultHandler
void SetResultHandler(UDPProxyClientResultHandler *rh);
/// Sends a request to proxyCoordinator to find a server and have that server setup UDPForwarder::StartForwarding() on our address to \a targetAddressAsSeenFromCoordinator
/// The forwarded datagrams can be from any UDP source, not just RakNet
/// \pre Must be connected to \a proxyCoordinator
/// \pre Systems running UDPProxyServer must be connected to \a proxyCoordinator and logged in via UDPProxyCoordinator::LoginServer() or UDPProxyServer::LoginToCoordinator()
/// \note May still fail, if all proxy servers have no open connections.
/// \note RakNet's protocol will ensure a message is sent at least every 5 seconds, so if routing RakNet messages, it is a reasonable value for timeoutOnNoDataMS, plus an extra few seconds for latency.
/// \param[in] proxyCoordinator System we are connected to that is running the UDPProxyCoordinator plugin
/// \param[in] sourceAddress External IP address of the system we want to forward messages from. This does not have to be our own system. To specify our own system, you can pass UNASSIGNED_SYSTEM_ADDRESS which the coordinator will treat as our external IP address.
/// \param[in] targetAddressAsSeenFromCoordinator External IP address of the system we want to forward messages to. If this system is connected to UDPProxyCoordinator at this address using RakNet, that system will ping the server and thus open the router for incoming communication. In any other case, you are responsible for doing your own network communication to have that system ping the server. See also targetGuid in the other version of RequestForwarding(), to avoid the need to know the IP address to the coordinator of the destination.
/// \param[in] timeoutOnNoData If no data is sent by the forwarded systems, how long before removing the forward entry from UDPForwarder? UDP_FORWARDER_MAXIMUM_TIMEOUT is the maximum value. Recommended 10 seconds.
/// \param[in] serverSelectionBitstream If you want to send data to UDPProxyCoordinator::GetBestServer(), write it here
/// \return true if the request was sent, false if we are not connected to proxyCoordinator
bool RequestForwarding(SystemAddress proxyCoordinator, SystemAddress sourceAddress, SystemAddress targetAddressAsSeenFromCoordinator, RakNet::TimeMS timeoutOnNoDataMS, RakNet::BitStream *serverSelectionBitstream=0);
/// Same as above, but specify the target with a GUID, in case you don't know what its address is to the coordinator
/// If requesting forwarding to a RakNet enabled system, then it is easier to use targetGuid instead of targetAddressAsSeenFromCoordinator
bool RequestForwarding(SystemAddress proxyCoordinator, SystemAddress sourceAddress, RakNetGUID targetGuid, RakNet::TimeMS timeoutOnNoDataMS, RakNet::BitStream *serverSelectionBitstream=0);
/// \internal
virtual void Update(void);
virtual PluginReceiveResult OnReceive(Packet *packet);
virtual void OnRakPeerShutdown(void);
struct ServerWithPing
{
unsigned short ping;
SystemAddress serverAddress;
};
struct SenderAndTargetAddress
{
SystemAddress senderClientAddress;
SystemAddress targetClientAddress;
};
struct PingServerGroup
{
SenderAndTargetAddress sata;
RakNet::TimeMS startPingTime;
SystemAddress coordinatorAddressForPings;
//DataStructures::Multilist serversToPing;
DataStructures::List serversToPing;
bool AreAllServersPinged(void) const;
void SendPingedServersToCoordinator(RakPeerInterface *rakPeerInterface);
};
//DataStructures::Multilist pingServerGroups;
DataStructures::List pingServerGroups;
protected:
void OnPingServers(Packet *packet);
void Clear(void);
UDPProxyClientResultHandler *resultHandler;
}; 服務端:struct UDPProxyServerResultHandler //服務端返回處理
{
UDPProxyServerResultHandler() {}
virtual ~UDPProxyServerResultHandler() {}
/// Called when our login succeeds
/// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator()
/// \param[out] proxyServer The plugin calling this callback
virtual void OnLoginSuccess(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0;
/// We are already logged in.
/// This login failed, but the system is operational as if it succeeded
/// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator()
/// \param[out] proxyServer The plugin calling this callback
virtual void OnAlreadyLoggedIn(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0;
/// The coordinator operator forgot to call UDPProxyCoordinator::SetRemoteLoginPassword()
/// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator()
/// \param[out] proxyServer The plugin calling this callback
virtual void OnNoPasswordSet(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0;
/// The coordinator operator set a different password in UDPProxyCoordinator::SetRemoteLoginPassword() than what we passed
/// \param[out] usedPassword The password we passed to UDPProxyServer::LoginToCoordinator()
/// \param[out] proxyServer The plugin calling this callback
virtual void OnWrongPassword(RakNet::RakString usedPassword, RakNet::UDPProxyServer *proxyServerPlugin)=0;
};class RAK_DLL_EXPORT UDPProxyServer : public PluginInterface2
{
public:
// GetInstance() and DestroyInstance(instance*)
STATIC_FACTORY_DECLARATIONS(UDPProxyServer)
UDPProxyServer();
~UDPProxyServer();
/// Sets the socket family to use, either IPV4 or IPV6
/// \param[in] socketFamily For IPV4, use AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC.
void SetSocketFamily(unsigned short _socketFamily);
/// Receives the results of calling LoginToCoordinator()
/// Set before calling LoginToCoordinator or you won't know what happened
/// \param[in] resultHandler
void SetResultHandler(UDPProxyServerResultHandler *rh);
/// Before the coordinator will register the UDPProxyServer, you must login
/// \pre Must be connected to the coordinator
/// \pre Coordinator must have set a password with UDPProxyCoordinator::SetRemoteLoginPassword()
/// \returns false if already logged in, or logging in. Returns true otherwise
bool LoginToCoordinator(RakNet::RakString password, SystemAddress coordinatorAddress);
/// \brief The server IP reported to the client is the IP address from the server to the coordinator.
/// If the server and coordinator are on the same LAN, you need to call SetServerPublicIP() to tell the client what address to connect to
/// \param[in] ip IP address to report in UDPProxyClientResultHandler::OnForwardingSuccess() and UDPProxyClientResultHandler::OnForwardingNotification() as proxyIPAddress
void SetServerPublicIP(RakString ip);
/// Operative class that performs the forwarding
/// Exposed so you can call UDPForwarder::SetMaxForwardEntries() if you want to change away from the default
/// UDPForwarder::Startup(), UDPForwarder::Shutdown(), and UDPForwarder::Update() are called automatically by the plugin
UDPForwarder udpForwarder;
virtual void OnAttach(void);
virtual void OnDetach(void);
/// \internal
virtual void Update(void);
virtual PluginReceiveResult OnReceive(Packet *packet);
virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
virtual void OnRakPeerStartup(void);
virtual void OnRakPeerShutdown(void);
protected:
void OnForwardingRequestFromCoordinatorToServer(Packet *packet);
DataStructures::OrderedList loggingInCoordinators;
DataStructures::OrderedList loggedInCoordinators;
UDPProxyServerResultHandler *resultHandler;
unsigned short socketFamily;
RakString serverPublicIp;
}; 31、穿透
有服務端和客戶端之分
客戶端定義結構如下:
struct RAK_DLL_EXPORT PunchthroughConfiguration
{
/// internal: (15 ms * 2 tries + 30 wait) * 5 ports * 8 players = 2.4 seconds
/// external: (50 ms * 8 sends + 200 wait) * 2 port * 8 players = 9.6 seconds
/// Total: 8 seconds
PunchthroughConfiguration() {
TIME_BETWEEN_PUNCH_ATTEMPTS_INTERNAL=15;
TIME_BETWEEN_PUNCH_ATTEMPTS_EXTERNAL=50;
UDP_SENDS_PER_PORT_INTERNAL=2;
UDP_SENDS_PER_PORT_EXTERNAL=8;
INTERNAL_IP_WAIT_AFTER_ATTEMPTS=30;
MAXIMUM_NUMBER_OF_INTERNAL_IDS_TO_CHECK=5; /// set to 0 to not do lan connects
MAX_PREDICTIVE_PORT_RANGE=2;
EXTERNAL_IP_WAIT_BETWEEN_PORTS=200;
EXTERNAL_IP_WAIT_AFTER_FIRST_TTL=100;
EXTERNAL_IP_WAIT_AFTER_ALL_ATTEMPTS=EXTERNAL_IP_WAIT_BETWEEN_PORTS;
retryOnFailure=false;
}
/// How much time between each UDP send
RakNet::Time TIME_BETWEEN_PUNCH_ATTEMPTS_INTERNAL;
RakNet::Time TIME_BETWEEN_PUNCH_ATTEMPTS_EXTERNAL;
/// How many tries for one port before giving up and going to the next port
int UDP_SENDS_PER_PORT_INTERNAL;
int UDP_SENDS_PER_PORT_EXTERNAL;
/// After giving up on one internal port, how long to wait before trying the next port
int INTERNAL_IP_WAIT_AFTER_ATTEMPTS;
/// How many external ports to try past the last known starting port
int MAX_PREDICTIVE_PORT_RANGE;
/// After sending TTL, how long to wait until first punch attempt
int EXTERNAL_IP_WAIT_AFTER_FIRST_TTL;
/// After giving up on one external port, how long to wait before trying the next port
int EXTERNAL_IP_WAIT_BETWEEN_PORTS;
/// After trying all external ports, how long to wait before returning ID_NAT_PUNCHTHROUGH_FAILED
int EXTERNAL_IP_WAIT_AFTER_ALL_ATTEMPTS;
/// Maximum number of internal IP address to try to connect to.
/// Cannot be greater than MAXIMUM_NUMBER_OF_INTERNAL_IDS
/// Should be high enough to try all internal IP addresses on the majority of computers
int MAXIMUM_NUMBER_OF_INTERNAL_IDS_TO_CHECK;
/// If the first punchthrough attempt fails, try again
/// This sometimes works because the remote router was looking for an incoming message on a higher numbered port before responding to a lower numbered port from the other system
bool retryOnFailure;
};
/// \ingroup NAT_PUNCHTHROUGH_GROUP
struct RAK_DLL_EXPORT NatPunchthroughDebugInterface
{
NatPunchthroughDebugInterface() {}
virtual ~NatPunchthroughDebugInterface() {}
virtual void OnClientMessage(const char *msg)=0;
};
/// \ingroup NAT_PUNCHTHROUGH_GROUP
struct RAK_DLL_EXPORT NatPunchthroughDebugInterface_Printf : public NatPunchthroughDebugInterface
{
virtual void OnClientMessage(const char *msg);
};
#if _RAKNET_SUPPORT_PacketLogger==1
/// \ingroup NAT_PUNCHTHROUGH_GROUP
struct RAK_DLL_EXPORT NatPunchthroughDebugInterface_PacketLogger : public NatPunchthroughDebugInterface
{
// Set to non-zero to write to the packetlogger!
PacketLogger *pl;
NatPunchthroughDebugInterface_PacketLogger() {pl=0;}
~NatPunchthroughDebugInterface_PacketLogger() {}
virtual void OnClientMessage(const char *msg);
};
#endif客戶端實現類:class RAK_DLL_EXPORT NatPunchthroughClient : public PluginInterface2
服務器定義結構如下:struct RAK_DLL_EXPORT NatPunchthroughServerDebugInterface
{
NatPunchthroughServerDebugInterface() {}
virtual ~NatPunchthroughServerDebugInterface() {}
virtual void OnServerMessage(const char *msg)=0;
};
/// \ingroup NAT_PUNCHTHROUGH_GROUP
struct RAK_DLL_EXPORT NatPunchthroughServerDebugInterface_Printf : public NatPunchthroughServerDebugInterface
{
virtual void OnServerMessage(const char *msg);
};
#if _RAKNET_SUPPORT_PacketLogger==1
/// \ingroup NAT_PUNCHTHROUGH_GROUP
struct RAK_DLL_EXPORT NatPunchthroughServerDebugInterface_PacketLogger : public NatPunchthroughServerDebugInterface
{
// Set to non-zero to write to the packetlogger!
PacketLogger *pl;
NatPunchthroughServerDebugInterface_PacketLogger() {pl=0;}
~NatPunchthroughServerDebugInterface_PacketLogger() {}
virtual void OnServerMessage(const char *msg);
};
#endif服務器實現類:class RAK_DLL_EXPORT NatPunchthroughServer : public PluginInterface2
{
public:
STATIC_FACTORY_DECLARATIONS(NatPunchthroughServer)
// Constructor
NatPunchthroughServer();
// Destructor
virtual ~NatPunchthroughServer();
/// Sets a callback to be called with debug messages
/// \param[in] i Pointer to an interface. The pointer is stored, so don't delete it while in progress. Pass 0 to clear.
void SetDebugInterface(NatPunchthroughServerDebugInterface *i);
/// \internal For plugin handling
virtual void Update(void);
/// \internal For plugin handling
virtual PluginReceiveResult OnReceive(Packet *packet);
/// \internal For plugin handling
virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
// Each connected user has a ready state. Ready means ready for nat punchthrough.
struct User;
struct ConnectionAttempt
{
ConnectionAttempt() {sender=0; recipient=0; startTime=0; attemptPhase=NAT_ATTEMPT_PHASE_NOT_STARTED;}
User *sender, *recipient;
uint16_t sessionId;
RakNet::Time startTime;
enum
{
NAT_ATTEMPT_PHASE_NOT_STARTED,
NAT_ATTEMPT_PHASE_GETTING_RECENT_PORTS,
} attemptPhase;
};
struct User
{
RakNetGUID guid;
SystemAddress systemAddress;
unsigned short mostRecentPort;
bool isReady;
DataStructures::OrderedList groupPunchthroughRequests;
DataStructures::List connectionAttempts;
bool HasConnectionAttemptToUser(User *user);
void DerefConnectionAttempt(ConnectionAttempt *ca);
void DeleteConnectionAttempt(ConnectionAttempt *ca);
void LogConnectionAttempts(RakNet::RakString &rs);
};
RakNet::Time lastUpdate;
static int NatPunchthroughUserComp( const RakNetGUID &key, User * const &data );
protected:
void OnNATPunchthroughRequest(Packet *packet);
DataStructures::OrderedList users;
void OnGetMostRecentPort(Packet *packet);
void OnClientReady(Packet *packet);
void SendTimestamps(void);
void StartPendingPunchthrough(void);
void StartPunchthroughForUser(User*user);
uint16_t sessionId;
NatPunchthroughServerDebugInterface *natPunchthroughServerDebugInterface;
SystemAddress boundAddresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS];
unsigned char boundAddressCount;
};
32、動態域名
enum DynDnsResultCode //返回結果
{
// ----- Success -----
RC_SUCCESS,
RC_DNS_ALREADY_SET, // RakNet detects no action is needed
// ----- Ignorable failure (treat same as success) -----
RC_NO_CHANGE, // DynDNS detects no action is needed (treated as abuse though)
// ----- User error -----
RC_NOT_DONATOR, // You have to pay to do this
RC_NO_HOST, // This host does not exist at all
RC_BAD_AUTH, // You set the wrong password
RC_NOT_YOURS, // This is not your host
// ----- Permanent failure -----
RC_ABUSE, // Your host has been blocked, too many failures disable your account
RC_TCP_FAILED_TO_START, // TCP port already in use
RC_TCP_DID_NOT_CONNECT, // DynDNS down?
RC_UNKNOWN_RESULT, // DynDNS returned a result code that was not documented as of 12/4/2010 on http://www.dyndns.com/developers/specs/flow.pdf
RC_PARSING_FAILURE, // Can't read the result returned, format change?
RC_CONNECTION_LOST_WITHOUT_RESPONSE, // Lost the connection to DynDNS while communicating
RC_BAD_AGENT, // ???
RC_BAD_SYS, // ???
RC_DNS_ERROR, // ???
RC_NOT_FQDN, // ???
RC_NUM_HOST, // ???
RC_911, // ???
RC_DYNDNS_TIMEOUT // DynDNS did not respond
};實現類:class RAK_DLL_EXPORT DynDNS
{
public:
DynDNS();
~DynDNS();
// Pass 0 for newIPAddress to autodetect whatever you are uploading from
// usernameAndPassword should be in the format username:password
void UpdateHostIPAsynch(const char *dnsHost, const char *newIPAddress, const char *usernameAndPassword );
void Update(void);
// Output
bool IsRunning(void) const {return connectPhase!=CP_IDLE;}
bool IsCompleted(void) const {return connectPhase==CP_IDLE;}
RakNet::DynDnsResultCode GetCompletedResultCode(void) {return result;}
const char *GetCompletedDescription(void) const {return resultDescription;}
bool WasResultSuccessful(void) const {return result==RC_SUCCESS || result==RC_DNS_ALREADY_SET || result==RC_NO_CHANGE;}
char *GetMyPublicIP(void) const {return (char*) myIPStr;} // We get our public IP as part of the process. This is valid once completed
protected:
void Stop(void);
void SetCompleted(RakNet::DynDnsResultCode _result, const char *_resultDescription) {Stop(); result=_result; resultDescription=_resultDescription;}
enum ConnectPhase
{
CP_CONNECTING_TO_CHECKIP,
CP_WAITING_FOR_CHECKIP_RESPONSE,
CP_CONNECTING_TO_DYNDNS,
CP_WAITING_FOR_DYNDNS_RESPONSE,
CP_IDLE
};
TCPInterface *tcp;
RakNet::RakString getString;
SystemAddress serverAddress;
ConnectPhase connectPhase;
RakNet::RakString host;
RakNet::Time phaseTimeout;
SystemAddress checkIpAddress;
const char *resultDescription;
RakNet::DynDnsResultCode result;
char myIPStr[32];
};
33、權重圖
權重圖類是一個模版類,如下:
template
class RAK_DLL_EXPORT WeightedGraph //權重圖
{
public:
static void IMPLEMENT_DEFAULT_COMPARISON(void) {DataStructures::defaultMapKeyComparison(node_type(),node_type());}
WeightedGraph();
~WeightedGraph();
WeightedGraph( const WeightedGraph& original_copy );
WeightedGraph& operator= ( const WeightedGraph& original_copy );
void AddNode(const node_type &node);
void RemoveNode(const node_type &node);
void AddConnection(const node_type &node1, const node_type &node2, weight_type weight);
void RemoveConnection(const node_type &node1, const node_type &node2);
bool HasConnection(const node_type &node1, const node_type &node2);
void Print(void);
void Clear(void);
bool GetShortestPath(DataStructures::List &path, node_type startNode, node_type endNode, weight_type INFINITE_WEIGHT);
bool GetSpanningTree(DataStructures::Tree &outTree, DataStructures::List *inputNodes, node_type startNode, weight_type INFINITE_WEIGHT );
unsigned GetNodeCount(void) const;
unsigned GetConnectionCount(unsigned nodeIndex) const;
void GetConnectionAtIndex(unsigned nodeIndex, unsigned connectionIndex, node_type &outNode, weight_type &outWeight) const;
node_type GetNodeAtIndex(unsigned nodeIndex) const;
protected:
void ClearDijkstra(void);
void GenerateDisjktraMatrix(node_type startNode, weight_type INFINITE_WEIGHT);
DataStructures::Map *> adjacencyLists;
// All these variables are for path finding with Dijkstra
// 08/23/06 Won't compile as a DLL inside this struct
// struct
// {
bool isValidPath;
node_type rootNode;
DataStructures::OrderedList costMatrixIndices;
weight_type *costMatrix;
node_type *leastNodeArray;
// } dijkstra;
struct NodeAndParent
{
DataStructures::Tree*node;
DataStructures::Tree*parent;
};
};
34、實例類
class RAK_DLL_EXPORT RakPeer : public RakPeerInterface, public RNS2EventHandler
{
public:
///Constructor
RakPeer();
///Destructor
virtual ~RakPeer();
//啟動網絡
StartupResult Startup( unsigned int maxConnections, SocketDescriptor *socketDescriptors, unsigned socketDescriptorCount, int threadPriority=-99999 );
/// 加密
bool InitializeSecurity( const char *publicKey, const char *privateKey, bool bRequireClientKey = false );
/// 關閉加密
void DisableSecurity( void );
/// 安全連接
void AddToSecurityExceptionList(const char *ip);
void RemoveFromSecurityExceptionList(const char *ip);
bool IsInSecurityExceptionList(const char *ip);
void SetMaximumIncomingConnections( unsigned short numberAllowed );
unsigned int GetMaximumIncomingConnections( void ) const;
unsigned short NumberOfConnections(void) const;
void SetIncomingPassword( const char* passwordData, int passwordDataLength );
void GetIncomingPassword( char* passwordData, int *passwordDataLength );
ConnectionAttemptResult Connect( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey=0, unsigned connectionSocketIndex=0, unsigned sendConnectionAttemptCount=6, unsigned timeBetweenSendConnectionAttemptsMS=1000, RakNet::TimeMS timeoutTime=0 );
virtual ConnectionAttemptResult ConnectWithSocket(const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, RakNetSocket2* socket, PublicKey *publicKey=0, unsigned sendConnectionAttemptCount=6, unsigned timeBetweenSendConnectionAttemptsMS=1000, RakNet::TimeMS timeoutTime=0);
//bool Console2LobbyConnect( void *networkServiceId, const char *passwordData, int passwordDataLength );*/
void Shutdown( unsigned int blockDuration, unsigned char orderingChannel=0, PacketPriority disconnectionNotificationPriority=LOW_PRIORITY );
bool IsActive( void ) const;
bool GetConnectionList( SystemAddress *remoteSystems, unsigned short *numberOfSystems ) const;
virtual uint32_t GetNextSendReceipt(void);
virtual uint32_t IncrementNextSendReceipt(void);
uint32_t Send( const char *data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 );
void SendLoopback( const char *data, const int length );
uint32_t Send( const RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 );
uint32_t SendList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 );
Packet* Receive( void );
void DeallocatePacket( Packet *packet );
unsigned int GetMaximumNumberOfPeers( void ) const;
void CloseConnection( const AddressOrGUID target, bool sendDisconnectionNotification, unsigned char orderingChannel=0, PacketPriority disconnectionNotificationPriority=LOW_PRIORITY );
void CancelConnectionAttempt( const SystemAddress target );
ConnectionState GetConnectionState(const AddressOrGUID systemIdentifier);
int GetIndexFromSystemAddress( const SystemAddress systemAddress ) const;
SystemAddress GetSystemAddressFromIndex( unsigned int index );
RakNetGUID GetGUIDFromIndex( unsigned int index );
void GetSystemList(DataStructures::List &addresses, DataStructures::List &guids) const;
void AddToBanList( const char *IP, RakNet::TimeMS milliseconds=0 );
void RemoveFromBanList( const char *IP );
void ClearBanList( void );
bool IsBanned( const char *IP );
void SetLimitIPConnectionFrequency(bool b);
void Ping( const SystemAddress target );
bool Ping( const char* host, unsigned short remotePort, bool onlyReplyOnAcceptingConnections, unsigned connectionSocketIndex=0 );
int GetAveragePing( const AddressOrGUID systemIdentifier );
int GetLastPing( const AddressOrGUID systemIdentifier ) const;
int GetLowestPing( const AddressOrGUID systemIdentifier ) const;
void SetOccasionalPing( bool doPing );
RakNet::Time GetClockDifferential( const AddressOrGUID systemIdentifier );
void SetOfflinePingResponse( const char *data, const unsigned int length );
void GetOfflinePingResponse( char **data, unsigned int *length );
SystemAddress GetInternalID( const SystemAddress systemAddress=UNASSIGNED_SYSTEM_ADDRESS, const int index=0 ) const;
void SetInternalID(SystemAddress systemAddress, int index=0);
SystemAddress GetExternalID( const SystemAddress target ) const;
const RakNetGUID GetMyGUID(void) const;
SystemAddress GetMyBoundAddress(const int socketIndex=0);
const RakNetGUID& GetGuidFromSystemAddress( const SystemAddress input ) const;
SystemAddress GetSystemAddressFromGuid( const RakNetGUID input ) const;
bool GetClientPublicKeyFromSystemAddress( const SystemAddress input, char *client_public_key ) const;
void SetTimeoutTime( RakNet::TimeMS timeMS, const SystemAddress target );
RakNet::TimeMS GetTimeoutTime( const SystemAddress target );
int GetMTUSize( const SystemAddress target ) const;
unsigned GetNumberOfAddresses( void );
const char* GetLocalIP( unsigned int index );
bool IsLocalIP( const char *ip );
void AllowConnectionResponseIPMigration( bool allow );
bool AdvertiseSystem( const char *host, unsigned short remotePort, const char *data, int dataLength, unsigned connectionSocketIndex=0 );
void SetSplitMessageProgressInterval(int interval);
int GetSplitMessageProgressInterval(void) const;
void SetUnreliableTimeout(RakNet::TimeMS timeoutMS);
void SendTTL( const char* host, unsigned short remotePort, int ttl, unsigned connectionSocketIndex=0 );
void AttachPlugin( PluginInterface2 *plugin );
void DetachPlugin( PluginInterface2 *messageHandler );
void PushBackPacket( Packet *packet, bool pushAtHead );
void ChangeSystemAddress(RakNetGUID guid, const SystemAddress &systemAddress);
Packet* AllocatePacket(unsigned dataSize);
virtual RakNetSocket2* GetSocket( const SystemAddress target );
virtual void GetSockets( DataStructures::List &sockets );
virtual void ReleaseSockets( DataStructures::List &sockets );
virtual void WriteOutOfBandHeader(RakNet::BitStream *bitStream);
virtual void SetUserUpdateThread(void (*_userUpdateThreadPtr)(RakPeerInterface *, void *), void *_userUpdateThreadData);
virtual void SetIncomingDatagramEventHandler( bool (*_incomingDatagramEventHandler)(RNS2RecvStruct *) );
virtual void ApplyNetworkSimulator( float packetloss, unsigned short minExtraPing, unsigned short extraPingVariance);
virtual void SetPerConnectionOutgoingBandwidthLimit( unsigned maxBitsPerSecond );
virtual bool IsNetworkSimulatorActive( void );
RakNetStatistics * GetStatistics( const SystemAddress systemAddress, RakNetStatistics *rns=0 );
bool GetStatistics( const unsigned int index, RakNetStatistics *rns );
virtual void GetStatisticsList(DataStructures::List &addresses, DataStructures::List &guids, DataStructures::List &statistics);
virtual unsigned int GetReceiveBufferSize(void);
bool RunUpdateCycle( BitStream &updateBitStream );
bool SendOutOfBand(const char *host, unsigned short remotePort, const char *data, BitSize_t dataLength, unsigned connectionSocketIndex=0 );
// static Packet *AllocPacket(unsigned dataSize, const char *file, unsigned int line);
/// \internal
/// \brief Holds the clock differences between systems, along with the ping
struct PingAndClockDifferential
{
unsigned short pingTime;
RakNet::Time clockDifferential;
};
/// \internal
/// \brief All the information representing a connected system
struct RemoteSystemStruct
{
bool isActive; // Is this structure in use?
SystemAddress systemAddress; /// Their external IP on the internet
SystemAddress myExternalSystemAddress; /// Your external IP on the internet, from their perspective
SystemAddress theirInternalSystemAddress[MAXIMUM_NUMBER_OF_INTERNAL_IDS]; /// Their internal IP, behind the LAN
ReliabilityLayer reliabilityLayer; /// The reliability layer associated with this player
bool weInitiatedTheConnection; /// True if we started this connection via Connect. False if someone else connected to us.
PingAndClockDifferential pingAndClockDifferential[ PING_TIMES_ARRAY_SIZE ]; /// last x ping times and calculated clock differentials with it
RakNet::Time pingAndClockDifferentialWriteIndex; /// The index we are writing into the pingAndClockDifferential circular buffer
unsigned short lowestPing; ///The lowest ping value encountered
RakNet::Time nextPingTime; /// When to next ping this player
RakNet::Time lastReliableSend; /// When did the last reliable send occur. Reliable sends must occur at least once every timeoutTime/2 units to notice disconnects
RakNet::Time connectionTime; /// connection time, if active.
// int connectionSocketIndex; // index into connectionSockets to send back on.
RakNetGUID guid;
int MTUSize;
// Reference counted socket to send back on
RakNetSocket2* rakNetSocket;
SystemIndex remoteSystemIndex;
#if LIBCAT_SECURITY==1
// Cached answer used internally by RakPeer to prevent DoS attacks based on the connexion handshake
char answer[cat::EasyHandshake::ANSWER_BYTES];
// If the server has bRequireClientKey = true, then this is set to the validated public key of the connected client
// Valid after connectMode reaches HANDLING_CONNECTION_REQUEST
char client_public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES];
#endif
enum ConnectMode {NO_ACTION, DISCONNECT_ASAP, DISCONNECT_ASAP_SILENTLY, DISCONNECT_ON_NO_ACK, REQUESTED_CONNECTION, HANDLING_CONNECTION_REQUEST, UNVERIFIED_SENDER, CONNECTED} connectMode;
};
// DS_APR
//void ProcessChromePacket(RakNetSocket2 *s, const char *buffer, int dataSize, const SystemAddress& recvFromAddress, RakNet::TimeUS timeRead);
// /DS_APR
protected:
friend RAK_THREAD_DECLARATION(UpdateNetworkLoop);
//friend RAK_THREAD_DECLARATION(RecvFromLoop);
friend RAK_THREAD_DECLARATION(UDTConnect);
friend bool ProcessOfflineNetworkPacket( SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSocket2* rakNetSocket, bool *isOfflineMessage, RakNet::TimeUS timeRead );
friend void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNet::TimeUS timeRead, BitStream &updateBitStream );
friend void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSocket2* rakNetSocket, RakNet::TimeUS timeRead, BitStream &updateBitStream );
int GetIndexFromSystemAddress( const SystemAddress systemAddress, bool calledFromNetworkThread ) const;
int GetIndexFromGuid( const RakNetGUID guid );
//void RemoveFromRequestedConnectionsList( const SystemAddress systemAddress );
// Two versions needed because some buggy compilers strip the last parameter if unused, and crashes
ConnectionAttemptResult SendConnectionRequest( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned int extraData, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNet::TimeMS timeoutTime, RakNetSocket2* socket );
ConnectionAttemptResult SendConnectionRequest( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey, unsigned connectionSocketIndex, unsigned int extraData, unsigned sendConnectionAttemptCount, unsigned timeBetweenSendConnectionAttemptsMS, RakNet::TimeMS timeoutTime );
///Get the reliability layer associated with a systemAddress.
/// \param[in] systemAddress The player identifier
/// \return 0 if none
RemoteSystemStruct *GetRemoteSystemFromSystemAddress( const SystemAddress systemAddress, bool calledFromNetworkThread, bool onlyActive ) const;
RakPeer::RemoteSystemStruct *GetRemoteSystem( const AddressOrGUID systemIdentifier, bool calledFromNetworkThread, bool onlyActive ) const;
void ValidateRemoteSystemLookup(void) const;
RemoteSystemStruct *GetRemoteSystemFromGUID( const RakNetGUID guid, bool onlyActive ) const;
///Parse out a connection request packet
void ParseConnectionRequestPacket( RakPeer::RemoteSystemStruct *remoteSystem, const SystemAddress &systemAddress, const char *data, int byteSize);
void OnConnectionRequest( RakPeer::RemoteSystemStruct *remoteSystem, RakNet::Time incomingTimestamp );
///Send a reliable disconnect packet to this player and disconnect them when it is delivered
void NotifyAndFlagForShutdown( const SystemAddress systemAddress, bool performImmediate, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority );
///Returns how many remote systems initiated a connection to us
unsigned int GetNumberOfRemoteInitiatedConnections( void ) const;
/// \brief Get a free remote system from the list and assign our systemAddress to it.
/// \note Should only be called from the update thread - not the user thread.
/// \param[in] systemAddress systemAddress to be assigned
/// \param[in] connectionMode connection mode of the RemoteSystem.
/// \param[in] rakNetSocket
/// \param[in] thisIPConnectedRecently Is this IP connected recently? set to False;
/// \param[in] bindingAddress Address to be binded with the remote system
/// \param[in] incomingMTU MTU for the remote system
RemoteSystemStruct * AssignSystemAddressToRemoteSystemList( const SystemAddress systemAddress, RemoteSystemStruct::ConnectMode connectionMode, RakNetSocket2* incomingRakNetSocket, bool *thisIPConnectedRecently, SystemAddress bindingAddress, int incomingMTU, RakNetGUID guid, bool useSecurity );
/// \brief Adjust the timestamp of the incoming packet to be relative to this system.
/// \param[in] data Data in the incoming packet.
/// \param[in] systemAddress Sender of the incoming packet.
void ShiftIncomingTimestamp( unsigned char *data, const SystemAddress &systemAddress ) const;
/// Get the most accurate clock differential for a certain player.
/// \param[in] systemAddress The player with whose clock the time difference is calculated.
/// \returns The clock differential for a certain player.
RakNet::Time GetBestClockDifferential( const SystemAddress systemAddress ) const;
bool IsLoopbackAddress(const AddressOrGUID &systemIdentifier, bool matchPort) const;
SystemAddress GetLoopbackAddress(void) const;
///Set this to true to terminate the Peer thread execution
volatile bool endThreads;
///true if the peer thread is active.
volatile bool isMainLoopThreadActive;
// RakNet::LocklessUint32_t isRecvFromLoopThreadActive;
bool occasionalPing; /// Do we occasionally ping the other systems?*/
///Store the maximum number of peers allowed to connect
unsigned int maximumNumberOfPeers;
//05/02/06 Just using maximumNumberOfPeers instead
///Store the maximum number of peers able to connect, including reserved connection slots for pings, etc.
//unsigned short remoteSystemListSize;
///Store the maximum incoming connection allowed
unsigned int maximumIncomingConnections;
RakNet::BitStream offlinePingResponse;
///Local Player ID
// SystemAddress mySystemAddress[MAXIMUM_NUMBER_OF_INTERNAL_IDS];
char incomingPassword[256];
unsigned char incomingPasswordLength;
/// This is an array of pointers to RemoteSystemStruct
/// This allows us to preallocate the list when starting, so we don't have to allocate or delete at runtime.
/// Another benefit is that is lets us add and remove active players simply by setting systemAddress
/// and moving elements in the list by copying pointers variables without affecting running threads, even if they are in the reliability layer
RemoteSystemStruct* remoteSystemList;
/// activeSystemList holds a list of pointers and is preallocated to be the same size as remoteSystemList. It is updated only by the network thread, but read by both threads
/// When the isActive member of RemoteSystemStruct is set to true or false, that system is added to this list of pointers
/// Threadsafe because RemoteSystemStruct is preallocated, and the list is only added to, not removed from
RemoteSystemStruct** activeSystemList;
unsigned int activeSystemListSize;
// Use a hash, with binaryAddress plus port mod length as the index
RemoteSystemIndex **remoteSystemLookup;
unsigned int RemoteSystemLookupHashIndex(const SystemAddress &sa) const;
void ReferenceRemoteSystem(const SystemAddress &sa, unsigned int remoteSystemListIndex);
void DereferenceRemoteSystem(const SystemAddress &sa);
RemoteSystemStruct* GetRemoteSystem(const SystemAddress &sa) const;
unsigned int GetRemoteSystemIndex(const SystemAddress &sa) const;
void ClearRemoteSystemLookup(void);
DataStructures::MemoryPool remoteSystemIndexPool;
void AddToActiveSystemList(unsigned int remoteSystemListIndex);
void RemoveFromActiveSystemList(const SystemAddress &sa);
// unsigned int LookupIndexUsingHashIndex(const SystemAddress &sa) const;
// unsigned int RemoteSystemListIndexUsingHashIndex(const SystemAddress &sa) const;
// unsigned int FirstFreeRemoteSystemLookupIndex(const SystemAddress &sa) const;
enum
{
// Only put these mutexes in user thread functions!
requestedConnectionList_Mutex,
offlinePingResponse_Mutex,
NUMBER_OF_RAKPEER_MUTEXES
};
SimpleMutex rakPeerMutexes[ NUMBER_OF_RAKPEER_MUTEXES ];
///RunUpdateCycle is not thread safe but we don't need to mutex calls. Just skip calls if it is running already
bool updateCycleIsRunning;
///The list of people we have tried to connect to recently
//DataStructures::Queue requestedConnectionsList;
///Data that both the client and the server needs
unsigned int bytesSentPerSecond, bytesReceivedPerSecond;
// bool isSocketLayerBlocking;
// bool continualPing,isRecvfromThreadActive,isMainLoopThreadActive, endThreads, isSocketLayerBlocking;
unsigned int validationInteger;
SimpleMutex incomingQueueMutex, banListMutex; //,synchronizedMemoryQueueMutex, automaticVariableSynchronizationMutex;
//DataStructures::Queue incomingpacketSingleProducerConsumer; //, synchronizedMemorypacketSingleProducerConsumer;
// BitStream enumerationData;
struct BanStruct
{
char *IP;
RakNet::TimeMS timeout; // 0 for none
};
struct RequestedConnectionStruct
{
SystemAddress systemAddress;
RakNet::Time nextRequestTime;
unsigned char requestsMade;
char *data;
unsigned short dataLength;
char outgoingPassword[256];
unsigned char outgoingPasswordLength;
unsigned socketIndex;
unsigned int extraData;
unsigned sendConnectionAttemptCount;
unsigned timeBetweenSendConnectionAttemptsMS;
RakNet::TimeMS timeoutTime;
PublicKeyMode publicKeyMode;
RakNetSocket2* socket;
enum {CONNECT=1, /*PING=2, PING_OPEN_CONNECTIONS=4,*/ /*ADVERTISE_SYSTEM=2*/} actionToTake;
#if LIBCAT_SECURITY==1
char handshakeChallenge[cat::EasyHandshake::CHALLENGE_BYTES];
cat::ClientEasyHandshake *client_handshake;
char remote_public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES];
// char remote_challenge[cat::EasyHandshake::CHALLENGE_BYTES];
// char random[16];
#endif
};
#if LIBCAT_SECURITY==1
bool GenerateConnectionRequestChallenge(RequestedConnectionStruct *rcs,PublicKey *publicKey);
#endif
//DataStructures::List* > automaticVariableSynchronizationList;
DataStructures::List banList;
// Threadsafe, and not thread safe
DataStructures::List pluginListTS, pluginListNTS;
DataStructures::Queue requestedConnectionQueue;
SimpleMutex requestedConnectionQueueMutex;
// void RunMutexedUpdateCycle(void);
struct BufferedCommandStruct
{
BitSize_t numberOfBitsToSend;
PacketPriority priority;
PacketReliability reliability;
char orderingChannel;
AddressOrGUID systemIdentifier;
bool broadcast;
RemoteSystemStruct::ConnectMode connectionMode;
NetworkID networkID;
bool blockingCommand; // Only used for RPC
char *data;
bool haveRakNetCloseSocket;
unsigned connectionSocketIndex;
unsigned short remotePortRakNetWasStartedOn_PS3;
unsigned int extraSocketOptions;
RakNetSocket2* socket;
unsigned short port;
uint32_t receipt;
enum {BCS_SEND, BCS_CLOSE_CONNECTION, BCS_GET_SOCKET, BCS_CHANGE_SYSTEM_ADDRESS,/* BCS_USE_USER_SOCKET, BCS_REBIND_SOCKET_ADDRESS, BCS_RPC, BCS_RPC_SHIFT,*/ BCS_DO_NOTHING} command;
};
// Single producer single consumer queue using a linked list
//BufferedCommandStruct* bufferedCommandReadIndex, bufferedCommandWriteIndex;
DataStructures::ThreadsafeAllocatingQueue bufferedCommands;
// DataStructures::ThreadsafeAllocatingQueue bufferedPackets;
DataStructures::Queue bufferedPacketsFreePool;
RakNet::SimpleMutex bufferedPacketsFreePoolMutex;
DataStructures::Queue bufferedPacketsQueue;
RakNet::SimpleMutex bufferedPacketsQueueMutex;
virtual void DeallocRNS2RecvStruct(RNS2RecvStruct *s, const char *file, unsigned int line);
virtual RNS2RecvStruct *AllocRNS2RecvStruct(const char *file, unsigned int line);
void SetupBufferedPackets(void);
void PushBufferedPacket(RNS2RecvStruct * p);
RNS2RecvStruct *PopBufferedPacket(void);
struct SocketQueryOutput
{
SocketQueryOutput() {}
~SocketQueryOutput() {}
DataStructures::List sockets;
};
DataStructures::ThreadsafeAllocatingQueue socketQueryOutput;
bool AllowIncomingConnections(void) const;
void PingInternal( const SystemAddress target, bool performImmediate, PacketReliability reliability );
// This stores the user send calls to be handled by the update thread. This way we don't have thread contention over systemAddresss
void CloseConnectionInternal( const AddressOrGUID& systemIdentifier, bool sendDisconnectionNotification, bool performImmediate, unsigned char orderingChannel, PacketPriority disconnectionNotificationPriority );
void SendBuffered( const char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt );
void SendBufferedList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, RemoteSystemStruct::ConnectMode connectionMode, uint32_t receipt );
bool SendImmediate( char *data, BitSize_t numberOfBitsToSend, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, bool useCallerDataAllocation, RakNet::TimeUS currentTime, uint32_t receipt );
//bool HandleBufferedRPC(BufferedCommandStruct *bcs, RakNet::TimeMS time);
void ClearBufferedCommands(void);
void ClearBufferedPackets(void);
void ClearSocketQueryOutput(void);
void ClearRequestedConnectionList(void);
void AddPacketToProducer(RakNet::Packet *p);
unsigned int GenerateSeedFromGuid(void);
RakNet::Time GetClockDifferentialInt(RemoteSystemStruct *remoteSystem) const;
SimpleMutex securityExceptionMutex;
//DataStructures::AVLBalancedBinarySearchTree rpcTree;
int defaultMTUSize;
bool trackFrequencyTable;
// Smart pointer so I can return the object to the user
DataStructures::List socketList;
void DerefAllSockets(void);
unsigned int GetRakNetSocketFromUserConnectionSocketIndex(unsigned int userIndex) const;
// Used for RPC replies
RakNet::BitStream *replyFromTargetBS;
SystemAddress replyFromTargetPlayer;
bool replyFromTargetBroadcast;
RakNet::TimeMS defaultTimeoutTime;
// Generate and store a unique GUID
void GenerateGUID(void);
unsigned int GetSystemIndexFromGuid( const RakNetGUID input ) const;
RakNetGUID myGuid;
unsigned maxOutgoingBPS;
// Nobody would use the internet simulator in a final build.
#ifdef _DEBUG
double _packetloss;
unsigned short _minExtraPing, _extraPingVariance;
#endif
///How long it has been since things were updated by a call to receiveUpdate thread uses this to determine how long to sleep for
//unsigned int lastUserUpdateCycle;
/// True to allow connection accepted packets from anyone. False to only allow these packets from servers we requested a connection to.
bool allowConnectionResponseIPMigration;
SystemAddress firstExternalID;
int splitMessageProgressInterval;
RakNet::TimeMS unreliableTimeout;
bool (*incomingDatagramEventHandler)(RNS2RecvStruct *);
// Systems in this list will not go through the secure connection process, even when secure connections are turned on. Wildcards are accepted.
DataStructures::List securityExceptionList;
SystemAddress ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ];
bool allowInternalRouting;
void (*userUpdateThreadPtr)(RakPeerInterface *, void *);
void *userUpdateThreadData;
SignaledEvent quitAndDataEvents;
bool limitConnectionFrequencyFromTheSameIP;
SimpleMutex packetAllocationPoolMutex;
DataStructures::MemoryPool packetAllocationPool;
SimpleMutex packetReturnMutex;
DataStructures::Queue packetReturnQueue;
Packet *AllocPacket(unsigned dataSize, const char *file, unsigned int line);
Packet *AllocPacket(unsigned dataSize, unsigned char *data, const char *file, unsigned int line);
/// This is used to return a number to the user when they call Send identifying the message
/// This number will be returned back with ID_SND_RECEIPT_ACKED or ID_SND_RECEIPT_LOSS and is only returned
/// with the reliability types that contain RECEIPT in the name
SimpleMutex sendReceiptSerialMutex;
uint32_t sendReceiptSerial;
void ResetSendReceipt(void);
void OnConnectedPong(RakNet::Time sendPingTime, RakNet::Time sendPongTime, RemoteSystemStruct *remoteSystem);
void CallPluginCallbacks(DataStructures::List &pluginList, Packet *packet);
#if LIBCAT_SECURITY==1
// Encryption and security
bool _using_security, _require_client_public_key;
char my_public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES];
cat::ServerEasyHandshake *_server_handshake;
cat::CookieJar *_cookie_jar;
bool InitializeClientSecurity(RequestedConnectionStruct *rcs, const char *public_key);
#endif
virtual void OnRNS2Recv(RNS2RecvStruct *recvStruct);
void FillIPList(void);
}
// #if defined(SN_TARGET_PSP2)
// __attribute__((aligned(8)))
// #endif
;
} // namespace RakNet 36、實例操作接口
class RAK_DLL_EXPORT RakPeerInterface//網絡接口
{
public:
STATIC_FACTORY_DECLARATIONS(RakPeerInterface)
virtual ~RakPeerInterface() {}
virtual StartupResult Startup( unsigned int maxConnections, SocketDescriptor *socketDescriptors, unsigned socketDescriptorCount, int threadPriority=-99999 )=0;
virtual bool InitializeSecurity( const char *publicKey, const char *privateKey, bool bRequireClientKey = false )=0;
virtual void DisableSecurity( void )=0;
virtual void AddToSecurityExceptionList(const char *ip)=0;
virtual void RemoveFromSecurityExceptionList(const char *ip)=0;
virtual bool IsInSecurityExceptionList(const char *ip)=0;
virtual void SetMaximumIncomingConnections( unsigned short numberAllowed )=0;
virtual unsigned int GetMaximumIncomingConnections( void ) const=0;
virtual unsigned short NumberOfConnections(void) const=0;
virtual void SetIncomingPassword( const char* passwordData, int passwordDataLength )=0;
virtual void GetIncomingPassword( char* passwordData, int *passwordDataLength )=0;
virtual ConnectionAttemptResult Connect( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey=0, unsigned connectionSocketIndex=0, unsigned sendConnectionAttemptCount=12, unsigned timeBetweenSendConnectionAttemptsMS=500, RakNet::TimeMS timeoutTime=0 )=0;
virtual ConnectionAttemptResult ConnectWithSocket(const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, RakNetSocket2* socket, PublicKey *publicKey=0, unsigned sendConnectionAttemptCount=12, unsigned timeBetweenSendConnectionAttemptsMS=500, RakNet::TimeMS timeoutTime=0)=0;
//virtual bool Console2LobbyConnect( void *networkServiceId, const char *passwordData, int passwordDataLength )=0;
virtual void Shutdown( unsigned int blockDuration, unsigned char orderingChannel=0, PacketPriority disconnectionNotificationPriority=LOW_PRIORITY )=0;
virtual bool IsActive( void ) const=0;
virtual bool GetConnectionList( SystemAddress *remoteSystems, unsigned short *numberOfSystems ) const=0;
virtual uint32_t GetNextSendReceipt(void)=0;
virtual uint32_t IncrementNextSendReceipt(void)=0;
virtual uint32_t Send( const char *data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 )=0;
virtual void SendLoopback( const char *data, const int length )=0;
virtual uint32_t Send( const RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 )=0;
virtual uint32_t SendList( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast, uint32_t forceReceiptNumber=0 )=0;
virtual Packet* Receive( void )=0;
virtual void DeallocatePacket( Packet *packet )=0;
virtual unsigned int GetMaximumNumberOfPeers( void ) const=0;
virtual void CloseConnection( const AddressOrGUID target, bool sendDisconnectionNotification, unsigned char orderingChannel=0, PacketPriority disconnectionNotificationPriority=LOW_PRIORITY )=0;
virtual ConnectionState GetConnectionState(const AddressOrGUID systemIdentifier)=0;
virtual void CancelConnectionAttempt( const SystemAddress target )=0;
virtual int GetIndexFromSystemAddress( const SystemAddress systemAddress ) const=0;
virtual SystemAddress GetSystemAddressFromIndex( unsigned int index )=0;
virtual RakNetGUID GetGUIDFromIndex( unsigned int index )=0;
virtual void GetSystemList(DataStructures::List &addresses, DataStructures::List &guids) const=0;
virtual void AddToBanList( const char *IP, RakNet::TimeMS milliseconds=0 )=0;
virtual void RemoveFromBanList( const char *IP )=0;
virtual void ClearBanList( void )=0;
virtual bool IsBanned( const char *IP )=0;
virtual void SetLimitIPConnectionFrequency(bool b)=0;
virtual void Ping( const SystemAddress target )=0;
virtual bool Ping( const char* host, unsigned short remotePort, bool onlyReplyOnAcceptingConnections, unsigned connectionSocketIndex=0 )=0;
virtual int GetAveragePing( const AddressOrGUID systemIdentifier )=0;
virtual int GetLastPing( const AddressOrGUID systemIdentifier ) const=0;
virtual int GetLowestPing( const AddressOrGUID systemIdentifier ) const=0;
virtual void SetOccasionalPing( bool doPing )=0;
virtual RakNet::Time GetClockDifferential( const AddressOrGUID systemIdentifier )=0;
virtual void SetOfflinePingResponse( const char *data, const unsigned int length )=0;
virtual void GetOfflinePingResponse( char **data, unsigned int *length )=0;
virtual SystemAddress GetInternalID( const SystemAddress systemAddress=UNASSIGNED_SYSTEM_ADDRESS, const int index=0 ) const=0;
virtual void SetInternalID(SystemAddress systemAddress, int index=0)=0;
virtual SystemAddress GetExternalID( const SystemAddress target ) const=0;
virtual const RakNetGUID GetMyGUID(void) const=0;
virtual SystemAddress GetMyBoundAddress(const int socketIndex=0)=0;
static uint64_t Get64BitUniqueRandomNumber(void);
virtual const RakNetGUID& GetGuidFromSystemAddress( const SystemAddress input ) const=0;
virtual SystemAddress GetSystemAddressFromGuid( const RakNetGUID input ) const=0;
virtual bool GetClientPublicKeyFromSystemAddress( const SystemAddress input, char *client_public_key ) const=0;
virtual void SetTimeoutTime( RakNet::TimeMS timeMS, const SystemAddress target )=0;
virtual RakNet::TimeMS GetTimeoutTime( const SystemAddress target )=0;
virtual int GetMTUSize( const SystemAddress target ) const=0;
virtual unsigned GetNumberOfAddresses( void )=0;
virtual const char* GetLocalIP( unsigned int index )=0;
virtual bool IsLocalIP( const char *ip )=0;
virtual void AllowConnectionResponseIPMigration( bool allow )=0;
virtual bool AdvertiseSystem( const char *host, unsigned short remotePort, const char *data, int dataLength, unsigned connectionSocketIndex=0 )=0;
virtual void SetSplitMessageProgressInterval(int interval)=0;
virtual int GetSplitMessageProgressInterval(void) const=0;
virtual void SetUnreliableTimeout(RakNet::TimeMS timeoutMS)=0;
virtual void SendTTL( const char* host, unsigned short remotePort, int ttl, unsigned connectionSocketIndex=0 )=0;
virtual void AttachPlugin( PluginInterface2 *plugin )=0;
virtual void DetachPlugin( PluginInterface2 *messageHandler )=0;
virtual void PushBackPacket( Packet *packet, bool pushAtHead )=0;
virtual void ChangeSystemAddress(RakNetGUID guid, const SystemAddress &systemAddress)=0;
virtual Packet* AllocatePacket(unsigned dataSize)=0;
virtual RakNetSocket2* GetSocket( const SystemAddress target )=0;
virtual void GetSockets( DataStructures::List &sockets )=0;
virtual void ReleaseSockets( DataStructures::List &sockets )=0;
virtual void WriteOutOfBandHeader(RakNet::BitStream *bitStream)=0;
virtual void SetUserUpdateThread(void (*_userUpdateThreadPtr)(RakPeerInterface *, void *), void *_userUpdateThreadData)=0;
virtual void SetIncomingDatagramEventHandler( bool (*_incomingDatagramEventHandler)(RNS2RecvStruct *) )=0;
virtual void ApplyNetworkSimulator( float packetloss, unsigned short minExtraPing, unsigned short extraPingVariance)=0;
virtual void SetPerConnectionOutgoingBandwidthLimit( unsigned maxBitsPerSecond )=0;
virtual bool IsNetworkSimulatorActive( void )=0;
virtual RakNetStatistics * GetStatistics( const SystemAddress systemAddress, RakNetStatistics *rns=0 )=0;
virtual bool GetStatistics( const unsigned int index, RakNetStatistics *rns )=0;
virtual void GetStatisticsList(DataStructures::List &addresses, DataStructures::List &guids, DataStructures::List &statistics)=0;
virtual unsigned int GetReceiveBufferSize(void)=0;
virtual bool RunUpdateCycle( BitStream &updateBitStream )=0;
virtual bool SendOutOfBand(const char *host, unsigned short remotePort, const char *data, BitSize_t dataLength, unsigned connectionSocketIndex=0 )=0;
} 總結
Raknet很強大,可強大的不是它的代碼而是它的思想。
Android解析XML的三種方式SAX、Pull、Dom
在android開發中,經常用到去解析xml文件,常見的解析xml的方式有一下三種:SAX、Pull、Dom解析方式。最近做了一個android版的CSDN閱讀器,用到了
我的Android進階之旅------)Android采用AES+RSA的加密機制對http請求進行加密
前言 最近維護公司APP應用的登錄模塊,由於測試人員用Fiddler抓包工具抓取到了公司關於登錄時候的明文登錄信息。雖然使用的是HTTPS的方式進行http請求的,但還是
Android 自定義View總結
Paint與Canvas下面先說下Paint的基本設置函數:paint.setAntiAlias(true);//抗鋸齒功能paint.setColor(Color.RE
CursorLoader異步加載信鴿推送消息
Android SDK快速接入信鴿推送信鴿平台注冊應用:創建後, 查看配置獲取AcessId和AcessKey信息工程配置配置jar包:配置jni相關文件:信鴿清單文件配