socket_libc.inl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "../io_common.h"
00044 #include "../socket.h"
00045 #include "../../util/impl/lass_errno.h"
00046 #include <sys/socket.h>
00047 #include <sys/types.h>
00048 #include <netinet/in.h>
00049 #include <netdb.h>
00050 #include <unistd.h>
00051
00052 namespace lass
00053 {
00054 namespace io
00055 {
00056 namespace impl
00057 {
00058
00059 class SocketImpl: public util::NonCopyable
00060 {
00061 public:
00062
00063 SocketImpl():
00064 socket_(invalidSocket)
00065 {
00066 }
00067
00068 ~SocketImpl()
00069 {
00070 try
00071 {
00072 closeSocket();
00073 }
00074 catch (std::exception& error)
00075 {
00076 std::cerr << "[LASS RUN MSG] WARNING: closeSocket() failed: " << error.what();
00077 }
00078 }
00079
00080 void bind(unsigned short portNumber)
00081 {
00082 openSocket();
00083
00084 ::sockaddr_in addr;
00085 addr.sin_addr.s_addr = htonl(INADDR_ANY);
00086 addr.sin_family = AF_INET;
00087 addr.sin_port = htons(portNumber);
00088
00089 if (::bind(socket_, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) != 0)
00090 {
00091 const int err = util::impl::lass_errno();
00092 LASS_THROW_EX(SocketError, "Failed to bind socket to port " << portNumber
00093 << ": " << util::impl::lass_strerror(err));
00094 }
00095 }
00096
00097 void listen()
00098 {
00099 LASS_ASSERT(socket_ != invalidSocket);
00100 if (::listen(socket_, SOMAXCONN) != 0)
00101 {
00102 const int err = util::impl::lass_errno();
00103 LASS_THROW_EX(SocketError, "Failed to listen: " << util::impl::lass_strerror(err));
00104 }
00105 }
00106
00107 void accept(SocketImpl* connection)
00108 {
00109 LASS_ASSERT(socket_ != invalidSocket);
00110 int socket = ::accept(socket_, 0, 0);
00111 if (socket == invalidSocket)
00112 {
00113 const int err = util::impl::lass_errno();
00114 LASS_THROW_EX(SocketError, "Failed to accept connection: " << util::impl::lass_strerror(err));
00115 }
00116 LASS_ASSERT(connection);
00117 connection->socket_ = socket;
00118 }
00119
00120 void connect(const std::string& ipAddress, unsigned short portNumber)
00121 {
00122 openSocket();
00123
00124 ::hostent* other = gethostbyname(ipAddress.c_str());
00125 if (!other)
00126 {
00127 LASS_THROW_EX(SocketError, "could not connect " << ipAddress << ":" << portNumber
00128 << " : failed to lookup hostname.");
00129 }
00130
00131 sockaddr_in dest;
00132 dest.sin_addr = *(in_addr*) other->h_addr;
00133 dest.sin_family = PF_INET;
00134 dest.sin_port = htons(portNumber);
00135 const int ret = ::connect(socket_, reinterpret_cast<sockaddr*>(&dest), sizeof(dest));
00136 if (ret != 0)
00137 {
00138 const int err = util::impl::lass_errno();
00139 LASS_THROW_EX(SocketError, "Could not connect " << ipAddress << ":" << portNumber
00140 << " : " << util::impl::lass_strerror(err));
00141 }
00142 }
00143
00144 const int send(const void* begin, int length)
00145 {
00146 LASS_ASSERT(socket_ != invalidSocket);
00147 const int ret = ::send(socket_, static_cast<const char*>(begin), length, 0);
00148 if (ret == -1)
00149 {
00150 const int err = util::impl::lass_errno();
00151 LASS_THROW_EX(SocketError, "Failed to send data: " << util::impl::lass_strerror(err));
00152 }
00153 LASS_ASSERT(ret >= 0);
00154 return ret;
00155 }
00156
00157 const int receive(void* begin, int length)
00158 {
00159 const int ret = ::recv(socket_, static_cast<char*>(begin), length, 0);
00160 if (ret == -1)
00161 {
00162 const int err = util::impl::lass_errno();
00163 LASS_THROW_EX(SocketError, "Failed to receive data: " << util::impl::lass_strerror(err));
00164 }
00165 LASS_ASSERT(ret >= 0);
00166 return ret;
00167 }
00168
00169 private:
00170
00171 enum { invalidSocket = -1 };
00172
00173 void openSocket()
00174 {
00175 if (socket_ == invalidSocket)
00176 {
00177 socket_ = socket(AF_INET, SOCK_STREAM, 0);
00178 if (socket_ == invalidSocket)
00179 {
00180 const int err = util::impl::lass_errno();
00181 LASS_THROW_EX(SocketError, "Failed to create socket: " << util::impl::lass_strerror(err));
00182 }
00183 }
00184 }
00185
00186 void closeSocket()
00187 {
00188 if (socket_ != invalidSocket)
00189 {
00190 const int ret = close(socket_);
00191 socket_ = invalidSocket;
00192 if (ret != 0)
00193 {
00194 const int err = util::impl::lass_errno();
00195 LASS_THROW_EX(SocketError, "Failed to close socket: " << util::impl::lass_strerror(err));
00196 }
00197 }
00198 }
00199
00200 int socket_;
00201 };
00202
00203 }
00204 }
00205 }
00206
00207