Subversion Repositories SvarDOS

Rev

Rev 1132 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1132 Rev 1133
Line 1... Line 1...
1
/*
1
/*
2
 * This file is part of the pkgnet package - the SvarDOS package manager.
2
 * This file is part of the pkgnet package - the SvarDOS package manager.
3
 * Copyright (C) Mateusz Viste 2013-2021
3
 * Copyright (C) Mateusz Viste 2013-2022
4
 *
4
 *
5
 * Provides all network functions used by pkgnet, wrapped around the
5
 * Provides all network functions used by pkgnet, wrapped around the
6
 * Watt-32 TCP/IP stack.
6
 * Watt-32 TCP/IP stack.
7
 */
7
 */
Line 27... Line 27...
27
 * 32K = 98 KiB/s
27
 * 32K = 98 KiB/s
28
 * 60K = 98 KiB/s
28
 * 60K = 98 KiB/s
29
 */
29
 */
Line 30... Line 30...
30
 
30
 
31
struct net_tcpsocket {
-
 
32
  tcp_Socket *sock;
31
struct net_tcpsocket {
33
  tcp_Socket _sock; /* watt32 socket */
32
  tcp_Socket sock; /* watt32 socket */
34
  char tcpbuff[1];
33
  char tcpbuff[1];
Line 35... Line 34...
35
};
34
};
Line 73... Line 72...
73
  /* ignore buffsz smaller than 2K (wattcp already has a 2K buffer) */
72
  /* ignore buffsz smaller than 2K (wattcp already has a 2K buffer) */
74
  if (buffsz <= 2048) buffsz = 0;
73
  if (buffsz <= 2048) buffsz = 0;
Line 75... Line 74...
75
 
74
 
76
  resultsock = calloc(sizeof(struct net_tcpsocket) + buffsz, 1);
75
  resultsock = calloc(sizeof(struct net_tcpsocket) + buffsz, 1);
77
  if (resultsock == NULL) return(NULL);
-
 
Line 78... Line 76...
78
  resultsock->sock = &(resultsock->_sock);
76
  if (resultsock == NULL) return(NULL);
79
 
77
 
80
  if (!tcp_open(resultsock->sock, 0, ipaddr, port, NULL)) {
78
  if (!tcp_open(&(resultsock->sock), 0, ipaddr, port, NULL)) {
81
    sock_abort(resultsock->sock);
79
    sock_abort(&(resultsock->sock));
82
    free(resultsock);
80
    free(resultsock);
Line 83... Line 81...
83
    return(NULL);
81
    return(NULL);
84
  }
82
  }
85
 
83
 
86
  /* set user-managed buffer if requested (watt32's default is only 2K)
84
  /* set user-managed buffer if requested (watt32's default is only 2K)
Line 87... Line 85...
87
   * this must be set AFTER tcp_open(), since the latter rewrites the tcp
85
   * this must be set AFTER tcp_open(), since the latter rewrites the tcp
88
   * rx buffer */
86
   * rx buffer */
Line 89... Line 87...
89
  if (buffsz > 0) sock_setbuf(resultsock->sock, resultsock->tcpbuff, buffsz);
87
  if (buffsz > 0) sock_setbuf(&(resultsock->sock), resultsock->tcpbuff, buffsz);
90
 
88
 
91
  return(resultsock);
89
  return(resultsock);
92
}
90
}
93
 
91
 
Line 94... Line 92...
94
 
92
 
95
int net_isconnected(struct net_tcpsocket *s) {
93
int net_isconnected(struct net_tcpsocket *s) {
96
  if (tcp_tick(s->sock) == 0) return(-1);
94
  if (tcp_tick(&(s->sock)) == 0) return(-1);
97
  if (sock_established(s->sock) == 0) return(0);
95
  if (sock_established(&(s->sock)) == 0) return(0);
98
  return(1);
96
  return(1);
99
}
97
}
100
 
98
 
101
 
99
 
Line 102... Line 100...
102
/* Sends data on socket 'socket'.
100
/* Sends data on socket 'socket'.
103
   Returns the number of bytes sent on success, and < 0 otherwise */
101
   Returns the number of bytes sent on success, and < 0 otherwise */
104
int net_send(struct net_tcpsocket *socket, const void *line, long len) {
102
int net_send(struct net_tcpsocket *socket, const void *line, long len) {
105
  /* call this to let Watt-32 handle its internal stuff */
103
  /* call this to let Watt-32 handle its internal stuff */
106
  if (tcp_tick(socket->sock) == 0) return(-1);
104
  if (tcp_tick(&(socket->sock)) == 0) return(-1);
107
  /* send bytes */
105
  /* send bytes */
108
  return(sock_write(socket->sock, line, len));
106
  return(sock_write(&(socket->sock), line, len));
Line 109... Line 107...
109
}
107
}
110
 
108
 
111
 
109
 
112
/* Reads data from socket 'sock' and write it into buffer 'buff', until end of connection. Will fall into error if the amount of data is bigger than 'maxlen' bytes.
110
/* Reads data from socket 'sock' and write it into buffer 'buff', until end of connection. Will fall into error if the amount of data is bigger than 'maxlen' bytes.
113
Returns the amount of data read (in bytes) on success, or a negative value otherwise. The error code can be translated into a human error message via libtcp_strerr(). */
111
Returns the amount of data read (in bytes) on success, or a negative value otherwise. The error code can be translated into a human error message via libtcp_strerr(). */
114
int net_recv(struct net_tcpsocket *socket, void *buff, long maxlen) {
112
int net_recv(struct net_tcpsocket *socket, void *buff, long maxlen) {
115
  /* call this to let WatTCP handle its internal stuff */
113
  /* call this to let WatTCP handle its internal stuff */
116
  if (tcp_tick(socket->sock) == 0) return(-1);
114
  if (tcp_tick(&(socket->sock)) == 0) return(-1);
Line 117... Line 115...
117
  return(sock_fastread(socket->sock, buff, maxlen));
115
  return(sock_fastread(&(socket->sock), buff, maxlen));