Handles UDP connection and communication for the client.
More...
|
| 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.
|
Handles UDP connection and communication for the client.
Definition at line 468 of file Client.cs.
◆ UDP()
Definition at line 472 of file Client.cs.
473 {
474 EndPoint = new IPEndPoint(IPAddress.Parse(instance.defaultNetworkSettings.ipAddress), instance.defaultNetworkSettings.port);
475 }
◆ 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
511 if(Socket != null)
512 {
514 }
515 }
516 catch (Exception ex)
517 {
518 Debug.LogError($"Error sending UDP data {ex}");
519 }
520 }
void AddIntAtStart(int number)
◆ 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);
498 PacketAction(PId, nPacket);
499 }) ;
500 }
int ReadInt(bool changeReadPosition=true)
Read int data.
byte[] ReadBytes(int arrayLength, bool changeReadPosition=true)
read byte array data
◆ 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 }
◆ EndPoint
| IPEndPoint Client.UDP.EndPoint |
◆ Socket
| UdpClient Client.UDP.Socket |