private void acceptConnection() { /*Pre: the listenOnPort method created a thread.*/ /*Post:the method will continually look for new connections*/ listening = true; ClientControl.addReferenceToMainController(this); ClientControl.addReferenceToView(this.view); while (listening == true) { //blocking - the socket starts listening tcpServer.Listen(int.MaxValue); try { //wait for connection attempted, store socket info inside tryToConnect Socket tryToConnect = tcpServer.Accept(); //if number of clients does not exceed MAXCLIENTS int numClients = clientList.Count; if (numClients < MAXCLIENTS && tryToConnect != null) { //create new client ClientControl client = new ClientControl(tryToConnect, (r.Next(10000) + r.Next(10))); //and add client object to linked list clientList.AddLast(client); //tell main view that client was added view.Invoke(view.writeToStatusTextBox, "Client: "+ client.clientThread.Name +" added."); } else { //otherwise tell main view client was not added view.Invoke(view.writeToStatusTextBox, "Client could not be added."); //create new network stream NetworkStream ntwkstrm = new NetworkStream(tryToConnect); ASCIIEncoding encode = new ASCIIEncoding(); byte[] sendBytes = new byte[4]; //read anything already on stream ntwkstrm.Read(sendBytes, 0, 4); //send last client BUSY signal sendBytes = encode.GetBytes("BUSY"); ntwkstrm.Write(sendBytes, 0, sendBytes.Length); //close network stream ntwkstrm.Flush(); ntwkstrm.Close(); } } catch (SocketException se) { //if socket fails close it if (tcpServer != null) tcpServer.Close(); //and abort thread if (listeningOnPort != null) listeningOnPort.Abort(); } catch (ThreadAbortException tae) { //if thread is attempted to abort check if socket is closed if not close it. if (tcpServer != null) tcpServer.Close(); //then remove all clients for (int i = clientList.Count; i>0; i--) clientList.RemoveFirst(); } catch (Exception e) { } } }
public void makeBid(string xml, ClientControl cle) { try { //client attempts to bid on item XmlDocument doc = new XmlDocument(); StringReader red = new StringReader(xml); doc.Load(red); XmlElement root = doc.DocumentElement; //read item id and bid ammount from xml string string id = root.SelectSingleNode("itemID").InnerText; string bid = root.SelectSingleNode("amount").InnerText; //attempt to get auction item from id Model.AuctionItem temp = this.listOItems.getItemByID(Int32.Parse(id)); //creates new bid Model.Bid newBid = new AuctionServer.Model.Bid(Int32.Parse(cle.clientThread.Name), temp, float.Parse(bid)); //attempt to bid on item if (temp.setCurrentHighBid(newBid)) view.Invoke(view.writeToStatusTextBox, newBid.ToString()); else view.Invoke(view.writeToStatusTextBox, "Client: " + cle.clientThread.Name + " was UNSUCCESSFUL."); } catch (FormatException fe) { view.Invoke(view.writeToStatusTextBox, "FormatException in ServerControl.makeBid(string, ClientControl) with: " +fe.Message); } catch (/*SomeKindOf*/Exception e) { view.Invoke(view.writeToStatusTextBox, "Exception in ServerControl.makeBid(string, ClientControl) with: " + e.Message); } }
public void removeClient(ClientControl removeThisClient) { /*Pre: a client object to be removed is supplied*/ /*Post:the client object has been removed from the linked list making room for other clients*/ try { //if client object is in the list if (clientList.Contains(removeThisClient)) { //remove the object from the list clientList.Remove(removeThisClient); view.Invoke(view.writeToStatusTextBox, "Client: "+ removeThisClient.clientThread.Name +" removed."); } } catch (ArgumentException ae) { //if a bad argument was supplied tell the main view what caused it if (view != null) view.Invoke(view.writeToStatusTextBox, "Argument exception in RemoveClient StreamingServer: " + ae.Message); } }
public byte[] getAuctionItemsAsBytes(ClientControl client) { /*Returns auction item at position i as bytes where every *item field is seperated by a two byte int16 containing *the length of the next field. */ try { //creates an XML file containg all auction items to be sent to specific client LinkedListNode<Model.AuctionItem> temp = this.listOItems.getItemFirstNode(); Model.AuctionItem work; //add root node tag string outString = "<Auction>"; for (int i = 0; i < this.listOItems.getNumberOfItems(); i++) { work = temp.Value; //add item node tag outString += "<item>"; //get AuctionItem XML string outString += work.makeXMLNode(); //open status tag outString += "<status>"; //determine status of auction item for specific client if (work.getCurrentHighBid().getClient().ToString() == "-1") { if (work.getClosingTime() < DateTime.Now) { outString += "This auction is over. No one won this item."; } else { outString += "This auction is not over. No one has bid yet."; } } else if (work.getCurrentHighBid().getClient().ToString() == client.clientThread.Name) { if (work.getClosingTime() < DateTime.Now) { outString += "This auction is over. You won this item."; } else { outString += "This auction is not over. You are the highest bidder."; } } else { if (work.getClosingTime() < DateTime.Now) { outString += "This auction is over. You lost this item."; } else { outString += "This auction is not over. You are not the highest bidder."; } } //close status tag and item node outString += "</status>"; outString += "</item>"; //next auction item temp = temp.Next; } //close Auction root node outString += "</Auction>"; //view.Invoke(view.writeToStatusTextBox, "Sending: "+outString); //return byte[] of XML string byte[] packet = encode.GetBytes(outString); return packet; } catch (NullReferenceException nre) { //if null reference exception occured return byte[] with error string. string error = nre.ToString(); byte[] errorPacket = new byte[error.Length]; encode.GetBytes(error.ToCharArray(), 0, error.Length, errorPacket, 0); return errorPacket; } catch (Exception e) { //if exception occured return byte[] with error string string error = e.ToString(); byte[] errorPacket = new byte[error.Length]; encode.GetBytes(error.ToCharArray(), 0, error.Length, errorPacket, 0); return errorPacket; } }