4267a229创建于 2024年8月2日历史提交
/*
    Copyright (c) [2023] [squallzhao]
    fountain is licensed under APACHE LICENSE, VERSION 2.0.
    You can use this software according to the terms and conditions of the APACHE LICENSE, VERSION 2.0.
    You may obtain a copy of APACHE LICENSE, VERSION 2.0 at: https://www.apache.org/licenses/LICENSE-2.0
*/
package microservice.web.socket
import std.time.*
import std.socket.*

public class SocketWrapper{
    let socket: TcpSocket;
    public init(socket : TcpSocket){
        this.socket = socket
    }
    
    public func onEvent(event: SocketEvent): Unit{
        let r = match(event){
            case OPENED => EventListenerHandler.getOpenedListener().onEvent(this)
            case CLOSED => EventListenerHandler.getClosedListener().onEvent(this)
        } 
    }

    public func read(buffer: Array<UInt8>): Int64{
        return socket.read(buffer)
    }

    public func read(buffer: Array<UInt8>, timeout: Duration): Int64{
        socket.readTimeout = timeout;
        return socket.read(buffer)
    }

    public func write(buffer: Array<UInt8>): Unit{
        socket.write(buffer)
    }

    public func write(buffer: Array<UInt8>, timeout: Duration): Unit{
        socket.writeTimeout = timeout
        socket.write(buffer)
    }

    public func isClosed(): Bool{
        return socket.isClosed()
    }

    public func close(): Unit{
        socket.close()
    }
}