Created
June 21, 2016 11:53
-
-
Save freekman/3836bdcd00fb3127f2b71bd3d73201df to your computer and use it in GitHub Desktop.
Req/Resp for Gps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (s *session) Send(req listener.CmdRequest) listener.CmdResponse { | |
bytes := req.Bytes() | |
_, err := s.conn.Write(bytes) | |
if err != nil { | |
return listener.CmdResponse{} | |
} | |
p := packet{bytes} | |
s.addCMD(p.command()) | |
res := <-s.response | |
return listener.CmdResponse{Data: res} | |
} | |
func (s *session) Close() error { | |
return s.conn.Close() | |
} | |
func (s *session) addCMD(id uint16) { | |
s.Lock() | |
defer s.Unlock() | |
if cmds, ok := s.pending[id]; ok { | |
cmds = append(cmds, id) | |
s.pending[id] = cmds | |
} else { | |
s.pending[id] = []uint16{id} | |
} | |
} | |
func (s *session) removeCMD(id uint16) { | |
s.Lock() | |
defer s.Unlock() | |
if cmds, ok := s.pending[id]; ok { | |
cmdLen := len(s.pending[id]) | |
if cmdLen > 0 { | |
c := make([]uint16, cmdLen-1) | |
copy(c, cmds[0:cmdLen-1]) | |
s.pending[id] = c | |
} | |
if len(s.pending[id]) == 0 { | |
delete(s.pending, id) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment