acl/lib_acl_cpp/samples/ssl/ssl_client2/ssl_client.cpp

164 lines
3.7 KiB
C++
Raw Normal View History

// ssl_client.cpp : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨Ӧ<CCA8>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㡣
2014-12-07 23:48:11 +08:00
//
#include "stdafx.h"
#include <iostream>
#include "lib_acl.h"
2020-01-02 23:29:06 +08:00
#include "acl_cpp/lib_acl.hpp"
2014-12-07 23:48:11 +08:00
2020-01-02 23:29:06 +08:00
static acl::sslbase_conf* __ssl_conf;
2014-12-07 23:48:11 +08:00
static bool test(const char* addr, int k, int nloop)
{
acl::socket_stream client;
2020-01-02 23:29:06 +08:00
if (!client.open(addr, 60, 60)) {
2014-12-07 23:48:11 +08:00
std::cout << "connect " << addr << " error!" << std::endl;
2020-01-03 15:57:34 +08:00
exit (1);
2014-12-07 23:48:11 +08:00
}
acl::sslbase_io* ssl = __ssl_conf->create(false);
2020-01-02 23:29:06 +08:00
if (client.setup_hook(ssl) == ssl) {
2014-12-07 23:48:11 +08:00
std::cout << "open ssl " << addr << " error!" << std::endl;
ssl->destroy();
2020-01-03 15:57:34 +08:00
exit (1);
2014-12-07 23:48:11 +08:00
}
std::cout << "ssl handshake ok, k: " << k << std::endl;
2020-01-02 23:29:06 +08:00
for (int i = 0 ; i < nloop; i++) {
char line[1024], line2[1024];
2014-12-07 23:48:11 +08:00
memset(line, 'x', sizeof(line));
line[1023] = 0;
line[1022] = '\n';
2020-01-02 23:29:06 +08:00
if (client.write(line, strlen(line)) == -1) {
2014-12-07 23:48:11 +08:00
std::cout << "write to " << addr << " error!" << std::endl;
return false;
}
2020-01-02 23:29:06 +08:00
size_t n = sizeof(line2);
if (!client.gets(line2, &n)) {
2014-12-07 23:48:11 +08:00
std::cout << "gets from " << addr << " error!"
<< acl_last_serror() << std::endl;
return false;
}
2020-01-02 23:29:06 +08:00
if (memcmp(line, line2, n) != 0) {
std::cout << "read invalid line" << std::endl;
return false;
}
if (i < 1 && k < 10) {
std::cout << ">>gets(" << n << "): " << line2 << std::endl;
}
if (i > 0 && i % 1000 == 0) {
2014-12-07 23:48:11 +08:00
char buf[256];
snprintf(buf, sizeof(buf), "write count: %d", i);
ACL_METER_TIME(buf);
}
}
return true;
}
2020-01-02 23:29:06 +08:00
class test_thread : public acl::thread
{
public:
test_thread(const char* addr, int k, int max)
: addr_(addr), k_(k), max_(max) {}
~test_thread(void) {}
protected:
void* run(void)
{
(void) test(addr_, k_, max_);
return NULL;
}
private:
acl::string addr_;
int k_;
int max_;
};
2014-12-07 23:48:11 +08:00
static void usage(const char* procname)
{
printf("usage: %s -h[help]\r\n"
2020-01-02 23:29:06 +08:00
" -d path_to_ssl\r\n"
" -s server_addr[default: 127.0.0.1:9001]\r\n"
" -c max_connections[default: 10]\r\n"
" -n max_loop_per_connection[default: 10]\r\n", procname);
2014-12-07 23:48:11 +08:00
}
int main(int argc, char* argv[])
{
int ch, max_loop = 10, max_connections = 10;
acl::string addr("127.0.0.1:9001"), libpath("../libpolarssl.so");
2014-12-07 23:48:11 +08:00
acl::acl_cpp_init();
2020-01-02 23:29:06 +08:00
while ((ch = getopt(argc, argv, "hd:s:n:c:")) > 0) {
switch (ch) {
2014-12-07 23:48:11 +08:00
case 'h':
usage(argv[0]);
return 0;
case 'd':
libpath = optarg;
break;
2014-12-07 23:48:11 +08:00
case 's':
addr = optarg;
break;
case 'n':
max_loop = atoi(optarg);
break;
case 'c':
max_connections = atoi(optarg);
break;
default:
break;
}
}
2020-01-02 23:29:06 +08:00
if (libpath.find("mbedtls") != NULL) {
const std::vector<acl::string>& libs = libpath.split2(";");
if (libs.size() != 3) {
printf("invalid libpath=%s\r\n", libpath.c_str());
return 1;
}
acl::mbedtls_conf::set_libpath(libs[0], libs[1], libs[2]);
2020-01-02 23:29:06 +08:00
if (!acl::mbedtls_conf::load()) {
printf("load %s error\r\n", libpath.c_str());
return 1;
}
__ssl_conf = new acl::mbedtls_conf(false);
} else if (libpath.find("polarssl") != NULL) {
acl::polarssl_conf::set_libpath(libpath);
if (!acl::polarssl_conf::load()) {
printf("load %s error\r\n", libpath.c_str());
return 1;
}
__ssl_conf = new acl::polarssl_conf;
}
2020-01-02 23:29:06 +08:00
if (max_connections <= 0) {
2014-12-07 23:48:11 +08:00
max_connections = 100;
2020-01-02 23:29:06 +08:00
}
2014-12-07 23:48:11 +08:00
2020-01-02 23:29:06 +08:00
std::vector<acl::thread*> threads;
for (int i = 0; i < max_connections; i++) {
acl::thread* thr = new test_thread(addr, i, max_loop);
threads.push_back(thr);
thr->start();
2014-12-07 23:48:11 +08:00
}
2020-01-02 23:29:06 +08:00
for (std::vector<acl::thread*>::iterator it = threads.begin();
it != threads.end(); ++it) {
(*it)->wait(NULL);
delete *it;
}
2014-12-07 23:48:11 +08:00
printf("Over, enter any key to exit!\n");
getchar();
delete __ssl_conf;
2014-12-07 23:48:11 +08:00
return (0);
}