|
|
|
@ -1,14 +1,14 @@ |
|
|
|
|
#include "linux_socket.h" |
|
|
|
|
#include <netinet/in.h> |
|
|
|
|
#include <sys/socket.h> |
|
|
|
|
|
|
|
|
|
namespace Bk::Net { |
|
|
|
|
LinuxSocket::LinuxSocket(int id, IpVersion ver, IpProtocol proto) |
|
|
|
|
: id(id), ip_proto(proto) |
|
|
|
|
LinuxSocket::LinuxSocket(int id, struct sockaddr_in client_addr, IpVersion ver, IpProtocol proto) |
|
|
|
|
: id(id), ip_proto(proto), addr(client_addr) |
|
|
|
|
{ |
|
|
|
|
char myIP[16] = " "; |
|
|
|
|
socklen_t len = sizeof(addr); |
|
|
|
|
getsockname(id, (struct sockaddr*)&addr, &len); |
|
|
|
|
inet_ntop((int)ver, &addr, myIP, sizeof(myIP)); |
|
|
|
|
ip_addr = IpAddress(std::string(myIP, 16), ver); |
|
|
|
|
|
|
|
|
|
char* myIP = inet_ntoa(addr.sin_addr); |
|
|
|
|
ip_addr = IpAddress(std::string(myIP), ver); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
LinuxSocket::LinuxSocket(IpAddress ip, int port, IpProtocol proto) |
|
|
|
@ -55,8 +55,9 @@ namespace Bk::Net { |
|
|
|
|
|
|
|
|
|
Socket* LinuxSocket::ack() |
|
|
|
|
{ |
|
|
|
|
socklen_t addrlen = sizeof(addr); |
|
|
|
|
return new LinuxSocket(accept(id, (struct sockaddr*)&addr, &addrlen), ip_addr.version, ip_proto); |
|
|
|
|
struct sockaddr_in client_addr; |
|
|
|
|
socklen_t addrlen = sizeof(client_addr); |
|
|
|
|
return new LinuxSocket(accept(id, (struct sockaddr*)&client_addr, &addrlen), client_addr, ip_addr.version, ip_proto); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool LinuxSocket::conn() |
|
|
|
@ -69,14 +70,6 @@ namespace Bk::Net { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool LinuxSocket::hasConnection(int seconds, int microseconds) { |
|
|
|
|
struct timeval tv = { seconds, microseconds }; |
|
|
|
|
fd_set rfds; |
|
|
|
|
FD_ZERO(&rfds); |
|
|
|
|
FD_SET(id, &rfds); |
|
|
|
|
return select(id + 1, &rfds, (fd_set*)0, (fd_set*)0, &tv) > 0; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
void LinuxSocket::emit(std::vector<char> packet) |
|
|
|
|
{ |
|
|
|
|
write(id, packet.data(), packet.size()); |
|
|
|
@ -95,9 +88,4 @@ namespace Bk::Net { |
|
|
|
|
{ |
|
|
|
|
return new LinuxSocket(ip, port, proto); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Socket* Socket::create(int id, IpVersion ver, IpProtocol proto) |
|
|
|
|
{ |
|
|
|
|
return new LinuxSocket(id, ver, proto); |
|
|
|
|
} |
|
|
|
|
} |