Commits

Anonymous committed 70771b4

add bash zmq_push

Comments (0)

Files changed (1)

+mktemp() {(
+	set -C
+	td=${TMPDIR:-/tmp}
+	[[ -d $td && -w $td && -r $td ]] || return 1
+	until { f=$td/$(tr -dc '[:alnum:]' < /dev/urandom | dd bs=8 count=1); :>"$f"; };
+	do :; done 2>/dev/null
+	printf "%s" "$f"
+);}
+
+zmq_push() {
+	local fd msg len pre="\x1\x0\xff\x0\x0\x0\x0"
+
+	(($# == 2)) && [[ $2 != *[![:digit:]]* ]] && (($2 > 0 && $2 < 65536)) &&
+	exec {fd}>"/dev/tcp/$1/$2" || return 1
+
+	msg=$(mktemp) || return 1
+	cat > "$msg"
+	len=$(wc -c "$msg")
+
+	for ((len = ${len% *} + 1, mask = 0xff000000; mask; mask >>= 8)); do
+		pre+="\x$(printf %x $((len & mask)))"
+	done
+	pre+="\x0"
+	{ printf %b "$pre"; cat "$msg"; } >&$fd
+	{fd}>&-
+	rm "$msg"
+}