private int SendIMessage(BufferFrame asdu) { byte[] buffer = asdu.GetBuffer(); int msgSize = asdu.GetMsgSize(); /* ASDU size + ACPI size */ buffer [0] = 0x68; /* set size field */ buffer [1] = (byte)(msgSize - 2); buffer [2] = (byte)((sendCount % 128) * 2); buffer [3] = (byte)(sendCount / 128); buffer [4] = (byte)((receiveCount % 128) * 2); buffer [5] = (byte)(receiveCount / 128); try { lock (socketStream) { socketStream.Write(buffer, 0, msgSize); DebugLog("SEND I (size = " + msgSize + ") : " + BitConverter.ToString(buffer, 0, msgSize)); sendCount = (sendCount + 1) % 32768; unconfirmedReceivedIMessages = 0; } } catch (System.IO.IOException) { // socket error --> close connection running = false; } return(sendCount); }
private void SendASDUInternal(ASDU asdu) { if (isActive) { lock (waitingASDUsHighPrio) { BufferFrame frame = new BufferFrame(new byte[256], 6); asdu.Encode(frame, parameters); waitingASDUsHighPrio.Enqueue(frame); } SendWaitingASDUs(); } }
private int SendIMessage(ASDU asdu) { BufferFrame frame = new BufferFrame(new byte[260], 6); /* reserve space for ACPI */ asdu.Encode(frame, parameters); byte[] buffer = frame.GetBuffer(); int msgSize = frame.GetMsgSize(); /* ACPI + ASDU */ buffer [0] = 0x68; /* set size field */ buffer [1] = (byte)(msgSize - 2); buffer [2] = (byte)((sendSequenceNumber % 128) * 2); buffer [3] = (byte)(sendSequenceNumber / 128); buffer [4] = (byte)((receiveSequenceNumber % 128) * 2); buffer [5] = (byte)(receiveSequenceNumber / 128); if (running) { socket.NoDelay = true; socket.Send(buffer, msgSize, SocketFlags.None); sendSequenceNumber = (sendSequenceNumber + 1) % 32768; statistics.SentMsgCounter++; unconfirmedReceivedIMessages = 0; if (sentMessageHandler != null) { sentMessageHandler(sentMessageHandlerParameter, buffer, msgSize); } return(sendSequenceNumber); } else { if (lastException != null) { throw new ConnectionException(lastException.Message, lastException); } else { throw new ConnectionException("not connected", new SocketException(10057)); } } }
public byte[] AsByteArray() { int expectedSize = IEC60870_5_104_MAX_ASDU_LENGTH - spaceLeft; BufferFrame frame = new BufferFrame(new byte[expectedSize], 0); Encode(frame, parameters); if (frame.GetMsgSize() == expectedSize) { return(frame.GetBuffer()); } else { return(null); } }
public BufferFrame Clone() { byte[] newBuffer = new byte[GetMsgSize()]; int newBufPos = 0; for (int i = startPos; i < bufPos; i++) { newBuffer [newBufPos++] = buffer [i]; } BufferFrame clone = new BufferFrame(newBuffer, 0); clone.bufPos = newBufPos; return(clone); }
private void sendNextAvailableASDU() { lock (sentASDUs) { if (isSentBufferFull()) { return; } long timestamp; int index; server.LockASDUQueue(); BufferFrame asdu = server.GetNextWaitingASDU(out timestamp, out index); try { if (asdu != null) { int currentIndex = 0; if (oldestSentASDU == -1) { oldestSentASDU = 0; newestSentASDU = 0; } else { currentIndex = (newestSentASDU + 1) % maxSentASDUs; } sentASDUs [currentIndex].entryTime = timestamp; sentASDUs [currentIndex].queueIndex = index; sentASDUs [currentIndex].seqNo = SendIMessage(asdu); sentASDUs [currentIndex].sentTime = SystemUtils.currentTimeMillis(); newestSentASDU = currentIndex; PrintSendBuffer(); } } finally { server.UnlockASDUQueue(); } } }
private bool sendNextHighPriorityASDU() { lock (sentASDUs) { if (isSentBufferFull()) { return(false); } BufferFrame asdu = waitingASDUsHighPrio.Dequeue(); if (asdu != null) { int currentIndex = 0; if (oldestSentASDU == -1) { oldestSentASDU = 0; newestSentASDU = 0; } else { currentIndex = (newestSentASDU + 1) % maxSentASDUs; } sentASDUs [currentIndex].queueIndex = -1; sentASDUs [currentIndex].seqNo = SendIMessage(asdu); sentASDUs [currentIndex].sentTime = SystemUtils.currentTimeMillis(); newestSentASDU = currentIndex; PrintSendBuffer(); } else { return(false); } } return(true); }