sipxmedialib  Version 3.3
NetworkChannel.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006-2013 SIPez LLC. All rights reserved.
3 //
4 // Copyright (C) 2004-2006 SIPfoundry Inc.
5 // Licensed by SIPfoundry under the LGPL license.
6 //
7 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
8 // Licensed to SIPfoundry under a Contributor Agreement.
9 //
10 // $$
12 
13 
14 #ifndef _INC_NETWORKCHANNEL_H_
15 #define _INC_NETWORKCHANNEL_H_
16 
17 #include "rtcp/RtcpConfig.h"
18 
19 #ifdef PINGTEL_OSSOCKET
20 #include "os/OsSocket.h"
21 #endif
22 
23 #if defined(_WIN32)
24 #include <winsock2.h>
25 #elif defined(_VXWORKS)
26 #include <sockLib.h>
27 #include <ioLib.h>
28 typedef struct sockaddr_in SOCKADDR_IN;
29 #define SOCKET_ERROR ERROR
30 #elif defined(__pingtel_on_posix__)
31 #include <unistd.h>
32 #include <netinet/in.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
36 typedef struct sockaddr_in SOCKADDR_IN;
37 typedef struct sockaddr SOCKADDR;
38 #define SOCKET_ERROR (-1)
39 #else
40 #error Unsupported target platform.
41 #endif
42 
43 // Includes
44 #include "BaseClass.h"
45 #include "INetworkChannel.h"
46 #include "INetworkSource.h"
47 #include "INetworkRender.h"
48 #include "INetDispatch.h"
49 
50 
51 
52 class CNetworkChannel : public CBaseClass,
53  public INetworkSource,
54  public INetworkRender
55 {
56 
57 
58 public:
59 
60 #ifndef PINGTEL_OSSOCKET
61 
62 
63 /*|><|***********************************************************************
64  Name: CNetworkChannel constructor
65 
66  Description: Performs initial construction on CNetworkChannel class in
67  addition to setting internal properties.
68 
69  Returns: None.
70  ***********************************************************************|><|*/
71  CNetworkChannel(INetDispatch *piNetDispatch=NULL, unsigned short usPort=0);
72 
73 #else
74 
75 /*|><|***********************************************************************
76  Name: CNetworkChannel constructor
77 
78  Description: An alternate constructor defined to handle use of Pingtel's
79  OSSocket object. In this mode, this object acts as a simple
80  wrapper that calls the Pingtel OSSocket object's Write method
81  whenever Send() is called. The OSSocket has already been
82  initialized by external logic and is operational when passed.
83 
84  Returns: None.
85  ***********************************************************************|><|*/
86  CNetworkChannel(OsSocket & rOsSocket);
87 #endif
88 
89 /*|><|***********************************************************************
90  Name: CNetworkChannel destructor
91 
92  Description: Performs final destruction on CNetworkChannel class.
93 
94  Returns: None.
95  ***********************************************************************|><|*/
96  ~CNetworkChannel(void);
97 
98 
99 /*|><|***********************************************************************
100  Name: Initialize
101 
102  Description: A polymorphic method that handles the creation and
103  initialization of an inbound or outbound connectionless
104  datagram channels. This method shall create and open the
105  datagram channel in addition to binding it to establish a
106  local port ID.
107 
108  Returns: bool.
109  ***********************************************************************|><|*/
110  virtual bool Initialize(void);
111 
112 
113 /*|><|***********************************************************************
114  Name: CNetworkChannel::Open
115 
116  Description: A polymorphic method that manages the creation and
117  of an inbound or outbound connectionless datagram channel.
118 
119  Returns: bool.
120  ***********************************************************************|><|*/
121  virtual bool Open(int iAddressType, int iSocketType, int iProtocol);
122 
123 
124 /*|><|***********************************************************************
125  Name: Connect
126 
127  Description: A polymorphic method that manages the connection of an inbound
128  or outbound connectionless datagram channels over TCP/IP.
129  This is a non-blocking call.
130 
131  Returns: bool.
132  ***********************************************************************|><|*/
133  virtual bool Connect(unsigned char *puchIPAddress=NULL,
134  unsigned short usPortID=0);
135 
136 /*|><|***********************************************************************
137  Name: Receive
138 
139  Description: A polymorphic method that processes any receive events
140  associated with a channel. This version of receive shall
141  handle inbound data on either a connectionless channel.
142 
143  Returns: int.
144  ***********************************************************************|><|*/
145  virtual int Receive(unsigned char * puchBuffer, unsigned long ulRxBytes);
146 
147 /*|><|***********************************************************************
148  Name: Dispatch
149 
150  Description: A method that takes a data transmission forwarded by the
151  Receive() method and routes it to the appopriate receiver.
152 
153  Returns: None.
154  ***********************************************************************|><|*/
155  virtual void Dispatch(unsigned char * puchBuffer,
156  unsigned long ulBytesRecvd);
157 
158 /*|><|***********************************************************************
159  Name: Send
160 
161  Description: A private method that takes a data transmission delivered by the
162  StreamClient's DataReceived() callback and sends it the
163  associated channel socket for transmission to the respective FE.
164  The Send() method also contains some conditionally compiled logic
165  to collect statistics regarding packets being sent.
166 
167  Returns: bool.
168  ***********************************************************************|><|*/
169  virtual int Send(unsigned char * puchDataBuffer,
170  unsigned long ulBytesToSend);
171 
172  virtual int getSocketDescriptor(void);
173  virtual int getSocketPort(void);
174 /*|><|***********************************************************************
175  Name: Close
176 
177  Description: Amethod that closes a connectionless datagram channel.
178 
179  Returns: bool.
180  ***********************************************************************|><|*/
181  virtual bool Close(void);
182 
183 
184 /*|><|***********************************************************************
185  Name: GetSocket
186 
187  Description: An IChannel interface method that retrieves the socket handle
188  associated with a given channel.
189 
190  Returns: unsigned int.
191  ***********************************************************************|><|*/
192  unsigned int GetSocket(void) { return m_hSocket; }
193 
194 /*|><|***********************************************************************
195  Name: GetPort
196 
197  Description: An IChannel interface method that retrieves the port ID
198  associated with a given channel.
199 
200  Returns: unsigned short.
201  ***********************************************************************|><|*/
202  unsigned short GetPort(void) { return m_ipLocal.sin_port; }
203 
204 
205 /*|><|***********************************************************************
206  Name: IsOpened
207 
208  Description: An IChannel interface method that returns a flag identifying
209  whether a channel has been opened.
210 
211  Returns: None.
212  ***********************************************************************|><|*/
213  bool IsOpened(void) { return((bool)m_fFlags.bfOpen); }
214 
215 /*|><|***********************************************************************
216  Name: IsConnected
217 
218  Description: An IChannel interface method that returns a flag identifying
219  whether a channel has been connected.
220 
221  Returns: None.
222  ***********************************************************************|><|*/
223  bool IsConnected(void) { return((bool)m_fFlags.bfConnected); }
224 
244 
245 private: // Private Member Functions
246 
247 /*|><|***********************************************************************
248  Name: Bind
249 
250  Description: A polymorphic method that manages the creation and
251  of an inbound or outbound connectionless datagram channels.
252 
253  Returns: bool.
254  ***********************************************************************|><|*/
255  virtual bool Bind(SOCKADDR_IN *lpAddr=NULL);
256 
257 
258 /*|><|***********************************************************************
259  Name: GetNewPort
260 
261  Description: A private method that generates a new port ID from a pool
262  reserved for communication.
263  The pool starts at ID 5000 and continues to 7000. When 7000
264  is reached, it wraps back to 5000.
265 
266  Returns: unsigned short.
267  ***********************************************************************|><|*/
268  static unsigned short GetNewPort(void);
269 
270 
271 /*|><|***********************************************************************
272  Name: GetRemoteAddress
273 
274  Description: A private method that returns the SOCKADDR_IN structure
275  provided by the caller with information identifying the
276  connected FE.
277 
278  Returns: None.
279  ***********************************************************************|><|*/
280  void GetRemoteAddress( SOCKADDR_IN *lpAddr );
281 
282 /*|><|***********************************************************************
283  Name: SetRemoteAddress
284 
285  Description: A private method that loads the SOCKADDR_IN structure
286  maintained as a channel property with the information provided
287  by the caller.
288 
289  Returns: None.
290  ***********************************************************************|><|*/
291  void SetRemoteAddress( SOCKADDR_IN *lpAddr );
292 
293 
294 private:
295 
296  CRITICAL_SECTION m_csChannelSynchronized;
297  INetDispatch *m_piNetDispatch;
298  int m_hSocket; // Socket
299 
300  SOCKADDR_IN m_ipLocal; // Local address:port
301  SOCKADDR_IN m_ipRemote; // Remote address:port
302 
303  struct tagChannelFlags // State/Status flags
304  {
305  unsigned short bfOpen : 1; // TRUE if socket is open
306  unsigned short bfDirection : 2;
307  // 01=Rx, 10=Tx, 11=bidirectional
308  unsigned short bfReliable : 1;
309  // Is the channel a reliable channel
310  unsigned short bfBound : 1; // TRUE if socket is bound
311  unsigned short bfConnectPending : 1; // Connection pending
312  unsigned short bfConnected : 1; // TRUE if socket is connected
313  unsigned short bfConnectFailed : 1;
314  // TRUE if the connection request failed
315  unsigned short bfListening : 1; // Socket is listening
316  unsigned short bfEnableRecv : 1; // Enable receiving of data
317  } m_fFlags;
318 
319 #ifdef PINGTEL_OSSOCKET
320 // Extension to allow the Network Channel object to be used as a wrapper
321 // for a Pingtel OS Socket object
322  OsSocket & m_rOsSocket;
323 #endif
324 
325 };
326 
327 
328 
329 
330 #endif
Definition: NetworkChannel.h:52
unsigned short bfBound
Definition: NetworkChannel.h:310
unsigned short GetPort(void)
Definition: NetworkChannel.h:202
virtual bool Initialize(void)
unsigned short bfEnableRecv
Definition: NetworkChannel.h:316
unsigned short bfOpen
Definition: NetworkChannel.h:305
unsigned int GetSocket(void)
Definition: NetworkChannel.h:192
unsigned short bfConnected
Definition: NetworkChannel.h:312
virtual int getSocketPort(void)
unsigned short bfReliable
Definition: NetworkChannel.h:308
SOCKADDR_IN m_ipLocal
Definition: NetworkChannel.h:300
~CNetworkChannel(void)
Definition: NetworkChannel.h:303
CRITICAL_SECTION m_csChannelSynchronized
Definition: NetworkChannel.h:296
virtual int getSocketDescriptor(void)
virtual bool Close(void)
bool IsConnected(void)
Definition: NetworkChannel.h:223
INetDispatch * m_piNetDispatch
Definition: NetworkChannel.h:297
unsigned short bfDirection
Definition: NetworkChannel.h:306
virtual bool Bind(SOCKADDR_IN *lpAddr=NULL)
bool IsOpened(void)
Definition: NetworkChannel.h:213
struct CNetworkChannel::tagChannelFlags m_fFlags
virtual bool Connect(unsigned char *puchIPAddress=NULL, unsigned short usPortID=0)
void SetRemoteAddress(SOCKADDR_IN *lpAddr)
void GetRemoteAddress(SOCKADDR_IN *lpAddr)
#define DECLARE_IBASE_M
Definition: BaseClass.h:368
unsigned short bfConnectFailed
Definition: NetworkChannel.h:313
SOCKADDR_IN m_ipRemote
Definition: NetworkChannel.h:301
unsigned short bfListening
Definition: NetworkChannel.h:315
CNetworkChannel(INetDispatch *piNetDispatch=NULL, unsigned short usPort=0)
static unsigned short GetNewPort(void)
Definition: BaseClass.h:118
unsigned short bfConnectPending
Definition: NetworkChannel.h:311
virtual int Receive(unsigned char *puchBuffer, unsigned long ulRxBytes)
virtual void Dispatch(unsigned char *puchBuffer, unsigned long ulBytesRecvd)
interface unsigned long ulBytesToSend
Definition: INetworkRender.h:57
virtual bool Open(int iAddressType, int iSocketType, int iProtocol)
int m_hSocket
Definition: NetworkChannel.h:298
virtual int Send(unsigned char *puchDataBuffer, unsigned long ulBytesToSend)