Browse Source

Add intercept and send raw data support (for unconnected data)

alexander.bondarev 4 years ago
parent
commit
3ec3dd6cd7
1 changed files with 62 additions and 2 deletions
  1. 62 2
      include/enet.h

+ 62 - 2
include/enet.h

@@ -914,6 +914,8 @@ extern "C" {
     ENET_API enet_uint32 enet_host_get_packets_received(ENetHost *);
     ENET_API enet_uint32 enet_host_get_bytes_sent(ENetHost *);
     ENET_API enet_uint32 enet_host_get_bytes_received(ENetHost *);
+    ENET_API enet_uint32 enet_host_get_received_data(ENetHost *, enet_uint8** data);
+    ENET_API enet_uint32 enet_host_get_mtu(ENetHost *);
 
     ENET_API enet_uint32 enet_peer_get_id(ENetPeer *);
     ENET_API enet_uint32 enet_peer_get_ip(ENetPeer *, char * ip, size_t ipLength);
@@ -942,9 +944,12 @@ extern "C" {
     ENET_API void       enet_host_destroy(ENetHost *);
     ENET_API ENetPeer * enet_host_connect(ENetHost *, const ENetAddress *, size_t, enet_uint32);
     ENET_API int        enet_host_check_events(ENetHost *, ENetEvent *);
-    ENET_API int        enet_host_service(ENetHost *, ENetEvent *, enet_uint32);
+    ENET_API int        enet_host_service(ENetHost *, ENetEvent *, enet_uint32);    
+    ENET_API int        enet_host_send_raw(ENetHost *, const ENetAddress *, enet_uint8 *, size_t);
+    ENET_API int        enet_host_send_raw_offset(ENetHost *, const ENetAddress *, enet_uint8 *, size_t, size_t);
+    ENET_API void       enet_host_set_intercept(ENetHost *, const ENetInterceptCallback);
     ENET_API void       enet_host_flush(ENetHost *);
-    ENET_API void       enet_host_broadcast(ENetHost *, enet_uint8, ENetPacket *);
+    ENET_API void       enet_host_broadcast(ENetHost *, enet_uint8, ENetPacket *);    
     ENET_API void       enet_host_compress(ENetHost *, const ENetCompressor *);
     ENET_API void       enet_host_channel_limit(ENetHost *, size_t);
     ENET_API void       enet_host_bandwidth_limit(ENetHost *, enet_uint32, enet_uint32);
@@ -3382,6 +3387,20 @@ extern "C" {
         return host->totalReceivedData;
     }
 
+    /** Gets received data buffer. Returns buffer length.
+     *  @param host host to access recevie buffer
+     *  @param data ouput parameter for recevied data
+     *  @retval buffer length
+     */
+    enet_uint32 enet_host_get_received_data(ENetHost *host, /*out*/ enet_uint8** data) {
+        *data = host->receivedData;
+        return host->receivedDataLength;
+    }
+
+    enet_uint32 enet_host_get_mtu(ENetHost *host) {
+        return host->mtu;
+    }
+
     enet_uint32 enet_peer_get_id(ENetPeer *peer) {
         return peer->connectID;
     }
@@ -4544,6 +4563,47 @@ extern "C" {
         }
     }
 
+    /** Sends raw data to specified address. Useful when you want to send unconnected data using host's socket.         
+     *  @param host host sending data
+     *  @param address destination address
+     *  @param data data pointer
+     *  @param dataLength length of data to send
+     *  @retval >=0 bytes sent
+     *  @retval <0 error
+     *  @sa enet_socket_send
+     */
+    int enet_host_send_raw(ENetHost *host, const ENetAddress* address, enet_uint8* data, size_t dataLength) {
+        ENetBuffer buffer;
+        buffer.data = data;
+        buffer.dataLength = dataLength;
+        return enet_socket_send(host->socket, address, &buffer, 1);
+    }
+
+    /** Sends raw data to specified address. Useful when you want to send unconnected data using host's socket.     
+     *  @param host host sending data
+     *  @param address destination address
+     *  @param data data pointer
+     *  @param dataOffset data offset
+     *  @param dataLength length of data to send from offset
+     *  @retval >=0 bytes sent
+     *  @retval <0 error
+     *  @sa enet_socket_send
+     */
+    int enet_host_send_raw_offset(ENetHost *host, const ENetAddress* address, enet_uint8* data, size_t dataOffset, size_t dataLength) {
+        ENetBuffer buffer;
+        buffer.data = data + dataOffset;
+        buffer.dataLength = dataLength;
+        return enet_socket_send(host->socket, address, &buffer, 1);
+    }
+
+    /** Sets intercept callback for the host.
+     *  @param host host to set a callback
+     *  @param callback intercept callback
+     */
+    void enet_host_set_intercept(ENetHost *host, const ENetInterceptCallback callback) {
+        host->intercept = callback;
+    }
+
     /** Sets the packet compressor the host should use to compress and decompress packets.
      *  @param host host to enable or disable compression for
      *  @param compressor callbacks for for the packet compressor; if NULL, then compression is disabled