Back to index

im-sdk  12.3.91
IIIMPTrans.cpp
Go to the documentation of this file.
00001 #include <config.h>
00002 #include <errno.h>
00003 #include <sys/types.h>
00004 #include "IIIMPTrans.hh"
00005 #include "IMUtil.hh"
00006 #include "IMLog.hh"
00007 
00008 bool
00009 IIIMPTrans::send(
00010     IIIMP_message *pmes
00011 )
00012 {
00013 #ifdef DEBUG
00014     fputs("S->C", stdout);
00015     iiimp_message_print(pimplibstate, pmes);
00016 #endif
00017     if (iiimf_stream_send(pstream, pimplibstate, pmes)
00018        != IIIMF_STATUS_SUCCESS)
00019        return false;
00020 
00021     return true;
00022 }
00023 
00024 IIIMP_message*
00025 IIIMPTrans::receive()
00026 {
00027     IIIMF_status st;
00028     IIIMP_message *pmes;
00029  
00030     st = iiimf_stream_receive(pstream, pimplibstate, &pmes);
00031     if (st != IIIMF_STATUS_SUCCESS) return NULL;
00032 #ifdef DEBUG
00033     fputs("C->S", stdout);
00034     iiimp_message_print(pimplibstate, pmes);
00035 #endif
00036 
00037     return pmes;
00038 }
00039 
00040 IIIMPTrans::IIIMPTrans(
00041     IMSocketTrans *x_pimst
00042 )
00043 {
00044     pimst = x_pimst;
00045     iiimf_create_stream(IIIMPTrans_read, IIIMPTrans_write, this, 0, &pstream);
00046     pimplibstate = iiimp_data_s_new();
00047     open = true;
00048 }
00049 
00050 IIIMPTrans::~IIIMPTrans()
00051 {
00052     delete pimst;
00053     iiimf_stream_delete(pstream);
00054     iiimp_data_s_delete(pimplibstate);
00055 }
00056 
00057 IIIMF_status
00058 IIIMPTrans_read(
00059     IIIMF_stream_private ptrans,
00060     void *buf,
00061     size_t nbyte
00062 )
00063 {
00064     int st;
00065     int n = nbyte;
00066     unsigned char *p = (unsigned char*) buf;
00067     IIIMPTrans *pimt = (IIIMPTrans*) ptrans;
00068     while (n > 0) {
00069        // TODO!! (NOWAIT support)
00070        st = pimt->pimst->recv(p, n);
00071        if (st == -1) {
00072            if (errno = EINTR) continue;
00073            return IIIMF_STATUS_STREAM_RECEIVE;
00074        } else if (st == 0) {
00075            // socket is closed.
00076            pimt->open = false;
00077            return IIIMF_STATUS_STREAM_RECEIVE;
00078        }
00079        n -= st;
00080        p += st;
00081     }
00082 
00083     return IIIMF_STATUS_SUCCESS;
00084 }
00085 
00086 IIIMF_status
00087 IIIMPTrans_write(
00088     IIIMF_stream_private ptrans,
00089     const void* buf,
00090     size_t nbyte
00091 )
00092 {
00093     int st;
00094     int n = nbyte;
00095     const unsigned char *p = (const unsigned char*) buf;
00096     IIIMPTrans *pimt = (IIIMPTrans*) ptrans;
00097 
00098     while (n > 0) {
00099        // TODO!!
00100        st = pimt->pimst->send(p, n);
00101        if (st == -1) {
00102            if (errno = EINTR) continue;
00103            return IIIMF_STATUS_STREAM_RECEIVE;
00104        }
00105        n -= st;
00106        p += st;
00107     }
00108 
00109     return IIIMF_STATUS_SUCCESS;
00110 }
00111 
00112 //
00113 /* Local Variables: */
00114 /* c-file-style: "iiim-project" */
00115 /* End: */