NetworkLevel2Connection connectiontoserver; // used by client #endregion Fields #region Constructors //bool connectionopen = false; // used by client //static NetworkModel instance = new NetworkModel(); //public static NetworkModel GetInstance(){ return instance; } public NetworkLevel2Controller() { this.networkimplementation = NetworkImplementationFactory.CreateNewInstance(); networkimplementation.ReceivedPacket += new Level1ReceivedPacketHandler( ReceivedPacketHandler ); //networkimplementation.NewConnection += new Level1NewConnectionHandler(networkimplementation_NewConnection); networkimplementation.Disconnection += new Level1DisconnectionHandler( networkimplementation_Disconnection ); }
void network_ReceivedPacket(INetworkImplementation source, ConnectionInfo connectioninfo, byte[] data, int offset, int length) { try { //LogFile.WriteLine("STUN network receivedpacket"); if (length > 2 && data[0] == 1 && data[0] == 1) // bindingresponse { LogFile.WriteLine(this.GetType() + "could be binding reponse"); Message responsemessage = Message.Decode(data, 0, data.Length); MappedAddressAttribute mappedaddress = (MappedAddressAttribute)responsemessage.GetAttribute(net.voxx.stun4cs.Attribute.MAPPED_ADDRESS); IPAddress ipaddress = new IPAddress(mappedaddress.GetAddressBytes()); int port = mappedaddress.GetAddress().GetPort(); Console.WriteLine(ipaddress + " " + port); network.ReceivedPacket -= packethandler; callback(ipaddress, port); } else if (length > 2 && data[0] == 1 && data[0] == 0x11) // bindingerror response { LogFile.WriteLine(this.GetType() + " could be binding error"); } } catch (Exception e) { //LogFile.WriteLine( "STUN networkreceivedpacket " + e); } }
NetworkLevel2Connection connectiontoserver; // used by client //bool connectionopen = false; // used by client //static NetworkModel instance = new NetworkModel(); //public static NetworkModel GetInstance(){ return instance; } public NetworkLevel2Controller() { this.networkimplementation = NetworkImplementationFactory.CreateNewInstance(); networkimplementation.ReceivedPacket += new Level1ReceivedPacketHandler(ReceivedPacketHandler); //networkimplementation.NewConnection += new Level1NewConnectionHandler(networkimplementation_NewConnection); networkimplementation.Disconnection += new Level1DisconnectionHandler(networkimplementation_Disconnection); }
public void Go(int port) { net = NetworkImplementationFactory.CreateNewInstance(); net.ListenAsServer(port); net.ReceivedPacket += new Level1ReceivedPacketHandler(net_ReceivedPacket); while (true) { net.Tick(); Thread.Sleep(50); } }
public void networkimplementation_Disconnection(INetworkImplementation source, OSMP.ConnectionInfo connectioninfo) { if (IsServer && connections.ContainsKey(connectioninfo.Connection)) { if (Disconnection != null) { Disconnection(connections[connectioninfo.Connection], connectioninfo); } connections[connectioninfo.Connection]._Disconnect(); connections.Remove(connectioninfo.Connection); } }
public void Go(string ipaddress, int port) { INetworkImplementation net = NetworkImplementationFactory.CreateNewInstance(); net.ConnectAsClient(ipaddress, port); net.ReceivedPacket += new Level1ReceivedPacketHandler(net_ReceivedPacket); net.Send(Encoding.UTF8.GetBytes("Hi, this is a test")); while (true) { net.Tick(); Thread.Sleep(50); } }
//~STUN() //{ // network.ReceivedPacket -= packethandler; // not sure if necessary? //} public STUN( INetworkImplementation network, GotExternalAddress callback ) { LogFile.WriteLine( "STUN " + network.LocalIPAddress + " " + network.LocalPort + " " + callback.Target + " " + callback.Method ); this.network = network; this.callback = callback; packethandler = new Level1ReceivedPacketHandler( network_ReceivedPacket ); network.ReceivedPacket += packethandler; byte[] bytes = CreateSTUNBindingRequestPacket(); network.Send( GetStunServerEndpoint(), bytes ); }
//~STUN() //{ // network.ReceivedPacket -= packethandler; // not sure if necessary? //} public STUN(INetworkImplementation network, GotExternalAddress callback) { LogFile.WriteLine("STUN " + network.LocalIPAddress + " " + network.LocalPort + " " + callback.Target + " " + callback.Method); this.network = network; this.callback = callback; packethandler = new Level1ReceivedPacketHandler(network_ReceivedPacket); network.ReceivedPacket += packethandler; byte[] bytes = CreateSTUNBindingRequestPacket(); network.Send(GetStunServerEndpoint(), bytes); }
Hashtable unsafepackethandlers = new Hashtable(); // packets which dont have correct sharedsecret, or sharedsecret not yet validated #endregion Fields #region Constructors public NetworkLevel2Connection( NetworkLevel2Controller parent, ConnectionInfo connectioninfo, bool isserver ) { LogFile.WriteLine("NetworkLevel2Connection()"); networkimplementation = parent.networkimplementation; this.connectioninfo = connectioninfo; this.parent = parent; //this.connection = connectioninfo.Connection; this.isserver = isserver; lasttimestamp = DateTime.Now; packetreferencecontroller = new NetPacketReferenceController( this, isserver ); sharedsecretexchange = new NetSharedSecretExchange( this, isserver ); //sharedsecretexchange.Tick(); }
public NetworkLevel2Connection(NetworkLevel2Controller parent, ConnectionInfo connectioninfo, bool isserver) { LogFile.WriteLine("NetworkLevel2Connection()"); networkimplementation = parent.networkimplementation; this.connectioninfo = connectioninfo; this.parent = parent; //this.connection = connectioninfo.Connection; this.isserver = isserver; lasttimestamp = DateTime.Now; packetreferencecontroller = new NetPacketReferenceController(this, isserver); sharedsecretexchange = new NetSharedSecretExchange(this, isserver); //sharedsecretexchange.Tick(); }
public void ReceivedPacketHandler(INetworkImplementation source, ConnectionInfo connectioninfo, byte[] data, int offset, int length) { object connection = connectioninfo.Connection; //LogFile.WriteLine("Connection: " + connection); if (IsServer) { if (!connections.ContainsKey(connection)) { NetworkLevel2Connection networklevel2connection = new NetworkLevel2Connection(this, connectioninfo, IsServer); connections.Add(connection, networklevel2connection); } connections[connection].ReceivedPacketHandler(connectioninfo, data, offset, length); } else { connectiontoserver.ReceivedPacketHandler(connectioninfo, data, offset, length); } }
void network_ReceivedPacket( INetworkImplementation source, ConnectionInfo connectioninfo, byte[] data, int offset, int length ) { try { //LogFile.WriteLine("STUN network receivedpacket"); if (length > 2 && data[0] == 1 && data[0] == 1) // bindingresponse { LogFile.WriteLine( this.GetType() + "could be binding reponse"); Message responsemessage = Message.Decode(data, 0, data.Length); MappedAddressAttribute mappedaddress = (MappedAddressAttribute)responsemessage.GetAttribute( net.voxx.stun4cs.Attribute.MAPPED_ADDRESS ); IPAddress ipaddress = new IPAddress( mappedaddress.GetAddressBytes() ); int port = mappedaddress.GetAddress().GetPort(); Console.WriteLine( ipaddress + " " + port ); network.ReceivedPacket -= packethandler; callback( ipaddress, port ); } else if (length > 2 && data[0] == 1 && data[0] == 0x11) // bindingerror response { LogFile.WriteLine( this.GetType() + " could be binding error"); } } catch( Exception e ) { //LogFile.WriteLine( "STUN networkreceivedpacket " + e); } }
void net_ReceivedPacket(INetworkImplementation source, ConnectionInfo connectioninfo, byte[] data, int offset, int length) { LogFile.WriteLine("Received packet: " + Encoding.UTF8.GetString(data)); }
void net_ReceivedPacket( INetworkImplementation source, ConnectionInfo connectioninfo, byte[] data, int offset, int length ) { LogFile.WriteLine("Received packet: " + Encoding.UTF8.GetString(data)); net.Send(connectioninfo.Connection, Encoding.UTF8.GetBytes( "Hello from server!" ) ); }
void net_ReceivedPacket( INetworkImplementation source, ConnectionInfo connectioninfo, byte[] data, int offset, int length ) { LogFile.WriteLine( "Received packet: " + Encoding.UTF8.GetString( data ) ); }
public void networkimplementation_Disconnection( INetworkImplementation source, OSMP.ConnectionInfo connectioninfo ) { if( IsServer && connections.ContainsKey( connectioninfo.Connection ) ) { if (Disconnection != null) { Disconnection(connections[connectioninfo.Connection], connectioninfo); } connections[connectioninfo.Connection]._Disconnect(); connections.Remove(connectioninfo.Connection); } }
public void ReceivedPacketHandler( INetworkImplementation source, ConnectionInfo connectioninfo, byte[] data, int offset, int length ) { object connection = connectioninfo.Connection; //LogFile.WriteLine("Connection: " + connection); if (IsServer) { if (!connections.ContainsKey(connection)) { NetworkLevel2Connection networklevel2connection = new NetworkLevel2Connection(this, connectioninfo, IsServer); connections.Add(connection, networklevel2connection); } connections[connection].ReceivedPacketHandler(connectioninfo, data, offset, length); } else { connectiontoserver.ReceivedPacketHandler(connectioninfo, data, offset, length); } }
void net_ReceivedPacket(INetworkImplementation source, ConnectionInfo connectioninfo, byte[] data, int offset, int length) { LogFile.WriteLine("Received packet: " + Encoding.UTF8.GetString(data)); net.Send(connectioninfo.Connection, Encoding.UTF8.GetBytes("Hello from server!")); }