~ubuntu-branches/ubuntu/trusty/soprano/trusty

« back to all changes in this revision

Viewing changes to client/socket.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2013-01-02 13:02:08 UTC
  • mfrom: (1.1.43)
  • Revision ID: package-import@ubuntu.com-20130102130208-25etkuo00kh8cb5w
Tags: 2.9.0+dfsg.1-0ubuntu1
* New upstream release
  - Update symbols/install files
* Make DFSG compliant

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * This file is part of Soprano Project.
3
3
 *
4
4
 * Copyright (C) 2010-2012 Sebastian Trueg <trueg@kde.org>
 
5
 * Copyright (C) 2012      Vishesh Handa <me@vhanda.in>
5
6
 *
6
7
 * This library is free software; you can redistribute it and/or
7
8
 * modify it under the terms of the GNU Library General Public
84
85
        tv.tv_usec = (timeout % 1000) * 1000;
85
86
 
86
87
        int r = ::select( m_handle + 1, &fds, 0, 0, timeout < 0 ? 0 : &tv);
 
88
        if ( r == -1 ) {
 
89
            if ( errno == EINTR /* Interrupted system call */ )
 
90
                return waitForReadyRead( timeout );
 
91
        }
 
92
 
87
93
        return r > 0;
88
94
    }
89
95
    else {
92
98
}
93
99
 
94
100
 
95
 
qint64 Soprano::Socket::read( char* buffer, qint64 max )
 
101
qint64 Soprano::Socket::read( char* buffer, qint64 size )
96
102
{
97
 
    qint64 r = ::read( m_handle, buffer, max );
98
 
    if ( r < 0 ) {
99
 
        setError( QString::fromLatin1( "Failed to read from fd %1 (%2)" ).arg( m_handle ).arg( QLatin1String( strerror( errno ) ) ) );
 
103
    int total = 0;
 
104
    while ( size > 0 ) {
 
105
        int bytesRead = ::read( m_handle, buffer, size );
 
106
        if( bytesRead == -1 ) {
 
107
            if (errno == EINTR) {
 
108
                continue;
 
109
            }
 
110
            else {
 
111
                QString error = QString::fromLatin1( "Failed to read from fd %1 (%2)")
 
112
                                .arg( m_handle )
 
113
                                .arg( QLatin1String( strerror( errno) ) );
 
114
                setError( error );
 
115
                return -1;
 
116
            }
 
117
        }
 
118
        else if( bytesRead == 0 ) {
 
119
            QString error = QString::fromLatin1( "Timeout after reading %1 of %2 bytes" )
 
120
                            .arg( total ).arg( total + size );
 
121
            setError( error );
 
122
            break;
 
123
        }
 
124
 
 
125
        buffer += bytesRead;
 
126
        total += bytesRead;
 
127
        size -= bytesRead;
100
128
    }
101
 
    return r;
 
129
 
 
130
    return total;
102
131
}
103
132
 
104
133
 
105
 
qint64 Soprano::Socket::write( const char* buffer, qint64 max )
 
134
qint64 Soprano::Socket::write( const char* buffer, qint64 size )
106
135
{
107
 
    qint64 r = ::write( m_handle, buffer, max );
108
 
    if ( r < 0 ) {
109
 
        setError( QString::fromLatin1( "Failed to write to fd %1 (%2)" ).arg( m_handle ).arg( QLatin1String( strerror( errno ) ) ) );
 
136
    int total = 0;
 
137
    while ( size > 0 ) {
 
138
        int written = ::write( m_handle, buffer, size );
 
139
        if (written == -1) {
 
140
            if (errno == EINTR) {
 
141
                continue;
 
142
            }
 
143
            else {
 
144
                QString error = QString::fromLatin1( "Failed to write fd %1 (%2)")
 
145
                                .arg( m_handle )
 
146
                                .arg( QLatin1String( strerror( errno) ) );
 
147
                setError( error );
 
148
                return -1;
 
149
            }
 
150
        }
 
151
        else if( written == 0 ) {
 
152
            QString error = QString::fromLatin1( "Timeout after writing %1 of %2 bytes" )
 
153
                            .arg( total ).arg( total + size );
 
154
            setError( error );
 
155
            break;
 
156
        }
 
157
 
 
158
        buffer += written;
 
159
        total += written;
 
160
        size -= written;
110
161
    }
111
 
    return r;
 
162
 
 
163
    return total;
112
164
}
113
165
 
114
166
 
168
220
    if ( ::connect( m_handle, (struct sockaddr *)&servAddr, sizeof( servAddr ) ) < 0 ) {
169
221
        setError( QString::fromLatin1( "Could not connect to server at %1 (%2)" ).arg( m_path ).arg( strerror(errno) ) );
170
222
        ::close( m_handle );
 
223
        m_handle = -1;
171
224
        return false;
172
225
    }
173
226