attempted to count watchers in Scoket

This commit is contained in:
hyunghwan.chung 2018-01-24 10:31:34 +00:00
parent b823b0b218
commit 625e2fbcdb
2 changed files with 81 additions and 53 deletions

View File

@ -10,8 +10,14 @@ class(#byte) IPAddress(Object)
} }
### TODO: extend the compiler ### TODO: extend the compiler
###class(#byte(4)) IP4Address(IPAddress) -> new basicNew always create 4 bytes. size to new: or basicNew: is ignored. ### #byte(4) basic size if 4 bytes. basicNew: xxx creates an instance of the size 4 + xxx.
###class(#byte(16)) IP6Address(IPAddress) ### -> extend to support fixed 4 bytes by throwing an error in basicNew:.
### -> #byte(4,fixed)?
### -> #byte -> byte variable/flexible
### -> #byte(4) -> byte variable with the mimimum size of 4
### -> (TODO)-> #byte(4,10) -> byte variable with the mimum size of 4 and maximum size of 10 => basicNew: should be allowed with upto 6.
### -> #byte(4,4) -> it can emulated fixed byte size. -> do i have space in spec to store the upper bound?
class(#byte(4)) IP4Address(IPAddress) class(#byte(4)) IP4Address(IPAddress)
{ {
@ -219,30 +225,13 @@ class(#byte) SocketAddress(Object) from 'sck.addr'
} }
} }
(**********************************************************
class X(Object)
{
var tt,yy.
method x {
self.yy := 9.
}
}
##class(#byte(3)) Y(X)
class(#pointer) Y(X)
{
}
##class (#word(2)) Z(Y)
##{
##}
##class InetSocketAddress(SocketAddress)
##{
##}
************************************************************)
class Socket(Object) from 'sck' class Socket(Object) from 'sck'
{ {
## the handle must be the first field in the Socket object to match
## the internal socket representation used by the sck module.
var(#get) handle := -1. var(#get) handle := -1.
var inwc := 0, outwc := 0. ## input watcher count and ouput watcher count
var insem, outsem. var insem, outsem.
var(#get,#set) inputAction, outputAction. var(#get,#set) inputAction, outputAction.
@ -255,7 +244,7 @@ class Socket(Object) from 'sck'
method(#primitive) _socketError. method(#primitive) _socketError.
method(#primitive) readBytes: bytes. method(#primitive) readBytes: bytes.
method(#primitive) writeBytes: bytes. method(#primitive) _writeBytes: bytes.
} }
(* TODO: generate these domain and type from the C header *) (* TODO: generate these domain and type from the C header *)
@ -345,9 +334,28 @@ extend Socket
] ]
} }
method writeBytes: bytes
{
| old_output_action |
old_output_action := self.outputAction.
self.outputAction := [ :sck :state |
self _writeBytes: bytes.
## restore the output action block before executing the previous
## one. i don't want this action block to be chained by the
## previous block if it ever does
self.outputAction := old_output_action.
if (old_output_action notNil) { old_output_action value: self value: true }.
self unwatchOutput.
].
self watchOutput.
}
## TODO: how to specify a timeout for an action? using another semaphore?? ## TODO: how to specify a timeout for an action? using another semaphore??
method watchInput method watchInput
{
if (self.inwc == 0)
{ {
if (self.insem isNil) if (self.insem isNil)
{ {
@ -355,16 +363,27 @@ extend Socket
self.insem signalAction: [:sem | self.inputAction value: self value: true]. self.insem signalAction: [:sem | self.inputAction value: self value: true].
System addAsyncSemaphore: self.insem. System addAsyncSemaphore: self.insem.
}. }.
System signal: self.insem onInput: self.handle
System signal: self.insem onInput: self.handle. }.
self.inwc := self.inwc + 1.
} }
method unwatchInput method unwatchInput
{ {
if (self.insem notNil) { System unsignal: self.insem }. if (self.inwc > 0)
{
self.inwc := self.inwc - 1.
if (self.inwc == 0)
{
##if (self.insem notNil) { System unsignal: self.insem }.
System unsignal: self.insem.
}.
}.
} }
method watchOutput method watchOutput
{
if (self.outwc == 0)
{ {
if (self.outsem isNil) if (self.outsem isNil)
{ {
@ -372,13 +391,22 @@ extend Socket
self.outsem signalAction: [:sem | self.outputAction value: self value: true]. self.outsem signalAction: [:sem | self.outputAction value: self value: true].
System addAsyncSemaphore: self.outsem. System addAsyncSemaphore: self.outsem.
}. }.
System signal: self.outsem onOutput: self.handle. System signal: self.outsem onOutput: self.handle.
}.
self.outwc := self.outwc + 1.
} }
method unwatchOutput method unwatchOutput
{ {
if (self.outsem notNil) { System unsignal: self.outsem }. if (self.outwc > 0)
{
self.outwc := self.outwc - 1.
if (self.outwc == 0)
{
##if (self.outsem notNil) { System unsignal: self.outsem }.
System unsignal: self.outsem.
}.
}.
} }
} }
@ -458,11 +486,14 @@ error -> exception
data dump. data dump.
}. }.
sck writeBytes: #[ $h, $e, $l, $l, $o, $., $., $., C'\n' ]. ##sck writeBytes: #[ $h, $e, $l, $l, $o, $., $., $., C'\n' ].
sck writeBytes: data.
} }
while (true). while (true).
]. ].
## TODO: what should it accept as block parameter
## socket, output result? , output object?
outact := [:sck :state | outact := [:sck :state |
if (state) if (state)
{ {
@ -478,9 +509,9 @@ error -> exception
if (state) if (state)
{ {
'CONNECTED NOW.............' dump. 'CONNECTED NOW.............' dump.
sck watchInput.
sck writeBytes: #[ $h, $e, $l, $l, $o, $w, $o, C'\n' ]. sck writeBytes: #[ $h, $e, $l, $l, $o, $w, $o, C'\n' ].
sck watchInput; watchOutput. ###sck watchInput; watchOutput.
} }
else else
{ {
@ -500,6 +531,8 @@ error -> exception
newsck inputAction: inact; outputAction: outact. newsck inputAction: inact; outputAction: outact.
##newsck watchInput; watchOutput. ##newsck watchInput; watchOutput.
newsck watchInput. newsck watchInput.
newsck writeBytes: #[ $W, $e, $l, $c, $o, $m, $e, $., C'\n' ].
]. ].
[ [

View File

@ -279,17 +279,12 @@ CTAGS = ctags
CSCOPE = cscope CSCOPE = cscope
AM_RECURSIVE_TARGETS = cscope AM_RECURSIVE_TARGETS = cscope
am__DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/config-h.in \ am__DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/config-h.in \
$(top_srcdir)/../ac/compile \ $(top_srcdir)/../ac/compile $(top_srcdir)/../ac/config.guess \
$(top_srcdir)/../ac/config.guess \ $(top_srcdir)/../ac/config.sub $(top_srcdir)/../ac/depcomp \
$(top_srcdir)/../ac/config.sub \ $(top_srcdir)/../ac/install-sh $(top_srcdir)/../ac/ltmain.sh \
$(top_srcdir)/../ac/depcomp \ $(top_srcdir)/../ac/missing ../ac/ar-lib ../ac/compile \
$(top_srcdir)/../ac/install-sh \ ../ac/config.guess ../ac/config.sub ../ac/depcomp \
$(top_srcdir)/../ac/ltmain.sh \ ../ac/install-sh ../ac/ltmain.sh ../ac/missing COPYING.LIB \
$(top_srcdir)/../ac/missing ../ac/compile \
../ac/config.guess ../ac/config.sub \
../ac/depcomp ../ac/install-sh \
../ac/ltmain.sh \
../ac/missing COPYING.LIB \
README lt__argz.c lt__dirent.c lt__strl.c README lt__argz.c lt__dirent.c lt__strl.c
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
distdir = $(PACKAGE)-$(VERSION) distdir = $(PACKAGE)-$(VERSION)