00001
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030 #include <unistd.h>
00031 #include <iostream>
00032 #include "servemsg.h"
00033 #ifndef ALPHA_LINUX_CXX
00034 #include <cstring>
00035 #endif
00036
00037 #ifdef WIN32
00038 #include "servewin32.h"
00039 #endif
00040
00041 using std::cout;
00042 using std::cerr;
00043 using std::endl;
00044
00045 namespace Exult_server
00046 {
00047
00048
00049
00050
00051
00052
00053
00054 int Send_data
00055 (
00056 int socket,
00057 Msg_type id,
00058 unsigned char *data,
00059 int datalen
00060 )
00061 {
00062 #ifdef USE_EXULTSTUDIO
00063 unsigned char buf[maxlength + hdrlength];
00064 buf[0] = magic&0xff;
00065 buf[1] = (magic>>8)&0xff;
00066 buf[2] = datalen&0xff;
00067 buf[3] = (datalen>>8)&0xff;
00068 buf[4] = id;
00069 if (datalen > 0)
00070 std::memcpy(&buf[5], data, datalen);
00071 int len = datalen + hdrlength;
00072
00073 return (write(socket, buf, len) == len ? 0 : -1);
00074 #else
00075 return -1;
00076 #endif
00077 }
00078
00079
00080
00081
00082
00083
00084
00085 int Receive_data
00086 (
00087 int& socket,
00088 Msg_type& id,
00089 unsigned char *data,
00090 int datalen
00091 )
00092 {
00093 #ifdef USE_EXULTSTUDIO
00094 unsigned char buf[hdrlength];
00095 int len = read(socket, buf, 2);
00096 if (!len)
00097 {
00098 close(socket);
00099 socket = -1;
00100 return -1;
00101 }
00102 if (len == -1)
00103 return -1;
00104 int magic = buf[0] + (buf[1]<<8);
00105 if (magic != Exult_server::magic)
00106 {
00107 cout << "Bad magic read" << endl;
00108 return -1;
00109 }
00110 if (read(socket, buf, 3) != 3)
00111 {
00112 cout << "Couldn't read length+type" << endl;
00113 return -1;
00114 }
00115 int dlen = buf[0] | (buf[1]<<8);
00116
00117 id = (Exult_server::Msg_type) buf[2];
00118 if (dlen > Exult_server::maxlength || dlen > datalen)
00119 {
00120 cout << "Length " << datalen << " exceeds max" << endl;
00121
00122 return -1;
00123 }
00124 datalen = read(socket, data, dlen);
00125
00126 if (datalen < dlen)
00127 {
00128 cout << "Failed to read all " << dlen << " bytes" << endl;
00129 return -1;
00130 }
00131 return datalen;
00132 #else
00133 return -1;
00134 #endif
00135 }
00136
00137
00138
00139 bool wait_for_response(int socket, int ms)
00140 {
00141 #if defined(WIN32) && defined(USE_EXULTSTUDIO)
00142 int ticks = GetTickCount();
00143 while(GetTickCount() < ticks+ms) {
00144 if (peek_pipe() > 0) return true;
00145 SleepEx(1, TRUE);
00146 }
00147 if (peek_pipe() > 0) return true;
00148 return false;
00149 #endif
00150 return true;
00151 }
00152
00153 }
00154