| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- #include "Network.h"
- #ifndef WIN32
- # include <iostream>
- # include <netdb.h>
- # include <signal.h>
- #endif
- #include <Logging.h>
- #include <openssl/bio.h>
- #include <openssl/err.h>
- #include <openssl/ssl.h>
- #include <Text.h>
- // 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;
- }
|