Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- namespace dots_server
- {
- class HttpProcessor {
- private games gm;
- private Socket s;
- private BufferedStream bs;
- private StreamReader sr;
- private StreamWriter sw;
- private String method; // ?
- private String url;
- private String protocol; // ?
- private Hashtable hashTable;
- public HttpProcessor(Socket s,games g) {
- this.s = s;
- this.gm=g;
- hashTable = new Hashtable();
- }
- public void process() {
- NetworkStream ns = new NetworkStream(s, FileAccess.ReadWrite);
- bs = new BufferedStream(ns);
- sr = new StreamReader(bs);
- sw = new StreamWriter(bs);
- parseRequest();
- readHeaders();
- writeURL();
- s.Shutdown(SocketShutdown.Both);
- ns.Close();
- }
- public void parseRequest() {
- String request = sr.ReadLine();
- string[] tokens = request.Split(new char[]{' '});
- method = tokens[0];
- url = tokens[1];
- protocol = tokens[2];
- }
- public void readHeaders() {
- String line;
- while((line = sr.ReadLine()) != null && line != "") {
- string[] tokens = line.Split(new char[]{':'});
- String name = tokens[0];
- String valuee = "";//
- for(int i = 1; i < tokens.Length; i++) {
- valuee += tokens[i];//
- if(i < tokens.Length - 1) tokens[i] += ":";
- }
- // Console.WriteLine(">>"+name+">>"+valuee); // HTTPVIEW
- hashTable[name] = valuee;//
- }
- }
- public void writeURL() {
- try {
- //URL PART :D :P ;)!! ANYTHING CAN BE DONE HERE YUPPY!!
- // FileStream fs = new FileStream(url.Substring(1), FileMode.Open, FileAccess.Read);
- //test :)!
- if(url.Substring(1).Equals("favicon.ico")) return;
- String res=gm.showgame(url.Substring(1),(string)hashTable["Host"]);
- byte[] bout=System.Text.Encoding.ASCII.GetBytes(res);
- byte[] bout2=System.Text.Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\n"+
- "Allow: GET\n"+
- "MIME-Version: 1.0\n"+
- "Server : HMJ Basic HTTP Server\n"+
- "Content-Type: "+"text/html" + "\n"+
- "Content-Length: "+ bout.Length +
- "\n\n"+res);
- writeSuccess(bout2.Length);
- BufferedStream bs2 = new BufferedStream(new MemoryStream(bout2));
- byte[] bytes = new byte[4096];
- int read;
- while((read = bs2.Read(bytes, 0, bytes.Length)) != 0) {
- bs.Write(bytes, 0, read);
- }
- bs2.Close();
- } catch(FileNotFoundException) {
- writeFailure();
- sw.WriteLine("File not found: " + url);
- }
- sw.Flush();
- }
- public void writeSuccess(int fileLength) {
- ///*
- //<<<<
- sw.Write(
- "HTTP/1.0 200 OK\n"+
- "Allow: GET\n"+
- "MIME-Version: 1.0\n"+
- "Server : HMJ Basic HTTP Server\n"+
- "Content-Type: "+"text/html" + "\n"+
- "Content-Length: "+ fileLength +
- "\n\n" );
- // */
- //sw.WriteLine("HTTP/1.0 200 OK"); //>>>>>
- //sw.WriteLine("Connection: close"); //>>>>>
- //sw.WriteLine(); //>>>>
- }
- public void writeFailure() {
- sw.WriteLine("HTTP/1.0 404 File not found");
- sw.WriteLine("Connection: close");
- sw.WriteLine();
- }
- }
- public class HttpServer {
- // ============================================================
- // Data
- protected int port;
- // ============================================================
- // Constructor
- public HttpServer() : this(80) {
- }
- public HttpServer(int port) {
- this.port = port;
- }
- // ============================================================
- // Listener
- private games gm;
- public void listen() {
- //Socket listener = new Socket(0, SocketType.SockStream, ProtocolType.ProtTCP); //>>>>>>
- //Socket listener = new Socket(0, SocketType.Stream, ProtocolType.Tcp);
- gm=new games();
- TcpListener listener = new System.Net.Sockets.TcpListener(IPAddress.Parse("127.0.0.1"),1290); //<<<<
- //IPAddress ipaddress = new IPAddress("127.0.0.1"); //>>>>>>
- //IPEndPoint endpoint = new IPEndPoint(ipaddress, port); //>>>>>
- //listener.Bind(endpoint); //>>>>
- //listener.Blocking = true; //>>>>>
- //listener.Listen(-1); />>>>>>
- listener.Start(); //<<<<<
- while(true) {
- //Socket s = listener.Accept(); //<<
- Socket s = listener.AcceptSocket();
- HttpProcessor processor = new HttpProcessor(s,gm);
- Thread thread = new Thread(new ThreadStart(processor.process));
- thread.Start();
- }
- }
- // ============================================================
- // Main
- public static int Main(String[] args) {
- HttpServer httpServer;
- if(args.GetLength(0) > 0) {
- //httpServer = new HttpServer(args[0].ToUInt16()); // >>>>>>>>
- httpServer = new HttpServer(1290); //<<<<<
- } else {
- httpServer = new HttpServer();
- }
- Thread thread = new Thread(new ThreadStart(httpServer.listen));
- thread.Start();
- return 0;
- }
- }
- }
- //
- class games
- {
- Dictionary<string, int> map = new Dictionary<string, int>();
- public int N=0,H=10,W=10;
- public String[] games_nam=new String[1000];
- public Hashtable p1=new Hashtable();//player1 hosts/IPs
- public Hashtable p2=new Hashtable();//player2 hosts/IPs
- public bool[] free=new bool[1000];
- public int[,,] g=new int[1000,50,50];
- public string[,,] cell_owner=new string[1000,50,50]; //cell owner ;P1 or P2 via IP
- private int[] dC=new int[]{0,0,-1,1};
- private int[] dR=new int[]{-1,1,0,0};
- string just2test="UDLR";
- private string[] lastPlay=new string[1000];
- private bool playAgain=false;
- private int floodFill_game_ind;
- private bool[,] vis=new bool[50,50];
- private bool pass=true;
- private string floodFill_owner;
- private StreamWriter sw=new StreamWriter("log.txt");
- /*
- V= 0001 = up=1<<0
- _________________
- 1<<2=left|<0100 |
- | >1000| =right=1<<3
- |________________|
- ^ 0010=down=1<<1
- */
- public games()
- {
- playAgain=false;
- int i,j,k;
- for(i=0;i<1000;i++)
- {
- free[i]=true;
- lastPlay[i]="";
- for(j=0;j<H;j++)
- for(k=0;k<W;k++)
- {
- g[i,j,k]=0;
- cell_owner[i,j,k]="";
- }
- }
- }
- public string homee()
- {
- string txt="<html><head><title>Available Games:</title></head><body><p aligen='center'>Available Games:<br><table align='center'>";
- for(int i=0;i<N;i++)
- if(free[i])
- txt+="<tr><td align='center'><a href='/"+games_nam[i]+"'><font size=40>"+games_nam[i]+"</font></a></td></tr>";
- txt+="</table>"+"<br><a href='javascript:location.href=prompt(\"Game Name:\");'>New Game!</a><br></p>"+"</body></html>";
- return txt;
- }
- public string showgame(string nam,string host)
- {
- if(nam=="") return homee();
- if( nam.IndexOf('?')>-1)
- if(
- map.ContainsKey( nam.Substring(0,nam.IndexOf('?')) )
- )
- return play(
- nam.Substring(0,nam.IndexOf('?'))
- ,host
- , int.Parse(nam.Substring(nam.IndexOf('?')+1,nam.IndexOf(',')-nam.IndexOf('?')-1 ) )
- ,int.Parse(nam.Substring(nam.IndexOf(',')+1,nam.LastIndexOf(',')-nam.IndexOf(',')-1 ))
- ,int.Parse(nam.Substring(nam.LastIndexOf(',')+1)));
- string res="";
- int i,j;
- if(!map.ContainsKey(nam)) //New Game ya3ny & this is player1 :)
- {
- if(nam.IndexOf('?')>-1) return "<html><head><title>'?'!</title></head><body><script>alert('Game name can't include \"?\"! :-(');</script></body></html>";
- if(N+1>=1000) return "<html><head><title>'?'!</title></head><body><script>alert('sorry,memory limit! :-(');</body></script></html>";
- map.Add(nam,N);
- free[N]=true;
- games_nam[N++]=nam;
- if(!p1.ContainsKey(host))
- {
- sw.WriteLine("New Player1:\t"+host+"@"+nam+"\t"+new DateTime().ToShortTimeString());
- Console.WriteLine("New Player1:\t"+host+"@"+nam+"\t"+new DateTime().ToShortTimeString());
- p1.Add(host,true);
- }
- }
- else //2nd player & the game is full!
- {
- if(!p1.ContainsKey(host) && !p2.ContainsKey(host))
- {
- // Console.WriteLine(">>>>>>>PLAYER 2<<<<<<<<<<=>"+host);
- free[map[nam]]=false;
- sw.WriteLine("New Player2:\t"+host+"@"+nam+"\t"+new DateTime().ToShortTimeString());
- Console.WriteLine("New Player2:\t"+host+"@"+nam+"\t"+new DateTime().ToShortTimeString());
- p2.Add(host,true);
- }
- }
- // if(p1.ContainsKey(host)) color="Blue";
- // else color="red";
- /*
- V= 0001 = up=1<<0
- _________________
- 1<<2=left|<0100 |
- | >1000| =right=1<<3
- |________________|
- ^ 0010=down=1<<1
- */
- int ind=map[nam],r1=0,r2=0;
- string color="";
- res="<html><head><title>"+games_nam[ind]+"</title></head><body><table>";
- for(i=0;i<H;i++)
- {
- res+="<tr>";
- for(j=0;j<W;j++)
- {
- if(p1.ContainsKey(cell_owner[ind,i,j]) && cell_owner[ind,i,j].Equals(cell_owner[ind,i,j]))
- {color="blue";r1++;}
- else if(p2.ContainsKey(cell_owner[ind,i,j]) && cell_owner[ind,i,j].Equals(cell_owner[ind,i,j]))
- {color="red";r2++;}
- else color="white";
- res+="<td><table>";
- if( (g[ind,i,j]&(1<<0)) >0 ) //up
- res+="<tr><td bgcolor='"+color+"' width='70%'>====</td><td bgcolor='"+color+"' width='30%'>O</td></tr>";
- else
- res+="<tr><td bgcolor='"+color+"' width='70%'><a href='/"+games_nam[map[nam]]+"?"+i+","+j+","+(1<<0)+"'>====</a></td><td bgcolor='"+color+"' width='30%'>0</td></tr>";
- if( (g[map[nam],i,j]&(1<<3)) >0 ) //right
- res+="<tr><td bgcolor='"+color+"' width='70%'></td><td bgcolor='"+color+"' width='30%'>||</td></tr>";
- else
- res+="<tr><td bgcolor='"+color+"' width='70%'></td><td bgcolor='"+color+"' width='30%'><a href='/"+games_nam[map[nam]]+"?"+i+","+j+","+(1<<3)+"'>||</a></td></tr>";
- res+="</table></td>";
- }
- res+="</tr>";
- }
- res+="</table><p aligen='center'>P1:"+r1+"<br>P2:"+r2+"</p><script>setTimeout('location.href=\""+games_nam[ind]+"\"; ',5000);</script></body></html>";
- return res;
- }
- /*
- V= 0001 = up=1<<0
- _________________
- 1<<2=left|<0100 |
- | >1000| =right=1<<3
- |________________|
- ^ 0010=down=1<<1
- */
- public string play(string nam,string host,int x,int y,int v)
- {
- if(!map.ContainsKey(nam)) //set a new game?!
- return homee();
- //check this host was last player or not!..HERE<
- floodFill_game_ind=map[nam];
- if(lastPlay[floodFill_game_ind].Equals(host) && (!playAgain))
- return "<html><head><title>re-directing...</title></head><body><script>alert('Plz w8 4 other Player!');location.href='/"+nam+"';</script></body></html>";
- if(playAgain) playAgain=false;
- sw.WriteLine("\tPLAY \t"+host+"\t"+x+","+y+","+(v==8 ? "L":"U") );
- Console.WriteLine("\tPLAY \t"+host+"\t"+x+","+y+","+(v==8 ? "L":"U") );
- //handle other near cells opening
- g[floodFill_game_ind,x,y] |=v;
- if( (v&(1<<0))>0 && x-1>=0) //up
- g[floodFill_game_ind,x-1,y] |=(1<<1); //upper side is closed
- if( (v&(1<<2))>0 && y-1>=0) //left
- g[floodFill_game_ind,x,y-1] |=(1<<3); //leftside is closed
- if( (v&(1<<3))>0 && y+1<W) //right
- g[floodFill_game_ind,x,y+1] |=(1<<2); //rightside is closed
- if( (v&(1<<1))>0 && x+1<H) //down
- g[floodFill_game_ind,x+1,y] |=(1<<0); //bottom is closed
- /*
- for(int i=0;i<5;i++)
- {
- for(int j=0;j<5;j++)
- {
- for(int k=0;k<4;k++)
- if( (g[floodFill_game_ind,i,j]&(1<<k)) >0)
- Console.Write(just2test[k]+"");
- else
- Console.Write("_");
- Console.Write("\t");
- }
- Console.WriteLine();
- }
- // */
- floodFill_game_ind=map[nam];
- floodFill_owner=host;
- vis=new bool[50,50];
- Console.WriteLine("START FF :)...");
- pass=true;
- floodFill(x,y);
- // IFF it doesn't exceed the boundaries,then this region exists :)
- if(pass)
- {
- playAgain=true;
- //Make him play 1 more time...HERE<
- vis=new bool[50,50];
- set_owner(x,y);
- Console.WriteLine("NEW REGION...The Happy END :)!");
- }
- for(int i=0;i<4;i++)
- {
- vis=new bool[50,50];
- this.pass=true;
- floodFill(x+dR[i],y+dC[i]);
- // IFF it doesn't exceed the boundaries,then this new region exists :)
- if(pass)
- {
- playAgain=true;
- //Make him play 1 more time...HERE<
- vis=new bool[50,50];
- set_owner(x+dR[i],y+dC[i]);
- Console.WriteLine("NEW REGION...The Happy END :)!");
- }
- }
- lastPlay[floodFill_game_ind]=host;
- return showgame(nam,host);
- }
- /*
- private bool FFset(int x,int y)
- {
- if(x<0 || y<0 || x>=H || y>=W)
- return false;
- if(vis[x,y]) return true;
- vis[x,y]=true;
- cell_owner[floodFill_game_ind,x,y]=floodFill_owner;
- for(int i=0;i<4;i++)
- if( (g[floodFill_game_ind,x,y]&(1<<i)) ==0)
- if(!FFset(x+dR[i],y+dC[i]))
- {
- cell_owner[floodFill_game_ind,x,y]="";
- return false;
- }
- return true;
- }
- */
- private void floodFill(int x,int y)
- {
- if(x<0 || y<0 || x>=H || y>=W)
- {
- pass=false;
- return;
- }
- if(vis[x,y]) return;
- vis[x,y]=true;
- for(int i=0;i<4;i++)
- if( (g[floodFill_game_ind,x,y]&(1<<i)) ==0)
- floodFill(x+dR[i],y+dC[i]);
- }
- private void set_owner(int x,int y)
- {
- //useless condition :P!;it wouldn't be a region iff it floods to the outer boundries!
- if(x<0 || y<0 || x>=H || y>=W) return;
- if(vis[x,y]) return;
- vis[x,y]=true;
- if(!cell_owner[floodFill_game_ind,x,y].Equals("")) return;
- cell_owner[floodFill_game_ind,x,y]=floodFill_owner;
- for(int i=0;i<4;i++)
- if( (g[floodFill_game_ind,x,y]&(1<<i)) ==0) //Avoid walls
- set_owner(x+dR[i],y+dC[i]);
- }
- }
Add Comment
Please, Sign In to add comment