在某些场景下,我们需要监测系统中网络接入情况的变化,据此作出相应的处理。
例如,用户成功接入了某一个网络,移动场景下从一个WiFi网络切换到另一个网络,网络断开等。
下面基于Go语言,在Mac平台实现了一个检测网络变化的例子。再这个示例中,使用到了cgo进行C和Go混合编程。Go语言负责处理业务逻辑,具体的操作系统事件的监测由C语言的接口完成。
在循环中监测到变化,调用C语言中定义的callback
函数,具体的处理动作再调用Go中定义的networkchange()函数处理。
package main
/*
#cgo LDFLAGS: -framework SystemConfiguration -framework CoreFoundation
#include <SystemConfiguration/SystemConfiguration.h>
#include <stdlib.h>
void callback(SCDynamicStoreRef store, CFArrayRef changedKeys, void *info) {
char **services = (char **)info;
CFIndex count = CFArrayGetCount(changedKeys);
for (CFIndex i = 0; i < count; i++) {
CFStringRef key = CFArrayGetValueAtIndex(changedKeys, i);
const char *cKey = CFStringGetCStringPtr(key, kCFStringEncodingUTF8);
if (cKey) {
services[i] = strdup(cKey);
}
}
}
extern void networkchange();
void callback(SCDynamicStoreRef store, CFArrayRef changedKeys, void *info) {
printf("network event detected\n");
networkchange();
}
*/
import "C"
import (
"fmt"
"time"
"unsafe"
)
func main() {
storeName := C.CString("NetworkChangeDetector")
defer C.free(unsafe.Pointer(storeName))
cfString := C.CFStringCreateWithCString(C.kCFAllocatorDefault, storeName, C.kCFStringEncodingUTF8)
defer C.CFRelease(C.CFTypeRef(cfString)) // 释放 CFStringRef
store := C.SCDynamicStoreCreate(nil, storeName, C.SCDynamicStoreCallBack(C.callback), nil)
if *(*C.uintptr_t)(unsafe.Pointer(&store)) == 0 {
fmt.Println("Failed to create SCDynamicStore")
return
}
keys := C.CFArrayCreateMutable(nil, 0, &C.kCFTypeArrayCallBacks)
defer C.CFRelease(C.CFTypeRef(keys))
key1 := C.CFStringCreateWithCString(C.kCFAllocatorDefault, C.CString("State:/Network/Interface"), C.kCFStringEncodingUTF8)
defer C.CFRelease(C.CFTypeRef(key1))
C.CFArrayAppendValue(keys, unsafe.Pointer(key1))
key2 := C.CFStringCreateWithCString(C.kCFAllocatorDefault, C.CString("State:/Network/Global/IPv4"), C.kCFStringEncodingUTF8)
defer C.CFRelease(C.CFTypeRef(key2))
C.CFArrayAppendValue(keys, unsafe.Pointer(key2))
key3 := C.CFStringCreateWithCString(C.kCFAllocatorDefault, C.CString("State:/Network/Global/IPv6"), C.kCFStringEncodingUTF8)
defer C.CFRelease(C.CFTypeRef(key1))
C.CFArrayAppendValue(keys, unsafe.Pointer(key3))
var context C.SCDynamicStoreContext
context.version = 0
context.info = nil
context.retain = nil
context.release = nil
context.copyDescription = nil
emptyArray := C.CreateEmptyCFArray()
C.SCDynamicStoreSetNotificationKeys(store, keys, emptyArray)
rl := C.CFRunLoopGetCurrent()
src := C.SCDynamicStoreCreateRunLoopSource(nil, store, 0)
C.CFRunLoopAddSource(rl, src, C.kCFRunLoopDefaultMode)
C.CFRunLoopRun()
}
//export onNetworkChange
func onNetworkChange(count C.int, keys **C.char, info unsafe.Pointer) {
ctx := (*Context)(info)
fmt.Printf("Network services changed: %s\n", ctx.message)
for i := 0; i < int(count); i++ {
key := C.GoString(*(**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(keys)) + uintptr(i)*unsafe.Sizeof(uintptr(0)))))
if key != "" {
fmt.Println(key)
}
}