Пример #1
0
    public void SendJob(JobInput j, EndPoint i)
    {
        TcpClient client = null;

        lock (connectedClients)
        {
            if (!connectedClients.TryGetValue(i, out client))
            {
                if (OnErrorLog != null)
                {
                    OnErrorLog("Tried to send job to not present external client " + i);
                }
                return;
            }
        }
        try
        {
            lock (client)
            {
                var wrapped = new PlatformIndependentWrapper(client);

                wrapped.WriteInt((int)ServerOpcodes.NEW_JOB);
                wrapped.WriteString(j.Guid);
                wrapped.WriteString(j.Src);
                wrapped.WriteInt(j.Key.Length);
                wrapped.WriteBytes(j.Key);
                wrapped.WriteInt(j.LargerThen ? 1 : 0);
                wrapped.WriteInt(j.Size);
                wrapped.WriteInt(j.ResultSize);
            }
        }
        catch (Exception e)
        {
            if (OnErrorLog != null)
            {
                OnErrorLog("Got " + e.GetType() + " while sending job to " + i);
            }
            client.Close();
            // nothing more to do here. HandleClient will remove this from connectedClients
        }
    }
Пример #2
0
    private void HandleClient(object obj)
    {
        var      client = obj as TcpClient;
        EndPoint ep     = client.Client.RemoteEndPoint;

        bool   identified = false;
        String name       = string.Empty;

        try
        {
            var wrapped = new PlatformIndependentWrapper(client);
            while (true)
            {
                switch ((ClientOpcodes)wrapped.ReadInt())
                {
                case ClientOpcodes.HELLO:
                {
                    name = wrapped.ReadString();
                    String password = wrapped.ReadString();
                    if (OnClientAuth == null || !OnClientAuth(ep, name, password))
                    {
                        wrapped.WriteInt((int)ServerOpcodes.WRONG_PASSWORD);
                        return;
                    }
                    identified = true;
                }
                break;

                case ClientOpcodes.JOB_RESULT:
                {
                    if (!identified)
                    {
                        if (OnErrorLog != null)
                        {
                            OnErrorLog("Client '" + ep + "' tried to post result without identification");
                        }
                        return;
                    }

                    var jobGuid          = wrapped.ReadString();
                    var resultList       = new List <KeyValuePair <float, int> >();
                    var resultListLength = wrapped.ReadInt();
                    for (int c = 0; c < resultListLength; c++)
                    {
                        var key  = wrapped.ReadInt();
                        var cost = wrapped.ReadFloat();
                        resultList.Add(new KeyValuePair <float, int>(cost, key));
                    }

                    JobResult rs = new JobResult();
                    rs.Guid       = jobGuid;
                    rs.ResultList = resultList;

                    if (OnJobCompleted != null)
                    {
                        OnJobCompleted(ep, rs, name);
                    }
                }
                break;

                case ClientOpcodes.JOB_REQUEST:
                {
                    if (!identified)
                    {
                        if (OnErrorLog != null)
                        {
                            OnErrorLog("Client '" + ep + "' tried to request job without identification");
                        }
                        return;
                    }

                    if (OnClientRequestedJob != null)
                    {
                        OnClientRequestedJob(ep);
                    }
                }
                break;
                }
            }
        }
        catch (SocketException)
        {
            // left blank intentionally. Will be thrown on client disconnect.
        }
        catch (Exception e)
        {
            if (OnErrorLog != null)
            {
                OnErrorLog("Client '" + ep + "' caused exception " + e);
            }
        }
        finally
        {
            // just to be sure..
            client.Close();

            lock (connectedClients)
            {
                connectedClients.Remove(ep);
            }

            if (OnClientDisconnected != null)
            {
                OnClientDisconnected(ep);
            }
        }
    }