Beispiel #1
0
        public MsgState ProcessStates(out MessageEventArgs mea)
        {
            mea = processMea();

            switch (this._state)
            {
            case MsgState.Idle:
                if (this._msgs2Send.Count > 0)
                {
                    lock (this._msgs2Send)
                    {
                        this._msg2Go = this._msgs2Send.Dequeue();
                    }
                    if (!String.IsNullOrWhiteSpace(this._msg2Go))
                    {
                        this._msgProcessed = false;
                        this._attempt      = 0;
                        this._imSendingAck = false;
                        this._state        = MsgState.ReadyForRemote;
                    }
                }
                break;

            case MsgState.ReadyForRemote:
                this._startTime = DateTime.Now;

                if (this._attempt >= 3)
                {
                    this._state = MsgState.Idle;
                }
                else if (this._msgProcessed)
                {
                    this._state = MsgState.WaitingResponse;
                }
                break;

            case MsgState.WaitingResponse:
                // message was already sent out, check timeout
                DateTime endTime = DateTime.Now;
                if (endTime.Subtract(this._startTime).Milliseconds >= 799)
                {
                    Sock.debug(_name + ": timeout, repeating");
                    this._msgProcessed = false;
                    this._attempt++;
                    this._state = MsgState.ReadyForRemote;
                }
                break;

            case MsgState.ReadyForAck:
                this._msgProcessed = false;
                this._msg2Go       = this._lastAckMsg;
                this._state        = MsgState.SendAck;
                break;

            case MsgState.SendAck:
                if (this._msgProcessed)
                {
                    this._msgProcessed = false;
                    this._imSendingAck = false;
                    this._state        = MsgState.Done;
                }
                break;

            case MsgState.Done:
                this._state = MsgState.Idle;
                break;

            default: break;
            }
            return(this._state);
        }
Beispiel #2
0
        private MessageEventArgs processMea()
        {
            string localMsg;
            int    myIndex;

            lock (_buildMsg)
            {
                localMsg = _buildMsg.ToString();
                myIndex  = localMsg.IndexOf("<EOF>");
                if (myIndex >= 0)
                {
                    localMsg = localMsg.Substring(0, myIndex + 1 + 4).Trim();
                    _buildMsg.Remove(0, myIndex + 1 + 4);
                }
            }

            if (myIndex >= 0)
            {
                MessageEventArgs mea = new MessageEventArgs(localMsg);

                if (mea.Valid)
                {
                    // for safety
                    _buddyIp = mea.FriendIP;

                    if (this._state == MsgState.WaitingResponse || this._state == MsgState.SendAck)
                    {
                        Sock.debug(_name + ":got ACK:" + this._state.ToString() + ":Id=" + mea.Id);
                        mea.IsAck = true;
                        if (this._state == MsgState.WaitingResponse)
                        {
                            this._state = MsgState.Done;
                        }
                        return(mea);
                    }

                    if (this._imSendingAck || mea.FriendIP == myip)
                    {
                        if (this._state == MsgState.WaitingResponse)
                        {
                            this._state = MsgState.Done;
                        }
                        return(null);
                    }
                    else if (this._state == MsgState.Idle || this._state == MsgState.ReadyForRemote)
                    {
                        this._imSendingAck = true;
                        _lastAckMsg        = generate(mea.Id, mea.FriendName, mea.FriendIP, Sock.Checksum(mea.TextFromFriend)); // no need to send entire msg payload
                        _lastAckIP         = mea.FriendIP;

                        Sock.debug(_name + ":Display(sendAck)" + this._state.ToString() + ":Id=" + mea.Id);
                        this._state = MsgState.ReadyForAck;
                    }
                    else
                    {
                        return(mea);
                    }
                }
                else
                {
                    Sock.debug(_name + ": oops, invalid message received");
                }

                return(mea);
            }
            else
            {
                return(null);
            }
        }