Neat idea. For better performance, you might want to use native filesystem APIs, for example on windows use CreateFile with the FILE_WRITE_THROUGH flag to prevent caching and and extra flushing.
Thanks J :) Absolutely! I did try various flags to optimise performance. For CreateFile (which in .NET is wrapped by the FileStream class) I tried FILE_WRITE_THROUGH (which is FileOptions.WriteThrough in .NET), and found it impacted performance quite a lot.
The key to high performance as you rightly pointed out was preventing flushing. In the end, what worked best was reducing the number of writes to disk (which is the bottleneck). I did that by buffering 10-50 ms worth of TCP data, coupled with only flushing explicitly (using a large buffer so that neither BinaryWriter or FileStream flush automatically).
if __name__ == '__main__':
if len(sys.argv) < 3:
print("Usage: python script.py [server|client] [machine_name] [server_name (for client only)]")
sys.exit(1)
mode, machine_name = sys.argv[1], sys.argv[2]
if mode == 'server':
proxy_server(machine_name)
elif mode == 'client':
if len(sys.argv) < 4:
print("Please provide the server name or IP address for the client mode.")
sys.exit(1)
server_name = sys.argv[3]
proxy_client(server_name, machine_name)
else:
print("Invalid mode. Please use 'server' or 'client'.")
sys.exit(1)