@@ -70,7 +70,6 @@ public void start()
70
70
{
71
71
logger .debug ( "~~ [CONNECT] %s:%d." , host , port );
72
72
channel = ChannelFactory .create ( host , port , config , logger );
73
-
74
73
protocol = negotiateProtocol ();
75
74
reader = protocol .reader ();
76
75
writer = protocol .writer ();
@@ -170,21 +169,38 @@ private SocketProtocol negotiateProtocol() throws IOException
170
169
{
171
170
logger .debug ( "~~ [HANDSHAKE] [0x6060B017, 1, 0, 0, 0]." );
172
171
//Propose protocol versions
173
- ByteBuffer buf = ByteBuffer .allocate ( 5 * 4 ).order ( BIG_ENDIAN );
172
+ ByteBuffer buf = ByteBuffer .allocateDirect ( 5 * 4 ).order ( BIG_ENDIAN );
174
173
buf .putInt ( MAGIC_PREAMBLE );
175
174
for ( int version : SUPPORTED_VERSIONS )
176
175
{
177
176
buf .putInt ( version );
178
177
}
179
178
buf .flip ();
180
179
181
- channel .write ( buf );
180
+ //Do a blocking write
181
+ while (buf .hasRemaining ())
182
+ {
183
+ if (channel .write ( buf ) < 0 )
184
+ {
185
+ throw new ClientException (
186
+ "Connection terminated while proposing protocol. This can happen due to network " +
187
+ "instabilities, or due to restarts of the database." );
188
+ }
189
+ }
182
190
183
- // Read back the servers choice
191
+ // Read (blocking) back the servers choice
184
192
buf .clear ();
185
193
buf .limit ( 4 );
186
194
187
- channel .read ( buf );
195
+ while (buf .hasRemaining ())
196
+ {
197
+ if ( channel .read ( buf ) < 0 )
198
+ {
199
+ throw new ClientException (
200
+ "Connection terminated while negotiating protocol. This can happen due to network " +
201
+ "instabilities, or due to restarts of the database." );
202
+ }
203
+ }
188
204
189
205
// Choose protocol, or fail
190
206
buf .flip ();
@@ -223,7 +239,6 @@ public static ByteChannel create( String host, int port, Config config, Logger l
223
239
SocketChannel soChannel = SocketChannel .open ();
224
240
soChannel .setOption ( StandardSocketOptions .SO_REUSEADDR , true );
225
241
soChannel .setOption ( StandardSocketOptions .SO_KEEPALIVE , true );
226
-
227
242
soChannel .connect ( new InetSocketAddress ( host , port ) );
228
243
229
244
ByteChannel channel ;
0 commit comments