/** * Accept a connection on a socket * * <p>Needs to trigger sending an ACK * * @return TCPSock The first established connection on the request queue remove from welcomeQueue * adds recvSocket to socketSpace sends Ack */ public TCPSock accept() { aa(this, this.sockType == SocketType.WELCOME, "accept() called by non-welcome socket"); aa(this, this.state == State.LISTEN, "accept() called when not listening"); if (welcomeQueue.isEmpty()) return null; p(this, 3, "accept()"); TCPSock recvSock = welcomeQueue.remove(0); recvSock.state = State.ESTABLISHED; p(recvSock, 4, "established"); tcpMan.add(recvSock.tsid, recvSock); tcpMan.sendACK(recvSock.tsid); return recvSock; }
/** * Initiate connection to a remote socket * * @param destAddr int Destination node address * @param destPort int Destination port * @return int 0 on success, -1 otherwise */ public int connect(int destAddr, int destPort) { // if not starting from a closed state, connect should fail. if (!isClosed() || !isSender()) { pe(this, " connect() called on inappropriated"); return -1; } p(this, 3, "Connect() to: " + destAddr + ":" + destPort); this.tsid.remoteAddr = destAddr; this.tsid.remotePort = destPort; tcpMan.add(this.tsid, this); tcpMan.sendSYN(this.tsid); this.state = State.SYN_SENT; return 0; }