Created
July 22, 2016 09:51
-
-
Save kn1kn1/63bb43e7867d96db35d6b28ab4a6d1ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bytes" | |
| "encoding/binary" | |
| "errors" | |
| "fmt" | |
| "net" | |
| "os" | |
| "regexp" | |
| "strconv" | |
| ) | |
| const ( | |
| defaultServerIp = "127.0.0.1" | |
| defaultServerPort = "4558" | |
| defaultBufferNo = "one" | |
| defaultCode = `# replaced by "replace-client-buffer-code.go" through "/replace-buffer" command | |
| live_loop :test do | |
| play 60 | |
| sleep 1 | |
| end` | |
| ) | |
| var ( | |
| // BuildVersion sets version string | |
| BuildVersion string | |
| // GitCommit sets commit hash of git | |
| GitCommit string | |
| // BuildDate sets date of built datetime | |
| BuildDate string | |
| senddata []byte | |
| oscarg []byte | |
| initdata bool = false | |
| ) | |
| func send(serverIP, serverPort string) { | |
| if !initdata { | |
| return | |
| } | |
| sndToAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1"+":"+serverPort) | |
| checkError(err) | |
| conn, err := net.DialUDP("udp", nil, sndToAddr) | |
| checkError(err) | |
| defer conn.Close() | |
| conn.Write(senddata) | |
| } | |
| func checkError(err error) { | |
| if err != nil { | |
| fmt.Fprintf(os.Stderr, "fatal: error: %s", err.Error()) | |
| os.Exit(1) | |
| } | |
| } | |
| func pushOscArgs(arr []string) error { | |
| for i := 0; i < len(arr); i++ { | |
| if match(`^[+-]?[0-9]+$`, arr[i]) { | |
| // Int32 | |
| num_i64, err := strconv.ParseInt(arr[i], 10, 32) | |
| num_i32 := int32(num_i64) | |
| if err != nil { | |
| return errors.New("osc args error") | |
| } | |
| pushDataI32(num_i32) | |
| } else if match(`^[+-]?[0-9.]+$`, arr[i]) { | |
| // Float32 | |
| num_f64, err := strconv.ParseFloat(arr[i], 32) | |
| num_f32 := float32(num_f64) | |
| if err != nil { | |
| return errors.New("osc args error") | |
| } | |
| pushDataF32(num_f32) | |
| } else { | |
| // String | |
| pushDataString(arr[i]) | |
| } | |
| } | |
| senddata = append(senddata, 0) | |
| fill4byte() | |
| senddata = append(senddata, oscarg...) | |
| initdata = true | |
| return nil | |
| } | |
| func match(reg, str string) bool { | |
| return regexp.MustCompile(reg).Match([]byte(str)) | |
| } | |
| func pushOscAddress(str string) { | |
| senddata = append(senddata, []byte(str)...) | |
| senddata = append(senddata, 0) | |
| fill4byte() | |
| senddata = append(senddata, 0x2c) | |
| } | |
| func fill4byte() { | |
| for datalen := len(senddata); datalen%4 != 0; datalen++ { | |
| senddata = append(senddata, 0) | |
| } | |
| } | |
| func pushDataI32(num int32) { | |
| senddata = append(senddata, 'i') | |
| buf := bytes.NewBuffer([]byte{}) | |
| binary.Write(buf, binary.BigEndian, num) | |
| oscarg = append(oscarg, buf.Bytes()...) | |
| } | |
| func pushDataF32(num float32) { | |
| senddata = append(senddata, 'f') | |
| buf := bytes.NewBuffer([]byte{}) | |
| binary.Write(buf, binary.BigEndian, num) | |
| oscarg = append(oscarg, buf.Bytes()...) | |
| } | |
| func pushDataString(str string) { | |
| senddata = append(senddata, 's') | |
| buf := bytes.NewBuffer([]byte(str)) | |
| oscarg = append(oscarg, buf.Bytes()...) | |
| oscarg = append(oscarg, 0) | |
| for datalen := len(oscarg); datalen%4 != 0; datalen++ { | |
| oscarg = append(oscarg, 0) | |
| } | |
| } | |
| func main() { | |
| // $ go run replace-client-buffer-code.go | |
| // $ go run replace-client-buffer-code.go "play 60" "one" | |
| // $ go run replace-client-buffer-code.go "play 60" "one" 4558 | |
| fmt.Println("version: " + BuildVersion + ", build: " + GitCommit + ", date:" + BuildDate) | |
| argslen := len(os.Args) | |
| if argslen < 1 { | |
| fmt.Fprintf(os.Stderr, "usage: replace-client-buffer-code [code] [buffer-no] [port]\n") | |
| os.Exit(1) | |
| return | |
| } | |
| code := defaultCode | |
| if argslen > 1 && len(os.Args[1]) != 0 { | |
| code = os.Args[1] | |
| } | |
| bufferNo := defaultBufferNo | |
| if argslen > 2 && len(os.Args[2]) != 0 { | |
| bufferNo = os.Args[2] | |
| } | |
| bufferId := "workspace_" + bufferNo | |
| serverPort := defaultServerPort | |
| if argslen > 3 && len(os.Args[3]) != 0 { | |
| serverPort = os.Args[3] | |
| } | |
| senddata = []byte{} | |
| oscarg = []byte{} | |
| pushOscAddress("/replace-buffer") | |
| arr := []string{ | |
| bufferId, | |
| code, | |
| "0", | |
| "0", | |
| "0"} | |
| pushOscArgs(arr) | |
| for i := 0; i < len(senddata); i++ { | |
| fmt.Printf("%02x ", senddata[i]) | |
| if i%16 == 15 { | |
| fmt.Printf("\n") | |
| } | |
| } | |
| serverIP := defaultServerIp | |
| send(serverIP, serverPort) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment