}//Start Button public void AddToList(object temp) { if (temp is string) { string user = temp as string; UserBox.Items.Add(user); } if (temp is CPacket.LogOutUser) { CPacket.LogOutUser user = new CPacket.LogOutUser(); user = temp as CPacket.LogOutUser; UserBox.Items.Remove(user.UsersDisconnected); } if (temp is CPacket.AcceptRequestGame) { CPacket.AcceptRequestGame list = new CPacket.AcceptRequestGame(); list = temp as CPacket.AcceptRequestGame; CPacket.UserInSession listInSession = new CPacket.UserInSession(); listInSession.user1 = list.requested_ID; listInSession.user2 = list.requester_ID; listInSession.hostIp = list.requester_IP; UserBox.Items.Remove(list.requested_ID); UserBox.Items.Remove(list.requester_ID); //Session[ListNumber].hostIp = list.requester_IP; //Session[ListNumber].HostPlayer = list.requester_ID; UserInSession.Items.Add(list.requester_ID + "-----> " + list.requested_ID); for (int x = 0; x < NextLocation; x++) { //if (AllSockets[x].user_id != list.requested_ID && AllSockets[x].user_id != list.requester_ID) //{ formatter.Serialize(AllSockets[x].ConnStream, listInSession); //} } //ListNumber++; } }
public void ListenProcedure() { object temp; //object UserList; try { while (true) { temp = formatter.Deserialize(Connectstream); PictureBoxState.Image = ClientList.Images[1];//change icons if (temp is CPacket.LoginPacket) { if (Connected.InvokeRequired) { BeginInvoke(new kdelegate(AddToList), ((CPacket.LoginPacket)temp).user_id, 1); } } //string user = ((CPacket.LoginPacket)temp).user_id; if (temp is CPacket.LogOut) { MessageBox.Show("Server Full"); PictureBoxState.Image = ClientList.Images[0]; Connectstream.Close(); } if (temp is CPacket.LogOutUser) { if (Connected.InvokeRequired) { BeginInvoke(new kdelegate(AddToList), ((CPacket.LogOutUser)temp).UsersDisconnected, 4); } } if (temp is CPacket.UserInSession) { CPacket.UserInSession list = new CPacket.UserInSession(); list = temp as CPacket.UserInSession; if (Connected.InvokeRequired) { BeginInvoke(new kdelegate1(RemovefromToList), list); } } if (temp is CPacket.RequestGame) { CPacket.RequestGame whoasked; whoasked = temp as CPacket.RequestGame; RequestForm requestform = new RequestForm(); if (requestform.ShowDialog() == DialogResult.Yes) { CPacket.AcceptRequestGame RequestAccept = new CPacket.AcceptRequestGame(); RequestAccept.Response = true; RequestAccept.requested_ID = login.user_id; RequestAccept.requester_ID = whoasked.requestedusers; RequestAccept.requester_IP = whoasked.requestedusers_ip; formatter.Serialize(Connectstream, RequestAccept); } else { CPacket.AcceptRequestGame RequestAccept = new CPacket.AcceptRequestGame(); RequestAccept.Response = false; formatter.Serialize(Connectstream, RequestAccept); } } if (temp is CPacket.StartGame) { hosting = new CPacket.StartGame(); hosting = temp as CPacket.StartGame; if (hosting.Which == true) { //HostMode Int32 port = 3006; which = true; //NewPack = new CPacket.GameData(); HostListener = new TcpListener(hosting.Host_IP, port); HostListener.Start(); try { Hostconnection = HostListener.AcceptSocket(); P2PConnectStream = new NetworkStream(Hostconnection);//temporary Networkstream pictureBoxRequestState.Image = ClientList.Images[1]; P2P = new Thread(new ThreadStart(PeerToPeerConnectionHost)); P2P.Start(); //AllThreads[NextLocation] = new Thread(new ParameterizedThreadStart(AreYouTalkingtome)); //AllThreads[NextLocation].Start(AllSockets[NextLocation]); } catch (SocketException e) { MessageBox.Show("No Communication"); } } else { //ClientMode which = false; //MessageBox.Show("Made it to peer to peer start game"); TcpClient Peer2Peer; Peer2Peer = new TcpClient(); try { Peer2Peer.Connect(hosting.Host_IP, 3006); P2PConnectStream = Peer2Peer.GetStream(); if (P2P == null || !P2P.IsAlive) { pictureBoxRequestState.Image = ClientList.Images[1]; P2P = new Thread(new ThreadStart(PeerToPeerConnectionClient)); P2P.Start(); } } catch (SocketException error) { } } } } } catch (Exception e) { MessageBox.Show(" Lost Connection Client"); //PictureBoxState.Image = ClientList.Images[0]; } }
}//Wait for connection public void AreYouTalkingtome(object obj) {//Are You Talking to me LoginData who = null; object temp; CPacket.RequestGame TempPacket; if (obj is LoginData) { who = (LoginData)obj; } //.Items[i].BackColor = Color.Green; try { while (true) { temp = formatter.Deserialize(who.ConnStream); //Waiting for one of the client to takl if (temp is CPacket.RequestGame) //What i'm Getting { try { TempPacket = temp as CPacket.RequestGame; TempPacket.requestedusers = ((CPacket.RequestGame)temp).requestedusers; //MessageBox.Show("Requeste Received"); for (int x = 0; x <= NextLocation; x++) { if (AllSockets[x] != null && TempPacket.requestedusers == AllSockets[x].user_id) { //MessageBox.Show("User Found"); CPacket.RequestGame NewTempPacket = new CPacket.RequestGame(); NewTempPacket.requestedusers = who.user_id; NewTempPacket.requestedusers_ip = who.user_ip; formatter.Serialize(AllSockets[x].ConnStream, NewTempPacket); } } } catch (System.IO.IOException e) { //MessageBox.Show() } //if() //System.IO.IOException } if (temp is CPacket.AcceptRequestGame) { CPacket.AcceptRequestGame ServerRequestAccepted = new CPacket.AcceptRequestGame(); ServerRequestAccepted = temp as CPacket.AcceptRequestGame; if (ServerRequestAccepted.Response == true) { //MessageBox.Show(ServerRequestAccepted.requester_ID + " is asking " + ServerRequestAccepted.requested_ID); CPacket.StartGame StartGame = new CPacket.StartGame(); StartGame.Client_ID = ServerRequestAccepted.requested_ID; StartGame.Host_ID = ServerRequestAccepted.requester_ID; StartGame.Client_IP = ServerRequestAccepted.requested_IP; StartGame.Host_IP = ServerRequestAccepted.requester_IP; // StartGame.session = who.position; //MessageBox.Show(Convert.ToString(StartGame.session)); //Send to Host for (int x = 0; x < NextLocation; x++) { if (AllSockets[x] != null && StartGame.Host_ID == AllSockets[x].user_id) { StartGame.Which = true; //MessageBox.Show(StartGame.Host_ID + " is the Host "); formatter.Serialize(AllSockets[x].ConnStream, StartGame); } } //Send To Client for (int x = 0; x < NextLocation; x++) { if (AllSockets[x] != null && StartGame.Client_ID == AllSockets[x].user_id) { StartGame.Which = false; //MessageBox.Show(StartGame.Client_ID + " is the client "); formatter.Serialize(AllSockets[x].ConnStream, StartGame); } } if (UserBox.InvokeRequired) { BeginInvoke(new kdelegate(AddToList), ServerRequestAccepted); } } if (ServerRequestAccepted.Response == false) { MessageBox.Show(" Don't Create a peer to peer "); } } if (temp is CPacket.LogOut) { try { CPacket.LogOutUser UsersLogedOutList = new CPacket.LogOutUser(); NextLocation--; //UsersLogedOutList = temp as CPacket.LogOutUser; UsersLogedOutList.UsersDisconnected = who.user_id; for (int x = 0; x < NextLocation; x++) { if (AllSockets[x] != null) { formatter.Serialize(AllSockets[x].ConnStream, UsersLogedOutList); } //System.IO.IOException } if (UserBox.InvokeRequired) { BeginInvoke(new kdelegate(AddToList), UsersLogedOutList); } AllSockets[who.position].ConnStream.Close(); AllSockets[who.position].ConnStream = null; AllSockets[who.position] = null; openLocation.Push(who.position); } catch (System.IO.IOException e) { } if (temp is CPacket.WatchingRequest) { CPacket.WatchingRequest SessionRequested = new CPacket.WatchingRequest(); SessionRequested = temp as CPacket.WatchingRequest; } } break; } //WFCThread.Start(); } catch (System.Runtime.Serialization.SerializationException e) { MessageBox.Show("Client is OFF"); } }//Are You Talking to ME