gestion des flux binaires

This commit is contained in:
Sebastien DI MERCURIO 2018-11-07 22:56:31 +01:00
parent a1cd0af6ef
commit 2c66078b46
9 changed files with 62 additions and 21 deletions

View file

@ -11,6 +11,7 @@ namespace monitor
private static NetworkStream myStream = null; private static NetworkStream myStream = null;
private const int BufferMaxSize = 512; private const int BufferMaxSize = 512;
private static byte[] buffer = new byte[BufferMaxSize]; private static byte[] buffer = new byte[BufferMaxSize];
private static StringBuilder sb = new StringBuilder(); private static StringBuilder sb = new StringBuilder();
private static int newLength = 1; private static int newLength = 1;
@ -51,10 +52,12 @@ namespace monitor
private const int BufferMaxSize = 512; private const int BufferMaxSize = 512;
private static byte[] buffer = new byte[BufferMaxSize]; private static byte[] buffer = new byte[BufferMaxSize];
private static byte[] receiveBuffer;
private static int initialReceiveBufferIndex = 0;
private static StringBuilder message = new StringBuilder(); private static StringBuilder message = new StringBuilder();
private static int newLength = 1; private static int newLength = 1;
public delegate void ReadEvent(string msg); public delegate void ReadEvent(string msg, byte[] buffer);
public static ReadEvent readEvent = null; public static ReadEvent readEvent = null;
public Client() public Client()
@ -124,6 +127,11 @@ namespace monitor
if (bytesRead > 0) if (bytesRead > 0)
{ {
message.Append(Encoding.ASCII.GetString(buffer, 0, bytesRead)); message.Append(Encoding.ASCII.GetString(buffer, 0, bytesRead));
if (receiveBuffer == null) receiveBuffer = new byte[bytesRead];
else Array.Resize<byte>(ref receiveBuffer, initialReceiveBufferIndex + bytesRead);
System.Buffer.BlockCopy(buffer, 0, receiveBuffer, initialReceiveBufferIndex, bytesRead);
initialReceiveBufferIndex = receiveBuffer.Length;
} }
if (client.Available > 0) if (client.Available > 0)
@ -134,9 +142,11 @@ namespace monitor
} }
else else
{ {
readEvent?.Invoke(message.ToString()); readEvent?.Invoke(message.ToString(), receiveBuffer);
message.Clear(); message.Clear();
receiveBuffer = null;
initialReceiveBufferIndex = 0;
} }
stream.BeginRead(buffer, 0, newLength, new AsyncCallback(ReadCallback), message); stream.BeginRead(buffer, 0, newLength, new AsyncCallback(ReadCallback), message);

View file

@ -4,7 +4,7 @@ namespace monitor
{ {
public class CommandManager public class CommandManager
{ {
public delegate void CommandReceivedEvent(string msg); public delegate void CommandReceivedEvent(string msg, byte[] buffer);
public CommandReceivedEvent commandReceivedEvent = null; public CommandReceivedEvent commandReceivedEvent = null;
private System.Timers.Timer waitTimer = new System.Timers.Timer(); private System.Timers.Timer waitTimer = new System.Timers.Timer();
@ -50,7 +50,7 @@ namespace monitor
Client.Close(); Client.Close();
} }
private void OnMessageReception(string message) private void OnMessageReception(string message, byte[] buffer)
{ {
waitTimer.Stop(); waitTimer.Stop();
this.messageReceived = message; this.messageReceived = message;
@ -62,14 +62,14 @@ namespace monitor
} }
else { else {
waitForAcknowledge = false; waitForAcknowledge = false;
this.commandReceivedEvent?.Invoke(message); this.commandReceivedEvent?.Invoke(message, buffer);
} }
} }
private void OnMessageTimeout(object sender, System.Timers.ElapsedEventArgs e) private void OnMessageTimeout(object sender, System.Timers.ElapsedEventArgs e)
{ {
messageReceived = null; messageReceived = null;
OnMessageReception(messageReceived); OnMessageReception(messageReceived, null);
} }
public CommandManagerStatus SendCommand(string cmd, out string answer, double timeout) public CommandManagerStatus SendCommand(string cmd, out string answer, double timeout)

View file

@ -50,7 +50,7 @@ namespace monitor
private string receivedHeader = null; private string receivedHeader = null;
private string receivedData = null; private string receivedData = null;
public delegate void CommandReceivedEvent(string header, string data); public delegate void CommandReceivedEvent(string header, string data, byte[] buffer);
public CommandReceivedEvent commandReceivedEvent = null; public CommandReceivedEvent commandReceivedEvent = null;
public double timeout = 100; // timeout pour les commandes avec acquitement public double timeout = 100; // timeout pour les commandes avec acquitement
@ -76,7 +76,7 @@ namespace monitor
if (commandManager != null) commandManager.Close(); if (commandManager != null) commandManager.Close();
} }
private void OnCommandReceived(string msg) private void OnCommandReceived(string msg, byte[] buffer)
{ {
string[] msgs = msg.Split(':'); string[] msgs = msg.Split(':');
@ -86,7 +86,7 @@ namespace monitor
if (msgs.Length >= 2) receivedData = msgs[1]; if (msgs.Length >= 2) receivedData = msgs[1];
else receivedData = null; else receivedData = null;
this.commandReceivedEvent?.Invoke(receivedHeader, receivedData); this.commandReceivedEvent?.Invoke(receivedHeader, receivedData, buffer);
} }
public bool Open(string hostname) public bool Open(string hostname)

View file

@ -129,7 +129,7 @@ public partial class MainWindow : Gtk.Window
a.RetVal = true; a.RetVal = true;
} }
public void OnCommandReceivedEvent(string header, string data) public void OnCommandReceivedEvent(string header, string data, byte[] buffer)
{ {
if (header != null) Console.WriteLine("Received header (" + header.Length + "): " + header); if (header != null) Console.WriteLine("Received header (" + header.Length + "): " + header);
if (data != null) Console.WriteLine("Received data (" + data.Length + "): " + data); if (data != null) Console.WriteLine("Received data (" + data.Length + "): " + data);
@ -152,6 +152,13 @@ public partial class MainWindow : Gtk.Window
break; break;
} }
} }
else if (header.ToUpper() == DestijlCommandList.HeaderStmImage)
{
Console.WriteLine("Image received");
byte[] image = new byte[buffer.Length-4];
System.Buffer.BlockCopy(buffer, 4, image, 0, image.Length);
drawingareaCameraPixbuf = new Pixbuf(image.Length, image, true);
}
} }
protected void OnQuitActionActivated(object sender, EventArgs e) protected void OnQuitActionActivated(object sender, EventArgs e)
@ -382,9 +389,9 @@ public partial class MainWindow : Gtk.Window
{ {
if (cmdManager.CameraClose() != DestijlCommandManager.CommandStatus.Success) if (cmdManager.CameraClose() != DestijlCommandManager.CommandStatus.Success)
{ {
MessagePopup(MessageType.Error, //MessagePopup(MessageType.Error,
ButtonsType.Ok, "Error", // ButtonsType.Ok, "Error",
"Error when closing camera: bad answer for supervisor or timeout"); // "Error when closing camera: bad answer for supervisor or timeout");
} }
} }
else else

View file

@ -44,7 +44,7 @@ int open_camera(Camera *camera)
// open the default camera, use something different from 0 otherwise; // open the default camera, use something different from 0 otherwise;
// Check VideoCapture documentation. // Check VideoCapture documentation.
printf("Opening Camera...\n"); printf("Opening Camera...\n");
if(!cap.open(0)) if(!cap.open(1))
return -1; return -1;
sleep(1); sleep(1);

View file

@ -52,6 +52,8 @@ int closeServer() {
close(socketFD); close(socketFD);
socketFD = -1; socketFD = -1;
return 0;
} }
int acceptClient() { int acceptClient() {

View file

@ -114,6 +114,20 @@ int sendAnswer(string cmd, string data) {
return status; return status;
} }
int sendBinaryData(string cmd, unsigned char* data) {
int status = 0;
string msg;
//TODO: Faire une version binaire de sendDataToServer
//TODO: Gerer la concatenation du header et des données binaires
msg = cmd + ':' + data;
cout << "Answer: " + msg;
cout << "\n";
sendDataToServer((char*) msg.c_str(), msg.length());
return status;
}
int decodeMessage(MessageFromMon *mes, int dataLength) { int decodeMessage(MessageFromMon *mes, int dataLength) {
int status = 0; int status = 0;
string header(mes->header, 4); string header(mes->header, 4);
@ -169,12 +183,12 @@ int main(int argc, char** argv) {
namedWindow("Sortie Camera"); namedWindow("Sortie Camera");
// Ouverture de la com robot // Ouverture de la com robot
if (open_communication_robot("/dev/ttyUSB0") != 0) { /*if (open_communication_robot("/dev/ttyUSB0") != 0) {
cerr << "Unable to open /dev/ttyUSB0: abort\n"; cerr << "Unable to open /dev/ttyUSB0: abort\n";
return -1; return -1;
} }
cout << "/dev/ttyUSB0 opened\n"; cout << "/dev/ttyUSB0 opened\n";
*/
// Ouverture de la camera // Ouverture de la camera
if (open_camera(0) == -1) { if (open_camera(0) == -1) {
cerr << "Unable to open camera: abort\n"; cerr << "Unable to open camera: abort\n";

View file

@ -69,27 +69,27 @@ ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testeur: ${OBJECTFILES}
${OBJECTDIR}/_ext/e4d40e25/image.o: ../../superviseur-robot/lib/src/image.cpp ${OBJECTDIR}/_ext/e4d40e25/image.o: ../../superviseur-robot/lib/src/image.cpp
${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25 ${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
${RM} "$@.d" ${RM} "$@.d"
$(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/image.o ../../superviseur-robot/lib/src/image.cpp $(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/image.o ../../superviseur-robot/lib/src/image.cpp
${OBJECTDIR}/_ext/e4d40e25/message.o: ../../superviseur-robot/lib/src/message.cpp ${OBJECTDIR}/_ext/e4d40e25/message.o: ../../superviseur-robot/lib/src/message.cpp
${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25 ${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
${RM} "$@.d" ${RM} "$@.d"
$(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/message.o ../../superviseur-robot/lib/src/message.cpp $(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/message.o ../../superviseur-robot/lib/src/message.cpp
${OBJECTDIR}/_ext/e4d40e25/robot.o: ../../superviseur-robot/lib/src/robot.cpp ${OBJECTDIR}/_ext/e4d40e25/robot.o: ../../superviseur-robot/lib/src/robot.cpp
${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25 ${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
${RM} "$@.d" ${RM} "$@.d"
$(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/robot.o ../../superviseur-robot/lib/src/robot.cpp $(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/robot.o ../../superviseur-robot/lib/src/robot.cpp
${OBJECTDIR}/_ext/e4d40e25/server.o: ../../superviseur-robot/lib/src/server.cpp ${OBJECTDIR}/_ext/e4d40e25/server.o: ../../superviseur-robot/lib/src/server.cpp
${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25 ${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
${RM} "$@.d" ${RM} "$@.d"
$(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/server.o ../../superviseur-robot/lib/src/server.cpp $(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/server.o ../../superviseur-robot/lib/src/server.cpp
${OBJECTDIR}/main.o: main.cpp ${OBJECTDIR}/main.o: main.cpp
${MKDIR} -p ${OBJECTDIR} ${MKDIR} -p ${OBJECTDIR}
${RM} "$@.d" ${RM} "$@.d"
$(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp $(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv` -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp
# Subprojects # Subprojects
.build-subprojects: .build-subprojects:

View file

@ -4,4 +4,12 @@
<activeConfTypeElem>1</activeConfTypeElem> <activeConfTypeElem>1</activeConfTypeElem>
<activeConfIndexElem>0</activeConfIndexElem> <activeConfIndexElem>0</activeConfIndexElem>
</data> </data>
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/home/dimercur/Documents/Travail/dumber/software/raspberry/superviseur-robot/lib/src/server.cpp</file>
<file>file:/home/dimercur/Documents/Travail/dumber/software/raspberry/superviseur-robot/lib/src/image.cpp</file>
<file>file:/home/dimercur/Documents/Travail/dumber/software/raspberry/testeur/testeur/main.cpp</file>
</group>
</open-files>
</project-private> </project-private>