1 | /* |
2 | * IXUdpSocket.h |
3 | * Author: Benjamin Sergeant |
4 | * Copyright (c) 2020 Machine Zone, Inc. All rights reserved. |
5 | */ |
6 | |
7 | #pragma once |
8 | |
9 | #include <atomic> |
10 | #include <memory> |
11 | #include <string> |
12 | |
13 | #ifdef _WIN32 |
14 | #include <basetsd.h> |
15 | #ifdef _MSC_VER |
16 | typedef SSIZE_T ssize_t; |
17 | #endif |
18 | #endif |
19 | |
20 | #include "IXNetSystem.h" |
21 | |
22 | namespace ix |
23 | { |
24 | class UdpSocket |
25 | { |
26 | public: |
27 | UdpSocket(int fd = -1); |
28 | ~UdpSocket(); |
29 | |
30 | // Virtual methods |
31 | bool init(const std::string& host, int port, std::string& errMsg); |
32 | ssize_t sendto(const std::string& buffer); |
33 | ssize_t recvfrom(char* buffer, size_t length); |
34 | |
35 | void close(); |
36 | |
37 | static int getErrno(); |
38 | static bool isWaitNeeded(); |
39 | static void closeSocket(int fd); |
40 | |
41 | private: |
42 | std::atomic<int> _sockfd; |
43 | struct sockaddr_in _server; |
44 | }; |
45 | } // namespace ix |
46 | |