judaism inc
This commit is contained in:
parent
828ab3f1c8
commit
b6312df730
11 changed files with 428 additions and 0 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,6 @@
|
|||
# Binaries
|
||||
bin/*
|
||||
|
||||
# Object files
|
||||
*.o
|
||||
*.ko
|
||||
|
|
2
debug
Executable file
2
debug
Executable file
|
@ -0,0 +1,2 @@
|
|||
gcc src/*.c -g -lncurses -o bin/mahou
|
||||
chmod +x bin/mahou
|
2
release
Executable file
2
release
Executable file
|
@ -0,0 +1,2 @@
|
|||
gcc src/*.c -lncurses -o bin/mahou
|
||||
chmod +x bin/mahou
|
139
src/client.c
Normal file
139
src/client.c
Normal file
|
@ -0,0 +1,139 @@
|
|||
#include "client.h"
|
||||
|
||||
#define BANNER_WIDTH 57
|
||||
#define BANNER_HEIGHT 6
|
||||
char BANNER[BANNER_HEIGHT][BANNER_WIDTH + 1] = {
|
||||
". : :::. :: .: ... ... :::",
|
||||
";;,. ;;; ;;`;; ,;; ;;, .;;;;;;;. ;; ;;;",
|
||||
"[[[[, ,[[[[, ,[[ '[[, ,[[[,,,[[[ ,[[ \\[[,[[' [[[",
|
||||
"$$$$$$$$\"$$$c$$$cc$$$c\"$$$\"\"\"$$$ $$$, $$$$$ $$$",
|
||||
"888 Y88\" 888o888 888,888 \"88o\"888,_ _,88P88 .d888",
|
||||
"MMM M' \"MMMYMM \"\"` MMM YMM \"YMMMMMP\" \"YmmMMMM\"\""
|
||||
};
|
||||
|
||||
#define MM_OPT_COUNT 3
|
||||
#define MM_MAX_OPT_LEN 11
|
||||
char MM_OPTIONS[MM_OPT_COUNT][MM_MAX_OPT_LEN + 1] = {
|
||||
"Join Game",
|
||||
"How to Play",
|
||||
"Quit"
|
||||
};
|
||||
#define MM_OPT_JOIN 0
|
||||
#define MM_OPT_HOW 1
|
||||
#define MM_OPT_QUIT 2
|
||||
|
||||
struct {
|
||||
socket_t *sock;
|
||||
} client_ctx;
|
||||
|
||||
static void hide_cursor();
|
||||
static int main_menu();
|
||||
static void how_to_play();
|
||||
|
||||
void client() {
|
||||
int selected;
|
||||
BOOL running = TRUE;
|
||||
|
||||
initscr();
|
||||
|
||||
start_color();
|
||||
keypad(stdscr, TRUE);
|
||||
|
||||
while(running == TRUE) {
|
||||
selected = main_menu();
|
||||
|
||||
switch(selected) {
|
||||
case MM_OPT_JOIN:
|
||||
|
||||
break;
|
||||
case MM_OPT_HOW:
|
||||
how_to_play();
|
||||
break;
|
||||
case MM_OPT_QUIT:
|
||||
running = FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
endwin();
|
||||
|
||||
/*char in[10];
|
||||
sock_recv(sock, in, 10);
|
||||
printf("got %s\r\n", in);
|
||||
|
||||
sock_send(sock, "hello", 6);
|
||||
printf("sent message");
|
||||
|
||||
sock_stop(sock);
|
||||
sock_free(sock);*/
|
||||
}
|
||||
|
||||
static void hide_cursor() {
|
||||
move(LINES - 1, COLS - 1);
|
||||
}
|
||||
|
||||
static int main_menu() {
|
||||
BOOL polling = TRUE;
|
||||
int i, j, selected = 0, color, ch;
|
||||
|
||||
clear();
|
||||
noecho();
|
||||
//raw();
|
||||
cbreak();
|
||||
|
||||
init_pair(1, COLOR_WHITE, COLOR_BLUE);
|
||||
init_pair(2, COLOR_BLACK, COLOR_CYAN);
|
||||
|
||||
attron(COLOR_PAIR(1) | A_BOLD);
|
||||
|
||||
for(i = 0; i < LINES; ++i)
|
||||
for(j = 0; j < COLS; ++j)
|
||||
addch(' ');
|
||||
|
||||
for(i = 0; i < BANNER_HEIGHT; ++i)
|
||||
mvprintw(3 + i, (COLS - BANNER_WIDTH) / 2, BANNER[i]);
|
||||
|
||||
attroff(COLOR_PAIR(1) | A_BOLD);
|
||||
|
||||
while(polling == TRUE) {
|
||||
for(i = 0; i < MM_OPT_COUNT; ++i) {
|
||||
color = selected == i ? 2 : 1;
|
||||
|
||||
attron(COLOR_PAIR(color));
|
||||
mvprintw(6 + BANNER_HEIGHT + i*2, (COLS - (MM_MAX_OPT_LEN + 2)) / 2,
|
||||
"* %s", MM_OPTIONS[i]);
|
||||
attroff(COLOR_PAIR(color));
|
||||
}
|
||||
|
||||
refresh();
|
||||
|
||||
hide_cursor();
|
||||
ch = getch();
|
||||
switch(ch) {
|
||||
case KEY_UP:
|
||||
selected = selected == 0 ? 0 : selected - 1;
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
selected = selected == MM_OPT_COUNT - 1 ? MM_OPT_COUNT - 1 : selected + 1;
|
||||
break;
|
||||
case KEY_LF:
|
||||
case KEY_ENTER:
|
||||
polling = FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return selected;
|
||||
}
|
||||
|
||||
void how_to_play() {
|
||||
WINDOW *win;
|
||||
win = newwin(10, 10, 4, 4);
|
||||
wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
|
||||
mvwprintw(win, 1, 1, "test");
|
||||
wrefresh(win);
|
||||
hide_cursor();
|
||||
getch();
|
||||
}
|
||||
|
||||
|
10
src/client.h
Normal file
10
src/client.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef CLIENT_H
|
||||
#define CLIENT_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ncurses.h>
|
||||
#include "sock.h"
|
||||
|
||||
void client();
|
||||
|
||||
#endif
|
10
src/common.h
Normal file
10
src/common.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#define FALSE 0
|
||||
#define TRUE 1
|
||||
#define BOOL char
|
||||
|
||||
#define KEY_LF 10
|
||||
|
||||
#endif
|
13
src/main.c
Normal file
13
src/main.c
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include <stdio.h>
|
||||
#include "common.h"
|
||||
#include "sock.h"
|
||||
#include "client.h"
|
||||
#include "server.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if(argc == 2 && strcmp(argv[1], "server") == 0) {
|
||||
server();
|
||||
} else {
|
||||
client();
|
||||
}
|
||||
}
|
20
src/server.c
Normal file
20
src/server.c
Normal file
|
@ -0,0 +1,20 @@
|
|||
#include "server.h"
|
||||
|
||||
void server() {
|
||||
socket_t *sock = sock_server_init("6770");
|
||||
sock_start(sock);
|
||||
|
||||
printf("awaiting connection...\r\n");
|
||||
socket_t *conn = sock_accept(sock);
|
||||
|
||||
printf("connected\r\n");
|
||||
sock_send(conn, "test", 5);
|
||||
printf("sent message\r\n");
|
||||
|
||||
char in[10];
|
||||
sock_recv(conn, in, 10);
|
||||
printf("got %s\r\n", in);
|
||||
|
||||
sock_stop(sock);
|
||||
sock_free(sock);
|
||||
}
|
9
src/server.h
Normal file
9
src/server.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#ifndef SERVER_H
|
||||
#define SERVER_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include "sock.h"
|
||||
|
||||
void server();
|
||||
|
||||
#endif
|
162
src/sock.c
Normal file
162
src/sock.c
Normal file
|
@ -0,0 +1,162 @@
|
|||
#include "sock.h"
|
||||
|
||||
static socket_t* sock_init() {
|
||||
return (socket_t*)malloc(sizeof(socket_t));
|
||||
}
|
||||
|
||||
socket_t* sock_client_init(char *addr, char *port) {
|
||||
socket_t* sock = sock_init();
|
||||
sock->type = SOCK_TYPE_CLIENT;
|
||||
sock->port = port;
|
||||
sock->addr = addr;
|
||||
}
|
||||
|
||||
socket_t* sock_server_init(char *port) {
|
||||
socket_t* sock = sock_init();
|
||||
sock->type = SOCK_TYPE_SERVER;
|
||||
sock->port = port;
|
||||
}
|
||||
|
||||
static int sock_server_start(socket_t *sock, struct addrinfo *hints) {
|
||||
struct addrinfo *result;
|
||||
if(getaddrinfo(NULL, sock->port, hints, &result) != 0)
|
||||
return SOCK_ERR_GETADDRINFO;
|
||||
|
||||
sock->socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
||||
if(sock->socket == NULL) {
|
||||
freeaddrinfo(result);
|
||||
return SOCK_ERR_SOCKET;
|
||||
}
|
||||
|
||||
if(bind(sock->socket, result->ai_addr, (int)result->ai_addrlen) < 0) {
|
||||
freeaddrinfo(result);
|
||||
close(sock->socket);
|
||||
return SOCK_ERR_BIND;
|
||||
}
|
||||
|
||||
freeaddrinfo(result);
|
||||
if(listen(sock->socket, SOMAXCONN) < 0) {
|
||||
close(sock->socket);
|
||||
return SOCK_ERR_LISTEN;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sock_client_start(socket_t *sock, struct addrinfo *hints) {
|
||||
struct addrinfo *results, *ptr;
|
||||
if(getaddrinfo(sock->addr, sock->port, hints, &results) != 0)
|
||||
return SOCK_ERR_GETADDRINFO;
|
||||
|
||||
for(ptr = results; ptr != NULL; ptr = ptr->ai_next) {
|
||||
if((sock->socket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol)) < 0) {
|
||||
freeaddrinfo(results);
|
||||
return SOCK_ERR_SOCKET;
|
||||
}
|
||||
|
||||
if(connect(sock->socket, ptr->ai_addr, (int)ptr->ai_addrlen) == 0)
|
||||
break;
|
||||
|
||||
close(sock->socket);
|
||||
sock->socket = -1;
|
||||
}
|
||||
|
||||
freeaddrinfo(results);
|
||||
if(sock->socket < 0)
|
||||
return SOCK_ERR_CONNECT;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sock_start(socket_t *sock) {
|
||||
struct addrinfo hints;
|
||||
bzero((char*)&hints, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_TCP;
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
|
||||
if(sock->type == SOCK_TYPE_CLIENT)
|
||||
return sock_client_start(sock, &hints);
|
||||
else
|
||||
return sock_server_start(sock, &hints);
|
||||
}
|
||||
|
||||
void sock_set_timeout(socket_t *sock, int secs, int msecs) {
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = secs;
|
||||
timeout.tv_usec = msecs * 1000;
|
||||
|
||||
setsockopt(sock->socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
|
||||
setsockopt(sock->socket, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));
|
||||
}
|
||||
|
||||
void sock_set_blocking(socket_t *sock) {
|
||||
int flags = fcntl(sock->socket, F_GETFL, 0);
|
||||
flags &= ~O_NONBLOCK;
|
||||
fcntl(sock->socket, F_SETFL, flags);
|
||||
}
|
||||
|
||||
void sock_set_nonblocking(socket_t *sock) {
|
||||
int flags = fcntl(sock->socket, F_GETFL, 0);
|
||||
flags |= O_NONBLOCK;
|
||||
fcntl(sock->socket, F_SETFL, flags);
|
||||
}
|
||||
|
||||
BOOL sock_is_blocking(socket_t *sock) {
|
||||
return (fcntl(sock->socket, F_GETFL, 0) & O_NONBLOCK) == 0;
|
||||
}
|
||||
|
||||
socket_t* sock_accept(socket_t *sock) {
|
||||
int hconn;
|
||||
struct sockaddr_in addr = {0};
|
||||
unsigned int addrlen = sizeof(addr);
|
||||
|
||||
hconn = accept(sock->socket, (struct sockaddr*)&addr, &addrlen);
|
||||
if(hconn < 0 && (errno == EWOULDBLOCK || errno == EAGAIN))
|
||||
return NULL;
|
||||
else if(hconn < 0)
|
||||
return -1;
|
||||
|
||||
socket_t *conn = sock_init();
|
||||
conn->type = SOCK_TYPE_SERVING;
|
||||
conn->socket = hconn;
|
||||
conn->addr_in = addr;
|
||||
conn->addrlen = addrlen;
|
||||
|
||||
return conn;
|
||||
}
|
||||
|
||||
int sock_recv(socket_t *sock, char *buffer, int length) {
|
||||
if(sock->type == SOCK_TYPE_SERVER)
|
||||
return -1;
|
||||
|
||||
int got = read(sock->socket, buffer, length);
|
||||
if(got < 0 && (errno == EWOULDBLOCK || errno == EAGAIN))
|
||||
return 0;
|
||||
else if(got <= 0)
|
||||
return -1;
|
||||
|
||||
return got;
|
||||
}
|
||||
|
||||
int sock_send(socket_t *sock, char *buffer, int length) {
|
||||
if(sock->type == SOCK_TYPE_SERVER)
|
||||
return -1;
|
||||
|
||||
int sent = write(sock->socket, buffer, length);
|
||||
if(sent < 0)
|
||||
return -1;
|
||||
|
||||
return sent;
|
||||
}
|
||||
|
||||
void sock_stop(socket_t *sock) {
|
||||
shutdown(sock->socket, SHUT_RDWR);
|
||||
close(sock->socket);
|
||||
}
|
||||
|
||||
void sock_free(socket_t *sock) {
|
||||
free(sock);
|
||||
}
|
||||
|
58
src/sock.h
Normal file
58
src/sock.h
Normal file
|
@ -0,0 +1,58 @@
|
|||
#ifndef SOCK_H
|
||||
#define SOCK_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <errno.h>
|
||||
#include "common.h"
|
||||
|
||||
#define SOCK_ERR_GETADDRINFO -1
|
||||
#define SOCK_ERR_SOCKET -2
|
||||
#define SOCK_ERR_BIND -3
|
||||
#define SOCK_ERR_LISTEN -4
|
||||
#define SOCK_ERR_CONNECT -5
|
||||
|
||||
// socket is client
|
||||
#define SOCK_TYPE_CLIENT 0
|
||||
// socket is server
|
||||
#define SOCK_TYPE_SERVER 1
|
||||
// socket is client connection on server's side
|
||||
#define SOCK_TYPE_SERVING 2
|
||||
|
||||
typedef struct socket_t socket_t;
|
||||
struct socket_t {
|
||||
int type;
|
||||
int socket;
|
||||
struct sockaddr_in addr_in;
|
||||
int addrlen;
|
||||
char *port;
|
||||
char *addr;
|
||||
};
|
||||
|
||||
socket_t* sock_client_init(char*, char*);
|
||||
socket_t* sock_server_init(char*);
|
||||
|
||||
int sock_start(socket_t*);
|
||||
|
||||
void sock_set_timeout(socket_t*, int, int);
|
||||
void sock_set_blocking(socket_t*);
|
||||
void sock_set_nonblocking(socket_t*);
|
||||
BOOL sock_is_blocking(socket_t*);
|
||||
|
||||
socket_t* sock_accept(socket_t*);
|
||||
int sock_recv(socket_t*, char*, int);
|
||||
int sock_send(socket_t*, char*, int);
|
||||
|
||||
void sock_stop(socket_t*);
|
||||
void sock_free(socket_t*);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in a new issue