2019-07-28 10:31:56 +08:00
|
|
|
#include "stdafx.h"
|
2022-01-18 23:58:40 +08:00
|
|
|
#include "tcp_transfer.h"
|
2019-03-14 15:24:11 +08:00
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
tcp_transfer::tcp_transfer(ACL_FIBER* parent, acl::socket_stream* in,
|
|
|
|
acl::socket_stream* out, bool running)
|
2022-02-15 22:34:10 +08:00
|
|
|
: acl::fiber(running)
|
2022-03-02 18:33:09 +08:00
|
|
|
, parent_(parent)
|
|
|
|
, me_(NULL)
|
|
|
|
, in_(in)
|
|
|
|
, out_(out)
|
|
|
|
, peer_(NULL)
|
2022-03-31 18:35:14 +08:00
|
|
|
, is_local_(false)
|
2019-03-14 15:24:11 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-19 23:47:53 +08:00
|
|
|
tcp_transfer::~tcp_transfer(void)
|
2019-03-14 15:24:11 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
void tcp_transfer::set_peer(tcp_transfer* peer)
|
2019-03-14 15:24:11 +08:00
|
|
|
{
|
2022-03-31 18:35:14 +08:00
|
|
|
peer_ = peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tcp_transfer::set_local(bool yes)
|
|
|
|
{
|
|
|
|
is_local_ = yes;
|
2019-03-14 15:24:11 +08:00
|
|
|
}
|
|
|
|
|
2022-02-16 23:01:54 +08:00
|
|
|
void tcp_transfer::unset_peer(void)
|
|
|
|
{
|
|
|
|
peer_ = NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-01 19:02:05 +08:00
|
|
|
void tcp_transfer::close(void)
|
|
|
|
{
|
2022-03-02 18:33:09 +08:00
|
|
|
printf(">>>close sockfd=%d, curr=%p, me=%p, parent=%p\r\n",
|
2022-03-31 18:35:14 +08:00
|
|
|
in_->sock_handle(), acl_fiber_running(), me_, parent_);
|
|
|
|
int fd = in_->sock_handle();
|
2022-03-01 19:02:05 +08:00
|
|
|
//in_.close();
|
2022-03-31 18:35:14 +08:00
|
|
|
in_->unbind_sock();
|
2022-03-02 18:33:09 +08:00
|
|
|
acl_fiber_close(fd);
|
2022-03-01 19:02:05 +08:00
|
|
|
//printf(">>>after close sockfd=%d\r\n", in_.sock_handle());
|
|
|
|
}
|
|
|
|
|
2022-01-18 23:58:40 +08:00
|
|
|
void tcp_transfer::wait(void)
|
2019-03-14 15:24:11 +08:00
|
|
|
{
|
|
|
|
(void) box_.pop();
|
|
|
|
}
|
|
|
|
|
2022-01-18 23:58:40 +08:00
|
|
|
void tcp_transfer::run(void)
|
2019-03-14 15:24:11 +08:00
|
|
|
{
|
2022-03-02 18:33:09 +08:00
|
|
|
me_ = acl_fiber_running();
|
2022-03-31 18:35:14 +08:00
|
|
|
|
2019-03-14 15:24:11 +08:00
|
|
|
char buf[8192];
|
2022-03-31 18:35:14 +08:00
|
|
|
|
2019-03-14 15:24:11 +08:00
|
|
|
while (true) {
|
2022-03-31 18:35:14 +08:00
|
|
|
int fd = in_->sock_handle();
|
|
|
|
int ret = in_->read(buf, sizeof(buf) - 1, false);
|
2019-03-14 15:24:11 +08:00
|
|
|
if (ret == -1) {
|
2022-03-02 18:33:09 +08:00
|
|
|
printf("%s: me=%p, parent=%p, peer=%p, read error %s,"
|
|
|
|
" fd=%d, %d\r\n", __FUNCTION__, me_, parent_,
|
|
|
|
peer_ ? peer_->peer_fiber() : NULL,
|
2022-03-31 18:35:14 +08:00
|
|
|
acl::last_serror(), in_->sock_handle(), fd);
|
2019-03-14 15:24:11 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-03-31 18:35:14 +08:00
|
|
|
buf[ret] = 0;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
printf("send from %s data, in=%d, out=%d\r\n",
|
|
|
|
is_local_ ? "local" : "remote",
|
|
|
|
in_->sock_handle(), out_->sock_handle());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (out_->write(buf, ret) == -1) {
|
|
|
|
printf(">>>write error\n");
|
2019-03-14 15:24:11 +08:00
|
|
|
break;
|
|
|
|
}
|
2022-03-31 18:35:14 +08:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
printf("send to %s data, in=%d, out=%d ok\r\n",
|
|
|
|
is_local_ ? "local" : "remote",
|
|
|
|
in_->sock_handle(), out_->sock_handle());
|
|
|
|
#endif
|
2019-03-14 15:24:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (peer_) {
|
2022-02-16 23:01:54 +08:00
|
|
|
peer_->unset_peer();
|
2022-03-01 19:02:05 +08:00
|
|
|
//peer_->kill();
|
|
|
|
peer_->close();
|
2019-03-14 15:24:11 +08:00
|
|
|
}
|
|
|
|
|
2022-02-20 11:39:46 +08:00
|
|
|
//printf("fd=%d, push\n", in_.sock_handle());
|
2019-03-14 15:24:11 +08:00
|
|
|
box_.push(NULL);
|
2022-03-01 19:02:05 +08:00
|
|
|
//printf("fd=%d, push done\n", in_.sock_handle());
|
2019-03-14 15:24:11 +08:00
|
|
|
}
|