servemsg.cc

Go to the documentation of this file.
00001 
00008 /*
00009 Copyright (C) 2000-2002 The Exult Team
00010 
00011 This program is free software; you can redistribute it and/or
00012 modify it under the terms of the GNU General Public License
00013 as published by the Free Software Foundation; either version 2
00014 of the License, or (at your option) any later version.
00015 
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 GNU General Public License for more details.
00020 
00021 You should have received a copy of the GNU General Public License
00022 along with this program; if not, write to the Free Software
00023 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00024 */
00025 
00026 #ifdef HAVE_CONFIG_H
00027 #  include <config.h>
00028 #endif
00029 
00030 #include <unistd.h>
00031 #include <iostream>     /* For debugging msgs. */
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  *  Send data.
00050  *
00051  *  Output: -1 if error.
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;    // Store magic (low-byte first).
00065   buf[1] = (magic>>8)&0xff;
00066   buf[2] = datalen&0xff;    // Data length.
00067   buf[3] = (datalen>>8)&0xff;
00068   buf[4] = id;
00069   if (datalen > 0)
00070     std::memcpy(&buf[5], data, datalen);  // The data itself.
00071   int len = datalen + hdrlength;
00072 
00073   return (write(socket, buf, len) == len ? 0 : -1);
00074 #else  /* USE_EXULTSTUDIO */
00075   return -1;
00076 #endif  /* USE_EXULTSTUDIO */
00077   }
00078 
00079 /*
00080  *  Read message from client.
00081  *
00082  *  Output: Length of data, else -1.
00083  */
00084 
00085 int Receive_data
00086   (
00087   int& socket,      // Closed, set to -1 if disconnected.
00088   Msg_type& id,     // ID returned.
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); // Get magic.
00096   if (!len)     // Closed?
00097     {
00098     close(socket);
00099     socket = -1;
00100     return -1;
00101     }
00102   if (len == -1)      // Nothing available?
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           // Message type.
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     //+++++++++Eat the chars.
00122     return -1;
00123     }
00124   datalen = read(socket, data, dlen); // Read data.
00125 
00126   if (datalen < dlen)
00127     {
00128     cout << "Failed to read all " << dlen << " bytes" << endl;
00129     return -1;
00130     }
00131   return datalen;
00132 #else  /* USE_EXULTSTUDIO */
00133   return -1;
00134 #endif  /* USE_EXULTSTUDIO */
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 

Generated on Mon Jul 9 14:42:50 2007 for ExultEngine by  doxygen 1.5.1