~ubuntu-branches/ubuntu/lucid/codelite/lucid

« back to all changes in this revision

Viewing changes to CodeLite/clindexerprotocol.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-02-10 02:27:55 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090210022755-m5692nfc1t5uf1w9
Tags: 1.0.2759+dfsg-0ubuntu1
* New upstream release (LP: #327216).
* debian/patches/series, debian/patches/00_fix-ia64-build.patch:
  + Dropped, applied upstream already.
* debian/patches/02_fix-desktop.patch,
  debian/patches/03_fix-sh.patch:
  + Refreshed to patch cleanly.
* debian/rules:
  + Make get-orig-source honour UPSTREAM_VERSION if set.
* debian/ctags-le.1,
  debian/codelite_indexer.1,
  debian/codelite.manpages:
  + Dropped ctags-le manpage, since ctags-le was replaced by
    codelite_indexer.
  + Added codelite_indexer manpage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "clindexerprotocol.h"
 
2
#include <memory>
 
3
 
 
4
#define ACK_MAGIC 1975
 
5
 
 
6
clIndexerProtocol::clIndexerProtocol()
 
7
{
 
8
}
 
9
 
 
10
clIndexerProtocol::~clIndexerProtocol()
 
11
{
 
12
}
 
13
 
 
14
bool clIndexerProtocol::ReadReply(clNamedPipe* conn, clIndexerReply& reply)
 
15
{
 
16
        std::auto_ptr<char> sp;
 
17
 
 
18
        // first we read sizeof(size_t) to get the actual data size
 
19
        size_t buff_len(0);
 
20
        size_t actual_read(0);
 
21
 
 
22
        if ( !conn->read((void*)&buff_len, sizeof(buff_len), &actual_read, 10000) ) {
 
23
                fprintf(stderr, "ERROR: ReadReply: Failed to read from the pipe, reason: %d\n", conn->getLastError());
 
24
                return false;
 
25
        }
 
26
 
 
27
        if (actual_read != sizeof(buff_len)) {
 
28
                fprintf(stderr, "ERROR: ReadReply: Protocol error: expected %d bytes, got %d. reason: %d\n",
 
29
                                sizeof(buff_len),
 
30
                                actual_read,
 
31
                                conn->getLastError());
 
32
                return false;
 
33
        }
 
34
 
 
35
        char *data = new char[buff_len];
 
36
        sp.reset(data);
 
37
 
 
38
        int bytes_left(buff_len);
 
39
        size_t bytes_read(0);
 
40
        while (bytes_left > 0) {
 
41
                if ( !conn->read(data+bytes_read, bytes_left, &actual_read, 10000) ) {
 
42
                        fprintf(stderr, "ERROR: Protocol error: expected %d bytes, got %d\n", buff_len, actual_read);
 
43
                        return false;
 
44
                }
 
45
                bytes_left -= actual_read;
 
46
                bytes_read += actual_read;
 
47
        }
 
48
 
 
49
        reply.fromBinary(data);
 
50
 
 
51
//#ifndef __WXMSW__
 
52
//      // send confirmation to the to server that we got data
 
53
//      // and it can close the connection
 
54
//      size_t ack(ACK_MAGIC);
 
55
//      conn->write(&ack, sizeof(ack), &actual_read, -1);
 
56
//#endif
 
57
        return true;
 
58
}
 
59
 
 
60
bool clIndexerProtocol::ReadRequest(clNamedPipe* conn, clIndexerRequest& req)
 
61
{
 
62
        std::auto_ptr<char> sp;
 
63
 
 
64
        // first we read sizeof(size_t) to get the actual data size
 
65
        size_t buff_len(0);
 
66
        size_t actual_read(0);
 
67
 
 
68
        if ( !conn->read((void*)&buff_len, sizeof(buff_len), &actual_read, -1) ) {
 
69
                fprintf(stderr, "ERROR: Failed to read from the pipe, reason: %d\n", conn->getLastError());
 
70
                return false;
 
71
        }
 
72
 
 
73
        if (actual_read != sizeof(buff_len)) {
 
74
                fprintf(stderr, "ERROR: Protocol error: expected %d bytes, got %d\n", sizeof(buff_len), actual_read);
 
75
                return false;
 
76
        }
 
77
 
 
78
        char *data = new char[buff_len];
 
79
        sp.reset(data);
 
80
 
 
81
        int bytes_left(buff_len);
 
82
        size_t bytes_read(0);
 
83
        while (bytes_left > 0) {
 
84
                if ( !conn->read(data+bytes_read, bytes_left, &actual_read, -1) ) {
 
85
                        fprintf(stderr, "ERROR: [%s] Protocol error: expected %d bytes, got %d\n", __PRETTY_FUNCTION__, buff_len, actual_read);
 
86
                        return false;
 
87
                }
 
88
                bytes_left -= actual_read;
 
89
                bytes_read += actual_read;
 
90
        }
 
91
 
 
92
        req.fromBinary(data);
 
93
        return true;
 
94
}
 
95
 
 
96
bool clIndexerProtocol::SendReply(clNamedPipe* conn, clIndexerReply& reply)
 
97
{
 
98
        size_t buff_size(0);
 
99
        char *data = reply.toBinary(buff_size);
 
100
        std::auto_ptr<char> sp(data);
 
101
 
 
102
        // send the reply size
 
103
        size_t written(0);
 
104
        conn->write((void*)&buff_size, sizeof(buff_size), &written, -1);
 
105
 
 
106
 
 
107
        int bytes_left(buff_size);
 
108
        int bytes_to_write(0);
 
109
        int bytes_written(0);
 
110
 
 
111
        while (bytes_left > 0) {
 
112
                // we write in chunks of 3000 bytes
 
113
                if (bytes_left < 3000) {
 
114
                        bytes_to_write = bytes_left;
 
115
                } else {
 
116
                        bytes_to_write = 3000;
 
117
                }
 
118
 
 
119
                size_t actual_written(0);
 
120
                if ( !conn->write(data+bytes_written, bytes_to_write, &actual_written, -1) ) {
 
121
                        return false;
 
122
                }
 
123
 
 
124
                bytes_left -= actual_written;
 
125
                bytes_written += actual_written;
 
126
        }
 
127
//#ifndef __WXMSW__
 
128
//      // to make sure that the message has been sent, we wait for the acknoldegment from the client
 
129
//      size_t ack(0);
 
130
//      size_t rr;
 
131
//      conn->read(&ack, sizeof(ack), &rr, -1);
 
132
//      if (ack == ACK_MAGIC) {
 
133
//              // we are OK
 
134
//              printf("INFO: Got ACK!\n");
 
135
//              return true;
 
136
//      } else {
 
137
//              printf("ERROR: Did not got the ack!\n");
 
138
//              return false;
 
139
//      }
 
140
//#else
 
141
        // the above problem does not exist under Windows' NamedPipes
 
142
        return true;
 
143
//#endif
 
144
}
 
145
 
 
146
bool clIndexerProtocol::SendRequest(clNamedPipe* conn, clIndexerRequest& req)
 
147
{
 
148
        size_t size(0);
 
149
        size_t written(0);
 
150
 
 
151
        char *data = req.toBinary(size);
 
152
        std::auto_ptr<char> sp(data);
 
153
 
 
154
        // write request
 
155
        if (!conn->write((void*)&size, sizeof(size), &written, -1)) {
 
156
                printf("ERROR: [%s] protocol error: rc %d\n", __PRETTY_FUNCTION__, conn->getLastError());
 
157
                return false;
 
158
        }
 
159
 
 
160
        int bytes_left(size);
 
161
        int bytes_to_write(0);
 
162
        int bytes_written(0);
 
163
 
 
164
        while (bytes_left > 0) {
 
165
                // we write in chunks of 3000 bytes
 
166
                if (bytes_left < 3000) {
 
167
                        bytes_to_write = bytes_left;
 
168
                } else {
 
169
                        bytes_to_write = 3000;
 
170
                }
 
171
 
 
172
                size_t actual_written(0);
 
173
                if ( !conn->write(data+bytes_written, bytes_to_write, &actual_written, -1) ) {
 
174
                        return false;
 
175
                }
 
176
 
 
177
                bytes_left -= actual_written;
 
178
                bytes_written += actual_written;
 
179
        }
 
180
        return true;
 
181
}