sources/network/udpsocket.h

branch
protocol5
changeset 195
be953e1621d9
parent 191
2e6cbacafdc7
equal deleted inserted replaced
176:060a13878ca0 195:be953e1621d9
1 /* 1 /*
2 Copyright 2014 - 2016 Teemu Piippo 2 Copyright 2014 - 2021 Teemu Piippo
3 All rights reserved. 3 All rights reserved.
4 4
5 Redistribution and use in source and binary forms, with or without 5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions 6 modification, are permitted provided that the following conditions
7 are met: 7 are met:
32 #include "../main.h" 32 #include "../main.h"
33 #include "ipaddress.h" 33 #include "ipaddress.h"
34 #include "bytestream.h" 34 #include "bytestream.h"
35 BEGIN_ZFC_NAMESPACE 35 BEGIN_ZFC_NAMESPACE
36 36
37 enum { MAX_DATAGRAM_LENGTH = 5120 }; 37 namespace net
38 {
39 constexpr int MAX_DATAGRAM_LENGTH = 5120;
40 struct Datagram;
41 class UDPSocket;
42 }
38 43
39 struct Datagram 44 struct net::Datagram
40 { 45 {
41 ByteArray message; 46 std::vector<unsigned char> message;
42 IPAddress address; 47 net::ip_address address;
43 }; 48 };
44 49
45 // ------------------------------------------------------------------------------------------------- 50 // -------------------------------------------------------------------------------------------------
46 // 51 //
47 class UDPSocket 52 class net::UDPSocket
48 { 53 {
49 public: 54 public:
50 UDPSocket(); 55 UDPSocket();
51 virtual ~UDPSocket(); 56 virtual ~UDPSocket();
52 57 [[nodiscard]] bool bind(port_t port, std::ostream &errors);
53 bool bind (unsigned short port); 58 [[nodiscard]] bool read(Datagram& datagram, std::ostream& errors);
54 bool read (Datagram& datagram); 59 [[nodiscard]] bool send(const ip_address& address, const std::vector<unsigned char>& data, std::ostream &errors);
55 bool send (const IPAddress& address, const ByteArray& data); 60 [[nodiscard]] bool set_blocking(bool a, std::ostream &errors);
56 bool set_blocking (bool a); 61 const int file_descriptor;
57 const String& error_string() const { return m_error; }
58 int file_descriptor() const { return m_socket; }
59
60 private:
61 static char HuffmanBuffer[131072];
62
63 IPAddress m_addr;
64 String m_error;
65 int m_socket;
66 }; 62 };
67 63
68 END_ZFC_NAMESPACE 64 END_ZFC_NAMESPACE

mercurial