summary refs log tree commit diff stats
path: root/go.mod
Commit message (Collapse)AuthorAgeFilesLines
* go moduleBen Morrison2019-05-091-0/+3
mitter bptato <nincsnevem662@gmail.com> 2024-03-12 14:09:53 +0100 io: add dynstream' href='/ahoang/chawan/commit/src/io/posixstream.nim?id=0cc8f357d8f44fcfccd141c078d34ed4530fec5b'>0cc8f357 ^
1e858c87 ^

0cc8f357 ^
d8c4b097 ^
1e858c87 ^

1e858c87 ^



2e1f3147 ^
1c0df44a ^
1e858c87 ^
7b6a2c6d ^


4e690f15 ^
7b6a2c6d ^
7b6a2c6d ^





2e1f3147 ^

1c0df44a ^

7b6a2c6d ^


66b9574b ^
7cb7ea1d ^








44451ed4 ^

44451ed4 ^

66b9574b ^
407b5253 ^




66b9574b ^
547a4926 ^
d8c4b097 ^





0cc8f357 ^
73311e56 ^

b789b0b0 ^
0cc8f357 ^
547a4926 ^

1e858c87 ^
fb21b1e4 ^
8e6783a4 ^
66b9574b ^
8e6783a4 ^



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
                

                   

    
                                        
             

                                 



                                       
                                           
                                      
 


                                                                     
                                             
                                            





                                               

                                                                        

                                                      


                                                 
                                                                  








                                         

                                       

               
                                                                  




                                  
                                                              
                       





                                                        
                                        

                                           
 
                                

                     
                                                   
                                            
 
                                                                    



                                           
import std/posix

import io/dynstream

type
  PosixStream* = ref object of DynStream
    fd*: cint

  ErrorAgain* = object of IOError
  ErrorBadFD* = object of IOError
  ErrorFault* = object of IOError
  ErrorInterrupted* = object of IOError
  ErrorInvalid* = object of IOError
  ErrorConnectionReset* = object of IOError
  ErrorBrokenPipe* = object of IOError

proc raisePosixIOError*() =
  # In the nim stdlib, these are only constants on linux amd64, so we
  # can't use a switch.
  if errno == EAGAIN or errno == EWOULDBLOCK:
    raise newException(ErrorAgain, "eagain")
  elif errno == EBADF:
    raise newException(ErrorBadFD, "bad fd")
  elif errno == EFAULT:
    raise newException(ErrorFault, "fault")
  elif errno == EINVAL:
    raise newException(ErrorInvalid, "invalid")
  elif errno == ECONNRESET:
    raise newException(ErrorConnectionReset, "connection reset by peer")
  elif errno == EPIPE:
    raise newException(ErrorBrokenPipe, "broken pipe")
  else:
    raise newException(IOError, $strerror(errno))

method recvData*(s: PosixStream; buffer: pointer; len: int): int =
  let n = read(s.fd, buffer, len)
  if n < 0:
    raisePosixIOError()
  if n == 0:
    if unlikely(s.isend):
      raise newException(EOFError, "eof")
    s.isend = true
  return n

proc sreadChar*(s: PosixStream): char =
  let n = read(s.fd, addr result, 1)
  assert n == 1

method sendData*(s: PosixStream; buffer: pointer; len: int): int =
  let n = write(s.fd, buffer, len)
  if n < 0:
    raisePosixIOError()
  return n

method setBlocking*(s: PosixStream; blocking: bool) {.base.} =
  s.blocking = blocking
  let ofl = fcntl(s.fd, F_GETFL, 0)
  if blocking:
    discard fcntl(s.fd, F_SETFL, ofl and not O_NONBLOCK)
  else:
    discard fcntl(s.fd, F_SETFL, ofl or O_NONBLOCK)

method seek*(s: PosixStream; off: int) =
  if lseek(s.fd, Off(off), SEEK_SET) == -1:
    raisePosixIOError()

method sclose*(s: PosixStream) =
  discard close(s.fd)

proc newPosixStream*(fd: FileHandle): PosixStream =
  return PosixStream(fd: fd, blocking: true)

proc newPosixStream*(path: string; flags, mode: cint): PosixStream =
  let fd = open(cstring(path), flags, mode)
  if fd == -1:
    return nil
  return newPosixStream(fd)