Browse Source

rajout du support udp

Sebastien DI MERCURIO 5 years ago
parent
commit
59f1442eba

+ 69
- 0
software/monitor/monitor/ClientUDP.cs View File

@@ -0,0 +1,69 @@
1
+using System;
2
+using System.Net;
3
+using System.Net.Sockets;
4
+using System.Text;
5
+
6
+namespace monitor
7
+{
8
+    public static class ClientUDP
9
+    {
10
+        private const int listenPort = 11000;
11
+        private static byte[] ImageBuffer = null;
12
+
13
+        private static UdpClient listener = null;
14
+        private static IPEndPoint groupEP = null;
15
+
16
+        public static void UDPOpen(int port)
17
+        {
18
+            listener = new UdpClient(port);
19
+            groupEP = new IPEndPoint(IPAddress.Any, port);
20
+        }
21
+
22
+        public static void UDPClose()
23
+        {
24
+            listener.Close();
25
+        }
26
+
27
+        public static byte[] GetImage()
28
+        {
29
+            bool done = false;
30
+
31
+            try
32
+            {
33
+                while (!done)
34
+                {
35
+                    Console.WriteLine("Waiting for broadcast");
36
+                    byte[] bytes = listener.Receive(ref groupEP);
37
+
38
+                    Console.WriteLine("Received broadcast from {0} :\n {1}\n",
39
+                        groupEP.ToString(),
40
+                        Encoding.ASCII.GetString(bytes, 0, bytes.Length));
41
+
42
+                    if (bytes[0]=='I') {
43
+                        // Nouvelle trame recu
44
+                        ImageBuffer = bytes;
45
+                    }
46
+                    else if (bytes[bytes.Length-1]=='D')
47
+                    {
48
+                        Array.Resize<byte>(ref ImageBuffer, ImageBuffer.Length + bytes.Length); // resize currrent buffer
49
+
50
+                        System.Buffer.BlockCopy(ImageBuffer, 0, bytes, ImageBuffer.Length-bytes.Length, bytes.Length);
51
+                        done = true;
52
+                    }
53
+                    else{
54
+                        Array.Resize<byte>(ref ImageBuffer, ImageBuffer.Length + bytes.Length); // resize currrent buffer
55
+
56
+                        System.Buffer.BlockCopy(ImageBuffer, 0, bytes, ImageBuffer.Length-bytes.Length, bytes.Length);
57
+                    }
58
+                }
59
+
60
+            }
61
+            catch (Exception e)
62
+            {
63
+                Console.WriteLine(e.ToString());
64
+            }
65
+     
66
+            return ImageBuffer;
67
+        }
68
+    }
69
+}

+ 1
- 0
software/monitor/monitor/monitor.csproj View File

@@ -70,6 +70,7 @@
70 70
     <Compile Include="Client.cs" />
71 71
     <Compile Include="CommandManager.cs" />
72 72
     <Compile Include="DestijlCommandManager.cs" />
73
+    <Compile Include="ClientUDP.cs" />
73 74
   </ItemGroup>
74 75
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
75 76
 </Project>

+ 63
- 2
software/raspberry/superviseur-robot/lib/src/server.cpp View File

@@ -19,15 +19,22 @@
19 19
 #include <stdio.h>
20 20
 #include <stdlib.h>
21 21
 #include <unistd.h>
22
+#include <string.h>
22 23
 
23 24
 #define NB_CONNECTION_MAX 1
24 25
 
25 26
 int socketFD = -1;
26 27
 int clientID = -1;
27 28
 
29
+int socketUDP= -1;
30
+struct sockaddr_in UDPcliaddr;
31
+int UDPcliaddrlen = -1;
32
+char *UDPBuffer=0;
33
+
28 34
 int openServer(int port) {
29 35
     struct sockaddr_in server;
30
-
36
+    struct sockaddr_in serverUDP;
37
+    
31 38
     socketFD = socket(AF_INET, SOCK_STREAM, 0);
32 39
     if (socketFD < 0) {
33 40
         perror("Can not create socket");
@@ -45,13 +52,33 @@ int openServer(int port) {
45 52
 
46 53
     listen(socketFD, NB_CONNECTION_MAX);
47 54
 
55
+    /* Open UDP connection */
56
+    socketUDP = socket(AF_INET, SOCK_DGRAM, 0);
57
+    if (socketUDP < 0) {
58
+        perror("Can not create UDP socket");
59
+        exit(-1);
60
+    }
61
+    
62
+    serverUDP.sin_addr.s_addr = INADDR_ANY;
63
+    serverUDP.sin_family = AF_INET;
64
+    serverUDP.sin_port = htons(port+1);
65
+    
66
+    memset(&UDPcliaddr, 0, sizeof(UDPcliaddr)); 
67
+    
68
+    if (bind(socketUDP, (struct sockaddr *) &serverUDP, sizeof (serverUDP)) < 0) {
69
+        perror("Can not bind UDP socket");
70
+        exit(-1);
71
+    }
72
+    
48 73
     return socketFD;
49 74
 }
50 75
 
51 76
 int closeServer() {
52 77
     close(socketFD);
53
-
78
+    close(socketUDP);
79
+    
54 80
     socketFD = -1;
81
+    socketUDP = -1;
55 82
     
56 83
     return 0;
57 84
 }
@@ -80,6 +107,40 @@ int sendDataToServerForClient(int client, char *data, int length) {
80 107
     else return 0;
81 108
 }
82 109
 
110
+void waitUDPClientAddr(void)
111
+{
112
+    char buffer[10]; 
113
+    recvfrom(socketUDP, (char *)buffer, 10,  
114
+            MSG_WAITALL, ( struct sockaddr *) &UDPcliaddr, &UDPcliaddrlen); 
115
+}
116
+
117
+int sendImage(char *data, int length) {
118
+    if (clientID >= 0)
119
+    {
120
+        UDPBuffer= (char*)malloc(length+12);
121
+    
122
+        UDPBuffer[0]='I';
123
+        UDPBuffer[1]='N';
124
+        UDPBuffer[2]='S';
125
+        UDPBuffer[3]='A';
126
+        UDPBuffer[4]='B';
127
+        UDPBuffer[5]='G';
128
+        
129
+        UDPBuffer[length+6]='A';
130
+        UDPBuffer[length+7]='S';
131
+        UDPBuffer[length+8]='N';
132
+        UDPBuffer[length+9]='I';
133
+        UDPBuffer[length+10]='E';
134
+        UDPBuffer[length+11]='D';
135
+        
136
+        memcpy((void*)(UDPBuffer+6),(const void *)data, length);
137
+        
138
+        return sendto(socketUDP, data, length, 
139
+        MSG_CONFIRM, (const struct sockaddr *) &UDPcliaddr, UDPcliaddrlen); 
140
+    }   
141
+    else return 0;
142
+}
143
+
83 144
 int receiveDataFromServer(char *data, int size) {
84 145
     return receiveDataFromServerFromClient(clientID, data, size);
85 146
 }

+ 5
- 5
software/raspberry/testeur/testeur/nbproject/Makefile-Debug.mk View File

@@ -69,27 +69,27 @@ ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testeur: ${OBJECTFILES}
69 69
 ${OBJECTDIR}/_ext/e4d40e25/image.o: ../../superviseur-robot/lib/src/image.cpp
70 70
 	${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
71 71
 	${RM} "$@.d"
72
-	$(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
72
+	$(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
73 73
 
74 74
 ${OBJECTDIR}/_ext/e4d40e25/message.o: ../../superviseur-robot/lib/src/message.cpp
75 75
 	${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
76 76
 	${RM} "$@.d"
77
-	$(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
77
+	$(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
78 78
 
79 79
 ${OBJECTDIR}/_ext/e4d40e25/robot.o: ../../superviseur-robot/lib/src/robot.cpp
80 80
 	${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
81 81
 	${RM} "$@.d"
82
-	$(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
82
+	$(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
83 83
 
84 84
 ${OBJECTDIR}/_ext/e4d40e25/server.o: ../../superviseur-robot/lib/src/server.cpp
85 85
 	${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
86 86
 	${RM} "$@.d"
87
-	$(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
87
+	$(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
88 88
 
89 89
 ${OBJECTDIR}/main.o: main.cpp
90 90
 	${MKDIR} -p ${OBJECTDIR}
91 91
 	${RM} "$@.d"
92
-	$(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
92
+	$(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
93 93
 
94 94
 # Subprojects
95 95
 .build-subprojects:

+ 3
- 6
software/raspberry/testeur/testeur/nbproject/private/private.xml View File

@@ -7,12 +7,9 @@
7 7
     <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
8 8
     <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
9 9
         <group>
10
-            <file>file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/superviseur-robot/lib/image.h</file>
11
-            <file>file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/superviseur-robot/lib/src/server.cpp</file>
12
-            <file>file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/superviseur-robot/lib/src/image.cpp</file>
13
-            <file>file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/superviseur-robot/lib/src/robot.cpp</file>
14
-            <file>file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/testeur/testeur/main.cpp</file>
15
-            <file>file:/home/dimercur/Documents/Travail/git/dumber/software/raspberry/superviseur-robot/lib/robot.h</file>
10
+            <file>file:/home/dimercur/Documents/Travail/dumber/software/raspberry/superviseur-robot/lib/src/server.cpp</file>
11
+            <file>file:/home/dimercur/Documents/Travail/dumber/software/raspberry/superviseur-robot/lib/src/image.cpp</file>
12
+            <file>file:/home/dimercur/Documents/Travail/dumber/software/raspberry/testeur/testeur/main.cpp</file>
16 13
         </group>
17 14
     </open-files>
18 15
 </project-private>

Loading…
Cancel
Save