Sfoglia il codice sorgente

文件名,目录名,类名和函数名首字母大写

tanghai 15 anni fa
parent
commit
8729217ea5
53 ha cambiato i file con 44 aggiunte e 350 eliminazioni
  1. 0 0
      src/Base/Base.h
  2. 0 0
      src/Base/Marcos.h
  3. 2 2
      src/Base/Typedefs.h
  4. 0 0
      src/Experimental/BoostAsioTest.cc
  5. 1 1
      src/Experimental/BoostFunctionTest.cc
  6. 0 0
      src/Experimental/SConscript
  7. 0 0
      src/Experimental/chat/chat_message.cc
  8. 0 0
      src/Experimental/chat/chat_message.h
  9. 0 0
      src/Experimental/chat/chat_server.cc
  10. 0 0
      src/Experimental/chat/posix_chat_client.cc
  11. 0 0
      src/Experimental/echo/async_tcp_echo_server.cc
  12. 0 0
      src/Experimental/echo/async_udp_echo_server.cc
  13. 0 0
      src/Experimental/echo/blocking_tcp_echo_client.cc
  14. 0 0
      src/Experimental/echo/blocking_tcp_echo_server.cc
  15. 0 0
      src/Experimental/http_client/async_client.cc
  16. 0 0
      src/Experimental/http_client/sync_client.cc
  17. 0 0
      src/Experimental/http_server/connection.cc
  18. 0 0
      src/Experimental/http_server/connection.h
  19. 0 0
      src/Experimental/http_server/connection_manager.cc
  20. 0 0
      src/Experimental/http_server/connection_manager.h
  21. 0 0
      src/Experimental/http_server/header.h
  22. 0 0
      src/Experimental/http_server/mime_types.cc
  23. 0 0
      src/Experimental/http_server/mime_types.h
  24. 0 0
      src/Experimental/http_server/posix_main.cc
  25. 0 0
      src/Experimental/http_server/reply.cc
  26. 0 0
      src/Experimental/http_server/reply.h
  27. 0 0
      src/Experimental/http_server/request.h
  28. 0 0
      src/Experimental/http_server/request_handler.cc
  29. 0 0
      src/Experimental/http_server/request_handler.h
  30. 0 0
      src/Experimental/http_server/request_parser.cc
  31. 0 0
      src/Experimental/http_server/request_parser.h
  32. 0 0
      src/Experimental/http_server/server.cc
  33. 0 0
      src/Experimental/http_server/server.h
  34. 2 2
      src/Net/RpcChannel.cc
  35. 2 2
      src/Net/RpcChannel.h
  36. 2 2
      src/Net/RpcCommunicator.cc
  37. 1 1
      src/Net/RpcCommunicator.h
  38. 0 0
      src/Net/RpcController.cc
  39. 2 2
      src/Net/RpcController.h
  40. 0 0
      src/Net/RpcProtobufData.proto
  41. 0 0
      src/Net/SConscript
  42. 2 2
      src/Thread/SConscript
  43. 11 11
      src/Thread/ThreadPool.cc
  44. 12 12
      src/Thread/ThreadPool.h
  45. 7 7
      src/Thread/ThreadPoolTest.cc
  46. 0 56
      src/net/asyn_server.cc
  47. 0 30
      src/net/asyn_server.h
  48. 0 0
      src/net/asyn_server_test.cc
  49. 0 31
      src/net/connection.cc
  50. 0 38
      src/net/connection.h
  51. 0 93
      src/net/connection_test.cc
  52. 0 29
      src/net/rpc_callback.cc
  53. 0 29
      src/net/rpc_callback.h

+ 0 - 0
src/base/base.h → src/Base/Base.h


+ 0 - 0
src/base/marcos.h → src/Base/Marcos.h


+ 2 - 2
src/base/typedefs.h → src/Base/Typedefs.h

@@ -12,7 +12,7 @@ typedef boost::uint32_t uint32;
 typedef boost::uint64_t uint64;
 
 // smart_ptr typedef
-typedef boost::shared_ptr<boost::thread> thread_ptr;
-typedef boost::shared_ptr<google::protobuf::Service> protobuf_service_ptr;
+typedef boost::shared_ptr<boost::thread> ThreadPtr;
+typedef boost::shared_ptr<google::protobuf::Service> ProtobufServicePtr;
 
 #endif // BASE_TYPEDEFS_H

+ 0 - 0
src/experimental/boost_asio_test.cc → src/Experimental/BoostAsioTest.cc


+ 1 - 1
src/experimental/boost_function_test.cc → src/Experimental/BoostFunctionTest.cc

@@ -7,7 +7,7 @@
 #include <gflags/gflags.h>
 #include <glog/logging.h>
 
-namespace hainan {
+namespace Hainan {
 class BoostTest: public testing::Test
 {
 	void SetUp()

+ 0 - 0
src/experimental/SConscript → src/Experimental/SConscript


+ 0 - 0
src/experimental/chat/chat_message.cc → src/Experimental/chat/chat_message.cc


+ 0 - 0
src/experimental/chat/chat_message.h → src/Experimental/chat/chat_message.h


+ 0 - 0
src/experimental/chat/chat_server.cc → src/Experimental/chat/chat_server.cc


+ 0 - 0
src/experimental/chat/posix_chat_client.cc → src/Experimental/chat/posix_chat_client.cc


+ 0 - 0
src/experimental/echo/async_tcp_echo_server.cc → src/Experimental/echo/async_tcp_echo_server.cc


+ 0 - 0
src/experimental/echo/async_udp_echo_server.cc → src/Experimental/echo/async_udp_echo_server.cc


+ 0 - 0
src/experimental/echo/blocking_tcp_echo_client.cc → src/Experimental/echo/blocking_tcp_echo_client.cc


+ 0 - 0
src/experimental/echo/blocking_tcp_echo_server.cc → src/Experimental/echo/blocking_tcp_echo_server.cc


+ 0 - 0
src/experimental/http_client/async_client.cc → src/Experimental/http_client/async_client.cc


+ 0 - 0
src/experimental/http_client/sync_client.cc → src/Experimental/http_client/sync_client.cc


+ 0 - 0
src/experimental/http_server/connection.cc → src/Experimental/http_server/connection.cc


+ 0 - 0
src/experimental/http_server/connection.h → src/Experimental/http_server/connection.h


+ 0 - 0
src/experimental/http_server/connection_manager.cc → src/Experimental/http_server/connection_manager.cc


+ 0 - 0
src/experimental/http_server/connection_manager.h → src/Experimental/http_server/connection_manager.h


+ 0 - 0
src/experimental/http_server/header.h → src/Experimental/http_server/header.h


+ 0 - 0
src/experimental/http_server/mime_types.cc → src/Experimental/http_server/mime_types.cc


+ 0 - 0
src/experimental/http_server/mime_types.h → src/Experimental/http_server/mime_types.h


+ 0 - 0
src/experimental/http_server/posix_main.cc → src/Experimental/http_server/posix_main.cc


+ 0 - 0
src/experimental/http_server/reply.cc → src/Experimental/http_server/reply.cc


+ 0 - 0
src/experimental/http_server/reply.h → src/Experimental/http_server/reply.h


+ 0 - 0
src/experimental/http_server/request.h → src/Experimental/http_server/request.h


+ 0 - 0
src/experimental/http_server/request_handler.cc → src/Experimental/http_server/request_handler.cc


+ 0 - 0
src/experimental/http_server/request_handler.h → src/Experimental/http_server/request_handler.h


+ 0 - 0
src/experimental/http_server/request_parser.cc → src/Experimental/http_server/request_parser.cc


+ 0 - 0
src/experimental/http_server/request_parser.h → src/Experimental/http_server/request_parser.h


+ 0 - 0
src/experimental/http_server/server.cc → src/Experimental/http_server/server.cc


+ 0 - 0
src/experimental/http_server/server.h → src/Experimental/http_server/server.h


+ 2 - 2
src/net/rpc_channel.cc → src/Net/RpcChannel.cc

@@ -1,7 +1,7 @@
 #include <google/protobuf/message.h>
 #include "net/rpc_channel.h"
 
-namespace hainan {
+namespace Hainan {
 
 google::protobuf::Closure* to_closure();
 
@@ -36,4 +36,4 @@ void rpc_channel::CallMethod(
 	communicator.send_message(req, callback);
 }
 
-} // namespace hainan
+} // namespace Hainan

+ 2 - 2
src/net/rpc_channel.h → src/Net/RpcChannel.h

@@ -7,7 +7,7 @@
 #include "base/base.h"
 #include "net/rpc_communicator.h"
 
-namespace hainan {
+namespace Hainan {
 
 class rpc_channel: public google::protobuf::RpcChannel
 {
@@ -25,6 +25,6 @@ public:
 			google::protobuf::Closure* done);
 };
 
-} // namespace hainan
+} // namespace Hainan
 
 #endif // NET_RPC_CHANNEL_H

+ 2 - 2
src/net/rpc_communicator.cc → src/Net/RpcCommunicator.cc

@@ -1,6 +1,6 @@
 #include "net/rpc_communicator.h"
 
-namespace hainan {
+namespace Hainan {
 
 void rpc_communicator::send_message(
 		const rpc_request& req, rpc_callback& callback)
@@ -8,4 +8,4 @@ void rpc_communicator::send_message(
 
 }
 
-} // namespace hainan
+} // namespace Hainan

+ 1 - 1
src/net/rpc_communicator.h → src/Net/RpcCommunicator.h

@@ -3,7 +3,7 @@
 
 #include <boost/asio.hpp>
 
-namespace hainan {
+namespace Hainan {
 
 class rpc_request;
 class rpc_callback;

+ 0 - 0
src/net/rpc_controller.cc → src/Net/RpcController.cc


+ 2 - 2
src/net/rpc_controller.h → src/Net/RpcController.h

@@ -3,7 +3,7 @@
 
 #include <google/protobuf/service.h>
 
-namespace hainan {
+namespace Hainan {
 
 class rpc_controller: public google::protobuf::RpcController
 {
@@ -11,6 +11,6 @@ public:
 
 };
 
-} // namespace hainan
+} // namespace Hainan
 
 #endif // NET_RPC_CONTROLLER_H

+ 0 - 0
src/net/protobuf_rpc.proto → src/Net/RpcProtobufData.proto


+ 0 - 0
src/net/SConscript → src/Net/SConscript


+ 2 - 2
src/thread/SConscript → src/Thread/SConscript

@@ -7,11 +7,11 @@ thread_env.Append(LIBS=[
 ])
 
 thread_src = [
-	'thread_pool.cc',
+	'ThreadPool.cc',
 ]
 
 thread_lib = thread_env.StaticLibrary('thread', thread_src)
 
 thread_env.Append(LIBS=thread_lib)
 
-thread_env.Test('thread_pool_test', 'thread_pool_test.cc')
+thread_env.Test('ThreadPoolTest', 'ThreadPoolTest.cc')

+ 11 - 11
src/thread/thread_pool.cc → src/Thread/ThreadPool.cc

@@ -1,30 +1,30 @@
 #include <glog/logging.h>
-#include "thread/thread_pool.h"
+#include "Thread/ThreadPool.h"
 
-namespace hainan {
+namespace Hainan {
 
-thread_pool::thread_pool() :
+ThreadPool::ThreadPool() :
 	num(0), running(false), work_num(0)
 {
 }
-thread_pool::~thread_pool()
+ThreadPool::~ThreadPool()
 {
 }
 
-void thread_pool::start()
+void ThreadPool::Start()
 {
 	running = true;
 	for (int i = 0; i < num; ++i)
 	{
 		thread_ptr t(new boost::thread(
-				boost::bind(&thread_pool::runner, this)));
+				boost::bind(&ThreadPool::Runner, this)));
 		threads.push_back(t);
 		t->detach();
 	}
 	work_num = num;
 }
 
-void thread_pool::stop()
+void ThreadPool::Stop()
 {
 	VLOG(3)<< "Stop";
 	boost::mutex::scoped_lock lock(mutex);
@@ -37,7 +37,7 @@ void thread_pool::stop()
 	}
 }
 
-void thread_pool::runner()
+void ThreadPool::Runner()
 {
 	VLOG(3) << "thread start";
 	bool continued = true;
@@ -78,7 +78,7 @@ void thread_pool::runner()
 	}
 }
 
-bool thread_pool::push_task(boost::function<void (void)> task)
+bool ThreadPool::PushTask(boost::function<void (void)> task)
 {
 	VLOG(3) << "push task";
 	{
@@ -94,9 +94,9 @@ bool thread_pool::push_task(boost::function<void (void)> task)
 	return true;
 }
 
-void thread_pool::set_num(int num)
+void ThreadPool::SetNum(int num)
 {
 	this->num = num;
 }
 
-} // namespace hainan
+} // namespace Hainan

+ 12 - 12
src/thread/thread_pool.h → src/Thread/ThreadPool.h

@@ -7,9 +7,9 @@
 #include <boost/bind.hpp>
 #include "base/base.h"
 
-namespace hainan {
+namespace Hainan {
 
-class thread_pool: private boost::noncopyable
+class ThreadPool: private boost::noncopyable
 {
 private:
 	int num;
@@ -18,19 +18,19 @@ private:
 	boost::mutex mutex;
 	boost::condition_variable cond;
 	boost::condition_variable done;
-	std::list<thread_ptr> threads;
+	std::list<ThreadPtr> threads;
 	std::list<boost::function<void (void)> > tasks;
 
-	void runner();
+	void Runner();
 public:
-	thread_pool();
-	~thread_pool();
-	void start();
-	void stop();
-	void set_num(int num);
-	bool push_task(boost::function<void (void)> task);
+	ThreadPool();
+	~ThreadPool();
+	void Start();
+	void Stop();
+	void SetNum(int num);
+	bool PushTask(boost::function<void (void)> task);
 };
 
-typedef boost::shared_ptr<thread_pool> thread_pool_ptr;
-} // namespace hainan
+typedef boost::shared_ptr<ThreadPool> ThreadPoolPtr;
+} // namespace Hainan
 #endif // THREAD_THREAD_POOL_H

+ 7 - 7
src/thread/thread_pool_test.cc → src/Thread/ThreadPoolTest.cc

@@ -3,22 +3,22 @@
 #include <gtest/gtest.h>
 #include <gflags/gflags.h>
 #include <glog/logging.h>
-#include "thread/thread_pool.h"
+#include "Thread/ThreadPool.h"
 
-namespace hainan {
+namespace Hainan {
 
 class thread_pool_test: public testing::Test
 {
 	void SetUp()
 	{
-		pool.set_num(10);
-		pool.start();
+		pool.SetNum(10);
+		pool.Start();
 	}
 	void TearDown()
 	{
 	}
 protected:
-	thread_pool pool;
+	ThreadPool pool;
 public:
 	thread_pool_test() :
 		pool()
@@ -40,13 +40,13 @@ TEST_F(thread_pool_test, Test1)
 		pool.push_task(
 				bind(&thread_pool_test::max, this, x[i], y[i], &z[i]));
 	}
-	pool.stop();
+	pool.Stop();
 	for (int i = 0; i < 100; ++i)
 	{
 		ASSERT_EQ(9, z[i])<< "i = " << i;
 	}
 }
-} // namespace hainan
+} // namespace Hainan
 
 int main(int argc, char* argv[])
 {

+ 0 - 56
src/net/asyn_server.cc

@@ -1,56 +0,0 @@
-#include <boost/foreach.hpp>
-#include <boost/bind.hpp>
-#include "base/base.h"
-#include "net/asyn_server.h"
-
-namespace hainan {
-
-asyn_server::asyn_server(const std::string& address, const std::string& port) :
-	io_service_(), acceptor_(io_service_),
-	connections_(), new_connection_(new connection(io_service_, connections_))
-{
-	boost::asio::ip::tcp::resolver resolver(io_service_);
-	boost::asio::ip::tcp::resolver::query query(address, port);
-	boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
-	acceptor_.open(endpoint.protocol());
-	acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
-	acceptor_.bind(endpoint);
-	acceptor_.listen();
-	acceptor_.async_accept(new_connection_->socket(),
-			boost::bind(&asyn_server::handle_accept, this,
-					boost::asio::placeholders::error));
-}
-
-void asyn_server::handle_accept(const boost::system::error_code& e)
-{
-	if (!e)
-	{
-		connections_.insert(new_connection_);
-		new_connection_.reset(new connection(io_service_, connections_));
-		acceptor_.async_accept(new_connection_->socket(),
-				boost::bind(&asyn_server::handle_accept, this,
-						boost::asio::placeholders::error));
-	}
-}
-
-void asyn_server::handle_stop()
-{
-	acceptor_.close();
-	foreach(connection_ptr connection, connections_)
-	{
-		connection->stop();
-	}
-	connections_.clear();
-}
-
-void asyn_server::start()
-{
-	io_service_.run();
-}
-
-void asyn_server::stop()
-{
-	io_service_.post(boost::bind(&asyn_server::handle_stop, this));
-}
-
-} // namespace hainan

+ 0 - 30
src/net/asyn_server.h

@@ -1,30 +0,0 @@
-#ifndef NET_ASYNSERVER_H
-#define NET_ASYNSERVER_H
-
-#include <string>
-#include <boost/asio.hpp>
-#include <boost/noncopyable.hpp>
-#include "net/connection.h"
-
-namespace hainan {
-
-class asyn_server: private boost::noncopyable
-{
-private:
-	// hold all connection
-	connection_set connections_;
-	boost::asio::io_service io_service_;
-	boost::asio::ip::tcp::acceptor acceptor_;
-	connection_ptr new_connection_;
-
-	void handle_accept(const boost::system::error_code& e);
-	void handle_stop();
-public:
-	explicit asyn_server(const std::string& address, const std::string& port);
-	void start();
-	void stop();
-};
-
-} // namespace hainan
-
-#endif // NET_ASYNSERVER_H

+ 0 - 0
src/net/asyn_server_test.cc


+ 0 - 31
src/net/connection.cc

@@ -1,31 +0,0 @@
-#include <vector>
-#include <boost/bind.hpp>
-#include "net/connection.h"
-
-namespace hainan {
-
-connection::connection(boost::asio::io_service& io_service,
-		connection_set& manager):
-	socket_(io_service), connections_(manager)
-{
-}
-
-boost::asio::ip::tcp::socket& connection::socket()
-{
-	return socket_;
-}
-
-void connection::start()
-{
-	socket_.async_read_some(boost::asio::buffer(buffer_),
-			boost::bind(&connection::handle_read, shared_from_this(),
-					boost::asio::placeholders::error,
-					boost::asio::placeholders::bytes_transferred));
-}
-
-void connection::stop()
-{
-	socket_.close();
-}
-
-} // namespace hainan

+ 0 - 38
src/net/connection.h

@@ -1,38 +0,0 @@
-#ifndef NET_CONNECTION_H
-#define NET_CONNECTION_H
-
-#include <string>
-#include <boost/asio.hpp>
-#include <boost/array.hpp>
-#include <boost/noncopyable.hpp>
-#include <boost/shared_ptr.hpp>
-#include <boost/unordered_set.hpp>
-#include <boost/enable_shared_from_this.hpp>
-
-namespace hainan {
-
-class connection;
-typedef boost::shared_ptr<connection> connection_ptr;
-typedef boost::unordered_set<connection_ptr> connection_set;
-
-class connection: private boost::noncopyable,
-		public boost::enable_shared_from_this<connection>
-{
-private:
-	boost::asio::ip::tcp::socket socket_;
-	connection_set& connections_;
-	boost::array<char, 8192> buffer_;
-public:
-	explicit connection(boost::asio::io_service& io_service,
-			connection_set& manager);
-	boost::asio::ip::tcp::socket& socket();
-	void start();
-	void stop();
-	virtual void handle_read(const boost::system::error_code& e,
-			size_t bytes_transferred) = 0;
-	virtual void handle_write(const boost::system::error_code& e) = 0;
-};
-
-} // namespace hainan
-
-#endif // NET_CONNECTION_H

+ 0 - 93
src/net/connection_test.cc

@@ -1,93 +0,0 @@
-#include <gtest/gtest.h>
-#include <gflags/gflags.h>
-#include "net/connection.h"
-
-namespace hainan {
-
-static const char address[] = "127.0.0.1";
-static int port = 10000;
-class connection1: public connection
-{
-public:
-	std::string content;
-	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,
-			size_t bytes_transferred)
-	{
-		if (!e)
-		{
-			content = buffer_.c_array();
-			bytes = bytes_transferred;
-		}
-	}
-
-	void handle_write()
-	{
-
-	}
-};
-
-class ConnectionTest: public testing::Test
-{
-private:
-	boost::asio::io_service io_service_;
-	boost::asio::ip::tcp::acceptor acceptor_;
-
-	connection_set connections_;
-	connection_ptr connection_;
-
-	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)
-{
-	server_start();
-	client_connect();
-	io_service_.run();
-	ASSERT_EQ(11, bytes_transferred);
-	ASSERT_EQ("test string", content);
-}
-
-} // namespace hainan

+ 0 - 29
src/net/rpc_callback.cc

@@ -1,29 +0,0 @@
-#include <google/protobuf/service.h>
-#include <google/protobuf/message.h>
-#include "net/rpc_callback.h"
-
-namespace hainan {
-
-rpc_callback::rpc_callback(google::protobuf::RpcController* controller,
-		google::protobuf::Message* response,
-		google::protobuf::Closure* done):
-		controller_(controller), response_(response), done_(done)
-{
-}
-
-google::protobuf::RpcController* rpc_callback::controller() const
-{
-    return controller_;
-}
-
-google::protobuf::Closure* rpc_callback::done() const
-{
-    return done_;
-}
-
-google::protobuf::Message* rpc_callback::response() const
-{
-    return response_;
-}
-
-} // hainan

+ 0 - 29
src/net/rpc_callback.h

@@ -1,29 +0,0 @@
-#ifndef NET_RPC_CALLBACK_H
-#define NET_RPC_CALLBACK_H
-
-namespace hainan {
-
-class google::protobuf::RpcController;
-class google::protobuf::Message;
-class google::protobuf::Closure;
-
-class rpc_callback
-{
-private:
-	google::protobuf::RpcController* controller_;
-	google::protobuf::Message* response_;
-	google::protobuf::Closure* done_;
-
-public:
-	rpc_callback(google::protobuf::RpcController* controller,
-			google::protobuf::Message* response,
-			google::protobuf::Closure* done);
-	~rpc_callback();
-    google::protobuf::RpcController*controller() const;
-    google::protobuf::Closure* done() const;
-    google::protobuf::Message* response() const;
-};
-
-} // hainan
-
-#endif // NET_RPC_CALLBACK_H