-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketserver.cpp
130 lines (127 loc) · 3.49 KB
/
socketserver.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
#include "socketserver.h"
namespace protorpc{
SocketServer::SocketServer(google::protobuf::Service* toServe,QHostAddress addr,uint16_t port)
{
service=toServe;
this->addr=addr;
this->prt=port;
running=false;
soclock=new QMutex(QMutex::Recursive);
// prepared=false;
}
SocketServer::~SocketServer(){
shutdown();
delete tsr;
delete soclock;
}
#if 0
QTcpServer *SocketServer::getTcpServer(){
printf("Wfc\n");
while(!prepared){
soclock.lock();
socwait.wait(&soclock);
soclock.unlock();
}
printf("Retsrv\n");
return tsr;
}
/**
* Start this server socket. The server socket should be set up before calling this method.
*/
void SocketServer::start(){
if(!running&&!prepared){
running=true;
prepared=true;
soclock.lock();
socwait.wakeAll();
soclock.unlock();
}
}
#endif
void SocketServer::start(){
if(!running){
running=true;
((QThread*)this)->start();
}
}
/**
* Shut down this server socket
* @param closeStreams - should the socket close the streams in this socket, leave to false if in doubt
*/
void SocketServer::shutdown(){
if(running){
running=false;
soclock->lock();
//FIXME:interrupt thread
TwoWayStream** s;
foreach(s,streamServers){
(*s)->shutdown(true);
delete*s;
free(s);
}
streamServers.clear();
tsr->close();
soclock->unlock();
}
}
/**
* Should this socket shut down if a client disconnects from it?
*/
bool SocketServer::doShutDownOnDisconnect() {
return shutdownOnDisconnect;
}
/**
* Should this socket shut down if a client disconnects from it?
*/
void SocketServer::setShutDownOnDisconnect(bool shutDownOnDisconnect) {
this->shutdownOnDisconnect= shutDownOnDisconnect;
}
void SocketServer::run(){
QTcpSocket *client;
bool gotConnection=false;
soclock->lock();
tsr=new QTcpServer(NULL);
tsr->listen(addr,prt);
while(running){
soclock->unlock();
//client=ssc.accept();
gotConnection=tsr->waitForNewConnection(250);
soclock->lock();
if(gotConnection){
client=tsr->nextPendingConnection();
TwoWayStream **ssp=(TwoWayStream **) malloc(sizeof(TwoWayStream*));
TwoWayStream *ss=new TwoWayStream(client,service,true);//,google::protobuf::NewCallback(shutdownCallback,this,ssp));//don't use callback
*ssp=ss;
streamServers.insert(ssp);
}
foreach(TwoWayStream** s, streamServers){
if((*s)->isFinished()){
if(shutdownOnDisconnect){
shutdown();
}else{
stopServer(s);
}
}
}
}
// socwait.wakeAll();
soclock->unlock();
running=false;
}
void SocketServer::stopServer(TwoWayStream** tws){
//(*tws)->shutdown(true);
streamServers.remove(tws);
delete *tws;
free(tws);
}
#if 0
void SocketServer::shutdownCallback(SocketServer *on,TwoWayStream **tws) {//delete the server when it has shut down
printf("Shutdown callback in socketserver called\n");
if(on->shutdownOnDisconnect)
on->shutdown();
else{//shutdown that server
on->stopServer(tws);
}
}
#endif
};