Create TCPSession2 for outpubound connections.

This commit is contained in:
Brad Arant committed 2022-10-17 20:03:23 -07:00
1 parent 5c87225c6f
commit fd70c79282
129 files changed
+1902 -710

No files matched your search

+18 -18
View File
@@ -9,19 +9,21 @@ namespace core {
void sigpipe_handler(int unused) {}
Socket::Socket(EPoll &ePoll, std::string text) : ePoll(ePoll), text(text) {
coreutils::Log(coreutils::LOG_DEBUG_2) << "Socket object created [" << text << "].";
buffer = (char *)malloc(4096);
length = 4096;
}
Socket::~Socket() {
shutDown = true;
onUnregister();
ePoll.unregisterSocket(this);
// coreutils::Log(coreutils::LOG_DEBUG_4) << "BMAXFree on socket " << descriptor;
free(buffer);
if(descriptor == -1)
return;
onUnregister();
ePoll.unregisterSocket(this);
coreutils::Log(coreutils::LOG_DEBUG_3) << "Socket destroyed for socket " << descriptor << ".";
close(descriptor);
coreutils::Log(coreutils::LOG_DEBUG_1) << text << " has ended (" << descriptor << ").";
sleep(1);
}
void Socket::setDescriptor(int descriptor) {
@@ -29,7 +31,8 @@ namespace core {
shutdown("Too many files open");
throw coreutils::Exception("Too many files open. Refusing connection.");
}
coreutils::Log(coreutils::LOG_DEBUG_3) << "Descriptor set to " << descriptor << " for Socket.";
coreutils::Log(coreutils::LOG_DEBUG_1) << text << " has started (" << descriptor << ").";
// coreutils::Log(coreutils::LOG_DEBUG_3) << "Descriptor set to " << descriptor << " for Socket.";
if(descriptor < 3)
throw coreutils::Exception("Descriptor out of range", __FILE__, __LINE__);
this->descriptor = descriptor;
@@ -45,7 +48,6 @@ namespace core {
void Socket::setBufferSize(int length) {
this->length = length;
buffer = (char *)realloc(buffer, length);
}
int Socket::getBufferSize() {
@@ -60,20 +62,21 @@ namespace core {
void Socket::onUnregistered() {}
bool Socket::eventReceived(struct epoll_event event) {
bool Socket::eventReceived(struct epoll_event event, long long eventId) {
// coreutils::Log(coreutils::LOG_DEBUG_3) << "eventReceived " << eventId << ": " << descriptor << ":" << event.events;
inHandler = true;
if(event.events & EPOLLRDHUP) {
readHangup = true;
shutdown("hangup received");
}
else if(event.events & EPOLLIN) {
if(event.events & EPOLLIN) {
coreutils::ZString zbuffer(buffer, length);
receiveData(zbuffer);
}
else if(event.events & EPOLLWRNORM) {
if(event.events & EPOLLWRNORM) {
writeSocket();
}
else if(event.events & EPOLLHUP) {
if(event.events & EPOLLHUP) {
shutdown();
}
inHandler = false;
@@ -89,15 +92,11 @@ namespace core {
}
void Socket::receiveData(coreutils::ZString &buffer) {
coreutils::ZString blank("");
if(buffer.getLength() <= 0)
throw coreutils::Exception("Request to receive data with a zero buffer length.", __FILE__, __LINE__, -1);
int len;
int error = -1;
if((len = ::read(getDescriptor(), buffer.getData(), buffer.getLength())) >= 0) {
coreutils::ZString zbuffer(buffer.getData(), len);
coreutils::Log(coreutils::LOG_DEBUG_1) << zbuffer;
@@ -129,7 +128,7 @@ namespace core {
if(fifo.size() > 0) {
outlock.lock();
if(!shutDown)
::write(descriptor, fifo.front().c_str(), fifo.front().length());
int rc = ::write(descriptor, fifo.front().c_str(), fifo.front().length());
fifo.pop();
outlock.unlock();
}
@@ -139,8 +138,9 @@ namespace core {
outlock.lock();
fifo.emplace(data);
outlock.unlock();
if(!inHandler)
ePoll.resetSocket(this);
// coreutils::Log(coreutils::LOG_DEBUG_4) << "inHandler " << descriptor << " " << inHandler << ":" << shutDown << ":[" << data << "]";
if(!inHandler)
ePoll.resetSocket(this);
return 1;
}
@@ -153,7 +153,7 @@ namespace core {
}
void Socket::shutdown(std::string text) {
coreutils::Log(coreutils::LOG_DEBUG_2) << "Shutdown requested on socket " << descriptor << " with reason " << text << ".";
coreutils::Log(coreutils::LOG_DEBUG_2) << "Shutdown requested from " << this->text << " (" << descriptor << ") with reason: " << text << ".";
shutDown = true;
reset = false;
}