public Scanner(ILogger logger) { _logger = logger; _waitEvents = new HybridDictionary(); _recevedEvents = new HybridDictionary(); _events = new Queue(); _mre = new ManualResetEvent(false); _validPageLengths = new ValidPageLength[MAX_SHEET_FORMATS]; _validPageOffsets = new ValidPageOffset[MAX_SHEET_FORMATS]; _sheetScanning = false; _scannerBusy = false; foreach (HardwareConfiguration t in s_ports) { _currentConfiguration = t; Socket udpSocketSend = null; Socket udpSocketReceive = null; Socket tcpSocket = null; try { udpSocketSend = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Unspecified); udpSocketReceive = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Unspecified); udpSocketReceive.Bind(new IPEndPoint(System.Net.IPAddress.Any, _currentConfiguration.UdpPortReceive)); byte[] data = { 1, 0, 0, 0 }; var um = new UserMessage(Command.umConnect, data); EndPoint ep = new IPEndPoint(LOCAL_HOST, _currentConfiguration.UdpPortSend); udpSocketSend.SendTo(um.Buffer, ep); if (!udpSocketReceive.Poll(TIMEOUT, SelectMode.SelectRead)) { continue; } data = new byte[1024]; udpSocketReceive.ReceiveFrom(data, ref ep); um = new UserMessage(data); if (um.Command != Command.umConnect) { throw new Exception("ScannerWantNotConnect"); } var cc = new UmConnectConfirmation(um.Data); if (cc.Answer != 1) { throw new Exception("ScannerWantNotConnect"); } _status = cc.Status; tcpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Unspecified); tcpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); ep = new IPEndPoint(System.Net.IPAddress.Any, _currentConfiguration.TcpPport); tcpSocket.Bind(ep); tcpSocket.Listen(1); ScannerSocket.SetBuffers(tcpSocket); _scannerSocket = new ScannerSocket(tcpSocket.Accept()); udpSocketSend.Close(); udpSocketReceive.Close(); tcpSocket.Close(); _workThread = new Thread(Work); _workThread.Start(); _sendEventsThread = new Thread(SendEvents); _sendEventsThread.Start(); um = SendAndWaitAnswer(Command.umGetVersion); DriverVersion = (new Versions(um.Data)).Driver; switch (_currentConfiguration.ScannerVersion) { case ScannerVersion.V2003: _sh = new SIB2003.SharedMemory(); break; case ScannerVersion.V2005: _sh = new SIB2005.SharedMemory(); break; case ScannerVersion.V2009: _sh = new SIB2009.SharedMemory(); break; case ScannerVersion.V2010: _sh = new SIB2010.SharedMemory(); break; } _x = new short[NUMBER_OF_SIDES]; _x[0] = _x[1] = 0; _y = new short[NUMBER_OF_SIDES]; _y[0] = _y[1] = 0; ScanningEnabled = false; ReloadProperties(); ReloadManufProps(); ReloadWhiteCoeffs(); logger.LogInfo(Message.ScannerManagerDetectedHardware, _currentConfiguration.ScannerVersion, _currentConfiguration.MaxLines, _currentConfiguration.DotsOneLine, _currentConfiguration.DotsOneSide, _currentConfiguration.SizeofBinaryBuffer, _currentConfiguration.SizeofHalftoneBuffer); break; } finally { if (udpSocketSend != null) udpSocketSend.Close(); if (udpSocketReceive != null) udpSocketReceive.Close(); if (tcpSocket != null) tcpSocket.Close(); } } if (_scannerSocket == null) { throw new Exception("не дождались соединения со сканером"); } }
private void Dispose(bool disposing) { lock (this) { if (disposing) { GC.SuppressFinalize(this); } if (_sendEventsThread != null) { _sendEventsThread.SafeAbort(); _sendEventsThread = null; } if (_workThread != null) { _workThread.SafeAbort(); _workThread = null; } if (_scannerSocket != null) { ScanningEnabled = false; _scannerSocket.Close(); _scannerSocket = null; } if (_sh != null) { _sh.Close(); _sh = null; } } }