コード例 #1
0
ファイル: HClient.cs プロジェクト: yazici/atasow-soundhub
        public async Task <Guid> ConnectAsync(HostName hostName, string port)
        {
            if (nameResponse != null)
            {
                throw new InvalidOperationException("The client is connecting to another server");
            }

            nameResponse = new TaskCompletionSource <HClientResponseWithNameAndIP>();

            // Connect to the server
            DatagramSocket socket = new DatagramSocket();

            socket.Control.QualityOfService = SocketQualityOfService.LowLatency;
            socket.MessageReceived         += client_MessageReceived;
            await socket.ConnectAsync(hostName, port);

            // Create new information
            HServerInformation serverInf = new HServerInformation();

            serverInf.Socket    = socket;
            serverInf.Converter = new ShortStreamConverter(socket.OutputStream.AsStreamForWrite());

            // Ping the server
            serverInf.Converter.Write(clientCONNHeader);
            serverInf.Converter.Write(Encoding.UTF8.GetBytes(clientName));
            serverInf.Converter.Flush();

            // Wait for ping (or timeout)
            Task.WhenAny(nameResponse.Task, Task.Delay(CONNECT_TIMEOUT)).Wait();
            if (!nameResponse.Task.IsCompleted)
            {
                nameResponse.TrySetCanceled();
                socket.Dispose();

                Log(serverInf.Guid, "Connection timeout");

                nameResponse = null;

                return(default(Guid));
            }

            // Add new entry
            serverInf.Guid = nameResponse.Task.Result.GUID;
            serverInf.Name = nameResponse.Task.Result.Name;
            serverInf.IP   = nameResponse.Task.Result.IP;
            Log(serverInf.Guid, "Profile added");
            serverList.Add(serverInf);

            nameResponse = null;

            // Fire event
            if (Connected != null)
            {
                Connected(this, serverInf.Guid);
            }

            return(serverInf.Guid);
        }
コード例 #2
0
ファイル: HClient.cs プロジェクト: yazici/atasow-soundhub
        public HServerInformation GetServer(Guid guid)
        {
            HServerInformation serverInf = serverList.FirstOrDefault((inf) => inf.Guid == guid);

            if (serverInf.Guid == default(Guid))
            {
                throw new InvalidOperationException("Guid's not exist");
            }
            return(serverInf);
        }
コード例 #3
0
ファイル: HClient.cs プロジェクト: yazici/atasow-soundhub
        public void Close(Guid guid)
        {
            HServerInformation serverInf = serverList.FirstOrDefault((inf) => inf.Guid == guid);

            if (serverInf.Guid != default(Guid))
            {
                serverInf.Converter.Write(clientCLSEPacket);
                serverInf.Converter.Flush();
            }
        }
コード例 #4
0
ファイル: HClient.cs プロジェクト: yazici/atasow-soundhub
        private void Header_CLSE(BinaryReader reader)
        {
            Guid guid = new Guid(reader.ReadBytes(16));

            HServerInformation serverInf = serverList.FirstOrDefault((inf) => inf.Guid == guid);

            if (serverInf != null)
            {
                serverList.Remove(serverInf);
                if (Disconnected != null)
                {
                    Disconnected(this, serverInf.Guid);
                }
            }

            Log(guid, "CLOSE received");
        }
コード例 #5
0
ファイル: HClient.cs プロジェクト: yazici/atasow-soundhub
        private void Header_NAME(BinaryReader reader, string ip)
        {
            Guid   guid = new Guid(reader.ReadBytes(16));
            string name = reader.ReadString();

            HServerInformation serverInf = serverList.FirstOrDefault((inf) => inf.Guid == guid);

            if (serverInf != null)
            {
                serverInf.Name = name;
            }
            else if (nameResponse != null)
            {
                HClientResponseWithNameAndIP response = new HClientResponseWithNameAndIP()
                {
                    GUID = guid,
                    Name = name,
                    IP   = ip
                };
                nameResponse.SetResult(response);
            }

            Log(guid, "NAME received: \"" + name + "\"");
        }