Пример #1
0
        public Arewegood(string apiKey, string serviceName = "_awgproxy._tcp.local.")
        {
            _serviceName       = serviceName;
            _wsIsAuthenticated = false;
            _buffer            = new Queue <string>();

            // resolve the service and test/find the first valid websocket
            // authenticate it, to


            _apiKey = apiKey;

            Task.Delay(WS_TIMEOUT_MS).ContinueWith((continuation) =>
            {
                _wsGaveUp   = true;
                JObject obj = new JObject();
                obj["logs"] = new JArray();
                foreach (var buf in _buffer)
                {
                    ((JArray)obj["logs"]).Add(buf);
                }
                MakeRawHttp(obj.ToString());
                _buffer.Clear();
            });

            try
            {
                ZeroconfResolver.ResolveAsync(serviceName).ContinueWith(async(continuation) =>
                {
                    var list = await continuation;
                    foreach (var host in list)
                    {
                        // try to find a valid ws @ a given service
                        var possible     = "ws://" + host.IPAddress.ToString() + ":" + host.Services[serviceName].Port;
                        WebSocket socket = new WebSocket(possible);
                        int flag         = 0;
                        var proceed      = new Task <int>(() => { while (flag == 0)
                                                                  {
                                                                  }
                                                                  return(flag); });
                        proceed.Start();
                        socket.Open();
                        socket.Error  += (s, e) => { flag = 1; };
                        socket.Opened += (s, a) => { flag = 2; };
                        proceed.Wait();
                        var result = proceed.Result;
                        if (result == 2)
                        {
                            _ws = socket;
                            break;
                        }
                        else
                        {
                            socket.Close();
                            socket = null;
                        }
                    }
                }).ContinueWith((continuation) =>
                {
                    // if we did, authenticate it using our auth protocol
                    if (_ws != null && _ws.State == WebSocketState.Open)
                    {
                        int flag    = 0;
                        var proceed = new Task(() => { while (flag == 0)
                                                       {
                                                       }
                                               });
                        proceed.Start();
                        string data          = null;
                        _ws.MessageReceived += (s, d) =>
                        {
                            data = d.Message; flag = d.Message.Length;
                        };
                        _ws.Send(new ApiAuthenticationObject(_apiKey.ToString()).ToString());
                        proceed.Wait();
                        if (data != null && data.Length > 0)
                        {
                            var response = JObject.Parse(data);
                            if (response["type"].ToString() == "api_token-response" &&
                                response["data"].ToString() == "OK")
                            {
                                // ok, we're authenticated!
                                _wsIsAuthenticated = true;
                            }
                        }
                    }
                }).ContinueWith((continuation) =>
                {
                    SocketUp.Invoke();
                    // write any buffered messages
                    if (_ws != null && _ws.State == WebSocketState.Open && _wsIsAuthenticated && _buffer.Count > 0)
                    {
                        foreach (var b in _buffer)
                        {
                            _ws.Send(b);
                        }
                        _buffer.Clear();
                    }
                });
            } catch (TypeInitializationException)
            {
                _wsGaveUp = true;
            }
        }
Пример #2
0
 public string Serialize()
 {
     return(Type.ToString() + MainDirection.ToString("d") + SocketUp.ToString("d") +
            SocketRight.ToString("d") + SocketDown.ToString("d") + SocketLeft.ToString("d") +
            Capacity.ToString() + (IsMain ? "1" : "0"));
 }