void Client() { running = true; try { while (running) { pipe = new NamedPipeClientStream(settings.Host, settings.PipeName, PipeDirection.InOut, PipeOptions.Asynchronous); eventPipe = new NamedPipeClientStream(settings.Host, settings.EventPipeName, PipeDirection.InOut, PipeOptions.Asynchronous); while (running) { try { pipe.Connect(2000); } catch { continue; } break; } if (!running) { if (pipe.IsConnected) { pipe.Close(); } break; } while (running) { try { eventPipe.Connect(2000); } catch { continue; } break; } if (!running) { if (eventPipe.IsConnected) { eventPipe.Close(); } break; } BinaryReader brPipe = new BinaryReader(pipe); BinaryWriter bwPipe = new BinaryWriter(pipe); bwEventPipe = new BinaryWriter(eventPipe); DriverConnected = true; try { while (running) { try { int command = brPipe.ReadInt32(); switch (command) { case 0: handler.ResetCard(true); Log("Reset"); if (cardInserted) { var ATR = handler.ATR; bwPipe.Write((Int32)ATR.Length); bwPipe.Write(ATR, 0, ATR.Length); bwPipe.Flush(); } else { bwPipe.Write((Int32)0); bwPipe.Flush(); } break; case 1: if (cardInserted) { var ATR = handler.ATR; bwPipe.Write((Int32)ATR.Length); bwPipe.Write(ATR, 0, ATR.Length); bwPipe.Flush(); } else { bwPipe.Write((Int32)0); bwPipe.Flush(); } //if (command == 0) // logMessage("Reset"); //else // logMessage("getATR"); break; case 2: int apduLen = brPipe.ReadInt32(); byte[] APDU = new byte[apduLen]; brPipe.Read(APDU, 0, apduLen); byte[] resp = handler.ProcessApdu(APDU); if (resp != null) { bwPipe.Write((Int32)resp.Length); bwPipe.Write(resp, 0, resp.Length); } else { bwPipe.Write((Int32)0); } bwPipe.Flush(); break; } } catch (Exception e) { if (!(e is EndOfStreamException) && !(e is ObjectDisposedException)) { Log(e.ToString()); } if (running) { break; } else { Log("Card Stop"); return; } } } } catch (Exception ex) { int p = 0; } finally { if (cardInserted) { cardInserted = false; if (CardInsert != null) { CardInsert(false); } } DriverConnected = false; if (pipe.IsConnected) { pipe.Close(); } if (eventPipe.IsConnected) { eventPipe.Close(); } pipe.Dispose(); eventPipe.Dispose(); } } } catch (Exception ex) { Log("Card Exception:" + ex.ToString()); } }
void Client() { running = true; while (running) { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); eventSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); while (running) { try { socket.Connect(settings.Host, settings.Port); } catch { continue; } break; } if (!running) { if (socket.Connected) { socket.Close(); } break; } while (running) { try { eventSocket.Connect(settings.Host, settings.EventPort); } catch { continue; } break; } if (!running) { if (eventSocket.Connected) { eventSocket.Close(); } break; } var socketStream = new NetworkStream(socket); var eventSocketStream = new NetworkStream(eventSocket); BinaryReader brPipe = new BinaryReader(socketStream); BinaryWriter bwPipe = new BinaryWriter(socketStream); bwEventPipe = new BinaryWriter(eventSocketStream); DriverConnected = true; try { while (running) { try { int command = brPipe.ReadInt32(); switch (command) { case 0: handler.ResetCard(true); Log("Reset"); if (cardInserted) { var ATR = handler.ATR; bwPipe.Write((Int32)ATR.Length); bwPipe.Write(ATR, 0, ATR.Length); bwPipe.Flush(); } else { bwPipe.Write((Int32)0); bwPipe.Flush(); } break; case 1: if (cardInserted) { var ATR = handler.ATR; bwPipe.Write((Int32)ATR.Length); bwPipe.Write(ATR, 0, ATR.Length); bwPipe.Flush(); } else { bwPipe.Write((Int32)0); bwPipe.Flush(); } break; case 2: int apduLen = brPipe.ReadInt32(); byte[] APDU = new byte[apduLen]; brPipe.Read(APDU, 0, apduLen); byte[] resp = handler.ProcessApdu(APDU); if (resp != null) { bwPipe.Write((Int32)resp.Length); bwPipe.Write(resp, 0, resp.Length); } else { bwPipe.Write((Int32)0); } bwPipe.Flush(); break; } } catch (Exception e) { if (!(e is EndOfStreamException) && !(e is ObjectDisposedException) && !(e is IOException)) { Log(e.ToString()); } if (running) { break; } else { Log("Card Stop"); return; } } } } finally { if (cardInserted) { cardInserted = false; if (CardInsert != null) { CardInsert(false); } } DriverConnected = false; if (socket.Connected) { socket.Close(); } if (eventSocket.Connected) { eventSocket.Close(); } } } }