Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Runtime.InteropServices;
  8. using System.Threading;
  9.  
  10.  
  11. namespace NetMoveCwS
  12. {
  13.     [StructLayout(LayoutKind.Sequential, Pack = 1)]
  14.     //------------------------------------//
  15.     //------ Struct for sending ----------//
  16.     //------------------------------------//
  17.  
  18.     public struct SendData
  19.     {
  20.         public int id_player;
  21.         public int x;
  22.         public int y;
  23.         public int count_player;
  24.         public int bird;
  25.         public int reserved1;
  26.         public int reserved2;
  27.  
  28.         public byte[] ToBytes()
  29.         {
  30.             Byte[] bytes = new Byte[Marshal.SizeOf(typeof(SendData))];
  31.             GCHandle pinStructure = GCHandle.Alloc(this, GCHandleType.Pinned);
  32.             try
  33.             {
  34.                 Marshal.Copy(pinStructure.AddrOfPinnedObject(), bytes, 0, bytes.Length);
  35.                 return bytes;
  36.             }
  37.             finally
  38.             {
  39.                 pinStructure.Free();
  40.             }
  41.         }
  42.     }
  43.  
  44.     public class NetServer
  45.     {
  46.  
  47.         public Socket Handle;
  48.         public const int MAX_PLAYERS = 12;
  49.         public Socket[] PlayersSocketHandles = new Socket[MAX_PLAYERS];
  50.         public bool[] PlayersActive = new bool[MAX_PLAYERS];
  51.         public bool[] PlayersInitilized = new bool[MAX_PLAYERS];
  52.         public int[] PlayersID = new int[MAX_PLAYERS];
  53.  
  54.         public int PlayersActiveCount = 0;
  55.         public bool Started = false;
  56.         public IPAddress[] IP4HostList;
  57.  
  58.         IPAddress ipAddr;
  59.         IPEndPoint ipEndPoint;
  60.         Socket sListener;
  61.          
  62.  
  63.         Thread ListenThread;
  64.  
  65.  
  66.         public SendData[] PlayersSendData = new SendData[MAX_PLAYERS];
  67.  
  68.         public void Start(string addr)
  69.         {
  70.             ipAddr = IPAddress.Parse(addr);
  71.             ipEndPoint = new IPEndPoint(ipAddr, 11000);
  72.             sListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  73.             sListener.Bind(ipEndPoint);
  74.  
  75.             ListenThread = new Thread(new ThreadStart(ListenRoutine));
  76.             ListenThread.Start();
  77.             Started = true;
  78.         }
  79.  
  80.         private void ListenRoutine()
  81.         {
  82.            
  83.             while (true)
  84.             {
  85.  
  86.                 if (PlayersActiveCount < MAX_PLAYERS)
  87.                 {
  88.                     sListener.Listen(10);
  89.                     PlayersSocketHandles[PlayersActiveCount] = sListener.Accept();
  90.                     PlayersSocketHandles[PlayersActiveCount].Blocking = false;
  91.                     PlayersActive[PlayersActiveCount] = true;
  92.                     PlayersActiveCount++;
  93.                 }
  94.                 else
  95.                 {
  96.                     break;
  97.                 }
  98.             }
  99.         }
  100.  
  101.         public void Send(Socket PlayerSocket, SendData sd)
  102.         {
  103.             byte[] bytes = new byte[Marshal.SizeOf(typeof(SendData))];
  104.             if (PlayerSocket != null)
  105.                 if (PlayerSocket.Connected)
  106.                 {
  107.                     if (PlayerSocket.Poll(0, SelectMode.SelectWrite))
  108.                     {
  109.                         PlayerSocket.Send(sd.ToBytes());
  110.                     }
  111.                 }
  112.         }
  113.  
  114.         public void SendForAll()
  115.         {
  116.             // По  кол-ву активных игроков
  117.             for (int i = 0; i < PlayersActiveCount; i++)
  118.             {
  119.                 // если игрок существует
  120.                 if (PlayersActive[i])
  121.                 {
  122.                     if (PlayersSocketHandles[i] != null)
  123.                         if (PlayersSocketHandles[i].Connected)
  124.                         {
  125.                             // отошлем ему данные других активных игроков
  126.                             for (int j = 0; j < PlayersActiveCount; j++)
  127.                             {
  128.                                 PlayersSendData[i].id_player = PlayersID[i];
  129.                                 PlayersSendData[i].count_player = PlayersActiveCount;
  130.  
  131.                                 Send(PlayersSocketHandles[i], PlayersSendData[j]);
  132.                             }
  133.                         }
  134.                 }
  135.             }
  136.         }
  137.  
  138.         public void Receive(Socket PlayerSocket, out SendData sd)
  139.         {
  140.             sd = new SendData();
  141.  
  142.             byte[] bytes = new byte[Marshal.SizeOf(typeof(SendData))];
  143.  
  144.             if (PlayerSocket != null)
  145.                 if (PlayerSocket.Connected)
  146.                     if (PlayerSocket.Poll(0, SelectMode.SelectRead))
  147.                     {
  148.                         int bytesRec = PlayerSocket.Receive(bytes);
  149.  
  150.  
  151.                         sd.id_player = BitConverter.ToInt32(bytes, 0);
  152.                         sd.x = BitConverter.ToInt32(bytes, 4);
  153.                         sd.y = BitConverter.ToInt32(bytes, 8);
  154.                         sd.count_player = BitConverter.ToInt32(bytes, 12);
  155.                         sd.bird = BitConverter.ToInt32(bytes, 16);
  156.  
  157.                     }
  158.         }
  159.  
  160.         public void ReceiveFromAll()
  161.         {
  162.             for (int i = 0; i < PlayersActiveCount; i++)
  163.             {
  164.                 if (PlayersActive[i])
  165.                 {
  166.                     if (PlayersSocketHandles[i] != null)
  167.                     if (PlayersSocketHandles[i].Connected)
  168.                     {
  169.                         SendData sd = new SendData();
  170.  
  171.                         while (PlayersSocketHandles[i].Available > 1)
  172.                         {
  173.                             Receive(PlayersSocketHandles[i], out sd);
  174.                         }
  175.  
  176.                        
  177.                         PlayersSendData[i] = sd;
  178.  
  179.                         // Если это первый пакет от игрока. Инициализация ID
  180.                         if (PlayersInitilized[i] == false)
  181.                         {
  182.                             Random rnd = new Random((int)DateTime.Now.Ticks);
  183.                             PlayersID[i] = rnd.Next(0, 1000);
  184.                             PlayersInitilized[i] = true;
  185.                         }
  186.  
  187.                     }
  188.                 }
  189.  
  190.             }
  191.        
  192.         }
  193.  
  194.         // Собирает все ip4 адреса в массив IP4HostList
  195.         public void LoadIPv4()
  196.         {
  197.             IPHostEntry ipHost = Dns.GetHostEntry(string.Empty);
  198.             //IPAddress ipAddr = null;
  199.  
  200.             int ip4Count  = 0;
  201.  
  202.             for (int i = 0; i < ipHost.AddressList.Length; i++)
  203.             {
  204.                 if (ipHost.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  205.                 {
  206.                     ip4Count++;
  207.                 }
  208.             }
  209.  
  210.             IP4HostList = new IPAddress[ip4Count];
  211.             int j = 0;
  212.  
  213.             for (int i = 0; i < ipHost.AddressList.Length; i++)
  214.             {
  215.                 if (ipHost.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  216.                 {
  217.                    
  218.                     IP4HostList[j] = ipHost.AddressList[i];
  219.                     j++;
  220.                 }
  221.             }
  222.         }
  223.  
  224.         public int GetIPv4Count()
  225.         {
  226.  
  227.             return IP4HostList.Length;      
  228.         }
  229.  
  230.         public string GetIPv4(int index)
  231.         {
  232.             if (index < IP4HostList.Length)
  233.             {
  234.                 return IP4HostList[index].ToString();
  235.             }
  236.             return "";
  237.         }
  238.  
  239.         public void ListenerThread()
  240.         {
  241.        
  242.         }
  243.  
  244.  
  245.     }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement