Browse Source

增加了asyn_server类,connection类,去除了原来的epoller类,
网络部分打算封装boost asio

tanghai 15 years ago
parent
commit
56de61de76

+ 38 - 0
src/net/asyn_server.cc

@@ -0,0 +1,38 @@
+#include <boost/foreach.hpp>
+#include "base/base.h"
+#include "net/asyn_server.h"
+#include "net/connection.h"
+
+namespace hainan {
+
+explicit asyn_server::asyn_server(
+		const string & address, const string & port):
+		io_service(), acceptor(io_service),
+		new_connection(new connection())
+{
+}
+
+void asyn_server::handle_accept(const system::error_code & e)
+{
+}
+
+void asyn_server::handle_stop()
+{
+	acceptor.close();
+	foreach(connection_ptr connection, all_connections)
+	{
+		connection.stop();
+	}
+}
+
+void asyn_server::start()
+{
+	io_service.run();
+}
+
+void asyn_server::stop()
+{
+	io_service.post(bind(&asyn_server::handle_stop(), this));
+}
+
+} // namespace hainan

+ 33 - 0
src/net/asyn_server.h

@@ -0,0 +1,33 @@
+#ifndef NET_ASYNSERVER_H
+#define NET_ASYNSERVER_H
+
+#include <string>
+#include <boost/asio.hpp>
+#include <boost/noncopyable.hpp>
+
+namespace hainan {
+
+using namespace std;
+using namespace boost;
+
+class asyn_server: private noncopyable
+{
+private:
+	typedef unordered_set<connection> connection_set;
+
+	// hold all connection
+	connectionSet all_connections;
+	asio::io_service io_service;
+	asio::ip::tcp::acceptor acceptor;
+	connection_ptr new_connection;
+
+	void handle_accept(const system::error_code& e);
+	void handle_stop();
+public:
+	explicit asyn_server(const string& address, const string& port);
+	void start();
+	void stop();
+};
+} // hainan
+
+#endif // NET_ASYNSERVER_H

+ 0 - 0
src/net/epoller.cc → src/net/asyn_server_test.cc


+ 0 - 0
src/net/epoller_test.cc → src/net/connection.cc


+ 34 - 0
src/net/connection.h

@@ -0,0 +1,34 @@
+#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>
+
+namespace hainan {
+
+using namespace std;
+using namespace boost;
+
+class connection: private noncopyable
+{
+private:
+	typedef unordered_set<connection> connection_set;
+
+	asio::ip::tcp::socket socket;
+	connection_set& all_connections;
+	boost::array<char, 8192> buffer;
+public:
+	explicit connection(asio::io_service& io_service);
+	void start();
+	void stop();
+	virtual void handle_read();
+	virtual void handle_writer();
+};
+typedef shared_ptr<connection> connection_ptr;
+
+} // namespace hainan
+
+#endif // NET_CONNECTION_H

+ 0 - 0
src/net/connection_test.cc


+ 0 - 19
src/net/epoller.h

@@ -1,19 +0,0 @@
-// author: tanghai
-
-#include <boost/function.hpp>
-#include <boost/unordered_map.hpp>
-
-namespace hainan {
-
-using namespace boost;
-typedef unordered_map< int, function<void (void)> > HandlerMap;
-
-class Epoller: private noncopyable
-{
-private:
-	HandlerMap handlers;
-public:
-	void Register();
-	void Add();
-};
-} // hainan