/** * Called to notify this provider for an incoming message. * @param event the event object that contains the new message. */ public virtual void HandleMessageEvent(StunMessageEvent @event) { Message msg = @event.GetMessage(); //request if (msg is Request) { TransactionID serverTid = TransactionID. CreateTransactionID(msg.GetTransactionID()); serverTransactions.Add(serverTid); if (requestListener != null) { requestListener.requestReceived(@event); } } //response else if (msg is Response) { TransactionID tid = TransactionID. CreateTransactionID(msg.GetTransactionID()); StunClientTransaction tran = (StunClientTransaction)clientTransactions[tid]; clientTransactions.Remove(tid); if (tran != null) { tran.HandleResponse(@event); } else { //do nothing - just drop the phantom response. } } }
/** * Creates a transaction identifier object with the specified id. * @param transactionID the id to give to the new TransactionID * @return a new TransactionID object with the specified id value; */ public static TransactionID CreateTransactionID(byte[] transactionID) { TransactionID tid = new TransactionID(); for (int x = 0; x < 16; x++) { tid.transactionID[x] = transactionID[x]; } //calculate hashcode for Hashtable storage. uint foo = tid.transactionID[3]; foo <<= 24; foo &= 0xFF000000; tid.hashCode = (int)foo; int tmp; tmp = tid.transactionID[2]; tmp <<= 16; tmp &= 0xFF0000; tid.hashCode |= tmp; tmp = tid.transactionID[1]; tmp <<= 8; tmp &= 0xFF00; tid.hashCode |= tmp; tmp = tid.transactionID[0]; tmp &= 0xFF; tid.hashCode |= tmp; return(tid); }
/** * Creates a client transaction * @param providerCallback the provider that created us. * @param request the request that we are living for. * @param requestDestination the destination of the request. * @param apDescriptor the access point through which we are supposed to * @param responseCollector the instance that should receive this request's * response. * retransmit. */ public StunClientTransaction(StunProvider providerCallback, Request request, StunAddress requestDestination, NetAccessPointDescriptor apDescriptor, ResponseCollector responseCollector) { this.providerCallback = providerCallback; this.request = request; this.apDescriptor = apDescriptor; this.responseCollector = responseCollector; this.requestDestination = requestDestination; this.transactionID = TransactionID.CreateTransactionID(); request.SetTransactionID(transactionID.GetTransactionID()); runningThread = new Thread(new ThreadStart(this.Run)); }
/** * Sends the specified response message through the specified access point. * * @param transactionID the id of the transaction to use when sending the * response. Actually we are getting kind of redundant here as we already * have the id in the response object, but I am bringing out as an extra * parameter as the user might otherwise forget to explicitly set it. * @param response the message to send. * @param sendThrough the access point to use when sending the message. * @param sendTo the destination of the message. * @throws StunException TRANSACTION_DOES_NOT_EXIST if the response message * has an invalid transaction id. <br/> * ILLEGAL_STATE if the stun stack is not started. <br/> * ILLEGAL_ARGUMENT if the apDescriptor references an access point that had * not been installed <br/> * NETWORK_ERROR if an error occurs while sending message bytes through the * network socket. <br/> */ public virtual void SendResponse(byte[] transactionID, Response response, NetAccessPointDescriptor sendThrough, StunAddress sendTo) { stunStack.CheckStarted(); TransactionID tid = TransactionID.CreateTransactionID(transactionID); serverTransactions.Remove(tid); #if false throw new StunException(StunException.TRANSACTION_DOES_NOT_EXIST, "The trensaction specified in the response " + "object does not exist."); #endif response.SetTransactionID(transactionID); GetNetAccessManager().SendMessage(response, sendThrough, sendTo); }
/** * Creates a transaction id object.The transaction id itself is genereated * using the folloing algorithm: * * The first 8 bytes of the id are given the value of System.currentTimeMillis() * Putting the right most bits first so that we get a more optimized equals() * method. * * @return A TransactionID object with a unique transaction id. */ public static TransactionID CreateTransactionID() { TransactionID tid = new TransactionID(); long left = DateTime.Now.Ticks; //the first 8 bytes of the id long right = random.Next(); //the last 8 bytes of the id right *= 0x100000000L; right += Math.Abs(random.Next()); for (int i = 0; i < 8; i++) { tid.transactionID[i] = (byte)((left >> (i * 8)) & 0xFFL); tid.transactionID[i + 8] = (byte)((right >> (i * 8)) & 0xFFL); } //calculate hashcode for Hashtable storage. uint foo = tid.transactionID[3]; foo <<= 24; foo &= 0xFF000000; tid.hashCode = (int)foo; int tmp; tmp = tid.transactionID[2]; tmp <<= 16; tmp &= 0xFF0000; tid.hashCode |= tmp; tmp = tid.transactionID[1]; tmp <<= 8; tmp &= 0xFF00; tid.hashCode |= tmp; tmp = tid.transactionID[0]; tmp &= 0xFF; tid.hashCode |= tmp; return(tid); }