示例#1
0
		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();
        }
示例#4
0
        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
                });
            }
        }
示例#5
0
文件: BatchChannel.cs 项目: riax/Clr
        private void ChannelOnFailedToSend(IChannel channel, ILetter letter)
        {
            _sentBatch = false;

            if (letter.Type == LetterType.Batch)
            {
                FailedQueuedLetters();
            }
            else
            {
                FailedToSend(this, letter);
            }
        }
示例#6
0
        public void Send(ILetter letter)
        {
            Action <ILetter, IQueuingEventArgs> evnt = Queuing;

            if (evnt != null)
            {
                evnt(letter, new QueuingEventArgs {
                    Socket = this
                });
            }

            _letterDispatcher.EnqueueLetter(letter);
        }
示例#7
0
        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);
            }
        }
示例#10
0
 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);
     }
 }
示例#11
0
文件: BatchChannel.cs 项目: riax/Clr
        public EnqueueResult Enqueue(ILetter letter)
        {
            ChangeTimerState(false);
            ChangeTimerState(true);

            if (!_sentBatch)
            {
                _stopwatch.Restart();
            }

            _queue.Enqueue(letter);
            _batchBuilder.Add(letter);

            return(TrySendBatch(false));
        }
示例#12
0
 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);
     }
 }
示例#13
0
        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);
            }
        }
示例#14
0
        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);
        }
示例#15
0
 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
             });
         }
     }
 }
示例#16
0
        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();
            }
        }
示例#17
0
        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;
            }
        }
示例#18
0
        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);
        }
示例#19
0
        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);
        }
示例#20
0
        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;
            }
        }
示例#21
0
文件: BatchChannel.cs 项目: riax/Clr
        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);
        }
示例#22
0
        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;
            }
        }
示例#23
0
 private void SendTask()
 {
     while (true)
     {
         try {
             ILetter letter = GetNextLetter();
             if (IsMulticastLetter(letter))
             {
                 SendMulticastLetter(letter);
             }
             else
             {
                 SendUnicastLetter(letter);
             }
         } catch (OperationCanceledException) {
             break;
         } catch (Exception) {
         }
     }
 }
示例#24
0
        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);
        }
示例#25
0
        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);
            }
        }
示例#26
0
 private void ChannelOnReceived(ILetter letter, ReceivedEventArgs receivedEventArgs)
 {
     Received(letter, receivedEventArgs);
 }
示例#27
0
        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;
        }
示例#28
0
 public void Enqueue(ILetter letter)
 {
     TrySend(letter);
 }
示例#29
0
        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;
            }
        }
示例#30
0
文件: HyperSocket.cs 项目: riax/Clr
 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;
 }
示例#32
0
文件: HyperSocket.cs 项目: riax/Clr
        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});
        }
示例#33
0
 public void EnqueueLetter(ILetter letter)
 {
     _letterQueue.TryAdd(letter);
 }
示例#34
0
文件: HyperSocket.cs 项目: riax/Clr
        public void Send(ILetter letter)
        {
            Action<ILetter, IQueuingEventArgs> evnt = Queuing;
            if(evnt != null)
                evnt(letter, new QueuingEventArgs {Socket = this});

            _letterDispatcher.EnqueueLetter(letter);
        }
示例#35
0
文件: HyperSocket.cs 项目: riax/Clr
 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});
     }
 }
示例#36
0
        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);
        }
示例#37
0
文件: HyperSocket.cs 项目: riax/Clr
 private void ChannelReceived(ILetter letter, ReceivedEventArgs receivedEventArgs)
 {
     Action<ILetter, IReceivedEventArgs> evnt = Received;
     if(evnt != null) {
         receivedEventArgs.Socket = this;
         evnt(letter, receivedEventArgs);
     }
 }
示例#38
0
 private static bool IsMulticastLetter(ILetter letter)
 {
     return (letter.Options & LetterOptions.Multicast) == LetterOptions.Multicast;
 }
示例#39
0
 private void QueueAck(ILetter letter)
 {
     _receivedQueue.Enqueue(letter);
     _transmitter.Enqueue(AckLetter);
 }
示例#40
0
 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);
 }
示例#42
0
        private void SendUnicastLetter(ILetter letter)
        {
            IChannel channel = GetNextChannel();
            EnqueueResult result = channel.Enqueue(letter);

            if(result == EnqueueResult.CanEnqueueMore) {
                _channelQueue.TryAdd(channel);
            }
        }
示例#43
0
 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;
        }
示例#45
0
文件: HyperSocket.cs 项目: riax/Clr
 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});
 }
示例#46
0
 public State(ILetter letter, IState next)
 {
     this.letter = letter; this.next = next;
 }
示例#47
0
 public abstract void SetResult(Metadata metadata, ILetter letter, IReceivedEventArgs receivedEventArgs);
示例#48
0
        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;
        }
示例#50
0
        private void ChannelOnFailedToSend(IChannel channel, ILetter letter)
        {
            _sentBatch = false;

            if(letter.Type == LetterType.Batch)
                FailedQueuedLetters();
            else
                FailedToSend(this, letter);
        }
示例#51
0
        // NFA construction

        public static ProtoState Accept(ILetter letter) => exit => new State(letter, exit);
示例#52
0
文件: BatchChannel.cs 项目: riax/Clr
 private void ChannelOnReceived(ILetter letter, ReceivedEventArgs receivedEventArgs)
 {
     Received(letter, receivedEventArgs);
 }
 public void method(ILetter l)
 {
     l.execute();
 }
示例#54
0
        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);
                }
            }
        }
示例#55
0
 public abstract void Invoke(TypedHyperSocket socket, ILetter letter, Metadata metadata, Type concreteType, IReceivedEventArgs receivedEventArgs);
示例#56
0
 private void QueueAck(ILetter letter)
 {
     _receivedQueue.Enqueue(letter);
     _transmitter.Enqueue(AckLetter);
 }
示例#57
0
        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;
            }
        }
示例#58
0
        private void HandleAckSent()
        {
            ILetter receivedLetter = _receivedQueue.Dequeue();

            HandleReceivedLetter(receivedLetter, AckState.AfterAck);
        }
示例#59
0
 private void SendMulticastLetter(ILetter letter)
 {
     foreach(IChannel channel in _hyperSocket.Channels) {
         channel.Enqueue(letter);
     }
 }
示例#60
0
文件: HyperSocket.cs 项目: riax/Clr
 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);
     }
 }