Пример #1
0
func receiveMsg(conn net.Conn) {
	buf := make([]byte, 128)
	n, err := conn.Read(buf)
	if checkerr(err) {
		os.Exit(-1)
		return
	}
	// fmt.Printf("\nread (%d) byte from %v :\n%v\n", n, conn.RemoteAddr(), buf[:n])

	protobuf := proto.NewBuffer(buf[:n])
	var _msg msg.OSMsg
	pumerr := protobuf.Unmarshal(&_msg)
	if checkerr(pumerr) {
		return
	}
	fmt.Printf("[received <----- message]%v\n", _msg.String())
}
Пример #2
0
func sendMsg(conn net.Conn) chan bool {
	// _msg := msg.OSMsg{Fromu: proto.String("jack"), Tou: proto.String("tom"), Content: proto.String("hello,tom")}
	_msg := msg.OSMsg{Fromu: proto.String("tom"), Tou: proto.String("jack"), Content: proto.String("hello,jack")}
	end := make(chan bool)
	for {
		// send
		data, err := proto.Marshal(&_msg)
		if checkerr(err) {
			end <- true
			break
		}
		conn.Write(data)

		proto.Unmarshal(data, &_msg)
		fmt.Printf("[send -----> message]%s\n", _msg.String())
		time.Sleep(4e9)
	}
	return end
}
Пример #3
0
func (s *Server) Accept(conn *net.TCPConn) {
	defer func() {
		if err := recover(); err != nil {
		}
	}()
	fmt.Println("客户端连接来自:", conn.RemoteAddr().String())
	bs := make([]byte, 1024)
	var msg_tmp msg.OSMsg
	var login_user string
	once := sync.Once{}
	for {
		n, err := conn.Read(bs)
		if checkerr(err) {
			break
		}
		err = proto.Unmarshal(bs[:n], &msg_tmp)
		if checkerr(err) {
			continue
		}
		once.Do(func() {
			login_user = msg_tmp.GetFromu()
			err := s.keeper.Login(login_user, conn)
			if checkerr(err) {
				panic(err)
			}
		})
		err = s.keeper.SendMsgTo(msg_tmp.GetTou(), bs[:n])
		if checkerr(err) {
			continue
		}
		msg_tmp.Reset()
	}
	defer func() {
		s.keeper.Logout(login_user)
	}()

}