rajout du support udp
This commit is contained in:
parent
1f907870f6
commit
59f1442eba
5 changed files with 141 additions and 13 deletions
69
software/monitor/monitor/ClientUDP.cs
Normal file
69
software/monitor/monitor/ClientUDP.cs
Normal file
|
@ -0,0 +1,69 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
namespace monitor
|
||||
{
|
||||
public static class ClientUDP
|
||||
{
|
||||
private const int listenPort = 11000;
|
||||
private static byte[] ImageBuffer = null;
|
||||
|
||||
private static UdpClient listener = null;
|
||||
private static IPEndPoint groupEP = null;
|
||||
|
||||
public static void UDPOpen(int port)
|
||||
{
|
||||
listener = new UdpClient(port);
|
||||
groupEP = new IPEndPoint(IPAddress.Any, port);
|
||||
}
|
||||
|
||||
public static void UDPClose()
|
||||
{
|
||||
listener.Close();
|
||||
}
|
||||
|
||||
public static byte[] GetImage()
|
||||
{
|
||||
bool done = false;
|
||||
|
||||
try
|
||||
{
|
||||
while (!done)
|
||||
{
|
||||
Console.WriteLine("Waiting for broadcast");
|
||||
byte[] bytes = listener.Receive(ref groupEP);
|
||||
|
||||
Console.WriteLine("Received broadcast from {0} :\n {1}\n",
|
||||
groupEP.ToString(),
|
||||
Encoding.ASCII.GetString(bytes, 0, bytes.Length));
|
||||
|
||||
if (bytes[0]=='I') {
|
||||
// Nouvelle trame recu
|
||||
ImageBuffer = bytes;
|
||||
}
|
||||
else if (bytes[bytes.Length-1]=='D')
|
||||
{
|
||||
Array.Resize<byte>(ref ImageBuffer, ImageBuffer.Length + bytes.Length); // resize currrent buffer
|
||||
|
||||
System.Buffer.BlockCopy(ImageBuffer, 0, bytes, ImageBuffer.Length-bytes.Length, bytes.Length);
|
||||
done = true;
|
||||
}
|
||||
else{
|
||||
Array.Resize<byte>(ref ImageBuffer, ImageBuffer.Length + bytes.Length); // resize currrent buffer
|
||||
|
||||
System.Buffer.BlockCopy(ImageBuffer, 0, bytes, ImageBuffer.Length-bytes.Length, bytes.Length);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.ToString());
|
||||
}
|
||||
|
||||
return ImageBuffer;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -70,6 +70,7 @@
|
|||
<Compile Include="Client.cs" />
|
||||
<Compile Include="CommandManager.cs" />
|
||||
<Compile Include="DestijlCommandManager.cs" />
|
||||
<Compile Include="ClientUDP.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
|
@ -19,15 +19,22 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#define NB_CONNECTION_MAX 1
|
||||
|
||||
int socketFD = -1;
|
||||
int clientID = -1;
|
||||
|
||||
int socketUDP= -1;
|
||||
struct sockaddr_in UDPcliaddr;
|
||||
int UDPcliaddrlen = -1;
|
||||
char *UDPBuffer=0;
|
||||
|
||||
int openServer(int port) {
|
||||
struct sockaddr_in server;
|
||||
|
||||
struct sockaddr_in serverUDP;
|
||||
|
||||
socketFD = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (socketFD < 0) {
|
||||
perror("Can not create socket");
|
||||
|
@ -45,13 +52,33 @@ int openServer(int port) {
|
|||
|
||||
listen(socketFD, NB_CONNECTION_MAX);
|
||||
|
||||
/* Open UDP connection */
|
||||
socketUDP = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (socketUDP < 0) {
|
||||
perror("Can not create UDP socket");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
serverUDP.sin_addr.s_addr = INADDR_ANY;
|
||||
serverUDP.sin_family = AF_INET;
|
||||
serverUDP.sin_port = htons(port+1);
|
||||
|
||||
memset(&UDPcliaddr, 0, sizeof(UDPcliaddr));
|
||||
|
||||
if (bind(socketUDP, (struct sockaddr *) &serverUDP, sizeof (serverUDP)) < 0) {
|
||||
perror("Can not bind UDP socket");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
return socketFD;
|
||||
}
|
||||
|
||||
int closeServer() {
|
||||
close(socketFD);
|
||||
|
||||
close(socketUDP);
|
||||
|
||||
socketFD = -1;
|
||||
socketUDP = -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -80,6 +107,40 @@ int sendDataToServerForClient(int client, char *data, int length) {
|
|||
else return 0;
|
||||
}
|
||||
|
||||
void waitUDPClientAddr(void)
|
||||
{
|
||||
char buffer[10];
|
||||
recvfrom(socketUDP, (char *)buffer, 10,
|
||||
MSG_WAITALL, ( struct sockaddr *) &UDPcliaddr, &UDPcliaddrlen);
|
||||
}
|
||||
|
||||
int sendImage(char *data, int length) {
|
||||
if (clientID >= 0)
|
||||
{
|
||||
UDPBuffer= (char*)malloc(length+12);
|
||||
|
||||
UDPBuffer[0]='I';
|
||||
UDPBuffer[1]='N';
|
||||
UDPBuffer[2]='S';
|
||||
UDPBuffer[3]='A';
|
||||
UDPBuffer[4]='B';
|
||||
UDPBuffer[5]='G';
|
||||
|
||||
UDPBuffer[length+6]='A';
|
||||
UDPBuffer[length+7]='S';
|
||||
UDPBuffer[length+8]='N';
|
||||
UDPBuffer[length+9]='I';
|
||||
UDPBuffer[length+10]='E';
|
||||
UDPBuffer[length+11]='D';
|
||||
|
||||
memcpy((void*)(UDPBuffer+6),(const void *)data, length);
|
||||
|
||||
return sendto(socketUDP, data, length,
|
||||
MSG_CONFIRM, (const struct sockaddr *) &UDPcliaddr, UDPcliaddrlen);
|
||||
}
|
||||
else return 0;
|
||||
}
|
||||
|
||||
int receiveDataFromServer(char *data, int size) {
|
||||
return receiveDataFromServerFromClient(clientID, data, size);
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -7,12 +7,9 @@
|
|||
<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/git/dumber/software/raspberry/superviseur-robot/lib/image.h</file>
|
||||
<file>file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/superviseur-robot/lib/src/server.cpp</file>
|
||||
<file>file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/superviseur-robot/lib/src/image.cpp</file>
|
||||
<file>file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/superviseur-robot/lib/src/robot.cpp</file>
|
||||
<file>file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/testeur/testeur/main.cpp</file>
|
||||
<file>file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/superviseur-robot/lib/robot.h</file>
|
||||
<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