dotnet-core_mail-server/MailServer/StartTcpConnection.cs
2016-12-09 15:41:35 +01:00

75 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace SMTPServer
{
class StartTcpConnection : Socket
{
private List<string> Lines = new List<string>();
private int LinesAvailable { get
{
return Lines.Count;
}
}
private string Others { get; set; }
private Encoding Encoding { get; set; }
public StartTcpConnection(int port, IPAddress destination, Encoding encoding) : base (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
{
this.Connect(new IPEndPoint(destination, port));
Thread readLines = new Thread(new ThreadStart(ReadClientInput));
}
private void ReadClientInput()
{
while (true)
{
byte[] buffer = new byte[ReceiveBufferSize];
Receive(buffer);
var str = Encoding.GetString(buffer);
lock (Others)
{
Others += str;
}
CheckLines();
}
}
private void CheckLines()
{
lock (Others)
{
var line = "";
foreach (char c in Others)
{
if (c.Equals('\n'))
{
lock (Lines)
{
Lines.Add(line);
line = "";
}
}
else line += c;
}
Others = line;
}
}
public string GetLine()
{
lock (Lines)
{
return Lines[0];
}
}
}
}