protected virtual object CreateInstance(Service service, TTransport transport) { ThriftService thriftService = service as ThriftService; if (thriftService == null) { throw new InvalidCastException("无法转换成ThriftService"); } if (transport == null) { throw new ArgumentNullException(nameof(transport)); } if (!transport.IsOpen) { transport.Open(); } TProtocol protocol = new TBinaryProtocol(transport); TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, thriftService.Name); object instance = Activator.CreateInstance(thriftService.ServiceType, multiplexedProtocol); IDynamicProxyBuilder proxyBuilder = GlobalSetting.GetService <IDynamicProxyBuilder>(); if (proxyBuilder == null) { return(instance); } Type proxyType = proxyBuilder.CreateProxy(thriftService.ServiceInterfaceType); return(Activator.CreateInstance(proxyType, instance)); }
public DecorateTransport(TTransport transport, ThriftConnectionStore store, ThriftService service, TransportPoolItem item) { Contract.Assert(transport != null && item != null && service != null && store != null); _transport = transport; _item = item; _service = service; _connectionStore = store; }
public static AgentServiceRegistration CreateAgentService(ThriftService service) { return(new AgentServiceRegistration { Name = service.Name, Address = service.Address, Port = service.Port, Tags = SerializeServiceFalg(service) }); }
public void ReleaseTransport(ThriftService service, TransportPoolItem item) { lock (this) { if (_connectionPool.ContainsKey(service)) { _connectionPool[service].SetFree(item); Monitor.Pulse(this); } } }
public TTransport GetOrAdd(ThriftService service, Func <ThriftService, TTransport> createAction) { Contract.Assert(service != null && createAction != null); TransportPoolItem item = null; lock (this) { if (_connectionPool.ContainsKey(service)) { while (true) { item = _connectionPool[service].GetUsableTransport(() => createAction(service)); if (item == null) { Monitor.Wait(this, _waitFreeMillisecond); } else { break; } } } else { TransportPoolItemCollection collection = new TransportPoolItemCollection(_connectionLimit, _transportOverdueInterval); TTransport transport = createAction(service); if (transport != null) { item = new TransportPoolItem { Transport = transport, IsFree = false, LastUseTime = DateTime.Now }; collection.Add(item); } _connectionPool.Add(service, collection); } } if (item != null) { return(new DecorateTransport(item.Transport, this, service, item)); } return(null); }
public TTransport GetTransport(Service service) { ThriftService thriftService = service as ThriftService; if (thriftService == null) { throw new NullReferenceException("thriftService"); } return(_connectionStore.GetOrAdd(thriftService, tmp => { TSocket socket = new TSocket(new TcpClient(tmp.Address, tmp.Port)); if (!socket.IsOpen) { socket.Open(); } return socket; })); }
public static string[] SerializeServiceFalg(ThriftService service) { return(new string[] { service.Type, service.ServiceInterfaceType?.FullName }); }