chore(include): changed include files from .h to .hpp
[KDIS.git] / include / KDIS / Network / Connection.hpp
blob1feacede45dcef2f8abaadf43affff2a88c8c062
1 /*********************************************************************
2 Copyright 2013 Karl Jones
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 For Further Information Please Contact me at
26 Karljj1@yahoo.com
27 http://p.sf.net/kdis/UserGuide
28 *********************************************************************/
30 /********************************************************************
31 class: Connection
32 created: 27/09/2010
33 author: Karl Jones
35 purpose: This class provides an interface to a DIS socket connection.
36 All sending and receiving of DIS PDU should be sent through this
37 class unless you wish to use your own socket implementation.
38 *********************************************************************/
40 #pragma once
42 #if defined( WIN32 ) | defined( _WIN32 ) | defined( WIN64 ) | defined( _WIN64 )
44 // Windows Headers //
45 #pragma comment( lib, "WS2_32" )
46 #include <WinSock2.h>
48 #else
50 // Linux Headers //
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <netdb.h>
56 #include <errno.h>
57 #include <fcntl.h>
59 #endif
61 #include "./../Extras/PDU_Factory.h"
62 #include "./ConnectionSubscriber.h"
63 #include <vector>
65 namespace KDIS {
66 namespace NETWORK {
68 class KDIS_EXPORT Connection
70 protected:
72 KINT32 m_iSocket[2]; // 1 for sending & 1 for receiving.
74 KUINT32 m_uiPort;
76 timeval m_blockingTimeout;
78 sockaddr_in m_SendToAddr;
79 KString m_sSendAddress;
81 sockaddr_in m_InterfaceAddr; // struct for IPv4 address of NIC to be used for outbound DIS
82 KString m_sInterfaceAddress; // e.g. "192.168.1.124"
84 KBOOL m_bBlockingSocket;
86 KBOOL m_bSendOnly;
87 KBOOL m_bReceiveOnly;
89 std::vector<ConnectionSubscriber*> m_vpSubscribers;
91 KDIS::UTILS::PDU_Factory * m_pPduFact;
93 // Allows us to handle pdu bundles
94 KDataStream m_stream;
95 KString m_sLastIP;
97 //************************************
98 // FullName: KDIS::NETWORK::Connection::startup
99 // Description: Setup the socket.
100 //************************************
101 void startup() ;
103 //************************************
104 // FullName: KDIS::NETWORK::Connection::bindSocket
105 // Description: Bind the socket to receive data from all.
106 //************************************
107 void bindSocket();
109 //************************************
110 // FullName: KDIS::NETWORK::Connection::shutdown
111 // Description: Shutdown all socket features.
112 //************************************
113 void shutdown() ;
115 //************************************
116 // FullName: KDIS::NETWORK::Connection::getErrorText
117 // Description: Convert an internal socket error code into a text description.
118 // Parameter: KINT32 ErrorCode
119 //************************************
120 const KCHAR8 * getErrorText( KINT32 ErrorCode ) const;
122 public:
124 // Note: If using multicast you should ensure you use a correct multicast address or an exception will occur.
125 Connection( const KString & SendAddress, KUINT32 Port = 3000, KBOOL SendAddressIsMulticast = false,
126 KBOOL Blocking = true, KDIS::UTILS::PDU_Factory * Custom = 0, KBOOL SendOnly = false,
127 KBOOL ReceiveOnly = false, const KString & InterfaceAddress = "" );
129 virtual ~Connection();
131 private:
132 //************************************
133 // FullName: KDIS::NETWORK::Connection::Connection copy constructor
134 // Description: Copy constructor for the Connection class.
135 // Parameter: const Connection& other
136 //************************************
137 Connection( const Connection& other );
139 //************************************
140 // FullName: KDIS::NETWORK::Connection::Connection copy assignment operator
141 // Description: Copy assignment operator for the Connection class.
142 // Parameter: const Connection& other
143 //************************************
144 Connection& operator=( const Connection& other );
146 public:
148 //************************************
149 // FullName: KDIS::NETWORK::Connection::SetInterfaceAddress
150 // KDIS::NETWORK::Connection::GetInterfaceAddress
151 // Description: The IPv4 address of the specific NIC to be used for DIS.
152 // Optional. For multi-NIC systems.
153 // Parameter: const KString & A
154 //************************************
155 virtual void SetInterfaceAddress( const KString & A ) ;
156 virtual const KString & GetInterfaceAddress() const;
158 //************************************
159 // FullName: KDIS::NETWORK::Connection::SetSendAddress
160 // KDIS::NETWORK::Connection::GetSendAddress
161 // Description: The address data is being sent to, if multicast then
162 // AddMulticastAddress will also be called.
163 // Parameter: const KString & A
164 // Parameter: KBOOL Multicast = false
165 //************************************
166 virtual void SetSendAddress( const KString & A, KBOOL Multicast = false ) ;
167 virtual const KString & GetSendAddress() const;
169 //************************************
170 // FullName: KDIS::NETWORK::Connection::AddMulticastAddress
171 // KDIS::NETWORK::Connection::RemoveMulticastAddress
172 // Description: Allows you to receive data from a multicast group.
173 // Note: If you want to also send data to this address you should
174 // call SetSendAddress with the same multicast address.
175 // Note: You can add multiple groups.
176 // Parameter: const KString & A
177 //************************************
178 virtual void AddMulticastAddress( const KString & A ) ;
179 virtual void RemoveMulticastAddress( const KString & A ) ;
181 //************************************
182 // FullName: KDIS::NETWORK::Connection::SetBlockingModeEnabled
183 // Description: Enabled/Disable blocking mode.
184 // If running in blocking mode calling Receive will not
185 // return until data has been received on the network.
186 // Parameter: KBOOL E
187 //************************************
188 virtual void SetBlockingModeEnabled( KBOOL E );
189 virtual KBOOL IsBlockingModeEnabled() const;
191 //************************************
192 // FullName: KDIS::NETWORK::Connection::SetBlockingTimeout
193 // Description: Sets the timeout for blocking I/O.
194 // Note: This is useful for reporting when there has been no socket
195 // I/O for a long time. Where long time == 1 second or so.
196 // Make it short enough, and you make it non-blocking I/O.
197 // Parameter: KINT32 sec
198 // Parameter: KINT32 usec
199 //************************************
200 virtual void SetBlockingTimeOut( KINT32 sec, KINT32 usec );
202 //************************************
203 // FullName: KDIS::NETWORK::Connection::AddSubscriber
204 // KDIS::NETWORK::Connection::RemoveSubscriber
205 // Description: Add/Remove a connection subscriber.
206 // Connection subscribers are notified when data is received/sent on the connection.
207 // The subscribers can also be used to add a Ip address filter thus allowing certain
208 // IP addresses to be "blocked".
209 // Note: The subscriber's will not be deleted by this class so if you are using
210 // dynamic Subscriber's you will also need to delete them when necessary.
211 // Parameter: const ConnectionSubscriber * S
212 //************************************
213 virtual void AddSubscriber( ConnectionSubscriber * S );
214 virtual void RemoveSubscriber( ConnectionSubscriber * S );
216 //************************************
217 // FullName: KDIS::NETWORK::Connection::SetPDU_Factory
218 // KDIS::NETWORK::Connection::GetPDU_Factory
219 // Description: Set/Get the PDU_Factory, by default the standard pdu factory is used however you
220 // can create a customised version and assign it using the set function.
221 // Note: Calling set will automatically delete the old PDU_Fatcory.
222 // Parameter: PDU_Factory * P
223 //************************************
224 virtual void SetPDU_Factory( KDIS::UTILS::PDU_Factory * P );
225 virtual KDIS::UTILS::PDU_Factory * GetPDU_Factory();
227 //************************************
228 // FullName: KDIS::NETWORK::Connection::Send
229 // Description: Send data over the network. Returns number of bytes sent.
230 // Note: This function does NOT fire subscriber events.
231 // Parameter: const KOCTET * Data, KDataStream & stream
232 // Parameter: KUINT32 DataSz
233 //************************************
234 virtual KINT32 Send( const KOCTET * Data, KUINT32 DataSz ) ;
235 virtual KINT32 Send( const KDataStream & stream ) ;
237 //************************************
238 // FullName: KDIS::NETWORK::Connection::SendPDU
239 // Description: Sends a PDU over the network, fires the OnPDUTransmit event.
240 // Returns number of bytes sent.
241 // Parameter: Header * H
242 //************************************
243 virtual KINT32 SendPDU( KDIS::PDU::Header * H ) ;
245 //************************************
246 // FullName: KDIS::NETWORK::Connection::Receive
247 // Description: Check for new data being sent to us. Returns size of data received in octets/bytes.
248 // This function will block in blocking mode.
249 // If using non blocking 0 be returned if no data is available.
250 // Note: This function does NOT fire subscriber events.
251 // Parameter: KOCTET * Buffer
252 // Parameter: KUINT32 BufferSz
253 // Parameter: KString * SenderIp - Optional field. Pass a none null pointer to get the senders IP address.
254 //************************************
255 virtual KINT32 Receive( KOCTET * Buffer, KUINT32 BufferSz, KString * SenderIp = 0 ) ;
257 //************************************
258 // FullName: KDIS::NETWORK::Connection::GetNextPDU
259 // Description: Checks the network for new data using Receive if not curently handling a pdu bundle,
260 // then decodes the data using the PDU factory and finally returns the decoded PDU.
261 // If the connection is none blocking then a NULL ptr will be returned if no data is available.
262 // Note: This function DOES fire subscriber events.
263 // Note: This function supports PDU Bundles.
264 // Parameter: KString * SenderIp - Optional field. Pass a none null pointer to get the senders IP address.
265 //************************************
266 virtual std::unique_ptr<KDIS::PDU::Header> GetNextPDU( KString * SenderIp = 0 ) ;
269 } // END namespace NETWORK
270 } // END namespace KDIS