private void Insert(ILetter node, string word, int minWordSize) { for (int i = 1; i < word.Length; i++) { var c = (byte)word[i]; // add a child node since it does not exist if (!node.Children.ContainsKey(c)) { node.Children.Add(c, new Node(c)); node = node.Children[c]; } // use existing child node since it already exists else { node = node.Children[c]; } } // flag the final node as a word if it is long enough if (word.Length >= minWordSize) { node.IsWord = true; } }
public void Add(ILetter letter) { if((letter.Options & LetterOptions.Ack) == LetterOptions.Ack) _batchOptions = LetterOptions.Ack; _letters.Enqueue(letter); }
public byte[] Serialize(ILetter letter) { var ms = new MemoryStream(); WriteMetadata(letter, ms); WriteParts(letter, ms); WriteTotalLength(ms); return ms.ToArray(); }
private void Requeue(IChannel channel, ILetter letter) { _letterDispatcher.EnqueueLetter(letter); Action <ILetter, IRequeuedEventArgs> evnt = Requeued; if (evnt != null) { evnt(letter, new RequeuedEventArgs { Socket = this, RemoteNodeId = channel.RemoteNodeId }); } }
private void ChannelOnFailedToSend(IChannel channel, ILetter letter) { _sentBatch = false; if (letter.Type == LetterType.Batch) { FailedQueuedLetters(); } else { FailedToSend(this, letter); } }
public void Send(ILetter letter) { Action <ILetter, IQueuingEventArgs> evnt = Queuing; if (evnt != null) { evnt(letter, new QueuingEventArgs { Socket = this }); } _letterDispatcher.EnqueueLetter(letter); }
private void HandleReceived(byte[] buffer, int length) { int bufferPosition = 0; while (bufferPosition < length) { if (IsNewMessage()) { int lengthPositionBefore = _lengthPosition; int read = ReadNewLetterLength(buffer, bufferPosition); if (read < 4) { _receiveBuffer.Write(_lengthBuffer, lengthPositionBefore, read); } if (_currentLength == 0) { return; } } if (!_initalized && (_currentLength != 30 && _currentLength != 10)) { HandleSocketError(ShutdownReason.Incompatible); return; } var write = (int)Math.Min(_currentLength - _receiveBuffer.Length, length - bufferPosition); _receiveBuffer.Write(buffer, bufferPosition, write); bufferPosition += write; if (!ReceivedFullLetter()) { return; } ILetter letter = _letterDeserializer.Deserialize(_receiveBuffer.ToArray()); _receiveBuffer = new MemoryStream(); _currentLength = 0; if (letter.Type == LetterType.Initialize) { _initalized = true; } if (letter.Type != LetterType.Heartbeat) { Received(letter); } } }
internal ILetter GetLetterForType(string letterType) { ILetter letter = null; if (!string.IsNullOrEmpty(letterType)) { if (templates.ContainsKey(letterType)) { letter = (ILetter)templates[letterType].Clone(); } } return(letter); }
private void BeginSend(ILetter letter) { _currentLetter = letter; byte[] serializedLetter = _letterSerializer.Serialize(letter); _sendEventArgs.SetBuffer(serializedLetter, 0, serializedLetter.Length); try { bool pending = _socket.SendAsync(_sendEventArgs); if(!pending) EndSend(_sendEventArgs); } catch(Exception) { HandleSocketError(ShutdownReason.Socket); } }
bool IModel.addLetter(string senderID, string recieverID, string tName, string lContent, string Date, string Letterid) { try { ILetter aletter = UserFactory.GetLetter(senderID, recieverID, tName, lContent, Date, Letterid); LetterList.Add(aletter); DataLayer.addNewLetterToDB(aletter); return(true); } catch (System.Exception excep) { return(false); } }
public EnqueueResult Enqueue(ILetter letter) { ChangeTimerState(false); ChangeTimerState(true); if (!_sentBatch) { _stopwatch.Restart(); } _queue.Enqueue(letter); _batchBuilder.Add(letter); return(TrySendBatch(false)); }
private void ChannelFailedToSend(IChannel channel, ILetter letter) { if ((letter.Options & LetterOptions.Multicast) == LetterOptions.Multicast) { Discard(channel, letter); } else if ((letter.Options & LetterOptions.Requeue) == LetterOptions.Requeue) { Requeue(channel, letter); } else { Discard(channel, letter); } }
private void BeginSend(ILetter letter) { _currentLetter = letter; byte[] serializedLetter = _letterSerializer.Serialize(letter); _sendEventArgs.SetBuffer(serializedLetter, 0, serializedLetter.Length); try { bool pending = _socket.SendAsync(_sendEventArgs); if (!pending) { EndSend(_sendEventArgs); } } catch (Exception) { HandleSocketError(ShutdownReason.Socket); } }
internal IAnswerable <TReply> Answer <TValue, TReply>(TValue value, AbstractAnswerable answerable, LetterOptions options) { ILetter letter = CreateLetter(value, options, answerable.ConversationId); var outstanding = new BlockingOutstanding <TReply>(this); _outstandings.Add(answerable.ConversationId, outstanding); _socket.SendTo(letter, answerable.RemoteNodeId); try { outstanding.Wait(); } finally { _outstandings.Remove(answerable.ConversationId); } return(outstanding.Result); }
public void SendTo(ILetter letter, Guid toNodeId) { if (_routeChannels.TryGetValue(toNodeId, out IChannel channel)) { channel.Enqueue(letter); } else { Action <ILetter, IDiscardedEventArgs> evnt = Discarded; if (evnt != null && (letter.Options & LetterOptions.SilentDiscard) != LetterOptions.SilentDiscard) { evnt(letter, new DiscardedEventArgs { Socket = this, RemoteNodeId = toNodeId }); } } }
private void EndSend(SocketAsyncEventArgs socketAsyncEvent) { SocketError status = socketAsyncEvent.SocketError; int sent = socketAsyncEvent.BytesTransferred; if (status != System.Net.Sockets.SocketError.Success || sent == 0) { HandleSocketError(ShutdownReason.Socket); } else { ILetter sentLetter = _currentLetter; Sent(sentLetter); Sending = false; TrySend(); } }
private void OnLetterTypeSelectedIndexChanged(object sender, EventArgs e) { int item = cmbBoxType.SelectedIndex; currentDetails = lGen.GetLetterForType((string)cmbBoxType.SelectedItem); switch (item) { case 0: case 1: DisplayDefaultLetter(); break; default: DisplayTBD(); break; } }
public IAnswerable <TReply> Send <TRequest, TReply>(TRequest value, LetterOptions options = LetterOptions.None) { Guid conversationId = Guid.NewGuid(); ILetter letter = CreateLetter(value, options, conversationId); var outstanding = new BlockingOutstanding <TReply>(this); _outstandings.Add(conversationId, outstanding); _socket.Send(letter); try { outstanding.Wait(); } finally { _outstandings.Remove(conversationId); } return(outstanding.Result); }
private void SocketOnReceived(ILetter letter, IReceivedEventArgs receivedEventArgs) { if (letter.Parts.Length != 2) { return; } var metadata = Serializer.Deserialize <Metadata>(letter.Parts[0]); Type messageType = Type.GetType(metadata.Type); if (messageType == null) { return; } TriggerOutstanding(metadata, letter, receivedEventArgs); TriggerRegistrations(messageType, metadata, letter, receivedEventArgs); }
private void HandleLetterSent(ILetter sentLetter) { switch (sentLetter.Type) { case LetterType.Initialize: HandleInitialize(); break; case LetterType.Batch: case LetterType.User: Sent(this, sentLetter); if (_queue.Count == 0 && ChannelQueueEmpty != null) { ChannelQueueEmpty(this); } break; } }
private void ChannelOnSent(IChannel channel, ILetter letter) { if (letter.Type == LetterType.Batch) { _sentBatch = false; for (int i = 0; i < letter.Parts.Length; i++) { Sent(this, _queue.Dequeue()); } } else { Sent(this, _queue.Dequeue()); } ChannelQueueEmpty(this); TrySendBatch(false); }
private void HandleLetterSent(ILetter sentLetter) { switch (sentLetter.Type) { case LetterType.Initialize: HandleInitialize(); break; case LetterType.Heartbeat: NotifyOnEmptyQueue(); break; case LetterType.Batch: case LetterType.User: Sent?.Invoke(this, sentLetter); NotifyOnEmptyQueue(); break; } }
private void SendTask() { while (true) { try { ILetter letter = GetNextLetter(); if (IsMulticastLetter(letter)) { SendMulticastLetter(letter); } else { SendUnicastLetter(letter); } } catch (OperationCanceledException) { break; } catch (Exception) { } } }
public virtual List <ILetter> GetAllLetters() { List <ILetter> LetterList = new List <ILetter>(); try { dataset = new DataSet(); string sql = "SELECT * From StoredLetters"; dataAdapter = new SqlDataAdapter(sql, connection); commandBuilder = new SqlCommandBuilder(dataAdapter); //Generates dataAdapter.Fill(dataset, "UsersData"); totUsers = dataset.Tables["UsersData"].Rows.Count; for (int i = 0; i < totUsers; i++) { DataRow dRow = dataset.Tables["UsersData"].Rows[i]; // Using a Factory to create the user entity object. ie seperating object creation from business logic ILetter letter = UserFactory.GetLetter(dRow.ItemArray.GetValue(0).ToString(), dRow.ItemArray.GetValue(1).ToString(), dRow.ItemArray.GetValue(2).ToString(), dRow.ItemArray.GetValue(3).ToString(), dRow.ItemArray.GetValue(4).ToString(), dRow.ItemArray.GetValue(5).ToString()); LetterList.Add(letter); } } catch (System.Exception excep) { MessageBox.Show(excep.Message); if (connection.State.ToString() == "Open") { connection.Close(); } Application.Exit(); } return(LetterList); }
private void TrySend(ILetter letter = null) { if (_shutdownRequested) { return; } lock (this) { if (Sending) { if (letter != null) { _queue.Enqueue(letter); } return; } if (_queue.TryDequeue(out _currentLetter)) { if (letter != null) { _queue.Enqueue(letter); } } else if (letter == null) { return; } else { _currentLetter = letter; } Sending = true; BeginSend(_currentLetter); } }
private void ChannelOnReceived(ILetter letter, ReceivedEventArgs receivedEventArgs) { Received(letter, receivedEventArgs); }
public EnqueueResult Enqueue(ILetter letter) { if(!CanSend && (letter.Type == LetterType.User || letter.Type == LetterType.Batch)) { FailedToSend(this, letter); return EnqueueResult.CantEnqueueMore; } _queue.Enqueue(letter); _transmitter.Enqueue(letter); return EnqueueResult.CantEnqueueMore; }
public void Enqueue(ILetter letter) { TrySend(letter); }
private void HandleLetterSent(ILetter sentLetter) { switch(sentLetter.Type) { case LetterType.Initialize: HandleInitialize(); break; case LetterType.Batch: case LetterType.User: Sent(this, sentLetter); if(_queue.Count == 0 && ChannelQueueEmpty != null) { ChannelQueueEmpty(this); } break; } }
private void Discard(IChannel channel, ILetter letter) { Action<ILetter, IDiscardedEventArgs> evnt = Discarded; if(evnt != null && (letter.Options & LetterOptions.SilentDiscard) != LetterOptions.SilentDiscard) evnt(letter, new DiscardedEventArgs {Binding = channel.Binding, Socket = this, RemoteNodeId = channel.RemoteNodeId}); }
public LetterPrinter(ILetter[] letters) { _letters = letters; }
private void Requeue(IChannel channel, ILetter letter) { _letterDispatcher.EnqueueLetter(letter); Action<ILetter, IRequeuedEventArgs> evnt = Requeued; if(evnt != null) evnt(letter, new RequeuedEventArgs {Socket = this, RemoteNodeId = channel.RemoteNodeId}); }
public void EnqueueLetter(ILetter letter) { _letterQueue.TryAdd(letter); }
public void Send(ILetter letter) { Action<ILetter, IQueuingEventArgs> evnt = Queuing; if(evnt != null) evnt(letter, new QueuingEventArgs {Socket = this}); _letterDispatcher.EnqueueLetter(letter); }
public void SendTo(ILetter letter, Guid toNodeId) { IChannel channel; if(_routeChannels.TryGetValue(toNodeId, out channel)) channel.Enqueue(letter); else { Action<ILetter, IDiscardedEventArgs> evnt = Discarded; if(evnt != null && (letter.Options & LetterOptions.SilentDiscard) != LetterOptions.SilentDiscard) evnt(letter, new DiscardedEventArgs {Socket = this, RemoteNodeId = toNodeId}); } }
private void ChannelOnSent(IChannel channel, ILetter letter) { if(letter.Type == LetterType.Batch) { _sentBatch = false; for(int i = 0; i < letter.Parts.Length; i++) Sent(this, _queue.Dequeue()); } else Sent(this, _queue.Dequeue()); ChannelQueueEmpty(this); TrySendBatch(false); }
private void ChannelReceived(ILetter letter, ReceivedEventArgs receivedEventArgs) { Action<ILetter, IReceivedEventArgs> evnt = Received; if(evnt != null) { receivedEventArgs.Socket = this; evnt(letter, receivedEventArgs); } }
private static bool IsMulticastLetter(ILetter letter) { return (letter.Options & LetterOptions.Multicast) == LetterOptions.Multicast; }
private void QueueAck(ILetter letter) { _receivedQueue.Enqueue(letter); _transmitter.Enqueue(AckLetter); }
private void TransmitterOnSent(ILetter sentLetter) { ResetHeartbeatTimer(); if(sentLetter.Type == LetterType.Ack) HandleAckSent(); else if((sentLetter.Options & LetterOptions.Ack) != LetterOptions.Ack) HandleLetterSent(_queue.Dequeue()); }
/// <summary> /// reply a letter in post mode /// </summary> /// <param name="toReply"></param> /// <param name="title"></param> /// <param name="content"></param> public void Reply(ILetter toReply, string title, object content = null, int timeout = int.MaxValue) { Send(toReply.Sender, title, content, StatusCode.Post, timeout); }
private void SendUnicastLetter(ILetter letter) { IChannel channel = GetNextChannel(); EnqueueResult result = channel.Enqueue(letter); if(result == EnqueueResult.CanEnqueueMore) { _channelQueue.TryAdd(channel); } }
private void ChannelSent(IChannel channel, ILetter letter) { Sent?.Invoke(letter, new SentEventArgs { Binding = channel.Binding, Socket = this, RemoteNodeId = channel.RemoteNodeId }); }
/// <summary> /// Counts the number of times a letter is guessed. /// </summary> /// <param name="letter">Letter to be guessed.</param> /// <returns>Integer value.</returns> public int GuessLetter(ILetter letter) { int guessedLetter = 0; foreach (var symbol in this.Word.Content) { if (!symbol.IsFound && symbol.Value == letter.Value) { symbol.IsFound = true; guessedLetter++; } } return guessedLetter; }
private void ChannelSent(IChannel channel, ILetter letter) { Action<ILetter, ISentEventArgs> evnt = Sent; if(evnt != null) evnt(letter, new SentEventArgs {Binding = channel.Binding, Socket = this, RemoteNodeId = channel.RemoteNodeId}); }
public State(ILetter letter, IState next) { this.letter = letter; this.next = next; }
public abstract void SetResult(Metadata metadata, ILetter letter, IReceivedEventArgs receivedEventArgs);
public EnqueueResult Enqueue(ILetter letter) { ChangeTimerState(false); ChangeTimerState(true); if(!_sentBatch) _stopwatch.Restart(); _queue.Enqueue(letter); _batchBuilder.Add(letter); return TrySendBatch(false); }
/// <summary> /// Sets the state of the letter if it is guessed or not. /// </summary> /// <param name="letter">Letter to be guessed.</param> /// <returns>Boolean is letter guessed.</returns> public bool IsLetterGuessed(ILetter letter) { int guessed = this.GuessLetter(letter); if (guessed > 0) { this.Player.Score += 5; return true; } else if (this.Player.Score > 0) { this.Player.Score--; } return false; }
private void ChannelOnFailedToSend(IChannel channel, ILetter letter) { _sentBatch = false; if(letter.Type == LetterType.Batch) FailedQueuedLetters(); else FailedToSend(this, letter); }
// NFA construction public static ProtoState Accept(ILetter letter) => exit => new State(letter, exit);
public void method(ILetter l) { l.execute(); }
private void ReceiverReceived(ILetter receivedLetter) { ResetHeartbeatTimer(); LetterType letterType = receivedLetter.Type; if(letterType == LetterType.Ack) { HandleLetterSent(_queue.Dequeue()); } else { if((receivedLetter.Options & LetterOptions.Ack) == LetterOptions.Ack) { if(_options.Notification.ReceivedNotifyOnAllAckStates && ((letterType & LetterType.User) == LetterType.User || (letterType & LetterType.Batch) == LetterType.Batch)) HandleReceivedLetter(receivedLetter, AckState.BeforeAck); QueueAck(receivedLetter); } else { HandleReceivedLetter(receivedLetter, AckState.NoAck); } } }
public abstract void Invoke(TypedHyperSocket socket, ILetter letter, Metadata metadata, Type concreteType, IReceivedEventArgs receivedEventArgs);
private void HandleReceivedLetter(ILetter receivedLetter, AckState ackState) { switch(receivedLetter.Type) { case LetterType.Initialize: RemoteNodeId = new Guid(receivedLetter.Parts[0]); HandleInitialize(); break; case LetterType.Shutdown: _remoteShutdownRequested = true; ChannelDisconnecting(this, ShutdownReason.Remote); break; case LetterType.User: Received(receivedLetter, CreateReceivedEventArgs(ackState)); break; case LetterType.Batch: for(int i = 0; i < receivedLetter.Parts.Length; i++) { ILetter batchedLetter = _letterDeserializer.Deserialize(receivedLetter.Parts[i]); Received(batchedLetter, CreateReceivedEventArgs(ackState)); } break; } }
private void HandleAckSent() { ILetter receivedLetter = _receivedQueue.Dequeue(); HandleReceivedLetter(receivedLetter, AckState.AfterAck); }
private void SendMulticastLetter(ILetter letter) { foreach(IChannel channel in _hyperSocket.Channels) { channel.Enqueue(letter); } }
private void ChannelFailedToSend(IChannel channel, ILetter letter) { if((letter.Options & LetterOptions.Multicast) == LetterOptions.Multicast) { Discard(channel, letter); } else if((letter.Options & LetterOptions.Requeue) == LetterOptions.Requeue) { Requeue(channel, letter); } else { Discard(channel, letter); } }