Zero buffer for test before buffer read.
This commit is contained in:
parent
ec39c1df1a
commit
7e43656c5c
127
Socket.cpp
127
Socket.cpp
@ -5,27 +5,27 @@
|
|||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
|
|
||||||
namespace core {
|
namespace core {
|
||||||
|
|
||||||
Socket::Socket(EPoll &ePoll, std::string text) : ePoll(ePoll), text(text) {
|
Socket::Socket(EPoll &ePoll, std::string text) : ePoll(ePoll), text(text) {
|
||||||
coreutils::Log(coreutils::LOG_DEBUG_2) << "Socket object created [" << text << "].";
|
coreutils::Log(coreutils::LOG_DEBUG_2) << "Socket object created [" << text << "].";
|
||||||
buffer = (char *)malloc(4096);
|
buffer = (char *)malloc(4096);
|
||||||
length = 4096;
|
length = 4096;
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket::~Socket() {
|
Socket::~Socket() {
|
||||||
free(buffer);
|
free(buffer);
|
||||||
if(descriptor == -1)
|
if(descriptor == -1)
|
||||||
return;
|
return;
|
||||||
onUnregister();
|
onUnregister();
|
||||||
ePoll.unregisterSocket(this);
|
ePoll.unregisterSocket(this);
|
||||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "Socket destroyed for socket " << descriptor << ".";
|
coreutils::Log(coreutils::LOG_DEBUG_3) << "Socket destroyed for socket " << descriptor << ".";
|
||||||
close(descriptor);
|
close(descriptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::setDescriptor(int descriptor) {
|
void Socket::setDescriptor(int descriptor) {
|
||||||
if((descriptor == -1) && (errno == 24)) {
|
if((descriptor == -1) && (errno == 24)) {
|
||||||
shutdown("Too many files open");
|
shutdown("Too many files open");
|
||||||
throw coreutils::Exception("Too many files open. Refusing connection.");
|
throw coreutils::Exception("Too many files open. Refusing connection.");
|
||||||
}
|
}
|
||||||
lock.lock();
|
lock.lock();
|
||||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "Descriptor set to " << descriptor << " for Socket.";
|
coreutils::Log(coreutils::LOG_DEBUG_3) << "Descriptor set to " << descriptor << " for Socket.";
|
||||||
@ -37,116 +37,119 @@ namespace core {
|
|||||||
onRegistered();
|
onRegistered();
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
int Socket::getDescriptor() {
|
int Socket::getDescriptor() {
|
||||||
return descriptor;
|
return descriptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::setBufferSize(int length) {
|
void Socket::setBufferSize(int length) {
|
||||||
this->length = length;
|
this->length = length;
|
||||||
buffer = (char *)realloc(buffer, length);
|
buffer = (char *)realloc(buffer, length);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Socket::getBufferSize() {
|
int Socket::getBufferSize() {
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::onRegister() {}
|
void Socket::onRegister() {}
|
||||||
|
|
||||||
void Socket::onRegistered() {}
|
void Socket::onRegistered() {}
|
||||||
|
|
||||||
void Socket::onUnregister() {}
|
void Socket::onUnregister() {}
|
||||||
|
|
||||||
void Socket::onUnregistered() {}
|
void Socket::onUnregistered() {}
|
||||||
|
|
||||||
bool Socket::eventReceived(struct epoll_event event) {
|
bool Socket::eventReceived(struct epoll_event event) {
|
||||||
|
|
||||||
lock.lock();
|
lock.lock();
|
||||||
|
|
||||||
if(event.events & EPOLLRDHUP) {
|
if(event.events & EPOLLRDHUP) {
|
||||||
readHangup = true;
|
readHangup = true;
|
||||||
shutdown("hangup received");
|
shutdown("hangup received");
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(event.events & EPOLLIN) {
|
if(event.events & EPOLLIN) {
|
||||||
coreutils::ZString zbuffer(buffer, length);
|
coreutils::ZString zbuffer(buffer, length);
|
||||||
receiveData(zbuffer);
|
receiveData(zbuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(event.events & EPOLLWRNORM) {
|
if(event.events & EPOLLWRNORM) {
|
||||||
writeSocket();
|
writeSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(event.events & EPOLLHUP) {
|
if(event.events & EPOLLHUP) {
|
||||||
shutdown();
|
shutdown();
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
|
|
||||||
reset = true;
|
reset = true;
|
||||||
return reset;
|
return reset;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::onDataReceived(std::string data) {
|
void Socket::onDataReceived(std::string data) {
|
||||||
throw coreutils::Exception("Need to override onDataReceived.", __FILE__, __LINE__, -1);
|
throw coreutils::Exception("Need to override onDataReceived.", __FILE__, __LINE__, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::onDataReceived(coreutils::ZString &data) {
|
void Socket::onDataReceived(coreutils::ZString &data) {
|
||||||
onDataReceived(std::string(data.getData(), data.getLength()));
|
onDataReceived(std::string(data.getData(), data.getLength()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::receiveData(coreutils::ZString &buffer) {
|
void Socket::receiveData(coreutils::ZString &buffer) {
|
||||||
|
|
||||||
coreutils::ZString blank("");
|
coreutils::ZString blank("");
|
||||||
|
|
||||||
if(buffer.getLength() <= 0)
|
if(buffer.getLength() <= 0)
|
||||||
throw coreutils::Exception("Request to receive data with a zero buffer length.", __FILE__, __LINE__, -1);
|
throw coreutils::Exception("Request to receive data with a zero buffer length.", __FILE__, __LINE__, -1);
|
||||||
|
|
||||||
int len;
|
int len;
|
||||||
int error = -1;
|
int error = -1;
|
||||||
|
|
||||||
|
for(int ix = 0; ix < buffer.getLength(); ++ix)
|
||||||
|
buffer[ix] = 0;
|
||||||
|
|
||||||
if((len = ::read(getDescriptor(), buffer.getData(), buffer.getLength())) >= 0) {
|
if((len = ::read(getDescriptor(), buffer.getData(), buffer.getLength())) >= 0) {
|
||||||
coreutils::ZString zbuffer(buffer.getData(), len);
|
coreutils::ZString zbuffer(buffer.getData(), len);
|
||||||
onDataReceived(zbuffer);
|
onDataReceived(zbuffer);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
error = errno;
|
error = errno;
|
||||||
|
|
||||||
switch (error) {
|
switch (error) {
|
||||||
|
|
||||||
// When a listening socket receives a connection
|
// When a listening socket receives a connection
|
||||||
// request we get one of these.
|
// request we get one of these.
|
||||||
//
|
//
|
||||||
case ENOTCONN:
|
case ENOTCONN:
|
||||||
onDataReceived(blank);
|
onDataReceived(blank);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ECONNRESET:
|
case ECONNRESET:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw coreutils::Exception("Error in read of data from socket.", __FILE__, __LINE__, error);
|
throw coreutils::Exception("Error in read of data from socket.", __FILE__, __LINE__, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::writeSocket() {
|
void Socket::writeSocket() {
|
||||||
if(fifo.size() > 0) {
|
if(fifo.size() > 0) {
|
||||||
outlock.lock();
|
outlock.lock();
|
||||||
::write(descriptor, fifo.front().c_str(), fifo.front().length());
|
::write(descriptor, fifo.front().c_str(), fifo.front().length());
|
||||||
fifo.pop();
|
fifo.pop();
|
||||||
if(shutDown && !needsToWrite())
|
if(shutDown && !needsToWrite())
|
||||||
delete this;
|
delete this;
|
||||||
outlock.unlock();
|
outlock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int Socket::write(std::string data) {
|
int Socket::write(std::string data) {
|
||||||
outlock.lock();
|
outlock.lock();
|
||||||
fifo.emplace(data);
|
fifo.emplace(data);
|
||||||
@ -154,22 +157,22 @@ namespace core {
|
|||||||
ePoll.resetSocket(this);
|
ePoll.resetSocket(this);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::output(std::stringstream &out) {
|
void Socket::output(std::stringstream &out) {
|
||||||
out << "|" << descriptor << "|";
|
out << "|" << descriptor << "|";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Socket::needsToWrite() {
|
bool Socket::needsToWrite() {
|
||||||
return fifo.size() > 0;
|
return fifo.size() > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::shutdown(std::string text) {
|
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 on socket " << descriptor << " with reason " << text << ".";
|
||||||
shutDown = true;
|
shutDown = true;
|
||||||
reset = false;
|
reset = false;
|
||||||
// if(!needsToWrite())
|
// if(!needsToWrite())
|
||||||
delete this;
|
delete this;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user