Tactics: Western Philosophers Vs. Musicians  0.12
A turn-based tactical game combining rules and gameplay elements inspired by Final Fantasy Tactics and the Mayfair Exponential Game System. Unlike most games of this type, motion is in full, grid-less 3D.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
USocket.h
Go to the documentation of this file.
1 // Copyright (C) 2014-2015 Dylan Blair
3 //
4 // email: dblair@alumni.cs.utexas.edu
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 
21 #ifndef USOCKET_H
22 #define USOCKET_H
23 
24 #ifdef _WIN32
25 #include <winsock2.h>
26 #define COPY_LAST_SOCKET_ERROR() sprintf(_errorBuffer, "%d", WSAGetLastError())
27 #else
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <errno.h>
33 
34 typedef int SOCKET;
35 
36 #define COPY_LAST_SOCKET_ERROR() sprintf(_errorBuffer, "%d", errno)
37 #define INVALID_SOCKET -1
38 #define SOCKET_ERROR -1
39 #endif
40 
41 #include <stdio.h>
42 
43 namespace OpenSkyNet {
44  namespace Utils {
46  class Socket {
47  public:
48  static bool init(char (&errorBuffer_)[MAX_STRING_SIZE]) {
49 #ifdef _WIN32
50  WSADATA wsaData;
51 
52  int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
53 
54  if (result != NO_ERROR) {
55  sprintf(errorBuffer_, "%d", result);
56  return false;
57  }
58 #endif
59 
60  return true;
61  }
62 
63  static void shutDown() {
64 #ifdef _WIN32
65  WSACleanup();
66 #endif
67  }
68 
69  Socket() : _socket(INVALID_SOCKET), _acceptSocket(INVALID_SOCKET), _isConnected(false) {}
70 
71  bool create() {
72  _socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
73 
74  if (_socket == INVALID_SOCKET) {
76  return false;
77  }
78 
79  return true;
80  }
81 
82  bool connectTo(const char* ipAddr_, unsigned int port_) {
83  sockaddr_in service;
84  service.sin_family = AF_INET;
85  service.sin_addr.s_addr = inet_addr(ipAddr_);
86  service.sin_port = htons(port_);
87 
88  int result = connect(_socket, (sockaddr*)&service, sizeof(service));
89 
90  if (result == SOCKET_ERROR) {
92  return false;
93  }
94 
95  _isConnected = true;
96  return true;
97  }
98 
99  bool listenTo(const char* ipAddr_, unsigned int port_) {
100  sockaddr_in service;
101  service.sin_family = AF_INET;
102  service.sin_addr.s_addr = inet_addr(ipAddr_);
103  service.sin_port = htons(port_);
104 
105  int result = bind(_socket, (sockaddr*)&service, sizeof(service));
106 
107  if (result == SOCKET_ERROR) {
109  return false;
110  }
111 
112  result = listen(_socket, SOMAXCONN);
113 
114  if (result == SOCKET_ERROR) {
116  return false;
117  }
118 
119  return true;
120  }
121 
123  assert(_acceptSocket == INVALID_SOCKET);
124  _acceptSocket = accept(_socket, NULL, NULL);
125 
126  if (_acceptSocket == INVALID_SOCKET) {
128  return false;
129  }
130 
131  _isConnected = true;
132  return true;
133  }
134 
136  if (_socket != INVALID_SOCKET) {
137 #ifdef _WIN32
138  int result = closesocket(_socket);
139 #else
140  int result = shutdown(_socket, SHUT_RDWR);
141 
142  if (result == SOCKET_ERROR) {
144  return false;
145  }
146 
147  result = close(_socket);
148 #endif
149 
150  if (result == SOCKET_ERROR) {
152  return false;
153  }
154  }
155 
156  if (_acceptSocket != INVALID_SOCKET && _acceptSocket != _socket) {
157 #ifdef _WIN32
158  int result = closesocket(_acceptSocket);
159 #else
160  int result = shutdown(_acceptSocket, SHUT_RDWR);
161 
162  if (result == SOCKET_ERROR) {
164  return false;
165  }
166 
167  result = close(_acceptSocket);
168 #endif
169 
170  if (result == SOCKET_ERROR) {
172  return false;
173  }
174  }
175 
176  _socket = INVALID_SOCKET;
177  _acceptSocket = INVALID_SOCKET;
178  _isConnected = false;
179  return true;
180  }
181 
182  bool sendData(const char* buffer_, OpenSkyNet::Utils::uint bufferLen_) {
183  if (isConnected()) {
184  int result = SOCKET_ERROR;
185 
186  if (isConnectedAsClient())
187  result = send(_socket, buffer_, static_cast<int>(bufferLen_), 0);
188  else {
189  assert(isConnectedAsServer());
190  result = send(_acceptSocket, buffer_, static_cast<int>(bufferLen_), 0);
191  }
192 
193  if (result == SOCKET_ERROR) {
195  return false;
196  }
197 
198  return true;
199  }
200 
201  sprintf(_errorBuffer, "Not Connected");
202  return false;
203  }
204 
205  int recvData(char* buffer_, OpenSkyNet::Utils::uint bufferLen_) {
206  if (isConnected()) {
207  int result = SOCKET_ERROR;
208 
209  if (isConnectedAsClient())
210  result = recv(_socket, buffer_, static_cast<int>(bufferLen_), 0);
211  else {
212  assert(isConnectedAsServer());
213  result = recv(_acceptSocket, buffer_, static_cast<int>(bufferLen_), 0);
214  }
215 
216  if (result == 0) {
217  memset(buffer_, 0, bufferLen_);
218  return 0;
219  }
220  else if (result == SOCKET_ERROR) {
222  return -1;
223  }
224 
225  return result;
226  }
227 
228  sprintf(_errorBuffer, "Not Connected");
229  return false;
230  }
231 
232  inline bool isConnected() const { return _isConnected; }
233  inline bool isConnectedAsServer() const { return isConnected() && _acceptSocket != INVALID_SOCKET; }
234  inline bool isConnectedAsClient() const { return isConnected() && _acceptSocket == INVALID_SOCKET; }
235 
236  inline const char* getLastErrorString() const { return _errorBuffer; }
237  private:
238  SOCKET _socket;
239  SOCKET _acceptSocket;
240  char _errorBuffer[MAX_STRING_SIZE];
241  bool _isConnected;
242  };
243 
245  template<class T>
246  void serialize(T val_, char*& buffer_, OpenSkyNet::Utils::uint& bufferLen_) {
247  static OpenSkyNet::Utils::uint size = sizeof(T);
248 
249  memcpy(buffer_, &val_, size);
250  bufferLen_ += size;
251  buffer_ += size;
252  }
253 
255  template<class T>
256  T assemble(char*& buffer_, OpenSkyNet::Utils::uint& bufferLen_) {
257  static OpenSkyNet::Utils::uint size = sizeof(T);
258 
259  T val = *reinterpret_cast<T*>(buffer_);
260  bufferLen_ += size;
261  buffer_ += size;
262 
263  return val;
264  }
265  }
266 }
267 
268 #endif //USOCKET_H
bool closeConnection()
Definition: USocket.h:135
const char * getLastErrorString() const
Definition: USocket.h:236
bool create()
Definition: USocket.h:71
void serialize(T val_, char *&buffer_, OpenSkyNet::Utils::uint &bufferLen_)
Definition: USocket.h:246
bool isConnectedAsClient() const
Definition: USocket.h:234
static bool init(char(&errorBuffer_)[MAX_STRING_SIZE])
Definition: USocket.h:48
#define MAX_STRING_SIZE
Definition: UTypes.h:33
bool sendData(const char *buffer_, OpenSkyNet::Utils::uint bufferLen_)
Definition: USocket.h:182
Definition: USocket.h:46
#define COPY_LAST_SOCKET_ERROR()
Definition: USocket.h:36
unsigned int uint
Definition: UTypes.h:39
bool isConnectedAsServer() const
Definition: USocket.h:233
#define INVALID_SOCKET
Definition: USocket.h:37
T assemble(char *&buffer_, OpenSkyNet::Utils::uint &bufferLen_)
Definition: USocket.h:256
static void shutDown()
Definition: USocket.h:63
bool isConnected() const
Definition: USocket.h:232
Socket()
Definition: USocket.h:69
int SOCKET
Definition: USocket.h:34
int recvData(char *buffer_, OpenSkyNet::Utils::uint bufferLen_)
Definition: USocket.h:205
#define SOCKET_ERROR
Definition: USocket.h:38
bool connectTo(const char *ipAddr_, unsigned int port_)
Definition: USocket.h:82
bool acceptConnection()
Definition: USocket.h:122
bool listenTo(const char *ipAddr_, unsigned int port_)
Definition: USocket.h:99