socket_winsock.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 <winsock.h>
00046
00047 #if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
00048 # pragma comment(lib, "ws2_32.lib")
00049 #endif
00050
00051 namespace lass
00052 {
00053 namespace io
00054 {
00055 namespace impl
00056 {
00057
00058 class SocketImpl: public util::NonCopyable
00059 {
00060 public:
00061
00062 SocketImpl():
00063 socket_(INVALID_SOCKET)
00064 {
00065 WSADATA wsaData;
00066 if (WSAStartup(0x0202, &wsaData) != 0)
00067 {
00068 LASS_THROW_EX(SocketError, "Failed to startup Windows Socket API.");
00069 }
00070 if (wsaData.wVersion != 0x0202)
00071 {
00072 WSACleanup();
00073 LASS_THROW_EX(SocketError, "Windows Socket API is not version 2.2.");
00074 }
00075 }
00076
00077 ~SocketImpl()
00078 {
00079 if (socket_ != INVALID_SOCKET)
00080 {
00081 const int err = closesocket(socket_);
00082 LASS_ASSERT(err == 0);
00083 }
00084
00085 const int err = WSACleanup();
00086 LASS_ASSERT(err == 0);
00087 }
00088
00089 void bind(unsigned short iPortNumber)
00090 {
00091 openSocket();
00092
00093 SOCKADDR_IN addr;
00094 addr.sin_addr.s_addr = htonl(INADDR_ANY);
00095 addr.sin_family = AF_INET;
00096 addr.sin_port = htons(iPortNumber);
00097
00098 if (::bind(socket_, (LPSOCKADDR)&addr, sizeof(addr)) != 0)
00099 {
00100 LASS_THROW_EX(SocketError, "Failed to bind socket to port " << iPortNumber);
00101 }
00102 }
00103
00104 void listen()
00105 {
00106 LASS_ASSERT(socket_ != INVALID_SOCKET);
00107 ::listen(socket_, SOMAXCONN);
00108 }
00109
00110 void accept(SocketImpl* oConnection)
00111 {
00112 LASS_ASSERT(socket_ != INVALID_SOCKET);
00113 SOCKET connnection = ::accept(socket_, 0, 0);
00114 if (connnection == INVALID_SOCKET)
00115 {
00116 LASS_THROW_EX(SocketError, "Failed to accept connection");
00117 }
00118 LASS_ASSERT(oConnection);
00119 oConnection->socket_ = connnection;
00120 }
00121
00122 void connect(const std::string& iIpAddress, unsigned short iPortNumber)
00123 {
00124 openSocket();
00125
00126 SOCKADDR_IN dest;
00127 dest.sin_addr.s_addr = inet_addr(iIpAddress.c_str());
00128 dest.sin_family = PF_INET;
00129 dest.sin_port = htons(iPortNumber);
00130 const int ret = ::connect(socket_, reinterpret_cast<SOCKADDR*>(&dest), sizeof(dest));
00131 if (ret == SOCKET_ERROR)
00132 {
00133 LASS_THROW_EX(SocketError, "could not connect " << iIpAddress << ":" << iPortNumber);
00134 }
00135 }
00136
00137 const int send(const void* iBegin, int iLength)
00138 {
00139 LASS_ASSERT(socket_ != INVALID_SOCKET);
00140 const int ret = ::send(socket_, static_cast<const char*>(iBegin), iLength, 0);
00141 if (ret == SOCKET_ERROR)
00142 {
00143 LASS_THROW_EX(SocketError, "Failure to send data: " << WSAGetLastError());
00144 }
00145 LASS_ASSERT(ret >= 0);
00146 return ret;
00147 }
00148
00149 const int receive(void* iBegin, int iLength)
00150 {
00151 const int ret = ::recv(socket_, static_cast<char*>(iBegin), iLength, 0);
00152 if (ret == SOCKET_ERROR)
00153 {
00154 LASS_THROW_EX(SocketError, "Failure to receive data: " << WSAGetLastError());
00155 }
00156 LASS_ASSERT(ret >= 0);
00157 return ret;
00158 }
00159
00160 private:
00161
00162 void openSocket()
00163 {
00164 if (socket_ == INVALID_SOCKET)
00165 {
00166 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
00167 if (socket_ == INVALID_SOCKET)
00168 {
00169 LASS_THROW_EX(SocketError, "Failed to create socket.");
00170 }
00171 }
00172 }
00173
00174 SOCKET socket_;
00175 };
00176
00177 }
00178 }
00179 }
00180
00181