attempted to split Socket to ServerSocket and ClientSocket

This commit is contained in:
hyunghwan.chung 2018-05-04 01:10:38 +00:00
parent b16ddbd942
commit 8c0787608f

View File

@ -260,7 +260,7 @@ class Socket(AsyncHandle) from 'sck'
{ {
var eventActions. var eventActions.
var pending_bytes, pending_offset, pending_length. var pending_bytes, pending_offset, pending_length.
var outreadysem, outdonesem, inreadysem, connsem. var outreadysem, outdonesem, inreadysem.
method(#primitive) open(domain, type, proto). method(#primitive) open(domain, type, proto).
method(#primitive) _close. method(#primitive) _close.
@ -313,7 +313,6 @@ extend Socket
self.outdonesem := Semaphore new. self.outdonesem := Semaphore new.
self.outreadysem := Semaphore new. self.outreadysem := Semaphore new.
self.inreadysem := Semaphore new. self.inreadysem := Semaphore new.
self.connsem := nil.
self.outdonesem signalAction: [ :sem | self.outdonesem signalAction: [ :sem |
(self.eventActions at: Socket.EventType.DATA_OUT) value: self. (self.eventActions at: Socket.EventType.DATA_OUT) value: self.
@ -361,13 +360,6 @@ extend Socket
self.outreadysem := nil. self.outreadysem := nil.
}. }.
if (self.connsem notNil)
{
System unsignal: self.connsem.
if (self.connsem _group notNil) { System removeAsyncSemaphore: self.connsem }.
self.connsem := nil.
}.
if (self.inreadysem notNil) if (self.inreadysem notNil)
{ {
System unsignal: self.inreadysem. System unsignal: self.inreadysem.
@ -383,50 +375,6 @@ extend Socket
self.eventActions at: event_type put: action_block. self.eventActions at: event_type put: action_block.
} }
method connect: target
{
| sem |
if ((self _connect: target) <= -1)
{
sem := Semaphore new.
sem signalAction: [ :xsem |
| soerr |
soerr := self _socketError.
if (soerr >= 0)
{
## finalize connection if not in progress
System unsignal: xsem.
System removeAsyncSemaphore: xsem.
'CHECKING FOR CONNECTION.....' dump.
(self.eventActions at: Socket.EventType.CONNECTED) value: self value: (soerr == 0).
if (soerr == 0)
{
System addAsyncSemaphore: self.inreadysem.
System signal: self.inreadysem onInput: self.handle.
System addAsyncSemaphore: self.outdonesem.
}.
}.
(* HOW TO HANDLE TIMEOUT? *)
].
self.connsem := sem.
System addAsyncSemaphore: self.connsem.
System signal: self.connsem onOutput: self.handle.
}
else
{
## connected immediately
'IMMEDIATELY CONNECTED.....' dump.
(self.eventActions at: Socket.EventType.CONNECTED) value: self value: true.
System addAsyncSemaphore: self.inreadysem.
System signal: self.inreadysem onInput: self.handle.
System addAsyncSemaphore: self.outdonesem.
}
}
method writeBytes: bytes offset: offset length: length method writeBytes: bytes offset: offset length: length
{ {
| n | | n |
@ -460,6 +408,71 @@ extend Socket
} }
class ClientSocket(Socket)
{
var connsem.
method initialize
{
super initialize.
self.connsem := Semaphore new.
self.connsem signalAction: [ :sem |
| soerr |
soerr := self _socketError.
if (soerr >= 0)
{
## finalize connection if not in progress
System unsignal: sem.
System removeAsyncSemaphore: sem.
'CHECKING FOR CONNECTION.....' dump.
(self.eventActions at: Socket.EventType.CONNECTED) value: self value: (soerr == 0).
if (soerr == 0)
{
System addAsyncSemaphore: self.inreadysem.
System signal: self.inreadysem onInput: self.handle.
System addAsyncSemaphore: self.outdonesem.
}.
}.
(* HOW TO HANDLE TIMEOUT? *)
].
}
method close
{
if (self.connsem notNil)
{
System unsignal: self.connsem.
if (self.connsem _group notNil) { System removeAsyncSemaphore: self.connsem }.
self.connsem := nil.
}.
^super close
}
method connect: target
{
| sem |
if ((self _connect: target) <= -1)
{
System addAsyncSemaphore: self.connsem.
System signal: self.connsem onOutput: self.handle.
}
else
{
## connected immediately
'IMMEDIATELY CONNECTED.....' dump.
(self.eventActions at: Socket.EventType.CONNECTED) value: self value: true.
System addAsyncSemaphore: self.inreadysem.
System signal: self.inreadysem onInput: self.handle.
System addAsyncSemaphore: self.outdonesem.
}
}
}
class ServerSocket(Socket) class ServerSocket(Socket)
{ {
method initialize method initialize
@ -489,7 +502,7 @@ class MyObject(Object)
count := 0. count := 0.
[ [
buf := ByteArray new: 128. buf := ByteArray new: 128.
s := Socket domain: Socket.Domain.INET type: Socket.Type.STREAM. s := ClientSocket domain: Socket.Domain.INET type: Socket.Type.STREAM.
s2 := ServerSocket domain: Socket.Domain.INET type: Socket.Type.STREAM. s2 := ServerSocket domain: Socket.Domain.INET type: Socket.Type.STREAM.
s2 onEvent: Socket.EventType.DATA_IN do: [:sck | s2 onEvent: Socket.EventType.DATA_IN do: [:sck |