func connectToServer(s *udpsession.Session) bool { conn, err := net.Dial("tcp", "localhost:90") if err != nil { log.Println("connectToServer", err) return false } s.C = &conn return true }
/** * server write **/ func processWrite(s *udpsession.Session, data []byte) { conn := *s.C index := 0 id := s.GetId() for { length, err := conn.Write(data[index:]) if err != nil { releaseSession(id, true) return } if length != len(data) { index = length } else { break } } return }
/** * server read **/ func processRead(s *udpsession.Session) { conn := *s.C LOG.Println("udp2tcp processRead") id := s.GetId() for { buf := make([]byte, 4096) n, err := conn.Read(buf[96:]) if err != nil { LOG.Println("server read ", err) releaseSession(id, true) break } s.ProcessNewDataToClientProxy(buf[:96+n]) for { p := s.GetNextSendDataToSend() if p == nil { break } // tunnel write p.LogPacket() ut.WritePacketToClientProxy(p) } } }
/** * client tcp read **/ func processRead(s *udpsession.Session) { //LOG.Println("tcp2udp processRead") conn := *s.C id := s.GetId() for { ///////////////////////////////////////////////// buf := make([]byte, 4096) length, err := conn.Read(buf[96:]) if err != nil { LOG.Println("client read error", err) releaseSession(id, true) break } ///////////////////////////////////////////////// s.ProcessNewDataToServerProxy(buf[:length+96]) for { p := s.GetNextSendDataToSend() if p == nil { break } p.LogPacket() rc := ut.WritePacketToServerProxy(p) // 检查数据处理结果 if rc == -1 { LOG.Println("client send err") break } } } conn.Close() }