Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/compiler/net/CommunicationStream.hpp
6000 views
1
/*******************************************************************************
2
* Copyright (c) 2018, 2022 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* 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-exception
21
*******************************************************************************/
22
23
#ifndef COMMUNICATION_STREAM_H
24
#define COMMUNICATION_STREAM_H
25
26
#include <unistd.h>
27
#include "net/LoadSSLLibs.hpp"
28
#include "net/Message.hpp"
29
#include "infra/Statistics.hpp"
30
#include "env/VerboseLog.hpp"
31
32
namespace JITServer
33
{
34
enum JITServerCompatibilityFlags
35
{
36
JITServerJavaVersionMask = 0x00000FFF,
37
JITServerCompressedRef = 0x00001000,
38
};
39
40
class CommunicationStream
41
{
42
public:
43
static bool useSSL();
44
static void initSSL();
45
46
#if defined(MESSAGE_SIZE_STATS)
47
static TR_Stats msgSizeStats[JITServer::MessageType_MAXTYPE];
48
#endif /* defined(MESSAGE_SIZE_STATS) */
49
50
static void initConfigurationFlags();
51
52
static uint32_t getJITServerVersion()
53
{
54
return (MAJOR_NUMBER << 24) | (MINOR_NUMBER << 8); // PATCH_NUMBER is ignored
55
}
56
57
static uint64_t getJITServerFullVersion()
58
{
59
return Message::buildFullVersion(getJITServerVersion(), CONFIGURATION_FLAGS);
60
}
61
62
static void printJITServerVersion()
63
{
64
// print the human-readable version string
65
TR_VerboseLog::writeLineLocked(TR_Vlog_JITServer, "JITServer version: %u.%u.%u", MAJOR_NUMBER, MINOR_NUMBER, PATCH_NUMBER);
66
}
67
68
protected:
69
CommunicationStream() : _ssl(NULL), _connfd(-1) { }
70
71
virtual ~CommunicationStream()
72
{
73
if (_connfd != -1)
74
close(_connfd);
75
76
if (_ssl)
77
(*OBIO_free_all)(_ssl);
78
}
79
80
void initStream(int connfd, BIO *ssl)
81
{
82
_connfd = connfd;
83
_ssl = ssl;
84
}
85
86
// Build a message sent by a remote party by reading from the socket
87
// as much as possible (up to internal buffer capacity)
88
void readMessage(Message &msg);
89
// Build a message sent by a remote party by first reading the message
90
// size and then reading the rest of the message
91
void readMessage2(Message &msg);
92
void writeMessage(Message &msg);
93
94
int getConnFD() const { return _connfd; }
95
96
BIO *_ssl; // SSL connection, null if not using SSL
97
int _connfd;
98
ServerMessage _sMsg;
99
ClientMessage _cMsg;
100
101
static const uint8_t MAJOR_NUMBER = 1;
102
static const uint16_t MINOR_NUMBER = 38;
103
static const uint8_t PATCH_NUMBER = 0;
104
static uint32_t CONFIGURATION_FLAGS;
105
106
private:
107
// readBlocking and writeBlocking are functions that directly read/write
108
// passed object from/to the socket. For the object to be correctly written,
109
// it needs to be contiguous.
110
template <typename T>
111
void readBlocking(T &val)
112
{
113
static_assert(std::is_trivially_copyable<T>::value == true, "T must be trivially copyable.");
114
readBlocking((char*)&val, sizeof(T));
115
}
116
117
void readBlocking(char *data, size_t size)
118
{
119
if (_ssl)
120
{
121
int32_t totalBytesRead = 0;
122
while (totalBytesRead < size)
123
{
124
int bytesRead = (*OBIO_read)(_ssl, data + totalBytesRead, size - totalBytesRead);
125
if (bytesRead <= 0)
126
{
127
(*OERR_print_errors_fp)(stderr);
128
throw JITServer::StreamFailure("JITServer I/O error: read error");
129
}
130
totalBytesRead += bytesRead;
131
}
132
}
133
else
134
{
135
int32_t totalBytesRead = 0;
136
while (totalBytesRead < size)
137
{
138
int32_t bytesRead = read(_connfd, data + totalBytesRead, size - totalBytesRead);
139
if (bytesRead <= 0)
140
{
141
throw JITServer::StreamFailure("JITServer I/O error: read error");
142
}
143
totalBytesRead += bytesRead;
144
}
145
}
146
}
147
148
int32_t readOnceBlocking(char *data, size_t size)
149
{
150
int32_t bytesRead = -1;
151
if (_ssl)
152
{
153
bytesRead = (*OBIO_read)(_ssl, data, size);
154
}
155
else
156
{
157
bytesRead = read(_connfd, data, size);
158
}
159
160
if (bytesRead <= 0)
161
{
162
if (_ssl)
163
{
164
(*OERR_print_errors_fp)(stderr);
165
}
166
throw JITServer::StreamFailure("JITServer I/O error: read error");
167
}
168
return bytesRead;
169
}
170
171
template <typename T>
172
void writeBlocking(const T &val)
173
{
174
static_assert(std::is_trivially_copyable<T>::value == true, "T must be trivially copyable.");
175
writeBlocking(&val, sizeof(T));
176
}
177
178
void writeBlocking(const char* data, size_t size)
179
{
180
if (_ssl)
181
{
182
int32_t totalBytesWritten = 0;
183
while (totalBytesWritten < size)
184
{
185
int32_t bytesWritten = (*OBIO_write)(_ssl, data + totalBytesWritten, size - totalBytesWritten);
186
if (bytesWritten <= 0)
187
{
188
(*OERR_print_errors_fp)(stderr);
189
throw JITServer::StreamFailure("JITServer I/O error: write error");
190
}
191
totalBytesWritten += bytesWritten;
192
}
193
}
194
else
195
{
196
int32_t totalBytesWritten = 0;
197
while (totalBytesWritten < size)
198
{
199
int32_t bytesWritten = write(_connfd, data + totalBytesWritten, size - totalBytesWritten);
200
if (bytesWritten <= 0)
201
{
202
throw JITServer::StreamFailure("JITServer I/O error: write error");
203
}
204
totalBytesWritten += bytesWritten;
205
}
206
}
207
}
208
}; // class CommunicationStream
209
}; // namespace JITServer
210
211
#endif // COMMUNICATION_STREAM_H
212
213