internal override void InitWork(Object stateInfo)
{
if (!Valid)
return;
if (DccServer != null)
{
Connection = DccServer.AcceptTcpClient();
RemoteEndPoint = (IPEndPoint)Connection.Client.RemoteEndPoint;
DccServer.Stop();
IsConnected = true;
}
else
{
while (!IsConnected)
{
Thread.Sleep(500); // We wait till Request is Accepted (or jump out when rejected)
if (Reject) return;
}
}
DccSendStartEvent(new DccEventArgs(this));
int bytes;
if (directionUp)
{
do
{
while (Connection.Available > 0)
{
switch (speed)
{
case DccSpeed.Rfc:
Connection.GetStream().Read(buffer, 0, buffer.Length);
// TODO: only send x not ACKed Bytes ahead / (nobody wants this anyway)
break;
case DccSpeed.RfcSendAhead:
Connection.GetStream().Read(buffer, 0, buffer.Length);
break;
case DccSpeed.Turbo: // Available > 0 should not happen
break;
}
}
bytes = file.Read(buffer, 0, buffer.Length);
try
{
Connection.GetStream().Write(buffer, 0, bytes);
}
catch (IOException)
{
bytes = 0; // Connection Lost
}
sentBytes += bytes;
if (bytes > 0)
{
DccSendSentBlockEvent(new DccSendEventArgs(this, buffer, bytes));
Console.Write(".");
}
} while (bytes > 0);
}
else
{
while ((bytes = Connection.GetStream().Read(buffer, 0, buffer.Length)) > 0)
{
file.Write(buffer, 0, bytes);
sentBytes += bytes;
if (speed != DccSpeed.Turbo)
{
Connection.GetStream().Write(GetAck(sentBytes), 0, 4);
}
DccSendReceiveBlockEvent(new DccSendEventArgs(this, buffer, bytes));
}
}
IsValid = false;
IsConnected = false;
Console.WriteLine("--> Filetrangsfer Endet / Bytes sent: " + sentBytes + " of " + filesize);
DccSendStopEvent(new DccEventArgs(this));
}