transactd 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. checksums.yaml +7 -0
  2. data/BUILD_UNIX-JA +174 -0
  3. data/BUILD_WIN-JA +256 -0
  4. data/CMakeLists.txt +96 -0
  5. data/COPYING +339 -0
  6. data/README +406 -0
  7. data/README-JA +424 -0
  8. data/bin/common/tdclc_32_1_0.dll +0 -0
  9. data/bin/common/tdclc_64_1_0.dll +0 -0
  10. data/build/common/check_for_link_iconv.cmake +73 -0
  11. data/build/common/copyifgreater.cmd +30 -0
  12. data/build/common/copyifgreater.js +290 -0
  13. data/build/common/get_boost_libs.cmake +106 -0
  14. data/build/common/get_ruby_path.cmake +115 -0
  15. data/build/common/options.cmake +127 -0
  16. data/build/common/smart_install.cmake +263 -0
  17. data/build/common/system.cmake +122 -0
  18. data/build/common/transactd.rc.in +52 -0
  19. data/build/common/transactd_cl_common.cmake +101 -0
  20. data/build/common/transactd_cl_output.cmake +93 -0
  21. data/build/common/transactd_common.cmake +237 -0
  22. data/build/common/transactd_required.cmake +28 -0
  23. data/build/swig/ruby/generate.cmake.in +35 -0
  24. data/build/swig/ruby/generate.cmd.in +19 -0
  25. data/build/swig/ruby/ruby.swg +101 -0
  26. data/build/swig/tdcl.i +188 -0
  27. data/build/tdclc/BUILDNUMBER.txt +1 -0
  28. data/build/tdclc/CMakeLists.txt +170 -0
  29. data/build/tdclc/tdclc_32.cbproj +181 -0
  30. data/build/tdclc/tdclc_64.cbproj +205 -0
  31. data/build/tdclcpp/BUILDNUMBER.txt +1 -0
  32. data/build/tdclcpp/CMakeLists.txt +142 -0
  33. data/build/tdclcpp/tdclcpp_bcb_32.cbproj +239 -0
  34. data/build/tdclcpp/tdclcpp_bcb_64.cbproj +304 -0
  35. data/build/tdclrb/BUILDNUMBER.txt +1 -0
  36. data/build/tdclrb/CMakeLists.txt +258 -0
  37. data/build/tdclrb/GEM_VERSION +3 -0
  38. data/build/tdclrb/bldgem/extconf.rb +123 -0
  39. data/build/tdclrb/gem/INSTALLLOG.win32 +9 -0
  40. data/build/tdclrb/gem/Makefile.win32-VS +65 -0
  41. data/build/tdclrb/gem/Makefile.win32-prebuilt +48 -0
  42. data/build/tdclrb/gem/detect.rb +31 -0
  43. data/build/tdclrb/gem/helper.rb +113 -0
  44. data/build/tdclrb/gem/transactd.rb +22 -0
  45. data/build/tdclrb/gem_output.cmake +44 -0
  46. data/source/bzs/db/IBlobBuffer.h +51 -0
  47. data/source/bzs/db/blobBuffer.h +177 -0
  48. data/source/bzs/db/blobStructs.h +85 -0
  49. data/source/bzs/db/engine/mysql/IReadRecords.h +52 -0
  50. data/source/bzs/db/engine/mysql/bookmark.h +195 -0
  51. data/source/bzs/db/engine/mysql/database.cpp +1882 -0
  52. data/source/bzs/db/engine/mysql/database.h +465 -0
  53. data/source/bzs/db/engine/mysql/dbManager.cpp +303 -0
  54. data/source/bzs/db/engine/mysql/dbManager.h +143 -0
  55. data/source/bzs/db/engine/mysql/errorMessage.cpp +75 -0
  56. data/source/bzs/db/engine/mysql/errorMessage.h +43 -0
  57. data/source/bzs/db/engine/mysql/fieldAccess.h +158 -0
  58. data/source/bzs/db/engine/mysql/mydebuglog.cpp +349 -0
  59. data/source/bzs/db/engine/mysql/mydebuglog.h +89 -0
  60. data/source/bzs/db/engine/mysql/mysqlInternal.h +171 -0
  61. data/source/bzs/db/engine/mysql/mysqlThd.cpp +169 -0
  62. data/source/bzs/db/engine/mysql/mysqlThd.h +35 -0
  63. data/source/bzs/db/engine/mysql/percentageKey.h +260 -0
  64. data/source/bzs/db/protocol/ICommandExecuter.h +49 -0
  65. data/source/bzs/db/protocol/hs/hsCommandExecuter.cpp +689 -0
  66. data/source/bzs/db/protocol/hs/hsCommandExecuter.h +228 -0
  67. data/source/bzs/db/protocol/tdap/btrDate.cpp +437 -0
  68. data/source/bzs/db/protocol/tdap/btrDate.h +227 -0
  69. data/source/bzs/db/protocol/tdap/client/bulkInsert.h +127 -0
  70. data/source/bzs/db/protocol/tdap/client/client.cpp +106 -0
  71. data/source/bzs/db/protocol/tdap/client/client.h +292 -0
  72. data/source/bzs/db/protocol/tdap/client/connMgr.cpp +144 -0
  73. data/source/bzs/db/protocol/tdap/client/connMgr.h +82 -0
  74. data/source/bzs/db/protocol/tdap/client/database.cpp +863 -0
  75. data/source/bzs/db/protocol/tdap/client/database.h +118 -0
  76. data/source/bzs/db/protocol/tdap/client/databaseFactory.cpp +100 -0
  77. data/source/bzs/db/protocol/tdap/client/dbDef.cpp +1640 -0
  78. data/source/bzs/db/protocol/tdap/client/dbDef.h +135 -0
  79. data/source/bzs/db/protocol/tdap/client/dllmain.cpp +434 -0
  80. data/source/bzs/db/protocol/tdap/client/errorMessage.cpp +92 -0
  81. data/source/bzs/db/protocol/tdap/client/errorMessage_ja.cpp +98 -0
  82. data/source/bzs/db/protocol/tdap/client/fieldDDF.cpp +174 -0
  83. data/source/bzs/db/protocol/tdap/client/fieldDDF.h +91 -0
  84. data/source/bzs/db/protocol/tdap/client/fileDDF.cpp +140 -0
  85. data/source/bzs/db/protocol/tdap/client/fileDDF.h +86 -0
  86. data/source/bzs/db/protocol/tdap/client/filter.cpp +527 -0
  87. data/source/bzs/db/protocol/tdap/client/filter.h +154 -0
  88. data/source/bzs/db/protocol/tdap/client/indexDDF.cpp +137 -0
  89. data/source/bzs/db/protocol/tdap/client/indexDDF.h +84 -0
  90. data/source/bzs/db/protocol/tdap/client/nsDatabase.cpp +724 -0
  91. data/source/bzs/db/protocol/tdap/client/nsDatabase.h +123 -0
  92. data/source/bzs/db/protocol/tdap/client/nsTable.cpp +899 -0
  93. data/source/bzs/db/protocol/tdap/client/nsTable.h +199 -0
  94. data/source/bzs/db/protocol/tdap/client/request.h +198 -0
  95. data/source/bzs/db/protocol/tdap/client/sharedData.cpp +58 -0
  96. data/source/bzs/db/protocol/tdap/client/sharedData.h +56 -0
  97. data/source/bzs/db/protocol/tdap/client/sqlBuilder.cpp +574 -0
  98. data/source/bzs/db/protocol/tdap/client/sqlBuilder.h +53 -0
  99. data/source/bzs/db/protocol/tdap/client/stringConverter.h +627 -0
  100. data/source/bzs/db/protocol/tdap/client/table.cpp +2613 -0
  101. data/source/bzs/db/protocol/tdap/client/table.h +221 -0
  102. data/source/bzs/db/protocol/tdap/client/trdboostapi.h +1096 -0
  103. data/source/bzs/db/protocol/tdap/client/trdboostapiInternal.h +179 -0
  104. data/source/bzs/db/protocol/tdap/client/trdclcppautolink.h +40 -0
  105. data/source/bzs/db/protocol/tdap/client/trnsctcl.def +11 -0
  106. data/source/bzs/db/protocol/tdap/myDateTime.cpp +500 -0
  107. data/source/bzs/db/protocol/tdap/mysql/characterset.cpp +184 -0
  108. data/source/bzs/db/protocol/tdap/mysql/characterset.h +60 -0
  109. data/source/bzs/db/protocol/tdap/mysql/databaseSchema.cpp +284 -0
  110. data/source/bzs/db/protocol/tdap/mysql/databaseSchema.h +53 -0
  111. data/source/bzs/db/protocol/tdap/mysql/debuglog.cpp +383 -0
  112. data/source/bzs/db/protocol/tdap/mysql/debuglog.h +106 -0
  113. data/source/bzs/db/protocol/tdap/mysql/recordsetReader.h +680 -0
  114. data/source/bzs/db/protocol/tdap/mysql/request.h +202 -0
  115. data/source/bzs/db/protocol/tdap/mysql/tdapCommandExecuter.cpp +1020 -0
  116. data/source/bzs/db/protocol/tdap/mysql/tdapCommandExecuter.h +141 -0
  117. data/source/bzs/db/protocol/tdap/tdapRequest.h +190 -0
  118. data/source/bzs/db/protocol/tdap/tdapSchema.cpp +295 -0
  119. data/source/bzs/db/protocol/tdap/tdapSchema.h +558 -0
  120. data/source/bzs/db/protocol/tdap/tdapcapi.h +423 -0
  121. data/source/bzs/db/transactd/appBuilderImple.h +55 -0
  122. data/source/bzs/db/transactd/appModule.cpp +183 -0
  123. data/source/bzs/db/transactd/appModule.h +80 -0
  124. data/source/bzs/db/transactd/connManager.cpp +201 -0
  125. data/source/bzs/db/transactd/connManager.h +60 -0
  126. data/source/bzs/db/transactd/connectionRecord.h +69 -0
  127. data/source/bzs/db/transactd/transactd.cpp +325 -0
  128. data/source/bzs/env/compiler.h +135 -0
  129. data/source/bzs/env/crosscompile.cpp +130 -0
  130. data/source/bzs/env/crosscompile.h +150 -0
  131. data/source/bzs/env/fileopen.h +36 -0
  132. data/source/bzs/env/mbcswchrLinux.cpp +40 -0
  133. data/source/bzs/env/mbcswchrLinux.h +88 -0
  134. data/source/bzs/env/tstring.h +183 -0
  135. data/source/bzs/example/changeSchema.cpp +117 -0
  136. data/source/bzs/example/changeSchema_c.cpp +78 -0
  137. data/source/bzs/example/connection_pool_c.cpp +171 -0
  138. data/source/bzs/example/createDatabase.cpp +305 -0
  139. data/source/bzs/example/createDatabase_c.cpp +202 -0
  140. data/source/bzs/example/deleteRecords.cpp +87 -0
  141. data/source/bzs/example/deleteRecords_c.cpp +57 -0
  142. data/source/bzs/example/dropDatabase.cpp +59 -0
  143. data/source/bzs/example/dropDatabase_c.cpp +34 -0
  144. data/source/bzs/example/insertRecords.cpp +212 -0
  145. data/source/bzs/example/insertRecords_c.cpp +153 -0
  146. data/source/bzs/example/readRecords.cpp +141 -0
  147. data/source/bzs/example/readRecords_c.cpp +107 -0
  148. data/source/bzs/example/updateRecords.cpp +99 -0
  149. data/source/bzs/example/updateRecords_c.cpp +71 -0
  150. data/source/bzs/example/update_with_transaction.cpp +104 -0
  151. data/source/bzs/example/update_with_transaction_c.cpp +80 -0
  152. data/source/bzs/netsvc/client/tcpClient.cpp +226 -0
  153. data/source/bzs/netsvc/client/tcpClient.h +489 -0
  154. data/source/bzs/netsvc/server/IAppModule.h +94 -0
  155. data/source/bzs/netsvc/server/iserver.h +65 -0
  156. data/source/bzs/netsvc/server/serverCpt.cpp +522 -0
  157. data/source/bzs/netsvc/server/serverCpt.h +88 -0
  158. data/source/bzs/netsvc/server/serverPipe.cpp +705 -0
  159. data/source/bzs/netsvc/server/serverPipe.h +96 -0
  160. data/source/bzs/netsvc/server/serverTpool.cpp +416 -0
  161. data/source/bzs/netsvc/server/serverTpool.h +84 -0
  162. data/source/bzs/rtl/benchmark.cpp +96 -0
  163. data/source/bzs/rtl/benchmark.h +65 -0
  164. data/source/bzs/rtl/datetime.cpp +375 -0
  165. data/source/bzs/rtl/datetime.h +53 -0
  166. data/source/bzs/rtl/debuglog.cpp +106 -0
  167. data/source/bzs/rtl/debuglog.h +97 -0
  168. data/source/bzs/rtl/exception.h +116 -0
  169. data/source/bzs/rtl/stl_uty.cpp +35 -0
  170. data/source/bzs/rtl/stl_uty.h +29 -0
  171. data/source/bzs/rtl/stringBuffers.cpp +101 -0
  172. data/source/bzs/rtl/stringBuffers.h +58 -0
  173. data/source/bzs/rtl/strtrim.cpp +135 -0
  174. data/source/bzs/rtl/strtrim.h +46 -0
  175. data/source/bzs/test/tdclatl/bench_tdclatl.js +445 -0
  176. data/source/bzs/test/tdclrb/bench_tdclcpp.rb +375 -0
  177. data/source/bzs/test/tdclrb/prepare.rb +226 -0
  178. data/source/bzs/test/tdclrb/transactd_datetime_spec.rb +172 -0
  179. data/source/bzs/test/tdclrb/transactd_kanjischema_spec.rb +208 -0
  180. data/source/bzs/test/tdclrb/transactd_spec.rb +1536 -0
  181. data/source/bzs/test/transactdBench/transactdBench.cpp +430 -0
  182. data/source/bzs/test/transactdBench/transactdBench2.cpp +342 -0
  183. data/source/bzs/test/trdclengn/test_trdclengn.cpp +2030 -0
  184. data/source/global/tdclatl/Database.cpp +503 -0
  185. data/source/global/tdclatl/Database.h +139 -0
  186. data/source/global/tdclatl/DbDef.cpp +242 -0
  187. data/source/global/tdclatl/DbDef.h +79 -0
  188. data/source/global/tdclatl/Field.cpp +92 -0
  189. data/source/global/tdclatl/Field.h +59 -0
  190. data/source/global/tdclatl/FieldDef.cpp +238 -0
  191. data/source/global/tdclatl/FieldDef.h +87 -0
  192. data/source/global/tdclatl/Flags.cpp +111 -0
  193. data/source/global/tdclatl/Flags.h +55 -0
  194. data/source/global/tdclatl/KeyDef.cpp +51 -0
  195. data/source/global/tdclatl/KeyDef.h +55 -0
  196. data/source/global/tdclatl/KeySegment.cpp +55 -0
  197. data/source/global/tdclatl/Table.cpp +600 -0
  198. data/source/global/tdclatl/Table.h +144 -0
  199. data/source/global/tdclatl/TableDef.cpp +291 -0
  200. data/source/global/tdclatl/TableDef.h +86 -0
  201. data/source/global/tdclatl/TdVersion.cpp +74 -0
  202. data/source/global/tdclatl/TdVersion.h +54 -0
  203. data/source/global/tdclatl/_IDatabaseEvents_CP.h +113 -0
  204. data/source/global/tdclatl/dllmain.cpp +30 -0
  205. data/source/global/tdclatl/dllmain.h +27 -0
  206. data/source/global/tdclatl/keySegment.h +57 -0
  207. data/source/global/tdclatl/resource.h +0 -0
  208. data/source/global/tdclatl/stdafx.cpp +2 -0
  209. data/source/global/tdclatl/stdafx.h +25 -0
  210. data/source/global/tdclatl/targetver.h +4 -0
  211. data/source/global/tdclatl/tdclatl.cpp +68 -0
  212. data/source/global/tdclatl/tdclatl.def +10 -0
  213. data/source/global/tdclatl/tdclatl.idl +1035 -0
  214. data/source/linux/charsetConvert.h +112 -0
  215. data/source/linux/linuxTypes.h +33 -0
  216. data/source/linux/tchar.h +428 -0
  217. data/transactd.gemspec +97 -0
  218. metadata +267 -0
@@ -0,0 +1,96 @@
1
+ /*=================================================================
2
+ Copyright (C) 2012 2013 BizStation Corp All rights reserved.
3
+
4
+ This program is free software; you can redistribute it and/or
5
+ modify it under the terms of the GNU General Public License
6
+ as published by the Free Software Foundation; either version 2
7
+ of the License, or (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program; if not, write to the Free Software
16
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17
+ 02111-1307, USA.
18
+ =================================================================*/
19
+ #ifndef BZS_NETSVC_SERVER_SERVERPIPE_H
20
+ #define BZS_NETSVC_SERVER_SERVERPIPE_H
21
+
22
+ #include "iserver.h"
23
+ #include <string>
24
+ #include <vector>
25
+ #include <boost/noncopyable.hpp>
26
+ #include <boost/shared_ptr.hpp>
27
+ #include <boost/asio.hpp>
28
+ #include <exception>
29
+
30
+
31
+ typedef HANDLE platform_descriptor;
32
+ typedef boost::asio::windows::stream_handle platform_stream;
33
+
34
+
35
+ namespace boost{class condition_variable;}
36
+
37
+ namespace bzs
38
+ {
39
+ namespace netsvc
40
+ {
41
+ namespace server
42
+ {
43
+ class IAppModuleBuilder;
44
+ extern ::boost::condition_variable condition;
45
+
46
+ namespace pipe
47
+ {
48
+ extern unsigned int g_connections;
49
+ extern unsigned int g_waitThread;
50
+
51
+
52
+ class acceptor : private boost::noncopyable
53
+ {
54
+ platform_descriptor m_fd;
55
+ std::string m_pipeName;
56
+ bool m_cancel;
57
+
58
+ public:
59
+ acceptor();
60
+ void open(const std::string& pipeName);
61
+ void cancel();
62
+ void accept(platform_stream& pipe);
63
+ std::string& name(){return m_pipeName;};
64
+ };
65
+
66
+ class connection;
67
+
68
+ class server :public iserver, private boost::noncopyable
69
+ {
70
+ boost::shared_ptr<IAppModuleBuilder> m_app;
71
+ boost::asio::io_service m_ios;
72
+ acceptor m_acceptor;
73
+ boost::shared_ptr<connection> m_newConnection;
74
+ const std::size_t m_maxConnections;
75
+
76
+ bool m_stopped;
77
+ void doStop();
78
+ void doClose();
79
+ void run();
80
+ void onAccept(const boost::system::error_code& e);
81
+ public:
82
+ server( boost::shared_ptr<IAppModuleBuilder>, const std::string& name
83
+ , std::size_t max_connections, unsigned int shareMemSize, const char* hostCheckName);
84
+ void start();
85
+ void stop();
86
+ void registerErrorHandler(inotifyHandler* eh){erh = eh;};
87
+ static inotifyHandler* erh;
88
+
89
+ };
90
+
91
+ }//namespace pipe
92
+ }//namespace server
93
+ }//namespace netsvc
94
+ }//namespace bzs
95
+ #endif //BZS_NETSVC_SERVER_SERVERPIPE_H
96
+
@@ -0,0 +1,416 @@
1
+ /*=================================================================
2
+ Copyright (C) 2012 2013 BizStation Corp All rights reserved.
3
+
4
+ This program is free software; you can redistribute it and/or
5
+ modify it under the terms of the GNU General Public License
6
+ as published by the Free Software Foundation; either version 2
7
+ of the License, or (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program; if not, write to the Free Software
16
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17
+ 02111-1307, USA.
18
+ =================================================================*/
19
+ #include "serverTpool.h"
20
+ #include <boost/bind.hpp>
21
+ #include <boost/array.hpp>
22
+ #include <boost/shared_ptr.hpp>
23
+ #include <boost/scoped_array.hpp>
24
+ #include <boost/thread.hpp>
25
+ #include <boost/asio/write.hpp>
26
+ #include <boost/thread/xtime.hpp>
27
+ #include <algorithm>
28
+ #include <boost/enable_shared_from_this.hpp>
29
+
30
+ #include <bzs/rtl/debuglog.h>
31
+ #include <bzs/env/crosscompile.h>
32
+ #include "IAppModule.h"
33
+
34
+ using namespace boost;
35
+ using namespace boost::asio;
36
+ using namespace boost::asio::ip;
37
+
38
+
39
+
40
+ #define READBUF_SIZE 66000
41
+ #define WRITEBUF_SIZE 66000
42
+
43
+ namespace bzs
44
+ {
45
+ namespace netsvc
46
+ {
47
+ namespace server
48
+ {
49
+ namespace tpool
50
+ {
51
+
52
+ unsigned int g_connections = 0;
53
+
54
+ // ---------------------------------------------------------------------------
55
+ // connection
56
+ // ---------------------------------------------------------------------------
57
+
58
+ /** Asynchronous thread pool server
59
+ * In transaction then use one thread. that like connection per thread.
60
+ *
61
+ */
62
+ class connection : public boost::enable_shared_from_this<connection>,
63
+ public iconnection, private boost::noncopyable
64
+ {
65
+ static mutex m_mutex;
66
+ io_service& m_ios;
67
+ boost::asio::ip::tcp::socket m_socket;
68
+ shared_ptr<IAppModule> m_module;
69
+ std::vector<char> m_buffer;
70
+ std::vector<char> m_result;
71
+ buffers m_optionalBuffes;
72
+
73
+ size_t m_readLen;
74
+ char* m_statck;
75
+
76
+ bool syncReadWrite()
77
+ {
78
+ bool complete = false;
79
+ boost::system::error_code e;
80
+ m_readLen = 0;
81
+ while (1)
82
+ {
83
+ m_readLen += boost::asio::read(m_socket, boost::asio::buffer(&m_buffer[m_readLen], m_buffer.size()-m_readLen)
84
+ ,boost::asio::transfer_at_least(4), e);
85
+ if (e) return false;
86
+
87
+ size_t n = m_module->onRead((const char*)&m_buffer[0], m_readLen, complete);
88
+ if (!complete)
89
+ {
90
+ if (n > m_buffer.size()) m_buffer.resize(n);
91
+ m_readLen += boost::asio::read(m_socket, boost::asio::buffer(&m_buffer[m_readLen], n-m_readLen)
92
+ , boost::asio::transfer_all(), e);
93
+ if (e) return false;
94
+ n = m_module->onRead((const char*)&m_buffer[0], m_readLen, complete);
95
+ }
96
+ if (complete)
97
+ {
98
+ m_readLen = 0;
99
+ size_t size=0;
100
+ m_optionalBuffes.clear();
101
+ int ret = m_module->execute(&m_result[0], size, &m_optionalBuffes);
102
+
103
+ if (ret == EXECUTE_RESULT_QUIT)
104
+ return false;
105
+ m_optionalBuffes.insert(m_optionalBuffes.begin(), buffer(&m_result[0], size));
106
+ if (ret == EXECUTE_RESULT_FORCSE_ASYNC)
107
+ return true;
108
+ else
109
+ {
110
+ m_readLen = 0;
111
+ DEBUG_PROFILE_START(1)
112
+
113
+ boost::asio::write(m_socket, m_optionalBuffes, boost::asio::transfer_all(), e);
114
+ if (e) return false;
115
+ }
116
+ }
117
+ }
118
+ }
119
+
120
+ void handle_read(const boost::system::error_code& e, std::size_t bytes_transferred)
121
+ {
122
+ DEBUG_PROFILE_START(m_readLen==0)
123
+ if (!e)
124
+ {
125
+ bool complete = false;
126
+
127
+ if (bytes_transferred==0)
128
+ return ;
129
+ m_readLen += bytes_transferred;
130
+ size_t n = m_module->onRead(&m_buffer[0], m_readLen, complete);
131
+ if (complete)
132
+ {
133
+ DEBUG_PROFILE_END(1, "handle_read")
134
+ size_t size=0;
135
+ m_optionalBuffes.clear();
136
+ int ret = m_module->execute(&m_result[0], size, &m_optionalBuffes);
137
+
138
+ if (ret == EXECUTE_RESULT_QUIT)
139
+ return ;
140
+
141
+ m_optionalBuffes.insert(m_optionalBuffes.begin(), buffer(&m_result[0], size));
142
+ if (ret == EXECUTE_RESULT_FORCSE_SYNC)
143
+ {
144
+ boost::system::error_code ec;
145
+ boost::asio::write(m_socket, m_optionalBuffes, boost::asio::transfer_all(), ec);
146
+ if (ec) return ;
147
+ syncReadWrite();
148
+ }
149
+ DEBUG_PROFILE_START(1)
150
+ async_write(m_socket, m_optionalBuffes, boost::bind(&connection::handle_write, shared_from_this(),
151
+ boost::asio::placeholders::error));
152
+
153
+ return;
154
+ }
155
+
156
+ if (n > m_buffer.size())
157
+ m_buffer.resize(n);
158
+ m_socket.async_read_some(buffer(&m_buffer[m_readLen], m_buffer.size()-m_readLen),
159
+ boost::bind(&connection::handle_read, shared_from_this(),
160
+ boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
161
+ }
162
+ }
163
+
164
+ void handle_write(const boost::system::error_code& e)
165
+ {
166
+ if (!e)
167
+ {
168
+ DEBUG_PROFILE_END(1, "write")
169
+ if (m_module->isShutDown())
170
+ return;
171
+ start();
172
+ }
173
+ }
174
+
175
+
176
+ public:
177
+ explicit connection(io_service& ios):m_socket(ios),m_ios(ios)
178
+ {
179
+ m_buffer.resize(READBUF_SIZE);
180
+ m_result.resize(WRITEBUF_SIZE);
181
+
182
+ mutex::scoped_lock lck(m_mutex);
183
+ connections.push_back(this);
184
+ ++g_connections;
185
+
186
+
187
+ }
188
+
189
+ ~connection()
190
+ {
191
+ if (connections.size())
192
+ {
193
+ mutex::scoped_lock lck(m_mutex);
194
+ std::vector<connection*>::iterator it = find(connections.begin(), connections.end(), this);
195
+ if (it != connections.end())
196
+ {
197
+ connections.erase( it);
198
+ --g_connections;
199
+ }
200
+ }
201
+ }
202
+
203
+ boost::asio::ip::tcp::socket& socket(){return m_socket;}
204
+
205
+ void sendConnectAccept()
206
+ {
207
+ size_t n = m_module->onAccept(&m_result[0], WRITEBUF_SIZE);
208
+ if (n)
209
+ boost::asio::write(m_socket, buffer(&m_result[0], n), boost::asio::transfer_all());
210
+ }
211
+
212
+ void start()
213
+ {
214
+ m_readLen = 0;
215
+ m_socket.async_read_some(buffer(&m_buffer[0], m_buffer.size()),
216
+ bind(&connection::handle_read, shared_from_this(), placeholders::error,
217
+ placeholders::bytes_transferred));
218
+ }
219
+
220
+ void setModule(boost::shared_ptr<IAppModule> p)
221
+ {
222
+ m_module = p;
223
+ }
224
+
225
+ void close()
226
+ {
227
+ m_ios.post(boost::bind(&connection::doClose, this));
228
+ }
229
+
230
+ void doClose()
231
+ {
232
+ boost::system::error_code ec;
233
+ m_socket.close(ec);
234
+ }
235
+
236
+ static std::vector<connection* > connections;
237
+
238
+ static void stop()
239
+ {
240
+ mutex::scoped_lock lck(m_mutex);
241
+ boost::system::error_code ec;
242
+ for (size_t i=0;i<connections.size();i++)
243
+ connections[i]->socket().close(ec);
244
+ }
245
+
246
+ };
247
+
248
+ std::vector<connection* > connection::connections;
249
+ mutex connection::m_mutex;
250
+
251
+ // ---------------------------------------------------------------------------
252
+ // listener
253
+ // ---------------------------------------------------------------------------
254
+
255
+ class listener
256
+ {
257
+ boost::shared_ptr<IAppModuleBuilder> m_app;
258
+ boost::asio::ip::tcp::acceptor m_acceptor;
259
+ boost::shared_ptr<connection> m_newConnection;
260
+ server* m_srv;
261
+ public:
262
+ listener(server* srv,
263
+ shared_ptr<IAppModuleBuilder> app, const std::string& address, const std::string& port)
264
+ :m_srv(srv), m_app(app),m_acceptor(srv->ios()),m_newConnection(new connection(srv->ios()))
265
+ {
266
+ tcp::resolver resolver(srv->ios());
267
+ tcp::resolver::query query(address, port, resolver_query_base::numeric_service);
268
+ tcp::endpoint endpoint = *resolver.resolve(query);
269
+
270
+ m_acceptor.open(endpoint.protocol());
271
+ m_acceptor.set_option(tcp::acceptor::reuse_address(true));
272
+ m_acceptor.bind(endpoint);
273
+ m_acceptor.listen();
274
+ m_acceptor.async_accept(m_newConnection->socket()
275
+ , bind(&listener::handle_accept, this, placeholders::error));
276
+ }
277
+
278
+ void handle_accept(const boost::system::error_code& e)
279
+ {
280
+ if (!e)
281
+ {
282
+ const boost::asio::ip::tcp::no_delay nodelay(true);
283
+ m_newConnection->socket().set_option(nodelay);
284
+ boost::system::error_code ec;
285
+ tcp::endpoint endpoint = m_newConnection->socket().remote_endpoint(ec);
286
+ boost::shared_ptr<IAppModule> mod(m_app->createSessionModule(endpoint,m_newConnection.get(), SERVER_TYPE_TPOOL));
287
+ m_newConnection->setModule(mod);
288
+ if (mod->checkHost(m_srv->hostCheckName()))
289
+ {
290
+ m_newConnection->sendConnectAccept();
291
+ m_newConnection->start();
292
+ }
293
+ m_newConnection.reset(new connection(m_srv->ios()));
294
+ m_acceptor.async_accept(m_newConnection->socket(), boost::bind(&listener::handle_accept, this,
295
+ boost::asio::placeholders::error));
296
+ }
297
+ }
298
+
299
+ void cancel()
300
+ {
301
+ boost::system::error_code ec;
302
+ m_acceptor.cancel(ec);
303
+ }
304
+ };
305
+
306
+
307
+ // ---------------------------------------------------------------------------
308
+ // server
309
+ // ---------------------------------------------------------------------------
310
+
311
+ static boost::shared_ptr<boost::thread> serverThread;
312
+ unsigned int server::m_threadPoolSize;
313
+ inotifyHandler* server::erh=NULL;
314
+ server::server(unsigned int thread_pool_size, const char* hostCheckName)
315
+ :m_timer(m_ios),m_hostCheckName(hostCheckName),m_stopping(0)
316
+ {
317
+ m_threadPoolSize = thread_pool_size;
318
+
319
+ }
320
+
321
+ server::~server()
322
+ {
323
+ m_listeners.clear();
324
+ }
325
+
326
+ void server::addApplication(boost::shared_ptr<IAppModuleBuilder> app, const std::string& address
327
+ , const std::string& port)
328
+ {
329
+ m_listeners.push_back(boost::shared_ptr<listener>(new listener(this, app, address, port)));
330
+ }
331
+
332
+ void server::startTimer()
333
+ {
334
+ m_timer.expires_from_now(posix_time::seconds(10));
335
+ m_timer.async_wait(bind(&server::onCheckInternlShutdown, this, placeholders::error));
336
+
337
+ }
338
+
339
+ /* check shutdown flags
340
+ * call start timer agin if it is not shutdown.
341
+ */
342
+ void server::onCheckInternlShutdown(const boost::system::error_code& e)
343
+ {
344
+ bool shutdown = false;
345
+ {
346
+ mutex::scoped_lock lck(modulesMutex);
347
+ for (size_t i=0;i<modules.size();i++)
348
+ {
349
+ IAppModule* mod = modules[i];
350
+ if (mod->isShutDown())
351
+ {
352
+ shutdown = true;
353
+ break;
354
+ }
355
+ }
356
+ }
357
+ if (shutdown)
358
+ stop();
359
+ else
360
+ startTimer();
361
+ }
362
+
363
+ void start1(io_service* p)
364
+ {
365
+ try
366
+ {
367
+ p->run();
368
+ }
369
+ catch(...){};
370
+ }
371
+
372
+ /** crete server thread and return immediately.
373
+ */
374
+ void server::start()
375
+ {
376
+ serverThread.reset(new boost::thread(bind(&server::run, this)));
377
+ }
378
+
379
+ void server::run()
380
+ {
381
+ if (erh)
382
+ erh->printInfo("Start Tpool server.");
383
+ startTimer();
384
+ std::vector<boost::shared_ptr<boost:: thread> >threads;
385
+ thread::attributes attr;
386
+ attr.set_stack_size( 125 * 1024);
387
+ for (std::size_t i = 0; i < m_threadPoolSize; ++i)
388
+ {
389
+ thread *t = new thread(attr, bind(&start1, &m_ios));
390
+ shared_ptr<thread> thread(t);
391
+ threads.push_back(thread);
392
+ }
393
+ for (std::size_t i = 0; i < threads.size(); ++i)
394
+ threads[i]->join();
395
+
396
+ }
397
+
398
+ void server::stop()
399
+ {
400
+ if (m_stopping) return;
401
+ m_stopping = true;
402
+ if (erh)
403
+ erh->printInfo("Stopping Tpool server ...");
404
+ for (size_t i = 0;i < m_listeners.size();i++)
405
+ m_listeners[i]->cancel();
406
+ connection::stop(); // Connection is not closed if io_service is stopped.
407
+ m_ios.stop();
408
+ serverThread->join();
409
+ m_ios.reset();
410
+
411
+ }
412
+
413
+ }//namespace tpool
414
+ }//namespace sever
415
+ }//namespace netsvc
416
+ }//namespace bzs
@@ -0,0 +1,84 @@
1
+ /*=================================================================
2
+ Copyright (C) 2012 2013 BizStation Corp All rights reserved.
3
+
4
+ This program is free software; you can redistribute it and/or
5
+ modify it under the terms of the GNU General Public License
6
+ as published by the Free Software Foundation; either version 2
7
+ of the License, or (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program; if not, write to the Free Software
16
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17
+ 02111-1307, USA.
18
+ =================================================================*/
19
+ #ifndef BZS_NETSVC_SERVER_TPOOLSERVER_H
20
+ #define BZS_NETSVC_SERVER_TPOOLSERVER_H
21
+
22
+ #include "iserver.h"
23
+ #include <string>
24
+ #include <vector>
25
+ #include <boost/noncopyable.hpp>
26
+ #include <boost/shared_ptr.hpp>
27
+ #include <boost/asio.hpp>
28
+
29
+
30
+
31
+ #define SERVER_SYNC_THREAD_PER_IOS
32
+
33
+ namespace boost{class condition_variable;}
34
+ namespace bzs
35
+ {
36
+ namespace netsvc
37
+ {
38
+ namespace server
39
+ {
40
+ class IAppModuleBuilder;
41
+
42
+
43
+ namespace tpool
44
+ {
45
+ extern unsigned int g_connections;
46
+
47
+
48
+ class connection;
49
+ class listener;
50
+ class server: public iserver, private boost::noncopyable
51
+ {
52
+
53
+ std::vector<boost::shared_ptr<listener> > m_listeners;
54
+ bool m_stopping;
55
+
56
+ boost::asio::io_service m_ios;
57
+ boost::asio::deadline_timer m_timer;
58
+ const char* m_hostCheckName;
59
+ void startTimer();
60
+ void doClose();
61
+ void onCheckInternlShutdown(const boost::system::error_code& e);
62
+ void run();
63
+ public:
64
+ explicit server(unsigned int thread_pool_size, const char* hostCheckName);
65
+ ~server();
66
+ void start();
67
+ void stop();
68
+ static unsigned int m_threadPoolSize;
69
+ void registerErrorHandler(inotifyHandler* eh){erh = eh;};
70
+ void addApplication(boost::shared_ptr<IAppModuleBuilder> app
71
+ , const std::string& address, const std::string& port);
72
+ boost::asio::io_service& ios(){return m_ios;}
73
+ const char* hostCheckName(){return m_hostCheckName;};
74
+ static inotifyHandler* erh;
75
+
76
+ };
77
+
78
+ }//namesapce tpool
79
+ }//namespace sever
80
+ }//namespace netsvc
81
+ }//namespace bzs
82
+
83
+ #endif //BZS_NETSVC_SERVER_TPOOLSERVER_H
84
+
@@ -0,0 +1,96 @@
1
+ /*=================================================================
2
+ Copyright (C) 2013 BizStation Corp All rights reserved.
3
+
4
+ This program is free software; you can redistribute it and/or
5
+ modify it under the terms of the GNU General Public License
6
+ as published by the Free Software Foundation; either version 2
7
+ of the License, or (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program; if not, write to the Free Software
16
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17
+ 02111-1307, USA.
18
+ =================================================================*/
19
+
20
+ #pragma hdrstop
21
+ #include "benchmark.h"
22
+ #include <tchar.h>
23
+ #include <stdio.h>
24
+
25
+ #define BOOST_ERROR_CODE_HEADER_ONLY
26
+ #define BOOST_CHRONO_INLINED
27
+
28
+
29
+ #pragma package(smart_init)
30
+
31
+ namespace bzs
32
+ {
33
+ namespace rtl
34
+ {
35
+
36
+ boost_timer benchmark::t;
37
+ #ifdef BOOST_HIGH_RESOL_TIMER_ENABLE
38
+ boost_timer::time_point benchmark::m_start;
39
+ #endif
40
+ void benchmark::showTimeSec(bool result, const char* name)
41
+ {
42
+ if (result == true)
43
+ {
44
+ #ifdef BOOST_CPUTIMER_ENABLE
45
+ boost::timer::cpu_times elapsed = t.elapsed();
46
+ printf("%d msec %s\n", (int)(elapsed.wall/1000000), name);
47
+ printf("%s\r\n", boost::timer::format(elapsed).c_str());
48
+ #else
49
+ #ifdef BOOST_HIGH_RESOL_TIMER_ENABLE
50
+ boost_timer::time_point p = boost_timer::now();
51
+ boost::chrono::nanoseconds ns = p - m_start;
52
+ printf("%d msec %s\n", (int)(ns.count()/1000000) , name);
53
+ #else
54
+ printf("%d msec %s\n", (int)(benchmark::t.elapsed()*1000), name);
55
+ #endif
56
+ #endif
57
+ }
58
+ else
59
+ printf("Erorr %s\n", name);
60
+ }
61
+ bool benchmark::report(boost::function<bool()> func, const char* name)
62
+ {
63
+ start();
64
+
65
+ bool ret = func();
66
+ showTimeSec(ret, name);
67
+ return ret;
68
+ }
69
+
70
+ void benchmark::report2(boost::function<void()> func, const char* name)
71
+ {
72
+
73
+ start();
74
+ func();
75
+ showTimeSec(1, name);
76
+
77
+ }
78
+
79
+ void benchmark::start()
80
+ {
81
+
82
+ #ifdef BOOST_CPUTIMER_ENABLE
83
+ t.stop();
84
+ t.start();
85
+ #else
86
+ #ifdef BOOST_HIGH_RESOL_TIMER_ENABLE
87
+ m_start = boost_timer::now();
88
+ #else
89
+ t.restart();
90
+ #endif
91
+ #endif
92
+ }
93
+
94
+
95
+ }//namespace rtl
96
+ }//namespace bzs