/mp_net_transport.h

https://github.com/DoubangoTelecom/webrtc2sip · C Header · 168 lines · 115 code · 20 blank · 33 comment · 3 complexity · 0e05e879eb9d4236e7bd9d13f2506a85 MD5 · raw file

  1. /* Copyright (C) 2012-2015 Doubango Telecom <http://www.doubango.org>
  2. *
  3. * This file is part of Open Source 'webrtc2sip' project
  4. * <http://code.google.com/p/webrtc2sip/>
  5. *
  6. * 'webrtc2sip' is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * 'webrtc2sip' is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with 'webrtc2sip'.
  18. */
  19. #if !defined(_MEDIAPROXY_NET_TRANSPORT_H_)
  20. #define _MEDIAPROXY_NET_TRANSPORT_H_
  21. #include "mp_config.h"
  22. #include "mp_object.h"
  23. #include "mp_common.h"
  24. #include "tnet_transport.h"
  25. #include "tsk_mutex.h"
  26. #include "tsk_buffer.h"
  27. #include <map>
  28. namespace webrtc2sip {
  29. class MPNetTransport;
  30. class MPNetTransportCallback;
  31. //
  32. // MPNetPeer
  33. //
  34. class MPNetPeer : public MPObject
  35. {
  36. friend class MPNetTransport;
  37. friend class MPNetTransportCallback;
  38. public:
  39. MPNetPeer(MPNetFd nFd, bool bConnected = false, const void* pcData = NULL, size_t nDataSize = 0)
  40. {
  41. m_bConnected = bConnected;
  42. m_nFd = nFd;
  43. m_pWrappedBuffer = tsk_buffer_create(pcData, nDataSize);
  44. }
  45. virtual ~MPNetPeer()
  46. {
  47. TSK_OBJECT_SAFE_FREE(m_pWrappedBuffer);
  48. }
  49. virtual MP_INLINE MPNetFd getFd(){ return m_nFd; }
  50. virtual MP_INLINE bool isConnected(){ return m_bConnected; }
  51. virtual MP_INLINE const void* getDataPtr() { return m_pWrappedBuffer ? m_pWrappedBuffer->data : NULL; }
  52. virtual MP_INLINE size_t getDataSize() { return m_pWrappedBuffer ? m_pWrappedBuffer->size : 0; }
  53. virtual bool sendData(const void* pcDataPtr, size_t nDataSize);
  54. virtual MP_INLINE bool isStream() = 0;
  55. protected:
  56. virtual MP_INLINE void setConnected(bool bConnected) { m_bConnected = bConnected; }
  57. protected:
  58. bool m_bConnected;
  59. MPNetFd m_nFd;
  60. tsk_buffer_t* m_pWrappedBuffer;
  61. };
  62. //
  63. // MPNetPeerDgram
  64. //
  65. class MPNetPeerDgram : public MPNetPeer
  66. {
  67. public:
  68. MPNetPeerDgram(MPNetFd nFd, const void* pcData = NULL, size_t nDataSize = 0)
  69. :MPNetPeer(nFd, false, pcData, nDataSize)
  70. {
  71. }
  72. virtual ~MPNetPeerDgram()
  73. {
  74. }
  75. virtual MP_INLINE const char* getObjectId() { return "MPNetPeerDgram"; }
  76. virtual MP_INLINE bool isStream(){ return false; }
  77. };
  78. //
  79. // MPNetPeerStream
  80. //
  81. class MPNetPeerStream : public MPNetPeer
  82. {
  83. public:
  84. MPNetPeerStream(MPNetFd nFd, bool bConnected = false, const void* pcData = NULL, size_t nDataSize = 0)
  85. :MPNetPeer(nFd, bConnected, pcData, nDataSize)
  86. {
  87. }
  88. virtual ~MPNetPeerStream()
  89. {
  90. }
  91. virtual MP_INLINE const char* getObjectId() { return "MPNetPeerStream"; }
  92. virtual MP_INLINE bool isStream(){ return true; }
  93. virtual MP_INLINE bool appenData(const void* pcData, size_t nDataSize){ return m_pWrappedBuffer ? tsk_buffer_append(m_pWrappedBuffer, pcData, nDataSize) == 0 : false; }
  94. virtual MP_INLINE bool remoteData(size_t nPosition, size_t nSize){ return m_pWrappedBuffer ? tsk_buffer_remove(m_pWrappedBuffer, nPosition, nSize) == 0 : false; }
  95. virtual MP_INLINE bool cleanupData(){ return m_pWrappedBuffer ? tsk_buffer_cleanup(m_pWrappedBuffer) == 0 : false; }
  96. };
  97. //
  98. // MPNetTransport
  99. //
  100. class MPNetTransportCallback : public MPObject
  101. {
  102. public:
  103. MPNetTransportCallback()
  104. {
  105. }
  106. virtual ~MPNetTransportCallback()
  107. {
  108. }
  109. virtual bool onData(MPObjectWrapper<MPNetPeer*> oPeer, size_t &nConsumedBytes) = 0;
  110. virtual bool onConnectionStateChanged(MPObjectWrapper<MPNetPeer*> oPeer) = 0;
  111. };
  112. //
  113. // MPNetTransport
  114. //
  115. class MPNetTransport : public MPObject
  116. {
  117. protected:
  118. MPNetTransport(MPNetTransporType_t eType, const char* pcLocalIP, unsigned short nLocalPort);
  119. public:
  120. virtual ~MPNetTransport();
  121. virtual MP_INLINE MPNetTransporType_t getType() { return m_eType; }
  122. virtual MP_INLINE bool isStarted(){ return m_bStarted; }
  123. virtual MP_INLINE bool isValid(){ return m_bValid; }
  124. virtual MP_INLINE void setCallback(MPObjectWrapper<MPNetTransportCallback*> oCallback) { m_oCallback = oCallback; }
  125. virtual bool setAllowedRemoteHost(const char* pcAllowedRemoteHost);
  126. virtual bool setSSLCertificates(const char* pcPrivateKey, const char* pcPublicKey, const char* pcCA, bool bVerify = false);
  127. virtual bool start();
  128. virtual MPNetFd connectTo(const char* pcHost, unsigned short nPort);
  129. virtual bool isConnected(MPNetFd nFd);
  130. virtual bool sendData(MPNetFd nFdFrom, const void* pcDataPtr, size_t nDataSize);
  131. virtual bool stop();
  132. protected:
  133. MPObjectWrapper<MPNetPeer*> getPeerByFd(MPNetFd nFd);
  134. void insertPeer(MPObjectWrapper<MPNetPeer*> oPeer);
  135. void removePeer(MPNetFd nFd);
  136. bool havePeer(MPNetFd nFd);
  137. static int MPNetTransportCb_Stream(const tnet_transport_event_t* e);
  138. protected:
  139. tnet_transport_handle_t* m_pWrappedTransport;
  140. MPNetTransporType_t m_eType;
  141. bool m_bValid, m_bStarted, m_bIPv6;
  142. std::map<MPNetFd, MPObjectWrapper<MPNetPeer*> > m_Peers;
  143. MPObjectWrapper<MPNetTransportCallback*> m_oCallback;
  144. tsk_mutex_handle_t *m_pWrappedPeersMutex;
  145. char* m_pAllowedRemoteHost;
  146. };
  147. }//namespace
  148. #endif /* _MEDIAPROXY_NET_TRANSPORT_H_ */