This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
/
socket.bmx
134 lines (118 loc) · 3.74 KB
/
socket.bmx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
Rem
Copyright (c) 2012 Christiaan Kras
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
End Rem
Rem
bbdoc: Type that represents a ZMQ socket
End Rem
Type TZMQ_Socket
Rem
bbdoc: Pointer to a ZMQ context
End Rem
Field context:Byte Ptr
Rem
bbdoc: Pointer to a ZMQ socket
about: Use @Close() to free socket
End Rem
Field socket:Byte Ptr
Rem
bbdoc: Create a ZMQ Socket
returns: TZMQ_Socket
about: Throws a TZMQ_Exception on errors
End Rem
Method Create:TZMQ_Socket(context:Byte Ptr, socket_type:Int)
If socket Then Throw "Socket already exists!"
Self.context = context
Self.socket = zmq_socket(Self.context, socket_type)
If Self.socket = Null Then Throw New TZMQ_Exception
Return Self
End Method
Rem
bbdoc: Destructor
about: Will call @Close() when collected
End Rem
Method Destroy()
Self.Close()
End Method
Rem
bbdoc: Bind to an address
about: Throws a TZMQ_Exception on errors
End Rem
Method Bind(address:String)
Local rc:Int = zmq_bind(Self.socket, address)
If rc = -1 Then Throw New TZMQ_Exception
End Method
Rem
bbdoc: Connect to a ZMQ socket
about: Throws a TZMQ_Exception on errors
End Rem
Method Connect(address:String)
Local rc:Int = zmq_connect(Self.socket, address)
If rc = -1 Then Throw New TZMQ_Exception
End Method
Rem
bbdoc: Close socket
about: Throws a TZMQ_Exception on errors
End Rem
Method Close()
Local rc:Int = zmq_close(Self.socket)
If rc = -1 Then Throw New TZMQ_Exception
End Method
Rem
bbdoc: Set a socket option
about: Throws a TZMQ_Exception on errors
End Rem
Method SetSockOpt(socket_option:Int, value:String)
Local rc:Int = zmq_setsockopt(Self.socket, socket_option, value, value.Length)
If rc = -1 Then Throw New TZMQ_Exception
End Method
Rem
bbdoc: Get a socket options
about: Not yet implemented!
End Rem
Method GetSockOpt(socket_option:Int)
Throw "Not yet implemented"
End Method
Rem
bbdoc: Send message to socket
about: Throws a TZMQ_Exception on errors
message can either be a String, which will be converted to a TZMQ_Message,
or you can pass a TZMQ_Message straight away
End Rem
Method Send(message:Object, flags:Int = 0)
Local rc:Int
If TZMQ_Message(message)
rc = zmq_send(Self.socket, TZMQ_Message(message).message, flags)
Else If String(message)
Local content:String = String(message)
Local msg:TZMQ_Message = New TZMQ_Message.Create(content)
rc = Self.Send(msg, flags)
msg.Close()
End If
If rc = -1 Then Throw New TZMQ_Exception
End Method
Rem
bbdoc: Receive message from socket
returns: TZMQ_Message
End Rem
Method Recv:TZMQ_Message(flags:Int = 0)
Local msg:Byte Ptr = bmx_zmq_message_t()
zmq_msg_init(msg)
If zmq_recv(Self.socket, msg, flags) Then Return Null
Return New TZMQ_Message.CreateFromMessage(msg)
End Method
End Type