platformNet.h

Engine/source/platform/platformNet.h

More...

Classes:

class

Platform-specific network operations.

class

Generic network address.

Public Defines

define
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#define TORQUE_NET_DEFAULT_MULTICAST_ADDRESS "ff04::7467::656E::6574::776B"
 36
 37typedef S32 NetConnectionId;
 38
 39/// Generic network address
 40///
 41/// This is used to represent IP addresses.
 42struct NetAddress 
 43{
 44   S32 type;        ///< Type of address (IPAddress currently)
 45
 46   /// Acceptable NetAddress types.
 47   enum Type
 48   {
 49      IPAddress,
 50      IPV6Address,
 51
 52      IPBroadcastAddress,
 53      IPV6MulticastAddress
 54   };
 55
 56   union
 57   {
 58      struct {
 59         U8 netNum[4];
 60      } ipv4;
 61
 62      struct {
 63         U8 netNum[16];
 64         U32 netFlow;
 65         U32 netScope;
 66      } ipv6;
 67
 68      struct {
 69         U8 netNum[16];
 70         U8 netFlow[4];
 71         U8 netScope[4];
 72      } ipv6_raw;
 73
 74   } address;
 75
 76   U16 port;
 77
 78   bool isSameAddress(const NetAddress &other) const
 79   {
 80      if (type != other.type)
 81         return false;
 82
 83      switch (type)
 84      {
 85      case NetAddress::IPAddress:
 86         return (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0);
 87         break;
 88      case NetAddress::IPV6Address:
 89         return (dMemcmp(other.address.ipv6.netNum, address.ipv6.netNum, 16) == 0);
 90         break;
 91      case NetAddress::IPBroadcastAddress:
 92         return true;
 93         break;
 94      case NetAddress::IPV6MulticastAddress:
 95         return true;
 96         break;
 97      }
 98
 99      return false;
100   }
101
102   bool isSameAddressAndPort(const NetAddress &other) const
103   {
104      if (type != other.type)
105         return false;
106
107      switch (type)
108      {
109      case NetAddress::IPAddress:
110         return (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0) && other.port == port;
111         break;
112      case NetAddress::IPV6Address:
113         return (dMemcmp(other.address.ipv6.netNum, address.ipv6.netNum, 16) == 0) && other.port == port;
114         break;
115      case NetAddress::IPBroadcastAddress:
116         return true;
117         break;
118      case NetAddress::IPV6MulticastAddress:
119         return true;
120         break;
121      }
122
123      return false;
124   }
125
126   bool isEqual(const NetAddress &other) const
127   {
128      if (type != other.type)
129         return false;
130
131      switch (type)
132      {
133      case NetAddress::IPAddress:
134         return other.port == port && (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0);
135         break;
136      case NetAddress::IPV6Address:
137         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);
138         break;
139      case NetAddress::IPBroadcastAddress:
140         return other.port == port;
141         break;
142      case NetAddress::IPV6MulticastAddress:
143         return other.port == port;
144         break;
145      }
146
147      return false;
148   }
149
150   U32 getHash() const;
151};
152
153class NetSocket
154{
155protected:
156   S32 mHandle;
157
158public:
159   NetSocket() : mHandle(-1) { ; }
160
161   inline void setHandle(S32 handleId) { mHandle = handleId; }
162   inline S32 getHandle() const { return mHandle;  }
163   inline U32 getHash() const { return mHandle; }
164
165   bool operator==(const NetSocket &other) const { return mHandle == other.mHandle; }
166   bool operator!=(const NetSocket &other) const { return mHandle != other.mHandle; }
167
168   static NetSocket fromHandle(S32 handleId) { NetSocket ret; ret.mHandle = handleId; return ret; }
169   static NetSocket INVALID;
170};
171
172/// void event(NetSocket sock, U32 state) 
173typedef JournaledSignal<void(NetSocket,U32)> ConnectionNotifyEvent;
174
175/// void event(NetSocket listeningPort, NetSocket newConnection, NetAddress originatingAddress)
176typedef JournaledSignal<void(NetSocket,NetSocket,NetAddress)> ConnectionAcceptedEvent;
177
178/// void event(NetSocket connection, RawData incomingData)
179typedef JournaledSignal<void(NetSocket,RawData)> ConnectionReceiveEvent;
180
181/// void event(NetAddress originator, RawData incomingData)
182typedef JournaledSignal<void(NetAddress,RawData)> PacketReceiveEvent;
183
184/// Platform-specific network operations.
185struct Net
186{
187   enum Error
188   {
189      NoError,
190      WrongProtocolType,
191      InvalidPacketProtocol,
192      WouldBlock,
193      NotASocket,
194      UnknownError,
195     NeedHostLookup
196   };
197
198   enum ConnectionState {
199      DNSResolved,
200      DNSFailed,
201      Connected,
202      ConnectFailed,
203      Disconnected
204   };
205
206   static const S32 MaxPacketDataSize = MAXPACKETSIZE;
207
208   static ConnectionNotifyEvent&   getConnectionNotifyEvent();
209   static ConnectionAcceptedEvent& getConnectionAcceptedEvent();
210   static ConnectionReceiveEvent&  getConnectionReceiveEvent();
211   static PacketReceiveEvent&      getPacketReceiveEvent();
212
213   static bool smMulticastEnabled;
214   static bool smIpv4Enabled;
215   static bool smIpv6Enabled;
216
217   static bool init();
218   static void shutdown();
219
220   // Unreliable net functions (UDP)
221   // sendto is for sending data
222   // all incoming data comes in on packetReceiveEventType
223   // App can only open one unreliable port... who needs more? ;)
224
225   static bool openPort(S32 connectPort, bool doBind = true);
226   static NetSocket getPort();
227
228   static void closePort();
229   static Error sendto(const NetAddress *address, const U8 *buffer, S32 bufferSize);
230
231   // Reliable net functions (TCP)
232   // all incoming messages come in on the Connected* events
233   static NetSocket openListenPort(U16 port, NetAddress::Type = NetAddress::IPAddress);
234   static NetSocket openConnectTo(const char *stringAddress); // does the DNS resolve etc.
235   static void closeConnectTo(NetSocket socket);
236   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>);
237
238   static bool compareAddresses(const NetAddress *a1, const NetAddress *a2);
239   static Net::Error stringToAddress(const char *addressString, NetAddress *address, bool hostLookup=true, int family=0);
240   static void addressToString(const NetAddress *address, char addressString[256]);
241
242   // lower level socked based network functions
243   static NetSocket openSocket();
244   static Error closeSocket(NetSocket socket);
245
246   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>);
247   static Error recv(NetSocket socket, U8 *buffer, S32 bufferSize, S32 *bytesRead);
248
249   static Error connect(NetSocket socket, const NetAddress *address);
250   static Error listen(NetSocket socket, S32 maxConcurrentListens);
251   static NetSocket accept(NetSocket acceptSocket, NetAddress *remoteAddress);
252
253   static Error bindAddress(const NetAddress &address, NetSocket socket, bool useUDP=false);
254   static Error setBufferSize(NetSocket socket, S32 bufferSize);
255   static Error setBroadcast(NetSocket socket, bool broadcastEnable);
256   static Error setBlocking(NetSocket socket, bool blockingIO);
257
258   /// Gets the desired default listen address for a specified address type
259   static Net::Error getListenAddress(const NetAddress::Type type, NetAddress *address, bool forceDefaults=false);
260   static void getIdealListenAddress(NetAddress *address);
261
262   // Multicast for ipv6 local net browsing
263   static void enableMulticast();
264   static void disableMulticast();
265   static bool isMulticastEnabled();
266
267   // Protocol state
268   static bool isAddressTypeAvailable(NetAddress::Type addressType);
269
270private:
271   static void process();
272   static void processListenSocket(NetSocket socket);
273
274};
275
276#endif
277