NetLink Sockets C++  1.0.0
Networking C++ Library
 All Classes Namespaces Files Functions Enumerations Enumerator Pages
socket.h
1 /*
2  NetLink Sockets: Networking C++ library
3  Copyright 2012 Pedro Francisco Pareja Ruiz (PedroPareja@Gmail.com)
4 
5  This file is part of NetLink Sockets.
6 
7  NetLink Sockets is free software: you can redistribute it and/or modify
8  it under the terms of the GNU 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  NetLink Sockets 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 General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with NetLink Sockets. If not, see <http://www.gnu.org/licenses/>.
19 
20 */
21 
22 #ifndef __NL_SOCKET
23 #define __NL_SOCKET
24 
25 #include "netlink/core.h"
26 
27 
28 NL_NAMESPACE
29 
39 class Socket {
40 
41  private:
42 
43  string _hostTo;
44  string _hostFrom;
45  unsigned _portTo;
46  unsigned _portFrom;
47  Protocol _protocol;
48  IPVer _ipVer;
49  SocketType _type;
50  bool _blocking;
51  unsigned _listenQueue;
52 
53  int _socketHandler;
54 
55 
56  public:
57 
58  Socket(const string& hostTo, unsigned portTo, Protocol protocol = TCP, IPVer ipVer = ANY);
59 
60  Socket(unsigned portFrom, Protocol protocol = TCP, IPVer ipVer = IP4, const string& hostFrom = "", unsigned listenQueue = DEFAULT_LISTEN_QUEUE);
61 
62  Socket(const string& hostTo, unsigned portTo, unsigned portFrom, IPVer ipVer = ANY);
63 
64  ~Socket();
65 
66 
67  Socket* accept();
68 
69  int read(void* buffer, size_t bufferSize);
70  void send(const void* buffer, size_t size);
71 
72  int readFrom(void* buffer, size_t bufferSize, string* HostFrom, unsigned* portFrom = NULL);
73  void sendTo(const void* buffer, size_t size, const string& hostTo, unsigned portTo);
74 
75  int nextReadSize() const;
76 
77  void disconnect();
78 
79  const string& hostTo() const;
80  const string& hostFrom() const;
81  unsigned portTo() const;
82  unsigned portFrom() const;
83  Protocol protocol() const;
84  IPVer ipVer() const;
85  SocketType type() const;
86  bool blocking() const;
87  unsigned listenQueue() const;
88  int socketHandler() const;
89 
90 
91  void blocking(bool blocking);
92 
93 
94  private:
95 
96  void initSocket();
97  Socket();
98 
99 };
100 
101 #include <netlink/socket.inline.h>
102 
103 NL_NAMESPACE_END
104 
105 #endif