Beispiel #1
0
        /// <summary>
        /// Sends the specified packet using UDP broadcast to all TCNet devices.
        /// </summary>
        /// <param name="packet">The packet to send on the network.</param>
        protected void Broadcast(Socket socket, TCNetPacket packet)
        {
            MemoryStream      data          = new MemoryStream();
            TCNetBinaryWriter writer        = new TCNetBinaryWriter(data);
            IPEndPoint        localEndpoint = (IPEndPoint)socket.LocalEndPoint;

            TCNetHeader header = packet as TCNetHeader;

            if (header != null)
            {
                header.NodeID         = NodeId;
                header.NodeType       = Role;
                header.NodeName       = NodeName;
                header.SequenceNumber = NextSequenceNumber();
                header.Timestamp      = DateTime.UtcNow.TimeOfDay;
            }

            packet.WriteData(writer);
            try
            {
                socket.SendTo(data.ToArray(), new IPEndPoint(BroadcastAddress, localEndpoint.Port));
            }
            catch (SocketException ex)
            {
                var broadcastError = new TCNetSocketException(this, "TCNet socket error while broadcasting. See inner exception for more details.", ex);
                broadcastError.Data["LocalPort"] = localEndpoint.Port;
                broadcastError.Data["Port"]      = localEndpoint.Port;
                OnUnhandledException(broadcastError);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Sends the specified packet using unicast to a specific DJ Tap device.
        /// </summary>
        /// <param name="packet">The packet to send on the network.</param>
        /// <param name="address">The address of the device to send the packet to.</param>
        protected void Unicast(TCNetPacket packet, TCNetEndPoint address)
        {
            MemoryStream      data          = new MemoryStream();
            TCNetBinaryWriter writer        = new TCNetBinaryWriter(data);
            IPEndPoint        localEndpoint = (IPEndPoint)nodeSocket.LocalEndPoint;

            TCNetHeader header = packet as TCNetHeader;

            if (header != null)
            {
                header.NodeID         = NodeId;
                header.NodeType       = Role;
                header.NodeName       = NodeName;
                header.SequenceNumber = NextSequenceNumber();
                header.Timestamp      = DateTime.UtcNow.TimeOfDay;

                TraceEventType traceLevel = TraceEventType.Information;
                DataTypes      dataType   = DataTypes.None;

                TCNetDataHeader dataHeader = header as TCNetDataHeader;
                if (dataHeader != null)
                {
                    dataType = dataHeader.DataType;
                }

                tcNetTrace.TraceEvent(traceLevel, (int)header.MessageType, $"{header.NodeName}: {header.GetType().Name} {(dataType != DataTypes.None ? dataType.ToString() : string.Empty)}");
            }

            packet.WriteData(writer);
            try
            {
                unicastTXSocket.SendTo(data.ToArray(), new IPEndPoint(address.Address, address.Port));
            }
            catch (SocketException ex)
            {
                OnUnhandledException(new TCNetSocketException(this, "Socket error unicast TCNet integration.", ex));
            }
        }