private async void DeveloperCommandToMcuAsync() { byte[] byteCommand; try { if (IsHex) { byteCommand = Conversion.StringToByteArray(DeveloperCommand); } else { byteCommand = Encoding.ASCII.GetBytes(DeveloperCommand + "\n"); } ReplyPacket reply = await Mcu.SendTcpAsync(byteCommand, true, fireOnSent : (isSent) => { ReplyPacket replyPacket = new ReplyPacket() { IsSent = isSent }; replyPacket.SetReply(byteCommand); DevSentCommandInfo?.Invoke(this, replyPacket); }).ConfigureAwait(false); if (reply.IsSentAndReplyReceived) { DevReplyIncoming?.Invoke(this, reply); } } catch (Exception ex) { ListenerTcp.ExceptionHandler?.Invoke(this, ex); } }
/// <summary> /// Authenticates the currently logged in user with the channel's chat. /// </summary> /// <returns>Whether the operation succeeded</returns> public async Task <bool> Authenticate() { try { MethodPacket packet = new MethodPacket() { method = "auth", arguments = new JArray() { this.Channel.id.ToString(), this.User.id.ToString(), this.channelChat.authkey }, }; this.Authenticated = false; ReplyPacket reply = await this.SendAndListen(packet, checkIfAuthenticated : false); if (reply != null && reply.dataObject.TryGetValue("authenticated", out JToken value)) { this.Authenticated = (bool)value; } return(this.Authenticated); } catch { return(false); } }
protected void AddReplyPacketForListeners(ReplyPacket packet) { if (this.replyIDListeners.ContainsKey(packet.id)) { this.replyIDListeners[packet.id] = packet; } }
/// <summary> /// Gets the memory throttling for all interactive APIs /// </summary> /// <returns>The memory throttling for all interactive APIs</returns> public async Task <InteractiveGetThrottleStateModel> GetThrottleState() { ReplyPacket reply = await this.SendAndListen(new MethodPacket("getThrottleState")); if (this.VerifyNoErrors(reply)) { return(new InteractiveGetThrottleStateModel(reply.resultObject)); } return(new InteractiveGetThrottleStateModel()); }
/// <summary> /// Gets the current server time. /// </summary> /// <returns>The current time on the server</returns> public async Task <DateTimeOffset?> GetTime() { ReplyPacket reply = await this.SendAndListen(new MethodPacket("getTime")); if (reply != null && reply.resultObject["time"] != null) { return(DateTimeOffsetExtensions.FromUTCUnixTimeMilliseconds((long)reply.resultObject["time"])); } return(null); }
/// <summary> /// Gets the current server time. /// </summary> /// <returns>The current time on the server</returns> public async Task <DateTimeOffset?> GetTime() { ReplyPacket reply = await this.SendAndListen(new MethodPacket("getTime")); if (reply != null && reply.resultObject["time"] != null) { return(DateTimeHelper.UnixTimestampToDateTimeOffset((long)reply.resultObject["time"])); } return(null); }
protected bool VerifyNoErrors(ReplyPacket replyPacket) { if (replyPacket == null) { return(false); } if (replyPacket.errorObject != null) { throw new ReplyPacketException(JsonConvert.DeserializeObject <ReplyErrorModel>(replyPacket.error.ToString())); } return(true); }
private void AssignLatestSequence(WebSocketPacket packet) { if (packet is MethodPacket) { MethodPacket mPacket = (MethodPacket)packet; mPacket.seq = this.lastSequenceNumber; } else if (packet is ReplyPacket) { ReplyPacket rPacket = (ReplyPacket)packet; rPacket.seq = this.lastSequenceNumber; } }
/// <summary> /// Processes a JSON packet received from the server. /// </summary> /// <param name="packet">The packet JSON</param> /// <returns>An awaitable Task</returns> protected override Task ProcessReceivedPacket(string packet) { List <JToken> packetJTokens = new List <JToken>(); JToken packetJToken = JToken.Parse(packet); if (packetJToken is JArray) { foreach (JToken t in (JArray)packetJToken) { packetJTokens.Add(t); } } else { packetJTokens.Add(packetJToken); } foreach (JToken token in packetJTokens) { WebSocketPacket webSocketPacket = token.ToObject <WebSocketPacket>(); string data = JSONSerializerHelper.SerializeToString(token); this.OnPacketReceivedOccurred?.Invoke(this, webSocketPacket); if (webSocketPacket.type.Equals("method")) { MethodPacket methodPacket = JsonConvert.DeserializeObject <MethodPacket>(data); this.SendSpecificPacket(methodPacket, this.OnMethodOccurred); } else if (webSocketPacket.type.Equals("reply")) { ReplyPacket replyPacket = JsonConvert.DeserializeObject <ReplyPacket>(data); if (this.replyIDListeners.ContainsKey(replyPacket.id)) { this.replyIDListeners[replyPacket.id] = replyPacket; } this.SendSpecificPacket(replyPacket, this.OnReplyOccurred); } else if (webSocketPacket.type.Equals("event")) { EventPacket eventPacket = JsonConvert.DeserializeObject <EventPacket>(data); this.SendSpecificPacket(eventPacket, this.OnEventOccurred); } } return(Task.FromResult(0)); }
protected T GetSpecificReplyResultValue <T>(ReplyPacket replyPacket) { this.VerifyNoErrors(replyPacket); if (replyPacket != null) { if (replyPacket.resultObject != null) { return(JsonConvert.DeserializeObject <T>(replyPacket.resultObject.ToString())); } else if (replyPacket.dataObject != null) { return(JsonConvert.DeserializeObject <T>(replyPacket.dataObject.ToString())); } } return(default(T)); }
protected override Task ProcessReceivedPacket(string packetJSON) { dynamic jsonObject = JsonConvert.DeserializeObject(packetJSON); List <WebSocketPacket> packets = new List <WebSocketPacket>(); if (jsonObject.Type == JTokenType.Array) { JArray array = JArray.Parse(packetJSON); foreach (JToken token in array.Children()) { packets.Add(token.ToObject <WebSocketPacket>()); } } else { packets.Add(JsonConvert.DeserializeObject <WebSocketPacket>(packetJSON)); } foreach (WebSocketPacket packet in packets) { if (this.OnPacketReceivedOccurred != null) { this.OnPacketReceivedOccurred(this, packet); } if (packet.type.Equals("method")) { MethodPacket methodPacket = JsonConvert.DeserializeObject <MethodPacket>(packetJSON); this.SendSpecificPacket(methodPacket, this.OnMethodOccurred); } else if (packet.type.Equals("reply")) { ReplyPacket replyPacket = JsonConvert.DeserializeObject <ReplyPacket>(packetJSON); this.AddReplyPacketForListeners(replyPacket); this.SendSpecificPacket(replyPacket, this.OnReplyOccurred); } else if (packet.type.Equals("event")) { EventPacket eventPacket = JsonConvert.DeserializeObject <EventPacket>(packetJSON); this.SendSpecificPacket(eventPacket, this.OnEventOccurred); } } return(Task.FromResult(0)); }
protected void WebSocketClient_OnPacketSentOccurred(object sender, WebSocketPacket e) { if (e is MethodPacket) { MethodPacket mPacket = (MethodPacket)e; Logger.Log(string.Format(Environment.NewLine + "WebSocket Method Sent: {0} - {1} - {2} - {3} - {4}" + Environment.NewLine, e.id, e.type, mPacket.method, mPacket.arguments, mPacket.parameters)); } else if (e is ReplyPacket) { ReplyPacket rPacket = (ReplyPacket)e; Logger.Log(string.Format(Environment.NewLine + "WebSocket Reply Sent: {0} - {1} - {2} - {3} - {4}" + Environment.NewLine, e.id, e.type, rPacket.result, rPacket.error, rPacket.data)); } else { Logger.Log(string.Format(Environment.NewLine + "WebSocket Packet Sent: {0} - {1}" + Environment.NewLine, e.id, e.type)); } }
protected async Task <ReplyPacket> SendAndListen(WebSocketPacket packet, bool checkIfAuthenticated = true) { ReplyPacket replyPacket = null; this.AssignPacketID(packet); this.replyIDListeners[packet.id] = null; await this.Send(packet, checkIfAuthenticated); await this.WaitForResponse(() => { if (this.replyIDListeners.ContainsKey(packet.id) && this.replyIDListeners[packet.id] != null) { replyPacket = this.replyIDListeners[packet.id]; return(true); } return(false); }); this.replyIDListeners.Remove(packet.id); return(replyPacket); }
private void InteractiveClient_OnReplyOccurred(object sender, ReplyPacket e) { this.replyPackets.Add(e); }
protected void WebSocketClient_OnReplyOccurred(object sender, ReplyPacket e) { Logger.Log(string.Format(Environment.NewLine + "WebSocket Reply: {0} - {1} - {2} - {3} - {4}" + Environment.NewLine, e.id, e.type, e.result, e.error, e.data)); }
protected async Task <T> SendAndListen <T>(WebSocketPacket packet, bool checkIfAuthenticated = true) { ReplyPacket reply = await this.SendAndListen(packet); return(this.GetSpecificReplyResultValue <T>(reply)); }
private void ConstellationClient_OnReplyOccurred(object sender, ReplyPacket e) { this.replyPackets.Add(e); }
private void ChatClient_OnReplyOccurred(object sender, ReplyPacket e) { this.replyPackets.Add(e); }
private void InteractiveClient_OnReplyOccurred(object sender, ReplyPacket e) { this.lastSequenceNumber = Math.Max(e.seq, this.lastSequenceNumber); }
protected bool VerifyDataExists(ReplyPacket replyPacket) { return(replyPacket != null && replyPacket.data != null && !string.IsNullOrEmpty(replyPacket.data.ToString())); }