Ignore:
Timestamp:
12/29/12 10:03:12 (11 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Moved the storage of the error code to where it belongs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/xplcommon/posix/ServerSocket.cc

    r6 r14  
    3434#include "Socket.h"
    3535
    36 #include <cstdio>
    37 
    3836#include <unistd.h>
    3937#include <sys/socket.h>
     
    4947    Waitable(waiter, Socket::socket(domain, type, protocol))
    5048{
     49    if (fd<0) setErrorCodeFromErrno();
    5150}
    5251
     
    5554ServerSocket::~ServerSocket()
    5655{
    57     ::close(fd);
     56    if (fd>=0) ::close(fd);
    5857}
    5958
    6059//------------------------------------------------------------------------------
    6160
    62 int ServerSocket::bind(const struct sockaddr* addr, size_t addrlen,
    63                        bool reuseaddr)
     61bool ServerSocket::bind(const struct sockaddr* addr, size_t addrlen,
     62                        bool reuseaddr)
    6463{
    6564    if (reuseaddr) {
    6665        int reuse = 1;
    6766        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))<0) {
    68             perror("xplcommon::posix::Socket::bind: setsockopt");
    69             return -1;
     67            setErrorCodeFromErrno();
     68            return false;
    7069        }
    7170    }
    7271
    73     return ::bind(fd, addr, addrlen);
     72    if (::bind(fd, addr, addrlen)<0) {
     73        setErrorCodeFromErrno();
     74        return false;
     75    }
     76
     77    return true;
    7478}
    7579
    7680//------------------------------------------------------------------------------
    7781
    78 int ServerSocket::listen(int backlog)
     82bool ServerSocket::listen(int backlog)
    7983{
    80     return ::listen(fd, backlog);
     84    if (::listen(fd, backlog)<0) {
     85        setErrorCodeFromErrno();
     86        return false;
     87    } else {
     88        return true;
     89    }
     90}
     91
     92//------------------------------------------------------------------------------
     93
     94int ServerSocket::accept(struct sockaddr* addr, size_t* addrlen)
     95{
     96    socklen_t alen = (addrlen==0) ? 0 : *addrlen;
     97    int result = ::accept(fd, addr, (addrlen==0) ? 0 : &alen);
     98    if (result<0 && errno!=EAGAIN && errno!=EWOULDBLOCK) {
     99        setErrorCodeFromErrno();
     100    } else {
     101        if (addrlen!=0) *addrlen = alen;
     102    }
     103    return result;
    81104}
    82105
Note: See TracChangeset for help on using the changeset viewer.