Exemplo n.º 1
0
        private T SendAndGetResponse <T>(WebSocketMessage query, object value)
        {
            if (_clientSocket.ReadyState == WebSocketState.Open)
            {
                using (var valueStream = Serialize(value))
                {
                    _clientSocket.Send(query.ToByteArray(valueStream));
                }
                var response = WaitForResponse(query);

                return(Deserialize <T>(response));
            }
            return(default(T));
        }
Exemplo n.º 2
0
        public void Set(ProxyBase dto, object value, string propertyName)
        {
            WebSocketMessage queryMessage = WebSocketMessageCreate(
                WebSocketMessage.WebSocketMessageType.Set,
                dto,
                propertyName,
                1);

            if (_clientSocket.ReadyState == WebSocketState.Open)
            {
                using (var valueStream = Serialize(value))
                    _clientSocket.Send(queryMessage.ToByteArray(valueStream));
            }
        }
        public async void sendResponse(WebSocketResponseMessage response)
        {
            if (socket == null)
            {
                throw new Exception("Connection closed!");
            }

            WebSocketMessage message = WebSocketMessage.CreateBuilder()
                                       .SetType(WebSocketMessage.Types.Type.RESPONSE)
                                       .SetResponse(response)
                                       .Build();

            messageWriter.WriteBytes(message.ToByteArray());
            await messageWriter.StoreAsync();
        }
Exemplo n.º 4
0
        public void Invoke(ProxyBase dto, string methodName, params object[] parameters)
        {
            WebSocketMessage queryMessage = WebSocketMessageCreate(
                WebSocketMessage.WebSocketMessageType.Invoke,
                dto,
                methodName,
                parameters.Length);

            if (_clientSocket.ReadyState == WebSocketState.Open)
            {
                using (var valueStream = Serialize(new WebSocketMessageArrayValue {
                    Value = parameters
                }))
                {
                    _clientSocket.Send(queryMessage.ToByteArray(valueStream));
                }
            }
        }
Exemplo n.º 5
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;
         *  }
         * }*/

        internal 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);
        }
        /*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 void sendMessage(WebSocketMessage message)
        {
            if (socket == null)
            {
                throw new Exception("Connection closed!");
            }

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