connection.hh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Plugin connection declarations
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef CC1_PLUGIN_CONNECTION_HH
  16. #define CC1_PLUGIN_CONNECTION_HH
  17. #include "status.hh"
  18. #include "callbacks.hh"
  19. namespace cc1_plugin
  20. {
  21. // The connection class represents one side of the connection
  22. // between the gdb-side library and the gcc plugin. It handles the
  23. // low-level details of reading and writing data.
  24. class connection
  25. {
  26. public:
  27. connection (int fd)
  28. : m_fd (fd),
  29. m_aux_fd (-1),
  30. m_callbacks ()
  31. {
  32. }
  33. connection (int fd, int aux_fd)
  34. : m_fd (fd),
  35. m_aux_fd (aux_fd),
  36. m_callbacks ()
  37. {
  38. }
  39. virtual ~connection () = default;
  40. connection (const connection &) = delete;
  41. connection &operator= (const connection &) = delete;
  42. // Send a single character. This is used to introduce various
  43. // higher-level protocol elements.
  44. status send (char c);
  45. // Send data in bulk.
  46. status send (const void *buf, int len);
  47. // Read a single byte from the connection and verify that it
  48. // matches the argument C.
  49. status require (char c);
  50. // Read data in bulk.
  51. status get (void *buf, int len);
  52. // This is called after a query (remote function call) has been
  53. // sent to the remote. It waits for a response packet. The
  54. // response character is read before returning. Any query packets
  55. // sent from the remote while waiting for a response are handled
  56. // by this function.
  57. status wait_for_result ()
  58. {
  59. return do_wait (true);
  60. }
  61. // Read and respond to query packets sent by the remote. This
  62. // function returns when the connection is closed.
  63. status wait_for_query ()
  64. {
  65. return do_wait (false);
  66. }
  67. // Register a callback with this connection. NAME is the name of
  68. // the method being registered. FUNC is the function. It must
  69. // know how to decode its own arguments. When a query packet is
  70. // received by one of the wait_* methods, the corresponding
  71. // callback is invoked.
  72. void add_callback (const char *name, callback_ftype *func)
  73. {
  74. m_callbacks.add_callback (name, func);
  75. }
  76. virtual void print (const char *)
  77. {
  78. }
  79. private:
  80. // Helper function for the wait_* methods.
  81. status do_wait (bool);
  82. // The file descriptor.
  83. int m_fd;
  84. // An auxiliary file descriptor, or -1 if none.
  85. int m_aux_fd;
  86. // Callbacks associated with this connection.
  87. callbacks m_callbacks;
  88. };
  89. }
  90. #endif // CC1_PLUGIN_CONNECTION_HH