Skip to content

Commit

Permalink
[-] Fixed error with PPP connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Kravtsov committed Jul 15, 2022
1 parent 7fc423f commit 3a3ef66
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 97 deletions.
3 changes: 2 additions & 1 deletion History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

********************************************************************************

08.07.2022
15.07.2022
deviceServiceVersion = 1013669

[+] Added directIO command SMFPTR_DIO_READ_PORT_NAMES to read bluetooth device
MAC address and name. Application can use this command to simplify
entering device address.
[-] Fixed error with text report and command 0xFF46, print receipt item 2
[-] Changed FDO server timeout - it is read from table "FDO parameters" (Т19П3)
[-] Fixed error with PPP connection

********************************************************************************

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class PrinterProtocol_2 implements PrinterProtocol {
private final Frame frame = new Frame();
byte[] rx = {};
private int byteTimeout = 100;
private int maxRepeatCount = 1;
private int maxRepeatCount = 3;
private final boolean isReliable;
private static CompositeLogger logger = CompositeLogger.getLogger(PrinterProtocol_2.class);

Expand All @@ -46,13 +46,11 @@ public void connect() throws Exception
public void send(PrinterCommand command) throws Exception
{
int repeatCount = maxRepeatCount;
if (isReliable){
repeatCount = 1;
}
synchronized (port.getSyncObject())
{
for (int i = 0; i < repeatCount; i++)
{

if (i > 0){
logger.debug(String.format("Retry %d/%d", i ,repeatCount));
}
Expand Down Expand Up @@ -98,7 +96,6 @@ private boolean sendCommand(PrinterCommand command, int retryNum) throws Excepti
logger.error(e.getMessage());
isSynchronized = false;
return false;
//throw e;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,18 @@ public void deviceExecute(PrinterCommand command) throws Exception {
}
device.send(command);
} catch (Exception e) {
//port.close(); !!!
port.close();
throw new DeviceException(PrinterConst.SMFPTR_E_NOCONNECTION, e.getMessage());
}

if (command.isSucceeded())
{
/*
// After receipt close
if (command.getCode() == 0xFF45){
Thread.sleep(3000);
}
*/
commandSucceeded(command);
} else {
if (capLastErrorText) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ public class PPPPort implements PrinterPort, PrinterPort.IPortEvents {
private boolean opened = false;
private boolean firstCommand = true;
private String localSocketName = null;
private Thread rxThread = null;
private Exception rxException = null;
private CircularBuffer rxBuffer = new CircularBuffer(1024);
private long lastTimeInMillis = 0;
private static CompositeLogger logger = CompositeLogger.getLogger(PPPPort.class);

private final FptrParameters params;
Expand Down Expand Up @@ -97,24 +95,25 @@ public void run() {

public void openSocket() throws Exception
{
if (socket != null){
return;
if ((lastTimeInMillis != 0) && ((Calendar.getInstance().getTimeInMillis()-lastTimeInMillis) > 60000))
{
logger.debug("Reopen connection after 60 seconds of inacticity");
closeSocket();
}

if (socket == null){
socket = new Socket();
socket.setTcpNoDelay(true);
}
if (!socket.isConnected())
{
logger.debug("socket.connect");
socket.setSoTimeout(connectTimeout);
socket.connect(new InetSocketAddress("127.0.0.1", 7778));
socket.setSoTimeout(readTimeout);
lastTimeInMillis = Calendar.getInstance().getTimeInMillis();
logger.debug("socket.connect: OK");
}
logger.debug("openSocket()");
socket = new Socket();
socket.setTcpNoDelay(true);
socket.setSoTimeout(connectTimeout);
socket.connect(new InetSocketAddress("127.0.0.1", 7778));
socket.setSoTimeout(0);
rxException = null;
rxThread = new Thread(new Runnable() {
@Override
public void run() {
rxProc();
}
});
rxThread.start();
logger.debug("openSocket: OK");
}

public void startPPPThread() throws Exception
Expand Down Expand Up @@ -144,6 +143,7 @@ public void openLocalSocket(int timeout) throws Exception
if (localSocket != null) {
return;
}
logger.debug("openLocalSocket");
localSocket = new LocalSocket();
long time = Calendar.getInstance().getTimeInMillis() + timeout;
for (;;) {
Expand All @@ -158,6 +158,7 @@ public void openLocalSocket(int timeout) throws Exception
Thread.sleep(100);
}
}
logger.debug("openLocalSocket: OK");
}

public synchronized void close()
Expand Down Expand Up @@ -229,33 +230,7 @@ public void closeSocket()
logger.error(e.getMessage());
}
socket = null;

if (rxThread != null) {
rxThread.interrupt();
try {
rxThread.join();
} catch (InterruptedException e) {
}
rxThread = null;
}
}

public void rxProc()
{
try {
while (true)
{
Thread.sleep(0);
int b = socket.getInputStream().read();
if (b < 0) break;
rxBuffer.write(b);
}

}
catch(Exception e){
rxException = e;
logger.error("rxProc: " + e.getMessage());
}
logger.debug("closeSocket: OK");
}

public void dispatchProc()
Expand Down Expand Up @@ -326,8 +301,8 @@ public int readByte() throws Exception
return byteToInt(readBytes(1)[0]);
}

/*
public byte[] readBytes2(int len) throws Exception {
public byte[] readBytes(int len) throws Exception
{
open();

long time = Calendar.getInstance().getTimeInMillis() + readTimeout;
Expand All @@ -337,63 +312,33 @@ public byte[] readBytes2(int len) throws Exception {
int offset = 0;
while (len > 0)
{
openSocket();

int count = Math.min(len, socket.getInputStream().available());
if (count > 0) {
count = socket.getInputStream().read(data, offset, count);
if (count == -1) {
closeSocket();
noConnectionError();
}
len -= count;
offset += count;
}
if (Calendar.getInstance().getTimeInMillis() > time){
lastTimeInMillis = Calendar.getInstance().getTimeInMillis();
if (lastTimeInMillis > time){
noConnectionError();
}
Thread.sleep(0);
}
return data;
}
*/

public byte[] readBytes(int len) throws Exception{
open();

//logger.debug("readBytes(" + len + ", " + readTimeout + ")");

firstCommand = false;
//logger.debug("readBytes: " + len);
if (len <= 0)
{
throw new Exception("Data length <= 0");
}

// wait for data available with timeout
long startTime = System.currentTimeMillis();
for (;;)
{
if (rxException != null){
throw rxException;
}
if (rxBuffer.available() >= len) break;
Time.delay(1);
long currentTime = System.currentTimeMillis();
if ((currentTime - startTime) > readTimeout)
{
throw new IOException("Read timed out");
}
}
byte[] data = rxBuffer.read(len);
//logger.debug("read(" + Hex.toHex(data) + ")");
return data;
}

public void write(byte[] b) throws Exception
{
open();
for (int i = 0; i < 2; i++) {
try {
openSocket();
rxBuffer.clear();
socket.getOutputStream().write(b);
socket.getOutputStream().flush();
return;
Expand Down Expand Up @@ -424,6 +369,9 @@ public void setTimeout(int timeout) throws Exception {
if (firstCommand){
readTimeout = timeout + 100000;
}
if (isOpened()){
socket.setSoTimeout(readTimeout);
}
logger.debug("setTimeout(" + readTimeout + ")");
}

Expand Down Expand Up @@ -462,12 +410,6 @@ public String readParameter(int parameterID){

public void onConnect()
{
try {
//open();
}
catch(Exception e){
logger.error("onConnect: " + e.getMessage());
}
}

public void onDisconnect(){
Expand Down

0 comments on commit 3a3ef66

Please sign in to comment.