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

Last change on this file since 651 was 651, checked in by Sam Hocevar, 9 years ago

build: add a --disable-network compilation flag and get rid of most of
the CELLOS_LV2 ifdefs.

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