dotnet-core_mail-server/MailServer/StartTcpConnection.cs

75 lines
1.8 KiB
C#
Raw Normal View History

2016-11-21 19:24:22 +00:00
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
2016-11-21 19:24:22 +00:00
using System.Text;
using System.Threading;
2016-11-21 19:24:22 +00:00
namespace SMTPServer
{
class StartTcpConnection : Socket
2016-11-21 19:24:22 +00:00
{
2016-11-26 11:11:27 +00:00
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()
2016-11-21 19:24:22 +00:00
{
while (true)
{
2016-11-26 11:11:27 +00:00
byte[] buffer = new byte[ReceiveBufferSize];
Receive(buffer);
2016-11-26 11:11:27 +00:00
var str = Encoding.GetString(buffer);
lock (Others)
{
2016-11-26 11:11:27 +00:00
Others += str;
}
CheckLines();
}
2016-11-21 19:24:22 +00:00
}
private void CheckLines()
{
2016-11-26 11:11:27 +00:00
lock (Others)
{
var line = "";
2016-11-26 11:11:27 +00:00
foreach (char c in Others)
{
if (c.Equals('\n'))
{
2016-11-26 11:11:27 +00:00
lock (Lines)
{
2016-11-26 11:11:27 +00:00
Lines.Add(line);
line = "";
}
}
else line += c;
}
2016-11-26 11:11:27 +00:00
Others = line;
}
}
public string GetLine()
{
2016-11-26 11:11:27 +00:00
lock (Lines)
{
2016-11-26 11:11:27 +00:00
return Lines[0];
}
}
2016-11-21 19:24:22 +00:00
}
}