Skip to content

UDPSubsystem

huggins edited this page May 3, 2023 · 7 revisions

UDP Subsystem

A class to handle receiving and sending data via UDP sockets and triggers the appropriate events

class UUDPSubsystem
  : public UGameInstanceSubsystem

Includes

Back to Top


Members

Access Type Name Description
Protected TMap<int32, FReceiveSocketMapValue> AllReceiveSockets Map containing all opened receive sockets.
Protected TMap<int32, FSocket*> AllSendSockets Map containing all opened send sockets.
Private FString LocalIPAddress The IP address of the local machine
Protected ISocketSubsystem* SocketSubsystem The socket subsystem. Used to open new sockets and close existing sockets.
Private int TotalReceiveSocketIterator The count of how many receive sockets have been opened whether closed or not.
Private int TotalSendSocketIterator The count of how many send sockets have been opened whether closed or not.

Back to Top


Events

Delegate Name Event Name Description
FUDPMessageSignature OnReceivedBytes(const TArray<uint8>& Bytes, const FString& IPAddress) Called after bytes are received by a bound UDP socket.
FUDPReceiveSocketStateSignature OnReceiveSocketClosed(int32 ReceiveSocketID, FString IpListeningOn, int32 PortListeningOn) Called after a receive UDP socket has been closed.
FUDPReceiveSocketStateSignature OnReceiveSocketOpened(int32 ReceiveSocketID, FString IpListeningOn, int32 PortListeningOn) Called after a new receive UDP socket is opened.
FUDPSendSocketStateSignature OnSendSocketClosed(int32 SendSocketID, FString LocalIp, int32 LocalPort, FString PeerIp, int32 PeerPort) Called after a send UDP socket has been closed.
FUDPSendSocketStateSignature OnSendSocketOpened(int32 SendSocketID, FString LocalIp, int32 LocalPort, FString PeerIp, int32 PeerPort) Called after a send UDP socket has been opened.

Back to Top

Functions

Access Return Name Description
Public bool AnyConnectedSockets Checks whether there are any connected receive or send sockets.
Public bool CloseAllReceiveSockets() Closes all opened receive sockets.
Public bool CloseAllSendSockets() Closes all opened send sockets.
Public bool CloseReceiveSocket(int32 ReceiveSocketIdToClose) Closes the specified receive socket.
Public bool CloseSendSocket(int32 SendSocketIdToClose) Closes the specified send socket.
Public virtual bool Deinitialize() Deinitialization of the subsystem.
Public bool EmitBytes(const TArray<uint8>& Bytes) Sends bytes over all opened send sockets.
Public TArray<int32> GetConnectedReceiveSocketIDs() Gets the IDs of the connected receive sockets, if any.
Public TArray<int32> GetConnectedSendSocketIDs() Gets the IDs of the connected send sockets, if any.
Public virtual bool Initialize(FSubsystemCollectionBase& Collection) Initialization of the subsystem.
Public bool OpenReceiveSocket(FReceiveSocketSettings SocketSettings, int32& ReceiveSocketID, const FString& IpToListenOn, const int32 PortToListenOn) Opens a new UDP receive socket at the given IP address and port.
Public bool OpenSendSocket(FSendSocketSettings SocketSettings, int32& SendSocketID, const FString& IpToSendOn, const int32 PortToSendOn)) Opens a new UDP send socket at the given IP address and port.

Back to Top


Stucts

Name Description
FReceiveSocketMapValue Contains all needed information for UDP send sockets.
FReceiveSocketSettings Contains related settings needed for UDP receive sockets.
FSendSocketSettings Contains related settings needed for UDP send sockets.

Back to Top


Details

All Receive Sockets

protected TMap<int32, FReceiveSocketMapValue> AllReceiveSockets

The mapping between opened receive sockets and their unique IDs.

Back to Top


All Send Sockets

protected TMap<int32, FSocket*> AllSendSockets

The mapping between opened send sockets and their unique IDs.

Back to Top


Local IP Address

private FString LocalIPAddress

The IP address of the local machine.

Back to Top


Socket Subsystem

protected ISocketSubsystem* SocketSubsystem

The socket subsystem. Used to open new sockets and close existing sockets.

Back to Top


Total Receive Socket Iterator

private int TotalReceiveSocketIterator

The total number of receive sockets that have been opened whether closed or not.

Back to Top


Total Send Socket Iterator

private int TotalSendSocketIterator

The total number of send sockets that have been opened whether closed or not.

Back to Top


On Received Bytes

public FUDPMessageSignature OnReceivedBytes

Called after bytes are received by a bound UDP socket.

Category: GRILL DIS|UDP Subsystem|Events

Parameter Description
Bytes The bytes that were received by the UDP receiver
IPAddress The IP address that the message was received on

Back to Top


On Receive Socket Closed

public FUDPReceiveSocketStateSignature OnReceiveSocketClosed

Called after a receive UDP socket has been closed.

Category: GRILL DIS|UDP Subsystem|Events

Parameter Description
ReceiveSocketID The receive socket ID that was closed
IpListeningOn The IP address that the closed receive socket was listening on
PortListeningOn The port that the closed receive socket was listening on

Back to Top


On Receive Socket Opened

public FUDPReceiveSocketStateSignature OnReceiveSocketOpened

Called after a new receive UDP socket is opened.

Category: GRILL DIS|UDP Subsystem|Events

Parameter Description
ReceiveSocketID The receive socket ID that was opened
IpListeningOn The IP address that the opened receive socket is listening on
PortListeningOn The port that the opened receive socket is listening on

Back to Top


On Send Socket Closed

public FUDPSendSocketStateSignature OnSendSocketClosed

Called after a send UDP socket has been closed.

Category: GRILL DIS|UDP Subsystem|Events

Parameter Description
SendSocketID The receive socket ID that was closed
LocalIp The local IP address that the closed send socket was on
LocalPort The local port that the closed send socket was on
PeerIp The local IP address that the closed send socket was sending to
PeerPort The local port that the closed send socket was sending to

Back to Top


On Send Socket Opened

public FUDPSendSocketStateSignature OnSendSocketOpened

Called after a send UDP socket has been opened.

Category: GRILL DIS|UDP Subsystem|Events

Parameter Description
SendSocketID The receive socket ID that was opened
LocalIp The local IP address that the opened send socket is on
LocalPort The local port that the opened send socket is on
PeerIp The local IP address that the opened send socket is sending to
PeerPort The local port that the opened send socket is sending to

Back to Top


Any Connected Sockets

public bool AnyConnectedSockets()

Checks whether there are any connected receive or send sockets.

Category: GRILL DIS|UDP Subsystem|Events

Returns
True if any sockets are connected, false otherwise

Back to Top


Close All Receive Sockets

public bool CloseAllReceiveSockets()

Closes all opened receive sockets.

Category: GRILL DIS|UDP Subsystem|Events

Returns
True all of the receive sockets were closed successfully. If none are opened, returns true.

Back to Top


Close All Send Sockets

public bool CloseAllSendSockets()

Closes all opened send sockets.

Category: GRILL DIS|UDP Subsystem|Events

Returns
True all of the send sockets were closed successfully. If none are opened, returns true.

Back to Top


Close Receive Socket

public bool CloseReceiveSocket
(
	int32 ReceiveSocketIdToClose
)

Closes the opened receive socket with the given ID.

Category: GRILL DIS|UDP Subsystem|Events

Parameter Description
ReceiveSocketIdToClose The ID of the receive socket to close
Returns
True if the receive socket was closed successfully. If none exist with the ID, returns true.

Back to Top


Close Send Socket

public bool CloseSendSocket
(
	int32 SendSocketIdToClose
)

Closes the opened send socket with the given ID.

Category: GRILL DIS|UDP Subsystem|Events

Parameter Description
SendSocketIdToClose The ID of the send socket to close
Returns
True if the send socket was closed successfully. If none exist with the ID, returns true.

Back to Top


Deinitialize

public virtual void Deinitialize() override

Deinitializes the subsystem.

Back to Top


Emit Bytes

public bool EmitBytes
(
	const TArray<uint8>& Bytes
)

Sends bytes over all opened send sockets.

Category: GRILL DIS|UDP Subsystem|Events

Parameter Description
Bytes The bytes to emit over the send sockets.
Returns
True if the sending was successful for every opened socket.

Back to Top


Get Connected Receive Socket IDs

public TArray<int32> GetConnectedReceiveSocketIDs()

Gets the IDs of the connected receive sockets, if any.

Category: GRILL DIS|UDP Subsystem|Events

Returns
An array containing all of the IDs of the connected receive sockets.

Back to Top


Get Connected Send Socket IDs

public TArray<int32> GetConnectedSendSocketIDs()

Gets the IDs of the connected send sockets, if any.

Category: GRILL DIS|UDP Subsystem|Events

Returns
An array containing all of the IDs of the connected send sockets.

Back to Top


Initialize

public virtual void Initialize
(
	FSubsystemCollectionBase& Collection
) override

Initialization of the subsystem.

Parameter Description
Collection The collection of all subsystems.

Back to Top


Open Receive Socket

public bool OpenReceiveSocket
(
	FReceiveSocketSettings SocketSettings, 
	int32& ReceiveSocketID, 
	const FString& IpToListenOn = TEXT("0.0.0.0"), 
	const int32 PortToListenOn = 3001
)

Opens a new UDP receive socket at the given IP address and port. Closes an opened connection if there was one prior to creating a new one.

Category: GRILL DIS|UDP Subsystem|Events

Parameter Description
SocketSettings The settings this seocket should utilize.
ReceiveSocketID The ID of the opened receive socket.
IpToListenOn The IP address to receive UDP packets to.
PortToListenOn The port to receive UDP packets on.
Returns
Whether or not the receive socket was opened successfully.

Back to Top


Open Send Socket

public bool OpenSendSocket
(
	FSendSocketSettings SocketSettings, 
	int32& SendSocketID, 
	const FString& IpToSendOn = TEXT("127.0.0.1"), 
	const int32 PortToSendOn = 3000
)

Opens a new UDP send socket at the given IP address and port. Closes an opened connection if there was one prior to creating a new one.

Category: GRILL DIS|UDP Subsystem|Events

Parameter Description
SocketSettings The settings this seocket should utilize.
SendSocketID The ID of the opened send socket.
IpToSendOn The IP address to send UDP packets to.
PortToSendOn The port to send UDP packets on.
Returns
Whether or not the send socket was opened successfully.

Back to Top


Clone this wiki locally