sdl_net.odin 6.6 KB

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