Spinning Topp Logo BlackTopp Studios
inc
udpsocket.h
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 
41 #ifndef _networkudpsocket_h
42 #define _networkudpsocket_h
43 
44 #include "Network/socket.h"
45 #include "Network/systemaddress.h"
46 
47 namespace Mezzanine
48 {
49  namespace Network
50  {
51  ///////////////////////////////////////////////////////////////////////////////
52  /// @brief A socket class using the User Datagram Protocol.
53  ///////////////////////////////////////
54  class MEZZ_LIB UDPSocket : virtual public Socket
55  {
56  protected:
57  public:
58  /// @brief Class constructor.
59  UDPSocket();
60  /// @brief Class destructor.
61  virtual ~UDPSocket();
62 
63  ///////////////////////////////////////////////////////////////////////////////
64  // Utility
65 
66  /// @copydoc Socket::GetTransportLayerProtocol() const
67  TransportLayerProtocol GetTransportLayerProtocol() const;
68 
69  ///////////////////////////////////////////////////////////////////////////////
70  // Core Operations
71 
72  /// @brief Sends raw data out the socket to it's configured peer.
73  /// @remarks This method is intended for use on unconnected sockets, although it's use on connected sockets is valid.
74  /// @param Buffer A raw buffer to be sent.
75  /// @param BufSize The size of the raw buffer to send.
76  /// @param Address The address to send the packet to.
77  /// @return Returns the number of bytes sent, or -1 if there was an error.
78  Integer Send(const void* Buffer, const UInt32 BufSize, const SystemAddress& Address) const;
79 
80  /// @brief Reads raw data recieved on this socket from it's configured peer.
81  /// @remarks This method is intended for use on unconnected sockets, although it's use on connected sockets is valid.
82  /// @param Buffer A raw buffer to be written to.
83  /// @param BufSize The size of the raw buffer to write to.
84  /// @param Address The address of the packet to retreive.
85  /// @return Returns the number of bytes received/written to the buffer, or -1 if there was an error.
86  Integer Receive(void* Buffer, const UInt32 BufSize, SystemAddress& Address) const;
87  /// @brief Reads raw data recieved on this socket from it's configured peer, but doesn't remove that data from the receive buffer.
88  /// @remarks This method is intended for use on unconnected sockets, although it's use on connected sockets is valid.
89  /// An additional call to Receive will return the same data returned by this call, as the data wasn't removed from the queue with this method.
90  /// @param Buffer A raw buffer to be written to.
91  /// @param BufSize The size of the raw buffer to write to.
92  /// @param Address The address of the packet to retreive.
93  /// @return Returns the number of bytes received/written to the buffer, or -1 if there was an error.
94  Integer ReceivePeek(void* Buffer, const UInt32 BufSize, SystemAddress& Address) const;
95 
96  ///////////////////////////////////////////////////////////////////////////////
97  // Multicasting
98 
99  /// @brief Subscribes this socket to a multicast group.
100  /// @param GroupAddress The remote address of the multicast group to subscribe to.
101  /// @param Interface The index of the local interface on which the multicast group should be joined. This can be zero to accept any viable interface.
102  void JoinMulticastGroup(const SystemAddress& GroupAddress, const UInt32 Interface = 0);
103  /// @brief Unsubscribes this socket from a multicast group.
104  /// @param GroupAddress The remote address of the multicast group to subscribe to.
105  /// @param Interface The index of the local interface on which the multicast group should be dropped. This can be zero to accept any viable interface.
106  void LeaveMulticastGroup(const SystemAddress& GroupAddress, const UInt32 Interface = 0);
107  /// @brief Subscribes this socket to specific source within a multicast group.
108  /// @param GroupAddress The remote address of the multicast group to subscribe to.
109  /// @param SourceAddress The remote address of a specific multicast transmitter to listen to.
110  /// @param Interface The index of the local interface on which the multicast group source should be joined. This can be zero to accept any viable interface.
111  void JoinMulticastGroupSource(const SystemAddress& GroupAddress, const SystemAddress& SourceAddress, const UInt32 Interface = 0);
112  /// @brief Unsubscribes this socket from a specific source within a multicast group.
113  /// @param GroupAddress The remote address of the multicast group to subscribe to.
114  /// @param SourceAddress The remote address of a specific multicast transmitter to stop listening to.
115  /// @param Interface The index of the local interface on which the multicast group source should be dropped. This can be zero to accept any viable interface.
116  void LeaveMulticastGroupSource(const SystemAddress& GroupAddress, const SystemAddress& SourceAddress, const UInt32 Interface = 0);
117  /// @brief Block transmissions from a specific source within a multicast group.
118  /// @param GroupAddress The remote address of the multicast group to subscribe to.
119  /// @param SourceAddress The remote address of a specific multicast transmitter to ignore.
120  /// @param Interface The index of the local interface on which the multicast group source should be blocked. This can be zero to accept any viable interface.
121  void BlockMulticastSource(const SystemAddress& GroupAddress, const SystemAddress& SourceAddress, const UInt32 Interface = 0);
122  /// @brief Unlock transmissions from a specific source within a multicast group.
123  /// @param GroupAddress The remote address of the multicast group to subscribe to.
124  /// @param SourceAddress The remote address of a specific multicast transmitter to stop ignoring.
125  /// @param Interface The index of the local interface on which the multicast group source should be unblocked. This can be zero to accept any viable interface.
126  void UnblockMulticastSource(const SystemAddress& GroupAddress, const SystemAddress& SourceAddress, const UInt32 Interface = 0);
127 
128  ///////////////////////////////////////////////////////////////////////////////
129  // Configuration
130 
131  /// @brief Sets whether or not the socket is allowed to send broadcast messages.
132  /// @param Broadcast Whether or not to enable broadcasting on this socket.
133  void SetBroadcast(const Boole Broadcast);
134  /// @brief Gets whether or not the socket is allowed to send broadcast messages.
135  /// @return Returns true if broadcasts are allowed on this socket, false otherwise.
136  Boole GetBroadcast() const;
137 
138  /// @brief Sets the number of hops permitted when sending to a multicast address before the message is destroyed.
139  /// @param Hops The number of hops a multicast message sent from this socket is allowed to make. Cannot be greater than 255.
140  virtual void SetNumMulticastHops(const Integer Hops) = 0;
141  /// @brief Gets the number of hops permitted when sending to a multicast address before the message is destroyed.
142  /// @return Returns the number of times a message from this socket sent to a multicast address is allowed to be routed before being destroyed.
143  virtual Integer GetNumMulticastHops() const = 0;
144  /// @brief Sets whether or not this socket will receive multicast trasmissions it sends.
145  /// @param Loop Whether or not multicast messages sent by this socket will also be received by this socket.
146  virtual void SetMulticastLoop(const Boole Loop) = 0;
147  /// @brief Gets whether or not this socket will receive multicast trasmissions it sends.
148  /// @return Returns true if this socket will receive it's own multicast transmissions, false otherwise.
149  virtual Boole GetMulticastLoop() const = 0;
150  };//UDPSocket
151  }//Network
152 }//Mezzanine
153 
154 #endif
This is a basic socket interface for the transmission and retrieval of packets.
Definition: socket.h:152
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
A socket class using the User Datagram Protocol.
Definition: udpsocket.h:54
TransportLayerProtocol
This is an enum listing for recognized protocols on Layer 4 of the OSI model.
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
A simple class that stores a complete set of information for establishing connections.
Definition: systemaddress.h:53