websocket_multiplayer.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*************************************************************************/
  2. /* websocket_multiplayer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef WEBSOCKET_MULTIPLAYER_PEER_H
  31. #define WEBSOCKET_MULTIPLAYER_PEER_H
  32. #include "core/error_list.h"
  33. #include "core/io/networked_multiplayer_peer.h"
  34. #include "core/list.h"
  35. #include "websocket_peer.h"
  36. class WebSocketMultiplayerPeer : public NetworkedMultiplayerPeer {
  37. GDCLASS(WebSocketMultiplayerPeer, NetworkedMultiplayerPeer);
  38. private:
  39. PoolVector<uint8_t> _make_pkt(uint32_t p_type, int32_t p_from, int32_t p_to, const uint8_t *p_data, uint32_t p_data_size);
  40. void _store_pkt(int32_t p_source, int32_t p_dest, const uint8_t *p_data, uint32_t p_data_size);
  41. Error _server_relay(int32_t p_from, int32_t p_to, const uint8_t *p_buffer, uint32_t p_buffer_size);
  42. protected:
  43. enum {
  44. SYS_NONE = 0,
  45. SYS_ADD = 1,
  46. SYS_DEL = 2,
  47. SYS_ID = 3,
  48. PROTO_SIZE = 9
  49. };
  50. struct Packet {
  51. int source;
  52. int destination;
  53. uint8_t *data;
  54. uint32_t size;
  55. };
  56. List<Packet> _incoming_packets;
  57. Map<int, Ref<WebSocketPeer> > _peer_map;
  58. Packet _current_packet;
  59. bool _is_multiplayer;
  60. int _target_peer;
  61. int _peer_id;
  62. int _refusing;
  63. static void _bind_methods();
  64. void _send_add(int32_t p_peer_id);
  65. void _send_sys(Ref<WebSocketPeer> p_peer, uint8_t p_type, int32_t p_peer_id);
  66. void _send_del(int32_t p_peer_id);
  67. int _gen_unique_id() const;
  68. public:
  69. /* NetworkedMultiplayerPeer */
  70. void set_transfer_mode(TransferMode p_mode);
  71. TransferMode get_transfer_mode() const;
  72. void set_target_peer(int p_peer_id);
  73. int get_packet_peer() const;
  74. int get_unique_id() const;
  75. virtual bool is_server() const = 0;
  76. void set_refuse_new_connections(bool p_enable);
  77. bool is_refusing_new_connections() const;
  78. virtual ConnectionStatus get_connection_status() const = 0;
  79. /* PacketPeer */
  80. virtual int get_available_packet_count() const;
  81. virtual int get_max_packet_size() const = 0;
  82. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size);
  83. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
  84. /* WebSocketPeer */
  85. virtual Ref<WebSocketPeer> get_peer(int p_peer_id) const = 0;
  86. void _process_multiplayer(Ref<WebSocketPeer> p_peer, uint32_t p_peer_id);
  87. void _clear();
  88. WebSocketMultiplayerPeer();
  89. ~WebSocketMultiplayerPeer();
  90. };
  91. #endif // WEBSOCKET_MULTIPLAYER_PEER_H