Parcourir la source

还没想好怎么将asyn_server与connection完全分离

tanghai il y a 15 ans
Parent
commit
fc791d994d
6 fichiers modifiés avec 72 ajouts et 20 suppressions
  1. 2 1
      src/SConscript
  2. 1 1
      src/net/SConscript
  3. 9 9
      src/net/asyn_server.cc
  4. 4 3
      src/net/asyn_server.h
  5. 4 3
      src/net/connection.h
  6. 52 3
      src/net/connection_test.cc

+ 2 - 1
src/SConscript

@@ -3,7 +3,8 @@ Import('env')
 subdirs = [
 subdirs = [
 	'experimental',
 	'experimental',
 	'thread',
 	'thread',
+	'net',
 ]
 ]
 
 
 for subdir in subdirs:
 for subdir in subdirs:
-	SConscript('%s/SConscript' % subdir)
+	SConscript('%s/SConscript' % subdir)

+ 1 - 1
src/net/SConscript

@@ -11,5 +11,5 @@ net_lib = net_env.StaticLibrary('net', net_src)
 
 
 net_env.Append(LIBS=net_lib)
 net_env.Append(LIBS=net_lib)
 
 
-net_env.Program('asyn_server_test.cc')
+#net_env.Program('asyn_server_test.cc')
 net_env.Program('connection_test.cc')
 net_env.Program('connection_test.cc')

+ 9 - 9
src/net/asyn_server.cc

@@ -1,13 +1,13 @@
 #include <boost/foreach.hpp>
 #include <boost/foreach.hpp>
+#include <boost/bind.hpp>
 #include "base/base.h"
 #include "base/base.h"
 #include "net/asyn_server.h"
 #include "net/asyn_server.h"
-#include "net/connection.h"
 
 
 namespace hainan {
 namespace hainan {
 
 
-explicit asyn_server::asyn_server(const string& address, const string& port) :
+asyn_server::asyn_server(const std::string& address, const std::string& port) :
 	io_service_(), acceptor_(io_service_),
 	io_service_(), acceptor_(io_service_),
-	new_connection_(new connection(io_service_, connections_))
+	connections_(), new_connection_(new connection(io_service_, connections_))
 {
 {
 	boost::asio::ip::tcp::resolver resolver(io_service_);
 	boost::asio::ip::tcp::resolver resolver(io_service_);
 	boost::asio::ip::tcp::resolver::query query(address, port);
 	boost::asio::ip::tcp::resolver::query query(address, port);
@@ -17,19 +17,19 @@ explicit asyn_server::asyn_server(const string& address, const string& port) :
 	acceptor_.bind(endpoint);
 	acceptor_.bind(endpoint);
 	acceptor_.listen();
 	acceptor_.listen();
 	acceptor_.async_accept(new_connection_->socket(),
 	acceptor_.async_accept(new_connection_->socket(),
-			boost::bind(&server::handle_accept, this,
-					asio::placeholders::error));
+			boost::bind(&asyn_server::handle_accept, this,
+					boost::asio::placeholders::error));
 }
 }
 
 
-void asyn_server::handle_accept(const system::error_code& e)
+void asyn_server::handle_accept(const boost::system::error_code& e)
 {
 {
 	if (!e)
 	if (!e)
 	{
 	{
 		connections_.insert(new_connection_);
 		connections_.insert(new_connection_);
 		new_connection_.reset(new connection(io_service_, connections_));
 		new_connection_.reset(new connection(io_service_, connections_));
 		acceptor_.async_accept(new_connection_->socket(),
 		acceptor_.async_accept(new_connection_->socket(),
-				boost::bind(&server::handle_accept, this,
-						asio::placeholders::error));
+				boost::bind(&asyn_server::handle_accept, this,
+						boost::asio::placeholders::error));
 	}
 	}
 }
 }
 
 
@@ -50,7 +50,7 @@ void asyn_server::start()
 
 
 void asyn_server::stop()
 void asyn_server::stop()
 {
 {
-	io_service_.post(boost::bind(&asyn_server::handle_stop(), this));
+	io_service_.post(boost::bind(&asyn_server::handle_stop, this));
 }
 }
 
 
 } // namespace hainan
 } // namespace hainan

+ 4 - 3
src/net/asyn_server.h

@@ -8,7 +8,7 @@
 
 
 namespace hainan {
 namespace hainan {
 
 
-class asyn_server: private noncopyable
+class asyn_server: private boost::noncopyable
 {
 {
 private:
 private:
 	// hold all connection
 	// hold all connection
@@ -17,13 +17,14 @@ private:
 	boost::asio::ip::tcp::acceptor acceptor_;
 	boost::asio::ip::tcp::acceptor acceptor_;
 	connection_ptr new_connection_;
 	connection_ptr new_connection_;
 
 
-	void handle_accept(const system::error_code& e);
+	void handle_accept(const boost::system::error_code& e);
 	void handle_stop();
 	void handle_stop();
 public:
 public:
-	explicit asyn_server(const string& address, const string& port);
+	explicit asyn_server(const std::string& address, const std::string& port);
 	void start();
 	void start();
 	void stop();
 	void stop();
 };
 };
+
 } // hainan
 } // hainan
 
 
 #endif // NET_ASYNSERVER_H
 #endif // NET_ASYNSERVER_H

+ 4 - 3
src/net/connection.h

@@ -6,22 +6,24 @@
 #include <boost/array.hpp>
 #include <boost/array.hpp>
 #include <boost/noncopyable.hpp>
 #include <boost/noncopyable.hpp>
 #include <boost/shared_ptr.hpp>
 #include <boost/shared_ptr.hpp>
+#include <boost/unordered_set.hpp>
 #include <boost/enable_shared_from_this.hpp>
 #include <boost/enable_shared_from_this.hpp>
 
 
 namespace hainan {
 namespace hainan {
 
 
+class connection;
+typedef boost::shared_ptr<connection> connection_ptr;
 typedef boost::unordered_set<connection_ptr> connection_set;
 typedef boost::unordered_set<connection_ptr> connection_set;
 
 
 class connection: private boost::noncopyable,
 class connection: private boost::noncopyable,
 		public boost::enable_shared_from_this<connection>
 		public boost::enable_shared_from_this<connection>
 {
 {
 private:
 private:
-
 	boost::asio::ip::tcp::socket socket_;
 	boost::asio::ip::tcp::socket socket_;
 	connection_set& connections_;
 	connection_set& connections_;
 	boost::array<char, 8192> buffer_;
 	boost::array<char, 8192> buffer_;
 public:
 public:
-	explicit connection::connection(boost::asio::io_service& io_service,
+	explicit connection(boost::asio::io_service& io_service,
 			connection_set& manager);
 			connection_set& manager);
 	boost::asio::ip::tcp::socket& socket();
 	boost::asio::ip::tcp::socket& socket();
 	void start();
 	void start();
@@ -30,7 +32,6 @@ public:
 			size_t bytes_transferred) = 0;
 			size_t bytes_transferred) = 0;
 	virtual void handle_writer(const boost::system::error_code& e) = 0;
 	virtual void handle_writer(const boost::system::error_code& e) = 0;
 };
 };
-typedef boost::shared_ptr<connection> connection_ptr;
 
 
 } // namespace hainan
 } // namespace hainan
 
 

+ 52 - 3
src/net/connection_test.cc

@@ -4,11 +4,19 @@
 
 
 namespace hainan {
 namespace hainan {
 
 
+static const char address[] = "127.0.0.1";
+static int port = 10000;
 class connection1: public connection
 class connection1: public connection
 {
 {
 public:
 public:
-	string content;
+	std::string content;
 	size_t bytes;
 	size_t bytes;
+	explicit connection1(boost::asio::io_service io_service,
+			connection_set connections):
+		connection(io_service, connections)
+	{
+	}
+
 	void handle_read(const system::error_code& e,
 	void handle_read(const system::error_code& e,
 			size_t bytes_transferred)
 			size_t bytes_transferred)
 	{
 	{
@@ -28,17 +36,58 @@ public:
 class ConnectionTest: public testing::Test
 class ConnectionTest: public testing::Test
 {
 {
 private:
 private:
-	asio::io_service io_service_;
+	boost::asio::io_service io_service_;
+	boost::asio::ip::tcp::acceptor acceptor_;
+
 	connection_set connections_;
 	connection_set connections_;
+	connection_ptr connection_;
 
 
 	void SetUp()
 	void SetUp()
 	{
 	{
+		connection_.reset(new connection1(io_service_, connections_));
+	}
+
+	void handle_accept()
+	{
+	}
+
+	void server_start()
+	{
+		boost::asio::ip::tcp::endpoint endpoint(
+				boost::asio::ip::address_v4.from_string(address), port);
+		acceptor_.open(endpoint.protocol());
+		acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
+		acceptor_.bind(endpoint);
+		acceptor_.listen();
+		acceptor_.async_accept(connection_->socket(),
+				boost::bind(&ConnectionTest::handle_accept, this,
+						boost::asio::placeholders::error));
+	}
+
+	void client_connect()
+	{
+		boost::asio::ip::tcp::endpoint endpoint(
+				boost::asio::ip::address_v4.from_string(address), port);
+
+		// Try each endpoint until we successfully establish a connection.
+		boost::asio::ip::tcp::socket socket(io_service_);
+		socket.connect(endpoint, error);
+
+		boost::asio::streambuf request;
+		std::ostream request_stream(&request);
+		request_stream << "test string";
+
+		socket.write_some(request);
 	}
 	}
 };
 };
 
 
 TEST_F(ConnectionTest, Test1)
 TEST_F(ConnectionTest, Test1)
 {
 {
-
+	server_start();
+	client_connect();
+	io_service_.run();
+	ASSERT_EQ(11, bytes_transferred);
+	ASSERT_EQ("test string", content);
 }
 }
 
 
 } // namespace hainan
 } // namespace hainan