Hi all,
Some time ago I played arround localConnection on Linux and how to
connect to the shared memory segment and syncronize with the named
semaphore. On linux localConnection uses systemV ipcs mechanisms: shm
(shared memory) and named semaphores.
The result is following C code. It is working right on gentoo box. The
only problem is on this lines:
if ( (shid= shmget(0x000053bd,sz,0)) < 0)
and
if ((sem_id = semget((key_t)0x000053bd,1,0)) < 0)
where 0x000053bd is a hardcoded value (shm key). When your swf movie is
running (the localconnection producer, that is, a swf movie wich sends
data to this C application) run ipcs shell command and substitute
0x000053bd with the *key* ID of the shared memory segment (shm) both on
the shmget and semget (named semaphores use to have the same ID). For
sure there's another way to do this but by now have no idea :P
<code>
/*********************************
* consumer.c *
*********************************/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <semaphore.h>
#include <errno.h>
#include <sys/mman.h>
#include "semSystemV.c"
main() {
int shid;
char *buffer;
char ch;
int i=0,sem_id,j=0;
int sz= 64528;
/* get shared memory segment
0x000053bd is a hardcoded value got from ipcs shell command
*/
if ( (shid= shmget(0x000053bd,sz,0)) < 0)
{
perror("error creating shm segment");
exit(EXIT_FAILURE);
}
// Bind of the shm segment to the local process
if ((buffer= (char *)shmat(shid,NULL, 0)) < 0)
{
perror("Error en shmat");
exit(EXIT_FAILURE);
}
/* connection to the named semaphore
again 0x000053bd is a hardcoded param.
*/
if ((sem_id = semget((key_t)0x000053bd,1,0)) < 0)
{
perror ("Error en creaci�n del sem�foro");
error (EXIT_FAILURE);
}
while (1)
{
// We wait until the producer generates content
// on the shm segment
P(sem_id);
i = 0;
// Dirty way to parse amf content
// version
printf ("-----------------------------------");
printf("Version: %d\n",buffer[i]); i += 4;
// #header elements
printf("Header Elements: %d\n",buffer[i]); i += 4;
// length element name
printf("Length Element name: %d\n",buffer[i]); i += 4;
printf("Some data: %d\n",buffer[i]); i+=4;
printf ("%d\n",i);
int type,len;
int k = 0;
while (k < 4)
{
k++;
type = (int) buffer[i]; i += 2;
len = (int) buffer[i];
printf ("Type: %d -- Len: %d -- String: ",type,len);
j = 0;
while (j <= len)
{
printf ("%c",buffer[j + i]);
j++;
}
i = j + i;
printf ("\n");
}
}
// disconnect from shm
if (shmdt (buffer) == -1)
{
fprintf (stderr, "error shmdt\n");
exit (EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
/*********************************
* semSystemV.c *
*********************************/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include "semSystemV.h"
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
};
static int set_semvalue(int sem_id)
{
union semun sem_union;
sem_union.val = 1;
if (semctl(sem_id, 0, SETVAL, sem_union) < 0)
{
perror ("Error incicializando sem�foro");
exit (5);
}
return(0);
}
static void del_semvalue(int sem_id)
{
union semun sem_union;
if (semctl(sem_id, 0, IPC_RMID, sem_union) == -1)
fprintf(stderr, "Failed to delete semaphore\n");
}
static int P(int sem_id)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = 0;
sem_b.sem_flg = SEM_UNDO;
if (semop(sem_id, &sem_b, 1) == -1) {
fprintf(stderr, "semaphore_p failed\n");
return(0);
}
return(1);
}
static int V(int sem_id)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = 1;
sem_b.sem_flg = SEM_UNDO;
if (semop(sem_id, &sem_b, 1) == -1)
{
fprintf(stderr, "semaphore_v failed\n");
return(0);
}
return(1);
}
</code>
to compile:
cc -I ./ consumer.c semSystemV.c -o consumer -lrt
NOTE: it's important to note the -lrt parameter. This is because the
program is using real time mechanisms. If we forget this it won't work
properly.
Hope that helps you all, and now maybe we could develop a synchronous
crossplatform extended player.
I'll add this to the wiki later.
Cheers
Xavi Beumala
http://www.code4net.com
_______________________________________________
osflash mailing list
[email protected]
http://osflash.org/mailman/listinfo/osflash_osflash.org