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

Last change on this file since 481 was 481, checked in by Sam Hocevar, 12 years ago

Fuck the history, I'm renaming all .hpp files to .h for my own sanity.

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