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
|
/*
** ClanLib SDK
** Copyright (c) 1997-2005 The ClanLib Team
**
** This software is provided 'as-is', without any express or implied
** warranty. In no event will the authors be held liable for any damages
** arising from the use of this software.
**
** Permission is granted to anyone to use this software for any purpose,
** including commercial applications, and to alter it and redistribute it
** freely, subject to the following restrictions:
**
** 1. The origin of this software must not be misrepresented; you must not
** claim that you wrote the original software. If you use this software
** in a product, an acknowledgment in the product documentation would be
** appreciated but is not required.
** 2. Altered source versions must be plainly marked as such, and must not be
** misrepresented as being the original software.
** 3. This notice may not be removed or altered from any source distribution.
**
** Note: Some of the libraries ClanLib may link to may have additional
** requirements or restrictions.
**
** File Author(s):
**
** Magnus Norddahl
** (if your name is missing here, please add it)
*/
//! clanNetwork="Sockets"
//! header=network.h
#ifndef header_socket
#define header_socket
#ifdef CL_API_DLL
#ifdef CL_NETWORK_EXPORT
#define CL_API_NETWORK __declspec(dllexport)
#else
#define CL_API_NETWORK __declspec(dllimport)
#endif
#else
#define CL_API_NETWORK
#endif
#if _MSC_VER > 1000
#pragma once
#endif
#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.
//- !group=Network/Sockets!
//- !header=network.h!
class CL_API_NETWORK CL_Socket
{
public:
//: Type enum
enum Type
{
tcp,
udp
};
//: ShutdownHow enum
enum ShutdownHow
{
shutdown_receive,
shutdown_send
};
//: Which protocol to use
enum Protocol
{
ipv4,
ipv6
};
//! 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, Protocol proto = ipv4);
//: Copy constructor.
CL_Socket(const CL_Socket ©);
//: 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 the maximum size of a message for message-oriented socket types (CL_Socket::udp).
int get_max_msg_size() 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;
//: Returns the list of broadcast addresses available to this socket. Works for UDP on IPv4 only.
std::list<CL_IPAddress> get_broadcast_addresses(const std::string&) const;
//! 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);
//: If enabled, do broadcasting. Works for UDP on IPv4 only
void set_broadcasting(bool broadcasting = 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);
//: Writes data to socket. Returns the amount that was written.
int send(const std::string &string);
//: Push data back into the received data buffer.
void push(const std::string &string);
//: 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();
//! Signals:
public:
//: 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();
//: This signal is invoked when the remote side drops the connection, either through timeout
//: or explicit shutdown
CL_Signal_v0 &sig_disconnected();
//! Implementation:
public:
CL_Socket(class CL_Socket_Generic *impl);
CL_Socket_Generic *impl;
Type proto;
};
#endif
|