Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. /*
  2. SERVER
  3. */
  4.     //This method is running on FixedUpdate
  5.         //FixedUpdate is set to 60 Ticks, so we have a 60 Tickrate
  6.         internal void Listen()
  7.         {
  8.             if (!m_host.IsSet) return;
  9.  
  10.             if (m_host.Service(0, out UDPEvent evnt))
  11.             {
  12.                 switch (evnt.Type)
  13.                 {
  14.                     case UDPEventType.Connect:
  15.             //Add the Event Peer as a new Connection
  16.                         AddConnection(evnt.Peer);
  17.                         break;
  18.                     case UDPEventType.Disconnect:
  19.             //Tell everyone, that Player XY is disconnected
  20.                         ResponseClientDisconnected(RemoveConnection(evnt.Peer.GetRemoteAddress()));
  21.                         break;
  22.                     case UDPEventType.Receive:
  23.             //Process incoming Packets and forwared it to other Players
  24.                         ProcessPackets(evnt.Packet, GetClient(evnt.Peer.GetRemoteAddress()));
  25.                         evnt.Packet.Dispose();
  26.                         break;
  27.                 }
  28.             }
  29.         }
  30.  
  31.  
  32. /* CLIENT */
  33.  
  34.         internal void Connect(string _ip, ushort _port)
  35.         {
  36.             if (isConnected)
  37.                 return;
  38.  
  39.             UDPAddress address = new UDPAddress();
  40.             address.SetHost(_ip);
  41.             address.Port = _port;
  42.  
  43.             m_peer = new UDPPeer();
  44.             m_peer = m_host.Connect(address, m_config.channelLimit, 0);
  45.  
  46.             if (m_host.Service(5000, out UDPEvent evnt))
  47.             {
  48.                 if (evnt.Type == UDPEventType.Connect)
  49.                 {
  50.             //Create a new Connection with the Event Peer
  51.                     m_connection = new Connection(evnt.Peer);
  52.                     //Send a Message to Server, requesting Client ID
  53.                     RequestClientIDEvent RPIDEvent = new RequestClientIDEvent();
  54.                     RPIDEvent.version = m_version;
  55.                     RPIDEvent.name = playerName;
  56.                     m_connection.SendEventCS(RPIDEvent);
  57.                 }
  58.             }
  59.             else
  60.             {
  61.                 SkyLog.Error("Can't connect");
  62.                 m_peer.Reset();
  63.             }
  64.         }
  65.  
  66.         //This method is running on FixedUpdate
  67.         //FixedUpdate is set to 60 Ticks, so we have a 60 Tickrate
  68.         internal void Listen()
  69.         {
  70.             if (!m_host.IsSet) return;
  71.  
  72.             if (m_host.Service(0, out UDPEvent evnt))
  73.             {
  74.                 if (evnt.Type == UDPEventType.Receive)
  75.                 {
  76.             //Process Packets
  77.                     ProcessPackets(evnt.Packet);
  78.                     evnt.Packet.Dispose();
  79.                 }
  80.             }
  81.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement