It works!!!
This commit is contained in:
1 parent
20d5c99517
commit
b7398afc05
16 files changed
+160
-197
No files matched your search
+48
-51
@@ -14,17 +14,17 @@ namespace core {
|
||||
Socket::~Socket() {
|
||||
free(buffer);
|
||||
if(descriptor == -1)
|
||||
return;
|
||||
return;
|
||||
onUnregister();
|
||||
ePoll.unregisterSocket(this);
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "Socket destroyed for socket " << descriptor << ".";
|
||||
close(descriptor);
|
||||
}
|
||||
|
||||
|
||||
void Socket::setDescriptor(int descriptor) {
|
||||
if((descriptor == -1) && (errno == 24)) {
|
||||
shutdown("Too many files open");
|
||||
coreutils::Exception("Too many files open. Refusing connection.");;
|
||||
shutdown("Too many files open");
|
||||
throw coreutils::Exception("Too many files open. Refusing connection.");
|
||||
}
|
||||
lock.lock();
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "Descriptor set to " << descriptor << " for Socket.";
|
||||
@@ -44,6 +44,7 @@ namespace core {
|
||||
void Socket::setBufferSize(int length) {
|
||||
this->length = length;
|
||||
buffer = (char *)realloc(buffer, length);
|
||||
|
||||
}
|
||||
|
||||
int Socket::getBufferSize() {
|
||||
@@ -53,15 +54,15 @@ namespace core {
|
||||
void Socket::onRegister() {}
|
||||
|
||||
void Socket::onRegistered() {}
|
||||
|
||||
|
||||
void Socket::onUnregister() {}
|
||||
|
||||
void Socket::onUnregistered() {}
|
||||
|
||||
|
||||
bool Socket::eventReceived(struct epoll_event event) {
|
||||
|
||||
|
||||
lock.lock();
|
||||
|
||||
|
||||
if(event.events & EPOLLRDHUP) {
|
||||
readHangup = true;
|
||||
shutdown("hangup received");
|
||||
@@ -70,9 +71,10 @@ namespace core {
|
||||
}
|
||||
|
||||
if(event.events & EPOLLIN) {
|
||||
receiveData(buffer);
|
||||
coreutils::ZString zbuffer(buffer, length);
|
||||
receiveData(zbuffer);
|
||||
}
|
||||
|
||||
|
||||
if(event.events & EPOLLWRNORM) {
|
||||
writeSocket();
|
||||
}
|
||||
@@ -85,90 +87,85 @@ namespace core {
|
||||
|
||||
lock.unlock();
|
||||
|
||||
reset = true;
|
||||
reset = true;
|
||||
return reset;
|
||||
}
|
||||
|
||||
|
||||
void Socket::onDataReceived(std::string data) {
|
||||
throw coreutils::Exception("Need to override onDataReceived.", __FILE__, __LINE__, -1);
|
||||
}
|
||||
|
||||
void Socket::onDataReceived(coreutils::ZString data) {
|
||||
onDataReceived(std::string(data.getData(), data.getLength()));
|
||||
|
||||
void Socket::onDataReceived(coreutils::ZString &data) {
|
||||
onDataReceived(std::string(data.getData(), data.getLength()));
|
||||
}
|
||||
|
||||
void Socket::receiveData(coreutils::ZString buffer) {
|
||||
|
||||
|
||||
void Socket::receiveData(coreutils::ZString &buffer) {
|
||||
|
||||
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) {
|
||||
onDataReceived(buffer);
|
||||
|
||||
if((len = ::read(getDescriptor(), buffer.getData(), buffer.getLength())) >= 0) {
|
||||
coreutils::ZString zbuffer(buffer.getData(), len);
|
||||
onDataReceived(zbuffer);
|
||||
}
|
||||
else {
|
||||
|
||||
|
||||
error = errno;
|
||||
|
||||
|
||||
switch (error) {
|
||||
|
||||
// When a listening socket receives a connection
|
||||
|
||||
// When a listening socket receives a connection
|
||||
// request we get one of these.
|
||||
//
|
||||
case ENOTCONN:
|
||||
onDataReceived(std::string(buffer.getData(), 0));
|
||||
//
|
||||
case ENOTCONN:
|
||||
onDataReceived(std::string(buffer.getData(), 0));
|
||||
break;
|
||||
|
||||
case ECONNRESET:
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
throw coreutils::Exception("Error in read of data from socket.", __FILE__, __LINE__, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::writeSocket() {
|
||||
|
||||
void Socket::writeSocket() {
|
||||
if(fifo.size() > 0) {
|
||||
outlock.lock();
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "Writing data to socket " << getDescriptor() << " [" << fifo.front() << "].";
|
||||
outlock.lock();
|
||||
::write(descriptor, fifo.front().c_str(), fifo.front().length());
|
||||
fifo.pop();
|
||||
if(shutDown && !needsToWrite())
|
||||
delete this;
|
||||
outlock.unlock();
|
||||
if(shutDown && !needsToWrite())
|
||||
delete this;
|
||||
outlock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int Socket::write(std::string data) {
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "Writing data to socket " << getDescriptor() << " buffer [" << data << "].";
|
||||
outlock.lock();
|
||||
fifo.emplace(data);
|
||||
coreutils::Log(coreutils::LOG_DEBUG_4) << "Enabling write on socket " << getDescriptor() << " with " << fifo.size() << " entries to write.";
|
||||
fifo.emplace(data);
|
||||
outlock.unlock();
|
||||
ePoll.resetSocket(this);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void Socket::output(std::stringstream &out) {
|
||||
out << "|" << descriptor << "|";
|
||||
}
|
||||
|
||||
bool Socket::needsToWrite() {
|
||||
coreutils::Log(coreutils::LOG_DEBUG_4) << "Socket " << getDescriptor() << " needs to write is " << (fifo.size() > 0) << ".";
|
||||
return fifo.size() > 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Socket::shutdown(std::string text) {
|
||||
coreutils::Log(coreutils::LOG_DEBUG_2) << "Shutdown requested on socket " << descriptor << " with reason " << text << ".";
|
||||
shutDown = true;
|
||||
reset = false;
|
||||
if(!needsToWrite()) {
|
||||
delete this;
|
||||
}
|
||||
|
||||
if(!needsToWrite())
|
||||
delete this;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user