acl/lib_fiber/samples/WinEchod/FiberClient.cpp

67 lines
1.1 KiB
C++
Raw Normal View History

2018-01-10 17:48:22 +08:00
#include "stdafx.h"
2018-01-11 18:30:06 +08:00
#include "FiberClient.h"
2018-01-10 17:48:22 +08:00
2018-01-11 18:30:06 +08:00
CFiberClient::CFiberClient(acl::socket_stream* conn)
2018-01-10 17:48:22 +08:00
: m_conn(conn)
{
}
2018-01-11 18:30:06 +08:00
CFiberClient::~CFiberClient(void)
2018-01-10 17:48:22 +08:00
{
}
2018-01-11 18:30:06 +08:00
void CFiberClient::run(void)
2018-01-10 17:48:22 +08:00
{
#if 0
socket_t sock = m_conn->sock_handle();
while (true)
{
char buf[1024];
2018-01-12 22:29:21 +08:00
int ret = acl_fiber_recv(sock, buf, sizeof(buf) - 1, 0);
2018-01-11 19:13:31 +08:00
if (ret <= 0)
{
2018-01-12 19:15:08 +08:00
int err0 = acl_fiber_last_error();
int err1 = acl::last_error();
printf("recv error: %s, %d, %d\r\n",
acl::last_serror(), err0, err1);
break;
}
buf[ret] = 0;
2018-01-12 00:28:30 +08:00
//printf("recv=%d, [%s]\r\n", ret, buf);
2018-01-12 22:29:21 +08:00
if (acl_fiber_send(sock, buf, ret, 0) == -1)
{
printf("write error %s\r\n", acl::last_serror());
break;
}
}
2018-01-11 19:13:31 +08:00
2018-01-12 22:29:21 +08:00
acl_fiber_close(sock);
2018-01-11 19:13:31 +08:00
m_conn->unbind();
delete m_conn;
delete this;
#else
2018-01-10 17:48:22 +08:00
acl::string buf;
int n = 0;
m_conn->set_rw_timeout(10);
2018-01-10 17:48:22 +08:00
while (true)
{
if (m_conn->read(buf, false) == false)
2018-01-10 17:48:22 +08:00
{
printf("read error %s, count=%d\r\n",
acl::last_serror(), n);
2018-01-10 17:48:22 +08:00
break;
}
//printf("read: %s\r\n", buf.c_str());
2018-01-10 17:48:22 +08:00
if (m_conn->write(buf) == -1)
{
printf("write error %s\r\n", acl::last_serror());
2018-01-10 17:48:22 +08:00
break;
}
n++;
2018-01-10 17:48:22 +08:00
}
delete m_conn;
delete this;
2018-01-11 19:13:31 +08:00
#endif
2018-01-11 18:30:06 +08:00
}