platformNet.h
Engine/source/platform/platformNet.h
Classes:
Public Defines
define
MAXPACKETSIZE() 1500
define
TORQUE_NET_DEFAULT_MULTICAST_ADDRESS() "ff04::7467::656E::6574::776B"
Public Typedefs
ConnectionAcceptedEvent
void event(NetSocket listeningPort, NetSocket newConnection, NetAddress originatingAddress)
ConnectionNotifyEvent
void event(NetSocket sock, U32 state)
ConnectionReceiveEvent
void event(NetSocket connection, RawData incomingData)
NetConnectionId
PacketReceiveEvent
void event(NetAddress originator, RawData incomingData)
Detailed Description
Public Defines
MAXPACKETSIZE() 1500
TORQUE_NET_DEFAULT_MULTICAST_ADDRESS() "ff04::7467::656E::6574::776B"
Public Typedefs
typedef JournaledSignal< void(NetSocket, NetSocket, NetAddress)> ConnectionAcceptedEvent
void event(NetSocket listeningPort, NetSocket newConnection, NetAddress originatingAddress)
typedef JournaledSignal< void(NetSocket, U32)> ConnectionNotifyEvent
void event(NetSocket sock, U32 state)
typedef JournaledSignal< void(NetSocket, RawData)> ConnectionReceiveEvent
void event(NetSocket connection, RawData incomingData)
typedef S32 NetConnectionId
typedef JournaledSignal< void(NetAddress, RawData)> PacketReceiveEvent
void event(NetAddress originator, RawData incomingData)
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _PLATFORM_PLATFORMNET_H_ 25#define _PLATFORM_PLATFORMNET_H_ 26 27#include "platform/platform.h" 28#include "core/util/rawData.h" 29#include "core/util/journal/journaledSignal.h" 30 31#ifndef MAXPACKETSIZE 32#define MAXPACKETSIZE 1500 33#endif 34 35#ifndef TORQUE_NET_DEFAULT_MULTICAST_ADDRESS 36#define TORQUE_NET_DEFAULT_MULTICAST_ADDRESS "ff04::7467::656E::6574::776B" 37#endif 38 39typedef S32 NetConnectionId; 40 41/// Generic network address 42/// 43/// This is used to represent IP addresses. 44struct NetAddress 45{ 46 S32 type; ///< Type of address (IPAddress currently) 47 48 /// Acceptable NetAddress types. 49 enum Type 50 { 51 IPAddress, 52 IPV6Address, 53 54 IPBroadcastAddress, 55 IPV6MulticastAddress 56 }; 57 58 union 59 { 60 struct { 61 U8 netNum[4]; 62 } ipv4; 63 64 struct { 65 U8 netNum[16]; 66 U32 netFlow; 67 U32 netScope; 68 } ipv6; 69 70 struct { 71 U8 netNum[16]; 72 U8 netFlow[4]; 73 U8 netScope[4]; 74 } ipv6_raw; 75 76 } address; 77 78 U16 port; 79 80 bool isSameAddress(const NetAddress &other) const 81 { 82 if (type != other.type) 83 return false; 84 85 switch (type) 86 { 87 case NetAddress::IPAddress: 88 return (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0); 89 break; 90 case NetAddress::IPV6Address: 91 return (dMemcmp(other.address.ipv6.netNum, address.ipv6.netNum, 16) == 0); 92 break; 93 case NetAddress::IPBroadcastAddress: 94 return true; 95 break; 96 case NetAddress::IPV6MulticastAddress: 97 return true; 98 break; 99 } 100 101 return false; 102 } 103 104 bool isSameAddressAndPort(const NetAddress &other) const 105 { 106 if (type != other.type) 107 return false; 108 109 switch (type) 110 { 111 case NetAddress::IPAddress: 112 return (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0) && other.port == port; 113 break; 114 case NetAddress::IPV6Address: 115 return (dMemcmp(other.address.ipv6.netNum, address.ipv6.netNum, 16) == 0) && other.port == port; 116 break; 117 case NetAddress::IPBroadcastAddress: 118 return true; 119 break; 120 case NetAddress::IPV6MulticastAddress: 121 return true; 122 break; 123 } 124 125 return false; 126 } 127 128 bool isEqual(const NetAddress &other) const 129 { 130 if (type != other.type) 131 return false; 132 133 switch (type) 134 { 135 case NetAddress::IPAddress: 136 return other.port == port && (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0); 137 break; 138 case NetAddress::IPV6Address: 139 return other.port == port && other.address.ipv6.netFlow == address.ipv6.netFlow && other.address.ipv6.netScope == address.ipv6.netScope && (dMemcmp(other.address.ipv6.netNum, address.ipv6.netNum, 16) == 0); 140 break; 141 case NetAddress::IPBroadcastAddress: 142 return other.port == port; 143 break; 144 case NetAddress::IPV6MulticastAddress: 145 return other.port == port; 146 break; 147 } 148 149 return false; 150 } 151 152 U32 getHash() const; 153}; 154 155class NetSocket 156{ 157protected: 158 S32 mHandle; 159 160public: 161 NetSocket() : mHandle(-1) { ; } 162 163 inline void setHandle(S32 handleId) { mHandle = handleId; } 164 inline S32 getHandle() const { return mHandle; } 165 inline U32 getHash() const { return mHandle; } 166 167 bool operator==(const NetSocket &other) const { return mHandle == other.mHandle; } 168 bool operator!=(const NetSocket &other) const { return mHandle != other.mHandle; } 169 170 static NetSocket fromHandle(S32 handleId) { NetSocket ret; ret.mHandle = handleId; return ret; } 171 static NetSocket INVALID; 172}; 173 174/// void event(NetSocket sock, U32 state) 175typedef JournaledSignal<void(NetSocket,U32)> ConnectionNotifyEvent; 176 177/// void event(NetSocket listeningPort, NetSocket newConnection, NetAddress originatingAddress) 178typedef JournaledSignal<void(NetSocket,NetSocket,NetAddress)> ConnectionAcceptedEvent; 179 180/// void event(NetSocket connection, RawData incomingData) 181typedef JournaledSignal<void(NetSocket,RawData)> ConnectionReceiveEvent; 182 183/// void event(NetAddress originator, RawData incomingData) 184typedef JournaledSignal<void(NetAddress,RawData)> PacketReceiveEvent; 185 186/// Platform-specific network operations. 187struct Net 188{ 189 enum Error 190 { 191 NoError, 192 WrongProtocolType, 193 InvalidPacketProtocol, 194 WouldBlock, 195 NotASocket, 196 UnknownError, 197 NeedHostLookup 198 }; 199 200 enum ConnectionState { 201 DNSResolved, 202 DNSFailed, 203 Connected, 204 ConnectFailed, 205 Disconnected 206 }; 207 208 static const S32 MaxPacketDataSize = MAXPACKETSIZE; 209 210 static ConnectionNotifyEvent& getConnectionNotifyEvent(); 211 static ConnectionAcceptedEvent& getConnectionAcceptedEvent(); 212 static ConnectionReceiveEvent& getConnectionReceiveEvent(); 213 static PacketReceiveEvent& getPacketReceiveEvent(); 214 215 static bool smMulticastEnabled; 216 static bool smIpv4Enabled; 217 static bool smIpv6Enabled; 218 219 static bool init(); 220 static void shutdown(); 221 222 // Unreliable net functions (UDP) 223 // sendto is for sending data 224 // all incoming data comes in on packetReceiveEventType 225 // App can only open one unreliable port... who needs more? ;) 226 227 static bool openPort(S32 connectPort, bool doBind = true); 228 static NetSocket getPort(); 229 230 static void closePort(); 231 static Error sendto(const NetAddress *address, const U8 *buffer, S32 bufferSize); 232 233 // Reliable net functions (TCP) 234 // all incoming messages come in on the Connected* events 235 static NetSocket openListenPort(U16 port, NetAddress::Type = NetAddress::IPAddress); 236 static NetSocket openConnectTo(const char *stringAddress); // does the DNS resolve etc. 237 static void closeConnectTo(NetSocket socket); 238 static Error sendtoSocket(NetSocket socket, const U8 *buffer, S32 bufferSize, S32 *bytesWritten=<a href="/coding/file/types_8lint_8h/#types_8lint_8h_1a070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>); 239 240 static bool compareAddresses(const NetAddress *a1, const NetAddress *a2); 241 static Net::Error stringToAddress(const char *addressString, NetAddress *address, bool hostLookup=true, int family=0); 242 static void addressToString(const NetAddress *address, char addressString[256]); 243 244 // lower level socked based network functions 245 static NetSocket openSocket(); 246 static Error closeSocket(NetSocket socket); 247 248 static Error send(NetSocket socket, const U8 *buffer, S32 bufferSize, S32 *outBytesWritten=<a href="/coding/file/types_8lint_8h/#types_8lint_8h_1a070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>); 249 static Error recv(NetSocket socket, U8 *buffer, S32 bufferSize, S32 *bytesRead); 250 251 static Error connect(NetSocket socket, const NetAddress *address); 252 static Error listen(NetSocket socket, S32 maxConcurrentListens); 253 static NetSocket accept(NetSocket acceptSocket, NetAddress *remoteAddress); 254 255 static Error bindAddress(const NetAddress &address, NetSocket socket, bool useUDP=false); 256 static Error setBufferSize(NetSocket socket, S32 bufferSize); 257 static Error setBroadcast(NetSocket socket, bool broadcastEnable); 258 static Error setBlocking(NetSocket socket, bool blockingIO); 259 260 /// Gets the desired default listen address for a specified address type 261 static Net::Error getListenAddress(const NetAddress::Type type, NetAddress *address, bool forceDefaults=false); 262 static void getIdealListenAddress(NetAddress *address); 263 264 // Multicast for ipv6 local net browsing 265 static void enableMulticast(); 266 static void disableMulticast(); 267 static bool isMulticastEnabled(); 268 269 // Protocol state 270 static bool isAddressTypeAvailable(NetAddress::Type addressType); 271 272private: 273 static void process(); 274 static void processListenSocket(NetSocket socket); 275 276}; 277 278#endif 279
