forked from tolga9009/elgato-gchd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tolga Cakir <tolga@cevel.net>
- Loading branch information
Showing
5 changed files
with
139 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* Copyright (c) 2014 - 2016 Tolga Cakir <tolga@cevel.net> | ||
* | ||
* This source file is part of Sidewinder daemon and is distributed under the | ||
* MIT License. For more information, see LICENSE file. | ||
*/ | ||
|
||
#include <csignal> | ||
#include <iostream> | ||
|
||
#include <fcntl.h> | ||
#include <netdb.h> | ||
#include <unistd.h> | ||
|
||
#include <sys/socket.h> | ||
#include <sys/stat.h> | ||
|
||
#include <socket.hpp> | ||
|
||
int Socket::enable(std::string ip, std::string port) { | ||
struct addrinfo hints = {}; | ||
struct addrinfo *result, *entry; | ||
|
||
hints.ai_family = AF_UNSPEC; | ||
hints.ai_socktype = SOCK_DGRAM; | ||
hints.ai_flags = AI_PASSIVE; | ||
|
||
// if <ip> is unconfigured, set it to nullptr, effectively IP 0.0.0.0 | ||
const char *ipAddress; | ||
|
||
if (ip.empty()) { | ||
ipAddress = nullptr; | ||
} else { | ||
ipAddress = ip.c_str(); | ||
} | ||
|
||
auto ret = getaddrinfo(ipAddress, port.c_str(), &hints, &result); | ||
|
||
if (ret) { | ||
std::cerr << "Socket error: " << gai_strerror(ret) << std::endl; | ||
|
||
return 1; | ||
} | ||
|
||
for (entry = result; entry != nullptr; entry = entry->ai_next) { | ||
fd_ = socket(entry->ai_family, entry->ai_socktype, entry->ai_protocol); | ||
|
||
if (fd_ < 0) { | ||
// error, try next iteration | ||
continue; | ||
} | ||
|
||
if (connect(fd_, entry->ai_addr, entry->ai_addrlen) != -1) { | ||
// success | ||
break; | ||
} | ||
|
||
close(fd_); | ||
fd_ = -1; | ||
|
||
return 1; | ||
} | ||
|
||
freeaddrinfo(result); | ||
|
||
auto flags = fcntl(fd_, F_GETFL); | ||
|
||
if (flags == -1) { | ||
std::cerr << "Socket error: fcntl get flags." << std::endl; | ||
|
||
return 1; | ||
} | ||
|
||
flags |= O_NONBLOCK; | ||
|
||
if (fcntl(fd_, F_SETFL, flags) == -1) { | ||
std::cerr << "Socket error: fcntl set flags." << std::endl; | ||
|
||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
void Socket::disable() { | ||
if (fd_ != -1) { | ||
close(fd_); | ||
fd_ = -1; | ||
} | ||
} | ||
|
||
void Socket::output(std::array<unsigned char, DATA_BUF> *buffer) { | ||
if (fd_ == -1) { | ||
return; | ||
} | ||
|
||
write(fd_, buffer->data(), buffer->size()); | ||
} | ||
|
||
Socket::Socket() { | ||
fd_ = -1; | ||
} | ||
|
||
Socket::~Socket() { | ||
disable(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright (c) 2014 - 2016 Tolga Cakir <tolga@cevel.net> | ||
* | ||
* This source file is part of Sidewinder daemon and is distributed under the | ||
* MIT License. For more information, see LICENSE file. | ||
*/ | ||
|
||
#ifndef SOCKET_CLASS_H | ||
#define SOCKET_CLASS_H | ||
|
||
#include <array> | ||
#include <string> | ||
|
||
#include <gchd.hpp> | ||
|
||
class Socket { | ||
public: | ||
int enable(std::string ip, std::string port); | ||
void disable(); | ||
void output(std::array<unsigned char, DATA_BUF> *buffer); | ||
Socket(); | ||
~Socket(); | ||
|
||
private: | ||
int fd_; | ||
std::string output_; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters