Facebook
From Serwer, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 254
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Threading;
  7. using System.Linq;
  8.  
  9. public class UdpSrvrSample
  10. {
  11.     private static int game_state = 0;//0-gra czeka na rozpoczecie 1- gra skonczona;
  12.     private static int connected_hosts = 0;//licznik hostow
  13.     private static int times_up = 0;//0-nie minelo jeszcze 10s
  14.     private static Random random = new Random();
  15.     public static int sekretna = random.Next(0, 15);
  16.     public static int time2;
  17.  
  18.     public static void Timer(string h2,string h1)
  19.     {
  20.         int id1 = Convert.ToInt32(h1, 2);
  21.         int id2 = Convert.ToInt32(h2, 2);
  22.         time2 = (((id1 + id2) * 99) % 100) + 30;
  23.        
  24.         while(time2!=0)
  25.         {
  26.             if (time2 >= 10)
  27.             {
  28.             Thread.Sleep(10000);
  29.             time2 = time2 - 10;
  30.             }
  31.                
  32.             else
  33.             {
  34.                 Thread.Sleep(time2 * 1000);
  35.                 time2 = 0;
  36.                 game_state++;//timeout
  37.             }
  38.            
  39.             times_up++;
  40.         }
  41.        
  42.     }
  43.     static byte [] ProtocolBuilder(string operation,string response,string idref,string dop="none")//buduje pakiet
  44.    {
  45.         int pom1 = 0, pom2 = 0, pom3 = 0, pom4 = 0;
  46.        BitArray c = new BitArray(24);
  47.         BitArray g = new BitArray(24);
  48.         byte[] data = new byte[3];
  49.         for (int i=0;i<c.Length;i++)
  50.         {
  51.             if(i<=5)
  52.             {
  53.                 if (operation[pom1] == '0')
  54.                     c[i] = false;
  55.                 else
  56.                     c[i] = true;
  57.                 pom1++;
  58.             }  
  59.             if(i>5 && i<=9)
  60.             {
  61.                 if (response[pom2] == '0')
  62.                     c[i] = false;
  63.                 else
  64.                     c[i] = true;
  65.                 pom2++;
  66.             }
  67.             if(i>9 && i<=17)
  68.             {
  69.                 if (idref[pom3] == '0')
  70.                     c[i] = false;
  71.                 else
  72.                     c[i] = true;
  73.                 pom3++;
  74.             }
  75.             if (i > 17)
  76.             {
  77.                     if(dop=="none")
  78.                     c[i] = false;
  79.                     else
  80.                     {
  81.                     if (dop[pom4] == '0')
  82.                         c[i] = false;
  83.                     else
  84.                         c[i] = true;
  85.                     pom4++;
  86.                     }
  87.                    
  88.             }
  89.                
  90.         }
  91.         for (int i = 0; i < c.Length; i++)
  92.         {
  93.             g[g.Length - i-1] = c[i];
  94.         }
  95.         g.CopyTo(data, 0);
  96.         Array.Reverse(data, 0, data.Length);
  97.         return data;
  98.    }
  99.  
  100.    
  101.     public static string ToBin(int value, int len)//int do binarki
  102.     {
  103.         return (len > 1 ? ToBin(value >> 1, len - 1) : null) + "01"[value & 1];
  104.     }
  105.     public static string[] DecodePacket(byte[] c)//funkcja odbiera pobrane inty i zamienia je na kod binarny
  106.     {
  107.         string h = "";
  108.         string [] a=new string[3];
  109.         for (int i = 0; i < c.Length;i++)
  110.         {
  111.             h=h+ ToBin(c[i], 8);//dopelnia do 8 bitow
  112.  
  113.         }
  114.         for(int i=0;i<h.Length;i++)// podzial pakietu na pole operacyjne, pole odpowiedzi i identyfikator
  115.         {//dopelnienie jest nieistotne wiec jest ignorowane
  116.             if (i <= 5)
  117.                 a[0] =a[0]+ h[i];
  118.             if (i > 5 && i <= 9)
  119.                 a[1] = a[1] + h[i];
  120.             if (i > 9 && i <= 17)
  121.                 a[2] = a[2] + h[i];
  122.         }
  123.         return a;
  124.     }
  125.    
  126.     public static string RandomString(int length)// funckja losujaca id sesji
  127.     {
  128.         string liczba;
  129.         if(connected_hosts==1)
  130.         {
  131.             int h = random.Next(0, 122);
  132.             liczba = ToBin(h,8);
  133.         }
  134.         else
  135.         {
  136.             int h = random.Next(122, 254);
  137.             liczba = ToBin(h, 8);
  138.         }
  139.         return liczba;
  140.     }
  141.     static byte [] TellAction(byte[] c,ref string h)//ustalanie akcji na podstawie pola operacji
  142.     {
  143.        
  144.         byte[] bytes = new byte[3];
  145.         string[] packet = new string[3];
  146.         packet = DecodePacket(c);
  147.         switch (packet[0])
  148.         {
  149.             case "000000":
  150.                 {
  151.                     h = RandomString(8);
  152.                     bytes = ProtocolBuilder("100000", "0000", h);
  153.  
  154.                     break;
  155.                 }
  156.             case "010000":
  157.                 {
  158.                     string l = ToBin(sekretna, 4);
  159.                     if (l == packet[1])
  160.                     {
  161.                         bytes = ProtocolBuilder("110000", "0000", h);
  162.                         game_state = 1;
  163.                     }
  164.  
  165.                     else
  166.                         bytes = ProtocolBuilder("001000", "0000", h);
  167.                     break;
  168.                 }
  169.             case "110100":
  170.                 {
  171.                     //connected_hosts--;
  172.                     //game_state = 1;
  173.                     break;
  174.                 }
  175.         }
  176.         return bytes;
  177.     }
  178.     public static void SecondChannel(int port,  string h)//kanal w ktorym do serwer inicjuje komunikacje
  179.     {
  180.         IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port+10);
  181.         UdpClient newsock = new UdpClient(ipep);
  182.         IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0);
  183.         ReceivePacket(ref newsock, ref sender, ref h);
  184.         while (true)
  185.         {
  186.             if(game_state == 1)// wysylanie pakietu informujacego o koncu gry
  187.             {
  188.                 byte[] newArray = new byte[3];
  189.                 if(time2==0)
  190.                 {
  191.                 newArray = ProtocolBuilder("111000", "0000", h);//timeout
  192.                 }
  193.                 else
  194.                 {
  195.                    newArray = ProtocolBuilder("111000", "1111", h);//liczba zostala zgadnieta
  196.                 }          
  197.                 SendPacket(newsock, newArray, sender);
  198.             }
  199.             if(times_up==1 && game_state==0)
  200.             {
  201.                 byte[] newArray = new byte[3];
  202.                 string czas = ToBin(time2,10);
  203.                 string part1 = "", part2 = "";
  204.                 for (int i = 0; i <= 3; i++)
  205.                     part1 = part1+czas[i];
  206.                 for (int i = 4; i <10; i++)
  207.                     part2 = part2 + czas[i];
  208.                
  209.  
  210.                 newArray = ProtocolBuilder("101000", part1, h, part2);    
  211.                 SendPacket(newsock, newArray, sender);
  212.                 Console.WriteLine("Wysylam pakiet czasu");
  213.                 times_up = 0;
  214.             }
  215.         }
  216.  
  217.     }
  218.     public static void CallToChildThread(int port,ref string h)
  219.     {
  220.         int g=0;
  221.         byte[] data = new byte[3];
  222.         string c = h;
  223.         Thread thread3 = new Thread(() => SecondChannel(port, c));
  224.         IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
  225.         UdpClient newsock = new UdpClient(ipep);
  226.         IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0);
  227.         byte[] bytes = new byte[3];  
  228.         data = ReceivePacket(ref newsock, ref sender, ref h);
  229.        
  230.  
  231.         bytes=TellAction(data, ref h);
  232.         SendPacket(newsock, bytes, sender);
  233.         connected_hosts++;
  234.         thread3.Start();
  235.         while (true)
  236.         {
  237.             if(g==0 && connected_hosts==2)//czeka na polaczenie obu hostow po czym wysyla informacje o rozpoczeciu rozgrywki
  238.             {    
  239.                 g = 1;
  240.                 byte[] newArray = new byte[3];
  241.                 newArray = ProtocolBuilder("100100", "0000", h);            
  242.                 SendPacket(newsock, newArray, sender);
  243.             }
  244.             if (g == 1 && connected_hosts == 2)//pobiera liczbe wyslana przez klienta i odsyla prawda/falsz
  245.             {
  246.                 data = ReceivePacket(ref newsock, ref sender, ref h);
  247.                 bytes = TellAction(data, ref h);
  248.                 SendPacket(newsock, bytes, sender);
  249.             }
  250.            
  251.         }
  252.  
  253.        
  254.     }
  255.     static void SendPacket(UdpClient klient,byte [] bytes,IPEndPoint sender)//wysyla dane po czym czeka na potwierdzenie
  256.     {
  257.         klient.Send(bytes, bytes.Length, sender);
  258.         klient.Receive(ref sender);
  259.  
  260.     }
  261.     static byte[] ReceivePacket(ref UdpClient klient,ref IPEndPoint sender,ref string h )//zapisuje wyslane dane i potwierdza ich odebranie
  262.     {
  263.         byte[] ACK = new byte[3];
  264.         byte[] data = new byte[3];
  265.         data = klient.Receive(ref sender);
  266.         ACK= ProtocolBuilder("111111", "0000", h);
  267.        
  268.         klient.Send(ACK, ACK.Length, sender);
  269.         return data;
  270.     }
  271.     public static void Main()
  272.     {
  273.         byte[] data = new byte[3];
  274.  
  275.         string h1 = "00000000";
  276.         string h2 = "00000000";
  277.         Console.Write("Losowa liczba to: ");
  278.         Console.Write(sekretna);
  279.  
  280.         Thread thread2 = new Thread(() => CallToChildThread(9050,ref h1));//po jednym watku dla kazdego klienta
  281.         Thread thread3 = new Thread(() => CallToChildThread(9051,ref h2));
  282.         thread2.Start();
  283.         thread3.Start();
  284.         while(true)//zatrzymuje watek do momentu polaczenia dwoch klientow, niesamowicie oszczedne rozwiazanie
  285.         {
  286.             if (connected_hosts == 2)
  287.                 break;
  288.         }
  289.  
  290.         Thread timer = new Thread(() => Timer( h1, h2));
  291.         timer.Start();
  292.  
  293.        
  294.         int a = Console.Read();
  295.            
  296.        
  297.  
  298.     }
  299. }