func (s *KFServer) startServer() bool { ret := s.handle.Start(&s.proc) if !ret { jklog.Lfile().Errorln("failed start server.") return false } for { jklog.L().Infoln("wait accept. ") jklog.Lfile().Infoln("wait accept. ") c := s.handle.Accept(&s.proc) if c == nil { jklog.Lfile().Errorln("accept failed.") return false } item := &sv.JKServerProcessItem{} item.Conn = c item.ReadDone = make(chan bool) go s.dealResponse(s.proc, item) go func() bool { for { _, err := s.handle.Read(&s.proc, item) if err != nil { item.ReadDone <- false return false } } return true }() } jklog.Lfile().Errorln("Program return for failed start.") return true }
func (s *KFServer) dealResponse(proc sv.JKServerProcess, item *sv.JKServerProcessItem) { for { jklog.Lfile().Infoln("wait response of read result.") ret := <-item.ReadDone /* now := time.Now().Unix() if item.TimeLast == 0 { item.TimeLast = now } if now-item.TimeLast > 5 { // 5 seconds continue } item.TimeLast = now */ if ret { jklog.Lfile().Debugln("response of deal ", item.RemoteAddr) // jklog.L().Debugln("data is : ", string(item.Data)) s.dealResponseCmd(string(item.Data), item) } else { jklog.Lfile().Errorln("read response failed ", item.RemoteAddr) break } } }
func (newser *JKNewServer) Accept(proc *JKServerProcess) net.Conn { acp, err := proc.Listen.Accept() if err != nil { jklog.Lfile().Errorln("accept failed. ", err) return nil } jklog.Lfile().Debugln("got accept from: ", acp.RemoteAddr().String()) return acp }
func (newser *JKNewServer) Read(proc *JKServerProcess, procItem *JKServerProcessItem) (int, error) { servItem := procItem remAddr := procItem.Conn.RemoteAddr().String() servItem.RemoteAddr = remAddr proc.addItem(servItem) jklog.L().Debugln("first read first bytes.") // first read 4 bytes for length. buflen := make([]byte, 4) _, err := procItem.Conn.Read(buflen) if err != nil { jklog.Lfile().Errorln("read failed of first read. ", err) return 0, err } datalen := int(BytesToInt(buflen)) jklog.L().Debugln("length of after data: ", datalen) readbuf := make([]byte, datalen) lenbuf := 0 jklog.L().Debugln("goto read data from : ", remAddr) for { if lenbuf >= datalen { procItem.ReadDone <- true break } buf := make([]byte, 1024) n, err := procItem.Conn.Read(buf) if err == io.EOF { jklog.Lfile().Infoln("EOF of read.") break } if err != nil { jklog.Lfile().Errorln("read data failed: ", err) return 0, err } copy(readbuf[lenbuf:lenbuf+n], buf[0:n]) lenbuf += n servItem.Data = readbuf } // procItem.ReadDone <- true jklog.L().Infoln("data from ", procItem.RemoteAddr, " with len ", lenbuf) return lenbuf, nil }
func (ser *JKServerProcess) listenLocalTCP(port int) bool { str := JK_NET_ADDRESS_LOCAL + ":" + fmt.Sprintf("%d", port) jklog.L().Debugln("start to listen : ", str) nt, err := net.ResolveTCPAddr("tcp", str) if err != nil { jklog.Lfile().Errorln("error resolve: ", err) return false } lis, err := net.ListenTCP("tcp", nt) if err != nil { jklog.Lfile().Errorln("error listen: ", err) return false } ser.Listen = lis return true }
func (s *KFServer) dealResponseCmd(data string, item *sv.JKServerProcessItem) { p := ParseJKProtocol(data) if p != nil { if p.Command() == JK_PROTOCOL_CMD_REGISTER { retstr := p.GenerateResponseOK() item.Conn.Write([]byte(retstr)) } else if p.Command() == JK_PROTOCOL_CMD_CONTROL && p.SubCommand() == JK_PROTOCOL_CMD_SAVEFILE { jklog.Lfile().Infoln("command with savefile.") filename, fdata := p.ParseFilenameData() ret := JKSaveFileData(p.ID(), filename, fdata) if ret { retstr := p.GenerateResponseOK() item.Conn.Write([]byte(retstr)) } else { retstr := p.GenerateResponseFail() item.Conn.Write([]byte(retstr)) } } } }