简体   繁体   English

多线程更新服务器

[英]Multi-Threaded Update Server

We are wanting to replace our current update system for the POS software at our stores. 我们希望在我们的商店中替换POS软件的当前更新系统。 Currently, we have a folder on our webserver that hosts a script that our registers call every 30 minutes or so. 当前,我们在网络服务器上有一个文件夹,该文件夹托管一个脚本,注册程序每30分钟左右调用一次。 They pass in their current version number, and the server returns an XML response containing any files that need to be updated, hash, and any other pertinent information for the register to download the updated files. 它们传递其当前版本号,并且服务器返回XML响应,其中包含需要更新的任何文件,哈希以及用于寄存器下载更新文件的任何其他相关信息。

We want a service that will listen for UDP packets that we send out that we can use to trigger an update. 我们需要一种服务,该服务将侦听所发送的UDP数据包,可用于触发更新。 This will keep the registers from unnecessarily polling for updates. 这将防止寄存器不必要地轮询更新。 We are writing an update server that will listen for a TCP connection from a machine, authenticate, do some logging, and then send along updates for the files that have changed. 我们正在编写一个更新服务器,它将侦听来自计算机的TCP连接,进行身份验证,进行一些日志记录,然后发送已更改文件的更新。 We want to move to a custom update server, because we have the need to release versions of the POS to certain machines for a trial run after testing before releasing it company-wide. 我们希望移至自定义更新服务器,因为我们需要将POS版本发布到某些计算机上,以便在测试后进行试运行,然后再在全公司范围内发布。

I've written the code to handle incoming connections and create a new thread to handle communicating with that client, etc, but I am having a hard time with handling file I/O in a multithreaded manner. 我已经编写了处理传入连接的代码,并创建了一个新线程来处理与该客户端的通信等,但是我很难以多线程方式处理文件I / O。

I'm pretty sure I won't be able to just access a file by opening it from one thread if it's already open in another. 我敢肯定,如果已经在另一个线程中打开了文件,我将无法通过从一个线程中打开文件来访问文件。 I don't want to read the files into memory and keep them in a threadsafe container beforehand, since that would use a lot of memory, especially if it turns out I need to keep a copy for each thread in order to avoid threading issues. 我不想事先将文件读入内存并将它们保存在线程安全的容器中,因为那样会占用大量内存,特别是如果事实证明,我需要为每个线程保留一个副本,以避免出现线程问题。

What would be the best way to go about being able to access these files from disk/memory in a thread-safe manner without using a lot of memory or causing threads to wait on other threads to finish with the files? 能够以线程安全的方式从磁盘/内存访问这些文件而不使用大量内存或导致线程等待其他线程完成文件的最佳方法是什么?

EDIT: Forgot to mention we are using C#. 编辑:忘记提及我们正在使用C#。

Multiple threads can open a file if it's for reading only. 如果文件仅供读取,则多个线程可以打开该文件。 Just set your file mode correctly. 只需正确设置文件模式即可。

You will get errors (on Windows at least) if two threads try and open a read/write handle to a file. 如果有两个线程尝试打开文件的读/写句柄,则会出现错误(至少在Windows上是)。

See C# async IO documentation. 请参阅C#异步IO文档。

Are the files changing? 文件是否正在更改? Why wouldn't you be able to read them from separate threads? 您为什么不能从单独的线程中读取它们? You can generally open a file twice in separate threads. 通常,您可以在单独的线程中两次打开文件。 But don't expect your writes/reads to be ordered. 但是不要期望您的写/读命令是有序的。

What happens if a register misses an update, as it might with a UDP update notification? 如果寄存器错过了更新(如UDP更新通知),会发生什么情况? You should probably continue to poll at least now and then. 您可能至少应该不时地继续进行轮询。

it sounds like you're reinventing a webserver... could proper configuration of a webserver acheive the same effect (TCP connection, logging, file delivery) 听起来好像您是在重新发明Web服务器...可以正确配置Web服务器达到相同的效果(TCP连接,日志记录,文件传递)

Windows has the TransmitFile function that may be of interest. Windows具有可能感兴趣的TransmitFile函数。 That way you let the OS handle any caching for you. 这样,您就可以让操作系统为您处理所有缓存。 This is a C call, but it should be fairly easy to use from a C++/CLI project. 这是一个C调用,但是在C ++ / CLI项目中应该相当容易使用。 The call can be done asynchronously using overlapped io. 可以使用重叠的io异步完成该调用。

This way the Windows Cache manager reads the file and sends the data on the socket without you having to touch the data at all. 这样,Windows缓存管理器就可以读取文件并在套接字上发送数据,而无需您完全触摸数据。 The function is intended for high-performance file data transfers over sockets. 该功能旨在通过套接字进行高性能的文件数据传输。

Just open the file, keep handles to files cached (so that you don't need to open them again and again) and call TransmitFile to send them on a connected socket. 只需打开文件,保留缓存文件的句柄(这样就无需一次又一次地打开它们),然后调用TransmitFile在连接的套接字上发送它们。 You need to be on a server OS for the function to work well (there are limits on transfers on client OS versions). 您必须在服务器操作系统上才能正常运行该功能(客户端操作系统版本上的传输受到限制)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM