void _listener_OnConnect(StompServerClient client) { lock (this) { if (_clients.Count + 1 > StompConfiguration.MaxClients) { if (StompLogger.CanLogWarning) { StompLogger.LogWarning("Maximum clients (" + StompConfiguration.MaxClients + ") reached. Disconnecting " + client.ToString()); } client.Stop(); return; } _clients.Add(client); } InitClientsEvents(client); StompStatistics.AddConnectedClient(); client.Start(); if (StompLogger.CanLogDebug) { StompLogger.LogDebug(client.ToString() + " connected"); } }
public void Send(byte[] buffer, int startIndex, int length) { StompStatistics.CountOutgoingMessage(); lock (_outgoingBuffers) { _outgoingBuffers.Add(new ArraySegment <byte>(buffer, startIndex, length)); Flush(); } }
void client_OnDisconnect(StompServerClient client) { lock (this) { if (StompLogger.CanLogDebug) { StompLogger.LogDebug(client.ToString() + " quitted"); } _clients.Remove(client); } StompStatistics.RemoveConnectedClient(); }
private void OnClientReceive(IAsyncResult ar) { try { int readBytes = _socket.EndReceive(ar); if (readBytes > 0) { _buffer.Cursor += readBytes; int localCursor = 0; for (int i = 0; i < _buffer.Cursor; i++) { if (_buffer.Buffer[i] == '\0') // match { StompMessage message = null; try { message = new StompMessage(_buffer.Buffer, localCursor, i - localCursor); StompStatistics.CountIncomingMessage(); if (OnMessageReceived != null) { try { OnMessageReceived(this, message); } catch (Exception) { } } } catch (Exception ex) { if (StompLogger.CanLogException) { StompLogger.LogException("Failed to parse stomp packet", ex); } } localCursor = i + 1; } } if (localCursor > 0) { _buffer.Remove(localCursor); } BeginReceive(); } else { throw new Exception("Connection closed."); } } catch (Exception) { OnInternalDisconnect(); } }
static void Main(string[] args) { if (StompLogger.CanLogInfo) { StompLogger.LogInfo("AcidStomp " + Assembly.GetExecutingAssembly().GetName().Version.ToString() + " - Starting in port " + StompConfiguration.ListenPort); } try { StompServer server = new StompServer(StompConfiguration.ListenPort); StompStatistics.Start(); server.Start(); DateTime lastLogFlush = DateTime.Now; DateTime lastStatistics = DateTime.Now; while (server.IsRunning) { try { if (StompConfiguration.LogStatisticsInterval > 0 && (DateTime.Now - lastStatistics).TotalSeconds >= StompConfiguration.LogStatisticsInterval) { StompLogger.LogInfo( String.Format("(uptime {0}, connected clients: {1}, messages [in: {2}/sec - out: {3}/sec])", StompStatistics.Uptime.ToString(), StompStatistics.ConnectedClients, StompStatistics.IncomingMessagesPerSecond, StompStatistics.OutgoingMessagesPerSecond)); lastStatistics = DateTime.Now; } if (StompConfiguration.LogFile != null) { if ((DateTime.Now - lastLogFlush).TotalSeconds >= StompConfiguration.LogFileFlushInterval) { StompLogger.Flush(); lastLogFlush = DateTime.Now; } } } catch (Exception ex) { if (StompLogger.CanLogException) { StompLogger.LogException("Failed to flush log file", ex); } } Thread.Sleep(1000); } } catch (Exception ex) { if (StompLogger.CanLogException) { StompLogger.LogException("Error initializing listen server ", ex); } } }