示例#1
0
文件: server.go 项目: bpxeax/go_start
func runTCP(port, password string) {
	tcp_link, err := net.Listen("tcp", ":"+port)
	if err != nil {
		log.Printf("error listening tcp port %v: %v\n", port, err)
		os.Exit(1)
	}
	passwdManager.add(port, password, tcp_link)
	var cipher *ss.Cipher
	log.Printf("server listening tcp port %v ...\n", port)
	for {
		conn, err := tcp_link.Accept()
		if err != nil {
			// listener maybe closed to update password
			debug.Printf("tcp accept error: %v\n", err)
			return
		}
		// Creating cipher upon first connection.
		if cipher == nil {
			log.Println("creating cipher for port:", port)
			cipher, err = ss.NewCipher(config.Method, password)
			if err != nil {
				log.Printf("Error generating cipher for port: %s %v\n", port, err)
				conn.Close()
				continue
			}
		}
		go handleConnection(ss.NewConn(conn, cipher.Copy()))
	}
}
示例#2
0
func get(connid int, uri, serverAddr string, rawAddr []byte, cipher *ss.Cipher, done chan []time.Duration) {
	reqDone := 0
	reqTime := make([]time.Duration, config.nreq)
	defer func() {
		done <- reqTime[:reqDone]
	}()
	tr := &http.Transport{
		Dial: func(_, _ string) (net.Conn, error) {
			return ss.DialWithRawAddr(rawAddr, serverAddr, cipher.Copy())
		},
	}

	buf := make([]byte, 8192)
	client := &http.Client{Transport: tr}
	for ; reqDone < config.nreq; reqDone++ {
		start := time.Now()
		if err := doOneRequest(client, uri, buf); err != nil {
			return
		}
		reqTime[reqDone] = time.Now().Sub(start)

		if (reqDone+1)%1000 == 0 {
			fmt.Printf("conn %d finished %d get requests\n", connid, reqDone+1)
		}
	}
}