1 /*************************************************************************/
2 /*  packet_peer_udp_winsock.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 PACKET_PEER_UDP_WINSOCK_H
31 #define PACKET_PEER_UDP_WINSOCK_H
32 
33 #include "io/packet_peer_udp.h"
34 #include "ring_buffer.h"
35 
36 class PacketPeerUDPWinsock : public PacketPeerUDP {
37 
38 	enum {
39 		PACKET_BUFFER_SIZE = 65536
40 	};
41 
42 	mutable RingBuffer<uint8_t> rb;
43 	uint8_t recv_buffer[PACKET_BUFFER_SIZE];
44 	mutable uint8_t packet_buffer[PACKET_BUFFER_SIZE];
45 	mutable IP_Address packet_ip;
46 	mutable int packet_port;
47 	mutable int queue_count;
48 	int sockfd;
49 	bool sock_blocking;
50 	IP::Type sock_type;
51 
52 	IP_Address peer_addr;
53 	int peer_port;
54 
55 	_FORCE_INLINE_ int _get_socket();
56 
57 	static PacketPeerUDP *_create();
58 
59 	void _set_sock_blocking(bool p_blocking);
60 
61 	Error _poll(bool p_wait);
62 
63 public:
64 	virtual int get_available_packet_count() const;
65 	virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) const;
66 	virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
67 
68 	virtual int get_max_packet_size() const;
69 
70 	virtual Error listen(int p_port, IP_Address p_bind_address = IP_Address("*"), int p_recv_buffer_size = 65536);
71 	virtual void close();
72 	virtual Error wait();
73 	virtual bool is_listening() const;
74 
75 	virtual IP_Address get_packet_address() const;
76 	virtual int get_packet_port() const;
77 
78 	virtual void set_send_address(const IP_Address &p_address, int p_port);
79 
80 	static void make_default();
81 	PacketPeerUDPWinsock();
82 	~PacketPeerUDPWinsock();
83 };
84 #endif // PACKET_PEER_UDP_WINSOCK_H
85