Commit daeb8d3c8f0090d5459eb86f51bc822a1bce59bf

Authored by David René
1 parent d216cf26

add udp sender for the muscle message

Showing 1 changed file with 59 additions and 1 deletions   Show diff stats
anubis_dev/library/system/muscle.anubis
... ... @@ -24,13 +24,19 @@ public define Int32 _B_STRING_TYPE = 1129534546. // 'CSTR',
24 24 public define Int32 _B_OBJECT_TYPE = 1330664530. // 'OPTR', // used for flattened objects
25 25 public define Int32 _B_RAW_TYPE = 1380013908. // 'RAWT', // used for raw byte arrays
26 26  
27   -public define Int32 _CURRENT_PROTOCOL_VERSION = 1347235888. // 'PM00' -- our magic number 0x504D3030
  27 +public define Int32 _CURRENT_PROTOCOL_VERSION = 1347235888. // 'PM00' -- our magic number 0x504D3030
  28 +
  29 +public define Int32 _MUSCLE_MESSAGE_ENCODING_DEFAULT = 1164862256. //header for network transfert
28 30  
29 31 public type Message:...
30 32  
31 33 public define Maybe(Message) unflatten_message ( Data_IO io ).
32 34 public define Maybe(ByteArray) flatten_message ( Message msg ).
33 35  
  36 +public type Message_Send_Result:
  37 + flatten_error,
  38 + network_error,
  39 + ok.
34 40  
35 41  
36 42 type Muscle_object:
... ... @@ -1514,6 +1520,58 @@ public define Maybe(ByteArray)
1514 1520 String name
1515 1521 )= find_raw(msg, name, 0).
1516 1522  
  1523 +/********************** I/O socket *****************************/
  1524 +
  1525 + /**
  1526 + * create the right udp socket. deterline if we must create broadcast
  1527 + * or not udp socket
  1528 + */
  1529 +
  1530 +define Create_UDP_Client_Socket_Result
  1531 + create_udp_socket
  1532 + (
  1533 + Int32 ip,
  1534 + )=
  1535 + if ip = ip_address((255,255,255,255)) then
  1536 + create_udp_client_broadcast_socket
  1537 + else
  1538 + create_udp_client_socket
  1539 + .
  1540 +
  1541 +
  1542 +public define Message_Send_Result
  1543 + send_message_by_udp
  1544 + (
  1545 + Message msg,
  1546 + Int32 ip_address, // where you want to send the data
  1547 + Int32 ip_port,
  1548 + )=
  1549 + if flatten_message(msg) is
  1550 + {
  1551 + failure then print("Error flattening identity Message"); flatten_error,
  1552 + success(buffer) then
  1553 + if create_udp_socket(ip_address) is
  1554 + {
  1555 + cannot_create_the_socket then network_error,
  1556 + ok(socket) then
  1557 + //the necessary UDP socket was created, hence we send the Message packet
  1558 + with header = host_to_lendian_Int32_ByteArray(length(buffer)) +
  1559 + host_to_lendian_Int32_ByteArray(_MUSCLE_MESSAGE_ENCODING_DEFAULT),
  1560 + //send the header. that header is construct as follow (size of the following message + header mark)
  1561 + if udp_send(socket, ip_address, ip_port, header) is
  1562 + {
  1563 + network_unreachable then network_error,
  1564 + packet_sent then
  1565 + //now we send the real message
  1566 + if udp_send(socket, ip_address, ip_port, buffer) is
  1567 + {
  1568 + network_unreachable then network_error,
  1569 + packet_sent then ok
  1570 + }
  1571 + }
  1572 + }
  1573 + }.
  1574 +
1517 1575  
1518 1576 /********************** TESTING SERVER *************************/
1519 1577  
... ...