Path: blob/master/runtime/compiler/net/CommunicationStream.hpp
6000 views
/*******************************************************************************1* Copyright (c) 2018, 2022 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/2122#ifndef COMMUNICATION_STREAM_H23#define COMMUNICATION_STREAM_H2425#include <unistd.h>26#include "net/LoadSSLLibs.hpp"27#include "net/Message.hpp"28#include "infra/Statistics.hpp"29#include "env/VerboseLog.hpp"3031namespace JITServer32{33enum JITServerCompatibilityFlags34{35JITServerJavaVersionMask = 0x00000FFF,36JITServerCompressedRef = 0x00001000,37};3839class CommunicationStream40{41public:42static bool useSSL();43static void initSSL();4445#if defined(MESSAGE_SIZE_STATS)46static TR_Stats msgSizeStats[JITServer::MessageType_MAXTYPE];47#endif /* defined(MESSAGE_SIZE_STATS) */4849static void initConfigurationFlags();5051static uint32_t getJITServerVersion()52{53return (MAJOR_NUMBER << 24) | (MINOR_NUMBER << 8); // PATCH_NUMBER is ignored54}5556static uint64_t getJITServerFullVersion()57{58return Message::buildFullVersion(getJITServerVersion(), CONFIGURATION_FLAGS);59}6061static void printJITServerVersion()62{63// print the human-readable version string64TR_VerboseLog::writeLineLocked(TR_Vlog_JITServer, "JITServer version: %u.%u.%u", MAJOR_NUMBER, MINOR_NUMBER, PATCH_NUMBER);65}6667protected:68CommunicationStream() : _ssl(NULL), _connfd(-1) { }6970virtual ~CommunicationStream()71{72if (_connfd != -1)73close(_connfd);7475if (_ssl)76(*OBIO_free_all)(_ssl);77}7879void initStream(int connfd, BIO *ssl)80{81_connfd = connfd;82_ssl = ssl;83}8485// Build a message sent by a remote party by reading from the socket86// as much as possible (up to internal buffer capacity)87void readMessage(Message &msg);88// Build a message sent by a remote party by first reading the message89// size and then reading the rest of the message90void readMessage2(Message &msg);91void writeMessage(Message &msg);9293int getConnFD() const { return _connfd; }9495BIO *_ssl; // SSL connection, null if not using SSL96int _connfd;97ServerMessage _sMsg;98ClientMessage _cMsg;99100static const uint8_t MAJOR_NUMBER = 1;101static const uint16_t MINOR_NUMBER = 38;102static const uint8_t PATCH_NUMBER = 0;103static uint32_t CONFIGURATION_FLAGS;104105private:106// readBlocking and writeBlocking are functions that directly read/write107// passed object from/to the socket. For the object to be correctly written,108// it needs to be contiguous.109template <typename T>110void readBlocking(T &val)111{112static_assert(std::is_trivially_copyable<T>::value == true, "T must be trivially copyable.");113readBlocking((char*)&val, sizeof(T));114}115116void readBlocking(char *data, size_t size)117{118if (_ssl)119{120int32_t totalBytesRead = 0;121while (totalBytesRead < size)122{123int bytesRead = (*OBIO_read)(_ssl, data + totalBytesRead, size - totalBytesRead);124if (bytesRead <= 0)125{126(*OERR_print_errors_fp)(stderr);127throw JITServer::StreamFailure("JITServer I/O error: read error");128}129totalBytesRead += bytesRead;130}131}132else133{134int32_t totalBytesRead = 0;135while (totalBytesRead < size)136{137int32_t bytesRead = read(_connfd, data + totalBytesRead, size - totalBytesRead);138if (bytesRead <= 0)139{140throw JITServer::StreamFailure("JITServer I/O error: read error");141}142totalBytesRead += bytesRead;143}144}145}146147int32_t readOnceBlocking(char *data, size_t size)148{149int32_t bytesRead = -1;150if (_ssl)151{152bytesRead = (*OBIO_read)(_ssl, data, size);153}154else155{156bytesRead = read(_connfd, data, size);157}158159if (bytesRead <= 0)160{161if (_ssl)162{163(*OERR_print_errors_fp)(stderr);164}165throw JITServer::StreamFailure("JITServer I/O error: read error");166}167return bytesRead;168}169170template <typename T>171void writeBlocking(const T &val)172{173static_assert(std::is_trivially_copyable<T>::value == true, "T must be trivially copyable.");174writeBlocking(&val, sizeof(T));175}176177void writeBlocking(const char* data, size_t size)178{179if (_ssl)180{181int32_t totalBytesWritten = 0;182while (totalBytesWritten < size)183{184int32_t bytesWritten = (*OBIO_write)(_ssl, data + totalBytesWritten, size - totalBytesWritten);185if (bytesWritten <= 0)186{187(*OERR_print_errors_fp)(stderr);188throw JITServer::StreamFailure("JITServer I/O error: write error");189}190totalBytesWritten += bytesWritten;191}192}193else194{195int32_t totalBytesWritten = 0;196while (totalBytesWritten < size)197{198int32_t bytesWritten = write(_connfd, data + totalBytesWritten, size - totalBytesWritten);199if (bytesWritten <= 0)200{201throw JITServer::StreamFailure("JITServer I/O error: write error");202}203totalBytesWritten += bytesWritten;204}205}206}207}; // class CommunicationStream208}; // namespace JITServer209210#endif // COMMUNICATION_STREAM_H211212213