# 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 "packet.h"
    00019
    00020
    00021 #ifdef __MORPHOS__
    00022 /* the library base is required here */
    00023 struct Library *SocketBase = NULL;
    00024 #endif
    00025
    00030 bool NetworkCoreInitialize()
    00031 {
    00032 #if defined(__MORPHOS__) || defined(__AMIGA__)
    00033   /*
    00034    *  IMPORTANT NOTE: SocketBase needs to be initialized before we use _any_
    00035    *  network related function, else: crash.
    00036    */
    00037   DEBUG(net, 3, "[core] loading bsd socket library");
    00038   SocketBase = OpenLibrary("bsdsocket.library", 4);
    00039   if (SocketBase == NULL) {
    00040     DEBUG(net, 0, "[core] can't open bsdsocket.library version 4, network unavailable");
    00041     return false;
    00042   }
    00043
    00044 #if defined(__AMIGA__)
    00045   /* for usleep() implementation (only required for legacy AmigaOS builds) */
    00046   TimerPort = CreateMsgPort();
    00047   if (TimerPort != NULL) {
    00048     TimerRequest = (struct timerequest*)CreateIORequest(TimerPort, sizeof(struct timerequest);
    00049     if (TimerRequest != NULL) {
    00050       if (OpenDevice("timer.device", UNIT_MICROHZ, (struct IORequest*)TimerRequest, 0) == 0) {
    00051         TimerBase = TimerRequest->tr_node.io_Device;
    00052         if (TimerBase == NULL) {
    00053           /* free ressources... */
    00054           DEBUG(net, 0, "[core] can't initialize timer, network unavailable");
    00055           return false;
    00056         }
    00057       }
    00058     }
    00059   }
    00060 #endif /* __AMIGA__ */
    00061 #endif /* __MORPHOS__ / __AMIGA__ */
    00062
    00063 /* Let's load the network in windows */
    00064 #ifdef WIN32
    00065   {
    00066     WSADATA wsa;
    00067     DEBUG(net, 3, "[core] loading windows socket library");
    00068     if (WSAStartup(MAKEWORD(2, 0), &wsa) != 0) {
    00069       DEBUG(net, 0, "[core] WSAStartup failed, network unavailable");
    00070       return false;
    00071     }
    00072   }
    00073 #endif /* WIN32 */
    00074
    00075   return true;
    00076 }
    00077
    00081 void NetworkCoreShutdown()
    00082 {
    00083 #if defined(__MORPHOS__) || defined(__AMIGA__)
    00084   /* free allocated resources */
    00085 #if defined(__AMIGA__)
    00086   if (TimerBase    != NULL) CloseDevice((struct IORequest*)TimerRequest); // XXX This smells wrong
    00087   if (TimerRequest != NULL) DeleteIORequest(TimerRequest);
    00088   if (TimerPort    != NULL) DeleteMsgPort(TimerPort);
    00089 #endif
    00090
    00091   if (SocketBase != NULL) CloseLibrary(SocketBase);
    00092 #endif
    00093
    00094 #if defined(WIN32)
    00095   WSACleanup();
    00096 #endif
    00097 }
    00098
    00099
    00105 void NetworkSocketHandler::Send_GRFIdentifier(Packet *p, const GRFIdentifier *grf)
    00106 {
    00107   uint j;
    00108   p->Send_uint32(grf->grfid);
    00109   for (j = 0; j < sizeof(grf->md5sum); j++) {
    00110     p->Send_uint8 (grf->md5sum[j]);
    00111   }
    00112 }
    00113
    00119 void NetworkSocketHandler::Recv_GRFIdentifier(Packet *p, GRFIdentifier *grf)
    00120 {
    00121   uint j;
    00122   grf->grfid = p->Recv_uint32();
    00123   for (j = 0; j < sizeof(grf->md5sum); j++) {
    00124     grf->md5sum[j] = p->Recv_uint8();
    00125   }
    00126 }
    00127
    00128 #endif /* ENABLE_NETWORK */