// Pop the current server and put onto the end of the list. // Select head of list as long as number of reconnect attempts // under MaxReconnect. internal Srv SelectNextServer(int maxReconnect) { lock (poolLock) { Srv s = currentServer; if (s == null) { return(null); } int num = sList.Count; // remove the current server. sList.Remove(s); if (maxReconnect > 0 && s.reconnects < maxReconnect) { // if we haven't surpassed max reconnects, add it // to try again. sList.AddLast(s); } if (isEmpty()) { return(null); } currentServer = sList.First(); return(currentServer); } }
// removes implict servers NOT found in the provided list. internal void PruneOutdatedServers(string[] newUrls) { LinkedList <string> ulist = new LinkedList <string>(newUrls); lock (poolLock) { var tmp = new Srv[sList.Count]; sList.CopyTo(tmp, 0); // if a server is implicit and cannot be found in the url // list the remove it unless we are connected to it. foreach (Srv s in tmp) { // The server returns "<host>:<port>". We can't compare // againts Uri.Authority becase that API may strip out // ports. string hp = string.Format("{0}:{1}", s.url.Host, s.url.Port); if (s.isImplicit && !ulist.Contains(hp) && s != currentServer) { sList.Remove(s); } } } }
// Sets the currently selected server public void SetCurrentServer(Srv value) { lock (poolLock) { currentServer = value; // make sure server is in list, it might have been removed. Add(currentServer); } }
// returns true if it modified the pool, false if // the url already exists. private bool add(Srv s) { lock (poolLock) { if (sList.Contains(s, duplicateSrvCheck)) { return(false); } sList.AddLast(s); return(true); } }
// returns true if it modified the pool, false if // the url already exists. private bool Add(Srv s) { lock (poolLock) { if (sList.Contains(s, duplicateSrvCheck)) { return(false); } if (s.isImplicit && randomize) { // pick a random spot to add the server. var randElem = sList.ElementAt(rand.Next(sList.Count)); sList.AddAfter(sList.Find(randElem), s); } else { sList.AddLast(s); } return(true); } }
internal void open(Srv s, int timeoutMillis) { lock (mu) { client = new TcpClient(s.url.Host, s.url.Port); #if async_connect client = new TcpClient(); IAsyncResult r = client.BeginConnect(s.url.Host, s.url.Port, null, null); if (r.AsyncWaitHandle.WaitOne( TimeSpan.FromMilliseconds(timeoutMillis)) == false) { client = null; throw new NATSConnectionException("Timeout"); } client.EndConnect(r); #endif client.NoDelay = false; client.ReceiveBufferSize = Defaults.defaultBufSize; client.SendBufferSize = Defaults.defaultBufSize; writeStream = client.GetStream(); readStream = new NetworkStream(client.Client); } }
internal bool connect(Srv s, out Exception exToThrow) { url = s.url; try { exToThrow = null; lock (mu) { if (createConn(s)) { processConnectInit(); exToThrow = null; return true; } } } catch (Exception e) { exToThrow = e; close(ConnState.DISCONNECTED, false); lock (mu) { url = null; } } return false; }
// createConn will connect to the server and wrap the appropriate // bufio structures. It will do the right thing when an existing // connection is in place. private bool createConn(Srv s) { try { conn.open(s, opts.Timeout); if (pending != null && bw != null) { // flush to the pending buffer; try { // Make a best effort, but this shouldn't stop // conn creation. bw.Flush(); } catch (Exception) { } } bw = conn.getWriteBufferedStream(Defaults.defaultBufSize); br = conn.getReadBufferedStream(); } catch (Exception) { return false; } return true; }
internal void open(Srv s, int timeoutMillis) { lock (mu) { // If a connection was lost during a reconnect we // we could have a defunct SSL stream remaining and // need to clean up. if (sslStream != null) { try { sslStream.Dispose(); } catch (Exception) { } sslStream = null; } #if NET45 client = new TcpClient(s.url.Host, s.url.Port); #else client = new TcpClient(); if (!client.ConnectAsync(s.url.Host, s.url.Port).Wait(TimeSpan.FromMilliseconds(timeoutMillis))) { throw new NATSConnectionException("timeout"); } #endif client.NoDelay = false; client.ReceiveBufferSize = Defaults.defaultBufSize*2; client.SendBufferSize = Defaults.defaultBufSize; stream = client.GetStream(); // save off the hostname hostName = s.url.Host; } }