Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- namespace MedvedyaNetworkEngein
- {
- public interface NetworkController
- {
- }
- public class UDPLevel
- {
- void StartReceive()
- {
- Thread rec = new Thread(new ThreadStart(Receiver));
- rec.Start();
- }
- public static void Receiver()
- {
- UdpClient receivingUdpClient = new UdpClient(5555);
- IPEndPoint RemoteIpEndPoint = null;
- try
- {
- while (true)
- {
- byte[] receiveBytes = receivingUdpClient.Receive(
- ref RemoteIpEndPoint);
- //если клиент не известин то добавляем
- if (!Client.allClients.ContainsKey(RemoteIpEndPoint.Address))
- {
- Client newClient = new Client(RemoteIpEndPoint.Address);
- newClient.port = RemoteIpEndPoint.Port;
- }
- Client client = Client.allClients[RemoteIpEndPoint.Address];
- //разбираем сообщения клиента
- int index = 0;
- while (true)
- {
- int length = BitConverter.ToUInt16(receiveBytes, index);
- if (length <= 0)
- {
- //хм. какоето очень короткое сообщение.
- break;
- }
- //получаем отдельное сообщение
- byte[] myMessage = new byte[length];
- receiveBytes.CopyTo(myMessage, index + 2);
- client.ReceiveMessage(myMessage);
- index += (2 + length);
- if (index >= receiveBytes.Length)
- {
- //кончились сообщения
- break;
- }
- }
- }
- }
- catch (Exception ex)
- {
- }
- }
- }
- public class Client
- {
- public static Dictionary<IPAddress, Client> allClients = new Dictionary<IPAddress, Client>();
- IPAddress ipAdress;
- public int port;
- public ClientMeasgeController clientMeasgeController = new ClientMeasgeController();
- public string idClient = "";
- public Client(IPAddress ipAdress)
- {
- this.ipAdress = ipAdress;
- allClients.Add(ipAdress, this);
- }
- public void ReceiveMessage(Byte[] bytes)
- {
- //хз, тут нужно много мозга
- }
- }
- public class Player
- {
- }
- public interface IMessageOnlyLast
- {
- uint idObject;
- byte messageType;
- byte[] date;
- public void Pack();
- public void UnPack();
- }
- public class ClientMeasgeController
- {
- private List<IMessageOnlyLast> onlyLastList = new List<IMessageOnlyLast>();
- public void addMessageOnlyLast(IMessageOnlyLast message)
- {
- List<IMessageOnlyLast> delList = new List<IMessageOnlyLast>();
- foreach (IMessageOnlyLast im in onlyLastList)
- {
- if (message.idObject == im.idObject && message.messageType == im.messageType)
- {
- delList.Add(im);
- }
- onlyLastList.Add(message);
- message.Pack();
- }
- foreach (IMessageOnlyLast imd in delList)
- {
- onlyLastList.Remove(imd);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement