<commit_before>package main

import (
	"net"
	"sync"

	"fmt"

	"math/rand"

	"time"

	"reflect"

	"os"

	"github.com/xiaonanln/goworld/common"
	"github.com/xiaonanln/goworld/config"
	"github.com/xiaonanln/goworld/consts"
	"github.com/xiaonanln/goworld/entity"
	"github.com/xiaonanln/goworld/gwlog"
	"github.com/xiaonanln/goworld/netutil"
	"github.com/xiaonanln/goworld/proto"
)

type ClientBot struct {
	sync.Mutex

	id                 int
	waiter             *sync.WaitGroup
	conn               *proto.GoWorldConnection
	entities           map[common.EntityID]*ClientEntity
	player             *ClientEntity
	currentSpace       *ClientSpace
	logined            bool
	startedDoingThings bool
}

func newClientBot(id int, waiter *sync.WaitGroup) *ClientBot {
	return &ClientBot{
		id:       id,
		waiter:   waiter,
		entities: map[common.EntityID]*ClientEntity{},
	}
}

func (bot *ClientBot) String() string {
	return fmt.Sprintf("ClientBot<%d>", bot.id)
}

func (bot *ClientBot) run() {
	defer bot.waiter.Done()

	gwlog.Info("%s is running ...", bot)

	serverIDs := config.GetServerIDs()
	// choose a random serverID
	serverID := serverIDs[rand.Intn(len(serverIDs))]
	gwlog.Debug("%s is connecting to server %d", bot, serverID)
	cfg := config.GetServer(serverID)
	cfg = cfg
	var conn net.Conn
	var err error
	for { // retry for ever
		conn, err = netutil.ConnectTCP("10.246.13.148", cfg.Port)
		if err != nil {
			gwlog.Error("Connect failed: %s", err)
			time.Sleep(time.Second * time.Duration(1+rand.Intn(10)))
			continue
		}
		// connected , ok
		break
	}
	conn.(*net.TCPConn).SetWriteBuffer(64 * 1024)
	conn.(*net.TCPConn).SetReadBuffer(64 * 1024)
	gwlog.Info("connected: %s", conn.RemoteAddr())
	bot.conn = proto.NewGoWorldConnection(conn)
	bot.conn.SetAutoFlush(time.Millisecond * 10)
	defer bot.conn.Close()

	bot.loop()
}

func (bot *ClientBot) loop() {
	var msgtype proto.MsgType_t
	for {
		err := bot.conn.SetRecvDeadline(time.Now().Add(time.Millisecond * 20))
		if err != nil {
			gwlog.Panic(err)
		}

		pkt, err := bot.conn.Recv(&msgtype)

		if pkt != nil {
			bot.handlePacket(msgtype, pkt)
			pkt.Release()
		} else if err != nil && !netutil.IsTemporaryNetError(err) {
			// bad error
			gwlog.Panic(err)
		}<commit_msg>gwc set deadline<commit_after>package main

import (
	"net"
	"sync"

	"fmt"

	"math/rand"

	"time"

	"reflect"

	"os"

	"github.com/xiaonanln/goworld/common"
	"github.com/xiaonanln/goworld/config"
	"github.com/xiaonanln/goworld/consts"
	"github.com/xiaonanln/goworld/entity"
	"github.com/xiaonanln/goworld/gwlog"
	"github.com/xiaonanln/goworld/netutil"
	"github.com/xiaonanln/goworld/proto"
)

type ClientBot struct {
	sync.Mutex

	id                 int
	waiter             *sync.WaitGroup
	conn               *proto.GoWorldConnection
	entities           map[common.EntityID]*ClientEntity
	player             *ClientEntity
	currentSpace       *ClientSpace
	logined            bool
	startedDoingThings bool
}

func newClientBot(id int, waiter *sync.WaitGroup) *ClientBot {
	return &ClientBot{
		id:       id,
		waiter:   waiter,
		entities: map[common.EntityID]*ClientEntity{},
	}
}

func (bot *ClientBot) String() string {
	return fmt.Sprintf("ClientBot<%d>", bot.id)
}

func (bot *ClientBot) run() {
	defer bot.waiter.Done()

	gwlog.Info("%s is running ...", bot)

	serverIDs := config.GetServerIDs()
	// choose a random serverID
	serverID := serverIDs[rand.Intn(len(serverIDs))]
	gwlog.Debug("%s is connecting to server %d", bot, serverID)
	cfg := config.GetServer(serverID)
	cfg = cfg
	var conn net.Conn
	var err error
	for { // retry for ever
		conn, err = netutil.ConnectTCP("10.246.13.148", cfg.Port)
		if err != nil {
			gwlog.Error("Connect failed: %s", err)
			time.Sleep(time.Second * time.Duration(1+rand.Intn(10)))
			continue
		}
		// connected , ok
		break
	}
	conn.(*net.TCPConn).SetWriteBuffer(64 * 1024)
	conn.(*net.TCPConn).SetReadBuffer(64 * 1024)
	gwlog.Info("connected: %s", conn.RemoteAddr())
	bot.conn = proto.NewGoWorldConnection(conn)
	defer bot.conn.Close()

	bot.loop()
}

func (bot *ClientBot) loop() {
	var msgtype proto.MsgType_t
	for {
		err := bot.conn.SetRecvDeadline(time.Now().Add(time.Millisecond * 20))
		if err != nil {
			gwlog.Panic(err)
		}

		pkt, err := bot.conn.Recv(&msgtype)

		if pkt != nil {
			bot.handlePacket(msgtype, pkt)
			pkt.Release()
		} else if err != nil && !netutil.IsTemporaryNetError(err) {
			// bad error
			gwlog.Panic(err)
		}
{'fraction_non_alphanumeric': 0.14359900654775345, 'fraction_numerical': 0.012643937683449989, 'mean_word_length': 3.3516699410609037, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1536082', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1662, 'n_words': 324}