f-stack/app/micro_thread/mt_api.cpp

1330 lines
36 KiB
C++
Raw Normal View History

2017-04-21 10:43:26 +00:00
/**
* Tencent is pleased to support the open source community by making MSEC available.
*
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the GNU General Public License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* https://opensource.org/licenses/GPL-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
/**
* @filename mt_sys_call.cpp
* @info ΢<EFBFBD>̷߳<EFBFBD>װϵͳapi, ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>΢<EFBFBD>߳<EFBFBD>API<EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
#include "kqueue_proxy.h"
#include "micro_thread.h"
#include "mt_connection.h"
#include "mt_api.h"
#include "ff_api.h"
#include "mt_sys_hook.h"
namespace NS_MICRO_THREAD {
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿ڵ<EFBFBD>socket<EFBFBD>շ<EFBFBD><EFBFBD>ӿ<EFBFBD>, <EFBFBD><EFBFBD>socket<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param dst -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>Ŀ<EFBFBD>ĵ<EFBFBD>ַ
* @param pkg -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD>
* @param len -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param rcv_buf -<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff
* @param buf_size -modify-<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff<EFBFBD><EFBFBD>С, <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ, <EFBFBD>޸<EFBFBD>ΪӦ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param timeout -<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>λms
* @return 0 <EFBFBD>ɹ<EFBFBD>, -1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>socketʧ<EFBFBD><EFBFBD>, -2 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -3 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, <EFBFBD>ɴ<EFBFBD>ӡerrno, -10 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
*/
int mt_udpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout)
{
int ret = 0;
int rc = 0;
int flags = 1;
struct sockaddr_in from_addr = {0};
int addr_len = sizeof(from_addr);
if(len<1 || buf_size<1 ||!dst || !pkg || !rcv_buf)
{
MTLOG_ERROR("mt_udpsendrcv input params invalid, dst[%p], pkg[%p], rcv_buf[%p], len[%d], buf_size[%d]",
dst, pkg, rcv_buf, len, buf_size);
return -10;
}
int sock = socket(PF_INET, SOCK_DGRAM, 0);
if ((sock < 0) || (ioctl(sock, FIONBIO, &flags) < 0))
{
MT_ATTR_API(320842, 1); // socketʧ<74><CAA7>
MTLOG_ERROR("mt_udpsendrcv new sock failed, sock: %d, errno: %d (%m)", sock, errno);
ret = -1;
goto EXIT_LABEL;
}
rc = MtFrame::sendto(sock, pkg, len, 0, (struct sockaddr*)dst, (int)sizeof(*dst), timeout);
if (rc < 0)
{
MT_ATTR_API(320844, 1); // <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
MTLOG_ERROR("mt_udpsendrcv send failed, rc: %d, errno: %d (%m)", rc, errno);
ret = -2;
goto EXIT_LABEL;
}
rc = MtFrame::recvfrom(sock, rcv_buf, buf_size, 0, (struct sockaddr*)&from_addr, (socklen_t*)&addr_len, timeout);
if (rc < 0)
{
MT_ATTR_API(320845, 1); // <20><><EFBFBD><EFBFBD>δ<EFBFBD><CEB4>ȫ<EFBFBD>ɹ<EFBFBD>
MTLOG_ERROR("mt_udpsendrcv recv failed, rc: %d, errno: %d (%m)", rc, errno);
ret = -3;
goto EXIT_LABEL;
}
buf_size = rc;
EXIT_LABEL:
if (sock > 0)
{
close(sock);
sock = -1;
}
return ret;
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>TCP<EFBFBD>׽<EFBFBD><EFBFBD>֣<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return >=0 <EFBFBD>ɹ<EFBFBD>, <0 ʧ<EFBFBD><EFBFBD>
*/
int mt_tcp_create_sock(void)
{
int fd;
int flag;
// <20><><EFBFBD><EFBFBD>socket
fd = ::socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
{
MTLOG_ERROR("create tcp socket failed, error: %m");
return -1;
}
// <20><><EFBFBD><EFBFBD>socket<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
flag = fcntl(fd, F_GETFL, 0);
if (flag == -1)
{
::close(fd);
MTLOG_ERROR("get fd flags failed, error: %m");
return -2;
}
if (flag & O_NONBLOCK)
return fd;
if (fcntl(fd, F_SETFL, flag | O_NONBLOCK | O_NDELAY) == -1)
{
::close(fd);
MTLOG_ERROR("set fd flags failed, error: %m");
return -3;
}
return fd;
}
/**
* @brief TCP<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֪ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>socket
*/
static TcpKeepConn* mt_tcp_get_keep_conn(struct sockaddr_in* dst, int& sock)
{
// 1. <20><>ȡ<EFBFBD>߳<EFBFBD>֪ͨע<D6AA><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
KqueuerObj* ntfy_obj = NtfyObjMgr::Instance()->GetNtfyObj(NTFY_OBJ_THREAD, 0);
if (NULL == ntfy_obj)
{
MTLOG_ERROR("get notify failed, logit");
return NULL;
}
// 2. <20><>ȡ<EFBFBD><C8A1><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>, <20><><EFBFBD><EFBFBD>֪ͨ<CDA8><D6AA>Ϣ
TcpKeepConn* conn = dynamic_cast<TcpKeepConn*>(ConnectionMgr::Instance()->GetConnection(OBJ_TCP_KEEP, dst));
if (NULL == conn)
{
MTLOG_ERROR("get connection failed, dst[%p]", dst);
NtfyObjMgr::Instance()->FreeNtfyObj(ntfy_obj);
return NULL;
}
conn->SetNtfyObj(ntfy_obj);
// 3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>socket<65><74><EFBFBD><EFBFBD>
int osfd = conn->CreateSocket();
if (osfd < 0)
{
ConnectionMgr::Instance()->FreeConnection(conn, true);
MTLOG_ERROR("create socket failed, ret[%d]", osfd);
return NULL;
}
// 4. <20>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
sock = osfd;
return conn;
}
/**
* @brief TCPѭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OK<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
* [ע<EFBFBD><EFBFBD>] <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߲<EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>mt_tcpsendrcv<EFBFBD>ȵ<EFBFBD><EFBFBD>ýӿڳ<EFBFBD>ͻ [<EFBFBD><EFBFBD>Ҫ]
*/
static int mt_tcp_check_recv(int sock, char* rcv_buf, int &len, int flags, int timeout, MtFuncTcpMsgLen func)
{
int recv_len = 0;
utime64_t start_ms = MtFrame::Instance()->GetLastClock();
do
{
utime64_t cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
if (cost_time > (utime64_t)timeout)
{
errno = ETIME;
MTLOG_ERROR("tcp socket[%d] recv not ok, timeout", sock);
return -3;
}
int rc = MtFrame::recv(sock, (rcv_buf + recv_len), (len - recv_len), 0, (timeout - (int)cost_time));
if (rc < 0)
{
MTLOG_ERROR("tcp socket[%d] recv failed ret[%d][%m]", sock, rc);
return -3;
}
else if (rc == 0)
{
len = recv_len;
MTLOG_ERROR("tcp socket[%d] remote close", sock);
return -7;
}
recv_len += rc;
/* <20><><EFBFBD><EFBFBD><E9B1A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
rc = func(rcv_buf, recv_len);
if (rc < 0)
{
MTLOG_ERROR("tcp socket[%d] user check pkg error[%d]", sock, rc);
return -5;
}
else if (rc == 0) // <20><><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
if (len == recv_len) // û<>ռ<EFBFBD><D5BC>ٽ<EFBFBD><D9BD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>
{
MTLOG_ERROR("tcp socket[%d] user check pkg not ok, but no more buff", sock);
return -6;
}
continue;
}
else // <20>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>
{
if (rc > recv_len) // <20><><EFBFBD>Ļ<EFBFBD>δ<EFBFBD><CEB4>ȫ
{
continue;
}
else
{
len = rc;
break;
}
}
} while (true);
return 0;
}
/**
* @brief TCP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳصķ<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP/PORT<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>ӱ<EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><EFBFBD>10<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* [ע<EFBFBD><EFBFBD>] tcp<EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD><EFBFBD><EFBFBD>buff, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>static<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD> [<EFBFBD><EFBFBD>Ҫ]
* [ע<EFBFBD><EFBFBD>] <EFBFBD>޸Ľӿڣ<EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸ķ<EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD>mt_tcpsendrcv_ex<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵƥ<EFBFBD><EFBFBD> [<EFBFBD><EFBFBD>Ҫ]
* @param dst -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>Ŀ<EFBFBD>ĵ<EFBFBD>ַ
* @param pkg -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD>
* @param len -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param rcv_buf -<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff
* @param buf_size -modify-<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff<EFBFBD><EFBFBD>С, <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ, <EFBFBD>޸<EFBFBD>ΪӦ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param timeout -<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>λms
* @param check_func -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return 0 <EFBFBD>ɹ<EFBFBD>, -1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>socketʧ<EFBFBD><EFBFBD>, -2 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -3 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>,
* -4 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -6 <EFBFBD><EFBFBD><EFBFBD>տռ<EFBFBD><EFBFBD>, -7 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>-10 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
*/
int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout, MtFuncTcpMsgLen func)
{
if (!dst || !pkg || !rcv_buf || !func || len<1 || buf_size<1)
{
MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], rcv_buf[%p], fun[%p], len[%d], buf_size[%d]",
dst, pkg, rcv_buf, func, len, buf_size);
return -10;
}
int ret = 0, rc = 0;
int addr_len = sizeof(struct sockaddr_in);
utime64_t start_ms = MtFrame::Instance()->GetLastClock();
utime64_t cost_time = 0;
int time_left = timeout;
// 1. <20><>ȡTCP<43><50><EFBFBD>ӳض<D3B3><D8B6><EFBFBD>, <20>ҽ<EFBFBD>֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD>
int sock = -1;
TcpKeepConn* conn = mt_tcp_get_keep_conn(dst, sock);
if ((conn == NULL) || (sock < 0))
{
MTLOG_ERROR("socket[%d] get conn failed, ret[%m]", sock);
ret = -1;
goto EXIT_LABEL;
}
// 2. <20><><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD>
rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] connect failed, ret[%d][%m]", sock, rc);
ret = -4;
goto EXIT_LABEL;
}
// 3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0;
rc = MtFrame::send(sock, pkg, len, 0, time_left);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] send failed, ret[%d][%m]", sock, rc);
ret = -2;
goto EXIT_LABEL;
}
// 4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0;
rc = mt_tcp_check_recv(sock, (char*)rcv_buf, buf_size, 0, time_left, func);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] rcv failed, ret[%d][%m]", sock, rc);
ret = rc;
goto EXIT_LABEL;
}
ret = 0;
EXIT_LABEL:
// ʧ<><CAA7><EFBFBD><EFBFBD>ǿ<EFBFBD><C7BF><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
if (conn != NULL)
{
ConnectionMgr::Instance()->FreeConnection(conn, (ret < 0));
}
return ret;
}
/**
* @brief TCP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* [ע<EFBFBD><EFBFBD>] tcp<EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD><EFBFBD><EFBFBD>buff, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>static<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD> [<EFBFBD><EFBFBD>Ҫ]
* [ע<EFBFBD><EFBFBD>] <EFBFBD>޸Ľӿڣ<EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸ķ<EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD>mt_tcpsendrcv_ex<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵƥ<EFBFBD><EFBFBD> [<EFBFBD><EFBFBD>Ҫ]
* @param dst -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>Ŀ<EFBFBD>ĵ<EFBFBD>ַ
* @param pkg -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD>
* @param len -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param rcv_buf -<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff
* @param buf_size -modify-<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff<EFBFBD><EFBFBD>С, <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ, <EFBFBD>޸<EFBFBD>ΪӦ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param timeout -<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>λms
* @param check_func -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return 0 <EFBFBD>ɹ<EFBFBD>, -1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>socketʧ<EFBFBD><EFBFBD>, -2 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -3 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>,
* -4 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -6 <EFBFBD><EFBFBD><EFBFBD>տռ<EFBFBD><EFBFBD>, -7 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>-10 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
*/
int mt_tcpsendrcv_short(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout, MtFuncTcpMsgLen func)
{
int ret = 0, rc = 0;
int addr_len = sizeof(struct sockaddr_in);
utime64_t start_ms = MtFrame::Instance()->GetLastClock();
utime64_t cost_time = 0;
int time_left = timeout;
// 1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (!dst || !pkg || !rcv_buf || !func || len<1 || buf_size<1)
{
MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], rcv_buf[%p], fun[%p], len[%d], buf_size[%d]",
dst, pkg, rcv_buf, func, len, buf_size);
return -10;
}
// 2. <20><><EFBFBD><EFBFBD>TCP socket
int sock;
sock = mt_tcp_create_sock();
if (sock < 0)
{
MTLOG_ERROR("create tcp socket failed, ret: %d", sock);
return -1;
}
// 3. <20><><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD>
rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] connect failed, ret[%d][%m]", sock, rc);
ret = -4;
goto EXIT_LABEL;
}
// 4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0;
rc = MtFrame::send(sock, pkg, len, 0, time_left);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] send failed, ret[%d][%m]", sock, rc);
ret = -2;
goto EXIT_LABEL;
}
// 5. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0;
rc = mt_tcp_check_recv(sock, (char*)rcv_buf, buf_size, 0, time_left, func);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] rcv failed, ret[%d][%m]", sock, rc);
ret = rc;
goto EXIT_LABEL;
}
ret = 0;
EXIT_LABEL:
if (sock >= 0)
::close(sock);
return ret;
}
/**
* @brief TCP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳصķ<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP/PORT<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>ӱ<EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><EFBFBD>10<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* [ע<EFBFBD><EFBFBD>] tcp<EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>static<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD> [<EFBFBD><EFBFBD>Ҫ]
* [ע<EFBFBD><EFBFBD>] <EFBFBD>޸Ľӿڣ<EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸ķ<EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD>mt_tcpsendrcv_ex<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵƥ<EFBFBD><EFBFBD> [<EFBFBD><EFBFBD>Ҫ]
* @param dst -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>Ŀ<EFBFBD>ĵ<EFBFBD>ַ
* @param pkg -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD>
* @param len -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param timeout -<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>λms
* @return 0 <EFBFBD>ɹ<EFBFBD>, -1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>socketʧ<EFBFBD><EFBFBD>, -2 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -4 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -10 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
*/
int mt_tcpsend(struct sockaddr_in* dst, void* pkg, int len, int timeout)
{
if (!dst || !pkg || len<1)
{
MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], len[%d]", dst, pkg, len);
return -10;
}
int ret = 0, rc = 0;
int addr_len = sizeof(struct sockaddr_in);
utime64_t start_ms = MtFrame::Instance()->GetLastClock();
utime64_t cost_time = 0;
int time_left = timeout;
// 1. <20><>ȡTCP<43><50><EFBFBD>ӳض<D3B3><D8B6><EFBFBD>, <20>ҽ<EFBFBD>֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD>
int sock = -1;
TcpKeepConn* conn = mt_tcp_get_keep_conn(dst, sock);
if ((conn == NULL) || (sock < 0))
{
MTLOG_ERROR("socket[%d] get conn failed, ret[%m]", sock);
ret = -1;
goto EXIT_LABEL;
}
// 2. <20><><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD>
rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] connect failed, ret[%d][%m]", sock, rc);
ret = -4;
goto EXIT_LABEL;
}
// 3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0;
rc = MtFrame::send(sock, pkg, len, 0, time_left);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] send failed, ret[%d][%m]", sock, rc);
ret = -2;
goto EXIT_LABEL;
}
ret = 0;
EXIT_LABEL:
// ʧ<><CAA7><EFBFBD><EFBFBD>ǿ<EFBFBD><C7BF><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
if (conn != NULL)
{
ConnectionMgr::Instance()->FreeConnection(conn, (ret < 0));
}
return ret;
}
/**
* @brief TCP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>սӿ<EFBFBD>
* [ע<EFBFBD><EFBFBD>] tcp<EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>static<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD> [<EFBFBD><EFBFBD>Ҫ]
* [ע<EFBFBD><EFBFBD>] <EFBFBD>޸Ľӿڣ<EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸ķ<EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD>mt_tcpsendrcv_ex<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵƥ<EFBFBD><EFBFBD> [<EFBFBD><EFBFBD>Ҫ]
* @param dst -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>Ŀ<EFBFBD>ĵ<EFBFBD>ַ
* @param pkg -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD>
* @param len -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param timeout -<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>λms
* @return 0 <EFBFBD>ɹ<EFBFBD>, -1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>socketʧ<EFBFBD><EFBFBD>, -2 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -4 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -10 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
*/
int mt_tcpsend_short(struct sockaddr_in* dst, void* pkg, int len, int timeout)
{
// 1. <20><><EFBFBD>μ<EFBFBD><CEBC><EFBFBD>
if (!dst || !pkg || len<1)
{
MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], len[%d]", dst, pkg, len);
return -10;
}
int ret = 0, rc = 0;
int addr_len = sizeof(struct sockaddr_in);
utime64_t start_ms = MtFrame::Instance()->GetLastClock();
utime64_t cost_time = 0;
int time_left = timeout;
// 2. <20><><EFBFBD><EFBFBD>TCP socket
int sock = -1;
sock = mt_tcp_create_sock();
if (sock < 0)
{
MTLOG_ERROR("create tcp socket failed, ret: %d", sock);
ret = -1;
goto EXIT_LABEL;
}
// 2. <20><><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD>
rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] connect failed, ret[%d][%m]", sock, rc);
ret = -4;
goto EXIT_LABEL;
}
// 3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0;
rc = MtFrame::send(sock, pkg, len, 0, time_left);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] send failed, ret[%d][%m]", sock, rc);
ret = -2;
goto EXIT_LABEL;
}
ret = 0;
EXIT_LABEL:
if (sock >= 0)
::close(sock);
return ret;
}
/**
* @brief TCP<EFBFBD>շ<EFBFBD><EFBFBD>ӿڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˱<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD><EFBFBD>߶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* [ע<EFBFBD><EFBFBD>] tcp<EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD><EFBFBD><EFBFBD>buff, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>static<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD> [<EFBFBD><EFBFBD>Ҫ]
* @param dst -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>Ŀ<EFBFBD>ĵ<EFBFBD>ַ
* @param pkg -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD>
* @param len -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param rcv_buf -<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff<EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>տ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪNULL
* @param buf_size -modify-<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff<EFBFBD><EFBFBD>С, <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ, <EFBFBD>޸<EFBFBD>ΪӦ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>գ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪNULL
* @param timeout -<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>λms
* @param check_func -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param type - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* MT_TCP_SHORT: һ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>
* MT_TCP_LONG : һ<EFBFBD><EFBFBD>һ<EFBFBD>ճ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>
* MT_TCP_LONG_SNDONLY : ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ճ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>
* MT_TCP_SHORT_SNDONLY: ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ն<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>
* @return 0 <EFBFBD>ɹ<EFBFBD>, -1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>socketʧ<EFBFBD><EFBFBD>, -2 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -3 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>,
* -4 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -6 <EFBFBD><EFBFBD><EFBFBD>տռ<EFBFBD><EFBFBD>, -7 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, -10 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
*/
int mt_tcpsendrcv_ex(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int* buf_size, int timeout, MtFuncTcpMsgLen func, MT_TCP_CONN_TYPE type)
{
if(!dst || !pkg || len<1)
{
MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], rcv_buf[%p], fun[%p], len[%d], buf_size[%p]type[%d]",
dst, pkg, rcv_buf, func, len, buf_size,type);
return -10;
}
switch (type)
{
// TCP<43><50><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
case MT_TCP_LONG:
{
return mt_tcpsendrcv(dst, pkg, len, rcv_buf, *buf_size, timeout, func);
}
// TCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
case MT_TCP_LONG_SNDONLY:
{
return mt_tcpsend(dst, pkg, len, timeout);
}
// TCP<43><50><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
case MT_TCP_SHORT:
{
return mt_tcpsendrcv_short(dst, pkg, len, rcv_buf, *buf_size, timeout, func);
}
// TCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
case MT_TCP_SHORT_SNDONLY:
{
return mt_tcpsend_short(dst, pkg, len, timeout);
}
default:
{
MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], rcv_buf[%p], fun[%p], len[%d], buf_size[%p]type[%d]",
dst, pkg, rcv_buf, func, len, buf_size,type);
return -10;
}
}
return 0;
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
static void mt_task_process(void* arg)
{
int rc = 0;
IMtTask* task = (IMtTask*)arg;
if (!task)
{
MTLOG_ERROR("Invalid arg, error");
return;
}
rc = task->Process();
if (rc != 0)
{
MTLOG_DEBUG("task process failed(%d), log", rc);
}
task->SetResult(rc);
return;
};
/**
* @brief <EFBFBD><EFBFBD>·IO<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̹߳<EFBFBD><EFBFBD><EFBFBD>
* @param req_list - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
* @return 0 <EFBFBD>ɹ<EFBFBD>, <0ʧ<EFBFBD><EFBFBD>
*/
int mt_exec_all_task(IMtTaskList& req_list)
{
MtFrame* mtframe = MtFrame::Instance();
MicroThread* thread = mtframe->GetActiveThread();
IMtTask* task = NULL;
MicroThread* sub = NULL;
MicroThread* tmp = NULL;
int rc = -1;
MicroThread::SubThreadList list;
TAILQ_INIT(&list);
// <20><>ֹû<D6B9><C3BB>task<73><6B><EFBFBD><EFBFBD><EFBFBD><EFBFBD>΢<EFBFBD>߳<EFBFBD>һֱ<D2BB><D6B1><EFBFBD><EFBFBD>ס
if (0 == req_list.size())
{
MTLOG_DEBUG("no task for execult");
return 0;
}
// 1. <20><><EFBFBD><EFBFBD><EFBFBD>̶߳<DFB3><CCB6><EFBFBD>
for (IMtTaskList::iterator it = req_list.begin(); it != req_list.end(); ++it)
{
task = *it;
sub = MtFrame::CreateThread(mt_task_process, task, false);
if (NULL == sub)
{
MTLOG_ERROR("create sub thread failed");
goto EXIT_LABEL;
}
sub->SetType(MicroThread::SUB_THREAD);
TAILQ_INSERT_TAIL(&list, sub, _sub_entry);
}
// 2. <20><><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
TAILQ_FOREACH_SAFE(sub, &list, _sub_entry, tmp)
{
TAILQ_REMOVE(&list, sub, _sub_entry);
thread->AddSubThread(sub);
mtframe->InsertRunable(sub);
}
// 3. <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD>߳<EFBFBD>ִ<EFBFBD>н<EFBFBD><D0BD><EFBFBD>
thread->Wait();
rc = 0;
EXIT_LABEL:
TAILQ_FOREACH_SAFE(sub, &list, _sub_entry, tmp)
{
TAILQ_REMOVE(&list, sub, _sub_entry);
mtframe->FreeThread(sub);
}
return rc;
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰIMtMsg<EFBFBD><EFBFBD>˽<EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>
* @info ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>Ҫҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
void mt_set_msg_private(void *data)
{
MicroThread *msg_thread = MtFrame::Instance()->GetRootThread();
if (msg_thread != NULL)
msg_thread->SetPrivate(data);
}
/**
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ǰIMtMsg<EFBFBD><EFBFBD>˽<EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>
* @return ˽<EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
*/
void* mt_get_msg_private()
{
MicroThread *msg_thread = MtFrame::Instance()->GetRootThread();
if (NULL == msg_thread)
{
return NULL;
}
return msg_thread->GetPrivate();
}
/**
* @brief ΢<EFBFBD>߳̿<EFBFBD><EFBFBD>ܳ<EFBFBD>ʼ<EFBFBD><EFBFBD>
* @info ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>spp<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>΢<EFBFBD>̣߳<EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>øó<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return false:<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD> true:<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>
*/
bool mt_init_frame(const char *conf, int argc, char * const argv[])
{
if (conf && argc) {
ff_init(conf, argc, argv);
ff_set_hook_flag();
}
memset(&g_mt_syscall_tab, 0, sizeof(g_mt_syscall_tab));
return MtFrame::Instance()->InitFrame();
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>΢<EFBFBD>̶߳<EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD>ռ<EFBFBD><EFBFBD><EFBFBD>С
* @info <EFBFBD>DZ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD>Ĭ<EFBFBD>ϴ<EFBFBD>СΪ128K
*/
void mt_set_stack_size(unsigned int bytes)
{
ThreadPool::SetDefaultStackSize(bytes);
}
/**
* @brief ΢<EFBFBD>̰߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳIO<EFBFBD><EFBFBD><EFBFBD><EFBFBD> recvfrom
* @param fd ϵͳsocket<EFBFBD><EFBFBD>Ϣ
* @param buf <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
* @param len <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param from <EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
* @param fromlen <EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD>ַ<EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param timeout <EFBFBD><EFBFBD>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return >0 <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ճ<EFBFBD><EFBFBD><EFBFBD>, <0 ʧ<EFBFBD><EFBFBD>
*/
int mt_recvfrom(int fd, void *buf, int len, int flags, struct sockaddr *from, socklen_t *fromlen, int timeout)
{
return MtFrame::recvfrom(fd, buf, len, flags, from, fromlen, timeout);
}
/**
* @brief ΢<EFBFBD>̰߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳIO<EFBFBD><EFBFBD><EFBFBD><EFBFBD> sendto
* @param fd ϵͳsocket<EFBFBD><EFBFBD>Ϣ
* @param msg <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD>Ϣָ<EFBFBD><EFBFBD>
* @param len <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param to Ŀ<EFBFBD>ĵ<EFBFBD>ַ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
* @param tolen Ŀ<EFBFBD>ĵ<EFBFBD>ַ<EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param timeout <EFBFBD><EFBFBD>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return >0 <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD><EFBFBD>, <0 ʧ<EFBFBD><EFBFBD>
*/
int mt_sendto(int fd, const void *msg, int len, int flags, const struct sockaddr *to, int tolen, int timeout)
{
return MtFrame::sendto(fd, msg, len, flags, to, tolen, timeout);
}
/**
* @brief ΢<EFBFBD>̰߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳIO<EFBFBD><EFBFBD><EFBFBD><EFBFBD> connect
* @param fd ϵͳsocket<EFBFBD><EFBFBD>Ϣ
* @param addr ָ<EFBFBD><EFBFBD>server<EFBFBD><EFBFBD>Ŀ<EFBFBD>ĵ<EFBFBD>ַ
* @param addrlen <EFBFBD><EFBFBD>ַ<EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD>
* @param timeout <EFBFBD><EFBFBD>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return >0 <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD><EFBFBD>, <0 ʧ<EFBFBD><EFBFBD>
*/
int mt_connect(int fd, const struct sockaddr *addr, int addrlen, int timeout)
{
return MtFrame::connect(fd, addr, addrlen, timeout);
}
/**
* @brief ΢<EFBFBD>̰߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳIO<EFBFBD><EFBFBD><EFBFBD><EFBFBD> accept
* @param fd <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׽<EFBFBD><EFBFBD><EFBFBD>
* @param addr <EFBFBD>ͻ<EFBFBD><EFBFBD>˵<EFBFBD>ַ
* @param addrlen <EFBFBD><EFBFBD>ַ<EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD>
* @param timeout <EFBFBD><EFBFBD>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return >=0 accept<EFBFBD><EFBFBD>socket<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <0 ʧ<EFBFBD><EFBFBD>
*/
int mt_accept(int fd, struct sockaddr *addr, socklen_t *addrlen, int timeout)
{
return MtFrame::accept(fd, addr, addrlen, timeout);
}
/**
* @brief ΢<EFBFBD>̰߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳIO<EFBFBD><EFBFBD><EFBFBD><EFBFBD> read
* @param fd ϵͳsocket<EFBFBD><EFBFBD>Ϣ
* @param buf <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
* @param nbyte <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param timeout <EFBFBD><EFBFBD>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return >0 <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ճ<EFBFBD><EFBFBD><EFBFBD>, <0 ʧ<EFBFBD><EFBFBD>
*/
ssize_t mt_read(int fd, void *buf, size_t nbyte, int timeout)
{
return MtFrame::read(fd, buf, nbyte, timeout);
}
/**
* @brief ΢<EFBFBD>̰߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳIO<EFBFBD><EFBFBD><EFBFBD><EFBFBD> write
* @param fd ϵͳsocket<EFBFBD><EFBFBD>Ϣ
* @param buf <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD>Ϣָ<EFBFBD><EFBFBD>
* @param nbyte <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param timeout <EFBFBD><EFBFBD>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return >0 <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD><EFBFBD>, <0 ʧ<EFBFBD><EFBFBD>
*/
ssize_t mt_write(int fd, const void *buf, size_t nbyte, int timeout)
{
return MtFrame::write(fd, buf, nbyte, timeout);
}
/**
* @brief ΢<EFBFBD>̰߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳIO<EFBFBD><EFBFBD><EFBFBD><EFBFBD> recv
* @param fd ϵͳsocket<EFBFBD><EFBFBD>Ϣ
* @param buf <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
* @param len <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param timeout <EFBFBD><EFBFBD>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return >0 <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ճ<EFBFBD><EFBFBD><EFBFBD>, <0 ʧ<EFBFBD><EFBFBD>
*/
ssize_t mt_recv(int fd, void *buf, int len, int flags, int timeout)
{
return MtFrame::recv(fd, buf, len, flags, timeout);
}
/**
* @brief ΢<EFBFBD>̰߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳIO<EFBFBD><EFBFBD><EFBFBD><EFBFBD> send
* @param fd ϵͳsocket<EFBFBD><EFBFBD>Ϣ
* @param buf <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD>Ϣָ<EFBFBD><EFBFBD>
* @param nbyte <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param timeout <EFBFBD><EFBFBD>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return >0 <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD><EFBFBD>, <0 ʧ<EFBFBD><EFBFBD>
*/
ssize_t mt_send(int fd, const void *buf, size_t nbyte, int flags, int timeout)
{
return MtFrame::send(fd, buf, nbyte, flags, timeout);
}
/**
* @brief ΢<EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>sleep<EFBFBD>ӿ<EFBFBD>, <EFBFBD><EFBFBD>λms
*/
void mt_sleep(int ms)
{
MtFrame::sleep(ms);
}
/**
* @brief ΢<EFBFBD>̻߳<EFBFBD>ȡϵͳʱ<EFBFBD><EFBFBD><EFBFBD>λms
*/
unsigned long long mt_time_ms(void)
{
return MtFrame::Instance()->GetLastClock();
}
/**
* @brief ΢<EFBFBD>̵߳ȴ<EFBFBD>epoll<EFBFBD>¼<EFBFBD><EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
int mt_wait_events(int fd, int events, int timeout)
{
return MtFrame::Instance()->WaitEvents(fd, events, timeout);
}
void* mt_start_thread(void* entry, void* args)
{
return MtFrame::Instance()->CreateThread((ThreadStart)entry, args, true);
}
#define BUF_ALIGNMENT_SIZE 4096
#define BUF_ALIGN_SIZE(x) (((x)+BUF_ALIGNMENT_SIZE-1)&~(BUF_ALIGNMENT_SIZE-1))
#define BUF_DEFAULT_SIZE 4096
class ScopedBuf
{
public:
ScopedBuf(void*& buf_keeper, bool keep)
:buf_keeper_(buf_keeper),buf_(0),len_(0),len_watermark_(0),keep_(keep)
{}
int Alloc(int len)
{
if(len<len_)
{
return -1; // <20>ռ<EFBFBD><D5BC><EFBFBD>СԽ<D0A1><D4BD>
}
if(len==0)
{
len = BUF_ALIGNMENT_SIZE;
}
if(len_==len)
{
return 0;
}
len_ = BUF_ALIGN_SIZE(len);
if(len_==0)
{
len_ = BUF_DEFAULT_SIZE;
}
len_watermark_ = len_-BUF_ALIGNMENT_SIZE;
char* tmp = (char*)realloc(buf_, len_);
if(tmp==NULL)
{
return -2; // <20><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>ʧ<EFBFBD><CAA7>
}
buf_ = tmp;
return 0;
}
void reset()
{
if(keep_)
{
buf_keeper_ = (void*)buf_;
buf_ = NULL;
}
}
~ScopedBuf()
{
if(buf_!=NULL)
{
free(buf_);
buf_ = NULL;
}
}
public:
void* &buf_keeper_;
char* buf_;
int len_;
int len_watermark_;
bool keep_;
};
/**
* @brief TCPѭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OK<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
* [ע<EFBFBD><EFBFBD>] <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߲<EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>mt_tcpsendrcv<EFBFBD>ȵ<EFBFBD><EFBFBD>ýӿڳ<EFBFBD>ͻ [<EFBFBD><EFBFBD>Ҫ]
*/
static int mt_tcp_check_recv(int sock, void*& rcv_buf, int &len, int flags,
int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx, bool keep_rcv_buf)
{
int recv_len = 0;
utime64_t start_ms = MtFrame::Instance()->GetLastClock();
int rc = 0;
int ret = 0;
int pkg_len = 0;
bool msg_len_detected = false;
ScopedBuf sbuf(rcv_buf, keep_rcv_buf);
ret = sbuf.Alloc(len);
if(ret!=0)
{
MTLOG_ERROR("tcp socket[%d] recv failed ret[%d], alloc rcv buf failed, [%m]", sock, ret);
return -11;
}
do
{
utime64_t cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
if (cost_time > (utime64_t)timeout)
{
errno = ETIME;
MTLOG_ERROR("tcp socket[%d] recv not ok, timeout", sock);
return -3;
}
rc = MtFrame::recv(sock, (sbuf.buf_ + recv_len), (sbuf.len_ - recv_len), 0, (timeout - (int)cost_time));
if (rc < 0)
{
MTLOG_ERROR("tcp socket[%d] recv failed ret[%d][%m]", sock, rc);
return -3;
}
else if (rc == 0) // Զ<>˹ر<CBB9>
{
if(recv_len==0) // δ<>ذ<EFBFBD><D8B0><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD>Զ<EFBFBD>˹ر<CBB9>
{
MTLOG_ERROR("tcp socket[%d] remote close", sock);
return -7;
}
/* <20><><EFBFBD><EFBFBD><E9B1A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
rc = check_func(sbuf.buf_, recv_len, true, msg_ctx, msg_len_detected);
if(rc!=recv_len) // ҵ<><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>˹رգ<D8B1>Ӧ<EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD><=0,<2C><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
MTLOG_ERROR("tcp socket[%d] remote close", sock);
return -7;
}
len = recv_len;
break;
}
recv_len += rc;
/* <20><><EFBFBD><EFBFBD><E9B1A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if((!msg_len_detected)||recv_len==pkg_len)
{
rc = check_func(sbuf.buf_, recv_len, false, msg_ctx,msg_len_detected);
if(msg_len_detected)
{
pkg_len = rc;
}
}
else
{
rc = pkg_len;
}
if (rc < 0)
{
MTLOG_ERROR("tcp socket[%d] user check pkg error[%d]", sock, rc);
return -5;
}
else if (rc == 0) // <20><><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C>Ҳ<EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>С
{
if(sbuf.len_ > recv_len)
{
continue;
}
// û<>ռ<EFBFBD><D5BC>ٽ<EFBFBD><D9BD><EFBFBD><EFBFBD><EFBFBD>, 2<><32><EFBFBD><EFBFBD>С<EFBFBD><D0A1>չbuf
ret = sbuf.Alloc(sbuf.len_<<1);
if(ret!=0)
{
MTLOG_ERROR("tcp socket[%d] recv failed ret[%d], alloc rcv buf failed, [%m]", sock, ret);
return -11;
}
}
else // <20>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>
{
if (rc > recv_len) // <20><><EFBFBD>Ļ<EFBFBD>δ<EFBFBD><CEB4>ȫ
{
if(sbuf.len_ > recv_len) // recv buf<75><66><EFBFBD>пռ<D0BF>.<2E><>δ<EFBFBD><CEB4><EFBFBD><EFBFBD>ˮλ
{
continue;
}
// û<>ռ<EFBFBD><D5BC>ٽ<EFBFBD><D9BD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5>ָʾ<D6B8><CABE>С<EFBFBD><D0A1>չ<EFBFBD>ڴ<EFBFBD>
ret = sbuf.Alloc(rc);
if(ret!=0)
{
MTLOG_ERROR("tcp socket[%d] recv failed ret[%d], alloc rcv buf failed, [%m]", sock, ret);
return -11;
}
}
else if(rc==recv_len) // <20>հ<EFBFBD><D5B0><EFBFBD><EFBFBD><EFBFBD>
{
len = rc;
break;
}
else // ΢<>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ճ<EFBFBD><D5B3>
{
MTLOG_ERROR("tcp socket[%d] user check pkg error, pkg len < recv_len", sock);
return -5;
}
}
} while (true);
sbuf.reset();
return 0;
}
/**
* @brief TCP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳصķ<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP/PORT<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>ӱ<EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><EFBFBD>10<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* [ע<EFBFBD><EFBFBD>] tcp<EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD><EFBFBD><EFBFBD>buff, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>static<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD> [<EFBFBD><EFBFBD>Ҫ]
* @param dst -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>Ŀ<EFBFBD>ĵ<EFBFBD>ַ
* @param pkg -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD>
* @param len -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param rcv_buf -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ο<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> keep_rcv_buf<EFBFBD><EFBFBD>
* @param recv_pkg_size -<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff<EFBFBD>ij<EFBFBD>ʼ<EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ʳ<EFBFBD><EFBFBD>ȡ<EFBFBD>
* @param timeout -<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>λms
* @param check_func -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param msg_ctx -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param keep_rcv_buf -true,<EFBFBD><EFBFBD><EFBFBD>ܽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>rcv_buf<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷŸ<EFBFBD>buf<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD>ý<EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ͷŸ<EFBFBD>buf<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>
* ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD>msg_ctx<EFBFBD>б<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>malloc<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ
* @return 0 <EFBFBD>ɹ<EFBFBD>, -1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>socketʧ<EFBFBD><EFBFBD>, -2 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -3 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>,
* -4 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -6 <EFBFBD><EFBFBD><EFBFBD>տռ<EFBFBD><EFBFBD>, -7 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, -10 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
*/
int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, int& recv_pkg_size,
int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx, bool keep_rcv_buf)
{
if(!dst || !pkg || len<1)
{
MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], len[%d], fun[%p]",
dst, pkg, len, check_func);
return -10;
}
int ret = 0, rc = 0;
int addr_len = sizeof(struct sockaddr_in);
utime64_t start_ms = MtFrame::Instance()->GetLastClock();
utime64_t cost_time = 0;
int time_left = timeout;
// 1. <20><>ȡTCP<43><50><EFBFBD>ӳض<D3B3><D8B6><EFBFBD>, <20>ҽ<EFBFBD>֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD>
int sock = -1;
TcpKeepConn* conn = mt_tcp_get_keep_conn(dst, sock);
if ((conn == NULL) || (sock < 0))
{
MTLOG_ERROR("socket[%d] get conn failed, ret[%m]", sock);
ret = -1;
goto EXIT_LABEL;
}
// 2. <20><><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD>
rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] connect failed, ret[%d][%m]", sock, rc);
ret = -4;
goto EXIT_LABEL;
}
// 3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0;
rc = MtFrame::send(sock, pkg, len, 0, time_left);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] send failed, ret[%d][%m]", sock, rc);
ret = -2;
goto EXIT_LABEL;
}
// 4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0;
rc = mt_tcp_check_recv(sock, rcv_buf, recv_pkg_size, 0, time_left, check_func, msg_ctx, keep_rcv_buf);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] rcv failed, ret[%d][%m]", sock, rc);
ret = rc;
goto EXIT_LABEL;
}
ret = 0;
EXIT_LABEL:
// ʧ<><CAA7><EFBFBD><EFBFBD>ǿ<EFBFBD><C7BF><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
if (conn != NULL)
{
ConnectionMgr::Instance()->FreeConnection(conn, (ret < 0));
}
return ret;
}
/**
* @brief TCP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* [ע<EFBFBD><EFBFBD>] tcp<EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD><EFBFBD><EFBFBD>buff, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>static<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD> [<EFBFBD><EFBFBD>Ҫ]
* [ע<EFBFBD><EFBFBD>] <EFBFBD>޸Ľӿڣ<EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸ķ<EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD>mt_tcpsendrcv_ex<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵƥ<EFBFBD><EFBFBD> [<EFBFBD><EFBFBD>Ҫ]
* @param dst -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>Ŀ<EFBFBD>ĵ<EFBFBD>ַ
* @param pkg -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD>
* @param len -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param rcv_buf -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ο<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> keep_rcv_buf<EFBFBD><EFBFBD>
* @param recv_pkg_size -<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff<EFBFBD>ij<EFBFBD>ʼ<EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ʳ<EFBFBD><EFBFBD>ȡ<EFBFBD>
* @param timeout -<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>λms
* @param check_func -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param msg_ctx -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param keep_rcv_buf -true,<EFBFBD><EFBFBD><EFBFBD>ܽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>rcv_buf<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷŸ<EFBFBD>buf<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD>ý<EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ͷŸ<EFBFBD>buf<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>
* ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD>msg_ctx<EFBFBD>б<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>malloc<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ
* @return 0 <EFBFBD>ɹ<EFBFBD>, -1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>socketʧ<EFBFBD><EFBFBD>, -2 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -3 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>,
* -4 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -6 <EFBFBD><EFBFBD><EFBFBD>տռ<EFBFBD><EFBFBD>, -7 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, -10 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
*/
int mt_tcpsendrcv_short(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, int& recv_pkg_size,
int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx, bool keep_rcv_buf)
{
int ret = 0, rc = 0;
int addr_len = sizeof(struct sockaddr_in);
utime64_t start_ms = MtFrame::Instance()->GetLastClock();
utime64_t cost_time = 0;
int time_left = timeout;
// 1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(!dst || !pkg || len<1)
{
MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], len[%d], fun[%p]",
dst, pkg, len, check_func);
return -10;
}
// 2. <20><><EFBFBD><EFBFBD>TCP socket
int sock;
sock = mt_tcp_create_sock();
if (sock < 0)
{
MTLOG_ERROR("create tcp socket failed, ret: %d", sock);
return -1;
}
// 3. <20><><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD>
rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] connect failed, ret[%d][%m]", sock, rc);
ret = -4;
goto EXIT_LABEL;
}
// 4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0;
rc = MtFrame::send(sock, pkg, len, 0, time_left);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] send failed, ret[%d][%m]", sock, rc);
ret = -2;
goto EXIT_LABEL;
}
// 5. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD>
cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0;
rc = mt_tcp_check_recv(sock, rcv_buf, recv_pkg_size, 0, time_left, check_func, msg_ctx, keep_rcv_buf);
if (rc < 0)
{
MTLOG_ERROR("socket[%d] rcv failed, ret[%d][%m]", sock, rc);
ret = rc;
goto EXIT_LABEL;
}
ret = 0;
EXIT_LABEL:
if (sock >= 0)
::close(sock);
return ret;
}
/**
* @brief TCP<EFBFBD>շ<EFBFBD><EFBFBD>ӿڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˱<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD><EFBFBD>߶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* [ע<EFBFBD><EFBFBD>] tcp<EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD><EFBFBD><EFBFBD>buff, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>static<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD> [<EFBFBD><EFBFBD>Ҫ]
* @param dst -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>Ŀ<EFBFBD>ĵ<EFBFBD>ַ
* @param pkg -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD>
* @param len -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>İ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param rcv_buf -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ο<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> keep_rcv_buf<EFBFBD><EFBFBD>
* @param recv_pkg_size -<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff<EFBFBD>ij<EFBFBD>ʼ<EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ʳ<EFBFBD><EFBFBD>ȡ<EFBFBD>
* @param timeout -<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>λms
* @param check_func -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param msg_ctx -<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @param type - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* MT_TCP_SHORT: һ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>
* MT_TCP_LONG : һ<EFBFBD><EFBFBD>һ<EFBFBD>ճ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>
* MT_TCP_LONG_SNDONLY : ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ճ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>
* MT_TCP_SHORT_SNDONLY: ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ն<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>
* @param keep_rcv_buf -true,<EFBFBD><EFBFBD><EFBFBD>ܽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>rcv_buf<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷŸ<EFBFBD>buf<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD>ý<EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ͷŸ<EFBFBD>buf<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>
* ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD>msg_ctx<EFBFBD>б<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>malloc<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ
* @return 0 <EFBFBD>ɹ<EFBFBD>, -1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>socketʧ<EFBFBD><EFBFBD>, -2 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -3 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>,
* -4 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -5 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>, -6 <EFBFBD><EFBFBD><EFBFBD>տռ<EFBFBD><EFBFBD>, -7 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, -10 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
*/
int mt_tcpsendrcv_ex(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, int& rcv_pkg_size,
int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx,
MT_TCP_CONN_TYPE type, bool keep_rcv_buf)
{
if(!dst || !pkg || len<1)
{
MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], len[%d], fun[%p], msg_ctx[%p], type[%d]",
dst, pkg, len, check_func, msg_ctx, type);
return -10;
}
switch (type)
{
// TCP<43><50><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
case MT_TCP_LONG:
{
return mt_tcpsendrcv(dst, pkg, len, rcv_buf, rcv_pkg_size, timeout, check_func, msg_ctx, keep_rcv_buf);
}
// TCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
case MT_TCP_LONG_SNDONLY:
{
return mt_tcpsend(dst, pkg, len, timeout);
}
// TCP<43><50><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
case MT_TCP_SHORT:
{
return mt_tcpsendrcv_short(dst, pkg, len, rcv_buf, rcv_pkg_size, timeout, check_func, msg_ctx, keep_rcv_buf);
}
// TCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
case MT_TCP_SHORT_SNDONLY:
{
return mt_tcpsend_short(dst, pkg, len, timeout);
}
default:
{
MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], len[%d], fun[%p], msg_ctx[%p], type[%d]",
dst, pkg, len, check_func, msg_ctx, type);
return -10;
}
}
return 0;
}
}