gwnavruntime/visualdebug/amp/Amp_Socket.h Source File

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