Example #1
0
 public Task <MessageConnection> GetConnection()
 {
     if (!IsConnected)
     {
         lock (sync)
         {
             if (!connecting && !IsConnected)
             {
                 connecting     = true;
                 tcp            = new TcpClient();
                 connectionTask = Task.Run(async() =>
                 {
                     try
                     {
                         await tcp.ConnectAsync(client.address, client.port);
                         if (client.KeepAlive)
                         {
                             tcp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                         }
                         tcp.NoDelay     = true;
                         tcp.SendTimeout = client.CommandTimeout;
                         connection      = new MessageConnection(tcp, (cn, msg) => client.OnMessage(cn, msg));
                         connection.Start();
                         if (expireMs > 0)
                         {
                             expireTime = DateTime.Now.AddMilliseconds(expireMs);
                         }
                         return(connection);
                     }
                     finally
                     {
                         connecting = false;
                     }
                 });
             }
         }
     }
     return(connectionTask);
 }
Example #2
0
 void Accept()
 {
     var nowait = listener.AcceptTcpClientAsync().ContinueWith(t =>
     {
         if (!t.IsFaulted)
         {
             Accept();
             var tcp = t.Result;
             tcp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
             tcp.NoDelay     = true;
             tcp.SendTimeout = CommandTimeout;
             var connection  = new MessageConnection(tcp, (cn, msg) => { try { OnMessage(cn, msg); } catch { } });
             lock (tcpConnections) tcpConnections.Add(new TcpConnectionInfo {
                     server = tcp, connection = connection
                 });
             connection.Start();
         }
         else
         {
             tcsDone.SetResult(true);
         }
     });
 }
Example #3
0
 protected virtual Task OnMessage(MessageConnection connection, Message message)
 {
     return(Task.FromResult(true));
 }
Example #4
0
 protected abstract Task OnMessage(MessageConnection connection, Message message);