OpenTTD
core.cpp
Go to the documentation of this file.
1 /* $Id: core.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
14 #ifdef ENABLE_NETWORK
15 
16 #include "../../stdafx.h"
17 #include "../../debug.h"
18 #include "os_abstraction.h"
19 #include "packet.h"
20 
21 #include "../../safeguards.h"
22 
23 
24 #ifdef __MORPHOS__
25 /* the library base is required here */
26 struct Library *SocketBase = NULL;
27 #endif
28 
34 {
35 #if defined(__MORPHOS__) || defined(__AMIGA__)
36  /*
37  * IMPORTANT NOTE: SocketBase needs to be initialized before we use _any_
38  * network related function, else: crash.
39  */
40  DEBUG(net, 3, "[core] loading bsd socket library");
41  SocketBase = OpenLibrary("bsdsocket.library", 4);
42  if (SocketBase == NULL) {
43  DEBUG(net, 0, "[core] can't open bsdsocket.library version 4, network unavailable");
44  return false;
45  }
46 
47 #if defined(__AMIGA__)
48  /* for usleep() implementation (only required for legacy AmigaOS builds) */
49  TimerPort = CreateMsgPort();
50  if (TimerPort != NULL) {
51  TimerRequest = (struct timerequest*)CreateIORequest(TimerPort, sizeof(struct timerequest);
52  if (TimerRequest != NULL) {
53  if (OpenDevice("timer.device", UNIT_MICROHZ, (struct IORequest*)TimerRequest, 0) == 0) {
54  TimerBase = TimerRequest->tr_node.io_Device;
55  if (TimerBase == NULL) {
56  /* free resources... */
57  DEBUG(net, 0, "[core] can't initialize timer, network unavailable");
58  return false;
59  }
60  }
61  }
62  }
63 #endif /* __AMIGA__ */
64 #endif /* __MORPHOS__ / __AMIGA__ */
65 
66 /* Let's load the network in windows */
67 #ifdef WIN32
68  {
69  WSADATA wsa;
70  DEBUG(net, 3, "[core] loading windows socket library");
71  if (WSAStartup(MAKEWORD(2, 0), &wsa) != 0) {
72  DEBUG(net, 0, "[core] WSAStartup failed, network unavailable");
73  return false;
74  }
75  }
76 #endif /* WIN32 */
77 
78  return true;
79 }
80 
85 {
86 #if defined(__MORPHOS__) || defined(__AMIGA__)
87  /* free allocated resources */
88 #if defined(__AMIGA__)
89  if (TimerBase != NULL) CloseDevice((struct IORequest*)TimerRequest); // XXX This smells wrong
90  if (TimerRequest != NULL) DeleteIORequest(TimerRequest);
91  if (TimerPort != NULL) DeleteMsgPort(TimerPort);
92 #endif
93 
94  if (SocketBase != NULL) CloseLibrary(SocketBase);
95 #endif
96 
97 #if defined(WIN32)
98  WSACleanup();
99 #endif
100 }
101 
102 
109 {
110  uint j;
111  p->Send_uint32(grf->grfid);
112  for (j = 0; j < sizeof(grf->md5sum); j++) {
113  p->Send_uint8 (grf->md5sum[j]);
114  }
115 }
116 
123 {
124  uint j;
125  grf->grfid = p->Recv_uint32();
126  for (j = 0; j < sizeof(grf->md5sum); j++) {
127  grf->md5sum[j] = p->Recv_uint8();
128  }
129 }
130 
131 #endif /* ENABLE_NETWORK */
Internal entity of a packet.
Definition: packet.h:44
Network stuff has many things that needs to be included and/or implemented by default.
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:250
bool NetworkCoreInitialize()
Initializes the network core (as that is needed for some platforms.
Definition: core.cpp:33
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:100
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:121
Basic data to distinguish a GRF.
Definition: newgrf_config.h:84
void NetworkCoreShutdown()
Shuts down the network core (as that is needed for some platforms.
Definition: core.cpp:84
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:221
Basic functions to create, fill and read packets.
uint32 grfid
GRF ID (defined by Action 0x08)
Definition: newgrf_config.h:85
void ReceiveGRFIdentifier(Packet *p, GRFIdentifier *grf)
Deserializes the GRFIdentifier (GRF ID and MD5 checksum) from the packet.
Definition: core.cpp:122
uint8 md5sum[16]
MD5 checksum of file to distinguish files with the same GRF ID (eg. newer version of GRF) ...
Definition: newgrf_config.h:86
void SendGRFIdentifier(Packet *p, const GRFIdentifier *grf)
Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet.
Definition: core.cpp:108