source: abuse/trunk/src/nfclient.cpp @ 531

Last change on this file since 531 was 524, checked in by Sam Hocevar, 11 years ago

core: Get rid of mostly useless headers, move endianness handling to
common.h (and rewrite functions so that they do not need the SDL headers)
and move a few functions out of sdlport's video.cpp. These functions
were in the original video.cpp (which reappears) and shouldn't be part
of the SDL port.

File size: 5.2 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
13#if (defined(__MACH__) || !defined(__APPLE__))
14#   include <sys/types.h>
15#endif
16#include <fcntl.h>
17#include <unistd.h>
18#include <ctype.h>
19
20#include "common.h"
21
22#include "netface.h"
23
24#include "specs.h"
25#include "nfserver.h"
26#include "dprint.h"
27#include "crc.h"
28#include "cache.h"
29
30#include "gserver.h"
31
32void remove_client(int client_number) { ; }
33
34CrcManager *net_crcs = NULL;
35extern net_protocol *prot;
36
37class nfs_file : public bFILE
38{
39  jFILE *local;
40  int nfs_fd;
41  int offset;
42  public :
43  nfs_file(char const *filename, char const *mode);
44  virtual int open_failure();
45  virtual int unbuffered_read(void *buf, size_t count);       // returns number of bytes read
46  int new_read(void *buf, size_t count);       // returns number of bytes read
47  virtual int unbuffered_write(void const *buf, size_t count);      // returns number of bytes written
48  virtual int unbuffered_seek(long offset, int whence);  // whence=SEEK_SET, SEEK_CUR, SEEK_END, ret=0=success
49  virtual int unbuffered_tell();
50  virtual int file_size();
51  virtual ~nfs_file();
52} ;
53
54bFILE *open_nfs_file(char const *filename, char const *mode)
55{
56  return new nfs_file(filename,mode);
57}
58
59
60nfs_file::nfs_file(char const *filename, char const *mode)
61{
62  local=NULL;
63  nfs_fd=-1;
64
65  int local_only=0;
66  char const *s=mode;
67  for (; *s; s++)    // check to see if writeable file, if so don't go through nfs
68    if (*s=='w' || *s=='W' || *s=='a' || *s=='A')
69      local_only=1;
70
71  char name[256], *c;
72  char const *f = filename;
73  c = name;
74  while (*f) { *c=*(f++); *c=toupper(*c); c++; } *c=0;
75  if (strstr(name,"REGISTER"))
76    local_only=1;
77
78  if (net_crcs && !local_only)
79  {
80    int fail1,fail2,fail3=0;
81    char const *local_filename = filename;
82    if (filename[0]=='/' && filename[1]=='/')
83    { local_filename+=2;
84      while (*local_filename && *local_filename!='/') local_filename++;
85      local_filename++;
86    }
87
88    int remote_file_num=net_crcs->get_filenumber(local_filename);
89    uint32_t remote_crc=net_crcs->get_crc(remote_file_num,fail2);
90    if (!fail2)
91    {
92      int local_file_num=crc_manager.get_filenumber(local_filename);
93      uint32_t local_crc=crc_manager.get_crc(local_file_num,fail1);
94      if (fail1)
95      {
96    bFILE *fp=new jFILE(local_filename,"rb");
97    if (!fp->open_failure())
98    {
99      local_crc=crc_file(fp);
100      crc_manager.set_crc(local_file_num,local_crc);
101    } else fail3=1;
102    delete fp;
103      }
104
105      if (!fail3)
106      {
107    if (local_crc==remote_crc)
108          local_only=1;
109      }
110    }
111  }
112
113
114  if (local_only)
115  {
116    local=new jFILE(filename,mode);
117    if (local->open_failure()) { delete local; local=NULL; }
118  }
119  else
120  {
121
122
123    char nm[256];
124    strcpy(nm,filename);
125    nfs_fd=NF_open_file(nm,mode);
126    if (nfs_fd==-2)
127    {
128      local=new jFILE(nm,mode);
129      if (local->open_failure()) { delete local; local=NULL; }
130      nfs_fd=-1;
131    }
132  }
133}
134
135
136int nfs_file::open_failure()
137{
138  if (local==NULL && nfs_fd<0) return 1;
139  else return 0;
140}
141
142
143int nfs_file::unbuffered_read(void *buf, size_t count)      // returns number of bytes read
144{
145  if (local)
146    return local->read(buf,count);
147  else if (nfs_fd>=0)
148  {
149    long a=NF_read(nfs_fd,buf,count);
150    if (a>(long)count)
151    {
152      fprintf(stderr,"ooch read too much\n");
153    }
154    return a;
155  }
156  else return 0;
157}
158
159int nfs_file::unbuffered_write(void const *buf, size_t count)      // returns number of bytes written
160{
161  if (local)
162    return local->write(buf,count);
163  else
164  {
165    fprintf(stderr,"write to nfs file not allowed for now!\n");
166    exit(0);
167  }
168  return 0;
169}
170
171
172int nfs_file::unbuffered_seek(long off, int whence) // whence=SEEK_SET, SEEK_CUR, SEEK_END, ret=0=success
173{
174  if (local)
175    return local->seek(off,whence);
176  else if (nfs_fd>=0)
177  {
178    if (whence!=SEEK_SET)
179      fprintf(stderr,"JC's a fork\n");
180    else
181      return NF_seek(nfs_fd,off);
182  }
183  return 0;
184}
185
186int nfs_file::unbuffered_tell()
187{
188  if (local)          return local->tell();
189  else if (nfs_fd>=0) return NF_tell(nfs_fd);
190  else                return 0;
191}
192
193
194int nfs_file::file_size()
195{
196  if (local)          return local->file_size();
197  else if (nfs_fd>=0) return NF_filelength(nfs_fd);
198  else                return 0;
199}
200
201nfs_file::~nfs_file()
202{
203  flush_writes();
204  if (local)          delete local;
205  else if (nfs_fd>=0) NF_close(nfs_fd);
206}
207
208int set_file_server(net_address *addr)
209{
210  if (NF_set_file_server(addr))
211  {
212    if (net_crcs)
213    {
214      net_crcs->clean_up();
215      delete net_crcs;
216    }
217
218    net_crcs=new CrcManager();
219    if (!net_crcs->load_crc_file(NET_CRC_FILENAME))
220    {
221      delete net_crcs;
222      net_crcs=NULL;
223      return 0;
224    }
225    return 1;
226  }
227  return 0;
228}
229
230
231int set_file_server(char const *name)
232{
233  if (prot)
234  {
235    net_address *addr=prot->get_node_address(name,DEFAULT_COMM_PORT,0);
236    if (!addr) { dprintf("\nUnable to locate server\n"); return 0; }
237    if (!set_file_server(addr))
238    {
239      delete addr;
240      return 0;
241    } else return 1;
242  } else return 0;
243}
244
Note: See TracBrowser for help on using the repository browser.