Skip to content

Commit

Permalink
[#79] First draft of CRSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatalinstan committed Sep 14, 2022
1 parent 9d0b2cb commit 71d70f0
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1010"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ADC1A5BF1CA30F49009A4A19"
BuildableName = "Criollo macOS App.app"
BlueprintName = "Criollo macOS App"
ReferencedContainer = "container:Criollo macOS App.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ADC1A5BF1CA30F49009A4A19"
BuildableName = "Criollo macOS App.app"
BlueprintName = "Criollo macOS App"
ReferencedContainer = "container:Criollo macOS App.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ADC1A5BF1CA30F49009A4A19"
BuildableName = "Criollo macOS App.app"
BlueprintName = "Criollo macOS App"
ReferencedContainer = "container:Criollo macOS App.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ADC1A5BF1CA30F49009A4A19"
BuildableName = "Criollo macOS App.app"
BlueprintName = "Criollo macOS App"
ReferencedContainer = "container:Criollo macOS App.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
2 changes: 1 addition & 1 deletion Apps/Criollo macOS App/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@import Criollo;
@import Cocoa;

@interface AppDelegate : NSObject <CRApplicationDelegate>
@interface AppDelegate : NSObject <CRApplicationDelegate, CRSocketDelegate>

@end

16 changes: 16 additions & 0 deletions Apps/Criollo macOS App/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,20 @@ - (void)server:(CRServer *)server didFinishRequest:(CRRequest *)request {
[SystemInfoHelper addRequest];
}

#pragma mark - CRSocketDelegate

- (void)socket:(CRSocket *)sock didAccept:(int)fd addr:(struct sockaddr *)sa len:(socklen_t)len {
#if DEBUG
NSLog(@"%s", __PRETTY_FUNCTION__);
#endif
}

- (void)socket:(nonnull CRSocket *)sock didReadData:(nonnull const void *)buf size:(size_t)len descriptor:(int)fd {
#if DEBUG
NSLog(@"%s", __PRETTY_FUNCTION__);
NSLog(@"%s", buf);
#endif
}


@end
13 changes: 12 additions & 1 deletion Apps/Criollo macOS App/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@

#import "AppDelegate.h"

int main(int argc, const char * argv[]) {


int main(int argc, const char * argv[]) {
@autoreleasepool {
NSError *error;
CRSocket *sock = [[CRSocket alloc] initWithDelegate:[AppDelegate new] delegateQueue:nil];
if ( ![sock listen:nil port:10781 error:&error]) {
NSLog(@"%@", error);
return EXIT_FAILURE;
}

dispatch_main();

return CRApplicationMain(argc, argv, [AppDelegate new]);
}
}
17 changes: 14 additions & 3 deletions Criollo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
4A689EE1222FD4C70074DEA3 /* CRRouterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A689EE0222FD4C70074DEA3 /* CRRouterTests.m */; };
4A689EE2222FD4C70074DEA3 /* CRRouterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A689EE0222FD4C70074DEA3 /* CRRouterTests.m */; };
4A689EE3222FD4C70074DEA3 /* CRRouterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A689EE0222FD4C70074DEA3 /* CRRouterTests.m */; };
4A6EA3BB21CA6BD300BCB435 /* CRSocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 4A6EA3B921CA6BD300BCB435 /* CRSocket.h */; settings = {ATTRIBUTES = (Public, ); }; };
4A6EA3BC21CA6BD300BCB435 /* CRSocket.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A6EA3BA21CA6BD300BCB435 /* CRSocket.m */; };
4A6EA3C021CA6BDF00BCB435 /* CRSocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 4A6EA3B921CA6BD300BCB435 /* CRSocket.h */; settings = {ATTRIBUTES = (Public, ); }; };
4A6EA3C121CA6BDF00BCB435 /* CRSocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 4A6EA3B921CA6BD300BCB435 /* CRSocket.h */; settings = {ATTRIBUTES = (Public, ); }; };
4A84C76F1F97E6FE001CC0A5 /* CRHTTPServerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A84C76E1F97E6FE001CC0A5 /* CRHTTPServerTests.m */; };
4A84C7701F97E6FE001CC0A5 /* CRHTTPServerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A84C76E1F97E6FE001CC0A5 /* CRHTTPServerTests.m */; };
4ABB1DE31F9E6B0A00480EC7 /* Criollo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4ABB1DDA1F9E6B0900480EC7 /* Criollo.framework */; };
Expand Down Expand Up @@ -484,6 +488,8 @@
4A63BAF51F96BD5C0062D930 /* Readme.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = Readme.md; sourceTree = "<group>"; };
4A63BAF61F96BD5C0062D930 /* CRHTTPSHelperTests.key.pem */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = CRHTTPSHelperTests.key.pem; sourceTree = "<group>"; };
4A689EE0222FD4C70074DEA3 /* CRRouterTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CRRouterTests.m; sourceTree = "<group>"; };
4A6EA3B921CA6BD300BCB435 /* CRSocket.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CRSocket.h; sourceTree = "<group>"; };
4A6EA3BA21CA6BD300BCB435 /* CRSocket.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CRSocket.m; sourceTree = "<group>"; };
4A84C76E1F97E6FE001CC0A5 /* CRHTTPServerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CRHTTPServerTests.m; sourceTree = "<group>"; };
4ABB1DDA1F9E6B0900480EC7 /* Criollo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Criollo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4ABB1DE21F9E6B0A00480EC7 /* CriolloTests tvOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "CriolloTests tvOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -948,6 +954,8 @@
AD8D1E491C0CF53A00A3763A /* Extensions */,
ADF84593266E311E00D40D9E /* CocoaAsyncSocket.h */,
ADEDB34F1AEFBD8800A26AF1 /* CRApplication.m */,
4A6EA3B921CA6BD300BCB435 /* CRSocket.h */,
4A6EA3BA21CA6BD300BCB435 /* CRSocket.m */,
AD63F8321BFEA9DD003815DC /* CRServer_Internal.h */,
AD19695D1B626EF90098CA89 /* CRServer.m */,
AD227F881BDC3F6000C23D52 /* CRServerConfiguration.h */,
Expand Down Expand Up @@ -1008,6 +1016,7 @@
4ABB1E251F9E6FD500480EC7 /* CRServer_Internal.h in Headers */,
4ABB1DF31F9E6E2700480EC7 /* CRRoute.h in Headers */,
4ABB1DFE1F9E6EDF00480EC7 /* CRNib.h in Headers */,
4A6EA3C121CA6BDF00BCB435 /* CRSocket.h in Headers */,
4ABB1E001F9E6EE800480EC7 /* CRView.h in Headers */,
4ABB1E2F1F9E700C00480EC7 /* CRMimeTypeHelper.h in Headers */,
4ABB1E321F9E702500480EC7 /* CRRequest_Internal.h in Headers */,
Expand Down Expand Up @@ -1064,6 +1073,7 @@
AD60BA4C1C03990500118EED /* CRViewController.h in Headers */,
AD63F83A1BFEAE96003815DC /* CRMessage_Internal.h in Headers */,
AD001AE41D457BB000DE4AC9 /* CRRouteMatchingResult.h in Headers */,
4A6EA3C021CA6BDF00BCB435 /* CRSocket.h in Headers */,
AD5FFFF81BF0031E00A57FE9 /* CRRoute.h in Headers */,
AD001ADD1D4555E000DE4AC9 /* CRRoute_Internal.h in Headers */,
ADEBD0251C6C9D5F00F91D28 /* CRMimeTypeHelper.h in Headers */,
Expand Down Expand Up @@ -1122,6 +1132,7 @@
ADF84594266E311E00D40D9E /* CocoaAsyncSocket.h in Headers */,
AD63F8391BFEAE96003815DC /* CRMessage_Internal.h in Headers */,
AD001AE01D455BA300DE4AC9 /* CRRouteMatchingResult.h in Headers */,
4A6EA3BB21CA6BD300BCB435 /* CRSocket.h in Headers */,
ADADAB5F1BEEF28E000FBE6B /* CRRoute.h in Headers */,
AD001ADA1D4555A500DE4AC9 /* CRRoute_Internal.h in Headers */,
ADEBD0241C6C9D5F00F91D28 /* CRMimeTypeHelper.h in Headers */,
Expand Down Expand Up @@ -1645,6 +1656,7 @@
ADA37C751BE38E69008D89CA /* CRFCGIRecord.m in Sources */,
ADB693011BE53FD400656BEC /* CRFCGIRequest.m in Sources */,
AD60BA411C0389F800118EED /* CRNib.m in Sources */,
4A6EA3BC21CA6BD300BCB435 /* CRSocket.m in Sources */,
AD19695F1B626EF90098CA89 /* CRServer.m in Sources */,
AD9BE7F51C91B9A400CE52B0 /* CRStaticFileManager.m in Sources */,
ADD2B60D1BFE70AE00E2E632 /* NSHTTPCookie+Criollo.m in Sources */,
Expand Down Expand Up @@ -2100,18 +2112,17 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_NO_COMMON_BLOCKS = YES;
FRAMEWORK_VERSION = A;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
LLVM_LTO = YES;
MTL_ENABLE_DEBUG_INFO = YES;
SDKROOT = macosx;
SKIP_INSTALL = YES;
Expand All @@ -2137,13 +2148,13 @@
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_NO_COMMON_BLOCKS = YES;
FRAMEWORK_VERSION = A;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
LLVM_LTO = YES;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SKIP_INSTALL = NO;
Expand Down
58 changes: 58 additions & 0 deletions Criollo/Public Headers/Criollo/CRSocket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// CRSocket.h
// Criollo macOS
//
// Created by Cătălin Stan on 19/12/2018.
// Copyright © 2018 Cătălin Stan. All rights reserved.
//

#import <Foundation/Foundation.h>


#define CRSocketErrorDomain @"CRSocketErrorDomain"

#define CRUnableToResolveAddress 5001


NS_ASSUME_NONNULL_BEGIN

@class CRSocket;

@protocol CRSocketDelegate <NSObject>

/**
Called when a listening socket has accepted a new connection.
@param sock The listening `CRSocket` object accepting the connection.
@param fd The file descriptor of the multiplexed socket
@param sa The `struct sockaddr` that is filled in with the address of the connecting entity, as known to the communications layer.
@param len Contains the actual length (in bytes) of the address returned.
@see `accept(2)`
*/
- (void)socket:(CRSocket *)sock didAccept:(int)fd addr:(struct sockaddr *)sa len:(socklen_t)len;


/**
Called when data has become available on a multiplexed descriptor of a scoket.
@param sock The listening `CRSocket` object owning the connection.
@param buf A pointer to the buffer conaining the data.
@param len The length (in bytes) of the data read.
@param fd The file descriptor of the multiplexed socket that has read the data.
*/
- (void)socket:(CRSocket *)sock didReadData:(const void *)buf size:(size_t)len descriptor:(int)fd;

@end

@interface CRSocket : NSObject

@property (nonatomic, strong, readonly) id<CRSocketDelegate> delegate;

- (instancetype)initWithDelegate:(id<CRSocketDelegate> _Nullable)delegate delegateQueue:(dispatch_queue_t _Nullable)delegateQueue;

- (BOOL)listen:(NSString * _Nullable)interface port:(NSUInteger)port error:(NSError * __autoreleasing *)error;

@end

NS_ASSUME_NONNULL_END
1 change: 1 addition & 0 deletions Criollo/Public Headers/Criollo/Criollo.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <Criollo/CRTypes.h>

#import <Criollo/CRApplication.h>
#import <Criollo/CRSocket.h>
#import <Criollo/CRRouter.h>
#import <Criollo/CRServer.h>
#import <Criollo/CRConnection.h>
Expand Down
Loading

0 comments on commit 71d70f0

Please sign in to comment.