summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2012-03-13 01:40:49 (GMT)
committerH. Peter Anvin <hpa@zytor.com>2012-03-13 01:40:49 (GMT)
commit4fd247e04834a6033f1eba42e2493af1356ed34a (patch)
treed581b28ea35f6d241a41cd2ec6ccdd8ce500ad61
parent75af84a7f18680f11a5cd4d85098a7caf856e890 (diff)
downloadgrv-4fd247e04834a6033f1eba42e2493af1356ed34a.zip
grv-4fd247e04834a6033f1eba42e2493af1356ed34a.tar.gz
grv-4fd247e04834a6033f1eba42e2493af1356ed34a.tar.bz2
grv-4fd247e04834a6033f1eba42e2493af1356ed34a.tar.xz
network: drop support for pre-NT Windows bug workaround
Pre-NT versions of Windows treated sockets as fundamentally different from file handles. In NT-based version of Windows they are "OS handles" which is an abstraction *below* file descriptors, but at least they can be coerced into being file descriptors, and so are one level of braindamage less. Thus, we can remove the whole netcon_t abstraction and all the warning messages associated with pointer type mismatches that go along with it. Signed-off-by: H. Peter Anvin <hpa@zytor.com>
-rw-r--r--grvscored.c10
-rw-r--r--highscore.c16
-rw-r--r--highscore.h7
-rw-r--r--netopen.c171
-rw-r--r--network.h21
-rw-r--r--scoretbl.c15
6 files changed, 43 insertions, 197 deletions
diff --git a/grvscored.c b/grvscored.c
index 7e60df4..01ce8f7 100644
--- a/grvscored.c
+++ b/grvscored.c
@@ -27,10 +27,10 @@ int score_send(const char *file)
flock(fileno(f), LOCK_SH);
alarm(120); /* Make sure we have a sane timeout */
- highscore_parse(f, fgets, 0);
+ highscore_parse(f, 0);
fclose(f);
- return highscore_write(stdout, fputs, 0) ? 1 : 0;
+ return highscore_write(stdout, 0) ? 1 : 0;
}
int score_recv(const char *file)
@@ -49,9 +49,9 @@ int score_recv(const char *file)
flock(fileno(f), LOCK_EX);
alarm(120); /* Make sure we have a sane timeout */
- highscore_parse(f, fgets, 0);
- highscore_parse(stdin, fgets, 0);
- if ( highscore_write(t, fputs, 0) ) {
+ highscore_parse(f, 0);
+ highscore_parse(stdin, 0);
+ if ( highscore_write(t, 0) ) {
unlink(tp);
fclose(f);
return 1;
diff --git a/highscore.c b/highscore.c
index e7a4073..77a8664 100644
--- a/highscore.c
+++ b/highscore.c
@@ -27,13 +27,13 @@ void highscore_load(void)
if ( !f )
return;
- highscore_parse(f, fgets, 1);
+ highscore_parse(f, 1);
fclose(f);
}
void highscore_download(void)
{
- netcon_t n;
+ FILE *n;
if ( opt.nonet )
return;
@@ -42,8 +42,8 @@ void highscore_download(void)
if ( !n )
return;
- highscore_parse(n, fgets_network, 0);
- fclose_network(n);
+ highscore_parse(n, 0);
+ fclose(n);
}
void highscore_save(void)
@@ -54,13 +54,13 @@ void highscore_save(void)
if ( !f )
return;
- highscore_write(f, fputs, 1);
+ highscore_write(f, 1);
fclose(f);
}
void highscore_upload(void)
{
- netcon_t n;
+ FILE *n;
if ( opt.nonet )
return;
@@ -74,8 +74,8 @@ void highscore_upload(void)
if ( !n )
return;
- highscore_write(n, fputs_network, 0);
- fclose_network(n);
+ highscore_write(n, 0);
+ fclose(n);
}
/*
diff --git a/highscore.h b/highscore.h
index 5976c69..ba10d40 100644
--- a/highscore.h
+++ b/highscore.h
@@ -6,6 +6,7 @@
#define HIGHSCORE_H 1
#include <inttypes.h>
+#include <stdio.h>
#define PLAYER_LEN 11 /* Max length of player alias */
#define MAX_BEST 100 /* Number of best_total to keep track of */
@@ -49,8 +50,8 @@ int highscore_add_level_score(uint64_t gameid, int lvl, int64_t score,
int highscore_add_level_time(uint64_t gameid, int lvl, int32_t time_ms,
uint8_t upload);
void highscore_init(void);
-void highscore_parse(void *f, char (*mygets)(char *, int, void *),
- uint8_t upload);
-int highscore_write(void *f, int (*myputs)(char *, void *), uint8_t leave);
+void highscore_parse(FILE *f, uint8_t upload);
+int highscore_write(FILE *f, uint8_t leave);
+int have_upload_scores(void);
#endif
diff --git a/netopen.c b/netopen.c
index cf17cda..bec777d 100644
--- a/netopen.c
+++ b/netopen.c
@@ -18,16 +18,6 @@
#include <winsock2.h>
#include <ws2tcpip.h>
-/* This is basically a lame subset of stdio functionality */
-#define NETCON_BUF 65536
-struct netcon {
- int is_output;
- int buffill;
- char *bufdata;
- SOCKET sock;
- unsigned char buffer[NETCON_BUF];
-};
-
static int winsock_started = 0;
int socket_errno; /* For debuggability */
@@ -49,148 +39,23 @@ static void start_winsock(void)
}
}
-static int input_get(netcon_t nc)
+static FILE *sock_to_file(SOCKET sock, int is_write)
{
- int rv;
-
- rv = recv(nc->sock, nc->buffer, NETCON_BUF, 0);
- if ( rv == SOCKET_ERROR )
- socket_errno = WSAGetLastError();
-
- nc->bufdata = nc->buffer;
- nc->buffill = (rv == SOCKET_ERROR) ? 0 : rv;
+ FILE *f;
+ int fd;
+ int osfhandle = sock;
- return rv;
-}
-
-static int output_send(netcon_t nc)
-{
- char *p = nc->buffer;
- int count = nc->buffill;
- int oc = count;
- int rv;
-
- while ( count ) {
- rv = send(nc->sock, p, count, 0);
- if ( rv == 0 || rv == SOCKET_ERROR ) {
- socket_errno = WSAGetLastError();
- return -1;
- }
-
- p += rv;
- count -= rv;
- }
-
- nc->bufdata = nc->buffer;
- nc->buffill = 0;
-
- return oc;
-}
-
-int fgetc_network(netcon_t nc)
-{
- int rv;
-
- if ( !nc->buffill ) {
- rv = input_get(nc);
- if ( rv == 0 || rv == SOCKET_ERROR )
- return EOF;
- }
-
- rv = *nc->bufdata;
-
- nc->bufdata++;
- nc->buffill--;
-
- return rv;
-}
-
-char *fgets_network(char *s, int size, netcon_t nc)
-{
- int ch;
- char *p = s;
- int cr = 0;
-
- if ( size == 0 )
+ fd = _open_osfhandle(&osfhandle, is_write ? 0 : _O_RDONLY);
+ if (fd < 0) {
+ closesocket(fd);
return NULL;
-
- while ( size > 1 ) {
- ch = fgetc_network(nc);
- if ( ch == EOF )
- break;
-
- *p++ = ch;
- cr++;
-
- if ( ch == '\n' )
- break;
}
- *p = '\0';
-
- return cr ? s : NULL;
-}
+ f = fdopen(fd, is_write ? "w" : "r");
+ if (!f)
+ _close(fd);
-int fputs_network(const char *s, netcon_t nc)
-{
- int b = strlen(s);
- int sl;
-
- while ( b ) {
- sl = NETCON_BUF - nc->buffill; /* Space left */
-
- if ( !sl ) {
- if ( output_send(nc) <= 0 )
- return EOF; /* Error */
- else
- continue;
- }
-
- if ( sl > b )
- sl = b;
-
- memcpy(nc->bufdata, s, sl);
-
- nc->buffill += sl;
- nc->bufdata += sl;
- s += sl;
- b -= sl;
- }
-
- return 0;
-}
-
-netcon_t sock_to_netcon(SOCKET sock, int is_write)
-{
- struct netcon *nc = malloc(sizeof(struct netcon));
-
- if ( !nc ) {
- closesocket(sock);
- return NULL;
- }
-
- /* Construct a struct netcon object */
- nc->bufdata = nc->buffer;
- nc->buffill = 0;
- nc->sock = sock;
- nc->is_output = is_write;
-
- return nc;
-}
-
-int fclose_network(netcon_t nc)
-{
- int rv = 0;
-
- if ( nc->is_output && nc->buffill ) {
- if ( output_send(nc) <= 0 )
- rv = EOF;
- }
-
- closesocket(nc->sock);
- free(nc);
-
- return rv;
+ return f;
}
#else
@@ -209,13 +74,13 @@ typedef int SOCKET;
#define start_winsock() do { } while (0)
#define closesocket(x) close(x)
-static netcon_t sock_to_netcon(int sock, int is_write)
+static FILE *sock_to_file(SOCKET sock, int is_write)
{
- FILE *f = fdopen(sock, is_write ? "w" : "r");
- if ( !f ) {
+ FILE *f;
+
+ f = fdopen(sock, is_write ? "w" : "f");
+ if (!f)
close(sock);
- return NULL;
- }
return f;
}
@@ -305,7 +170,7 @@ static SOCKET open_socket(const char *name, int port)
#endif
-netcon_t fopen_network(const char *name, int port, int is_write)
+FILE *fopen_network(const char *name, int port, int is_write)
{
SOCKET sock;
@@ -313,5 +178,5 @@ netcon_t fopen_network(const char *name, int port, int is_write)
if (sock == INVALID_SOCKET)
return NULL;
- return sock_to_netcon(sock, is_write);
+ return sock_to_file(sock, is_write);
}
diff --git a/network.h b/network.h
index 386221a..fd0ea6f 100644
--- a/network.h
+++ b/network.h
@@ -7,25 +7,6 @@
#include <stdio.h>
-#ifndef WIN32
-/* Sane systems */
-
-typedef FILE * netcon_t;
-# define fgets_network fgets
-# define fputs_network fputs
-# define fclose_network fclose
-
-#else /* WIN32 */
-
-struct netcon;
-typedef struct netcon *netcon_t;
-
-int fputs_network(const char *, netcon_t);
-char *fgets_network(char *, int, netcon_t);
-int fclose_network(netcon_t);
-
-#endif /* WIN32 */
-
-netcon_t fopen_network(const char *name, int port, int is_write);
+FILE *fopen_network(const char *name, int port, int is_write);
#endif /* NETWORK_H */
diff --git a/scoretbl.c b/scoretbl.c
index 0b7e877..fb57489 100644
--- a/scoretbl.c
+++ b/scoretbl.c
@@ -173,8 +173,7 @@ static char *quote(const unsigned char *i)
* kind of sane way to use the stdio library with sockets, we have to abstract
* out the I/O function that is allowed to access this. Puke.
*/
-void highscore_parse(void *f, char (*mygets)(char *, int, void *),
- uint8_t upload)
+void highscore_parse(FILE *f, uint8_t upload)
{
char line[MAXLINE], pname[MAXLINE];
uint64_t gameid;
@@ -183,7 +182,7 @@ void highscore_parse(void *f, char (*mygets)(char *, int, void *),
int lvl;
unsigned char *pptr;
- while ( mygets(line, MAXLINE, f) ) {
+ while ( fgets(line, MAXLINE, f) ) {
if ( !strchr(line, '\n') )
return; /* Something bogus here... */
@@ -207,19 +206,19 @@ void highscore_parse(void *f, char (*mygets)(char *, int, void *),
/*
* Write out a high score file; return 0 on success, -1 on failure
*/
-int highscore_write(void *f, int (*myputs)(char *, void *), uint8_t leave)
+int highscore_write(FILE *f, uint8_t leave)
{
char buffer[MAXLINE];
int i, j;
- myputs("VC 1.0\n", f); /* Version C 1.0 */
+ fputs("VC 1.0\n", f); /* Version C 1.0 */
for ( i = 0 ; i < MAX_BEST ; i++ ) {
if ( bests.total[i].score ) {
sprintf(buffer, "TS %" PRIx64 " %" PRId64 " %d %s\n",
bests.total[i].gameid, bests.total[i].score,
bests.total[i].endlvl+1, quote(bests.total[i].player));
- if ( myputs(buffer, f) == EOF )
+ if ( fputs(buffer, f) == EOF )
return -1;
bests.total[i].upload &= leave;
}
@@ -231,7 +230,7 @@ int highscore_write(void *f, int (*myputs)(char *, void *), uint8_t leave)
sprintf(buffer, "LS %" PRIx64 " %d %" PRId64 "\n",
bests.level[i].score[j].gameid,
i+1, bests.level[i].score[j].score);
- if ( myputs(buffer, f) == EOF )
+ if ( fputs(buffer, f) == EOF )
return -1;
bests.level[i].score[j].upload &= leave;
}
@@ -240,7 +239,7 @@ int highscore_write(void *f, int (*myputs)(char *, void *), uint8_t leave)
sprintf(buffer, "LT %" PRIx64 " %d %" PRId32 "\n",
bests.level[i].time_ms[j].gameid,
i+1, bests.level[i].time_ms[j].time_ms);
- if ( myputs(buffer, f) == EOF )
+ if ( fputs(buffer, f) == EOF )
return -1;
bests.level[i].time_ms[j].upload &= leave;
}