Master::Master(const std::shared_ptr<Library>& library, const MasterParameters& parameters): _library(library), _parameters(parameters), nodeAcceptor(library->ioService(), parseTcpEndpoint(parameters.nodeEndpoint)), clientAcceptor(library->ioService(), parseTcpEndpoint(parameters.clientEndpoint)), heartbeatTimer(library->ioService()), heartbeatRunning(true), _sslContext(ssl::context::tlsv1), disposed(false), nodeAcceptorRunning(true), clientAcceptorRunning(true) { // Setup SSL std::string pw = parameters.sslPassword; sslContext().set_password_callback([pw](std::size_t, ssl::context::password_purpose) { return pw; }); try { BOOST_LOG_SEV(log, LogLevel::Info) << format("Using %s as the certificate chain file.") % parameters.sslCertificateFile; sslContext().use_certificate_chain_file(parameters.sslCertificateFile); } catch (const std::exception& ex) { MSCThrow(InvalidDataException (str(format("Error occured while attempting to use '%s' as the " "certificate chain file: %s") % parameters.sslCertificateFile % ex.what()))); } try { BOOST_LOG_SEV(log, LogLevel::Info) << format("Using %s as the private key file.") % parameters.sslPrivateKeyFile; sslContext().use_private_key_file(parameters.sslPrivateKeyFile, boost::asio::ssl::context::pem); } catch (const std::exception& ex) { MSCThrow(InvalidDataException (str(format("Error occured while attempting to use '%s' as the " "private key file: %s") % parameters.sslPrivateKeyFile % ex.what()))); } BOOST_LOG_SEV(log, LogLevel::Debug) << "SSL is ready."; // Prepare to accept the first client and node BOOST_LOG_SEV(log, LogLevel::Debug) << "Preparing to accept clients and nodes."; waitingNodeConnection = std::make_shared<MasterNodeConnection>(*this); waitingClient = std::make_shared<MasterClient>(*this, 1, _parameters.allowVersionSpecification); acceptNodeConnectionAsync(true); acceptClientAsync(true); // Start heartbeat BOOST_LOG_SEV(log, LogLevel::Debug) << "Starting heartbeat."; doHeartbeat(boost::system::error_code()); BOOST_LOG_SEV(log, LogLevel::Info) << format("Merlion Master Server Core (%s) running.") % MSC_VERSION_STRING; }
/*! * \brief Parses the header read using the specified \a reader. * \throws Throws InvalidDataException if the data read from the stream is * no valid frame header. */ void AdtsFrame::parseHeader(IoUtilities::BinaryReader &reader) { m_header1 = reader.readUInt16BE(); // check whether syncword is present if ((m_header1 & 0xFFF6u) != 0xFFF0u) { throw InvalidDataException(); } m_header2 = hasCrc() ? reader.readUInt56BE() : (reader.readUInt40BE() << 16); // check whether frame length is ok if (totalSize() < headerSize()) { throw InvalidDataException(); } }
/*! * \brief Prepares making. * \returns Returns a MatroskaTagFieldMaker object which can be used to actually make the field. * \remarks The field must NOT be mutated after making is prepared when it is intended to actually * make the field using the make method of the returned object. * \throws Throws TagParser::Failure or a derived exception when a making * error occurs. * * This method might be useful when it is necessary to know the size of the field before making it. */ MatroskaTagFieldMaker MatroskaTagField::prepareMaking(Diagnostics &diag) { static const string context("making Matroska \"SimpleTag\" element."); // check whether ID is empty if (id().empty()) { diag.emplace_back(DiagLevel::Critical, "Can not make \"SimpleTag\" element with empty \"TagName\".", context); throw InvalidDataException(); } try { return MatroskaTagFieldMaker(*this, diag); } catch (const ConversionException &) { diag.emplace_back(DiagLevel::Critical, "The assigned tag value can not be converted to be written appropriately.", context); throw InvalidDataException(); } }
void Web::Request::Connect() { SOCKET httpSocket; int ret; struct sockaddr_in httpAddr; struct hostent *hostinfo; char buffer[REQUESTBUFFERSIZE + 1]; int bufferSize; int timeout = REQUESTTIMEOUT; /* Choses settings for the socket to connect to */ httpAddr.sin_family = AF_INET; /* Set the port */ //httpAddr.sin_port = htons(url->getPort()); httpAddr.sin_port = htons(80); /* Do a hostname lookup on the host */ //hostinfo = gethostbyname (url->getHost()); hostinfo = gethostbyname ("localhost"); if (hostinfo == NULL) { throw HostNotFoundException(); } /* Fill the httpAddr with IP data */ httpAddr.sin_addr = *(struct in_addr *) hostinfo->h_addr; /* Creates the socket */ httpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) ; /* Check for errors */ if (httpSocket == INVALID_SOCKET) throw SocketException(); /* Add a timeout on all communiction*/ ret = setsockopt(httpSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)); /* Check for errors */ if (ret == SOCKET_ERROR) { closesocket(httpSocket); throw SocketException(); } /* Connect to the server */ ret = connect(httpSocket,(LPSOCKADDR)&httpAddr, sizeof(struct sockaddr)); /* Check for errors */ if (ret == SOCKET_ERROR) { closesocket(httpSocket); throw ConnectionFailedException(WSAGetLastError()); } /* Create a request */ bufferSize = _snprintf(buffer, REQUESTBUFFERSIZE, "GET %s HTTP/1.0\r\n\r\n", url->getURL()); if (bufferSize <= 0) { closesocket(httpSocket); throw Exception("Request Buffer Too Small"); } /* Send a HTTP request */ ret = send(httpSocket, buffer, bufferSize, 0); if (ret < bufferSize) { closesocket(httpSocket); throw TransferException(); } reply.assign(""); ret = REQUESTBUFFERSIZE; /* Now wait for the return and loop while its coming */ while (ret > 0) { ret = recv(httpSocket, buffer, REQUESTBUFFERSIZE, 0); /* Append data to the buffer */ if (ret > 0) reply.append(buffer, ret); } /* Clean up the socket */ closesocket(httpSocket); /* Now if we didn't get any data throw a error */ if (reply.length()==0) { throw TransferException(); } /* Got some data let parse it */ ret = (int)reply.find("\r\n\r\n"); if (ret==-1) { throw InvalidDataException(); } header = reply.substr(0, ret); ret += 4; body = reply.substr(ret, reply.length() - ret); }