the juciest squids you will ever see

This commit is contained in:
mallocnull 2017-11-09 17:06:23 -06:00
parent f1dd4943ae
commit e3171c9494
12 changed files with 159 additions and 72 deletions

BIN
core

Binary file not shown.

View file

@ -23,29 +23,72 @@ byte the bytes after the header octet bytes bytes:
---
client -> server
id 0: registration attempt
r.0 - username - string (max 9b)
r.1 - pin - ushort (max 9999)
id 0: clerical request
r.0 - clerical id - byte
id 1: login attempt
r.0 - username - string (max 9b)
r.1 - pin - ushort (max 9999)
id 1: alert prompt response
r.0 - clerical id - byte
r.1 - prompt id - byte
id 2: ctx change response
id 2: boolean prompt response
r.0 - clerical id - byte
r.1 - prompt id - byte
r.2 - response - bool
id 3: string prompt response
r.0 - clerical id - byte
r.1 - prompt id - byte
r.2 - response - string (256b)
id 4: list prompt response
r.0 - clerical id - byte
r.1 - prompt id - byte
r.2 - response - ushort
id 5: ctx change response
r.0 - ack - bool
r.1 - ctx id - ushort
---
server -> client
id 0: registration response
r.0 - succeeded - bool
r.1 - status - string (17b)
id 0: clerical response
r.0 - clerical id - byte
r.1 - success - bool
id 1: login response
r.0 - succeeded - bool
r.1 - user id - ushort
r.2 - status - string (17b)
id 1: alert prompt
r.0 - clerical id - byte
r.1 - prompt id - byte
r.2 - prompt width - ushort
r.3 - text - string (512b)
id 2: ctx change request
id 2: boolean prompt
r.0 - clerical id - byte
r.1 - prompt id - byte
r.2 - prompt width - ushort
r.3 - text - string (512b)
id 3: string prompt
r.0 - clerical id - byte
r.1 - prompt id - byte
r.2 - prompt width - ushort
r.3 - text - string (512b)
r.4 - strlen - byte
id 4: list prompt
r.0 - clerical id - byte
r.1 - prompt id - byte
r.2 - prompt width - ushort
r.3 - text - string (512b)
r.4 - option count - n - ushort
r.x | 4 < x <= 4+n
- option text - string (256b)
id 5: ctx change request
r.0 - ctx id - ushort
---
CLERICAL IDs
id 0: register
id 1: login

View file

@ -116,8 +116,8 @@ static int main_menu() {
#endif
attron(color);
mvprintw(AFTER_BANNER + 3 + i*2, (COLS - (MM_MAX_OPT_LEN + 2)) / 2,
"* %s", MM_OPTIONS[i]);
mvprintw(AFTER_BANNER + 3 + i*2, (COLS - (MM_MAX_OPT_LEN + 3)) / 2,
"%s %s", selected == i ? "->" : " *", MM_OPTIONS[i]);
attroff(color);
}
@ -155,8 +155,20 @@ void how_to_play() {
void create_account() {
int len;
packet_t *pck = packet_init_out(0, 0);
packet_append_uint8(pck, PCK_CLERIC_REG);
uint8_t *out = packet_get_raw(pck, &len);
sock_send(ctx.sock, out, len);
packet_free(pck);
char input[9];
scr_prompt_string(20, "this prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test", input, 8);
//scr_prompt_string(20, "this prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test of a prompt box is a test", input, 8);
scr_prompt_voptions(40, "test of a prompt box of a prompt box of a prompt box of a prompt\n \nbox of a prompt box of a prompt box\n \nthis is a test of a prompt box", 3,
"testtesttesttesttesttesttest a", "test b", "test c");
/*int a = 0;
while(a != KEY_LF)

View file

@ -5,6 +5,7 @@
#include <locale.h>
#include <ncurses.h>
#include "sock.h"
#include "packet.h"
#include "screen.h"
#include "ui.h"

View file

@ -73,28 +73,28 @@ WINDOW* scr_win_box(int top, int left, int width, int height) {
}
static int calculate_height(int width, char *text) {
int len = strlen(text), lines = 0, line_width, line_len, char_width, i;
int len = strlen(text), lines = 0, char_width = 0,
line_len, line_width, i;
char *ptr, *cpy = (char*)malloc(len * sizeof(char));
strcpy(cpy, text);
ptr = strtok(cpy, "\n");
while(ptr != NULL) {
line_width = 0;
line_len = strlen(ptr);
for(i = 0; i < line_len; ++i) {
if(ptr[i] & 0x80 == 0 && isprint(ptr[i])) {
for(i = 0; ptr[i] != '\0'; ++i) {
if((ptr[i] & 0x80) == 0 && isprint(ptr[i])) {
char_width = 0;
++line_width;
} else if(ptr[i] & 0xE0 == 0xC0) {
} else if((ptr[i] & 0xE0) == 0xC0) {
char_width = 1;
++line_width;
} else if(ptr[i] & 0xF0 == 0xE0) {
} else if((ptr[i] & 0xF0) == 0xE0) {
char_width = 2;
++line_width;
} else if(ptr[i] & 0xF8 == 0xF0) {
} else if((ptr[i] & 0xF8) == 0xF0) {
char_width = 3;
++line_width;
} else if(ptr[i] & 0xC0 == 0x80) {
} else if((ptr[i] & 0xC0) == 0x80) {
if(char_width > 0)
--char_width;
else
@ -102,11 +102,11 @@ static int calculate_height(int width, char *text) {
}
}
lines += MAX(1, (int)ceil((float)line_width / width));
ptr = strtok(NULL, "\n");
}
free(cpy);
return lines;
}
@ -117,7 +117,7 @@ void scr_alert(int width, char *text) {
short pair;
WINDOW *win;
int text_height = calculate_height(width, strlen(text));
int text_height = calculate_height(width, text);
#ifdef WORK
pair = SCR_PAIR(SCR_WHITE, SCR_BLACK);
@ -158,7 +158,7 @@ BOOL scr_prompt(int width, char *text) {
char check[] = "[ \u2714 ]";
char cross[] = "[ \u2718 ]";
WINDOW *win;
int text_height = calculate_height(width, strlen(text));
int text_height = calculate_height(width, text);
#ifdef WORK
pair = SCR_PAIR(SCR_WHITE, SCR_BLACK);
@ -230,7 +230,7 @@ void scr_prompt_string(int width, char *text, char *out, int outlen) {
int ch, curlen = 0, i, origin;
short pair;
WINDOW *win;
int text_height = calculate_height(width, strlen(text));
int text_height = calculate_height(width, text);
#ifdef WORK
pair = SCR_PAIR(SCR_WHITE, SCR_BLACK);
@ -268,10 +268,10 @@ void scr_prompt_string(int width, char *text, char *out, int outlen) {
wattroff(win, A_UNDERLINE);
wattroff(win, pair);
//if(curlen < outlen)
if(curlen < outlen)
wmove(win, text_height + 1, origin + 3 + curlen);
//else
//scr_hide_cursor();
else
scr_hide_cursor();
wrefresh(win);
ch = getch();
@ -298,7 +298,7 @@ int scr_prompt_options(int width, char *text, char **options, int optcount) {
short pair;
BOOL loop = TRUE;
WINDOW *win;
int text_height = calculate_height(width, strlen(text));
int text_height = calculate_height(width, text);
#ifdef WORK
pair = SCR_PAIR(SCR_WHITE, SCR_BLACK);
@ -320,31 +320,23 @@ int scr_prompt_options(int width, char *text, char **options, int optcount) {
wattroff(win, SCR_PAIR(SCR_WHITE, SCR_BLACK));
for(i = 0; i < optcount; ++i)
maxlen = MAX(maxlen, strlen(options[i]) + 3);
maxlen = MIN(width, MAX(maxlen, strlen(options[i]) + 3));
origin = (width - maxlen) / 2;
while(loop) {
origin = (width - (outlen + 3)) / 2;
wmove(win, text_height + 1, origin);
for(i = 0; i < optcount; ++i) {
pair = i == selected ? SCR_PAIR(SCR_BLACK, SCR_CYAN)
: SCR_PAIR(SCR_WHITE, SCR_BLACK);
}
wattron(win, pair);
wprintw(win, "-> ");
wattron(win, A_UNDERLINE);
wprintw(win, out);
for(i = curlen; i < outlen; ++i)
waddch(win, ' ');
wattroff(win, A_UNDERLINE);
wattroff(win, pair);
wmove(win, text_height + i + 1, origin);
wrefresh(win);
wattron(win, pair);
wprintw(win, "%s %s", i == selected ? "->" : " *", options[i]);
wattroff(win, pair);
}
wrefresh(win);
scr_hide_cursor();
ch = getch();
switch(ch) {
@ -364,3 +356,18 @@ int scr_prompt_options(int width, char *text, char **options, int optcount) {
delwin(win);
return selected;
}
int scr_prompt_voptions(int width, char *text, int optcount, ...) {
int retval, i;
char **options = (char**)malloc(optcount * sizeof(char*));
va_list args;
va_start(args, optcount);
for(i = 0; i < optcount; ++i)
options[i] = va_arg(args, char*);
va_end(args);
retval = scr_prompt_options(width, text, options, optcount);
free(options);
return retval;
}

View file

@ -3,6 +3,7 @@
#include <ncurses.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
@ -36,5 +37,6 @@ void scr_alert(int, char*);
BOOL scr_prompt(int, char*);
void scr_prompt_string(int, char*, char*, int);
int scr_prompt_options(int, char*, char**, int);
int scr_prompt_voptions(int, char*, int, ...);
#endif

View file

@ -4,14 +4,20 @@
#include "server/server.h"
int main(int argc, char **argv) {
packet_context_init(2);
packet_context_init(5);
packet_context_register(PCK_CTX_C2S, PCK_ID_REGISTER, 0, 2, 9, 2);
packet_context_register(PCK_CTX_C2S, PCK_ID_LOGIN, 0, 2, 9, 2);
packet_context_register(PCK_CTX_C2S, PCK_ID_CLERIC_REQ, 0, 1, 1);
packet_context_register(PCK_CTX_C2S, PCK_ID_ALERT, 0, 2, 1, 1);
packet_context_register(PCK_CTX_C2S, PCK_ID_PROMPT_BOOL, 0, 3, 1, 1, 1);
packet_context_register(PCK_CTX_C2S, PCK_ID_PROMPT_STR, 0, 3, 1, 1, 256);
packet_context_register(PCK_CTX_C2S, PCK_ID_PROMPT_LIST, 0, 3, 1, 1, 2);
packet_context_register(PCK_CTX_C2S, PCK_ID_CHANGE_CTX, 0, 2, 1, 2);
packet_context_register(PCK_CTX_S2C, PCK_ID_REGISTER, 0, 2, 1, 17);
packet_context_register(PCK_CTX_S2C, PCK_ID_LOGIN, 0, 3, 1, 2, 17);
packet_context_register(PCK_CTX_S2C, PCK_ID_CLERIC_REQ, 0, 2, 1, 1);
packet_context_register(PCK_CTX_S2C, PCK_ID_ALERT, 0, 4, 1, 1, 2, 512);
packet_context_register(PCK_CTX_S2C, PCK_ID_PROMPT_BOOL, 0, 4, 1, 1, 2, 512);
packet_context_register(PCK_CTX_S2C, PCK_ID_PROMPT_STR, 0, 5, 1, 1, 2, 512, 1);
packet_context_register(PCK_CTX_S2C, PCK_ID_PROMPT_LIST, 0, 6, 1, 1, 2, 512, 1, 256);
packet_context_register(PCK_CTX_S2C, PCK_ID_CHANGE_CTX, 0, 1, 2);
if(argc == 2 && strcmp(argv[1], "server") == 0) {

View file

@ -116,7 +116,7 @@ packet_t* packet_init_out(uint8_t id, uint16_t iterations) {
return NULL;
packet_ctx_t *out = &(ctx.out[id]);
int regions = out->region_count;
uint32_t regions = out->region_count, length;
if(out->iter_start != 0)
regions = out->iter_start + (iterations * out->iter_count);
@ -128,9 +128,15 @@ packet_t* packet_init_out(uint8_t id, uint16_t iterations) {
packet->region_count = regions;
packet->region_lengths = out->region_lengths;
packet->length = out->pre_iter_length + out->iter_length * iterations;
packet->raw = (uint8_t*)malloc((7 * packet->length) * sizeof(uint8_t));
packet->raw = (uint8_t*)malloc((7 + packet->length) * sizeof(uint8_t));
packet_fill_regions(packet, out);
packet->raw[0] = 0xDE;
packet->raw[1] = 0xAD;
packet->raw[2] = id;
length = htonl(packet->length);
memcpy(packet->raw + 3, &length, 4);
return packet;
}

View file

@ -14,9 +14,15 @@
#define PCK_IN 0
#define PCK_OUT 1
#define PCK_ID_REGISTER 0
#define PCK_ID_LOGIN 1
#define PCK_ID_CHANGE_CTX 2
#define PCK_CLERIC_REG 0
#define PCK_CLERIC_LOGIN 1
#define PCK_ID_CLERIC_REQ 0
#define PCK_ID_ALERT 1
#define PCK_ID_PROMPT_BOOL 2
#define PCK_ID_PROMPT_STR 3
#define PCK_ID_PROMPT_LIST 4
#define PCK_ID_CHANGE_CTX 5
typedef struct packet_t packet_t;
struct packet_t {

View file

@ -5,6 +5,7 @@
#include "context.h"
#include "user.h"
#include "sock.h"
#include "packet.h"
#include "queue.h"
#include "flimit.h"

View file

@ -82,17 +82,17 @@ int sock_start(socket_t *sock) {
return sock_server_start(sock, &hints);
}
void sock_set_timeout(socket_t *sock, int secs, int msecs) {
sock_set_timeout_us(sock, secs, msecs * 1000);
void sock_set_timeout(socket_t *sock, uint8_t dir, int secs, int msecs) {
sock_set_timeout_us(sock, dir, secs, msecs * 1000);
}
void sock_set_timeout_us(socket_t *sock, int secs, int usecs) {
void sock_set_timeout_us(socket_t *sock, uint8_t dir, int secs, int usecs) {
struct timeval timeout;
timeout.tv_sec = secs;
timeout.tv_usec = usecs;
setsockopt(sock->socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
setsockopt(sock->socket, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));
setsockopt(sock->socket, SOL_SOCKET, dir == SOCK_DIR_SEND ? SO_SNDTIMEO : SO_RCVTIMEO,
(char*)&timeout, sizeof(timeout));
}
void sock_set_blocking(socket_t *sock) {

View file

@ -28,6 +28,9 @@
// socket is client connection on server's side
#define SOCK_TYPE_SERVING 2
#define SOCK_DIR_SEND 0
#define SOCK_DIR_RECV 1
typedef struct socket_t socket_t;
struct socket_t {
int type;
@ -44,9 +47,9 @@ socket_t* sock_server_init(char*);
int sock_start(socket_t*);
// second int is millisecs
void sock_set_timeout(socket_t*, int, int);
void sock_set_timeout(socket_t*, uint8_t, int, int);
// second int is microsecs
void sock_set_timeout_us(socket_t*, int, int);
void sock_set_timeout_us(socket_t*, uint8_t, int, int);
void sock_set_blocking(socket_t*);
void sock_set_nonblocking(socket_t*);
BOOL sock_is_blocking(socket_t*);