Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. public sealed class ConnectionManager
  2.     {
  3.         TcpListener serverSocket;
  4.         public Hashtable clientsList;
  5.         int idCounter;
  6.  
  7.         private static volatile ConnectionManager instance;
  8.         private static object syncRoot = new Object();
  9.  
  10.  
  11.         public static ConnectionManager Instance
  12.         {
  13.             get
  14.             {
  15.                 if (instance == null)
  16.                 {
  17.                     lock (syncRoot)
  18.                     {
  19.                         if (instance == null)
  20.                             instance = new ConnectionManager();
  21.                     }
  22.                 }
  23.  
  24.                 return instance;
  25.             }
  26.         }
  27.  
  28.  
  29.         private ConnectionManager()
  30.         {
  31.             Int32 port = 8888;
  32.             clientsList = new Hashtable();
  33.             IPAddress localAddr = IPAddress.Parse("127.0.0.1");
  34.             serverSocket = new TcpListener(localAddr, port);
  35.            
  36.  
  37.             serverSocket.Start();
  38.             Console.WriteLine("Server started listening...");
  39.             idCounter = 0;
  40.             while(true)
  41.             {
  42.                 TcpClient clientSocket = serverSocket.AcceptTcpClient();
  43.                 Console.WriteLine("New connection accepted with id " + idCounter);
  44.                 HandleClient hc = new HandleClient(idCounter);
  45.                 clientsList.Add(idCounter++, clientSocket);
  46.                 hc.handle();
  47.  
  48.             }
  49.  
  50.  
  51.         }
  52.  
  53.  
  54.  
  55.  
  56.         public void SendMessage<T>(Message<T> m, int connectionId)
  57.         {
  58.  
  59.            
  60.             NetworkStream clientStream = ((TcpClient)clientsList[connectionId]).GetStream();
  61.             StreamWriter sw = new StreamWriter(clientStream);
  62.             var js = new JsonSerializer();
  63.             js.Serialize(sw, m);
  64.             sw.Flush();
  65.             clientStream.Flush();
  66.            
  67.         }
  68.  
  69.         public void foo()
  70.         {
  71.             Console.WriteLine("korte");
  72.         }
  73.  
  74.  
  75.        
  76.  
  77.  
  78.  
  79.  
  80.  
  81.     }
  82.  
  83.     class HandleClient
  84.     {
  85.         int connectionId;
  86.         public HandleClient(int cId)
  87.         {
  88.             this.connectionId = cId;
  89.  
  90.         }
  91.  
  92.  
  93.         public void handle()
  94.         {
  95.             Thread cThread = new Thread(start);
  96.             cThread.Start();
  97.         }
  98.  
  99.         private void start()
  100.         {
  101.            
  102.             Console.WriteLine("Thread started for client " + connectionId);
  103.             User user = new User()
  104.             {
  105.                 connectionId = connectionId
  106.             };
  107.             Game.Instance.AddUser(user);
  108.            
  109.             /*clientSocket.Close();
  110.             clientsList.Remove(connectionId);
  111.             Console.WriteLine("Thread stopped for client " + connectionId);
  112.             Thread.CurrentThread.Abort();*/
  113.  
  114.  
  115.  
  116.  
  117.         }
  118.  
  119.  
  120.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement