[go: up one dir, main page]

Menu

[r82]: / trunk / ehsconnection.h  Maximize  Restore  History

Download this file

155 lines (109 with data), 5.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* $Id$
*
* EHS is a library for embedding HTTP(S) support into a C++ application
*
* Copyright (C) 2004 Zachary J. Hansen
*
* Code cleanup, new features and bugfixes: Copyright (C) 2010 Fritz Elfert
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation;
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* This can be found in the 'COPYING' file.
*
*/
#ifndef _EHSCONNECTION_H_
#define _EHSCONNECTION_H_
class EHSServer;
class NetworkAbstraction;
/**
* EHSConnection abstracts the concept of a connection to an EHS application.
* It stores file descriptor information, unhandled data, and the current
* state of the request
*/
class EHSConnection {
private:
EHSConnection ( const EHSConnection & );
EHSConnection & operator = ( const EHSConnection & );
private:
bool m_bDoneReading; ///< we're never reading from this again
bool m_bDisconnected; ///< client has closed connection on us
HttpRequest * m_poCurrentHttpRequest; ///< request we're currently parsing
EHSServer * m_poEHSServer; ///< server with which this is associated
time_t m_nLastActivity; ///< time at which the last activity occured
int m_nRequests; ///< holds id of last request received
int m_nResponses; ///< holds id of last response sent
int m_nActiveRequests; ///< Number of currently processing requests
/// file descriptor associated with this client
NetworkAbstraction * m_poNetworkAbstraction;
/// raw data received from client that doesn't comprise a full request
std::string m_sBuffer;
/// holds out-of-order httpresponses that aren't ready to go out yet
HttpResponseMap m_oHttpResponseMap;
/// holds pending requests
HttpRequestList m_oHttpRequestList;
/// address from which the connection originated
std::string m_sAddress;
/// remote port from which the connection originated
int m_nPort;
size_t m_nMaxRequestSize;
pthread_mutex_t m_oMutex; ///< mutex protecting entire object
public:
/// returns address of the connection.
std::string GetAddress() const { return m_sAddress; }
/// returns client port of the connection.
int GetPort() const { return m_nPort; }
/// returns whether the client has disconnected from us.
bool Disconnected() const { return m_bDisconnected; }
private:
/// Constructor
EHSConnection(NetworkAbstraction * ipoNetworkAbstraction,
EHSServer * ipoEHSServer);
/// destructor
~EHSConnection();
/// updates the last activity to the current time
void UpdateLastActivity() { m_nLastActivity = time ( NULL ); }
/// returns the time of last activity
time_t LastActivity() { return m_nLastActivity; }
/// returns whether we're still reading from this socket -- mutex must be locked
bool StillReading() { return !m_bDoneReading; }
/// call when no more reads will be performed on this object.
/// ibDisconnected is true when client has disconnected
void DoneReading ( bool ibDisconnected );
/// gets the next request object
HttpRequest *GetNextRequest();
/// returns true if object should be deleted
int CheckDone();
/// Enumeration result for AddBuffer
enum AddBufferResult {
ADDBUFFER_INVALID = 0,
ADDBUFFER_OK,
ADDBUFFER_INVALIDREQUEST,
ADDBUFFER_TOOBIG,
ADDBUFFER_NORESOURCE
};
/// adds new data to psBuffer
AddBufferResult AddBuffer(char * ipsData, int inSize);
/// sends the actual data back to the client
void SendHttpResponse(std::auto_ptr<HttpResponse> response);
/// adds a response to the response list and sends as many responses as are ready -- takes over the memory in ipoHttpResponse
void AddResponse(HttpResponse *response);
/// returns true of httprequestlist is not empty
int RequestsPending() { return (0 != m_nActiveRequests) || !m_oHttpRequestList.empty(); }
/// returns underlying network abstraction
NetworkAbstraction * GetNetworkAbstraction();
/// Sets the maximum request size
void SetMaxRequestSize(size_t n) { m_nMaxRequestSize = n; }
friend class EHSServer;
};
#endif // _EHSCONNECTION_H_