102 protoUnspec = 0, protoIp = 0,
105 protoIpip, protoIpv4 = protoIpip,
133 #if defined(_MSC_VER) || defined(WIN32) 142 static int const MsgOob;
143 static int const MsgPeek;
144 static int const MsgDontRoute;
147 static int const MsgTryHard;
149 static int const MsgCTrunc;
150 static int const MsgProxy;
151 static int const MsgTrunc;
152 static int const MsgDontWait;
153 static int const MsgEor;
154 static int const MsgWaitAll;
155 static int const MsgFin;
156 static int const MsgSyn;
157 static int const MsgConfirm;
158 static int const MsgRst;
159 static int const MsgErrQueue;
160 static int const MsgNoSignal;
161 static int const MsgMore;
162 static int const MsgWaitForOne;
163 static int const MsgCMsgCloexec;
179 explicit Socket(
int sock = -1,
bool isNewSock =
false );
203 void setSockType(
SockType sockType );
233 int shutdown(
int how = SdSend );
236 int send(
void const * data,
size_t size,
int msgFlags = MsgDefault );
245 bool sendUntil(
size_t targetSize,
void const * data,
int msgFlags = MsgDefault );
269 FunctionSuccessCallback eachSuccessCallback = FunctionSuccessCallback(),
270 void * param =
nullptr,
271 int msgFlags = MsgDefault
279 FunctionSuccessCallback eachSuccessCallback = FunctionSuccessCallback(),
280 void * param =
nullptr,
281 int msgFlags = MsgDefault
282 ) {
return this->sendWaitUntil( data.size(), data.c_str(), hadSent, sec, rcWait, std::move(eachSuccessCallback), param, msgFlags ); }
289 FunctionSuccessCallback eachSuccessCallback = FunctionSuccessCallback(),
290 void * param =
nullptr,
291 int msgFlags = MsgDefault
292 ) {
return this->sendWaitUntil( data.
getSize(), data.
getBuf(), hadSent, sec, rcWait, std::move(eachSuccessCallback), param, msgFlags ); }
295 template <
typename _PodType >
296 bool sendUntilType( _PodType
const & v,
size_t size =
sizeof(_PodType),
int msgFlags = MsgDefault ) {
return this->sendUntil( size, &v, msgFlags ); }
299 int recv(
void * buf,
size_t size,
int msgFlags = MsgDefault );
307 winux::Buffer recv(
size_t size,
int msgFlags = MsgDefault );
323 int recvWaitUntilTarget(
332 FunctionSuccessCallback eachSuccessCallback = FunctionSuccessCallback(),
333 void * param =
nullptr,
334 int msgFlags = MsgDefault
341 bool recvUntilSize(
size_t targetSize,
winux::GrowBuffer * data,
int msgFlags = MsgDefault );
352 int recvWaitUntilSize(
358 FunctionSuccessCallback eachSuccessCallback = FunctionSuccessCallback(),
359 void * param =
nullptr,
360 int msgFlags = MsgDefault
364 template <
typename _PodType >
365 bool recvUntilType( _PodType * v,
size_t size =
sizeof(_PodType),
int msgFlags = MsgDefault )
368 data.
setBuf( v, 0, size,
true );
369 return this->recvUntilSize( size, &data, msgFlags );
388 winux::Buffer recvWaitAvail(
double sec,
int * rcWait,
int msgFlags = MsgDefault );
391 int sendTo(
EndPoint const & ep,
void const * data,
size_t size,
int msgFlags = MsgDefault );
400 int recvFrom(
EndPoint * ep,
void * buf,
size_t size,
int msgFlags = MsgDefault );
411 bool connect(
EndPoint const & ep );
417 bool listen(
int backlog );
422 bool accept(
int * sock,
EndPoint * ep = NULL );
434 int getRecvBufSize()
const;
436 bool setRecvBufSize(
int optval );
439 int getSendBufSize()
const;
441 bool setSendBufSize(
int optval );
456 bool getReUseAddr()
const;
458 bool setReUseAddr(
bool optval );
461 bool getBroadcast()
const;
463 bool setBroadcast(
bool optval );
466 bool getIpv6Only()
const;
468 bool setIpv6Only(
bool optval );
471 int getError()
const;
479 bool isListening()
const;
484 int getAvailable()
const;
487 bool setBlocking(
bool blocking );
495 operator bool()
const {
return this->
get() > -1; }
540 this->_addrFamily = afUnspec;
541 this->_sockType = sockUnknown;
542 this->_protocol = protoUnspec;
544 _attrBlocking =
true;
545 _attrBroadcast =
false;
546 _attrReUseAddr =
false;
547 _attrSendTimeout = 0
U;
548 _attrRecvTimeout = 0
U;
549 _attrSendBufSize = 0;
550 _attrRecvBufSize = 0;
552 _attrIpv6Only =
true;
554 _attrIpv6Only =
false;
557 this->_resetManaged();
564 this->_isNewSock =
false;
578 virtual void *
get()
const = 0;
580 template <
typename _Ty >
581 _Ty *
get()
const {
return reinterpret_cast<_Ty *
>( this->
get() ); }
587 virtual EndPoint * clone()
const = 0;
616 this->extraData.
free();
625 this->targetBytes = 0;
626 this->retryCount = 0;
632 this->data.append(data);
638 template <
typename _IndexType >
644 if ( this->data.
getSize() >= target.size() ) this->startpos = this->data.
getSize() - target.size() + 1;
660 this->extraData.
append( this->data.
getBuf<
char>() + actualDataSize, this->data.getSize() - actualDataSize );
661 this->data.
_setSize(actualDataSize);
666 this->data = std::move(extraData);
681 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out,
682 size_t inputBufSize = (
size_t)-1,
683 size_t outputBufSize = (
size_t)-1
691 virtual int_type underflow();
696 virtual int_type overflow( int_type c );
737 std::streamsize getAvailable()
const;
743 std::streamsize waitAvail(
double sec );
766 #ifndef MOVE_SEMANTICS_DISABLED 783 virtual void *
get()
const override;
785 template <
typename _Ty >
786 _Ty *
get()
const {
return reinterpret_cast<_Ty *
>( this->
get() ); }
818 EndPointArray::iterator
begin() {
return _epArr.begin(); }
819 EndPointArray::const_iterator
begin()
const {
return _epArr.begin(); }
820 EndPointArray::iterator
end() {
return _epArr.end(); }
821 EndPointArray::const_iterator
end()
const {
return _epArr.end(); }
824 size_t count()
const {
return _epArr.size(); }
830 EndPointArray::value_type
const & operator [] (
int i )
const {
return _epArr[i]; }
831 EndPointArray::value_type & operator [] (
int i ) {
return _epArr[i]; }
833 EndPointArray &
getArr() {
return _epArr; }
834 EndPointArray
const &
getArr()
const {
return _epArr; }
845 EndPointArray _epArr;
860 explicit Socket(
int sock,
bool isNewSock =
false ) : BaseClass( sock, isNewSock ) { }
863 Socket() : BaseClass( BaseClass::afInet, BaseClass::sockStream, BaseClass::protoUnspec ) { }
898 explicit Socket(
int sock,
bool isNewSock =
false ) : BaseClass( sock, isNewSock ) { }
901 Socket() : BaseClass( BaseClass::afInet, BaseClass::sockDatagram, BaseClass::protoUnspec ) { }
932 int hasReadFd(
int fd )
const;
938 int wait(
double sec = -1 );
963 int hasWriteFd(
int fd )
const;
969 int wait(
double sec = -1 );
994 int hasExceptFd(
int fd )
const;
1000 int wait(
double sec = -1 );
1033 Select &
clear() { SelectRead::clear(); SelectWrite::clear(); SelectExcept::clear();
return *
this; }
1039 int wait(
double sec = -1 );
1111 bool startup(
bool autoReadData,
ip::EndPoint const & ep,
int threadCount = 4,
int backlog = 0,
double serverWait = 0.002,
double verboseInterval = 0.01,
VerboseOutputType verbose =
votConsole );
1114 virtual int run(
void * runParam );
1117 void stop(
bool b =
true );
1120 size_t getClientsCount()
const;
1127 virtual bool _canAddClient(
ClientCtx * clientCtx );
1131 template <
typename _Fx,
typename... _ArgType >
1136 clientCtxPtr->processingEvent =
true;
1137 this->_pool.task( [routine, clientCtxPtr] () {
1140 clientCtxPtr->processingEvent =
false;
1152 ( clientCtxPtr, readableSize )
1163 ( clientCtxPtr, data )
1187 bool _servSockAIsListening;
1188 bool _servSockBIsListening;
1189 bool _isAutoReadData;
1192 double _verboseInterval;
1195 friend class ClientCtx;
1211 clientEpStr(clientEpStr),
1212 clientSockPtr(clientSockPtr)
1233 template <
class _ClientCtxClass >
1246 _cumulativeClientId(0),
1251 _servSock.setReUseAddr(
true);
1252 _stop = !( _servSock.eiennet::Socket::bind(ep) && _servSock.listen(backlog) );
1267 int rc = sel.
wait(0.01);
1273 auto clientSockPtr = _servSock.accept(&clientEp);
1274 if ( clientSockPtr )
1276 auto & clientCtxPtr = this->_addClient( clientEp, clientSockPtr );
1279 this->onStartup(clientCtxPtr);
1289 _pool.whenEmptyStopAndWait();
1294 void stop(
bool b =
true ) {
static_cast<volatile bool &
>(_stop) = b; }
1299 return _clients.size();
1304 _startupHandler = handler;
1310 _clients.erase(clientId);
1319 ++_cumulativeClientId;
1320 client = &_clients[_cumulativeClientId];
1322 client->
attachNew(
new _ClientCtxClass( _cumulativeClientId, clientEp.
toString(), clientSockPtr ) );
1331 std::map< winux::uint64, ClientCtxSharedPointer >
_clients;
1336 if ( this->_startupHandler ) this->_startupHandler(clientCtxPtr);
1350 #endif // __SOCKET_HPP__
XString< char > AnsiString
#define EIENNET_FUNC_DECL(ret)
SelectWrite & setWriteSock(Socket const *sock)
RunableT< _Fx, std::tuple< typename std::decay< _ArgType >::type... > > * NewRunable(_Fx fn, _ArgType &&...arg)
创建一个Runable对象
void stop(bool b=true)
是否停止服务运行
void append(void const *data, size_t size)
添加数据:C语言缓冲区
int hasExceptSock(Socket const &sock) const
winux::GrowBuffer extraData
额外收到的数据
SelectRead & setReadSock(Socket const &sock)
winux::String clientEpStr
SocketStreamIn(SocketStreamBuf *sockBuf)
void * getBuf() const
暴露缓冲区指针
SocketStreamOut(winux::SimplePointer< SocketStreamBuf > &sockBuf)
Select & setExceptFd(int fd)
int sendTo(EndPoint const &ep, winux::AnsiString const &data, int msgFlags=MsgDefault)
无连接模式发送数据到指定端点。返回已发送大小,出错返回-1。
int wait(double sec=-1)
等待相应的fd就绪。sec<1表示小于1秒的时间,sec<0表示无限等待。eg: sec=1.5表示等待1500ms ...
winux::SharedPointer< ip::tcp::Socket > clientSockPtr
eiennet::Socket BaseClass
Select & clearExceptFds()
SelectExcept & setExceptSock(Socket const *sock)
int sendWaitUntil(winux::Buffer const &data, size_t *hadSent, double sec, int *rcWait, FunctionSuccessCallback eachSuccessCallback=FunctionSuccessCallback(), void *param=nullptr, int msgFlags=MsgDefault)
winux::String const & getHostname() const
获取主机名
void resetData()
重置数据和额外数据为空
Select & setReadSock(Socket const &sock)
Select & setWriteFd(int fd)
EndPointArray::const_iterator end() const
bool processingEvent
是否事件处理中,保证同一个客户连接仅投递一个事件到线程池中
static int const MsgDefault
int get() const
Windows:socket句柄,或Linux:socket描述符
void _postTask(winux::SharedPointer< ClientCtx > clientCtxPtr, _Fx fn, _ArgType &&...arg)
往线程池投递任务
size_t count() const
获取解析到的IP端点数
EndPointArray::iterator begin()
Socket * getSocket() const
winux::ushort getPort() const
获取端口号
int sendTo(EndPoint const &ep, winux::Buffer const &data, int msgFlags=MsgDefault)
无连接模式发送数据到指定端点。返回已发送大小,出错返回-1。
SocketStreamIn(SocketStreamBuf &sockBuf)
Socket(int sock, bool isNewSock=false)
构造函数1,包装现有socket描述符
SocketStreamIn(winux::SimplePointer< SocketStreamBuf > &sockBuf)
SelectRead & setReadSock(Socket const *sock)
ClientCtx(winux::uint64 clientId, winux::String clientEpStr, winux::SharedPointer< ip::tcp::Socket > clientSockPtr)
int hasReadSock(Socket const &sock) const
winux::String clientEpStr
客户终端字符串
size_t getSize() const
获取数据大小
#define DISABLE_OBJECT_COPY(clsname)
Select & setWriteSock(Socket const &sock)
Select & delWriteFd(int fd)
static int const MsgDontRoute
Select & delExceptFd(int fd)
winux::uint64 _cumulativeClientId
winux::uint32 _attrRecvTimeout
winux::SharedPointer< Socket > accept(EndPoint *ep=NULL)
接受一个客户连接
bool recvUntilType(_PodType *v, size_t size=sizeof(_PodType), int msgFlags=MsgDefault)
接收一个Plain of Data类型的变量,若成功返回true,否则返回false。
void removeClient(winux::uint64 clientId)
winux::uint32 _attrSendTimeout
SocketStreamIn(winux::SharedPointer< SocketStreamBuf > &sockBuf)
std::function< void(size_t hadBytes, void *param) > FunctionSuccessCallback
Select & setReadFds(winux::Mixed const &fds)
Select & setExceptSock(Socket const &sock)
int ConnectAttempt(Socket *sock, Resolver const &resolver, winux::uint32 perCnnTimeoutMs)
阻塞模式Socket连接尝试,连接成功返回0,超时返回1,失败返回-1
eiennet::Socket BaseClass
int send(winux::Buffer const &data, int msgFlags=MsgDefault)
发送数据。返回已发送大小,出错返回-1。
void attachNew(_Ty *p)
附加新指针,管理新对象
winux::SharedPointer< Socket > accept(EndPoint *ep=NULL)
接受一个客户连接
static constexpr size_t const npos
非位置,值为-1。
void onStartupHandler(StartupHandlerFunction handler)
Select & setReadFd(int fd)
缓冲区,表示内存中一块二进制数据(利用malloc/realloc进行内存分配)
Socket(int sock, bool isNewSock=false)
构造函数1,包装现有socket描述符
virtual winux::String toString() const override
转换成"IP:port"的字符串形式
winux::Members< struct SelectExcept_Data > _self
size_t getClientsCount() const
EndPointArray::iterator end()
#define DEFINE_CUSTOM_EVENT_RETURN_EX(ret, evtname, paramtypes)
static int const MsgPartial
void _setSize(size_t dataSize)
设置数据大小,不能超过容量大小(不建议外部调用)
SelectExcept & setExceptSock(Socket const &sock)
void setBuf(void const *buf, size_t size, size_t capacity, bool isPeek)
设置缓冲区,当isPeek为false时拷贝数据缓冲区
winux::Members< struct SelectWrite_Data > _self
int hasWriteSock(Socket const &sock) const
Select & setWriteFds(winux::Mixed const &fds)
static size_t _Templ_KmpMatchEx(_ChTy const *str, size_t len, _ChTy const *substr, size_t sublen, size_t pos, std::vector< _IndexType > const &next)
KMP匹配算法:传入已经求好的next进行匹配
std::map< winux::uint64, ClientCtxSharedPointer > _clients
线程池,创建一组线程等待着从任务队列中获取任务执行
SocketStreamOut(winux::SharedPointer< SocketStreamBuf > &sockBuf)
void append(winux::Buffer const &data)
添加数据到data
static int const MsgWaitAll
std::vector< AttrCategory > _attrExecSets
int sendWaitUntil(winux::AnsiString const &data, size_t *hadSent, double sec, int *rcWait, FunctionSuccessCallback eachSuccessCallback=FunctionSuccessCallback(), void *param=nullptr, int msgFlags=MsgDefault)
VerboseOutputType
冗余信息输出类型
virtual void onStartup(ClientCtxSharedPointer clientCtxPtr)
Select & delReadFd(int fd)
bool sendUntil(winux::Buffer const &data, int msgFlags=MsgDefault)
发送缓冲区,直到发送完该大小的缓冲区。
static int const MsgInterrupt
ClientCtxSharedPointer & _addClient(ip::EndPoint const &clientEp, winux::SharedPointer< ip::tcp::Socket > clientSockPtr)
SocketError(int errType, winux::AnsiString const &errStr)
#define DEFINE_CUSTOM_EVENT(evtname, paramtypes, calledparams)
bool find(winux::AnsiString const &target, std::vector< _IndexType > const &targetNextVal)
在data里查找target内容。startpos指定起始位置,pos接收搜索到的位置。
bool sendUntilType(_PodType const &v, size_t size=sizeof(_PodType), int msgFlags=MsgDefault)
发送一个Plain of Data类型的变量,若成功返回true,否则返回false。
ip::tcp::Socket _servSock
主机名解析器(可以把域名解析为一个或多个IP端点)
virtual winux::uint & size() const =0
取得地址的数据大小,一般为内部地址结构体的大小
StartupHandlerFunction _startupHandler
std::vector< ip::EndPoint > EndPointArray
int send(winux::AnsiString const &data, int msgFlags=MsgDefault)
发送数据。返回已发送大小,出错返回-1。
std::function< void(ClientCtxSharedPointer clientCtxPtr) > StartupHandlerFunction
static int const MsgMaxIovLen
SimplePointer< _Ty > MakeSimple(_Ty *newObj)
创建一个SimplePointer来管理新对象资源
winux::Members< struct SelectRead_Data > _self
EndPointArray::const_iterator begin() const
static int const SdReceive
SelectWrite & setWriteSock(Socket const &sock)
bool sendUntil(winux::AnsiString const &data, int msgFlags=MsgDefault)
发送字符串,直到发送完该长度的字符串。
Server(ip::EndPoint const &ep, int threadCount=4, int backlog=0)
构造函数1
SocketStreamOut(SocketStreamBuf &sockBuf)
winux::SharedPointer< ip::tcp::Socket > clientSockPtr
客户套接字
SocketStreamOut(SocketStreamBuf *sockBuf)
winux::uint64 clientId
客户Id
winux::Buffer adjust(size_t actualDataSize)
在find()到目标内容后,调整data大小。把多余的数据放入extraData,然后返回data内容,并把extraData移到dat...
EndPointArray const & getArr() const
Select & setExceptFds(winux::Mixed const &fds)