Better linux socket

main
anulax1225 ago%!(EXTRA string=2 months)
parent 0f5e3e6d77
commit 67b2440096
  1. 2
      src/bakanet/core/socket.h
  2. 2
      src/platform/linux/linux_ip_address.cpp
  3. 29
      src/platform/linux/linux_socket.cpp
  4. 4
      src/platform/linux/linux_socket.h

@ -19,6 +19,8 @@ namespace Bk::Net {
virtual Socket* ack() = 0; virtual Socket* ack() = 0;
virtual bool conn() = 0; virtual bool conn() = 0;
virtual bool isConnected() = 0;
virtual int get_raw_socket() = 0; virtual int get_raw_socket() = 0;
virtual const std::string get_ip() = 0; virtual const std::string get_ip() = 0;

@ -1,6 +1,4 @@
#include <bakanet/core/ip_address.h> #include <bakanet/core/ip_address.h>
#include <string.h>
namespace Bk::Net { namespace Bk::Net {
struct in_addr IpAddress::get_data() struct in_addr IpAddress::get_data()
{ {

@ -1,4 +1,6 @@
#include "linux_socket.h" #include "linux_socket.h"
#include <cstddef>
#include <unistd.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -6,7 +8,6 @@ namespace Bk::Net {
LinuxSocket::LinuxSocket(int id, struct sockaddr_in client_addr, IpVersion ver, IpProtocol proto) LinuxSocket::LinuxSocket(int id, struct sockaddr_in client_addr, IpVersion ver, IpProtocol proto)
: id(id), ip_proto(proto), addr(client_addr) : id(id), ip_proto(proto), addr(client_addr)
{ {
char* myIP = inet_ntoa(addr.sin_addr); char* myIP = inet_ntoa(addr.sin_addr);
ip_addr = IpAddress(std::string(myIP), ver); ip_addr = IpAddress(std::string(myIP), ver);
} }
@ -62,7 +63,7 @@ namespace Bk::Net {
bool LinuxSocket::conn() bool LinuxSocket::conn()
{ {
if (connect(id, (struct sockaddr*)&addr, sizeof(addr)) < 0) if (connect(id, (struct sockaddr*)&addr, sizeof(addr)) != 0)
{ {
BK_CORE_ERROR("Connection failed"); BK_CORE_ERROR("Connection failed");
return false; return false;
@ -70,18 +71,30 @@ namespace Bk::Net {
return true; return true;
} }
bool LinuxSocket::isConnected()
{
char data = 0;
int read_size = recv(id, &data, 1, MSG_PEEK);
return (bool)read_size;
}
void LinuxSocket::emit(std::vector<char> packet) void LinuxSocket::emit(std::vector<char> packet)
{ {
write(id, packet.data(), packet.size()); size_t size = send(id, packet.data(), packet.size(), 0);
} }
std::vector<char> LinuxSocket::obtain(int size) std::vector<char> LinuxSocket::obtain(int size)
{ {
std::vector<char> buffer; if(size > this->size)
buffer.resize(size); {
int read_size = read(id, buffer.data(), buffer.size() - 1); delete [] buffer;
buffer.resize(read_size); buffer = new char[size];
return buffer; this->size = size;
}
int read_size = recv(id, buffer, this->size - 1, 0);
if(read_size == -1) return std::vector<char>(0);
std::vector<char> packet(buffer, buffer + read_size);
return packet;
} }
Socket* Socket::create(IpAddress ip, int port, IpProtocol proto) Socket* Socket::create(IpAddress ip, int port, IpProtocol proto)

@ -16,6 +16,8 @@ namespace Bk::Net {
Socket* ack() override; Socket* ack() override;
bool conn() override; bool conn() override;
bool isConnected() override;
int get_raw_socket() override { return id; } int get_raw_socket() override { return id; }
const std::string get_ip() override { return ip_addr.str; } const std::string get_ip() override { return ip_addr.str; }
@ -24,6 +26,8 @@ namespace Bk::Net {
private: private:
Connection id; Connection id;
int size = 1024;
char* buffer = new char[size];
struct sockaddr_in addr; struct sockaddr_in addr;
IpAddress ip_addr; IpAddress ip_addr;
IpProtocol ip_proto; IpProtocol ip_proto;

Loading…
Cancel
Save