using System; using System.IO; using System.Collections.Generic; using System.Collections; using System.Xml; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; namespace das.sit { // State object for reading client data asynchronously /******************************************************************************/ public class StateObject { /******************************************************************************/ // Client socket. public Socket workSocket = null; // Size of receive buffer. public const int BufferSize = 1024; // Receive buffer. public byte[] buffer = new byte[BufferSize]; // Received data string. public StringBuilder sb = new StringBuilder(); public int hrac; } /******************************************************************************/ public class net { /******************************************************************************/ // Thread signal. public static ManualResetEvent allDone = new ManualResetEvent(false); public static Socket listener; public static int istav = 0; public static Socket s1; public static Socket[] handler_pole = new Socket[6]; public static string[] ip_pole = new string[6]; public static int sitovy_hraci = 0; private static int z_id = 0; public static int id() { return z_id++; } public static void StartListening() { // Data buffer for incoming data. //byte[] bytes = new Byte[1024]; // Establish the local endpoint for the socket. //original kod //IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); //IPAddress ipAddress = ipHostInfo.AddressList[0]; //IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); //poslouchame vsude na portu 9050 IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 9050); // Create a TCP/IP socket. listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and listen try { listener.Bind(localEndPoint); listener.Listen(200); } catch (Exception e) { Console.WriteLine("Nepodarilo se otevrit port, server uz mozna bezi\nchyba: "+e.ToString()); zpravy.log("Nepodarilo se otevrit port, server uz mozna bezi\nchyba: "+e.ToString()); Environment.Exit(1); } } public static void AcceptCallback(IAsyncResult ar) { // Get the socket that handles the client request. Socket listener = (Socket) ar.AsyncState; Socket handler = listener.EndAccept(ar); // Signal the main thread to continue. allDone.Set(); // Create the state object. StateObject state = new StateObject(); state.workSocket = handler; handler_pole[sitovy_hraci] = state.workSocket; zpravy.log("Hrac "+sitovy_hraci+" prihlasen handler ulozen"); state.hrac = sitovy_hraci; string ip = handler_pole[sitovy_hraci].RemoteEndPoint.ToString(); Console.WriteLine(ip); ip_pole[sitovy_hraci] = ip; zpravy.log("Hrac " + sitovy_hraci + " ma ip " + ip_pole[sitovy_hraci]); sitovy_hraci++; handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } public static void testuj_prikaz(string prikaz, string obsah, Socket handler, StateObject state) { if (obsah.IndexOf(prikaz) > -1) { Send(handler, "OK\n"); zpravy.notifikuj(state.hrac, prikaz); state.sb.Remove(0,state.sb.Length); } } public static void testuj_prikaz_s_parametry(string prikaz, string obsah, Socket handler, StateObject state) { string dataprikaz; string parametry = ""; string[] slova = new string[10]; if (obsah.IndexOf(" ") == -1) { return; } try { slova = obsah.Split(' '); } catch { Console.WriteLine("fixme catch\n"); return; } dataprikaz = slova[0]; slova[0] = string.Empty; try { parametry = string.Join(" ", slova).TrimStart(' '); } catch {} if (obsah.IndexOf(prikaz) > -1) { zpravy.log("testuj_prikaz_s_parametry("+obsah+"):testuji "+prikaz+" dostal sem "+dataprikaz+" ma parametry:"+parametry); Send(handler, "OK\n"); zpravy.notifikuj2(state.hrac, prikaz, parametry); state.sb.Remove(0,state.sb.Length); } } public static void zpracuj_prikaz(string obsah, Socket handler, StateObject state) { zpravy.log("zpracuj_prikaz():Nacteno "+obsah.Length+" bytu ze socketu. Data:"+obsah+".\n"); if (obsah.IndexOf("konec_hry") > -1) { state.sb.Remove(0,state.sb.Length); posli_vsem("konec_hry\n"); System.Threading.Thread.Sleep(1000); //cekat na to az se klienti uvolni!!! for(int i = 0; i < net.sitovy_hraci; i++) { try { zpravy.log("Zaviram socket "+i+"."); net.handler_pole[i].Close(); } catch { //fixme neco udelat } } Environment.Exit(0); } if (obsah.IndexOf("test") > -1) { Send(handler, "OK nazdar po testu\n"); state.sb.Remove(0,state.sb.Length); } if (obsah.IndexOf("chat") > -1) { Send(handler, "OK\n"); posli_vsem("prichozi_chat " + obsah); state.sb.Remove(0,state.sb.Length); } testuj_prikaz_s_parametry("pujcit_z_banky", obsah, handler, state); testuj_prikaz_s_parametry("vratit_do_banky", obsah, handler, state); testuj_prikaz_s_parametry("sazim_na_kone", obsah, handler, state); testuj_prikaz_s_parametry("ahoj", obsah, handler, state); testuj_prikaz_s_parametry("prodej_kone", obsah, handler, state); testuj_prikaz_s_parametry("prodej_sluzbu", obsah, handler, state); testuj_prikaz_s_parametry("preved_kone", obsah, handler, state); testuj_prikaz_s_parametry("preved_sluzbu", obsah, handler, state); testuj_prikaz_s_parametry("uloz_hru", obsah, handler, state); testuj_prikaz_s_parametry("nacti_hru", obsah, handler, state); testuj_prikaz_s_parametry("vzdat_se", obsah, handler, state); testuj_prikaz_s_parametry("osvobodit_se", obsah, handler, state); testuj_prikaz("hazim_kostkou", obsah, handler, state); testuj_prikaz("uz_nehazim", obsah, handler, state); testuj_prikaz("akceptuji_stop", obsah, handler, state); testuj_prikaz("akceptuji_distanc", obsah, handler, state); testuj_prikaz("akceptuji_finance", obsah, handler, state); testuj_prikaz("akceptuji_nahodu", obsah, handler, state); testuj_prikaz("zaplacena_prohlidka", obsah, handler, state); testuj_prikaz("zaplaceno_penize", obsah, handler, state); testuj_prikaz("kupuji_trenera", obsah, handler, state); testuj_prikaz("kupuji_dostih", obsah, handler, state); testuj_prikaz("platim_za_trenera", obsah, handler, state); testuj_prikaz("kupuji_kone", obsah, handler, state); testuj_prikaz("kupuji_prepravu", obsah, handler, state); testuj_prikaz("kupuji_staje", obsah, handler, state); testuj_prikaz("platim_za_staj", obsah, handler, state); testuj_prikaz("platim_za_prepravu", obsah, handler, state); testuj_prikaz("pokracuj", obsah, handler, state); } public static void ReadCallback(IAsyncResult ar) { String content = String.Empty; // Retrieve the state object and the handler socket // from the asynchronous state object. StateObject state = (StateObject) ar.AsyncState; Socket handler = state.workSocket; int bytesRead = 0; // Cti data z klientiho socketu try { bytesRead = handler.EndReceive(ar); } catch { // tady nastane timeout vyjimka zpravy.log("ReadCallback(): timeout"); } if (bytesRead > 0) { state.sb.Append(Encoding.ASCII.GetString( state.buffer,0,bytesRead)); content = state.sb.ToString(); zpracuj_prikaz(content, handler, state); handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } } public static void Send(Socket handler, String data) { data = id()+":"+data; byte[] byteData = Encoding.ASCII.GetBytes(data); try { handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler); } catch (Exception e) { zpravy.log("exception Send():"+e.ToString()); } finally { zpravy.log("Send("+z_id+"):"+data); } } public static void posli_vsem(String data) { for (int i = 0; i < sitovy_hraci; i++) { Send (handler_pole[i], data); } } private static void SendCallback(IAsyncResult ar) { zpravy.log("SendCallback("+z_id+"):trying..."); try { Socket handler = (Socket) ar.AsyncState; int bytesSent = handler.EndSend(ar); zpravy.log("SendCallback("+z_id+"):to send "+bytesSent+" bytes ..."); } catch (Exception e) { Console.WriteLine(e.ToString()); zpravy.log("EXEPSNA:\nexception SendCallback("+z_id+"):"+e.ToString()); } finally { zpravy.log("SendCallback("+z_id+"):Sent"); } } }//class }//namespace