Exemplo n.º 1
0
// ClientLive method for accept new connection from socket
func ClientLive(buffers Buffers, conf core.Config, c net.Conn) {
	defer core.ErrorNetworkHandler(c)

	buffer := bytes.NewBuffer([]byte{})
	player := entitie.NewPlayer(c)
	newPill := pill.NewPill()

	for getBytes := range buffers.GetReadChannel() {
		buffer.Reset()
		buffer.Write(getBytes)

		log.WithField("bytes", fmt.Sprintf("% x", buffer.Bytes())).Info("Print message from client")

		if buffer.Len() <= 2 {
			buffers.GetWriteChannel() <- []byte{0x00, 0x02}
			continue
		}

		opcodes, err := newPill.Decrypt(buffer.Bytes(), player)
		if err != nil {
			log.WithError(err).Panic("Error in pill decrypt")
		}

		if opcodes != nil {
			for _, opcode := range opcodes {
				pillEncrypt, err := newPill.Encrypt(newPill.GetPill(opcode), player)
				if err != nil {
					log.WithError(err).Error("Error in pill encrypt")
				}

				buffers.GetWriteChannel() <- pillEncrypt
			}
		}
	}
}
Exemplo n.º 2
0
// Run method for starting server
func (a *Application) Run() (err error) {
	listen, err := net.Listen("tcp", a.Config.Base.IP+":"+a.Config.Base.Port)
	if err != nil {
		return
	}

	for {
		client, err := listen.Accept()
		if err != nil {
			log.WithError(err).Error("Error in accept connection")
			continue
		}

		go func(c net.Conn, conf core.Config) {
			var buffers *core.Buffers = core.NewBuffers()

			defer func() {
				buffers.Close()
				core.ErrorNetworkHandler(c)
			}()

			log.WithFields(log.Fields{
				"address": c.RemoteAddr(),
			}).Info("Client is connected")

			go ConnectHandler(buffers, *a, c)
			go buffers.WriteHandler(c)

			buffers.ReadHandler(c, conf)
		}(client, a.Config)
	}
}
Exemplo n.º 3
0
// ConnectHandler method for accept new connection from socket
func ConnectHandler(buffers *core.Buffers, app Application, c net.Conn) {
	defer core.ErrorNetworkHandler(c)

	var (
		buffer      *bytes.Buffer      = bytes.NewBuffer([]byte{})
		player      *entitie.Player    = entitie.NewPlayer(buffers)
		database    *database.Database = database.NewDatabase(&app.DatabaseInfo)
		packetAlloc *packet.Packet     = packet.NewPacket(player, database)
	)

	// Once send first a packet
	packet, err := packetAlloc.Encode(support.OP_SERVER_CHAPSTR)
	if err != nil {
		log.WithError(err).Error("Error in packet encode")
	}

	buffers.GetWC() <- packet

	for getBytes := range buffers.GetRC() {
		buffer.Reset()
		buffer.Write(getBytes)

		log.WithField("bytes", fmt.Sprintf("% x", buffer.Bytes())).Info("Print message from client")

		// Ping <-> pong
		if buffer.Len() <= 2 {
			buffers.GetWC() <- []byte{0x00, 0x02}
			continue
		}

		opcodes, err := packetAlloc.Decode(buffer.Bytes())
		if err != nil {
			log.WithError(err).Error("Error in packet decode")
			return
		}

		if len(opcodes) == 0 {
			continue
		}

		for _, opcode := range opcodes {
			response, err := packetAlloc.Encode(opcode)
			if err != nil {
				log.WithError(err).Error("Error in packet encode")
				break
			}

			buffers.GetWC() <- response
		}

		if player.Error != nil {
			// Before disconnect
			time.Sleep(time.Second)

			log.WithError(player.Error).Error("Client was rejected by God!")
			return
		}
	}
}
Exemplo n.º 4
0
// WriteHandler method for write bytes to socket in loop from channel
func (b *Buffers) WriteHandler(c net.Conn) {
	defer core.ErrorNetworkHandler(c)

	// Write one packet for client with time.Now()
	pillInit := pill.NewPill()
	packet, err := pillInit.Encrypt(pillInit.SetOpcode(940).GetOutcomingCrumb())
	if err != nil {
		log.WithError(err).Error("Error in pill encrypt")
	}

	c.Write(packet)

	for v := range b.WriteChannel {
		c.Write(v)
	}
}
Exemplo n.º 5
0
// WriteHandler method for write bytes to socket in loop from channel
func (b *Buffers) WriteHandler(c net.Conn) {
	defer core.ErrorNetworkHandler(c)

	// Write one packet for client with time.Now()
	newPill := pill.NewPill()
	packet, err := newPill.Encrypt(newPill.GetPill(pill.DateOpcode), &entitie.Player{})
	if err != nil {
		log.WithError(err).Error("Error in pill encrypt")
	}

	c.Write(packet)

	for v := range b.WriteChannel {
		c.Write(v)
	}
}
Exemplo n.º 6
0
// ClientLive method for accept new connection from socket
func ClientLive(buffers Buffers, conf core.Config, c net.Conn) {
	defer core.ErrorNetworkHandler(c)

	buffer := bytes.NewBuffer([]byte{})
	player := entitie.NewPlayer(c)
	pillInit := pill.NewPill()

	for getBytes := range buffers.GetReadChannel() {
		buffer.Write(getBytes)

		if conf.Base.Test {
			log.Panic("Client is break =_=")
		} else {
			log.WithField("bytes", fmt.Sprintf("% x", buffer.Bytes())).Info("Print message from client")

			if buffer.Len() >= 8 {
				opcodes, err := pillInit.Decrypt(buffer.Bytes(), *player)
				if err != nil {
					log.WithError(err).Panic("Error in pill decrypt")
				}

				if opcodes != nil {
					for _, v := range opcodes {
						pillEncrypt, err := pillInit.Encrypt(pillInit.SetOpcode(v).GetOutcomingCrumb())
						if err != nil {
							log.WithError(err).Error("Error in pill encrypt")
						}

						buffers.GetWriteChannel() <- pillEncrypt
					}
				}
			} else {
				buffers.GetWriteChannel() <- []byte{0x00, 0x02}
			}
		}

		buffer.Reset()
	}
}
Exemplo n.º 7
0
// Run method for starting server
func (a *Application) Run() (err error) {
	listen, err := net.Listen("tcp", a.Config.Base.IP+":"+a.Config.Base.Port)
	if err != nil {
		return
	}

	robot := robot.NewRobot()
	for _, factory := range robot.Factories {
		go factory.Process(a.Config)
	}

	for {
		client, err := listen.Accept()
		if err != nil {
			log.WithError(err).Error("Error in accept connection")
			continue
		}

		go func(c net.Conn, conf core.Config) {
			var buffers *Buffers = NewBuffers()

			defer func() {
				close(buffers.GetReadChannel())
				close(buffers.GetWriteChannel())
				core.ErrorNetworkHandler(c)
			}()

			log.WithFields(log.Fields{
				"address": c.RemoteAddr(),
			}).Info("Client is connected")

			go ClientLive(*buffers, conf, c)
			go buffers.WriteHandler(c)

			buffers.ReadHandler(c, conf)
		}(client, a.Config)
	}
}
Exemplo n.º 8
0
// Run method for starting server
func (a *Application) Run() (err error) {
	listen, err := net.Listen("tcp", a.Config.Base.IP+":"+a.Config.Base.Port)
	if err != nil {
		return
	}

	// Loading station for hard work
	databaseRobot := station.NewDatabaseRobot()
	go databaseRobot.SaveOnTimeout(a.Config)

	for {
		client, err := listen.Accept()
		if err != nil {
			log.WithError(err).Error("Error in accept connection")
			continue
		}

		go func(c net.Conn, conf core.Config) {
			var buffers *Buffers = NewBuffers()

			defer func() {
				close(buffers.GetReadChannel())
				close(buffers.GetWriteChannel())
				core.ErrorNetworkHandler(c)
			}()

			log.WithFields(log.Fields{
				"address": c.RemoteAddr(),
			}).Info("Client is connected")

			go ClientLive(*buffers, conf, c)
			go buffers.WriteHandler(c)

			buffers.ReadHandler(c, conf)
		}(client, a.Config)
	}
}