private void Button_Click_Save(object sender, RoutedEventArgs e) { FileStream stream = File.Create("C:\\Users\\Thomas\\Documents\\GitHub\\Serializer\\Test\\bin\\Debug\\test.txt"); StreamWriter writer = new StreamWriter(stream); manager.Serialize <TestClass>(testClass, "Test", new XMLWriter(writer)); writer.Flush(); writer.Close(); }
public void SaveCredentialsForAutoLogin(UserCredentials creds) { try { SerializeManager.Serialize <UserCredentials>(creds, StaticResources.CurrentUserSerializedPath); Logger.Log("Current user was serialized"); } catch (Exception e) { Logger.Log("Current user was not saved", e); } }
private static void SaveSessionToDisk() { SerializeManager <ConcurrentDictionary <string, SessionIdentity> > .Serialize(SessionBagFile, _SessionPools); }
/** * 发送数据包 * * 发送的数据格式: * ----------2---------------4--------------------2--------------data--------------------- * -------字节长度----------序列id----------------消息类型---------消息体(model)-------------- */ public void Send(NetProto.Api.ENetMsgId msgId, IMessage model) { byte[] data = SerializeManager.Serialize(model); // 序列化对象 Int16 id = (Int16)msgId; UInt16 payloadSize = 6; // sizeof(seqid) + sizeof(msgid) //payloadSize += (UInt16)data.Length; payloadSize += (UInt16)data.Length; // payload byte[] payload = new byte[payloadSize]; // seqid Byte[] _seqid = BitConverter.GetBytes(seqId); if (BitConverter.IsLittleEndian) { Array.Reverse(_seqid); } _seqid.CopyTo(payload, 0); // opcode Byte[] _opcode = BitConverter.GetBytes(id); if (BitConverter.IsLittleEndian) { Array.Reverse(_opcode); } _opcode.CopyTo(payload, 4); // data if (data != null) { data.CopyTo(payload, 6); } // try encrypt byte[] encrypted = EncryptStream(payload); // =>pack byte[] buffer = new byte[2 + payloadSize]; // sizeof(header) + payloadSize // =>header Byte[] _header = BitConverter.GetBytes(payloadSize); if (BitConverter.IsLittleEndian) { Array.Reverse(_header); } _header.CopyTo(buffer, 0); // =>payload encrypted.CopyTo(buffer, 2); sendDone.Reset(); // 发出一条消息后重置心跳时间 heartbeatCount = 0; try { //Debug.Log("msgId---------------send : " + msgId); socket.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(SendCallback), socket); sendDone.WaitOne(); } catch (Exception e) { isConnected = false; // 如果socket已经断了,报这个异常 // System.Net.Sockets.SocketException: The socket is not connected Debug.Log(e.ToString()); startReConnect(); } }