gestion des flux binaires
This commit is contained in:
parent
a1cd0af6ef
commit
2c66078b46
9 changed files with 62 additions and 21 deletions
|
@ -11,6 +11,7 @@ namespace monitor
|
|||
private static NetworkStream myStream = null;
|
||||
private const int BufferMaxSize = 512;
|
||||
private static byte[] buffer = new byte[BufferMaxSize];
|
||||
|
||||
private static StringBuilder sb = new StringBuilder();
|
||||
private static int newLength = 1;
|
||||
|
||||
|
@ -51,10 +52,12 @@ namespace monitor
|
|||
|
||||
private const int BufferMaxSize = 512;
|
||||
private static byte[] buffer = new byte[BufferMaxSize];
|
||||
private static byte[] receiveBuffer;
|
||||
private static int initialReceiveBufferIndex = 0;
|
||||
private static StringBuilder message = new StringBuilder();
|
||||
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 Client()
|
||||
|
@ -124,6 +127,11 @@ namespace monitor
|
|||
if (bytesRead > 0)
|
||||
{
|
||||
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)
|
||||
|
@ -134,9 +142,11 @@ namespace monitor
|
|||
}
|
||||
else
|
||||
{
|
||||
readEvent?.Invoke(message.ToString());
|
||||
readEvent?.Invoke(message.ToString(), receiveBuffer);
|
||||
|
||||
message.Clear();
|
||||
receiveBuffer = null;
|
||||
initialReceiveBufferIndex = 0;
|
||||
}
|
||||
|
||||
stream.BeginRead(buffer, 0, newLength, new AsyncCallback(ReadCallback), message);
|
||||
|
|
|
@ -4,7 +4,7 @@ namespace monitor
|
|||
{
|
||||
public class CommandManager
|
||||
{
|
||||
public delegate void CommandReceivedEvent(string msg);
|
||||
public delegate void CommandReceivedEvent(string msg, byte[] buffer);
|
||||
public CommandReceivedEvent commandReceivedEvent = null;
|
||||
|
||||
private System.Timers.Timer waitTimer = new System.Timers.Timer();
|
||||
|
@ -50,7 +50,7 @@ namespace monitor
|
|||
Client.Close();
|
||||
}
|
||||
|
||||
private void OnMessageReception(string message)
|
||||
private void OnMessageReception(string message, byte[] buffer)
|
||||
{
|
||||
waitTimer.Stop();
|
||||
this.messageReceived = message;
|
||||
|
@ -62,14 +62,14 @@ namespace monitor
|
|||
}
|
||||
else {
|
||||
waitForAcknowledge = false;
|
||||
this.commandReceivedEvent?.Invoke(message);
|
||||
this.commandReceivedEvent?.Invoke(message, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnMessageTimeout(object sender, System.Timers.ElapsedEventArgs e)
|
||||
{
|
||||
messageReceived = null;
|
||||
OnMessageReception(messageReceived);
|
||||
OnMessageReception(messageReceived, null);
|
||||
}
|
||||
|
||||
public CommandManagerStatus SendCommand(string cmd, out string answer, double timeout)
|
||||
|
|
|
@ -50,7 +50,7 @@ namespace monitor
|
|||
private string receivedHeader = 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 double timeout = 100; // timeout pour les commandes avec acquitement
|
||||
|
@ -76,7 +76,7 @@ namespace monitor
|
|||
if (commandManager != null) commandManager.Close();
|
||||
}
|
||||
|
||||
private void OnCommandReceived(string msg)
|
||||
private void OnCommandReceived(string msg, byte[] buffer)
|
||||
{
|
||||
string[] msgs = msg.Split(':');
|
||||
|
||||
|
@ -86,7 +86,7 @@ namespace monitor
|
|||
if (msgs.Length >= 2) receivedData = msgs[1];
|
||||
else receivedData = null;
|
||||
|
||||
this.commandReceivedEvent?.Invoke(receivedHeader, receivedData);
|
||||
this.commandReceivedEvent?.Invoke(receivedHeader, receivedData, buffer);
|
||||
}
|
||||
|
||||
public bool Open(string hostname)
|
||||
|
|
|
@ -129,7 +129,7 @@ public partial class MainWindow : Gtk.Window
|
|||
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 (data != null) Console.WriteLine("Received data (" + data.Length + "): " + data);
|
||||
|
@ -152,6 +152,13 @@ public partial class MainWindow : Gtk.Window
|
|||
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)
|
||||
|
@ -382,9 +389,9 @@ public partial class MainWindow : Gtk.Window
|
|||
{
|
||||
if (cmdManager.CameraClose() != DestijlCommandManager.CommandStatus.Success)
|
||||
{
|
||||
MessagePopup(MessageType.Error,
|
||||
ButtonsType.Ok, "Error",
|
||||
"Error when closing camera: bad answer for supervisor or timeout");
|
||||
//MessagePopup(MessageType.Error,
|
||||
// ButtonsType.Ok, "Error",
|
||||
// "Error when closing camera: bad answer for supervisor or timeout");
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
|
@ -44,7 +44,7 @@ int open_camera(Camera *camera)
|
|||
// open the default camera, use something different from 0 otherwise;
|
||||
// Check VideoCapture documentation.
|
||||
printf("Opening Camera...\n");
|
||||
if(!cap.open(0))
|
||||
if(!cap.open(1))
|
||||
return -1;
|
||||
|
||||
sleep(1);
|
||||
|
|
|
@ -52,6 +52,8 @@ int closeServer() {
|
|||
close(socketFD);
|
||||
|
||||
socketFD = -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int acceptClient() {
|
||||
|
|
|
@ -114,6 +114,20 @@ int sendAnswer(string cmd, string data) {
|
|||
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 status = 0;
|
||||
string header(mes->header, 4);
|
||||
|
@ -169,12 +183,12 @@ int main(int argc, char** argv) {
|
|||
namedWindow("Sortie Camera");
|
||||
|
||||
// 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";
|
||||
return -1;
|
||||
}
|
||||
cout << "/dev/ttyUSB0 opened\n";
|
||||
|
||||
*/
|
||||
// Ouverture de la camera
|
||||
if (open_camera(0) == -1) {
|
||||
cerr << "Unable to open camera: abort\n";
|
||||
|
|
|
@ -69,27 +69,27 @@ ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testeur: ${OBJECTFILES}
|
|||
${OBJECTDIR}/_ext/e4d40e25/image.o: ../../superviseur-robot/lib/src/image.cpp
|
||||
${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
|
||||
${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
|
||||
${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
|
||||
${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
|
||||
${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
|
||||
${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
|
||||
${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
|
||||
${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
|
||||
${MKDIR} -p ${OBJECTDIR}
|
||||
${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
|
||||
.build-subprojects:
|
||||
|
|
|
@ -4,4 +4,12 @@
|
|||
<activeConfTypeElem>1</activeConfTypeElem>
|
||||
<activeConfIndexElem>0</activeConfIndexElem>
|
||||
</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>
|
||||
|
|
Loading…
Reference in a new issue