00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <stdlib.h>
00036 #include <stdio.h>
00037 #include <string.h>
00038 #include <assert.h>
00039
00040 #include <mps/mps.h>
00041
00042 #define WANT_PROTOCOL_DEBUG 0
00043
00044 #if WANT_PROTOCOL_DEBUG
00045 #define TPRINT() ({ fprintf(stderr, "TPRINT: %s: %s line %d\n", \
00046 __PRETTY_FUNCTION__, __FILE__, __LINE__); fflush(NULL); })
00047 #else
00048 #define TPRINT()
00049 #endif
00050
00051 namespace MPS {
00052
00053 int InputStream::readint() {
00054 TPRINT();
00055
00056 int result = 0;
00057 result = result | (unsigned char) read();
00058 result = (result << 8) | (unsigned char) read();
00059 result = (result << 8) | (unsigned char) read();
00060 result = (result << 8) | (unsigned char) read();
00061 return result;
00062 }
00063
00064 string InputStream::readstring() {
00065 TPRINT();
00066
00067 string result;
00068 char ch;
00069
00070 while (1) {
00071 ch = (char) read();
00072 if (ch == 0)
00073 break;
00074
00075 result.append(1, ch);
00076 }
00077
00078 return result;
00079 }
00080
00081 bool InputStream::readbool() {
00082 TPRINT();
00083
00084 return (read() != 0);
00085 }
00086
00087 long long InputStream::readlong() {
00088 TPRINT();
00089
00090 long long result = 0;
00091 result = result | (unsigned char) read();
00092 result = (result << 8) | (unsigned char) read();
00093 result = (result << 8) | (unsigned char) read();
00094 result = (result << 8) | (unsigned char) read();
00095 result = (result << 8) | (unsigned char) read();
00096 result = (result << 8) | (unsigned char) read();
00097 result = (result << 8) | (unsigned char) read();
00098 result = (result << 8) | (unsigned char) read();
00099 return result;
00100 }
00101
00102 char InputStream::readchar() {
00103 TPRINT();
00104
00105 return (char) read();
00106 }
00107
00108 string InputStream::readReference() {
00109 TPRINT();
00110
00111 return readstring();
00112 }
00113
00114 float InputStream::readfloat() {
00115 TPRINT();
00116
00117 assert(sizeof(int) == sizeof(float));
00118 int intBits = readint();
00119 return * (float *) &intBits;
00120 }
00121
00122 void OutputStream::writeint(int i) {
00123 TPRINT();
00124
00125 write((i >> 24) & 255);
00126 write((i >> 16) & 255);
00127 write((i >> 8) & 255);
00128 write(i & 255);
00129 }
00130
00131 void OutputStream::writestring(string const &s) {
00132 TPRINT();
00133
00134 for (unsigned int i = 0; i < s.size(); i++) {
00135 char c = s[i];
00136 if (c == 0)
00137 break;
00138 write(c);
00139 }
00140 write(0);
00141 }
00142
00143 void OutputStream::writebool(bool b) {
00144 TPRINT();
00145
00146 write(b ? 1 : 0);
00147 }
00148
00149 void OutputStream::writelong(long long i) {
00150 TPRINT();
00151
00152 write((int) (i >> 56) & 255);
00153 write((int) (i >> 48) & 255);
00154 write((int) (i >> 40) & 255);
00155 write((int) (i >> 32) & 255);
00156 write((int) (i >> 24) & 255);
00157 write((int) (i >> 16) & 255);
00158 write((int) (i >> 8) & 255);
00159 write((int) i & 255);
00160 }
00161
00162 void OutputStream::writechar(char ch) {
00163 TPRINT();
00164
00165 write(ch);
00166 }
00167
00168 void OutputStream::writeReference(ref<Interface> const &what) {
00169 TPRINT();
00170
00171 InterfaceServer *server = what->_getInterfaceServer();
00172 string boundName = "";
00173
00174 if (server == 0) {
00175
00176
00177
00178 boundName = what->_getRemoteAddress();
00179 } else {
00180 boundName = server->getBoundName(referenceTransport());
00181 if (boundName.empty()) {
00182
00183
00184 boundName = server->getBoundName("inet");
00185 }
00186 }
00187
00188 if (boundName.empty()) {
00189 char buf[256];
00190 sprintf(buf, "Interface %p has no bound name for transport '%s'",
00191 (void *) what.get(),
00192 referenceTransport().c_str());
00193 throw MPSException(buf);
00194 }
00195
00196 writestring(boundName);
00197 }
00198
00199 void OutputStream::writefloat(float f) {
00200 TPRINT();
00201
00202 assert(sizeof(float) == sizeof(int));
00203 writeint(* (int *) &f);
00204 }
00205
00206 }