Simplify server main

This commit is contained in:
WebFreak001 2017-08-24 21:00:43 +02:00
parent 843a7783c3
commit fdb5ff10ce
2 changed files with 48 additions and 52 deletions

View File

@ -165,6 +165,16 @@ struct AutocompleteResponse
* Symbol identifier * Symbol identifier
*/ */
ulong symbolIdentifier; ulong symbolIdentifier;
/**
* Creates an empty acknowledgement response
*/
static AutocompleteResponse ack()
{
AutocompleteResponse response;
response.completionType = "ack";
return response;
}
} }
/** /**

View File

@ -113,7 +113,7 @@ int main(string[] args)
return 1; return 1;
} }
if (serverIsRunning(useTCP, socketFile, port)) if (serverIsRunning(useTCP, socketFile, port))
{ {
fatal("Another instance of DCD-server is already running"); fatal("Another instance of DCD-server is already running");
return 1; return 1;
@ -211,9 +211,11 @@ int main(string[] args)
s.shutdown(SocketShutdown.BOTH); s.shutdown(SocketShutdown.BOTH);
s.close(); s.close();
} }
ptrdiff_t bytesReceived = s.receive(buffer); ptrdiff_t bytesReceived = s.receive(buffer);
auto requestWatch = StopWatch(AutoStart.yes); sw.reset();
sw.start();
size_t messageLength; size_t messageLength;
// bit magic! // bit magic!
@ -237,6 +239,7 @@ int main(string[] args)
AutocompleteRequest request; AutocompleteRequest request;
msgpack.unpack(buffer[size_t.sizeof .. bytesReceived], request); msgpack.unpack(buffer[size_t.sizeof .. bytesReceived], request);
if (request.kind & RequestKind.clearCache) if (request.kind & RequestKind.clearCache)
{ {
info("Clearing cache."); info("Clearing cache.");
@ -249,82 +252,65 @@ int main(string[] args)
} }
else if (request.kind & RequestKind.query) else if (request.kind & RequestKind.query)
{ {
AutocompleteResponse response; s.sendResponse(AutocompleteResponse.ack);
response.completionType = "ack";
ubyte[] responseBytes = msgpack.pack(response);
s.send(responseBytes);
continue; continue;
} }
if (request.kind & RequestKind.addImport) if (request.kind & RequestKind.addImport)
{ {
cache.addImportPaths(request.importPaths); cache.addImportPaths(request.importPaths);
} }
if (request.kind & RequestKind.listImports) if (request.kind & RequestKind.listImports)
{ {
AutocompleteResponse response; AutocompleteResponse response;
response.importPaths = cache.getImportPaths().map!(a => cast() a).array(); response.importPaths = cache.getImportPaths().map!(a => cast() a).array();
ubyte[] responseBytes = msgpack.pack(response);
info("Returning import path list"); info("Returning import path list");
s.send(responseBytes); s.sendResponse(response);
} }
else if (request.kind & RequestKind.autocomplete) else if (request.kind & RequestKind.autocomplete)
{ {
info("Getting completions"); info("Getting completions");
AutocompleteResponse response = complete(request, cache); s.sendResponse(complete(request, cache));
ubyte[] responseBytes = msgpack.pack(response);
s.send(responseBytes);
} }
else if (request.kind & RequestKind.doc) else if (request.kind & RequestKind.doc)
{ {
info("Getting doc comment"); info("Getting doc comment");
try s.trySendResponse(getDoc(request, cache), "Could not get DDoc information");
{
AutocompleteResponse response = getDoc(request, cache);
ubyte[] responseBytes = msgpack.pack(response);
s.send(responseBytes);
}
catch (Exception e)
{
warning("Could not get DDoc information", e.msg);
}
} }
else if (request.kind & RequestKind.symbolLocation) else if (request.kind & RequestKind.symbolLocation)
{ s.trySendResponse(findDeclaration(request, cache), "Could not get symbol location");
try
{
AutocompleteResponse response = findDeclaration(request, cache);
ubyte[] responseBytes = msgpack.pack(response);
s.send(responseBytes);
}
catch (Exception e)
{
warning("Could not get symbol location", e.msg);
}
}
else if (request.kind & RequestKind.search) else if (request.kind & RequestKind.search)
{ s.sendResponse(symbolSearch(request, cache));
AutocompleteResponse response = symbolSearch(request, cache); else if (request.kind & RequestKind.localUse)
ubyte[] responseBytes = msgpack.pack(response); s.trySendResponse(findLocalUse(request, cache), "Couldnot find local usage");
s.send(responseBytes);
} sw.stop();
else if (request.kind & RequestKind.localUse) info("Request processed in ", sw.peek().to!("msecs", float), " milliseconds");
{
try
{
AutocompleteResponse response = findLocalUse(request, cache);
ubyte[] responseBytes = msgpack.pack(response);
s.send(responseBytes);
}
catch (Exception e)
{
warning("Could not find local usage", e.msg);
}
}
info("Request processed in ", requestWatch.peek().to!("msecs", float), " milliseconds");
} }
return 0; return 0;
} }
/// Lazily evaluates a response with an exception handler and sends it to a socket or logs msg if evaluating response fails.
void trySendResponse(Socket socket, lazy AutocompleteResponse response, lazy string msg)
{
try
{
sendResponse(socket, response);
}
catch (Exception e)
{
warningf("%s: %s", msg, e.msg);
}
}
/// Packs an AutocompleteResponse and sends it to a socket.
void sendResponse(Socket socket, AutocompleteResponse response)
{
ubyte[] responseBytes = msgpack.pack(response);
socket.send(responseBytes);
}
/// IP v4 address as bytes and a uint /// IP v4 address as bytes and a uint
union IPv4Union union IPv4Union
{ {