Beispiel #1
0
        static void _bwUDPListener_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                _udpListener = new UdpClient();
                var currentIPAddress = string.Empty;

                _udpListener.ExclusiveAddressUse = false;
                _remoteEndPoint = new IPEndPoint(IPAddress.Any, _listenPort);
                _udpListener.Client.Bind(_remoteEndPoint);

                UdpState state = new UdpState();
                state.RemoteEndPoint = _remoteEndPoint;
                state.UdpListener    = _udpListener;

                _udpListener.BeginReceive(new AsyncCallback(OnUDPReceive), state);
            }
            catch (Exception exc)
            {
                Debug.WriteLine("bwUDPListener_DoWork: " + exc.Message);
            }
            LoggingManager.WriteToLog("Network", "Broadcast", "Listener started");
            Debug.WriteLine(_remoteEndPoint.Address.ToString());
        }
Beispiel #2
0
        static void OnUDPReceive(IAsyncResult ar)
        {
            try
            {
                UdpClient  u = (UdpClient)((UdpState)(ar.AsyncState)).UdpListener;
                IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).RemoteEndPoint;

                Byte[] receiveBytes  = u.EndReceive(ar, ref e);
                string receiveString = Encoding.ASCII.GetString(receiveBytes);

                switch (receiveString)
                {
                case "10;":
                    var         ms      = new System.IO.MemoryStream();
                    AtumPrinter printer = new AtumNULLPrinter();

                    switch (PrinterFirmware.HardwareType)
                    {
                    case "{289EF7C2-3342-4EA3-AF7D-883B26454318}":
                        printer = new Atum.DAL.Hardware.AtumV10TDPrinter();
                        printer.CreateProperties();
                        printer.CreateProjectors();
                        break;

                    case "{967BD0EC-35C1-436A-8D92-145823F17F6E}":
                    case "{57E748B0-EB49-4A59-AEB3-73B2A973D55F}":         //AtumV2
                        printer = new Atum.DAL.Hardware.AtumV15Printer();
                        printer.CreateProperties();
                        printer.CreateProjectors();
                        break;
                    }

                    printer.DetectConnection();

                    _binFormatter.Serialize(ms, printer);

                    if (_remoteSocket == null)
                    {
                        _remoteEndPoint = new IPEndPoint(e.Address, _listenPort);
                    }

                    try
                    {
                        _remoteSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        connectDone   = new ManualResetEvent(false);
                        Debug.WriteLine(string.Format("Connecting to: {0}:{1}", e.Address.ToString(), _listenPort));
                        _remoteSocket.BeginConnect(_remoteEndPoint, new AsyncCallback(ConnectCallback), _remoteSocket);
                        connectDone.WaitOne();
                        _remoteSocket.BeginSend(ms.ToArray(), 0, ms.ToArray().Length, SocketFlags.None, new AsyncCallback(OnTCPSend), _remoteSocket);
                    }
                    catch (Exception exc)
                    {
                        LoggingManager.WriteToLog("OnUDPReceive", "Exception2", exc.Message);
                    }
                    break;
                }

                UdpState s = new UdpState();
                s.RemoteEndPoint = _remoteEndPoint;
                s.UdpListener    = _udpListener;
                u.BeginReceive(OnUDPReceive, s);
            }
            catch (Exception exc)
            {
                LoggingManager.WriteToLog("OnUDPReceive", "Exception", exc.Message);
            }
        }