sdl_net.odin 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package sdl2_net
  2. import "core:c"
  3. import SDL ".."
  4. when ODIN_OS == "windows" { foreign import lib "SDL2_net.lib" }
  5. when ODIN_OS == "linux" { foreign import lib "system:SDL2_net" }
  6. when ODIN_OS == "darwin" { foreign import lib "system:SDL2_net" }
  7. when ODIN_OS == "freebsd" { foreign import lib "system:SDL2_net" }
  8. bool :: SDL.bool
  9. MAJOR_VERSION :: 2
  10. MINOR_VERSION :: 0
  11. PATCHLEVEL :: 1
  12. IPaddress :: struct {
  13. host: u32, /* 32-bit IPv4 host address */
  14. port: u16, /* 16-bit protocol port */
  15. }
  16. INADDR_ANY :: 0x00000000
  17. INADDR_NONE :: 0xFFFFFFFF
  18. INADDR_LOOPBACK :: 0x7f000001
  19. INADDR_BROADCAST :: 0xFFFFFFFF
  20. @(default_calling_convention="c", link_prefix="SDLNet_")
  21. foreign lib {
  22. Linked_Version :: proc() -> ^SDL.version ---
  23. Init :: proc() -> c.int ---
  24. Quit :: proc() ---
  25. ResolveHost :: proc(address: ^IPaddress, host: cstring, port: u16) -> c.int ---
  26. ResolveIP :: proc(ip: ^IPaddress) -> cstring ---
  27. GetLocalAddresses :: proc(addresses: ^IPaddress, maxcount: c.int) -> c.int ---
  28. }
  29. /***********************************************************************/
  30. /* TCP network API */
  31. /***********************************************************************/
  32. TCPsocket :: distinct rawptr
  33. @(default_calling_convention="c", link_prefix="SDLNet_")
  34. foreign lib {
  35. TCP_Open :: proc(ip: ^IPaddress) -> TCPsocket ---
  36. TCP_Accept :: proc(server: TCPsocket) -> TCPsocket ---
  37. TCP_GetPeerAddress :: proc(sock: TCPsocket) -> ^IPaddress ---
  38. TCP_Send :: proc(sock: TCPsocket, data: rawptr, len: c.int) -> c.int ---
  39. TCP_Recv :: proc(sock: TCPsocket, data: rawptr, maxlen: c.int) -> c.int ---
  40. TCP_Close :: proc(sock: TCPsocket) ---
  41. }
  42. /* The maximum channels on a a UDP socket */
  43. MAX_UDPCHANNELS :: 32
  44. /* The maximum addresses bound to a single UDP socket channel */
  45. MAX_UDPADDRESSES :: 4
  46. UDPsocket :: distinct rawptr
  47. UDPpacket :: struct {
  48. channel: c.int, /* The src/dst channel of the packet */
  49. data: [^]u8, /* The packet data */
  50. len: c.int, /* The length of the packet data */
  51. maxlen: c.int, /* The size of the data buffer */
  52. status: c.int, /* packet status after sending */
  53. address: IPaddress, /* The source/dest address of an incoming/outgoing packet */
  54. }
  55. /***********************************************************************/
  56. /* UDP network API */
  57. /***********************************************************************/
  58. @(default_calling_convention="c", link_prefix="SDLNet_")
  59. foreign lib {
  60. AllocPacket :: proc(size: c.int) -> ^UDPpacket ---
  61. ResizePacket :: proc(packet: ^UDPpacket, newsize: c.int) -> c.int ---
  62. FreePacket :: proc(packet: ^UDPpacket) ---
  63. AllocPacketV :: proc(howmany: c.int, size: c.int) -> [^]^UDPpacket ---
  64. FreePacketV :: proc(packetV: [^]^UDPpacket) ---
  65. UDP_Open :: proc(port: u16) -> UDPsocket ---
  66. UDP_SetPacketLoss :: proc(sock: UDPsocket, percent: c.int) ---
  67. UDP_Bind :: proc(sock: UDPsocket, channel: c.int, address: ^IPaddress) -> c.int ---
  68. UDP_Unbind :: proc(sock: UDPsocket, channel: c.int) ---
  69. UDP_GetPeerAddress :: proc(sock: UDPsocket, channel: c.int) -> IPaddress ---
  70. UDP_SendV :: proc(sock: UDPsocket, packets: [^]^UDPpacket, npackets: c.int) -> c.int ---
  71. UDP_Send :: proc(sock: UDPsocket, channel: c.int, packet: ^UDPpacket) -> c.int ---
  72. UDP_RecvV :: proc(sock: UDPsocket, packets: [^]^UDPpacket) -> c.int ---
  73. UDP_Recv :: proc(sock: UDPsocket, packet: ^UDPpacket) -> c.int ---
  74. UDP_Close :: proc(sock: UDPsocket) ---
  75. }
  76. AllocPacketSlice :: proc "c" (howmany: c.int, size: c.int) -> []^UDPpacket {
  77. if packets := AllocPacketV(howmany, size); packets != nil {
  78. return packets[:howmany]
  79. }
  80. return nil
  81. }
  82. FreePacketSlice :: proc "c" (packets: []^UDPpacket) {
  83. FreePacketV(raw_data(packets))
  84. }
  85. UDP_SendSlice :: proc "c" (sock: UDPsocket, packets: []^UDPpacket) -> c.int {
  86. return UDP_SendV(sock, raw_data(packets), c.int(len(packets)))
  87. }
  88. /***********************************************************************/
  89. /* Hooks for checking sockets for available data */
  90. /***********************************************************************/
  91. SocketSet :: distinct rawptr
  92. /* Any network socket can be safely cast to this socket type */
  93. GenericSocket :: ^struct { ready: c.int }
  94. TCP_AddSocket :: #force_inline proc "c" (set: SocketSet, sock: TCPsocket) -> c.int {
  95. return AddSocket(set, (GenericSocket)(sock))
  96. }
  97. UDP_AddSocket :: #force_inline proc "c" (set: SocketSet, sock: UDPsocket) -> c.int {
  98. return AddSocket(set, (GenericSocket)(sock))
  99. }
  100. TCP_DelSocket :: #force_inline proc "c" (set: SocketSet, sock: TCPsocket) -> c.int {
  101. return DelSocket(set, (GenericSocket)(sock))
  102. }
  103. UDP_DelSocket :: #force_inline proc "c" (set: SocketSet, sock: UDPsocket) -> c.int {
  104. return DelSocket(set, (GenericSocket)(sock))
  105. }
  106. SocketReady :: #force_inline proc "c" (sock: rawptr) -> bool {
  107. s := (GenericSocket)(sock)
  108. return bool(s != nil && s.ready != 0)
  109. }
  110. @(default_calling_convention="c", link_prefix="SDLNet_")
  111. foreign lib {
  112. AllocSocketSet :: proc(maxsockets: c.int) -> SocketSet ---
  113. AddSocket :: proc(set: SocketSet, sock: GenericSocket) -> c.int ---
  114. DelSocket :: proc(set: SocketSet, sock: GenericSocket) -> c.int ---
  115. CheckSockets :: proc(set: SocketSet, timeout: u32) -> c.int ---
  116. FreeSocketSet :: proc(set: SocketSet) ---
  117. }
  118. /***********************************************************************/
  119. /* Error reporting functions */
  120. /***********************************************************************/
  121. @(default_calling_convention="c", link_prefix="SDLNet_")
  122. foreign lib {
  123. SetError :: proc(fmt: cstring, #c_vararg args: ..any) ---
  124. GetError :: proc() -> cstring ---
  125. }
  126. /***********************************************************************/
  127. /* Inline functions to read/write network data */
  128. /***********************************************************************/
  129. /* Write a 16/32-bit value to network packet buffer */
  130. Write16 :: #force_inline proc "c" (value: u16, areap: rawptr) {
  131. area := (^[2]u8)(areap)
  132. area[0] = u8((value >> 8) & 0xFF)
  133. area[1] = u8( value & 0xFF)
  134. }
  135. Write32 :: #force_inline proc "c" (value: u32, areap: rawptr) {
  136. area := (^[4]u8)(areap)
  137. area[0] = u8((value >> 24) & 0xFF)
  138. area[1] = u8((value >> 16) & 0xFF)
  139. area[2] = u8((value >> 8) & 0xFF)
  140. area[3] = u8( value & 0xFF)
  141. }
  142. /* Read a 16/32-bit value from network packet buffer */
  143. Read16 :: #force_inline proc "c" (areap: rawptr) -> u16 {
  144. area := (^[2]u8)(areap)
  145. return u16(area[0])<<8 | u16(area[1])
  146. }
  147. Read32 :: #force_inline proc "c" (areap: rawptr) -> u32 {
  148. area := (^[4]u8)(areap)
  149. return u32(area[0])<<24 | u32(area[1])<<16 | u32(area[2])<<8 | u32(area[3])
  150. }