Esempio n. 1
0
File: Peer.cs Progetto: n-suudai/Lab
        public void Send(RemoteEntity remoteEntity)
        {
            if (client == null)
            {
                return;
            }

            sendQueue.Enqueue(remoteEntity);
            enqueuedWaitHandle.Set();
        }
Esempio n. 2
0
File: Peer.cs Progetto: n-suudai/Lab
        private void SendMain()
        {
            try
            {
                Serializer serializer = new Serializer(client.GetStream());

                while (client.Connected)
                {
                    // キューに追加されるのを待つ
                    enqueuedWaitHandle.WaitOne();
                    enqueuedWaitHandle.Reset();

                    RemoteEntity remoteEntity = null;
                    if (sendQueue.TryDequeue(out remoteEntity))
                    {
                        if (remoteEntity == null)
                        {
                            break;
                        }                                    // null が来た時点で終了

                        serializer.WriteString(remoteEntity.Name);

                        remoteEntity.Serialize(serializer);

                        // post sended
                        if (synchronization != null)
                        {
                            synchronization.Post(_ => Sended?.Invoke(remoteEntity), null);
                        }
                        else
                        {
                            Sended?.Invoke(remoteEntity);
                        }
                    }
                }
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            catch (IOException e)
            {
                Console.WriteLine("IOException: {0}", e);
            }
            finally
            {
            }

            client.Close();
        }
Esempio n. 3
0
File: Peer.cs Progetto: n-suudai/Lab
        private void ReceiveMain()
        {
            try
            {
                Serializer serializer = new Serializer(client.GetStream());

                while (client.Connected)
                {
                    string entityName = serializer.ReadString();

                    RemoteEntity remoteEntity = RemoteEntity.Create(entityName);

                    if (remoteEntity == null)
                    {
                        break;
                    }

                    remoteEntity.Deserialize(serializer);

                    // post received
                    if (synchronization != null)
                    {
                        synchronization.Post(_ => Received?.Invoke(remoteEntity), null);
                    }
                    else
                    {
                        Received?.Invoke(remoteEntity);
                    }
                }
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            catch (IOException e)
            {
                Console.WriteLine("IOException: {0}", e);
            }
            finally
            {
            }

            Terminate();
        }
Esempio n. 4
0
        public static RemoteEntity Create(string name)
        {
            Type type = Type.GetType(name);

            if (type == null)
            {
                return(null);
            }

            ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);

            object instance = constructor.Invoke(null);

            RemoteEntity remoteEntity = instance as RemoteEntity;

            return(remoteEntity);
        }