diff options
| author | Ginger Bill <bill@gingerbill.org> | 2016-09-22 21:32:25 +0100 |
|---|---|---|
| committer | Ginger Bill <bill@gingerbill.org> | 2016-09-22 21:32:25 +0100 |
| commit | 2e506b7e6ebf527f94ac5faaecb54771ed137c48 (patch) | |
| tree | 297009da3366df1d098ed54ffdb4ebbea3e2b1df /code | |
| parent | 6907951f1eebcc9053ccfbe32a30dc704e790747 (diff) | |
Demo 003
Diffstat (limited to 'code')
| -rw-r--r-- | code/demo.odin | 196 | ||||
| -rw-r--r-- | code/game.odin | 2 | ||||
| -rw-r--r-- | code/http_test.odin | 181 | ||||
| -rw-r--r-- | code/punity.odin | 4 |
4 files changed, 199 insertions, 184 deletions
diff --git a/code/demo.odin b/code/demo.odin index 4311ec213..1fbce19c5 100644 --- a/code/demo.odin +++ b/code/demo.odin @@ -1,5 +1,8 @@ #import "fmt.odin" #import "os.odin" +// #import "http_test.odin" as ht +// #import "game.odin" as game +// #import "punity.odin" as pn @@ -11,6 +14,15 @@ main :: proc() { // crazy_introspection() // namespaces_and_files() // miscellany() + + // ht.run() + // game.run() + // { + // init :: proc(c: ^pn.Core) {} + // step :: proc(c: ^pn.Core) {} + + // pn.run(init, step) + // } } struct_padding :: proc() { @@ -260,6 +272,8 @@ crazy_introspection :: proc() { fmt.printf("\t%\t= %,\n", info.names[i], info.values[i]) } fmt.printf("}\n") + + // NOTE(bill): look at that type-safe printf! } { @@ -276,6 +290,7 @@ crazy_introspection :: proc() { // n.b. This pretty much "solves" serialization (to strings) } +// #import "test.odin" namespaces_and_files :: proc() { /* @@ -317,184 +332,3 @@ miscellany :: proc() { - -// #import "fmt.odin" as fmt - -// #foreign_system_library "Ws2_32" - - -// SOCKET :: type uint -// INVALID_SOCKET :: ~(0 as SOCKET) - -// AF :: enum i32 { -// UNSPEC = 0, // unspecified -// UNIX = 1, // local to host (pipes, portals) -// INET = 2, // internetwork: UDP, TCP, etc. -// IMPLINK = 3, // arpanet imp addresses -// PUP = 4, // pup protocols: e.g. BSP -// CHAOS = 5, // mit CHAOS protocols -// NS = 6, // XEROX NS protocols -// ISO = 7, // ISO protocols -// OSI = ISO, // OSI is ISO -// ECMA = 8, // european computer manufacturers -// DATAKIT = 9, // datakit protocols -// CCITT = 10, // CCITT protocols, X.25 etc -// SNA = 11, // IBM SNA -// DECnet = 12, // DECnet -// DLI = 13, // Direct data link interface -// LAT = 14, // LAT -// HYLINK = 15, // NSC Hyperchannel -// APPLETALK = 16, // AppleTalk -// ROUTE = 17, // Internal Routing Protocol -// LINK = 18, // Link layer interface -// XTP = 19, // eXpress Transfer Protocol (no AF) -// COIP = 20, // connection-oriented IP, aka ST II -// CNT = 21, // Computer Network Technology -// RTIP = 22, // Help Identify RTIP packets -// IPX = 23, // Novell Internet Protocol -// SIP = 24, // Simple Internet Protocol -// PIP = 25, // Help Identify PIP packets -// MAX = 26, -// } - -// SOCK_STREAM :: 1 -// SOCKET_ERROR :: -1 -// IPPROTO_TCP :: 6 -// AI_PASSIVE :: 0x0020 -// SOMAXCONN :: 128 - -// SD_RECEIVE :: 0 -// SD_SEND :: 1 -// SD_BOTH :: 2 - -// WSADESCRIPTION_LEN :: 256 -// WSASYS_STATUS_LEN :: 128 -// WSADATA :: struct #ordered { -// version: i16 -// high_version: i16 - - -// // NOTE(bill): This is x64 ordering -// max_sockets: u16 -// max_udp_dg: u16 -// vendor_info: ^byte -// description: [WSADESCRIPTION_LEN+1]byte -// system_status: [WSASYS_STATUS_LEN+1]byte -// } - -// addrinfo :: struct #ordered { -// flags: i32 -// family: i32 -// socktype: i32 -// protocol: i32 -// addrlen: uint -// canonname: ^u8 -// addr: ^sockaddr -// next: ^addrinfo -// } - -// sockaddr :: struct #ordered { -// family: u16 -// data: [14]byte -// } - - -// WSAStartup :: proc(version_requested: i16, data: ^WSADATA) -> i32 #foreign #dll_import -// WSACleanup :: proc() -> i32 #foreign #dll_import -// getaddrinfo :: proc(node_name, service_name: ^u8, hints: ^addrinfo, result: ^^addrinfo) -> i32 #foreign #dll_import -// freeaddrinfo :: proc(ai: ^addrinfo) #foreign #dll_import -// socket :: proc(af, type_, protocol: i32) -> SOCKET #foreign #dll_import -// closesocket :: proc(s: SOCKET) -> i32 #foreign #dll_import -// bind :: proc(s: SOCKET, name: ^sockaddr, name_len: i32) -> i32 #foreign #dll_import -// listen :: proc(s: SOCKET, back_log: i32) -> i32 #foreign #dll_import -// accept :: proc(s: SOCKET, addr: ^sockaddr, addr_len: i32) -> SOCKET #foreign #dll_import -// recv :: proc(s: SOCKET, buf: ^byte, len: i32, flags: i32) -> i32 #foreign #dll_import -// send :: proc(s: SOCKET, buf: ^byte, len: i32, flags: i32) -> i32 #foreign #dll_import -// shutdown :: proc(s: SOCKET, how: i32) -> i32 #foreign #dll_import -// WSAGetLastError :: proc() -> i32 #foreign #dll_import - -// to_c_string :: proc(s: string) -> ^byte { -// c_str := new_slice(byte, s.count+1) -// assert(c_str.data != null) -// copy(c_str, s as []byte) -// c_str[s.count] = 0 -// return c_str.data -// } - -// main :: proc() { -// wsa: WSADATA -// res: ^addrinfo = null -// hints: addrinfo -// s, client: SOCKET - -// if WSAStartup(2 | (2 << 8), ^wsa) != 0 { -// fmt.println("WSAStartup failed: ", WSAGetLastError()) -// return -// } -// defer WSACleanup() - -// hints.family = AF.INET as i32 -// hints.socktype = SOCK_STREAM -// hints.protocol = IPPROTO_TCP -// hints.flags = AI_PASSIVE - -// if getaddrinfo(null, to_c_string("8080"), ^hints, ^res) != 0 { -// fmt.println("getaddrinfo failed: ", WSAGetLastError()) -// return -// } -// defer freeaddrinfo(res) - -// s = socket(res.family, res.socktype, res.protocol) -// if s == INVALID_SOCKET { -// fmt.println("socket failed: ", WSAGetLastError()) -// return -// } -// defer closesocket(s) - -// bind(s, res.addr, res.addrlen as i32) -// listen(s, SOMAXCONN) - -// client = accept(s, null, null) -// if client == INVALID_SOCKET { -// fmt.println("socket failed: ", WSAGetLastError()) -// return -// } -// defer closesocket(client) - -// html := -// `HTTP/1.1 200 OK -// Connection: close -// Content-type: text/html - -// <html> -// <head> -// <title>Demo Title</title> -// </head> -// <body> -// <h1 style="color: orange;">Odin Server Demo</h1> -// </body> -// </html> -// ` - -// buf: [1024]byte -// for { -// bytes := recv(client, ^buf[0], buf.count as i32, 0) -// if bytes > 0 { -// // fmt.println(buf[:bytes] as string) -// bytes_sent := send(client, html.data, (html.count-1) as i32, 0) -// if bytes_sent == SOCKET_ERROR { -// fmt.println("send failed: ", WSAGetLastError()) -// return -// } -// break -// } else if bytes == 0 { -// fmt.println("Connection closing...") -// break -// } else { -// fmt.println("recv failed: ", WSAGetLastError()) -// return -// } -// } - -// shutdown(client, SD_SEND) -// } diff --git a/code/game.odin b/code/game.odin index 123f89982..df3473128 100644 --- a/code/game.odin +++ b/code/game.odin @@ -134,7 +134,6 @@ run :: proc() { return DefWindowProcA(hwnd, msg, wparam, lparam) } - window, window_success := make_window("Odin Language Demo", 854, 480, win32_proc) if !window_success { return @@ -142,6 +141,7 @@ run :: proc() { defer destroy_window(^window) + prev_time := time_now() running := true diff --git a/code/http_test.odin b/code/http_test.odin new file mode 100644 index 000000000..e39273834 --- /dev/null +++ b/code/http_test.odin @@ -0,0 +1,181 @@ + +#import "fmt.odin" as fmt + +#foreign_system_library "Ws2_32" + + +SOCKET :: type uint +INVALID_SOCKET :: ~(0 as SOCKET) + +AF :: enum i32 { + UNSPEC = 0, // unspecified + UNIX = 1, // local to host (pipes, portals) + INET = 2, // internetwork: UDP, TCP, etc. + IMPLINK = 3, // arpanet imp addresses + PUP = 4, // pup protocols: e.g. BSP + CHAOS = 5, // mit CHAOS protocols + NS = 6, // XEROX NS protocols + ISO = 7, // ISO protocols + OSI = ISO, // OSI is ISO + ECMA = 8, // european computer manufacturers + DATAKIT = 9, // datakit protocols + CCITT = 10, // CCITT protocols, X.25 etc + SNA = 11, // IBM SNA + DECnet = 12, // DECnet + DLI = 13, // Direct data link interface + LAT = 14, // LAT + HYLINK = 15, // NSC Hyperchannel + APPLETALK = 16, // AppleTalk + ROUTE = 17, // Internal Routing Protocol + LINK = 18, // Link layer interface + XTP = 19, // eXpress Transfer Protocol (no AF) + COIP = 20, // connection-oriented IP, aka ST II + CNT = 21, // Computer Network Technology + RTIP = 22, // Help Identify RTIP packets + IPX = 23, // Novell Internet Protocol + SIP = 24, // Simple Internet Protocol + PIP = 25, // Help Identify PIP packets + MAX = 26, +} + +SOCK_STREAM :: 1 +SOCKET_ERROR :: -1 +IPPROTO_TCP :: 6 +AI_PASSIVE :: 0x0020 +SOMAXCONN :: 128 + +SD_RECEIVE :: 0 +SD_SEND :: 1 +SD_BOTH :: 2 + +WSADESCRIPTION_LEN :: 256 +WSASYS_STATUS_LEN :: 128 +WSADATA :: struct #ordered { + version: i16 + high_version: i16 + + +// NOTE(bill): This is x64 ordering + max_sockets: u16 + max_udp_dg: u16 + vendor_info: ^byte + description: [WSADESCRIPTION_LEN+1]byte + system_status: [WSASYS_STATUS_LEN+1]byte +} + +addrinfo :: struct #ordered { + flags: i32 + family: i32 + socktype: i32 + protocol: i32 + addrlen: uint + canonname: ^u8 + addr: ^sockaddr + next: ^addrinfo +} + +sockaddr :: struct #ordered { + family: u16 + data: [14]byte +} + + +WSAStartup :: proc(version_requested: i16, data: ^WSADATA) -> i32 #foreign #dll_import +WSACleanup :: proc() -> i32 #foreign #dll_import +getaddrinfo :: proc(node_name, service_name: ^u8, hints: ^addrinfo, result: ^^addrinfo) -> i32 #foreign #dll_import +freeaddrinfo :: proc(ai: ^addrinfo) #foreign #dll_import +socket :: proc(af, type_, protocol: i32) -> SOCKET #foreign #dll_import +closesocket :: proc(s: SOCKET) -> i32 #foreign #dll_import +bind :: proc(s: SOCKET, name: ^sockaddr, name_len: i32) -> i32 #foreign #dll_import +listen :: proc(s: SOCKET, back_log: i32) -> i32 #foreign #dll_import +accept :: proc(s: SOCKET, addr: ^sockaddr, addr_len: i32) -> SOCKET #foreign #dll_import +recv :: proc(s: SOCKET, buf: ^byte, len: i32, flags: i32) -> i32 #foreign #dll_import +send :: proc(s: SOCKET, buf: ^byte, len: i32, flags: i32) -> i32 #foreign #dll_import +shutdown :: proc(s: SOCKET, how: i32) -> i32 #foreign #dll_import +WSAGetLastError :: proc() -> i32 #foreign #dll_import + +to_c_string :: proc(s: string) -> ^byte { + c_str := new_slice(byte, s.count+1) + assert(c_str.data != null) + copy(c_str, s as []byte) + c_str[s.count] = 0 + return c_str.data +} + +run :: proc() { + wsa: WSADATA + res: ^addrinfo = null + hints: addrinfo + s, client: SOCKET + + if WSAStartup(2 | (2 << 8), ^wsa) != 0 { + fmt.println("WSAStartup failed: ", WSAGetLastError()) + return + } + defer WSACleanup() + + hints.family = AF.INET as i32 + hints.socktype = SOCK_STREAM + hints.protocol = IPPROTO_TCP + hints.flags = AI_PASSIVE + + if getaddrinfo(null, to_c_string("8080"), ^hints, ^res) != 0 { + fmt.println("getaddrinfo failed: ", WSAGetLastError()) + return + } + defer freeaddrinfo(res) + + s = socket(res.family, res.socktype, res.protocol) + if s == INVALID_SOCKET { + fmt.println("socket failed: ", WSAGetLastError()) + return + } + defer closesocket(s) + + bind(s, res.addr, res.addrlen as i32) + listen(s, SOMAXCONN) + + client = accept(s, null, null) + if client == INVALID_SOCKET { + fmt.println("socket failed: ", WSAGetLastError()) + return + } + defer closesocket(client) + + html := +`HTTP/1.1 200 OK +Connection: close +Content-type: text/html + +<html> +<head> + <title>Demo Title</title> +</head> +<body> + <h1 style="color: orange;">Odin Server Demo</h1> +</body> +</html> +` + + buf: [1024]byte + for { + bytes := recv(client, ^buf[0], buf.count as i32, 0) + if bytes > 0 { + // fmt.println(buf[:bytes] as string) + bytes_sent := send(client, html.data, (html.count-1) as i32, 0) + if bytes_sent == SOCKET_ERROR { + fmt.println("send failed: ", WSAGetLastError()) + return + } + break + } else if bytes == 0 { + fmt.println("Connection closing...") + break + } else { + fmt.println("recv failed: ", WSAGetLastError()) + return + } + } + + shutdown(client, SD_SEND) +} diff --git a/code/punity.odin b/code/punity.odin index de29555fd..69ab1dff8 100644 --- a/code/punity.odin +++ b/code/punity.odin @@ -434,7 +434,7 @@ run :: proc(user_init, user_step: proc(c: ^Core)) { { data: [128]byte buf := data[:0] - fmt.printf_to_buffer(^buf, "Punity: % ms\x00", dt*1000) + fmt.bprintf(^buf, "Punity: % ms\x00", dt*1000) win32.SetWindowTextA(win32_window, buf.data) } @@ -447,7 +447,7 @@ run :: proc(user_init, user_step: proc(c: ^Core)) { } } - memory_zero(^_core.key_deltas[0], size_of(_core.key_deltas[0])) + memory_zero(^_core.key_deltas[0], size_of_val(_core.key_deltas[0])) for PeekMessageA(^message, null, 0, 0, PM_REMOVE) != 0 { |