1
0

PeerLink.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef PeerLink_H
  16. #define PeerLink_H
  17. #include "wire/Message.h"
  18. #include "memory/Allocator.h"
  19. #include "util/events/EventBase.h"
  20. #include "util/Linker.h"
  21. Linker_require("net/PeerLink.c")
  22. #include <stdbool.h>
  23. /**
  24. * The PeerLink adds a PeerHeader to outgoing messages and removes it from incoming messages while
  25. * checking clock skew to detect latency and react by sending congestion notifications to the peer.
  26. * In response to congestion notifications, this module will detect an optimal flow rate and buffer
  27. * packets to avoid sending faster than this rate. In the event that the buffer is over-filled,
  28. * packets will be dropped or replaced depending on highest penalty.
  29. */
  30. struct PeerLink
  31. {
  32. int queueLength;
  33. int linkMTU;
  34. bool peerHeaderEnabled;
  35. };
  36. struct PeerLink_Kbps
  37. {
  38. uint32_t sendKbps;
  39. uint32_t recvKbps;
  40. };
  41. /**
  42. * Attempt to get a message from the peerlink to send, if it is time to send one.
  43. * If there are no messages in the queue or the link is already at capacity, NULL will be returned.
  44. */
  45. struct Message* PeerLink_poll(struct PeerLink* pl);
  46. /**
  47. * Enqueue a message to be sent.
  48. * @return the number of messages which are ready to be encrypted and written to the device.
  49. * Call PeerLink_poll() to get these messages for actual sending.
  50. */
  51. int PeerLink_send(struct Message* msg, struct PeerLink* pl);
  52. /**
  53. * Receive (check the PeerHeader on) a message which has come in from the wire.
  54. * PeerHeader_SIZE bytes will be popped from the message if peerHeaders are enabled for this
  55. * peerLink.
  56. */
  57. void PeerLink_recv(struct Message* msg, struct PeerLink* pl);
  58. void PeerLink_kbps(struct PeerLink* peerLink, struct PeerLink_Kbps* output);
  59. struct PeerLink* PeerLink_new(struct EventBase* base, struct Allocator* alloc);
  60. #endif