Date: Mon, 16 Dec 1996 20:20:55 -0500 (EST) From: "Marc G. Fournier" <scrappy@hub.org> To: hackers@freebsd.org Subject: Almost have mmap() figured, I think... Message-ID: <Pine.NEB.3.95.961216200508.283F-100000@hub.org>
next in thread | raw e-mail | index | archive | help
Evening... Altho I really hate to ask, I think I almost have this licked, but there is something I'm still missing, so the code isn't *quite* working the way it should. Essentially, to simplify things while I "understand it better", I've created n mmap()'d regions, each representing a frame. There are n corresponding mmap()'d regions, each of which correspond to the *size* of the frame. (I know, inelegant, but I can make it more elegant *after* I know better that which I'm doing...) The regions are being created as: ====== while(ii < FRAMES) { sprintf(framestr, "/tmp/frame-%02d", ii); /* Setup Video Stream Buffer */ if((fd = open(framestr, O_RDWR|O_CREAT, 0666)) < 0) { fprintf(stderr, "cannot open %s\n", framestr); exit(-1); } ftruncate(fd, FRAMESIZE); if((frame[ii] = mmap(0, FRAMESIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == (caddr_t) -1) { fprintf(stderr, "cannot mmap frame buffer #%02d\n", ii); exit(-1); } close(fd); } ===== Where frame[ii] is declared as "caddr_t frame[FRAMES];":$ Now, the information that is being written to each frame is coming from a socket, which all worked before I switched over to mmap(), and is read in as: ----- void readn(int fd, char *buf, size_t bytes) { int n; char *p = buf; while (bytes) { n = read(fd, p, bytes); if (n < 0) fprintf(stderr, "read failed in send-photo.cgi"); if (n == 0 && bytes != 0) fprintf(stderr, "short file on read, %d bytes left", bytes); bytes -= n; p += n; } } ---- And is being called as: ===== readn(sockfd, frame[ii], datasize); ===== Now, this is where I think I'm making my mistake, because the results of running this code is: hub> ./client Frame == 000, ii == 00, datasize == 1944, Size == 1944 Frame == 001, ii == 01, datasize == 1981, Size == 1981 Frame == 002, ii == 02, datasize == 1972, Size == 1972 Frame == 003, ii == 03, datasize == 1990, Size == 1990 Frame == 004, ii == 04, datasize == 2006, Size == 2006 Frame == 005, ii == 05, datasize == 1985, Size == 1985 Frame == 006, ii == 06, datasize == 2021, Size == 2021 Frame == 007, ii == 07, datasize == 2034, Size == 2034 Frame == 008, ii == 08, datasize == 1984, Size == 1984 Frame == 009, ii == 09, datasize == 2023, Size == 2023 Frame == 010, ii == 10, datasize == 2047, Size == 2047 datasize is what I got from the remote server, through the socket, Size is what the 'size' mmap()'d region sees, so that mapping is working fine. The 'client' that is reading the mmap()'d region is seeing the exact same size value as the server is writting to it...so that aspect is working right. The problem comes up when I try and print out the frame itself, using 'printf("%s\n", frame[ii]);'...the value printed out each time is exactly the same: <FF><D8><FF><E0> Ack, okay...just tried something quickly, and changed %s to %d, and the results are: hub> ./client Frame == 000, ii == 00, datasize == 1944, Size == 1944 134840320 Frame == 001, ii == 01, datasize == 1981, Size == 1981 134860800 Frame == 002, ii == 02, datasize == 1972, Size == 1972 134881280 Frame == 003, ii == 03, datasize == 1990, Size == 1990 134905856 Frame == 004, ii == 04, datasize == 2006, Size == 2006 134930432 So...am I accurate in assuming that what I'm getting is the memory location? I still don't understand this fully, so please bear with me...I'm *purely* guessing here :( So, I'm making the assumption that either the way I'm calling, or using, readn() is incorrect for use with mmap()'d regions...would I be better off to read in the socket, then memcpy() the data, or strncpy()? Or is what I'm missing even more simple then that? Thanks again... Marc G. Fournier scrappy@hub.org Systems Administrator @ hub.org scrappy@freebsd.org
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.NEB.3.95.961216200508.283F-100000>