00001 /* Copyright 2010 Jukka Jylänki 00002 00003 Licensed under the Apache License, Version 2.0 (the "License"); 00004 you may not use this file except in compliance with the License. 00005 You may obtain a copy of the License at 00006 00007 http://www.apache.org/licenses/LICENSE-2.0 00008 00009 Unless required by applicable law or agreed to in writing, software 00010 distributed under the License is distributed on an "AS IS" BASIS, 00011 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 See the License for the specific language governing permissions and 00013 limitations under the License. */ 00014 #pragma once 00015 00019 #include "LockFreePoolAllocator.h" 00020 #include "FragmentedTransferManager.h" 00021 00022 namespace kNet 00023 { 00024 00026 typedef unsigned long packet_id_t; 00027 00030 inline bool PacketIDIsNewerThan(packet_id_t newID, packet_id_t oldID) 00031 { 00032 if (newID > oldID) 00033 return true; 00034 if (oldID - newID >= (1 << 21)) 00035 return true; 00036 return false; 00037 } 00038 00040 inline packet_id_t AddPacketID(packet_id_t id, int increment) 00041 { 00042 return (id + increment) & ((1 << 22) - 1); 00043 } 00044 00046 inline packet_id_t SubPacketID(packet_id_t id, int sub) 00047 { 00048 if ((int)id >= sub) 00049 return (packet_id_t)(id - sub); 00050 else 00051 return (packet_id_t)((1 << 22) - (sub-id)); 00052 } 00053 00056 class NetworkMessage : public PoolAllocatable<NetworkMessage> 00057 { 00058 public: 00062 NetworkMessage(); 00063 00064 NetworkMessage &operator=(const NetworkMessage &rhs); 00065 NetworkMessage(const NetworkMessage &rhs); 00066 00067 ~NetworkMessage(); 00068 00073 char *data; 00074 00075 size_t Capacity() const { return dataCapacity; } 00076 00077 size_t Size() const { return dataSize; } 00078 00085 void Resize(size_t newBytes, bool discard = true); 00086 00089 unsigned long priority; 00090 00092 packet_id_t id; 00093 00097 unsigned long contentID; 00098 00101 bool reliable; 00102 00106 bool inOrder; 00107 00111 bool obsolete; 00112 00114 bool IsNewerThan(const NetworkMessage &rhs) const { return (unsigned long)(messageNumber - rhs.messageNumber) < 0x80000000; } 00115 00117 static const unsigned long cPriorityDontSend = 0xFFFFFFFF; 00119 static const unsigned long cMaxPriority = 0xFFFFFFFE; 00120 00122 int GetTotalDatagramPackedSize() const; 00123 00125 unsigned long MessageNumber() const { return messageNumber; } 00126 00127 private: 00128 friend class MessageConnection; 00129 friend class UDPMessageConnection; 00130 friend class TCPMessageConnection; 00131 friend class FragmentedSendManager; 00132 friend struct FragmentedSendManager::FragmentedTransfer; 00133 00138 unsigned long messageNumber; 00139 00142 unsigned long reliableMessageNumber; 00143 00145 unsigned long sendCount; 00146 00148 unsigned long fragmentIndex; 00149 00151 size_t dataCapacity; 00152 00154 size_t dataSize; 00155 00158 FragmentedSendManager::FragmentedTransfer *transfer; 00159 }; 00160 00161 } // ~kNet
1.7.1