public static void ManageAnonymous(object socket) { AnonymousThread client = (AnonymousThread)socket; try { var clientStream = client.Client.GetStream(); if (Program.ServerState == 0) { SocketUtilities.SendShutdown(clientStream, "The server is shutting down", "0"); clientStream.Close(); return; } string smessage = SocketUtilities.RecieveMessage(clientStream, SocketUtilities.RecieveMessageLength(clientStream)); if (smessage == null) { Program.Disconnect(ThreadType.AnonymousThread, client.Guid); return; } CommandParameterPair message = MessageUtilites.DecodeMessage(smessage); var builder = new StringBuilder(); foreach (string i in message.Parameters) { builder.Append(String.Format("{0} with", i)); } ConsoleUtilities.PrintCommand(String.Format("{0} has sent command {1} with parameters {2}", client.Guid, message.Command, builder)); InitializeCommand execute; if (Program.InitializeCommands.TryGetValue(message.Command, out execute)) { execute(client.Client, message.Parameters); } else { SocketUtilities.SendError(clientStream, "Unable to find command (Concurrency Issues)"); clientStream.Close(); } } catch (SocketException e) { if (client.Client.Client.RemoteEndPoint == null) { Console.WriteLine("A severe error has occurred with the clientStream. Data: {0}", e.Data); return; } Console.WriteLine("Error: A clientStream has disconnected from {0}. ", (client.Client.Client.RemoteEndPoint as IPEndPoint).Address); } catch (IOException e) { if (client.Client.Client.RemoteEndPoint == null) { Console.WriteLine("A severe error has occurred with the clientStream. Data: {0}", e.Data); return; } Console.WriteLine("Error: A clientStream has disconnected from {0}. ", (client.Client.Client.RemoteEndPoint as IPEndPoint).Address); } }
public static void RequestInfo(TcpClient tcpClient, params string[] value) { var stream = tcpClient.GetStream(); if (value.Length != 0) { SocketUtilities.SendInvalid(stream, "INFOREQ should not send parameters"); tcpClient.Close(); return; } SocketUtilities.SendCommand(stream, new CommandParameterPair("INFORESP", JsonConvert.SerializeObject(Program.ServerProperties.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)))); Thread thread = new Thread(AcceptClientManagement.ManageAnonymous); AnonymousThread athread = new AnonymousThread(tcpClient.Client.RemoteEndPoint.ToString(), tcpClient); thread.Start(athread); Program.AnonymousThreads.TryAdd(tcpClient.Client.RemoteEndPoint.ToString(), thread); }