tools/hexnet: start implementing console input

Soda
Shwoomple 2022-08-30 00:51:53 +05:30
parent 209d0828b4
commit 48f8a7dcf2
1 changed files with 15 additions and 0 deletions

View File

@ -84,6 +84,16 @@ void signalHandler(int signal) {
std::exit(signal); std::exit(signal);
} }
void readFromConsole(){
while(!exitProgram){
char * test = new char[420];
consoleMutex.lock();
std::cout << "> ";
std::cin.read(test, 1);
consoleMutex.unlock();
}
}
void readFromTCPSocket(sockpp::tcp_socket* socket, int64_t mtu) { void readFromTCPSocket(sockpp::tcp_socket* socket, int64_t mtu) {
ssize_t numBytes; ssize_t numBytes;
uint8_t buffer[mtu]; uint8_t buffer[mtu];
@ -91,9 +101,11 @@ void readFromTCPSocket(sockpp::tcp_socket* socket, int64_t mtu) {
while (!exitProgram && (numBytes = socket->read(buffer, sizeof(buffer))) > 0) { while (!exitProgram && (numBytes = socket->read(buffer, sizeof(buffer))) > 0) {
tcpSocketMutex.unlock(); tcpSocketMutex.unlock();
consoleMutex.lock(); consoleMutex.lock();
std::cout << "\n";
for (ssize_t i=0; i<numBytes; i++) { for (ssize_t i=0; i<numBytes; i++) {
std::cout << std::hex << std::setfill('0') << std::setw(2) << (short) buffer[i]; std::cout << std::hex << std::setfill('0') << std::setw(2) << (short) buffer[i];
} }
std::cout << "\n";
std::cout.flush(); std::cout.flush();
consoleMutex.unlock(); consoleMutex.unlock();
tcpSocketMutex.lock(); tcpSocketMutex.lock();
@ -314,6 +326,9 @@ int main(int argc, char* argv[]){
} }
std::thread threadReadFromTCP = std::thread(readFromTCPSocket, tcpSocket, mtu); std::thread threadReadFromTCP = std::thread(readFromTCPSocket, tcpSocket, mtu);
std::thread threadReadFromConsole = std::thread(readFromConsole);
threadReadFromConsole.join();
threadReadFromTCP.join(); threadReadFromTCP.join();
delete tcpSocket; delete tcpSocket;