# core.cpp

Go to the documentation of this file.

    00001 /* $Id: core.cpp 9050 2007-03-07 11:47:46Z rubidium $ */
    00002
    00007 #ifdef ENABLE_NETWORK
    00008
    00009 #include "../../stdafx.h"
    00010 #include "../../debug.h"
    00011 #include "os_abstraction.h"
    00012 #include "core.h"
    00013 #include "packet.h"
    00014
    00015 #ifdef __MORPHOS__
    00016 /* the library base is required here */
    00017 struct Library *SocketBase = NULL;
    00018 #endif
    00019
    00024 bool NetworkCoreInitialize()
    00025 {
    00026 #if defined(__MORPHOS__) || defined(__AMIGA__)
    00027   /*
    00028    *  IMPORTANT NOTE: SocketBase needs to be initialized before we use _any_
    00029    *  network related function, else: crash.
    00030    */
    00031   DEBUG(net, 3, "[core] loading bsd socket library");
    00032   SocketBase = OpenLibrary("bsdsocket.library", 4);
    00033   if (SocketBase == NULL) {
    00034     DEBUG(net, 0, "[core] can't open bsdsocket.library version 4, network unavailable");
    00035     return false;
    00036   }
    00037
    00038 #if defined(__AMIGA__)
    00039   /* for usleep() implementation (only required for legacy AmigaOS builds) */
    00040   TimerPort = CreateMsgPort();
    00041   if (TimerPort != NULL) {
    00042     TimerRequest = (struct timerequest*)CreateIORequest(TimerPort, sizeof(struct timerequest);
    00043     if (TimerRequest != NULL) {
    00044       if (OpenDevice("timer.device", UNIT_MICROHZ, (struct IORequest*)TimerRequest, 0) == 0) {
    00045         TimerBase = TimerRequest->tr_node.io_Device;
    00046         if (TimerBase == NULL) {
    00047           /* free ressources... */
    00048           DEBUG(net, 0, "[core] can't initialize timer, network unavailable");
    00049           return false;
    00050         }
    00051       }
    00052     }
    00053   }
    00054 #endif // __AMIGA__
    00055 #endif // __MORPHOS__ / __AMIGA__
    00056
    00057 /* Let's load the network in windows */
    00058 #ifdef WIN32
    00059   {
    00060     WSADATA wsa;
    00061     DEBUG(net, 3, "[core] loading windows socket library");
    00062     if (WSAStartup(MAKEWORD(2, 0), &wsa) != 0) {
    00063       DEBUG(net, 0, "[core] WSAStartup failed, network unavailable");
    00064       return false;
    00065     }
    00066   }
    00067 #endif /* WIN32 */
    00068
    00069   return true;
    00070 }
    00071
    00075 void NetworkCoreShutdown()
    00076 {
    00077 #if defined(__MORPHOS__) || defined(__AMIGA__)
    00078   /* free allocated resources */
    00079 #if defined(__AMIGA__)
    00080   if (TimerBase    != NULL) CloseDevice((struct IORequest*)TimerRequest); // XXX This smells wrong
    00081   if (TimerRequest != NULL) DeleteIORequest(TimerRequest);
    00082   if (TimerPort    != NULL) DeleteMsgPort(TimerPort);
    00083 #endif
    00084
    00085   if (SocketBase != NULL) CloseLibrary(SocketBase);
    00086 #endif
    00087
    00088 #if defined(WIN32)
    00089   WSACleanup();
    00090 #endif
    00091 }
    00092
    00093
    00099 void NetworkSocketHandler::Send_GRFIdentifier(Packet *p, const GRFIdentifier *grf)
    00100 {
    00101   uint j;
    00102   p->Send_uint32(grf->grfid);
    00103   for (j = 0; j < sizeof(grf->md5sum); j++) {
    00104     p->Send_uint8 (grf->md5sum[j]);
    00105   }
    00106 }
    00107
    00113 void NetworkSocketHandler::Recv_GRFIdentifier(Packet *p, GRFIdentifier *grf)
    00114 {
    00115   uint j;
    00116   grf->grfid = p->Recv_uint32();
    00117   for (j = 0; j < sizeof(grf->md5sum); j++) {
    00118     grf->md5sum[j] = p->Recv_uint8();
    00119   }
    00120 }
    00121
    00122 #endif /* ENABLE_NETWORK */