Network Library Documentation
Loading...
Searching...
No Matches
Client.UDP Class Reference

Handles UDP connection and communication for the client. More...

Public Member Functions

 UDP ()
void Connect (int Port)
 Initializes and connects the UDP socket to the server.
void SendUdpData (Packet SendPacket)
 Sends UDP data packet to the server.
void Disconnect ()
 Disconnects the UDP client from the server.

Public Attributes

UdpClient Socket
IPEndPoint EndPoint

Private Member Functions

void UdpDataHandler (byte[] Data)
 Handles incoming UDP data and calls PacketAction() on the main thread.
void UdpReceiveCallback (IAsyncResult Result)
 Callback for receiving data over the UDP. Reads data into the data buffer, manages it by Handler and continues reading.

Detailed Description

Handles UDP connection and communication for the client.

Definition at line 468 of file Client.cs.

Constructor & Destructor Documentation

◆ UDP()

Client.UDP.UDP ( )

Definition at line 472 of file Client.cs.

473 {
474 EndPoint = new IPEndPoint(IPAddress.Parse(instance.defaultNetworkSettings.ipAddress), instance.defaultNetworkSettings.port);
475 }

Member Function Documentation

◆ Connect()

void Client.UDP.Connect ( int Port)

Initializes and connects the UDP socket to the server.

Definition at line 479 of file Client.cs.

480 {
481 Socket = new UdpClient(Port);
482 Socket.Connect(EndPoint);
483 Socket.BeginReceive(UdpReceiveCallback, null);
484 Packet SendPacket = new Packet();
485 SendUdpData(SendPacket);
486 }

◆ Disconnect()

void Client.UDP.Disconnect ( )

Disconnects the UDP client from the server.

Definition at line 524 of file Client.cs.

525 {
526 instance.Disconnect();
527 EndPoint = null;
528 Socket = null;
529 }

◆ SendUdpData()

void Client.UDP.SendUdpData ( Packet SendPacket)

Sends UDP data packet to the server.

Definition at line 504 of file Client.cs.

505 {
506 try
507 {
508 if (Socket == null || !Client.instance.isConnected) return;
509
510 SendPacket.AddIntAtStart(instance.id);
511 if(Socket != null)
512 {
513 Socket.BeginSend(SendPacket.ConvertToArray(), SendPacket.DataLength(), null, null);
514 }
515 }
516 catch (Exception ex)
517 {
518 Debug.LogError($"Error sending UDP data {ex}");
519 }
520 }
byte[] ConvertToArray()
Definition Packet.cs:287
int DataLength()
Definition Packet.cs:300
void AddIntAtStart(int number)
Definition Packet.cs:292

◆ UdpDataHandler()

void Client.UDP.UdpDataHandler ( byte[] Data)
private

Handles incoming UDP data and calls PacketAction() on the main thread.

Definition at line 490 of file Client.cs.

491 {
492 Packet ReceivedPacket = new Packet(Data);
493 int DataLength = ReceivedPacket.ReadInt();
494 Data = ReceivedPacket.ReadBytes(DataLength);
495 ThreadsController.StartOnMainThread(() => {
496 Packet nPacket = new Packet(Data);
497 int PId = nPacket.ReadInt();
498 PacketAction(PId, nPacket);
499 }) ;
500 }
int ReadInt(bool changeReadPosition=true)
Read int data.
Definition Packet.cs:80
byte[] ReadBytes(int arrayLength, bool changeReadPosition=true)
read byte array data
Definition Packet.cs:188

◆ UdpReceiveCallback()

void Client.UDP.UdpReceiveCallback ( IAsyncResult Result)
private

Callback for receiving data over the UDP. Reads data into the data buffer, manages it by Handler and continues reading.

Definition at line 534 of file Client.cs.

535 {
536 try
537 {
538 byte[] data = Socket.EndReceive(Result, ref EndPoint);
539 Socket.BeginReceive(UdpReceiveCallback, null);
540 if(data.Length < 4)
541 {
542 instance.Disconnect();
543 return;
544 }
545 UdpDataHandler(data);
546 } catch
547 {
548 Disconnect();
549 }
550 }

Member Data Documentation

◆ EndPoint

IPEndPoint Client.UDP.EndPoint

Definition at line 471 of file Client.cs.

◆ Socket

UdpClient Client.UDP.Socket

Definition at line 470 of file Client.cs.