/** * tunnel call **/ func onData(p *udppacket.Packet) int { LOG.Println("udp2tcp onData") p.LogPacket() // close packet if p.Length == 0 { releaseSession(p.SessionId, false) return 1 } s := getSession(p.SessionId) if s == nil { return -1 } //LOG.Println("udp2tcp check") s.Slock.Lock() s.ProcessNewPacketFromClientProxy(p) for { p := s.GetNextRecvDataToSend() if p == nil { LOG.Println("send data nil") break } processWrite(s, p.GetPacket()) } s.Slock.Unlock() return 0 }
/** * tunnel call * **/ func onData(p *udppacket.Packet) int { LOG.Println("tcp2udp onData") p.LogPacket() // getsession s, ok := idSessionMap[p.SessionId] if !ok { return -1 } if p.Length == 0 { releaseSession(s.GetId(), false) } // processNewPacketFromServerProxy s.Slock.Lock() s.ProcessNewPacketFromServerProxy(p) // getNextDataToSend for { p := s.GetNextRecvDataToSend() if p == nil { break } processWrite(s, p.GetPacket()) } s.Slock.Unlock() return 0 }
func (utAddId *UT_ADD_ID) WriteToClientProxy(p *udppacket.Packet) *udppacket.Packet { utAddId.LOG.Print("ut_add_id_module WriteToClientProxy") utAddId.lock.Lock() defer utAddId.lock.Unlock() if p.ModulesCtx[utAddId.Index] == nil { p.ModulesCtx[utAddId.Index] = new(CTX_UT_ADD_ID) tunnelId := utAddId.Ut.GetNewTunnelId() p.ChangeTunnelId(tunnelId) } return p }
/** * 处理服务器网关写原始数据 * rawData: 原始数据,但是前面预留了96(ut.Reserved)字节的头空间 **/ func (ut *UDPTunnel) WritePacketToClientProxy(p *udppacket.Packet) { //log.Println("udptunnel WritePacketToClientProxy") var sendP *udppacket.Packet = p size := len(ut.Handlers) for i := 0; i < size; i++ { sendP = ut.Handlers[i].WriteToClientProxy(sendP) if sendP == nil { break } } if sendP != nil { ut.send <- sendP.GetPacket() } }
/*********************************************************** * 处理发送给tunnel的数据包 * - 功能:将新数据包加入发送队列,重传的数据包,更新状态 ***********************************************************/ func (utNack *UT_NACK) processSendPacket(p *udppacket.Packet) { /***** SEND *****/ if wrapper, ok := utNack.SendMap[p.TunnelId]; ok { wrapper.p = p wrapper.status = WRAPPER_STATUS_RTR } else { // 新包,发送队列中不存在该packet的wrapper wrapper = new(PACKET_WRAPPER) wrapper.p = p wrapper.status = WRAPPER_STATUS_SENDED utNack.SendMap[p.TunnelId] = wrapper } if p.TunnelId > utNack.maxSendId { utNack.maxSendId = p.TunnelId } /***** RECV *****/ otherTunnelId, flag := utNack.getNackPacketTunnelId() p.ChangeOtherTunnelId(otherTunnelId) p.ChangePacketType(flag) }
/************************************************************* * 处理从tunnel收到的数据包 * - 功能:根据包的类型,处理相应的OtherTunnelId的数据包,是还没有到 * 该数据包,还是需要重传顺便对需要确认的数据包进行确认释放 ************************************************************/ func (utNack *UT_NACK) processRecvPacket(p *udppacket.Packet) { utNack.LOG.Println("processRecvPacket") status := p.GetPacketType() utNack.LOG.Println("tunnelId", p.TunnelId, "packetType", status) /***** SEND *****/ if status&udppacket.PACK_ACK == udppacket.PACK_ACK { utNack.LOG.Println("ack") utNack.ackPackets(p.OtherTunnelId) } /***** RECV *****/ wrapper, ok := utNack.RecvMap[p.TunnelId] if ok { wrapper.status = WRAPPER_STATUS_RECVED } else { wrapper = new(PACKET_WRAPPER) wrapper.status = WRAPPER_STATUS_RECVED } utNack.RecvMap[p.TunnelId] = wrapper if p.TunnelId > utNack.maxRecvId { utNack.maxRecvId = p.TunnelId } }
func (utWriter *UT_WRITER) WriteToClientProxy(p *udppacket.Packet) *udppacket.Packet { utWriter.Ut.WriteData(p.GetPacket()) return nil }