/// <summary> /// Registers or retreive a server (IOC) chain. /// </summary> /// <param name="gateway"></param> /// <param name="endPoint"></param> /// <returns></returns> public static WorkerChain GetIocChain(Gateway gateway, IPEndPoint endPoint) { lock (iocConnections) { if (endPoint == null) { } Debug.Assert(endPoint != null, "endPoint != null"); if (iocConnections.ContainsKey(endPoint)) return iocChains[endPoint]; } if (gateway == null) return null; WorkerChain chain = WorkerChain.TcpResponseChain(gateway, ChainSide.SERVER_CONN, endPoint, endPoint); WorkerChain result = null; lock (iocConnections) { if (iocConnections.ContainsKey(endPoint)) result = iocChains[endPoint]; else { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); try { //SocketAsyncEventArgs async = new SocketAsyncEventArgs(); if (!SocketConnect(socket, endPoint, 200)) { try { socket.Dispose(); } catch { } return null; } //socket.Connect(endPoint); //socket.ConnectAsync(new SocketAsyncEventArgs { RemoteEndPoint = endPoint }); //socket.BeginConnect(endPoint, SocketConnected, null); } catch (Exception ex) { socket.Dispose(); return null; } iocConnections.Add(endPoint, socket); // Add a monitor on the known channels chain.Channels.BagModified += (ConcurrentBagModification<string>)((bag, newItem) => gateway.DoIocConnectedChannels(endPoint.ToString(), newItem)); ((TcpReceiver)chain[0]).Socket = socket; iocChains.Add(endPoint, chain); if (Log.WillDisplay(System.Diagnostics.TraceEventType.Start)) Log.TraceEvent(System.Diagnostics.TraceEventType.Start, -1, "Building new IOC chain for " + endPoint); result = chain; } } if (!object.ReferenceEquals(result, chain)) chain.Dispose(); return result; }