KDIS  2-8-x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Connection.h
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright 2013 Karl Jones
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
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.
13 
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.
24 
25 For Further Information Please Contact me at
26 Karljj1@yahoo.com
27 http://p.sf.net/kdis/UserGuide
28 *********************************************************************/
29 
30 /********************************************************************
31  class: Connection
32  created: 27/09/2010
33  author: Karl Jones
34 
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 *********************************************************************/
39 
40 #pragma once
41 
42 #if defined( WIN32 ) | defined( _WIN32 ) | defined( WIN64 ) | defined( _WIN64 )
43 
44 // Windows Headers //
45 #pragma comment( lib, "WS2_32" )
46 #include <WinSock2.h>
47 
48 #else
49 
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>
58 
59 #endif
60 
61 #include "./../Extras/PDU_Factory.h"
62 #include "./ConnectionSubscriber.h"
63 #include <vector>
64 
65 namespace KDIS {
66 namespace NETWORK {
67 
69 {
70 protected:
71 
72  KINT32 m_iSocket[2]; // 1 for sending & 1 for receiving.
73 
75 
76  sockaddr_in m_SendToAddr;
78 
80 
81  std::vector<ConnectionSubscriber*> m_vpSubscribers;
82 
84 
85  // Allows us to handle pdu bundles
88 
89  //************************************
90  // FullName: KDIS::NETWORK::Connection::startup
91  //!Description: Setup the socket.
92  //************************************
93  void startup() throw ( KException );
94 
95  //************************************
96  // FullName: KDIS::NETWORK::Connection::bindSocket
97  //!Description: Bind the socket to receive data from all.
98  //************************************
99  void bindSocket();
100 
101  //************************************
102  // FullName: KDIS::NETWORK::Connection::shutdown
103  //!Description: Shutdown all socket features.
104  //************************************
105  void shutdown() throw ( KException );
106 
107  //************************************
108  // FullName: KDIS::NETWORK::Connection::getErrorText
109  //!Description: Convert an internal socket error code into a text description.
110  // Parameter: KINT32 ErrorCode
111  //************************************
112  const KCHAR8 * getErrorText( KINT32 ErrorCode ) const;
113 
114 public:
115 
116  // Note: If using multicast you should ensure you use a correct multicast address or an exception will occur.
117  Connection( const KString & SendAddress, KUINT32 Port = 3000, KBOOL SendAddressIsMulticast = false,
118  KBOOL Blocking = true, KDIS::UTILS::PDU_Factory * Custom = 0 );
119 
120  virtual ~Connection();
121 
122  //************************************
123  // FullName: KDIS::NETWORK::Connection::SetSendAddress
124  // KDIS::NETWORK::Connection::GetSendAddress
125  //!Description: The address data is being sent to, if multicast then
126  //! AddMulticastAddress will also be called.
127  // Parameter: const KString & A
128  // Parameter: KBOOL Multicast = false
129  //************************************
130  void SetSendAddress( const KString & A, KBOOL Multicast = false ) throw( KException );
131  const KString & GetSendAddress() const;
132 
133  //************************************
134  // FullName: KDIS::NETWORK::Connection::AddMulticastAddress
135  // KDIS::NETWORK::Connection::RemoveMulticastAddress
136  //!Description: Allows you to receive data from a multicast group.
137  //! Note: If you want to also send data to this address you should
138  //! call SetSendAddress with the same multicast address.
139  //! Note: You can add multiple groups.
140  // Parameter: const KString & A
141  //************************************
142  void AddMulticastAddress( const KString & A ) throw( KException );
143  void RemoveMulticastAddress( const KString & A ) throw( KException );
144 
145  //************************************
146  // FullName: KDIS::NETWORK::Connection::SetBlockingModeEnabled
147  //!Description: Enabled/Disable blocking mode.
148  //! If running in blocking mode calling Receive will not
149  //! return until data has been received on the network.
150  // Parameter: KBOOL E
151  //************************************
152  void SetBlockingModeEnabled( KBOOL E );
153  KBOOL IsBlockingModeEnabled() const;
154 
155  //************************************
156  // FullName: KDIS::NETWORK::Connection::AddSubscriber
157  // KDIS::NETWORK::Connection::RemoveSubscriber
158  //!Description: Add/Remove a connection subscriber.
159  //! Connection subscribers are notified when data is received/sent on the connection.
160  //! The subscribers can also be used to add a Ip address filter thus allowing certain
161  //! IP addresses to be "blocked".
162  //! Note: The subscriber's will not be deleted by this class so if you are using
163  //! dynamic Subscriber's you will also need to delete them when necessary.
164  // Parameter: const ConnectionSubscriber * S
165  //************************************
166  void AddSubscriber( ConnectionSubscriber * S );
167  void RemoveSubscriber( ConnectionSubscriber * S );
168 
169  //************************************
170  // FullName: KDIS::NETWORK::Connection::SetPDU_Factory
171  // KDIS::NETWORK::Connection::GetPDU_Factory
172  //!Description: Set/Get the PDU_Factory, by default the standard pdu factory is used however you
173  //! can create a customised version and assign it using the set function.
174  //! Note: Calling set will automatically delete the old PDU_Fatcory.
175  // Parameter: PDU_Factory * P
176  //************************************
177  void SetPDU_Factory( KDIS::UTILS::PDU_Factory * P );
178  KDIS::UTILS::PDU_Factory * GetPDU_Factory();
179 
180  //************************************
181  // FullName: KDIS::NETWORK::Connection::Send
182  //!Description: Send data over the network. Returns number of bytes sent.
183  //! Note: This function does NOT fire subscriber events.
184  // Parameter: const KOCTET * Data, KDataStream & stream
185  // Parameter: KUINT32 DataSz
186  //************************************
187  KINT32 Send( const KOCTET * Data, KUINT32 DataSz ) throw ( KException );
188  KINT32 Send( const KDataStream & stream ) throw ( KException );
189 
190  //************************************
191  // FullName: KDIS::NETWORK::Connection::SendPDU
192  //!Description: Sends a PDU over the network, fires the OnPDUTransmit event.
193  // Returns number of bytes sent.
194  // Parameter: Header * H
195  //************************************
196  KINT32 SendPDU( KDIS::PDU::Header * H ) throw ( KException );
197 
198  //************************************
199  // FullName: KDIS::NETWORK::Connection::Receive
200  //!Description: Check for new data being sent to us. Returns size of data received in octets/bytes.
201  //! This function will block in blocking mode.
202  //! If using non blocking 0 be returned if no data is available.
203  //! Note: This function does NOT fire subscriber events.
204  // Parameter: KOCTET * Buffer
205  // Parameter: KUINT32 BufferSz
206  // Parameter: KString * SenderIp - Optional field. Pass a none null pointer to get the senders IP address.
207  //************************************
208  KINT32 Receive( KOCTET * Buffer, KUINT32 BufferSz, KString * SenderIp = 0 ) throw ( KException );
209 
210  //************************************
211  // FullName: KDIS::NETWORK::Connection::GetNextPDU
212  //!Description: Checks the network for new data using Receive if not curently handling a pdu bundle,
213  // then decodes the data using the PDU factory and finally returns the decoded PDU.
214  //! If the connection is none blocking then a NULL ptr will be returned if no data is available.
215  //! Note: This function DOES fire subscriber events.
216  // Note: This function supports PDU Bundles.
217  // Parameter: KString * SenderIp - Optional field. Pass a none null pointer to get the senders IP address.
218  //************************************
219  std::auto_ptr<KDIS::PDU::Header> GetNextPDU( KString * SenderIp = 0 ) throw ( KException );
220 };
221 
222 } // END namespace NETWORK
223 } // END namespace KDIS
unsigned int KUINT32
Definition: KDefines.h:103
KString m_sSendAddress
Definition: Connection.h:77
KBOOL m_bBlockingSocket
Definition: Connection.h:79
KDIS::UTILS::PDU_Factory * m_pPduFact
Definition: Connection.h:83
char KCHAR8
Definition: KDefines.h:110
Definition: Connection.h:68
Definition: PDU_Factory.h:49
Definition: ConnectionSubscriber.h:53
std::vector< ConnectionSubscriber * > m_vpSubscribers
Definition: Connection.h:81
Definition: KDefines.h:182
Definition: KDataStream.h:48
int KINT32
Definition: KDefines.h:104
bool KBOOL
Definition: KDefines.h:119
sockaddr_in m_SendToAddr
Definition: Connection.h:76
std::string KString
Definition: KDefines.h:116
KString m_sLastIP
Definition: Connection.h:87
KDataStream m_stream
Definition: Connection.h:86
Header7 Header
Definition: Header.h:48
char KOCTET
Definition: KDefines.h:108
KUINT32 m_uiPort
Definition: Connection.h:74
#define KDIS_EXPORT
Definition: KDefines.h:82