private void HandleRead(SelectionKey key) { try { c = (DatagramChannel)key.Channel(); b = (UDPBuffer)key.Attachment(); b.buffer.Clear(); b.socketAddress = c.Receive(b.buffer); b.buffer.Flip(); b.buffer.Get(buff, 0, b.buffer.Limit()); MessagingCenter.Send <PacketEvent>(new PacketEvent(new Java.Net.DatagramPacket(buff, b.buffer.Limit(), b.socketAddress)), "PacketEvent"); } catch (Exception e) { throw new IOException(e.StackTrace); } }
private void HandleWrite(SelectionKey key) { if (!(outboundQueue.Count == 0)) { if (DEBUG) { Log.Debug("MIDIPort2", "handleWrite " + outboundQueue.Count); } try { DatagramChannel c = (DatagramChannel)key.Channel(); DatagramPacket d = outboundQueue.Dequeue(); if (d != null) { c.Send(ByteBuffer.Wrap(d.GetData()), d.SocketAddress); } } catch (IOException e) { throw new IOException(e.StackTrace); } } }
private void processInput(SelectionKey key, List <SelectionKey> keyIterator) { System.Console.WriteLine("TCP In"); try { if (keyIterator[0] != null) { keyIterator.RemoveAt(0); } ByteBuffer receiveBuffer = ByteBufferPool.acquire(); // Leave space for the header receiveBuffer.Position(HEADER_SIZE); TCB tcb = (TCB)key.Attachment(); lock (tcb) { Packet referencePacket = tcb.referencePacket; SocketChannel inputChannel = (SocketChannel)key.Channel(); int readBytes; try { readBytes = inputChannel.Read(receiveBuffer); } catch (IOException e) { Log.Error(TAG, "Network read error: " + tcb.ipAndPort, e); referencePacket.updateTCPBuffer(receiveBuffer, (byte)Packet.TCPHeader.RST, 0, tcb.myAcknowledgementNum, 0); outputQueue.Offer(receiveBuffer); TCB.CloseTCB(tcb); return; } if (readBytes == -1) { // End of stream, stop waiting until we push more data //key.InterestOps(0); key.InterestOps(); tcb.waitingForNetworkData = false; if (tcb.status != TCBStatus.CLOSE_WAIT) { ByteBufferPool.Release(receiveBuffer); return; } tcb.status = TCBStatus.LAST_ACK; referencePacket.updateTCPBuffer(receiveBuffer, (byte)Packet.TCPHeader.FIN, tcb.mySequenceNum, tcb.myAcknowledgementNum, 0); tcb.mySequenceNum++; // FIN counts as a byte } else { // XXX: We should ideally be splitting segments by MTU/MSS, but this seems to work without referencePacket.updateTCPBuffer(receiveBuffer, (byte)(Packet.TCPHeader.PSH | Packet.TCPHeader.ACK), tcb.mySequenceNum, tcb.myAcknowledgementNum, readBytes); tcb.mySequenceNum += readBytes; // Next sequence number receiveBuffer.Position(HEADER_SIZE + readBytes); } } outputQueue.Offer(receiveBuffer); } catch (Java.Lang.Exception ex) { System.Console.WriteLine(ex.Message); } }