34
34
import org .neo4j .driver .v1 .internal .spi .Logger ;
35
35
import org .neo4j .driver .v1 .internal .messaging .MessageFormat ;
36
36
37
+ import static java .nio .ByteOrder .*;
38
+
37
39
public class SocketClient
38
40
{
41
+ private static final int MAGIC_PREAMBLE = 0x6060B017 ;
42
+ private static final int VERSION1 = 1 ;
43
+ private static final int NO_VERSION = 0 ;
44
+ private static final int [] SUPPORTED_VERSIONS = new int []{VERSION1 , NO_VERSION , NO_VERSION , NO_VERSION };
45
+
39
46
private final String host ;
40
47
private final int port ;
41
48
private final Logger logger ;
@@ -121,14 +128,15 @@ public void stop()
121
128
122
129
private SocketProtocol negotiateProtocol () throws IOException
123
130
{
124
- // TODO make this not so hard-coded
125
- logger .debug ( "~~ [HANDSHAKE] [1, 0, 0, 0]." );
126
- // Propose protocol versions
127
- ByteBuffer buf = ByteBuffer .wrap ( new byte []{
128
- 0 , 0 , 0 , 1 ,
129
- 0 , 0 , 0 , 0 ,
130
- 0 , 0 , 0 , 0 ,
131
- 0 , 0 , 0 , 0 } );
131
+ logger .debug ( "~~ [HANDSHAKE] [0x6060B017, 1, 0, 0, 0]." );
132
+ //Propose protocol versions
133
+ ByteBuffer buf = ByteBuffer .allocate ( 5 * 4 ).order ( BIG_ENDIAN );
134
+ buf .putInt ( MAGIC_PREAMBLE );
135
+ for ( int version : SUPPORTED_VERSIONS )
136
+ {
137
+ buf .putInt ( version );
138
+ }
139
+ buf .flip ();
132
140
133
141
channel .write ( buf );
134
142
@@ -141,13 +149,12 @@ private SocketProtocol negotiateProtocol() throws IOException
141
149
// Choose protocol, or fail
142
150
buf .flip ();
143
151
final int proposal = buf .getInt ();
144
-
145
152
switch ( proposal )
146
153
{
147
- case 1 :
154
+ case VERSION1 :
148
155
logger .debug ( "~~ [HANDSHAKE] 1" );
149
156
return new SocketProtocolV1 ( channel );
150
- case 0 : throw new ClientException ( "The server does not support any of the protocol versions supported by " +
157
+ case NO_VERSION : throw new ClientException ( "The server does not support any of the protocol versions supported by " +
151
158
"this driver. Ensure that you are using driver and server versions that " +
152
159
"are compatible with one another." );
153
160
default : throw new ClientException ( "Protocol error, server suggested unexpected protocol version: " +
0 commit comments