Exemplo n.º 1
0
        private void ListenClient(System.IAsyncResult ar)
        {
            Socket _listener = (Socket)ar.AsyncState;
            Socket client    = null;

            try
            {
                client = _listener.EndAccept(ar);
                _listener.BeginAccept(ListenClient, _listener);
            }
            catch (Exception e)
            {
                DebugMessage(e.ToString());
            }

            if (client != null)
            {
                #region Set def var
                int defReadTimeout = client.ReceiveTimeout;
                #endregion

                #region Set Read func
                SendData onRead(PacketType checkType, string key, Func <SendData, bool> datacheck)
                {
                    SendData ReadData = new SendData();

                    try
                    {
                        client.ReceiveTimeout = 1000;

                        byte[] data = new byte[Packet.header_length];
                        if (client.Receive(data) <= 0)
                        {
                            Disconnect(client);
                            return(new SendData());
                        }

                        client.ReceiveTimeout = defReadTimeout;

                        int len = BitConverter.ToInt32(data, 0);
                        data = ReadSocketData(len, client);

                        using (Packet packet = new Packet(client, data, null, true))
                        {
                            if (packet.BeginRead() != checkType)
                            {
                                Disconnect(client);
                                return(new SendData());
                            }
                            ReadData = packet.ReadSendData(key);
                        }
                    }
                    catch (Exception)
                    {
                        Disconnect(client);
                        return(new SendData());
                    }

                    if (!datacheck(ReadData))
                    {
                        Disconnect(client);
                        return(new SendData());
                    }
                    return(ReadData);
                }

                #endregion

                #region Set Send func
                void onSend(PacketType sendType, string key, EncryptAndCompress.LockType lockType, SendData send)
                {
                    using (Packet packet = new Packet(client))
                    {
                        packet.BeginWrite(sendType);
                        packet.WriteSendData(send, key, lockType);
                        Send(packet, client);
                    }
                }

                #endregion

                #region Send RSA key
                onSend(PacketType.RSAKEY, "", EncryptAndCompress.LockType.None, new SendData(0, RSAkey.PublicKeyBytes));
                #endregion

                #region Get AES key
                SendData sendData = onRead(PacketType.AESKEY, RSAkey.PrivateKey, (a) => true);
                if (sendData == new SendData())
                {
                    return;
                }

                string AESkey = (string)sendData.Parameters;
                #endregion

                #region Send AES Check
                onSend(PacketType.AESKEY, AESkey, EncryptAndCompress.LockType.AES, new SendData(0, "Connect check"));
                #endregion

                #region Get CONNECT_SUCCESSFUL
                sendData = onRead(PacketType.CONNECT_SUCCESSFUL, AESkey, (a) => a.Parameters.ToString() == "Connect successful");
                if (sendData == new SendData())
                {
                    return;
                }
                #endregion

                #region On CONNECT
                PushPacket(PacketType.CONNECT_SUCCESSFUL, AESkey, client);

                byte[] bytes = new byte[Packet.header_length];
                client.BeginReceive(bytes, 0, Packet.header_length, SocketFlags.None, Receive, new object[] { client, bytes });
                #endregion
            }
        }
Exemplo n.º 2
0
        private void Receive(IAsyncResult ar)
        {
            object[] state  = (object[])ar.AsyncState;
            Socket   client = (Socket)state[0];

            byte[]     data   = (byte[])state[1];
            PacketType msgid  = (PacketType)(-1);
            Packet     packet = null;

            try
            {
                int len = client.EndReceive(ar);

                if (!SocketList.Contains(ToPeer[SocketToEndPoint[client]]))
                {
                    PushPacket(PacketType.CONNECTION_LOST, "連線已關閉", client);
                    return;
                }

                // 伺服器斷開連接
                if (len < 1)
                {
                    PushPacket(PacketType.CONNECTION_LOST, "遠端已斷線", client);
                    return;
                }

                byte[] databody = ReadSocketData(BitConverter.ToInt32(data, 0), client);
                packet = new Packet(client, databody, null, true);
                msgid  = packet.BeginRead();
                packet.ResetPosition();

                if (data.Length != Packet.header_length)
                {
                    data = new byte[Packet.header_length];
                }
                client.BeginReceive(data, 0, Packet.header_length, SocketFlags.None, Receive, new object[] { client, data });
            }
            catch (Exception e)
            {
                PushPacket(PacketType.CONNECTION_LOST, e.ToString(), client);
                return;
            }
            if (msgid > PacketType.SendAllowTypeTop && msgid < PacketType.SendAllowTypeEnd)
            {
                PushPacket(packet);
            }
            else
            {
                switch (msgid)
                {
                case PacketType.CONNECTION_LOST:
                {
                    packet.BeginRead();
                    SendData sendData = packet.ReadSendData("");
                    PushPacket(PacketType.CONNECTION_LOST, sendData.DebugMessage, client);
                    packet.CloseStream();
                    break;
                }

                case PacketType.ClientDebugMessage:
                {
                    packet.BeginRead();
                    SendData sendData = packet.ReadSendData(SocketToKey[packet.peer]);
                    DebugMessage("ClientDebugMessage:" + packet.peer.ToString() + " " + sendData.DebugMessage);

                    packet.CloseStream();
                    break;
                }

                default:
                {
                    PushPacket(PacketType.CONNECTION_LOST, "不正確的標頭資訊 Receive", client);
                    packet.CloseStream();
                    break;
                }
                }
            }
        }