1//
2// UDPClient.h
3//
4// Library: Net
5// Package: UDP
6// Module: UDPClient
7//
8// Definition of the UDPClient class.
9//
10// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Net_UDPClient_INCLUDED
18#define Net_UDPClient_INCLUDED
19
20
21#include "Poco/Net/Net.h"
22#include "Poco/Net/SocketAddress.h"
23#include "Poco/Net/DatagramSocket.h"
24#include "Poco/Timespan.h"
25#include "Poco/Runnable.h"
26#include "Poco/Thread.h"
27#include <atomic>
28
29
30namespace Poco {
31namespace Net {
32
33
34class Net_API UDPClient : public Poco::Runnable
35 /// UDP client can either send, or send/receive UDP packets.
36 /// The mode of operation is specified at construction time.
37 /// If receiving functionality is enabled, it will run in a
38 /// separate thread.
39 /// This class is written as a "companion" to Poco::Net::UDPServer.
40 /// For other servers, inherit from this class and override the
41 /// handleResponse(char*, int) virtual member.
42{
43public:
44 UDPClient(const std::string& address, Poco::UInt16 port, bool listen = false);
45 /// Creates UDP client and connects it to specified address/port.
46 /// If listen is true, a thread is launched where client can receive
47 /// responses rom the server.
48
49 virtual ~UDPClient();
50 /// Destroys UDPClient.
51
52 void run();
53 /// Runs listener (typically invoked internally, in separate thread).
54
55 SocketAddress address() const;
56 /// Returns client address.
57
58 SocketAddress peerAddress() const;
59 /// Returns server address.
60
61 int send(void* data, int length);
62 /// Sends data.
63
64 int send(const SocketBufVec& vec);
65 /// Sends data.
66
67 virtual int handleResponse(char* buffer, int length);
68 /// Handles responses from UDP server. For non-POCO UDP servers,
69 /// this function should be overriden in inheriting class.
70
71 void setOption(int opt, int val);
72 /// Sets socket option.
73
74 int getOption(int opt);
75 /// Returns socket option.
76
77 void stop();
78 /// Stops the server reply receiving thread, if running.
79
80 int dataBacklog() const;
81 /// Returns current server data backlog.
82
83 int errorBacklog();
84 /// Returns current server error backlog.
85
86private:
87 DatagramSocket _socket;
88 SocketAddress _address;
89 Thread* _pThread;
90 bool _stop;
91 std::atomic<int> _dataBacklog;
92 std::atomic<int> _errorBacklog;
93};
94
95
96//
97// inlines
98//
99
100inline SocketAddress UDPClient::address() const
101{
102 return _socket.address();
103}
104
105
106inline SocketAddress UDPClient::peerAddress() const
107{
108 return _address;
109}
110
111
112inline int UDPClient::send(void* data, int length)
113{
114 return _socket.sendBytes(data, length);
115}
116
117
118inline int UDPClient::send(const SocketBufVec& vec)
119{
120 return _socket.sendBytes(vec);
121}
122
123
124inline void UDPClient::setOption(int opt, int val)
125{
126 _socket.setOption(SOL_SOCKET, opt, val);
127}
128
129
130inline int UDPClient::getOption(int opt)
131{
132 int val = 0;
133 _socket.getOption(SOL_SOCKET, opt, val);
134 return val;
135}
136
137
138inline void UDPClient::stop()
139{
140 _stop = true;
141}
142
143
144inline int UDPClient::dataBacklog() const
145{
146 return _dataBacklog;
147}
148
149
150inline int UDPClient::errorBacklog()
151{
152 return _errorBacklog;
153}
154
155
156} } // namespace Poco::Net
157
158
159#endif // Net_UDPClient_INCLUDED
160