| OLD | NEW |
| 1 // Copyright (c) 2014, the Fletch project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Fletch project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 part of system; | 5 part of system; |
| 6 | 6 |
| 7 const int EPOLLIN = 0x1; | 7 const int EPOLLIN = 0x1; |
| 8 const int EPOLLOUT = 0x4; | 8 const int EPOLLOUT = 0x4; |
| 9 const int EPOLLERR = 0x8; | 9 const int EPOLLERR = 0x8; |
| 10 const int EPOLLHUP = 0x10; | 10 const int EPOLLHUP = 0x10; |
| 11 const int EPOLLRDHUP = 0x2000; | 11 const int EPOLLRDHUP = 0x2000; |
| 12 const int EPOLLONESHOT = 0x40000000; | 12 const int EPOLLONESHOT = 0x40000000; |
| 13 | 13 |
| 14 const int EPOLL_CTL_ADD = 1; | 14 const int EPOLL_CTL_ADD = 1; |
| 15 const int EPOLL_CTL_DEL = 2; | 15 const int EPOLL_CTL_DEL = 2; |
| 16 const int EPOLL_CTL_MOD = 3; | 16 const int EPOLL_CTL_MOD = 3; |
| 17 | 17 |
| 18 class LinuxAddrInfo extends AddrInfo { | 18 class LinuxAddrInfo extends AddrInfo { |
| 19 LinuxAddrInfo() : super._(); | 19 LinuxAddrInfo() : super._(); |
| 20 LinuxAddrInfo.fromAddress(int address) : super._fromAddress(address); | 20 LinuxAddrInfo.fromAddress(int address) : super._fromAddress(address); |
| 21 | 21 |
| 22 Foreign get ai_addr { | 22 ForeignMemory get ai_addr { |
| 23 int offset = _addrlenOffset + wordSize; | 23 int offset = _addrlenOffset + wordSize; |
| 24 return new Foreign.fromAddress(getWord(offset), ai_addrlen); | 24 return new ForeignMemory.fromAddress(getWord(offset), ai_addrlen); |
| 25 } | 25 } |
| 26 | 26 |
| 27 get ai_canonname { | 27 get ai_canonname { |
| 28 int offset = _addrlenOffset + wordSize * 2; | 28 int offset = _addrlenOffset + wordSize * 2; |
| 29 return getWord(offset); | 29 return getWord(offset); |
| 30 } | 30 } |
| 31 | 31 |
| 32 AddrInfo get ai_next { | 32 AddrInfo get ai_next { |
| 33 int offset = _addrlenOffset + wordSize * 3; | 33 int offset = _addrlenOffset + wordSize * 3; |
| 34 return new LinuxAddrInfo.fromAddress(getWord(offset)); | 34 return new LinuxAddrInfo.fromAddress(getWord(offset)); |
| 35 } | 35 } |
| 36 } | 36 } |
| 37 | 37 |
| 38 class EpollEvent extends Foreign { | 38 class EpollEvent extends ForeignMemory { |
| 39 // epoll_event is packed on ia32/x64, but not on arm. | 39 // epoll_event is packed on ia32/x64, but not on arm. |
| 40 static int eventSize = Foreign.architecture == Foreign.ARM ? 8 : 4; | 40 static int eventSize = Foreign.architecture == Foreign.ARM ? 8 : 4; |
| 41 | 41 |
| 42 EpollEvent() : super.allocatedFinalize(eventSize + 8); | 42 EpollEvent() : super.allocatedFinalize(eventSize + 8); |
| 43 | 43 |
| 44 int get events => getInt32(0); | 44 int get events => getInt32(0); |
| 45 void set events(int value) { | 45 void set events(int value) { |
| 46 setInt32(0, value); | 46 setInt32(0, value); |
| 47 } | 47 } |
| 48 | 48 |
| 49 int get data => getInt64(eventSize); | 49 int get data => getInt64(eventSize); |
| 50 void set data(int value) { | 50 void set data(int value) { |
| 51 setInt64(eventSize, value); | 51 setInt64(eventSize, value); |
| 52 } | 52 } |
| 53 } | 53 } |
| 54 | 54 |
| 55 class LinuxSystem extends PosixSystem { | 55 class LinuxSystem extends PosixSystem { |
| 56 static final Foreign _epollCtl = Foreign.lookup("epoll_ctl"); | 56 static final ForeignFunction _epollCtl = |
| 57 static final Foreign _lseekLinux = Foreign.lookup("lseek64"); | 57 ForeignLibrary.main.lookup("epoll_ctl"); |
| 58 static final Foreign _openLinux = Foreign.lookup("open64"); | 58 static final ForeignFunction _lseekLinux = |
| 59 ForeignLibrary.main.lookup("lseek64"); |
| 60 static final ForeignFunction _openLinux = |
| 61 ForeignLibrary.main.lookup("open64"); |
| 59 | 62 |
| 60 final EpollEvent _epollEvent = new EpollEvent(); | 63 final EpollEvent _epollEvent = new EpollEvent(); |
| 61 | 64 |
| 62 int get FIONREAD => 0x541B; | 65 int get FIONREAD => 0x541B; |
| 63 | 66 |
| 64 int get SOL_SOCKET => 1; | 67 int get SOL_SOCKET => 1; |
| 65 | 68 |
| 66 int get SO_REUSEADDR => 2; | 69 int get SO_REUSEADDR => 2; |
| 67 | 70 |
| 68 Foreign get _lseek => _lseekLinux; | 71 ForeignFunction get _lseek => _lseekLinux; |
| 69 Foreign get _open => _openLinux; | 72 ForeignFunction get _open => _openLinux; |
| 70 | 73 |
| 71 int addToEventHandler(int fd) { | 74 int addToEventHandler(int fd) { |
| 72 _epollEvent.events = 0; | 75 _epollEvent.events = 0; |
| 73 _epollEvent.data = 0; | 76 _epollEvent.data = 0; |
| 74 int eh = System.eventHandler; | 77 int eh = System.eventHandler; |
| 75 return _retry(() => _epollCtl.icall$4(eh, EPOLL_CTL_ADD, fd, _epollEvent)); | 78 return _retry(() => _epollCtl.icall$4(eh, EPOLL_CTL_ADD, fd, _epollEvent)); |
| 76 } | 79 } |
| 77 | 80 |
| 78 int removeFromEventHandler(int fd) { | 81 int removeFromEventHandler(int fd) { |
| 79 // TODO(ajohnsen): If we increased the refcount of the port before adding it | 82 // TODO(ajohnsen): If we increased the refcount of the port before adding it |
| 80 // to the epoll set and we remove it now, we can leak memory. | 83 // to the epoll set and we remove it now, we can leak memory. |
| 81 int eh = System.eventHandler; | 84 int eh = System.eventHandler; |
| 82 return _retry(() => _epollCtl.icall$4(eh, EPOLL_CTL_DEL, fd, Foreign.NULL)); | 85 return _retry(() => _epollCtl.icall$4(eh, EPOLL_CTL_DEL, fd, |
| 86 ForeignPointer.NULL)); |
| 83 } | 87 } |
| 84 | 88 |
| 85 int setPortForNextEvent(int fd, Port port, int mask) { | 89 int setPortForNextEvent(int fd, Port port, int mask) { |
| 86 int events = EPOLLRDHUP | EPOLLHUP | EPOLLONESHOT; | 90 int events = EPOLLRDHUP | EPOLLHUP | EPOLLONESHOT; |
| 87 if ((mask & READ_EVENT) != 0) events |= EPOLLIN; | 91 if ((mask & READ_EVENT) != 0) events |= EPOLLIN; |
| 88 if ((mask & WRITE_EVENT) != 0) events |= EPOLLOUT; | 92 if ((mask & WRITE_EVENT) != 0) events |= EPOLLOUT; |
| 89 _epollEvent.events = events; | 93 _epollEvent.events = events; |
| 90 _epollEvent.data = System._incrementPortRef(port); | 94 _epollEvent.data = System._incrementPortRef(port); |
| 91 int eh = System.eventHandler; | 95 int eh = System.eventHandler; |
| 92 return _retry(() => _epollCtl.icall$4(eh, EPOLL_CTL_MOD, fd, _epollEvent)); | 96 return _retry(() => _epollCtl.icall$4(eh, EPOLL_CTL_MOD, fd, _epollEvent)); |
| 93 } | 97 } |
| 94 } | 98 } |
| OLD | NEW |