func main() { str0 := "monkey" C.CreateDB((*C.char)(convert.String2C(str0))) //创建基础数据库 str0 = "config" //配置数据库 C.CreateDB((*C.char)(convert.String2C(str0))) key := "passwd" //初始密码 data := "monkey" db := C.SwitchDB((*C.char)(convert.String2C(str0))) C.Set(&(*db).tIndex, (*C.char)(convert.String2C(key)), (convert.String2C(data))) servicePort := ":1517" tcpAddr, err := net.ResolveTCPAddr("tcp4", servicePort) if err != nil { panic(err) } l, err := net.ListenTCP("tcp", tcpAddr) //侦听TCP if err != nil { panic(err) } for { conn, err := l.AcceptTCP() conn.SetKeepAlive(true) conn.SetNoDelay(true) if err != nil { panic(err) } go Handler(conn) } }
func initDB() { //初始化数据库 str0 := "monkey" C.CreateDB((*C.char)(convert.String2C(str0))) //创建基础数据库 str0 = "config" //配置数据库 C.CreateDB((*C.char)(convert.String2C(str0))) key := "passwd" //初始密码 data := "monkey" db := C.SwitchDB((*C.char)(convert.String2C(str0))) C.Set(&(db.tIndex), (*C.char)(convert.String2C(key)), (convert.String2C(data))) }
func (cmd command) Set(db **C.Database) []byte { response := []byte{} key, next := convert.ParseUntil(cmd, ' ', 4) value, _ := convert.ParseUntil(cmd, 0, next+1) //fmt.Println("set ",key,value) r := C.Set(&(*db).tIndex, (*C.char)(convert.Bytes2C(key)), (convert.Bytes2C(value))) for i := 0; ; i++ { response = append(response, byte(r.msg[i])) if response[i] == 0 { break } } return response }
func (cmd command) Set(db **C.Database) []byte { //锁并发 _, ok := mutex[**db] if !ok { mutex[**db] = new(sync.RWMutex) } mutex[**db].Lock() defer mutex[**db].Unlock() response := []byte{} key, next := convert.ParseUntil(cmd, ' ', 4) value, _ := convert.ParseUntil(cmd, 0, next+1) //fmt.Println("set ",key,value) r := C.Set(&(*db).tIndex, (*C.char)(convert.Bytes2C(key)), (convert.Bytes2C(value))) for i := 0; ; i++ { response = append(response, byte(r.msg[i])) if response[i] == 0 { break } } return response }
////////////////////////////////////////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) }