#include "Network.h" #ifndef WIN32 # include # include # include #endif #include #include #include #include #include // Start network void Network::Start(int maxClients) { #ifdef WIN32 WSADATA lpwd; lpwd.iMaxSockets = maxClients; int error = WSAStartup(MAKEWORD(2, 0), &lpwd); if (error != 0) Framework::Logging::error() << "Could not initialize Win Sock 2.0 WSAStartup: " << error; #else signal(SIGPIPE, SIG_IGN); #endif SSL_library_init(); SSL_load_error_strings(); OpenSSL_add_ssl_algorithms(); } void Network::getHostName(char* name, int bufferLen) { gethostname(name, bufferLen); } char* Network::getHostAddress() { char* address; char name[255] = ""; getHostName(name, 255); PHOSTENT hostinfo; hostinfo = gethostbyname(name); if (!hostinfo) { Framework::Logging::error() << "The ip address of host '" << name << "' could not be resolved"; return 0; } address = inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list); return address; } // Stop network void Network::Exit() { #ifdef WIN32 WSACleanup(); #endif } Network::NetworkReader::NetworkReader(Connection* v) { connection = v; } Network::NetworkReader::~NetworkReader() {} //! Reads from the resource //! \param bytes An array to be filled with bytes from the resource //! \param len How many bytes to read from the resource void Network::NetworkReader::read(char* bytes, int len) { connection->getMessage(bytes, len); } //! Reads the next line from the resource //! \return The read line as text with line break Framework::Text* Network::NetworkReader::readLine() { int maxLength = 2048; char* buffer = new char[maxLength]; char b; int index = 0; do { connection->getMessage(&b, 1); buffer[index++] = b; if (index == maxLength) { maxLength += 2048; char* tmp = new char[maxLength]; memcpy(tmp, buffer, (__int64)maxLength - 2048); delete[] buffer; buffer = tmp; } } while (b != '\n'); buffer[index] = 0; Framework::Text* result = new Framework::Text(buffer); delete[] buffer; return result; } //! Checks whether the resource has been fully read //! return 1 if the resource has been fully read. 0 otherwise bool Network::NetworkReader::isEnd() const { return 0; } Network::NetworkWriter::NetworkWriter(Connection* v) { connection = v; } Network::NetworkWriter::~NetworkWriter() {} //! Writes to the resource //! \param bytes An array containing the bytes to write to the resource //! \param len How many bytes to write to the resource void Network::NetworkWriter::write(const char* bytes, int len) { connection->send(bytes, len); } //! Checks whether the resource has been fully written //! return 1 if the resource has been fully written. 0 otherwise bool Network::NetworkWriter::isEnd() const { return 0; } Network::EncryptedNetworkReader::EncryptedNetworkReader(EncryptedConnection* v) { connection = v; } //! Reads from the resource //! \param bytes An array to be filled with bytes from the resource //! \param len How many bytes to read from the resource void Network::EncryptedNetworkReader::read(char* bytes, int len) { connection->getMessageEncrypted(bytes, len); } //! Reads the next line from the resource //! \return The read line as text with line break Framework::Text* Network::EncryptedNetworkReader::readLine() { int maxLength = 2048; char* buffer = new char[maxLength]; char b; int index = 0; do { connection->getMessageEncrypted(&b, 1); buffer[index++] = b; if (index == maxLength) { maxLength += 2048; char* tmp = new char[maxLength]; memcpy(tmp, buffer, (__int64)maxLength - 2048); delete[] buffer; buffer = tmp; } } while (b != '\n'); buffer[index] = 0; Framework::Text* result = new Framework::Text(buffer); delete[] buffer; return result; } //! Checks whether the resource has been fully read //! return 1 if the resource has been fully read. 0 otherwise bool Network::EncryptedNetworkReader::isEnd() const { return 0; } Network::EncryptedNetworkWriter::EncryptedNetworkWriter(EncryptedConnection* v) { connection = v; } //! Writes to the resource //! \param bytes An array containing the bytes to write to the resource //! \param len How many bytes to write to the resource void Network::EncryptedNetworkWriter::write(const char* bytes, int len) { connection->sendEncrypted(bytes, len); } //! Checks whether the resource has been fully written //! return 1 if the resource has been fully written. 0 otherwise bool Network::EncryptedNetworkWriter::isEnd() const { return 0; }