private const string RESERVED_CHARACTERS_CREDENTIALS = "\t\r\n\v\f"; //NOXLATE /// <summary> /// Encrypts the specified credentials. For a feature source that uses %MG_USERNAME% and %MG_PASSWORD% placeholder tokens to /// properly use these encrypted credentials, the encrypted string must be set as the MG_USER_CREDENTIALS resource data item /// for that feature source /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <returns>A <see cref="T:System.IO.Stream"/> containing the encrypted credentials</returns> public static Stream Write(string username, string password) { string credentials; EncryptStrings(username, password, out credentials, RESERVED_CHARACTERS_CREDENTIALS); return(MemoryStreamPool.GetStream("CredentialWriter.Write", ASCIIEncoding.Default.GetBytes(credentials))); }
private void SendData() { MessageTriplete toSend = null; if (SocketStatus && _messagesToSend?.TryDequeue(out toSend) == true) { byte[] msg = toSend.Payload; bool success = false; using (var msh = new MemoryStreamPool()) { using (var bw = new BinaryWriter(msh.Stream, Encoding.UTF8, true)) { bw.Write(msg.Length); bw.Write(msg); } try { _connection.GetStream().Write(msh.Stream.ToArray(), 0, (int)msh.Stream.Length); LastSocketIO.Restart(); success = true; } catch (Exception ex) { this.ConnectionError?.Invoke(this, this); return; } finally { toSend.EventWait?.TrySetResult(success); } } } }
/// <summary> /// Re-bases any resource id references in the resource document /// </summary> /// <param name="sourceRoot"></param> /// <param name="targetRoot"></param> /// <returns>A re-based copy of the original resource</returns> public IResource Rebase(string sourceRoot, string targetRoot) { if (sourceRoot == targetRoot) { return(_res); } var xml = ObjectFactory.SerializeAsString(_res); var doc = new XmlDocument(); doc.LoadXml(xml); var elements = doc.GetElementsByTagName("ResourceId"); //NOXLATE foreach (XmlNode el in elements) { el.InnerText = el.InnerText.Replace(sourceRoot, targetRoot); } using (var ms = MemoryStreamPool.GetStream()) { doc.Save(ms); ms.Position = 0L; var modifiedRes = ObjectFactory.Deserialize(_res.ResourceType, ms); modifiedRes.ResourceID = _res.ResourceID; return(modifiedRes); } }
internal static TcpRawMessage GetEmpty(MemoryStreamPool memoryStreamPool, MessageHeaderFlags flags) { return(new TcpRawMessage(memoryStreamPool, new ArraySegment <byte>(RawMessage.emptyArray, 0, 0), false) { Flags = flags }); }
private void Do(int C, int P, int M, bool noisy, bool useRAII, Func <MemoryStream, int> test) { var pool = MemoryStreamPool.Create(C); void testCore(MemoryStream ms) { var len = ms.Length; Assert.AreEqual(0, len); var L = test(ms); len = ms.Length; Assert.AreEqual(L, len); } if (useRAII) { Run(() => pool.New(), o => o.MemoryStream, o => o.Dispose(), testCore, P, M, noisy); } else { Run(() => pool.Allocate(), o => (MemoryStream)o, o => pool.Free(o), testCore, P, M, noisy); } }
/// <summary> /// Extends <see cref="HttpClient"/> by adding a method that can serialize and /// transmit a <b>cadence-proxy</b> proxy request message. /// </summary> /// <typeparam name="TRequest">The request message type.</typeparam> /// <param name="client">The HTTP client.</param> /// <param name="request">The message to be sent.</param> /// <returns>The <see cref="HttpResponse"/>.</returns> public async static Task <HttpResponseMessage> SendRequestAsync <TRequest>(this HttpClient client, TRequest request) where TRequest : ProxyRequest { Covenant.Requires <ArgumentNullException>(request != null); if (client == null) { throw new TaskCanceledException(); } var stream = request.SerializeAsStream(); try { var content = new StreamContent(stream); content.Headers.ContentType = new MediaTypeHeaderValue(ProxyMessage.ContentType); var httpRequest = new HttpRequestMessage(HttpMethod.Put, "/") { Content = content }; return(await client.SendAsync(httpRequest)); } finally { MemoryStreamPool.Free(stream); } }
/// <summary> /// Sets the configuration document content /// </summary> /// <param name="fs">The feature source</param> /// <param name="conn">The server connection</param> /// <param name="xmlContent">The confiugration document XML content</param> public static void SetConfigurationContent(this IFeatureSource fs, IServerConnection conn, string xmlContent) { Check.ArgumentNotNull(fs, nameof(fs)); Check.ArgumentNotNull(conn, nameof(conn)); if (string.IsNullOrEmpty(fs.ConfigurationDocument)) { fs.ConfigurationDocument = "config.xml"; //NOXLATE } if (string.IsNullOrEmpty(xmlContent)) { bool hasResourceData = false; var resDataList = conn.ResourceService.EnumerateResourceData(fs.ResourceID).ResourceData; foreach (var resData in resDataList) { if (resData.Name == fs.ConfigurationDocument) { hasResourceData = true; break; } } if (hasResourceData) { conn.ResourceService.DeleteResourceData(fs.ResourceID, fs.ConfigurationDocument); } } else { using (var ms = MemoryStreamPool.GetStream("SetConfigurationContent", Encoding.UTF8.GetBytes(xmlContent))) //NOXLATE { conn.ResourceService.SetResourceData(fs.ResourceID, fs.ConfigurationDocument, ResourceDataType.Stream, ms); } } }
/// <summary> /// <para> /// Extends <see cref="HttpClient"/> by adding a method that can serialize and /// transmit a <b>cadence-proxy</b> reply to a proxy request message. /// </para> /// <note> /// This method ensures that the reply message's <see cref="ProxyReply.RequestId"/> /// matches the request's <see cref="ProxyRequest.RequestId"/> before sending the /// reply. /// </note> /// </summary> /// <typeparam name="TRequest">The request message type.</typeparam> /// <typeparam name="TReply">The reply message type.</typeparam> /// <param name="client">The HTTP client.</param> /// <param name="request">The request being responsed to.</param> /// <param name="reply">The reply message.</param> /// <returns>The <see cref="HttpResponse"/>.</returns> public async static Task <HttpResponseMessage> SendReplyAsync <TRequest, TReply>(this HttpClient client, TRequest request, TReply reply) where TRequest : ProxyRequest where TReply : ProxyReply { Covenant.Requires <ArgumentNullException>(request != null); Covenant.Requires <ArgumentNullException>(reply != null); Covenant.Requires <ArgumentException>(reply.Type == request.ReplyType, $"Reply message type [{reply.Type}] is not a suitable response for a [{request.Type}] request."); reply.RequestId = request.RequestId; var stream = reply.SerializeAsStream(); try { var content = new StreamContent(stream); content.Headers.ContentType = new MediaTypeHeaderValue(ProxyMessage.ContentType); var httpRequest = new HttpRequestMessage(HttpMethod.Put, "/") { Content = content }; return(await client.SendAsync(httpRequest)); } finally { MemoryStreamPool.Free(stream); } }
public ChannelBase(MemoryStreamPool memoryStreamPool, ILogManager logManager, ChannelDescriptor descriptor, IChannelConnection connection) { this.memoryStreamPool = memoryStreamPool; this.Descriptor = descriptor; this.connection = connection; this.logger = logManager.GetLogger(this.GetType().Name); }
private SocketManager() { _socketPools = new Dictionary<SocketSettings, Dictionary<IPEndPoint, SocketPool>>(2); _defaultSettings = SocketClient.GetDefaultSettings(); Dictionary<IPEndPoint, SocketPool> defaultSettingsPools = new Dictionary<IPEndPoint, SocketPool>(50); _sharedBufferPool = new MemoryStreamPool(_defaultSettings.InitialMessageSize, _defaultSettings.BufferReuses, SocketClient.Config.SharedPoolMinimumItems); _socketPools.Add(_defaultSettings, defaultSettingsPools); }
public void PooledMemoryStream_GottenTooBig() { var bigPool = MemoryStreamPool.Create(1, 16, 2048); var smallPool = MemoryStreamPool.Create(1, 16, 16); TooBig(() => PooledMemoryStream.New(), h => h.MemoryStream, (h, n) => h.Write(new byte[n], 0, n), 1024); TooBig(() => bigPool.New(), h => h.MemoryStream, (h, n) => h.Write(new byte[n], 0, n), 2048); TooBig(() => smallPool.New(), h => h.MemoryStream, (h, n) => h.Write(new byte[n], 0, n), 16); }
private SocketManager() { _socketPools = new Dictionary <SocketSettings, Dictionary <IPEndPoint, SocketPool> >(2); _defaultSettings = SocketClient.GetDefaultSettings(); Dictionary <IPEndPoint, SocketPool> defaultSettingsPools = new Dictionary <IPEndPoint, SocketPool>(50); _sharedBufferPool = new MemoryStreamPool(_defaultSettings.InitialMessageSize, _defaultSettings.BufferReuses, SocketClient.Config.SharedPoolMinimumItems); _socketPools.Add(_defaultSettings, defaultSettingsPools); }
public void Object_Pool_Checks_Out_Object() { MemoryStreamPool memoryStreamPool = new MemoryStreamPool(new TimeSpan()); memoryStreamPool.CheckOutObject(); Assert.Single(memoryStreamPool._locked); Assert.Empty(memoryStreamPool._unlocked); }
private async Task ReceiveAsync(CancellationToken cancellationToken) { try { var buffer = new ArraySegment <byte>(new Byte[_bufferSize]); while (_webSocket.State == WebSocketState.Open) { using (var ms = MemoryStreamPool.GetStream("nanoSession")) { WebSocketReceiveResult result; do { result = await _webSocket.ReceiveAsync(buffer, cancellationToken).ConfigureAwait(false); switch (result.MessageType) { case WebSocketMessageType.Text: break; case WebSocketMessageType.Binary: ms.Write(buffer.Array, buffer.Offset, result.Count); break; case WebSocketMessageType.Close: await _webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None); break; default: throw new ArgumentOutOfRangeException(); } } while (!result.EndOfMessage); var bytes = ms.ToArray(); Trace.TraceEvent(TraceEventType.Verbose, 1000, () => "Rx {0} bytes", bytes.Length.ToString()); if (bytes.Length > 0) { ProcessMessageAsync(bytes, cancellationToken).ConfigureAwait(false); } } } } catch (Exception e) { Trace.TraceEvent(TraceEventType.Error, 2003, () => "Exception: {0}", e); } finally { } }
public void Object_Pool_Returns_Same_Object_If_Not_Expired() { MemoryStreamPool memoryStreamPool = new MemoryStreamPool(new TimeSpan(0, 1, 0)); MemoryStream memoryStreamOne = memoryStreamPool.CheckOutObject(); memoryStreamPool.CheckInObject(memoryStreamOne); MemoryStream memoryStreamTwo = memoryStreamPool.CheckOutObject(); Assert.Equal(memoryStreamOne, memoryStreamTwo); }
public byte[] GetBytes() { using (var ms = MemoryStreamPool.Get()) { using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) { writer.Write(this); } return(ms.ToArray()); } }
private static void PooledMemoryStream_Simple_Impl(MemoryStreamPool pool) { for (var i = 0; i < 100; i++) { using var obj = i % 2 == 0 ? pool.New() : PooledMemoryStream.New(pool); var ms = obj.MemoryStream; Assert.AreEqual(0, ms.Length); ms.Write(bytes, 0, 4); } }
public void CreateBinaryWriter() { var pool = new MemoryStreamPool(); var expected = pool.Rent(); using (var writer = MemoryStreamPool.Shared.CreateBinaryWriter()) { writer.Write(42); } pool.Return(expected); var actual = pool.Rent(); Assert.AreSame(expected, actual); Assert.AreEqual(0, actual.Length); }
public static unsafe void Save(MemoryStreamPool memPool, IBinaryStorage store, short typeId, int primaryId, byte[] extendedId, PayloadStorage header, byte[] value) { if (value != null) { byte[] entryBytes = new byte[BdbHeaderSize + value.Length]; fixed(byte *pBytes = &entryBytes[0]) { *((PayloadStorage *)pBytes) = header; } Buffer.BlockCopy(value, 0, entryBytes, BdbHeaderSize, value.Length); store.Put(typeId, new StorageKey(extendedId, primaryId), entryBytes); } }
public byte[] GetBytes() { byte[] blockData; using (var ms = MemoryStreamPool.Get(TotalBlockLength)) { using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) { WriteTo(writer); } blockData = ms.ToArray(); } return(blockData); }
public void Object_Pool_Expires_Object() { MemoryStreamPool memoryStreamPool = new MemoryStreamPool(new TimeSpan(0, 0, 1)); MemoryStream memoryStreamOne = memoryStreamPool.CheckOutObject(); memoryStreamPool.CheckInObject(memoryStreamOne); Thread.Sleep(new TimeSpan(0, 0, 2)); MemoryStream memoryStreamTwo = memoryStreamPool.CheckOutObject(); //Checkout object should expire memoryStreamOne, which destroys it, and returned a new object Assert.Single(memoryStreamPool._locked); Assert.Empty(memoryStreamPool._unlocked); Assert.NotEqual(memoryStreamOne, memoryStreamTwo); }
/// <summary> /// Reads the stream. /// </summary> /// <returns></returns> public Stream ReadStream() { MgBinaryStreamArgumentPacket p = ReadBinaryStreamArgumentPacket(); if (p.ArgumentType != MgArgumentType.Stream) { throw new InvalidCastException(string.Format(Strings.ErrorBinarySerializerUnexpectedType, p.ArgumentType.ToString(), "Stream")); //NOXLATE } if (ReadBool()) { return(null); } //TODO: If the stream is large, we use a lot of memory, perhaps the filebacked async stream could be used //Due to the annoying embedded buffer size markers, we cannot return the stream 'as is' Grrrrrr! int r; ulong remain = p.Length; byte[] buf = new byte[1024 * 8]; var ms = MemoryStreamPool.GetStream(); do { int blocksize = ReadInt32(); r = 1; while (r > 0 && blocksize > 0) { r = m_stream.Read(buf, 0, (int)Math.Min((ulong)remain, (ulong)buf.Length)); blocksize -= r; remain -= (ulong)r; ms.Write(buf, 0, r); } if (blocksize != 0) { throw new Exception(Strings.ErrorBinarySerializerPrematureEndOfStream); } } while (remain > 0); ms.Position = 0; return(ms); }
public void TileSeeder_WithDefaultWalker() { var mdfId = "Library://Samples/Sheboygan/MapsTiled/Sheboygan.MapDefinition"; var mdf = ObjectFactory.CreateMapDefinition(new Version(1, 0, 0), "Sheboygan"); mdf.ResourceID = mdfId; mdf.InitBaseMap(); var group = mdf.BaseMap.AddBaseLayerGroup("Base Layer Group"); group.Visible = true; foreach (double scale in TileTests.SCALE_LIST.Reverse()) { mdf.BaseMap.AddFiniteDisplayScale(scale); } mdf.SetExtents(-87.764986990962839, 43.691398128787782, -87.695521510899724, 43.797520000480347); var walkOptions = new DefaultTileWalkOptions(mdf); walkOptions.MetersPerUnit = 111319.490793274; var walker = new DefaultTileWalker(walkOptions); var mockTileSvc = new Mock <ITileService>(); mockTileSvc .Setup(t => t.GetTile(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>())) .Returns(() => MemoryStreamPool.GetStream()); var options = new TileSeederOptions(); var seeder = new TileSeeder(mockTileSvc.Object, walker, options); var stats = seeder.Run(); Assert.Equal(mdfId, stats.ResourceID); Assert.Equal(127472, stats.TilesRendered); mockTileSvc.Verify(t => t.GetTile(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>()), Times.Exactly(stats.TilesRendered)); /* * var tiles = walker.GetTileList(); * for (int i = 0; i < tiles.Length; i++) * { * var t = tiles[i]; * mockTileSvc.Verify(ts => ts.GetTile(mdfId, t.GroupName, t.Col, t.Row, t.Scale), Times.Once()); * } */ }
/// <summary> /// Encodes the given combined keyset into an ID string for use in the Xml /// </summary> /// <param name="values">The combined key</param> /// <returns>A base64 encoded ID string</returns> public string EncodeIDString(object[] values) { object[] tmp = NormalizeAndValidate(values); using (var ms = MemoryStreamPool.GetStream()) { for (int i = 0; i < m_layer.IdentityProperties.Length; i++) { Type type = m_layer.IdentityProperties[i].Type; if (type == typeof(short)) { byte[] x = BitConverter.GetBytes((short)tmp[i]); ms.Write(x, 0, x.Length); } else if (type == typeof(int)) { byte[] x = BitConverter.GetBytes((int)tmp[i]); ms.Write(x, 0, x.Length); } else if (type == typeof(long)) { byte[] x = BitConverter.GetBytes((long)tmp[i]); ms.Write(x, 0, x.Length); } else if (type == typeof(double)) { byte[] x = BitConverter.GetBytes((double)tmp[i]); ms.Write(x, 0, x.Length); } else if (type == typeof(string)) { byte[] x = System.Text.Encoding.UTF8.GetBytes((string)tmp[i]); ms.Write(x, 0, x.Length); ms.WriteByte(0); } else { throw new Exception(string.Format(Strings.ErrorUnsupportedPkType, type.ToString())); } } return(Convert.ToBase64String(ms.ToArray())); } }
protected PostgresDbConnectionBase( string connectionString, bool asyncOnly) { try { PostgresConnectionString = new PostgresConnectionString( connectionString); _buffer = PostgresDbConnection.GetBuffer(); _writeBuffer = MemoryStreamPool.Get(); AsyncOnly = asyncOnly; } catch { _cancel.TryDispose(); throw; } }
/// <summary> /// Overriden. Obtains an instance from this factory. /// </summary> /// <returns>An <see cref="IObjectStorage"/> instance.</returns> public override IObjectStorage ObtainInstance() { var memoryPool = new MemoryStreamPool(); if (_memoryPoolInitialBufferSize.HasValue) { memoryPool.InitialBufferSize = _memoryPoolInitialBufferSize.Value; } var storage = _storageSection.ObtainInstance(); var ret = new SerializingObjectStorage(); ret.Initialize(new SerializingObjectStorageConfig { StreamPool = memoryPool, Storage = storage }); return(ret); }
public ReliableChannel(MemoryStreamPool memoryStreamPool, ILogManager logManager, ChannelDescriptor descriptor, IChannelConnection connection, bool ordered) : base(memoryStreamPool, logManager, descriptor, connection) { this.ordered = ordered; this.recvWindowStart = 0; this.ackWindowStart = 0; if (ordered) { this.recvWithheld = new Datagram[WINDOW_SIZE]; } else { this.recvEarlyReceived = new bool[WINDOW_SIZE]; } this.sendPendingPackets = new PendingPacket[WINDOW_SIZE]; this.sendDelayedPackets = new ConcurrentQueue <Datagram>(); }
private static string CreateDebugWatermark(IMapDefinition2 mdf, IServerConnection conn, string layerSc) { //Tidy up the CS WKT so that it can display nicely in a watermark StringBuilder cleanCs = new StringBuilder(mdf.CoordinateSystem); cleanCs.Replace("[", "[\n"); cleanCs.Replace("],", "],\n"); string message = null; if (!string.IsNullOrEmpty(layerSc)) { message = string.Format(Strings.DebugWatermarkMessageLayer, mdf.Extents.MinX, mdf.Extents.MinY, mdf.Extents.MaxX, mdf.Extents.MaxY, cleanCs.ToString(), layerSc); } else { message = string.Format(Strings.DebugWatermarkMessage, mdf.Extents.MinX, mdf.Extents.MinY, mdf.Extents.MaxX, mdf.Extents.MaxY, cleanCs.ToString()); } string watermarkXml = string.Format(Strings.TextWatermark, message); string resId = "Session:" + conn.SessionID + "//Debug.WatermarkDefinition"; //NOXLATE using (var ms = MemoryStreamPool.GetStream("DebugWatermark", Encoding.UTF8.GetBytes(watermarkXml))) //NOXLATE { conn.ResourceService.SetResourceXmlData(resId, ms); } //Add watermark to Map Definition var wmd = (IWatermarkDefinition)conn.ResourceService.GetResource(resId); mdf.AddWatermark(wmd); return(resId); }
internal SocketPool( IPEndPoint destination, SocketSettings settings ) { this.destination = destination; poolSize = settings.PoolSize; Settings = settings; TimeSpan socketLifetime = new TimeSpan(0, 0, settings.SocketLifetimeMinutes, 0, 0); socketLifetimeTicks = socketLifetime.Ticks; if (SocketClient.Config.UseSharedBufferPool) { rebufferedStreamPool = SocketManager.Instance.SharedBufferPool; } else { rebufferedStreamPool = new MemoryStreamPool(settings.InitialMessageSize, settings.BufferReuses); } }
public byte[] GetBytes() { byte[] blockData; // Block Total Length var blockLength = FixedBlockSize + this.timestampedData.Data.GetAlignedLength(); using (var ms = MemoryStreamPool.Get(blockLength)) { using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) { writer.Write(this); } blockData = ms.ToArray(); } return(blockData); }
public void TileSeeder_WithDefaultWalkerAndCustomBounds() { var mdfId = "Library://Samples/Sheboygan/MapsTiled/Sheboygan.MapDefinition"; var mdf = ObjectFactory.CreateMapDefinition(new Version(1, 0, 0), "Sheboygan"); mdf.ResourceID = mdfId; mdf.InitBaseMap(); var group = mdf.BaseMap.AddBaseLayerGroup("Base Layer Group"); group.Visible = true; foreach (double scale in TileTests.SCALE_LIST.Reverse()) { mdf.BaseMap.AddFiniteDisplayScale(scale); } mdf.SetExtents(-87.764986990962839, 43.691398128787782, -87.695521510899724, 43.797520000480347); var ovExtents = ObjectFactory.CreateEnvelope(-87.7278601614039, 43.7443959276596, -87.7135994943579, 43.7592852552018); var walkOptions = new DefaultTileWalkOptions(mdf); walkOptions.MetersPerUnit = 111319.490793274; walkOptions.OverrideExtents = ovExtents; var walker = new DefaultTileWalker(walkOptions); var mockTileSvc = new Mock <ITileService>(); mockTileSvc .Setup(t => t.GetTile(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>())) .Returns(() => MemoryStreamPool.GetStream()); var options = new TileSeederOptions(); var seeder = new TileSeeder(mockTileSvc.Object, walker, options); var stats = seeder.Run(); Assert.Equal(mdfId, stats.ResourceID); //I don't know the exact number here, but it should be less than the original and //greater than the bogus amount of 10 tiles Assert.True(stats.TilesRendered < 127472); Assert.True(stats.TilesRendered > 10); mockTileSvc.Verify(t => t.GetTile(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>()), Times.Exactly(stats.TilesRendered)); }
public void CopyIfStreamChanged_MemoryStreamPool() { var pool = new MemoryStreamPool(); var expected = pool.Rent(); pool.Return(expected); using (var writer = pool.CreateStreamWriter()) { writer.WriteLine("bar"); writer.Flush(); Assert.IsTrue(MonoAndroidHelper.CopyIfStreamChanged(writer.BaseStream, temp), "Should write on new file."); FileAssert.Exists(temp); } var actual = pool.Rent(); Assert.AreSame(expected, actual); Assert.AreEqual(0, actual.Length); }
/// <summary> /// Start listening for connections. /// </summary> /// <param name="connectionWhiteList">An optional <see cref="ConnectionWhitelist"/> /// used to evaluate whether incoming and existing connections are /// whitelisted.</param> public void Start(ConnectionWhitelist connectionWhiteList) { if (isRunning) { if (log.IsWarnEnabled) log.Warn("Attempt to start already running socket server on port " + this.portNumber); return; } this.connectionWhitelist = connectionWhiteList; InitializeCounters(); GetConfig(); if (log.IsInfoEnabled) log.InfoFormat("Using a memory stream pool with an initial size of {0} bytes, reusing buffers {1} times. Reusing connectionstates {2} times. Using {3} sync threads and {4} one way threads", config.InitialMessageSize, config.BufferPoolReuses, config.ConnectionStateReuses, SyncThreads, OnewayThreads); bufferPool = new MemoryStreamPool(config.InitialMessageSize, config.BufferPoolReuses); if (countersInitialized) { bufferPool.AllocatedItemsCounter = this.allocatedBuffers; } buildConnectionStateDelegate = new ResourcePool<ConnectionState>.BuildItemDelegate(BuildConnectionState); resetConnectionStateDelegate = new ResourcePool<ConnectionState>.ResetItemDelegate(ResetConnectionState); connectionStatePool = new ResourcePool<ConnectionState>(buildConnectionStateDelegate, resetConnectionStateDelegate); connectionStatePool.MaxItemReuses = config.ConnectionStateReuses; listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); connections = new ConnectionList(socketCountCounter); connectionCheckInterval = config.ConnectionCheckIntervalSeconds * 1000; connectionWatcher = new Timer(new TimerCallback(CheckConnections), null, connectionCheckInterval, connectionCheckInterval); int currentWorker, currentCompletion, oldWorker, oldCompletion; ThreadPool.GetAvailableThreads(out currentWorker, out currentCompletion); oldWorker = currentWorker; oldCompletion= currentCompletion; if (config.MaximumWorkerThreads > 0) currentWorker = config.MaximumWorkerThreads; if (config.MaximumCompletionPortThreads > 0) currentCompletion = config.MaximumCompletionPortThreads; bool maxSet = ThreadPool.SetMaxThreads(currentWorker, currentCompletion); if (config.MaximumWorkerThreads > 0 || config.MaximumCompletionPortThreads > 0) { if (maxSet == false) { if (log.IsWarnEnabled) { log.WarnFormat("FAILED to change max ThreadPool threads from ({0}worker/{1}completion) to ({2}worker/{3}completion)", oldWorker, oldCompletion, currentWorker, currentCompletion); } } else { if (log.IsInfoEnabled) log.InfoFormat("Successfully changed max ThreadPool threads from ({0}worker/{1}completion) to ({2}worker/{3}completion)", oldWorker, oldCompletion, currentWorker, currentCompletion); } } //kick off CCR OnewayDispatcher = new Dispatcher(OnewayThreads, ThreadPriority.Normal, true, "Socket Server OneWay:" + portNumber.ToString()); SyncDispatcher = new Dispatcher(SyncThreads, ThreadPriority.AboveNormal, true, "Socket Server Sync:" + portNumber.ToString()); OnewayMessageQueue = new DispatcherQueue("Socket Server " + portNumber.ToString() + " one way", OnewayDispatcher, TaskExecutionPolicy.ConstrainQueueDepthThrottleExecution, config.OnewayQueueDepth); SyncMessageQueue = new DispatcherQueue("Socket Server " + portNumber.ToString() + " sync", SyncDispatcher, TaskExecutionPolicy.ConstrainQueueDepthThrottleExecution, config.SyncQueueDepth); Arbiter.Activate(OnewayMessageQueue, Arbiter.Receive<ProcessState>(true, OnewayMessagePort, delegate(ProcessState state) { ProcessCall(state); })); Arbiter.Activate(SyncMessageQueue, Arbiter.Receive<ProcessState>(true, SyncMessagePort, delegate(ProcessState state) { ProcessCall(state); })); listener.Bind(new IPEndPoint(IPAddress.Any, this.portNumber)); listener.Listen(500); isRunning = true; listener.BeginAccept(acceptCallBack, listener); timerThread = new Thread(TimerStart); timerThread.IsBackground = true; timerThread.Start(); if (log.IsInfoEnabled) log.Info("MySpace SocketTransport Server started on port " + this.portNumber); }