private void HandleGetUserName(Client client, MessageData messageData) { Console.WriteLine("Handle User Name Register."); try { // Register the name _server.ClientHandleToUserName.Add(client.ClientHandle, (string)messageData.name); MessageData msg = new MessageData(); msg.handle = messageData.handle; msg.id = 1; msg.name = messageData.name; msg.response = false; msg.message = String.Format("[{0}] has joined and says \'{1}\'.", messageData.name, messageData.message); IMessageImpl impl = MessageImplFactory.Instance().MakeMessageImpl((int)MessageImplFactory.MessageFactoryTypesEnum.GLOBAL_MSG_TYPE, client.ClientHandle); if (impl != default(IMessageImpl)) { _server.MessageHandler.Handle(client, msg, impl, _server); //HandleGlobalMessageSendAsync(client, msg); } } catch (Exception e) { Console.WriteLine("Client already registered a Name?: " + e.Message); } }
private bool HandleClientExit(Client client, MessageData messageData) { bool handleExit = false; try { MessageData msg = new MessageData(); msg.handle = messageData.handle; msg.id = 1; msg.name = messageData.name; msg.response = false; msg.message = String.Format("[{0}] has left.", messageData.name); IMessageImpl impl = MessageImplFactory.Instance().MakeMessageImpl((int)MessageImplFactory.MessageFactoryTypesEnum.GLOBAL_MSG_TYPE, client.ClientHandle); if (impl != default(IMessageImpl)) { handleExit = _server.MessageHandler.Handle(client, msg, impl, _server); } if (handleExit) { // Remove client handle record keeping var remHandle = _server.ClientHandleToUserName.Remove(client.ClientHandle); // Remove the client impls var remImpl = MessageImplFactory.Instance().RemoveClient(client.ClientHandle); // Remove the client object var remClient = ClientStore.RemoveClient(client.ClientHandle); return(remHandle && remImpl && remClient); } } catch (Exception ex) { Console.WriteLine("Client Handle Remove Exception: " + ex.Message); handleExit = false; } return(handleExit); }
static void Main(string[] args) { ParseArgs(args); //TaskServer server = null; MessageServer svr = null; CliServLib.DefaultImpl.TaskServer tSvr = null; try { if (useTestObjs) { tSvr = new CliServLib.DefaultImpl.TaskServer(useLocalhost); } else { //server = new TaskServer(); svr = new MessageServer(useLocalhost); svr.MessageFactory = MessageImplFactory.Instance(); } } catch (Exception e) { Console.WriteLine("Server Exception: " + e.Message); return; } ConsoleKeyInfo keyInfo = new ConsoleKeyInfo(); //while (!server.ClientsAllDone() && !server.AllClientsRemoved) bool clientsDone = false; bool clientsRemoved = false; if (useTestObjs) { clientsDone = tSvr.ClientsAllDone(); } else { clientsDone = svr.ClientsAllDone(); clientsRemoved = svr.AllClientsRemoved; } while (!clientsDone && !clientsRemoved) { keyInfo = Console.ReadKey(); if (keyInfo.Modifiers == ConsoleModifiers.Control && keyInfo.Key == ConsoleKey.X) { Console.Write("Console> "); string entry = Console.ReadLine(); if (entry == "exit") { if (useTestObjs) { try { tSvr.InternMsgServer.RemoveAllClients(); tSvr.InternMsgServer.ServerIsDone = true; } catch (Exception e) { Console.WriteLine("Exiting Exception: " + e.Message); } } else { try { //server.RemoveAllClients(); //server.ServerIsDone = true; svr.RemoveAllClients(); svr.ServerIsDone = true; } catch (Exception e) { Console.WriteLine("Exiting Exception: " + e.Message); } } } } if (useTestObjs) { clientsDone = tSvr.ClientsAllDone(); } else { clientsDone = svr.ClientsAllDone(); clientsRemoved = svr.AllClientsRemoved; } } return; }
/// <summary> /// Instance accessor that follows the Singleton pattern /// </summary> /// <returns></returns> public static MessageImplFactory Instance() { return(_instance ?? (_instance = new MessageImplFactory())); }