File: socket.h

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (155 lines) | stat: -rw-r--r-- 4,125 bytes parent folder | download
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
/*
	$Id: socket.h,v 1.21 2001/10/15 09:37:43 mbn Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	See http://www.clanlib.org
	------------------------------------------------------------------------
*/

//! clanNetwork="Sockets"
//! header=network.h

#ifndef header_socket
#define header_socket

#include "../signals.h"
#include "ip_address.h"
#include "inputsource_socket.h"
#include "outputsource_socket.h"

class CL_EventTrigger;
class CL_Socket_Generic;

//: This is the lowlevel raw socket interface in clanlib.
//- <p>Its main purpose is to write the C sockets API, providing error handling 
//- via exceptions, and socket waiting (select) via the clanlib event waiting 
//- system (CL_EventListener, CL_EventTrigger).</p>
class CL_Socket
{
public:
	//: Type enum
	enum Type
	{
		tcp,
		udp
	};

	//: ShutdownHow enum
	enum ShutdownHow
	{
		shutdown_receive,
		shutdown_send
	};

//! Construction:
public:
	//: Constructs a null socket.
	CL_Socket();

	//: Constructs an attached socket.
	CL_Socket(int socket);

	//: Constructs a new socket using the specified protocol type.
	CL_Socket(Type type);

	//: Copy constructor.
	CL_Socket(const CL_Socket &copy);

	//: Socket destructor
	virtual ~CL_Socket();

//! Attributes:
public:
	//: Input source that operates on the socket.
	CL_InputSource_Socket input;

	//: Output source that operates on the socket.
	CL_OutputSource_Socket output;

	//: Returns the raw socket handle (for use with the lowlevel socket functions).
	int get_socket() const;

	//: Returns an event trigger that triggers when socket has data ready to be read.
	CL_EventTrigger *get_read_trigger() const;

	//: Returns an event trigger that triggers when socket is ready for additional data.
	CL_EventTrigger *get_write_trigger() const;

	//: Returns an event trigger that triggers when an exception occurs on the socket.
	CL_EventTrigger *get_exception_trigger() const;

	//: Returns the socket name.
	CL_IPAddress get_source_address() const;

	//: Returns the name of the destination socket we are connected to.
	CL_IPAddress get_dest_address() const;

	//: This signal is invoked when the socket has data ready to be read.
	CL_Signal_v0 &sig_read_triggered();

	//: This signal is invoked when the socket has data ready to be written.
	CL_Signal_v0 &sig_write_triggered();

	//: This signal is invoked when an exception occurred on the socket.
	CL_Signal_v0 &sig_exception_triggered();

//! Operations:
public:
	//: Copy operator
	CL_Socket &operator =(const CL_Socket &other);

	//: Sets the socket blocking mode.
	void set_nonblocking(bool nonblocking = true);

	//: If enabled, don't delay send to coalesce packets.
	void set_nodelay(bool nodelay = true);

	//: Writes data to socket. Returns the amount that was written.
	int send(const void *data, int size);

	//: Writes data to socket, using the specified destination host.
	int send(const void *data, int size, const CL_IPAddress &dest);

	int send(const std::string &string);
	//!operations:
	// Writes data to socket. Returns the amount that was written.

	void push(const std::string &string);
	//!operations:
	// Push data back into the received data buffer.

	//: Reads data from the socket. Returns the amount that was read.
	int recv(void *data, int size);

	//: Reads data from the socket, storing the from address in the passed parameter.
	int recv(void *data, int size, CL_IPAddress &from);

	//: Initiate a connection on the socket.
	void connect(const CL_IPAddress &address);

	//: Shut down part of full-duplex connection.
	void shutdown(ShutdownHow how);

	//: Bind the socket to the specified address.
	void bind(const CL_IPAddress &address);

	//: Listen for connections on the socket.
	void listen(int backlog);

	//: Accept a connection on the socket.
	CL_Socket accept();

//! Implementation:
public:
	CL_Socket(class CL_Socket_Generic *impl);
	CL_Socket_Generic *impl;
};

#endif