简体   繁体   English

boost :: asio tcp数据库服务器

[英]boost::asio tcp database server

I need some hints: I'm writing a tcp database server, that can hold 100 strings (I used vector of strings to hold the data). 我需要一些提示:我正在编写一个tcp数据库服务器,该服务器可以容纳100个字符串(我使用了字符串向量来保存数据)。 Basic functionality, achieved by me so far is: 到目前为止,我实现的基本功能是:

client sends: PUT "index" "value" - string is put in the vector[index]. 客户端发送:PUT“索引”“值”-将字符串放在vector [index]中。
client sends: GET "index" - vector[index] is sent to client 客户端发送:GET“索引”-矢量[索引]发送到客户端

I based my work on boost::asio async echo server example http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/example/echo/async_tcp_echo_server.cpp 我的工作基于boost :: asio异步回显服务器示例http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/example/echo/async_tcp_echo_server.cpp

Right now, I want to extend the functionality of the server to: 现在,我想将服务器的功能扩展到:

STILL UNSOLVED 1. Be able to load the starting content of vector from file on start and save it on exit. 仍然未解决 1.能够在开始时从文件加载向量的开始内容,并在退出时将其保存。

SOLVED 2. Put timer on database elements: lifetime of string in vector[index] should be 30 seconds, if within 30seconds of input, the new data doesn't arrive to vector[index] the content should be erased. 已解决 2.在数据库元素上放置计时器:vector [index]中字符串的生存期应为30秒,如果在输入的30秒内,新数据未到达vector [index],则应擦除其内容。

The only issue left, is saving/loading vector data on exit/start. 剩下的唯一问题是在退出/启动时保存/加载矢量数据。 Since the vector contains only 100 strings, I could simply read/write using fstream, but I have a problem with triggering the save. 由于向量仅包含100个字符串,因此我可以简单地使用fstream进行读取/写入,但是触发保存存在问题。

My async server's main function contains: io_service.run(); 我的异步服务器的主要功能包括:io_service.run(); How can I trigger an action by pressing a key during running server? 在服务器运行过程中,如何通过按键触发动作? My first guess is async_read from user input as in http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/example/chat/posix_chat_client.cpp example. 我的第一个猜测是来自用户输入的async_read,如http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/example/chat/posix_chat_client.cpp示例中所示。 Any other ideas? 还有其他想法吗?

I'd implement the timer by saving a timestamp with the value, and on lookup, check whether the entry has expired. 我将通过使用值保存时间戳来实现计时器,并在查找时检查条目是否已过期。 No need to wake up the CPU with a timer if noone asked whether the entry is still valid. 如果没有人询问该条目是否仍然有效,则无需使用计时器唤醒CPU。

Also, as long as you have a proper abstraction between the network code and the database implementation, you can adapt your database to whatever is most suitable. 另外,只要您在网络代码和数据库实现之间有适当的抽象,就可以使数据库适应最合适的条件。 If there is a maximum number of entries and they all fit into memory, then by all means a vector is fine, and SQLite is probably overkill. 如果有最大数量的条目并且它们全部都适合内存,那么通过任何方式都可以使用vector ,而SQLite可能会过大。

Use Redis or Memcached because they have value time to live already. 使用RedisMemcached,因为它们已经拥有宝贵的生存时间。 Both have C++ drivers. 两者都有C ++驱动程序。

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

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