public static UserStore loadStore(string file) { UserStore store = null; try { using (var reader = new StreamReader(file, Encoding.UTF8, true)) { XmlSerializer ser = new XmlSerializer(typeof(TDIN_chatlib.LoginUser[])); TDIN_chatlib.LoginUser[] _load = (TDIN_chatlib.LoginUser[])ser.Deserialize(reader); store = new UserStore(); foreach (var u in _load) { if (u != null && u.Username != null && u.UUID != null) { store.userStore.Add(u.Username, u); } } reader.Close(); } return store; } catch (Exception e) { Console.WriteLine("Error: " + e.Message); return null; } }
private static void registerServer() { store = UserStore.loadStore(USER_STORE_FILE); if (store != null) Console.WriteLine("* Loaded " + store.userStore.Count + " users from store"); else { Console.WriteLine("* No users loaded from file"); store = UserStore.getNewStore(); //UserStore.saveStore(store, USER_STORE_FILE); } //Creating a custom formatter for a TcpChannel sink chain. BinaryServerFormatterSinkProvider providerNext = new BinaryServerFormatterSinkProvider(); providerNext.TypeFilterLevel = TypeFilterLevel.Full; TDIN_chatlib.ClientIPServerSinkProvider provider = new TDIN_chatlib.ClientIPServerSinkProvider(); provider.Next = providerNext; //Creating the IDictionary to set the port on the channel instance. IDictionary props = new Hashtable(); props["port"] = TDIN_chatlib.Constants.DEFAULT_SERVER_PORT; //Pass the properties for the port setting and the server provider in the server chain argument. (Client remains null here.) TcpChannel chan = new TcpChannel(props, null, provider); // register the channel ChannelServices.RegisterChannel(chan, false); //Console.WriteLine("a: " + localAddress.IP + ", p: " + localAddress.PORT); Console.WriteLine("* Registering Server Object."); Console.WriteLine("* Running on port: " + TDIN_chatlib.Constants.DEFAULT_SERVER_PORT); RemotingConfiguration.RegisterWellKnownServiceType(typeof(ChatServer), TDIN_chatlib.Constants.SERVER_SERVICE, WellKnownObjectMode.Singleton); System.Console.ReadLine(); }
public static bool saveStore(UserStore store, string file) { try { using (var fileStream = new FileStream(file, FileMode.Create)) { var ser = new XmlSerializer(typeof(TDIN_chatlib.LoginUser[])); TDIN_chatlib.LoginUser[] _save = new TDIN_chatlib.LoginUser[store.userStore.Count]; int count = 0; foreach (var u in store.userStore.Values) { _save[count++] = u; } var memoryStream = new MemoryStream(); var streamWriter = new StreamWriter(memoryStream, System.Text.Encoding.UTF8); ser.Serialize(streamWriter, _save); byte[] utf8EncodedXml = memoryStream.ToArray(); memoryStream.Close(); fileStream.Write(utf8EncodedXml, 0, utf8EncodedXml.Length); fileStream.Close(); } return true; } catch (Exception e) { Console.WriteLine("Error: " + e.Message); return false; } }