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.

92 lines
2.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef SERVICE_CONN_MGR_H
  8. #define SERVICE_CONN_MGR_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "utllinkedlist.h"
  13. #include "utlvector.h"
  14. #include "tcpsocket.h"
  15. #include "ThreadedTCPSocketEmu.h"
  16. class CServiceConn
  17. {
  18. public:
  19. CServiceConn();
  20. ~CServiceConn();
  21. int m_ID;
  22. ITCPSocket *m_pSocket;
  23. DWORD m_LastRecvTime;
  24. };
  25. // ------------------------------------------------------------------------------------------ //
  26. // CServiceConnMgr. This class manages connections to all the UIs (there should only be one UI at
  27. // any given time, but it's conceivable that multiple people can be logged into NT servers
  28. // simultaneously).
  29. // ------------------------------------------------------------------------------------------ //
  30. class CServiceConnMgr
  31. {
  32. public:
  33. CServiceConnMgr();
  34. ~CServiceConnMgr();
  35. bool InitServer(); // Registers as a systemwide server.
  36. bool InitClient(); // Connects to the server.
  37. void Term();
  38. // Returns true if there are any connections. If you used InitClient() and there are
  39. // no connections, it will continuously try to connect with a server.
  40. bool IsConnected();
  41. // This should be called as often as possible. It checks for dead connections and it
  42. // handles incoming packets from UIs.
  43. void Update();
  44. // This sends out a message. If id is -1, then it sends to all connections.
  45. void SendPacket( int id, const void *pData, int len );
  46. // Overridables.
  47. public:
  48. virtual void OnNewConnection( int id );
  49. virtual void OnTerminateConnection( int id );
  50. virtual void HandlePacket( const char *pData, int len );
  51. private:
  52. void AttemptConnect();
  53. private:
  54. CUtlLinkedList<CServiceConn*, int> m_Connections;
  55. bool m_bShuttingDown;
  56. // This tells if we're running as a client or server.
  57. bool m_bServer;
  58. // If we're a client, this is the last time we tried to connect.
  59. DWORD m_LastConnectAttemptTime;
  60. // If we're the server, this is the socket we listen on.
  61. ITCPListenSocket *m_pListenSocket;
  62. };
  63. #endif // SERVICE_CONN_MGR_H