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 ConnectionNotifyEvent* smConnectionNotify; 220 static ConnectionAcceptedEvent* smConnectionAccept; 221 static ConnectionReceiveEvent* smConnectionReceive; 222 static PacketReceiveEvent* smPacketReceive; 223 224 225 static bool init(); 226 static void shutdown(); 227 228 // Unreliable net functions (UDP) 229 // sendto is for sending data 230 // all incoming data comes in on packetReceiveEventType 231 // App can only open one unreliable port... who needs more? ;) 232 233 static bool openPort(S32 connectPort, bool doBind = true); 234 static NetSocket getPort(); 235 236 static void closePort(); 237 static Error sendto(const NetAddress *address, const U8 *buffer, S32 bufferSize); 238 239 // Reliable net functions (TCP) 240 // all incoming messages come in on the Connected* events 241 static NetSocket openListenPort(U16 port, NetAddress::Type = NetAddress::IPAddress); 242 static NetSocket openConnectTo(const char *stringAddress); // does the DNS resolve etc. 243 static void closeConnectTo(NetSocket socket); 244 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>); 245 246 static bool compareAddresses(const NetAddress *a1, const NetAddress *a2); 247 static Net::Error stringToAddress(const char *addressString, NetAddress *address, bool hostLookup=true, int family=0); 248 static void addressToString(const NetAddress *address, char addressString[256]); 249 250 // lower level socked based network functions 251 static NetSocket openSocket(); 252 static Error closeSocket(NetSocket socket); 253 254 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>); 255 static Error recv(NetSocket socket, U8 *buffer, S32 bufferSize, S32 *bytesRead); 256 257 static Error connect(NetSocket socket, const NetAddress *address); 258 static Error listen(NetSocket socket, S32 maxConcurrentListens); 259 static NetSocket accept(NetSocket acceptSocket, NetAddress *remoteAddress); 260 261 static Error bindAddress(const NetAddress &address, NetSocket socket, bool useUDP=false); 262 static Error setBufferSize(NetSocket socket, S32 bufferSize); 263 static Error setBroadcast(NetSocket socket, bool broadcastEnable); 264 static Error setBlocking(NetSocket socket, bool blockingIO); 265 266 /// Gets the desired default listen address for a specified address type 267 static Net::Error getListenAddress(const NetAddress::Type type, NetAddress *address, bool forceDefaults=false); 268 static void getIdealListenAddress(NetAddress *address); 269 270 // Multicast for ipv6 local net browsing 271 static void enableMulticast(); 272 static void disableMulticast(); 273 static bool isMulticastEnabled(); 274 275 // Protocol state 276 static bool isAddressTypeAvailable(NetAddress::Type addressType); 277 278private: 279 static void process(); 280 static void processListenSocket(NetSocket socket); 281 282}; 283 284#endif 285