-
Notifications
You must be signed in to change notification settings - Fork 25
/
event.go
87 lines (74 loc) · 2.58 KB
/
event.go
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
78
79
80
81
82
83
84
85
86
87
package libssh
/*
#cgo pkg-config: libssh
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/types.h>
#include <libssh/libssh.h>
extern int event_add_fd(ssh_event event, socket_t fd, short events, void *userdata);
*/
import "C"
import "unsafe"
type Event struct {
event C.ssh_event
}
func NewEvent() (Event, error) {
ev := C.ssh_event_new()
if ev != nil {
return Event{ev}, nil
}
return Event{}, apiError("ssh_event_new", "NULL")
}
func (e Event) Free() {
C.ssh_event_free(e.event)
}
type EventCallback struct {
OnSshEvent func(socketFd int, revents int) int
}
// Add a fd to the event and assign it a callback, when used in blocking mode.
//
// socketFd:
// Socket that will be polled.
// pollEvents:
// Poll events that will be monitored for the socket. i.e. POLLIN, POLLPRI, POLLOUT
// callback:
func (e Event) AddFd(socketFd int, pollEvents int, callback *EventCallback) error {
return apiError("ssh_event_add_fd", C.event_add_fd(e.event, C.socket_t(socketFd), C.short(pollEvents), unsafe.Pointer(callback)))
}
//export event_callback
func event_callback(fd C.socket_t, revents C.int, userdata unsafe.Pointer) C.int {
callback := (*EventCallback)(userdata)
return C.int(callback.OnSshEvent(int(fd), int(revents)))
}
// remove the poll handle from session and assign them to a event, when used in
// blocking mode.
func (e Event) AddSession(session Session) error {
// FIXME: removeCallback here
return apiError("ssh_event_add_session", C.ssh_event_add_session(e.event, session.ptr))
}
// Poll all the sockets and sessions associated through an event object.
//
// If any of the events are set after the poll, the call back functions of the
// sessions or sockets will be called. This function should be called once
// within the programs main loop
//
// timeout:
// An upper limit on the time for which the poll will block, in milliseconds.
// Specifying a negative value means an infinite timeout. This parameter is
// passed to the poll( function.)
/*func (e Event) Poll(timeout int) error {
return e.eventError(C.sh_event_dopoll(e.event, C.int(timeout)))
}*/
// Remove a socket fd from an event context.
func (e Event) RemoveFd(fd int) error {
return apiError("ssh_event_remove_fd", C.ssh_event_remove_fd(e.event, C.socket_t(fd)))
}
// Remove a session object from an event context.
func (e Event) RemoveSession(session Session) error {
return apiError("ssh_event_remove_session", C.ssh_event_remove_session(e.event, session.ptr))
}
func (e Event) Poll(timeout int) error {
return apiError("ssh_event_dopoll", C.ssh_event_dopoll(e.event, C.int(timeout)))
}