root/plugins/branches/0185_GEN_PHYSICS_REFACTOR_2/ZBoostNetworking/src/TransmissionControlProtocolService.hpp @ 3447

Revision 3447, 6.5 KB (checked in by mgray, 6 months ago)

Debug and refactor of the TCP Protocol Adapter in ZBoostNetworking.

Line 
1//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2// Zen Enterprise Framework
3//
4// Copyright (C) 2001 - 2009 Tony Richards
5//
6//  This software is provided 'as-is', without any express or implied
7//  warranty.  In no event will the authors be held liable for any damages
8//  arising from the use of this software.
9//
10//  Permission is granted to anyone to use this software for any purpose,
11//  including commercial applications, and to alter it and redistribute it
12//  freely, subject to the following restrictions:
13//
14//  1. The origin of this software must not be misrepresented; you must not
15//     claim that you wrote the original software. If you use this software
16//     in a product, an acknowledgment in the product documentation would be
17//     appreciated but is not required.
18//  2. Altered source versions must be plainly marked as such, and must not be
19//     misrepresented as being the original software.
20//  3. This notice may not be removed or altered from any source distribution.
21//
22//  Tony Richards trichards@indiezen.com
23//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
24#ifndef ZEN_ENTERPRISE_APPSERVER_TCP_PROTOCOL_SERVICE_HPP_INCLUDED
25#define ZEN_ENTERPRISE_APPSERVER_TCP_PROTOCOL_SERVICE_HPP_INCLUDED
26
27#include "TCP/Connection.hpp"
28#include "TCP/Message.hpp"
29
30#include <Zen/Core/Memory/managed_ptr.hpp>
31#include <Zen/Core/Memory/managed_weak_ptr.hpp>
32#include <Zen/Core/Memory/managed_self_ref.hpp>
33
34#include <Zen/Core/Threading/I_Thread.hpp>
35#include <Zen/Core/Threading/I_Mutex.hpp>
36
37#include <Zen/Enterprise/AppServer/I_ProtocolService.hpp>
38
39#include <boost/noncopyable.hpp>
40#include <boost/function.hpp>
41#include <boost/asio.hpp>
42
43#include <boost/shared_ptr.hpp>
44
45#include <map>
46#include <string>
47#include <vector>
48
49//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
50namespace Zen {
51namespace Enterprise {
52namespace AppServer {
53//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
54
55class TransmissionControlProtocolService
56:   public I_ProtocolService
57,   public Memory::managed_self_ref<I_ProtocolService>
58{
59    /// @name Types
60    /// @{
61public:
62    typedef std::vector<Threading::I_Thread*>                   Threads_type;
63    typedef Memory::managed_weak_ptr<Networking::I_Endpoint>    wpEndpoint_type;
64
65    typedef boost::shared_ptr<TCP::Connection>                  pConnection_type;
66    typedef boost::asio::ip::tcp::endpoint                      EndpointKey_type;
67    typedef std::map<EndpointKey_type, pConnection_type>        ConnectionMap_type;
68    /// @}
69
70    /// @name I_ProtocolService implementation
71    /// @{
72public:
73    virtual I_ApplicationServer& getApplicationServer();
74    virtual pEndpoint_type resolveEndpoint(const std::string& _address, const std::string& _port);
75    virtual void sendTo(pMessage_type _pMessage, pEndpoint_type _pEndpoint);
76    virtual Event::I_Event& getConnectedEvent();
77    virtual Event::I_Event& getDisconnectedEvent();
78    /// @}
79
80    /// @name I_StartupShutdownParticipant implementation
81    /// @{
82public:
83    virtual void setConfiguration(const Plugins::I_ConfigurationElement& _config);
84    virtual Threading::I_Condition* prepareToStart(Threading::ThreadPool& _threadPool);
85    virtual void start();
86    virtual Threading::I_Condition* prepareToStop();
87    virtual void stop();
88    /// @}
89
90    /// @name TransmissionControlProtocolService implementation
91    /// @{
92public:
93    void startThreads();
94
95    void handleAccept(const boost::system::error_code& _error);
96
97    void createConnection();
98    void asyncAccept();
99    void destroyEndpoint(wpEndpoint_type _pEndpoint);
100
101    /// This is called by the connection after it has established
102    /// a TCP connection to a server.
103    void onConnected(pConnection_type _pConnection);
104
105    /// This is called by the connection after it has lost a
106    /// connection to the server.
107    void onDisconnected(pConnection_type _pConnection);
108    void onHandleMessage(pConnection_type  _pConnection, TCP::MessageBuffer& _message);
109
110    /// Get the location for this URL
111    /// @see I_WebProtocolService::registerURL()
112    //pResourceLocation_type getLocation(const std::string& _url);
113    /// @}
114
115    /// @name 'Structors
116    /// @{
117protected:
118    friend class ProtocolServiceFactory;
119
120    explicit TransmissionControlProtocolService(I_ApplicationServer& _server);
121    virtual ~TransmissionControlProtocolService();
122    /// @}
123
124    /// @name Inner Classes
125    /// @{
126public:
127
128    /// @}
129
130    /// @name Member Variables
131    /// @{
132private:
133    /// Reference to the parent application server to which this protocol service is bound
134    AppServer::I_ApplicationServer&    m_appServer;
135
136    /// IO Service to perform asynchronous operations
137    boost::asio::io_service             m_ioService;
138    boost::asio::io_service::work*      m_pWork;
139
140    /// Acceptor used to listen for incoming connections
141    boost::asio::ip::tcp::acceptor      m_acceptor;
142
143    /// Address on which to bind
144    std::string                         m_address;
145
146    /// Port to which to bind
147    std::string                         m_port;
148
149    /// Number of threads
150    int                                 m_threadCount;
151
152    /// True if this is a server that is listening
153    bool                                m_isServer;
154
155    volatile bool                       m_threadsStarted;
156
157    /// New connection used for the next accepted connection
158    /// if this is a server side service.
159    /// This is a shared pointer instead of a managed pointer
160    /// because boost::bind<> handles shared_ptr correctly
161    boost::shared_ptr<TCP::Connection> m_pNewConnection;
162
163    /// Collection of threads
164    Threads_type                        m_threads;
165
166    /// Endpoint on which this service is listening (if any)
167    boost::asio::ip::tcp::endpoint      m_endpoint;
168
169    ConnectionMap_type                  m_connectionMap;
170
171    /// Guard for m_connections. 
172    /// Also used as a guard for m_threads
173    Threading::I_Mutex*                 m_pConnectionsGuard;
174
175    pMessageRegistry_type               m_pMessageRegistry;
176    /// @}
177
178};  // class TransmissionControlProtocolService
179
180//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
181}   // namespace AppServer
182}   // namespace Enterprise
183}   // namespace Zen
184//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
185
186#endif // ZEN_ENTERPRISE_APPSERVER_TCP_PROTOCOL_SERVICE_HPP_INCLUDED
Note: See TracBrowser for help on using the browser.