Пример #1
0
func (self *Server) Start(connString string) {

	listener, err := net.Listen("tcp", connString)

	if err != nil {
		log.Println(err)
	}

	self.listener = listener

	log.Println("Server start")

	// filling the tokens
	for i := 0; i < MAX_SESSIONS; i++ {
		self.generateToken()
	}

	for {
		conn, err := self.listener.Accept()

		if err != nil {
			log.Println(err)
			return
		}

		log.Printf("A new connection [ %+v ]  %s\n", conn, conn.RemoteAddr().String())

		self.takeToken()
		self.pending <- conn
	}
}
Пример #2
0
func (self *Session) Write() {

	for packet := range self.outgoing {
		if err := self.packetWriter.WriteAPacket(&packet); err != nil {
			log.Println("Write a packet error:", err)
			self.quit()
			return
		}

		if err := self.packetWriter.Flush(); err != nil {
			log.Println("Write error:", err)
			self.quit()
			return
		}
	}

}
Пример #3
0
func (w *PacketWriter) WriteAPacket(packet *Packet) (err error) {

	buf := make([]byte, 4)

	//write packet hat
	_, err = w.bw.Write(PacketHat)
	if err != nil {
		return err
	}
	log.Println("数据头", PacketHat)
	//write packet type
	binary.BigEndian.PutUint32(buf, packet.GetType())
	_, err = w.bw.Write(buf)
	if err != nil {
		return err
	}
	log.Println("数据类型", buf)
	//write packet length
	binary.BigEndian.PutUint32(buf, uint32(len(packet.GetData())))
	_, err = w.bw.Write(buf)
	if err != nil {
		return err
	}
	log.Println("数据长度", buf)
	//write packet data
	_, err = w.bw.Write(packet.GetData())
	if err != nil {
		return err
	}
	log.Println("数据体", packet.GetData())
	//.....................写CRC32校验..........................
	intCRC := crc32.ChecksumIEEE(packet.GetData())
	binary.BigEndian.PutUint32(buf, intCRC)
	_, err = w.bw.Write(buf)
	if err != nil {
		return err
	}
	log.Println("数据校验", buf)
	return nil
}
Пример #4
0
func (self *Session) Read() {

	for {
		if packet, err := self.packetReader.ReadAPacket(); err == nil {
			log.Printf("放入进入信息通道%+v", packet)
			self.incoming <- *packet
		} else {
			log.Println("Read error:", err)
			self.quit()
			return
		}
	}
}
Пример #5
0
func (r *PacketReader) ReadAPacket() (packet *Packet, err error) {

	packet = NewPacket()

	//check packet hat
	hasHat, err := r.checkPacketHat()
	if !hasHat {
		return nil, ErrInvalidPacketHat
	}

	//check packet type
	packetType, err := r.readPacketType()

	if err != nil {
		return nil, err
	}

	packet.SetType(packetType)

	//check packet length
	packetLength, err := r.readPacketLength()

	if err != nil {
		return nil, err
	}

	if packetLength > maxPacketLength {
		return nil, ErrInvalidPacketLength
	}

	//alloc packet data buffer
	packetData := make([]byte, packetLength)
	//read packet data
	err = r.readPacketData(packetData)
	if err != nil {
		return nil, err
	}
	packet.SetData(packetData)
	log.Printf("收到的数据包-->%+v", packet)
	err = r.checkDataSumIEEE(packetData)
	if err != nil {
		log.Println("read errors: %s", err)
		return nil, err
	}
	return packet, nil
}
Пример #6
0
func main() {
	s2c_server := s2c.CreateServer()
	log.Println("Running on")
	s2c_server.Start(":1114")
}
Пример #7
0
func StartAgent(in chan Packet, conn net.Conn) {
	log.Println("hello agent")
}