debug_io_net.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /////////////////////////////////////////////////////////////////////////EA-V1
  19. // $File: //depot/GeneralsMD/Staging/code/Libraries/Source/debug/debug_io_net.cpp $
  20. // $Author: mhoffe $
  21. // $Revision: #1 $
  22. // $DateTime: 2003/07/03 11:55:26 $
  23. //
  24. // ©2003 Electronic Arts
  25. //
  26. // Debug I/O class net (Network destination via named pipe)
  27. //////////////////////////////////////////////////////////////////////////////
  28. #include "_pch.h"
  29. #include <new> // needed for placement new prototype
  30. DebugIONet::DebugIONet(void)
  31. {
  32. }
  33. DebugIONet::~DebugIONet()
  34. {
  35. if (m_pipe!=INVALID_HANDLE_VALUE)
  36. CloseHandle(m_pipe);
  37. }
  38. int DebugIONet::Read(char *buf, int maxchar)
  39. {
  40. if (m_pipe==INVALID_HANDLE_VALUE)
  41. return 0;
  42. DWORD mode=PIPE_READMODE_MESSAGE|PIPE_NOWAIT;
  43. SetNamedPipeHandleState(m_pipe,&mode,NULL,NULL);
  44. DWORD read;
  45. if (!ReadFile(m_pipe,buf,maxchar-1,&read,NULL))
  46. read=0;
  47. mode=PIPE_READMODE_MESSAGE|PIPE_WAIT;
  48. SetNamedPipeHandleState(m_pipe,&mode,NULL,NULL);
  49. return read;
  50. }
  51. void DebugIONet::Write(StringType type, const char *src, const char *str)
  52. {
  53. if (m_pipe==INVALID_HANDLE_VALUE)
  54. return;
  55. DWORD dummy;
  56. WriteFile(m_pipe,&type,1,&dummy,NULL);
  57. unsigned len;
  58. len=src?strlen(src):0;
  59. WriteFile(m_pipe,&len,4,&dummy,NULL);
  60. if (len)
  61. WriteFile(m_pipe,src,len,&dummy,NULL);
  62. len=strlen(str);
  63. WriteFile(m_pipe,&len,4,&dummy,NULL);
  64. if (len)
  65. WriteFile(m_pipe,str,len,&dummy,NULL);
  66. }
  67. void DebugIONet::EmergencyFlush(void)
  68. {
  69. }
  70. void DebugIONet::Execute(class Debug& dbg, const char *cmd, bool structuredCmd,
  71. unsigned argn, const char * const * argv)
  72. {
  73. if (!cmd||!strcmp(cmd,"help"))
  74. {
  75. dbg << "net I/O help:\n"
  76. " add [ <machine> ]\n"
  77. " create net I/O (optionally specifying the machine to connect to)\n";
  78. }
  79. else if (!strcmp(cmd,"add"))
  80. {
  81. const char *machine=argn?argv[0]:".";
  82. char buf[256];
  83. wsprintf(buf,"\\\\%s\\pipe\\ea_debug_v1",machine);
  84. m_pipe=CreateFile(buf,GENERIC_READ|GENERIC_WRITE,
  85. 0,NULL,OPEN_EXISTING,0,NULL);
  86. if (m_pipe==INVALID_HANDLE_VALUE)
  87. {
  88. dbg << "Could not connect to given machine.\n";
  89. return;
  90. }
  91. // we're reading messages
  92. DWORD mode=PIPE_READMODE_MESSAGE;
  93. SetNamedPipeHandleState(m_pipe,&mode,NULL,NULL);
  94. // write welcome message
  95. char comp[128];
  96. mode=sizeof(comp);
  97. GetComputerName(comp,&mode);
  98. wsprintf(buf,"Client at %s\n",comp);
  99. Write(Other,NULL,buf);
  100. }
  101. }
  102. DebugIOInterface *DebugIONet::Create(void)
  103. {
  104. return new (DebugAllocMemory(sizeof(DebugIONet))) DebugIONet();
  105. }
  106. void DebugIONet::Delete(void)
  107. {
  108. this->~DebugIONet();
  109. DebugFreeMemory(this);
  110. }