public ClientServer(SendMappings sendMappings, string name, NetConnection connection) { Name = name ?? throw new ArgumentNullException(nameof(name)); Connection = connection ?? throw new ArgumentNullException(nameof(connection)); _pendingMessages = new PendingMessages(sendMappings ?? throw new ArgumentNullException(nameof(sendMappings))); }
public void SendPendingMessages(bool resumed) { if (resumed) { //Resend any messages waiting an Ack Queue foreach (var message in AckProcessor.GetQueuedMessages()) { SendToTransport(message); } } lock (_pendingQueueLock) { if (Logger.IsDebug) { Logger.Debug("Sending pending message: Count: " + PendingMessages.Count); } while (PendingMessages.Count > 0) { var queuedMessage = PendingMessages.Dequeue(); SendMessage(queuedMessage.Message, queuedMessage.Callback); } } }
private void Close() { if (_isDisposed) { return; } _isDisposed = true; try { Properties.SetReadOnly(); PendingMessages.SetReadOnly(); BranchHistory.SetReadOnly(); if (SuppressEmtpyContextMessages && PendingMessages.IsEmpty) { SuppressMessages = true; } _startTimer.Stop(); if (SuppressMessages != true) { LogConfig.Current.Engine.OnContextClose(this); } } finally { Context.Context.Unregister <LogCastContext>(); } }
private ServerClient( SendMappings sendMappings, NetConnection connection, ITime engineTime, NetworkObjectListTransmitter objectListTransmitter, int index, int userId, string name) { _sendMappings = sendMappings ?? throw new ArgumentNullException(nameof(sendMappings)); Connection = connection ?? throw new ArgumentNullException(nameof(connection)); _engineTime = engineTime ?? throw new ArgumentNullException(nameof(engineTime)); if (objectListTransmitter == null) { throw new ArgumentNullException(nameof(objectListTransmitter)); } FrameListTransmitter = objectListTransmitter.CreateTransmitter(this); Index = index; UserId = userId; Name = name ?? throw new ArgumentNullException(nameof(name)); _reliableMessages = new PendingMessages(_sendMappings); _unreliableMessages = new PendingMessages(_sendMappings); }
public void Dispose() { if (PendingMessages != null) { PendingMessages.Dispose(); PendingMessages = null; } }
/// <summary> /// Push a message to be sent. /// </summary> /// <param name="message">The message.</param> public void PushMessage(Message message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } PendingMessages.Enqueue(message); }
public void Send(ProtocolMessage message, Action <bool, ErrorInfo> callback = null, ChannelOptions channelOptions = null) { if (Logger.IsDebug) { Logger.Debug($"Current state: {Connection.State}. Sending message: {message}"); } if (message.ConnectionId.IsNotEmpty()) { Logger.Warning("Setting ConnectionId to null. ConnectionId should never be included in an outbound message on a realtime connection, it’s always implicit"); message.ConnectionId = null; } var result = VerifyMessageHasCompatibleClientId(message); if (result.IsFailure) { callback?.Invoke(false, result.Error); return; } // Encode message/presence payloads Handler.EncodeProtocolMessage(message, channelOptions); if (State.CanSend) { SendMessage(message, callback); return; } if (State.CanQueue) { if (Options.QueueMessages) { lock (_pendingQueueLock) { if (Logger.IsDebug) { Logger.Debug($"Queuing message with action: {message.Action}. Connection State: {ConnectionState}"); } PendingMessages.Enqueue(new MessageAndCallback(message, callback)); } } else { throw new AblyException( $"Current state is [{State.State}] which supports queuing but Options.QueueMessages is set to False.", Connection.ConnectionState.DefaultErrorInfo.Code, HttpStatusCode.ServiceUnavailable); } return; } throw new AblyException($"The current state [{State.State}] does not allow messages to be sent."); }
private void RejectPendingMessages(Exception ex = null) { ex = ex ?? new ClientDisconnectedException(); foreach (var pair in PendingMessages.ToArray()) { pair.Value?.CompletionSource?.TrySetException(ex); } PendingMessages.Clear(); }
/// <summary> /// Processes the response. /// </summary> /// <param name="response">The response.</param> private void ProcessResponse(Response response) { if (PendingMessages.TryRemove(response.Id, out var pendingMessage)) { pendingMessage.Source.TrySetResult(response); } else { Console.WriteLine($"Got unknown response to {response.Id}"); } }
/// <summary> /// Closes the chatroom /// </summary> public void Close() { var presence = new Presence { Id = XmppIdentifierGenerator.Generate(), To = Identifier, Type = PresenceType.Unavailable }; PendingMessages.Add(presence.Id); Session.Send(presence); }
/// <summary> /// Place the passed message into this queue processor's queue /// </summary> /// <param name="message"></param> public void SendMessage(OutgoingMessage message) { if (ThisQueueState == QueueState.Accepting) { PendingMessages.Enqueue(message); Log.Debug($"Queued message {message.Message} with {Priority} priority for delivery"); } else { Log.Warning($"{Priority} asked to accept message {message.Message} but processor is closed."); Log.Warning("Message not sent. This is probably okay."); } }
public void Send(ProtocolMessage message, Action <bool, ErrorInfo> callback = null, ChannelOptions channelOptions = null) { if (Logger.IsDebug) { Logger.Debug($"Current state: {Connection.State}. Sending message: {message}"); } var result = VerifyMessageHasCompatibleClientId(message); if (result.IsFailure) { callback?.Invoke(false, result.Error); return; } //Encode message/presence payloads Handler.EncodeProtocolMessage(message, channelOptions); if (State.CanSend) { SendMessage(message, callback); return; } if (State.CanQueue) { if (Options.QueueMessages) { lock (_pendingQueueLock) { if (Logger.IsDebug) { Logger.Debug($"Queuing message with action: {message.Action}. Connection State: {ConnectionState}"); } PendingMessages.Enqueue(new MessageAndCallback(message, callback)); } } else { throw new AblyException($"Current state is [{State.State}] which supports queuing but Options.QueueMessages is set to False."); } return; } throw new AblyException($"The current state [{State.State}] does not allow messages to be sent."); }
/// <summary> /// Performs the gateway unregistration process /// </summary> public void Unregister() { var query = new RegisterQuery(); var iq = new IQ(); iq.ID = XmppIdentifierGenerator.Generate(); iq.Type = IQType.Set; iq.From = Session.UserId; iq.To = Identifier; iq.Items.Add(query); query.Remove = ""; PendingMessages.Add(iq.ID); Session.Send(iq); }
/// <summary> /// Performs the gateway registration process /// </summary> /// <param name = "username"></param> /// <param name = "password"></param> public void Register(string username, string password) { var query = new RegisterQuery(); var iq = new IQ(); iq.ID = XmppIdentifierGenerator.Generate(); iq.Type = IQType.Set; iq.From = Session.UserId; iq.To = Identifier; iq.Items.Add(query); query.UserName = username; query.Password = password; PendingMessages.Add(iq.ID); Session.Send(iq); }
/// <summary> /// Sends the message and waits for the response. /// </summary> /// <param name="message">The message.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <param name="timeout">The timeout in milliseconds.</param> /// <returns>The response to the message.</returns> public async Task <Response> SendMessageAsync(Message message, CancellationToken cancellationToken, int timeout = 30000) { var tcs = new TaskCompletionSource <Response>(); using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken)) { if (timeout != Timeout.Infinite) { cts.CancelAfter(timeout); } using (cts.Token.Register(() => tcs.SetCanceled(), false)) { var pendingMessage = new PendingMessage(tcs); PendingMessages.AddOrUpdate(message.Id, (_) => pendingMessage, (_1, _2) => pendingMessage); SendNotification(message); return(await tcs.Task); } } }
/// <summary> /// try to add a new client's mac address. /// </summary> /// <param name="clientAddress">The new client's address.</param> /// <returns><c>true</c> if the client was added successfully, <c>false</c> if it was already registered.</returns> public bool TryAddClient(string clientAddress) { if (ConnectedClients.Contains(clientAddress)) { return(false); } lock (ConnectedClients) { ConnectedClients.Add(clientAddress); } // Initialize the pending messages for this client if (!PendingMessages.ContainsKey(clientAddress)) { lock (PendingMessages) { PendingMessages.Add(clientAddress, new Stack <ChatMessage>()); } } return(true); }
/// <summary> /// try to add a new client's mac address. /// </summary> /// <param name="deviceInfo">The new client's info.</param> /// <returns><c>true</c> if the client was added successfully, <c>false</c> if it was already registered.</returns> public bool TryAddClient(DeviceInfo deviceInfo) { if (ConnectedClients.Any(d => d.MacAddress == deviceInfo.MacAddress)) { return(false); } lock (ConnectedClients) { ConnectedClients.Add(deviceInfo); } // Initialize the pending messages for this client if (!PendingMessages.ContainsKey(deviceInfo.MacAddress)) { lock (PendingMessages) { PendingMessages.Add(deviceInfo.MacAddress, new Queue <ChatMessage>()); } } return(true); }
/// <summary> /// Fires up the thread for this queue processor /// </summary> public void StartProcessor() { Log.Information($"{Priority} QueueProcessor starting up"); ThisProcessorState = ProcessorState.Running; using (var writer = new StreamWriter(Stream) { NewLine = "\r\n", AutoFlush = true }) while (ThisProcessorState == ProcessorState.Running) { if (PendingMessages.Count > 0) { var msg = PendingMessages.Dequeue(); Log.Debug($"Processing message {msg.Message} with priority {Priority}"); writer.WriteLine(msg.Message); Log.Debug($"Message sent! Time in queue: {msg.GetElapsedTimeMillis()}ms"); } else { Thread.Sleep(Interval); } } Log.Information($"{Priority} QueueProcessor has shut down"); }
public void Run() { IsRunning = true; while (IsRunning) { Logger.Log("Broadcaster {0} waiting for connection...", Id); TcpClient client = null; try { client = Listener.AcceptTcpClient(); #if DEBUG == false //timeouts affect debugging when stepping through code client.ReceiveTimeout = 5000; #endif } catch (Exception ex) { Logger.Log("Broadcaster {0} could not accept TCP client: {1}", Id, ex.Message); continue; } Logger.Log("Broadcaster {0} accepting client: {1}", Id, client.Client.RemoteEndPoint); BinaryReader reader = null; BinaryWriter writer = null; try { BufferedStream stream = new BufferedStream(client.GetStream()); reader = new BinaryReader(stream); writer = new BinaryWriter(stream); //grab auth key int authKeyLength = IPAddress.NetworkToHostOrder(reader.ReadInt32()); byte[] authKeyBytes = reader.ReadBytes(authKeyLength); string authKey = Encoding.UTF8.GetString(authKeyBytes); if (Server.ActiveUsers.ContainsKey(authKey) == true) { /* * //send back total count first * var hcc = PendingMessages.Count; * byte[] hccBytes = Encoding.UTF8.GetBytes(hcc.ToString()); * writer.Write(IPAddress.HostToNetworkOrder(hccBytes.Length)); * writer.Write(hccBytes); */ while (true) { if (PendingMessages.Count > 0) { var message = PendingMessages.Dequeue(); byte[] userBytes = Encoding.UTF8.GetBytes(message.Key.Name); writer.Write(IPAddress.HostToNetworkOrder(userBytes.Length)); writer.Write(userBytes); byte[] messageBytes = Encoding.UTF8.GetBytes(message.Value); writer.Write(IPAddress.HostToNetworkOrder(messageBytes.Length)); writer.Write(messageBytes); stream.Flush(); } else { //break; Thread.Sleep(500); } } } } catch (Exception ex) { Logger.Log("Broadcaster {0} exception handling client {1}, {2}: ", Id, client.Client.RemoteEndPoint, ex.Message); } finally { Logger.Log("Broadcaster {0} done handling client {1}", Id, client.Client.RemoteEndPoint); reader.Close(); writer.Close(); if (client != null) { client.Close(); } } } }
public void AddMessage(ChatUser user, string message) { PendingMessages.Enqueue(new KeyValuePair <ChatUser, string>(user, message)); }