gSoap 多线程实验报告
一、开发环境(客户端、服务器端均相同)
硬件环境:
AMD 3200+ 2.0G + 512M DDR + 10/100M 以太网卡
软件环境:
Operating System:Windows XP sp2
Compiler: Visual C++ 6.0
SOAP SDK: gSoap 2.7.9c
Multithread Library: pthread-2.5.0-win32-release
SSL Library: OpenSSL 0.9.7m
二、gSoap多线程程序的编写
2.1 服务器端程序的编写
对于服务器端,首先调用
soap_ssl_server_contex完成
SSL上下文环境的初始化,然后等待客户端发出服务请求。当主线程中检测到客户端发出新的SOAP请求时,调用
struct
soap *soap_copy(struct soap *soap) 函数创建新的soap 运行时环境(Runtime enviroment),新创建的 soap 与原先的soap runtime enviroment 是相互独立的,因此在多线程环境中各个线程可以独立处理 SOAP 请求而互不干涉。要在gSoap的服务器端支持OpenSSL,必须在调用
soap_accept后继续调用
soap_ssl_accept函数。在
soap_ssl_accept 验证后服务器和客户端建立安全的
SSL通道,然后主线程调用
pthread_create 创建新的线程来完成
SOAP服务。一个基本的服务器框架如下所示。
注意:
要在多线程的环境中使用
OpenSSL
,必须使用到这两个例程
CRYPTO_thread_setup()
和
CRYPTO_thread_cleanup()
。
附录中给出了在
Win32
和
POSIX
的这两个例程的实现代码。
int
main()
{
int m, s;
pthread_t tid;
struct soap soap, *tsoap;
soap_ssl_init(); /* init OpenSSL (just once) */
/* Note: CRYPTO_thread_setup() must be called in multi-thread environment */
if (CRYPTO_thread_setup())
{
fprintf(stderr, "Cannot setup thread mutex/n");
exit(1);
}
soap_init(&soap);
if (soap_ssl_server_context(&soap,
SOAP_SSL_DEFAULT,
"server.pem", /* keyfile: required when server must authenticate to clients (see SSL docs on how to obtain this file) */
"password", /* password to read the key file */
"cacert.pem", /* optional cacert file to store trusted certificates */
NULL, /* optional capath to directory with trusted certificates */
"dh512.pem", /* DH file, if NULL use RSA */
NULL, /* if randfile!=NULL: use a file with random data to seed randomness */
NULL /* optional server identification to enable SSL session cache (must be a unique name) */ ))
{
soap_print_fault(&soap, stderr);
exit(1);
}
m = soap_bind(&soap, NULL, 18000, 100); // use port 18000
if (m < 0)
{
soap_print_fault(&soap, stderr);
exit(1);
}
fprintf(stderr, "Socket connection successful: master socket = %d
/
n", m);
for (;;)
{
s = soap_accept(&soap);
fprintf(stderr, "Socket connection successful: slave socket = %d
/
n", s);
if (s < 0)
{
soap_print_fault(&soap, stderr);
break;
}
tsoap = soap_copy(&soap); /* should call soap_ssl_accept on a copy */
if (!tsoap)
break;
if (soap_ssl_accept(tsoap))
{
soap_print_fault(tsoap, stderr);
soap_free(tsoap);
continue; /* when soap_ssl_accept fails, we should just go on */
}
pthread_create(&tid, NULL, &process_request, (void*)tsoap);
}
soap_done(&soap); /* deallocates SSL context */
CRYPTO_thread_cleanup();
return 0;
}
void *process_request(void *soap)
{
pthread_detach(pthread_self());
soap_serve((struct soap*)soap);
soap_destroy((struct soap*)soap);
soap_end((struct soap*)soap);
soap_done((struct soap*)soap);
free(soap);
return NULL;
}
2.2 客户端程序的编写
客户端中,使用 #define MAX_THREAD 来定义最大的线程数。主线程使用
pthread_create()创建 MAX_THREAD个线程。新线程创建后,调用
soap_ssl_client_context初始化
SSL上下文环境。SSL 验证通过后,服务器端和客户端线程之间建立SSL通道,之后客户端发出SOAP服务请求。多线程的客户端程序框架如下所示。
同样地,为了在多线程环境中使用使用OpenSSL,
必须使用到这两个例程
CRYPTO_thread_setup()
和
CRYPTO_thread_cleanup()
。
int main(void)
{
int i=0;
for(i=0; i
{
/* create the threads to send the SOAP request */
THREAD_CREATE(tid[i],(void(*)(void*))test,NULL);
}
/* stay forever for the sub-threads to execute */
while (1)
{
/* Do something here. */
}
return 0;
}
void test()
{
struct soap soap;
struct _ns1__Query q;
struct _ns1__QueryResponse response;
struct _ns1__Pay p;
struct _ns1__PayResponse p_res;
/* Init OpenSSL */
soap_ssl_init();
/* Note: CRYPTO_thread_setup() must be called in multi-thread environment */
if (CRYPTO_thread_setup())
{ fprintf(stderr, "Cannot setup thread mutex/n");
exit(1);
}
if (soap_ssl_client_context(&soap,
SOAP_SSL_REQUIRE_SERVER_AUTHENTICATION|SOAP_SSL_SKIP_HOST_CHECK|SOAP_SSL_REQUIRE_CLIENT_AUTHENTICATION ,//| SOAP_SSL_SKIP_HOST_CHECK, /* use SOAP_SSL_DEFAULT in production code, we don't want the host name checks since these will change from machine to machine */
"client2003.pem",//NULL, /* keyfile: required only when client must authenticate to server (see SSL docs on how to obtain this file) */
"crusader",//NULL, /* password to read the keyfile */
"ca2003.pem", /* optional cacert file to store trusted certificates, use cacerts.pem for all public certificates issued by common CAs */
NULL, /* optional capath to directory with trusted certificates */
NULL /* if randfile!=NULL: use a file with random data to seed randomness */
))
{ soap_print_fault(&soap, stderr);
exit(1);
}
while(1)
{
/* the thread must stop for a while. Will be explaned later */
Sleep(1000);
q.UserID =”test”;;
q.Pasword = "test";
q.QueryType = 100;
/* send the SOAP request to query */
if (soap_call___ns1__query(&soap,server,"",&q,&response)==SOAP_OK)
{
printf("Status=%d,Amount=%d,SerialNO=%d/n",response.Status,response.Amount,response.SerialNO);
}
else
{
soap_print_fault(&soap, stderr);
}
srand((int)time(NULL));
p.Amount = rand()%10000+300;
p.Password =”password”;
p.PayType = rand()%4;
p.Description =”descrpition”;
p.UserID = names[rand()%4];
/* send the request to pay */
if (soap_call___ns1__pay(&soap,server,"",&p,&p_res)==SOAP_OK)
{
printf("Status=%d,Amount=%d,SerialNO=%d/n",p_res.Status,p_res.Amount,p_res.SerialNO);
}
else
{
soap_print_fault(&soap, stderr);
}
}
soap_destroy(&soap); /* C++ */
soap_end(&soap);
soap_done(&soap);
CRYPTO_thread_cleanup();
}
三、实验结果及分析
实验中我们发现:虽然我们可以在客户端中开启大量的线程(如设置最大线程数MAX_THREAD=200),但我们如果使用netstat命令来观察socket连接时可以看到:实际上同一时刻只有部分线程的socket 的状态是 ESTABLISHED(已连接),其他的socket处于TIME_WAIT状态。查阅资料后总结原因:服务器的一个端口监听多个socket 连接的
soap_bind(struct soap *soap, char *host, int port, int backlog) 函数中的backlog指定了请求的队列大小。backlog的值受到系统的限制(在Linux下默认为128,在Windows下,根据MSDN 的内容,没有确定值)。当同一时刻有大量的线程发送请求时,队列过小无法处理所有的请求,部分线程要等待服务器处理完原有的队列后才能得到服务。如:在MAX_THREAD=300的情况下,某一时刻,真正接受服务的线程只有 125 个。
同时我们在实验中也可以看到,在
MAX_THREAD=300
的情况下,所有的线程都能够得到服务,但各个线程得到的服务的次数有些差别,如:某一时刻,得到最多服务次数的线程完成服务次数为
399
次,而得到最少服务次数的线程完成服务次数为
364
次。
实验中我们还发现,在gSoap完成请求并断开socket之后,原有的socket并不能立即被重用,必须等待一定的时间(该值可以在注册表中设置:HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Tcpip/Parameters/ TcpTimedWaitDelay。 取值范围为30-240s)在客户端开启线程较多的情况下,socket没有立即回收导致socket资源很快就耗竭的问题。我们通过在客户端程序中的每次SOAP请求之后调用Sleep(1000),降低请求频率来解决这个问题。
四、附录
CRYPTO_thread_setup() 和CRYPTO_thread_cleanup() 的实现代码。
#include < unistd.h > /* defines _POSIX_THREADS if pthreads are available */
#ifdef _POSIX_THREADS
# include < pthread.h >
#endif
#if defined(WIN32)
# define MUTEX_TYPE HANDLE
# define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL)
# define MUTEX_CLEANUP(x) CloseHandle(x)
# define MUTEX_LOCK(x) WaitForSingleObject((x), INFINITE)
# define MUTEX_UNLOCK(x) ReleaseMutex(x)
# define THREAD_ID GetCurrentThreadID()
#elif defined(_POSIX_THREADS)
# define MUTEX_TYPE pthread_mutex_t
# define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL)
# define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x))
# define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
# define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
# define THREAD_ID pthread_self()
#else
# error "You must define mutex operations appropriate for your platform"
# error "See OpenSSL /threads/th-lock.c on how to implement mutex on your platform"
#endif
struct CRYPTO_dynlock_value { MUTEX_TYPE mutex; };
static MUTEX_TYPE *mutex_buf;
static struct CRYPTO_dynlock_value *dyn_create_function(const char *file, int line)
{
struct CRYPTO_dynlock_value *value;
value = (struct CRYPTO_dynlock_value*)malloc(sizeof(struct CRYPTO_dynlock_value));
if (value)
MUTEX_SETUP(value
->
mutex);
return value;
}
static void dyn_lock_function(int mode, struct CRYPTO_dynlock_value *l, const char *file, int line)
{
if (mode & CRYPTO_LOCK)
MUTEX_LOCK(l
->
mutex);
else
MUTEX_UNLOCK(l
->
mutex);
}
static void dyn_destroy_function(struct CRYPTO_dynlock_value *l, const char *file, int line)
{
MUTEX_CLEANUP(l
-
>mutex);
free(l);
}
void locking_function(int mode, int n, const char *file, int line)
{
if (mode & CRYPTO_LOCK)
MUTEX_LOCK(mutex_buf[n]);
else
MUTEX_UNLOCK(mutex_buf[n]);
}
unsigned long id_function()
{
return (unsigned long)THREAD_ID;
}
int CRYPTO_thread_setup()
{
int i;
mutex_buf = (MUTEX_TYPE*)malloc(CRYPTO_num_locks() * sizeof(MUTEX_TYPE));
if (!mutex_buf)
return SOAP_EOM;
for (i = 0; i < CRYPTO_num_locks(); i++)
MUTEX_SETUP(mutex_buf[i]);
CRYPTO_set_id_callback(id_function);
CRYPTO_set_locking_callback(locking_function);
CRYPTO_set_dynlock_create_callback(dyn_create_function);
CRYPTO_set_dynlock_lock_callback(dyn_lock_function);
CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function);
return SOAP_OK;
}
void CRYPTO_thread_cleanup()
{
int i;
if (!mutex_buf)
return;
CRYPTO_set_id_callback(NULL);
CRYPTO_set_locking_callback(NULL);
CRYPTO_set_dynlock_create_callback(NULL);
CRYPTO_set_dynlock_lock_callback(NULL);
CRYPTO_set_dynlock_destroy_callback(NULL);
for (i = 0; i < CRYPTO_num_locks(); i++)
MUTEX_CLEANUP(mutex_buf[i]);
free(mutex_buf);
mutex_buf = NULL;
}
欢迎光临我的blog: http://www.info-life.cn