-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMygSoapUtil.h
164 lines (153 loc) · 3.96 KB
/
MygSoapUtil.h
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
155
156
157
158
159
160
161
162
163
164
//pthread_mutex_t queue_cs;
//pthread_cond_t queue_cv;
DWORD WINAPI StartgSoapServer(LPVOID lpThreadParam)
{
startSvr = true;
//ServiceService calc_service;
//calc_service.serve();
// soap_serve(soap_new());
// use the service operation request dispatcher
// open the log file.
struct soap* ptsoap[MAX_THR] = {0};
HANDLE th[MAX_THR] = {0};
DWORD tid[MAX_THR] = {0};
int i;
struct soap calc_soap;
int m, s; // master and slave sockets
// soap init
soap_init(&calc_soap);
my_soap_init(&calc_soap);
//
m = soap_bind(&calc_soap,
NULL, // 任何IP地址都可以访问
HTTP_SVR_PORT, // 端口
100); // 请求队列的长度
if (m < 0) //!soap_valid_socket(m)
{
WriteLog("Start Server Error!");
//
MessageBox(0, _T("Start Server Error!\n"), _T("Info"), MB_OK);
}
else
{
WriteLog("Start Server successful........");
MessageBox(0, _T("Start Server successful........"), _T("Info"), MB_OK);
while (startSvr)
{
for (i = 0; i < MAX_THR; i++)
{
s = soap_accept(&calc_soap);
if (s < 0)
{
SoapErr(&calc_soap);
MessageBox(0, _T("soap_accept Error!"), _T("Error"), MB_OK);
break;
}
// fprintf(...
WriteLog("Thread %d accept socket %d connection from IP %d.%d.%d.%d",
tid[i], s, (calc_soap.ip >> 24) & 0xFF,
(calc_soap.ip >> 16) & 0xFF,
(calc_soap.ip >> 8) & 0xFF, calc_soap.ip & 0xFF);
if (!ptsoap[i]) // first time around
{
ptsoap[i] = soap_copy(&calc_soap);
if (!ptsoap[i])
{
ASSERT(0);
exit(1);
// error
}
}
else
{
WaitForSingleObject(th[i], 1 * 1000);
WriteLog("Thread %d[%d] completed, status tid = %d", th[i], i, tid[i]);
// deallocate C++ data of old thread
soap_destroy(ptsoap[i]);
// deallocate data of old thread
soap_end(ptsoap[i]);
}
// new socket fd
ptsoap[i]->socket = s;
th[i] = MyThread(ProcessRequest, &tid[i], ptsoap[i]);
}
WaitForMultipleObjects(MAX_THR, th, TRUE, 1 * 1000);
for (i = 0; i < MAX_THR; i++)
{
CloseHandle(th[i]);
if (ptsoap[i])
{
soap_done(ptsoap[i]); //detach context
free(ptsoap[i]); //free up
}
}
startSvr = false;
}
}
soap_done(&calc_soap);
MessageBox(0, _T("soap_done!"), _T("Info"), MB_OK);
startSvr = false;
WriteLog("Web Server End........");
return 0;
}
//void* process_queue(void* soap);
//int enqueue(SOAP_SOCKET sock);
//SOAP_SOCKET dequeue();
//void* process_queue(void* soap)
//{
// struct soap* tsoap = (struct soap*)soap;
// for (;;)
// {
// tsoap->socket = dequeue();
// if (0)
// {
// break;
// }
// soap_serve(tsoap);
// soap_destroy(tsoap);
// soap_end(tsoap);
// fprintf(stderr, "served\n");
// }
//
// return NULL;
//}
//
//int enqueue(SOAP_SOCKET sock)
//{
// int status = SOAP_OK;
// int next;
// pthread_mutex_lock(&queue_cs);
// next = tail + 1;
// if (next >= MAX_QUEUE)
// {
// next = 0;
// }
// if (next == head)
// {
// status = SOAP_EOM;
// } else {
// queue[tail] = sock;
// tail = next;
// }
// pthread_cond_signal(&queue_cv);
// pthread_mutex_unlock(&queue_cs);
//
// return status;
//}
//
//SOAP_SOCKET dequeue()
//{
// SOAP_SOCKET sock;
// pthread_mutex_lock(&queue_cs);
// while (head == tail)
// {
// pthread_cond_wait(&queue_cv, &queue_cs);
// }
// sock = queue[head++];
// if (head >= MAX_QUEUE)
// {
// head = 0;
// }
// pthread_mutex_unlock(&queue_cs);
// return sock;
//}