Counter Strike : Global Offensive Source Code
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.

140 lines
4.4 KiB

  1. //===== Copyright � 1996-2009, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #ifndef X360_NETMGR_H
  7. #define X360_NETMGR_H
  8. #ifdef _X360
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. abstract_class IX360NetworkEvents
  13. {
  14. public:
  15. //
  16. // A packet has arrived and we have created a secure association for the IN_ADDR,
  17. // converted to XNADDR and paired it with our registered XNKID/XNKEY.
  18. //
  19. // Handler must either:
  20. //
  21. // - open peer connection in passive mode
  22. // we have already completed a secure key exchange (we are in CONNECTED
  23. // state), just use ConnectionPeerOpenPassive
  24. // and return true from OnX360NetConnectionlessPacket
  25. //
  26. // or:
  27. // - return false, in which case the secure association will be
  28. // unregistered and the remote side end of the secure connection
  29. // will get into unrecoverable LOST state
  30. //
  31. virtual bool OnX360NetConnectionlessPacket( netpacket_t *pkt, KeyValues *msg ) = 0;
  32. //
  33. // A packet has been received from a remote peer and successfully unpacked,
  34. // connection is still alive.
  35. //
  36. virtual void OnX360NetPacket( KeyValues *msg ) = 0;
  37. //
  38. // Connection with remote peer has been disconnected, connection object
  39. // and all connection records have already been removed, secure association
  40. // unregistered.
  41. //
  42. virtual void OnX360NetDisconnected( XUID xuidRemote ) = 0;
  43. };
  44. class CX360NetworkMgr : public IConnectionlessPacketHandler
  45. {
  46. public:
  47. explicit CX360NetworkMgr( IX360NetworkEvents *pListener, INetSupport::NetworkSocket_t eSocket );
  48. public:
  49. void SetListener( IX360NetworkEvents *pListener );
  50. public:
  51. enum UpdateResult_t { UPDATE_SUCCESS, UPDATE_LISTENER_CHANGED, UPDATE_DESTROYED };
  52. UpdateResult_t Update();
  53. void Destroy();
  54. void DebugPrint();
  55. bool IsUpdating() const;
  56. public:
  57. bool ConnectionPeerOpenPassive( XUID xuidRemote, netpacket_t *pktIncoming, XNKID *pxnkidSession = NULL );
  58. bool ConnectionPeerOpenActive( XUID xuidRemote, XSESSION_INFO const &xRemote );
  59. void ConnectionPeerUpdateXuid( XUID xuidRemoteOld, XUID xuidRemoteNew );
  60. void ConnectionPeerClose( XUID xuidRemote );
  61. void ConnectionPeerClose( netpacket_t *pktIncoming );
  62. void ConnectionPeerSendConnectionless( XUID xuidRemote, KeyValues *pMsg );
  63. void ConnectionPeerSendMessage( KeyValues *pMsg );
  64. char const * ConnectionPeerGetAddress( XUID xuidRemote );
  65. //
  66. // IConnectionlessPacketHandler
  67. //
  68. protected:
  69. virtual bool ProcessConnectionlessPacket( netpacket_t *packet );
  70. //
  71. // INetChannelHandler-delegates
  72. //
  73. public:
  74. void OnConnectionClosing( INetChannel *pNetChannel );
  75. void OnConnectionMessage( KeyValues *pMsg );
  76. protected:
  77. IX360NetworkEvents *m_pListener;
  78. INetSupport::NetworkSocket_t m_eSocket;
  79. protected:
  80. struct ConnectionMessageHandler_t : public INetChannelHandler
  81. {
  82. explicit ConnectionMessageHandler_t( CX360NetworkMgr *pMgr, INetChannel *pChannel ) : m_pMgr( pMgr ), m_pChannel( pChannel ) {}
  83. public:
  84. virtual void ConnectionStart(INetChannel *chan);
  85. virtual void ConnectionClosing(const char *reason) { m_pMgr->OnConnectionClosing( m_pChannel ); }
  86. virtual void ConnectionCrashed(const char *reason) { ConnectionClosing( reason ); }
  87. virtual void PacketStart(int incoming_sequence, int outgoing_acknowledged) {}
  88. virtual void PacketEnd( void ) {}
  89. virtual void FileRequested(const char *fileName, unsigned int transferID, bool isReplayDemoFile) {} // other side request a file for download
  90. virtual void FileReceived(const char *fileName, unsigned int transferID, bool isReplayDemoFile) {} // we received a file
  91. virtual void FileDenied(const char *fileName, unsigned int transferID, bool isReplayDemoFile) {} // a file request was denied by other side
  92. virtual void FileSent(const char *fileName, unsigned int transferID, bool isReplayDemoFile) {} // we sent a file
  93. virtual bool ChangeSplitscreenUser( int nSplitScreenUserSlot ) { return true; }
  94. public:
  95. CX360NetworkMgr *m_pMgr;
  96. INetChannel *m_pChannel;
  97. };
  98. struct ConnectionInfo_t
  99. {
  100. XUID m_xuid;
  101. IN_ADDR m_inaddr;
  102. XNADDR m_xnaddr;
  103. INetChannel *m_pNetChannel;
  104. ConnectionMessageHandler_t *m_pHandler;
  105. };
  106. CUtlMap< XUID, ConnectionInfo_t > m_arrConnections;
  107. enum State_t
  108. {
  109. STATE_IDLE, // network mgr is idle
  110. STATE_UPDATING, // network mgr is in the middle of an update frame function
  111. STATE_DESTROY_DEFERRED, // network mgr has been destroyed while in the middle of an update
  112. } m_eState;
  113. UpdateResult_t m_eUpdateResult;
  114. };
  115. #endif
  116. #endif