Я написал асинхронный сервер, используя сокет .Net, и когда этот сервер перегружен (получает много данных, например, файл), все пакеты не принимаются сервером.
Если яувеличить длину буфера, я получаю больше данных, но не все пакеты.
У кого-нибудь есть решение моей проблемы?
Код моего сервера:
class ServerFile
{
#region Delegate
private delegate void Method(ConnectionInfoFile Infos, Object Content);
#endregion
#region Fields
private Int32 Port;
private Socket ServerSocket = null;
private List<ConnectionInfoFile> Connections = new List<ConnectionInfoFile>();
private Dictionary<Command, Method> Map = new Dictionary<Command, Method>();
#endregion
#region Constructor
public ServerFile(Int32 Port)
{
this.Port = Port;
this.Map[Command.Server_User_Login] = this.Login;
}
#endregion
#region Methods - Login
private void Login(ConnectionInfoFile Client, Object Content)
{
Client.Id = (Int32)Content;
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - The client is now authentified as '" + Client.Id + "'.");
}
#endregion
#region Methods - Receive
private void ReceiveCallback(IAsyncResult Result)
{
ConnectionInfoFile Connection = (ConnectionInfoFile)Result.AsyncState;
try
{
Int32 BytesRead = Connection.Socket.EndReceive(Result);
if (BytesRead != 0)
{
Byte[] Message = (new MemoryStream(Connection.AsyncBuffer, 0, BytesRead)).ToArray();
Connection.Append(Message);
Packet Packet;
while ((Packet = Connection.TryParseBuffer()) != null)
{
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - Command received : " + Packet.Command.ToString());
if (Packet.Recipient == 0)
{
if (this.Map.ContainsKey(Packet.Command))
this.Map[Packet.Command](Connection, Packet.Content);
}
else
{
foreach (ConnectionInfoFile Item in this.Connections)
{
if (Item.Id == Packet.Recipient)
{
Byte[] Buffer = Packet.Serialize(Connection.Id, Packet.Command, Packet.Content);
this.Send(Item, Buffer);
}
}
}
}
Connection.Socket.BeginReceive(Connection.AsyncBuffer, 0, Connection.AsyncBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), Connection);
}
else
this.CloseConnection(Connection);
}
catch (SocketException E)
{
CloseConnection(Connection);
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - Receive socket exception : " + E.SocketErrorCode);
}
catch (Exception E)
{
CloseConnection(Connection);
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - Receive exception : "+ E.Message);
}
}
#endregion
#region Methods - CloseConnection
private void CloseConnection(ConnectionInfoFile Connection)
{
Connection.Socket.Close();
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - The user " + Connection.Id + " has been disconnected !");
lock (this.Connections)
{
this.Connections.Remove(Connection);
}
}
#endregion
#region Methods - Send
protected void Send(IConnectionInfo Connection, Byte[] Buffer)
{
Connection.Socket.BeginSend(Buffer, 0, Buffer.Length, 0, new AsyncCallback(SendCallback), Connection);
}
private static void SendCallback(IAsyncResult Result)
{
try
{
IConnectionInfo Connection = (IConnectionInfo)Result.AsyncState;
Int32 BytesSent = Connection.Socket.EndSend(Result);
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + typeof(ServerFile).Name.Insert(6, " ") + " - Sent " + BytesSent + " bytes " + Connection.Id.ToString() + ".");
}
catch (Exception E)
{
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + typeof(ServerFile).GetType().Name.Insert(6, " ") + " - Exception : " + E.ToString());
}
}
#endregion
#region Methods - Stop
public void Stop()
{
this.ServerSocket.Close();
this.ServerSocket = null;
}
#endregion
#region Methods - Start
public void Start()
{
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - Start listening on port " + this.Port + "...");
this.ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.ServerSocket.Bind(new IPEndPoint(IPAddress.Any, this.Port));
this.ServerSocket.Listen((Int32)SocketOptionName.MaxConnections);
for (Int32 i = 0; i < 5; i++)
this.ServerSocket.BeginAccept(new AsyncCallback(this.AcceptCallback), this.ServerSocket);
}
#endregion
#region Methods - Accept
private void AcceptCallback(IAsyncResult Result)
{
ConnectionInfoFile Connection = new ConnectionInfoFile();
Socket Socket = (Socket)Result.AsyncState;
try
{
Connection.Socket = Socket.EndAccept(Result);
Connection.ConnectionDate = DateTime.Now;
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - A new client has been registered");
lock (this.Connections)
{
this.Connections.Add(Connection);
}
Connection.Socket.BeginReceive(Connection.AsyncBuffer, 0, Connection.AsyncBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), Connection);
}
catch (SocketException E)
{
this.CloseConnection(Connection);
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - Accept socket exception: " + E.SocketErrorCode);
}
catch (Exception E)
{
this.CloseConnection(Connection);
Console.WriteLine("[" + DateTime.Now.ToShortTimeString() + "] - " + this.GetType().Name.Insert(6, " ") + " - Accept exception: " + E.Message);
}
Socket.BeginAccept(new AsyncCallback(AcceptCallback), Result.AsyncState);
}
#endregion
}
Извините за мой английский и большое спасибо.
NeoKript