mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
Make many more utility classes/headers public. (#76)
Moving these headers from src to include enables other libraries to use the functionality provided. * tcpsockets * atomic_static * raw_istream * timestamp * SafeThread * Base64 * LEB128 * ConcurrentQueue The classes have been moved into the wpi namespace as they're generic.
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef TCPSOCKETS_NETWORKACCEPTOR_H_
|
||||
#define TCPSOCKETS_NETWORKACCEPTOR_H_
|
||||
|
||||
#include "NetworkStream.h"
|
||||
|
||||
class NetworkAcceptor {
|
||||
public:
|
||||
NetworkAcceptor() = default;
|
||||
virtual ~NetworkAcceptor() = default;
|
||||
|
||||
virtual int start() = 0;
|
||||
virtual void shutdown() = 0;
|
||||
virtual std::unique_ptr<NetworkStream> accept() = 0;
|
||||
|
||||
NetworkAcceptor(const NetworkAcceptor&) = delete;
|
||||
NetworkAcceptor& operator=(const NetworkAcceptor&) = delete;
|
||||
};
|
||||
|
||||
#endif // TCPSOCKETS_NETWORKACCEPTOR_H_
|
||||
@@ -1,39 +0,0 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef TCPSOCKETS_NETWORKSTREAM_H_
|
||||
#define TCPSOCKETS_NETWORKSTREAM_H_
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "llvm/StringRef.h"
|
||||
|
||||
class NetworkStream {
|
||||
public:
|
||||
NetworkStream() = default;
|
||||
virtual ~NetworkStream() = default;
|
||||
|
||||
enum Error {
|
||||
kConnectionClosed = 0,
|
||||
kConnectionReset = -1,
|
||||
kConnectionTimedOut = -2
|
||||
};
|
||||
|
||||
virtual std::size_t send(const char* buffer, std::size_t len, Error* err) = 0;
|
||||
virtual std::size_t receive(char* buffer, std::size_t len, Error* err,
|
||||
int timeout = 0) = 0;
|
||||
virtual void close() = 0;
|
||||
|
||||
virtual llvm::StringRef getPeerIP() const = 0;
|
||||
virtual int getPeerPort() const = 0;
|
||||
virtual void setNoDelay() = 0;
|
||||
|
||||
NetworkStream(const NetworkStream&) = delete;
|
||||
NetworkStream& operator=(const NetworkStream&) = delete;
|
||||
};
|
||||
|
||||
#endif // TCPSOCKETS_NETWORKSTREAM_H_
|
||||
@@ -5,7 +5,7 @@
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "SocketError.h"
|
||||
#include "tcpsockets/SocketError.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
@@ -13,7 +13,7 @@
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
namespace tcpsockets {
|
||||
namespace wpi {
|
||||
|
||||
std::string SocketStrerror(int code) {
|
||||
#ifdef _WIN32
|
||||
@@ -28,4 +28,4 @@ std::string SocketStrerror(int code) {
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace tcpsockets
|
||||
} // namespace wpi
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef TCPSOCKETS_SOCKETERROR_H_
|
||||
#define TCPSOCKETS_SOCKETERROR_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <WinSock2.h>
|
||||
#else
|
||||
#include <errno.h>
|
||||
#endif
|
||||
|
||||
namespace tcpsockets {
|
||||
|
||||
static inline int SocketErrno() {
|
||||
#ifdef _WIN32
|
||||
return WSAGetLastError();
|
||||
#else
|
||||
return errno;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string SocketStrerror(int code);
|
||||
|
||||
static inline std::string SocketStrerror() {
|
||||
return SocketStrerror(SocketErrno());
|
||||
}
|
||||
|
||||
} // namespace tcpsockets
|
||||
|
||||
#endif // TCPSOCKETS_SOCKETERROR_H_
|
||||
@@ -21,7 +21,7 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include "TCPAcceptor.h"
|
||||
#include "tcpsockets/TCPAcceptor.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
@@ -36,16 +36,17 @@
|
||||
#endif
|
||||
|
||||
#include "llvm/SmallString.h"
|
||||
#include "../Log.h"
|
||||
#include "SocketError.h"
|
||||
#include "support/Logger.h"
|
||||
#include "tcpsockets/SocketError.h"
|
||||
|
||||
using namespace tcpsockets;
|
||||
using namespace wpi;
|
||||
|
||||
TCPAcceptor::TCPAcceptor(int port, const char* address)
|
||||
TCPAcceptor::TCPAcceptor(int port, const char* address, Logger& logger)
|
||||
: m_lsd(0),
|
||||
m_port(port),
|
||||
m_address(address),
|
||||
m_listening(false) {
|
||||
m_listening(false),
|
||||
m_logger(logger) {
|
||||
m_shutdown = false;
|
||||
#ifdef _WIN32
|
||||
WSAData wsaData;
|
||||
@@ -73,7 +74,7 @@ int TCPAcceptor::start() {
|
||||
|
||||
m_lsd = socket(PF_INET, SOCK_STREAM, 0);
|
||||
if (m_lsd < 0) {
|
||||
ERROR("could not create socket");
|
||||
WPI_ERROR(m_logger, "could not create socket");
|
||||
return -1;
|
||||
}
|
||||
struct sockaddr_in address;
|
||||
@@ -99,13 +100,13 @@ int TCPAcceptor::start() {
|
||||
|
||||
int result = bind(m_lsd, (struct sockaddr*)&address, sizeof(address));
|
||||
if (result != 0) {
|
||||
ERROR("bind() failed: " << SocketStrerror());
|
||||
WPI_ERROR(m_logger, "bind() failed: " << SocketStrerror());
|
||||
return result;
|
||||
}
|
||||
|
||||
result = listen(m_lsd, 5);
|
||||
if (result != 0) {
|
||||
ERROR("listen() failed: " << SocketStrerror());
|
||||
WPI_ERROR(m_logger, "listen() failed: " << SocketStrerror());
|
||||
return result;
|
||||
}
|
||||
m_listening = true;
|
||||
@@ -172,7 +173,8 @@ std::unique_ptr<NetworkStream> TCPAcceptor::accept() {
|
||||
std::memset(&address, 0, sizeof(address));
|
||||
int sd = ::accept(m_lsd, (struct sockaddr*)&address, &len);
|
||||
if (sd < 0) {
|
||||
if (!m_shutdown) ERROR("accept() failed: " << SocketStrerror());
|
||||
if (!m_shutdown)
|
||||
WPI_ERROR(m_logger, "accept() failed: " << SocketStrerror());
|
||||
return nullptr;
|
||||
}
|
||||
if (m_shutdown) {
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
TCPAcceptor.h
|
||||
|
||||
TCPAcceptor class interface. TCPAcceptor provides methods to passively
|
||||
establish TCP/IP connections with clients.
|
||||
|
||||
------------------------------------------
|
||||
|
||||
Copyright © 2013 [Vic Hargrave - http://vichargrave.com]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef TCPSOCKETS_TCPACCEPTOR_H_
|
||||
#define TCPSOCKETS_TCPACCEPTOR_H_
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "NetworkAcceptor.h"
|
||||
#include "TCPStream.h"
|
||||
|
||||
class TCPAcceptor : public NetworkAcceptor {
|
||||
int m_lsd;
|
||||
int m_port;
|
||||
std::string m_address;
|
||||
bool m_listening;
|
||||
std::atomic_bool m_shutdown;
|
||||
|
||||
public:
|
||||
TCPAcceptor(int port, const char* address);
|
||||
~TCPAcceptor();
|
||||
|
||||
int start() override;
|
||||
void shutdown() override;
|
||||
std::unique_ptr<NetworkStream> accept() override;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -21,7 +21,7 @@
|
||||
limitations under the License
|
||||
*/
|
||||
|
||||
#include "TCPConnector.h"
|
||||
#include "tcpsockets/TCPConnector.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
@@ -38,13 +38,13 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "TCPStream.h"
|
||||
#include "tcpsockets/TCPStream.h"
|
||||
|
||||
#include "llvm/SmallString.h"
|
||||
#include "../Log.h"
|
||||
#include "SocketError.h"
|
||||
#include "support/Logger.h"
|
||||
#include "tcpsockets/SocketError.h"
|
||||
|
||||
using namespace tcpsockets;
|
||||
using namespace wpi;
|
||||
|
||||
static int ResolveHostName(const char* hostname, struct in_addr* addr) {
|
||||
struct addrinfo hints;
|
||||
@@ -68,7 +68,8 @@ static int ResolveHostName(const char* hostname, struct in_addr* addr) {
|
||||
}
|
||||
|
||||
std::unique_ptr<NetworkStream> TCPConnector::connect(const char* server,
|
||||
int port, int timeout) {
|
||||
int port, Logger& logger,
|
||||
int timeout) {
|
||||
#ifdef _WIN32
|
||||
struct WSAHelper {
|
||||
WSAHelper() {
|
||||
@@ -90,7 +91,7 @@ std::unique_ptr<NetworkStream> TCPConnector::connect(const char* server,
|
||||
addr_copy.push_back('\0');
|
||||
int size = sizeof(address);
|
||||
if (WSAStringToAddress(addr_copy.data(), PF_INET, nullptr, (struct sockaddr*)&address, &size) != 0) {
|
||||
ERROR("could not resolve " << server << " address");
|
||||
WPI_ERROR(logger, "could not resolve " << server << " address");
|
||||
return nullptr;
|
||||
}
|
||||
#else
|
||||
@@ -102,11 +103,11 @@ std::unique_ptr<NetworkStream> TCPConnector::connect(const char* server,
|
||||
if (timeout == 0) {
|
||||
int sd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sd < 0) {
|
||||
ERROR("could not create socket");
|
||||
WPI_ERROR(logger, "could not create socket");
|
||||
return nullptr;
|
||||
}
|
||||
if (::connect(sd, (struct sockaddr*)&address, sizeof(address)) != 0) {
|
||||
ERROR("connect() to " << server << " port " << port << " failed: " << SocketStrerror());
|
||||
WPI_ERROR(logger, "connect() to " << server << " port " << port << " failed: " << SocketStrerror());
|
||||
#ifdef _WIN32
|
||||
closesocket(sd);
|
||||
#else
|
||||
@@ -122,7 +123,7 @@ std::unique_ptr<NetworkStream> TCPConnector::connect(const char* server,
|
||||
socklen_t len;
|
||||
int result = -1, valopt, sd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sd < 0) {
|
||||
ERROR("could not create socket");
|
||||
WPI_ERROR(logger, "could not create socket");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -154,15 +155,15 @@ std::unique_ptr<NetworkStream> TCPConnector::connect(const char* server,
|
||||
len = sizeof(int);
|
||||
getsockopt(sd, SOL_SOCKET, SO_ERROR, (char*)(&valopt), &len);
|
||||
if (valopt) {
|
||||
ERROR("select() to " << server << " port " << port << " error " << valopt << " - " << SocketStrerror(valopt));
|
||||
WPI_ERROR(logger, "select() to " << server << " port " << port << " error " << valopt << " - " << SocketStrerror(valopt));
|
||||
}
|
||||
// connection established
|
||||
else
|
||||
result = 0;
|
||||
} else
|
||||
INFO("connect() to " << server << " port " << port << " timed out");
|
||||
WPI_INFO(logger, "connect() to " << server << " port " << port << " timed out");
|
||||
} else
|
||||
ERROR("connect() to " << server << " port " << port << " error " << SocketErrno() << " - " << SocketStrerror());
|
||||
WPI_ERROR(logger, "connect() to " << server << " port " << port << " error " << SocketErrno() << " - " << SocketStrerror());
|
||||
}
|
||||
|
||||
// Return socket to blocking mode
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
TCPConnector.h
|
||||
|
||||
TCPConnector class interface. TCPConnector provides methods to actively
|
||||
establish TCP/IP connections with a server.
|
||||
|
||||
------------------------------------------
|
||||
|
||||
Copyright © 2013 [Vic Hargrave - http://vichargrave.com]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License
|
||||
*/
|
||||
|
||||
#ifndef TCPSOCKETS_TCPCONNECTOR_H_
|
||||
#define TCPSOCKETS_TCPCONNECTOR_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "NetworkStream.h"
|
||||
|
||||
class TCPConnector {
|
||||
public:
|
||||
static std::unique_ptr<NetworkStream> connect(const char* server, int port,
|
||||
int timeout = 0);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -21,7 +21,7 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include "TCPStream.h"
|
||||
#include "tcpsockets/TCPStream.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <WinSock2.h>
|
||||
@@ -31,13 +31,15 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
TCPStream::TCPStream(int sd, struct sockaddr_in* address) : m_sd(sd) {
|
||||
using namespace wpi;
|
||||
|
||||
TCPStream::TCPStream(int sd, sockaddr_in* address) : m_sd(sd) {
|
||||
char ip[50];
|
||||
#ifdef _WIN32
|
||||
unsigned long size = sizeof(ip) - 1;
|
||||
WSAAddressToString((struct sockaddr*)address, sizeof sockaddr_in, nullptr, ip, &size);
|
||||
WSAAddressToString((sockaddr*)address, sizeof sockaddr_in, nullptr, ip, &size);
|
||||
#else
|
||||
inet_ntop(PF_INET, (struct in_addr*)&(address->sin_addr.s_addr), ip,
|
||||
inet_ntop(PF_INET, (in_addr*)&(address->sin_addr.s_addr), ip,
|
||||
sizeof(ip) - 1);
|
||||
#endif
|
||||
m_peerIP = ip;
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
TCPStream.h
|
||||
|
||||
TCPStream class interface. TCPStream provides methods to trasnfer
|
||||
data between peers over a TCP/IP connection.
|
||||
|
||||
------------------------------------------
|
||||
|
||||
Copyright © 2013 [Vic Hargrave - http://vichargrave.com]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef TCPSOCKETS_TCPSTREAM_H_
|
||||
#define TCPSOCKETS_TCPSTREAM_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
#include "NetworkStream.h"
|
||||
|
||||
class TCPStream : public NetworkStream {
|
||||
int m_sd;
|
||||
std::string m_peerIP;
|
||||
int m_peerPort;
|
||||
|
||||
public:
|
||||
friend class TCPAcceptor;
|
||||
friend class TCPConnector;
|
||||
|
||||
~TCPStream();
|
||||
|
||||
std::size_t send(const char* buffer, std::size_t len, Error* err) override;
|
||||
std::size_t receive(char* buffer, std::size_t len, Error* err,
|
||||
int timeout = 0) override;
|
||||
void close() override;
|
||||
|
||||
llvm::StringRef getPeerIP() const override;
|
||||
int getPeerPort() const override;
|
||||
void setNoDelay() override;
|
||||
|
||||
TCPStream(const TCPStream& stream) = delete;
|
||||
TCPStream& operator=(const TCPStream&) = delete;
|
||||
private:
|
||||
bool WaitForReadEvent(int timeout);
|
||||
|
||||
TCPStream(int sd, struct sockaddr_in* address);
|
||||
TCPStream() = delete;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user