summaryrefslogtreecommitdiff
path: root/test/client-server/socket_wrapper.h
blob: a18e67946791b0af427391229efce7fd8cfc291d (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
// 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.
//

#ifndef _SOCKET_WRAPPER_H
#define _SOCKET_WRAPPER_H

struct SocketWrapperImpl;

#if defined(__linux__ ) || defined(__ORBIS__)
typedef int SOCKET_TYPE;
#else
typedef int socklen_t;
#if defined(_WIN64)
	typedef unsigned __int64 SOCKET_TYPE;
#else
	typedef unsigned int SOCKET_TYPE;
#endif
#endif

int init_networking();
int term_networking();
int set_non_blocking(SOCKET_TYPE s);
bool wouldblock();

/*********************************************************************************
 Super-simple wrapper class for doing send/receive on non-blocking sockets
*********************************************************************************/
class SocketWrapper
{
public:

	SocketWrapper(SOCKET_TYPE platformSocket);
	~SocketWrapper();

	bool is_usable() const;

	void enqueue_send_message(void* msgData, unsigned int msgLength);

	bool has_recv_message() const;
	unsigned int recv_message_length() const;
	void* recv_message() const;
	void consume_recv_message();

	enum PumpResult 
	{
		PumpResult_NoTraffic = 0,	// The internal send/recv queues are both empty, and there was no new traffic on this pump
		PumpResult_Traffic,
		PumpResult_Error
	};

	PumpResult pump();

private:

	SocketWrapperImpl* pImpl;
};

/*********************************************************************************
 Misc x-plat helpers...
*********************************************************************************/

////////////////////////////////////////////////////////////////////////////////
#if defined(__linux__) || defined(__ORBIS__)
////////////////////////////////////////////////////////////////////////////////
#define closesocket close
#define INVALID_SOCKET (-1)
////////////////////////////////////////////////////////////////////////////////
#endif
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
#if defined(__ORBIS__)
////////////////////////////////////////////////////////////////////////////////
int gethostname(char* buff, int buffsize);
#define SOMAXCONN 128
////////////////////////////////////////////////////////////////////////////////
#endif
////////////////////////////////////////////////////////////////////////////////

#endif	// _MESSAGETYPES_H