public void OnTimeout(TimeoutEventArgs e)
        {
            TimeoutEventHandler handler = TimeoutEvent;

            if (TimeoutEvent != null)
            {
                handler(this, e);
            }
        }
Esempio n. 2
0
 protected void InvokeTimeoutEventHandler()
 {
     Stop();
     if (TimeoutEventHandler != null)
     {
         TimeoutEventHandler.Invoke(this, EventArgs.Empty);
     }
     else
     {
         showIdleMessage();
     }
     Start();
 }
Esempio n. 3
0
        private void AddExpectedReply(FunapiMessage fun_msg, string reply_type,
                                       float reply_time, TimeoutEventHandler onReplyMissed)
        {
            lock (expected_reply_lock)
            {
                if (!expected_replies_.ContainsKey(reply_type))
                {
                    expected_replies_[reply_type] = new List<FunapiMessage>();
                }

                fun_msg.SetReply(reply_type, reply_time, onReplyMissed);
                expected_replies_[reply_type].Add(fun_msg);
                DebugUtils.Log("Adds expected reply message - {0} > {1}", fun_msg.msg_type, reply_type);
            }
        }
Esempio n. 4
0
        public void SendMessage(string msg_type, object message,
                                 EncryptionType encryption = EncryptionType.kDefaultEncryption,
                                 TransportProtocol protocol = TransportProtocol.kDefault,
                                 string expected_reply_type = null, float expected_reply_time = 0f,
                                 TimeoutEventHandler onReplyMissed = null)
        {
            if (protocol == TransportProtocol.kDefault)
                protocol = GetMessageProtocol(msg_type);

            bool transport_reliability = (protocol == TransportProtocol.kTcp && session_reliability_);

            // Invalidates session id if it is too stale.
            if (last_received_.AddSeconds(kFunapiSessionTimeout) < DateTime.Now)
            {
                DebugUtils.Log("Session is too stale. The server might have invalidated my session. Resetting.");
                session_id_ = "";
            }

            FunapiTransport transport = GetTransport(protocol);
            if (transport != null && transport.state == FunapiTransport.State.kEstablished &&
                (transport_reliability == false || unsent_queue_.Count <= 0))
            {
                FunapiMessage fun_msg = null;

                if (transport.Encoding == FunEncoding.kJson)
                {
                    fun_msg = new FunapiMessage(protocol, msg_type, transport.JsonHelper.Clone(message), encryption);

                    // Encodes a messsage type
                    transport.JsonHelper.SetStringField(fun_msg.message, kMsgTypeBodyField, msg_type);

                    // Encodes a session id, if any.
                    if (session_id_.Length > 0)
                    {
                        transport.JsonHelper.SetStringField(fun_msg.message, kSessionIdBodyField, session_id_);
                    }

                    if (transport_reliability)
                    {
                        transport.JsonHelper.SetIntegerField(fun_msg.message, kSeqNumberField, seq_);
                        ++seq_;

                        send_queue_.Enqueue(fun_msg);
                        DebugUtils.DebugLog("{0} send message - msgtype:{1} seq:{2}", protocol, msg_type, seq_ - 1);
                    }
                    else
                    {
                        DebugUtils.DebugLog("{0} send message - msgtype:{1}", protocol, msg_type);
                    }
                }
                else if (transport.Encoding == FunEncoding.kProtobuf)
                {
                    fun_msg = new FunapiMessage(protocol, msg_type, message, encryption);

                    FunMessage pbuf = fun_msg.message as FunMessage;
                    pbuf.msgtype = msg_type;

                    // Encodes a session id, if any.
                    if (session_id_.Length > 0)
                    {
                        pbuf.sid = session_id_;
                    }

                    if (transport_reliability)
                    {
                        pbuf.seq = seq_;
                        ++seq_;

                        send_queue_.Enqueue(fun_msg);
                        DebugUtils.DebugLog("{0} send message - msgtype:{1} seq:{2}", protocol, msg_type, pbuf.seq);
                    }
                    else
                    {
                        DebugUtils.DebugLog("{0} send message - msgtype:{1}", protocol, msg_type);
                    }
                }

                if (expected_reply_type != null && expected_reply_type.Length > 0)
                {
                    AddExpectedReply(fun_msg, expected_reply_type, expected_reply_time, onReplyMissed);
                }

                transport.SendMessage(fun_msg);
            }
            else if (transport != null &&
                     (transport_reliability || transport.state == FunapiTransport.State.kEstablished))
            {
                if (transport.Encoding == FunEncoding.kJson)
                {
                    if (transport == null)
                        unsent_queue_.Enqueue(new FunapiMessage(protocol, msg_type, message, encryption));
                    else
                        unsent_queue_.Enqueue(new FunapiMessage(protocol, msg_type, transport.JsonHelper.Clone(message), encryption));
                }
                else if (transport.Encoding == FunEncoding.kProtobuf)
                {
                    unsent_queue_.Enqueue(new FunapiMessage(protocol, msg_type, message, encryption));
                }

                DebugUtils.Log("SendMessage - '{0}' message queued.", msg_type);
            }
            else
            {
                StringBuilder strlog = new StringBuilder();
                strlog.AppendFormat("SendMessage - '{0}' message skipped.", msg_type);
                if (transport == null)
                    strlog.AppendFormat(" There's no '{0}' transport.", protocol);
                else if (transport.state != FunapiTransport.State.kEstablished)
                    strlog.AppendFormat(" Transport's state is '{0}'.", transport.state);

                DebugUtils.Log(strlog.ToString());
            }
        }
Esempio n. 5
0
 public void SendMessage(string msg_type, object message,
                          string expected_reply_type, float expected_reply_time, TimeoutEventHandler onReplyMissed)
 {
     SendMessage(msg_type, message, EncryptionType.kDefaultEncryption, GetMessageProtocol(msg_type),
                 expected_reply_type, expected_reply_time, onReplyMissed);
 }
Esempio n. 6
0
 public void SendMessage(MessageType msg_type, object message,
                          string expected_reply_type, float expected_reply_time, TimeoutEventHandler onReplyMissed)
 {
     string _msg_type = MessageTable.Lookup(msg_type);
     SendMessage(_msg_type, message, EncryptionType.kDefaultEncryption, GetMessageProtocol(_msg_type),
                 expected_reply_type, expected_reply_time, onReplyMissed);
 }
Esempio n. 7
0
 public void SendMessage(MessageType msg_type, object message,
                          EncryptionType encryption = EncryptionType.kDefaultEncryption,
                          TransportProtocol protocol = TransportProtocol.kDefault,
                          string expected_reply_type = null, float expected_reply_time = 0f,
                          TimeoutEventHandler onReplyMissed = null)
 {
     string _msg_type = MessageTable.Lookup(msg_type);
     SendMessage(_msg_type, message, encryption, protocol, expected_reply_type, expected_reply_time, onReplyMissed);
 }
Esempio n. 8
0
 // Sets expected reply
 public void SetReply(string reply_type, float reply_timeout, TimeoutEventHandler callback)
 {
     this.reply_type = reply_type;
     this.reply_timeout = reply_timeout;
     this.timeout_callback = callback;
 }