Spinning Topp Logo BlackTopp Studios
inc
udpsocket.cpp
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_cpp
42 #define _networkudpsocket_cpp
43 
44 #include "Network/platformsocket.h.cpp"
45 
46 #include "Network/udpsocket.h"
47 
48 // Because MinGW doesn't have it's shit together
49 #ifdef MEZZ_WIN_MINGW
50  #ifndef MCAST_JOIN_GROUP
51  #define MCAST_JOIN_GROUP 41
52  #endif//MCAST_JOIN_GROUP
53  #ifndef MCAST_LEAVE_GROUP
54  #define MCAST_LEAVE_GROUP 42
55  #endif//MCAST_LEAVE_GROUP
56  #ifndef MCAST_BLOCK_SOURCE
57  #define MCAST_BLOCK_SOURCE 43
58  #endif//MCAST_BLOCK_SOURCE
59  #ifndef MCAST_UNBLOCK_SOURCE
60  #define MCAST_UNBLOCK_SOURCE 44
61  #endif//MCAST_UNBLOCK_SOURCE
62  #ifndef MCAST_JOIN_SOURCE_GROUP
63  #define MCAST_JOIN_SOURCE_GROUP 45
64  #endif//MCAST_JOIN_SOURCE_GROUP
65  #ifndef MCAST_LEAVE_SOURCE_GROUP
66  #define MCAST_LEAVE_SOURCE_GROUP 46
67  #endif//MCAST_LEAVE_SOURCE_GROUP
68  #ifndef MCAST_MSFILTER
69  #define MCAST_MSFILTER 47
70  #endif//MCAST_MSFILTER
71 #endif//MEZZ_WIN_MINGW
72 
73 namespace Mezzanine
74 {
75  namespace Network
76  {
78  { }
79 
81  { }
82 
83  ///////////////////////////////////////////////////////////////////////////////
84  // Utility
85 
87  { return Network::TLP_UDP; }
88 
89  ///////////////////////////////////////////////////////////////////////////////
90  // Core Operations
91 
92  Integer UDPSocket::Send(const void* Buffer, const UInt32 BufSize, const SystemAddress& Address) const
93  { return this->InternalSocket->SendTo(Buffer,BufSize,0,Address); }
94 
95  Integer UDPSocket::Receive(void* Buffer, const UInt32 BufSize, SystemAddress& Address) const
96  { return this->InternalSocket->ReceiveFrom(Buffer,BufSize,0,Address); }
97 
98  Integer UDPSocket::ReceivePeek(void* Buffer, const UInt32 BufSize, SystemAddress& Address) const
99  { return this->InternalSocket->ReceiveFrom(Buffer,BufSize,MSG_PEEK,Address); }
100 
101  ///////////////////////////////////////////////////////////////////////////////
102  // Multicasting
103 
104  void UDPSocket::JoinMulticastGroup(const SystemAddress& GroupAddress, const UInt32 Interface)
105  {
106  group_req MCast;
107  MCast.gr_interface = Interface;
108  MCast.gr_group = ConvertToSocketStorage(GroupAddress);
109  this->InternalSocket->SetSocketOption(ConvertLayer3ProtocolType(this->GetNetworkLayerProtocol()),MCAST_JOIN_GROUP,(char*)&MCast,sizeof(MCast));
110  }
111 
112  void UDPSocket::LeaveMulticastGroup(const SystemAddress& GroupAddress, const UInt32 Interface)
113  {
114  group_req MCast;
115  MCast.gr_interface = Interface;
116  MCast.gr_group = ConvertToSocketStorage(GroupAddress);
117  this->InternalSocket->SetSocketOption(ConvertLayer3ProtocolType(this->GetNetworkLayerProtocol()),MCAST_LEAVE_GROUP,(char*)&MCast,sizeof(MCast));
118  }
119 
120  void UDPSocket::JoinMulticastGroupSource(const SystemAddress& GroupAddress, const SystemAddress& SourceAddress, const UInt32 Interface)
121  {
122  group_source_req MCast;
123  MCast.gsr_interface = Interface;
124  MCast.gsr_group = ConvertToSocketStorage(GroupAddress);
125  MCast.gsr_source = ConvertToSocketStorage(SourceAddress);
126  this->InternalSocket->SetSocketOption(ConvertLayer3ProtocolType(this->GetNetworkLayerProtocol()),MCAST_JOIN_SOURCE_GROUP,(char*)&MCast,sizeof(MCast));
127  }
128 
129  void UDPSocket::LeaveMulticastGroupSource(const SystemAddress& GroupAddress, const SystemAddress& SourceAddress, const UInt32 Interface)
130  {
131  group_source_req MCast;
132  MCast.gsr_interface = Interface;
133  MCast.gsr_group = ConvertToSocketStorage(GroupAddress);
134  MCast.gsr_source = ConvertToSocketStorage(SourceAddress);
135  this->InternalSocket->SetSocketOption(ConvertLayer3ProtocolType(this->GetNetworkLayerProtocol()),MCAST_LEAVE_SOURCE_GROUP,(char*)&MCast,sizeof(MCast));
136  }
137 
138  void UDPSocket::BlockMulticastSource(const SystemAddress& GroupAddress, const SystemAddress& SourceAddress, const UInt32 Interface)
139  {
140  group_source_req MCast;
141  MCast.gsr_interface = Interface;
142  MCast.gsr_group = ConvertToSocketStorage(GroupAddress);
143  MCast.gsr_source = ConvertToSocketStorage(SourceAddress);
144  this->InternalSocket->SetSocketOption(ConvertLayer3ProtocolType(this->GetNetworkLayerProtocol()),MCAST_BLOCK_SOURCE,(char*)&MCast,sizeof(MCast));
145  }
146 
147  void UDPSocket::UnblockMulticastSource(const SystemAddress& GroupAddress, const SystemAddress& SourceAddress, const UInt32 Interface)
148  {
149  group_source_req MCast;
150  MCast.gsr_interface = Interface;
151  MCast.gsr_group = ConvertToSocketStorage(GroupAddress);
152  MCast.gsr_source = ConvertToSocketStorage(SourceAddress);
153  this->InternalSocket->SetSocketOption(ConvertLayer3ProtocolType(this->GetNetworkLayerProtocol()),MCAST_UNBLOCK_SOURCE,(char*)&MCast,sizeof(MCast));
154  }
155 
156  ///////////////////////////////////////////////////////////////////////////////
157  // Configuration
158 
159  void UDPSocket::SetBroadcast(const Boole Broadcast)
160  {
161  int Value = ( Broadcast ? 1 : 0 );
162  this->InternalSocket->SetSocketOption(Network::Sol_Socket,Network::SockOpts_Broadcast,(char*)&Value,sizeof(Value));
163  }
164 
166  {
167  int Value = 0;
168  AddrLen Size;
169  this->InternalSocket->GetSocketOption(Network::Sol_Socket,Network::SockOpts_Broadcast,(char*)&Value,&Size);
170  return ( Value != 0 );
171  }
172  }//Network
173 }//Mezzanine
174 
175 #endif
UDPSocket()
Class constructor.
Definition: udpsocket.cpp:77
PlatformSocket * InternalSocket
A pointer to the internal system socket.
Definition: socket.h:163
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
sockaddr_storage ConvertToSocketStorage(const SystemAddress &MezzAddr)
Converts a Mezzanine address to a Berkeley sockets address.
User Datagram Protocol.
Boole GetSocketOption(const Integer Level, const Integer OptID, char *Value, AddrLen *ValueLen) const
Get a low level socket parameter.
void LeaveMulticastGroupSource(const SystemAddress &GroupAddress, const SystemAddress &SourceAddress, const UInt32 Interface=0)
Unsubscribes this socket from a specific source within a multicast group.
Definition: udpsocket.cpp:129
short int ConvertLayer3ProtocolType(const NetworkLayerProtocol Protocol)
Converts from a Mezzanine address protocol type to it's internal counterpart.
virtual ~UDPSocket()
Class destructor.
Definition: udpsocket.cpp:80
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
void BlockMulticastSource(const SystemAddress &GroupAddress, const SystemAddress &SourceAddress, const UInt32 Interface=0)
Block transmissions from a specific source within a multicast group.
Definition: udpsocket.cpp:138
Integer ReceivePeek(void *Buffer, const UInt32 BufSize, SystemAddress &Address) const
Reads raw data recieved on this socket from it's configured peer, but doesn't remove that data from t...
Definition: udpsocket.cpp:98
void JoinMulticastGroupSource(const SystemAddress &GroupAddress, const SystemAddress &SourceAddress, const UInt32 Interface=0)
Subscribes this socket to specific source within a multicast group.
Definition: udpsocket.cpp:120
void UnblockMulticastSource(const SystemAddress &GroupAddress, const SystemAddress &SourceAddress, const UInt32 Interface=0)
Unlock transmissions from a specific source within a multicast group.
Definition: udpsocket.cpp:147
Integer SendTo(const void *Buffer, const Integer BufSize, const Integer Flags, const SystemAddress &Address)
Sends data to a remote host.
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
Integer ReceiveFrom(void *Buffer, const Integer BufSize, const Integer Flags, SystemAddress &Address)
Receives data from a remote host.
Integer Send(const void *Buffer, const UInt32 BufSize, const SystemAddress &Address) const
Sends raw data out the socket to it's configured peer.
Definition: udpsocket.cpp:92
void JoinMulticastGroup(const SystemAddress &GroupAddress, const UInt32 Interface=0)
Subscribes this socket to a multicast group.
Definition: udpsocket.cpp:104
TransportLayerProtocol GetTransportLayerProtocol() const
Gets the layer 4 protocol this socket is operating on.
Definition: udpsocket.cpp:86
virtual NetworkLayerProtocol GetNetworkLayerProtocol() const =0
Gets the layer 3 protocol this socket is operating on.
Boole GetBroadcast() const
Gets whether or not the socket is allowed to send broadcast messages.
Definition: udpsocket.cpp:165
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
void SetBroadcast(const Boole Broadcast)
Sets whether or not the socket is allowed to send broadcast messages.
Definition: udpsocket.cpp:159
void LeaveMulticastGroup(const SystemAddress &GroupAddress, const UInt32 Interface=0)
Unsubscribes this socket from a multicast group.
Definition: udpsocket.cpp:112
Integer Receive(void *Buffer, const UInt32 BufSize, SystemAddress &Address) const
Reads raw data recieved on this socket from it's configured peer.
Definition: udpsocket.cpp:95
A simple class that stores a complete set of information for establishing connections.
Definition: systemaddress.h:53
Boole SetSocketOption(const Integer Level, const Integer OptID, const char *Value, const AddrLen ValueLen)
Set a low level socket parameter.