Пример #1
0
func auth(s *tcp.TCPSession) bool {
	buff := s.ReadMessage()
	params := strings.Split(string(buff), " ")
	str0 := "config"
	db := C.SwitchDB((*C.char)(convert.String2C(str0)))
	if params[0] != "auth" {
		s.SendMessage([]byte("Please auth first!"))
		return false
	}
	r := C.Get(&(db.tIndex), (*C.char)(convert.String2C("passwd")))
	if int(r.code) == 0 {
		passwd := []byte{}
		for i := 0; ; i++ {
			passwd = append(passwd, byte(*(*C.char)(unsafe.Pointer((uintptr(r.pData) + uintptr(i))))))
			if passwd[i] == 0 {
				break
			}
		}
		if convert.Equal(passwd, []byte(params[1])) {
			s.SendMessage([]byte("Auth success"))
			return true
		} else {
			s.SendMessage([]byte("Auth fail"))
			return false
		}
	} else {
		s.SendMessage([]byte("Auth success"))
		return true
	}
}
Пример #2
0
func (cmd command) Get(db **C.Database) []byte {
	response := []byte{}
	key, _ := convert.ParseUntil(cmd, 0, 4)
	r := C.Get(&(*db).tIndex, (*C.char)(convert.Bytes2C(key)))
	if int(r.code) == 0 {
		for i := 0; ; i++ {
			response = append(response, byte(*(*C.char)(unsafe.Pointer((uintptr(r.pData) + uintptr(i))))))
			if response[i] == 0 {
				break
			}
		}
	} else {
	}
	return response
}
Пример #3
0
func Handler(conn *net.TCPConn) {
	str := "config"
	db := C.SwitchDB((*C.char)(convert.String2C(str))) //环境变量-当前数据库
	for {
		buff := tcp.Receive(conn) //可能发生无限循环->已经ok,本机go编译器有问题
		params := strings.Split(string(buff), " ")
		if params[0] != "auth" {
			tcp.Send(conn, []byte("Please auth first!"))
			continue
		}
		r := C.Get(&(*db).tIndex, (*C.char)(convert.String2C("passwd")))
		if int(r.code) == 0 {
			passwd := []byte{}
			for i := 0; ; i++ {
				passwd = append(passwd, byte(*(*C.char)(unsafe.Pointer((uintptr(r.pData) + uintptr(i))))))
				if passwd[i] == 0 {
					break
				}
			}
			if convert.Equal(passwd, []byte(params[1])) {
				tcp.Send(conn, []byte("Auth success"))
				break
			} else {
				tcp.Send(conn, []byte("Auth fail"))
				continue
			}
		} else {
			break
		}
	}
	str = "monkey"
	db = C.SwitchDB((*C.char)(convert.String2C(str))) //环境变量-当前数据库
	for {
		buff := tcp.Receive(conn)
		// if err != nil {
		// 	conn.Close()
		// 	break
		// }
		if len(buff) == 0 {
			return
		}
		TranslateMessage(conn, &db, buff) //解析消息
	}

}
Пример #4
0
func (cmd command) Get(db **C.Database) []byte {
	//锁并发
	_, ok := mutex[**db]
	if !ok {
		mutex[**db] = new(sync.RWMutex)
	}
	mutex[**db].RLock()
	defer mutex[**db].RUnlock()

	response := []byte{}
	key, _ := convert.ParseUntil(cmd, 0, 4)
	r := C.Get(&(*db).tIndex, (*C.char)(convert.Bytes2C(key)))
	if int(r.code) == 0 {
		for i := 0; ; i++ {
			response = append(response, byte(*(*C.char)(unsafe.Pointer((uintptr(r.pData) + uintptr(i))))))
			if response[i] == 0 {
				break
			}
		}
	} else {
	}
	return response
}
Пример #5
0
////////////////////////////////////////Dumplated//////////////////////////////////////////////////////////////////////////
func TranslateMessage(s *tcp.TCPSession, db **C.Database, message []byte) {
	command := string(message)
	params := strings.Split(command, " ")
	//fmt.Println(params)
	response := []byte{}
	if params[0] == "set" {
		r := C.Set(&(*db).tIndex, (*C.char)(convert.String2C(params[1])), (convert.String2C(params[2])))
		for i := 0; ; i++ {
			response = append(response, byte(r.msg[i]))
			if response[i] == 0 {
				break
			}
		}
	} else if params[0] == "get" {
		r := C.Get(&(*db).tIndex, (*C.char)(convert.String2C(params[1])))
		// for i := 0;;i++ {
		// 	response = append(response,byte(r.msg[i]))
		// 	if response[i] == 0 { break; }
		// }
		if int(r.code) == 0 {
			for i := 0; ; i++ {
				response = append(response, byte(*(*C.char)(unsafe.Pointer((uintptr(r.pData) + uintptr(i))))))
				if response[i] == 0 {
					break
				}
			}
		} else {
			// for i := 0;;i++ {
			// response = append(response,byte(r.msg[i]))
			// if response[i] == 0 { break; }
			// }
		}

	} else if params[0] == "delete" || params[0] == "remove" {
		r := C.Delete(&(*db).tIndex, (*C.char)(convert.String2C(params[1])))
		for i := 0; ; i++ {
			response = append(response, byte(r.msg[i]))
			if response[i] == 0 {
				break
			}
		}

	} else if params[0] == "createdb" {
		d := C.CreateDB((*C.char)(convert.String2C(params[1])))
		if d != nil {
			*db = d
			response = []byte("Already exist,switched\n")
		} else {
			response = []byte("Created\n")
		}
	} else if params[0] == "switchdb" {
		d := C.SwitchDB((*C.char)(convert.String2C(params[1])))
		if d != nil {
			*db = d
			response = []byte("ok\n")
		} else {
			response = []byte("fail\n")
		}
	} else if params[0] == "dropdb" {
		*db = C.DropDB((*C.char)(convert.String2C(params[1])))
	} else if strings.EqualFold("listdb", params[0]) {
		r := C.ListDB()
		for i := 0; i < 1024; i++ {
			b := byte(*(*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(r)) + uintptr(i))))
			response = append(response, b)
			if b == 0 {
				break
			}
		}
		C.free(unsafe.Pointer(r))
	} else {
		//fmt.Println("unkown command:",params[0])
	}
	s.SendMessage(response)
}