Update client.d

Moved socket operations out to their own functions where they should have been from the beginning. Changed "127.0.0.1" to "localhost" to make DCD work on certain Windows configurations.
This commit is contained in:
Hackerpilot 2013-09-03 13:42:25 -07:00
parent 1b943fcab2
commit 0de3d52c9b
1 changed files with 43 additions and 39 deletions

View File

@ -100,34 +100,18 @@ int /*_*/main(string[] args)
request.kind = RequestKind.shutdown; request.kind = RequestKind.shutdown;
else if (clearCache) else if (clearCache)
request.kind = RequestKind.clearCache; request.kind = RequestKind.clearCache;
auto socket = new TcpSocket(AddressFamily.INET); TcpSocket socket = createSocket(port);
scope (exit) { socket.shutdown(SocketShutdown.BOTH); socket.close(); } scope (exit) { socket.shutdown(SocketShutdown.BOTH); socket.close(); }
socket.connect(new InternetAddress("127.0.0.1", port)); return sendRequest(socket, request) ? 0 : 1;
socket.blocking = true;
socket.setOption(SocketOptionLevel.TCP, SocketOption.TCP_NODELAY, 1);
ubyte[] message = msgpack.pack(request);
ubyte[] messageBuffer = new ubyte[message.length + message.length.sizeof];
auto messageLength = message.length;
messageBuffer[0 .. size_t.sizeof] = (cast(ubyte*) &messageLength)[0 .. size_t.sizeof];
messageBuffer[size_t.sizeof .. $] = message[];
return socket.send(messageBuffer) == messageBuffer.length ? 0 : 1;
} }
else if (importPaths.length > 0) else if (importPaths.length > 0)
{ {
AutocompleteRequest request; AutocompleteRequest request;
request.kind = RequestKind.addImport; request.kind = RequestKind.addImport;
request.importPaths = importPaths.map!(a => isRooted(a) ? a : absolutePath(a)).array; request.importPaths = importPaths.map!(a => isRooted(a) ? a : absolutePath(a)).array;
auto socket = new TcpSocket(AddressFamily.INET); TcpSocket socket = createSocket(port);
scope (exit) { socket.shutdown(SocketShutdown.BOTH); socket.close(); } scope (exit) { socket.shutdown(SocketShutdown.BOTH); socket.close(); }
socket.connect(new InternetAddress("127.0.0.1", port)); return sendRequest(socket, request) ? 0 : 1;
socket.blocking = true;
socket.setOption(SocketOptionLevel.TCP, SocketOption.TCP_NODELAY, 1);
ubyte[] message = msgpack.pack(request);
ubyte[] messageBuffer = new ubyte[message.length + message.length.sizeof];
auto messageLength = message.length;
messageBuffer[0 .. size_t.sizeof] = (cast(ubyte*) &messageLength)[0 .. size_t.sizeof];
messageBuffer[size_t.sizeof .. $] = message[];
return socket.send(messageBuffer) == messageBuffer.length ? 0 : 1;
} }
else if (cursorPos == size_t.max) else if (cursorPos == size_t.max)
{ {
@ -169,30 +153,14 @@ int /*_*/main(string[] args)
request.importPaths = importPaths; request.importPaths = importPaths;
request.sourceCode = sourceCode; request.sourceCode = sourceCode;
request.cursorPosition = cursorPos; request.cursorPosition = cursorPos;
ubyte[] message = msgpack.pack(request);
// Send message to server // Send message to server
TcpSocket socket = new TcpSocket(AddressFamily.INET); TcpSocket socket = createSocket(port);
socket.setOption(SocketOptionLevel.SOCKET, SocketOption.RCVTIMEO, dur!"seconds"(5));
scope (exit) { socket.shutdown(SocketShutdown.BOTH); socket.close(); } scope (exit) { socket.shutdown(SocketShutdown.BOTH); socket.close(); }
socket.connect(new InternetAddress("127.0.0.1", port)); if (!sendRequest(socket, request))
socket.blocking = true;
ubyte[] messageBuffer = new ubyte[message.length + message.length.sizeof];
auto messageLength = message.length;
messageBuffer[0 .. size_t.sizeof] = (cast(ubyte*) &messageLength)[0 .. size_t.sizeof];
messageBuffer[size_t.sizeof .. $] = message[];
auto bytesSent = socket.send(messageBuffer);
// Get response and write it out
ubyte[1024 * 16] buffer;
auto bytesReceived = socket.receive(buffer);
if (bytesReceived == Socket.ERROR)
{
return 1; return 1;
}
AutocompleteResponse response; AutocompleteResponse response = getResponse(socket);
msgpack.unpack(buffer[0..bytesReceived], response);
if (response.completions.length > 0) if (response.completions.length > 0)
{ {
@ -249,3 +217,39 @@ Options:
Uses PORTNUMBER to communicate with the server instead of the default Uses PORTNUMBER to communicate with the server instead of the default
port 9166.`, programName); port 9166.`, programName);
} }
TcpSocket createSocket(ushort port)
{
TcpSocket socket = new TcpSocket(AddressFamily.INET);
socket.setOption(SocketOptionLevel.SOCKET, SocketOption.RCVTIMEO, dur!"seconds"(5));
socket.connect(new InternetAddress("localhost", port));
socket.blocking = true;
return socket;
}
/**
* Returns: true on success
*/
bool sendRequest(TcpSocket socket, AutocompleteRequest request)
{
ubyte[] message = msgpack.pack(request);
ubyte[] messageBuffer = new ubyte[message.length + message.length.sizeof];
auto messageLength = message.length;
messageBuffer[0 .. size_t.sizeof] = (cast(ubyte*) &messageLength)[0 .. size_t.sizeof];
messageBuffer[size_t.sizeof .. $] = message[];
return socket.send(messageBuffer) == messageBuffer.length;
}
/**
* Gets the response from the server
*/
AutocompleteResponse getResponse(TcpSocket socket)
{
ubyte[1024 * 16] buffer;
auto bytesReceived = socket.receive(buffer);
if (bytesReceived == Socket.ERROR)
throw new Exception("Incorrect number of bytes received");
AutocompleteResponse response;
msgpack.unpack(buffer[0..bytesReceived], response);
return response;
}