core
????????源代码位置:frameworks/base/core/java/android/net。
????????CaptivePortalTracker是一个状态机,使用HTTP请求判断是否重定向,确定网络提供商是否有用户登陆页面。
????????CaptivePortalTracker状态:
状态 | enter | processMessage | Default | | | ? NoActiveNetwork | | | ? ActiveNetwork | | | ??? DelayedCaptiveCheck | | |
????????NetworkStateTracker跟踪和控制一种类型网络的状态,通知状态变化。
????????BaseNetworkStateTracker继承并简单实现NetworkStateTracker。提供一些dispatch接口发送网络状态变化。
????????BaseNetworkStateTracker派生类
类 | 说明 | DummyDataStateTracker | 只记录控制状态,没有实际的网络连接。 | EthernetDataTracker | 监听NetworkManagementService网络变化,使用DHCP配置网络。 | MobileDataStateTracker | 监听TelephonyIntents广播,通过TelephonyManager监听移动网络信号强度,通过PhoneService控制移动网络开关。 | WifiStateTracker | 通过WifiManager监听Wifi状态,控制Wifi开关。 | BluetoothTetheringDataTracker | 通过AsyncChannel连接监听NetworkStateTracker事件,使用DHCP配置网络。 |
????????SamplingDataTracker
nsd
????????源代码位置:frameworks/base/core/java/android/net/nsd。
????????NsdServiceInfo描述MDNS域名信息。
????????NsdManager管理register、discover、resolve的Listener,将其映射为ID发送给DnsService(INsdManager),在ServiceHandler.handleMessage处理DnsService返回的消息,调用Listener相应的回调。消息处理运行在“NsdManager”内部线程。
????????App加载NsdManager:
framework/base/core/java/android/app/ContextImpl.java:
-------------------------------------------------------------------------
static {
??? // ……
??? registerService(NSD_SERVICE, new ServiceFetcher() {
??????????? @Override
??????????? public Object createService(ContextImpl ctx) {
??????????????? IBinder b = ServiceManager.getService(NSD_SERVICE);
??????????????? INsdManager service = INsdManager.Stub.asInterface(b);
??????????????? return new NsdManager(ctx.getOuterContext(), service);
??????????? }});
??? // ……
}
|
services
????????源代码位置:frameworks/base/services/java/com/android/server。
????????NativeDaemonConnector与netd的建立套接字连接,然后循环读取返回的报文,交给外部回调接口INativeDaemonConnectorCallbacks(Unsolicited事件,在系统前台线程FgThread调用回调)或者内部ResponseQueue(命令的应答)处理。
????????应答Code分类与HTTP相似,100-199有后续应答,200-299成功,等等。特别的600-699表示服务端主动发出的事件。
network_management
????????服务名:network_management。
????????NetworkManagementService实现INetworkManagementService接口。创建NativeDaemonConnector与netd的“netd”套接字连接,在其上注册NetdCallbackReceiver回调接口。创建线程执行NativeDaemonConnector::run方法。
????????NetdCallbackReceiver通过INetworkManagementEventObserver接口发送事件给外部。使用RemoteCallbackList协助管理回调接口对象。
????????ResponseQueue协助等待应答,使用ArrayBlockingQueue实现等待。允许应答在等待前到达,允许多个应答报文。
????????服务由SystemServer启动,运行在system_server进程中。
framework/base/services/java/com/android/server/SystemServer.java:
-------------------------------------------------------------------------
public void initAndLoop() {
??? // ……
??? if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
??????? // ……
??????? if (!disableNetwork) {
???? ???????try {
??????????????? Slog.i(TAG, "NetworkManagement Service");
??????????????? networkManagement =
??????????????? ????NetworkManagementService.create(context);
??????????????? ServiceManager.addService(
??????????????? ????Context.NETWORKMANAGEMENT_SERVICE, networkManagement);
??????????? } catch (Throwable e) {
??????????????? reportWtf("starting NetworkManagement Service", e);
??????????? }
??????? }
??????? // ……
??? }
??? // ……
}
|
servicediscovery
????????服务名:servicediscovery。
????????NsdService实现INsdManager接口。创建NativeDaemonConnector与netd的“mdns”套接字连接,注册NativeCallbackReceiver回调接口。创建线程执行NativeDaemonConnector::run方法。
????????NativeCallbackReceiver将接收到的事件发送给状态机NsdStateMachine处理。事件包括应答,因为应答码都在600-699范围。
????????NsdService通过ClientInfo管理客户端连接,客户端请求的ID会映射到全局ID作为到netd的MDnsSdListener的请求ID。删除请求ID取消注册。
????????NsdStateMachine状态机的状态只有Default、Enabled、Disabled三个状态。Default是父状态,处理子状态未处理的事件。真正状态只有两个。
????????客户端连接有AsyncChannel、Messager实现,AsyncChannel与NsdStateMachine的Handler绑定,客户端发送的消息由NsdStateMachine处理。
????????服务由SystemServer启动,运行在system_server进程中。
framework/base/services/java/com/android/server/SystemServer.java:
-------------------------------------------------------------------------
public void initAndLoop() {
??? // ……
??? if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
??????? // ……
??????? if (!disableNetwork) {
??????????? // ……
??????????? try {
?????????????? Slog.i(TAG, "Network Service Discovery Service");
?????????????? serviceDiscovery = NsdService.create(context);
?????????????? ServiceManager.addService(
?????????????????? Context.NSD_SERVICE, serviceDiscovery);
??????????? } catch (Throwable e) {
??????????????? reportWtf("starting Service Discovery Service", e);
??????????? }
??????? }
??????? // ……
??? }
??? // ……
}
|
netstats
????????服务名:netstats。
????????源代码位置:frameworks/base/services/java/com/android/server/net。
????????NetworkStatsCollection保存NetworkStatsHistory集合。
????????NetworkStatsRecorder收集NetworkStats到NetworkStatsHistory,使用FileRotator保存到文件。
????????NetworkStatsService收集详细网络状态、统计。
????????服务由SystemServer启动,运行在system_server进程中。
framework/base/services/java/com/android/server/SystemServer.java:
-------------------------------------------------------------------------
public void initAndLoop() {
??? // ……
?? ?if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
??????? // ……
??????? if (!disableNetwork) {
??????????? try {
??????????????? Slog.i(TAG, "NetworkStats Service");
??????????????? networkStats = new NetworkStatsService(
??????????????? ????context, networkManagement, alarm);
??????????????? ServiceManager.addService(
??????????????? ????Context.NETWORK_STATS_SERVICE, networkStats);
??????????? } catch (Throwable e) {
??????????????? reportWtf("starting NetworkStats Service", e);
??????????? }
??????????? // ……
??????? }
??????? // ……
??? }
??? // ……
}
|
netpolicy
????????服务名:netpolicy。
????????源代码位置:frameworks/base/services/java/com/android/server/net。
????????服务由SystemServer启动,运行在system_server进程中。
framework/base/services/java/com/android/server/SystemServer.java:
-------------------------------------------------------------------------
public void initAndLoop() {
??? // ……
??? if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
??????? // ……
??????? if (!disableNetwork) {
??????????? try {
??????????????? Slog.i(TAG, "NetworkPolicy Service");
??????????????? networkPolicy = new NetworkPolicyManagerService(
??????????????????????? context, ActivityManagerService.self(), power,
??????????????????????? networkStats, networkManagement);
??????????????? ServiceManager.addService(
??????????????????? Context.NETWORK_POLICY_SERVICE, networkPolicy);
??????????? } catch (Throwable e) {
??????????????? reportWtf("starting NetworkPolicy Service", e);
??????????? }
??????????? // ……
??????? }
??????? // ……
??? }
??? // ……
}
|
connectivity
????????服务名:connectivity。
????????服务由SystemServer启动,运行在system_server进程中。
????????ConnectivityService处理网络切换、路由、VPN、Tether(手机Modem)。
????????相关服务:
服务 | 说明 | NetworkManagementService | 1、监听网络事件 2、设置路由、域名服务器、MARK、防火墙、MTU 3、用于VPN(Nat464Xlat)、Tether | NetworkPolicyManagerService | | TelephonyManager | 获取SIM信息 | PowerManager | PARTIAL_WAKE_LOCK | AlarmManager | 定期EVENT_SAMPLE_INTERVAL_ELAPSED | PacService,ProxyService | PacManager | NotificationManager | |
????????监听事件源:
源 | 事件 | 说明 | PolicyManager | onUidRulesChanged onMeteredIfacesChanged onRestrictBackgroundChanged | mPolicyListener | Intent | ACTION_USER_STARTING ACTION_USER_STOPPING | mUserIntentReceiver 为用户启动停止VPN | NetworkManagement | interfaceClassDataActivityChanged | mDataActivityObserver ACTION_DATA_ACTIVITY_CHANGE | Settings.Global | HTTP_PROXY | mSettingsObserver 处理, EVENT_APPLY_GLOBAL_HTTP_PROXY | | CONNECTED_TO_PROVISIONING_NETWORK_ACTION | mProvisioningReceiver | NetworkStateTracker | EVENT_STATE_CHANGED EVENT_CONFIGURATION_CHANGED EVENT_NETWORK_SUBTYPE_CHANGED | mTrackerHandler |
源 | 事件 | 说明 | TelephonyManager | LISTEN_ SERVICE_STATE SIGNAL_STRENGTHS DATA_CONNECTION_STATE DATA_ACTIVITY | DataConnectionStats.mPhoneStateListener |
framework/base/services/java/com/android/server/SystemServer.java:
-------------------------------------------------------------------------
public void initAndLoop() {
??? // ……
??? if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
??????? // ……
??????? if (!disableNetwork) {
??????????? // ……
??????????? try {
??????????????? Slog.i(TAG, "Connectivity Service");
??????????????? connectivity = new ConnectivityService(
??????????????????????? context, networkManagement,
??????????????????????? networkStats, networkPolicy);
??????????????? ServiceManager.addService(
??????????????????????? Context.CONNECTIVITY_SERVICE, connectivity);
??????????????? networkStats.bindConnectivityManager(connectivity);
??????????????? networkPolicy.bindConnectivityManager(connectivity);
??????????????? wifiP2p.connectivityServiceReady();
??????????????? wifi.checkAndStartWifi();
??????????? } catch (Throwable e) {
??????????????? reportWtf("starting Connectivity Service", e);
??????????? }
??????????? // ……
??????? }
??????? // ……
??? }
??? // ……
}
|
ntp
????????NetworkTimeUpdateService。
????????NtpTrustedTime(core/util)。
????????SntpClient(core/net)。
|