-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
648 additions
and
160 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
## Features | ||
|
||
- UDP (default) or TCP | ||
- OSC Bundles, including timetags | ||
- OSC Messages | ||
- OSC Client | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,114 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"net" | ||
"math/rand" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hypebeast/go-osc/osc" | ||
) | ||
|
||
func main() { | ||
addr := "127.0.0.1:8765" | ||
server := &osc.Server{} | ||
conn, err := net.ListenPacket("udp", addr) | ||
if err != nil { | ||
fmt.Println("Couldn't listen: ", err) | ||
} | ||
defer conn.Close() | ||
func indent(str string, indentLevel int) string { | ||
indentation := strings.Repeat(" ", indentLevel) | ||
|
||
fmt.Println("### Welcome to go-osc receiver demo") | ||
fmt.Println("Press \"q\" to exit") | ||
|
||
go func() { | ||
fmt.Println("Start listening on", addr) | ||
|
||
for { | ||
packet, err := server.ReceivePacket(conn) | ||
if err != nil { | ||
fmt.Println("Server error: " + err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
if packet != nil { | ||
switch packet.(type) { | ||
default: | ||
fmt.Println("Unknow packet type!") | ||
|
||
case *osc.Message: | ||
fmt.Printf("-- OSC Message: ") | ||
osc.PrintMessage(packet.(*osc.Message)) | ||
|
||
case *osc.Bundle: | ||
fmt.Println("-- OSC Bundle:") | ||
bundle := packet.(*osc.Bundle) | ||
for i, message := range bundle.Messages { | ||
fmt.Printf(" -- OSC Message #%d: ", i+1) | ||
osc.PrintMessage(message) | ||
} | ||
} | ||
} | ||
result := "" | ||
|
||
for i, line := range strings.Split(str, "\n") { | ||
if i != 0 { | ||
result += "\n" | ||
} | ||
}() | ||
|
||
reader := bufio.NewReader(os.Stdin) | ||
result += indentation + line | ||
} | ||
|
||
return result | ||
} | ||
|
||
func debug(packet osc.Packet, indentLevel int) string { | ||
switch packet.(type) { | ||
default: | ||
return "Unknown packet type!" | ||
|
||
for { | ||
c, err := reader.ReadByte() | ||
if err != nil { | ||
os.Exit(0) | ||
case *osc.Message: | ||
return fmt.Sprintf("-- OSC Message: %s", packet.(*osc.Message)) | ||
|
||
case *osc.Bundle: | ||
bundle := packet.(*osc.Bundle) | ||
|
||
result := fmt.Sprintf("-- OSC Bundle (%s):", bundle.Timetag.Time()) | ||
|
||
for i, message := range bundle.Messages { | ||
result += "\n" + indent( | ||
fmt.Sprintf("-- OSC Message #%d: %s", i+1, message), | ||
indentLevel+1, | ||
) | ||
} | ||
|
||
if c == 'q' { | ||
os.Exit(0) | ||
for _, bundle := range bundle.Bundles { | ||
result += "\n" + indent(debug(bundle, 0), indentLevel+1) | ||
} | ||
|
||
return result | ||
} | ||
} | ||
|
||
// Debugger is a simple Dispatcher that prints all messages and bundles as they | ||
// are received. | ||
type Debugger struct{} | ||
|
||
// Dispatch implements Dispatcher.Dispatch by printing the packet received. | ||
func (Debugger) Dispatch(packet osc.Packet) { | ||
if packet != nil { | ||
fmt.Println(debug(packet, 0) + "\n") | ||
} | ||
} | ||
|
||
func printUsage() { | ||
fmt.Printf("Usage: %s PROTOCOL PORT\n", os.Args[0]) | ||
} | ||
|
||
func main() { | ||
rand.Seed(time.Now().Unix()) | ||
|
||
numArgs := len(os.Args[1:]) | ||
|
||
if numArgs != 2 { | ||
printUsage() | ||
os.Exit(1) | ||
} | ||
|
||
var protocol osc.NetworkProtocol | ||
switch strings.ToLower(os.Args[1]) { | ||
case "udp": | ||
protocol = osc.UDP | ||
case "tcp": | ||
protocol = osc.TCP | ||
default: | ||
fmt.Println("Invalid protocol: " + os.Args[1]) | ||
printUsage() | ||
os.Exit(1) | ||
} | ||
|
||
port, err := strconv.ParseInt(os.Args[2], 10, 32) | ||
if err != nil { | ||
fmt.Println(err) | ||
printUsage() | ||
os.Exit(1) | ||
} | ||
|
||
addr := fmt.Sprintf("127.0.0.1:%d", port) | ||
|
||
server := osc.NewServer(addr, Debugger{}, 0) | ||
server.SetNetworkProtocol(protocol) | ||
|
||
fmt.Println("### Welcome to go-osc receiver demo") | ||
fmt.Printf("Listening via %s on port %d...\n", protocol, port) | ||
|
||
if err := server.ListenAndServe(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,67 @@ | ||
package main | ||
|
||
import "github.com/hypebeast/go-osc/osc" | ||
import ( | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hypebeast/go-osc/osc" | ||
) | ||
|
||
func printUsage() { | ||
fmt.Printf("Usage: %s PROTOCOL PORT\n", os.Args[0]) | ||
} | ||
|
||
func main() { | ||
addr := "127.0.0.1:8765" | ||
rand.Seed(time.Now().Unix()) | ||
|
||
numArgs := len(os.Args[1:]) | ||
|
||
if numArgs != 2 { | ||
printUsage() | ||
os.Exit(1) | ||
} | ||
|
||
var protocol osc.NetworkProtocol | ||
switch strings.ToLower(os.Args[1]) { | ||
case "udp": | ||
protocol = osc.UDP | ||
case "tcp": | ||
protocol = osc.TCP | ||
default: | ||
fmt.Println("Invalid protocol: " + os.Args[1]) | ||
printUsage() | ||
os.Exit(1) | ||
} | ||
|
||
port, err := strconv.ParseInt(os.Args[2], 10, 32) | ||
if err != nil { | ||
fmt.Println(err) | ||
printUsage() | ||
os.Exit(1) | ||
} | ||
|
||
addr := fmt.Sprintf("127.0.0.1:%d", port) | ||
|
||
d := osc.NewStandardDispatcher() | ||
d.AddMsgHandler("/message/address", func(msg *osc.Message) { | ||
|
||
if err := d.AddMsgHandler("/message/address", func(msg *osc.Message) { | ||
osc.PrintMessage(msg) | ||
}) | ||
server := &osc.Server{ | ||
Addr: addr, | ||
Dispatcher: d, | ||
}); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
server.ListenAndServe() | ||
server := osc.NewServer(addr, d, 0) | ||
server.SetNetworkProtocol(protocol) | ||
|
||
fmt.Printf("Listening via %s on port %d...\n", protocol, port) | ||
|
||
if err := server.ListenAndServe(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
Oops, something went wrong.