public void Disconnect() { if (!connected) { throw new Exception("You are not connected already"); } connected = false; ip = ""; port = 0; room = null; me = null; try { TCPMessageHandler.CloseConnection(server_client); } catch { } try { listenerThread.Abort(); listenerThread = null; } catch { } }
public static bool Host(int Port, SharpRoom room, out HostResult result) { if (IsConnected) { result = HostResult.AlreadyHosted; return(false); } return(_ishost = isconnected = server.Host(out result, Port, room)); }
public void Disconnect() { if (!hosted) { throw new Exception("You are not hosted already"); } hosted = false; foreach (var item in this.room.ConnectedClients) { try { TCPMessageHandler.CloseConnection(item.Connection); } catch { } try { item.ListeningThread.Abort(); } catch { } item.ListeningThread = null; } port = 0; serverclient = null; room = null; try { listener.Stop(); } catch { } try { listener = null; } catch { } try { S_StoppedHosted(); } catch { } try { listenerThread.Abort(); listenerThread = null; } catch { } }
public bool Host(out HostResult result, int Port, SharpRoom room) { if (hosted) { result = HostResult.AlreadyHosted; return(false); } try { this.port = Port; this.room = room; IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, this.port); listener = new TcpListener(endPoint); listener.Start(); serverclient = new SharpClient("ServerClient"); room.AssignClient(serverclient); listenerThread = new Thread(ListenClients); listenerThread.Start(); hosted = true; S_OnHosted(); result = HostResult.Succes; return(true); } catch (Exception e) { try { listener.Stop(); } catch { } try { listenerThread.Abort(); } catch { } listenerThread = null; listener = null; hosted = false; result = HostResult.UnhandledException; return(false); } }
void C_OnConnected(SharpRoom room) { //Console.WriteLine("Connected To Server : Room name => " + room.RoomName); SharpNetworking.Instance._OnNetworkEvent(SharpNetworkEvent.Connected); }
public bool Connect(string IP, int Port, string Password, SharpClient Self, out ConnectResults result) { if (Password == null) { Password = ""; } if (this.connected) { result = ConnectResults.AlreadyConnected; return(false); } try { server_client = new TcpClient(); IPAddress address = null; #region IP Parse try { address = IPAddress.Parse(IP); } catch { result = ConnectResults.InvalidIpAddress; return(false); } #endregion IPEndPoint remoteAddress = new IPEndPoint(address, Port); #region Physical Connetion try { server_client.Connect(remoteAddress); } catch { result = ConnectResults.UnhandledException; return(false); } #endregion #region ID Self SharpSerializer hailMessage = SharpSerializer.Create(); hailMessage.Write((byte)IncomingConnectionRequests.ConnectionApprove); Self.Write(ref hailMessage); hailMessage.Write(Password); TCPMessageHandler.Write(server_client, hailMessage.DataAndPost()); #endregion #region Wait For Response byte[] responseRaw = TCPMessageHandler.Read(server_client); SharpSerializer response = SharpSerializer.Create(responseRaw); #endregion IncomingConnectionResponse message = (IncomingConnectionResponse)response.ReadByte(); if (message == IncomingConnectionResponse.WrongPassword) { TCPMessageHandler.CloseConnection(server_client); result = ConnectResults.InvalidPassword; return(false); } if (message == IncomingConnectionResponse.ServerIsFull) { TCPMessageHandler.CloseConnection(server_client); result = ConnectResults.ServerIsFull; return(false); } if (message == IncomingConnectionResponse.NameIsExist) { TCPMessageHandler.CloseConnection(server_client); result = ConnectResults.NameIsExist; return(false); } short myID = response.ReadInt16(); Self.AssingNetID(myID); this.room = new SharpRoom(ref response); this.listenerThread = new Thread(ListenServer); this.listenerThread.Start(); connected = true; me = Self; room.AssignClient(me); room.Server.Connection = server_client; room.Server.ListeningThread = listenerThread; C_OnConnected(room); result = ConnectResults.Succes; return(true); } catch (Exception e) { try { TCPMessageHandler.CloseConnection(server_client); } catch { } server_client = null; connected = false; try { listenerThread.Abort(); } catch { } listenerThread = null; me = null; room = null; result = ConnectResults.UnhandledException; return(false); } }