Esempio n. 1
0
 private void sendKeepAlive(object state)
 {
     if (socket != null)
     {
         Debug.WriteLine("keepAlive");
         byte[] message = WebSocketMessage.CreateBuilder()
                          .SetType(WebSocketMessage.Types.Type.REQUEST)
                          .SetRequest(WebSocketRequestMessage.CreateBuilder()
                                      .SetId(KeyHelper.getTime())
                                      .SetPath("/v1/keepalive")
                                      .SetVerb("GET")
                                      .Build()).Build()
                          .ToByteArray();
         messageWriter.WriteBytes(message);
         messageWriter.StoreAsync();
     }
 }
Esempio n. 2
0
        /*public  WebSocketRequestMessage readRequest(ulong timeoutMillis)
         * {
         *  if (client == null)
         *  {
         *      throw new Exception("Connection closed!");
         *  }
         *
         *  ulong startTime = KeyHelper.getTime();
         *
         *  while (client != null && incomingRequests.Count == 0 && elapsedTime(startTime) < timeoutMillis)
         *  {
         *      //Util.wait(this, Math.Max(1, timeoutMillis - elapsedTime(startTime)));
         *  }
         *
         *  if (incomingRequests.Count == 0 && client == null) throw new Exception("Connection closed!");
         *  else if (incomingRequests.Count == 0) throw new TimeoutException("Timeout exceeded");
         *  else
         *  {
         *      WebSocketRequestMessage message = incomingRequests.First();
         *      incomingRequests.RemoveFirst();
         *      return message;
         *  }
         * }*/

        public async Task <Tuple <int, string> > sendRequest(WebSocketRequestMessage request)
        {
            if (socket == null || !connected)
            {
                throw new IOException("No connection!");
            }

            WebSocketMessage message = WebSocketMessage.CreateBuilder()
                                       .SetType(WebSocketMessage.Types.Type.REQUEST)
                                       .SetRequest(request)
                                       .Build();

            Tuple <int, string> empty = new Tuple <int, string>(0, string.Empty);

            outgoingRequests.Add((long)request.Id, empty);

            messageWriter.WriteBytes(message.ToByteArray());
            await messageWriter.StoreAsync();

            return(empty);
        }
Esempio n. 3
0
        public async Task <Tuple <uint, string> > SendRequest(WebSocketRequestMessage request)
        {
            Tuple <CountdownEvent, uint, string> t = new Tuple <CountdownEvent, uint, string>(new CountdownEvent(1), 0, null);
            WebSocketMessage message = new WebSocketMessage
            {
                Type    = WebSocketMessage.Types.Type.Request,
                Request = request
            };

            OutgoingRequests.AddOrUpdate(request.Id, t, (k, v) => t);
            WebSocket.OutgoingQueue.Add(message.ToByteArray());
            return(await Task.Run(() =>
            {
                if (t.Item1.Wait(10 * 1000, Token))
                {
                    var handledTuple = OutgoingRequests[request.Id];
                    return new Tuple <uint, string>(handledTuple.Item2, handledTuple.Item3);
                }
                throw new IOException("wait for confirmation timeout");
            }));
        }