Esempio n. 1
0
 private void RemoveClient(SocketClientBase cl)
 {
     clients.Remove(cl.Id);
     iclients.Remove(cl);
     if (cl.Login != null)
     {
         nameToClient.Remove(cl.Login);
     }
 }
Esempio n. 2
0
        private bool ReceiveMessage(MsgHeader hdr, SocketClientBase cl, out IMessage msg)
        {
            msg = null;
            // XXX: could be optimized - use one large buffer + unclosable MemoryStream
            var buf = new byte[hdr.Size];

            try
            { cl.NetStream.Read(buf, 0, buf.Length); }
            catch (IOException e)
            {
                if (NetUtil.CheckConnectionReset(e))
                {
                    OnClientConnectionReset(cl);
                    return(false);
                }
                throw;
            }
            using (var ms = new MemoryStream(buf))
            {
                var msWrap = ms.AsUnclosable();
                if ((hdr.Flags & MessageFlags.Secure) == MessageFlags.Secure)
                {
                    using (var container = new MessageCryptoContainer())
                    {
                        container.Load(msWrap, Protocol);
                        symCp.Key = cl.SessionKey;
                        msg       = container.Extract(hdr.Id, symCp, Protocol);
                    }
                }
                else
                {
                    msg = MessageFactory.CreateMessage(hdr.Id);
                    try
                    { msg.Load(msWrap, Protocol); }
                    catch (MessageLoadException e)
                    {
                        Root.Log(LogLevel.Warning, "Client {0} : bad message ({1})",
                                 GetClientName(cl), e.Message);
                        DisconnectClient(cl, DisconnectReason.Unknown, "bad message");
                        return(false);
                    }
                }
            }
            return(true);
        }
Esempio n. 3
0
 private void DisconnectClient(SocketClientBase cl)
 {
     if (cl.DeferredDisconnect)
     {
         return;
     }
     Root.Log(LogLevel.Info, "Server: disconnecting client: " + GetClientName(cl));
     cl.DeferredDisconnect = true;
     delClients.Enqueue(cl);
     cl.Params.Socket.Shutdown(SocketShutdown.Both);
     cl.Params.Socket.Close();
     if (cl.Authenticated)
     {
         var msg = new SvUserlistUpdate();
         msg.Disconnected = new[] { cl.Login };
         SendBroadcast(cl.Id, msg);
     }
 }
Esempio n. 4
0
 private bool ReceiveMessageHeader(MsgHeader hdr, SocketClientBase cl)
 {
     try
     { hdr.Load(cl.NetStream, Protocol); }
     catch (MessageLoadException e)
     {
         if (NetUtil.CheckConnectionReset(e))
         {
             OnClientConnectionReset(cl);
         }
         else
         {
             Root.Log(LogLevel.Warning, "Client {0} : bad message header ({1})",
                      GetClientName(cl), e.Message);
             DisconnectClient(cl, DisconnectReason.Unknown, "bad message header");
         }
         return(false);
     }
     return(true);
 }
Esempio n. 5
0
 private void AddClient(SocketClientBase cl)
 {
     clients.Add(cl.Id, cl);
     iclients.Add(cl);
 }
Esempio n. 6
0
        private bool CheckClientConnection(SocketClientBase cl)
        {
            var s = cl.Params.Socket;

            return(s.Connected && !(s.Poll(1, SelectMode.SelectRead) && s.Available == 0));
        }
Esempio n. 7
0
 private void OnClientConnectionReset(SocketClientBase cl)
 {
     Root.Log(LogLevel.Info, "Client {0} : connection lost", GetClientName(cl));
     DisconnectClient(cl);
 }