i have server application that handle request all received command from clinets, every things is ok in response when my clients are blow 1000 after my clients connect more than 1000 this service work slowly and will not response in good time more than 20sec... and after 20sec my clients time out error, i dont want increse time out more than 20sec... also if i run this app in seprate port every things ok in more client, i must run in one app, where is my mistake?
static class Program {
static void Main(string[] args) {
TcpListener baseListernerNewVer = new TcpListener(new System.Net.IPEndPoint(IPAddress.Any, 1425));
baseListernerNewVer.Start();
BeginAccept(baseListernerNewVer);
while (true) {
System.Threading.Thread.Sleep(1);
}
}
}
//Wait For New Clinet
private static void BeginAccept(TcpListener baseListernerNewVer) {
baseListernerNewVer.BeginAcceptTcpClient((ar) => {
while (true) {
try {
BeginAccept(baseListernerNewVer);
break;
}
catch(Exception e) {
System.Threading.Thread.Sleep(100);
}
}
//All Client Will Manage Here
Manage(baseListernerNewVer.EndAcceptTcpClient(ar));
}, baseListernerNewVer);
}
public void Manage(TcpClient tcpClient) {
GC.Collect();
var onlineClientInfo = new OnlineClientInfo(tcpClient);
try {
lock (onlineClientsInfos) {
onlineClientsInfos.Add(onlineClientInfo);
}
System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback((object state) => {
System.Threading.Thread.Sleep(1000);
while (tcpClient.Connected && onlineClientInfo.IsConnect && service1.Running) {
try {
if (FeedMeMessage(onlineClientInfo)) {
System.Threading.Thread.Sleep(1000);
}
else {
System.Threading.Thread.Sleep(1000);
}
}
catch (Exception e) {
}
}
}));
//Wait from command form clients
CallGetNextCommand(onlineClientInfo);
}
catch (Exception e) {
}
finally {
}
}
private void DisconnectFromClient(OnlineClientInfo onlineClientInfo) {
lock (onlineClientsInfos) {
onlineClientsInfos.Remove(onlineClientInfo);
try {
onlineClientInfo.TcpClient.Close();
}
catch {
}
}
}
private void ReadNextSocketBuffer(OnlineClientInfo onlineClientInfo, DateTime baseDateAndTime, byte[] buffer, int offset, int length, Action fullBufferFilled) {
TcpClient tcpClient = onlineClientInfo.TcpClient;
try {
tcpClient.Client.BeginReceive(buffer, offset, length, SocketFlags.Partial, (ar) => {
int len = 0;
try {
len = tcpClient.Client.EndReceive(ar);
if (len == 0) {
if (DateTime.Now > baseDateAndTime.AddMinutes(1)) {
DisconnectFromClient(onlineClientInfo);
return;
}
System.Threading.Thread.Sleep(1);
}
}
catch {
DisconnectFromClient(onlineClientInfo);
}
if (offset + len == buffer.Length) {
fullBufferFilled();
}
else {
int newOffset = offset + len;
ReadNextSocketBuffer(onlineClientInfo, baseDateAndTime, buffer, newOffset, buffer.Length - newOffset, fullBufferFilled);
}
}, null);
}
catch {
DisconnectFromClient(onlineClientInfo);
}
}
private void ReadSocketLength(OnlineClientInfo onlineClientInfo, int length, Action<byte[]> readedToLength) {
byte[] buffer = new byte[length];
ReadNextSocketBuffer(onlineClientInfo, DateTime.Now, buffer, 0, length, () => {
try {
readedToLength(buffer);
}
catch { }
});
}
private void ReadCommandLength(OnlineClientInfo onlineClientInfo, Action<int> length) {
ReadSocketLength(onlineClientInfo, 4, (commandLength) => {
try {
length(Common.ToInt(commandLength));
}
catch { }
});
}
private void ReadCommand(OnlineClientInfo onlineClientInfo, int commandLength, Action<byte[]> commandResult) {
ReadSocketLength(onlineClientInfo, commandLength, (command) => {
try {
commandResult(command);
}
catch { }
});
}
private void GetNextCommand(OnlineClientInfo onlineClientInfo, Action finishedExecute) {
ReadCommandLength(onlineClientInfo, (commandLength) => {
ReadCommand(onlineClientInfo, 1, (commandHeader) => {
if (commandLength == 2 && (ServerCommandType)commandHeader[0] == ServerCommandType.AckVer1) {
ReadCommand(onlineClientInfo, 1, (newCommandHeader) => {
try {
byte ackCode = newCommandHeader[0];
onlineClientInfo.AckReceived(ackCode);
}
catch { }
finishedExecute();
});
}
else {
ReadCommand(onlineClientInfo, commandLength, (command) => {
try {
if (ExecuteCommand(command, onlineClientInfo)) {
onlineClientInfo.SendAck(commandHeader[0]);
}
}
catch {
}
finishedExecute();
});
}
});
});
}
private void CallGetNextCommand(OnlineClientInfo onlineClientInfo) {
TcpClient tcpClient = onlineClientInfo.TcpClient;
Action finishedExecute = null;
finishedExecute = () => {
if (tcpClient.Connected && onlineClientInfo.IsConnect) {
try {
GetNextCommand(onlineClientInfo, finishedExecute);
}
catch {
}
}
else {
DisconnectFromClient(onlineClientInfo);
}
};
finishedExecute();
}