Changeset 22:d13fbc745959 in xplcommon


Ignore:
Timestamp:
01/02/13 06:43:16 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Cleaned up and simplified the code

Location:
src/xplcommon/win32
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • src/xplcommon/win32/BufferedStream.cc

    r21 r22  
    6464{
    6565    DWORD numRead = 0;
    66     overlapped->reset();
    67     bool result = ReadFile(handle, dest, length, 0, overlapped->get());
    68                            // (overlapped==0) ? &numRead : 0,
    69                            // (overlapped==0) ? 0 : overlapped->get());
     66    if (overlapped!=0) overlapped->reset();
     67    bool result = ReadFile(handle, dest, length,
     68                           (overlapped==0) ? &numRead : 0,
     69                           (overlapped==0) ? 0 : overlapped->get());
    7070    DWORD error = GetLastError();
    71     if (!result && (overlapped==0 ||
    72                     (error!=ERROR_IO_PENDING && error!=ERROR_IO_INCOMPLETE)))
     71    if (!result &&
     72        (overlapped==0 ||
     73         (error!=ERROR_IO_PENDING && error!=ERROR_IO_INCOMPLETE)))
    7374    {
    7475        setErrorCode(error);
     
    8687                           Overlapped* overlapped)
    8788{
     89    if (overlapped!=0) overlapped->reset();
    8890    bool result = WriteFile(handle, src, length, 0,
    8991                            (overlapped==0) ? 0 : overlapped->get());
    9092    DWORD error = GetLastError();
    91     if (!result && (overlapped==0 ||
    92                     (error!=ERROR_IO_PENDING && error!=ERROR_IO_INCOMPLETE)))
     93    if (!result &&
     94        (overlapped==0 ||
     95         (error!=ERROR_IO_PENDING && error!=ERROR_IO_INCOMPLETE)))
    9396    {
    9497        setErrorCode(error);
  • src/xplcommon/win32/Completer.h

    r21 r22  
    5151protected:
    5252    /**
     53     * The overlappable object this completer belongs to.
     54     */
     55    Overlappable& overlappable;
     56
     57    /**
    5358     * The overlapped object.
    5459     */
     
    6469     * Destroy the completer.
    6570     */
    66     ~Completer();
     71    virtual ~Completer();
    6772
    6873    /**
     
    8893     * @see Overlapped::getResult
    8994     */
    90     bool getResult(DWORD& size, HANDLE file);
     95    bool getResult(DWORD& size);
     96
     97    /**
     98     * Check whether we are waiting for an overlapped result and if
     99     * so, return that.
     100     *
     101     * @param result will contain the result to return if we are
     102     * waiting for an overlapped result.
     103     *
     104     * @return whether we are waiting for an overlapped result
     105     */
     106    bool checkWaitedResult(bool& result);
     107
     108    /**
     109     * Handle the given result coming from an overlapped object.
     110     */
     111    virtual void handleWaitedResult(DWORD size) = 0;
    91112};
    92113
     
    96117
    97118inline Completer::Completer(Overlappable* overlappable) :
     119    overlappable(*overlappable),
    98120    overlapped((overlappable->waiter==0) ? 0 : new Overlapped(*overlappable))
    99121{
     
    130152//------------------------------------------------------------------------------
    131153
    132 inline bool Completer::getResult(DWORD& size, HANDLE file)
     154inline bool Completer::getResult(DWORD& size)
    133155{
    134     return overlapped!=0 && overlapped->getResult(size, file);
     156    return overlapped!=0 && overlapped->getResult(size, overlappable.handle);
     157}
     158
     159//------------------------------------------------------------------------------
     160
     161inline bool Completer::checkWaitedResult(bool& result)
     162{
     163    if (!isWaited()) {
     164        return false;
     165    }
     166
     167    DWORD size = 0;
     168    result = getResult(size);
     169
     170    if (result) {
     171        removeFromWaiter();
     172        handleWaitedResult(size);
     173    } else if (overlappable.failed()) {
     174        removeFromWaiter();
     175    }
     176
     177    return true;
    135178}
    136179
  • src/xplcommon/win32/LocalAcceptor.cc

    r21 r22  
    4343    accepted(false)
    4444{
    45     LocalSocket::setupName(this->name, name);
     45    LocalSocket::setupPath(path, name);
    4646}
    4747
     
    5050bool LocalAcceptor::accept()
    5151{
     52    if (failed()) return false;
    5253    if (accepted) return true;
    5354
    54     if (isWaited()) {
    55         DWORD size = 0;
    56         if (getResult(size, socket.handle)) {
    57             removeFromWaiter();
    58             accepted = true;
    59             return true;
    60         } else {
    61             if (socket.failed()) {
    62                 removeFromWaiter();
    63             }
    64             return false;
    65         }
     55    bool result = false;
     56    if (checkWaitedResult(result)) {
     57        return result;
    6658    }
    6759
    68 
    69     if (!socket.createNamedPipe(name)) {
     60    if (!socket.createNamedPipe(path)) {
    7061        return false;
    7162    }
    7263
    73     bool result = socket.connectNamedPipe(overlapped);
     64    result = socket.connectNamedPipe(overlapped);
    7465    if (result) {
    7566        accepted = true;
     
    8374//------------------------------------------------------------------------------
    8475
     76void LocalAcceptor::handleWaitedResult(DWORD /*size*/)
     77{
     78    accepted = true;
     79}
     80
     81//------------------------------------------------------------------------------
     82
    8583// Local Variables:
    8684// mode: C++
  • src/xplcommon/win32/LocalAcceptor.h

    r21 r22  
    5757
    5858    /**
    59      * The name of the pipe to accept.
     59     * The path of the pipe to accept.
    6060     */
    61     char name[256];
     61    char path[256];
    6262
    6363    /**
     
    103103
    104104
     105protected:
     106    /**
     107     * Set the indicator of an accepted connection to true.
     108     *
     109     * @see Completer::handleWaitedResult
     110     */
     111    virtual void handleWaitedResult(DWORD size);
     112
    105113private:
    106114    /**
     
    128136    if (!accepted) return 0;
    129137
    130     LocalSocket* s= new LocalSocket(waiter, socket.releaseHandle(),
    131                                     readingCapacity, writingCapacity);
     138    LocalSocket* s = new LocalSocket(waiter, socket.releaseHandle(),
     139                                     readingCapacity, writingCapacity);
    132140    accepted = false;
    133141    return s;
  • src/xplcommon/win32/LocalClientSocket.cc

    r21 r22  
    3838//------------------------------------------------------------------------------
    3939
    40 HANDLE LocalClientSocket::create(const char* name, bool overlapped)
    41 {
    42     char buf[256];
    43     setupName(buf, name);
    44 
    45     return CreateFile(buf, GENERIC_READ|GENERIC_WRITE,
    46                       0, 0, OPEN_EXISTING,
    47                       FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, 0);
    48 }
    49 
    50 //------------------------------------------------------------------------------
    51 
    5240// Local Variables:
    5341// mode: C++
  • src/xplcommon/win32/LocalClientSocket.h

    r21 r22  
    5151private:
    5252    /**
    53      * Create a named pipe for the given user-specified name.
    54      */
    55     static HANDLE create(const char* name, bool overlapped);
    56 
    57     /**
    5853     * The local connector.
    5954     */
  • src/xplcommon/win32/LocalConnector.cc

    r21 r22  
    4242bool LocalConnector::connect()
    4343{
    44     printf("LocalConnector::connect0\n");
    4544    if (connected) return true;
     45    if (failed()) return false;
    4646
    47     printf("LocalConnector::connect3\n");
    48 
    49     HANDLE handle = CreateFile(name, GENERIC_READ|GENERIC_WRITE,
    50                                0, 0, OPEN_EXISTING,
    51                                FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, 0);
    52     printf("LocalConnector::connect4\n");
    53     if (handle!=0) {
    54         printf("LocalConnector::connect5\n");
    55         socket.handle = handle;
     47    if (socket.connect(path)) {
    5648        connected = true;
    5749        return true;
    5850    } else {
    59         printf("LocalConnector::connect6\n");
    60         socket.setErrorCode(GetLastError());
    6151        return false;
    6252    }
  • src/xplcommon/win32/LocalConnector.h

    r21 r22  
    5252
    5353    /**
    54      * The name of the pipe to connect to.
     54     * The path of the pipe to connect to.
    5555     */
    56     char name[256];
     56    char path[256];
    5757
    5858    /**
     
    105105    connected(false)
    106106{
    107     LocalSocket::setupName(this->name, name);
     107    LocalSocket::setupPath(path, name);
    108108}
    109109
  • src/xplcommon/win32/LocalServerSocketBase.cc

    r21 r22  
    4040//------------------------------------------------------------------------------
    4141
    42 bool LocalServerSocketBase::createNamedPipe(const char* name)
     42bool LocalServerSocketBase::createNamedPipe(const char* path)
    4343{
    44     handle = CreateNamedPipe(name, PIPE_ACCESS_DUPLEX |
     44    handle = CreateNamedPipe(path, PIPE_ACCESS_DUPLEX |
    4545                             ((waiter==0) ? 0 : FILE_FLAG_OVERLAPPED),
    4646                             PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
     
    5959bool LocalServerSocketBase::connectNamedPipe(Overlapped* overlapped)
    6060{
     61    if (overlapped!=0) overlapped->reset();
    6162    bool result = LocalSocket::connectNamedPipe(handle, overlapped);
    6263    if (!result && overlapped==0) {
  • src/xplcommon/win32/LocalServerSocketBase.h

    r21 r22  
    6060     * this object will be set to the created handle.
    6161     *
    62      * @param name the name of the pipe to create. It should be the
    63      * final name, not the user-given one.
     62     * @param path the path of the pipe to create. It should be the
     63     * final path, not the user-given name.
    6464     *
    6565     * @return whether the pipe could be created
    6666     */
    67     bool createNamedPipe(const char* name);
     67    bool createNamedPipe(const char* path);
    6868
    6969    /**
  • src/xplcommon/win32/LocalSocket.h

    r21 r22  
    5151public:
    5252    /**
    53      * Create a concrete pipe name from the given user-specified name.
     53     * Create a concrete pipe path from the given user-specified name.
    5454     *
    5555     * @param dest the destination buffer, which should be at least
    5656     * 256 bytes long.
    5757     */
    58     static void setupName(char* dest, const char* name);
     58    static void setupPath(char* dest, const char* name);
    5959
    6060    /**
     
    9090protected:
    9191    /**
    92      * Connect the named pipe with the current handle of this object.
    93      *
    94      * @return whether the connection succeeded. If not, it may be a
    95      * failure, which is then indicated, or it may be an overlapped
    96      * operation which needs to wait.
     92     * Connect the client socket to the server.
    9793     */
    98     bool connect(Overlapped* overlapped);
     94    bool connect(const char* path);
    9995
    10096    friend class LocalAcceptor;
     
    106102//------------------------------------------------------------------------------
    107103
    108 inline void LocalSocket::setupName(char* dest, const char* name)
     104inline void LocalSocket::setupPath(char* dest, const char* name)
    109105{
    110106    snprintf(dest, 256, "\\\\.\\pipe\\%s", name);
     
    134130//------------------------------------------------------------------------------
    135131
    136 inline bool LocalSocket::connect(Overlapped* overlapped)
     132inline bool LocalSocket::connect(const char* path)
    137133{
    138     bool result = connectNamedPipe(handle, overlapped);
    139     if (!result && overlapped==0) {
     134    handle = CreateFile(path, GENERIC_READ|GENERIC_WRITE,
     135                        0, 0, OPEN_EXISTING,
     136                        FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, 0);
     137    if (handle==0) {
    140138        setErrorCode(GetLastError());
    141139    }
    142     return result;
     140
     141    return handle!=0;
    143142}
    144143
  • src/xplcommon/win32/Overlapped.cc

    r21 r22  
    4242    memset(&overlapped, 0, sizeof(overlapped));
    4343    overlapped.hEvent = getHandle();
     44    clear();
    4445}
    4546
     
    5253        DWORD error = GetLastError();
    5354        if (error!=ERROR_IO_PENDING && error!=ERROR_IO_INCOMPLETE) {
    54             reset();
    5555            setErrorCode(error);
    5656        }
    57     } else {
    58         reset();
    5957    }
    6058    return result;
  • src/xplcommon/win32/Overlapped.h

    r21 r22  
    6262
    6363    /**
    64      * Reset the overlapped structure and set its event handle.
     64     * Reset the overlapped structure and set its event handle. The
     65     * event is also cleared.
    6566     */
    6667    void reset();
     
    100101    Event(eventFailable)
    101102{
    102     reset();
    103103}
    104104
  • src/xplcommon/win32/ReadingBuffer.cc

    r21 r22  
    4848    }
    4949
    50     if (isWaited()) {
    51         DWORD size = 0;
    52         if (getResult(size, stream.handle)) {
    53             removeFromWaiter();
    54             overlapped->clear();
    55             addLength(size);
    56             return true;
    57         } else {
    58             if (stream.failed()) {
    59                 overlapped->clear();
    60                 removeFromWaiter();
    61             }
    62             return false;
    63         }
     50    bool result = false;
     51    if (checkWaitedResult(result)) {
     52        return result;
    6453    }
    6554
    66     ssize_t result = stream.read(getData(), getCapacity(), overlapped);
    67     if (result<0) {
     55    ssize_t numRead = stream.read(getData(), getCapacity(), overlapped);
     56    if (numRead<0) {
    6857        if (!stream.failed()) {
    6958            addTo(stream.getWaiter());
    70         } else {
    71             overlapped->reset();
    7259        }
    7360    } else {
    74         overlapped->reset();
    75         addLength(result);
     61        addLength(numRead);
    7662    }
    77     return result>=0;
     63    return numRead>=0;
     64}
     65
     66//------------------------------------------------------------------------------
     67
     68void ReadingBuffer::handleWaitedResult(DWORD size)
     69{
     70    addLength(size);
    7871}
    7972
  • src/xplcommon/win32/ReadingBuffer.h

    r21 r22  
    7777    bool read();
    7878
     79protected:
     80    /**
     81     * Set the buffer's length to the given value.
     82     *
     83     * @see Completer::handleWaitedResult
     84     */
     85    virtual void handleWaitedResult(DWORD size);
     86
    7987    friend class BufferedStream;
    8088};
  • src/xplcommon/win32/WritingBuffer.cc

    r21 r22  
    4646    }
    4747
    48     if (isWaited()) {
    49         DWORD size = 0;
    50         if (getResult(size, stream.handle)) {
    51             removeFromWaiter();
    52             overlapped->clear();
    53             reset();
    54             return true;
    55         } else {
    56             if (stream.failed()) {
    57                 removeFromWaiter();
    58                 overlapped->clear();
    59             }
    60             return false;
    61         }
     48    bool result;
     49    if (checkWaitedResult(result)) {
     50        return result;
    6251    }
    6352
    64     bool result = stream.write(getData(), getLength(), overlapped);
     53    result = stream.write(getData(), getLength(), overlapped);
    6554    if (result) {
    6655        reset();
     
    7463//------------------------------------------------------------------------------
    7564
     65void WritingBuffer::handleWaitedResult(DWORD /*size*/)
     66{
     67    reset();
     68}
     69
     70//------------------------------------------------------------------------------
     71
    7672// Local Variables:
    7773// mode: C++
  • src/xplcommon/win32/WritingBuffer.h

    r21 r22  
    7777    bool write();
    7878
     79protected:
     80    /**
     81     * Reset the buffer to signify that its contents have been written.
     82     *
     83     * @see Completer::handleWaitedResult
     84     */
     85    virtual void handleWaitedResult(DWORD size);
     86
    7987    friend class BufferedStream;
    8088};
Note: See TracChangeset for help on using the changeset viewer.