LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_socket.h
Go to the documentation of this file.
1 /*
2  LibNRG - Networking for Real-time Games
3 
4  Copyright (C) 2012-2014 Alex Baines <alex@abaines.me.uk>
5 
6  This software is provided 'as-is', without any express or implied
7  warranty. In no event will the authors be held liable for any damages
8  arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any purpose,
11  including commercial applications, and to alter it and redistribute it
12  freely, subject to the following restrictions:
13 
14  1. The origin of this software must not be misrepresented; you must not
15  claim that you wrote the original software. If you use this software
16  in a product, an acknowledgment in the product documentation would be
17  appreciated but is not required.
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20  3. This notice may not be removed or altered from any source distribution.
21 */
25 #ifndef NRG_SOCKET_H
26 #define NRG_SOCKET_H
27 #include "nrg_core.h"
28 #include "nrg_netaddress.h"
29 #include "nrg_packet.h"
30 #include <memory>
31 
32 namespace nrg {
33 
35 class Socket {
36 public:
38  Socket(int type, int family = PF_UNSPEC);
40  Socket(int type, const NetAddress& addr);
41 
43  void setFamilyFromAddress(const NetAddress& addr);
44 
46  Status bind(const NetAddress& addr);
47 
49  Status connect(const NetAddress& addr);
50 
52  void disconnect();
53 
55  bool isConnected() const;
56 
58  Status sendPacket(const Packet& p) const;
59 
61  Status sendPacket(const Packet& p, const NetAddress& addr) const;
62 
64  Status recvPacket(Packet& p) const;
65 
68 
70  template<typename T>
71  bool setOption(int level, int name, const T& opt){
72  return setsockopt(fd, level, name, &opt, sizeof(T)) == 0;
73  }
74 
76  bool dataPending(int usToBlock = 0) const;
77 
79  void setNonBlocking(bool nonblock);
80 
82  void enableTimestamps(bool enable);
83 
85  void handleUnconnectedICMPErrors(bool enable);
86 
88  const std::unique_ptr<NetAddress>& getBoundAddress() const;
89 
91  const std::unique_ptr<NetAddress>& getConnectedAddress() const {
92  return connected_addr;
93  }
94 
96  uint64_t getLastTimestamp() const {
97  return last_timestamp;
98  }
99 
101  virtual ~Socket();
102 private:
103  Status checkErrorQueue(NetAddress& culprit);
104 
105  mutable std::unique_ptr<NetAddress> bound_addr, connected_addr;
106  int fd, family, type;
107  bool do_timestamp, use_errqueue;
108  uint64_t last_timestamp;
109 };
110 
112 struct UDPSocket : public Socket {
113  UDPSocket(int family = PF_UNSPEC);
114  UDPSocket(const NetAddress& a);
115 };
116 
117 }
118 
119 #endif
void enableTimestamps(bool enable)
Enables or disables the use of getLastTimestamp()
Socket derivative specifically for the User-Datagram Protocol.
Definition: nrg_socket.h:112
virtual ~Socket()
Default destructor.
void setNonBlocking(bool nonblock)
Sets this socket as non-blocking.
bool isConnected() const
Returns true if the socket is connected.
Class for storing data to be sent / received across the network.
Definition: nrg_packet.h:58
Contains the NetAddress class for wrapping POSIX sockaddr structures and resolving hostnames to IP ad...
uint64_t getLastTimestamp() const
Gets a timestamp of when the last packet was received, if available and enabled.
Definition: nrg_socket.h:96
void handleUnconnectedICMPErrors(bool enable)
Enables or disables checking for ICMP related errors on this socket, if available.
void setFamilyFromAddress(const NetAddress &addr)
Sets the socket's family using the family of the given address.
Status connect(const NetAddress &addr)
Connects the socket to the given remote address.
const std::unique_ptr< NetAddress > & getConnectedAddress() const
Returns the address this socket is connected to.
Definition: nrg_socket.h:91
Socket(int type, int family=PF_UNSPEC)
Constructs a socket with the given type and family.
bool setOption(int level, int name, const T &opt)
Template wrapper around setsockopt.
Definition: nrg_socket.h:71
Status bind(const NetAddress &addr)
Bind the socket to the given local address.
Status sendPacket(const Packet &p) const
Sends a Packet to the address this socket is connected to.
const std::unique_ptr< NetAddress > & getBoundAddress() const
Returns the address this socket is bound to.
Class to wrap system and internal errors.
Definition: nrg_status.h:34
bool dataPending(int usToBlock=0) const
Uses select() to determine if data is available for reading on this socket.
Base socket class.
Definition: nrg_socket.h:35
void disconnect()
Disconnects the socket.
Status recvPacket(Packet &p) const
Receive a Packet from this socket's connected address.
Common defines and includes used by all the other nrg header files.
Class to wrap the various POSIX sockaddrs and resolve hostnames.
Definition: nrg_netaddress.h:32