public object GetClientData(object data, ref BitSet flag, LanguageContext languageContext) { byte[] serializedObject = null; try { switch (languageContext) { case LanguageContext.DOTNET: serializedObject = SerializationUtil.SafeSerialize(data, _context.SerializationFormat, _context.SerializationContext, ref flag) as byte[]; break; } if (serializedObject != null && _context.CompressionEnabled) { serializedObject = CompressionUtil.Compress(serializedObject, ref flag, _context.CompressionThreshold); } if (serializedObject != null) { return(UserBinaryObject.CreateUserBinaryObject(serializedObject, _context.TransactionalPoolManager)); } } catch (Exception ex) { if (_context.NCacheLog != null) { if (_context.NCacheLog.IsErrorEnabled) { _context.NCacheLog.Error("ObjectDataFormatService.GetClientData()", ex.Message); } } } return(data); }
/// <summary> /// Send data (byte[]) to connected clients /// </summary> /// <param name="server"></param> /// <param name="data">data to send to connected clients</param> /// <param name="compression">compress data using GZIP if set to true</param> public static void SendAll(this EasyTcpServer server, byte[] data, bool compression = false) { if (compression) { data = CompressionUtil.Compress(data); } server.SendAll(dataArray: data); }
/// <summary> /// Send action (byte[]) to connected clients /// </summary> /// <param name="server"></param> /// <param name="action">action code</param> /// <param name="data">data to send to connected clients</param> /// <param name="compression">compress data using GZIP if set to true</param> public static void SendAllAction(this EasyTcpServer server, int action, byte[] data = null, bool compression = false) { if (compression && data != null) { data = CompressionUtil.Compress(data); } server.SendAll(BitConverter.GetBytes(action), data); }
/// <summary> /// Send data to remote host /// </summary> /// <param name="client"></param> /// <param name="data">data to send to remote host</param> /// <param name="compression">compress data using deflate if set to true</param> public static void Send(this EasyTcpClient client, byte[] data, bool compression = false) { if (compression) { data = CompressionUtil.Compress(data); } client.Protocol.SendMessage(client, data); }
/// <summary> /// Send action to remote host /// </summary> /// <param name="client"></param> /// <param name="action">action code</param> /// <param name="data">data to send to remote host</param> /// <param name="compression">compress data using deflate if set to true</param> public static void SendAction(this EasyTcpClient client, int action, byte[] data = null, bool compression = false) { if (compression && data != null) { data = CompressionUtil.Compress(data); } client.Send(BitConverter.GetBytes(action), data); }
/// <summary> /// Send data (byte[]) to the remote host /// </summary> /// <param name="client"></param> /// <param name="data">data to send to server</param> /// <param name="compression">compress data using GZIP if set to true</param> public static void Send(this EasyTcpClient client, byte[] data, bool compression = false) { if (compression) { data = CompressionUtil.Compress(data); } SendMessage(client?.BaseSocket, CreateMessage(data)); }
/// <summary> /// Send data (byte[]) to remote host. Then wait and return the reply /// </summary> /// <param name="client"></param> /// <param name="data">data to send to remote host</param> /// <param name="timeout">maximum time to wait for a reply, if time expired this function returns null</param> /// <param name="compression">compress data using GZIP if set to true</param> /// <returns>received reply</returns> public static Message SendAndGetReply(this EasyTcpClient client, byte[] data, TimeSpan?timeout = null, bool compression = false) { if (compression) { data = CompressionUtil.Compress(data); } return(client.SendAndGetReply(timeout, data)); }
/// <summary> /// Send action (byte[]) to remote host. Then wait and return the reply /// </summary> /// <param name="client"></param> /// <param name="action">action code</param> /// <param name="data">data to send to remote host</param> /// <param name="timeout">maximum time to wait for a reply, if time expired this function returns null</param> /// <param name="compression">compress data using GZIP if set to true</param> /// <returns>received reply</returns> public static Message SendActionAndGetReply(this EasyTcpClient client, int action, byte[] data = null, TimeSpan?timeout = null, bool compression = false) { if (compression && data != null) { data = CompressionUtil.Compress(data); } return(client.SendAndGetReply(timeout, BitConverter.GetBytes(action), data)); }
public void TestStringToBytesCompressed() { const string a = "abcd"; var bytes = a.GetBytes(); var compressed = CompressionUtil.Compress(bytes); var decompressed = CompressionUtil.Decompress(compressed); Assert.AreEqual(a, StringExtensions.GetString(decompressed)); }
public byte[] Serialize(object obj) { var data = SerializerFactory.Serializer(obj); if (data.Length > 1024 * 1 * 1024) { return(ByteUtil.Combine(CompressedBytes, CompressionUtil.Compress(data))); } else { return(ByteUtil.Combine(UnCompressedBytes, data)); } }
// TODO: candidate for move to playgen.photon // extend messsage deserialization to support configurable serializeration handlers per message type? public static byte[] Serialize(object content) { var serialziedString = JsonConvert.SerializeObject(content, Formatting.None, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }); var bytes = Encoding.UTF8.GetBytes(serialziedString); var compressedBytes = CompressionUtil.Compress(bytes); return(compressedBytes); }
public override void OnActionExecuted(HttpActionExecutedContext actContext) { if (actContext.Response == null) { //playing safe here, on case of exceptions base.OnActionExecuted(actContext); return; } var content = actContext.Response.Content; var bytes = content == null ? null : content.ReadAsByteArrayAsync().Result; var zlibbedContent = bytes == null ? new byte[0] : CompressionUtil.Compress(bytes); actContext.Response.Content = new ByteArrayContent(zlibbedContent); actContext.Response.Content.Headers.Remove("Content-Type"); actContext.Response.Content.Headers.Add("Content-encoding", "gzip"); actContext.Response.Content.Headers.Add("Content-Type", "application/json"); base.OnActionExecuted(actContext); }
private byte[] SerializeDocument(JSONDocument document) { Stream stream = new ClusteredMemoryStream(); JSONDocument.Serialize(stream, document); stream.Position = 0; if (enableCompression) { stream = CompressionUtil.Compress(stream); } var array = new byte[stream.Length + 1]; if (enableCompression) { array[0] |= (byte)PersistenceBits.Compressed; } stream.Read(array, 1, (int)stream.Length); stream.Dispose(); return(array); }
/// <summary> /// Recupera uma entrada do cache. /// </summary> /// <param name="key"></param> /// <param name="item"></param> /// <param name="flag"></param> /// <param name="group"></param> /// <param name="subGroup"></param> /// <param name="cacheEntry"></param> /// <returns></returns> public UserBinaryObject GetCacheEntry(string key, ProviderCacheItem item, ref BitSet flag, string group, string subGroup, out CacheEntry cacheEntry) { UserBinaryObject val = null; cacheEntry = null; object serializableObject = null; if ((item != null) && (item.Value != null)) { if ((item.Group == null) && (item.SubGroup != null)) { throw new OperationFailedException("Error occurred while synchronization with data source; group must be specified for sub group"); } if ((group != null) && !CacheHelper.CheckDataGroupsCompatibility(new GroupInfo(item.Group, item.SubGroup), new GroupInfo(group, subGroup))) { throw new OperationFailedException("Error occurred while synchronization with data source; groups are incompatible"); } if (flag == null) { flag = new BitSet(); } serializableObject = item.Value; Hashtable hashtable = new Hashtable(); TypeInfoMap typeInfoMap = _context.CacheRoot.GetTypeInfoMap(); hashtable["query-info"] = CacheLoaderUtil.GetQueryInfo(item.Value, typeInfoMap); if (item.Tags != null) { Hashtable tagInfo = CacheLoaderUtil.GetTagInfo(item.Value, item.Tags); if (tagInfo != null) { hashtable.Add("tag-info", tagInfo); } } if (item.NamedTags != null) { try { Hashtable hashtable3 = CacheLoaderUtil.GetNamedTagsInfo(item.Value, item.NamedTags, typeInfoMap); if (hashtable3 != null) { hashtable.Add("named-tag-info", hashtable3); } } catch (Exception exception) { throw new OperationFailedException("Error occurred while synchronization with data source; " + exception.Message); } } if (!item.Value.GetType().IsSerializable&& !_type.IsAssignableFrom(item.Value.GetType())) { throw new OperationFailedException("Read through provider returned an object that is not serializable."); } serializableObject = SerializationUtil.SafeSerialize(serializableObject, _context.SerializationContext, ref flag); if (_context.CompressionEnabled) { item.Value = CompressionUtil.Compress(item.Value as byte[], ref flag, _context.CompressionThreshold); } val = UserBinaryObject.CreateUserBinaryObject(serializableObject as byte[]); EvictionHint evictionHint = new PriorityEvictionHint(item.ItemPriority); ExpirationHint expiryHint = DependencyHelper.GetExpirationHint(item.Dependency, item.AbsoluteExpiration, item.SlidingExpiration); if (expiryHint != null) { expiryHint.CacheKey = key; if (item.ResyncItemOnExpiration) { expiryHint.SetBit(2); } } cacheEntry = new CacheEntry(val, expiryHint, evictionHint); cacheEntry.Flag = flag; cacheEntry.GroupInfo = new GroupInfo(item.Group, item.SubGroup); cacheEntry.QueryInfo = hashtable; cacheEntry.ResyncProviderName = (item.ResyncProviderName == null) ? null : item.ResyncProviderName.ToLower(); } return(val); }