static public void SaveToBinFile(String fileName, T_DictFile dictFile) { Stream s = CSerialization.SerializeBinary(dictFile); s.Position = 0; CFile.WriteStream(fileName, (MemoryStream)s); }
void OnClientSessionCallback(CClientSocket ClientSocket, int ConnectionIndex, byte[] arguments) { SSessionPacket SessionPacket = CSerialization.Deserialize <SSessionPacket>(arguments); //Relay the session to the appropriate controller foreach (SConnection Connection in Connections) { if (Connection.ConnectionType != EConnectionType.Controller) { continue; } if (Connection.Information == null) { continue; } SControllerInformation ControllerInformation = (SControllerInformation)Connection.Information; if (ControllerInformation.IsAuthorized && ControllerInformation.Key == SessionPacket.ControllerKey) { ClientSocket.SendPacket <SSessionPacket>((byte)EControllerPackets.Session, SessionPacket); break; } } }
public void Load(String fileName) { try { List <T_ChsNameWordTraffic> newWords = new List <T_ChsNameWordTraffic>(); if (!File.Exists(fileName)) { //字典文件不存在 return; } MemoryStream s = CFile.ReadFileToStream(fileName); s.Position = 0; object obj; CSerialization.DeserializeBinary(s, out obj); m_Traffic = (T_ChsNameTraffic)obj; m_Traffic.AfterCount = 0; m_Traffic.AfterWordCount = 0; m_Traffic.BeforeCount = 0; m_Traffic.BeforeWordCount = 0; //整理姓名前缀后缀表 foreach (T_ChsNameWordTraffic wordTraffic in m_Traffic.Words) { if (wordTraffic.Word.Length > 1) { if (wordTraffic.Word[0] >= 0x4e00 && wordTraffic.Word[0] <= 0x9fa5) { //是汉字,且长度大于2,才作为有效前后缀 newWords.Add(wordTraffic); if (wordTraffic.After > 0) { m_Traffic.AfterCount += wordTraffic.After; m_Traffic.AfterWordCount++; } if (wordTraffic.Before > 0) { m_Traffic.BeforeCount += wordTraffic.After; m_Traffic.BeforeWordCount++; } } } } m_Traffic.Words = newWords; } catch { m_Traffic = new T_ChsNameTraffic(); } foreach (T_ChsNameWordTraffic wordTraffic in m_Traffic.Words) { m_Table[wordTraffic.Word] = wordTraffic; } }
public void Save(String fileName) { Stream s = CSerialization.SerializeBinary(m_Traffic); s.Position = 0; CFile.WriteStream(fileName, (MemoryStream)s); }
void OnClientIntroductionCallback(CClientSocket ClientSocket, int ConnectionIndex, byte[] arguments) { SClientIntroduction ClientIntroduction = (SClientIntroduction)CSerialization.Deserialize <SClientIntroduction>(arguments); SConnection Connection = Connections[ConnectionIndex]; Connection.Information = ClientIntroduction; Connections[ConnectionIndex] = Connection; }
void PacketSessionResponse(byte[] arguments) { SSessionPacket SessionPacket = CSerialization.Deserialize <SSessionPacket>(arguments); if (OnSessionPacketReceived != null) { OnSessionPacketReceived.Invoke(SessionPacket); } }
void OnSessionCallback(byte[] arguments) { SSessionPacket SessionPacket = CSerialization.Deserialize <SSessionPacket>(arguments); if (SessionPacket.Packet == ESessionPackets.Introduction) { ClientSocket.SendPacket <SSessionPacket>((byte)EClientPackets.Session, SessionPacket); } }
static public T_DictFile LoadFromBinFile(String fileName) { MemoryStream s = CFile.ReadFileToStream(fileName); s.Position = 0; object obj; CSerialization.DeserializeBinary(s, out obj); return((T_DictFile)obj); }
void PacketDataSyncResponse(byte[] arguments) { SDataSync DataSync = CSerialization.Deserialize <SDataSync>(arguments); foreach (SSyncCallback SyncCallback in SyncCallbacks) { if (SyncCallback.list == DataSync.list) { SyncCallback.callback(DataSync.type, DataSync.index, DataSync.data); break; } } }
void OnClientsSync(EDataSyncType type, int index, object data) { if (type == EDataSyncType.Remove) { if (OnRemoveClient != null) { OnRemoveClient.Invoke(index); } } else { if (OnUpdateClient != null) { SClient client = CSerialization.Deserialize <SClient>((byte[])data); OnUpdateClient.Invoke(index, client); } } }
static bool CheckSerialization <T>(T one, T two, out byte[] source) { source = CSerialization.Serialize <T>(one); byte[] destination = CSerialization.Serialize <T>(two); if (source.Length != destination.Length) { return(false); } bool result = true; for (int i = 0; i < source.Length; i++) { if (source[i] != destination[i]) { result = false; return(false); } } return(result); }
void OnControllerSessionCallback(CClientSocket ClientSocket, int ConnectionIndex, byte[] arguments) { SSessionPacket SessionPacket = CSerialization.Deserialize <SSessionPacket>(arguments); //Relay the session packet to the appropriate client foreach (SConnection Connection in Connections) { if (Connection.ConnectionType != EConnectionType.Client) { continue; } if (Connection.Information == null) { continue; } if (Connection.ClientSocket.GetHandle() == SessionPacket.ClientHandle) { Connection.ClientSocket.SendPacket <SSessionPacket>((byte)EClientPackets.Session, SessionPacket); break; } } }
void PacketIntroductionResponse(byte[] arguments) { SControllerAnswer ControllerAnswer = CSerialization.Deserialize <SControllerAnswer>(arguments); if (ControllerAnswer.IsAuthorized) { if (OnConnectionAuthorized != null) { OnConnectionAuthorized.Invoke(); } } else { if (OnConnectionAuthorizationFailed != null) { OnConnectionAuthorizationFailed.Invoke(); } } if (OnUpdateKey != null) { OnUpdateKey.Invoke(ControllerAnswer.key); } }
void OnControllerIntroductionCallback(CClientSocket ClientSocket, int ConnectionIndex, byte[] arguments) { SControllerIntroduction ControllerIntroduction = (SControllerIntroduction)CSerialization.Deserialize <SControllerIntroduction>(arguments); SControllerAnswer ControllerAnswer = new SControllerAnswer { IsAuthorized = false, key = -1 }; SConnection Connection = Connections[ConnectionIndex]; if (ControllerIntroduction.Password != Program.ControllerPassword) { //Tell the controller that password was not accepted Connection.ClientSocket.SendPacket <SControllerAnswer>((byte)EControllerPackets.Introduction, ControllerAnswer); //At this stage the password is not valid, so we disconnect the invalid controller ClientSocket.Disconnect(); return; } int key = GenerateKeyForController(); //Authorize connection SControllerInformation ControllerInformation = (SControllerInformation)Connection.Information; ControllerInformation.IsAuthorized = true; ControllerInformation.Key = key; Connection.Information = (object)ControllerInformation; Connections[ConnectionIndex] = Connection; //Key for controlling clients ControllerAnswer.key = key; ControllerAnswer.IsAuthorized = true; //Tell the controller that he is authorized Connection.ClientSocket.SendPacket <SControllerAnswer>((byte)EControllerPackets.Introduction, ControllerAnswer); }
static void SynchronizeStructArray <T>(ESyncList list, List <T> one, ref List <T> result, CClientSocket ClientSocket) { for (int i = 0; i < one.Count; i++) { bool check = true; if (i > (result.Count - 1)) { result.Add(one[i]); check = false; } bool sync = true; byte[] source = null; if (check) { if (CheckSerialization <T>(one[i], result[i], out source)) { sync = false; } } else { source = CSerialization.Serialize <T>(one[i]); } if (sync) { SDataSync DataSync = new SDataSync { index = i, type = EDataSyncType.Set, list = list, data = source }; ClientSocket.SendPacket <SDataSync>((byte)EControllerPackets.SyncData, DataSync); } } if (one.Count < result.Count) { int removed = 0; for (int i = one.Count; i < result.Count; i++) { int index_correction = i - removed; result.RemoveAt(index_correction); SDataSync DataSync = new SDataSync { index = index_correction, type = EDataSyncType.Remove, list = list, data = null }; removed++; ClientSocket.SendPacket <SDataSync>((byte)EControllerPackets.SyncData, DataSync); } } }