1 /*
2  * Copyright (c) 2015, 2021, Oracle and/or its affiliates.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License, version 2.0,
6  * as published by the Free Software Foundation.
7  *
8  * This program is also distributed with certain software (including
9  * but not limited to OpenSSL) that is licensed under separate terms,
10  * as designated in a particular file or component or in included license
11  * documentation.  The authors of MySQL hereby grant you an additional
12  * permission to link the program and your derivative works with the
13  * separately licensed software that they have included with MySQL.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License, version 2.0, for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301  USA
24  */
25 
26 #ifndef _NGS_CLIENT_SESSION_H_
27 #define _NGS_CLIENT_SESSION_H_
28 
29 #include "interface/session_interface.h"
30 #include "ngs/protocol_encoder.h"
31 #include "ngs/thread.h"
32 #include "ngs/protocol_authentication.h"
33 
34 namespace ngs
35 {
36   class Client;
37   class Protocol_encoder;
38 
39   class Session: public Session_interface
40   {
41   public:
42     typedef int32_t Session_id;
43 
44     Session(Client_interface &client, Protocol_encoder *proto, const Session_id session_id);
45     virtual ~Session();
46 
session_id()47     virtual Session_id session_id() const { return m_id; }
48     virtual bool is_ready() const;
49 
50   public:
51     virtual void on_close(const bool update_old_state = false);
52     virtual void on_kill();
53     virtual void on_auth_success(const Authentication_handler::Response &response);
54     virtual void on_auth_failure(const Authentication_handler::Response &response);
55 
56     // handle a single message, returns true if message was handled false if not
57     virtual bool handle_message(ngs::Request &command);
58 
client()59     Client_interface &client() { return m_client; }
60 
proto()61     Protocol_encoder &proto() { return *m_encoder; }
62 
63   protected:
64     virtual bool handle_auth_message(ngs::Request &command);
65     virtual bool handle_ready_message(ngs::Request &command);
66 
67     void stop_auth();
68 
69     static bool can_forward_error_code_to_client(const int error_code);
70 
71   public:
state()72     State state() const { return m_state; }
state_before_close()73     State state_before_close() const { return m_state_before_close; }
74 
75   protected:
76     Client_interface &m_client;
77     Protocol_encoder *m_encoder;
78     Authentication_handler_ptr m_auth_handler;
79     State m_state;
80     State m_state_before_close;
81 
82     const Session_id m_id;
83     // true if a session session was already scheduled for execution in a thread
84     int32 m_thread_pending;
85     // true if the session is currently assigned to a thread and executing
86     int32 m_thread_active;
87 
check_thread()88     void check_thread()
89     {
90 #ifndef WIN32
91       assert(mdbg_my_thread == pthread_self());
92 #endif
93     }
94 #ifndef WIN32
95     pthread_t mdbg_my_thread;
96 #endif
97   };
98 
99 
100 } // namespace ngs
101 
102 #endif
103