public static int onConnected(IntPtr handle, ref FSP_SocketParameter context) { Console.WriteLine("Connected, handle = {0:X}", handle); if (handle == (IntPtr)0) { Console.WriteLine("Connection failed."); return(-1); } if (context.len <= 0) { goto l_nokey; } // well, how to marshal a complicate, mixed string and byte array? byte[] original = new byte[1024]; Marshal.Copy(context.welcome, original, 0, context.len); // we knew there must be a welcome message. int mLen = Array.IndexOf <byte>(original, 0); if (mLen++ <= 0) { goto l_nokey; } // Now mLen is the number of bytes occupied by the message, including the termination '\0' Console.WriteLine("--- Welcome message from remote peer --- "); Console.WriteLine(">>>"); Console.WriteLine(Encoding.UTF8.GetString(original, 0, mLen)); //TODO! .GetEncoding("GBK")? ASCII!? Console.WriteLine("<<<"); if (mLen >= context.len) { goto l_nokey; } Array.Copy(original, mLen, bufPeersKey, 0, FSPAPI.CRYPTO_NACL_KEYBYTES); FSPAPI.InstallAuthenticKey(handle, bufSharedKey, FSPAPI.CRYPTO_NACL_KEYBYTES, Int32.MaxValue, FlagEndOfMessage.NOT_END_ANYWAY); FSPAPI.FSPControl(handle , FSP_ControlCode.FSP_SET_CALLBACK_ON_ERROR , Marshal.GetFunctionPointerForDelegate(new NotifyOrReturn(onError2))); FSPAPI.WriteTo(handle, bufPublicKey, FSPAPI.CRYPTO_NACL_KEYBYTES , FlagEndOfMessage.END_OF_MESSAGE , new NotifyOrReturn(onPublicKeySent)); return(0); l_nokey: Console.Write("To read the filename directly...\t"); if (FSPAPI.ReadFrom(handle, fileName, fileName.Length, new NotifyOrReturn(onReceiveFileNameReturn)) < 0) { finished = true; return(-1); // to dispose/reject the connection } return(0); }
static void Main(string[] args) { FSP_SocketParameter context = new FSP_SocketParameter(); context.onError = new NotifyOrReturn(onNotified); context.afterAccept = new CallbackConnected(onConnected); FSPAPI.Connect2(REMOTE_APPLAYER_NAME, ref context); Console.Write("Press return or any other key to exit..."); while (!finished && !Console.KeyAvailable) { System.Threading.Thread.Sleep(0); } }
public static extern IntPtr Connect2(string peerURI, ref FSP_SocketParameter context);
public static extern IntPtr ConnectMU(IntPtr handle, ref FSP_SocketParameter context);
public static extern IntPtr ListenAt(ref FSP_IN6_ADDR bindPoint, ref FSP_SocketParameter context);