예제 #1
0
func NewChatServer(listenAddr string) *ChatServer {
	return &ChatServer{
		listener:   muduo.ListenTcpOrDie(listenAddr),
		conns:      make(map[*connection]bool),
		boardcast:  make(chan []byte),      // size?
		register:   make(chan *connection), // size?
		unregister: make(chan *connection), // size?
	}
}
예제 #2
0
func receive() {
	listener := muduo.ListenTcpOrDie(fmt.Sprintf(":%d", opt.port))
	defer listener.Close()
	println("Accepting", listener.Addr().String())
	conn, err := listener.Accept()
	muduo.PanicOnError(err)
	defer conn.Close()

	// Read header
	var sessionMessage SessionMessage
	err = binary.Read(conn, binary.BigEndian, &sessionMessage)
	muduo.PanicOnError(err)

	fmt.Printf("receive buffer length = %d\nreceive number of buffers = %d\n",
		sessionMessage.Length, sessionMessage.Number)
	total_mb := float64(sessionMessage.Number) * float64(sessionMessage.Length) / 1024.0 / 1024.0
	fmt.Printf("%.3f MiB in total\n", total_mb)

	payload := make([]byte, sessionMessage.Length)
	start := time.Now()
	for i := 0; i < int(sessionMessage.Number); i++ {
		var length int32
		err = binary.Read(conn, binary.BigEndian, &length)
		muduo.PanicOnError(err)
		muduo.Check(length == sessionMessage.Length, "read length")

		var n int
		n, err = io.ReadFull(conn, payload)
		muduo.PanicOnError(err)
		muduo.Check(n == len(payload), "read payload")

		// ack
		err = binary.Write(conn, binary.BigEndian, &length)
		muduo.PanicOnError(err)
	}

	elapsed := time.Since(start).Seconds()
	fmt.Printf("%.3f seconds\n%.3f MiB/s\n", elapsed, total_mb/elapsed)
}
예제 #3
0
func NewEchoServer(listenAddr string) *EchoServer {
	server := new(EchoServer)
	server.listener = muduo.ListenTcpOrDie(listenAddr)
	return server
}
예제 #4
0
func NewDaytimeServer(listenAddr string) *DaytimeServer {
	server := new(DaytimeServer)
	server.listener = muduo.ListenTcpOrDie(listenAddr)
	return server
}
예제 #5
0
func NewDiscardServer(listenAddr string) *DiscardServer {
	server := new(DiscardServer)
	server.listener = muduo.ListenTcpOrDie(listenAddr)
	return server
}
예제 #6
0
func NewChargenServer(listenAddr string) *ChargenServer {
	server := new(ChargenServer)
	server.listener = muduo.ListenTcpOrDie(listenAddr)
	return server
}