开源主页 http://www.cs.fsu.edu/~engelen/soap.html http://www.w3.org/TR/2000/NOTE-SOAP-20000508/
一、SOAP协议介绍
二、gSoap库介绍
三、gSoap使用实例
/* my_tpm.c 线程池管理 */
struct MY_SOAP_QUEUE
{
SOAP_SOCKET aQueue[MAX_QUEUE];
int head;
int tail;
};
struct MY_SOAP_QUEUE g_mySoapQueue; /* SOAP消息队列 */
pthread_mutex_t g_mutexSoapQueue; /* 读写SOAP队列互斥量 */
pthread_cond_t g_condSoapQuere; /* SOAP队列收到消息条件变量 */
pthread_t g_myThreadPool[CONCURRENT_THREAD_MAX_NUM];
int g_myThreadPoolIndex[CONCURRENT_THREAD_MAX_NUM];
int g_myThreadNum;
int MY_TPM_initQueue (struct MY_SOAP_QUEUE *pQueue)
{
pQueue->head = 0;
pQueue->tail = 0;
memset (pQueue->aQueue, 0, sizeof (pQueue->aQueue));
return MY_OK;
}
BOOL MY_TPM_isQueueEmpty (struct MY_SOAP_QUEUE *pQueue)
{
if (pQueue->head == pQueue->tail)
{
return BOOL_TRUE;
}
return BOOL_FALSE;
}
BOOL MY_TPM_isQueueFull (struct MY_SOAP_QUEUE *pQueue)
{
int next = pQueue->tail + 1;
if (next >= MAX_QUEUE)
next = 0;
if (next == pQueue->head)
{
return BOOL_TRUE;
}
return BOOL_FALSE;
}
SOAP_SOCKET MY_TPM_readQueue (struct MY_SOAP_SOCKET *pQueue)
{
SOAP_SOCKET socket;
socket = pQueue->aQueue[pQueue->head++];
if (pQueue->head == MAX_QUEUE)
{
pQueue->head = 0;
}
return socket;
}
void MY_TPM_writeQueue (struct MY_SOAP_QUEUE *pQueue, SOAP_SOCKET socket)
{
pQueue->aQueue[pQueue->tail++] = socket;
if (pQueue->tail >= MAX_QUEUE)
{
pQueue->tail = 0;
}
return;
}
int MY_TPM_init ()
{
int i;
pthread_mutex_init (&g_mutexSoapQueue, NULL);
pthread_cond_init (&g_condSoapQueue, NULL);
g_myThreadNum = MAX_CONCURRENT_THREAD_NUM;
if (MY_ERR == MY_TPM_init (&g_mySoapQueue))
{
return MY_ERR;
}
for (i=0; i<MAX_THREAD; i++)
{
g_myThreadPoolIndex[i] = i;
pthread_create (&g_myThreadPool[i], NULL, (void* (*) (void*))MY_TPM_processQueue, (void*) (&g_myThreadPoolIndex[i]) );
}
return MY_OK;
}
int MY_TPM_finish ()
{
int i = 0;
while (i < CONCURRENT_THREAD_MAX_NUM && MY_OK == MY_TPM_enqueue (INVALID_SOCKET, 1))
{
i++;
}
for (i=0; i<MAX_THREAD; i++)
{
pthread_join (g_myThreadPool[i], NULL);
}
pthread_mutex_destroy (&g_mutexSoapQueue);
pthread_cond_destroy (&g_condSoapQueue);
return MY_OK;
}
int MY_TPM_lock (int *waitTime)
{
struct timespec timesp;
timesp.tv_sec = 0;
timesp.tv_nsec = SLEEP_TIME;
while (*waitTime > 0)
{
if (pthread_mutex_trylock (&g_mutexSoapQueue) != 0)
{
nanosleep (×p, NULL);
*waitTime -= timesp.tv_nsec / SLEEP_TIME;
}
else
{
return MY_OK;
}
}
return MY_ERR;
}
/* waitTime - 毫秒单位 */
int MY_TPM_enqueue (SOAP_SOCKET socket, unsigned int waitTime)
{
unsigned int sleepTime = waitTime;
struct timespec timesp;
timesp.tv_sec = 0;
timesp.tv_nsec = SLEEP_TIME;
while (!MY_TPM_lock (&sleepTime))
{
if (BOOL_TRUE == MY_TPM_isQueueFull (&g_mySoapQueue))
{
pthread_mutex_unlock (&g_mutexSoapQueue);
nanosleep (×p, NULL);
sleepTime -= SLEEP_TIME/1000000;
}
else
{
MY_TPM_writeQueue (&g_mySoapQueue, socket);
pthread_cond_signal (&g_condSoapQueue);
pthread_mutex_unlock (&g_mutexSoapQueue);
return MY_OK;
}
}
return MY_ERR;
}
SOAP_SOCKET MY_TPM_dequeue ()
{
SOAP_SOCKET socket;
pthread_mutex_lock (&g_mutexSoapQueue);
while (BOOL_TRUE == MY_TPM_isQueueEmpty (&g_mySoapQueue))
{
pthread_cond_wait (&g_condSoapQueue, &g_mutexSoapQueue);
}
socket = MY_TPM_readQueue (&g_mySoapQueue);
pthread_mutex_unlock (&g_mutexSoapQueue);
return socket;
}
void* MY_TPM_processQueue (void *param)
{
struct soap *soap;
int index = *(int*) param;
soap = soap_copy (&g_soap);
if (soap == NULL)
{
return NULL;
}
while (index < g_myThreadNum)
{
soap->socket = MY_TPM_dequeue ();
if (soap_valid_socket (soap->socket))
{
break;
}
soap_serve (soap);
soap_destroy (soap);
soap_end (soap);
}
soap_end (soap);
free (soap);
return NULL;
}
/* main.c 主函数 */
#define SIG_MY_STOP SIGUNUSED /* 停止信号 */
#define BACKLOG_MAX_LEN 100 /* TCP连接最大排队数量 */
struct soap g_soap;
int m; /* master socket */
BOOL isContinue = BOOL_TRUE; /* 是否继续执行 */
void sig_handler (int sig)
{
(void) sig;
isContinue = BOOL_FALSE;
close (m);
}
int main (void)
{
int s; /* slave socket */
soap_init (&g_soap);
soap_set_mode (&g_soap, SOAP_C_UTFSTRING);
signal (SIG_MY_STOP, sig_handler);
g_soap.bind_flag |= SO_REUSEADDR;
m = soap_bind (&g_soap, SERVER_IP, SERVICE_PORT, BACKLOG_MAX_LEN);
if (m < 0)
{
return MY_ERR;
}
else
{
while (isContinue)
{
s = soap_accept (&g_soap);
if (!isContinue)
{
break;
}
if (!soap_valid_socket (s))
{
break;
}
if (MY_TPM_enqueue (s, ENQUEUE_MAX_TIME) == MY_ERR)
{
close (s);
}
}
soap_done (&g_soap);
}
return 0;
}