<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

  • 随笔 - 19
  • 文章 - 0
  • 评论 - 0
  • 引用 - 0

常用链接

留言簿

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

[导入]Sockops
/*
* Multiplatform Async Network Library
* Copyright (c) 2007 Burlex
*
* SocketOpsLinux.cpp - Linux implementation of SocketOps.
*
*/


#include "Network.h"
#ifdef CONFIG_USE_EPOLL

namespace SocketOps
{
// Create file descriptor for socket i/o operations.
SOCKET CreateTCPFileDescriptor()
{
// create a socket for use with overlapped i/o.
return socket(AF_INET, SOCK_STREAM, 0);
}

// Disable blocking send/recv calls.
bool Nonblocking(SOCKET fd)
{
uint32 arg = 1;
return (::ioctl(fd, FIONBIO, &arg) == 0);
}

// Disable blocking send/recv calls.
bool Blocking(SOCKET fd)
{
uint32 arg = 0;
return (ioctl(fd, FIONBIO, &arg) == 0);
}

// Disable nagle buffering algorithm
bool DisableBuffering(SOCKET fd)
{
uint32 arg = 1;
return (setsockopt(fd, 0x6, 0x1, (const char*)&arg, sizeof(arg)) == 0);
}

// Enable nagle buffering algorithm
bool EnableBuffering(SOCKET fd)
{
uint32 arg = 0;
return (setsockopt(fd, 0x6, 0x1, (const char*)&arg, sizeof(arg)) == 0);
}

// Set internal buffer size to socket.
bool SetSendBufferSize(SOCKET fd, uint32 size)
{
return (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const char*)&size, sizeof(size)) == 0);
}

// Set internal buffer size to socket.
bool SetRecvBufferSize(SOCKET fd, uint32 size)
{
return (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&size, sizeof(size)) == 0);
}

// Set internal timeout.
bool SetTimeout(SOCKET fd, uint32 timeout)
{
struct timeval to;
to.tv_sec = timeout;
to.tv_usec = 0;
if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&to, (socklen_t)sizeof(to)) != 0) return false;
return (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&to, (socklen_t)sizeof(to)) == 0);
}

// Closes a socket fully.
void CloseSocket(SOCKET fd)
{
shutdown(fd, SHUT_RDWR);
close(fd);
}

// Sets reuseaddr
void ReuseAddr(SOCKET fd)
{
uint32 option = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&option, 4);
}
}

#endif

类别:网络编程 查看评论
文章来源:http://hi.baidu.com/wgcno7/blog/item/98719f49deea1efc83025c5d.html

posted on 2010-04-21 17:46 wgcno7 阅读(340) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理