ServerCore/TCPSession.h

145 lines
3.8 KiB
C++

#ifndef __Session_h__
# define __Session_h__
# include "SessionFilter.h"
# include "TCPSocket.h"
# include <sstream>
namespace core {
class Command;
class TCPServer;
///
/// TCPSession
///
/// TCPSession defines the nature of the interaction with the client
/// and stores persistent data for a defined session. TCPSession objects
/// are not sockets but instead provide a communications control
/// mechanism. Protocol conversations are provided through extensions
/// from this object.
///
///
///
class TCPSession : public TCPSocket {
public:
///
///
///
TCPSession(EPoll &ePoll, std::string text = "");
///
///
///
TCPSession(EPoll &ePoll, TCPServer *server, std::string text = "");
///
///
///
virtual ~TCPSession();
Command *grab = NULL;
virtual void output(std::stringstream &data);
///
/// The send method is used to output the contents of the out stream
/// to the session containing the stream.
///
void send();
///
/// Use this method to terminate this TCPSession.
///
void terminate();
///
/// This is a pointer to the server that was used to 'spawn' the server connection.
/// If the session is just client side connection this pointer should be NULL;
///
TCPServer *server;
///
/// Use out to send data to the session socket or other session sockets.
///
std::stringstream out;
///
/// uuid is generated automatically when the session object is instantiated. This
/// value can be used to uniquely identify a session and is the default value
/// pointed to by the alias pointer.
///
char uuid[37];
///
/// alias is a void pointer that can be set to point to any object that identifies
/// this session uniquely. Using this approach, inheriting objects can determine
/// how it knows the contacts that this server manages.
///
void *alias;
///
///
///
virtual bool compareAlias(void *alias);
virtual void outputAlias(std::stringstream &out);
protected:
///
///
///
virtual void onRegistered() override;
///
/// Override the onLineReceived method to receive a string of characters that
/// represents a single line of data terminated by a LF or CRLF. If onDataReceived
/// was overriden this method will not be called unless the onDataReceived calls
/// this method explicitly using the class and member name.
///
virtual void onLineReceived(coreutils::ZString &line) override;
///
/// This method is called from within the protocol method when protocol is called
/// on the initial connection where the data is an empty string. Use this method
/// to deliver a message to the connection upon connection.
///
virtual void onConnected();
///
/// Override the protocol method to manage and control the session communications
/// in your inherited session. If you do not override this method then the Session
/// default will process the 'commands' added to the server object using the
/// processRequest method on the session input.
///
/// When data is received within the session two modes are available to pass the
/// data through the protocol method: LINE or BLOCK.
///
virtual void protocol(coreutils::ZString &data);
private:
std::mutex mtx;
};
}
#endif