Exemple #1
0
        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);
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        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();
            }
        }
Exemple #4
0
        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");
        }
Exemple #5
0
        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 + "\"");
        }