コード例 #1
0
ファイル: echo_client.go プロジェクト: Jasonix/link
func main() {
	var addr string

	flag.StringVar(&addr, "addr", "127.0.0.1:10010", "echo server address")
	flag.Parse()

	session, err := link.Connect("tcp", addr, link.String(link.Uint16BE))
	if err != nil {
		panic(err)
	}

	go func() {
		var msg string
		for {
			if err := session.Receive(&msg); err != nil {
				break
			}
			fmt.Printf("%s\n", msg)
		}
	}()

	for {
		var msg string
		if _, err := fmt.Scanf("%s\n", &msg); err != nil {
			break
		}
		if err = session.Send(msg); err != nil {
			break
		}
	}

	session.Close()
	println("bye")
}
コード例 #2
0
ファイル: main.go プロジェクト: ilahsa/go
// This is an echo server demo work with the echo_client.
// usage:
//     go run echo_server/main.go
func main() {
	common.ULogger.Info("stat chat at ", 10010)

	//flag.Parse()

	link.DefaultConnBufferSize = *buffersize
	link.DefaultProtocol = http.HttpPacket

	server, err := link.Listen("tcp", "0.0.0.0:10010")
	if err != nil {
		panic(err)
	}
	//server.Protocol()
	common.ULogger.Info("server start:", server.Listener().Addr().String())

	server.Serve(func(session *link.Session) {

		common.ULogger.Info("client", session.Conn().RemoteAddr().String(), "in")
		SessionInfo := &proxy.Session_Info{}
		session.State = SessionInfo

		session.Process(func(msg *link.InBuffer) error {

			common.ULogger.Info("receive request ", session.Conn().RemoteAddr().String(), ":", string(msg.Data))

			req, _ := http.Parse(msg.Data)
			req_CallId, ok1 := req.HEADER["CallId"]
			resp, other := proxy.ProcessRequest(req,
				SessionInfo)

			_ = other
			_, ok2 := resp.HEADER["CallId"]
			// add callid
			if ok1 && !ok2 {
				resp.AddResponseHeader("CallId", req_CallId)
			}
			//str := "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"

			common.ULogger.Info("send response ", session.Conn().RemoteAddr().String(), ":", string(resp.ToByte()))

			if SessionInfo.Data_Ex != "" {
				session.Send(link.Bytes(resp.ToByte()))
				return session.Send(link.String(SessionInfo.Data_Ex))
			} else {
				return session.Send(link.Bytes(resp.ToByte()))
			}
		})

		common.ULogger.Info("client", session.Conn().RemoteAddr().String(), "close")
	})
}
コード例 #3
0
ファイル: echo_broadcast.go プロジェクト: Jasonix/link
// This is broadcast server demo work with the echo_client.
// usage:
//     go run echo_broadcast.go
func main() {
	var addr string

	flag.StringVar(&addr, "addr", ":10010", "echo server address")
	flag.Parse()

	server, err := link.Serve("tcp", addr, link.String(link.Uint16BE))
	if err != nil {
		panic(err)
	}
	println("server start:", server.Listener().Addr().String())

	channel := link.NewChannel()
	go func() {
		for range time.Tick(time.Second * 2) {
			now := time.Now().Format("2006-01-02 15:04:05")
			channel.Broadcast("from channel: " + now)
		}
	}()

	for {
		session, err := server.Accept()
		if err != nil {
			break
		}

		go func() {
			addr := session.Conn().RemoteAddr().String()
			println("client", addr, "connected")

			session.EnableAsyncSend(1024)
			channel.Join(session)

			for {
				var msg string
				if err := session.Receive(&msg); err != nil {
					break
				}
				println(addr, "say:", msg)
				channel.Broadcast("from " + addr + ": " + string(msg))
			}

			println("client", addr, "closed")
			channel.Exit(session)
		}()
	}
}