Changeset 3140:2cdc8f58b5da in roaraudio


Ignore:
Timestamp:
01/15/10 12:06:29 (14 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

implemented read(), write(), close() and a lot internal aux functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroaross/libroaross.c

    r3139 r3140  
    6666#define _MAX_POINTER  8 
    6767 
     68// handle type: 
     69#define HT_NONE       0 
     70#define HT_STREAM     1 
     71#define HT_MIXER      2 
     72 
    6873struct session { 
    6974 int refc; 
     
    7681 int refc; // refrence counter 
    7782 struct session * session; 
     83 int type; 
     84 struct roar_stream    stream; 
     85 struct roar_vio_calls stream_vio; 
     86 int                   stream_opened; 
    7887}; 
    7988 
     
    8594} _os; 
    8695 
    87 static struct { 
     96static struct pointer { 
    8897 int fh; 
    8998 struct handle * handle; 
     
    155164} 
    156165 
     166static struct handle * _open_handle(struct session * session) { 
     167 struct handle * handle; 
     168 
     169 if ( (handle = roar_mm_malloc(sizeof(struct handle))) == NULL ) 
     170  return NULL; 
     171 
     172 memset(handle, 0, sizeof(struct handle)); 
     173 
     174 handle->refc = 1; 
     175 handle->session = session; 
     176 session->refc++; // TODO: better warp this 
     177 handle->type = HT_NONE; 
     178 roar_stream_new_empty(&(handle->stream)); 
     179 
     180 return handle; 
     181} 
     182 
     183static void _close_handle(struct handle * handle) { 
     184 if (handle == NULL) 
     185  return; 
     186 
     187 handle->refc--; 
     188 
     189 if ( handle->refc == 0 ) { 
     190  _close_session(handle->session); 
     191 
     192  if ( handle->stream_opened ) 
     193   roar_vio_close(&(handle->stream_vio)); 
     194 
     195  roar_mm_free(handle); 
     196 } 
     197} 
     198 
     199static int _open_stream (struct handle * handle) { 
     200  return -1; 
     201} 
     202 
     203static struct pointer * _get_pointer_by_fh (int fh) { 
     204 int i; 
     205 
     206 for (i = 0; i < _MAX_POINTER; i++) { 
     207  if ( _ptr[i].fh == fh ) 
     208   return &(_ptr[i]); 
     209 } 
     210 
     211 return NULL; 
     212} 
     213 
     214static struct pointer * _open_pointer(struct handle * handle) { 
     215 struct pointer * ret = _get_pointer_by_fh(-1); 
     216 
     217 if ( ret == NULL ) 
     218  return NULL; 
     219 
     220 if ( (ret->fh = _open_dummy()) == -1 ) 
     221  return NULL; 
     222 
     223 ret->handle = handle; 
     224 
     225 return ret; 
     226} 
     227 
     228static void _close_pointer(struct pointer * pointer) { 
     229 if ( pointer == NULL ) 
     230  return; 
     231 
     232 _os.close(pointer->fh); 
     233 
     234 pointer->fh = -1; 
     235 
     236 _close_handle(pointer->handle); 
     237} 
    157238 
    158239// ------------------------------------- 
     
    176257 
    177258int     close(int fd) { 
     259 struct pointer * pointer; 
    178260 _init(); 
    179261 
     262 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { 
     263  _close_pointer(pointer); 
     264  return 0; 
     265 } 
     266 
    180267 return _os.close(fd); 
    181268} 
    182269 
    183270ssize_t write(int fd, const void *buf, size_t count) { 
     271 struct pointer * pointer; 
     272 
    184273 _init(); 
    185274 
     275 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { 
     276  if ( pointer->handle->type == HT_STREAM ) { 
     277   if ( pointer->handle->stream_opened == 0 ) { 
     278    if ( _open_stream(pointer->handle) == -1 ) { 
     279     errno = EIO; 
     280     return -1; 
     281    } 
     282   } 
     283   return roar_vio_write(&(pointer->handle->stream_vio), buf, count); 
     284  } else { 
     285   errno = EINVAL; 
     286   return -1; 
     287  } 
     288 } 
     289 
    186290 return _os.write(fd, buf, count); 
    187291} 
    188292 
    189293ssize_t read(int fd, void *buf, size_t count) { 
     294 struct pointer * pointer; 
     295 
    190296 _init(); 
    191297 
     298 if ( (pointer = _get_pointer_by_fh(fd)) != NULL ) { 
     299  if ( pointer->handle->type == HT_STREAM ) { 
     300   if ( pointer->handle->stream_opened == 0 ) { 
     301    if ( _open_stream(pointer->handle) == -1 ) { 
     302     errno = EIO; 
     303     return -1; 
     304    } 
     305   } 
     306   return roar_vio_read(&(pointer->handle->stream_vio), buf, count); 
     307  } else { 
     308   errno = EINVAL; 
     309   return -1; 
     310  } 
     311 } 
     312 
    192313 return _os.read(fd, buf, count); 
    193314} 
Note: See TracChangeset for help on using the changeset viewer.