2525## invalidate any other reasons why the executable file might be covered by
2626## the GNU General Public License.
2727
28- from socket import AF_INET , SOCK_DGRAM , SOL_SOCKET , SO_REUSEADDR # SO_ERROR
28+ from socket import AF_INET , SOCK_DGRAM , SOL_SOCKET , SO_REUSEADDR , SO_BROADCAST # SO_ERROR
2929from socket import gethostname
3030from select import select
3131import utils , socket
@@ -126,6 +126,12 @@ def send_end(self):
126126 self .curr_txmeta = None
127127 return True
128128
129+ def set_broadcast (self , v ):
130+ if v :
131+ self .sock .setsockopt (SOL_SOCKET , SO_BROADCAST , 1 )
132+ else :
133+ self .sock .setsockopt (SOL_SOCKET , SO_BROADCAST , 0 )
134+
129135 def close (self ):
130136 self .sock .close ()
131137 self .opened = False
@@ -187,11 +193,25 @@ def run(self, data):
187193 if sock is None :
188194 return chr (0 )
189195 port = (ord (data [1 ]) << 8 ) + ord (data [2 ])
196+ sock .set_broadcast (False )
190197 if sock .send_start (data [3 :], port ) is None :
191198 return chr (0 )
192199 else :
193200 return chr (1 )
194201
202+ class WRITE_BEGIN_BROADCAST_Command :
203+ def run (self , data ):
204+ id = ord (data [0 ])
205+ sock = udp_sockets .get (id )
206+ if sock is None :
207+ return chr (0 )
208+ port = (ord (data [1 ]) << 8 ) + ord (data [2 ])
209+ sock .set_broadcast (True )
210+ if sock .send_start ('<broadcast>' , port ) is None :
211+ return chr (0 )
212+ else :
213+ return chr (1 )
214+
195215class WRITE_Command :
196216 def run (self , data ):
197217 id = ord (data [0 ])
@@ -271,6 +291,7 @@ def run(self, data):
271291
272292def init (command_processor ):
273293 command_processor .register ('e' , CREATE_Command ())
294+ command_processor .register ('v' , WRITE_BEGIN_BROADCAST_Command ())
274295 command_processor .register ('E' , WRITE_BEGIN_Command ())
275296 command_processor .register ('h' , WRITE_Command ())
276297 command_processor .register ('H' , WRITE_END_Command ())
@@ -285,6 +306,11 @@ def test():
285306 from time import sleep
286307 import struct
287308 udp = UDPSocket ('' , 5555 )
309+ udp .set_broadcast (True )
310+ udp .send_start ('<broadcast>' , 5555 )
311+ udp .send ('hi' )
312+ udp .send_end ()
313+ udp .set_broadcast (False )
288314 while True :
289315 udp .run ()
290316 sleep (0.1 )
0 commit comments