本文整理汇总了C++中setBlocking函数的典型用法代码示例。如果您正苦于以下问题:C++ setBlocking函数的具体用法?C++ setBlocking怎么用?C++ setBlocking使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了setBlocking函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: assert
int Socket::connect(int timeoutInMillis) {
assert(serverInfo != NULL && "Socket is already connected");
setBlocking(false);
::connect(socketId, serverInfo->ai_addr, serverInfo->ai_addrlen);
struct timeval tv;
tv.tv_sec = timeoutInMillis / 1000;
tv.tv_usec = (timeoutInMillis - tv.tv_sec * 1000) * 1000;
fd_set mySet, err;
FD_ZERO(&mySet);
FD_ZERO(&err);
FD_SET(socketId, &mySet);
FD_SET(socketId, &err);
errno = 0;
if (select(socketId + 1, NULL, &mySet, &err, &tv) > 0) {
setBlocking(true);
return 0;
}
setBlocking(true);
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
int error = WSAGetLastError();
#else
int error = errno;
#endif
return error;
}
示例2: init
void SocketImpl::connect(const SocketAddress& address, const Poco::Timespan& timeout)
{
if (_sockfd == POCO_INVALID_SOCKET)
{
init(address.af());
}
setBlocking(false);
try
{
#if defined(POCO_VXWORKS)
int rc = ::connect(_sockfd, (sockaddr*) address.addr(), address.length());
#else
int rc = ::connect(_sockfd, address.addr(), address.length());
#endif
if (rc != 0)
{
int err = lastError();
if (err != POCO_EINPROGRESS && err != POCO_EWOULDBLOCK)
error(err, address.toString());
if (!poll(timeout, SELECT_READ | SELECT_WRITE | SELECT_ERROR))
throw Poco::TimeoutException("connect timed out", address.toString());
err = socketError();
if (err != 0) error(err);
}
}
catch (Poco::Exception&)
{
setBlocking(true);
throw;
}
setBlocking(true);
}
示例3: setBlocking
int EasySocket::connect()
{
if (server == NULL || m_socket <=0 ) {
return -1;
//fprintf(stderr,"ERROR, no such host\n");
}
int res = 0;
//TODO: more intelligence
#ifndef _WIN32
setBlocking(m_socket,false);
int counter = 0;
int sleepMs = 20;
int waitSec = 1;
int waitMs = waitSec*1000/sleepMs;
while(counter++ < waitMs)
{
res = ::connect(m_socket,(struct sockaddr *) &serv_addr,sizeof(serv_addr));
checkResult(res);
if (res == 0)
break;
if (m_state == CONNECTION_STATE_IN_PROGRESS)
{
std::this_thread::sleep_for(std::chrono::milliseconds(sleepMs));
continue;
}
if(m_state != CONNECTION_STATE_IN_PROGRESS && m_state != CONNECTION_STATE_SUCCESS )
break;
}
setBlocking(m_socket,true);
#else
res = ::connect(m_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
checkResult(res);
#endif
if(res == 0){
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
m_replySocket = m_socket;
}
return res;
}
示例4: lockR
bool CPPTcpClientSocket::connect(short port, unsigned int addr, int timeout, bool spin){
std::lock_guard<std::mutex> lockR(recvLock), lockS(sendLock);
m_port = port;
m_addr = addr;
struct sockaddr_in remote;
memset(&remote, 0, sizeof(remote));
remote.sin_family = AF_INET;
remote.sin_addr.s_addr = m_addr;
remote.sin_port = htons(m_port);
double tremaining = timeout;
auto start = std::chrono::system_clock::now();
bool success = false;
do{
if (!open()){
break;
}
setBlocking(false, false);
if (::connect(m_sock, (struct sockaddr *)&remote, sizeof(remote)) == 0){
success = true;
break;
}
if (errno != EINPROGRESS){
break;
}
struct pollfd pfd = {m_sock, POLLWRNORM, 0};
if (poll(&pfd, 1, (int)tremaining) > 0){
int optionValue = -1;
socklen_t optionLength = sizeof(optionValue);
if (getSocketOption(SOL_SOCKET, SO_ERROR, (void *)&optionValue, &optionLength, false) && optionValue == 0){
success = true;
break;
}
}
std::chrono::duration<double, std::milli> dur = std::chrono::system_clock::now() - start;
tremaining = timeout - dur.count();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
::shutdown(m_sock, SHUT_RDWR);
::close(m_sock);
m_sock = -1;
if (!spin) break;
}while((tremaining > 0.0 || timeout < 0) && !sigClose);
setBlocking(true, false);
if (success) return true;
if (m_sock != -1){
::shutdown(m_sock, SHUT_RDWR);
::close(m_sock);
m_sock=-1;
}
return false;
}
示例5: isBlocking
/// Will not buffer anything but always send right away. Blocks.
/// Any data that could not be send will block until it can be send or the connection is severed.
void Socket::Connection::SendNow(const char * data, size_t len) {
bool bing = isBlocking();
if (!bing) {
setBlocking(true);
}
unsigned int i = iwrite(data, std::min((long unsigned int)len, SOCKETSIZE));
while (i < len && connected()) {
i += iwrite(data + i, std::min((long unsigned int)(len - i), SOCKETSIZE));
}
if (!bing) {
setBlocking(false);
}
}
示例6: isBlocking
/// Updates the downbuffer and upbuffer internal variables until upbuffer is empty.
/// Returns true if new data was received, false otherwise.
bool Socket::Connection::flush(){
bool bing = isBlocking();
if (!bing){setBlocking(true);}
while (upbuffer.size() > 0 && connected()){
iwrite(upbuffer.get());
}
if (!bing){setBlocking(false);}
/// \todo Provide better mechanism to prevent overbuffering.
if (downbuffer.size() > 1000){
return true;
}else{
return iread(downbuffer);
}
}
示例7: warning
// --------------------------------------------------
void WSAClientSocket::bind(Host &h)
{
struct sockaddr_in localAddr;
#pragma warning(disable : 4244)
if ((sockNum = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
#pragma warning(default : 4244)
throw SockException("Can`t open socket");
setBlocking(false);
setReuse(true);
memset(&localAddr,0,sizeof(localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_port = htons(h.port);
localAddr.sin_addr.s_addr = INADDR_ANY;
if( ::bind (sockNum, (sockaddr *)&localAddr, sizeof(localAddr)) == -1)
throw SockException("Can`t bind socket");
if (::listen(sockNum,SOMAXCONN))
throw SockException("Can`t listen",WSAGetLastError());
host = h;
}
示例8: transmitString
void HttpMultipartClient::response()
{
condition.lock();
responding = true;
string boundary = "--RhobanMultipartBoundary\r\n";
ostringstream oss;
oss << "HTTP/1.0 200 OK\r\n";
oss << "Content-type: multipart/x-mixed-replace; boundary=" << boundary;
oss << "\r\n";
transmitString(oss.str());
char dummy;
setBlocking(false);
while (!isDead()) {
receive(&dummy, 1);
condition.wait();
oss.str("");
oss << boundary;
oss << "Content-type: " << type << "\r\n";
oss << "Content-length: " << frame->length() << "\r\n";
oss << "\r\n" << *frame;
transmitString(oss.str());
}
condition.unlock();
}
示例9: setInterfaceAttribs
int DLS::setFD (int fd)
{
fd_ = fd;
setInterfaceAttribs (B115200, 0); // set speed to 115,200 bps, 8n1 (no parity)
setBlocking (0); // set no blocking
return EXIT_SUCCESS;
}
示例10: setBlocking
void NetworkSocket::setupBehavior()
{
setBlocking(false);
setConnectionReset(false);
//setBroadCasting(true);
setReUseAddress(true);
}
示例11: RawSocket_create
/*
* Creates a socket suitable for raw socket operations. The socket is
* bound to the interface specified by the supplied name. The socket
* value is placed into the supplied FileDescriptor instance.
*
* TODO(chesnutt): consider breaking this into pieces: create a
* variety of constructors for different socket types, then a generic
* setBlocking() method followed by polymorphic bind().
*/
static void RawSocket_create(JNIEnv* env, jclass, jobject fileDescriptor,
jshort protocolType, jstring interfaceName)
{
ScopedUtfChars ifname(env, interfaceName);
if (ifname.c_str() == NULL) {
return;
}
memset(&su, 0, sizeof(su));
su.sll.sll_family = PF_PACKET;
su.sll.sll_protocol = htons(protocolType);
su.sll.sll_ifindex = if_nametoindex(ifname.c_str());
int sock = socket(PF_PACKET, SOCK_DGRAM, htons(protocolType));
if (sock == -1) {
ALOGE("Can't create socket %s", strerror(errno));
jniThrowSocketException(env, errno);
return;
}
jniSetFileDescriptorOfFD(env, fileDescriptor, sock);
if (!setBlocking(sock, false)) {
ALOGE("Can't set non-blocking mode on socket %s", strerror(errno));
jniThrowSocketException(env, errno);
return;
}
int err = bind(sock, &su.sa, sizeof(su));
if (err != 0) {
ALOGE("Socket bind error %s", strerror(errno));
jniThrowSocketException(env, errno);
return;
}
}
示例12: socket
// --------------------------------------------------
void UClientSocket::open(Host &rh)
{
sockNum = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockNum == INVALID_SOCKET)
throw SockException("Can`t open socket");
setBlocking(false);
#ifdef DISABLE_NAGLE
setNagle(false);
#endif
setBufSize(65535);
host = rh;
memset(&remoteAddr,0,sizeof(remoteAddr));
remoteAddr.sin_family = AF_INET;
remoteAddr.sin_port = htons(host.port);
remoteAddr.sin_addr.s_addr = htonl(host.ip);
}
示例13: Java_java_nio_channels_SocketChannel_configureBlocking
extern "C" JNIEXPORT void JNICALL
Java_java_nio_channels_SocketChannel_configureBlocking(JNIEnv *e,
jclass,
jint socket,
jboolean blocking)
{
setBlocking(e, socket, blocking);
}
示例14: getKeyAsChar
// ######################################################################
int KeyBoard::getKeyAsChar(const bool block)
{
// make sure we are in the correct blocking/non-blocking mode:
if (blocking != block) setBlocking(block);
// get the key & return:
int ch = getc(stdin);
return ch;
}
示例15: setBlocking
// ######################################################################
KeyBoard:: ~KeyBoard()//Destructor
{
// restore blocking mode:
setBlocking(true);
// restore previous keyboard attributes:
if (tcsetattr(0, TCSANOW, &stored_settings) == -1)
PLERROR("Cannot tcsetattr");
}