core.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00014 #ifdef ENABLE_NETWORK
00015 
00016 #include "../../stdafx.h"
00017 #include "../../debug.h"
00018 #include "os_abstraction.h"
00019 #include "packet.h"
00020 
00021 
00022 #ifdef __MORPHOS__
00023 /* the library base is required here */
00024 struct Library *SocketBase = NULL;
00025 #endif
00026 
00031 bool NetworkCoreInitialize()
00032 {
00033 #if defined(__MORPHOS__) || defined(__AMIGA__)
00034   /*
00035    *  IMPORTANT NOTE: SocketBase needs to be initialized before we use _any_
00036    *  network related function, else: crash.
00037    */
00038   DEBUG(net, 3, "[core] loading bsd socket library");
00039   SocketBase = OpenLibrary("bsdsocket.library", 4);
00040   if (SocketBase == NULL) {
00041     DEBUG(net, 0, "[core] can't open bsdsocket.library version 4, network unavailable");
00042     return false;
00043   }
00044 
00045 #if defined(__AMIGA__)
00046   /* for usleep() implementation (only required for legacy AmigaOS builds) */
00047   TimerPort = CreateMsgPort();
00048   if (TimerPort != NULL) {
00049     TimerRequest = (struct timerequest*)CreateIORequest(TimerPort, sizeof(struct timerequest);
00050     if (TimerRequest != NULL) {
00051       if (OpenDevice("timer.device", UNIT_MICROHZ, (struct IORequest*)TimerRequest, 0) == 0) {
00052         TimerBase = TimerRequest->tr_node.io_Device;
00053         if (TimerBase == NULL) {
00054           /* free resources... */
00055           DEBUG(net, 0, "[core] can't initialize timer, network unavailable");
00056           return false;
00057         }
00058       }
00059     }
00060   }
00061 #endif /* __AMIGA__ */
00062 #endif /* __MORPHOS__ / __AMIGA__ */
00063 
00064 /* Let's load the network in windows */
00065 #ifdef WIN32
00066   {
00067     WSADATA wsa;
00068     DEBUG(net, 3, "[core] loading windows socket library");
00069     if (WSAStartup(MAKEWORD(2, 0), &wsa) != 0) {
00070       DEBUG(net, 0, "[core] WSAStartup failed, network unavailable");
00071       return false;
00072     }
00073   }
00074 #endif /* WIN32 */
00075 
00076   return true;
00077 }
00078 
00082 void NetworkCoreShutdown()
00083 {
00084 #if defined(__MORPHOS__) || defined(__AMIGA__)
00085   /* free allocated resources */
00086 #if defined(__AMIGA__)
00087   if (TimerBase    != NULL) CloseDevice((struct IORequest*)TimerRequest); // XXX This smells wrong
00088   if (TimerRequest != NULL) DeleteIORequest(TimerRequest);
00089   if (TimerPort    != NULL) DeleteMsgPort(TimerPort);
00090 #endif
00091 
00092   if (SocketBase != NULL) CloseLibrary(SocketBase);
00093 #endif
00094 
00095 #if defined(WIN32)
00096   WSACleanup();
00097 #endif
00098 }
00099 
00100 
00106 void NetworkSocketHandler::SendGRFIdentifier(Packet *p, const GRFIdentifier *grf)
00107 {
00108   uint j;
00109   p->Send_uint32(grf->grfid);
00110   for (j = 0; j < sizeof(grf->md5sum); j++) {
00111     p->Send_uint8 (grf->md5sum[j]);
00112   }
00113 }
00114 
00120 void NetworkSocketHandler::ReceiveGRFIdentifier(Packet *p, GRFIdentifier *grf)
00121 {
00122   uint j;
00123   grf->grfid = p->Recv_uint32();
00124   for (j = 0; j < sizeof(grf->md5sum); j++) {
00125     grf->md5sum[j] = p->Recv_uint8();
00126   }
00127 }
00128 
00129 #endif /* ENABLE_NETWORK */