private void start_listening() { IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 11111); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsock.Bind(localEndPoint); newsock.Listen(10); Console.WriteLine("participant started listening.."); while (true) { Socket client = newsock.Accept(); byte[] remotehdr = new byte[128]; client.Receive(remotehdr); RFSCommHeader hobj = new RFSCommHeader(); hobj.bytestream_to_headerobj(remotehdr, 0); curr_drive_id = hobj.driveid; long seqn = hobj.seq_num; Console.WriteLine("recived new connection attempt"); if (hobj.optype == RCS_OP.CONNECT) { bool alreadymounted = false; long drivesize = 0; bool drivepresent = check_if_driveid_exists(hobj.driveid, ref alreadymounted, ref drivesize); if (drivepresent == true && alreadymounted == false) { byte[] replyhdr = new byte[128]; Prepare_Header2(replyhdr, RCS_OP.CONNECT_ACK_OKAY, 1, drivesize, 0, seqn); client.Send(replyhdr); Lun_Item ci = create_new_conn_entry(hobj.driveid, client); Thread t = new Thread(start_servicing); t.Start(ci); Console.WriteLine("sent new connection okay"); } else { byte[] replyhdr = new byte[128]; Prepare_Header2(replyhdr, RCS_OP.CONNECT_ACK_FAIL, 1, 0, 0, seqn); client.Send(replyhdr); client.Close(); Console.WriteLine("sent new connection failed"); } } } }
private void Prepare_Header2(byte[] buf, RCS_OP op, int did, long offset, int size, long seqn) { for (int i = 0; i < buf.Length; i++) { buf[i] = 0; } switch (op) { case RCS_OP.NO_OP: case RCS_OP.CONNECT_ACK_FAIL: case RCS_OP.CONNECT_ACK_OKAY: case RCS_OP.READ_REPLY: case RCS_OP.WRITE_ACK: RFSCommHeader hobj = new RFSCommHeader(op, did, offset, size); hobj.seq_num = seqn; hobj.headerobj_to_bytestream(buf, 0); break; //assert otherwize } }
/* * Accept requests for any drive ID serially, and reply back to them in the * tcp port. This runs as a new thread. */ private void start_servicing(object parameter1) { Lun_Item ci = (Lun_Item)parameter1; Socket client = ci.client; byte[] remote_hdr = new byte[128]; byte[] my_hdr = new byte[128]; while (true) { RFSCommHeader hobj = new RFSCommHeader(); long seqn = 0; try { client.Receive(remote_hdr); hobj.bytestream_to_headerobj(remote_hdr, 0); seqn = hobj.seq_num; } catch (Exception e) { Console.WriteLine("Encountered error : " + e.Message); return; } switch (hobj.optype) { case RCS_OP.NO_OP: break; case RCS_OP.DISCONNECT: Console.WriteLine("Client has disconnected!"); mark_end_connection(hobj.driveid); client.Close(); return; case RCS_OP.READ: Prepare_Header2(my_hdr, RCS_OP.READ_REPLY, hobj.driveid, hobj.driveoffset, hobj.datasize, seqn); client.Send(my_hdr); uint sxsize = 0; byte[] tmpbuf = new byte[hobj.datasize]; string fname = hobj.driveid.ToString(); REDDY.ptrIFSDMux.ReadFile(1, fname, tmpbuf, ref sxsize, hobj.driveoffset, null); int send_ds = 0; while (send_ds < hobj.datasize) { int sx = client.Send(tmpbuf, send_ds, hobj.datasize - send_ds, SocketFlags.None); send_ds += sx; } break; case RCS_OP.WRITE: Prepare_Header2(my_hdr, RCS_OP.WRITE_ACK, hobj.driveid, hobj.driveoffset, hobj.datasize, seqn); int recv_ds = 0; byte[] tmpbuf2 = new byte[hobj.datasize]; while (recv_ds < hobj.datasize) { int sx = client.Receive(tmpbuf2, recv_ds, hobj.datasize - recv_ds, SocketFlags.None); recv_ds += sx; } uint sxsize2 = 0; string fname2 = hobj.driveid.ToString(); REDDY.ptrIFSDMux.WriteFile(1, fname2, tmpbuf2, ref sxsize2, hobj.driveoffset, null); client.Send(my_hdr); break; //assert otherwise } } }