Exemplo n.º 1
0
 public override void Send(Message message)
 {
     if (message.Type == MessageType.NormalConnection)
     {
         Log.debug("Service {0} : Send {1} bytes of data", this.localId, message.Length);
         try { tcpClient.GetStream().Write(message.data); }
         catch { sendConnectionEnd(); Close(); }
     }
     else if (message.Type == MessageType.EndConnection)
     {
         Log.debug("Service {0} : Receive Connection closed message. connection closed", this.localId);
         onClosing = true;
         try
         {
             tcpClient.GetStream().Write(new byte[0], 0, 0);
             tcpClient.Close();
         }
         finally
         {
             Close();
         }
     }
     else if (message.Type == MessageType.NewConnection)
     {
         connection = message.From;
         HandleLoop.addJob(handleRead);
         // try { tcpClient.GetStream().Write(message.data); }
         // catch { sendConnectionEnd(); Close(); }
     }
 }
Exemplo n.º 2
0
        public ClientConnection(TcpClient tcpClient)
        {
            HttpHeader header;

            this.tcpClient = tcpClient;
            try { header = readHeader(tcpClient); }
            catch { return; }

            Log.debug("Client {0} : New Connection, Routing to {1}", this.localId, destinedService);

            connection = NodeManager.shortestPathTo(destinedService);
            if (connection == null)
            {
                Log.error("Client {0} : Can not make new connection to {1}", this.localId, destinedService);
                header.closeWithNotFound();
                return;
            }

            Message message = new Message
            {
                From = this,
                Type = MessageType.NewConnection,
                URL  = this.destinedService,
                data = Encoding.UTF8.GetBytes(this.destinedService),
            };

            connection.Send(message);

            message = new Message
            {
                From = this,
                Type = MessageType.NormalConnection,
                URL  = this.destinedService,
                data = header.getBytes(),
            };
            connection.Send(message);

            HandleLoop.addJob(handleRead);
        }