fixes
This commit is contained in:
parent
9bdf133d0f
commit
c49f947a9b
6 changed files with 123 additions and 21 deletions
|
|
@ -48,22 +48,13 @@ ClientTCP::ClientTCP(int socketid) : socketid(socketid)
|
|||
|
||||
ClientTCP::ClientTCP(const char* hostName, std::uint16_t port)
|
||||
{
|
||||
hostent *host;
|
||||
sockaddr_in serv_addr;
|
||||
|
||||
host=gethostbyname(hostName);
|
||||
if((socketid = socket(AF_INET, SOCK_STREAM, 0)) == -1){
|
||||
std::cerr << "Could not open socket" << std::endl;
|
||||
}
|
||||
|
||||
host = gethostbyname(hostName);
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_port = htons(port);
|
||||
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
|
||||
bzero(&(serv_addr.sin_zero),8);
|
||||
|
||||
if(connect(socketid,(sockaddr*)&serv_addr, sizeof(sockaddr)) == -1){
|
||||
std::cerr << "Could not connect to server" << std::endl;
|
||||
}
|
||||
Connect();
|
||||
}
|
||||
|
||||
ClientTCP::ClientTCP(std::string hostName, std::uint16_t port): ClientTCP(hostName.c_str(), port)
|
||||
|
|
@ -71,6 +62,14 @@ ClientTCP::ClientTCP(std::string hostName, std::uint16_t port): ClientTCP(hostNa
|
|||
|
||||
}
|
||||
|
||||
ClientTCP::ClientTCP(ClientTCP&& other) noexcept : socketid(other.socketid) {
|
||||
if(socketid != 1) {
|
||||
shutdown(socketid, SHUT_RDWR);
|
||||
close(socketid);
|
||||
}
|
||||
other.socketid = -1;
|
||||
}
|
||||
|
||||
ClientTCP::~ClientTCP()
|
||||
{
|
||||
if(socketid != 1) {
|
||||
|
|
@ -79,6 +78,16 @@ ClientTCP::~ClientTCP()
|
|||
}
|
||||
}
|
||||
|
||||
void ClientTCP::Connect() {
|
||||
if((socketid = socket(AF_INET, SOCK_STREAM, 0)) == -1){
|
||||
std::cerr << "Could not open socket" << std::endl;
|
||||
}
|
||||
|
||||
if(connect(socketid,(sockaddr*)&serv_addr, sizeof(sockaddr)) == -1){
|
||||
std::cerr << "Could not connect to server" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void ClientTCP::Stop() {
|
||||
shutdown(socketid, SHUT_RDWR);
|
||||
close(socketid);
|
||||
|
|
@ -86,9 +95,23 @@ void ClientTCP::Stop() {
|
|||
}
|
||||
|
||||
void ClientTCP::Send(const void* buffer, std::uint32_t size) const {
|
||||
send(socketid, reinterpret_cast<const char*>(buffer), size, 0);
|
||||
}
|
||||
const char* data = reinterpret_cast<const char*>(buffer);
|
||||
std::uint_fast32_t totalSent = 0;
|
||||
const std::uint_fast32_t chunkSize = 1024;
|
||||
|
||||
while (totalSent < size) {
|
||||
std::uint_fast32_t bytesToSend = std::min(chunkSize, size - totalSent);
|
||||
int status = send(socketid, data + totalSent, bytesToSend, 0);
|
||||
|
||||
if (status == 0) {
|
||||
throw SocketClosedException();
|
||||
} else if (status < 0) {
|
||||
throw std::runtime_error(std::strerror(errno));
|
||||
}
|
||||
|
||||
totalSent += status;
|
||||
}
|
||||
}
|
||||
std::vector<char> ClientTCP::RecieveSync(std::uint32_t bufferSize) const {
|
||||
std::vector<char> totalBuffer(bufferSize);
|
||||
int read = recv(socketid, totalBuffer.data(), bufferSize, 0);
|
||||
|
|
@ -159,9 +182,9 @@ std::vector<char> ClientTCP::RecieveUntilFullSync(std::uint32_t bufferSize) cons
|
|||
int read = 0;
|
||||
while(read < bufferSize) {
|
||||
int newRead = recv(socketid, buffer.data()+read, bufferSize-read, 0);
|
||||
if(read < 0) {
|
||||
if(newRead < 0) {
|
||||
throw std::runtime_error(std::strerror(errno));
|
||||
} else if(read == 0) {
|
||||
} else if(newRead == 0) {
|
||||
throw SocketClosedException();
|
||||
}
|
||||
read+=newRead;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue