|
|
@ -4,8 +4,19 @@ |
|
|
|
namespace Bk::Net { |
|
|
|
namespace Bk::Net { |
|
|
|
int WindowsSocket::socket_count = 0; |
|
|
|
int WindowsSocket::socket_count = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WindowsSocket::WindowsSocket(int id, IpVersion ver, IpProtocol proto) |
|
|
|
|
|
|
|
: id(id), ip_proto(proto), main(false) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
char myIP[16]; |
|
|
|
|
|
|
|
struct sockaddr sock_addr; |
|
|
|
|
|
|
|
getsockname(id, (struct sockaddr*)&addr, sizeof(addr)); |
|
|
|
|
|
|
|
inet_ntop((int)ver, &addr.sin_addr, myIP, sizeof(myIP)); |
|
|
|
|
|
|
|
ip_addr = IpAddress(std::string(myIP, 16), ver); |
|
|
|
|
|
|
|
addr = sock_addr; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
WindowsSocket::WindowsSocket(IpAddress ip, int port, IpProtocol proto) |
|
|
|
WindowsSocket::WindowsSocket(IpAddress ip, int port, IpProtocol proto) |
|
|
|
: ip_addr(ip), ip_proto(proto) |
|
|
|
: ip_addr(ip), ip_proto(proto), main(true) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (socket_count++ < 1) |
|
|
|
if (socket_count++ < 1) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -22,27 +33,28 @@ namespace Bk::Net { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//WindowsSocket creation step
|
|
|
|
//WindowsSocket creation step
|
|
|
|
if ((socket_id = (int)socket((int)ip_addr.version, (int)ip_proto, 0)) < 0) |
|
|
|
if ((id = (int)socket((int)ip_addr.version, (int)ip_proto, 0)) < 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
log("socket failed " << WSAGetLastError()); |
|
|
|
log("socket failed " << WSAGetLastError()); |
|
|
|
exit(EXIT_FAILURE); |
|
|
|
exit(EXIT_FAILURE); |
|
|
|
} |
|
|
|
} |
|
|
|
addr.sin_addr = ip_addr.get_data(); |
|
|
|
struct sockaddr_in in_addr; |
|
|
|
addr.sin_family = (int)ip_addr.version; |
|
|
|
in_addr.sin_addr = ip_addr.get_data(); |
|
|
|
addr.sin_port = htons(port); |
|
|
|
in_addr.sin_family = (int)ip_addr.version; |
|
|
|
|
|
|
|
in_addr.sin_port = htons(port); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
WindowsSocket::~WindowsSocket() |
|
|
|
WindowsSocket::~WindowsSocket() |
|
|
|
{ |
|
|
|
{ |
|
|
|
WSACleanup(); |
|
|
|
if (main && socket_count-- < 1) WSACleanup(); |
|
|
|
closesocket(socket_id); |
|
|
|
closesocket(id); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool WindowsSocket::init() |
|
|
|
bool WindowsSocket::init() |
|
|
|
{ |
|
|
|
{ |
|
|
|
//Binding step
|
|
|
|
//Binding step
|
|
|
|
int status; |
|
|
|
int status; |
|
|
|
if ((status = bind(socket_id, (struct sockaddr*)&addr, sizeof(addr)) < 0)) |
|
|
|
if ((status = bind(id, (struct sockaddr*)&addr, sizeof(addr)) < 0)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
log("bind failed " << WSAGetLastError()); |
|
|
|
log("bind failed " << WSAGetLastError()); |
|
|
|
return false; |
|
|
|
return false; |
|
|
@ -53,58 +65,43 @@ namespace Bk::Net { |
|
|
|
bool WindowsSocket::start(int cpt_conn = SOMAXCONN) |
|
|
|
bool WindowsSocket::start(int cpt_conn = SOMAXCONN) |
|
|
|
{ |
|
|
|
{ |
|
|
|
//Listening step
|
|
|
|
//Listening step
|
|
|
|
if (listen(socket_id, cpt_conn) == SOCKET_ERROR) |
|
|
|
if (listen(id, cpt_conn) == SOCKET_ERROR) { return false; } |
|
|
|
{ |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Connection WindowsSocket::ack() |
|
|
|
Connection WindowsSocket::ack() |
|
|
|
{ |
|
|
|
{ |
|
|
|
socklen_t addrlen = sizeof(addr); |
|
|
|
socklen_t addrlen = sizeof(addr); |
|
|
|
return accept(socket_id, (struct sockaddr*)&addr, &addrlen); |
|
|
|
return accept(id, (struct sockaddr*)&addr, &addrlen); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool WindowsSocket::conn() |
|
|
|
bool WindowsSocket::conn() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (connect(socket_id, (struct sockaddr*)&addr, sizeof(addr)) < 0) |
|
|
|
if (connect(id, (struct sockaddr*)&addr, sizeof(addr)) < 0) { return false; } |
|
|
|
{ |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void WindowsSocket::emit(std::vector<char> packet) |
|
|
|
void WindowsSocket::emit(std::vector<char> packet) |
|
|
|
{ |
|
|
|
{ |
|
|
|
send((SOCKET)socket_id, packet.data(), packet.size(), 0); |
|
|
|
send((SOCKET)id, packet.data(), packet.size(), 0); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void WindowsSocket::emit(Connection conn, std::vector<char> packet) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
send((SOCKET)conn, packet.data(), packet.size(), 0); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::vector<char> WindowsSocket::obtain(int size) |
|
|
|
std::vector<char> WindowsSocket::obtain(int size) |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::vector<char> buffer; |
|
|
|
std::vector<char> buffer; |
|
|
|
buffer.resize(size); |
|
|
|
buffer.resize(size); |
|
|
|
int read_size = recv((SOCKET)socket_id, buffer.data(), buffer.size() - 1, 0); |
|
|
|
int read_size = recv((SOCKET)id, buffer.data(), buffer.size() - 1, 0); |
|
|
|
buffer.resize(read_size); |
|
|
|
buffer.resize(read_size); |
|
|
|
return buffer; |
|
|
|
return buffer; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::vector<char> WindowsSocket::obtain(Connection conn, int size) |
|
|
|
std::unique_ptr<Socket> Socket::create(IpAddress ip, int port, IpProtocol proto) |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::vector<char> buffer; |
|
|
|
return std::unique_ptr<Socket>(new WindowsSocket(ip, port, proto)); |
|
|
|
buffer.resize(size); |
|
|
|
|
|
|
|
int read_size = recv((SOCKET)conn, buffer.data(), buffer.size() - 1, 0); |
|
|
|
|
|
|
|
buffer.resize(read_size); |
|
|
|
|
|
|
|
return buffer; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<Socket> Socket::create(IpAddress ip, int port, IpProtocol proto) |
|
|
|
std::unique_ptr<Socket> Socket::create(int id, IpVersion ver, IpProtocol proto) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return std::unique_ptr<Socket>(new WindowsSocket(ip, port, proto)); |
|
|
|
return std::unique_ptr<Socket>(new WindowsSocket(int id, IpVersion ver, IpProtocol proto)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |