Skip to content

Instantly share code, notes, and snippets.

@gallir
Created January 24, 2016 12:22
Show Gist options
  • Save gallir/05be611db38c6beec310 to your computer and use it in GitHub Desktop.
Save gallir/05be611db38c6beec310 to your computer and use it in GitHub Desktop.
zmq_sendmessage.go
func (soc *Socket) sendMessage(dontwait Flag, parts ...interface{}) (total int, err error) {
// TODO: make this faster
// Done, ;)
var partial int
// Pop the last empty slices
popempty:
for i := len(parts) - 1; i >= 0; i-- {
switch tLast := parts[i].(type) {
case []string:
if len(tLast) > 0 {
break popempty // A non empty slice, stop
}
case [][]byte:
if len(tLast) > 0 {
break popempty // A non empty slice, stop
}
default:
break popempty // Empty strings must be sent, stop
}
parts = parts[:len(parts)-1]
}
opt := SNDMORE | dontwait
last0 := len(parts) - 1 // The last index, just to shorten the comparison
for i0, p0 := range parts {
switch t0 := p0.(type) {
case []string:
last1 := len(t0) - 1
for i1, p1 := range t0 {
if i0 == last0 && i1 == last1 {
opt = dontwait
}
partial, err = soc.sendSinglePart(opt, p1)
if err != nil {
break // Don't continue
}
total += partial
}
if err != nil {
return -1, err
}
case [][]byte:
last1 := len(t0) - 1
for i1, p1 := range t0 {
if i0 >= last0 && i1 >= last1 {
opt = dontwait
}
partial, err = soc.sendSinglePart(opt, p1)
if err != nil {
break // Don't continue
}
total += partial
}
if err != nil {
return -1, err
}
default:
if i0 == last0 {
opt = dontwait
}
switch t := p0.(type) {
case string:
partial, err = soc.sendSinglePart(opt, t)
case []byte:
partial, err = soc.sendSinglePart(opt, t)
default:
partial, err = soc.sendSinglePart(opt, fmt.Sprintf("%v", t))
}
if err != nil {
return -1, err
}
total += partial
}
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment