{-| Module : Bindings.Netcode.IO Description : Low-level bindings to the netcode.io library. Copyright : (c) Pavel Krajcevski, 2020 License : BSD-3 Maintainer : krajcevski@gmail.com Stability : experimental Portability : Portable This module contains the low-level bindings that represent the direct interface between Haskell and the C library . The bindings here are meant for advanced usage, as they are not particularly idiomatic Haskell, and largely represent the types that we get from the library. For high level bindings (recommended), please refer to the "Netcode.IO" module. -} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE StandaloneDeriving #-} -------------------------------------------------------------------------------- #include #include -------------------------------------------------------------------------------- module Bindings.Netcode.IO where import Data.Word (Word8, Word16, Word64) import Foreign.C.String (CString) import Foreign.C.Types (CInt(..), CDouble(..)) import Foreign.Marshal.Array (peekArray, pokeArray) import Foreign.Ptr (Ptr, FunPtr, plusPtr) import Foreign.Storable (Storable(..)) import Prelude ( IO, Eq, Show, Num , ($) , div, undefined, return, take ) -------------------------------------------------------------------------------- #num NETCODE_CONNECT_TOKEN_BYTES #num NETCODE_KEY_BYTES #num NETCODE_MAC_BYTES #num NETCODE_USER_DATA_BYTES #num NETCODE_MAX_SERVERS_PER_CONNECT #num NETCODE_CLIENT_STATE_CONNECT_TOKEN_EXPIRED #num NETCODE_CLIENT_STATE_INVALID_CONNECT_TOKEN #num NETCODE_CLIENT_STATE_CONNECTION_TIMED_OUT #num NETCODE_CLIENT_STATE_CONNECTION_RESPONSE_TIMED_OUT #num NETCODE_CLIENT_STATE_CONNECTION_REQUEST_TIMED_OUT #num NETCODE_CLIENT_STATE_CONNECTION_DENIED #num NETCODE_CLIENT_STATE_DISCONNECTED #num NETCODE_CLIENT_STATE_SENDING_CONNECTION_REQUEST #num NETCODE_CLIENT_STATE_SENDING_CONNECTION_RESPONSE #num NETCODE_CLIENT_STATE_CONNECTED #num NETCODE_MAX_CLIENTS #num NETCODE_MAX_PACKET_SIZE #num NETCODE_LOG_LEVEL_NONE #num NETCODE_LOG_LEVEL_ERROR #num NETCODE_LOG_LEVEL_INFO #num NETCODE_LOG_LEVEL_DEBUG #num NETCODE_OK #num NETCODE_ERROR #num NETCODE_ADDRESS_NONE #num NETCODE_ADDRESS_IPV4 #num NETCODE_ADDRESS_IPV6 #ccall netcode_init, IO CInt #ccall netcode_term, IO () #starttype struct netcode_address_t #array_field data.ipv4, Word8 #array_field data.ipv6, Word16 #field port, Word16 #field type, Word8 #stoptype #ccall netcode_parse_address, CString -> Ptr -> IO CInt #ccall netcode_address_to_string, Ptr -> CString -> IO CString #ccall netcode_address_equal, Ptr -> Ptr -> IO CInt #opaque_t netcode_network_simulator_t #opaque_t netcode_client_t #opaque_t netcode_server_t #callback_t allocate_function_t, Ptr () -> Word64 -> IO (Ptr ()) #callback_t free_function_t, Ptr () -> Ptr () -> IO () #callback_t state_change_callback_t, Ptr () -> CInt -> CInt -> IO () #callback_t connect_disconnect_callback_t, Ptr () -> CInt -> CInt -> IO () #callback_t send_loopback_packet_callback_t, Ptr () -> CInt -> Ptr Word8 -> CInt -> Word64 -> IO () #callback_t send_packet_override_t, Ptr () -> Ptr -> Ptr Word8 -> CInt -> IO () #callback_t receive_packet_override_t, Ptr () -> Ptr -> Ptr Word8 -> CInt -> IO CInt #starttype struct netcode_client_config_t #field allocator_context, Ptr () #field allocate_function, #field free_function, #field network_simulator, Ptr #field callback_context, Ptr () #field state_change_callback, #field send_loopback_packet_callback, #field override_send_and_receive, CInt #field send_packet_override, #field receive_packet_override, #stoptype #ccall netcode_default_client_config, Ptr -> IO () #ccall netcode_client_create, CString -> Ptr -> CDouble -> IO (Ptr ) #ccall netcode_client_destroy, Ptr -> IO () #ccall netcode_client_connect, Ptr -> Ptr Word8 -> IO () #ccall netcode_client_update, Ptr -> CDouble -> IO () #ccall netcode_client_next_packet_sequence, Ptr -> IO Word64 #ccall netcode_client_send_packet, Ptr -> Ptr Word8 -> CInt -> IO () #ccall netcode_client_receive_packet, Ptr -> Ptr CInt -> Ptr Word64 -> IO (Ptr Word8) #ccall netcode_client_free_packet, Ptr -> Ptr () -> IO () #ccall netcode_client_disconnect, Ptr -> IO () #ccall netcode_client_state, Ptr -> IO CInt #ccall netcode_client_index, Ptr -> IO CInt #ccall netcode_client_max_clients, Ptr -> IO CInt #ccall netcode_client_connect_loopback, Ptr -> CInt -> CInt -> IO () #ccall netcode_client_disconnect_loopback, Ptr -> IO () #ccall netcode_client_process_packet, Ptr -> Ptr -> Ptr Word8 -> CInt -> IO () #ccall netcode_client_loopback, Ptr -> IO CInt #ccall netcode_client_process_loopback_packet, Ptr -> Ptr Word8 -> CInt -> Word64 -> IO () #ccall netcode_client_get_port, Ptr -> IO Word16 #ccall netcode_client_server_address, Ptr -> IO (Ptr ) #ccall netcode_generate_connect_token, CInt -> Ptr CString -> Ptr CString -> CInt -> CInt -> Word64 -> Word64 -> Ptr Word8 -> Ptr Word8 -> Ptr Word8 -> IO CInt #starttype struct netcode_server_config_t #field protocol_id, Word64 #array_field private_key, Word8 #field allocator_context, Ptr () #field allocate_function, #field free_function, #field network_simulator, Ptr #field callback_context, Ptr () #field connect_disconnect_callback, #field send_loopback_packet_callback, #field override_send_and_receive, CInt #field send_packet_override, #field receive_packet_override, #stoptype #ccall netcode_default_server_config, Ptr -> IO () #ccall netcode_server_create, CString -> Ptr -> CDouble -> IO (Ptr ) #ccall netcode_server_destroy, Ptr -> IO () #ccall netcode_server_start, Ptr -> CInt -> IO () #ccall netcode_server_stop, Ptr -> IO () #ccall netcode_server_running, Ptr -> IO CInt #ccall netcode_server_max_clients, Ptr -> IO CInt #ccall netcode_server_update, Ptr -> CDouble -> IO () #ccall netcode_server_client_connected, Ptr -> CInt -> IO CInt #ccall netcode_server_client_id, Ptr -> CInt -> IO Word64 #ccall netcode_server_client_address, Ptr -> CInt -> IO (Ptr ) #ccall netcode_server_disconnect_client, Ptr -> CInt -> IO () #ccall netcode_server_disconnect_all_clients, Ptr -> IO () #ccall netcode_server_next_packet_sequence, Ptr -> CInt -> IO Word64 #ccall netcode_server_send_packet, Ptr -> CInt -> Ptr Word8 -> CInt -> IO () #ccall netcode_server_receive_packet, Ptr -> CInt -> Ptr CInt -> Ptr Word64 -> IO (Ptr Word8) #ccall netcode_server_free_packet, Ptr -> Ptr () -> IO () #ccall netcode_server_num_connected_clients, Ptr -> IO CInt #ccall netcode_server_client_user_data, Ptr -> CInt -> IO (Ptr ()) #ccall netcode_server_process_packet, Ptr -> Ptr -> Ptr Word8 -> CInt -> IO () #ccall netcode_server_connect_loopback_client, Ptr -> CInt -> Word64 -> Ptr Word8 -> IO () #ccall netcode_server_disconnect_loopback_client, Ptr -> CInt -> IO () #ccall netcode_server_client_loopback, Ptr -> CInt -> IO CInt #ccall netcode_server_process_loopback_packet, Ptr -> CInt -> Ptr Word8 -> CInt -> Word64 -> IO () #ccall netcode_server_get_port, Ptr -> IO Word16 #ccall netcode_log_level, CInt -> IO () #callback_t netcode_assert_function_ty, CString -> CString -> CString -> CInt -> IO () #ccall netcode_set_assert_function, -> IO () -- void netcode_set_printf_function( int (*function)( NETCODE_CONST char *, ... ) ); #ccall netcode_random_bytes, Ptr Word8 -> CInt -> IO () #ccall netcode_sleep, CDouble -> IO () #ccall netcode_time, IO CDouble -- | For testing only. #ccall netcode_test, IO ()