aboutsummaryrefslogtreecommitdiff
path: root/server/main.ha
blob: abcb0fedb1230a4a5bd3a419b62ffbed50fd02da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use bufio;
use fmt;
use net;
use net::tcp;
use net::ip;
use io;
use os;
use fs;
use strings;
use time;
use time::date;
use time::chrono;
use unix::poll;
use drawing;
use drawing::{pos};
use packet_reader;


def PORT: u16 = 41460;

type conn = net::socket;

def CHUNKSIZE = 512;

export fn main() void = {
	// create 4 pictures. later we will have more pictures
	// but for now there are just 4, and they are at fixed positions
	const offs: [_]pos = [
		(0,0), (0,CHUNKSIZE), (CHUNKSIZE,0), (CHUNKSIZE,CHUNKSIZE),
	];

	let pictures: []drawing::picture = alloc([],len(offs));
	for (let i = 0z; i < len(offs); i +=1){
		append(pictures, drawing::picture {
			w = CHUNKSIZE,
			h = CHUNKSIZE,
			d = alloc([0...],CHUNKSIZE*CHUNKSIZE: size): *[*]u32,
			world_pos = offs[i],
		});
	};
	for (const p &.. pictures) drawing::clear_picture(p, 0xffffff);


	const listener = tcp::listen(ip::ANY_V4, PORT)!;
	loop(listener, pictures);
};

def save_interval = 5 * time::SECOND;

fn save_world(pictures: []drawing::picture) void = {
	fmt::printfln("saving world!")!;
	for (const pic &.. pictures) {
		const filename = fmt::asprintf("./c.{}.{}.ppm",pic.world_pos.0, pic.world_pos.1);
		fmt::printfln("\t-> {}",filename)!;
		defer free(filename);
		const mode = fs::mode::USER_RW | fs::mode::GROUP_RW;
		const file = os::create(filename, mode)!;
		defer {
			fmt::printfln("\t. {}",filename)!;
			io::close(file)!;
		};
		const header = fmt::asprintf("P6\n{} {}\n{}\n", pic.w, pic.h, 255);
		defer free(header);
		io::writeall(file, strings::toutf8(header))!;
		for (let i = 0z; i < pic.w * pic.h; i += 1) {
			const px = pic.d[i];
			io::writeall(file, [(px&0xff): u8, ((px>>8)&0xff): u8, ((px>>16)&0xff): u8])!;
		};
	};
		
};

fn loop(listener: net::socket, pictures: []drawing::picture) void = {
	// pollfds[0] is the listener
	// pollfds[n>0] corresponds to packet_readers[n-1]
	let pollfds: []poll::pollfd = alloc([ poll::pollfd {
		fd = listener, events = poll::event::POLLIN, revents = 0
	}]);
	let packet_readers: []packet_reader::packet_reader = [];


	let now = time::now(time::clock::MONOTONIC);
	let next = time::add(now, save_interval);

	for (true) {
		fmt::println("poll.")!;
		const timeout = time::diff(now, next);
		poll::poll(pollfds, timeout)!;

		now = time::now(time::clock::MONOTONIC);
		if (time::compare(now, next) >= 0) {
			save_world(pictures);
			next = time::add(now, save_interval);
		};

		if (0 != pollfds[0].revents & poll::event::POLLIN) {
			fmt::println("new conn")!;
			const new_conn = tcp::accept(pollfds[0].fd)!;
			fmt::println("a")!;
			append(pollfds, poll::pollfd {
				fd = new_conn,
				events = poll::event::POLLIN, revents = 0
			});
			append(packet_readers, packet_reader::new());
			fmt::printfln("there are now {},{} conns.",len(pollfds),len(packet_readers))!;
		};
		for (let i = 1z; i < len(pollfds); i += 1) {
			if (0 != pollfds[i].revents & poll::event::POLLIN) {
				match (packet_reader::read(&packet_readers[i-1], pollfds[i].fd)) {
				case void =>
					let j = 1;
					for (const packet_bytes => packet_reader::next(&packet_readers[i-1])) {
						fmt::printf("#{}:{} got {:2}",i,j,len(packet_bytes))!;
						j += 1;
						for (let byte .. packet_bytes) fmt::printf(" {:x}",byte)!;
						const op = drawing::deser_op(packet_bytes) as drawing::op_circle;
						drawing::perform(pictures, op);
						fmt::printfln("\t({:+6},{:+6})",op.0,op.1)!;
						for (let k = 1z; k < len(pollfds); k += 1) {
							if (k == i) continue;
							fmt::printfln("\t -> #{}",k)!;
							io::writeall(pollfds[k].fd, packet_bytes)!;
						};
					};
				case let err: io::error =>
					fmt::printfln("#{} error: {}",i,io::strerror(err))!;
					delete(pollfds[i]);
					delete(packet_readers[i-1]);
					i -= 1;
				case io::EOF =>
					fmt::printfln("#{} disconnect",i)!;
					delete(pollfds[i]);
					delete(packet_readers[i-1]);
					i -= 1;
				};
			};
		};
	};
};