Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * ServerUDP.cs
- * Created by: Jadson Almeida [jadson.sistemas@gmail.com]
- * Created on: 19/10/2017 (dd/mm/yy)
- */
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- /// <summary>
- /// Simple example for a server UDP receive and send data
- /// </summary>
- public class ServerUDP {
- /// <summary>
- /// UDP connection to send data
- /// </summary>
- UdpClient udpSend;
- /// <summary>
- /// UDP connection to receive data
- /// </summary>
- UdpClient udpReceive;
- /// <summary>
- /// IP end point used to send data in UDP connection
- /// </summary>
- IPEndPoint ipEndPointSend;
- /// <summary>
- /// IP end point used to receive data in UDP connection
- /// </summary>
- IPEndPoint ipEndPointReceive;
- /// <summary>
- /// Port used to send data in UDP connection
- /// </summary>
- int portToSend;
- /// <summary>
- /// Port used to send data in UDP connection
- /// </summary>
- int portToReceive;
- /// <summary>
- /// Thread used to listener UDP connection with <see cref="ReceiveData"/>
- /// </summary>
- Thread thread;
- public ServerUDP()
- {
- // Creates a UDP connection to send data
- ipEndPointSend = new IPEndPoint(IPAddress.Parse("127.0.0.1"), portToSend);
- udpSend = new UdpClient();
- // Creates a UDP connection to receive data
- ipEndPointReceive = new IPEndPoint(IPAddress.Any, portToReceive);
- udpReceive = new UdpClient()
- {
- ExclusiveAddressUse = false
- };
- udpReceive.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
- udpReceive.Client.Bind(ipEndPointReceive);
- // Start thread to listen received data
- thread = new Thread(new ThreadStart(ListeningData));
- thread.Start();
- }
- /// <summary>
- /// (Used as thread) Listen external input from UDP and pass to <see cref="ReceiveData(IAsyncResult)"/>
- /// </summary>
- void ListeningData()
- {
- try
- {
- udpReceive.BeginReceive(new AsyncCallback(ReceiveData), null);
- }
- catch (Exception err)
- {
- }
- }
- /// <summary>
- /// Receive the data from clients and pass it to <see cref="HandleData(byte[])"/>
- /// </summary>
- /// <param name="res"></param>
- void ReceiveData(IAsyncResult res)
- {
- IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, portToReceive);
- byte[] received = udpReceive.EndReceive(res, ref RemoteIpEndPoint);
- HandleData(received);
- ListeningData(); // repeat
- }
- /// <summary>
- /// Handles what data means with <see cref="CalculatePhysics(byte)"/>
- /// </summary>
- /// <param name="data">data to pass to <see cref=""/></param>
- void HandleData(byte[] data)
- {
- // Check if data is wrong
- if (data.Length > 0 && data[0] > 0)
- CalculatePhysics(data[0]);
- }
- /// <summary>
- /// Calculate physic movement based in external input and pass the movement to be sended with <see cref="SendCommand(byte[])"/>
- /// </summary>
- /// <param name="inputs">External input (client UDP)</param>
- void CalculatePhysics(byte inputs)
- {
- byte[] data;
- // calculate what we need to do with the player input
- // ...
- // transform the new position, rotation etc in byte array like this:
- // index: 0 to 3 are values of the front, 4 to 7 are values of the side, 8 to 11 are up, 12 to 15 are x-axis, 16 to 19 are y-axis, 20 to 23 are z-axis,
- // 24 to 27 are board port, 28 to 31 are starboard, 32 to 35 are wing angle, and 36 to 39 are sail angle
- // ...
- // Calls send command
- SendCommand(data);
- }
- /// <summary>
- /// Sends data for client in <see cref="ipEndPointSend"/>
- /// </summary>
- /// <param name="data">data to send</param>
- void SendCommand(byte[] data)
- {
- try
- {
- udpSend.Send(data, data.Length, ipEndPointSend);
- }
- catch (Exception err)
- {
- print(err.ToString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement