📄 sockstream.c
字号:
throw sockoob (); int rval = 0; int sa_len = sa.size (); if ((rval = ::recvfrom (rep->sock, (char*) buf, len, msgf, sa.addr (), &sa_len)) == -1) throw sockerr (errno); return rval;}int sockbuf::write(const void* buf, int len)// upon error, write throws the number of bytes writen so far instead// of sockerr.{ if (rep->stmo != -1 && is_writeready (rep->stmo)==0) throw sockerr (ETIMEDOUT); int wlen=0; while(len>0) { int wval = ::write (rep->sock, (char*) buf, len); if (wval == -1) throw wlen; len -= wval; wlen += wval; } return wlen; // == len if every thing is all right}int sockbuf::send (const void* buf, int len, int msgf)// upon error, write throws the number of bytes writen so far instead// of sockerr.{ if (rep->stmo != -1 && is_writeready (rep->stmo)==0) throw sockerr (ETIMEDOUT); int wlen=0; while(len>0) { int wval = ::send (rep->sock, (char*) buf, len, msgf); if (wval == -1) throw wlen; len -= wval; wlen += wval; } return wlen;}int sockbuf::sendto (sockAddr& sa, const void* buf, int len, int msgf)// upon error, write throws the number of bytes writen so far instead// of sockerr.{ if (rep->stmo != -1 && is_writeready (rep->stmo)==0) throw sockerr (ETIMEDOUT); int wlen=0; while(len>0) { int wval = ::sendto (rep->sock, (char*) buf, len, msgf, sa.addr (), sa.size()); if (wval == -1) throw wlen; len -= wval; wlen += wval; } return wlen;}#ifndef __linux__// linux does not have sendmsg or recvmsgint sockbuf::recvmsg (msghdr* msg, int msgf){ if (rep->rtmo != -1 && is_readready (rep->rtmo)==0) throw sockerr (ETIMEDOUT); if (rep->oob && atmark ()) throw sockoob (); int rval = ::recvmsg(rep->sock, msg, msgf); if (rval == -1) throw sockerr (errno); return rval;}int sockbuf::sendmsg (msghdr* msg, int msgf)// upon error, write throws the number of bytes writen so far instead// of sockerr.{ if (rep->stmo != -1 && is_writeready (rep->stmo)==0) throw sockerr (ETIMEDOUT); int wlen = ::sendmsg (rep->sock, msg, msgf); if (wlen == -1) throw 0; return wlen;}#endif //!__linux__int sockbuf::sendtimeout (int wp){ int oldstmo = rep->stmo; rep->stmo = (wp < 0) ? -1: wp; return oldstmo;}int sockbuf::recvtimeout (int wp){ int oldrtmo = rep->rtmo; rep->rtmo = (wp < 0) ? -1: wp; return oldrtmo;}int sockbuf::is_readready (int wp_sec, int wp_usec) const{ fd_set fds; FD_ZERO (&fds); FD_SET (rep->sock, &fds); timeval tv; tv.tv_sec = wp_sec; tv.tv_usec = wp_usec; int ret = select (rep->sock+1, &fds, 0, 0, (wp_sec == -1) ? 0: &tv); if (ret == -1) throw sockerr (errno); return ret;}int sockbuf::is_writeready (int wp_sec, int wp_usec) const{ fd_set fds; FD_ZERO (&fds); FD_SET (rep->sock, &fds); timeval tv; tv.tv_sec = wp_sec; tv.tv_usec = wp_usec; int ret = select (rep->sock+1, 0, &fds, 0, (wp_sec == -1) ? 0: &tv); if (ret == -1) throw sockerr (errno); return ret;}int sockbuf::is_exceptionpending (int wp_sec, int wp_usec) const{ fd_set fds; FD_ZERO (&fds); FD_SET (rep->sock, &fds); timeval tv; tv.tv_sec = wp_sec; tv.tv_usec = wp_usec; int ret = select (rep->sock+1, 0, 0, &fds, (wp_sec == -1) ? 0: &tv); if (ret == -1) throw sockerr (errno); return ret;}void sockbuf::shutdown (shuthow sh){ switch (sh) { case shut_read: delete [] eback (); setg (0, 0, 0); break; case shut_write: delete [] pbase (); setp (0, 0); break; case shut_readwrite: shutdown (shut_read); shutdown (shut_write); break; } if (::shutdown(rep->sock, sh) == -1) throw sockerr (errno);}int sockbuf::getopt (int op, void* buf, int len, int level) const{ if (::getsockopt (rep->sock, level, op, (char*) buf, &len) == -1) throw sockerr (errno); return len;}void sockbuf::setopt (int op, void* buf, int len, int level) const{ if (::setsockopt (rep->sock, level, op, (char*) buf, len) == -1) throw sockerr (errno);}sockbuf::type sockbuf::gettype () const{ int ty=0; getopt (so_type, &ty, sizeof (ty)); return sockbuf::type(ty);}int sockbuf::clearerror () const{ int err=0; getopt (so_error, &err, sizeof (err)); return err;}bool sockbuf::debug () const{ int old = 0; getopt (so_debug, &old, sizeof (old)); return old;}bool sockbuf::debug (bool set) const{ int old=0; int opt = set; getopt (so_debug, &old, sizeof (old)); setopt (so_debug, &opt, sizeof (opt)); return old;}bool sockbuf::reuseaddr () const{ int old = 0; getopt (so_reuseaddr, &old, sizeof (old)); return old;}bool sockbuf::reuseaddr (bool set) const{ int old=0; int opt = set; getopt (so_reuseaddr, &old, sizeof (old)); setopt (so_reuseaddr, &opt, sizeof (opt)); return old;}bool sockbuf::keepalive () const{ int old = 0; getopt (so_keepalive, &old, sizeof (old)); return old;}bool sockbuf::keepalive (bool set) const{ int old=0; int opt = set; getopt (so_keepalive, &old, sizeof (old)); setopt (so_keepalive, &opt, sizeof (opt)); return old;}bool sockbuf::dontroute () const{ int old = 0; getopt (so_dontroute, &old, sizeof (old)); return old;}bool sockbuf::dontroute (bool set) const{ int old = 0; int opt = set; getopt (so_dontroute, &old, sizeof (old)); setopt (so_dontroute, &opt, sizeof (opt)); return old;}bool sockbuf::broadcast () const{ int old=0; getopt (so_broadcast, &old, sizeof (old)); return old;}bool sockbuf::broadcast (bool set) const{ int old = 0; int opt = set; getopt (so_broadcast, &old, sizeof (old)); setopt (so_broadcast, &opt, sizeof (opt)); return old;}bool sockbuf::oobinline () const{ int old=0; getopt (so_oobinline, &old, sizeof (old)); return old;} bool sockbuf::oobinline (bool set) const{ int old = 0; int opt = set; getopt (so_oobinline, &old, sizeof (old)); setopt (so_oobinline, &opt, sizeof (opt)); return old;}bool sockbuf::oob (bool b){ bool old = rep->oob; rep->oob = b; return old;}sockbuf::socklinger sockbuf::linger () const{ socklinger old (0, 0); getopt (so_linger, &old, sizeof (old)); return old;}sockbuf::socklinger sockbuf::linger (sockbuf::socklinger opt) const{ socklinger old (0, 0); getopt (so_linger, &old, sizeof (old)); setopt (so_linger, &opt, sizeof (opt)); return old;}int sockbuf::sendbufsz () const{ int old=0; getopt (so_sndbuf, &old, sizeof (old)); return old;}int sockbuf::sendbufsz (int sz) const{ int old=0; getopt (so_sndbuf, &old, sizeof (old)); setopt (so_sndbuf, &sz, sizeof (sz)); return old;}int sockbuf::recvbufsz () const{ int old=0; getopt (so_rcvbuf, &old, sizeof (old)); return old;}int sockbuf::recvbufsz (int sz) const{ int old=0; getopt (so_rcvbuf, &old, sizeof (old)); setopt (so_rcvbuf, &sz, sizeof (sz)); return old;}bool sockbuf::atmark () const// return true, if the read pointer for socket points to an// out of band data{ int arg; if (::ioctl (rep->sock, SIOCATMARK, &arg) == -1) throw sockerr (errno); return arg;}int sockbuf::pgrp () const// return the process group id that would receive SIGIO and SIGURG// signals{ int arg; if (::ioctl (rep->sock, SIOCGPGRP, &arg) == -1) throw sockerr (errno); return arg;}int sockbuf::pgrp (int new_pgrp) const// set the process group id that would receive SIGIO and SIGURG signals.// return the old pgrp{ int old = pgrp (); if (::ioctl (rep->sock, SIOCSPGRP, &new_pgrp) == -1) throw sockerr (errno); return old;}void sockbuf::closeonexec (bool set) const// if set is true, set close on exec flag// else clear close on exec flag{ if (set) { if (::ioctl (rep->sock, FIOCLEX, 0) == -1) throw sockerr (errno); } else { if (::ioctl (rep->sock, FIONCLEX, 0) == -1) throw sockerr (errno); }}long sockbuf::nread () const// return how many chars are available for reading in the recvbuf of// the socket.{ long arg; if (::ioctl (rep->sock, FIONREAD, &arg) == -1) throw sockerr (errno); return arg;}long sockbuf::howmanyc () const// return how many chars are available for reading in the input buffer// and the recvbuf of the socket.{ return showmanyc () + nread ();}void sockbuf::nbio (bool set) const// if set is true, set socket to non-blocking io. Henceforth, any// write or read operation will not wait if write or read would block.// The read or write operation will result throwing a sockerr// exception with errno set to EWOULDBLOCK.{ int arg = set; if (::ioctl (rep->sock, FIONBIO, &arg) == -1) throw sockerr (errno);}void sockbuf::async (bool set) const// if set is true, set socket for asynchronous io. If any io is// possible on the socket, the process will get SIGIO{ int arg = set; if (::ioctl (rep->sock, FIOASYNC, &arg) == -1) throw sockerr (errno);}osockstream& crlf (osockstream& o){ o << "\r\n"; o.rdbuf ()->pubsync (); return o;}osockstream& lfcr (osockstream& o){ o << "\n\r"; o.rdbuf ()->pubsync (); return o;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -