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

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

style: remove trailing spaces, fix copyright statements.

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