aboutsummaryrefslogtreecommitdiff
path: root/server/main.ha
blob: ae7d9eb108575cbe51e67854545c0e3f14553bd2 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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 = match(tcp::listen(ip::ANY_V4, PORT)) {
	case let err: net::error =>
		fmt::fatalf(net::strerror(err));
	case let sock: net::socket =>
		yield sock;
	};

	loop(listener, pictures);
};

def save_interval = 20 * 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))!;

		let buf: []u8 = alloc([0...],3*pic.w*pic.h);
		defer free(buf);
		for (let i = 0z; i < pic.w * pic.h; i += 1) {
			const px = pic.d[i];
			buf[3*i  ] = ((px>>16)&0xff): u8;
			buf[3*i+1] = ((px>>8) &0xff): u8;
			buf[3*i+2] = ((px)    &0xff): u8;
		};
		io::writeall(file, buf)!;
	};
		
};

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);
	save_world(pictures);

	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))!;
			send_world(new_conn, pictures)!;
		};
		for (let connidx = 1z; connidx < len(pollfds); connidx += 1) {
			if (0 != pollfds[connidx].revents & poll::event::POLLIN) {
				match (packet_reader::read(&packet_readers[connidx-1], pollfds[connidx].fd)) {
				case void =>
					for (const packet => packet_reader::next(&packet_readers[connidx-1])!) {
						handle_packet(pictures, pollfds, packet, connidx);
					};
				case let err: io::error =>
					fmt::printfln("#{} error: {}",connidx,io::strerror(err))!;
					delete(pollfds[connidx]);
					delete(packet_readers[connidx-1]);
					connidx -= 1;
				case io::EOF =>
					fmt::printfln("#{} disconnect",connidx)!;
					delete(pollfds[connidx]);
					delete(packet_readers[connidx-1]);
					connidx -= 1;
				};
			};
		};
	};
};

// ehh, really need a struct to put pollfds, pictures, connections... in
fn handle_packet(pictures: []drawing::picture, pollfds: []poll::pollfd, packet: packet_reader::packet, connidx: size) void = {
	match (packet) {
	case let op: packet_reader::packet_drawop =>
		const opc = op as drawing::op_circle;
		fmt::printfln("#{}: drawop  ({:+6},{:+6})",connidx,opc.0,opc.1)!;
		drawing::perform(pictures, opc);
		for (let otheridx = 1z; otheridx < len(pollfds); otheridx += 1) {
			if (otheridx == connidx) continue;
			fmt::printfln("\t -> #{}",otheridx)!;
			// ehhh shouldn't reserialize for each other thing
			packet_reader::send(pollfds[otheridx].fd, packet)!;
		};
	case =>
		abort("other packets not supported yet");
	};
};

fn send_world(conn: io::file, pictures: []drawing::picture) (void | io::error) = {
	for (const pic &.. pictures) {
		packet_reader::send(conn, packet_reader::packet_sendchunk {
			world_pos = pic.world_pos,
			chunk_data = pic.d[..pic.w*pic.h],
		})?;
	};

};