Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "loopback_channel.h"
  8. #include "utllinkedlist.h"
  9. #include "iphelpers.h"
  10. // -------------------------------------------------------------------------------- //
  11. // CLoopbackChannel.
  12. // -------------------------------------------------------------------------------- //
  13. typedef struct
  14. {
  15. int m_Len;
  16. unsigned char m_Data[1];
  17. } LoopbackMsg_t;
  18. class CLoopbackChannel : public IChannel
  19. {
  20. public:
  21. virtual ~CLoopbackChannel()
  22. {
  23. FOR_EACH_LL( m_Messages, i )
  24. {
  25. free( m_Messages[i] );
  26. }
  27. m_Messages.Purge();
  28. }
  29. virtual void Release()
  30. {
  31. delete this;
  32. }
  33. virtual bool Send( const void *pData, int len )
  34. {
  35. const void *pChunks[1] = { pData };
  36. int chunkLengths[1] = { len };
  37. return SendChunks( pChunks, chunkLengths, 1 );
  38. }
  39. virtual bool SendChunks( void const * const *pChunks, const int *pChunkLengths, int nChunks )
  40. {
  41. CChunkWalker walker( pChunks, pChunkLengths, nChunks );
  42. LoopbackMsg_t *pMsg = (LoopbackMsg_t*)malloc( sizeof( LoopbackMsg_t ) - 1 + walker.GetTotalLength() );
  43. walker.CopyTo( pMsg->m_Data, walker.GetTotalLength() );
  44. pMsg->m_Len = walker.GetTotalLength();
  45. m_Messages.AddToTail( pMsg );
  46. return true;
  47. }
  48. virtual bool Recv( CUtlVector<unsigned char> &data, double flTimeout )
  49. {
  50. int iNext = m_Messages.Head();
  51. if ( iNext == m_Messages.InvalidIndex() )
  52. {
  53. return false;
  54. }
  55. else
  56. {
  57. LoopbackMsg_t *pMsg = m_Messages[iNext];
  58. data.CopyArray( pMsg->m_Data, pMsg->m_Len );
  59. free( pMsg );
  60. m_Messages.Remove( iNext );
  61. return true;
  62. }
  63. }
  64. virtual bool IsConnected()
  65. {
  66. return true;
  67. }
  68. virtual void GetDisconnectReason( CUtlVector<char> &reason )
  69. {
  70. }
  71. private:
  72. CUtlLinkedList<LoopbackMsg_t*,int> m_Messages; // FIFO for messages we've sent.
  73. };
  74. IChannel* CreateLoopbackChannel()
  75. {
  76. return new CLoopbackChannel;
  77. }