mirror of
https://github.com/AlexandreRouma/SDRPlusPlus.git
synced 2025-07-09 02:25:24 +02:00
SDR++ server beta :)
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
#include <utils/networking.h>
|
||||
#include <assert.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
namespace net {
|
||||
|
||||
@ -70,19 +71,34 @@ namespace net {
|
||||
if (_udp) {
|
||||
socklen_t fromLen = sizeof(remoteAddr);
|
||||
ret = recvfrom(_sock, (char*)buf, count, 0, (struct sockaddr*)&remoteAddr, &fromLen);
|
||||
}
|
||||
else {
|
||||
ret = recv(_sock, (char*)buf, count, 0);
|
||||
if (ret <= 0) {
|
||||
{
|
||||
std::lock_guard lck(connectionOpenMtx);
|
||||
connectionOpen = false;
|
||||
}
|
||||
connectionOpenCnd.notify_all();
|
||||
return -1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
if (ret <= 0) {
|
||||
{
|
||||
std::lock_guard lck(connectionOpenMtx);
|
||||
connectionOpen = false;
|
||||
int beenRead = 0;
|
||||
while (beenRead < count) {
|
||||
ret = recv(_sock, (char*)&buf[beenRead], count - beenRead, 0);
|
||||
|
||||
if (ret <= 0) {
|
||||
{
|
||||
std::lock_guard lck(connectionOpenMtx);
|
||||
connectionOpen = false;
|
||||
}
|
||||
connectionOpenCnd.notify_all();
|
||||
return -1;
|
||||
}
|
||||
connectionOpenCnd.notify_all();
|
||||
|
||||
beenRead += ret;
|
||||
}
|
||||
return ret;
|
||||
|
||||
return beenRead;
|
||||
}
|
||||
|
||||
bool ConnClass::write(int count, uint8_t* buf) {
|
||||
@ -93,19 +109,31 @@ namespace net {
|
||||
if (_udp) {
|
||||
int fromLen = sizeof(remoteAddr);
|
||||
ret = sendto(_sock, (char*)buf, count, 0, (struct sockaddr*)&remoteAddr, sizeof(remoteAddr));
|
||||
}
|
||||
else {
|
||||
ret = send(_sock, (char*)buf, count, 0);
|
||||
if (ret <= 0) {
|
||||
{
|
||||
std::lock_guard lck(connectionOpenMtx);
|
||||
connectionOpen = false;
|
||||
}
|
||||
connectionOpenCnd.notify_all();
|
||||
}
|
||||
return (ret > 0);
|
||||
}
|
||||
|
||||
if (ret <= 0) {
|
||||
{
|
||||
std::lock_guard lck(connectionOpenMtx);
|
||||
connectionOpen = false;
|
||||
int beenWritten = 0;
|
||||
while (beenWritten < count) {
|
||||
ret = send(_sock, (char*)buf, count, 0);
|
||||
if (ret <= 0) {
|
||||
{
|
||||
std::lock_guard lck(connectionOpenMtx);
|
||||
connectionOpen = false;
|
||||
}
|
||||
connectionOpenCnd.notify_all();
|
||||
return false;
|
||||
}
|
||||
connectionOpenCnd.notify_all();
|
||||
beenWritten += ret;
|
||||
}
|
||||
return (ret > 0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ConnClass::readAsync(int count, uint8_t* buf, void (*handler)(int count, uint8_t* buf, void* ctx), void* ctx) {
|
||||
|
2
core/src/utils/new_networking.cpp
Normal file
2
core/src/utils/new_networking.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
#include "new_networking.h"
|
||||
|
95
core/src/utils/new_networking.h
Normal file
95
core/src/utils/new_networking.h
Normal file
@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
#include <queue>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
|
||||
/*
|
||||
Ryzerth's Epic Networking Functins
|
||||
*/
|
||||
|
||||
namespace net {
|
||||
enum SocketType {
|
||||
SOCK_TYPE_TCP,
|
||||
SOCK_TYPE_UDP
|
||||
};
|
||||
|
||||
struct ReadHandler {
|
||||
int count;
|
||||
void* buf;
|
||||
void (*handle)(int count, void* buf, void* ctx);
|
||||
void* ctx;
|
||||
};
|
||||
|
||||
class SocketClass {
|
||||
public:
|
||||
SocketClass(struct addrinfo* localAddr, struct addrinfo* remoteAddr, SocketType sockType);
|
||||
~SocketClass();
|
||||
|
||||
int read(int count, void* buf, int timeout);
|
||||
int write(int count, void* buf);
|
||||
|
||||
void readAsync(int count, void* buf, void (*handle)(int count, void* buf, void* ctx), void* ctx);
|
||||
|
||||
bool isOpen();
|
||||
void close();
|
||||
|
||||
private:
|
||||
void readWorker();
|
||||
|
||||
bool open = false;
|
||||
|
||||
struct addrinfo* laddr;
|
||||
struct addrinfo* raddr;
|
||||
SocketType type;
|
||||
|
||||
std::queue<ReadHandler> readQueue;
|
||||
std::thread readThread;
|
||||
std::mutex readMtx;
|
||||
std::condition_variable readCnd;
|
||||
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<SocketClass> Socket;
|
||||
|
||||
namespace tcp {
|
||||
struct AcceptHandler {
|
||||
void (*handle)(Socket client, void* ctx);
|
||||
void* ctx;
|
||||
};
|
||||
|
||||
class ListenerClass {
|
||||
public:
|
||||
ListenerClass(struct addrinfo* addr);
|
||||
~ListenerClass();
|
||||
|
||||
Socket accept(int count, void* buf, int timeout);
|
||||
void acceptAsync(void (*handle)(int count, void* buf, void* ctx), void* ctx);
|
||||
|
||||
bool isOpen();
|
||||
void close();
|
||||
|
||||
private:
|
||||
void acceptWorker();
|
||||
|
||||
bool open = false;
|
||||
|
||||
struct addrinfo* addr;
|
||||
|
||||
std::queue<AcceptHandler> acceptQueue;
|
||||
std::thread acceptThread;
|
||||
std::mutex acceptMtx;
|
||||
std::condition_variable acceptCnd;
|
||||
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<ListenerClass> Listener;
|
||||
|
||||
Socket connect(std::string host, int port);
|
||||
Listener listen(std::string host, int port);
|
||||
}
|
||||
|
||||
namespace udp {
|
||||
Socket open(std::string remoteHost, int remotePort, std::string localHost = "", int localPort = -1);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user