コード例 #1
0
 internal void SendNonReturn(string ClientMethodName, byte[] data)
 {
     try
     {
         lock (lockObject)
         {
             var packet = new SocketyPacket()
             {
                 MethodName = ClientMethodName, PackData = data
             };
             var d = MessagePackSerializer.Serialize(packet);
             if (serverSocket != null)
             {
                 PacketSize size  = PacketSize.Create(d.Length);
                 var        sizeb = size.GetBytes();
                 commnicateStream.Write(sizeb, 0, sizeb.Length);
                 commnicateStream.Write(d, 0, d.Length);
             }
         }
     }
     catch (IOException ex)
     {
         //Logger.LogError($"SendNonReturn:{ex.ToString()}");
         return;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
コード例 #2
0
        private void SendHeartBeat()
        {
            try
            {
                lock (lockObject)
                {
                    var packet = new SocketyPacket()
                    {
                        SocketyPacketType = SocketyPacket.SOCKETY_PAKCET_TYPE.HaertBeat
                    };
                    var        d     = MessagePackSerializer.Serialize(packet);
                    PacketSize size  = PacketSize.Create(d.Length);
                    var        sizeb = size.GetBytes();
                    commnicateStream.Write(sizeb, 0, sizeb.Length);

                    commnicateStream.Write(d, 0, d.Length);
                }
            }
            catch (IOException ex)
            {
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #3
0
        /// <summary>
        /// 受信を一括して行う
        /// </summary>
        private void ReceiveProcess()
        {
            byte[] sizeb = new byte[sizeof(int)];

            Logger.LogInformation("ReceiveProcess Start");
            while (_stoppingCts.IsCancellationRequested == false)
            {
                try
                {
                    if (serverSocket == null)
                    {
                        TcpReceiveThreadFinishEvent.Set();
                        return;
                    }

                    int bytesRec = commnicateStream.Read(sizeb, 0, sizeof(int));
                    lock (lockObject)
                    {
                        if (bytesRec > 0)
                        {
                            int    size     = BitConverter.ToInt32(sizeb, 0);
                            byte[] buffer   = new byte[size];
                            int    DataSize = 0;
                            do
                            {
                                bytesRec = commnicateStream.Read(buffer, DataSize, size - DataSize);

                                DataSize += bytesRec;
                            } while (size > DataSize);

                            var packet = MessagePackSerializer.Deserialize <SocketyPacket>(buffer);

                            //AuthentificationFilter
                            bool AuthentificationSuccess = true;
                            var  authentificationFilter  = SocketyFilters.Get <IAuthenticationFIlter>();
                            var  method = GetMethod(packet);

                            if (packet.SocketyPacketType == SocketyPacket.SOCKETY_PAKCET_TYPE.Data && authentificationFilter != null)
                            {
                                bool FindIgnore = false;

                                if (method.GetCustomAttribute <SocketyAuthentificationIgnoreAttribute>() != null)
                                {
                                    //SocketyAuthentificationIgnoreがあるメソッドは認証を行わない
                                    FindIgnore = true;
                                    AuthentificationSuccess = true;
                                }

                                if (FindIgnore == false)
                                {
                                    AuthentificationSuccess = authentificationFilter.Authentication(packet.Toekn);
                                }
                            }

                            if (AuthentificationSuccess == true)
                            {
                                if (packet.SocketyPacketType == SocketyPacket.SOCKETY_PAKCET_TYPE.HaertBeat)
                                {
                                    ReceiveHeartBeat();
                                }
                                else
                                {
                                    //メソッドの戻り値を詰め替える
                                    packet.PackData = InvokeMethod(method, packet);

                                    //InvokeMethodAsyncの戻り値を送り返す
                                    var        d          = MessagePackSerializer.Serialize(packet);
                                    PacketSize packetSize = PacketSize.Create(d.Length);
                                    var        sizeb2     = packetSize.GetBytes();
                                    commnicateStream.Write(sizeb2, 0, sizeb2.Length);
                                    commnicateStream.Write(d, 0, d.Length);
                                }
                            }
                            else
                            {
                                Logger.LogInformation($"Client Authentificateion Fail \r\n{packet.clientInfo.ToString()}");
                                //認証失敗は接続を切断
                                DisConnect();
                            }
                        }
                    }
                }
                catch (IOException ex)
                {
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex.ToString());
                }
                Thread.Sleep(10);
            }
        }