Advertisement
medvedya

Untitled

Feb 7th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Threading;
  9.  
  10. namespace MedvedyaNetworkEngein
  11. {
  12.  
  13.     public interface NetworkController
  14.     {
  15.  
  16.     }
  17.     public class UDPLevel
  18.     {
  19.         void StartReceive()
  20.         {
  21.             Thread rec = new Thread(new ThreadStart(Receiver));
  22.             rec.Start();
  23.         }
  24.  
  25.         public static void Receiver()
  26.         {
  27.             UdpClient receivingUdpClient = new UdpClient(5555);
  28.             IPEndPoint RemoteIpEndPoint = null;
  29.             try
  30.             {
  31.                 while (true)
  32.                 {
  33.                     byte[] receiveBytes = receivingUdpClient.Receive(
  34.                        ref RemoteIpEndPoint);
  35.                     //если клиент не известин то добавляем
  36.                     if (!Client.allClients.ContainsKey(RemoteIpEndPoint.Address))
  37.                     {
  38.  
  39.                         Client newClient = new Client(RemoteIpEndPoint.Address);
  40.                         newClient.port = RemoteIpEndPoint.Port;
  41.                     }
  42.                     Client client = Client.allClients[RemoteIpEndPoint.Address];
  43.                     //разбираем сообщения клиента
  44.                     int index = 0;
  45.                     while (true)
  46.                     {
  47.                         int length = BitConverter.ToUInt16(receiveBytes, index);
  48.                         if (length <= 0)
  49.                         {
  50.                             //хм. какоето очень короткое сообщение.
  51.                             break;
  52.                         }
  53.                         //получаем отдельное сообщение
  54.                         byte[] myMessage = new byte[length];
  55.                         receiveBytes.CopyTo(myMessage, index + 2);
  56.                         client.ReceiveMessage(myMessage);
  57.                         index += (2 + length);
  58.                         if (index >= receiveBytes.Length)
  59.                         {
  60.                             //кончились сообщения
  61.                             break;
  62.                         }
  63.                     }
  64.                 }
  65.             }
  66.             catch (Exception ex)
  67.             {
  68.             }
  69.         }
  70.     }
  71.     public class Client
  72.     {
  73.  
  74.         public static Dictionary<IPAddress, Client> allClients = new Dictionary<IPAddress, Client>();
  75.         IPAddress ipAdress;
  76.         public int port;
  77.  
  78.         public ClientMeasgeController clientMeasgeController = new ClientMeasgeController();
  79.         public string idClient = "";
  80.         public Client(IPAddress ipAdress)
  81.         {
  82.             this.ipAdress = ipAdress;
  83.             allClients.Add(ipAdress, this);
  84.         }
  85.         public void ReceiveMessage(Byte[] bytes)
  86.         {
  87.             //хз, тут нужно много мозга
  88.         }
  89.  
  90.     }
  91.     public class Player
  92.     {
  93.  
  94.     }
  95.  
  96.  
  97.     public interface IMessageOnlyLast
  98.     {
  99.         uint idObject;
  100.         byte messageType;
  101.         byte[] date;
  102.         public void Pack();
  103.         public void UnPack();
  104.     }
  105.     public class ClientMeasgeController
  106.     {
  107.         private List<IMessageOnlyLast> onlyLastList = new List<IMessageOnlyLast>();
  108.         public void addMessageOnlyLast(IMessageOnlyLast message)
  109.         {
  110.             List<IMessageOnlyLast> delList = new List<IMessageOnlyLast>();
  111.             foreach (IMessageOnlyLast im in onlyLastList)
  112.             {
  113.                 if (message.idObject == im.idObject && message.messageType == im.messageType)
  114.                 {
  115.                     delList.Add(im);
  116.                 }
  117.                 onlyLastList.Add(message);
  118.                 message.Pack();
  119.             }
  120.             foreach (IMessageOnlyLast imd in delList)
  121.             {
  122.                 onlyLastList.Remove(imd);
  123.             }
  124.         }
  125.  
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement