我正在实现一个 TCP 服务器应用程序,它在无限循环中接受传入的 TCP 连接。
我试图在整个应用程序中使用 Context 来允许关闭,这通常很好用。
我正在努力解决的一件事是取消一个正在等待 Accept() 的 net.Listener。我正在使用 ListenConfig,我相信它的优点是在创建侦听器时采用 Context。但是,取消此上下文并不具有中止 Accept 调用的预期效果。
这是一个演示相同问题的小应用程序:
package main
import (
"context"
"fmt"
"net"
"time"
)
func main() {
lc := net.ListenConfig{}
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(2*time.Second)
fmt.Println("cancelling context...")
cancel()
}()
ln, err := lc.Listen(ctx, "tcp", ":9801")
if err != nil {
fmt.Println("error creating listener:", err)
} else {
fmt.Println("listen returned without error")
defer ln.Close()
}
conn, err := ln.Accept()
if err != nil {
fmt.Println("accept returned …
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个简单的应用程序来捕获ACTION_NEW_OUTGOING_CALL
意图并编写一些调试信息.
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.apis"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name="DialerReceiver" android:exported="false" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
</manifest>
Run Code Online (Sandbox Code Playgroud)
这是DialerReceiver的代码:
package com.example.android.apis;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class DialerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
debugOut("arg0: " + arg0.toString());
debugOut("arg1: " + arg1.toString());
debugOut("isOrderedBroadcast = " + isOrderedBroadcast());
}
private static void debugOut(String str) {
Log.i("DialerReceiver", …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个由几个单独的类组成的库,这些类一起工作以执行库的功能.其中一个类然后公开了一些公共函数,所有公共函数都使用外部代码来使用库.
由于不同的类需要交互,我将它们全部放在同一个包中并且有很多"受保护"的东西(类和函数).
问题是ProGuard默认不会混淆这些受保护的元素,因为它们有可能在以后与同一个包中的另一个类组合.但是,出于安全原因,我想阻止这种情况.
所以问题是,除了私有元素之外,我还可以强制ProGuard对这些受保护的元素进行模糊处理吗?
换句话说,有没有办法告诉ProGuard"我已经完成了向这个软件包添加内容的东西,请不要混淆每个类中的私有内容,而是包中的受保护内容"?
谢谢!
我对 Golang 比较陌生,正在尝试将 Contexts 合并到我的代码中。
我看到了从父级取消以及共享特定于上下文的内容(例如记录器)方面的好处。
除此之外,我可能会遗漏一些东西,但我看不到孩子取消上下文的方法。此处的示例是如果子例程之一遇到错误,这意味着整个上下文已完成。
这是一些示例代码:
package main
import (
"context"
"fmt"
"math/rand"
"os"
"os/signal"
"sync"
"time"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
// handle SIGINT (control+c)
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
fmt.Println("main: interrupt received. cancelling context.")
cancel()
}()
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
child1DoWork(ctx)
wg.Done()
}()
wg.Add(1)
go func() {
child2DoWork(ctx)
wg.Done()
}()
fmt.Println("main: waiting for children to finish")
wg.Wait()
fmt.Println("main: children done. exiting.")
}
func child1DoWork(ctx …
Run Code Online (Sandbox Code Playgroud)