AnCH Framework  0.1
Another C++ Hack Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
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 #ifndef _ANCH_NETWORK_SOCKET_H_
21 #define _ANCH_NETWORK_SOCKET_H_
22 
23 #include <iostream>
24 
25 #include "events/observable.hpp"
26 #include "network/socketEvent.hpp"
27 #include "network/ioException.hpp"
28 
29 
30 // Microsoft Windows operating systems defintions +
31 #ifdef ANCH_WINDOWS
32 typedef int socklen_t;
33 // Microsoft Windows operating systems defintions -
34 
35 // POSIX operating systems definitions +
36 #elif defined ANCH_POSIX
37 #include <netdb.h>
38 #define INVALID_SOCKET -1
39 #define SOCKET_ERROR -1
40 typedef int SOCKET;
41 typedef struct sockaddr_in SOCKADDR_IN;
42 typedef struct sockaddr SOCKADDR;
43 #endif
44 // POSIX operating systems definitions -
45 
46 
47 namespace anch {
48  namespace network {
49 
55  enum SocketType {
57  UNKNOWN = 0,
58 
60  TCP,
61 
63  UDP,
64 
65 #ifdef ANCH_POSIX
66 
68 #endif
69  };
70 
77  enum Direction {
79  RECEPTION = 0,
80 
83 
85  BOTH = 2,
86  };
87 
93  class Socket : public anch::events::Observable<anch::network::SocketEvent> {
94 
95  private:
96  // Attributes +
98  std::string _ipAddress;
99 
101  uint16_t _port;
102 
104  SocketType _type;
105 
106  protected:
108  SOCKET _sock;
109 
111  int _backlog;
112 
114  addrinfo* _address;
115  // Attributes -
116 
117  // Constructors +
118  protected:
125 
126  public:
137  Socket(const std::string& ipAddress,
138  uint16_t port,
139  anch::network::SocketType type = anch::network::SocketType::UNKNOWN)
140  throw(anch::network::IOException);
141  // Constructors -
142 
143  // Destructor +
147  virtual ~Socket() noexcept;
148  // Destructor -
149 
150  public:
151  // Methods +
157  virtual void bind() throw(anch::network::IOException);
158 
164  virtual void connect() throw(anch::network::IOException);
165 
171  virtual void listen() throw(anch::network::IOException);
172 
180  virtual void accept(Socket& socket) throw(anch::network::IOException);
181 
189  virtual void send(const std::string& message) throw(anch::network::IOException) = 0;
190 
198  virtual void receive(std::string& message) throw(anch::network::IOException) = 0;
199 
205  virtual void receive() throw(anch::network::IOException);
206 
215  virtual void shutdown(anch::network::Direction how = anch::network::Direction::BOTH)
216  throw(anch::network::IOException);
217 
221  virtual void close() noexcept;
222  // Methods -
223 
224  public:
225  // Accessors +
231  virtual int getDomain() const = 0;
232 
238  virtual int getType() const = 0;
239 
245  inline const std::string& getIpAddress() const {
246  return _ipAddress;
247  }
248 
254  inline void setIpAddress(const std::string& ipAddress) {
255  _ipAddress = ipAddress;
256  }
257 
263  inline uint16_t getPort() const {
264  return _port;
265  }
266 
272  inline void setIpAddress(uint16_t port) {
273  _port = port;
274  }
275 
282  return _type;
283  }
284 
290  inline void setSocketType(SocketType type) {
291  _type = type;
292  }
293 
299  inline int getBacklog() const {
300  return _backlog;
301  }
302 
308  inline void setBacklog(int backlog) {
309  _backlog = backlog;
310  }
311  // Accessors -
312 
313  };
314 
315  }
316 }
317 
318 #endif // _ANCH_NETWORK_SOCKET_H_
virtual void shutdown(anch::network::Direction how=anch::network::Direction::BOTH)
Definition: socket.cpp:238
virtual void receive()
Definition: socket.cpp:224
const std::string & getIpAddress() const
Definition: socket.hpp:245
void setIpAddress(uint16_t port)
Definition: socket.hpp:272
Socket(anch::network::SocketType type)
Definition: socket.cpp:62
Definition: socket.hpp:60
int getBacklog() const
Definition: socket.hpp:299
virtual void close() noexcept
Definition: socket.cpp:249
An observable implementation of the observers/observable design pattern.
Definition: observable.hpp:43
anch::network::SocketType getSocketType() const
Definition: socket.hpp:281
STL namespace.
virtual void accept(Socket &socket)
Definition: socket.cpp:208
int _backlog
Definition: socket.hpp:111
addrinfo * _address
Definition: socket.hpp:114
uint16_t getPort() const
Definition: socket.hpp:263
SOCKET _sock
Definition: socket.hpp:108
virtual void connect()
Definition: socket.cpp:182
Definition: socket.hpp:57
AnCH framework base namespace.
Definition: base64.hpp:28
Definition: socket.hpp:85
virtual void send(const std::string &message)=0
virtual int getDomain() const =0
Definition: socket.hpp:79
virtual void listen()
Definition: socket.cpp:195
Definition: socket.hpp:63
Definition: socket.hpp:93
void setIpAddress(const std::string &ipAddress)
Definition: socket.hpp:254
virtual int getType() const =0
Definition: socket.hpp:82
virtual void bind()
Definition: socket.cpp:164
Direction
Definition: socket.hpp:77
Definition: ioException.hpp:34
void setSocketType(SocketType type)
Definition: socket.hpp:290
SocketType
Definition: socket.hpp:55
void setBacklog(int backlog)
Definition: socket.hpp:308
Definition: socket.hpp:67