TarsCloud_TarsCpp/servant/libservant/NetworkUtil.cpp

106 lines
2.4 KiB
C++
Raw Normal View History

2020-02-07 13:50:04 +08:00
/**
2018-09-05 11:26:21 +08:00
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (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/BSD-3-Clause
*
* 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.
*/
#include "servant/NetworkUtil.h"
#include "servant/Global.h"
#include "util/tc_epoller.h"
2020-01-28 21:51:45 +08:00
#include "util/tc_port.h"
2018-09-05 11:26:21 +08:00
#include <sstream>
#include <assert.h>
using namespace std;
using namespace tars;
2020-01-28 21:51:45 +08:00
int NetworkUtil::createSocket(bool udp, bool isLocal, bool isIpv6)
2018-09-05 11:26:21 +08:00
{
2020-01-28 21:51:45 +08:00
#if TARGET_PLATFORM_WINDOWS
int domain = (isIpv6 ? PF_INET6 : PF_INET);
#else
2018-10-23 10:11:06 +08:00
int domain = isLocal ? PF_LOCAL : (isIpv6 ? PF_INET6 : PF_INET);
2020-01-28 21:51:45 +08:00
#endif
2018-09-05 11:26:21 +08:00
2020-01-28 21:51:45 +08:00
int type = udp ? SOCK_DGRAM : SOCK_STREAM;
// int protocol = udp ? IPPROTO_UDP : IPPROTO_TCP;
2018-09-05 11:26:21 +08:00
2020-01-28 21:51:45 +08:00
TC_Socket s;
s.createSocket(type, domain);
2018-09-05 11:26:21 +08:00
2020-01-28 21:51:45 +08:00
if(!udp)
2018-09-05 11:26:21 +08:00
{
2020-01-28 21:51:45 +08:00
s.setTcpNoDelay();
s.setKeepAlive();
2018-09-05 11:26:21 +08:00
}
else
{
2020-01-28 21:51:45 +08:00
s.setRecvBufferSize(512*1024);
2018-09-05 11:26:21 +08:00
}
2020-01-28 21:51:45 +08:00
s.setOwner(false);
s.setblock(false);
return s.getfd();
2018-09-05 11:26:21 +08:00
}
2020-01-28 21:51:45 +08:00
void NetworkUtil::closeSocketNoThrow(int fd)
2018-10-23 10:11:06 +08:00
{
2020-01-28 21:51:45 +08:00
TC_Port::closeSocket(fd);
2018-10-23 10:11:06 +08:00
}
2020-01-28 21:51:45 +08:00
//bool NetworkUtil::doConnect(int fd, const struct sockaddr& addr)
//{
// bool bConnected = false;
//
// int iRet = ::connect(fd, (struct sockaddr*)(&addr), int(sizeof(addr)));
//
// if (iRet == 0)
// {
// bConnected = true;
// }
// else if (!TC_Socket::isInProgress())
// {
// closeSocketNoThrow(fd);
2020-01-29 21:04:15 +08:00
// TARS_THROW_EXCEPTION_SYSCODE(TafNetConnectException, "NetworkUtil::doConnect error");
2020-01-28 21:51:45 +08:00
// }
//
// return bConnected;
//}
2018-09-05 11:26:21 +08:00
2018-10-23 10:11:06 +08:00
bool NetworkUtil::doConnect(int fd, const struct sockaddr *addr, socklen_t len)
{
2020-01-28 21:51:45 +08:00
bool bConnected = false;
2018-10-23 10:11:06 +08:00
2020-01-28 21:51:45 +08:00
int iRet = ::connect(fd, addr, len);
2018-10-23 10:11:06 +08:00
2020-01-28 21:51:45 +08:00
if (iRet == 0)
{
bConnected = true;
}
else if (!TC_Socket::isInProgress())
{
closeSocketNoThrow(fd);
2020-01-29 21:04:15 +08:00
TARS_THROW_EXCEPTION_SYSCODE(TarsNetConnectException, "NetworkUtil::doConnect error");
2020-01-28 21:51:45 +08:00
}
2018-10-23 10:11:06 +08:00
2020-01-28 21:51:45 +08:00
return bConnected;
2018-10-23 10:11:06 +08:00
}
2018-09-05 11:26:21 +08:00
string NetworkUtil::errorToString(int error)
{
return strerror(error);
}
2020-01-28 21:51:45 +08:00