forked from uroni/urbackup_backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cpp
152 lines (132 loc) · 3.06 KB
/
Client.cpp
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
/*************************************************************************
* UrBackup - Client/Server backup system
* Copyright (C) 2011-2016 Martin Raiber
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#include "vld.h"
#include "Client.h"
#include "Server.h"
#include "AcceptThread.h"
#include "libfastcgi/fastcgi.hpp"
CClient::CClient()
{
mutex=Server->createMutex();
m_lock=NULL;
processing=false;
}
CClient::~CClient()
{
Server->destroy(mutex);
}
SOCKET CClient::getSocket()
{
return s;
}
OutputCallback * CClient::getOutputCallback()
{
return output;
}
FCGIProtocolDriver * CClient::getFCGIProtocolDriver()
{
return driver;
}
#ifndef TCP_CORK
#define TCP_CORK TCP_NOPUSH
#endif
void CClient::set(SOCKET ps, OutputCallback *poutput, FCGIProtocolDriver * pdriver )
{
IScopedLock l(mutex);
s=ps;
output=poutput;
driver=pdriver;
int flag;
#ifdef _WIN32
flag=1;
setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
#else
flag=0;
setsockopt(s, IPPROTO_TCP, TCP_CORK, (char *) &flag, sizeof(int));
#endif
}
void CClient::lock()
{
IScopedLock *n_lock=new IScopedLock(mutex);
m_lock=n_lock;
}
void CClient::unlock()
{
delete m_lock;
}
void CClient::remove()
{
IScopedLock l(mutex);
delete output;
delete driver;
}
void CClient::addRequest( FCGIRequest* req)
{
IScopedLock l(mutex);
requests.push_back( req );
}
bool CClient::removeRequest( FCGIRequest *req)
{
IScopedLock l(mutex);
for(size_t i=0;i<requests.size();++i)
{
if(requests[i]==req )
{
requests.erase( requests.begin()+i);
return true;
}
}
return false;
}
size_t CClient::numRequests(void)
{
IScopedLock l(mutex);
return requests.size();
}
FCGIRequest* CClient::getRequest(size_t num)
{
IScopedLock l(mutex);
if( num>=requests.size() )
return NULL;
return requests[num];
}
FCGIRequest* CClient::getAndRemoveReadyRequest(void)
{
IScopedLock l(mutex);
for(size_t i=0;i<requests.size();++i)
{
if( requests[i]->stdin_eof==true )
{
FCGIRequest* req=requests[i];
requests.erase( requests.begin()+i);
return req;
}
}
return NULL;
}
bool CClient::isProcessing(void)
{
return processing;
}
bool CClient::setProcessing(bool b)
{
IScopedLock l(mutex);
bool ret=processing;
processing=b;
return ret;
}