/// <summary> /// Dispose client manager and connection objects /// </summary> internal void Dispose(bool disposingIntentionally) { lock (_disposeSync) { _disposed = true; if (_enumerationPointers != null && !_cacheStopped) { foreach (string key in _enumerationPointers.Keys) { _cmdExecuter.DisposeEnumerator(_enumerationPointers[key]); } _enumerationPointers = null; } if (_clientSocket != null) { if (_clientSocket.Connected) { try { _clientSocket.Shutdown(SocketShutdown.Both); } catch (SocketException e) { /* log.Append(e.Message); */ } catch (ObjectDisposedException e) { /*log.Append(e.Message);*/ } } if (_clientSocket != null) { _clientSocket.Close(); } _clientSocket = null; } Buffer = null; BufferPool.CheckinBuffer(PinnedBuffer); BufferPool.CheckinBuffer(sendBuffer); PinnedBuffer = null; sendBuffer = null; try { lock (this) { while (this.PendingSendOperationQueue.Count > 0) { object operation = PendingSendOperationQueue.remove(); SendContext opContext = (SendContext)operation; if (opContext != null) { if (SocketServer.IsServerCounterEnabled) { _connectionManager.PerfStatsColl.DecrementResponsesQueueCountStats(); } if (SocketServer.IsServerCounterEnabled) { _connectionManager.PerfStatsColl.DecrementResponsesQueueSizeStats(opContext.expectedSize); } } } } } catch (Exception e) { } ConnectionManager.EventsAndCallbackQueue.UnRegisterSlaveQueue(_slaveId); if (SocketServer.IsServerCounterEnabled) { _connectionManager.PerfStatsColl.SetEventQueueCountStats(ConnectionManager.EventsAndCallbackQueue.Count); } if (_cmdExecuter != null) { if (!_cacheStopped && _raiseClientDisconnectedEvent) { try { _cmdExecuter.OnClientDisconnected(ClientID, UniqueCacheID); } catch (Exception e) { if (SocketServer.Logger.IsErrorLogsEnabled) { SocketServer.Logger.NCacheLog.Error("ClientManager.Dispose", e.ToString()); } } } if (_cmdExecuter != null) { _cmdExecuter.Dispose(); _cmdExecuter = null; } } if (!disposingIntentionally) { _clientDisposed(this.ClientID); } } }
private static void UpdateContext(SendContext context, int dataSendOrReceived) { int remainingBytes = context.expectedSize - dataSendOrReceived; context.expectedSize = remainingBytes; context.offset += dataSendOrReceived; }
private static void AssureSendOld(ClientManager clientManager, byte[] buffer, int count, SendContext context) { TimeSpan tempTaken; try { Send(clientManager, buffer, count); SendContext opContext = null; do { opContext = null; lock (clientManager) { if (clientManager.PendingSendOperationQueue.Count > 0) { object operation = clientManager.PendingSendOperationQueue.remove(); opContext = (SendContext)operation; clientManager.ResetAsyncSendTime(); if (SocketServer.IsServerCounterEnabled) clientManager.ConnectionManager.PerfStatsColl.DecrementResponsesQueueCountStats(); if (SocketServer.IsServerCounterEnabled) clientManager.ConnectionManager.PerfStatsColl.DecrementResponsesQueueSizeStats(opContext.expectedSize); } } if (opContext != null) { Send(opContext.clientManager, opContext.buffer, opContext.expectedSize); } lock (clientManager) { if (clientManager.PendingSendOperationQueue.Count == 0) { clientManager.DeMarkOperationInProcess(); return; } } } while (clientManager.PendingSendOperationQueue.Count > 0); } catch (Exception e) { if (SocketServer.Logger != null && SocketServer.Logger.IsErrorLogsEnabled) SocketServer.Logger.NCacheLog.Error("ConnectionManager.AssureSend", e.ToString()); DisposeClient(clientManager); } }
private static void AssureSend(ClientManager clientManager, byte[] buffer, int count, SendContext context) { try { Send(clientManager, buffer, count); lock (clientManager) { if (clientManager.PendingSendOperationQueue.Count == 0) { clientManager.DeMarkOperationInProcess(); return; } } #if !NET20 try { Task t = null; //as there are times in the queue; let's start sending pending responses in separate thread. TaskFactory factory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.LongRunning); t = factory.StartNew(() => ProccessResponseQueue(clientManager), TaskCreationOptions.LongRunning); } catch (AggregateException aggEx) { } #else ThreadPool.QueueUserWorkItem(new WaitCallback(ProccessResponseQueue), clientManager); #endif } catch (Exception e) { if (SocketServer.Logger != null && SocketServer.Logger.IsErrorLogsEnabled) SocketServer.Logger.NCacheLog.Error("ConnectionManager.AssureSend", e.ToString()); DisposeClient(clientManager); } }
private static void AssureSend(ClientManager clientManager, byte[] buffer, Array userpayLoad, Alachisoft.NCache.Common.Enum.Priority priority) { SendContext context = new SendContext(); context.clientManager = clientManager; context.buffer = buffer; context.expectedSize = buffer.Length; try { bool doOperation = true; if (clientManager != null) { lock (clientManager) { doOperation = clientManager.MarkOperationInProcess(); if (!doOperation) { if (clientManager.IsDisposed) return; clientManager.PendingSendOperationQueue.add(context, priority); if (SocketServer.IsServerCounterEnabled) clientManager.ConnectionManager.PerfStatsColl.IncrementResponsesQueueCountStats(); if (SocketServer.IsServerCounterEnabled) clientManager.ConnectionManager.PerfStatsColl.IncrementResponsesQueueSizeStats(buffer.Length); return; } } AssureSend(clientManager, buffer, buffer.Length, context); } } catch (SocketException se) { DisposeClient(clientManager); } catch (ObjectDisposedException o) { return; } }
private void ReceiveCommmand(IAsyncResult result) { SendContext context = (SendContext)result.AsyncState; ClientManager clientManager = context.clientManager; Alachisoft.NCache.Common.Protobuf.Command command = null; int bytesRecieved = 0; try { if (clientManager.ClientSocket == null) return; bytesRecieved = clientManager.ClientSocket.EndReceive(result); clientManager.AddToClientsBytesRecieved(bytesRecieved); if (SocketServer.IsServerCounterEnabled) PerfStatsColl.IncrementBytesReceivedPerSecStats(bytesRecieved); if (bytesRecieved == 0) { DisposeClient(clientManager); return; } if (bytesRecieved < context.expectedSize) { UpdateContext(context, bytesRecieved); clientManager.ClientSocket.BeginReceive(context.buffer, context.offset, context.expectedSize, SocketFlags.None, new AsyncCallback(ReceiveCommmand), context); } if (bytesRecieved == context.expectedSize) { byte[] buffer = context.buffer; using (MemoryStream stream = new MemoryStream(buffer, 0, (int)context.totalExpectedSize)) { command = ProtoBuf.Serializer.Deserialize<Alachisoft.NCache.Common.Protobuf.Command>(stream); stream.Close(); } clientManager.ReinitializeBuffer(); context = new SendContext(); context.clientManager = clientManager; context.buffer = clientManager.Buffer; context.expectedSize = clientManager.discardingBuffer.Length; clientManager.ClientSocket.BeginReceive(context.buffer, context.offset, context.expectedSize, SocketFlags.None, new AsyncCallback(ReceiveDiscardLength), context); if (ServerMonitor.MonitorActivity) { ServerMonitor.RegisterClient(clientManager.ClientID, clientManager.ClientSocketId); ServerMonitor.StartClientActivity(clientManager.ClientID); ServerMonitor.LogClientActivity("ConMgr.RecvClbk", "enter"); } if (SocketServer.Logger.IsDetailedLogsEnabled) SocketServer.Logger.NCacheLog.Info("ConnectionManager.ReceiveCallback", clientManager.ToString() + " COMMAND to be executed : " + command.type.ToString() + " RequestId :" + command.requestID); clientManager.AddToClientsRequest(1); if (SocketServer.IsServerCounterEnabled) PerfStatsColl.IncrementRequestsPerSecStats(1); clientManager.StartCommandExecution(); cmdManager.ProcessCommand(clientManager, command); if (SocketServer.Logger.IsDetailedLogsEnabled) SocketServer.Logger.NCacheLog.Info("ConnectionManager.ReceiveCallback", clientManager.ToString() + " after executing COMMAND : " + command.type.ToString() + " RequestId :" + command.requestID); } } catch (SocketException so_ex) { if (ServerMonitor.MonitorActivity) ServerMonitor.LogClientActivity("ConMgr.RecvClbk", "Error :" + so_ex.ToString()); DisposeClient(clientManager); return; } catch (Exception e) { if (ServerMonitor.MonitorActivity) ServerMonitor.LogClientActivity("ConMgr.RecvClbk", "Error :" + e.ToString()); if (SocketServer.Logger.IsErrorLogsEnabled) SocketServer.Logger.NCacheLog.Error("ConnectionManager.ReceiveCallback", clientManager.ToString() + command + " Error " + e.ToString()); DisposeClient(clientManager); return; } finally { clientManager.StopCommandExecution(); if (ServerMonitor.MonitorActivity) ServerMonitor.StopClientActivity(clientManager.ClientID); } }