This repository was archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFTPDataConnection.bas
More file actions
143 lines (128 loc) · 3.42 KB
/
FTPDataConnection.bas
File metadata and controls
143 lines (128 loc) · 3.42 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=6.5
@EndOfDesignText@
'Class module
Sub Class_Globals
Private mClient As FTPClient
Private mServer As FTPServer
Private ssocket As ServerSocket
Private AStream As AsyncStreams
Private mTask As FTPTask
Private months() As String = Array As String("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", _
"Sep", "Oct", "Nov", "Dec")
Private FileIn As InputStream
Private FileOut As OutputStream
Private FileBuffer(81920) As Byte
Private RETRTimer As Timer
End Sub
Public Sub Initialize (Client As FTPClient, Server As FTPServer, port As Int)
ssocket.Initialize(port, "ssocket")
ssocket.Listen
mClient = Client
mServer = Server
RETRTimer.Initialize("RETRTimer", 30)
End Sub
Public Sub SetTask (task As FTPTask)
mTask = task
AfterConnectionAndTask
End Sub
Private Sub AfterConnectionAndTask
If mTask.IsInitialized And AStream.IsInitialized Then
Try
Select mTask.Command
Case "LIST"
HandleLIST
Case "RETR"
FileIn = File.OpenInput(mServer.BaseDir, mTask.Path)
RETRTimer.Enabled = True
Case "STOR"
FileOut = File.OpenOutput(mServer.BaseDir, mTask.Path, False)
End Select
Catch
mClient.SendResponse(451, "")
Close
Log(LastException)
End Try
End If
End Sub
Private Sub ssocket_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
AStream.Initialize(NewSocket.InputStream, NewSocket.OutputStream, "astream")
AfterConnectionAndTask
End If
End Sub
Private Sub RETRTimer_Tick
Try
If AStream.OutputQueueSize > 50 Then Return
Dim c As Int = FileIn.ReadBytes(FileBuffer, 0, FileBuffer.Length)
If c <= 0 Then
AStream.SendAllAndClose
RETRTimer.Enabled = False
Else
AStream.Write2(FileBuffer, 0, c)
End If
Catch
Log(LastException)
Close
End Try
End Sub
Private Sub HandleLIST
Dim sb As StringBuilder
sb.Initialize
Dim dir As String = File.Combine(mServer.BaseDir, mTask.Path)
'create the LIST format
For Each f As String In File.ListFiles(dir)
If File.IsDirectory(dir, f) Then
sb.Append("drwxr-xr-x 1 owner group ")
Else
sb.Append("-rw-r--r-- 1 owner group ")
End If
Dim size As String = File.Size(dir, f)
Pad(sb, 13, size).Append(" ")
Dim date As Long = File.LastModified(dir, f)
sb.Append(months(DateTime.GetMonth(date) - 1)).Append(" ")
Dim day As String = DateTime.GetDayOfMonth(date)
Pad(sb, 3, day).Append(" ")
If DateTime.Now - date > 180 * DateTime.TicksPerDay Then
Pad(sb, 5, DateTime.GetYear(date))
Else
sb.Append($"$2.0{DateTime.GetHour(date)}:$2.0{DateTime.GetHour(date)}"$)
End If
sb.Append(" ").Append(f).Append(mServer.EOL)
Next
AStream.Write(sb.ToString.GetBytes("UTF8"))
AStream.SendAllAndClose
End Sub
Private Sub Pad(sb As StringBuilder, n As Int, value As String) As StringBuilder
For i = 1 To n - value.Length
sb.Append(" ")
Next
sb.Append(value)
Return sb
End Sub
Private Sub AStream_NewData (buffer() As Byte)
Try
FileOut.WriteBytes(buffer, 0, buffer.Length)
Catch
Log(LastException)
Close
End Try
End Sub
Public Sub Close
If AStream.IsInitialized Then AStream.Close
ssocket.Close
If FileIn.IsInitialized Then FileIn.Close
If FileOut.IsInitialized Then FileOut.Close
RETRTimer.Enabled = False
End Sub
Private Sub AStream_Terminated
Log("Data connection terminated: " & mTask.Path)
CallSubDelayed3(mClient, "SendResponse", 226, "")
Close
End Sub
Private Sub AStream_Error
AStream_Terminated
End Sub