2 * Copyright (c) 2017 Uber Technologies, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef JAEGERTRACING_TESTUTILS_TUDPTRANSPORT_H
18 #define JAEGERTRACING_TESTUTILS_TUDPTRANSPORT_H
20 #include "jaegertracing/Compilers.h"
25 #include <sys/socket.h>
28 #include <thrift/transport/TVirtualTransport.h>
30 #include "jaegertracing/utils/UDPTransporter.h"
32 namespace jaegertracing
{
36 : public apache::thrift::transport::TVirtualTransport
<TUDPTransport
> {
38 explicit TUDPTransport(const net::IPAddress
& addr
)
42 _socket
.open(AF_INET
, SOCK_DGRAM
);
43 _socket
.bind(_serverAddr
);
44 if (_serverAddr
.port() == 0) {
45 ::sockaddr_storage addrStorage
;
46 ::socklen_t addrLen
= sizeof(addrStorage
);
47 const auto returnCode
=
48 ::getsockname(_socket
.handle(),
49 reinterpret_cast<::sockaddr
*>(&addrStorage
),
51 if (returnCode
== 0) {
52 _serverAddr
= net::IPAddress(addrStorage
, addrLen
);
57 bool isOpen() override
{ return _socket
.handle() >= 0; }
59 void open() override
{}
61 void close() override
{ _socket
.close(); }
63 net::IPAddress
addr() const { return _serverAddr
; }
65 uint32_t read(uint8_t* buf
, uint32_t len
)
67 ::sockaddr_storage clientAddr
;
68 auto clientAddrLen
= static_cast<::socklen_t
>(sizeof(clientAddr
));
70 ::recvfrom(_socket
.handle(),
72 reinterpret_cast<char*>(buf
), // this cast is safe
78 reinterpret_cast<::sockaddr
*>(&clientAddr
),
80 _clientAddr
= net::IPAddress(clientAddr
, clientAddrLen
);
81 // the return value conrreponds to the size of the data read from the
82 // socket. upon error, recvfrom return -1. In this case, we return 0 to
83 // say that noothing was read.
84 return std::max
<long>(0, numRead
);
87 void write(const uint8_t* buf
, uint32_t len
)
89 ::sendto(_socket
.handle(),
91 reinterpret_cast<const char*>(buf
), // this cast is safe
97 reinterpret_cast<const ::sockaddr
*>(&_clientAddr
.addr()),
98 _clientAddr
.addrLen());
103 net::IPAddress _serverAddr
;
104 net::IPAddress _clientAddr
;
105 ::socklen_t _clientAddrLen
;
108 } // namespace testutils
109 } // namespace jaegertracing
111 #endif // JAEGERTRACING_TESTUTILS_TUDPTRANSPORT_H