tcp nes wip
This commit is contained in:
@@ -132,20 +132,25 @@ std::string TcpNet::client(const std::string &address, int portno, const std::st
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::string TcpNet::read_from_socket(int sockfd) {
|
std::string TcpNet::read_from_socket(int sockfd) {
|
||||||
char buffer[TCPNET_BUFFER_SIZE];
|
char buffer[TCPNET_BUFFER_SIZE];
|
||||||
std::string request;
|
std::string request;
|
||||||
|
|
||||||
// read length header
|
// read length header
|
||||||
unsigned long long bytesLen = 0;
|
unsigned long long readdatalen = 0;
|
||||||
if (USE_LENGTH_HEADER) {
|
if (USE_LENGTH_HEADER) {
|
||||||
long n = read(sockfd, &bytesLen, sizeof(bytesLen));
|
long n = read(sockfd, &readdatalen, sizeof(readdatalen));
|
||||||
if (n != 0 && n != sizeof(bytesLen))
|
if (n != 0 && n != sizeof(readdatalen))
|
||||||
error("ERROR reading length header failed");
|
error("ERROR reading length header failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
// read data
|
// read data
|
||||||
long n;
|
long n;
|
||||||
|
long readed = 0;
|
||||||
do {
|
do {
|
||||||
memset(buffer, 0, TCPNET_BUFFER_SIZE);
|
memset(buffer, 0, TCPNET_BUFFER_SIZE);
|
||||||
n = read(sockfd, buffer, TCPNET_BUFFER_SIZE - 1);
|
n = read(sockfd, buffer, TCPNET_BUFFER_SIZE - 1);
|
||||||
@@ -160,25 +165,26 @@ std::string TcpNet::read_from_socket(int sockfd) {
|
|||||||
|
|
||||||
std::string part{buffer};
|
std::string part{buffer};
|
||||||
request.append(part);
|
request.append(part);
|
||||||
} while ((USE_LENGTH_HEADER && n < bytesLen) || n == TCPNET_BUFFER_SIZE - 1); // TODO what if data exactly of this size
|
readed += n;
|
||||||
|
} while ((USE_LENGTH_HEADER && readed < readdatalen) || n == TCPNET_BUFFER_SIZE - 1); // TODO what if data exactly of this size
|
||||||
|
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpNet::write_to_socket(int sockfd, const std::string &str) {
|
void TcpNet::write_to_socket(int sockfd, const std::string &str) {
|
||||||
const char *buffer = str.c_str();
|
const char *buffer = str.c_str();
|
||||||
int pos = 0;
|
|
||||||
long n;
|
long n;
|
||||||
|
|
||||||
// write length header
|
// write length header
|
||||||
unsigned long long bytesLen = str.length();
|
unsigned long long writedatalen = str.length();
|
||||||
if (USE_LENGTH_HEADER) {
|
if (USE_LENGTH_HEADER) {
|
||||||
n = write(sockfd, &bytesLen, (int) sizeof(bytesLen));
|
n = write(sockfd, &writedatalen, (int) sizeof(writedatalen));
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
error("ERROR writing size number to socket");
|
error("ERROR writing size number to socket");
|
||||||
}
|
}
|
||||||
|
|
||||||
// write data
|
// write data
|
||||||
|
int pos = 0;
|
||||||
do {
|
do {
|
||||||
n = write(sockfd, buffer + pos, (int) (str.length() - pos));
|
n = write(sockfd, buffer + pos, (int) (str.length() - pos));
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
|
|||||||
1
ml.cpp
1
ml.cpp
@@ -21,6 +21,7 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|||||||
@@ -6,6 +6,9 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
|
||||||
std::string read_file_contents(const std::string &filename) {
|
std::string read_file_contents(const std::string &filename) {
|
||||||
std::ifstream f;
|
std::ifstream f;
|
||||||
|
|||||||
Reference in New Issue
Block a user