AnCH Framework 0.1
Another C++ Hack Framework
Loading...
Searching...
No Matches
socket.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3 Copyright (C) 2012 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include <cstring>
23
24#include "events/observable.hpp"
25#include "network/socketEvent.hpp"
26#include "network/ioException.hpp"
27
28
29// Microsoft Windows operating systems defintions +
30#ifdef ANCH_WINDOWS
31typedef int socklen_t;
32// Microsoft Windows operating systems defintions -
33
34// POSIX operating systems definitions +
35#elif defined ANCH_POSIX
36#include <netdb.h>
37#define INVALID_SOCKET -1
38#define SOCKET_ERROR -1
39typedef int SOCKET;
40typedef struct sockaddr_in SOCKADDR_IN;
41typedef struct sockaddr SOCKADDR;
42#endif
43// POSIX operating systems definitions -
44
45
46namespace anch::network {
47
53 enum class SocketType {
56
59
62
63#ifdef ANCH_POSIX
66#endif
67 };
68
75 enum class Direction {
78
81
83 BOTH = 2,
84 };
85
91 class Socket : public anch::events::Observable<SocketEvent> {
92
93 private:
94 // Attributes +
96 std::string _ipAddress;
97
99 uint16_t _port;
100
102 SocketType _type;
103
104 protected:
106 SOCKET _sock;
107
110
112 addrinfo* _address;
113 // Attributes -
114
115 // Constructors +
116 protected:
123
124 public:
135 Socket(const std::string& ipAddress, uint16_t port, SocketType type = SocketType::UNKNOWN);
136 // Constructors -
137
138 // Destructor +
142 virtual ~Socket() noexcept;
143 // Destructor -
144
145 public:
146 // Methods +
152 virtual void bind();
153
159 virtual void connect();
160
166 virtual void listen();
167
175 virtual void accept(Socket& socket);
176
184 virtual void send(const std::string& message) = 0;
185
193 virtual void receive(std::string& message) = 0;
194
200 virtual void receive();
201
210 virtual void shutdown(Direction how = Direction::BOTH);
211
215 virtual void close() noexcept;
216 // Methods -
217
218 public:
219 // Accessors +
225 virtual int getDomain() const = 0;
226
232 virtual int getType() const = 0;
233
239 inline const std::string& getIpAddress() const {
240 return _ipAddress;
241 }
242
248 inline void setIpAddress(const std::string& ipAddress) {
249 _ipAddress = ipAddress;
250 }
251
257 inline uint16_t getPort() const {
258 return _port;
259 }
260
266 inline void setIpAddress(uint16_t port) {
267 _port = port;
268 }
269
276 return _type;
277 }
278
284 inline void setSocketType(SocketType type) {
285 _type = type;
286 }
287
293 inline int getBacklog() const {
294 return _backlog;
295 }
296
302 inline void setBacklog(int backlog) {
303 _backlog = backlog;
304 }
305 // Accessors -
306
307 };
308
309}
An observable implementation of the observers/observable design pattern.
Definition observable.hpp:44
SOCKET _sock
Definition socket.hpp:106
void setIpAddress(uint16_t port)
Definition socket.hpp:266
virtual void listen()
virtual void bind()
Socket(SocketType type)
virtual int getDomain() const =0
virtual void send(const std::string &message)=0
void setIpAddress(const std::string &ipAddress)
Definition socket.hpp:248
virtual void connect()
virtual int getType() const =0
virtual void receive(std::string &message)=0
virtual ~Socket() noexcept
virtual void accept(Socket &socket)
addrinfo * _address
Definition socket.hpp:112
Socket(const std::string &ipAddress, uint16_t port, SocketType type=SocketType::UNKNOWN)
int getBacklog() const
Definition socket.hpp:293
void setBacklog(int backlog)
Definition socket.hpp:302
const std::string & getIpAddress() const
Definition socket.hpp:239
anch::network::SocketType getSocketType() const
Definition socket.hpp:275
void setSocketType(SocketType type)
Definition socket.hpp:284
virtual void close() noexcept
virtual void shutdown(Direction how=Direction::BOTH)
uint16_t getPort() const
Definition socket.hpp:257
int _backlog
Definition socket.hpp:109
Network namespace.
Definition ioException.hpp:25
Direction
Definition socket.hpp:75
@ RECEPTION
Definition socket.hpp:77
@ TRANSMISSION
Definition socket.hpp:80
@ BOTH
Definition socket.hpp:83
SocketType
Definition socket.hpp:53
@ POSIX
Definition socket.hpp:65
@ UNKNOWN
Definition socket.hpp:55
@ TCP
Definition socket.hpp:58
@ UDP
Definition socket.hpp:61