Beispiel #1
0
        private ProtoPadServer(View window, int? overrideListeningPort = null, string overrideBroadcastedAppName = null)
        {
            _window = window;
            _contextActivity = window.Context as Activity;

            _httpServer = new SimpleHttpServer(responseBytes =>
            {
                var response = "{}";
                var remoteCommandDoneEvent = new AutoResetEvent(false);
                _contextActivity.RunOnUiThread(() => Response(responseBytes, remoteCommandDoneEvent, ref response));
                remoteCommandDoneEvent.WaitOne();
                return response;
            });

            IPAddress broadCastAddress;
            using (var wifi = _contextActivity.GetSystemService(Android.Content.Context.WifiService) as WifiManager)
            {
                _mcLock = wifi.CreateMulticastLock("ProtoPadLock");
                _mcLock.Acquire();
                broadCastAddress = GetBroadcastAddress(wifi);
            }

            BroadcastedAppName = overrideBroadcastedAppName ?? String.Format("ProtoPad Service on ANDROID Device {0}", Android.OS.Build.Model);
            ListeningPort = overrideListeningPort ?? 8080;
            LocalIPAddress = Helpers.GetCurrentIPAddress();

            _udpServer = new UdpDiscoveryServer(BroadcastedAppName, String.Format("http://{0}:{1}/", LocalIPAddress, ListeningPort), broadCastAddress);
        }
Beispiel #2
0
        private ProtoPadServer(Activity activity, int? overrideListeningPort = null, string overrideBroadcastedAppName = null)
        {
            _contextActivity = activity;

            BroadcastedAppName = overrideBroadcastedAppName ?? String.Format("ProtoPad Service on ANDROID Device {0}", Android.OS.Build.Model);
            ListeningPort = overrideListeningPort ?? 8080;
            LocalIPAddress = Helpers.GetCurrentIPAddress();

            var mainMonodroidAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name.ToLower() == "mono.android");

            var requestHandlers = new Dictionary<string, Func<byte[], string>>
                {
                    {"GetMainXamarinAssembly", data => mainMonodroidAssembly.FullName},
                    {"WhoAreYou", data => "Android"},
                    {"ExecuteAssembly", data =>
                        {
                            var response = "{}";
                            var remoteCommandDoneEvent = new AutoResetEvent(false);
                            _contextActivity.RunOnUiThread(() => Response(data, remoteCommandDoneEvent, ref response));
                            remoteCommandDoneEvent.WaitOne();
                            return response;
                        }
                    }
                };

            _httpServer = new SimpleHttpServer(ListeningPort, requestHandlers);

            IPAddress broadCastAddress;
            using (var wifi = _contextActivity.GetSystemService(Android.Content.Context.WifiService) as WifiManager)
            {
                try
                {
                    _mcLock = wifi.CreateMulticastLock("ProtoPadLock");
                    _mcLock.Acquire();

                }
                catch (Java.Lang.SecurityException e)
                {
                    Debug.WriteLine("Could not optain Multicast lock: {0}. Did you enable CHANGE_WIFI_MULTICAST_STATE permission in your app manifest?", e.Message);
                }

                broadCastAddress = GetBroadcastAddress(wifi);
            }

            var inEmulator = Build.Brand.Equals("generic", StringComparison.InvariantCultureIgnoreCase);
            _udpServer = new UdpDiscoveryServer(BroadcastedAppName, String.Format("http://{0}:{1}/", inEmulator ? "localhost" : LocalIPAddress.ToString(), inEmulator ? "?" : ListeningPort.ToString()), broadCastAddress);
        }