summaryrefslogtreecommitdiff
path: root/test/client-server/socket_wrapper.cpp
blob: ab759ba6a4096ba4805deaff4131a56a91904423 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// This code contains NVIDIA Confidential Information and is disclosed 
// under the Mutual Non-Disclosure Agreement. 
// 
// Notice 
// ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES 
// NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO 
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT, 
// MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. 
// 
// NVIDIA Corporation assumes no responsibility for the consequences of use of such 
// information or for any infringement of patents or other rights of third parties that may 
// result from its use. No license is granted by implication or otherwise under any patent 
// or patent rights of NVIDIA Corporation. No third party distribution is allowed unless 
// expressly authorized by NVIDIA.  Details are subject to change without notice. 
// This code supersedes and replaces all information previously supplied. 
// NVIDIA Corporation products are not authorized for use as critical 
// components in life support devices or systems without express written approval of 
// NVIDIA Corporation. 
// 
// Copyright � 2008- 2013 NVIDIA Corporation. All rights reserved.
//
// NVIDIA Corporation and its licensors retain all intellectual property and proprietary
// rights in and to this software and related documentation and any modifications thereto.
// Any use, reproduction, disclosure or distribution of this software and related
// documentation without an express license agreement from NVIDIA Corporation is
// strictly prohibited.
//

#include "socket_wrapper.h"
#include <stdio.h>


////////////////////////////////////////////////////////////////////////////////
#if defined(__linux__) || defined(__ORBIS__)
////////////////////////////////////////////////////////////////////////////////

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>

#include <errno.h>

bool wouldblock()
{
    return EWOULDBLOCK == errno || EAGAIN == errno;
}

int init_networking() { return 0; }
int term_networking() { return 0; }

#define SOCKET_ERROR (-1)

////////////////////////////////////////////////////////////////////////////////
#endif // __linux__ || __ORBIS__
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
#if defined(__linux__)
////////////////////////////////////////////////////////////////////////////////

#include <netdb.h> 

int set_non_blocking(SOCKET_TYPE s)
{
    int flags = fcntl(s, F_GETFL, 0);
    return fcntl(s, F_SETFL, flags | O_NONBLOCK);
}


////////////////////////////////////////////////////////////////////////////////
#elif defined(__ORBIS__)
////////////////////////////////////////////////////////////////////////////////

#include <libnetctl.h>
#include <string.h>

int set_non_blocking(SOCKET_TYPE s)
{
	const int nonzero = -1;
	return setsockopt(s,SCE_NET_SOL_SOCKET,SCE_NET_SO_NBIO,&nonzero,sizeof(nonzero));
}

int gethostname(char* buff, int buffsize)
{
	SceNetCtlInfo ci;
	if(sceNetCtlGetInfo(SCE_NET_CTL_INFO_DHCP_HOSTNAME,&ci))
	{
		return -1;
	}

	if(strlen(ci.dhcp_hostname) >= buffsize)
	{
		return -1;
	}

	if(0 == ci.dhcp_hostname[0])
	{
		// Effectively no name
		return -1;
	}

	strcpy(buff,ci.dhcp_hostname);

	return 0;
}


////////////////////////////////////////////////////////////////////////////////
#else   // !__linux__ && !__ORBIS__
////////////////////////////////////////////////////////////////////////////////

#include <winsock2.h>

int set_non_blocking(SOCKET_TYPE s)
{
    u_long iMode=1;
    return ioctlsocket(s,FIONBIO,&iMode);
}

bool wouldblock()
{
	const int lse = WSAGetLastError();
	return WSAEWOULDBLOCK == lse;
}

int init_networking()
{
	// Start networking
	WSADATA wsData;
	if(WSAStartup(0x202,&wsData)) {
		fprintf(stderr,"ERROR: WSAStartup failed\n");
		return -1;
	}

    return 0;
}

int term_networking()
{
    WSACleanup();
	return 0;
}

////////////////////////////////////////////////////////////////////////////////
#endif
////////////////////////////////////////////////////////////////////////////////


#define _HAS_EXCEPTIONS 0
#include <vector>
#include <assert.h>

struct SocketWrapperImpl
{
	SOCKET_TYPE socket;
	std::vector<char> recvMessageBytes;
	std::vector<char> sendMessageBytes;

	bool hasError;
	bool isConnected;
};

SocketWrapper::SocketWrapper(SOCKET_TYPE platformSocket) :
	pImpl(new SocketWrapperImpl)
{
	pImpl->socket = platformSocket;
	pImpl->hasError = false;

	// Assume connected and error-free until we know otherwise
	pImpl->isConnected = true;

	// Set the socket to non-blocking mode
	if(set_non_blocking(pImpl->socket)) {
		fprintf(stderr,"ERROR: Failed to set socket to non-blocking\n");
		pImpl->hasError = true;
	}
}

SocketWrapper::~SocketWrapper()
{
	delete pImpl;
}

void SocketWrapper::enqueue_send_message(void* msgData, unsigned int msgLength)
{
	// We add in bytes for the message length, which makes the message longer...
	unsigned int modifiedMsgLength = sizeof(msgLength) + msgLength;

	pImpl->sendMessageBytes.insert(pImpl->sendMessageBytes.end(),(char*)&modifiedMsgLength,((char*)&modifiedMsgLength)+sizeof(modifiedMsgLength));
	pImpl->sendMessageBytes.insert(pImpl->sendMessageBytes.end(),(char*)msgData,((char*)msgData)+msgLength);
}

bool SocketWrapper::is_usable() const
{
	return pImpl->isConnected && !pImpl->hasError;
}

bool SocketWrapper::has_recv_message() const
{
	if(pImpl->recvMessageBytes.size() > sizeof(unsigned int)) {
		unsigned int* pMessageLength = (unsigned int*)&pImpl->recvMessageBytes[0];
		if(pImpl->recvMessageBytes.size() >= *pMessageLength) {
			return true;
		}
	}

	return false;
}

unsigned int SocketWrapper::recv_message_length() const
{
	assert(has_recv_message());

	unsigned int* pMessageLength = (unsigned int*)&pImpl->recvMessageBytes[0];
	return *pMessageLength;
}

void* SocketWrapper::recv_message() const
{
	assert(has_recv_message());
	return &pImpl->recvMessageBytes[sizeof(unsigned int)];
}

void SocketWrapper::consume_recv_message()
{
	assert(has_recv_message());

	unsigned int* pMessageLength = (unsigned int*)&pImpl->recvMessageBytes[0];
	pImpl->recvMessageBytes.erase(pImpl->recvMessageBytes.begin(),pImpl->recvMessageBytes.begin()+*pMessageLength);
}

SocketWrapper::PumpResult SocketWrapper::pump()
{
	PumpResult result = pImpl->recvMessageBytes.empty() ? PumpResult_NoTraffic : PumpResult_Traffic;

	// Always check recv
	char recvbuffer[256];
	int recvresult = recv(pImpl->socket, recvbuffer, sizeof(recvbuffer), 0);
	if(SOCKET_ERROR == recvresult) {
		if(!wouldblock()) {
			fprintf(stderr,"ERROR: Failed to recv client socket\n");
			pImpl->hasError = true;
			result = PumpResult_Error;
		}
	} else if(0 == recvresult) {
		pImpl->isConnected = false;
	} else {
		result = PumpResult_Traffic;
		pImpl->recvMessageBytes.insert(pImpl->recvMessageBytes.end(), recvbuffer, recvbuffer + recvresult);
	}

	// Only need to pump send when there is stuff to send
	if(!pImpl->sendMessageBytes.empty()) {
		result = PumpResult_Traffic;			// Just attempting to send qualifies as traffic
		int sendresult = send(pImpl->socket,(char*)&pImpl->sendMessageBytes[0],(int)pImpl->sendMessageBytes.size(),0);
		if(SOCKET_ERROR == sendresult) {
			if(!wouldblock()) {
				fprintf(stderr,"ERROR: Failed to send on client socket\n");
				pImpl->hasError = true;
				result = PumpResult_Error;
			}
		} else {
			pImpl->sendMessageBytes.erase(pImpl->sendMessageBytes.begin(),pImpl->sendMessageBytes.begin()+sendresult);
		}
	}

	return result;
}