Reformat source
This commit is contained in:
parent
15fe1a928a
commit
5df7c881b9
184
Socket.cpp
184
Socket.cpp
@ -2,15 +2,15 @@
|
||||
#include "Socket.h"
|
||||
|
||||
|
||||
#include <sys/types.h> // For data types
|
||||
#include <sys/socket.h> // For socket(), connect(), send(), and recv()
|
||||
#include <netdb.h> // For gethostbyname()
|
||||
#include <arpa/inet.h> // For inet_addr()
|
||||
#include <unistd.h> // For close()
|
||||
#include <netinet/in.h> // For sockaddr_in
|
||||
#include <netinet/tcp.h> // TCP_KEEPCNT
|
||||
#include <fcntl.h>
|
||||
typedef void raw_type; // Type used for raw data on this platform
|
||||
#include <sys/types.h> // For data types
|
||||
#include <sys/socket.h> // For socket(), connect(), send(), and recv()
|
||||
#include <netdb.h> // For gethostbyname()
|
||||
#include <arpa/inet.h> // For inet_addr()
|
||||
#include <unistd.h> // For close()
|
||||
#include <netinet/in.h> // For sockaddr_in
|
||||
#include <netinet/tcp.h> // TCP_KEEPCNT
|
||||
#include <fcntl.h>
|
||||
typedef void raw_type; // Type used for raw data on this platform
|
||||
|
||||
#include <errno.h> // For errno
|
||||
|
||||
@ -19,28 +19,34 @@ using namespace std;
|
||||
// SocketException Code
|
||||
|
||||
SocketException::SocketException(const string &message, bool inclSysMsg)
|
||||
: userMessage(message) {
|
||||
if (inclSysMsg) {
|
||||
: userMessage(message)
|
||||
{
|
||||
if (inclSysMsg)
|
||||
{
|
||||
userMessage.append(": ");
|
||||
userMessage.append(strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
SocketException::~SocketException() noexcept (true) {
|
||||
SocketException::~SocketException() noexcept (true)
|
||||
{
|
||||
}
|
||||
|
||||
const char *SocketException::what(){
|
||||
const char *SocketException::what()
|
||||
{
|
||||
return userMessage.c_str();
|
||||
}
|
||||
|
||||
// Function to fill in address structure given an address and port
|
||||
static void fillAddr(const string &address, unsigned short port,
|
||||
sockaddr_in &addr) {
|
||||
sockaddr_in &addr)
|
||||
{
|
||||
memset(&addr, 0, sizeof(addr)); // Zero out address structure
|
||||
addr.sin_family = AF_INET; // Internet address
|
||||
|
||||
hostent *host; // Resolve name
|
||||
if ((host = gethostbyname(address.c_str())) == NULL) {
|
||||
if ((host = gethostbyname(address.c_str())) == NULL)
|
||||
{
|
||||
// strerror() will not work for gethostbyname() and hstrerror()
|
||||
// is supposedly obsolete
|
||||
throw SocketException("Failed to resolve name (gethostbyname())");
|
||||
@ -52,15 +58,18 @@ static void fillAddr(const string &address, unsigned short port,
|
||||
|
||||
// Socket Code
|
||||
|
||||
Socket::Socket(int type, int protocol) {
|
||||
Socket::Socket(int type, int protocol)
|
||||
{
|
||||
|
||||
// Make a new socket
|
||||
if ((sockDesc = socket(PF_INET, type, protocol)) < 0) {
|
||||
if ((sockDesc = socket(PF_INET, type, protocol)) < 0)
|
||||
{
|
||||
throw SocketException("Socket creation failed (socket())", true);
|
||||
}
|
||||
}
|
||||
|
||||
Socket::Socket(int sockDesc) {
|
||||
Socket::Socket(int sockDesc)
|
||||
{
|
||||
this->sockDesc = sockDesc;
|
||||
}
|
||||
|
||||
@ -70,27 +79,32 @@ Socket::~Socket()
|
||||
sockDesc = -1;
|
||||
}
|
||||
|
||||
string Socket::getLocalAddress() {
|
||||
string Socket::getLocalAddress()
|
||||
{
|
||||
sockaddr_in addr;
|
||||
unsigned int addr_len = sizeof(addr);
|
||||
|
||||
if (getsockname(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
|
||||
if (getsockname(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0)
|
||||
{
|
||||
throw SocketException("Fetch of local address failed (getsockname())", true);
|
||||
}
|
||||
return inet_ntoa(addr.sin_addr);
|
||||
}
|
||||
|
||||
unsigned short Socket::getLocalPort() {
|
||||
unsigned short Socket::getLocalPort()
|
||||
{
|
||||
sockaddr_in addr;
|
||||
unsigned int addr_len = sizeof(addr);
|
||||
|
||||
if (getsockname(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
|
||||
if (getsockname(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0)
|
||||
{
|
||||
throw SocketException("Fetch of local port failed (getsockname())", true);
|
||||
}
|
||||
return ntohs(addr.sin_port);
|
||||
}
|
||||
|
||||
void Socket::setLocalPort(unsigned short localPort) {
|
||||
void Socket::setLocalPort(unsigned short localPort)
|
||||
{
|
||||
// Bind the socket to its port
|
||||
sockaddr_in localAddr;
|
||||
memset(&localAddr, 0, sizeof(localAddr));
|
||||
@ -98,18 +112,21 @@ void Socket::setLocalPort(unsigned short localPort) {
|
||||
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
localAddr.sin_port = htons(localPort);
|
||||
|
||||
if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) {
|
||||
if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0)
|
||||
{
|
||||
throw SocketException("Set of local port failed (bind())", true);
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::setLocalAddressAndPort(const string &localAddress,
|
||||
unsigned short localPort) {
|
||||
unsigned short localPort)
|
||||
{
|
||||
// Get the address of the requested host
|
||||
sockaddr_in localAddr;
|
||||
fillAddr(localAddress, localPort, localAddr);
|
||||
|
||||
if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) {
|
||||
if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0)
|
||||
{
|
||||
throw SocketException("Set of local address and port failed (bind())", true);
|
||||
}
|
||||
}
|
||||
@ -132,12 +149,15 @@ void Socket::setKeepalive()
|
||||
void Socket::setBlocking(bool flag)
|
||||
{
|
||||
int flags = fcntl(sockDesc, F_GETFL, 0);
|
||||
if( !flag ) flags = flags | O_NONBLOCK;
|
||||
else flags = flags & ~O_NONBLOCK;
|
||||
if( !flag )
|
||||
flags = flags | O_NONBLOCK;
|
||||
else
|
||||
flags = flags & ~O_NONBLOCK;
|
||||
fcntl(sockDesc, F_SETFL, flags);
|
||||
}
|
||||
|
||||
void Socket::cleanUp() {
|
||||
void Socket::cleanUp()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@ -147,7 +167,8 @@ int Socket::getFD()
|
||||
}
|
||||
|
||||
unsigned short Socket::resolveService(const string &service,
|
||||
const string &protocol) {
|
||||
const string &protocol)
|
||||
{
|
||||
struct servent *serv; /* Structure containing service information */
|
||||
|
||||
if ((serv = getservbyname(service.c_str(), protocol.c_str())) == NULL)
|
||||
@ -159,27 +180,32 @@ unsigned short Socket::resolveService(const string &service,
|
||||
// CommunicatingSocket Code
|
||||
|
||||
CommunicatingSocket::CommunicatingSocket(int type, int protocol)
|
||||
: Socket(type, protocol) {
|
||||
: Socket(type, protocol)
|
||||
{
|
||||
}
|
||||
|
||||
CommunicatingSocket::CommunicatingSocket(int newConnSD) : Socket(newConnSD) {
|
||||
CommunicatingSocket::CommunicatingSocket(int newConnSD) : Socket(newConnSD)
|
||||
{
|
||||
}
|
||||
|
||||
void CommunicatingSocket::connect(const string &foreignAddress,
|
||||
unsigned short foreignPort) {
|
||||
unsigned short foreignPort)
|
||||
{
|
||||
// Get the address of the requested host
|
||||
sockaddr_in destAddr;
|
||||
fillAddr(foreignAddress, foreignPort, destAddr);
|
||||
|
||||
// Try to connect to the given port
|
||||
if (::connect(sockDesc, (sockaddr *) &destAddr, sizeof(destAddr)) < 0) {
|
||||
if (::connect(sockDesc, (sockaddr *) &destAddr, sizeof(destAddr)) < 0)
|
||||
{
|
||||
throw SocketException("Connect failed (connect())", true);
|
||||
}
|
||||
}
|
||||
|
||||
void CommunicatingSocket::send(const void *buffer, int bufferLen)
|
||||
{
|
||||
if (::send(sockDesc, (raw_type *) buffer, bufferLen, 0) < 0)
|
||||
{
|
||||
if (::send(sockDesc, (raw_type *) buffer, bufferLen, 0) < 0) {
|
||||
throw SocketException("Send failed (send())", true);
|
||||
}
|
||||
}
|
||||
@ -189,29 +215,34 @@ int CommunicatingSocket::recv(void *buffer, int bufferLen)
|
||||
int rtn;
|
||||
if ((rtn = ::recv(sockDesc, (raw_type *) buffer, bufferLen, MSG_DONTWAIT)) < 0 )
|
||||
{
|
||||
if(errno == EWOULDBLOCK || errno == EAGAIN) return -1;
|
||||
else throw SocketException("Received failed (recv())", true);
|
||||
if(errno == EWOULDBLOCK || errno == EAGAIN)
|
||||
return -1;
|
||||
else
|
||||
throw SocketException("Received failed (recv())", true);
|
||||
}
|
||||
|
||||
return rtn;
|
||||
}
|
||||
|
||||
string CommunicatingSocket::getForeignAddress()
|
||||
{
|
||||
{
|
||||
sockaddr_in addr;
|
||||
unsigned int addr_len = sizeof(addr);
|
||||
|
||||
if (getpeername(sockDesc, (sockaddr *) &addr,(socklen_t *) &addr_len) < 0) {
|
||||
if (getpeername(sockDesc, (sockaddr *) &addr,(socklen_t *) &addr_len) < 0)
|
||||
{
|
||||
throw SocketException("Fetch of foreign address failed (getpeername())", true);
|
||||
}
|
||||
return inet_ntoa(addr.sin_addr);
|
||||
}
|
||||
|
||||
unsigned short CommunicatingSocket::getForeignPort() {
|
||||
unsigned short CommunicatingSocket::getForeignPort()
|
||||
{
|
||||
sockaddr_in addr;
|
||||
unsigned int addr_len = sizeof(addr);
|
||||
|
||||
if (getpeername(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
|
||||
if (getpeername(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0)
|
||||
{
|
||||
throw SocketException("Fetch of foreign port failed (getpeername())", true);
|
||||
}
|
||||
return ntohs(addr.sin_port);
|
||||
@ -221,16 +252,20 @@ unsigned short CommunicatingSocket::getForeignPort() {
|
||||
|
||||
TCPSocket::TCPSocket()
|
||||
: CommunicatingSocket(SOCK_STREAM,
|
||||
IPPROTO_TCP) {
|
||||
IPPROTO_TCP)
|
||||
{
|
||||
}
|
||||
|
||||
TCPSocket::TCPSocket(const string &foreignAddress, unsigned short foreignPort, bool keepalive)
|
||||
: CommunicatingSocket(SOCK_STREAM, IPPROTO_TCP) {
|
||||
: CommunicatingSocket(SOCK_STREAM, IPPROTO_TCP)
|
||||
{
|
||||
connect(foreignAddress, foreignPort);
|
||||
if(keepalive) setKeepalive();
|
||||
if(keepalive)
|
||||
setKeepalive();
|
||||
}
|
||||
|
||||
TCPSocket::TCPSocket(int newConnSD) : CommunicatingSocket(newConnSD) {
|
||||
TCPSocket::TCPSocket(int newConnSD) : CommunicatingSocket(newConnSD)
|
||||
{
|
||||
}
|
||||
|
||||
// TCPServerSocket Code
|
||||
@ -262,15 +297,18 @@ TCPSocket* TCPServerSocket::accept()
|
||||
if(newConnSD > 0)
|
||||
{
|
||||
newSocket = new TCPSocket(newConnSD);
|
||||
if(keepalive) newSocket->setKeepalive();
|
||||
if(keepalive)
|
||||
newSocket->setKeepalive();
|
||||
}
|
||||
return newSocket;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void TCPServerSocket::setListen(int queueLen) {
|
||||
if (listen(sockDesc, queueLen) < 0) {
|
||||
void TCPServerSocket::setListen(int queueLen)
|
||||
{
|
||||
if (listen(sockDesc, queueLen) < 0)
|
||||
{
|
||||
throw SocketException("Set listening socket failed (listen())", true);
|
||||
}
|
||||
}
|
||||
@ -278,23 +316,27 @@ void TCPServerSocket::setListen(int queueLen) {
|
||||
// UDPSocket Code
|
||||
|
||||
UDPSocket::UDPSocket() : CommunicatingSocket(SOCK_DGRAM,
|
||||
IPPROTO_UDP) {
|
||||
IPPROTO_UDP)
|
||||
{
|
||||
setBroadcast();
|
||||
}
|
||||
|
||||
UDPSocket::UDPSocket(unsigned short localPort) :
|
||||
CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) {
|
||||
CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP)
|
||||
{
|
||||
setLocalPort(localPort);
|
||||
setBroadcast();
|
||||
}
|
||||
|
||||
UDPSocket::UDPSocket(const string &localAddress, unsigned short localPort)
|
||||
: CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) {
|
||||
: CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP)
|
||||
{
|
||||
setLocalAddressAndPort(localAddress, localPort);
|
||||
setBroadcast();
|
||||
}
|
||||
|
||||
void UDPSocket::setBroadcast() {
|
||||
void UDPSocket::setBroadcast()
|
||||
{
|
||||
// If this fails, we'll hear about it when we try to send. This will allow
|
||||
// system that cannot broadcast to continue if they don't plan to broadcast
|
||||
int broadcastPermission = 1;
|
||||
@ -302,14 +344,17 @@ void UDPSocket::setBroadcast() {
|
||||
(raw_type *) &broadcastPermission, sizeof(broadcastPermission));
|
||||
}
|
||||
|
||||
void UDPSocket::disconnect() {
|
||||
void UDPSocket::disconnect()
|
||||
{
|
||||
sockaddr_in nullAddr;
|
||||
memset(&nullAddr, 0, sizeof(nullAddr));
|
||||
nullAddr.sin_family = AF_UNSPEC;
|
||||
|
||||
// Try to disconnect
|
||||
if (::connect(sockDesc, (sockaddr *) &nullAddr, sizeof(nullAddr)) < 0) {
|
||||
if (errno != EAFNOSUPPORT) {
|
||||
if (::connect(sockDesc, (sockaddr *) &nullAddr, sizeof(nullAddr)) < 0)
|
||||
{
|
||||
if (errno != EAFNOSUPPORT)
|
||||
{
|
||||
throw SocketException("Disconnect failed (connect())", true);
|
||||
}
|
||||
}
|
||||
@ -317,23 +362,26 @@ void UDPSocket::disconnect() {
|
||||
|
||||
void UDPSocket::sendTo(const void *buffer, int bufferLen,
|
||||
const string &foreignAddress, unsigned short foreignPort)
|
||||
{
|
||||
{
|
||||
sockaddr_in destAddr;
|
||||
fillAddr(foreignAddress, foreignPort, destAddr);
|
||||
|
||||
// Write out the whole buffer as a single message.
|
||||
if (sendto(sockDesc, (raw_type *) buffer, bufferLen, 0,
|
||||
(sockaddr *) &destAddr, sizeof(destAddr)) != bufferLen) {
|
||||
(sockaddr *) &destAddr, sizeof(destAddr)) != bufferLen)
|
||||
{
|
||||
throw SocketException("Send failed (sendto())", true);
|
||||
}
|
||||
}
|
||||
|
||||
int UDPSocket::recvFrom(void *buffer, int bufferLen, string &sourceAddress,
|
||||
unsigned short &sourcePort) {
|
||||
unsigned short &sourcePort)
|
||||
{
|
||||
sockaddr_in clntAddr;
|
||||
socklen_t addrLen = sizeof(clntAddr);
|
||||
int rtn;
|
||||
if ((rtn = recvfrom(sockDesc, (raw_type *) buffer, bufferLen, MSG_DONTWAIT, (sockaddr *) &clntAddr, (socklen_t *) &addrLen)) < 0)
|
||||
if ((rtn = recvfrom(sockDesc, (raw_type *) buffer, bufferLen, MSG_DONTWAIT, (sockaddr *) &clntAddr,
|
||||
(socklen_t *) &addrLen)) < 0)
|
||||
{
|
||||
throw SocketException("Receive failed (recvfrom())", true);
|
||||
}
|
||||
@ -343,33 +391,39 @@ int UDPSocket::recvFrom(void *buffer, int bufferLen, string &sourceAddress,
|
||||
return rtn;
|
||||
}
|
||||
|
||||
void UDPSocket::setMulticastTTL(unsigned char multicastTTL) {
|
||||
void UDPSocket::setMulticastTTL(unsigned char multicastTTL)
|
||||
{
|
||||
if (setsockopt(sockDesc, IPPROTO_IP, IP_MULTICAST_TTL,
|
||||
(raw_type *) &multicastTTL, sizeof(multicastTTL)) < 0) {
|
||||
(raw_type *) &multicastTTL, sizeof(multicastTTL)) < 0)
|
||||
{
|
||||
throw SocketException("Multicast TTL set failed (setsockopt())", true);
|
||||
}
|
||||
}
|
||||
|
||||
void UDPSocket::joinGroup(const string &multicastGroup) {
|
||||
void UDPSocket::joinGroup(const string &multicastGroup)
|
||||
{
|
||||
struct ip_mreq multicastRequest;
|
||||
|
||||
multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str());
|
||||
multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);
|
||||
if (setsockopt(sockDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP,
|
||||
(raw_type *) &multicastRequest,
|
||||
sizeof(multicastRequest)) < 0) {
|
||||
sizeof(multicastRequest)) < 0)
|
||||
{
|
||||
throw SocketException("Multicast group join failed (setsockopt())", true);
|
||||
}
|
||||
}
|
||||
|
||||
void UDPSocket::leaveGroup(const string &multicastGroup) {
|
||||
void UDPSocket::leaveGroup(const string &multicastGroup)
|
||||
{
|
||||
struct ip_mreq multicastRequest;
|
||||
|
||||
multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str());
|
||||
multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);
|
||||
if (setsockopt(sockDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP,
|
||||
(raw_type *) &multicastRequest,
|
||||
sizeof(multicastRequest)) < 0) {
|
||||
sizeof(multicastRequest)) < 0)
|
||||
{
|
||||
throw SocketException("Multicast group leave failed (setsockopt())", true);
|
||||
}
|
||||
}
|
||||
|
18
Socket.h
18
Socket.h
@ -9,7 +9,8 @@
|
||||
/**
|
||||
* Signals a problem with the execution of a socket call.
|
||||
*/
|
||||
class SocketException : public std::exception {
|
||||
class SocketException : public std::exception
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Construct a SocketException with a explanatory message.
|
||||
@ -37,7 +38,8 @@ private:
|
||||
/**
|
||||
* Base class representing basic communication endpoint
|
||||
*/
|
||||
class Socket {
|
||||
class Socket
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Close and deallocate this socket
|
||||
@ -120,7 +122,8 @@ protected:
|
||||
/**
|
||||
* Socket which is able to connect, send, and receive
|
||||
*/
|
||||
class CommunicatingSocket : public Socket {
|
||||
class CommunicatingSocket : public Socket
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Establish a socket connection with the given foreign
|
||||
@ -173,7 +176,8 @@ protected:
|
||||
/**
|
||||
* TCP socket for communication with other TCP sockets
|
||||
*/
|
||||
class TCPSocket : public CommunicatingSocket {
|
||||
class TCPSocket : public CommunicatingSocket
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Construct a TCP socket with no connection
|
||||
@ -202,7 +206,8 @@ private:
|
||||
/**
|
||||
* TCP socket class for servers
|
||||
*/
|
||||
class TCPServerSocket : public Socket {
|
||||
class TCPServerSocket : public Socket
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Construct a TCP socket for use with a server, accepting connections
|
||||
@ -243,7 +248,8 @@ private:
|
||||
/**
|
||||
* UDP socket class
|
||||
*/
|
||||
class UDPSocket : public CommunicatingSocket {
|
||||
class UDPSocket : public CommunicatingSocket
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Construct a UDP socket
|
||||
|
@ -55,7 +55,8 @@ bool ClientHandler::run(std::vector<ClientHandler>* clients, int serial, bool ve
|
||||
try
|
||||
{
|
||||
reclen = _socket->recv(buffer.data(), 4096);
|
||||
if(verbose) std::cout<<"Recived "<<reclen<<" bytes\n";
|
||||
if(verbose)
|
||||
std::cout<<"Recived "<<reclen<<" bytes\n";
|
||||
}
|
||||
catch (SocketException &e)
|
||||
{
|
||||
@ -64,29 +65,39 @@ bool ClientHandler::run(std::vector<ClientHandler>* clients, int serial, bool ve
|
||||
}
|
||||
if(reclen > 0)
|
||||
{
|
||||
if(!_isBroadcasting && reclen >= 5 && strncmp( buffer.data(), "bcst:", 5) == 0) _isBroadcasting = true;
|
||||
if(!_isBroadcasting && reclen >= 5 && strncmp( buffer.data(), "bcst:", 5) == 0)
|
||||
_isBroadcasting = true;
|
||||
|
||||
if(_isBroadcasting)
|
||||
{
|
||||
if(verbose) std::cout<<"Boradcasting "<<reclen<<" bytes\n";
|
||||
for(ClientHandler& item : *clients) if(operator!=(item))item.write(buffer.data(), reclen);
|
||||
if(verbose)
|
||||
std::cout<<"Boradcasting "<<reclen<<" bytes\n";
|
||||
for(ClientHandler& item : *clients)
|
||||
if(operator!=(item))
|
||||
item.write(buffer.data(), reclen);
|
||||
char* newline = std::find(buffer.begin(), buffer.end(), '\n');
|
||||
if(newline != std::end(buffer)) _isBroadcasting = false;
|
||||
if(newline != std::end(buffer))
|
||||
_isBroadcasting = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(verbose) std::cout<<"wrote "<<reclen<<" bytes to serial\n";
|
||||
if(verbose)
|
||||
std::cout<<"wrote "<<reclen<<" bytes to serial\n";
|
||||
if(sWrite(serial, buffer.data(), reclen) < 0 && (errno != EAGAIN || errno != EWOULDBLOCK))
|
||||
{
|
||||
throw serialIoException(serial, errno);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(reclen == 0) _disconnected = true;
|
||||
else if(reclen == 0)
|
||||
_disconnected = true;
|
||||
return !_disconnected;
|
||||
}
|
||||
|
||||
bool ClientHandler::isDisconnected(){return _disconnected;}
|
||||
bool ClientHandler::isDisconnected()
|
||||
{
|
||||
return _disconnected;
|
||||
}
|
||||
|
||||
void ClientHandler::write(const char* buffer, const size_t len)
|
||||
{
|
||||
|
24
main.cpp
24
main.cpp
@ -60,18 +60,24 @@ static int parseCmdArgs(int argc, char** argv, Config *config)
|
||||
}
|
||||
else if (std::string(argv[i]) == "--serialport" || std::string(argv[i]) == "-p")
|
||||
{
|
||||
if(argc > i) config->portFileName = argv[i+1];
|
||||
else return -1;
|
||||
if(argc > i)
|
||||
config->portFileName = argv[i+1];
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
else if (std::string(argv[i]) == "--port" || std::string(argv[i]) == "-P")
|
||||
{
|
||||
if(argc > i) config->port = atoi(argv[i+1]);
|
||||
else return -1;
|
||||
if(argc > i)
|
||||
config->port = atoi(argv[i+1]);
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
else if (std::string(argv[i]) == "--baud" || std::string(argv[i]) == "-b")
|
||||
{
|
||||
if(argc > i) config->baud = atoi(argv[i+1]);
|
||||
else return -1;
|
||||
if(argc > i)
|
||||
config->baud = atoi(argv[i+1]);
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
else if (std::string(argv[i]) == "--sinkless" || std::string(argv[i]) == "-s" )
|
||||
{
|
||||
@ -94,7 +100,8 @@ static int parseCmdArgs(int argc, char** argv, Config *config)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void acceptThreadFunction( TCPServerSocket* servSock, std::vector<ClientHandler>* clients, std::mutex* clientsMutex, int pollQue )
|
||||
void acceptThreadFunction( TCPServerSocket* servSock, std::vector<ClientHandler>* clients, std::mutex* clientsMutex,
|
||||
int pollQue )
|
||||
{
|
||||
while(!stop)
|
||||
{
|
||||
@ -127,7 +134,8 @@ int openSerialPort(const Config& config)
|
||||
std::cout<<"Opeing serial port failed\n";
|
||||
tcflush(serial, TCIOFLUSH);
|
||||
}
|
||||
else std::cout<<"Sinkless mode\n";
|
||||
else
|
||||
std::cout<<"Sinkless mode\n";
|
||||
return serial;
|
||||
}
|
||||
|
||||
|
@ -2,14 +2,18 @@
|
||||
|
||||
ssize_t sWrite(int port, char string[], size_t length)
|
||||
{
|
||||
if(port != -1) return write(port, string, length);
|
||||
else return 0;
|
||||
if(port != -1)
|
||||
return write(port, string, length);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t sWrite(int port, const char string[], size_t length)
|
||||
{
|
||||
if(port != -1) return write(port, string, length);
|
||||
else return 0;
|
||||
if(port != -1)
|
||||
return write(port, string, length);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t sRead(int port, void *buf, size_t count)
|
||||
@ -20,7 +24,7 @@ ssize_t sRead(int port, void *buf, size_t count)
|
||||
#ifdef __cplusplus
|
||||
void printRates()
|
||||
{
|
||||
std::cout<<"Rates:\n"\
|
||||
std::cout<<"Rates:\n"\
|
||||
<<"Unchanged 0\n" \
|
||||
<<"B50 "<<B50<<'\n'\
|
||||
<<"B75 "<<B75<<'\n'\
|
||||
|
@ -29,11 +29,12 @@ int serialport_init(const char* device, int baud = BAUDRATE, bool block = false)
|
||||
#ifdef __cplusplus
|
||||
class serialIoException: public std::runtime_error
|
||||
{
|
||||
public:
|
||||
public:
|
||||
int fd;
|
||||
int errorNumber;
|
||||
serialIoException(int fd_, int errorNumber_):
|
||||
std::runtime_error("file descriptor error, fd: " + std::to_string(fd_) + " error: " + strerror(errorNumber_) + "\n"), fd(fd_), errorNumber(errorNumber_)
|
||||
std::runtime_error("file descriptor error, fd: " + std::to_string(fd_) + " error: " + strerror(errorNumber_) + "\n"),
|
||||
fd(fd_), errorNumber(errorNumber_)
|
||||
{}
|
||||
};
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user