15 #ifndef ccuty_ccsocket
16 #define ccuty_ccsocket
20 #include <sys/types.h>
21 #include <sys/socket.h>
27 #if defined(MSG_NOSIGNAL)
28 # define _NO_SIGPIPE(flags) (flags | MSG_NOSIGNAL)
30 # define _NO_SIGPIPE(flags) (flags)
49 enum Errors {Failed = -1, InvalidSocket = -2, UnknownHost = -3};
57 Socket(
int type = SOCK_STREAM);
60 Socket(
int type,
int sockfd);
69 virtual int bind(
int port);
75 virtual int bind(
const std::string& host,
int port);
82 virtual int connect(
const std::string& host,
int port);
103 ssize_t
send(
const void* buf,
size_t len,
int flags = 0) {
104 return ::send(_sockfd, buf, len, _NO_SIGPIPE(flags));
116 ssize_t
receive(
void* buf,
size_t len,
int flags = 0) {
117 return ::recv(_sockfd, buf, len, flags);
124 ssize_t
sendTo(
const void* buf,
size_t len,
int flags,
125 const struct sockaddr* dest_addr, socklen_t addrlen) {
126 return ::sendto(_sockfd, buf, len, _NO_SIGPIPE(flags), dest_addr, addrlen);
135 struct sockaddr* src_addr, socklen_t* addrlen) {
136 return ::recvfrom(_sockfd, buf, len, flags, src_addr, addrlen);
185 virtual int setAddress(
struct sockaddr_in& addr,
const std::string& host,
int port);
221 virtual int bind(
int port,
int backlog = 50);
245 virtual Socket* createSocket(
int sockfd);
314 SocketBuffer(
Socket& socket,
size_t inputBufferSize = 8192,
size_t ouputBufferSize = 8192);
335 virtual ssize_t
readLine(std::string& message);
356 virtual ssize_t
writeLine(
const std::string& message);
366 virtual ssize_t read(
char* buffer,
size_t len);
376 virtual ssize_t write(
const char* str,
size_t len);
413 virtual bool retrieveLine(std::string& str, ssize_t received);
414 size_t _inSize, _outSize;
417 struct InputBuffer* _in;