gwnavruntime/visualdebug/amp/Amp_Socket.h Source File

Amp_Socket.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 /**************************************************************************
8 
9 Filename : Amp_Socket.h
10 Content : Socket wrapper class
11 
12 Created : July 03, 2009
13 Authors : Boris Rayskiy, Alex Mantzaris
14 
15 **************************************************************************/
16 
17 #pragma once
18 
22 
23 #ifdef KY_ENABLE_SOCKETS
24 
25 namespace Kaim {
26 class String;
27 class Lock;
28 namespace Net {
29 namespace AMP {
30 
31 class SocketInterface
32 {
33 public:
34  virtual ~SocketInterface() { }
35  virtual bool CreateStream(bool listener) = 0;
36  virtual bool CreateDatagram(bool broadcast) = 0;
37  virtual bool Bind() = 0;
38  virtual bool Listen(int) = 0;
39  virtual bool Connect() = 0;
40  virtual bool Accept(int timeoutMs) = 0;
41  virtual int Send(const char*, UPInt) const = 0;
42  virtual int Receive(char*, int) const = 0;
43  virtual int SendBroadcast(const char*, UPInt) const = 0;
44  virtual int ReceiveBroadcast(char*, int) const = 0;
45  virtual void SetListenPort(UInt32) = 0;
46  virtual void SetBroadcastPort(UInt32) = 0;
47  virtual void SetAddress(UInt32, const char*) = 0;
48  virtual void SetBlocking(bool) = 0;
49  virtual void SetBroadcast(bool) = 0;
50  virtual void GetName(UInt32*, UInt32*, char*, UInt32) = 0;
51  virtual bool Shutdown(int timeoutMsForOrderlyRelease) = 0;
52  virtual bool Startup() = 0;
53  virtual void Cleanup() = 0;
54  virtual int GetLastError() const = 0;
55  virtual bool IsValid() const = 0;
56  virtual bool IsListening() const = 0;
57  virtual bool ShutdownListener(int timeoutMsForOrderlyRelease) = 0;
58  virtual bool CheckAbort() const = 0;
59 };
60 
61 class SocketImplFactory
62 {
63 public:
64  virtual ~SocketImplFactory() { }
65  virtual SocketInterface* Create() = 0;
66  virtual void Destroy(SocketInterface* socketImpl) = 0;
67 };
68 
69 template<class C>
70 class DefaultSocketFactory : public NewOverrideBase<Stat_Default_Mem>, public SocketImplFactory
71 {
72 public:
73  virtual ~DefaultSocketFactory() { }
74  virtual SocketInterface* Create()
75  {
76  return KY_NEW C();
77  }
78  virtual void Destroy(SocketInterface* socketImpl)
79  {
80  delete socketImpl;
81  }
82 };
83 
84 enum LogSilentMode
85 {
86  LOG_SILENT,
87  LOG_VERBOSE
88 };
89 
90 class Socket
91 {
92 public:
93  Socket(bool initLib, SocketImplFactory* socketImplFactory);
94  ~Socket();
95 
96  // Create a socket, either to a specific address and port (client), or a listener (server)
97  bool CreateClient(const char* ipAddress, UInt32 port, String* errorMsg);
98  bool CreateServer(UInt32 port, String* errorMsg);
99  // Shuts down and performs cleanup, timeoutMsForOrderlyRelease == 0 means no orderly release
100  void Destroy(int timeoutMsForOrderlyRelease = 0);
101  // Wait until an incoming connection is accepted (only relevant for server sockets)
102  bool Accept(int timeoutMs);
103  // Send a packet over the network
104  int Send(const char* dataBuffer, UPInt dataBufferSize, LogSilentMode silentMode = LOG_VERBOSE) const;
105  // Receive a packet over the network (blocks)
106  int Receive(char* dataBuffer, int dataSize, LogSilentMode silentMode = LOG_VERBOSE) const;
107  // Test the outgoing connection
108  bool IsConnected() const;
109  // Set blocking
110  void SetBlocking(bool blocking);
111  // Set a lock for multithreaded access
112  void SetLock(Lock* lock);
113  // Is the socket valid?
114  bool IsValid() const;
115  // Check whether a reconnection should happen
116  bool CheckAbort() const;
117 
118 private:
119  enum { SocketListenBacklog = 10 }; // Number of simultaneous connections (server only)
120 
121  SocketImplFactory* SocketFactory;
122  SocketInterface* SocketImpl;
123  bool IsServer;
124  bool InitLib;
125 
126  Lock* CreateLock;
127 
128  // Private helper functions
129  bool Shutdown(int timeoutMsForOrderlyRelease);
130  bool Startup();
131  void Cleanup();
132 };
133 
134 class BroadcastSocket
135 {
136 public:
137  BroadcastSocket(bool initLib, SocketImplFactory* socketImplFactory);
138  ~BroadcastSocket();
139  bool Create(UInt32 port, bool broadcast);
140  void Destroy();
141  int Broadcast(const char* dataBuffer, UPInt dataBufferSize) const;
142  int Receive(char* dataBuffer, int dataSize) const;
143  void GetName(UInt32* port, UInt32* address, char* name, UInt32 nameSize) const;
144  int GetLastError() const { return SocketImpl->GetLastError(); }
145 
146 private:
147  SocketImplFactory* SocketFactory;
148  SocketInterface* SocketImpl;
149  bool InitLib;
150 };
151 
152 
153 #ifndef KY_AMP_CHANNEL_NAME
154 #define KY_AMP_CHANNEL_NAME "GwNavAmp"
155 #endif
156 
157 } // namespace AMP
158 } // namespace Net
159 } // namespace Kaim
160 
161 #endif // KY_ENABLE_SOCKETS
162 
std::uint32_t UInt32
uint32_t
Definition: SF_Types.h:137
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17