private async void Listen() { Listener.Start(); var semaphore = new Semaphore(64, 512); while (Listening) { semaphore.WaitOne(); Socket client; try { semaphore.Release(); client = await Listener.AcceptSocketAsync(); } catch (Exception e) // Exception thrown when socket is closed in Stop() { break; } // pass control to ConnectionHandler.Handle var handler = new ConnectionHandler(this, client); ThreadPool.QueueUserWorkItem((threadContext) => handler.Handle()); } Listener.Stop(); }
public void Start() { NetIncomingMessage msg; //Register message receiving loop in Update. Server.OnUpdate += () => { msg = Server.NetServer.ReadMessage(); if (msg != null) { string[] splitStr = msg.ReadString().Split(':'); if (splitStr[0].Length > 0) { if (msg.MessageType == NetIncomingMessageType.Data && splitStr[0] != "CON" && !Player.GlobalList.ContainsKey(splitStr[1].ToUpper())) { Console.WriteLine(splitStr[0] + ":ERROR:Player does not exist."); ConnectionHandler.AddPlayer(splitStr, msg.SenderConnection); } switch (splitStr[0]) { case "CON": ConnectionHandler.AddPlayer(splitStr, msg.SenderConnection); break; case "MSG": Chat.SortMessage(splitStr); break; case "COM": Command.Sort(splitStr); break; default: Console.WriteLine(splitStr[0]); break; } } Server.NetServer.Recycle(msg); } }; }
/// <summary> /// Creates a new <see cref="ConnectionHandler" /> to connect the client and the server. /// </summary> /// <param name="userId">The user id to link this connection handler with.</param> /// <param name="tcpClient">The TCP connection between this client and the Server.</param> private void CreateConnectionHandler(int userId, TcpClient tcpClient) { connectionHandler = new ConnectionHandler(userId, tcpClient); connectionHandler.MessageReceived += OnMessageReceived; }
/// <summary> /// raj work /// </summary> public void authenticate(Socket Client) //For authentication, check if login is found or not { NetworkStream ns2 = new NetworkStream(Client); byte[] bytes = new byte[1024]; int bytesName = ns2.Read(bytes, 0, bytes.Length); name = Encoding.ASCII.GetString(bytes, 0, bytesName); int bytesNum = ns2.Read(bytes, 0, bytes.Length); string num = Encoding.ASCII.GetString(bytes, 0, bytesNum); if (name.Contains("-Registered") == false) { bool result = login(name, num); if (result == true) { if (!ActiveUsers.ContainsKey(name)) { String s = "Success"; byte[] byteTime = Encoding.ASCII.GetBytes(s); ns2.Write(byteTime, 0, byteTime.Length); ns2.Flush(); ActiveUsers.Add(name, Client); clientJoin = true; ConnectionHandler handler = new ConnectionHandler(Client, this); ThreadPool.QueueUserWorkItem(new WaitCallback(handler.HandleConnection)); } else { String s = "InUsed"; byte[] byteTime = Encoding.ASCII.GetBytes(s); ns2.Write(byteTime, 0, byteTime.Length); ns2.Flush(); } } else { String s = "Failed"; byte[] byteTime = Encoding.ASCII.GetBytes(s); ns2.Write(byteTime, 0, byteTime.Length); ns2.Flush(); } } else { name = name.Split('-')[0]; bool ok = register(name, num); if (ok == true) { String s = "Registered"; byte[] byteTime = Encoding.ASCII.GetBytes(s); ns2.Write(byteTime, 0, byteTime.Length); ns2.Flush(); phoneBook.Add(name, num); ActiveUsers.Add(name, Client); clientJoin = true; ConnectionHandler handler = new ConnectionHandler(Client, this); ThreadPool.QueueUserWorkItem(new WaitCallback(handler.HandleConnection)); } else { String s = "Nope"; byte[] byteTime = Encoding.ASCII.GetBytes(s); ns2.Write(byteTime, 0, byteTime.Length); ns2.Flush(); } } }
public Server() { _serverSocket = ConnectionHandler.CreateListener(); _clients = new List <ConnectedObject>(); }