示例#1
0
func callScriptFunc(L *lua.LState, scriptID int, fn string, params ...lua.LValue) {
	if err := ScriptSystem.loadScript(L, scriptID); err != nil {
		logger.Errorf("[callScriptFunc] ScriptSystem.loadScript failed! err=%v", err)
		return
	}

	top := L.GetTop()
	L.Push(L.GetGlobal(fmt.Sprintf("x%d_%s", scriptID, fn)))
	for _, v := range params {
		L.Push(v)
	}

	if err := L.PCall(len(params), lua.MultRet, nil); err != nil {
		L.SetTop(top)
		logger.Errorf("[callScriptFunc] L.PCall failed! err=%v", err)
		return
	}

	//parse return values from lua
	argnow := L.GetTop()
	// arglen := argnow - top
	// for i := 1; i < arglen; i++ {

	// }
	L.Pop(argnow)
}
示例#2
0
func SendShakeHand(obj *network.SocketBase, result uint16) {
	msg := new(packet.M2C_Resp_ShakeHand)
	msg.Result = result

	message, err := msg.Pack()
	if err != nil {
		logger.Errorf("msg.Pack failed! err=%v", err)
		return
	}

	if err := obj.SendMsg(message); err != nil {
		logger.Errorf("obj.SendMsg failed! err=%v", err)
	}
}
示例#3
0
func ShakeHand(obj *network.SocketBase, msg *network.Message) int {

	message := new(packet.C2M_Req_ShakeHand)
	err := message.UnPack(msg)
	if err != nil {
		SendShakeHand(obj, ID.REQ_INVALID)
		logger.Errorf("[ShakeHand] UnPack failed! err=%v\n", err)
		return ID.MESSAGE_OK
	}

	SendShakeHand(obj, ID.REQ_SHAKEHAND_OK)
	logger.Errorf("[ShakeHand] Process successful! Greeting=%s\n", message.Greeting)

	return ID.MESSAGE_OK
}
示例#4
0
func ProcessConnection(conn *net.TCPConn) {
	defer logger.CatchException()

	SocketBase := network.NewSocketBase(conn)
	defer SocketBase.Close()

	SocketBase.SetNoDelay(true)                                        //无延迟
	SocketBase.SetKeepAlive(true)                                      //保持激活
	SocketBase.SetReadBuffer(64 * 1024)                                //设置读缓冲区大小
	SocketBase.SetWriteBuffer(64 * 1024)                               //设置写缓冲区大小
	SocketBase.SetReadDeadline(time.Now().Add(30000000 * time.Second)) //设置读超时

	for {
		msgs, err := SocketBase.RecvMsgs()
		if err != nil {
			logger.Errorf("SocketBase.RecvMsgs failed! err=%v", err)
			return
		}

		if len(msgs) == 0 {
			continue
		}

		for _, msg := range msgs {
			ret := network.Dispatcher(SocketBase, msg)
			if ret == ID.MESSAGE_OK {
				return
			}
		}
	}
}
示例#5
0
文件: client.go 项目: danuxguin/dxnet
func main() {

	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", config.SERVER_IP, config.SERVER_PORT))
	if err != nil {
		logger.Errorf("net.Dial err=%v server_ip_port = %v", err, fmt.Sprintf("%s:%s", config.SERVER_IP, config.SERVER_PORT))
		return
	}

	for {
		data := make([]byte, 1024)

		pos := 0
		common.WriteUint32(data[pos:pos+4], uint32(1001))
		pos += 4
		common.WriteUint32(data[pos:pos+4], uint32(4))
		pos += 4
		common.WriteUint32(data[pos:pos+4], uint32(4))
		pos += 4
		common.WriteUint32(data[pos:pos+4], uint32(4))
		pos += 4
		common.WriteUint32(data[pos:pos+4], uint32(1))
		pos += 4

		_, err := conn.Write(data[0:pos])
		if err != nil {
			fmt.Printf("net.Write err=%v\n", err)
			return
		}

		c, err := io.ReadAtLeast(conn, data, 16)
		if err != nil {
			fmt.Printf("err=%v\n", err)
			return
		}

		fmt.Printf("header=%v\n", data[0:c])

		// header := network.ParseHeader(data[0:common.PACKET_HEAD_LEN])
		//
		// _, err = conn.Read(data[0:header.MsgLen])
		// if err != nil {
		// 	fmt.Printf("err=%v\n", err)
		// 	return
		// }
		result, err := ioutil.ReadAll(conn)
		if err != nil {
			fmt.Printf("err=%v\n", err)
			return
		}
		fmt.Printf("result=%v\n", string(result))

		// fmt.Printf("header=%#v\n", header)
		// fmt.Printf("data=%v len=%v\n", data[0:c], c)

		conn.Close()
	}
}
示例#6
0
func (this *scriptsystem) init(dir string) {
	this.funcs = make(map[string]lua.LGFunction)
	this.sm = make(map[string]*scriptmodule)
	if err := this.scripts.Init(this.root); err != nil {
		logger.Errorf("[scriptsystem] init failed! err=%v")
	}

	this.pool.Init()
}
示例#7
0
文件: config.go 项目: danuxguin/dxnet
func LoadServerInfo() {
	// server_ip, err := ServerConfig.GetValue("", "server_ip")
	// if err != nil {
	// 	logger.Errorf("Err = %v", err)
	// 	return
	// }
	var err error
	SERVER_IP, err = ServerConfig.GetValue("Server", "IP_0")
	if err != nil {
		logger.Errorf("Err = %v", err)
		return
	}

	SERVER_PORT, err = ServerConfig.Int("Server", "PORT_0")
	if err != nil {
		logger.Errorf("Err = %v", err)
		return
	}
}
示例#8
0
func registerZK() {
	for {
		err := registerZKNetwork()
		if err != nil {
			logger.Errorf("registerZKNetwork failed! err=%v", err)
		}

		time.Sleep(15 * time.Second)
	}
}
示例#9
0
文件: config.go 项目: danuxguin/dxnet
func init() {
	ini := "../../conf/conf.ini"
	var err error
	ServerConfig, err = goconfig.LoadConfigFile(ini)
	if err != nil {
		logger.Errorf("Err = %v", err)
		return
	}

	LoadServerInfo()
}
示例#10
0
func Run(index int) {
	defer logger.CatchException()

	logger.Errorf("index=%v", index)
	InitLog()
	InitConf(index)
	logger.Infof("server start...")

	InitZK()

	RunHttpServer()
}
示例#11
0
func (this *scripttable) OnInit() error {
	L := lua.NewState()
	defer L.Close()

	L.Register("SetScriptFile", this.LuaSetScriptFile)

	if err := this.doFile(L, this.dir, "script/__scripts__.lua"); err != nil {
		logger.Errorf("[scripttable] OnInit() this.doFile failed! err=%#v\n", err)
		return err
	}

	return nil
}
示例#12
0
func Run(index int) {
	defer logger.CatchException()

	logger.Errorf("index=%v", index)
	InitLog()
	InitConf(index)
	logger.Infof("server start...")

	InitZK()

	luascript.Init("LoginServer")

	HandlerPackageFunc()

	tcpaddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", config.SERVER_IP, config.SERVER_PORT))
	if err != nil {
		logger.Errorf("net.ResolveTCPAddr failed! err=%v", err)
		return
	}

	listener, err := net.ListenTCP("tcp4", tcpaddr)
	if err != nil {
		logger.Errorf("net.Listen failed! err=%v", err)
		return
	}

	for {
		conn, err := listener.AcceptTCP()
		logger.Infof("=>%v connecting...", conn.RemoteAddr().String())
		if err != nil {
			logger.Errorf("listener.Accept failed! err=%v", err)
			continue
		}

		go ProcessConnection(conn)
	}

}
示例#13
0
func (this *SQLServer) requestDatabaseList(observer Observer) {
	defer logger.CatchException()

	for {
		sl, _, err := zkm.ChildrenW(zkm.MakeDatabaseGroup(DB_DRIVER_POSTGRES, observer.Group()))
		if err != nil {
			logger.Errorf("zkm.ChildrenW failed! err=%v", err)
			time.Sleep(5 * time.Second)
			continue
		}

		var master []zkm.DBServerInfo
		var slaves []zkm.DBServerInfo
		var node zkm.DBServerInfo

		for _, s := range sl {
			if err := json.Unmarshal([]byte(s), &node); err != nil {
				logger.Errorf("requestDatabaseList failed! parse.err=%v", err)
				continue
			}

			if len(node.Slaveof) == 0 {
				master = append(master, node)
			} else {
				slaves = append(slaves, node)
			}
		}

		this.Lock()
		defer this.Unlock()
		this.master = master
		this.slaves = slaves

		observer.OnUpdateDatabaseList(master, slaves)
		return
	}
}
示例#14
0
func main() {
	//====================================================================
	//---------------------------- Package Info --------------------------
	//====================================================================
	size := uint32(common.MAX_HEADER_LEN + common.COMMON_PACKAGE_LEN + 2 + len(greeting))
	header := new(network.Header)
	header.PacketID = 100
	header.PackageLen = size - 8

	//====================================================================
	//---------------------------- Pack Message---------------------------
	//====================================================================
	message := network.NewMessage(100, size-common.MAX_HEADER_LEN)
	message.Data = make([]byte, size)

	//CommonPackage
	message.Account = account
	message.Signature = signature
	message.Token = token

	//Pack Message
	pos := message.PackHeader()
	pos = message.PackCommonPackage(pos)

	//Pach body
	leng := len(greeting)
	common.WriteUint16(message.Data[pos:pos+2], uint16(leng))
	pos += 2
	common.WriteString(message.Data[pos:pos+leng], greeting)
	pos += common.MAX_GREETING_LEN

	//====================================================================
	//---------------------------- New Session ---------------------------
	//====================================================================
	tcpaddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:4001")
	if err != nil {
		logger.Errorf("[client] net.ResolveTCPAddr failed! err=%v", err)
	}

	for {
		conn, err := net.DialTCP("tcp", nil, tcpaddr)
		if err != nil {
			logger.Errorf("[client] net.DiaTCP failed! err=%v", err)
		}
		defer func() {
			conn.Close()
			logger.Errorf("[client] ok...!")
		}()

		//====================================================================
		//---------------------------- Send ----------------------------------
		//====================================================================
		TCPConn := network.NewSocketBase(conn)
		leng, err = TCPConn.Write(message.Data)
		if err != nil {
			logger.Errorf("[client] conn.Write failed! err=%v", err)
		}

		//====================================================================
		//---------------------------- Recv ----------------------------------
		//====================================================================
		//Parse Message
		msgs, err := TCPConn.RecvMsgs()
		if err != nil {
			logger.Errorf("err=%v", err)
			return
		}

		//handler messages
		for _, msg := range msgs {
			result := common.ReadUint16(msg.Data[0:2])
			logger.Errorf("result=%v\n", result)
		}

		time.Sleep(1 * time.Second)
	}

	return
}