setup ignore update for .history files.
This commit is contained in:
1 parent
001cd8c06c
commit
1e76a7f49a
85 files changed
+528
-10920
No files matched your search
+81
-57
@@ -1,54 +1,61 @@
|
||||
#include "EPoll.h"
|
||||
#include "Socket.h"
|
||||
#include "EPoll.h"
|
||||
#include "Exception.h"
|
||||
#include "ZString.h"
|
||||
#include "Log.h"
|
||||
#include "ZString.h"
|
||||
|
||||
namespace core {
|
||||
namespace core
|
||||
{
|
||||
|
||||
void sigpipe_handler(int unused) {}
|
||||
|
||||
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 << "].";
|
||||
buffer = (char *)malloc(4096);
|
||||
length = 4096;
|
||||
}
|
||||
|
||||
Socket::~Socket() {
|
||||
Socket::~Socket()
|
||||
{
|
||||
free(buffer);
|
||||
if(descriptor == -1)
|
||||
return;
|
||||
if (descriptor == -1)
|
||||
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");
|
||||
throw coreutils::Exception("Too many files open. Refusing connection.");
|
||||
void Socket::setDescriptor(int descriptor)
|
||||
{
|
||||
if ((descriptor == -1) && (errno == 24))
|
||||
{
|
||||
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.";
|
||||
if(descriptor < 3)
|
||||
throw coreutils::Exception("Descriptor out of range", __FILE__, __LINE__);
|
||||
if (descriptor < 3)
|
||||
throw coreutils::Exception("Descriptor out of range", __FILE__, __LINE__);
|
||||
this->descriptor = descriptor;
|
||||
onRegister();
|
||||
ePoll.registerSocket(this);
|
||||
onRegistered();
|
||||
}
|
||||
|
||||
int Socket::getDescriptor() {
|
||||
int Socket::getDescriptor()
|
||||
{
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
void Socket::setBufferSize(int length) {
|
||||
void Socket::setBufferSize(int length)
|
||||
{
|
||||
this->length = length;
|
||||
buffer = (char *)realloc(buffer, length);
|
||||
|
||||
}
|
||||
|
||||
int Socket::getBufferSize() {
|
||||
int Socket::getBufferSize()
|
||||
{
|
||||
return length;
|
||||
}
|
||||
|
||||
@@ -60,99 +67,116 @@ namespace core {
|
||||
|
||||
void Socket::onUnregistered() {}
|
||||
|
||||
bool Socket::eventReceived(struct epoll_event event) {
|
||||
inHandler = true;
|
||||
if(event.events & EPOLLRDHUP) {
|
||||
bool Socket::eventReceived(struct epoll_event event)
|
||||
{
|
||||
inHandler = true;
|
||||
if (event.events & EPOLLRDHUP)
|
||||
{
|
||||
readHangup = true;
|
||||
shutdown("hangup received");
|
||||
}
|
||||
else if(event.events & EPOLLIN) {
|
||||
else if (event.events & EPOLLIN)
|
||||
{
|
||||
coreutils::ZString zbuffer(buffer, length);
|
||||
receiveData(zbuffer);
|
||||
receiveData(zbuffer);
|
||||
}
|
||||
else if(event.events & EPOLLWRNORM) {
|
||||
writeSocket();
|
||||
else if (event.events & EPOLLWRNORM)
|
||||
{
|
||||
writeSocket();
|
||||
}
|
||||
else if(event.events & EPOLLHUP) {
|
||||
else if (event.events & EPOLLHUP)
|
||||
{
|
||||
shutdown();
|
||||
}
|
||||
inHandler = false;
|
||||
inHandler = false;
|
||||
return !shutDown;
|
||||
}
|
||||
|
||||
void Socket::onDataReceived(std::string data) {
|
||||
void Socket::onDataReceived(std::string data)
|
||||
{
|
||||
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()));
|
||||
}
|
||||
|
||||
void Socket::receiveData(coreutils::ZString &buffer) {
|
||||
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);
|
||||
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) {
|
||||
if ((len = ::read(getDescriptor(), buffer.getData(), buffer.getLength())) >= 0)
|
||||
{
|
||||
coreutils::ZString zbuffer(buffer.getData(), len);
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << zbuffer;
|
||||
onDataReceived(zbuffer);
|
||||
onDataReceived(zbuffer);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
|
||||
error = errno;
|
||||
error = errno;
|
||||
|
||||
switch (error) {
|
||||
switch (error)
|
||||
{
|
||||
|
||||
// When a listening socket receives a connection
|
||||
// request we get one of these.
|
||||
//
|
||||
case ENOTCONN:
|
||||
// When a listening socket receives a connection
|
||||
// request we get one of these.
|
||||
//
|
||||
case ENOTCONN:
|
||||
onDataReceived(blank);
|
||||
break;
|
||||
|
||||
case ECONNRESET:
|
||||
break;
|
||||
|
||||
default:
|
||||
case ECONNRESET:
|
||||
break;
|
||||
|
||||
default:
|
||||
throw coreutils::Exception("Error in read of data from socket.", __FILE__, __LINE__, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::writeSocket() {
|
||||
if(fifo.size() > 0) {
|
||||
outlock.lock();
|
||||
if(!shutDown)
|
||||
::write(descriptor, fifo.front().c_str(), fifo.front().length());
|
||||
void Socket::writeSocket()
|
||||
{
|
||||
if (fifo.size() > 0)
|
||||
{
|
||||
outlock.lock();
|
||||
if (!shutDown)
|
||||
::write(descriptor, fifo.front().c_str(), fifo.front().length());
|
||||
fifo.pop();
|
||||
outlock.unlock();
|
||||
outlock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
int Socket::write(std::string data) {
|
||||
int Socket::write(std::string data)
|
||||
{
|
||||
outlock.lock();
|
||||
fifo.emplace(data);
|
||||
outlock.unlock();
|
||||
if(!inHandler)
|
||||
ePoll.resetSocket(this);
|
||||
if (!inHandler)
|
||||
ePoll.resetSocket(this);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Socket::output(std::stringstream &out) {
|
||||
void Socket::output(std::stringstream &out)
|
||||
{
|
||||
out << "|" << descriptor << "|";
|
||||
}
|
||||
|
||||
bool Socket::needsToWrite() {
|
||||
bool Socket::needsToWrite()
|
||||
{
|
||||
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 << ".";
|
||||
shutDown = true;
|
||||
reset = false;
|
||||
|
||||
Reference in new issue
Block a user