Skip to content

Commit

Permalink
Non-blocking input
Browse files Browse the repository at this point in the history
  • Loading branch information
makuke1234 committed Apr 22, 2022
1 parent 065fc81 commit 776f57a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
26 changes: 11 additions & 15 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static inline void keyboardHandler(udp_t * restrict udp, char * restrict buffer,
{
udpThread_t udpThread;
udpThread_init(udp, &udpThread);
if (!udpThread_read(&udpThread, buffer, buflen))
if (!udpThread_read(&udpThread, buffer, buflen, fout))
{
fprintf(stderr, "Async server receiver thread creation failed!\n");
exit(1);
Expand Down Expand Up @@ -117,11 +117,6 @@ static inline void keyboardHandler(udp_t * restrict udp, char * restrict buffer,
break;
}
}
if (udpThread_hasIncome(&udpThread))
{
fwrite(buffer, buflen, 1, fout);
udpThread_received(&udpThread);
}
Sleep(1);
}

Expand Down Expand Up @@ -317,16 +312,17 @@ void printhelp(const char * restrict app)
printf(
"Correct usage:\n"
"\n"
"%s --server --port=<port number>\n"
"%s --client --ip=<IP address> --port=<port number>\n"
"%s --server --port=<port number> --output=<file>\n"
"%s --client --ip=<IP address> --port=<port number> --output=<file>\n"
"\n"
"All switches:\n"
" --help - Shows this page\n"
" --? - Shows this page\n"
" --server - Specifies server mode\n"
" --client - Specifies client mode\n"
" --ip=<addr> - Specifies <addr> as the IPv4 address to connect to\n"
" --port=<port> - Specifies <port> as the port number in the range of 0 to 65535\n"
" --help - Shows this page\n"
" --? - Shows this page\n"
" --server - Specifies server mode\n"
" --client - Specifies client mode\n"
" --ip=<addr> - Specifies <addr> as the IPv4 address to connect to\n"
" --port=<port> - Specifies <port> as the port number in the range of 0 to 65535\n"
" --output=<file> - Specifies the binary output file\n"
"\n"
"Methods for closing the program:\n"
"Ctrl+Z\n"
Expand Down Expand Up @@ -383,7 +379,7 @@ int main(int argc, char ** argv)
printhelp(argv[0]);
return 1;
}
int buflen = BUFLEN;
int buflen = 2000000;

// Opens output file in binary mode
FILE * fout = fopen(bufs[1], "wb");
Expand Down
24 changes: 10 additions & 14 deletions src/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ void udpThread_init(udp_t * restrict udp, udpThread_t * restrict uThread)
.u = udp,
.hThread = INVALID_HANDLE_VALUE,
.killSwitch = false,
.hasIncome = false
.hasIncome = false,
.fout = NULL
};
}

#define UDP_THREAD_STACK_SIZE 20
#define UDP_THREAD_STACK_SIZE 40
#define UDP_THREAD_TIMEOUT_US 10

static inline DWORD WINAPI s_udpThread_read_thread(LPVOID lpParams)
Expand All @@ -68,7 +69,7 @@ static inline DWORD WINAPI s_udpThread_read_thread(LPVOID lpParams)
fd_set currentSockets, readySockets;
FD_ZERO(&currentSockets);
FD_SET(uThread->u->s, &currentSockets);

while (!uThread->killSwitch)
{
struct timeval timeout = {
Expand All @@ -86,21 +87,24 @@ static inline DWORD WINAPI s_udpThread_read_thread(LPVOID lpParams)

if (FD_ISSET(uThread->u->s, &readySockets))
{
if (udp_read(uThread->u, uThread->buffer, uThread->bufSize) > 0)
int numBytes = udp_read(uThread->u, uThread->buffer, uThread->bufSize);
if (numBytes > 0)
{
uThread->hasIncome = true;
// Set received packet size before setting the flag
fwrite(uThread->buffer, (size_t)numBytes, 1, uThread->fout);
}
}
}

return 0;
}
bool udpThread_read(udpThread_t * restrict uThread, void * restrict buffer, int bufSize)
bool udpThread_read(udpThread_t * restrict uThread, void * restrict buffer, int bufSize, FILE * restrict fout)
{
uThread->killSwitch = false;
uThread->hasIncome = false;
uThread->buffer = buffer;
uThread->bufSize = bufSize;
uThread->fout = fout;
uThread->hThread = CreateThread(
NULL,
UDP_THREAD_STACK_SIZE,
Expand All @@ -111,14 +115,6 @@ bool udpThread_read(udpThread_t * restrict uThread, void * restrict buffer, int
);
return uThread->hThread != INVALID_HANDLE_VALUE;
}
bool udpThread_hasIncome(const udpThread_t * restrict uThread)
{
return uThread->hasIncome;
}
void udpThread_received(udpThread_t * restrict uThread)
{
uThread->hasIncome = false;
}

bool udpThread_stopRead(udpThread_t * restrict uThread)
{
Expand Down
7 changes: 4 additions & 3 deletions src/udp.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef UDP_H
#define UDP_H

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <WinSock2.h>
Expand All @@ -21,6 +22,8 @@ typedef struct udpThread
void * buffer;
int bufSize;

FILE * fout;

} udpThread_t;

bool udp_init(void);
Expand All @@ -36,9 +39,7 @@ bool udp_free(void);

void udpThread_init(udp_t * restrict udp, udpThread_t * restrict uThread);

bool udpThread_read(udpThread_t * restrict uThread, void * restrict buffer, int bufSize);
bool udpThread_hasIncome(const udpThread_t * restrict uThread);
void udpThread_received(udpThread_t * restrict uThread);
bool udpThread_read(udpThread_t * restrict uThread, void * restrict buffer, int bufSize, FILE * restrict fout);

bool udpThread_stopRead(udpThread_t * restrict uThread);

Expand Down

0 comments on commit 776f57a

Please sign in to comment.