Permalink
Browse files

Some cleanups.

  • Loading branch information...
1 parent 939a083 commit b13d57ddce8bfaff9cf95c252f87d3bbc47f2266 gammy committed Apr 8, 2012
Showing with 21 additions and 31 deletions.
  1. +1 −0 Makefile
  2. +4 −7 main.c
  3. +13 −20 net.c
  4. +3 −4 net.h
View
@@ -12,6 +12,7 @@ all: $(EXE)
$(EXE): $(OBJS)
@echo -e "\e[4;1m$@\e[0m"
$(CC) $(CFLAGS) $(LFLAGS) -o $(EXE) $(OBJS)
+ @strip $(EXE)
@echo
@echo "Success: $(EXE)"
View
11 main.c
@@ -42,7 +42,7 @@ void usage(char *me) {
void signal_handle(int sig) {
endwin();
fprintf(stderr, "Caught signal %d\n", sig);
- net_deinit();
+ net_finish();
exit(EXIT_SUCCESS);
}
@@ -124,9 +124,6 @@ int main(int argc, char *argv[]){
signal_install();
- if(! net_init())
- return(EXIT_FAILURE);
-
#ifdef DEBUG
if(mode == MODE_CLIENT)
printf("Host: %s\n", host);
@@ -170,11 +167,11 @@ int main(int argc, char *argv[]){
ui_keypress(&UI_TOP, c);
if(c != ERR)
- net_send(mode, &c);
+ net_send(&c);
c = ERR;
- if(net_recv(mode, &c))
+ if(net_recv(&c))
ui_keypress(&UI_BOT, c);
#ifdef DEBUG
@@ -187,7 +184,7 @@ int main(int argc, char *argv[]){
}
endwin();
- net_deinit();
+ net_finish();
return(EXIT_SUCCESS);
}
View
33 net.c
@@ -1,14 +1,10 @@
#include "net.h"
-void net_deinit(void) {
+void net_finish(void) {
+ fsync(net_sock_client);
close(net_sock_client);
}
-unsigned int net_init(void) {
- atexit(net_deinit);
- return(1);
-}
-
void *net_get_addr(struct sockaddr *sa) {
if(sa->sa_family == AF_INET)
return &(((struct sockaddr_in*)sa)->sin_addr);
@@ -180,23 +176,20 @@ unsigned int net_connect(char *host, char *port) {
}
unsigned int net_begin(unsigned int mode, char *host, char *port) {
-
- if(mode == MODE_SERVER) {
- if(! net_listen(port))
- return(0);
- } else if(mode == MODE_CLIENT){
- if(! net_connect(host, port))
- return(0);
- }
- return(1);
+ if(mode != MODE_SERVER && mode != MODE_CLIENT) {
+ fprintf(stderr, "Error: net_begin called with invalid mode\n");
+ return(0);
+ }
+
+ if(mode == MODE_SERVER)
+ return(net_listen(port));
+
+ return(net_connect(host, port));
}
-unsigned int net_send(unsigned int mode, chtype *c) {
+unsigned int net_send(chtype *c) {
- if(net_sock_client == -1)
- abort();
-
if(send(net_sock_client, c, sizeof(char) * sizeof(chtype), MSG_DONTWAIT) == -1) {
endwin();
fprintf(stderr, "Error: send: %s\n", strerror(errno));
@@ -206,7 +199,7 @@ unsigned int net_send(unsigned int mode, chtype *c) {
return(1);
}
-unsigned int net_recv(unsigned int mode, chtype *c) {
+unsigned int net_recv(chtype *c) {
int rx = 0;
View
7 net.h
@@ -15,12 +15,11 @@ int net_sock_client;
#include <netdb.h>
#include <arpa/inet.h>
-void net_deinit(void);
-unsigned int net_init(void);
+void net_finish(void);
void *net_get_addr(struct sockaddr *sa);
unsigned int net_listen(char *port);
unsigned int net_connect(char *host, char *port);
unsigned int net_begin(unsigned int mode, char *host, char *port);
-unsigned int net_send(unsigned int mode, chtype *c);
-unsigned int net_recv(unsigned int mode, chtype *c);
+unsigned int net_send(chtype *c);
+unsigned int net_recv(chtype *c);
#endif

0 comments on commit b13d57d

Please sign in to comment.