public async static Task <T> ReadAsProtoJson <T>(this HttpContent httpContent) where T : new()
        {
            //using (System.IO.Stream stream = await content.ReadAsStreamAsync())
            //{
            //    var reader = new Newtonsoft.Json.JsonTextReader(new System.IO.StreamReader(stream));
            //    var arrayBody = JArray.Load(reader);
            //    arrayBody.RemoveAt(0);
            //    return ProtoJsonSerializer.Deserialize<T>(arrayBody);
            //}

            System.IO.Stream content = await httpContent.ReadAsStreamAsync();

            var arrayBody = new DynamicJson(content);

            arrayBody.RemoveAt(0);
            return(ProtoJsonSerializer.Deserialize <T>(arrayBody));
        }
Beispiel #2
0
        async void _channel_OnDataReceived(object sender, DynamicJson rawdata)
        {
            //Parse channel array and call the appropriate events.
            if (rawdata[0].ToString() == "noop")
            {
                // set active client if more than 120 sec of inactivity
                if ((DateTime.UtcNow - _last_response_date).TotalSeconds > 120 && _client_id != null)
                {
                    SetActiveClientAsync();
                    _last_response_date = DateTime.UtcNow;
                }
            }
            else if (rawdata[0].ToString() == "resync") // internal handling
            {
                SyncAllNewEventsAsync(long.Parse(rawdata[1].ToString()));
            }
            else if (rawdata[0]["p"] != null)
            {
                DynamicJson wrapper = new DynamicJson(rawdata[0]["p"].ToString());
                if (wrapper["4"] != null && wrapper["4"]["2"] != null)
                {
                    if (_channel.Connected && !_wasConnected)
                    {
                        _client_id     = wrapper["4"]["2"].ToString();
                        _requestHeader = null;
                        _channel.SendAck(0);

                        _wasConnected = _channel.Connected;

                        // load self info
                        await GetSelfInfoAsync();

                        // load conversations if not loaded
                        await SyncRecentConversationsAsync();

                        if (OnConnectionEstablished != null)
                        {
                            OnConnectionEstablished(this, null);
                        }
                    }
                }

                if (wrapper["2"] != null && wrapper["2"]["2"] != null)
                {
                    DynamicJson cbu = new DynamicJson(wrapper["2"]["2"].ToString());
                    cbu.RemoveAt(0);
                    BatchUpdate batchUpdate = ProtoJsonSerializer.Deserialize <BatchUpdate>(cbu as DynamicJson);

                    foreach (StateUpdate state_update in batchUpdate.state_update)
                    {
                        if (state_update.event_notification != null)
                        {
                            switch (state_update.event_notification.current_event.event_type)
                            {
                            case EventType.EVENT_TYPE_REGULAR_CHAT_MESSAGE:
                            case EventType.EVENT_TYPE_UNKNOWN:
                                if (state_update.event_notification.current_event.conversation_id == null)
                                {
                                    break;
                                }

                                if (_active_conversations.ContainsKey(state_update.event_notification.current_event.conversation_id.id))
                                {
                                    _active_conversations[state_update.event_notification.current_event.conversation_id.id].events.Add(state_update.event_notification.current_event);
                                    if (OnNewMessageReceived != null)
                                    {
                                        OnNewMessageReceived(this, state_update.event_notification.current_event);
                                    }
                                }
                                else
                                {
                                    // get conversation
                                    await GetConversationAsync(state_update.event_notification.current_event.conversation_id.id);

                                    //ConversationState s = new ConversationState()
                                    //{
                                    //    conversation_id = state_update.event_notification.current_event.conversation_id,
                                    //    conversation = state_update.conversation,
                                    //    events = new List<Event>() { state_update.event_notification.current_event }
                                    //};

                                    //_active_conversations.Add(s.conversation_id.id, new Model.Conversation(s));
                                    //if (NewConversationCreated != null)
                                    //    NewConversationCreated(this, _active_conversations.Last().Value);
                                }
                                break;

                            case EventType.EVENT_TYPE_OTR_MODIFICATION:
                                _active_conversations[state_update.event_notification.current_event.conversation_id.id].conversation.otr_status = state_update.event_notification.current_event.otr_status;
                                break;

                            case EventType.EVENT_TYPE_CONVERSATION_RENAME:
                                _active_conversations[state_update.event_notification.current_event.conversation_id.id].conversation.name = state_update.event_notification.current_event.conversation_rename.new_name;
                                break;
                            }
                        }

                        if (state_update.presence_notification != null)
                        {
                            foreach (var presence in state_update.presence_notification.presence)
                            {
                                if (_contacts.ContainsKey(presence.user_id.gaia_id))
                                {
                                    _contacts[presence.user_id.gaia_id].presence = presence.presence;
                                }
                            }
                        }


                        if (state_update.self_presence_notification != null)
                        {
                            CurrentUser.presence = new Presence()
                            {
                                available = state_update.self_presence_notification.client_presence_state.state == ClientPresenceStateType.CLIENT_PRESENCE_STATE_DESKTOP_ACTIVE
                            }
                        }
                        ;


                        if (state_update.watermark_notification != null)
                        {
                            if (state_update.watermark_notification.sender_id.gaia_id == CurrentUser.id.gaia_id)
                            {
                                _active_conversations[state_update.watermark_notification.conversation_id.id].conversation.self_conversation_state.self_read_state.latest_read_timestamp = state_update.watermark_notification.latest_read_timestamp;
                            }
                            //else
                            //    _active_conversations[state_update.watermark_notification.conversation_id.id].read_state. = state_update.watermark_notification.latest_read_timestamp.FromUnixTime();
                        }
                    }

                    _timestamp = long.Parse(wrapper["1"]["4"].ToString());
                }
            }
        }