/// <summary> /// This method loads the data from cache into a Hashtable. /// /// Pass a SockIO object which is ready to receive data and a Hashtable /// to store the results. /// </summary> /// <param name="sock">socket waiting to pass back data</param> /// <param name="hm">hashmap to store data into</param> /// <param name="asString">if true, and if we are using NativehHandler, return string val</param> private void LoadItems(SockIO sock, Hashtable hm, bool asString) { while(true) { string line = sock.ReadLine(); if(log.IsDebugEnabled) { log.Debug(GetLocalizedString("loaditems line").Replace("$$Line$$", line)); } if(line.StartsWith(VALUE)) { string[] info = line.Split(' '); string key = info[1]; int flag = int.Parse(info[2], new NumberFormatInfo()); int length = int.Parse(info[3], new NumberFormatInfo()); if(log.IsDebugEnabled) { log.Debug(GetLocalizedString("loaditems header").Replace("$$Key$$", key).Replace("$$Flags$$", flag.ToString(new NumberFormatInfo())).Replace("$$Length$$", length.ToString(new NumberFormatInfo()))); } // read obj into buffer byte[] buf = new byte[length]; sock.Read(buf); sock.ClearEndOfLine(); // ready object object o; // check for compression if((flag & F_COMPRESSED) != 0) { try { // read the input stream, and write to a byte array output stream since // we have to read into a byte array, but we don't know how large it // will need to be, and we don't want to resize it a bunch GZipInputStream gzi = new GZipInputStream(new MemoryStream(buf)); MemoryStream bos = new MemoryStream(buf.Length); int count; byte[] tmp = new byte[2048]; while((count = gzi.Read(tmp, 0, tmp.Length)) > 0) { bos.Write(tmp, 0, count); } // store uncompressed back to buffer buf = bos.ToArray(); gzi.Close(); } catch(IOException e) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("loaditems uncompression IOException").Replace("$$Key$$", key), e); } throw new IOException(GetLocalizedString("loaditems uncompression IOException").Replace("$$Key$$", key), e); } } // we can only take out serialized objects if((flag & F_SERIALIZED) == 0) { if(_primitiveAsString || asString) { // pulling out string value if(log.IsInfoEnabled) { log.Info(GetLocalizedString("loaditems retrieve as string")); } o = Encoding.GetEncoding(_defaultEncoding).GetString(buf); } else { // decoding object try { o = NativeHandler.Decode(buf); } catch(Exception e) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("loaditems deserialize error").Replace("$$Key$$", key), e); } throw new IOException(GetLocalizedString("loaditems deserialize error").Replace("$$Key$$", key), e); } } } else { // deserialize if the data is serialized try { MemoryStream memStream = new MemoryStream(buf); o = new BinaryFormatter().Deserialize(memStream); if(log.IsInfoEnabled) { log.Info(GetLocalizedString("loaditems deserializing").Replace("$$Class$$", o.GetType().Name)); } } catch(SerializationException e) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("loaditems SerializationException").Replace("$$Key$$", key), e); } throw new IOException(GetLocalizedString("loaditems SerializationException").Replace("$$Key$$", key), e); } } // store the object into the cache hm[ key ] = o ; } else if(END == line) { if(log.IsDebugEnabled) { log.Debug(GetLocalizedString("loaditems finished")); } break; } } }
protected static void RemoveSocketFromPool(Hashtable pool, string host, SockIO socket) { if (host != null && host.Length == 0 || pool == null) return; if(pool.ContainsKey(host)) { Hashtable sockets = (Hashtable)pool[host]; if(sockets != null) { sockets.Remove(socket); } } }
/// <summary> /// Adds a socket to a given pool for the given host. /// /// Internal utility method. /// </summary> /// <param name="pool">pool to add to</param> /// <param name="host">host this socket is connected to</param> /// <param name="socket">socket to add</param> //[MethodImpl(MethodImplOptions.Synchronized)] protected static void AddSocketToPool(Hashtable pool, string host, SockIO socket) { if (pool == null) return; Hashtable sockets; if (host != null && host.Length != 0 && pool.ContainsKey(host)) { sockets = (Hashtable)pool[host]; if (sockets != null) { sockets[socket] = DateTime.Now; //MaxM 1.16.05: Use current DateTime to indicate socker creation time rather than milliseconds since 1970 return; } } sockets = new Hashtable(); sockets[socket] = DateTime.Now; //MaxM 1.16.05: Use current DateTime to indicate socker creation time rather than milliseconds since 1970 pool[host] = sockets; }
/// <summary> /// Creates a new SockIO obj for the given server. /// ///If server fails to connect, then return null and do not try ///again until a duration has passed. This duration will grow ///by doubling after each failed attempt to connect. /// </summary> /// <param name="host">host:port to connect to</param> /// <returns>SockIO obj or null if failed to create</returns> protected SockIO CreateSocket(string host) { SockIO socket = null; // if host is dead, then we don't need to try again // until the dead status has expired // we do not try to put back in if failover is off if(_failover && _hostDead.ContainsKey(host) && _hostDeadDuration.ContainsKey(host)) { DateTime store = (DateTime)_hostDead[host]; long expire = ((long)_hostDeadDuration[host]); if((store.AddMilliseconds(expire)) > DateTime.Now) return null; } try { socket = new SockIO(this, host, _socketTimeout, _socketConnectTimeout, _nagle); if(!socket.IsConnected) { if(Log.IsErrorEnabled) { Log.Error(GetLocalizedString("failed to get socket").Replace("$$Host$$", host)); } try { socket.TrueClose(); } catch(SocketException ex) { if(Log.IsErrorEnabled) { Log.Error(GetLocalizedString("failed to close socket on host").Replace("$$Host$$", host), ex); } socket = null; } } } catch(SocketException ex) { if(Log.IsErrorEnabled) { Log.Error(GetLocalizedString("failed to get socket").Replace("$$Host$$", host), ex); } socket = null; } catch(ArgumentException ex) { if(Log.IsErrorEnabled) { Log.Error(GetLocalizedString("failed to get socket").Replace("$$Host$$", host), ex); } socket = null; } catch(IOException ex) { if(Log.IsErrorEnabled) { Log.Error(GetLocalizedString("failed to get socket").Replace("$$Host$$", host), ex); } socket = null; } // if we failed to get socket, then mark // host dead for a duration which falls off if(socket == null) { DateTime now = DateTime.Now; _hostDead[host] = now; long expire = (_hostDeadDuration.ContainsKey(host)) ? (((long)_hostDeadDuration[host]) * 2) : 100; _hostDeadDuration[host] = expire; if(Log.IsDebugEnabled) { Log.Debug(GetLocalizedString("ignoring dead host").Replace("$$Host$$", host).Replace("$$Expire$$", expire.ToString(new NumberFormatInfo()))); } // also clear all entries for this host from availPool ClearHostFromPool(_availPool, host); } else { if(Log.IsDebugEnabled) { Log.Debug(GetLocalizedString("created socket").Replace("$$ToString$$", socket.ToString()).Replace("$$Host$$", host)); } _hostDead.Remove(host); _hostDeadDuration.Remove(host); if(_buckets.BinarySearch(host) < 0) _buckets.Add(host); } return socket; }
public void CheckIn(SockIO socket) { CheckIn(socket, true); }
public void CheckIn(SockIO socket, bool addToAvail) { if (socket == null) return; string host = socket.Host; if(Log.IsDebugEnabled) { Log.Debug("Calling check-in on socket: " + socket.ToString() + " for host: " + host); } // remove from the busy pool if(Log.IsDebugEnabled) { Log.Debug("Removing socket (" + socket.ToString() + ") from busy pool for host: " + host); } RemoveSocketFromPool(_busyPool, host, socket); // add to avail pool if(addToAvail && socket.IsConnected) { if(Log.IsDebugEnabled) { Log.Debug("Returning socket (" + socket.ToString() + " to avail pool for host: " + host); } AddSocketToPool(_availPool, host, socket); } }