Undo some changes in main

This commit is contained in:
WebFreak001 2017-08-11 06:26:52 +02:00
parent a6074cfb7a
commit cd4a027eac
2 changed files with 42 additions and 9 deletions

View File

@ -167,7 +167,7 @@ struct AutocompleteResponse
ulong symbolIdentifier; ulong symbolIdentifier;
/** /**
* Creates an empty acknoledgement response * Creates an empty acknowledgement response
*/ */
static AutocompleteResponse ack() static AutocompleteResponse ack()
{ {

View File

@ -131,7 +131,7 @@ int main(string[] args)
socket = new TcpSocket(AddressFamily.INET); socket = new TcpSocket(AddressFamily.INET);
socket.blocking = true; socket.blocking = true;
socket.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true); socket.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true);
socket.bind(new InternetAddress("127.0.0.1", port)); socket.bind(new InternetAddress("localhost", port));
info("Listening on port ", port); info("Listening on port ", port);
} }
else else
@ -182,21 +182,40 @@ int main(string[] args)
// No relative paths // No relative paths
version (Posix) chdir("/"); version (Posix) chdir("/");
version (LittleEndian)
immutable expectedClient = IPv4Union([1, 0, 0, 127]);
else
immutable expectedClient = IPv4Union([127, 0, 0, 1]);
serverLoop: while (true) serverLoop: while (true)
{ {
auto s = socket.accept(); auto s = socket.accept();
s.blocking = true; s.blocking = true;
if (useTCP)
{
// Only accept connections from localhost
IPv4Union actual;
InternetAddress clientAddr = cast(InternetAddress) s.remoteAddress();
actual.i = clientAddr.addr;
// Shut down if somebody tries connecting from outside
if (actual.i != expectedClient.i)
{
fatal("Connection attempted from ", clientAddr.toAddrString());
return 1;
}
}
scope (exit) scope (exit)
{ {
s.shutdown(SocketShutdown.BOTH); s.shutdown(SocketShutdown.BOTH);
s.close(); s.close();
} }
sw.reset();
sw.start();
auto bytesReceived = s.receive(buffer); auto bytesReceived = s.receive(buffer);
auto requestWatch = StopWatch(AutoStart.yes);
size_t messageLength; size_t messageLength;
// bit magic! // bit magic!
(cast(ubyte*) &messageLength)[0..size_t.sizeof] = buffer[0..size_t.sizeof]; (cast(ubyte*) &messageLength)[0..size_t.sizeof] = buffer[0..size_t.sizeof];
@ -205,12 +224,18 @@ int main(string[] args)
immutable b = s.receive(buffer[bytesReceived .. $]); immutable b = s.receive(buffer[bytesReceived .. $]);
if (b == Socket.ERROR) if (b == Socket.ERROR)
{ {
warning("Socket recieve failed"); bytesReceived = Socket.ERROR;
continue serverLoop; break;
} }
bytesReceived += b; bytesReceived += b;
} }
if (bytesReceived == Socket.ERROR)
{
warning("Socket recieve failed");
break;
}
AutocompleteRequest request; AutocompleteRequest request;
msgpack.unpack(buffer[size_t.sizeof .. bytesReceived], request); msgpack.unpack(buffer[size_t.sizeof .. bytesReceived], request);
@ -259,12 +284,20 @@ int main(string[] args)
else if (request.kind & RequestKind.localUse) else if (request.kind & RequestKind.localUse)
s.trySendResponse(findLocalUse(request, cache), "Could not find local usage"); s.trySendResponse(findLocalUse(request, cache), "Could not find local usage");
sw.stop(); info("Request processed in ", requestWatch.peek().to!("msecs", float), " milliseconds");
info("Request processed in ", sw.peek().to!("msecs", float), " milliseconds");
} }
return 0; return 0;
} }
/// IP v4 address as bytes and a uint
union IPv4Union
{
/// the bytes
ubyte[4] b;
/// the uint
uint i;
}
/// Lazily evaluates a response with an exception handler and sends it to a socket or logs msg if evaluating response fails. /// 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, string msg) void trySendResponse(Socket socket, lazy AutocompleteResponse response, string msg)
{ {